Re: [PATCH v4 02/28] rust: alloc: separate `aligned_size` from `krealloc_aligned`

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 05.08.24 17:19, Danilo Krummrich wrote:
> Separate `aligned_size` from `krealloc_aligned`.
> 
> Subsequent patches implement `Allocator` derivates, such as `Kmalloc`,
> that require `aligned_size` and replace the original `krealloc_aligned`.
> 
> Reviewed-by: Alice Ryhl <aliceryhl@xxxxxxxxxx>
> Signed-off-by: Danilo Krummrich <dakr@xxxxxxxxxx>
> ---
>  rust/kernel/alloc/allocator.rs | 27 ++++++++++++++++++---------
>  1 file changed, 18 insertions(+), 9 deletions(-)
> 
> diff --git a/rust/kernel/alloc/allocator.rs b/rust/kernel/alloc/allocator.rs
> index e6ea601f38c6..e7b7eba84acb 100644
> --- a/rust/kernel/alloc/allocator.rs
> +++ b/rust/kernel/alloc/allocator.rs
> @@ -8,27 +8,36 @@
> 
>  struct KernelAllocator;
> 
> -/// 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 {
> +/// Returns a proper size to alloc a new object aligned to `new_layout`'s alignment.
> +fn aligned_size(new_layout: Layout) -> usize {
>      // Customized layouts from `Layout::from_size_align()` can have size < align, so pad first.
>      let layout = new_layout.pad_to_align();
> 
>      // Note that `layout.size()` (after padding) is guaranteed to be a multiple of `layout.align()`
>      // which together with the slab guarantees means the `krealloc` will return a properly aligned
>      // object (see comments in `kmalloc()` for more information).
> -    let size = layout.size();
> +    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, size, flags.0) as *mut u8 }
> +    unsafe {
> +        bindings::krealloc(
> +            ptr as *const core::ffi::c_void,
> +            aligned_size(new_layout),

Can you move the safe operation outside of the `unsafe` block?

With that changed,

Reviewed-by: Benno Lossin <benno.lossin@xxxxxxxxx>

---
Cheers,
Benno

> +            flags.0,
> +        ) as *mut u8
> +    }
>  }
> 
>  unsafe impl GlobalAlloc for KernelAllocator {
> --
> 2.45.2
> 






[Index of Archives]     [Linux ARM Kernel]     [Linux ARM]     [Linux Omap]     [Fedora ARM]     [IETF Annouce]     [Bugtraq]     [Linux OMAP]     [Linux MIPS]     [eCos]     [Asterisk Internet PBX]     [Linux API]

  Powered by Linux