From: Andreas Hindborg <a.hindborg@kernel.org>
To: "Alice Ryhl" <aliceryhl@google.com>,
"Lorenzo Stoakes" <lorenzo.stoakes@oracle.com>,
"Liam R. Howlett" <Liam.Howlett@oracle.com>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>
Cc: linux-mm@kvack.org, rust-for-linux@vger.kernel.org,
linux-kernel@vger.kernel.org,
Andreas Hindborg <a.hindborg@kernel.org>
Subject: [PATCH 1/2] rust: page: add `SafePage` for race-free page access
Date: Sun, 15 Feb 2026 21:03:30 +0100 [thread overview]
Message-ID: <20260215-page-additions-v1-1-4827790a9bc4@kernel.org> (raw)
In-Reply-To: <20260215-page-additions-v1-0-4827790a9bc4@kernel.org>
`SafePage` wraps a regular page but adds an invariant that the page data
area does not incur data races. This means `SafePage` cannot be mapped to
user space or shared with devices, and it becomes simpler to directly
reference the contents of the page.
Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
---
rust/kernel/page.rs | 69 +++++++++++++++++++++++++++++++++++++++++++++++------
1 file changed, 62 insertions(+), 7 deletions(-)
diff --git a/rust/kernel/page.rs b/rust/kernel/page.rs
index 803f3e3d76b22..af6d2ad408ed7 100644
--- a/rust/kernel/page.rs
+++ b/rust/kernel/page.rs
@@ -8,8 +8,10 @@
Flags, //
},
bindings,
- error::code::*,
- error::Result,
+ error::{
+ code::*,
+ Result, //
+ },
types::{
Opaque,
Ownable,
@@ -20,7 +22,7 @@
use core::{
marker::PhantomData,
mem::ManuallyDrop,
- ops::Deref,
+ ops::{Deref, DerefMut},
ptr::{
self,
NonNull, //
@@ -147,6 +149,13 @@ unsafe impl Send for Page {}
unsafe impl Sync for Page {}
impl Page {
+ fn alloc_page_raw(flags: Flags) -> Result<NonNull<bindings::page>, AllocError> {
+ // SAFETY: Depending on the value of `gfp_flags`, this call may sleep. Other than that, it
+ // is always safe to call this method.
+ let page = unsafe { bindings::alloc_pages(flags.as_raw(), 0) };
+ NonNull::new(page).ok_or(AllocError)
+ }
+
/// Allocates a new page.
///
/// # Examples
@@ -170,10 +179,7 @@ impl Page {
/// ```
#[inline]
pub fn alloc_page(flags: Flags) -> Result<Owned<Self>, AllocError> {
- // SAFETY: Depending on the value of `gfp_flags`, this call may sleep. Other than that, it
- // is always safe to call this method.
- let page = unsafe { bindings::alloc_pages(flags.as_raw(), 0) };
- let page = NonNull::new(page).ok_or(AllocError)?;
+ let page = Self::alloc_page_raw(flags)?;
// SAFETY: We just successfully allocated a page, so we now have ownership of the newly
// allocated page. We transfer that ownership to the new `Owned<Page>` object.
// Since `Page` is transparent, we can cast the pointer directly.
@@ -378,3 +384,52 @@ unsafe fn release(this: NonNull<Self>) {
unsafe { bindings::__free_pages(this.cast().as_ptr(), 0) };
}
}
+
+/// A page whose data area does not incur data races.
+///
+/// [`SafePage`] has the same usage constraints as other Rust types. Thus, it cannot be mapped to
+/// user space or shared with devices. This makes it safe to reference the contents of the page
+/// while the page is mapped in kernel space.
+///
+/// # Invariants
+///
+/// There are no data races for the contents of this page.
+#[repr(transparent)]
+pub struct SafePage(Page);
+
+impl SafePage {
+ /// Allocate a new `SafePage`.
+ pub fn alloc_page(flags: Flags) -> Result<Owned<Self>, AllocError> {
+ let page = Page::alloc_page_raw(flags)?;
+
+ // SAFETY: We just successfully allocated a page, so we now have ownership of the newly
+ // allocated page. We transfer that ownership to the new `Owned<Page>` object.
+ // Since `Page` and `SafePage` is transparent, we can cast the pointer directly.
+ Ok(unsafe { Owned::from_raw(page.cast()) })
+ }
+}
+
+// SAFETY: `Owned<SafePage>` objects returned by SafePage::alloc_page() follow the requirements of
+// the Ownable abstraction.
+unsafe impl Ownable for SafePage {
+ #[inline]
+ unsafe fn release(this: NonNull<Self>) {
+ // SAFETY: By the type invariants, we have ownership of the page and can free it. Since
+ // `SafePage` and `Page` are transparent, we can cast the raw pointer directly.
+ unsafe { bindings::__free_pages(this.cast().as_ptr(), 0) };
+ }
+}
+
+impl Deref for SafePage {
+ type Target = Page;
+
+ fn deref(&self) -> &Self::Target {
+ &self.0
+ }
+}
+
+impl DerefMut for SafePage {
+ fn deref_mut(&mut self) -> &mut Self::Target {
+ &mut self.0
+ }
+}
--
2.51.2
next prev parent reply other threads:[~2026-02-15 20:04 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-02-15 20:03 [PATCH 0/2] rust: pages that cannot be racy Andreas Hindborg
2026-02-15 20:03 ` Andreas Hindborg [this message]
2026-02-16 8:52 ` [PATCH 1/2] rust: page: add `SafePage` for race-free page access Alice Ryhl
2026-02-15 20:03 ` [PATCH 2/2] rust: page: add method to copy data between safe pages Andreas Hindborg
2026-02-15 22:33 ` Miguel Ojeda
2026-02-15 23:40 ` Andreas Hindborg
2026-02-17 21:35 ` Miguel Ojeda
2026-02-18 9:37 ` Andreas Hindborg
2026-02-18 11:41 ` Miguel Ojeda
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=20260215-page-additions-v1-1-4827790a9bc4@kernel.org \
--to=a.hindborg@kernel.org \
--cc=Liam.Howlett@oracle.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=dakr@kernel.org \
--cc=gary@garyguo.net \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=lorenzo.stoakes@oracle.com \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
/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