xref: /openbmc/linux/rust/kernel/allocator.rs (revision 0beaf546b4f198dd767b5a01a73f24db269ff6e2)
1247b365dSWedson Almeida Filho // SPDX-License-Identifier: GPL-2.0
2247b365dSWedson Almeida Filho 
3247b365dSWedson Almeida Filho //! Allocator support.
4247b365dSWedson Almeida Filho 
5247b365dSWedson Almeida Filho use core::alloc::{GlobalAlloc, Layout};
6247b365dSWedson Almeida Filho use core::ptr;
7247b365dSWedson Almeida Filho 
8247b365dSWedson Almeida Filho use crate::bindings;
9247b365dSWedson Almeida Filho 
10247b365dSWedson Almeida Filho struct KernelAllocator;
11247b365dSWedson Almeida Filho 
12b3d8aa84SBoqun Feng /// Calls `krealloc` with a proper size to alloc a new object aligned to `new_layout`'s alignment.
13b3d8aa84SBoqun Feng ///
14b3d8aa84SBoqun Feng /// # Safety
15b3d8aa84SBoqun Feng ///
16b3d8aa84SBoqun Feng /// - `ptr` can be either null or a pointer which has been allocated by this allocator.
17b3d8aa84SBoqun Feng /// - `new_layout` must have a non-zero size.
18b3d8aa84SBoqun Feng unsafe fn krealloc_aligned(ptr: *mut u8, new_layout: Layout, flags: bindings::gfp_t) -> *mut u8 {
19b3d8aa84SBoqun Feng     // Customized layouts from `Layout::from_size_align()` can have size < align, so pad first.
20b3d8aa84SBoqun Feng     let layout = new_layout.pad_to_align();
21b3d8aa84SBoqun Feng 
22b3d8aa84SBoqun Feng     let mut size = layout.size();
23b3d8aa84SBoqun Feng 
24b3d8aa84SBoqun Feng     if layout.align() > bindings::BINDINGS_ARCH_SLAB_MINALIGN {
25b3d8aa84SBoqun Feng         // The alignment requirement exceeds the slab guarantee, thus try to enlarge the size
26b3d8aa84SBoqun Feng         // to use the "power-of-two" size/alignment guarantee (see comments in `kmalloc()` for
27b3d8aa84SBoqun Feng         // more information).
28b3d8aa84SBoqun Feng         //
29b3d8aa84SBoqun Feng         // Note that `layout.size()` (after padding) is guaranteed to be a multiple of
30b3d8aa84SBoqun Feng         // `layout.align()`, so `next_power_of_two` gives enough alignment guarantee.
31b3d8aa84SBoqun Feng         size = size.next_power_of_two();
32b3d8aa84SBoqun Feng     }
33b3d8aa84SBoqun Feng 
34b3d8aa84SBoqun Feng     // SAFETY:
35b3d8aa84SBoqun Feng     // - `ptr` is either null or a pointer returned from a previous `k{re}alloc()` by the
36b3d8aa84SBoqun Feng     //   function safety requirement.
37b3d8aa84SBoqun Feng     // - `size` is greater than 0 since it's either a `layout.size()` (which cannot be zero
38b3d8aa84SBoqun Feng     //    according to the function safety requirement) or a result from `next_power_of_two()`.
39b3d8aa84SBoqun Feng     unsafe { bindings::krealloc(ptr as *const core::ffi::c_void, size, flags) as *mut u8 }
40b3d8aa84SBoqun Feng }
41b3d8aa84SBoqun Feng 
42247b365dSWedson Almeida Filho unsafe impl GlobalAlloc for KernelAllocator {
43247b365dSWedson Almeida Filho     unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
44f39a97d0SBoqun Feng         // SAFETY: `ptr::null_mut()` is null and `layout` has a non-zero size by the function safety
45f39a97d0SBoqun Feng         // requirement.
46f39a97d0SBoqun Feng         unsafe { krealloc_aligned(ptr::null_mut(), layout, bindings::GFP_KERNEL) }
47247b365dSWedson Almeida Filho     }
48247b365dSWedson Almeida Filho 
49247b365dSWedson Almeida Filho     unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
50247b365dSWedson Almeida Filho         unsafe {
51247b365dSWedson Almeida Filho             bindings::kfree(ptr as *const core::ffi::c_void);
52247b365dSWedson Almeida Filho         }
53247b365dSWedson Almeida Filho     }
54*0beaf546SBjörn Roy Baron 
55*0beaf546SBjörn Roy Baron     unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
56*0beaf546SBjörn Roy Baron         // SAFETY:
57*0beaf546SBjörn Roy Baron         // - `new_size`, when rounded up to the nearest multiple of `layout.align()`, will not
58*0beaf546SBjörn Roy Baron         //   overflow `isize` by the function safety requirement.
59*0beaf546SBjörn Roy Baron         // - `layout.align()` is a proper alignment (i.e. not zero and must be a power of two).
60*0beaf546SBjörn Roy Baron         let layout = unsafe { Layout::from_size_align_unchecked(new_size, layout.align()) };
61*0beaf546SBjörn Roy Baron 
62*0beaf546SBjörn Roy Baron         // SAFETY:
63*0beaf546SBjörn Roy Baron         // - `ptr` is either null or a pointer allocated by this allocator by the function safety
64*0beaf546SBjörn Roy Baron         //   requirement.
65*0beaf546SBjörn Roy Baron         // - the size of `layout` is not zero because `new_size` is not zero by the function safety
66*0beaf546SBjörn Roy Baron         //   requirement.
67*0beaf546SBjörn Roy Baron         unsafe { krealloc_aligned(ptr, layout, bindings::GFP_KERNEL) }
68*0beaf546SBjörn Roy Baron     }
69*0beaf546SBjörn Roy Baron 
70*0beaf546SBjörn Roy Baron     unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
71*0beaf546SBjörn Roy Baron         // SAFETY: `ptr::null_mut()` is null and `layout` has a non-zero size by the function safety
72*0beaf546SBjörn Roy Baron         // requirement.
73*0beaf546SBjörn Roy Baron         unsafe {
74*0beaf546SBjörn Roy Baron             krealloc_aligned(
75*0beaf546SBjörn Roy Baron                 ptr::null_mut(),
76*0beaf546SBjörn Roy Baron                 layout,
77*0beaf546SBjörn Roy Baron                 bindings::GFP_KERNEL | bindings::__GFP_ZERO,
78*0beaf546SBjörn Roy Baron             )
79*0beaf546SBjörn Roy Baron         }
80*0beaf546SBjörn Roy Baron     }
81247b365dSWedson Almeida Filho }
82247b365dSWedson Almeida Filho 
83247b365dSWedson Almeida Filho #[global_allocator]
84247b365dSWedson Almeida Filho static ALLOCATOR: KernelAllocator = KernelAllocator;
85247b365dSWedson Almeida Filho 
86247b365dSWedson Almeida Filho // `rustc` only generates these for some crate types. Even then, we would need
87247b365dSWedson Almeida Filho // to extract the object file that has them from the archive. For the moment,
88247b365dSWedson Almeida Filho // let's generate them ourselves instead.
89247b365dSWedson Almeida Filho //
90b3d8aa84SBoqun Feng // Note: Although these are *safe* functions, they are called by the compiler
91b3d8aa84SBoqun Feng // with parameters that obey the same `GlobalAlloc` function safety
92b3d8aa84SBoqun Feng // requirements: size and align should form a valid layout, and size is
93b3d8aa84SBoqun Feng // greater than 0.
94b3d8aa84SBoqun Feng //
95247b365dSWedson Almeida Filho // Note that `#[no_mangle]` implies exported too, nowadays.
96247b365dSWedson Almeida Filho #[no_mangle]
97b3d8aa84SBoqun Feng fn __rust_alloc(size: usize, align: usize) -> *mut u8 {
98b3d8aa84SBoqun Feng     // SAFETY: See assumption above.
99b3d8aa84SBoqun Feng     let layout = unsafe { Layout::from_size_align_unchecked(size, align) };
100b3d8aa84SBoqun Feng 
101b3d8aa84SBoqun Feng     // SAFETY: `ptr::null_mut()` is null, per assumption above the size of `layout` is greater
102b3d8aa84SBoqun Feng     // than 0.
103b3d8aa84SBoqun Feng     unsafe { krealloc_aligned(ptr::null_mut(), layout, bindings::GFP_KERNEL) }
104247b365dSWedson Almeida Filho }
105247b365dSWedson Almeida Filho 
106247b365dSWedson Almeida Filho #[no_mangle]
107247b365dSWedson Almeida Filho fn __rust_dealloc(ptr: *mut u8, _size: usize, _align: usize) {
108247b365dSWedson Almeida Filho     unsafe { bindings::kfree(ptr as *const core::ffi::c_void) };
109247b365dSWedson Almeida Filho }
110247b365dSWedson Almeida Filho 
111247b365dSWedson Almeida Filho #[no_mangle]
112b3d8aa84SBoqun Feng fn __rust_realloc(ptr: *mut u8, _old_size: usize, align: usize, new_size: usize) -> *mut u8 {
113b3d8aa84SBoqun Feng     // SAFETY: See assumption above.
114b3d8aa84SBoqun Feng     let new_layout = unsafe { Layout::from_size_align_unchecked(new_size, align) };
115b3d8aa84SBoqun Feng 
116b3d8aa84SBoqun Feng     // SAFETY: Per assumption above, `ptr` is allocated by `__rust_*` before, and the size of
117b3d8aa84SBoqun Feng     // `new_layout` is greater than 0.
118b3d8aa84SBoqun Feng     unsafe { krealloc_aligned(ptr, new_layout, bindings::GFP_KERNEL) }
119247b365dSWedson Almeida Filho }
120247b365dSWedson Almeida Filho 
121247b365dSWedson Almeida Filho #[no_mangle]
122b3d8aa84SBoqun Feng fn __rust_alloc_zeroed(size: usize, align: usize) -> *mut u8 {
123b3d8aa84SBoqun Feng     // SAFETY: See assumption above.
124b3d8aa84SBoqun Feng     let layout = unsafe { Layout::from_size_align_unchecked(size, align) };
125b3d8aa84SBoqun Feng 
126b3d8aa84SBoqun Feng     // SAFETY: `ptr::null_mut()` is null, per assumption above the size of `layout` is greater
127b3d8aa84SBoqun Feng     // than 0.
128247b365dSWedson Almeida Filho     unsafe {
129b3d8aa84SBoqun Feng         krealloc_aligned(
130b3d8aa84SBoqun Feng             ptr::null_mut(),
131b3d8aa84SBoqun Feng             layout,
132247b365dSWedson Almeida Filho             bindings::GFP_KERNEL | bindings::__GFP_ZERO,
133b3d8aa84SBoqun Feng         )
134247b365dSWedson Almeida Filho     }
135247b365dSWedson Almeida Filho }
136