Re: [PATCH v7 4/4] rust: add abstraction for `struct page`

From: Alice Ryhl
Date: Tue Jun 11 2024 - 04:51:44 EST


On Mon, Jun 10, 2024 at 10:47 PM Abdiel Janulgue <abdiel@xxxxxxxxxx> wrote:
>
> Hi,
>
> On 28/05/2024 17:58, Alice Ryhl wrote:
> > Adds a new struct called `Page` that wraps a pointer to `struct page`.
> > This struct is assumed to hold ownership over the page, so that Rust
> > code can allocate and manage pages directly.
> >
> > +
> > +impl Drop for Page {
> > + fn drop(&mut self) {
> > + // SAFETY: By the type invariants, we have ownership of the page and can free it.
> > + unsafe { bindings::__free_pages(self.page.as_ptr(), 0) };
> > + }
> > +}
> >
>
> What about cases where the struct page pointer is not owned or allocated
> by this wrapper? For example, pages returned vmalloc_to_page()?
> Any thoughts about exposing a provision to avoid freeing those pages
> during Drop?
>
> We've been experimenting in adapting this Page wrapper in advance for
> page management within the Nova DRM driver.

That would make a lot of sense, but this Page wrapper doesn't support
it. You are very welcome to extend it.

There are essentially two ways to go about it:

1. Change the Page struct to be a `Opaque<bindings::page>` and use the
types &Page and BoxLikeType<Page> for these two purposes. Here,
BoxLikeType would be a new type that is to Box in the same way as how
ARef is to Arc.
2. Introduce a new PageRef<'a> type that's like a reference to a page.
You can have Page deref to PageRef so that they share methods.

The second solution is easiest right now. The first solution is
probably what we want long-term.

Alice