[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20240704170738.3621-7-dakr@redhat.com>
Date: Thu, 4 Jul 2024 19:06:34 +0200
From: Danilo Krummrich <dakr@...hat.com>
To: ojeda@...nel.org,
alex.gaynor@...il.com,
wedsonaf@...il.com,
boqun.feng@...il.com,
gary@...yguo.net,
bjorn3_gh@...tonmail.com,
benno.lossin@...ton.me,
a.hindborg@...sung.com,
aliceryhl@...gle.com
Cc: daniel.almeida@...labora.com,
faith.ekstrand@...labora.com,
boris.brezillon@...labora.com,
lina@...hilina.net,
mcanal@...lia.com,
zhiw@...dia.com,
acurrid@...dia.com,
cjia@...dia.com,
jhubbard@...dia.com,
airlied@...hat.com,
ajanulgu@...hat.com,
lyude@...hat.com,
linux-kernel@...r.kernel.org,
rust-for-linux@...r.kernel.org,
Danilo Krummrich <dakr@...hat.com>
Subject: [PATCH 06/20] rust: alloc: remove `krealloc_aligned`
Now that we have `Allocator` for `Kmalloc` in place, remove explicit
calls to `krealloc_aligned` and get rid of `krealloc_aligned` itself.
`bindings::krealloc` should only be called from `Kmalloc::realloc`.
Signed-off-by: Danilo Krummrich <dakr@...hat.com>
---
rust/kernel/alloc/allocator.rs | 21 ---------------------
rust/kernel/alloc/box_ext.rs | 13 ++++---------
rust/kernel/alloc/vec_ext.rs | 23 +++++++++++++----------
3 files changed, 17 insertions(+), 40 deletions(-)
diff --git a/rust/kernel/alloc/allocator.rs b/rust/kernel/alloc/allocator.rs
index b7c0490f6415..1860cb79b875 100644
--- a/rust/kernel/alloc/allocator.rs
+++ b/rust/kernel/alloc/allocator.rs
@@ -36,27 +36,6 @@ fn aligned_size(new_layout: Layout) -> usize {
size
}
-/// Calls `krealloc` with a proper size to alloc a new object.
-///
-/// # 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 either a `layout.size()` (which cannot be zero
- // according to the function safety requirement) or a result from `next_power_of_two()`.
- unsafe {
- bindings::krealloc(
- ptr as *const core::ffi::c_void,
- aligned_size(new_layout),
- flags.0,
- ) as *mut u8
- }
-}
-
unsafe impl Allocator for Kmalloc {
unsafe fn realloc(
&self,
diff --git a/rust/kernel/alloc/box_ext.rs b/rust/kernel/alloc/box_ext.rs
index 829cb1c1cf9e..1aeae02c147e 100644
--- a/rust/kernel/alloc/box_ext.rs
+++ b/rust/kernel/alloc/box_ext.rs
@@ -33,24 +33,19 @@ fn new_uninit(_flags: Flags) -> Result<Box<MaybeUninit<T>>, AllocError> {
#[cfg(not(any(test, testlib)))]
fn new_uninit(flags: Flags) -> Result<Box<MaybeUninit<T>>, AllocError> {
let ptr = if core::mem::size_of::<MaybeUninit<T>>() == 0 {
- core::ptr::NonNull::<_>::dangling().as_ptr()
+ core::ptr::NonNull::dangling()
} else {
+ let alloc: &dyn super::Allocator = &super::allocator::Kmalloc;
let layout = core::alloc::Layout::new::<MaybeUninit<T>>();
// SAFETY: Memory is being allocated (first arg is null). The only other source of
// safety issues is sleeping on atomic context, which is addressed by klint. Lastly,
// the type is not a SZT (checked above).
- let ptr =
- unsafe { super::allocator::krealloc_aligned(core::ptr::null_mut(), layout, flags) };
- if ptr.is_null() {
- return Err(AllocError);
- }
-
- ptr.cast::<MaybeUninit<T>>()
+ alloc.alloc(layout, flags)?.cast()
};
// SAFETY: For non-zero-sized types, we allocate above using the global allocator. For
// zero-sized types, we use `NonNull::dangling`.
- Ok(unsafe { Box::from_raw(ptr) })
+ Ok(unsafe { Box::from_raw(ptr.as_ptr()) })
}
}
diff --git a/rust/kernel/alloc/vec_ext.rs b/rust/kernel/alloc/vec_ext.rs
index e9a81052728a..bf277976ed38 100644
--- a/rust/kernel/alloc/vec_ext.rs
+++ b/rust/kernel/alloc/vec_ext.rs
@@ -118,6 +118,7 @@ fn reserve(&mut self, additional: usize, _flags: Flags) -> Result<(), AllocError
#[cfg(not(any(test, testlib)))]
fn reserve(&mut self, additional: usize, flags: Flags) -> Result<(), AllocError> {
+ let alloc: &dyn super::Allocator = &super::allocator::Kmalloc;
let len = self.len();
let cap = self.capacity();
@@ -145,16 +146,18 @@ fn reserve(&mut self, additional: usize, flags: Flags) -> Result<(), AllocError>
// 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.
- let new_ptr = unsafe { super::allocator::krealloc_aligned(ptr.cast(), layout, flags) };
- if new_ptr.is_null() {
- // SAFETY: We are just rebuilding the existing `Vec` with no changes.
- unsafe { rebuild(self, old_ptr, len, cap) };
- Err(AllocError)
- } else {
- // SAFETY: `ptr` has been reallocated with the layout for `new_cap` elements. New cap
- // is greater than `cap`, so it continues to be >= `len`.
- unsafe { rebuild(self, new_ptr.cast::<T>(), len, new_cap) };
- Ok(())
+ match unsafe { alloc.realloc(ptr.cast(), cap, layout, flags) } {
+ Ok(ptr) => {
+ // SAFETY: `ptr` has been reallocated with the layout for `new_cap` elements.
+ // `new_cap` is greater than `cap`, so it continues to be >= `len`.
+ unsafe { rebuild(self, ptr.as_ptr().cast(), len, new_cap) };
+ Ok(())
+ }
+ Err(err) => {
+ // SAFETY: We are just rebuilding the existing `Vec` with no changes.
+ unsafe { rebuild(self, old_ptr, len, cap) };
+ Err(err)
+ }
}
}
}
--
2.45.2
Powered by blists - more mailing lists