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 05/11] rust: uaccess: take a raw pointer in `UserSliceReader::read_raw`
Date: Fri, 20 Nov 2026 11:27:04 +0100 [thread overview]
Message-ID: <20261120-page-shared-v1-5-66f8c4c62822@kernel.org> (raw)
In-Reply-To: <20261120-page-shared-v1-0-66f8c4c62822@kernel.org>
`UserSliceReader::read_raw()` takes the destination as a mutable slice.
Thus, it cannot be used to copy into memory that is subject to concurrent
accesses, such as a page that is mapped to user space, since a mutable
reference to such memory must not exist.
Change `UserSliceReader::read_raw()` to take the destination as a raw
pointer and a length, and make it unsafe. The copy is done by
`copy_from_user()`, which uses byte-wise atomic store instructions for the
kernel memory, so the destination only has to be valid for atomic writes.
Update the callers. `UserSliceReader::read_slice()` remains as the safe
method for copying into a slice.
`Page::copy_from_user_slice_raw()` no longer creates a mutable slice of
the data of the page, and thus no longer writes to the page with plain
accesses. Remove the remark about this from its safety requirements.
Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
---
rust/kernel/page.rs | 9 ++++-----
rust/kernel/uaccess.rs | 47 ++++++++++++++++++++++++++++++++---------------
2 files changed, 36 insertions(+), 20 deletions(-)
diff --git a/rust/kernel/page.rs b/rust/kernel/page.rs
index ff191bfcd3f9..643af7cfbef4 100644
--- a/rust/kernel/page.rs
+++ b/rust/kernel/page.rs
@@ -600,8 +600,7 @@ pub unsafe fn fill_zero_raw(&self, offset: usize, len: usize) -> Result {
/// # Safety
///
/// * Callers must ensure that this call does not race with a read or write to the same page
- /// that overlaps with this write. This includes byte-wise atomic accesses, as this method
- /// writes to the page with plain accesses.
+ /// that overlaps with this write.
/// * Callers must ensure that no reference to the data of the page that overlaps with this
/// write exists during this call. Such references can be obtained from a
/// [`Page<Exclusive>`], see [`Page::as_slice`].
@@ -613,9 +612,9 @@ pub unsafe fn copy_from_user_slice_raw(
) -> Result {
self.with_pointer_into_page(offset, len, move |dst| {
// SAFETY: If `with_pointer_into_page` calls into this closure, then it has performed a
- // bounds check and guarantees that `dst` is valid for `len` bytes. Furthermore, we have
- // exclusive access to the slice since the caller guarantees that there are no races.
- reader.read_raw(unsafe { core::slice::from_raw_parts_mut(dst.cast(), len) })
+ // bounds check and guarantees that `dst` is valid for `len` bytes. The caller
+ // guarantees that there are no races, so `dst` is valid for atomic writes.
+ unsafe { reader.read_raw(dst, len) }
})
}
}
diff --git a/rust/kernel/uaccess.rs b/rust/kernel/uaccess.rs
index f1aecda7a90c..b1df79500b98 100644
--- a/rust/kernel/uaccess.rs
+++ b/rust/kernel/uaccess.rs
@@ -252,25 +252,38 @@ pub fn is_empty(&self) -> bool {
self.length == 0
}
- /// Reads raw data from the user slice into a kernel buffer.
+ /// Reads raw data from the user slice into the kernel memory at `out`.
///
- /// For a version that uses `&mut [u8]`, please see [`UserSliceReader::read_slice`].
+ /// The writes to `out` are byte-wise atomic, so they are well-defined even if the memory at
+ /// `out` is concurrently accessed by hardware, by user space, or by other kernel code using
+ /// atomic or volatile accesses. For a safe version that uses `&mut [u8]`, please see
+ /// [`UserSliceReader::read_slice`].
///
/// Fails with [`EFAULT`] if the read happens on a bad address, or if the read goes out of
- /// bounds of this [`UserSliceReader`]. This call may modify `out` even if it returns an error.
+ /// bounds of this [`UserSliceReader`]. This call may modify the memory at `out` even if it
+ /// returns an error.
+ ///
+ /// Implementation note: this is implemented by the kernel's `copy_from_user()`, which is
+ /// implemented in a way such that byte-wise atomic memory store instructions are used.
///
/// # Guarantees
///
- /// After a successful call to this method, all bytes in `out` are initialized.
- pub fn read_raw(&mut self, out: &mut [MaybeUninit<u8>]) -> Result {
- let len = out.len();
- let out_ptr = out.as_mut_ptr().cast::<c_void>();
+ /// This method only writes initialized bytes to `out`. After a successful call to this method,
+ /// all `len` bytes at `out` are initialized.
+ ///
+ /// # Safety
+ ///
+ /// Callers must ensure that `out` is valid for atomic writes for `len` bytes for the duration
+ /// of the call.
+ pub unsafe fn read_raw(&mut self, out: *mut u8, len: usize) -> Result {
if len > self.length {
return Err(EFAULT);
}
- // SAFETY: `out_ptr` points into a mutable slice of length `len`, so we may write
- // that many bytes to it.
- let res = unsafe { bindings::copy_from_user(out_ptr, self.ptr.as_const_ptr(), len) };
+ // SAFETY: By the safety requirements of this function, `out` is valid for atomic writes
+ // for `len` bytes. `copy_from_user()` is a byte-wise atomic operation on the kernel
+ // memory.
+ let res =
+ unsafe { bindings::copy_from_user(out.cast::<c_void>(), self.ptr.as_const_ptr(), len) };
if res != 0 {
return Err(EFAULT);
}
@@ -284,10 +297,10 @@ pub fn read_raw(&mut self, out: &mut [MaybeUninit<u8>]) -> Result {
/// Fails with [`EFAULT`] if the read happens on a bad address, or if the read goes out of
/// bounds of this [`UserSliceReader`]. This call may modify `out` even if it returns an error.
pub fn read_slice(&mut self, out: &mut [u8]) -> Result {
- // SAFETY: The types are compatible and `read_raw` doesn't write uninitialized bytes to
- // `out`.
- let out = unsafe { &mut *(core::ptr::from_mut(out) as *mut [MaybeUninit<u8>]) };
- self.read_raw(out)
+ // SAFETY: `out` is a mutable slice, so it is valid for writing `out.len()` bytes, and
+ // nothing else accesses it during this call. `read_raw` doesn't write uninitialized bytes
+ // to `out`.
+ unsafe { self.read_raw(out.as_mut_ptr(), out.len()) }
}
/// Reads raw data from the user slice into a kernel buffer partially.
@@ -372,7 +385,11 @@ pub fn read_all<A: Allocator>(mut self, buf: &mut Vec<u8, A>, flags: Flags) -> R
buf.reserve(len, flags)?;
// The call to `reserve` was successful, so the spare capacity is at least `len` bytes long.
- self.read_raw(&mut buf.spare_capacity_mut()[..len])?;
+ let spare = &mut buf.spare_capacity_mut()[..len];
+
+ // SAFETY: `spare` is a mutable slice, so it is valid for writing `spare.len()` bytes, and
+ // nothing else accesses it during this call.
+ unsafe { self.read_raw(spare.as_mut_ptr().cast(), spare.len()) }?;
// SAFETY: Since the call to `read_raw` was successful, so the next `len` bytes of the
// vector have been initialized.
--
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 ` Andreas Hindborg [this message]
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 ` [PATCH 08/11] rust: io: add `SysMemLocal` Andreas Hindborg
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-5-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
all inboxes | Powered by JetHome®