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 no plain access races with either side of
the copy.

Call `kernel::sync::atomic::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 <[email protected]>
---
 rust/kernel/io.rs | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)

diff --git a/rust/kernel/io.rs b/rust/kernel/io.rs
index 00bab24d613c..76904c63eb0a 100644
--- a/rust/kernel/io.rs
+++ b/rust/kernel/io.rs
@@ -21,7 +21,8 @@
     ptr::{
         Alignment,
         KnownSize, //
-    }, //
+    },
+    sync::atomic, //
 };
 
 #[cfg(CONFIG_HAS_IOMEM)]
@@ -1501,20 +1502,26 @@ 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, and no plain access races with this read.
+        // - 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::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, and no plain access races with this write.
+        // - Per the safety requirements of this function, `buffer` does not 
overlap with `view`.
+        unsafe { atomic::memcpy(buffer, view.ptr.cast(), view.ptr.len()) };
     }
 
     #[inline]

-- 
2.54.0


Reply via email to