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>
---
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..1f173038cec9 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_kunit)]
+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 alignment_valid(&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.alignment_valid(128));
+
+ let ta = TestAlign::<LargeAlignBlob, Kmalloc>::new()?;
+ assert!(ta.alignment_valid(8192));
+
+ let ta = TestAlign::<Blob, Vmalloc>::new()?;
+ assert!(ta.alignment_valid(128));
+
+ let ta = TestAlign::<LargeAlignBlob, Vmalloc>::new()?;
+ assert!(ta.alignment_valid(8192));
+
+ let ta = TestAlign::<Blob, KVmalloc>::new()?;
+ assert!(ta.alignment_valid(128));
+
+ let ta = TestAlign::<LargeAlignBlob, KVmalloc>::new()?;
+ assert!(ta.alignment_valid(8192));
+
+ Ok(())
+ }
+}
--
2.43.0
On Wed, Jul 30, 2025 at 5:37 AM Hui Zhu <hui.zhu@linux.dev> wrote:
>
> +#[macros::kunit_tests(rust_allocator_kunit)]
Is there any reason for the `_kunit` suffix? If not, then we should
avoid suffixing `_kunit` to every suite name.
I see we already have `rust_kernel_kunit`, but that one is because it
is the KUnit file itself.
There is also `rust_kvec_kunit`, but we should clean that one up.
> + fn test_alignment() -> Result<()> {
`-> Result` since the prelude is available.
> + fn alignment_valid(&self, align: usize) -> bool {
We typically prefix these with `is_`.
I would also call it `is_aligned_to`, to match the upstream Rust one,
which we could perhaps use if it becomes stable.
Cheers,
Miguel
2025年7月30日 17:16, "Miguel Ojeda" <miguel.ojeda.sandonis@gmail.com mailto:miguel.ojeda.sandonis@gmail.com?to=%22Miguel%20Ojeda%22%20%3Cmiguel.ojeda.sandonis%40gmail.com%3E > 写到:
>
> On Wed, Jul 30, 2025 at 5:37 AM Hui Zhu <hui.zhu@linux.dev> wrote:
>
> >
> > +#[macros::kunit_tests(rust_allocator_kunit)]
> >
> Is there any reason for the `_kunit` suffix? If not, then we should
> avoid suffixing `_kunit` to every suite name.
>
> I see we already have `rust_kernel_kunit`, but that one is because it
> is the KUnit file itself.
>
> There is also `rust_kvec_kunit`, but we should clean that one up.
>
> >
> > + fn test_alignment() -> Result<()> {
> >
> `-> Result` since the prelude is available.
>
> >
> > + fn alignment_valid(&self, align: usize) -> bool {
> >
> We typically prefix these with `is_`.
>
> I would also call it `is_aligned_to`, to match the upstream Rust one,
> which we could perhaps use if it becomes stable.
>
Hi Miguel,
I sent the v7 version according to your comments.
Thanks,
Hui
> Cheers,
> Miguel
>
On Wed, Jul 30, 2025 at 11:35:21AM +0800, 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: Alice Ryhl <aliceryhl@google.com>
© 2016 - 2026 Red Hat, Inc.