[PATCH v5 3/4] rust: io: copy `SysMem` with `atomic_per_byte_memcpy`

From: Andreas Hindborg

Date: Tue Nov 10 2026 - 10:52:17 EST


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@xxxxxxxxxx>
---
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