From: Boqun Feng <boqun.feng@gmail.com>
To: Abdiel Janulgue <abdiel.janulgue@gmail.com>
Cc: Alice Ryhl <aliceryhl@google.com>,
rust-for-linux@vger.kernel.org, dakr@redhat.com,
linux-kernel@vger.kernel.org, lyude@redhat.com,
airlied@redhat.com, miguel.ojeda.sandonis@gmail.com
Subject: Re: [PATCH 1/3] rust: page: replace the page pointer wrapper with Opaque
Date: Tue, 8 Oct 2024 04:33:46 -0700 [thread overview]
Message-ID: <ZwUYmunVpzpexGV8@boqun-archlinux> (raw)
In-Reply-To: <2b56118d-a0f1-470e-9e36-65811a87a177@gmail.com>
On Tue, Oct 08, 2024 at 02:29:47PM +0300, Abdiel Janulgue wrote:
>
>
> On 08/10/2024 10:04, Boqun Feng wrote:
> > On Tue, Oct 08, 2024 at 08:58:56AM +0200, Alice Ryhl wrote:
> > > On Mon, Oct 7, 2024 at 10:28 PM Abdiel Janulgue
> > > <abdiel.janulgue@gmail.com> wrote:
> > > >
> > > > Replace NonNull with Opaque to make it possible to cast to a Page pointer
> > > > from a raw struct page pointer.
> > > >
> > > > Signed-off-by: Abdiel Janulgue <abdiel.janulgue@gmail.com>
> > > > ---
> > > > rust/kernel/page.rs | 19 +++++++++++++------
> > > > 1 file changed, 13 insertions(+), 6 deletions(-)
> > > >
> > > > diff --git a/rust/kernel/page.rs b/rust/kernel/page.rs
> > > > index 208a006d587c..08ff09a25223 100644
> > > > --- a/rust/kernel/page.rs
> > > > +++ b/rust/kernel/page.rs
> > > > @@ -8,8 +8,9 @@
> > > > error::code::*,
> > > > error::Result,
> > > > uaccess::UserSliceReader,
> > > > + types::Opaque,
> > > > };
> > > > -use core::ptr::{self, NonNull};
> > > > +use core::ptr::{self};
> > > >
> > > > /// A bitwise shift for the page size.
> > > > pub const PAGE_SHIFT: usize = bindings::PAGE_SHIFT as usize;
> > > > @@ -25,8 +26,9 @@
> > > > /// # Invariants
> > > > ///
> > > > /// The pointer is valid, and has ownership over the page.
> > > > +#[repr(transparent)]
> > > > pub struct Page {
> > > > - page: NonNull<bindings::page>,
> > > > + page: Opaque<bindings::page>,
> > > > }
> > > >
> > > > // SAFETY: Pages have no logic that relies on them staying on a given thread, so moving them across
> > > > @@ -65,15 +67,20 @@ pub fn alloc_page(flags: Flags) -> Result<Self, AllocError> {
> > > > // SAFETY: Depending on the value of `gfp_flags`, this call may sleep. Other than that, it
> > > > // is always safe to call this method.
> > > > let page = unsafe { bindings::alloc_pages(flags.as_raw(), 0) };
> > > > - let page = NonNull::new(page).ok_or(AllocError)?;
> > > > + if page.is_null() {
> > > > + return Err(AllocError);
> > > > + }
> > > > + // CAST: Self` is a `repr(transparent)` wrapper around `bindings::page`.
> > > > + let ptr = page.cast::<Self>();
> > > > // INVARIANT: We just successfully allocated a page, so we now have ownership of the newly
> > > > // allocated page. We transfer that ownership to the new `Page` object.
> > > > - Ok(Self { page })
> > > > + // SAFETY: According to invariant above ptr is valid.
> > > > + Ok(unsafe { ptr::read(ptr) })
> > >
> > > Using `ptr::read` on the page is definitely not okay. That duplicates
> > > the contents of the `struct page`. You'll need some sort of pointer
> > > type around `Page` instead.
> > >
> >
> > Agreed. So may I suggest we introduce `Owned` type and `Ownable` trait
> > [1]? `alloc_page()` can be refactor to return a `Result<Owned<Self>,
> > AllocError>`.
> >
> > [1]: https://lore.kernel.org/rust-for-linux/ZnCzLIly3DRK2eab@boqun-archlinux/
>
> Thanks for the feedback. How do you propose we move forward, do I take a
> stab at implementing `Owned` type and `Ownable` trait?
If you're interested, go ahead ;-)
Regards,
Boqun
>
> Regards,
> Abdiel
>
>
> >
> > Regards,
> > Boqun
> >
> > > Alice
> >
next prev parent reply other threads:[~2024-10-08 11:35 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-07 20:27 rust: page: Add support for vmalloc_to_page Abdiel Janulgue
2024-10-07 20:27 ` [PATCH 1/3] rust: page: replace the page pointer wrapper with Opaque Abdiel Janulgue
2024-10-08 6:58 ` Alice Ryhl
2024-10-08 7:04 ` Boqun Feng
2024-10-08 11:29 ` Abdiel Janulgue
2024-10-08 11:33 ` Boqun Feng [this message]
2024-10-11 11:07 ` Fiona Behrens
2024-10-15 13:21 ` Alice Ryhl
2024-10-07 20:27 ` [PATCH 2/3] rust: page: Extend support to vmalloc_to_page Abdiel Janulgue
2024-10-07 20:27 ` [PATCH 3/3] rust: page: Add page_slice_to_page Abdiel Janulgue
2024-10-11 11:03 ` kernel test robot
2024-10-07 20:58 ` rust: page: Add support for vmalloc_to_page Miguel Ojeda
2024-10-08 11:29 ` Abdiel Janulgue
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ZwUYmunVpzpexGV8@boqun-archlinux \
--to=boqun.feng@gmail.com \
--cc=abdiel.janulgue@gmail.com \
--cc=airlied@redhat.com \
--cc=aliceryhl@google.com \
--cc=dakr@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=lyude@redhat.com \
--cc=miguel.ojeda.sandonis@gmail.com \
--cc=rust-for-linux@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.