Re: [PATCH 1/3] powerpc: Add support for 128-bit cmpxchg

From: Nysal Jan K.A.

Date: Tue Dec 01 2026 - 07:53:31 EST


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.

>
> 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