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 2/3] rust: allocator: Vmalloc: Support alignments larger than PAGE_SIZE
Date: Tue, 15 Jul 2025 17:59:47 +0800 [thread overview]
Message-ID: <5e78317d87c42b548929ca386670837ad617fc9d.1752573305.git.zhuhui@kylinos.cn> (raw)
In-Reply-To: <cover.1752573305.git.zhuhui@kylinos.cn>
From: Hui Zhu <zhuhui@kylinos.cn>
This commit add code to make rust alloc Vmalloc support alignments
larger than PAGE_SIZE.
It adds a new option element to ReallocFunc. When an object supports
aligned reallocation, it can register its alignment-specific realloc
function here.
During VREALLOC initialization, it sets bindings::vrealloc_align to
this element.
When ReallocFunc::call executes, if the object supports aligned
reallocation and the alignment exceeds PAGE_SIZE, the aligned realloc
function is used to support alignment capabilities.
Co-developed-by: Geliang Tang <geliang@kernel.org>
Signed-off-by: Geliang Tang <geliang@kernel.org>
Signed-off-by: Hui Zhu <zhuhui@kylinos.cn>
---
rust/helpers/vmalloc.c | 7 +++++++
rust/kernel/alloc/allocator.rs | 32 ++++++++++++++++++++------------
2 files changed, 27 insertions(+), 12 deletions(-)
diff --git a/rust/helpers/vmalloc.c b/rust/helpers/vmalloc.c
index 80d34501bbc0..3290c4c4c42f 100644
--- a/rust/helpers/vmalloc.c
+++ b/rust/helpers/vmalloc.c
@@ -7,3 +7,10 @@ rust_helper_vrealloc(const void *p, size_t size, gfp_t flags)
{
return vrealloc(p, size, flags);
}
+
+void * __must_check __realloc_size(2)
+rust_helper_vrealloc_align(const void *p, size_t size, size_t align,
+ gfp_t flags)
+{
+ return vrealloc_align(p, size, align, flags);
+}
diff --git a/rust/kernel/alloc/allocator.rs b/rust/kernel/alloc/allocator.rs
index aa2dfa9dca4c..197222e15c26 100644
--- a/rust/kernel/alloc/allocator.rs
+++ b/rust/kernel/alloc/allocator.rs
@@ -59,17 +59,25 @@ fn aligned_size(new_layout: Layout) -> usize {
/// One of the following: `krealloc`, `vrealloc`, `kvrealloc`.
struct ReallocFunc(
unsafe extern "C" fn(*const crate::ffi::c_void, usize, u32) -> *mut crate::ffi::c_void,
+ Option<
+ unsafe extern "C" fn(
+ *const crate::ffi::c_void,
+ usize,
+ usize,
+ u32,
+ ) -> *mut crate::ffi::c_void,
+ >,
);
impl ReallocFunc {
// INVARIANT: `krealloc` satisfies the type invariants.
- const KREALLOC: Self = Self(bindings::krealloc);
+ const KREALLOC: Self = Self(bindings::krealloc, None);
// INVARIANT: `vrealloc` satisfies the type invariants.
- const VREALLOC: Self = Self(bindings::vrealloc);
+ const VREALLOC: Self = Self(bindings::vrealloc, Some(bindings::vrealloc_align));
// INVARIANT: `kvrealloc` satisfies the type invariants.
- const KVREALLOC: Self = Self(bindings::kvrealloc);
+ const KVREALLOC: Self = Self(bindings::kvrealloc, None);
/// # Safety
///
@@ -108,9 +116,15 @@ unsafe fn call(
// GUARANTEE:
// - `self.0` is one of `krealloc`, `vrealloc`, `kvrealloc`.
// - Those functions provide the guarantees of this function.
- let raw_ptr = unsafe {
- // If `size == 0` and `ptr != NULL` the memory behind the pointer is freed.
- self.0(ptr.cast(), size, flags.0).cast()
+ // If `size == 0` and `ptr != NULL` the memory behind the pointer is freed.
+ let raw_ptr = if let Some(f) = self.1 {
+ if layout.align() > bindings::PAGE_SIZE {
+ unsafe { f(ptr.cast(), size, layout.align(), flags.0).cast() }
+ } else {
+ unsafe { self.0(ptr.cast(), size, flags.0).cast() }
+ }
+ } else {
+ unsafe { self.0(ptr.cast(), size, flags.0).cast() }
};
let ptr = if size == 0 {
@@ -152,12 +166,6 @@ unsafe fn realloc(
old_layout: Layout,
flags: Flags,
) -> Result<NonNull<[u8]>, AllocError> {
- // TODO: Support alignments larger than PAGE_SIZE.
- if layout.align() > bindings::PAGE_SIZE {
- pr_warn!("Vmalloc does not support alignments larger than PAGE_SIZE yet.\n");
- return Err(AllocError);
- }
-
// SAFETY: If not `None`, `ptr` is guaranteed to point to valid memory, which was previously
// allocated with this `Allocator`.
unsafe { ReallocFunc::VREALLOC.call(ptr, layout, old_layout, flags) }
--
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] " 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 ` Hui Zhu [this message]
2025-07-15 9:59 ` [PATCH 3/3] rust: add a sample allocator usage Hui Zhu
2025-07-15 10:37 ` 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=5e78317d87c42b548929ca386670837ad617fc9d.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