Re: [PATCH v2 08/16] LoongArch: membarrier: Add full memory barrier in switch_mm()
From: Mathieu Desnoyers
Date: Thu Dec 03 2026 - 11:41:36 EST
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?
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