Re: [PATCH 1/3] powerpc: Add support for 128-bit cmpxchg
From: David Laight
Date: Tue Dec 01 2026 - 09:34:22 EST
On Tue, 1 Dec 2026 18:22:33 +0530
"Nysal Jan K.A." <nysal@xxxxxxxxxxxxx> wrote:
> On Mon, Nov 30, 2026 at 06:58:11PM +0100, Christophe Leroy (CS GROUP) wrote:
> >
> >
> > Le 25/11/2026 à 18:40, Nysal Jan K.A. a écrit :
> > > + exit_mb \
> > > +"2: mr %[t],%[r]\n" \
> > > +" mr %[r],%L[r]\n" \
> > > +" mr %L[r],%[t]\n" \
> > > + : [r] "=&r" (r.full), \
> > > + [t] "=&r" (tmp.full), "+m" (*p) \
> > > + : [ptr] "r" (p), \
> > > + [ol] "r" (o.low), [oh] "r" (o.high), \
> > > + [n] "r" (new) \
> > > + : "cc", "memory"); \
> >
> > Why a memory clobber ? Is it to get a barrier ? Otherwise I see no direct
> > hidden memory modification in the sequence.
> >
>
> Yes, to act like a compiler barrier. I see a similar clobber for other
> cmpxchg implementations in this header file, so took the conservative path.
I think you need to use "=Z" for the memory operand (allows reg+reg but not
reg+constant).
All the register swaps shouldn't be needed.
(I suspect the compiler is only passing the low 64bits? and you are
picking up the high bits from r+1 because that is how they get passed
on a function call.)
AFAICT lq and stq put the high bits into the even register and the low bits
into the odd register (thus matching %L[xx]).
But you may need to ensure both halves of a 128bit value are explicitly
passed in (probably except for the __mode(PTI) ones).
Since with:
register long r4 asm("r4"), r5 asm("r5");
and
[r4] "=&r" (r4), "=&r" (r5)
you can use %L[r4] to reference r5; the asm instructions can be the
same with and without PTI.
You just need to tweak the variable definitions and constraints (you
need the extra one for the odd register containing the low data.
I'm also not sure it is worth the two scratch registers to avoid
a conditional branch.
You expect the compares to succeed, if not you are already on a slow
path and a mispredect won't do any extra harm.
The asm code might then be (I was going to try testing compiling it...):
1: lqarx %[rslt],%[mem]
cmp %L[rslt],%[old_lo]
bne- 2f:
cmp %[rslt],%[old_hi]
bne- 2f
stqcx. %[new],%[mem]
bne- 1b
2:
For non-PTI would need something like:
register u64 rslt_hi asm("r6"), rslt_lo("r7");
with the matching:
[rslt] "=&r" (rslt_hi), "=&r" (rslt_lo)
(instead of [rslt] "=&r" (rslt.full))
David
>
> >
> > You get much better code if you leave the last three 'mr' to the compiler,
> > something like the following IIUC:
> >
> > diff --git a/arch/powerpc/include/asm/cmpxchg.h
> > b/arch/powerpc/include/asm/cmpxchg.h
> > index efd7261a4a53..1770d48a11b9 100644
> > --- a/arch/powerpc/include/asm/cmpxchg.h
> > +++ b/arch/powerpc/include/asm/cmpxchg.h
> > @@ -651,9 +651,7 @@ __cmpxchg_u128##name(volatile u128 *p, u128 old, u128
> > new) \
> > " stqcx. %[t],0,%[ptr]\n" \
> > " bne- 1b\n" \
> > exit_mb \
> > -"2: mr %[t],%[r]\n" \
> > -" mr %[r],%L[r]\n" \
> > -" mr %L[r],%[t]\n" \
> > +"2:\n" \
> > : [r] "=&r" (r.full), \
> > [t] "=&r" (tmp.full), "+m" (*p) \
> > : [ptr] "r" (p), \
> > @@ -661,6 +659,10 @@ __cmpxchg_u128##name(volatile u128 *p, u128 old, u128
> > new) \
> > [n] "r" (new) \
> > : "cc", "memory"); \
> > \
> > + tmp.low = r.low; \
> > + r.low = r.high; \
> > + r.high = tmp.low; \
> > + \
> > return r.full; \
> > }
> > #else
> >
> >
>
> I have something close to this in v2 that I'm working on. David suggested
> something similar as well (to use arithmetic, I assume a shift left + or
> operation to merge the low and high 64-bits). I'll test both and see what
> works better. The swap in C does indeed produce better code when I did a quick
> check on godbolt. I'll do something similar for the fallback path as well.
>
> --Nysal