Skip to content

Commit 6c6f2cb

Browse files
Danilo Krummrichfbq
authored andcommitted
rust: alloc: implement KVmalloc allocator
Implement `Allocator` for `KVmalloc`, an `Allocator` that tries to allocate memory wth `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> Signed-off-by: Danilo Krummrich <dakr@kernel.org> Reviewed-by: Benno Lossin <benno.lossin@proton.me> Link: https://lore.kernel.org/r/20240911225449.152928-8-dakr@kernel.org
1 parent bf39195 commit 6c6f2cb

3 files changed

Lines changed: 42 additions & 0 deletions

File tree

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: 35 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.
@@ -82,6 +91,9 @@ impl ReallocFunc {
8291
// INVARIANT: `vrealloc` satisfies the type invariants.
8392
const VREALLOC: Self = Self(bindings::vrealloc);
8493

94+
// INVARIANT: `kvrealloc` satisfies the type invariants.
95+
const KVREALLOC: Self = Self(bindings::kvrealloc);
96+
8597
/// # Safety
8698
///
8799
/// This method has the same safety requirements as [`Allocator::realloc`].
@@ -200,6 +212,29 @@ unsafe impl Allocator for Vmalloc {
200212
}
201213
}
202214

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

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)