Re: [PATCH v2] rust: page: add byte-wise atomic memory copy methods
From: Peter Zijlstra
Date: Wed Feb 18 2026 - 05:24:50 EST
On Wed, Feb 18, 2026 at 09:31:13AM +0000, Alice Ryhl wrote:
> > Must we really go write things like:
> >
> > struct foo val, *ptr;
> >
> > ptr = kmap_local_page(page);
> > memcpy(ptr, val, sizeof(val));
> > kunmap_local(ptr);
> >
> > ptr = RELOC_HIDE(&val, 0);
> >
> > if (ptr->field) {
> > ...
> > }
> >
> > That seems 'unfortunate'. It basically means we must never use local
> > stack for copies or somesuch.
>
> No I don't think RELOC_HIDE is what you want to be using here.
>
> The way to stop the compiler from doing this is to ensure that, in
> LLVM's eyes, the memcpy is either a volatile memcpy, an atomic memcpy,
> or an opaque function call. According to Gary's reply to my email on V3,
> it sounds like an explicit call to memcpy like this apparently falls into
> opaque function call, so it should be okay.
Ah, so we should write:
struct foo val, *ptr;
ptr = kmap_local_page(page);
__memcpy(ptr, val, sizeof(val));
kunmap_local(ptr);
if (val.field) {
...
}
That forces things to be a function call since it elides the C memcpy
intrinsic for not having the right name.
But that also elides KASAN and such that want to hijack the various mem
functions for instrumentation purposes.