Rust allocator and kvec improvements
Subsystems (2)
v8 cover Rust allocator and kvec improvements
From: Hui Zhu <[email protected]>
This series adds tests and docs for Rust kernel components:
Patch 1 adds KUnit tests for allocator alignment guarantees. and simplifies KVec test module naming convention. Patch 2 documents KVec::as_slice with a usage example.
Both patches are co-developed with Geliang Tang. Based on [1]. Tested on x86_64 using KUnit.
Changelog:
v8:
Updated according to the comments of David.
v7:
Updated according to the comments of Miguel.
v6:
According to the comments of Danilo, updated test for allocator.rs and allocator rebase onto [1].
v5:
According to the comments of Danilo, change to use generic struct and allocator Generics in allocator.rs.
v4:
According to the comments of, add the error check for push. v3:
According to the comments of Danilo and Boqun, move KVec test to doc example and move VBox to allocator unit tests.
v2:
According to the comments of Danilo, updated the commit to samples the usage of VBox and KVec.
[1] https://lore.kernel.org/lkml/[email protected]/
Hui Zhu (2):
rust: alloc: add alignment tests for allocators
rust: alloc: kvec: add doc example for as_slice method
rust/kernel/alloc/allocator.rs | 56 ++++++++++++++++++++++++++++++++++ rust/kernel/alloc/kvec.rs | 12 +++++++- 2 files changed, 67 insertions(+), 1 deletion(-)
Signature · 1 line
2.43.0
PATCH v8 1/2 rust: alloc: add alignment tests for allocators
From: Hui Zhu <[email protected]>
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.
--- rust/kernel/alloc/allocator.rs | 56 ++++++++++++++++++++++++++++++++++ rust/kernel/alloc/kvec.rs | 2 +- 2 files changed, 57 insertions(+), 1 deletion(-)
rust/kernel/alloc/allocator.rs +56 -0
@@ -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 (()) + } +}
rust/kernel/alloc/kvec.rs +1 -1
@@ -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::*;
Signature · 1 line
2.43.0
Danilo Krummrich
maintainerRe: [PATCH v8 1/2] rust: alloc: add alignment tests for allocators
Quoted 15 lines · Hui Zhu
rust/kernel/alloc/allocator.rs +7
-0@@ -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::*; +
<snip>
rust/kernel/alloc/allocator.rs
Quoted 5 lines
rust/kernel/alloc/allocator.rs +1
-1-#[macros::kunit_tests(rust_kvec_kunit)] +#[macros::kunit_tests(rust_kvec)] mod tests {use super ::*;use crate ::prelude::*;
I think v7 was fine. David probably confused "rust_allocator" with "rust_kvec". Renaming the kvec kunit tests should, as you correctly did in v7, indeed be a separate patch.
No need to resend now, I think we can just pick v7.
In general, please send new version a little bit less frequent to leave some time for people to comment.
Thanks,
Danilo
PATCH v8 2/2 rust: alloc: kvec: add doc example for as_slice method
In reply to Hui ZhuFrom: Hui Zhu <[email protected]>
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().
--- rust/kernel/alloc/kvec.rs | 10 ++++++++++ 1 file changed, 10 insertions(+)
rust/kernel/alloc/kvec.rs +10 -0
@@ -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
Signature · 1 line
2.43.0