From: Zi Yan <ziy@nvidia.com>
To: Andreas Hindborg <a.hindborg@kernel.org>,
Matthew Wilcox <willy@infradead.org>,
David Hildenbrand <david@kernel.org>
Cc: "Andrew Morton" <akpm@linux-foundation.org>,
"Vlastimil Babka" <vbabka@kernel.org>,
"Lorenzo Stoakes" <ljs@kernel.org>,
"Liam R. Howlett" <liam@infradead.org>,
"Uladzislau Rezki" <urezki@gmail.com>,
"Suren Baghdasaryan" <surenb@google.com>,
"Michal Hocko" <mhocko@suse.com>,
"Brendan Jackman" <brendan.jackman@linux.dev>,
"Johannes Weiner" <hannes@cmpxchg.org>,
"Mike Rapoport" <rppt@kernel.org>,
"Joshua Hahn" <joshua.hahnjy@gmail.com>,
"Rakie Kim" <rakie.kim@sk.com>,
"Gregory Price" <gourry@gourry.net>,
"Ying Huang" <ying.huang@linux.alibaba.com>,
"Danilo Krummrich" <dakr@kernel.org>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Daniel Almeida" <daniel.almeida@collabora.com>,
"Tamir Duberstein" <tamird@kernel.org>,
"Alexandre Courbot" <acourbot@nvidia.com>,
"Onur Özkan" <work@onurozkan.dev>,
"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
"Arve Hjønnevåg" <arve@android.com>,
"Todd Kjos" <tkjos@android.com>,
"Christian Brauner" <brauner@kernel.org>,
"Carlos Llamas" <cmllamas@google.com>,
"Igor B" <igor.b@beldev.am>,
"Vitaly Wool" <vitaly.wool@konsulko.se>,
linux-mm@kvack.org, rust-for-linux@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH v22 0/6] rust: page: add page kinds
Date: Mon, 30 Nov 2026 17:15:04 -0500 [thread overview]
Message-ID: <513DF0BA-DC37-4A90-ADD7-9FCCCFC00623@nvidia.com> (raw)
In-Reply-To: <874id3vqd4.fsf@t14s.mail-host-address-is-not-set>
On 26 Nov 2026, at 5:28, Andreas Hindborg wrote:
> Zi Yan <ziy@nvidia.com> writes:
>
>> On 20 Nov 2026, at 7:03, Andreas Hindborg wrote:
>>
>>> Zi Yan <ziy@nvidia.com> writes:
>>>
>>>> On 18 Nov 2026, at 14:34, Andreas Hindborg wrote:
>>>>
>>>>> Zi Yan <ziy@nvidia.com> writes:
>>>>>
>>>>>> On 18 Nov 2026, at 11:18, Andreas Hindborg wrote:
>>>>>>
>>>>>>> Note: The page patches were part of the `Ownable` series [1] until v21.
>>>>>>> This series continues the version numbering of that series, and it depends
>>>>>>> on v22 of that series for `Ownable`, `Owned` and the split of `RefCounted`
>>>>>>> and `AlwaysRefCounted`.
>>>>>>>
>>>>>>> Not all pages can be treated the same way. A page allocated with
>>>>>>> `alloc_pages()` is managed by the page reference count, and kernel C code
>>>>>>> routinely takes its own references to such a page. A page owned by the
>>>>>>> slab allocator must not have references taken to it, but it can still
>>>>>>> show up in places such as a bio vector. A frozen page has a reference
>>>>>>> count of zero and is exclusively owned by the party that allocated it.
>>>>>>>
>>>>>>> A single `Page` type cannot describe all of these. This series makes
>>>>>>> `Page` generic over a sealed `PageKind` that describes what is known about
>>>>>>> the page:
>>>>>>>
>>>>>>> - `Page<Base>` is the base kind. Nothing is known about the page
>>>>>>> beyond the validity of the `struct page`.
>>>>>>> - `Page<RefCounted>` is the default kind. The page is managed by the
>>>>>>> page reference count, and `Page<RefCounted>` implements
>>>>>>> `AlwaysRefCounted`.
>>>>>>> - `Page<Exclusive>` is a uniquely owned frozen page whose data area
>>>>>>> follows standard Rust aliasing rules, so the data is available as
>>>>>>> `&[u8]` and `&mut [u8]`. It is handled through
>>>>>>> `Owned<Page<Exclusive>>` and is the first in-tree user of `Owned`.
>>>>>>> - `Page<Frozen>` is a frozen page whose allocation is uniquely owned.
>>>>>>> Unlike for `Page<Exclusive>`, nothing is known about who accesses the
>>>>>>> data of the page, so the data is only available through the unsafe
>>>>>>> raw accessors.
>>>>>>
>>>>>> Why do we need to allocate Page<Frozen> when we can allocate Page<Exclusive>?
>>>>>> To me, Page<Frozen> is an observed page state from a non-owner point
>>>>>> of view.
>>>>>
>>>>> I'm not sure we need it, which is why I added this note:
>>>>>
>>>>> This currently has no users, but I add it here for completeness. Let's
>>>>> discuss if we should keep it or not.
>>>>>
>>>>> The difference between the `Page<Frozen>` and `Page<Exclusive>` is that
>>>>> `Page<Exclusive>` has stronger invariants. Namely that the (physical)
>>>>> memory region of the page does not incur data races, and that the page
>>>>> not in highmem. From a Rust point of view, this allows us to obtain a
>>>>> reference to the page contents without jumping through any hoops.
>>>>
>>>> But access to memory region of the page is associated with page table
>>>> mapping or IOMMU mapping (for device, and I assume devices are accessing
>>>> data via virtual addresses instead of physical addresses to avoid further
>>>> complication). It seems that you are trying to use page refcount == 0
>>>> to infer that no page table mapping exists for the page. But how about
>>>> kernel direct map?
>>>
>>> `Page<Exclusive>` has the invariant that it must not be in highmem, and
>>> as such is always accessible in the kernel virtual address space. As far
>>> as I understand, this means we do not need `kmap_local` before accessing
>>> the contents.
>>>
>>>> Anyone with struct page can use kmap_local*()
>>>> to get kernel direct map and access the page content. kmap_local*()
>>>> does not reject frozen pages.
>>>
>>> `Page<Exclusive>` is a unique handle. Nobody can get a hold of the
>>> underlying `struct page` to perform mapping operations on the page.
>>>
>>
>> Right, but if kernel direct map exists, how can you claim
>> “the (physical) memory region of the page does not incur data races”?
>> At least, “assuming no one is accessing the region via kernel direct
>> map” could be added.
>
> I think I see the disconnect. Let me see if I can explain.
>
> In Rust, when you allocate memory that is not reference counted, you get
> a handle to it. In safe Rust, you are the only one with that handle. The
> type system makes sure that you can only write through this handle if
> you did not share it with anyone. All this is ensured statically, and
> the handle compiles down to a pointer at runtime. `Page<Exclusive>` is
> such a handle. Assuming absence of unsafe Rust and C code, the allocated
> memory is race free.
>
> In the kernel of course we have C code and any thread in the kernel can
> access any piece of memory mapped into the address space, including all
> physical memory via the direct map (assuming no highmem). But that is
> not how we generally write software. If we want to access the contents
> of a frozen `struct page`, we will get the pointer to it from somewhere,
> probably an allocator. Probably we will call `alloc_frozen_pages()`. And
> assuming the allocator is correct, it will not hand out the same page
> twice, at least not until it is freed first.
>
> So the page backing `Page<Exclusive>` comes from `alloc_frozen_pages`,
> and since this function will not hand out pages that are already in use,
> we can claim that the page contents is free of data races. If this is
> not the case, there is a bug in the kernel. Pages handed out by
> `alloc_frozen_pages` should not be in use.
>
> And because of the type system, users of `Page<Exclusive>` can only
> write the page contents if the compiler can assert that the handle is
> unique. This is called an exclusive reference, or a mutable reference,
> in Rust.
>
> They `Page<Exclusive>` type makes sure that the page cannot be used in a
> way that would cause the page contents to incur data races. It cannot be
> inserted into a vma, for instance.
>
> So the guarantee comes from ownership of the allocation, not from mm
> preventing direct map access.
Thank you for the explanation. It matches my understanding. These Page
types would only concerns about Rust side, assuming C code respects
the exclusive ownership. My point is that this assumption should be
written in the comment for clarification, so in the future when a bug
appears, one will check C code directly. Or this assumption is implicit
since Rust type system guarantees the correctness of Rust code?
>
>>
>>>>
>>>>>
>>>>> I do not have a user for a frozen page without these invariants, but I
>>>>> would assume there are use cases?
>>>>
>>>> For the above reason, everything is Page<Frozen>?
>>>
>>> Pages that are part of user space process vmas are not frozen? We see
>>> these in system calls for IO.
>>
>> Sorry I was too vague. I mean Page<Exclusive> and Page<Frozen> should
>> be Page<Frozen> given that kernel direct map exists. Strictly speaking,
>> Page<Exclusive> should be pages allocated without kernel direct mapping.
>
> It is OK for the pages to be direct mapped. In fact I think they _have_
> to be, otherwise we would not be able to access their content.
OK. Should this be documented clearly in the comment?
>
>>
>>>
>>>>
>>>>>
>>>>>> In core-mm, a page is frozen, when its refcount == 0. The code looking
>>>>>> at the page knows the page is exclusively owned by others. If the code
>>>>>> allocates a frozen page, it will not check its refcount, because
>>>>>> the code knows it has the exclusive ownership of the page. To reflect
>>>>>> this, the code calling alloc_frozen_pages() should just get
>>>>>> Page<Exclusive>
>>>>>
>>>>> Is it always the case that users of frozen pages will use them in such a
>>>>> way that they can guarantee no races to the page contents (physical
>>>>> memory)? They cannot be mapped for DMA access or anything like that -
>>>>> that always requires a refcount?
>>>>
>>>> See kmap_local*() case above.
>>>
>>> Right. Both `Page<Frozen>` and `Page<Exclusive>` are guaranteed to be
>>> the only reference the underlying `struct page`, and so nobody can do
>>> mapping operations from under us. The difference of the two is that
>>> `Page<Exclusive>` provides guarantees about placement out of highmem and
>>> no races on the page contents.
>>>
>>
>> Sorry for being pedantic, but IMHO “no races on the page content” is
>> too strong. LLM tells me, let me know if it is wrong, that DMA map
>> code does not check page refcount and currently gives a dma_addr_t
>> for slab and kmalloc pages, which are frozen. This means a Page<Exclusive>
>> can be mapped via dma map C code. What is the plan to prevent that?
>
> `Page<Exclusive>` cannot be mapped for dma with safe Rust code. We use
> the Rust type system to ensure this. We use different types to express
> different capabilities.
>
> (Future) Rust DMA mapping APIs may not take `Page<Exclusive>` or
> `Page<Base>` as arguments.
Does this imply that Rust code does not and will not hand this page to
C code? The claim should be restricted to Safe Rust. Especially for
“A Page<Exclusive> ... Thus, the exclusive ownership of the page is
upheld by the kernel, and not only by the Rust type system.”,
core-mm does not uphold the exclusive ownership of the page, since
a frozen page just means no one else can take a reference on the page.
>
>>
>>>>
>>>>>
>>>>>> and if the code encounters a Page<Base> and finds it has no refcount,
>>>>>> it should convert it to Page<Frozen> or there should be a method
>>>>>> that accepts PFN and return Page<Frozen> if the page of the PFN has no
>>>>>> refcount, for PFN walkers.
>>>>>
>>>>> We can add conversion functions, that makes sense.
>>>>>
>>>>>>
>>>>>> “the data” mentioned in Page<Exclusive> and Page<Frozen> is struct page
>>>>>> data or the data in the physical frame pointed by Page<...>? If it
>>>>>> is the former, is it better to use metadata?
>>>>>
>>>>> I'm referring to the actual physical memory region that the struct page
>>>>> describes.
>>>>>
>>>>>>
>>>>>> I am new to Rust, so feel free to correct me. Thanks. :)
>>>>>>
>>>>>
>>>>> I'm new to mm, I really appreciate your eyes on this.
>>>>
>>>> Do you mind describing the high level workflow around Page in Rust?
>>>> From my point of view, exclusive page content access is more related
>>>> to page table (including IOMMU page table) than struct page itself.
>>>> struct page ownership is more about the metadata stored in struct
>>>> page for physical memory maintenance.
>>>
>>> Just as in C code, there are many workflows around `Page`. Three main
>>> ones I have seen in Rust code are:
>>>
>>> - Pages that are part of io buffers during a syscall. The pages are
>>> mapped in at least one user space process and the kernel may want to
>>> read or write from/to them. These would be `Page<RefCounted>`.
>>
>> Is RefCounted too broad for this? A refcounted page might or might not
>> be mapped and a mapped page might not might not be mapped into userspace.
>> Is UserMapped more descriptive?
>
> I think for the invariants we put on `Page<RefCounted>` it does not
> matter whether the page is mapped or not. The fact that it _may_ be
> mapped is enough. If we need to figure out whether a page is mapped or
> not in order to give access to particular functionality, we can add
> another type for that. This pattern is some times referred to as
> typestates.
Got it.
>
>>
>> FYI, I am working on adding PG_folio to distinguish user rmappable
>> folios from general mapped pages. In terms of pages mapped into
>> userspace process, they can be either rmappable or vm_insert*()
>> into userspace.
>
> For now we do not have any infrastructure for handling folios in Rust.
> We only handle order 0 regular pages. But I imagine we will need it
> eventually.
>
>>
>>>
>>> - Pages that hit the block device driver API via the block layer. These
>>> can be anything, so we model them as `Page<Base>`.
>>
>> Even they can be frozen without a refcount?
>
> Yes. Quick robot overview on v7.4-rc2 (I did not verify all of it):
>
> Kernel buffers from kmalloc() live on frozen pages (refcount 0). The
> block layer turns such a buffer into a bio_vec with plain
> virt_to_page() and never takes a page reference, so the frozen page
> ends up in bi_io_vec unchanged.
>
> 1. Where frozen pages come from
>
> SLUB allocates the backing pages for all slabs as frozen pages:
>
> - Normal slabs: mm/slub.c:3379-3384, allocate_slab() →
> alloc_frozen_pages() / __alloc_frozen_pages().
> - Large kmalloc (bigger than KMALLOC_MAX_CACHE_SIZE):
> mm/slub.c:5474-5477 calls alloc_frozen_pages_noprof() and then
> __SetPageLargeKmalloc(page).
>
> So every kmalloc()'d buffer sits on a page whose _refcount is 0,
> whether it comes from a slab or the large-kmalloc path. The pages are
> freed again with free_frozen_pages().
There is a caveat here, __SetPageLargeKmalloc(page). It sets a
page type to the frozen page. It helps one check page ownership.
>
>>
>>>
>>> - Pages used for backing storage like in the null block driver. These
>>> are modeled as `Page<Exclusive>`.
>>
>> null_alloc_page() uses alloc_pages() and gets refcount == 1 pages.
>> unless you mean struct nullb_page.
>
> Yes, the C null block driver uses reference counted pages. The Rust null
> block driver implementation will use frozen pages. Matthew suggested
> this approach.
This worries me, because
1. willy, David, and I have been talking about a new page type for
device allocated memory to separate it from rmappable folio (anon
or pagecache folios) and this using frozen pages is different;
2. it introduces a new long-live frozen page (exclude zone device memory), whereas existing frozen pages without a page type are either transient or
marked by PG_hwpoison. The transient ones are
a) PCP pages,
b) compaction’s cc->freepages[],
c) temporarily isolated free pages.
I am not sure core-mm has code properly handles this new long-live
frozen page. At least, page_is_unmovable() used by memory offline
needs to handle this kind of frozen page, since it assumes
frozen and untyped pages are free or about to be freed and can lead
offline_pages() looping on them. See commit 213268869301f ("mm:
page_isolation: do not take slab pages for free") for more context.
David and willy, what is your take on this? Either we assign a
page type to Page<Exclusive> or still uses refcounted pages.
Best Regards,
Yan, Zi
next prev parent reply other threads:[~2026-11-30 22:15 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-18 16:18 [PATCH v22 0/6] rust: page: add page kinds Andreas Hindborg
2026-11-18 16:18 ` [PATCH v22 1/6] rust: page: add page kinds and make `Page` reference counted Andreas Hindborg
2026-11-19 16:20 ` sashiko-bot
2026-11-18 16:18 ` [PATCH v22 2/6] rust: page: add `from_raw()` Andreas Hindborg
2026-11-18 16:18 ` [PATCH v22 3/6] mm/page_alloc: make alloc_frozen_pages() available outside of mm Andreas Hindborg
2026-11-18 16:18 ` [PATCH v22 4/6] rust: page: add `Exclusive` page kind for race-free page access Andreas Hindborg
2026-11-18 16:18 ` [PATCH v22 5/6] rust: page: add `Frozen` page kind Andreas Hindborg
2026-11-18 16:18 ` [PATCH v22 6/6] rust: page: add slice access to `Page<Exclusive>` Andreas Hindborg
2026-11-18 17:02 ` [PATCH v22 0/6] rust: page: add page kinds Zi Yan
2026-11-18 19:34 ` Andreas Hindborg
2026-11-19 16:58 ` Zi Yan
2026-11-20 12:03 ` Andreas Hindborg
2026-11-20 16:03 ` Zi Yan
2026-11-26 10:28 ` Andreas Hindborg
2026-11-30 22:15 ` Zi Yan [this message]
2026-12-01 10:35 ` Andreas Hindborg
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=513DF0BA-DC37-4A90-ADD7-9FCCCFC00623@nvidia.com \
--to=ziy@nvidia.com \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=akpm@linux-foundation.org \
--cc=aliceryhl@google.com \
--cc=arve@android.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=brauner@kernel.org \
--cc=brendan.jackman@linux.dev \
--cc=cmllamas@google.com \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=david@kernel.org \
--cc=gary@garyguo.net \
--cc=gourry@gourry.net \
--cc=gregkh@linuxfoundation.org \
--cc=hannes@cmpxchg.org \
--cc=igor.b@beldev.am \
--cc=joshua.hahnjy@gmail.com \
--cc=liam@infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ljs@kernel.org \
--cc=lossin@kernel.org \
--cc=mhocko@suse.com \
--cc=ojeda@kernel.org \
--cc=rakie.kim@sk.com \
--cc=rppt@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=surenb@google.com \
--cc=tamird@kernel.org \
--cc=tkjos@android.com \
--cc=tmgross@umich.edu \
--cc=urezki@gmail.com \
--cc=vbabka@kernel.org \
--cc=vitaly.wool@konsulko.se \
--cc=willy@infradead.org \
--cc=work@onurozkan.dev \
--cc=ying.huang@linux.alibaba.com \
/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.