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 11 use super::{flags::*, Flags}; 12 use core::alloc::{GlobalAlloc, Layout}; 13 use core::ptr; 14 use core::ptr::NonNull; 15 16 use crate::alloc::{AllocError, Allocator}; 17 use crate::bindings; 18 19 /// The contiguous kernel allocator. 20 /// 21 /// `Kmalloc` is typically used for physically contiguous allocations up to page size, but also 22 /// supports larger allocations up to `bindings::KMALLOC_MAX_SIZE`, which is hardware specific. 23 /// 24 /// For more details see [self]. 25 pub struct Kmalloc; 26 27 /// Returns a proper size to alloc a new object aligned to `new_layout`'s alignment. 28 fn aligned_size(new_layout: Layout) -> usize { 29 // Customized layouts from `Layout::from_size_align()` can have size < align, so pad first. 30 let layout = new_layout.pad_to_align(); 31 32 // Note that `layout.size()` (after padding) is guaranteed to be a multiple of `layout.align()` 33 // which together with the slab guarantees means the `krealloc` will return a properly aligned 34 // object (see comments in `kmalloc()` for more information). 35 layout.size() 36 } 37 38 /// Calls `krealloc` with a proper size to alloc a new object aligned to `new_layout`'s alignment. 39 /// 40 /// # Safety 41 /// 42 /// - `ptr` can be either null or a pointer which has been allocated by this allocator. 43 /// - `new_layout` must have a non-zero size. 44 pub(crate) unsafe fn krealloc_aligned(ptr: *mut u8, new_layout: Layout, flags: Flags) -> *mut u8 { 45 let size = aligned_size(new_layout); 46 47 // SAFETY: 48 // - `ptr` is either null or a pointer returned from a previous `k{re}alloc()` by the 49 // function safety requirement. 50 // - `size` is greater than 0 since it's from `layout.size()` (which cannot be zero according 51 // to the function safety requirement) 52 unsafe { bindings::krealloc(ptr as *const core::ffi::c_void, size, flags.0) as *mut u8 } 53 } 54 55 /// # Invariants 56 /// 57 /// One of the following: `krealloc`, `vrealloc`, `kvrealloc`. 58 struct ReallocFunc( 59 unsafe extern "C" fn(*const core::ffi::c_void, usize, u32) -> *mut core::ffi::c_void, 60 ); 61 62 impl ReallocFunc { 63 // INVARIANT: `krealloc` satisfies the type invariants. 64 const KREALLOC: Self = Self(bindings::krealloc); 65 66 /// # Safety 67 /// 68 /// This method has the same safety requirements as [`Allocator::realloc`]. 69 /// 70 /// # Guarantees 71 /// 72 /// This method has the same guarantees as `Allocator::realloc`. Additionally 73 /// - it accepts any pointer to a valid memory allocation allocated by this function. 74 /// - memory allocated by this function remains valid until it is passed to this function. 75 unsafe fn call( 76 &self, 77 ptr: Option<NonNull<u8>>, 78 layout: Layout, 79 old_layout: Layout, 80 flags: Flags, 81 ) -> Result<NonNull<[u8]>, AllocError> { 82 let size = aligned_size(layout); 83 let ptr = match ptr { 84 Some(ptr) => { 85 if old_layout.size() == 0 { 86 ptr::null() 87 } else { 88 ptr.as_ptr() 89 } 90 } 91 None => ptr::null(), 92 }; 93 94 // SAFETY: 95 // - `self.0` is one of `krealloc`, `vrealloc`, `kvrealloc` and thus only requires that 96 // `ptr` is NULL or valid. 97 // - `ptr` is either NULL or valid by the safety requirements of this function. 98 // 99 // GUARANTEE: 100 // - `self.0` is one of `krealloc`, `vrealloc`, `kvrealloc`. 101 // - Those functions provide the guarantees of this function. 102 let raw_ptr = unsafe { 103 // If `size == 0` and `ptr != NULL` the memory behind the pointer is freed. 104 self.0(ptr.cast(), size, flags.0).cast() 105 }; 106 107 let ptr = if size == 0 { 108 crate::alloc::dangling_from_layout(layout) 109 } else { 110 NonNull::new(raw_ptr).ok_or(AllocError)? 111 }; 112 113 Ok(NonNull::slice_from_raw_parts(ptr, size)) 114 } 115 } 116 117 // SAFETY: `realloc` delegates to `ReallocFunc::call`, which guarantees that 118 // - memory remains valid until it is explicitly freed, 119 // - passing a pointer to a valid memory allocation is OK, 120 // - `realloc` satisfies the guarantees, since `ReallocFunc::call` has the same. 121 unsafe impl Allocator for Kmalloc { 122 #[inline] 123 unsafe fn realloc( 124 ptr: Option<NonNull<u8>>, 125 layout: Layout, 126 old_layout: Layout, 127 flags: Flags, 128 ) -> Result<NonNull<[u8]>, AllocError> { 129 // SAFETY: `ReallocFunc::call` has the same safety requirements as `Allocator::realloc`. 130 unsafe { ReallocFunc::KREALLOC.call(ptr, layout, old_layout, flags) } 131 } 132 } 133 134 // SAFETY: TODO. 135 unsafe impl GlobalAlloc for Kmalloc { 136 unsafe fn alloc(&self, layout: Layout) -> *mut u8 { 137 // SAFETY: `ptr::null_mut()` is null and `layout` has a non-zero size by the function safety 138 // requirement. 139 unsafe { krealloc_aligned(ptr::null_mut(), layout, GFP_KERNEL) } 140 } 141 142 unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) { 143 // SAFETY: TODO. 144 unsafe { 145 bindings::kfree(ptr as *const core::ffi::c_void); 146 } 147 } 148 149 unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 { 150 // SAFETY: 151 // - `new_size`, when rounded up to the nearest multiple of `layout.align()`, will not 152 // overflow `isize` by the function safety requirement. 153 // - `layout.align()` is a proper alignment (i.e. not zero and must be a power of two). 154 let layout = unsafe { Layout::from_size_align_unchecked(new_size, layout.align()) }; 155 156 // SAFETY: 157 // - `ptr` is either null or a pointer allocated by this allocator by the function safety 158 // requirement. 159 // - the size of `layout` is not zero because `new_size` is not zero by the function safety 160 // requirement. 161 unsafe { krealloc_aligned(ptr, layout, GFP_KERNEL) } 162 } 163 164 unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 { 165 // SAFETY: `ptr::null_mut()` is null and `layout` has a non-zero size by the function safety 166 // requirement. 167 unsafe { krealloc_aligned(ptr::null_mut(), layout, GFP_KERNEL | __GFP_ZERO) } 168 } 169 } 170 171 #[global_allocator] 172 static ALLOCATOR: Kmalloc = Kmalloc; 173 174 // See <https://github.com/rust-lang/rust/pull/86844>. 175 #[no_mangle] 176 static __rust_no_alloc_shim_is_unstable: u8 = 0; 177