From: "Danilo Krummrich" <dakr@kernel.org>
To: "Hui Zhu" <hui.zhu@linux.dev>
Cc: "Andrew Morton" <akpm@linux-foundation.org>,
"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>,
"Geliang Tang" <geliang@kernel.org>,
"Hui Zhu" <zhuhui@kylinos.cn>, <linux-kernel@vger.kernel.org>,
<linux-mm@kvack.org>, <rust-for-linux@vger.kernel.org>
Subject: Re: [PATCH 3/3] rust: add a sample allocator usage
Date: Tue, 15 Jul 2025 12:37:52 +0200 [thread overview]
Message-ID: <DBCKAOSOMXHB.3IEHVGIH7ZANN@kernel.org> (raw)
In-Reply-To: <ea067b4df1cef7f724a9e8ef0d345087f06ad6a7.1752573305.git.zhuhui@kylinos.cn>
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 })
> + }
> +}
next prev parent reply other threads:[~2025-07-15 10:38 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-15 9:59 [PATCH 0/3] rust: allocator: Vmalloc: Support alignments larger than PAGE_SIZE Hui Zhu
2025-07-15 9:59 ` [PATCH 1/3] vmalloc: Add vrealloc_align to support allocation of aligned vmap pages Hui Zhu
2025-07-15 23:19 ` kernel test robot
2025-07-16 7:02 ` Uladzislau Rezki
2025-07-15 9:59 ` [PATCH 2/3] rust: allocator: Vmalloc: Support alignments larger than PAGE_SIZE Hui Zhu
2025-07-15 9:59 ` [PATCH 3/3] rust: add a sample allocator usage Hui Zhu
2025-07-15 10:37 ` Danilo Krummrich [this message]
2025-07-17 10:02 ` Your Name
2025-07-15 10:21 ` [PATCH 0/3] rust: allocator: Vmalloc: Support alignments larger than PAGE_SIZE Danilo Krummrich
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=DBCKAOSOMXHB.3IEHVGIH7ZANN@kernel.org \
--to=dakr@kernel.org \
--cc=a.hindborg@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=gary@garyguo.net \
--cc=geliang@kernel.org \
--cc=hui.zhu@linux.dev \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
--cc=urezki@gmail.com \
--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).