From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id B1A06C3DA7F for ; Sun, 4 Aug 2024 06:31:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D05886B007B; Sun, 4 Aug 2024 02:31:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CB5B16B0082; Sun, 4 Aug 2024 02:31:19 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B55806B0085; Sun, 4 Aug 2024 02:31:19 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 98A636B007B for ; Sun, 4 Aug 2024 02:31:19 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 2321C160AAC for ; Sun, 4 Aug 2024 06:31:19 +0000 (UTC) X-FDA: 82413591078.16.5FA2CAB Received: from mail-qv1-f50.google.com (mail-qv1-f50.google.com [209.85.219.50]) by imf24.hostedemail.com (Postfix) with ESMTP id E72F6180009 for ; Sun, 4 Aug 2024 06:31:16 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="Kp9/Tyzl"; spf=pass (imf24.hostedemail.com: domain of boqun.feng@gmail.com designates 209.85.219.50 as permitted sender) smtp.mailfrom=boqun.feng@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1722753047; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=/NZxw82SfdAXEz+K8bi67990DiXrbNqIt6CzOagLaek=; b=Fj6B5eTG+bQ11NQdN70cVYEL80GDhO7v7Q1Vyg0nllwBa7EIlNMgvJdco9zgN+4Zi8UFeh 8YTLgLYCfq26T5I8W8rnQZzFjKn2c08XUvDy+FGeTHui1X3s9Qo/EIPkPIcg5MjWzL22O/ lph4HdyaEAY17UFoi9Y5Ue0EFL+htn4= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="Kp9/Tyzl"; spf=pass (imf24.hostedemail.com: domain of boqun.feng@gmail.com designates 209.85.219.50 as permitted sender) smtp.mailfrom=boqun.feng@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722753047; a=rsa-sha256; cv=none; b=UjKN0tcetkasWtm50cT4DkkWoIInYetB9UrwT1cgFkGTC9gDH3bc76sN9Xrn6mI+ylBQVe 8+SEFJfl2i7hjs7wt/aKSUTX6ihi2/bIyDlBmdsJGkbl7ecpqWfwHfCqvK+h0A9IN63tFe tvR7ODBmh6Fvn8xqYmUsQ9cohamapVg= Received: by mail-qv1-f50.google.com with SMTP id 6a1803df08f44-6b97097f7fdso63251816d6.0 for ; Sat, 03 Aug 2024 23:31:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722753076; x=1723357876; darn=kvack.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:feedback-id:from:to:cc:subject:date :message-id:reply-to; bh=/NZxw82SfdAXEz+K8bi67990DiXrbNqIt6CzOagLaek=; b=Kp9/TyzlFAMS5ytgzewsjYXnB/o2CrqJxStJZxnZIpJYGPAv5mEimvybpB8bvw5Y9b kiQ2Sqo3LTeRzwTw7y58bIc06Zrw3kOPhwxnpnkj43zSSXbkMARNYJEJMlMkev4nkneP TEhEzX0bE+x7ssjU1J9CvINhS+7wFRHiZew3DaNPe7+ynrfcA7/195nLzf5v4yfSwWch JWk6mcgWWoJ5gA1PUmcZJPiPi7Crz4Kx82HXIMaSF6KF9r1kZdvPA5UC8jOQEdsJHRyp az+IeOhKSGaKUWHCDImm4EuTnEpUs0kP1lc9W2IvMQbkOATy2tryizkTVHh8lrJwDAjv SZaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722753076; x=1723357876; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:feedback-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/NZxw82SfdAXEz+K8bi67990DiXrbNqIt6CzOagLaek=; b=PVQ1jxPxw0gvRim9Nx5kLM5J72BLqycdc2URHrwL7BGp0L5faLnPfmsfCDIOrfKZNa LEQCbMJPX6CSI/sPQP2edqLs721S2L6XpMpR54wiSiEz3vvIUyJInNx8tk/GwEEapYV+ 0KnsraoWKONXgi8w1SGlQ/+MnAyClKkpuhwfU3RcrgDDMLWlpoBIke4wSBE2VAaGz5jj wbDieXYKsigFOVNqR8/tAMpCDZywa2Of5aICt72SLItfjZYDS0jpMINBRmB6LnjWz0P2 T0TXBLIETmVQdUjWi4NgVqJmJgfeu3ihA2+dQ4FbxOPErvN7ix1CkquYl1fEym9OInYc kbaQ== X-Forwarded-Encrypted: i=1; AJvYcCValD049hs7ynGN83mdD2ngi5S9ra+r04v7LH18oWCATUC7Fsynjl8AnD/hdnMK3leTUzTqKK8CgSrFP7LMsS1xbbU= X-Gm-Message-State: AOJu0YzJ69i63Pu41u0CjN0UWVFS+eqATkJNjNP7qm493bmf7EXQK4as rkifbm3skANhDrm/RbTh0X5874/Eo/XdC+k1DbQzMMdJWwBdOTOt X-Google-Smtp-Source: AGHT+IHBTO+D88JzfW6pu9QPm3Lvx4nQGCDMnbUFWj/ag9pX33msBTQ3hbPO/V+yYk0ZXWdu3W1w2g== X-Received: by 2002:a05:6214:3f8b:b0:6b5:40d:c2d9 with SMTP id 6a1803df08f44-6bb9836ed10mr115535966d6.19.1722753075899; Sat, 03 Aug 2024 23:31:15 -0700 (PDT) Received: from fauth2-smtp.messagingengine.com (fauth2-smtp.messagingengine.com. [103.168.172.201]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6bb9c8631d0sm22519876d6.106.2024.08.03.23.31.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 03 Aug 2024 23:31:15 -0700 (PDT) Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailfauth.nyi.internal (Postfix) with ESMTP id BDEDC120006E; Sun, 4 Aug 2024 02:22:17 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute3.internal (MEProxy); Sun, 04 Aug 2024 02:22:17 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrkeefgddutdelucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvdenucfhrhhomhepuehoqhhu nhcuhfgvnhhguceosghoqhhunhdrfhgvnhhgsehgmhgrihhlrdgtohhmqeenucggtffrrg htthgvrhhnpeehudfgudffffetuedtvdehueevledvhfelleeivedtgeeuhfegueeviedu ffeivdenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpe gsohhquhhnodhmvghsmhhtphgruhhthhhpvghrshhonhgrlhhithihqdeiledvgeehtdei gedqudejjeekheehhedvqdgsohhquhhnrdhfvghngheppehgmhgrihhlrdgtohhmsehfih igmhgvrdhnrghmvgdpnhgspghrtghpthhtoheptd X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sun, 4 Aug 2024 02:22:17 -0400 (EDT) Date: Sat, 3 Aug 2024 23:21:21 -0700 From: Boqun Feng To: Danilo Krummrich Cc: ojeda@kernel.org, alex.gaynor@gmail.com, wedsonaf@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@samsung.com, aliceryhl@google.com, akpm@linux-foundation.org, daniel.almeida@collabora.com, faith.ekstrand@collabora.com, boris.brezillon@collabora.com, lina@asahilina.net, mcanal@igalia.com, zhiw@nvidia.com, acurrid@nvidia.com, cjia@nvidia.com, jhubbard@nvidia.com, airlied@redhat.com, ajanulgu@redhat.com, lyude@redhat.com, linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-mm@kvack.org Subject: Re: [PATCH v3 01/25] rust: alloc: add `Allocator` trait Message-ID: References: <20240801000641.1882-1-dakr@kernel.org> <20240801000641.1882-2-dakr@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240801000641.1882-2-dakr@kernel.org> X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: E72F6180009 X-Stat-Signature: m6c1xqu3hk8huod7te3y37f6xq6scgae X-HE-Tag: 1722753076-511516 X-HE-Meta: U2FsdGVkX19SK5A65gXCYb+Uyn9URohZ4f+8x9XaEZ3WKN5uo+l/bEVOys9a0+lbqH7jtEH5PsoAd+pF3hwAj1NG8wugIImiU0DRWZmZGlgriDtGMSd061DMbhuDlGBZkxxs+5uDx+BG5zOguOH+AOrocEGPQNkvIzaiNH6bgxafw9vtFI4U9t2/FANi/WCAsEkqDVpNxgTEAgMgCUwKSxf92DmbNW6fhV5TPoibd094+dUwLPhXrWriKjAqQkxKJI0UwULzJEv4OmzfByRtinx0mw/5ex6GiPwd32ZV1H6sI2D3oBn6rpWtyZbUDsk3XSTrDueuv+JHWfgPAZstjNWXOmilTDy1GGlNf4JrhoahOe9BQ7CGBzxDfgZ16qkRnUyhMAf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tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Thu, Aug 01, 2024 at 02:02:00AM +0200, Danilo Krummrich wrote: [...] > +/// The kernel's [`Allocator`] trait. > +/// > +/// An implementation of [`Allocator`] can allocate, re-allocate and free memory buffer described > +/// via [`Layout`]. > +/// > +/// [`Allocator`] is designed to be implemented as a ZST; [`Allocator`] functions do not operate on > +/// an object instance. > +/// > +/// # Safety > +/// > +/// Memory returned from an allocator must point to a valid memory buffer and remain valid until > +/// it is explicitly freed. > +/// > +/// Any pointer to a memory buffer which is currently allocated must be valid to be passed to any > +/// other [`Allocator`] function. The same applies for a NULL pointer. > +/// Are you saying you could kmalloc() a memory buffer and pass it to a vfree()? Or am I missing something here? Regards, Boqun > +/// If `realloc` is called with: > +/// - a size of zero, the given memory allocation, if any, must be freed > +/// - a NULL pointer, a new memory allocation must be created > +pub unsafe trait Allocator { > + /// Allocate memory based on `layout` and `flags`. > + /// > + /// On success, returns a buffer represented as `NonNull<[u8]>` that satisfies the layout > + /// constraints (i.e. minimum size and alignment as specified by `layout`). > + /// > + /// This function is equivalent to `realloc` when called with a NULL pointer. > + fn alloc(layout: Layout, flags: Flags) -> Result, AllocError> { > + // SAFETY: Passing a NULL pointer to `realloc` is valid by it's safety requirements and asks > + // for a new memory allocation. > + unsafe { Self::realloc(None, layout, flags) } > + } > + > + /// Re-allocate an existing memory allocation to satisfy the requested `layout`. If the > + /// requested size is zero, `realloc` behaves equivalent to `free`. > + /// > + /// If the requested size is larger than the size of the existing allocation, a successful call > + /// to `realloc` guarantees that the new or grown buffer has at least `Layout::size` bytes, but > + /// may also be larger. > + /// > + /// If the requested size is smaller than the size of the existing allocation, `realloc` may or > + /// may not shrink the buffer; this is implementation specific to the allocator. > + /// > + /// On allocation failure, the existing buffer, if any, remains valid. > + /// > + /// The buffer is represented as `NonNull<[u8]>`. > + /// > + /// # Safety > + /// > + /// `ptr` must point to an existing and valid memory allocation created by this allocator > + /// instance. > + /// > + /// Additionally, `ptr` is allowed to be a NULL pointer; in this case a new memory allocation is > + /// created. > + unsafe fn realloc( > + ptr: Option>, > + layout: Layout, > + flags: Flags, > + ) -> Result, AllocError>; > + > + /// Free an existing memory allocation. > + /// > + /// # Safety > + /// > + /// `ptr` must point to an existing and valid memory allocation created by this `Allocator` > + /// instance. > + unsafe fn free(ptr: NonNull) { > + // SAFETY: `ptr` is guaranteed to be previously allocated with this `Allocator` or NULL. > + // Calling `realloc` with a buffer size of zero, frees the buffer `ptr` points to. > + let _ = unsafe { Self::realloc(Some(ptr), Layout::new::<()>(), Flags(0)) }; > + } > +} > -- > 2.45.2 >