Skip to content

Commit 865d50e

Browse files
Danilo Krummrichfbq
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 <[email protected]> Signed-off-by: Danilo Krummrich <[email protected]> Link: https://lore.kernel.org/r/[email protected] [boqun: Resolve conflicts with helper split]
1 parent fa20251 commit 865d50e

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

rust/helpers/slab.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
// SPDX-License-Identifier: GPL-2.0
22

33
#include <linux/slab.h>
4+
// TODO: Move vrealloc() to a different file or rename this file.
5+
#include <linux/vmalloc.h>
46

57
void * __must_check __realloc_size(2)
68
rust_helper_krealloc(const void *objp, size_t new_size, gfp_t flags)
79
{
810
return krealloc(objp, new_size, flags);
911
}
12+
13+
// TODO: Move vrealloc() to a different file or rename this file.
14+
void * __must_check __realloc_size(2)
15+
rust_helper_vrealloc(const void *p, size_t size, gfp_t flags)
16+
{
17+
return vrealloc(p, size, flags);
18+
}

rust/kernel/alloc/allocator.rs

Lines changed: 32 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`].
@@ -141,6 +154,25 @@ unsafe impl GlobalAlloc for Kmalloc {
141154
}
142155
}
143156

157+
unsafe impl Allocator for Vmalloc {
158+
#[inline]
159+
unsafe fn realloc(
160+
ptr: Option<NonNull<u8>>,
161+
layout: Layout,
162+
flags: Flags,
163+
) -> Result<NonNull<[u8]>, AllocError> {
164+
// TODO: Support alignments larger than PAGE_SIZE.
165+
if layout.align() > bindings::PAGE_SIZE {
166+
pr_warn!("Vmalloc does not support alignments larger than PAGE_SIZE yet.\n");
167+
return Err(AllocError);
168+
}
169+
170+
// SAFETY: If not `None`, `ptr` is guaranteed to point to valid memory, which was previously
171+
// allocated with this `Allocator`.
172+
unsafe { ReallocFunc::VREALLOC.call(ptr, layout, flags) }
173+
}
174+
}
175+
144176
#[global_allocator]
145177
static ALLOCATOR: Kmalloc = Kmalloc;
146178

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)