Add a helper to copy `len` bytes from `src` to `dst` using byte-wise atomic memory operations. This is the concurrent-safe counterpart of `core::ptr::copy()` (the equivalent of standard C's `memcpy()`). Because of the atomicity at byte level, when the copy races with another concurrent atomic access (or when a normal read races with an atomic read), or with an external access (from DMA or userspace), the behavior of this function is defined: the memory is copied at (at least) byte granularity. The helper is the building block for higher-level byte-wise atomic copy methods (e.g., on `Page`) that need to remain defined under racing accesses, such as when copying to or from a buffer that may be concurrently accessed by userspace or DMA. The implementation forwards to the kernel's `memcpy()`, which is implemented in a way that byte-wise atomic memory load/store instructions are used. Signed-off-by: Andreas Hindborg --- rust/kernel/sync/atomic.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/rust/kernel/sync/atomic.rs b/rust/kernel/sync/atomic.rs index 9cd009d57e35..3c76f1a14b53 100644 --- a/rust/kernel/sync/atomic.rs +++ b/rust/kernel/sync/atomic.rs @@ -848,3 +848,38 @@ pub unsafe fn cmpxchg( // per LKMM. unsafe { Atomic::from_ptr(ptr) }.cmpxchg(old, new, o) } + +/// Copy `len` bytes from `src` to `dst` using byte-wise atomic operations. +/// +/// This is the concurrent-safe counterpart of `core::ptr::copy()` (the equivalent of standard +/// C's `memcpy()`). Because of the atomicity at byte level, when racing with another concurrent +/// atomic access (or when a normal read races with an atomic read), or with an external access +/// (from DMA or userspace), the behavior of this function is defined: the memory is copied at +/// (at least) byte granularity. +/// +/// Implementation note: this is currently implemented by the kernel's `memcpy()`, which is +/// implemented in a way such that byte-wise atomic memory load/store instructions are used. +/// +/// This copy operation is volatile. +/// +/// # Safety +/// +/// Callers must ensure that: +/// +/// - `src` is valid for atomic reads for `len` bytes for the duration of the call. +/// - `dst` is valid for atomic writes for `len` bytes for the duration of the call. +pub unsafe fn atomic_per_byte_memcpy(src: *const u8, dst: *mut u8, len: usize) { + // SAFETY: By the safety requirements of this function, the following operation will not: + // - Trap. + // - Invalidate any reference invariants. + // - Race with any operation by the Rust AM, as `bindings::memcpy` is a byte-wise atomic + // operation and all operations by the Rust AM to the involved memory areas use byte-wise + // atomic semantics. + unsafe { + bindings::memcpy( + dst.cast::(), + src.cast::(), + len, + ) + }; +} -- 2.51.2