Re: [PATCH v2 -tip] x86/percpu: Use C for arch_raw_cpu_ptr()
From: Peter Zijlstra
Date: Thu Oct 19 2023 - 14:14:35 EST
On Thu, Oct 19, 2023 at 10:04:56AM -0700, Linus Torvalds wrote:
> So if you do
>
> seq = load_acquire(orig_seq);
> load-data
>
> then that acquire actually makes that first 'rmb' pointless. Acquire
> already guarantees that all subsequent memory operations are ordered
> wrt that read.
>
> And 'acquire' is likely faster than 'rmb' on sane modern architectures.
>
> On x86 it doesn't matter (rmb is a no-op, and all loads are acquires).
>
> But on arm64, for example, you can do a 'ld.acq' in one instruction
> and you're done - while a rmb then ends up being a barrier (ok, the
> asm mnemonics are horrible: it's not "ld.acq", it's "ldar", but
> whatever - I like arm64 as an architecture, but I think they made the
> standard assembly syntax pointlessly and actively hostile to humans).
>
> Of course then microarchitectures may end up doing basically the same
> thing, but at least technically the 'load acquire' is likely more
> targeted and more optimized.
Sure, acquire should work fine here.
> The second rmb is then harder to change, and that is going to stay an
> rmb ( you could say "do an acquire on the last data load, but that
> doesn't fit the sane locking semantics of a sequence lock).
Wouldn't even work, acquire allows an earlier load to pass it. It only
constraints later loads to not happen before.
> Of course, then the percpu case doesn't care about the SMP ordering,
> but it should still use an UP barrier to make sure things don't get
> re-ordered. Relying on our "percpu_read()" ordering other reads around
> it is *wrong*.
I'm happy to put barrier() in there if it makes you feel better.
But are you really saying this_cpu_read() should not imply READ_ONCE()?
If so, we should probably go audit a ton of code :/