From: Hui Zhu <hui.zhu@linux.dev>
To: 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>,
Danilo Krummrich <dakr@kernel.org>,
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: [PATCH 3/3] rust: add a sample allocator usage
Date: Tue, 15 Jul 2025 17:59:48 +0800 [thread overview]
Message-ID: <ea067b4df1cef7f724a9e8ef0d345087f06ad6a7.1752573305.git.zhuhui@kylinos.cn> (raw)
In-Reply-To: <cover.1752573305.git.zhuhui@kylinos.cn>
From: Hui Zhu <zhuhui@kylinos.cn>
Add a sample to the samples memory allocator usage.
Co-developed-by: Geliang Tang <geliang@kernel.org>
Signed-off-by: Geliang Tang <geliang@kernel.org>
Signed-off-by: Hui Zhu <zhuhui@kylinos.cn>
---
samples/rust/Kconfig | 10 ++++
samples/rust/Makefile | 1 +
samples/rust/rust_allocator.rs | 104 +++++++++++++++++++++++++++++++++
3 files changed, 115 insertions(+)
create mode 100644 samples/rust/rust_allocator.rs
diff --git a/samples/rust/Kconfig b/samples/rust/Kconfig
index 7f7371a004ee..79c73f6c5216 100644
--- a/samples/rust/Kconfig
+++ b/samples/rust/Kconfig
@@ -105,6 +105,16 @@ config SAMPLE_RUST_DRIVER_AUXILIARY
If unsure, say N.
+config SAMPLE_RUST_ALLOCATOR
+ tristate "Allocator Test Driver"
+ help
+ This option builds the Rust allocator Test driver sample.
+
+ To compile this as a module, choose M here:
+ the module will be called rust_dma.
+
+ If unsure, say N.
+
config SAMPLE_RUST_HOSTPROGS
bool "Host programs"
help
diff --git a/samples/rust/Makefile b/samples/rust/Makefile
index bd2faad63b4f..b378959eab19 100644
--- a/samples/rust/Makefile
+++ b/samples/rust/Makefile
@@ -10,6 +10,7 @@ obj-$(CONFIG_SAMPLE_RUST_DRIVER_PLATFORM) += rust_driver_platform.o
obj-$(CONFIG_SAMPLE_RUST_DRIVER_FAUX) += rust_driver_faux.o
obj-$(CONFIG_SAMPLE_RUST_DRIVER_AUXILIARY) += rust_driver_auxiliary.o
obj-$(CONFIG_SAMPLE_RUST_CONFIGFS) += rust_configfs.o
+obj-$(CONFIG_SAMPLE_RUST_ALLOCATOR) += rust_allocator.o
rust_print-y := rust_print_main.o rust_print_events.o
diff --git a/samples/rust/rust_allocator.rs b/samples/rust/rust_allocator.rs
new file mode 100644
index 000000000000..13d23cc9d682
--- /dev/null
+++ b/samples/rust/rust_allocator.rs
@@ -0,0 +1,104 @@
+// SPDX-License-Identifier: GPL-2.0
+
+// Copyright (c) 2025, Kylin Software
+
+//! Rust allocator sample.
+
+use core::{alloc::Layout, ptr::NonNull};
+use kernel::alloc::allocator;
+use kernel::alloc::Allocator;
+use kernel::bindings;
+use kernel::prelude::*;
+
+module! {
+ type: RustAllocator,
+ name: "rust_allocator",
+ authors: ["Rust for Linux Contributors"],
+ description: "Rust allocator sample",
+ license: "GPL",
+}
+
+const VMALLOC_ARG: [(usize, usize); 2] = [
+ (bindings::PAGE_SIZE * 4, bindings::PAGE_SIZE * 2),
+ (1024, 128),
+];
+
+struct RustAllocator {
+ vmalloc_vec: KVec<(usize, Layout)>,
+}
+
+fn vmalloc_align(size: usize, align: usize) -> Result<(NonNull<[u8]>, Layout)> {
+ let layout = Layout::from_size_align(size, align).map_err(|_| EINVAL)?;
+
+ Ok((
+ <allocator::Vmalloc as Allocator>::alloc(layout, GFP_KERNEL).map_err(|_| EINVAL)?,
+ layout,
+ ))
+}
+
+fn vfree(addr: usize, layout: Layout) {
+ let vmalloc_ptr = NonNull::new(addr as *mut u8);
+ if let Some(ptr) = vmalloc_ptr {
+ unsafe {
+ <allocator::Vmalloc as Allocator>::free(ptr, layout);
+ }
+ } else {
+ pr_err!("Failed to vfree: pointer is null\n");
+ }
+}
+
+fn check_ptr(ptr: NonNull<[u8]>, size: usize, align: usize) -> (usize, bool) {
+ let current_size = unsafe { ptr.as_ref().len() };
+ if current_size != size {
+ pr_err!(
+ "The length to be allocated is {}, and the actually allocated memory length is {}.\n",
+ size,
+ current_size
+ );
+ return (0, false);
+ }
+
+ let addr = ptr.cast::<u8>().as_ptr() as usize;
+ debug_assert!(align.is_power_of_two());
+ if addr & (align - 1) != 0 {
+ pr_err!("Address {:#x} is not aligned with {:#x}.\n", addr, align);
+ return (0, false);
+ }
+
+ (addr, true)
+}
+
+fn clear_vmalloc_vec(v: &KVec<(usize, Layout)>) {
+ for (addr, layout) in v {
+ vfree(*addr, *layout);
+ }
+}
+
+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)?;
+
+ 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 })
+ }
+}
+
+impl Drop for RustAllocator {
+ fn drop(&mut self) {
+ pr_info!("Rust allocator sample (exit)\n");
+
+ clear_vmalloc_vec(&self.vmalloc_vec);
+ }
+}
--
2.43.0
next prev parent reply other threads:[~2025-07-15 10:00 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 ` Hui Zhu [this message]
2025-07-15 10:37 ` [PATCH 3/3] rust: add a sample allocator usage Danilo Krummrich
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=ea067b4df1cef7f724a9e8ef0d345087f06ad6a7.1752573305.git.zhuhui@kylinos.cn \
--to=hui.zhu@linux.dev \
--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=dakr@kernel.org \
--cc=gary@garyguo.net \
--cc=geliang@kernel.org \
--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).