rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Hui Zhu <hui.zhu@linux.dev>
To: Danilo Krummrich <dakr@kernel.org>
Cc: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>,
	Vlastimil Babka <vbabka@suse.cz>,
	"Liam R . Howlett" <Liam.Howlett@oracle.com>,
	Uladzislau Rezki <urezki@gmail.com>,
	Miguel Ojeda <ojeda@kernel.org>,
	Alex Gaynor <alex.gaynor@gmail.com>,
	Boqun Feng <boqun.feng@gmail.com>, Gary Guo <gary@garyguo.net>,
	bjorn3_gh@protonmail.com, Benno Lossin <lossin@kernel.org>,
	Andreas Hindborg <a.hindborg@kernel.org>,
	Alice Ryhl <aliceryhl@google.com>,
	Trevor Gross <tmgross@umich.edu>,
	rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
	Hui Zhu <zhuhui@kylinos.cn>, Geliang Tang <geliang@kernel.org>
Subject: Re: [PATCH v4 1/2] rust: allocator: add unit tests of kmalloc, vmalloc and kvmalloc
Date: Fri, 25 Jul 2025 15:05:29 +0800	[thread overview]
Message-ID: <aIMsuS5XTvKpr-a6@teawaterdeMacBook-Pro.local> (raw)
In-Reply-To: <DBKERZ03P2WS.3HQ0NZC9OO5AZ@kernel.org>

Hi Danilo,

On Thu, Jul 24, 2025 at 05:59:39PM +0200, Danilo Krummrich wrote:
> On Thu Jul 24, 2025 at 11:25 AM CEST, Hui Zhu wrote:
> > From: Hui Zhu <zhuhui@kylinos.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@kernel.org>
> > Signed-off-by: Geliang Tang <geliang@kernel.org>
> > Signed-off-by: Hui Zhu <zhuhui@kylinos.cn>
> 
> Thanks for the patch, additional test are always welcome! :)

Great!

> 
> > ---
> >  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)?;
> 
> Since those are now actual unit tests on the Allocator implementations, it would
> be fine to use them directly. However, for the case you are testing here, i.e.
> alignment using Box is perfectly fine.
> 
> Having that said, I wouldn't call those tests test_*malloc(), since they're not
> really testing all aspects of a certain allocator, but only the success to
> allocate with certain alignment arguments.
> 
> Instead, I propose to write just a single test, test_alignment(), with a few
> helper functions.
> 
> For instance, your Blob test structure could have a constructor that is generic
> over A: Allocator.
> 
> However, given that you really only want to check alignment, you probably want a
> structure like this instead.
> 
> 	struct TestAlign<A: Allocator>(Box<MaybeUninit<[u8; TEST_SIZE]>, A>);
> 
> 	impl<A: Allocator> TestAlign<A> {
> 	   fn new() -> Result<Self> {
> 	      Box::<_, A>::new_uninit(GFP_KERNEL)
> 	   }
> 
> 	   fn alignment_valid(&self) -> bool {
> 	      ...
> 	   }
> 	}
> 
> and then test_alignment() can just do
> 
> 	let test = TestAlign::<Kmalloc>::new()?;
> 	assert!(test.alignment_valid());
> 
> 	...
> 
> Given that this is now a unit test I also think that actually validating the
> alignment of the pointer Box wraps makes sense, similar to what you had in v2.
> 
> > +        for b in blob.0.as_slice().iter() {
> > +            assert_eq!(*b, 0xfeu8);
> > +        }
> 
> I don't think that this has any valid in the context of testing alignment.
> 
> > +        let blob = KBox::new(LargeAlignBlob([0xfdu8; LARGE_ALIGN_TEST_SIZE]), GFP_KERNEL)?;
> 
> For the large alignment case, you can consider to let TestAlign take a const
> generic, additional to A: Allocator, e.g.
> 
> 	struct TestAlign<A: Allocator, const SIZE: usize>(Box<MaybeUninit<[u8; SIZE]>, A>);
> 
> This way you can keep test_alignment() function very compact.
>

I sent new patches according to your comments.
 
> - Danilo

Best,
Hui

  reply	other threads:[~2025-07-25  7:05 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-24  9:25 [PATCH v4 0/2] rust: alloc: kvec doc example and allocator unit tests Hui Zhu
2025-07-24  9:25 ` [PATCH v4 1/2] rust: allocator: add unit tests of kmalloc, vmalloc and kvmalloc Hui Zhu
2025-07-24 15:59   ` Danilo Krummrich
2025-07-25  7:05     ` Hui Zhu [this message]
2025-07-24  9:25 ` [PATCH v4 2/2] rust: alloc: kvec: add doc example for as_slice method Hui Zhu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aIMsuS5XTvKpr-a6@teawaterdeMacBook-Pro.local \
    --to=hui.zhu@linux.dev \
    --cc=Liam.Howlett@oracle.com \
    --cc=a.hindborg@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=dakr@kernel.org \
    --cc=gary@garyguo.net \
    --cc=geliang@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lorenzo.stoakes@oracle.com \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tmgross@umich.edu \
    --cc=urezki@gmail.com \
    --cc=vbabka@suse.cz \
    --cc=zhuhui@kylinos.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).