Re: [PATCH v3] rust: page: add byte-wise atomic memory copy methods

From: Boqun Feng

Date: Tue Feb 17 2026 - 12:32:44 EST


On Tue, Feb 17, 2026 at 12:03:00PM +0000, Alice Ryhl wrote:
> On Fri, Feb 13, 2026 at 07:42:53AM +0100, Andreas Hindborg wrote:
> > When copying data from buffers that are mapped to user space, it is
> > impossible to guarantee absence of concurrent memory operations on those
> > buffers. Copying data to/from `Page` from/to these buffers would be
> > undefined behavior if no special considerations are made.
> >
> > Add methods on `Page` to read and write the contents using byte-wise atomic
> > operations.
> >
> > Also improve clarity by specifying additional requirements on
> > `read_raw`/`write_raw` methods regarding concurrent operations on involved
> > buffers.
> >
> > Signed-off-by: Andreas Hindborg <a.hindborg@xxxxxxxxxx>
>
> > +/// Copy `len` bytes from `src` to `dst` using byte-wise atomic operations.
> > +///
> > +/// This copy operation is volatile.
> > +///
> > +/// # Safety
> > +///
> > +/// Callers must ensure that:
> > +///
> > +/// - `src` is valid for reads for `len` bytes for the duration of the call.
> > +/// - `dst` is valid for writes for `len` bytes for the duration of the call.
> > +/// - For the duration of the call, other accesses to the areas described by `src`, `dst` and `len`,
> > +/// must not cause data races (defined by [`LKMM`]) against atomic operations executed by this
> > +/// function. Note that if all other accesses are atomic, then this safety requirement is
> > +/// trivially fulfilled.
> > +///
> > +/// [`LKMM`]: srctree/tools/memory-model
> > +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::<kernel::ffi::c_void>(),
> > + src.cast::<kernel::ffi::c_void>(),
> > + len,
>
> Are we sure that LLVM will not say "memcpy is a special function name, I
> know what it means" and optimize this like a non-atomic memcpy?
>
> I think we should consider using the
>
> std::intrinsics::volatile_copy_nonoverlapping_memory
>

But two racing volatile_copy_nonoverlapping_memory()s are still data
race hence UB, no?

Regards,
Boqun

> intrinsic until Rust stabilizes a built-in atomic per-byte memcpy. Yes I
> know the intrinsic is unstable, but we should at least ask the Rust
> folks about it. They are plausibly ok with this particular usage.
>
> Alice