Re: [PATCH 1/3] powerpc: Add support for 128-bit cmpxchg
From: David Laight
Date: Thu Nov 26 2026 - 06:27:34 EST
On Wed, 25 Nov 2026 23:10:22 +0530
"Nysal Jan K.A." <nysal@xxxxxxxxxxxxx> wrote:
> Implement arch_cmpxchg128() and arch_cmpxchg128_local() for 64-bit
> little-endian PowerPC (POWER8 and later) using the lqarx and stqcx.
> instructions.
>
> This enables HAVE_CMPXCHG_DOUBLE, which allows SLUB to use the lockless
> __update_freelist_fast() path and provides kmalloc_nolock() required by
> BPF task-local storage and arena allocations.
> Select HAVE_ALIGNED_STRUCT_PAGE to ensure 16-byte alignment for struct
> page as required by quadword load/store instructions.
>
> The lqarx and stqcx. instructions require a consecutive even/odd
> register pair, with the most significant 64 bits in the even register
> and the least significant 64 bits in the odd register. Leverage GCC's
> PTI mode variable attribute to allow the compiler to allocate suitable
> register pairs. On little-endian, the halves are swapped to match the
> memory layout.
>
> Link: https://github.com/linuxppc/issues/issues/463
> Link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106895
> Signed-off-by: Nysal Jan K.A. <nysal@xxxxxxxxxxxxx>
> ---
...
> index dbb50c06f0bf..b3164c3c0b81 100644
> --- a/arch/powerpc/include/asm/cmpxchg.h
> +++ b/arch/powerpc/include/asm/cmpxchg.h
> @@ -6,6 +6,7 @@
> #include <linux/compiler.h>
> #include <asm/synch.h>
> #include <linux/bug.h>
> +#include <linux/types.h>
>
> #ifdef __BIG_ENDIAN
> #define BITOFF_CAL(size, off) ((sizeof(u32) - size - off) * BITS_PER_BYTE)
> @@ -617,6 +618,65 @@ __cmpxchg_u64_acquire(u64 *p, unsigned long old, unsigned long new)
> }
> #endif
>
> +#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.
> + };
> +};
> +
> +#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.
> + \
> + __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.
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.
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.
David
> + : [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"); \
> + \
> + return r.full; \
> +}
...