kernel/alloc/allocator.rs
1// SPDX-License-Identifier: GPL-2.0
2
3//! Allocator support.
4//!
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.
8//!
9//! Reference: <https://docs.kernel.org/core-api/memory-allocation.html>
10
11use super::Flags;
12use core::alloc::Layout;
13use core::ptr;
14use core::ptr::NonNull;
15
16use crate::alloc::{AllocError, Allocator};
17use crate::bindings;
18use crate::pr_warn;
19
20/// The contiguous kernel allocator.
21///
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.
24///
25/// For more details see [self].
26pub struct Kmalloc;
27
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].
35pub struct Vmalloc;
36
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].
44pub struct KVmalloc;
45
46/// # Invariants
47///
48/// One of the following: `krealloc`, `vrealloc`, `kvrealloc`.
49struct ReallocFunc(
50 unsafe extern "C" fn(*const crate::ffi::c_void, usize, u32) -> *mut crate::ffi::c_void,
51);
52
53impl ReallocFunc {
54 // INVARIANT: `krealloc` satisfies the type invariants.
55 const KREALLOC: Self = Self(bindings::krealloc);
56
57 // INVARIANT: `vrealloc` satisfies the type invariants.
58 const VREALLOC: Self = Self(bindings::vrealloc);
59
60 // INVARIANT: `kvrealloc` satisfies the type invariants.
61 const KVREALLOC: Self = Self(bindings::kvrealloc);
62
63 /// # Safety
64 ///
65 /// This method has the same safety requirements as [`Allocator::realloc`].
66 ///
67 /// # Guarantees
68 ///
69 /// This method has the same guarantees as `Allocator::realloc`. Additionally
70 /// - it accepts any pointer to a valid memory allocation allocated by this function.
71 /// - memory allocated by this function remains valid until it is passed to this function.
72 unsafe fn call(
73 &self,
74 ptr: Option<NonNull<u8>>,
75 layout: Layout,
76 old_layout: Layout,
77 flags: Flags,
78 ) -> Result<NonNull<[u8]>, AllocError> {
79 let size = layout.size();
80 let ptr = match ptr {
81 Some(ptr) => {
82 if old_layout.size() == 0 {
83 ptr::null()
84 } else {
85 ptr.as_ptr()
86 }
87 }
88 None => ptr::null(),
89 };
90
91 // SAFETY:
92 // - `self.0` is one of `krealloc`, `vrealloc`, `kvrealloc` and thus only requires that
93 // `ptr` is NULL or valid.
94 // - `ptr` is either NULL or valid by the safety requirements of this function.
95 //
96 // GUARANTEE:
97 // - `self.0` is one of `krealloc`, `vrealloc`, `kvrealloc`.
98 // - Those functions provide the guarantees of this function.
99 let raw_ptr = unsafe {
100 // If `size == 0` and `ptr != NULL` the memory behind the pointer is freed.
101 self.0(ptr.cast(), size, flags.0).cast()
102 };
103
104 let ptr = if size == 0 {
105 crate::alloc::dangling_from_layout(layout)
106 } else {
107 NonNull::new(raw_ptr).ok_or(AllocError)?
108 };
109
110 Ok(NonNull::slice_from_raw_parts(ptr, size))
111 }
112}
113
114impl Kmalloc {
115 /// Returns a [`Layout`] that makes [`Kmalloc`] fulfill the requested size and alignment of
116 /// `layout`.
117 pub fn aligned_layout(layout: Layout) -> Layout {
118 // Note that `layout.size()` (after padding) is guaranteed to be a multiple of
119 // `layout.align()` which together with the slab guarantees means that `Kmalloc` will return
120 // a properly aligned object (see comments in `kmalloc()` for more information).
121 layout.pad_to_align()
122 }
123}
124
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.
129unsafe impl Allocator for Kmalloc {
130 #[inline]
131 unsafe fn realloc(
132 ptr: Option<NonNull<u8>>,
133 layout: Layout,
134 old_layout: Layout,
135 flags: Flags,
136 ) -> Result<NonNull<[u8]>, AllocError> {
137 let layout = Kmalloc::aligned_layout(layout);
138
139 // SAFETY: `ReallocFunc::call` has the same safety requirements as `Allocator::realloc`.
140 unsafe { ReallocFunc::KREALLOC.call(ptr, layout, old_layout, flags) }
141 }
142}
143
144// SAFETY: `realloc` delegates to `ReallocFunc::call`, which guarantees that
145// - memory remains valid until it is explicitly freed,
146// - passing a pointer to a valid memory allocation is OK,
147// - `realloc` satisfies the guarantees, since `ReallocFunc::call` has the same.
148unsafe impl Allocator for Vmalloc {
149 #[inline]
150 unsafe fn realloc(
151 ptr: Option<NonNull<u8>>,
152 layout: Layout,
153 old_layout: Layout,
154 flags: Flags,
155 ) -> Result<NonNull<[u8]>, AllocError> {
156 // TODO: Support alignments larger than PAGE_SIZE.
157 if layout.align() > bindings::PAGE_SIZE {
158 pr_warn!("Vmalloc does not support alignments larger than PAGE_SIZE yet.\n");
159 return Err(AllocError);
160 }
161
162 // SAFETY: If not `None`, `ptr` is guaranteed to point to valid memory, which was previously
163 // allocated with this `Allocator`.
164 unsafe { ReallocFunc::VREALLOC.call(ptr, layout, old_layout, flags) }
165 }
166}
167
168// SAFETY: `realloc` delegates to `ReallocFunc::call`, which guarantees that
169// - memory remains valid until it is explicitly freed,
170// - passing a pointer to a valid memory allocation is OK,
171// - `realloc` satisfies the guarantees, since `ReallocFunc::call` has the same.
172unsafe impl Allocator for KVmalloc {
173 #[inline]
174 unsafe fn realloc(
175 ptr: Option<NonNull<u8>>,
176 layout: Layout,
177 old_layout: Layout,
178 flags: Flags,
179 ) -> Result<NonNull<[u8]>, AllocError> {
180 // `KVmalloc` may use the `Kmalloc` backend, hence we have to enforce a `Kmalloc`
181 // compatible layout.
182 let layout = Kmalloc::aligned_layout(layout);
183
184 // TODO: Support alignments larger than PAGE_SIZE.
185 if layout.align() > bindings::PAGE_SIZE {
186 pr_warn!("KVmalloc does not support alignments larger than PAGE_SIZE yet.\n");
187 return Err(AllocError);
188 }
189
190 // SAFETY: If not `None`, `ptr` is guaranteed to point to valid memory, which was previously
191 // allocated with this `Allocator`.
192 unsafe { ReallocFunc::KVREALLOC.call(ptr, layout, old_layout, flags) }
193 }
194}