Re: [PATCH 1/3] powerpc: Add support for 128-bit cmpxchg
From: Nysal Jan K.A.
Date: Tue Dec 01 2026 - 08:52:52 EST
On Thu, Nov 26, 2026 at 11:27:14AM +0000, David Laight wrote:
> On Wed, 25 Nov 2026 23:10:22 +0530
> "Nysal Jan K.A." <nysal@xxxxxxxxxxxxx> wrote:
>
> >
> > +#ifdef CONFIG_HAVE_CMPXCHG_DOUBLE
> > +union __u128_halves {
> > + u128 full __mode(PTI);
> > + struct {
> > + u64 low, high;
>
> There seems to be a mismatch between the register order that PTI
> assigns and the one that C uses.
> It might be less confusing to swap low and high in the above
> and only use 'full' (probably renamed) in the asm statement.
>
I can swap high and low here to indicate that it maps to the big-endian
register layout if that makes it less confusing.
> > + };
> > +};
> > +
> > +#define __CMPXCHG128(name, entry_mb, exit_mb) \
> > +static __always_inline u128 \
> > +__cmpxchg_u128##name(volatile u128 *p, u128 old, u128 new) \
> > +{ \
> > + union __u128_halves tmp, r, o = { .full = (old) }; \
>
> You don't need 'o' in a register pair, it might be better
> to just have two u64 for the two halves.
>
I'll make this change in v2.
> > + \
> > + __asm__ __volatile__ ( \
> > + entry_mb \
> > +"1: lqarx %[r],0,%[ptr]\n" \
> > +" xor %L[t],%L[r],%[ol]\n" \
> > +" xor %[t],%[r],%[oh]\n" \
> > +" or. %[t],%[t],%L[t]\n" \
> > +" bne- 2f\n" \
> > +" mr %L[t],%[n]\n" \
> > +" mr %[t],%L[n]\n" \
> > +" 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" \
>
> If you leave this exchange to the C code it will likely get
> optimised away.
>
I tried it out on godbolt and it does indeed look better.
> The same is true for the fallback code in the next patch
> where you can use asm("r14") and asm("r15) for the two halves
> of 'r' and then combine them with arithmetic for the return.
>
Makes sense.
> This would all be more readable with slightly longer variable names.
>
> I'm sure the above and the fallback code could be made similar
> enough to avoid the duplication.
>
There were some differences between both paths, I'll see if this is possible.
Thanks for the review.
--Nysal