[PATCH v21 9/9] rust: page: add `ExclusivePage` for race-free page access
Andreas Hindborg
a.hindborg at kernel.org
Sun Sep 13 09:36:53 UTC 2026
Matthew Wilcox <willy at infradead.org> writes:
> On Thu, Sep 10, 2026 at 11:00:13AM +0200, Andreas Hindborg wrote:
>> `ExclusivePage` wraps a regular page but adds an invariant that the
>> page data area does not incur data races. This means `ExclusivePage`
>> cannot be mapped to user space or shared with devices, and it
>> becomes simpler to directly reference the contents of the page.
>>
>> Since `Page` implements `AlwaysRefCounted`, handing out a `&Page`
>> from an `ExclusivePage` would allow safe code to obtain an
>> `ARef<Page>` to the page and break the aliasing invariant of
>> `ExclusivePage`. Thus, do not implement `Deref<Target = Page>` for
>> `ExclusivePage`.
>
> Given this description, I wonder if this shouldn't be implemented by
> alloc_frozen_pages() / free_frozen_pages(). We'ree certainly trying
> to move the C parts of the kernel away from thinking that pages have a
> refcount (folios have a refcount; otheer memdescs may or may not have
> a refcount). Do your intended users of this actually have a need for a
> refcount?
I'm not familiar with that API, but I will take a look. The
`ExclusivePage` would not need a refcount. The reference is intended to
be exclusive, giving mutable access to the page contents.
I could imagine a situation where someone might want to transition a
page reference from exclusive to shared, but I do not have a user for
that at the moment.
>
>> +++ b/rust/kernel/page.rs
>> @@ -17,7 +17,11 @@
>> AlwaysRefCounted,
>> RefCounted, //
>> },
>> - types::Opaque,
>> + types::{
>> + Opaque,
>> + Ownable,
>> + Owned, //
>> + },
>
> While I'm asking stupid questions, what's the significance of the //
> after Owned? I see it after RefCounted too.
It is a hack to get rustfmt to format the import statements in a Linus
compatible manner [1]. As far as I know, it will go away once rustfmt
learns to do this by itself.
Best regards,
Andreas Hindborg
[1] https://docs.kernel.org/rust/coding-guidelines.html#imports
More information about the Linux-security-module-archive
mailing list