mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Andreas Hindborg <a.hindborg@kernel.org>
To: "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>,
	"David Hildenbrand" <david@kernel.org>,
	"Suren Baghdasaryan" <surenb@google.com>,
	"Michal Hocko" <mhocko@suse.com>,
	"Brendan Jackman" <brendan.jackman@linux.dev>,
	"Johannes Weiner" <hannes@cmpxchg.org>, "Zi Yan" <ziy@nvidia.com>,
	"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>,
	"Matthew Wilcox" <willy@infradead.org>,
	"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>,
	"Andreas Hindborg" <a.hindborg@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>
Cc: 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: [PATCH v22 0/6] rust: page: add page kinds
Date: Wed, 18 Nov 2026 17:18:16 +0100	[thread overview]
Message-ID: <20261118-page-ownable-v22-0-d63ff73f4075@kernel.org> (raw)

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.

Operations that are valid for any page are available for all page kinds.
All other page kinds dereference to `Page<Base>`. The purpose of the
dereference is coercion: a reference to a page of any kind can be passed
to code that takes a `&Page<Base>`.

`Page<Exclusive>` is backed by a frozen page, as suggested by Matthew
Wilcox in the review of v21. References cannot be taken to a frozen page,
so the exclusive ownership is upheld by mm rather than only by the Rust
type system.

Patch 1 adds the page kinds `Base` and `RefCounted`, makes
`Page<RefCounted>` reference counted, and updates the users of `Page`.

Patch 2 adds `Page::from_raw()`.

Patch 3 makes `alloc_frozen_pages()` and `free_frozen_pages()` available
outside of mm. The choice of `include/linux/gfp.h` as the new home of the
declarations is open for discussion.

Patch 4 adds the page kind `Exclusive`.

Patch 5 adds the page kind `Frozen`. It has no user in this series and
can be dropped if it is not wanted at this time.

Patch 6 adds slice access to the data of a `Page<Exclusive>`, and
tightens the safety requirements of the raw data accessors accordingly.

[1] https://msgid.link/20260910-unique-ref-v21-0-e83257373062@kernel.org

Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
---
Changes in v22:
- Split the `Page` patches out of the `Ownable` series.
- Make `Page` generic over a `PageKind`, with the kinds `Base`,
  `RefCounted`, `Frozen` and `Exclusive`. `Page<Exclusive>` replaces the
  `ExclusivePage` type (Andreas).
- Back `Page<Exclusive>` by a frozen page (Matthew).
- Add a patch that makes `alloc_frozen_pages()` and `free_frozen_pages()`
  available outside of mm.
- Add the page kind `Frozen` in a separate patch (Andreas).
- Return `&Page<Base>` from `Vmalloc::to_page()` and `AsPageIter`, as
  these pages are not known to be reference counted.
- Make `Page::from_raw()` available for all page kinds. The caller selects
  the kind and upholds its invariants.
- Rename `ExclusivePage::alloc_page()` to `Page::alloc_exclusive()`
  (Andreas).
- Let `Page<Exclusive>` dereference to `Page<Base>` (Andreas).
- Add `Page::as_slice()` and `Page::as_mut_slice()` for `Page<Exclusive>`
  in a separate patch. Always zero the page and never allocate it from
  highmem, and require callers of the raw data accessors to ensure that
  no conflicting reference to the data exists. Update the safety comments
  of the existing callers (Andreas).
- Justify the `AlwaysRefCounted` implementation for `Page` by all
  instances of the type being reference counted, rather than by the type
  not implementing `Ownable` (Alice, Gary).
- Tighten the safety requirements of `Page::from_raw()` to require, for
  `Page<RefCounted>`, that the page is reference counted and that taking
  additional references is permitted (Sashiko).
- Mark `Page::alloc_exclusive()` as `#[inline]` (Sashiko).
- Drop the Reviewed-by tag from the `Page::from_raw()` patch, as its
  safety requirements changed.
- Link to v21: https://msgid.link/20260910-unique-ref-v21-0-e83257373062@kernel.org

The entries below are the `Page` related changes from the change log of
the `Ownable` series.

Changes in v21:
- Import `Page` directly in `allocator.rs` instead of using the
  `page::Page` path (Danilo).
- Convert `Page` to `AlwaysRefCounted` backed by `get_page()` and
  `put_page()` instead of `Ownable`, and return `ARef<Page>` from
  `Page::alloc_page()` (Alice).
- Update Rust Binder to store `ARef<Page>` instead of `Owned<Page>`
  (Alice).
- Add `ExclusivePage` as the in-tree `Owned` user, without
  `Deref<Target = Page>` as that would break its aliasing invariant.
  The type was previously posted as `SafePage` and is renamed here
  (Alice).
- Link to v20: https://msgid.link/20260824-unique-ref-v20-0-490735672187@kernel.org

Changes in v20:
- Use `self.page.get()` rather than `Opaque::cast_into` (Alice).
- Link to v19: https://msgid.link/20260626-unique-ref-v19-0-2607ca88dfdf@kernel.org

Changes in v18:
- Remove `BorrowedPage`; use `&Page` directly (Alice).
- Update Rust Binder for the `Owned<Page>` conversion (Alice).
- Fix stale `Page` docs (Sashiko).
- Link to v17: https://msgid.link/20260604-unique-ref-v17-0-7b4c3d2930b9@kernel.org

Changes in v16:
- Simplify pointer to reference cast in `Page::from_raw`.
- Use `NonNull<Page>` rather than `Owned<Page>` for `BorrowedPage` internals.
- Fix formatting for `Page::from_raw` docs.
- Link to v15: https://msgid.link/20260220-unique-ref-v15-0-893ed86b06cc@kernel.org

Changes in v14:
- Add patch: rust: page: convert to `Ownable`.
- Add patch: rust: page: add `from_raw()`.
- Link to v13: https://lore.kernel.org/r/20251117-unique-ref-v13-0-b5b243df1250@pm.me

---
Andreas Hindborg (6):
      rust: page: add page kinds and make `Page` reference counted
      rust: page: add `from_raw()`
      mm/page_alloc: make alloc_frozen_pages() available outside of mm
      rust: page: add `Exclusive` page kind for race-free page access
      rust: page: add `Frozen` page kind
      rust: page: add slice access to `Page<Exclusive>`

 drivers/android/binder/page_range.rs |  24 +-
 include/linux/gfp.h                  |   9 +
 mm/mempolicy.c                       |   1 +
 mm/page_alloc.c                      |  10 +
 mm/page_alloc.h                      |  14 -
 rust/helpers/page.c                  |  21 ++
 rust/kernel/alloc/allocator.rs       |  29 ++-
 rust/kernel/alloc/allocator/iter.rs  |   7 +-
 rust/kernel/alloc/kbox.rs            |   3 +-
 rust/kernel/alloc/kvec.rs            |   3 +-
 rust/kernel/page.rs                  | 481 ++++++++++++++++++++++++++++-------
 11 files changed, 474 insertions(+), 128 deletions(-)
---
base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb
change-id: 20261112-page-ownable-2e97941a83b2
prerequisite-change-id: 20250305-unique-ref-29fcd675f9e9:v22
prerequisite-patch-id: 4e3c7a1a2077aaa0ece2d557fa73486355903ae5
prerequisite-patch-id: d1073c5dcd6287fbcba7848a5d182108930230c1
prerequisite-patch-id: 3244def11bbdf2eabaa6e94e15ce06540b2f5814
prerequisite-patch-id: cef14c5bef5ba519ceaf0a0e7c9e93ef974db7b3
prerequisite-patch-id: 56410da35ed8dbe7b210adae0985af705cc0b5b0

Best regards,
--  
Andreas Hindborg <a.hindborg@kernel.org>



             reply	other threads:[~2026-11-18 16:19 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18 16:18 Andreas Hindborg [this message]
2026-11-18 16:18 ` [PATCH v22 1/6] rust: page: add page kinds and make `Page` reference counted Andreas Hindborg
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

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=20261118-page-ownable-v22-0-d63ff73f4075@kernel.org \
    --to=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 \
    --cc=ziy@nvidia.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®