Re: [PATCH v2 08/16] LoongArch: membarrier: Add full memory barrier in switch_mm()

From: Huacai Chen

Date: Fri Dec 04 2026 - 00:04:07 EST


On Fri, Dec 4, 2026 at 12:41 AM Mathieu Desnoyers
<mathieu.desnoyers@xxxxxxxxxxxx> wrote:
>
> Hi Huacai,
>
> On 2026-11-28 23:00, Huacai Chen wrote:
> > First of all, thank you for your patch. But after consulting with the
> > chip designer, I found this patch is unnecessary.
> >
> > LoongArch doesn't ensure every CSR write imply a full memory barrier,
> > but it protects all hazards by hardware. This means part of CSR write
> > imply a "dbar 0", and CRMD, EUEN, EBASE, ASID, PGDH, PGDL are all
> > included.
>
> Thank you for asking the chip designer.
>
> If the write to PGDL or ASID implies a "dbar 0", this patch is not
> needed. switch_mm_irqs_off() writes both with atomic_update_pgd_asid()
> on every switch to a user task. This happens after the store to
> rq->curr and before the return to user-space.
>
> I could not find this barrier in the manual. Section 7.3 of Volume 1
> says that the hardware handles CSR conflicts. I read this as: later
> instructions see the new CSR value. It says nothing about memory
> ordering.
>
> membarrier needs the store to rq->curr to be visible to other CPUs
> before the next user-space load on this CPU. A few questions:
>
> 1. Is it written down anywhere that a write to PGDL or ASID implies
> "dbar 0"? If not, could it be added to the manual?
>
> 2. Is it part of the architecture, so that it holds for all future
> LoongArch processors, not only for the current Loongson cores?
> A future core without it would break membarrier with no warning.
>
> 3. Is it a full "dbar 0"? Protection against hazards may only stall
> the pipeline. Does the write also order an earlier store before a
> later load, as seen by other CPUs?
>
> 4. Does it also hold in a KVM guest, when the guest writes PGDL and
> ASID?
>
> If all four 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 full barrier. Then a
> later change will not remove or skip them by accident. Patch 10
> ("Documentation/membarrier: Update the per-architecture barrier list")
> would name these CSR writes as the LoongArch barrier. Would you ack
> such a comment?
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).


Huacai

>
> Otherwise, 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.
>
> > Another question:
> > You said smp_store_release() is not enough, but smp_store_release() is
> > the same as smp_mb(), and this patch uses smp_mb(). Why?
>
> On older cores they are the same. Those cores run every dbar hint as
> "dbar 0". Loongson-3A6000 and newer cores implement the hints (commit
> e031a5f3f1ed). arch/loongarch/include/asm/barrier.h gives the two
> different hints:
>
> smp_mb() -> o_sync() -> dbar 0b10000
> smp_store_release() -> strel_mb() -> dbar 0b10010
>
> The comment at the top of that file says: "Bit1: barrier for
> succeeding read (0: true, 1: false)". Bit 1 is set for
> smp_store_release(). So smp_store_release() does not order a store
> before a later load. membarrier needs that order. Does Loongson-3A6000
> run 0b10010 as a full barrier anyway?
>
> My commit message and the comment in asm/membarrier.h called
> smp_store_release() an "ordering barrier". This was confusing:
> smp_mb() is an ordering barrier too (bit 4 is set in both). The
> difference is bit 1. If the patch stays, the planned next version says
> that smp_store_release() does not order a store before a later load.
>
> Thanks,
>
> Mathieu
>
> --
> Mathieu Desnoyers
> EfficiOS Inc.
> https://www.efficios.com