From: Andreas Hindborg <a.hindborg@kernel.org>
To: "Alice Ryhl" <aliceryhl@google.com>,
"Lorenzo Stoakes" <ljs@kernel.org>,
"Liam R. Howlett" <liam@infradead.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>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
"Daniel Almeida" <daniel.almeida@collabora.com>,
"Tamir Duberstein" <tamird@kernel.org>,
"Alexandre Courbot" <acourbot@nvidia.com>,
"Onur Özkan" <work@onurozkan.dev>,
"Will Deacon" <will@kernel.org>,
"Peter Zijlstra" <peterz@infradead.org>,
"Mark Rutland" <mark.rutland@arm.com>,
"Lyude Paul" <lyude@redhat.com>,
"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>, "Zi Yan" <ziy@nvidia.com>
Cc: linux-mm@kvack.org, rust-for-linux@vger.kernel.org,
linux-kernel@vger.kernel.org, driver-core@lists.linux.dev,
Andreas Hindborg <a.hindborg@kernel.org>
Subject: [PATCH 08/11] rust: io: add `SysMemLocal`
Date: Fri, 20 Nov 2026 11:27:07 +0100 [thread overview]
Message-ID: <20261120-page-shared-v1-8-66f8c4c62822@kernel.org> (raw)
In-Reply-To: <20261120-page-shared-v1-0-66f8c4c62822@kernel.org>
A `SysMem` view is `Send` and `Sync`. Thus, it cannot safely represent
memory that is only accessible to the current task, such as a page that is
mapped with a mapping that is local to the current task.
Make `SysMem` and `SysMemBackend` generic over a new marker trait
`SysMemScope`, with the scopes `AnyTask` and `CurrentTask`. `AnyTask` is
the default, so existing users are not affected. Only a `SysMem` of the
scope `AnyTask` is `Send` and `Sync`. All other functionality is shared
between the scopes.
Add the alias `SysMemLocal` for a `SysMem` of the scope `CurrentTask`,
together with the constructor `SysMemLocal::new_local()` and a conversion
from a `SysMem` of the default scope.
Make `SysMem::fill_zero()` and `UserSliceReader::read_sysmem()` available
for a byte view of any scope.
Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
---
rust/kernel/io.rs | 142 ++++++++++++++++++++++++++++++++++++++++++-------
rust/kernel/uaccess.rs | 4 +-
2 files changed, 125 insertions(+), 21 deletions(-)
diff --git a/rust/kernel/io.rs b/rust/kernel/io.rs
index 33cb5a9fb3a8..1496735c08a2 100644
--- a/rust/kernel/io.rs
+++ b/rust/kernel/io.rs
@@ -1436,11 +1436,41 @@ pub fn relaxed(self) -> RelaxedMmio<'a, T> {
#[cfg(CONFIG_64BIT)]
impl_mmio_io_capable!(RelaxedMmioBackend, u64, readq_relaxed, writeq_relaxed);
+mod private {
+ pub trait Sealed {}
+}
+
+/// Marker trait for the scopes of a [`SysMem`].
+///
+/// The scope of a [`SysMem`] describes which tasks the view can be used on. See [`AnyTask`] and
+/// [`CurrentTask`].
+pub trait SysMemScope: private::Sealed {}
+
+/// The default [`SysMemScope`]: the view can be used on any task.
+///
+/// A [`SysMem`] of this scope is [`Send`] and [`Sync`] when its target is [`Sync`].
+pub enum AnyTask {}
+
+impl private::Sealed for AnyTask {}
+impl SysMemScope for AnyTask {}
+
+/// A [`SysMemScope`] for a view that can only be used on the task that created it.
+///
+/// This is the scope of a view whose memory is only accessible to the current task, such as a
+/// view that is backed by a mapping that is local to the current task. A [`SysMem`] of this scope,
+/// that is, a [`SysMemLocal`], is neither [`Send`] nor [`Sync`].
+pub enum CurrentTask {}
+
+impl private::Sealed for CurrentTask {}
+impl SysMemScope for CurrentTask {}
+
/// I/O Backend for system memory.
-pub struct SysMemBackend;
+///
+/// The backend is generic over the [`SysMemScope`] of its views.
+pub struct SysMemBackend<S: SysMemScope = AnyTask>(PhantomData<S>);
-impl IoBackend for SysMemBackend {
- type View<'a, T: ?Sized + KnownSize> = SysMem<'a, T>;
+impl<S: SysMemScope> IoBackend for SysMemBackend<S> {
+ type View<'a, T: ?Sized + KnownSize> = SysMem<'a, T, S>;
#[inline]
fn as_ptr<'a, T: ?Sized + KnownSize>(view: Self::View<'a, T>) -> *mut T {
@@ -1466,9 +1496,9 @@ unsafe fn project_view<'a, T: ?Sized + KnownSize, U: ?Sized + KnownSize>(
/// `write_volatile`.
macro_rules! impl_sysmem_io_capable {
($ty:ty) => {
- impl IoCapable<$ty> for SysMemBackend {
+ impl<S: SysMemScope> IoCapable<$ty> for SysMemBackend<S> {
#[inline]
- fn io_read(view: SysMem<'_, $ty>) -> $ty {
+ fn io_read(view: SysMem<'_, $ty, S>) -> $ty {
// SAFETY:
// - Per type invariant, `ptr` is valid and aligned.
// - Using read_volatile() here so that race with hardware is well-defined.
@@ -1480,7 +1510,7 @@ fn io_read(view: SysMem<'_, $ty>) -> $ty {
}
#[inline]
- fn io_write(view: SysMem<'_, $ty>, value: $ty) {
+ fn io_write(view: SysMem<'_, $ty, S>, value: $ty) {
// SAFETY:
// - Per type invariant, `ptr` is valid and aligned.
// - Using write_volatile() here so that race with hardware is well-defined.
@@ -1499,7 +1529,7 @@ fn io_write(view: SysMem<'_, $ty>, value: $ty) {
#[cfg(CONFIG_64BIT)]
impl_sysmem_io_capable!(u64);
-impl IoCopyable for SysMemBackend {
+impl<S: SysMemScope> IoCopyable for SysMemBackend<S> {
#[inline]
unsafe fn copy_from_io(view: Self::View<'_, [u8]>, buffer: *mut u8) {
// SAFETY:
@@ -1562,31 +1592,103 @@ fn copy_write<T: IntoBytes>(view: Self::View<'_, T>, value: T) {
/// Accesses performed by hardware or by user space are not plain accesses in this sense. Racing
/// with them is well-defined, but the data observed may be arbitrary.
///
+/// A `SysMem` is generic over a [`SysMemScope`], which describes the tasks that the view can be
+/// used on. A view of the default scope [`AnyTask`] can be sent to and shared with other tasks. A
+/// view of the scope [`CurrentTask`], a [`SysMemLocal`], cannot leave the task that created it.
+/// Apart from that, the two are identical.
+///
/// # Invariants
///
/// - `self.ptr.addr() .. self.ptr.addr() + KnownSize::size(self.ptr)` is valid and aligned kernel
-/// accessible memory region for the lifetime `'a`.
+/// accessible memory region for the lifetime `'a`. For a view of the scope [`CurrentTask`],
+/// this holds on the task that created the view.
/// - No plain access to that memory region races with an access performed through this view.
-pub struct SysMem<'a, T: ?Sized> {
+pub struct SysMem<'a, T: ?Sized, S: SysMemScope = AnyTask> {
ptr: *mut T,
- phantom: PhantomData<&'a ()>,
+ phantom: PhantomData<(&'a (), S)>,
}
-impl<T: ?Sized> Copy for SysMem<'_, T> {}
-impl<T: ?Sized> Clone for SysMem<'_, T> {
+/// A view of a system memory region that can only be used on the task that created it.
+///
+/// This is a [`SysMem`] that is neither [`Send`] nor [`Sync`]. It is used for memory that is only
+/// accessible to the current task, such as a page that is mapped with a mapping that is local to
+/// the current task.
+///
+/// A [`SysMem`] of the default scope converts to a `SysMemLocal` with [`From`].
+///
+/// # Examples
+///
+/// ```
+/// use kernel::io::{
+/// Io,
+/// SysMem,
+/// SysMemLocal, //
+/// };
+///
+/// let mut data = [0u8; 4];
+///
+/// // SAFETY: `data` is valid for the lifetime of `mem`, and it is not accessed in any other way
+/// // while `mem` is alive.
+/// let mem = unsafe { SysMem::new(&raw mut data as *mut [u8]) };
+/// let local = SysMemLocal::from(mem);
+///
+/// local.copy_from_slice(&[1, 2, 3, 4]);
+/// local.fill_zero();
+///
+/// let mut buf = [0xffu8; 4];
+/// local.copy_to_slice(&mut buf);
+/// assert_eq!(buf, [0u8; 4]);
+/// ```
+pub type SysMemLocal<'a, T> = SysMem<'a, T, CurrentTask>;
+
+impl<T: ?Sized, S: SysMemScope> Copy for SysMem<'_, T, S> {}
+impl<T: ?Sized, S: SysMemScope> Clone for SysMem<'_, T, S> {
#[inline]
fn clone(&self) -> Self {
*self
}
}
-// SAFETY: `SysMem<'_, T>` is conceptually `&T`.
-unsafe impl<T: ?Sized + Sync> Send for SysMem<'_, T> {}
+// SAFETY: `SysMem<'_, T>` is conceptually `&T`. A view of the scope `AnyTask` can be used on any
+// task.
+unsafe impl<T: ?Sized + Sync> Send for SysMem<'_, T, AnyTask> {}
-// SAFETY: `SysMem<'_, T>` is conceptually `&T`.
-unsafe impl<T: ?Sized + Sync> Sync for SysMem<'_, T> {}
+// SAFETY: `SysMem<'_, T>` is conceptually `&T`. A view of the scope `AnyTask` can be used on any
+// task.
+unsafe impl<T: ?Sized + Sync> Sync for SysMem<'_, T, AnyTask> {}
-impl SysMem<'_, [u8]> {
+impl<'a, T: ?Sized> From<SysMem<'a, T>> for SysMemLocal<'a, T> {
+ #[inline]
+ fn from(value: SysMem<'a, T>) -> Self {
+ // INVARIANT: By the type invariant of `value`, the memory region is valid on all tasks,
+ // and no plain access races with an access through a view of it.
+ Self {
+ ptr: value.ptr,
+ phantom: PhantomData,
+ }
+ }
+}
+
+impl<'a, T: ?Sized> SysMemLocal<'a, T> {
+ /// Create a `SysMemLocal` from a raw pointer.
+ ///
+ /// # Safety
+ ///
+ /// - `ptr.addr() .. ptr.addr() + KnownSize::size(ptr)` must be valid and aligned kernel
+ /// accessible memory region on the current task for the lifetime `'a`.
+ /// - No plain access (see [`SysMem`]) to that memory region may race with an access performed
+ /// through the returned view.
+ #[inline]
+ pub unsafe fn new_local(ptr: *mut T) -> Self {
+ // INVARIANT: Per safety requirement.
+ Self {
+ ptr,
+ phantom: PhantomData,
+ }
+ }
+}
+
+impl<S: SysMemScope> SysMem<'_, [u8], S> {
/// Writes zeroes to all bytes of the view using byte-wise atomic operations.
#[inline]
pub fn fill_zero(self) {
@@ -1614,7 +1716,9 @@ pub unsafe fn new(ptr: *mut T) -> Self {
phantom: PhantomData,
}
}
+}
+impl<T: ?Sized, S: SysMemScope> SysMem<'_, T, S> {
/// Obtain the raw pointer to the memory.
#[inline]
pub fn as_ptr(self) -> *mut T {
@@ -1622,8 +1726,8 @@ pub fn as_ptr(self) -> *mut T {
}
}
-impl<'a, T: ?Sized + KnownSize> IoBase<'a> for SysMem<'a, T> {
- type Backend = SysMemBackend;
+impl<'a, T: ?Sized + KnownSize, S: SysMemScope> IoBase<'a> for SysMem<'a, T, S> {
+ type Backend = SysMemBackend<S>;
type Target = T;
#[inline]
diff --git a/rust/kernel/uaccess.rs b/rust/kernel/uaccess.rs
index 313479d80834..6956a7cc519a 100644
--- a/rust/kernel/uaccess.rs
+++ b/rust/kernel/uaccess.rs
@@ -11,7 +11,7 @@
error::Result,
ffi::{c_char, c_void},
fs::file,
- io::SysMem,
+ io::{SysMem, SysMemScope},
prelude::*,
ptr::KnownSize,
transmute::{AsBytes, FromBytes},
@@ -315,7 +315,7 @@ pub unsafe fn read_raw(&mut self, out: *mut u8, len: usize) -> Result {
/// reader.read_sysmem(mem)
/// }
/// ```
- pub fn read_sysmem(&mut self, out: SysMem<'_, [u8]>) -> Result {
+ pub fn read_sysmem<S: SysMemScope>(&mut self, out: SysMem<'_, [u8], S>) -> Result {
let out = out.as_ptr();
// SAFETY: By the type invariant of `SysMem`, the memory of `out` is valid for writing
--
2.54.0
next prev parent reply other threads:[~2026-11-20 10:27 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-20 10:26 [PATCH 00/11] rust: page: improve access methods for racy memory regions Andreas Hindborg
2026-11-20 10:27 ` [PATCH 01/11] rust: page: tighten safety requirements of `SysMem` copy methods Andreas Hindborg
2026-11-20 10:27 ` [PATCH 02/11] rust: sync: atomic: add memset Andreas Hindborg
2026-11-20 10:27 ` [PATCH 03/11] rust: io: add `SysMem::fill_zero` Andreas Hindborg
2026-11-20 10:27 ` [PATCH 04/11] rust: page: use byte-wise atomic operations in raw accessors Andreas Hindborg
2026-11-20 10:27 ` [PATCH 05/11] rust: uaccess: take a raw pointer in `UserSliceReader::read_raw` Andreas Hindborg
2026-11-20 10:27 ` [PATCH 06/11] rust: uaccess: add `UserSliceReader::read_sysmem` Andreas Hindborg
2026-11-20 10:27 ` [PATCH 07/11] rust: page: add `Shared` page kind Andreas Hindborg
2026-11-20 10:27 ` Andreas Hindborg [this message]
2026-11-20 10:27 ` [PATCH 09/11] rust: page: add `Page::with_page_as_sysmem_local` Andreas Hindborg
2026-11-20 10:27 ` [PATCH 10/11] rust_binder: access page data through `SysMemLocal` Andreas Hindborg
2026-11-20 10:27 ` [PATCH 11/11] rust: page: remove `Page::copy_from_user_slice_raw` 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=20261120-page-shared-v1-8-66f8c4c62822@kernel.org \
--to=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=aliceryhl@google.com \
--cc=arve@android.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=brauner@kernel.org \
--cc=cmllamas@google.com \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=driver-core@lists.linux.dev \
--cc=gary@garyguo.net \
--cc=gregkh@linuxfoundation.org \
--cc=liam@infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ljs@kernel.org \
--cc=lossin@kernel.org \
--cc=lyude@redhat.com \
--cc=mark.rutland@arm.com \
--cc=ojeda@kernel.org \
--cc=peterz@infradead.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tamird@kernel.org \
--cc=tkjos@android.com \
--cc=tmgross@umich.edu \
--cc=will@kernel.org \
--cc=work@onurozkan.dev \
--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