Skip to content

Commit 3340149

Browse files
Danilo Krummrichgregkh
authored andcommitted
rust: alloc: replace aligned_size() with Kmalloc::aligned_layout()
[ Upstream commit fde578c ] aligned_size() dates back to when Rust did support kmalloc() only, but is now used in ReallocFunc::call() and hence for all allocators. However, the additional padding applied by aligned_size() is only required by the kmalloc() allocator backend. Hence, replace aligned_size() with Kmalloc::aligned_layout() and use it for the affected allocators, i.e. kmalloc() and kvmalloc(), only. While at it, make Kmalloc::aligned_layout() public, such that Rust abstractions, which have to call subsystem specific kmalloc() based allocation primitives directly, can make use of it. Fixes: 8a79983 ("rust: alloc: implement `ReallocFunc`") Reviewed-by: Alice Ryhl <aliceryhl@google.com> Link: https://lore.kernel.org/r/20250731154919.4132-2-dakr@kernel.org [ Remove `const` from Kmalloc::aligned_layout(). - Danilo ] Signed-off-by: Danilo Krummrich <dakr@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 6b14c9c commit 3340149

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

rust/kernel/alloc/allocator.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,6 @@ pub struct Vmalloc;
4343
/// For more details see [self].
4444
pub struct KVmalloc;
4545

46-
/// Returns a proper size to alloc a new object aligned to `new_layout`'s alignment.
47-
fn aligned_size(new_layout: Layout) -> usize {
48-
// Customized layouts from `Layout::from_size_align()` can have size < align, so pad first.
49-
let layout = new_layout.pad_to_align();
50-
51-
// Note that `layout.size()` (after padding) is guaranteed to be a multiple of `layout.align()`
52-
// which together with the slab guarantees means the `krealloc` will return a properly aligned
53-
// object (see comments in `kmalloc()` for more information).
54-
layout.size()
55-
}
56-
5746
/// # Invariants
5847
///
5948
/// One of the following: `krealloc`, `vrealloc`, `kvrealloc`.
@@ -88,7 +77,7 @@ impl ReallocFunc {
8877
old_layout: Layout,
8978
flags: Flags,
9079
) -> Result<NonNull<[u8]>, AllocError> {
91-
let size = aligned_size(layout);
80+
let size = layout.size();
9281
let ptr = match ptr {
9382
Some(ptr) => {
9483
if old_layout.size() == 0 {
@@ -123,6 +112,17 @@ impl ReallocFunc {
123112
}
124113
}
125114

115+
impl Kmalloc {
116+
/// Returns a [`Layout`] that makes [`Kmalloc`] fulfill the requested size and alignment of
117+
/// `layout`.
118+
pub fn aligned_layout(layout: Layout) -> Layout {
119+
// Note that `layout.size()` (after padding) is guaranteed to be a multiple of
120+
// `layout.align()` which together with the slab guarantees means that `Kmalloc` will return
121+
// a properly aligned object (see comments in `kmalloc()` for more information).
122+
layout.pad_to_align()
123+
}
124+
}
125+
126126
// SAFETY: `realloc` delegates to `ReallocFunc::call`, which guarantees that
127127
// - memory remains valid until it is explicitly freed,
128128
// - passing a pointer to a valid memory allocation is OK,
@@ -135,6 +135,8 @@ unsafe impl Allocator for Kmalloc {
135135
old_layout: Layout,
136136
flags: Flags,
137137
) -> Result<NonNull<[u8]>, AllocError> {
138+
let layout = Kmalloc::aligned_layout(layout);
139+
138140
// SAFETY: `ReallocFunc::call` has the same safety requirements as `Allocator::realloc`.
139141
unsafe { ReallocFunc::KREALLOC.call(ptr, layout, old_layout, flags) }
140142
}
@@ -176,6 +178,10 @@ unsafe impl Allocator for KVmalloc {
176178
old_layout: Layout,
177179
flags: Flags,
178180
) -> Result<NonNull<[u8]>, AllocError> {
181+
// `KVmalloc` may use the `Kmalloc` backend, hence we have to enforce a `Kmalloc`
182+
// compatible layout.
183+
let layout = Kmalloc::aligned_layout(layout);
184+
179185
// TODO: Support alignments larger than PAGE_SIZE.
180186
if layout.align() > bindings::PAGE_SIZE {
181187
pr_warn!("KVmalloc does not support alignments larger than PAGE_SIZE yet.\n");

0 commit comments

Comments
 (0)