diff options
Diffstat (limited to 'rust/kernel/alloc')
| -rw-r--r-- | rust/kernel/alloc/allocator.rs | 19 | ||||
| -rw-r--r-- | rust/kernel/alloc/box_ext.rs | 6 | ||||
| -rw-r--r-- | rust/kernel/alloc/vec_ext.rs | 7 | 
3 files changed, 15 insertions, 17 deletions
| diff --git a/rust/kernel/alloc/allocator.rs b/rust/kernel/alloc/allocator.rs index 229642960cd1..e6ea601f38c6 100644 --- a/rust/kernel/alloc/allocator.rs +++ b/rust/kernel/alloc/allocator.rs @@ -18,23 +18,16 @@ pub(crate) unsafe fn krealloc_aligned(ptr: *mut u8, new_layout: Layout, flags: F      // Customized layouts from `Layout::from_size_align()` can have size < align, so pad first.      let layout = new_layout.pad_to_align(); -    let mut size = layout.size(); - -    if layout.align() > bindings::ARCH_SLAB_MINALIGN { -        // The alignment requirement exceeds the slab guarantee, thus try to enlarge the size -        // to use the "power-of-two" size/alignment guarantee (see comments in `kmalloc()` for -        // more information). -        // -        // Note that `layout.size()` (after padding) is guaranteed to be a multiple of -        // `layout.align()`, so `next_power_of_two` gives enough alignment guarantee. -        size = size.next_power_of_two(); -    } +    // 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();      // 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 either a `layout.size()` (which cannot be zero -    //   according to the function safety requirement) or a result from `next_power_of_two()`. +    // - `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 }  } diff --git a/rust/kernel/alloc/box_ext.rs b/rust/kernel/alloc/box_ext.rs index 829cb1c1cf9e..9f1c1c489189 100644 --- a/rust/kernel/alloc/box_ext.rs +++ b/rust/kernel/alloc/box_ext.rs @@ -21,8 +21,10 @@ pub trait BoxExt<T>: Sized {  impl<T> BoxExt<T> for Box<T> {      fn new(x: T, flags: Flags) -> Result<Self, AllocError> { -        let b = <Self as BoxExt<_>>::new_uninit(flags)?; -        Ok(Box::write(b, x)) +        let mut b = <Self as BoxExt<_>>::new_uninit(flags)?; +        b.write(x); +        // SAFETY: We just wrote to it. +        Ok(unsafe { b.assume_init() })      }      #[cfg(any(test, testlib))] diff --git a/rust/kernel/alloc/vec_ext.rs b/rust/kernel/alloc/vec_ext.rs index e9a81052728a..1297a4be32e8 100644 --- a/rust/kernel/alloc/vec_ext.rs +++ b/rust/kernel/alloc/vec_ext.rs @@ -4,7 +4,6 @@  use super::{AllocError, Flags};  use alloc::vec::Vec; -use core::ptr;  /// Extensions to [`Vec`].  pub trait VecExt<T>: Sized { @@ -141,7 +140,11 @@ impl<T> VecExt<T> for Vec<T> {          // `krealloc_aligned`. A `Vec<T>`'s `ptr` value is not guaranteed to be NULL and might be          // dangling after being created with `Vec::new`. Instead, we can rely on `Vec<T>`'s capacity          // to be zero if no memory has been allocated yet. -        let ptr = if cap == 0 { ptr::null_mut() } else { old_ptr }; +        let ptr = if cap == 0 { +            core::ptr::null_mut() +        } else { +            old_ptr +        };          // SAFETY: `ptr` is valid because it's either NULL or comes from a previous call to          // `krealloc_aligned`. We also verified that the type is not a ZST. | 
