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