From nobody Thu Sep 19 21:59:17 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B3DE3149002; Wed, 11 Sep 2024 22:55:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726095306; cv=none; b=q7x8CfLpe6+6fLeMk73LS7JDG3Rg5FFAs3ajnWUuZ/nN1DinOJKsPngOTXy3ZECz1qbpG/Y5Jp4xyUnXaq9+NH+AHijJc1s8AFCXHbRExIGzTdBn2gqhtC8s9RYNHUUhc47SsTF+CldhH7mQwBjcnjEyt33YnMonPoF314jcTRE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726095306; c=relaxed/simple; bh=YJ+3Hmuf4lg4RSyjGhOmEDyEN9eOYFpIUvOB6gUqpsA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pWBoRaJDU/uzcOx2WjDo2fNoxkoXUlVVCmvx5VvqThlCeIyx44Vj8FuUkXVtAqGLqEn1iXiPqbtiU1K9qbcdnyNNEDSWx0I33u/RyVBAjuP2PPytwLMQ/5iz4EkWCTys4k0JtN4fl84ObnAkb6iaj6Xnz3cQIxvVRCcOt+BzzQQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=PP0oko47; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="PP0oko47" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 65187C4CECD; Wed, 11 Sep 2024 22:55:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1726095306; bh=YJ+3Hmuf4lg4RSyjGhOmEDyEN9eOYFpIUvOB6gUqpsA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PP0oko47af8i3c5/Hy4kVN84YWSFkT4p5u/bF6oMUDUOZRQfFG0aMXubN+SNpeYQl cctwMTAnRRXh1rDSCnD52PpqzOTQ8w3HGFwhoqSWbCCXY6aeRmN1FhE64VK72pQ2j5 ydR9BBdGMz8nUgJnI5R/AVyqMLrm1FRRz3vPpsyXc0byHV2UD41PKjPn/A5YR59pLq JZlF/t+zbfPr1eW0CAOx1/tMzOxJl1uF5rGQM7OdGcNzE19DB/60OCTs4r65NJkf87 KT1UlGA/9BV/3iuLjtUoQTHY49o5fI3MBDBfB5Mt+5RXpSYXbJKaq6fMc7wKLgnoAm 9dA6oIh6GatmA== From: Danilo Krummrich To: ojeda@kernel.org, alex.gaynor@gmail.com, wedsonaf@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@samsung.com, aliceryhl@google.com, akpm@linux-foundation.org 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, Danilo Krummrich Subject: [PATCH v7 01/26] rust: alloc: add `Allocator` trait Date: Thu, 12 Sep 2024 00:52:37 +0200 Message-ID: <20240911225449.152928-2-dakr@kernel.org> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240911225449.152928-1-dakr@kernel.org> References: <20240911225449.152928-1-dakr@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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. Subsequent patches add the following trait implementors: `Kmalloc`, `Vmalloc` and `KVmalloc`. Reviewed-by: Alice Ryhl Signed-off-by: Danilo Krummrich --- rust/kernel/alloc.rs | 112 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) diff --git a/rust/kernel/alloc.rs b/rust/kernel/alloc.rs index 1966bd407017..6c21bd2edad9 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,114 @@ 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 free = memory buffers described +/// via [`Layout`]. +/// +/// [`Allocator`] is designed to be implemented as a ZST; [`Allocator`] fu= nctions do not operate on +/// an object instance. +/// +/// In order to be able to support `#[derive(SmartPointer)]` later on, we = need to avoid a design +/// that requires an `Allocator` to be instantiated, hence its functions m= ust not contain any kind +/// of `self` parameter. +/// +/// # Safety +/// +/// - A memory allocation returned from an allocator must remain valid unt= il it is explicitly freed. +/// +/// - Any pointer to a valid memory allocation must be valid to be passed = to any other [`Allocator`] +/// function of the same type. +/// +/// - Implementers must ensure that all trait functions abide by the guara= ntees documented in the +/// `# Guarantees` sections. +// +// Note that `Allocator::{realloc,free}` don't have an `old_layout` argume= nt (like stdlib's +// corresponding `Allocator` trait functions have), since the implemented = (kernel) allocators +// neither need nor honor such an argument. Thus, it would be misleading t= o make this API require it +// anyways. +// +// More generally, this trait isn't intended for implementers to encode a = lot of semantics, but +// rather provide a thin generalization layer for the kernel's allocators. +// +// Depending on future requirements, the requirements for this trait may c= hange as well and +// implementing allocators that need to encode more semantics may become d= esirable. +pub unsafe trait Allocator { + /// Allocate memory based on `layout` and `flags`. + /// + /// On success, returns a buffer represented as `NonNull<[u8]>` that s= atisfies the layout + /// constraints (i.e. minimum size and alignment as specified by `layo= ut`). + /// + /// This function is equivalent to `realloc` when called with `None`. + /// + /// # Guarantees + /// + /// When the return value is `Ok(ptr)`, then `ptr` is + /// - valid for reads and writes for `layout.size()` bytes, until it i= s passed to + /// [`Allocator::free`] or [`Allocator::realloc`], + /// - aligned to `layout.align()`, + /// + /// Additionally, `Flags` are honored as documented in + /// . + fn alloc(layout: Layout, flags: Flags) -> Result, AllocE= rror> { + // SAFETY: Passing `None` to `realloc` is valid by it's safety req= uirements 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 `fr= ee`. + /// + /// If the requested size is larger than the size of the existing allo= cation, 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 all= ocation, `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 + /// + /// If `ptr =3D=3D Some(p)`, then `p` must point to an existing and va= lid memory allocation created + /// by this allocator. The alignment encoded in `layout` must be small= er than or equal to the + /// alignment requested in the previous `alloc` or `realloc` call of t= he same allocation. + /// + /// Additionally, `ptr` is allowed to be `None`; in this case a new me= mory allocation is + /// created. + /// + /// # Guarantees + /// + /// This function has the same guarantees as [`Allocator::alloc`]. Whe= n `ptr =3D=3D Some(p)`, then + /// it additionally guarantees that: + /// - the contents of the memory pointed to by `p` are preserved up to= the lesser of the new + /// and old size, + /// and old size, i.e. + /// `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 va= lid. + 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 create= d by this `Allocator` and + /// must not be a dangling pointer. + /// + /// The memory allocation at `ptr` must never again be read from or wr= itten to. + unsafe fn free(ptr: NonNull) { + // SAFETY: The caller guarantees that `ptr` points at a valid allo= cation created by this + // allocator. We are passing a `Layout` with the smallest possible= alignment, so it is + // smaller than or equal to the alignment previously used with thi= s allocation. + let _ =3D unsafe { Self::realloc(Some(ptr), Layout::new::<()>(), F= lags(0)) }; + } +} --=20 2.46.0