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

Danilo Krummrich posted 28 patches 1 year, 4 months ago
There is a newer version of this series
[PATCH v4 21/28] rust: alloc: remove `GlobalAlloc` and `krealloc_aligned`
Posted by Danilo Krummrich 1 year, 4 months ago
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;
-- 
2.45.2
Re: [PATCH v4 21/28] rust: alloc: remove `GlobalAlloc` and `krealloc_aligned`
Posted by Benno Lossin 1 year, 4 months ago
On 05.08.24 17:19, Danilo Krummrich wrote:
> Now that we have all the `Allocator`s and the kernel `Box` and `Vec`
> implementations in place, remove `GlobalAlloc` and `krealloc_aligned`.

When I try to compile this patch, then I get this error:

      UPD     include/generated/utsversion.h
      CC      init/version-timestamp.o
      KSYMS   .tmp_vmlinux0.kallsyms.S
      AS      .tmp_vmlinux0.kallsyms.o
      LD      .tmp_vmlinux1
    ld.lld: error: undefined symbol: __rust_realloc
    >>> referenced by usercopy_64.c
    >>>               vmlinux.o:(alloc::raw_vec::finish_grow::<alloc::alloc::Global>)
    
    ld.lld: error: undefined symbol: __rust_alloc
    >>> referenced by usercopy_64.c
    >>>               vmlinux.o:(alloc::raw_vec::finish_grow::<alloc::alloc::Global>)
    make[2]: *** [scripts/Makefile.vmlinux:34: vmlinux] Error 1

So maybe the `#[global_allocator]` needs to stay until we remove the
`alloc` dependency?

---
Cheers,
Benno

> Signed-off-by: Danilo Krummrich <dakr@kernel.org>
> ---
>  rust/kernel/alloc/allocator.rs | 63 ++--------------------------------
>  1 file changed, 2 insertions(+), 61 deletions(-)
Re: [PATCH v4 21/28] rust: alloc: remove `GlobalAlloc` and `krealloc_aligned`
Posted by Danilo Krummrich 1 year, 4 months ago
On Wed, Aug 07, 2024 at 09:23:04PM +0000, Benno Lossin wrote:
> On 05.08.24 17:19, Danilo Krummrich wrote:
> > Now that we have all the `Allocator`s and the kernel `Box` and `Vec`
> > implementations in place, remove `GlobalAlloc` and `krealloc_aligned`.
> 
> When I try to compile this patch, then I get this error:
> 
>       UPD     include/generated/utsversion.h
>       CC      init/version-timestamp.o
>       KSYMS   .tmp_vmlinux0.kallsyms.S
>       AS      .tmp_vmlinux0.kallsyms.o
>       LD      .tmp_vmlinux1
>     ld.lld: error: undefined symbol: __rust_realloc
>     >>> referenced by usercopy_64.c
>     >>>               vmlinux.o:(alloc::raw_vec::finish_grow::<alloc::alloc::Global>)
>     
>     ld.lld: error: undefined symbol: __rust_alloc
>     >>> referenced by usercopy_64.c
>     >>>               vmlinux.o:(alloc::raw_vec::finish_grow::<alloc::alloc::Global>)
>     make[2]: *** [scripts/Makefile.vmlinux:34: vmlinux] Error 1
> 
> So maybe the `#[global_allocator]` needs to stay until we remove the
> `alloc` dependency?

Yes, I think so. I compile checked the single patches with `M=rust` and
`M=drivers`, which only compiles, but doesn't link things, so it didn't catch
it.

> 
> ---
> Cheers,
> Benno
> 
> > Signed-off-by: Danilo Krummrich <dakr@kernel.org>
> > ---
> >  rust/kernel/alloc/allocator.rs | 63 ++--------------------------------
> >  1 file changed, 2 insertions(+), 61 deletions(-)
>
Re: [PATCH v4 21/28] rust: alloc: remove `GlobalAlloc` and `krealloc_aligned`
Posted by Björn Roy Baron 1 year, 4 months ago
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
Re: [PATCH v4 21/28] rust: alloc: remove `GlobalAlloc` and `krealloc_aligned`
Posted by Miguel Ojeda 1 year, 4 months ago
On Tue, Aug 6, 2024 at 9:07 PM Björn Roy Baron <bjorn3_gh@protonmail.com> wrote:
>
> 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.

Thanks Björn, noted in issue #2 (and linked to the new tracking issue).

Cheers,
Miguel