[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <ZoklmPl0P+D5Fa+N@pollux.localdomain>
Date: Sat, 6 Jul 2024 13:08:08 +0200
From: Danilo Krummrich <dakr@...hat.com>
To: Benno Lossin <benno.lossin@...ton.me>
Cc: 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,
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 Sat, Jul 06, 2024 at 10:37:00AM +0000, Benno Lossin wrote:
> 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.
I may have done this to not break the rusttest target, but I think with what I
ended up with, this shouldn't be required anymore and can be removed in a single
patch at the end of the series.
>
> ---
> 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