xref: /openbmc/linux/rust/kernel/allocator.rs (revision f39a97d0d8a765268b8de17f2e34393e7cb2cd2d)
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 {
44*f39a97d0SBoqun Feng         // SAFETY: `ptr::null_mut()` is null and `layout` has a non-zero size by the function safety
45*f39a97d0SBoqun Feng         // requirement.
46*f39a97d0SBoqun 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     }
54247b365dSWedson Almeida Filho }
55247b365dSWedson Almeida Filho 
56247b365dSWedson Almeida Filho #[global_allocator]
57247b365dSWedson Almeida Filho static ALLOCATOR: KernelAllocator = KernelAllocator;
58247b365dSWedson Almeida Filho 
59247b365dSWedson Almeida Filho // `rustc` only generates these for some crate types. Even then, we would need
60247b365dSWedson Almeida Filho // to extract the object file that has them from the archive. For the moment,
61247b365dSWedson Almeida Filho // let's generate them ourselves instead.
62247b365dSWedson Almeida Filho //
63b3d8aa84SBoqun Feng // Note: Although these are *safe* functions, they are called by the compiler
64b3d8aa84SBoqun Feng // with parameters that obey the same `GlobalAlloc` function safety
65b3d8aa84SBoqun Feng // requirements: size and align should form a valid layout, and size is
66b3d8aa84SBoqun Feng // greater than 0.
67b3d8aa84SBoqun Feng //
68247b365dSWedson Almeida Filho // Note that `#[no_mangle]` implies exported too, nowadays.
69247b365dSWedson Almeida Filho #[no_mangle]
70b3d8aa84SBoqun Feng fn __rust_alloc(size: usize, align: usize) -> *mut u8 {
71b3d8aa84SBoqun Feng     // SAFETY: See assumption above.
72b3d8aa84SBoqun Feng     let layout = unsafe { Layout::from_size_align_unchecked(size, align) };
73b3d8aa84SBoqun Feng 
74b3d8aa84SBoqun Feng     // SAFETY: `ptr::null_mut()` is null, per assumption above the size of `layout` is greater
75b3d8aa84SBoqun Feng     // than 0.
76b3d8aa84SBoqun Feng     unsafe { krealloc_aligned(ptr::null_mut(), layout, bindings::GFP_KERNEL) }
77247b365dSWedson Almeida Filho }
78247b365dSWedson Almeida Filho 
79247b365dSWedson Almeida Filho #[no_mangle]
80247b365dSWedson Almeida Filho fn __rust_dealloc(ptr: *mut u8, _size: usize, _align: usize) {
81247b365dSWedson Almeida Filho     unsafe { bindings::kfree(ptr as *const core::ffi::c_void) };
82247b365dSWedson Almeida Filho }
83247b365dSWedson Almeida Filho 
84247b365dSWedson Almeida Filho #[no_mangle]
85b3d8aa84SBoqun Feng fn __rust_realloc(ptr: *mut u8, _old_size: usize, align: usize, new_size: usize) -> *mut u8 {
86b3d8aa84SBoqun Feng     // SAFETY: See assumption above.
87b3d8aa84SBoqun Feng     let new_layout = unsafe { Layout::from_size_align_unchecked(new_size, align) };
88b3d8aa84SBoqun Feng 
89b3d8aa84SBoqun Feng     // SAFETY: Per assumption above, `ptr` is allocated by `__rust_*` before, and the size of
90b3d8aa84SBoqun Feng     // `new_layout` is greater than 0.
91b3d8aa84SBoqun Feng     unsafe { krealloc_aligned(ptr, new_layout, bindings::GFP_KERNEL) }
92247b365dSWedson Almeida Filho }
93247b365dSWedson Almeida Filho 
94247b365dSWedson Almeida Filho #[no_mangle]
95b3d8aa84SBoqun Feng fn __rust_alloc_zeroed(size: usize, align: usize) -> *mut u8 {
96b3d8aa84SBoqun Feng     // SAFETY: See assumption above.
97b3d8aa84SBoqun Feng     let layout = unsafe { Layout::from_size_align_unchecked(size, align) };
98b3d8aa84SBoqun Feng 
99b3d8aa84SBoqun Feng     // SAFETY: `ptr::null_mut()` is null, per assumption above the size of `layout` is greater
100b3d8aa84SBoqun Feng     // than 0.
101247b365dSWedson Almeida Filho     unsafe {
102b3d8aa84SBoqun Feng         krealloc_aligned(
103b3d8aa84SBoqun Feng             ptr::null_mut(),
104b3d8aa84SBoqun Feng             layout,
105247b365dSWedson Almeida Filho             bindings::GFP_KERNEL | bindings::__GFP_ZERO,
106b3d8aa84SBoqun Feng         )
107247b365dSWedson Almeida Filho     }
108247b365dSWedson Almeida Filho }
109