[PATCH v2 0/2] rust: pages that cannot be racy

Andreas Hindborg posted 2 patches 2 days, 15 hours ago
rust/kernel/page.rs | 121 +++++++++++++++++++++++++++++++++++++++++++++++-----
1 file changed, 110 insertions(+), 11 deletions(-)
[PATCH v2 0/2] rust: pages that cannot be racy
Posted by Andreas Hindborg 2 days, 15 hours ago
Some drivers might want to use pages for data storage in the same way
one would use an array of u8. The current page cannot be used for this
purpose, as it provides no guarantees for how it is accessed.

This series adds a newtype around `Page` with an additional invariant
that the data of the page does not incur races. This makes it possible
to treat the page as a regular array and even obtain a slice into the
page.

Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
---
Changes in v2:
- Reimplement `Page::alloc_page()` in terms of `SafePage::alloc_page()`
  to drop the duplicated raw allocation helper (Alice).
- Reword the `SafePage` invariant to spell out the standard Rust
  aliasing rules that apply to its data (Alice, Miguel).
- Replace the `# Arguments` list in `copy_to_page` with a prose
  description, matching the prevailing style of the kernel crate
  (Miguel).
- Expand the `copy_to_page` doctest to populate the source page,
  assert that the copy succeeds and produced the expected bytes, and
  demonstrate the out-of-bounds error path (Miguel).
- Reword the SAFETY justification in `copy_to_page` from "no other
  writes" to "no writes" on the source side, since this call only
  reads from `src` (Miguel).
- Rebase on v7.1-rc2.
- Link to v1: https://msgid.link/20260215-page-additions-v1-0-4827790a9bc4@kernel.org

To: Alice Ryhl <aliceryhl@google.com>
To: Lorenzo Stoakes <ljs@kernel.org>
To: "Liam R. Howlett" <liam@infradead.org>
To: Miguel Ojeda <ojeda@kernel.org>
To: Boqun Feng <boqun@kernel.org>
To: Gary Guo <gary@garyguo.net>
To: Björn Roy Baron <bjorn3_gh@protonmail.com>
To: Benno Lossin <lossin@kernel.org>
To: Andreas Hindborg <a.hindborg@kernel.org>
To: Trevor Gross <tmgross@umich.edu>
To: Danilo Krummrich <dakr@kernel.org>
Cc: linux-mm@kvack.org
Cc: rust-for-linux@vger.kernel.org
Cc: linux-kernel@vger.kernel.org

---
Andreas Hindborg (2):
      rust: page: add `SafePage` for race-free page access
      rust: page: add method to copy data between safe pages

 rust/kernel/page.rs | 121 +++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 110 insertions(+), 11 deletions(-)
---
base-commit: 9e0898f1c0f134c6bad146ca8578f73c3e40ac0a
change-id: 20260215-page-additions-bc36046e9ffd
prerequisite-change-id: 20250305-unique-ref-29fcd675f9e9:v17
prerequisite-patch-id: 6c6a7fdd56627293ec3bba61c495f16a0858700c
prerequisite-patch-id: c1958590235ee32d6ddb31ea168105bd9cf248f2
prerequisite-patch-id: c5a4b231dc8adf37e93ebdce308dacbe6a244bf3
prerequisite-patch-id: 541dba7938ba874f8d17fee05a36b1cd9fa2c4d7
prerequisite-patch-id: 3668fd640e4c411bae0c8ea9d986c3fa5d3c9e82
prerequisite-patch-id: da1274864841e267697be9529a50531126c64872
prerequisite-patch-id: c1463b6578e94b56d2bad41f6e614b5286fb1db3
prerequisite-patch-id: a31185fe1abbf553377d6d695c5d206eebc84358
prerequisite-patch-id: 4f392b5736e55a354ec3022644389f89b52fda42
prerequisite-patch-id: b6388ff0ebdd54610010d72a5398842a3c668bbf

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