Re: [PATCH v4 21/28] rust: alloc: remove `GlobalAlloc` and `krealloc_aligned`

Björn Roy Baron
2024-08-06T19:07:19Z

On Monday, August 5th, 2024 at 17:19, Danilo Krummrich <dakr@kernel.org> wrote:

> Now that we have all the `Allocator`s and the kernel `Box` and `Vec`
> implementations in place, remove `GlobalAlloc` and `krealloc_aligned`.
>
> Signed-off-by: Danilo Krummrich <dakr@kernel.org>
> ---
> rust/kernel/alloc/allocator.rs | 63 ++--------------------------------
> 1 file changed, 2 insertions(+), 61 deletions(-)
>
> diff --git a/rust/kernel/alloc/allocator.rs b/rust/kernel/alloc/allocator.rs
> index 91ff21372a8f..6abd55694422 100644
> --- a/rust/kernel/alloc/allocator.rs
> +++ b/rust/kernel/alloc/allocator.rs
> @@ -2,8 +2,8 @@
>
> //! Allocator support.
>
> -use super::{flags::*, Flags};
> -use core::alloc::{GlobalAlloc, Layout};
> +use super::Flags;
> +use core::alloc::Layout;
> use core::ptr;
> use core::ptr::NonNull;
>
> @@ -40,27 +40,6 @@ fn aligned_size(new_layout: Layout) -> usize {
> layout.size()
> }
>
> -/// Calls `krealloc` with a proper size to alloc a new object aligned to `new_layout`'s alignment.
> -///
> -/// # Safety
> -///
> -/// - `ptr` can be either null or a pointer which has been allocated by this allocator.
> -/// - `new_layout` must have a non-zero size.
> -pub(crate) unsafe fn krealloc_aligned(ptr: *mut u8, new_layout: Layout, flags: Flags) -> *mut u8 {
> - // SAFETY:
> - // - `ptr` is either null or a pointer returned from a previous `k{re}alloc()` by the
> - // function safety requirement.
> - // - `size` is greater than 0 since it's from `layout.size()` (which cannot be zero according
> - // to the function safety requirement)
> - unsafe {
> - bindings::krealloc(
> - ptr as *const core::ffi::c_void,
> - aligned_size(new_layout),
> - flags.0,
> - ) as *mut u8
> - }
> -}
> -
> /// # Invariants
> ///
> /// One of the following `krealloc`, `vrealloc`, `kvrealloc`.
> @@ -129,41 +108,6 @@ unsafe fn realloc(
> }
> }
>
> -unsafe impl GlobalAlloc for Kmalloc {
> - unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
> - // SAFETY: `ptr::null_mut()` is null and `layout` has a non-zero size by the function safety
> - // requirement.
> - unsafe { krealloc_aligned(ptr::null_mut(), layout, GFP_KERNEL) }
> - }
> -
> - unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
> - unsafe {
> - bindings::kfree(ptr as *const core::ffi::c_void);
> - }
> - }
> -
> - unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
> - // SAFETY:
> - // - `new_size`, when rounded up to the nearest multiple of `layout.align()`, will not
> - // overflow `isize` by the function safety requirement.
> - // - `layout.align()` is a proper alignment (i.e. not zero and must be a power of two).
> - let layout = unsafe { Layout::from_size_align_unchecked(new_size, layout.align()) };
> -
> - // SAFETY:
> - // - `ptr` is either null or a pointer allocated by this allocator by the function safety
> - // requirement.
> - // - the size of `layout` is not zero because `new_size` is not zero by the function safety
> - // requirement.
> - unsafe { krealloc_aligned(ptr, layout, GFP_KERNEL) }
> - }
> -
> - unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
> - // SAFETY: `ptr::null_mut()` is null and `layout` has a non-zero size by the function safety
> - // requirement.
> - unsafe { krealloc_aligned(ptr::null_mut(), layout, GFP_KERNEL | __GFP_ZERO) }
> - }
> -}
> -
> unsafe impl Allocator for Vmalloc {
> unsafe fn realloc(
> ptr: Option<NonNull<u8>>,
> @@ -204,9 +148,6 @@ unsafe fn realloc(
> }
> }
>
> -#[global_allocator]
> -static ALLOCATOR: Kmalloc = Kmalloc;
> -
> // See <https://github.com/rust-lang/rust/pull/86844>.
> #[no_mangle]
> static __rust_no_alloc_shim_is_unstable: u8 = 0;

This can be removed too. liballoc needed it for us to acknowledge that we
were linking liballoc in a way that isn't guaranteed to be stable, but now
that we are removing liballoc entirely, this static is no longer necessary.

> --
> 2.45.2