[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAH5fLghhgrM_EyCkmHSZ=VpQZSvg3Co+HHYyAU9+gssMkKGNhA@mail.gmail.com>
Date: Thu, 1 Aug 2024 14:48:52 +0200
From: Alice Ryhl <aliceryhl@...gle.com>
To: Danilo Krummrich <dakr@...nel.org>
Cc: 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, akpm@...ux-foundation.org,
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,
linux-mm@...ck.org
Subject: Re: [PATCH v3 09/25] rust: alloc: implement kernel `Box`
On Thu, Aug 1, 2024 at 2:45 PM Danilo Krummrich <dakr@...nel.org> wrote:
>
> On Thu, Aug 01, 2024 at 10:55:51AM +0200, Alice Ryhl wrote:
> > On Thu, Aug 1, 2024 at 2:07 AM Danilo Krummrich <dakr@...nel.org> wrote:
> > > + /// Converts a `Box<T>` into a `Pin<Box<T>>`.
> > > + #[inline]
> > > + pub fn into_pin(b: Self) -> Pin<Self>
> > > + where
> > > + A: 'static,
> > > + {
> > > + // SAFETY: It's not possible to move or replace the insides of a `Pin<Box<T>>` when
> > > + // `T: !Unpin`, so it's safe to pin it directly without any additional requirements.
> > > + unsafe { Pin::new_unchecked(b) }
> > > + }
> >
> > In the standard library, this functionality is provided using the From
> > trait rather than an inherent method. I think it makes sense to match
> > std here.
>
> I already provide `impl<T, A> From<Box<T, A>> for Pin<Box<T, A>>` in this patch,
> which just calls `Box::into_pin`.
Ah, ok, I might drop into_pin and only have From, but I'm ok either way.
> > > +impl<T: 'static, A> ForeignOwnable for crate::alloc::Box<T, A>
> > > +where
> > > + A: Allocator,
> > > +{
> > > + type Borrowed<'a> = &'a T;
> > > +
> > > + fn into_foreign(self) -> *const core::ffi::c_void {
> > > + crate::alloc::Box::into_raw(self) as _
> > > + }
> > > +
> > > + unsafe fn borrow<'a>(ptr: *const core::ffi::c_void) -> &'a T {
> > > + // SAFETY: The safety requirements for this function ensure that the object is still alive,
> > > + // so it is safe to dereference the raw pointer.
> > > + // The safety requirements of `from_foreign` also ensure that the object remains alive for
> > > + // the lifetime of the returned value.
> > > + unsafe { &*ptr.cast() }
> > > + }
> > > +
> > > + unsafe fn from_foreign(ptr: *const core::ffi::c_void) -> Self {
> > > + // SAFETY: The safety requirements of this function ensure that `ptr` comes from a previous
> > > + // call to `Self::into_foreign`.
> > > + unsafe { crate::alloc::Box::from_raw(ptr as _) }
> > > + }
> > > +}
> >
> > You may want to also implement ForeignOwnable for Pin<Box<T>>. See:
> > https://lore.kernel.org/all/20240730-foreign-ownable-pin-box-v1-1-b1d70cdae541@google.com/
>
> Yeah, I think I've also seen another patch that it about to add a function to
> convert a `Box` back into uninit state.
>
> Depending how fast you need ForeignOwnable for Pin<Box<T>>, do you prefer to
> contribute a corresponding patch to this series?
It's easiest for me if you just add it to this patch of your series. I
don't care about credit in this particular case.
Alice
Powered by blists - more mailing lists