lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <CAH5fLgiDwghSy_P3ER8WAYyAtRPgVji_wsmkiNfvc5HHpUpGnA@mail.gmail.com>
Date: Thu, 6 Nov 2025 08:53:49 +0100
From: Alice Ryhl <aliceryhl@...gle.com>
To: Elijah Wright <git@...jahs.space>
Cc: Miguel Ojeda <ojeda@...nel.org>, Alex Gaynor <alex.gaynor@...il.com>, 
	Boqun Feng <boqun.feng@...il.com>, Gary Guo <gary@...yguo.net>, 
	Björn Roy Baron <bjorn3_gh@...tonmail.com>, 
	Benno Lossin <lossin@...nel.org>, Andreas Hindborg <a.hindborg@...nel.org>, 
	Trevor Gross <tmgross@...ch.edu>, rust-for-linux@...r.kernel.org, 
	linux-kernel@...r.kernel.org, Lorenzo Stoakes <lorenzo.stoakes@...cle.com>, 
	Vlastimil Babka <vbabka@...e.cz>, "Liam R . Howlett" <Liam.Howlett@...cle.com>, 
	Uladzislau Rezki <urezki@...il.com>, linux-mm@...ck.org
Subject: Re: [PATCH v2] rust: slab: add basic slab module

On Wed, Oct 1, 2025 at 6:45 AM Elijah Wright <git@...jahs.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@...jahs.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

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ