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.
krealloc_aligned(ptr: *mut u8, new_layout: Layout, flags: bindings::gfp_t) -> *mut u818b3d8aa84SBoqun 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 {
alloc(&self, layout: Layout) -> *mut u843247b365dSWedson 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
dealloc(&self, ptr: *mut u8, _layout: Layout)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 }
540beaf546SBjörn Roy Baron
realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8550beaf546SBjörn Roy Baron unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
560beaf546SBjörn Roy Baron // SAFETY:
570beaf546SBjörn Roy Baron // - `new_size`, when rounded up to the nearest multiple of `layout.align()`, will not
580beaf546SBjörn Roy Baron // overflow `isize` by the function safety requirement.
590beaf546SBjörn Roy Baron // - `layout.align()` is a proper alignment (i.e. not zero and must be a power of two).
600beaf546SBjörn Roy Baron let layout = unsafe { Layout::from_size_align_unchecked(new_size, layout.align()) };
610beaf546SBjörn Roy Baron
620beaf546SBjörn Roy Baron // SAFETY:
630beaf546SBjörn Roy Baron // - `ptr` is either null or a pointer allocated by this allocator by the function safety
640beaf546SBjörn Roy Baron // requirement.
650beaf546SBjörn Roy Baron // - the size of `layout` is not zero because `new_size` is not zero by the function safety
660beaf546SBjörn Roy Baron // requirement.
670beaf546SBjörn Roy Baron unsafe { krealloc_aligned(ptr, layout, bindings::GFP_KERNEL) }
680beaf546SBjörn Roy Baron }
690beaf546SBjörn Roy Baron
alloc_zeroed(&self, layout: Layout) -> *mut u8700beaf546SBjörn Roy Baron unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
710beaf546SBjörn Roy Baron // SAFETY: `ptr::null_mut()` is null and `layout` has a non-zero size by the function safety
720beaf546SBjörn Roy Baron // requirement.
730beaf546SBjörn Roy Baron unsafe {
740beaf546SBjörn Roy Baron krealloc_aligned(
750beaf546SBjörn Roy Baron ptr::null_mut(),
760beaf546SBjörn Roy Baron layout,
770beaf546SBjörn Roy Baron bindings::GFP_KERNEL | bindings::__GFP_ZERO,
780beaf546SBjörn Roy Baron )
790beaf546SBjörn Roy Baron }
800beaf546SBjörn Roy Baron }
81247b365dSWedson Almeida Filho }
82247b365dSWedson Almeida Filho
83247b365dSWedson Almeida Filho #[global_allocator]
84247b365dSWedson Almeida Filho static ALLOCATOR: KernelAllocator = KernelAllocator;
85247b365dSWedson Almeida Filho
86*89eed1abSMiguel Ojeda // See <https://github.com/rust-lang/rust/pull/86844>.
87247b365dSWedson Almeida Filho #[no_mangle]
88*89eed1abSMiguel Ojeda static __rust_no_alloc_shim_is_unstable: u8 = 0;
89