1 // SPDX-License-Identifier: GPL-2.0
5 //! Documentation for the kernel's memory allocators can found in the "Memory Allocation Guide"
6 //! linked below. For instance, this includes the concept of "get free page" (GFP) flags and the
7 //! typical application of the different kernel allocators.
9 //! Reference: <https://docs.kernel.org/core-api/memory-allocation.html>
12 use core::alloc::Layout;
14 use core::ptr::NonNull;
16 use crate::alloc::{AllocError, Allocator};
20 /// The contiguous kernel allocator.
22 /// `Kmalloc` is typically used for physically contiguous allocations up to page size, but also
23 /// supports larger allocations up to `bindings::KMALLOC_MAX_SIZE`, which is hardware specific.
25 /// For more details see [self].
28 /// The virtually contiguous kernel allocator.
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.
34 /// For more details see [self].
37 /// The kvmalloc kernel allocator.
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`.
43 /// For more details see [self].
46 /// Returns a proper size to alloc a new object aligned to `new_layout`'s alignment.
47 fn aligned_size(new_layout: Layout) -> usize {
48 // Customized layouts from `Layout::from_size_align()` can have size < align, so pad first.
49 let layout = new_layout.pad_to_align();
51 // Note that `layout.size()` (after padding) is guaranteed to be a multiple of `layout.align()`
52 // which together with the slab guarantees means the `krealloc` will return a properly aligned
53 // object (see comments in `kmalloc()` for more information).
59 /// One of the following: `krealloc`, `vrealloc`, `kvrealloc`.
61 unsafe extern "C" fn(*const crate::ffi::c_void, usize, u32) -> *mut crate::ffi::c_void,
65 // INVARIANT: `krealloc` satisfies the type invariants.
66 const KREALLOC: Self = Self(bindings::krealloc);
68 // INVARIANT: `vrealloc` satisfies the type invariants.
69 const VREALLOC: Self = Self(bindings::vrealloc);
71 // INVARIANT: `kvrealloc` satisfies the type invariants.
72 const KVREALLOC: Self = Self(bindings::kvrealloc);
76 /// This method has the same safety requirements as [`Allocator::realloc`].
80 /// This method has the same guarantees as `Allocator::realloc`. Additionally
81 /// - it accepts any pointer to a valid memory allocation allocated by this function.
82 /// - memory allocated by this function remains valid until it is passed to this function.
85 ptr: Option<NonNull<u8>>,
89 ) -> Result<NonNull<[u8]>, AllocError> {
90 let size = aligned_size(layout);
93 if old_layout.size() == 0 {
103 // - `self.0` is one of `krealloc`, `vrealloc`, `kvrealloc` and thus only requires that
104 // `ptr` is NULL or valid.
105 // - `ptr` is either NULL or valid by the safety requirements of this function.
108 // - `self.0` is one of `krealloc`, `vrealloc`, `kvrealloc`.
109 // - Those functions provide the guarantees of this function.
110 let raw_ptr = unsafe {
111 // If `size == 0` and `ptr != NULL` the memory behind the pointer is freed.
112 self.0(ptr.cast(), size, flags.0).cast()
115 let ptr = if size == 0 {
116 crate::alloc::dangling_from_layout(layout)
118 NonNull::new(raw_ptr).ok_or(AllocError)?
121 Ok(NonNull::slice_from_raw_parts(ptr, size))
125 // SAFETY: `realloc` delegates to `ReallocFunc::call`, which guarantees that
126 // - memory remains valid until it is explicitly freed,
127 // - passing a pointer to a valid memory allocation is OK,
128 // - `realloc` satisfies the guarantees, since `ReallocFunc::call` has the same.
129 unsafe impl Allocator for Kmalloc {
132 ptr: Option<NonNull<u8>>,
136 ) -> Result<NonNull<[u8]>, AllocError> {
137 // SAFETY: `ReallocFunc::call` has the same safety requirements as `Allocator::realloc`.
138 unsafe { ReallocFunc::KREALLOC.call(ptr, layout, old_layout, flags) }
142 // SAFETY: `realloc` delegates to `ReallocFunc::call`, which guarantees that
143 // - memory remains valid until it is explicitly freed,
144 // - passing a pointer to a valid memory allocation is OK,
145 // - `realloc` satisfies the guarantees, since `ReallocFunc::call` has the same.
146 unsafe impl Allocator for Vmalloc {
149 ptr: Option<NonNull<u8>>,
153 ) -> Result<NonNull<[u8]>, AllocError> {
154 // TODO: Support alignments larger than PAGE_SIZE.
155 if layout.align() > bindings::PAGE_SIZE {
156 pr_warn!("Vmalloc does not support alignments larger than PAGE_SIZE yet.\n");
157 return Err(AllocError);
160 // SAFETY: If not `None`, `ptr` is guaranteed to point to valid memory, which was previously
161 // allocated with this `Allocator`.
162 unsafe { ReallocFunc::VREALLOC.call(ptr, layout, old_layout, flags) }
166 // SAFETY: `realloc` delegates to `ReallocFunc::call`, which guarantees that
167 // - memory remains valid until it is explicitly freed,
168 // - passing a pointer to a valid memory allocation is OK,
169 // - `realloc` satisfies the guarantees, since `ReallocFunc::call` has the same.
170 unsafe impl Allocator for KVmalloc {
173 ptr: Option<NonNull<u8>>,
177 ) -> Result<NonNull<[u8]>, AllocError> {
178 // TODO: Support alignments larger than PAGE_SIZE.
179 if layout.align() > bindings::PAGE_SIZE {
180 pr_warn!("KVmalloc does not support alignments larger than PAGE_SIZE yet.\n");
181 return Err(AllocError);
184 // SAFETY: If not `None`, `ptr` is guaranteed to point to valid memory, which was previously
185 // allocated with this `Allocator`.
186 unsafe { ReallocFunc::KVREALLOC.call(ptr, layout, old_layout, flags) }