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 E8763C3DA4A for ; Wed, 14 Aug 2024 16:13:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7E4796B0095; Wed, 14 Aug 2024 12:13:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 794DC6B0096; Wed, 14 Aug 2024 12:13:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 65C666B0098; Wed, 14 Aug 2024 12:13:18 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 464756B0095 for ; Wed, 14 Aug 2024 12:13:18 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id BA64BC0F2C for ; Wed, 14 Aug 2024 16:13:17 +0000 (UTC) X-FDA: 82451345634.14.C64E1C3 Received: from mail-40134.protonmail.ch (mail-40134.protonmail.ch [185.70.40.134]) by imf29.hostedemail.com (Postfix) with ESMTP id B5DC512000A for ; Wed, 14 Aug 2024 16:13:15 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=proton.me header.s=protonmail header.b=PnrlFts0; dmarc=pass (policy=quarantine) header.from=proton.me; spf=pass (imf29.hostedemail.com: domain of benno.lossin@proton.me designates 185.70.40.134 as permitted sender) smtp.mailfrom=benno.lossin@proton.me ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723651983; a=rsa-sha256; cv=none; b=cyRPZwHTIeSoLtdM73y0JJoWIAvYoRxnSKde6pJxO7Z9KOxPaZqVvbd/LUD4yOrVGoQ+9O AlNueFiF2bbLAIaJzQEXY2xIVtxQSqEVab2SVaXmGciOfvIeHO1IsIU1+y7uDGSHyupcIU yRlS+tznp5BuyaK+wHAj/DwAMCm4egM= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=proton.me header.s=protonmail header.b=PnrlFts0; dmarc=pass (policy=quarantine) header.from=proton.me; spf=pass (imf29.hostedemail.com: domain of benno.lossin@proton.me designates 185.70.40.134 as permitted sender) smtp.mailfrom=benno.lossin@proton.me ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723651983; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=xgUscqXXYtwDOlu+dM+bPEi3GjHXvIJyy7e+gW0dRD4=; b=wliwOyZQuOJBhvrtg2QRSEySeHb0poRYrqkDCR5add++jlhqtTWFdgOSMlZnPROp+Vi86J /mKvYOEuou5k/FaGiUMzCIiaxdJBakEqAaJ3S6WDBf+H5pUq11ufufa3Wt4zhQvC6Gkfqj o0K3+DMh4ByX9epct/qpTunczx8Wm+U= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1723651992; x=1723911192; bh=xgUscqXXYtwDOlu+dM+bPEi3GjHXvIJyy7e+gW0dRD4=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=PnrlFts0ErI2yAsgLbggDypo8LPX0CsBWM7jblway8hx9km1tTfFUaQ8/tgYSgXbU Xr6+zR7yeeVC6O3jQnvDemt6RG9VhF/AwtA3W7iyP6Sl5rVjWrXORnjx6p9Bc9Jf5l AUwzdKcQMtBToxeOu7IkfkNdcOHpRvOSn6t9YjA/hsnEf8NZUhBa39piN/GQIf3IX5 7hs6SfbTPTKsbS+DFT1yqbHUXKGiph/KTAh76MJyjxmb1s+WlwXtXh+ZPmR7QquAAG VDfPfbh77RXjuzyWk4WzedXZFUENsQoO//7adD7XhNHRu9Xf4RCmIfSRHJ+BQFtB9h yK0v8+u166dTw== Date: Wed, 14 Aug 2024 16:13:06 +0000 To: Danilo Krummrich , ojeda@kernel.org, alex.gaynor@gmail.com, wedsonaf@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, a.hindborg@samsung.com, aliceryhl@google.com, akpm@linux-foundation.org From: Benno Lossin Cc: daniel.almeida@collabora.com, faith.ekstrand@collabora.com, boris.brezillon@collabora.com, lina@asahilina.net, mcanal@igalia.com, zhiw@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 v5 01/26] rust: alloc: add `Allocator` trait Message-ID: In-Reply-To: <20240812182355.11641-2-dakr@kernel.org> References: <20240812182355.11641-1-dakr@kernel.org> <20240812182355.11641-2-dakr@kernel.org> Feedback-ID: 71780778:user:proton X-Pm-Message-ID: fb4aacd9e1618193c3c420b9aaebbdc5d458f11e MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Rspam-User: X-Rspamd-Queue-Id: B5DC512000A X-Rspamd-Server: rspam01 X-Stat-Signature: 4osezsswejmirsytkbmu5s8e88h1r15h X-HE-Tag: 1723651995-933998 X-HE-Meta: U2FsdGVkX183JwcDgNPyDP4fvTbtaD91m/Y5+YFkPtoN6XR4jqiwKztQJBX89D6hk19KbLyWMXITB39zcYJybjIyh5ErYYZSvhIUXBnAPVLL6AgtsE9I2gDGdeu0y22EW4ZtdRFyO7uZTtFob6KTgMsF38U99nbc3QyqnbmSofigS411ywqdCyjWXamF8KM898rPstUAVXSCco2ZwQDF/6Yeev74cao1m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Jd27Q58067g/4rdDLe2VRDCKPLMdSdSz3crldyzC8iVurVQ6hn/lf/VwoaEqGwWFGlGhEgYKanfdYwXULxBFRUH3BQjMpAqrpxiJ5Licq1r7pKP7tdbCJvc7OwEs68 X-Bogosity: Ham, 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 12.08.24 20:22, Danilo Krummrich wrote: > Add a kernel specific `Allocator` trait, that in contrast to the one in > Rust's core library doesn't require unstable features and supports GFP > flags. >=20 > Subsequent patches add the following trait implementors: `Kmalloc`, > `Vmalloc` and `KVmalloc`. >=20 > Reviewed-by: Alice Ryhl > Signed-off-by: Danilo Krummrich > --- > rust/kernel/alloc.rs | 81 ++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 81 insertions(+) >=20 > diff --git a/rust/kernel/alloc.rs b/rust/kernel/alloc.rs > index 1966bd407017..194745498a75 100644 > --- a/rust/kernel/alloc.rs > +++ b/rust/kernel/alloc.rs > @@ -11,6 +11,7 @@ > /// Indicates an allocation error. > #[derive(Copy, Clone, PartialEq, Eq, Debug)] > pub struct AllocError; > +use core::{alloc::Layout, ptr::NonNull}; >=20 > /// Flags to be used when allocating memory. > /// > @@ -86,3 +87,83 @@ pub mod flags { > /// small allocations. > pub const GFP_NOWAIT: Flags =3D Flags(bindings::GFP_NOWAIT); > } > + > +/// The kernel's [`Allocator`] trait. > +/// > +/// An implementation of [`Allocator`] can allocate, re-allocate and fre= e memory buffer described > +/// via [`Layout`]. > +/// > +/// [`Allocator`] is designed to be implemented as a ZST; [`Allocator`] = functions do not operate on > +/// an object instance. > +/// > +/// In order to be able to support `#[derive(SmartPointer)]` later on, w= e need to avoid a design > +/// that requires an `Allocator` to be instantiated, hence its functions= must not contain any kind > +/// of `self` parameter. > +/// > +/// # Safety > +/// > +/// Memory returned from an allocator must point to a valid memory buffe= r and remain valid until > +/// it is explicitly freed. I wouldn't say that an allocator "returns memory", and in general I don't think the structure of the safety comment here is nice, how about the following: we put "Implementers must ensure that all trait functions abide by the guarantees documented in the `# Guarantees` sections."... > +/// > +/// Any pointer to a memory buffer which is currently allocated must be = valid to be passed to any > +/// other [`Allocator`] function of the same type. > +/// > +/// If `realloc` is called with: > +/// - a size of zero, the given memory allocation, if any, must be fre= ed > +/// - `None`, 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 `la= yout`). > + /// > + /// This function is equivalent to `realloc` when called with `None`= . ... Then we can add this here: /// # Guarantees /// /// When the return value is `Ok(ptr)`, then `ptr` is /// - valid for writes (and reads after the memory has been initialized= ) for `layout.size()` bytes, /// until it is passed to [`Allocator::free`] or [`Allocator::realloc= `], /// - aligned to `layout.align()`, /// - is valid for reads, if `flags.contains(flags::__GFP_ZERO)`, Do we need to handle other flags? Also IIRC the memory given to us by C is considered initialized by Rust (though it has a non-deterministic value), so we might have an unconditional "valid for reads". Am I correct? > + fn alloc(layout: Layout, flags: Flags) -> Result, Allo= cError> { > + // SAFETY: Passing `None` to `realloc` is valid by it's safety r= equirements and asks for a > + // new memory allocation. > + unsafe { Self::realloc(None, layout, flags) } > + } > + > + /// Re-allocate an existing memory allocation to satisfy the request= ed `layout`. If the > + /// requested size is zero, `realloc` behaves equivalent to `free`. I don't think we want to include the second sentence in the short description of this function, please add an empty line in between. > + /// > + /// If the requested size is larger than the size of the existing al= location, a successful call > + /// to `realloc` guarantees that the new or grown buffer has at leas= t `Layout::size` bytes, but > + /// may also be larger. > + /// > + /// If the requested size is smaller than the size of the existing a= llocation, `realloc` may or > + /// may not shrink the buffer; this is implementation specific to th= e allocator. > + /// > + /// On allocation failure, the existing buffer, if any, remains vali= d. > + /// > + /// The buffer is represented as `NonNull<[u8]>`. > + /// > + /// # Safety > + /// > + /// If `ptr =3D Some(p)`, then `p` must point to an existing and val= id memory allocation created I don't like the single `=3D` (I might have written it in haste myself), how about `=3D=3D` or we use if-let syntax? > + /// by this allocator. The alignment encoded in `layout` must be sma= ller than or equal to the > + /// alignment requested in the previous `alloc` or `realloc` call of= the same allocation. > + /// > + /// Additionally, `ptr` is allowed to be `None`; in this case a new = memory allocation is > + /// created. This Safety section does not talk about the case `layout.size() =3D=3D 0`, but it should have the same requirement as `free()`. Also add a `# Guarantees` section here: /// # Guarantees /// /// This function has the same guarantees as [`Allocator::alloc`]. When= `ptr =3D=3D Some(p)`, then it /// additionally has the following: /// - when `Ok(ret_ptr)` is the return value, then /// `ret_ptr[0..min(layout.size(), old_size)] =3D=3D p[0..min(layout.= size(), old_size)]`, where /// `old_size` is the size of the allocation that `p` points at. /// - when the return value is `Err(AllocError)`, then `p` is still val= id. --- Cheers, Benno > + 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 crea= ted by this `Allocator` and > + /// must not be a dangling pointer. > + /// > + /// The memory allocation at `ptr` must never again be read from or = written to. > + unsafe fn free(ptr: NonNull) { > + // SAFETY: The caller guarantees that `ptr` points at a valid al= location created by this > + // allocator. We are passing a `Layout` with the smallest possib= le alignment, so it is > + // smaller than or equal to the alignment previously used with t= his allocation. > + let _ =3D unsafe { Self::realloc(Some(ptr), Layout::new::<()>(),= Flags(0)) }; > + } > +} > -- > 2.45.2 >=20