[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <aHjKMfPAsVeuUrlk@teawaterdeMacBook-Pro.local>
Date: Thu, 17 Jul 2025 18:02:47 +0800
From: Your Name <hui.zhu@...ux.dev>
To: Danilo Krummrich <dakr@...nel.org>
Cc: Andrew Morton <akpm@...ux-foundation.org>,
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>,
Geliang Tang <geliang@...nel.org>, Hui Zhu <zhuhui@...inos.cn>,
linux-kernel@...r.kernel.org, linux-mm@...ck.org,
rust-for-linux@...r.kernel.org
Subject: Re: [PATCH 3/3] rust: add a sample allocator usage
Hi Danilo,
Thanks for your help.
On Tue, Jul 15, 2025 at 12:37:52PM +0200, Danilo Krummrich wrote:
> On Tue Jul 15, 2025 at 11:59 AM CEST, Hui Zhu wrote:
> > +impl kernel::Module for RustAllocator {
> > + fn init(_module: &'static ThisModule) -> Result<Self> {
> > + pr_info!("Rust allocator sample (init)\n");
> > +
> > + let mut vmalloc_vec = KVec::new();
> > + for (size, align) in VMALLOC_ARG {
> > + let (ptr, layout) = vmalloc_align(size, align)?;
>
> Ok, I think I get the idea, you want to demonstrate how to use the Allocator
> trait for raw memory allocations.
>
> However, doing so is discouraged unless there's really no other way. One obvious
> example are Rust's own memory allocation primitives, such as Box and Vec.
>
> So, instead of this raw allocation, you can just use VBox::new() or
> VBox::new_uninit() in the following way.
>
> [repr(align(ALIGN))]
> struct Blob([u8; SIZE]);
>
> // Creates a vmalloc allocation of size `SIZE` with an alignment of
> // `ALIGN`. The allocation is freed once `b` is dropped.
> let b = VBox::<Blob>::new_uninit(GFP_KERNEL)?;
>
> This way you don't have to handle the layout and the Allocator type yourself and
> you also don't have to care about explicitly calling vfree(), VBox does all this
> for you.
>
> > +
> > + let (addr, is_ok) = check_ptr(ptr, size, align);
> > + if !is_ok {
> > + clear_vmalloc_vec(&vmalloc_vec);
> > + return Err(EINVAL);
> > + }
> > +
> > + vmalloc_vec.push((addr, layout), GFP_KERNEL)?;
> > + }
> > +
> > + Ok(RustAllocator { vmalloc_vec })
> > + }
> > +}
>
I sent version v2.
It only included the sample code and updated to use VBox according to your comments.
Best,
Hui
Powered by blists - more mailing lists