[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <32d7663b7d07d13564bdfb6a1ec4cde1be8b8f80.1753348867.git.zhuhui@kylinos.cn>
Date: Thu, 24 Jul 2025 17:25:34 +0800
From: Hui Zhu <hui.zhu@...ux.dev>
To: Danilo Krummrich <dakr@...nel.org>,
Lorenzo Stoakes <lorenzo.stoakes@...cle.com>,
Vlastimil Babka <vbabka@...e.cz>,
"Liam R . Howlett" <Liam.Howlett@...cle.com>,
Uladzislau Rezki <urezki@...il.com>,
Miguel Ojeda <ojeda@...nel.org>,
Alex Gaynor <alex.gaynor@...il.com>,
Boqun Feng <boqun.feng@...il.com>,
Gary Guo <gary@...yguo.net>,
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
Cc: Hui Zhu <zhuhui@...inos.cn>,
Geliang Tang <geliang@...nel.org>
Subject: [PATCH v4 1/2] rust: allocator: add unit tests of kmalloc, vmalloc and kvmalloc
From: Hui Zhu <zhuhui@...inos.cn>
Add KUnit test cases to validate the functionality of Rust allocation
wrappers (kmalloc, vmalloc, kvmalloc).
The tests include:
Basic allocation tests for each allocator using a 1024-byte Blob
structure initialized with a 0xfe pattern.
Large alignment (> PAGE_SIZE) allocation testing using an 8192-byte
aligned LargeAlignBlob structure.
Verification of allocation constraints:
- kmalloc successfully handles large alignments.
- vmalloc and kvmalloc correctly fail for unsupported large alignments.
Content verification through byte-by-byte pattern checking.
Co-developed-by: Geliang Tang <geliang@...nel.org>
Signed-off-by: Geliang Tang <geliang@...nel.org>
Signed-off-by: Hui Zhu <zhuhui@...inos.cn>
---
rust/kernel/alloc/allocator.rs | 57 ++++++++++++++++++++++++++++++++++
1 file changed, 57 insertions(+)
diff --git a/rust/kernel/alloc/allocator.rs b/rust/kernel/alloc/allocator.rs
index aa2dfa9dca4c..430d1f664fdf 100644
--- a/rust/kernel/alloc/allocator.rs
+++ b/rust/kernel/alloc/allocator.rs
@@ -187,3 +187,60 @@ unsafe fn realloc(
unsafe { ReallocFunc::KVREALLOC.call(ptr, layout, old_layout, flags) }
}
}
+
+#[macros::kunit_tests(rust_allocator_kunit)]
+mod tests {
+ use super::*;
+ use kernel::prelude::*;
+
+ const TEST_SIZE: usize = 1024;
+ const LARGE_ALIGN_TEST_SIZE: usize = kernel::page::PAGE_SIZE * 4;
+ #[repr(align(128))]
+ struct Blob([u8; TEST_SIZE]);
+ // This structure is used to test the allocation of alignments larger
+ // than PAGE_SIZE.
+ // Since this is not yet supported, avoid accessing the contents of
+ // the structure for now.
+ #[allow(dead_code)]
+ #[repr(align(8192))]
+ struct LargeAlignBlob([u8; LARGE_ALIGN_TEST_SIZE]);
+
+ #[test]
+ fn test_kmalloc() -> Result<(), AllocError> {
+ let blob = KBox::new(Blob([0xfeu8; TEST_SIZE]), GFP_KERNEL)?;
+ for b in blob.0.as_slice().iter() {
+ assert_eq!(*b, 0xfeu8);
+ }
+
+ let blob = KBox::new(LargeAlignBlob([0xfdu8; LARGE_ALIGN_TEST_SIZE]), GFP_KERNEL)?;
+ for b in blob.0.as_slice().iter() {
+ assert_eq!(*b, 0xfdu8);
+ }
+
+ Ok(())
+ }
+
+ #[test]
+ fn test_vmalloc() -> Result<(), AllocError> {
+ let blob = VBox::new(Blob([0xfeu8; TEST_SIZE]), GFP_KERNEL)?;
+ for b in blob.0.as_slice().iter() {
+ assert_eq!(*b, 0xfeu8);
+ }
+
+ assert!(VBox::<LargeAlignBlob>::new_uninit(GFP_KERNEL).is_err());
+
+ Ok(())
+ }
+
+ #[test]
+ fn test_kvmalloc() -> Result<(), AllocError> {
+ let blob = KVBox::new(Blob([0xfeu8; TEST_SIZE]), GFP_KERNEL)?;
+ for b in blob.0.as_slice().iter() {
+ assert_eq!(*b, 0xfeu8);
+ }
+
+ assert!(KVBox::<LargeAlignBlob>::new_uninit(GFP_KERNEL).is_err());
+
+ Ok(())
+ }
+}
--
2.43.0
Powered by blists - more mailing lists