* [PATCH v8 1/2] rust: alloc: add alignment tests for allocators
2025-07-31 8:42 [PATCH v8 0/2] Rust allocator and kvec improvements Hui Zhu
@ 2025-07-31 8:42 ` Hui Zhu
2025-07-31 8:58 ` Danilo Krummrich
2025-07-31 8:42 ` [PATCH v8 2/2] rust: alloc: kvec: add doc example for as_slice method Hui Zhu
1 sibling, 1 reply; 4+ messages in thread
From: Hui Zhu @ 2025-07-31 8:42 UTC (permalink / raw)
To: Danilo Krummrich, Lorenzo Stoakes, Vlastimil Babka,
Liam R . Howlett, Uladzislau Rezki, Miguel Ojeda, Alex Gaynor,
Boqun Feng, Gary Guo, bjorn3_gh, Benno Lossin, Andreas Hindborg,
Alice Ryhl, Trevor Gross, rust-for-linux, linux-kernel, akpm,
vitaly.wool
Cc: Hui Zhu, Geliang Tang, Kunwu Chan, David Gow
From: Hui Zhu <zhuhui@kylinos.cn>
Add unit tests to verify alignment guarantees of Kmalloc, Vmalloc,
and KVmallocallocators. Tests create types with explicit alignment
requirements (128B and 8192B) and validate allocations meet alignment.
Includes two test blobs with custom alignments and helper struct
TestAlignto abstract allocation and alignment checks.
Also updates kvec test macro name from rust_kvec_kunitto rust_kvec
for consistency with other test modules.
Co-developed-by: Geliang Tang <geliang@kernel.org>
Signed-off-by: Geliang Tang <geliang@kernel.org>
Signed-off-by: Hui Zhu <zhuhui@kylinos.cn>
Reviewed-by: Kunwu Chan <chentao@kylinos.cn>
Reviewed-by: David Gow <davidgow@google.com>
---
rust/kernel/alloc/allocator.rs | 56 ++++++++++++++++++++++++++++++++++
rust/kernel/alloc/kvec.rs | 2 +-
2 files changed, 57 insertions(+), 1 deletion(-)
diff --git a/rust/kernel/alloc/allocator.rs b/rust/kernel/alloc/allocator.rs
index 63f271624428..a20111b40529 100644
--- a/rust/kernel/alloc/allocator.rs
+++ b/rust/kernel/alloc/allocator.rs
@@ -184,3 +184,59 @@ unsafe fn realloc(
unsafe { ReallocFunc::KVREALLOC.call(ptr, layout, old_layout, flags, nid) }
}
}
+
+#[macros::kunit_tests(rust_allocator)]
+mod tests {
+ use super::*;
+ use core::mem::MaybeUninit;
+ use kernel::prelude::*;
+
+ #[test]
+ fn test_alignment() -> Result {
+ const TEST_SIZE: usize = 1024;
+ const TEST_LARGE_ALIGN_SIZE: usize = kernel::page::PAGE_SIZE * 4;
+
+ // These two structs are used to test allocating aligned memory.
+ // they don't need to be accessed, so they're marked as dead_code.
+ #[expect(dead_code)]
+ #[repr(align(128))]
+ struct Blob([u8; TEST_SIZE]);
+ #[expect(dead_code)]
+ #[repr(align(8192))]
+ struct LargeAlignBlob([u8; TEST_LARGE_ALIGN_SIZE]);
+
+ struct TestAlign<T, A: Allocator>(Box<MaybeUninit<T>, A>);
+ impl<T, A: Allocator> TestAlign<T, A> {
+ fn new() -> Result<Self> {
+ Ok(Self(Box::<_, A>::new_uninit(GFP_KERNEL)?))
+ }
+
+ fn is_aligned_to(&self, align: usize) -> bool {
+ assert!(align.is_power_of_two());
+
+ let addr = self.0.as_ptr() as usize;
+ addr & (align - 1) == 0
+ }
+ }
+
+ let ta = TestAlign::<Blob, Kmalloc>::new()?;
+ assert!(ta.is_aligned_to(128));
+
+ let ta = TestAlign::<LargeAlignBlob, Kmalloc>::new()?;
+ assert!(ta.is_aligned_to(8192));
+
+ let ta = TestAlign::<Blob, Vmalloc>::new()?;
+ assert!(ta.is_aligned_to(128));
+
+ let ta = TestAlign::<LargeAlignBlob, Vmalloc>::new()?;
+ assert!(ta.is_aligned_to(8192));
+
+ let ta = TestAlign::<Blob, KVmalloc>::new()?;
+ assert!(ta.is_aligned_to(128));
+
+ let ta = TestAlign::<LargeAlignBlob, KVmalloc>::new()?;
+ assert!(ta.is_aligned_to(8192));
+
+ Ok(())
+ }
+}
diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs
index 92d0ed3f302e..f912a9642969 100644
--- a/rust/kernel/alloc/kvec.rs
+++ b/rust/kernel/alloc/kvec.rs
@@ -1301,7 +1301,7 @@ fn drop(&mut self) {
}
}
-#[macros::kunit_tests(rust_kvec_kunit)]
+#[macros::kunit_tests(rust_kvec)]
mod tests {
use super::*;
use crate::prelude::*;
--
2.43.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH v8 2/2] rust: alloc: kvec: add doc example for as_slice method
2025-07-31 8:42 [PATCH v8 0/2] Rust allocator and kvec improvements Hui Zhu
2025-07-31 8:42 ` [PATCH v8 1/2] rust: alloc: add alignment tests for allocators Hui Zhu
@ 2025-07-31 8:42 ` Hui Zhu
1 sibling, 0 replies; 4+ messages in thread
From: Hui Zhu @ 2025-07-31 8:42 UTC (permalink / raw)
To: Danilo Krummrich, Lorenzo Stoakes, Vlastimil Babka,
Liam R . Howlett, Uladzislau Rezki, Miguel Ojeda, Alex Gaynor,
Boqun Feng, Gary Guo, bjorn3_gh, Benno Lossin, Andreas Hindborg,
Alice Ryhl, Trevor Gross, rust-for-linux, linux-kernel, akpm,
vitaly.wool
Cc: Hui Zhu, Geliang Tang, Kunwu Chan, David Gow
From: Hui Zhu <zhuhui@kylinos.cn>
Add a practical usage example to the documentation of KVec::as_slice()
showing how to:
Create a new KVec.
Push elements into it.
Convert to a slice via as_slice().
Co-developed-by: Geliang Tang <geliang@kernel.org>
Signed-off-by: Geliang Tang <geliang@kernel.org>
Signed-off-by: Hui Zhu <zhuhui@kylinos.cn>
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Reviewed-by: Kunwu Chan <chentao@kylinos.cn>
Reviewed-by: David Gow <davidgow@google.com>
---
rust/kernel/alloc/kvec.rs | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs
index f912a9642969..d71f6a1513e2 100644
--- a/rust/kernel/alloc/kvec.rs
+++ b/rust/kernel/alloc/kvec.rs
@@ -224,6 +224,16 @@ unsafe fn dec_len(&mut self, count: usize) -> &mut [T] {
}
/// Returns a slice of the entire vector.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let mut v = KVec::new();
+ /// v.push(1, GFP_KERNEL)?;
+ /// v.push(2, GFP_KERNEL)?;
+ /// assert_eq!(v.as_slice(), &[1, 2]);
+ /// # Ok::<(), Error>(())
+ /// ```
#[inline]
pub fn as_slice(&self) -> &[T] {
self
--
2.43.0
^ permalink raw reply related [flat|nested] 4+ messages in thread