Skip to content

Commit 61c0047

Browse files
Danilo Krummrichojeda
authored andcommitted
rust: alloc: implement Vmalloc allocator
Implement `Allocator` for `Vmalloc`, the kernel's virtually contiguous allocator, typically used for larger objects, (much) larger than page size. All memory allocations made with `Vmalloc` end up in `vrealloc()`. 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-9-dakr@kernel.org Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
1 parent 5a888c2 commit 61c0047

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

rust/helpers/helpers.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@
2222
#include "spinlock.c"
2323
#include "task.c"
2424
#include "uaccess.c"
25+
#include "vmalloc.c"
2526
#include "wait.c"
2627
#include "workqueue.c"

rust/helpers/vmalloc.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <linux/vmalloc.h>
4+
5+
void * __must_check __realloc_size(2)
6+
rust_helper_vrealloc(const void *p, size_t size, gfp_t flags)
7+
{
8+
return vrealloc(p, size, flags);
9+
}

rust/kernel/alloc/allocator.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use core::ptr::NonNull;
1515

1616
use crate::alloc::{AllocError, Allocator};
1717
use crate::bindings;
18+
use crate::pr_warn;
1819

1920
/// The contiguous kernel allocator.
2021
///
@@ -24,6 +25,15 @@ use crate::bindings;
2425
/// For more details see [self].
2526
pub struct Kmalloc;
2627

28+
/// The virtually contiguous kernel allocator.
29+
///
30+
/// `Vmalloc` allocates pages from the page level allocator and maps them into the contiguous kernel
31+
/// virtual space. It is typically used for large allocations. The memory allocated with this
32+
/// allocator is not physically contiguous.
33+
///
34+
/// For more details see [self].
35+
pub struct Vmalloc;
36+
2737
/// Returns a proper size to alloc a new object aligned to `new_layout`'s alignment.
2838
fn aligned_size(new_layout: Layout) -> usize {
2939
// Customized layouts from `Layout::from_size_align()` can have size < align, so pad first.
@@ -63,6 +73,9 @@ impl ReallocFunc {
6373
// INVARIANT: `krealloc` satisfies the type invariants.
6474
const KREALLOC: Self = Self(bindings::krealloc);
6575

76+
// INVARIANT: `vrealloc` satisfies the type invariants.
77+
const VREALLOC: Self = Self(bindings::vrealloc);
78+
6679
/// # Safety
6780
///
6881
/// This method has the same safety requirements as [`Allocator::realloc`].
@@ -168,6 +181,30 @@ unsafe impl GlobalAlloc for Kmalloc {
168181
}
169182
}
170183

184+
// SAFETY: `realloc` delegates to `ReallocFunc::call`, which guarantees that
185+
// - memory remains valid until it is explicitly freed,
186+
// - passing a pointer to a valid memory allocation is OK,
187+
// - `realloc` satisfies the guarantees, since `ReallocFunc::call` has the same.
188+
unsafe impl Allocator for Vmalloc {
189+
#[inline]
190+
unsafe fn realloc(
191+
ptr: Option<NonNull<u8>>,
192+
layout: Layout,
193+
old_layout: Layout,
194+
flags: Flags,
195+
) -> Result<NonNull<[u8]>, AllocError> {
196+
// TODO: Support alignments larger than PAGE_SIZE.
197+
if layout.align() > bindings::PAGE_SIZE {
198+
pr_warn!("Vmalloc does not support alignments larger than PAGE_SIZE yet.\n");
199+
return Err(AllocError);
200+
}
201+
202+
// SAFETY: If not `None`, `ptr` is guaranteed to point to valid memory, which was previously
203+
// allocated with this `Allocator`.
204+
unsafe { ReallocFunc::VREALLOC.call(ptr, layout, old_layout, flags) }
205+
}
206+
}
207+
171208
#[global_allocator]
172209
static ALLOCATOR: Kmalloc = Kmalloc;
173210

rust/kernel/alloc/allocator_test.rs

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

99
pub struct Kmalloc;
10+
pub type Vmalloc = Kmalloc;
1011

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

0 commit comments

Comments
 (0)