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>,
"Will Deacon" <will@kernel.org>,
"Peter Zijlstra" <peterz@infradead.org>,
"Mark Rutland" <mark.rutland@arm.com>,
"Daniel Almeida" <daniel.almeida@collabora.com>,
"Tamir Duberstein" <tamird@kernel.org>,
"Alexandre Courbot" <acourbot@nvidia.com>,
"Onur Özkan" <work@onurozkan.dev>,
"Lyude Paul" <lyude@redhat.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 v5 3/4] rust: io: copy `SysMem` with `atomic_per_byte_memcpy`
Date: Tue, 10 Nov 2026 15:44:21 +0100 [thread overview]
Message-ID: <20261110-page-volatile-io-v5-3-a91ef252c24b@kernel.org> (raw)
In-Reply-To: <20261110-page-volatile-io-v5-0-a91ef252c24b@kernel.org>
The `IoCopyable` implementation for `SysMemBackend` calls
`bindings::memcpy()` directly. The comments say that this is done "for
volatile", but the requirements of the call are not stated anywhere. The
reason the call is sound is that the kernel's `memcpy()` is byte-wise
atomic, and that neither side of the copy is subject to plain accesses.
Call `atomic_per_byte_memcpy()` instead, so that the requirements are
spelled out at the call site and checked against the invariants of `SysMem`
and the safety requirements of `IoCopyable`.
Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
---
rust/kernel/io.rs | 26 +++++++++++++++++---------
1 file changed, 17 insertions(+), 9 deletions(-)
diff --git a/rust/kernel/io.rs b/rust/kernel/io.rs
index 3bc4be35e355..b51cfc04e3ca 100644
--- a/rust/kernel/io.rs
+++ b/rust/kernel/io.rs
@@ -21,7 +21,8 @@
ptr::{
Alignment,
KnownSize, //
- }, //
+ },
+ sync::atomic::atomic_per_byte_memcpy, //
};
#[cfg(CONFIG_HAS_IOMEM)]
@@ -1500,20 +1501,27 @@ fn io_write(view: SysMem<'_, $ty>, value: $ty) {
impl IoCopyable for SysMemBackend {
#[inline]
unsafe fn copy_from_io(view: Self::View<'_, [u8]>, buffer: *mut u8) {
- // Use `bindings::memcpy` instead of `copy_nonoverlapping` for volatile.
// SAFETY:
- // - `view.ptr` is in CPU address space and valid for read.
- // - `buffer` is valid for write for `view.size()` bytes which is equal to `view.ptr.len()`.
- unsafe { bindings::memcpy(buffer.cast(), view.ptr.cast(), view.ptr.len()) };
+ // - Per type invariant, `view.ptr` is a valid kernel accessible memory region of
+ // `view.ptr.len()` bytes that is free of plain accesses, so it is valid for atomic reads.
+ // - Per the safety requirements of this function, `buffer` is valid for volatile writes for
+ // `view.size()` bytes, which is equal to `view.ptr.len()`, so it is valid for atomic
+ // writes.
+ // - Per the safety requirements of this function, `buffer` does not overlap with `view`.
+ unsafe { atomic_per_byte_memcpy(view.ptr.cast(), buffer, view.ptr.len()) };
}
#[inline]
unsafe fn copy_to_io(view: Self::View<'_, [u8]>, buffer: *const u8) {
- // Use `bindings::memcpy` instead of `copy_nonoverlapping` for volatile.
// SAFETY:
- // - `view.ptr` is in CPU address space and valid for write.
- // - `buffer` is valid for read for `view.size()` bytes which is equal to `view.ptr.len()`.
- unsafe { bindings::memcpy(view.ptr.cast(), buffer.cast(), view.ptr.len()) };
+ // - Per the safety requirements of this function, `buffer` is valid for volatile reads for
+ // `view.size()` bytes, which is equal to `view.ptr.len()`, so it is valid for atomic
+ // reads.
+ // - Per type invariant, `view.ptr` is a valid kernel accessible memory region of
+ // `view.ptr.len()` bytes that is free of plain accesses, so it is valid for atomic
+ // writes.
+ // - Per the safety requirements of this function, `buffer` does not overlap with `view`.
+ unsafe { atomic_per_byte_memcpy(buffer, view.ptr.cast(), view.ptr.len()) };
}
#[inline]
--
2.54.0
next prev parent reply other threads:[~2026-11-10 14:45 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-10 14:44 [PATCH v5 0/4] rust: byte-wise atomic copies between `Page` and shared memory Andreas Hindborg
2026-11-10 14:44 ` [PATCH v5 1/4] rust: io: require freedom from plain accesses for `SysMem` Andreas Hindborg
2026-11-10 14:44 ` [PATCH v5 2/4] rust: sync: atomic: add atomic_per_byte_memcpy Andreas Hindborg
2026-11-10 14:44 ` Andreas Hindborg [this message]
2026-11-10 14:44 ` [PATCH v5 4/4] rust: page: add byte-wise atomic copy methods for `SysMem` 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=20261110-page-volatile-io-v5-3-a91ef252c24b@kernel.org \
--to=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=driver-core@lists.linux.dev \
--cc=gary@garyguo.net \
--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=tmgross@umich.edu \
--cc=will@kernel.org \
--cc=work@onurozkan.dev \
/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®