Re: [PATCH v1 1/2] perf auxtrace: Change to use SMP memory barriers
From: Peter Zijlstra
Date: Thu May 27 2021 - 05:58:33 EST
On Thu, May 27, 2021 at 12:24:15PM +0300, Adrian Hunter wrote:
> > If all we want is a compiler barrier, then shouldn't that be what we use?
> > i.e. barrier()
>
> I guess you are saying we still need to stop potential re-ordering across
> CPUs, so please ignore my comments.
Right; so the ordering issue is real, consider:
CPU0 (kernel) CPU1 (user)
write data read head
smp_wmb() smp_rmb()
write head read data
Without explicit ordering (on either side), we might either read data
that isn't written yet:
,--(read data)
write data |
smp_wmb() |
write head ---. |
`--> | read head
`- read data
Where the head load observes the new head writte, but the data load is
speculated and loads data before it is written.
Or, we can write the head before the data write is visible:
,-- write data
| write head
| read head
| smp_rmb()
| read data
`-> (data visible)
Where we read the head head, but still observe stale data because the
stores got committed out of order.
x86 is TSO, so neither reordering is actually possible, hence both
barriers being a compiler barrier (to ensure the compiler doesn't
reorder them for us). But weaker hardware *will* allow those orderings
and we very much need actual barriers there.
Welcome to the magical world of memory ordering and weak architectures.