From: Danilo Krummrich <dakr@kernel.org>
To: Abdiel Janulgue <abdiel.janulgue@gmail.com>
Cc: rust-for-linux@vger.kernel.org, aliceryhl@google.com,
dakr@redhat.com, linux-kernel@vger.kernel.org,
airlied@redhat.com, miguel.ojeda.sandonis@gmail.com,
boqun.feng@gmail.com
Subject: Re: [PATCH v2 3/5] rust: page: Extend support to vmalloc_to_page
Date: Wed, 23 Oct 2024 11:03:52 +0200 [thread overview]
Message-ID: <Zxi7-Gaf0U3xRdQt@pollux> (raw)
In-Reply-To: <Zxi287W_MJcMB4YO@pollux>
On Wed, Oct 23, 2024 at 10:42:27AM +0200, Danilo Krummrich wrote:
> On Wed, Oct 23, 2024 at 01:44:47AM +0300, Abdiel Janulgue wrote:
> > Extend Page to support pages that are not allocated by the constructor, for
> > example, those returned by vmalloc_to_page(). Since we don't own those pages
> > we shouldn't Drop them either. Hence we take advantage of the switch to Opaque
> > so we can cast to a Page pointer from a struct page pointer and be able to
> > retrieve the reference on an existing struct page mapping. In this case
> > no destructor will be called since we are not instantiating a new Page instance.
> >
> > Signed-off-by: Abdiel Janulgue <abdiel.janulgue@gmail.com>
> > ---
> > rust/kernel/page.rs | 32 ++++++++++++++++++++++++++++++--
> > 1 file changed, 30 insertions(+), 2 deletions(-)
> >
> > diff --git a/rust/kernel/page.rs b/rust/kernel/page.rs
> > index a8288c15b860..465928986f4b 100644
> > --- a/rust/kernel/page.rs
> > +++ b/rust/kernel/page.rs
> > @@ -31,11 +31,12 @@ pub const fn page_align(addr: usize) -> usize {
> > (addr + (PAGE_SIZE - 1)) & PAGE_MASK
> > }
> >
> > -/// A pointer to a page that owns the page allocation.
> > +/// A pointer to a page that may own the page allocation.
> > ///
> > /// # Invariants
> > ///
> > -/// The pointer is valid, and has ownership over the page.
> > +/// The pointer is valid, and has ownership over the page if the page is allocated by this
> > +/// abstraction.
> > #[repr(transparent)]
> > pub struct Page {
> > page: Opaque<bindings::page>,
> > @@ -88,6 +89,33 @@ pub fn alloc_page(flags: Flags) -> Result<Owned<Self>, AllocError> {
> > Ok(unsafe { Owned::to_owned(ptr) })
> > }
> >
> > + /// This is just a wrapper to vmalloc_to_page which returns an existing page mapping, hence
>
> In documentation, try to avoid filler words, such as "just". Better say
> something like:
>
> "This is an abstraction around the C `vmalloc_to_page()` function. Note that by
> a call to this function the caller doesn't take ownership of the returned `Page`
> [...]."
>
> > + /// we don't take ownership of the page. Returns an error if the pointer is null or if it
> > + /// is not returned by vmalloc().
> > + pub fn vmalloc_to_page<'a>(
> > + cpu_addr: *const core::ffi::c_void
>
> When you have a raw pointer argument in your function it becomes unsafe by
> definition.
Actually, this was phrased badly, the pointer must also be dereferenced by the
function in some way to become unsafe (which `vmalloc_to_page` does).
>
> I also think it would also be better to pass a `NonNull<u8>` instead.
>
> > + ) -> Result<&'a Self, AllocError>
>
> Please don't use `AllocError`. We're not allocating anything here.
>
> Anyway, do we need this as a separate function at all?
>
> > + {
> > + if cpu_addr.is_null() {
> > + return Err(AllocError);
> > + }
> > + // SAFETY: We've checked that the pointer is not null, so it is safe to call this method.
> > + if unsafe { !bindings::is_vmalloc_addr(cpu_addr) } {
> > + return Err(AllocError);
> > + }
> > + // SAFETY: We've initially ensured the pointer argument to this function is not null and
> > + // checked for the requirement the the buffer passed to it should be allocated by vmalloc,
> > + // so it is safe to call this method.
More specifically, `is_vmalloc_addr()` only checks that the address is between
`VMALLOC_START` and `VMALLOC_END`, but not whether it's pointing to a valid
allocation.
So, this isn't safe unless you make it a safety requirement of your function.
> > + let page = unsafe { bindings::vmalloc_to_page(cpu_addr) };
> > + if page.is_null() {
> > + return Err(AllocError);
> > + }
>
> I think those should all return `EINVAL` instead.
>
> > + // CAST: `Self` is a `repr(transparent)` wrapper around `bindings::page`.
> > + // SAFETY: We just successfully allocated a page, therefore dereferencing
> > + // the page pointer is valid.
> > + Ok(unsafe { &*page.cast() })
> > + }
> > +
> > /// Returns a raw pointer to the page.
> > pub fn as_ptr(&self) -> *mut bindings::page {
> > self.page.get()
> > --
> > 2.43.0
> >
>
next prev parent reply other threads:[~2024-10-23 9:03 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-22 22:44 [PATCH v2 0/5] Introduce Owned type and Ownable trait (was: "rust: page: Add support for vmalloc_to_page") Abdiel Janulgue
2024-10-22 22:44 ` [PATCH v2 1/5] rust: types: add `Owned` type and `Ownable` trait Abdiel Janulgue
2024-10-23 8:10 ` Danilo Krummrich
2024-10-23 9:28 ` Alice Ryhl
2024-10-23 10:26 ` Abdiel Janulgue
2024-10-23 14:58 ` Boqun Feng
2024-10-23 17:52 ` Alice Ryhl
2024-10-23 18:07 ` Boqun Feng
2024-10-24 7:23 ` Alice Ryhl
2024-10-24 7:33 ` Alice Ryhl
2024-11-01 13:38 ` Abdiel Janulgue
2024-11-01 13:49 ` Alice Ryhl
2024-10-22 22:44 ` [PATCH v2 2/5] rust: page: Make ownership of the page pointer explicit Abdiel Janulgue
2024-10-22 22:44 ` [PATCH v2 3/5] rust: page: Extend support to vmalloc_to_page Abdiel Janulgue
2024-10-23 8:42 ` Danilo Krummrich
2024-10-23 9:03 ` Danilo Krummrich [this message]
2024-10-23 10:26 ` Abdiel Janulgue
2024-10-23 11:30 ` Danilo Krummrich
2024-10-22 22:44 ` [PATCH v2 4/5] rust: page: Add page_slice_to_page Abdiel Janulgue
2024-10-22 22:44 ` [PATCH v2 5/5] rust: firmware: implement `Ownable` for Firmware Abdiel Janulgue
2024-10-23 9:35 ` Danilo Krummrich
2024-10-23 9:45 ` Danilo Krummrich
2024-10-27 22:20 ` Boqun Feng
2024-10-28 13:37 ` Danilo Krummrich
2024-10-23 8:03 ` [PATCH v2 0/5] Introduce Owned type and Ownable trait (was: "rust: page: Add support for vmalloc_to_page") Danilo Krummrich
2024-10-23 9:51 ` Miguel Ojeda
2024-10-23 12:01 ` Danilo Krummrich
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=Zxi7-Gaf0U3xRdQt@pollux \
--to=dakr@kernel.org \
--cc=abdiel.janulgue@gmail.com \
--cc=airlied@redhat.com \
--cc=aliceryhl@google.com \
--cc=boqun.feng@gmail.com \
--cc=dakr@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).