Skip to content

Commit 8362c26

Browse files
Danilo Krummrichojeda
authored andcommitted
rust: alloc: implement KVmalloc allocator
Implement `Allocator` for `KVmalloc`, an `Allocator` that tries to allocate memory with `kmalloc` first and, on failure, falls back to `vmalloc`. All memory allocations made with `KVmalloc` end up in `kvrealloc_noprof()`; all frees in `kvfree()`. Reviewed-by: Alice Ryhl <aliceryhl@google.com> Reviewed-by: Benno Lossin <benno.lossin@proton.me> Reviewed-by: Gary Guo <gary@garyguo.net> Signed-off-by: Danilo Krummrich <dakr@kernel.org> Link: https://lore.kernel.org/r/20241004154149.93856-10-dakr@kernel.org [ Reworded typo. - Miguel ] Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
1 parent 61c0047 commit 8362c26

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

rust/helpers/slab.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,9 @@ rust_helper_krealloc(const void *objp, size_t new_size, gfp_t flags)
77
{
88
return krealloc(objp, new_size, flags);
99
}
10+
11+
void * __must_check __realloc_size(2)
12+
rust_helper_kvrealloc(const void *p, size_t size, gfp_t flags)
13+
{
14+
return kvrealloc(p, size, flags);
15+
}

rust/kernel/alloc/allocator.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ pub struct Kmalloc;
3434
/// For more details see [self].
3535
pub struct Vmalloc;
3636

37+
/// The kvmalloc kernel allocator.
38+
///
39+
/// `KVmalloc` attempts to allocate memory with `Kmalloc` first, but falls back to `Vmalloc` upon
40+
/// failure. This allocator is typically used when the size for the requested allocation is not
41+
/// known and may exceed the capabilities of `Kmalloc`.
42+
///
43+
/// For more details see [self].
44+
pub struct KVmalloc;
45+
3746
/// Returns a proper size to alloc a new object aligned to `new_layout`'s alignment.
3847
fn aligned_size(new_layout: Layout) -> usize {
3948
// Customized layouts from `Layout::from_size_align()` can have size < align, so pad first.
@@ -76,6 +85,9 @@ impl ReallocFunc {
7685
// INVARIANT: `vrealloc` satisfies the type invariants.
7786
const VREALLOC: Self = Self(bindings::vrealloc);
7887

88+
// INVARIANT: `kvrealloc` satisfies the type invariants.
89+
const KVREALLOC: Self = Self(bindings::kvrealloc);
90+
7991
/// # Safety
8092
///
8193
/// This method has the same safety requirements as [`Allocator::realloc`].
@@ -205,6 +217,30 @@ unsafe impl Allocator for Vmalloc {
205217
}
206218
}
207219

220+
// SAFETY: `realloc` delegates to `ReallocFunc::call`, which guarantees that
221+
// - memory remains valid until it is explicitly freed,
222+
// - passing a pointer to a valid memory allocation is OK,
223+
// - `realloc` satisfies the guarantees, since `ReallocFunc::call` has the same.
224+
unsafe impl Allocator for KVmalloc {
225+
#[inline]
226+
unsafe fn realloc(
227+
ptr: Option<NonNull<u8>>,
228+
layout: Layout,
229+
old_layout: Layout,
230+
flags: Flags,
231+
) -> Result<NonNull<[u8]>, AllocError> {
232+
// TODO: Support alignments larger than PAGE_SIZE.
233+
if layout.align() > bindings::PAGE_SIZE {
234+
pr_warn!("KVmalloc does not support alignments larger than PAGE_SIZE yet.\n");
235+
return Err(AllocError);
236+
}
237+
238+
// SAFETY: If not `None`, `ptr` is guaranteed to point to valid memory, which was previously
239+
// allocated with this `Allocator`.
240+
unsafe { ReallocFunc::KVREALLOC.call(ptr, layout, old_layout, flags) }
241+
}
242+
}
243+
208244
#[global_allocator]
209245
static ALLOCATOR: Kmalloc = Kmalloc;
210246

rust/kernel/alloc/allocator_test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use core::ptr::NonNull;
88

99
pub struct Kmalloc;
1010
pub type Vmalloc = Kmalloc;
11+
pub type KVmalloc = Kmalloc;
1112

1213
unsafe impl Allocator for Kmalloc {
1314
unsafe fn realloc(

0 commit comments

Comments
 (0)