From: Alice Ryhl <aliceryhl@google.com>
To: Elijah Wright <git@elijahs.space>
Cc: "Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Trevor Gross" <tmgross@umich.edu>,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
"Lorenzo Stoakes" <lorenzo.stoakes@oracle.com>,
"Vlastimil Babka" <vbabka@suse.cz>,
"Liam R . Howlett" <Liam.Howlett@oracle.com>,
"Uladzislau Rezki" <urezki@gmail.com>,
linux-mm@kvack.org
Subject: Re: [PATCH v2] rust: slab: add basic slab module
Date: Thu, 6 Nov 2025 08:53:49 +0100 [thread overview]
Message-ID: <CAH5fLgiDwghSy_P3ER8WAYyAtRPgVji_wsmkiNfvc5HHpUpGnA@mail.gmail.com> (raw)
In-Reply-To: <20251001044508.23126-1-git@elijahs.space>
On Wed, Oct 1, 2025 at 6:45 AM Elijah Wright <git@elijahs.space> wrote:
>
> this revision adds gen_kmem_cache_allocator, a macro that implements
> Allocator::realloc for kmem_cache. the one concern that I did have was realloc()
> for resizing, since that obviously isn't possible for slab
>
> Signed-off-by: Elijah Wright <git@elijahs.space>
When you send a new version, please don't reply to the previous
version. It's too easy to miss the ne version if you do.
> ---
> rust/kernel/slab.rs | 52 +++++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 52 insertions(+)
>
> diff --git a/rust/kernel/slab.rs b/rust/kernel/slab.rs
> index 8b418f9db7cb..3f1310f309c5 100644
> --- a/rust/kernel/slab.rs
> +++ b/rust/kernel/slab.rs
> @@ -83,3 +83,55 @@ fn drop(&mut self) {
> unsafe { bindings::kmem_cache_destroy(self.cache.as_ptr()) };
> }
> }
> +
> +// SAFETY: The pointer does not change after creation, so `Slab<T>` may
> +// be used from multiple threads.
> +unsafe impl<T> Send for Slab<T> {}
> +unsafe impl<T> Sync for Slab<T> {}
> +
> +/// Generates a zero-sized allocator type that allocates from a given
> +/// `Slab<T>`.
> +#[macro_export]
> +macro_rules! gen_kmem_cache_allocator {
> + (struct $name:ident for $cache:expr $(,)?) => {
> + #[derive(Clone, Copy, Default)]
> + pub struct $name;
> +
> + // SAFETY: Allocation and free happen through kernel APIs which
> + // provide guarantees. The ZST carries no state, so it can be
> + // duplicated freely.
> + unsafe impl $crate::alloc::Allocator for $name {
> + #[inline]
> + unsafe fn realloc(
> + ptr: Option::<::core::ptr::NonNull<u8>>,
> + layout: ::core::alloc::Layout,
> + old_layout: ::core::alloc::Layout,
> + flags: $crate::alloc::Flags,
> + ) -> ::core::result::Result<::core::ptr::NonNull<[u8]>, $crate::alloc::AllocError> {
> + if layout.size() == 0 {
> + if let Some(p) = ptr {
> + // SAFETY: Caller promises `p` came from this allocator.
> + unsafe {
> + $crate::bindings::kmem_cache_free($cache.as_ptr(), p.as_ptr().cast());
> + }
> + }
> + let dang = $crate::alloc::dangling_from_layout(layout);
> + let slice = ::core::ptr::NonNull::slice_from_raw_parts(dang, 0);
> + return Ok(slice);
> + }
> +
> + if ptr.is_some() {
> + return Err($crate::alloc::AllocError);
> + }
> +
> + let raw_ptr = unsafe {
> + $crate::bindings::kmem_cache_alloc($cache.as_ptr(), flags.as_raw())
> + };
> + let nn = ::core::ptr::NonNull::new(raw_ptr.cast())
> + .ok_or($crate::alloc::AllocError)?;
> + let slice = ::core::ptr::NonNull::slice_from_raw_parts(nn.cast::<u8>(), layout.size());
> + Ok(slice)
Hm, this is kind of tricky. We specify a size when calling this, but
kmem caches only support one single size. I don't know what to do
about that.
Alice
prev parent reply other threads:[~2025-11-06 7:54 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-24 19:36 [PATCH] rust: slab: add basic slab module Elijah Wright
2025-09-25 2:17 ` John Hubbard
2025-09-25 8:01 ` Benno Lossin
2025-09-25 9:54 ` Danilo Krummrich
2025-09-25 17:20 ` Elijah
2025-09-25 17:43 ` Danilo Krummrich
2025-09-25 18:02 ` Liam R. Howlett
2025-09-25 18:08 ` Danilo Krummrich
2025-09-25 18:11 ` Boqun Feng
2025-09-25 18:05 ` Elijah
2025-09-25 18:15 ` Danilo Krummrich
2025-09-25 18:17 ` Danilo Krummrich
[not found] ` <74b3ef24-a307-4d3c-891a-8c5283448b20@elijahs.space>
2025-09-25 18:52 ` Elijah
2025-09-25 17:54 ` Alice Ryhl
2025-09-26 15:00 ` Vlastimil Babka
2025-09-26 15:33 ` Danilo Krummrich
2025-09-26 15:55 ` Danilo Krummrich
2025-09-26 16:32 ` Vlastimil Babka
2025-09-26 16:58 ` Danilo Krummrich
2025-09-26 17:11 ` Vlastimil Babka
2025-09-26 19:01 ` Danilo Krummrich
2025-09-28 14:47 ` Danilo Krummrich
2025-09-29 7:14 ` Vlastimil Babka
2025-09-29 14:11 ` Elijah
2025-09-29 20:32 ` Danilo Krummrich
2025-10-01 4:44 ` [PATCH v2] " Elijah Wright
2025-11-06 7:53 ` Alice Ryhl [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CAH5fLgiDwghSy_P3ER8WAYyAtRPgVji_wsmkiNfvc5HHpUpGnA@mail.gmail.com \
--to=aliceryhl@google.com \
--cc=Liam.Howlett@oracle.com \
--cc=a.hindborg@kernel.org \
--cc=alex.gaynor@gmail.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=gary@garyguo.net \
--cc=git@elijahs.space \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=lorenzo.stoakes@oracle.com \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
--cc=urezki@gmail.com \
--cc=vbabka@suse.cz \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).