[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <033c72ba-fb31-4eff-b98c-5c5d35057280@proton.me>
Date: Sat, 06 Jul 2024 10:37:00 +0000
From: Benno Lossin <benno.lossin@...ton.me>
To: Danilo Krummrich <dakr@...hat.com>, ojeda@...nel.org, alex.gaynor@...il.com, wedsonaf@...il.com, boqun.feng@...il.com, gary@...yguo.net, bjorn3_gh@...tonmail.com, 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
Subject: Re: [PATCH 04/20] rust: alloc: implement `Allocator` for `Kmalloc`
On 04.07.24 19:06, Danilo Krummrich wrote:
> @@ -48,20 +57,54 @@ pub(crate) unsafe fn krealloc_aligned(ptr: *mut u8, new_layout: Layout, flags: F
> }
> }
>
> +unsafe impl Allocator for Kmalloc {
> + unsafe fn realloc(
> + &self,
> + old_ptr: *mut u8,
> + _old_size: usize,
> + layout: Layout,
> + flags: Flags,
> + ) -> Result<NonNull<[u8]>, AllocError> {
> + let size = aligned_size(layout);
> +
> + // SAFETY: `src` is guaranteed to point to valid memory with a size of at least
> + // `old_size`, which was previously allocated with this `Allocator` or NULL.
> + let raw_ptr = unsafe {
> + // If `size == 0` and `old_ptr != NULL` `krealloc()` frees the memory behind the
> + // pointer.
> + bindings::krealloc(old_ptr.cast(), size, flags.0).cast()
> + };
> +
> + let ptr = if size == 0 {
> + NonNull::dangling()
> + } else {
> + NonNull::new(raw_ptr).ok_or(AllocError)?
> + };
> +
> + Ok(NonNull::slice_from_raw_parts(ptr, size))
> + }
> +}
> +
> unsafe impl GlobalAlloc for Kmalloc {
Since you remove `alloc` entirely at the end of this series, do we even
need this?
If not, it would be best to just leave the implementation as-is until a
patch removes it entirely.
---
Cheers,
Benno
> 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) }
> + let this: &dyn Allocator = self;
> +
> + match this.alloc(layout, GFP_KERNEL) {
> + Ok(ptr) => ptr.as_ptr().cast(),
> + Err(_) => ptr::null_mut(),
> + }
> }
Powered by blists - more mailing lists