[PATCH v7 1/3] rust: allocator: add KUnit tests for alignment guarantees

Hui Zhu posted 3 patches 2 months ago
There is a newer version of this series
[PATCH v7 1/3] rust: allocator: add KUnit tests for alignment guarantees
Posted by Hui Zhu 2 months ago
From: Hui Zhu <zhuhui@kylinos.cn>

Add a test module to verify memory alignment guarantees for Rust kernel
allocators.
The tests cover `Kmalloc`, `Vmalloc` and `KVmalloc` allocators
with both standard and large page-aligned allocations.

Key features of the tests:
1. Creates alignment-constrained types:
   - 128-byte aligned `Blob`
   - 8192-byte (4-page) aligned `LargeAlignBlob`
2. Validates allocators using `TestAlign` helper which:
   - Checks address alignment masks
   - Supports uninitialized allocations
3. Tests all three allocators with both alignment requirements:
   - Kmalloc with 128B and 8192B
   - Vmalloc with 128B and 8192B
   - KVmalloc with 128B and 8192B

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>
---
 rust/kernel/alloc/allocator.rs | 56 ++++++++++++++++++++++++++++++++++
 1 file changed, 56 insertions(+)

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(())
+    }
+}
-- 
2.43.0
Re: [PATCH v7 1/3] rust: allocator: add KUnit tests for alignment guarantees
Posted by Danilo Krummrich 2 months ago
On Thu Jul 31, 2025 at 4:50 AM CEST, Hui Zhu wrote:
> From: Hui Zhu <zhuhui@kylinos.cn>
>
> Add a test module to verify memory alignment guarantees for Rust kernel
> allocators.
> The tests cover `Kmalloc`, `Vmalloc` and `KVmalloc` allocators
> with both standard and large page-aligned allocations.
>
> Key features of the tests:
> 1. Creates alignment-constrained types:
>    - 128-byte aligned `Blob`
>    - 8192-byte (4-page) aligned `LargeAlignBlob`
> 2. Validates allocators using `TestAlign` helper which:
>    - Checks address alignment masks
>    - Supports uninitialized allocations
> 3. Tests all three allocators with both alignment requirements:
>    - Kmalloc with 128B and 8192B
>    - Vmalloc with 128B and 8192B
>    - KVmalloc with 128B and 8192B
>
> 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>

Acked-by: Danilo Krummrich <dakr@kernel.org>

@Andrew: Can you please pick this one up once we land Vitaly's series [1]? I'll
take the other two patches of the series through the Rust alloc tree once -rc1
is out.

[1] https://lore.kernel.org/lkml/20250730191921.352591-1-vitaly.wool@konsulko.se/
Re: [PATCH v7 1/3] rust: allocator: add KUnit tests for alignment guarantees
Posted by Danilo Krummrich 1 month, 3 weeks ago
On Thu Jul 31, 2025 at 11:19 AM CEST, Danilo Krummrich wrote:
> On Thu Jul 31, 2025 at 4:50 AM CEST, Hui Zhu wrote:
>> From: Hui Zhu <zhuhui@kylinos.cn>
>>
>> Add a test module to verify memory alignment guarantees for Rust kernel
>> allocators.
>> The tests cover `Kmalloc`, `Vmalloc` and `KVmalloc` allocators
>> with both standard and large page-aligned allocations.
>>
>> Key features of the tests:
>> 1. Creates alignment-constrained types:
>>    - 128-byte aligned `Blob`
>>    - 8192-byte (4-page) aligned `LargeAlignBlob`
>> 2. Validates allocators using `TestAlign` helper which:
>>    - Checks address alignment masks
>>    - Supports uninitialized allocations
>> 3. Tests all three allocators with both alignment requirements:
>>    - Kmalloc with 128B and 8192B
>>    - Vmalloc with 128B and 8192B
>>    - KVmalloc with 128B and 8192B
>>
>> 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>
>
> Acked-by: Danilo Krummrich <dakr@kernel.org>
>
> @Andrew: Can you please pick this one up once we land Vitaly's series [1]? I'll
> take the other two patches of the series through the Rust alloc tree once -rc1
> is out.

Just a reminder for this one to be picked up; I gave it a quick shot on top of
mm-everything and everything looks fine..

> [1] https://lore.kernel.org/lkml/20250730191921.352591-1-vitaly.wool@konsulko.se/