[PATCH 04/20] rust: alloc: implement `Allocator` for `Kmalloc`
From: Danilo Krummrich
Date: Thu Jul 04 2024 - 13:09:49 EST
Implement `Allocator` for `Kmalloc`, the kernel's default allocator,
typically used for objects smaller than page size.
All memory allocations made with `Kmalloc` end up in `krealloc()`.
It serves as the default allocator for the subsequently introduced types
`KBox` and `KVec`.
Signed-off-by: Danilo Krummrich <dakr@xxxxxxxxxx>
---
rust/kernel/alloc.rs | 2 +-
rust/kernel/alloc/allocator.rs | 74 ++++++++++++++++++++++++++++------
2 files changed, 63 insertions(+), 13 deletions(-)
diff --git a/rust/kernel/alloc.rs b/rust/kernel/alloc.rs
index 462e00982510..8d79cc95dc1e 100644
--- a/rust/kernel/alloc.rs
+++ b/rust/kernel/alloc.rs
@@ -4,7 +4,7 @@
#[cfg(not(test))]
#[cfg(not(testlib))]
-mod allocator;
+pub mod allocator;
pub mod box_ext;
pub mod vec_ext;
diff --git a/rust/kernel/alloc/allocator.rs b/rust/kernel/alloc/allocator.rs
index e47ab8fe21ab..b7c0490f6415 100644
--- a/rust/kernel/alloc/allocator.rs
+++ b/rust/kernel/alloc/allocator.rs
@@ -5,9 +5,18 @@
use super::{flags::*, Flags};
use core::alloc::{GlobalAlloc, Layout};
use core::ptr;
+use core::ptr::NonNull;
-struct Kmalloc;
+use crate::alloc::{AllocError, Allocator};
+use crate::bindings;
+/// The contiguous kernel allocator.
+///
+/// The contiguous kernel allocator only ever allocates physically contiguous memory through
+/// `bindings::krealloc`.
+pub struct Kmalloc;
+
+/// Returns a proper size to alloc a new object aligned to `new_layout`'s alignment.
fn aligned_size(new_layout: Layout) -> usize {
// Customized layouts from `Layout::from_size_align()` can have size < align, so pad first.
let layout = new_layout.pad_to_align();
@@ -27,7 +36,7 @@ fn aligned_size(new_layout: Layout) -> usize {
size
}
-/// Calls `krealloc` with a proper size to alloc a new object aligned to `new_layout`'s alignment.
+/// Calls `krealloc` with a proper size to alloc a new object.
///
/// # Safety
///
@@ -48,20 +57,54 @@ pub(crate) unsafe fn krealloc_aligned(ptr: *mut u8, new_layout: Layout, flags: F
}
}
+unsafe impl Allocator for Kmalloc {
+ unsafe fn realloc(
+ &self,
+ old_ptr: *mut u8,
+ _old_size: usize,
+ layout: Layout,
+ flags: Flags,
+ ) -> Result<NonNull<[u8]>, AllocError> {
+ let size = aligned_size(layout);
+
+ // SAFETY: `src` is guaranteed to point to valid memory with a size of at least
+ // `old_size`, which was previously allocated with this `Allocator` or NULL.
+ let raw_ptr = unsafe {
+ // If `size == 0` and `old_ptr != NULL` `krealloc()` frees the memory behind the
+ // pointer.
+ bindings::krealloc(old_ptr.cast(), size, flags.0).cast()
+ };
+
+ let ptr = if size == 0 {
+ NonNull::dangling()
+ } else {
+ NonNull::new(raw_ptr).ok_or(AllocError)?
+ };
+
+ Ok(NonNull::slice_from_raw_parts(ptr, size))
+ }
+}
+
unsafe impl GlobalAlloc for Kmalloc {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
- // SAFETY: `ptr::null_mut()` is null and `layout` has a non-zero size by the function safety
- // requirement.
- unsafe { krealloc_aligned(ptr::null_mut(), layout, GFP_KERNEL) }
+ let this: &dyn Allocator = self;
+
+ match this.alloc(layout, GFP_KERNEL) {
+ Ok(ptr) => ptr.as_ptr().cast(),
+ Err(_) => ptr::null_mut(),
+ }
}
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
- unsafe {
- bindings::kfree(ptr as *const core::ffi::c_void);
- }
+ // SAFETY: The safety requirements of `dealloc` are a superset of the ones of
+ // `Allocator::free`.
+ unsafe { self.free(ptr) }
}
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
+ let this: &dyn Allocator = self;
+ let old_size = layout.size();
+
// SAFETY:
// - `new_size`, when rounded up to the nearest multiple of `layout.align()`, will not
// overflow `isize` by the function safety requirement.
@@ -73,13 +116,20 @@ unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut
// requirement.
// - the size of `layout` is not zero because `new_size` is not zero by the function safety
// requirement.
- unsafe { krealloc_aligned(ptr, layout, GFP_KERNEL) }
+ // - `old_size` represents the memory that needs to be preserved.
+ match unsafe { this.realloc(ptr, old_size, layout, GFP_KERNEL) } {
+ Ok(ptr) => ptr.as_ptr().cast(),
+ Err(_) => ptr::null_mut(),
+ }
}
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
- // SAFETY: `ptr::null_mut()` is null and `layout` has a non-zero size by the function safety
- // requirement.
- unsafe { krealloc_aligned(ptr::null_mut(), layout, GFP_KERNEL | __GFP_ZERO) }
+ let this: &dyn Allocator = self;
+
+ match this.alloc(layout, GFP_KERNEL | __GFP_ZERO) {
+ Ok(ptr) => ptr.as_ptr().cast(),
+ Err(_) => ptr::null_mut(),
+ }
}
}
--
2.45.2