From nobody Thu Nov 14 16:41:42 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 5AB901C75E7; Fri, 4 Oct 2024 15:42:54 +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=1728056574; cv=none; b=DxfaTyZTmHU80+yYH9OXFBnn15pTNMoypr/6G6h60guO/e1cFUKKdGg/RRJ9h5Q51Uxfs6dF80vK7BI9416QS8EcRhUwJ7MHmw1FP52hOwou8w9sIcuQtzp5LUIUTuX+1E1ST9+DwAz7Sx3HQAltuklWFxqTDx/r1ABmwVYfMFw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728056574; c=relaxed/simple; bh=bmN7ni692A736mVNYrpUELQFpNi/F1V47sEOIzb2Zxc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=HRBP0LamQjWnWZ04bW+wnEQMU3robQLRkB5d8E5a7EB6kWATQ7xCwhbNhtU6NSr05xE2xHft5vVDbq+B2GnhFDYp9tidW85epsHrtGFKgFhhvtFNh/Yem7mUjnE75h5BrDlTp1NmJhaqvmCfBYCZclR+Iv9zPlG7aBgxZOq++TE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=qKjQ8ZPm; 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="qKjQ8ZPm" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4CD91C4CEC7; Fri, 4 Oct 2024 15:42:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1728056574; bh=bmN7ni692A736mVNYrpUELQFpNi/F1V47sEOIzb2Zxc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qKjQ8ZPmnVtLHqr4nor207d5+lle0HUgclARf5EncF5XP8/iOOzpJ08rCz3Zf3K/q K+XtuJ8Un3vl+Adb3MVI6prusot2SL+Z/UwzTKQLoqlt68BtrM+123fOkwQXTrMhjH 89pD77I6HL94tZs/5aC52cRVLNRIILom+coxCtCDk1R+Aa19I135JJNEoC1pJr39Ar jOyml3k76DlaNKTwO6U9mfO1p/9EVrZug5sZopte54z/yzrBOhwDmvAnReyaZ8g4K/ VepMZe3eyDzdjg5iHBnqXzIHbrRBxVCc7ia98DqMjjvONZjKWXFbGZjnCfAsNdrKVh f/C5KfKLN6ySQ== 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 v9 09/29] rust: alloc: implement `KVmalloc` allocator Date: Fri, 4 Oct 2024 17:41:13 +0200 Message-ID: <20241004154149.93856-10-dakr@kernel.org> X-Mailer: git-send-email 2.46.1 In-Reply-To: <20241004154149.93856-1-dakr@kernel.org> References: <20241004154149.93856-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" Implement `Allocator` for `KVmalloc`, an `Allocator` that tries to allocate memory wth `kmalloc` first and, on failure, falls back to `vmalloc`. All memory allocations made with `KVmalloc` end up in `kvrealloc_noprof()`; all frees in `kvfree()`. Reviewed-by: Alice Ryhl Reviewed-by: Benno Lossin Reviewed-by: Gary Guo Signed-off-by: Danilo Krummrich --- rust/helpers/slab.c | 6 +++++ rust/kernel/alloc/allocator.rs | 36 +++++++++++++++++++++++++++++ rust/kernel/alloc/allocator_test.rs | 1 + 3 files changed, 43 insertions(+) diff --git a/rust/helpers/slab.c b/rust/helpers/slab.c index f043e087f9d6..a842bfbddcba 100644 --- a/rust/helpers/slab.c +++ b/rust/helpers/slab.c @@ -7,3 +7,9 @@ rust_helper_krealloc(const void *objp, size_t new_size, gfp= _t flags) { return krealloc(objp, new_size, flags); } + +void * __must_check __realloc_size(2) +rust_helper_kvrealloc(const void *p, size_t size, gfp_t flags) +{ + return kvrealloc(p, size, flags); +} diff --git a/rust/kernel/alloc/allocator.rs b/rust/kernel/alloc/allocator.rs index 5742ce7d0453..d9043ca42ce5 100644 --- a/rust/kernel/alloc/allocator.rs +++ b/rust/kernel/alloc/allocator.rs @@ -34,6 +34,15 @@ /// For more details see [self]. pub struct Vmalloc; =20 +/// The kvmalloc kernel allocator. +/// +/// `KVmalloc` attempts to allocate memory with `Kmalloc` first, but falls= back to `Vmalloc` upon +/// failure. This allocator is typically used when the size for the reques= ted allocation is not +/// known and may exceed the capabilities of `Kmalloc`. +/// +/// For more details see [self]. +pub struct KVmalloc; + /// Returns a proper size to alloc a new object aligned to `new_layout`'s = alignment. fn aligned_size(new_layout: Layout) -> usize { // Customized layouts from `Layout::from_size_align()` can have size <= align, so pad first. @@ -76,6 +85,9 @@ impl ReallocFunc { // INVARIANT: `vrealloc` satisfies the type invariants. const VREALLOC: Self =3D Self(bindings::vrealloc); =20 + // INVARIANT: `kvrealloc` satisfies the type invariants. + const KVREALLOC: Self =3D Self(bindings::kvrealloc); + /// # Safety /// /// This method has the same safety requirements as [`Allocator::reall= oc`]. @@ -205,6 +217,30 @@ unsafe fn realloc( } } =20 +// SAFETY: `realloc` delegates to `ReallocFunc::call`, which guarantees th= at +// - memory remains valid until it is explicitly freed, +// - passing a pointer to a valid memory allocation is OK, +// - `realloc` satisfies the guarantees, since `ReallocFunc::call` has the= same. +unsafe impl Allocator for KVmalloc { + #[inline] + unsafe fn realloc( + ptr: Option>, + layout: Layout, + old_layout: Layout, + flags: Flags, + ) -> Result, AllocError> { + // TODO: Support alignments larger than PAGE_SIZE. + if layout.align() > bindings::PAGE_SIZE { + pr_warn!("KVmalloc does not support alignments larger than PAG= E_SIZE yet.\n"); + return Err(AllocError); + } + + // SAFETY: If not `None`, `ptr` is guaranteed to point to valid me= mory, which was previously + // allocated with this `Allocator`. + unsafe { ReallocFunc::KVREALLOC.call(ptr, layout, old_layout, flag= s) } + } +} + #[global_allocator] static ALLOCATOR: Kmalloc =3D Kmalloc; =20 diff --git a/rust/kernel/alloc/allocator_test.rs b/rust/kernel/alloc/alloca= tor_test.rs index e7bf2982f68f..1b2642c547ec 100644 --- a/rust/kernel/alloc/allocator_test.rs +++ b/rust/kernel/alloc/allocator_test.rs @@ -8,6 +8,7 @@ =20 pub struct Kmalloc; pub type Vmalloc =3D Kmalloc; +pub type KVmalloc =3D Kmalloc; =20 unsafe impl Allocator for Kmalloc { unsafe fn realloc( --=20 2.46.1