Re: [PATCH v2 08/16] LoongArch: membarrier: Add full memory barrier in switch_mm()
From: Mathieu Desnoyers
Date: Fri Dec 04 2026 - 09:35:54 EST
Hi Huacai,
On 2026-12-04, Huacai Chen wrote:
> I should say sorry because my previous reply has some mistakes.
> Writing to PGDL/PGDH/ASID doesn't imply a "dbar 0", instead, it
> implies a "ibar 0".
> But "ibar 0" is also enough for the current purpose. It doesn't block
> previous read, but blocks previous write, and for hazard protection it
> flush (not just stall) the pipeline, so blocks succeeding read and
> write.
> So the effect is similar to "dbar 0b11000", and it will still be true
> in future (yes, I think our manuals should be updated).
Thank you for the correction.
I agree that "dbar 0b11000" is enough here. membarrier needs the
store to rq->curr to be ordered before the user-space accesses which
follow. That store is a write, so the reads which stay unordered do
not matter. I checked it with the Linux kernel memory model. I added
a fence which orders previous writes before all later accesses, and
nothing else. The membarrier scenario of patch 8 cannot fail with it.
It fails with "dbar 0b10010", the barrier of smp_store_release().
I can send these litmus tests if you want them.
The model assumes one more property. Before a later access runs, the
previous stores must be visible to the other CPUs. Ordering them on
the local CPU only is not enough. Is that what "blocks previous write"
means?
Two more questions:
1. Can this guarantee be written down, in the manual or in a document
from Loongson which the kernel can cite? Without it, a later
change to switch_mm_irqs_off() can drop these CSR writes, and
nobody will know that membarrier needs them.
2. Does it also hold in a KVM guest, when the guest writes PGDL and
ASID?
If the answers are yes, the planned next version drops this patch. It
adds a comment in atomic_update_pgd_asid() instead. The comment says
that membarrier relies on these CSR writes as a barrier, and cites
the document. Patch 10 ("Documentation/membarrier: Update the
per-architecture barrier list") then names these CSR writes as the
LoongArch barrier. Would you ack such a comment?
Until the guarantee is written down, I would rather keep the smp_mb().
It runs only when the next mm is registered for an expedited
membarrier command, so other processes do not pay for it.
Thanks,
Mathieu
--
Mathieu Desnoyers
EfficiOS Inc.
https://www.efficios.com