[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <20251001044508.23126-1-git@elijahs.space>
Date: Tue, 30 Sep 2025 21:44:21 -0700
From: Elijah Wright <git@...jahs.space>
To: Elijah Wright <git@...jahs.space>,
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>,
Alice Ryhl <aliceryhl@...gle.com>,
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: [PATCH v2] rust: slab: add basic slab module
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>
---
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)
+ }
+ }
+ };
+}
\ No newline at end of file
--
2.49.1
Powered by blists - more mailing lists