* [PATCH 0/3] powerpc: Implement cmpxchg128()
@ 2026-11-25 17:40 Nysal Jan K.A.
2026-11-25 17:40 ` [PATCH 1/3] powerpc: Add support for 128-bit cmpxchg Nysal Jan K.A.
` (3 more replies)
0 siblings, 4 replies; 12+ messages in thread
From: Nysal Jan K.A. @ 2026-11-25 17:40 UTC (permalink / raw)
To: linuxppc-dev
Cc: Jeevitha Palanisamy, Nysal Jan K.A.,
Madhavan Srinivasan, Michael Ellerman, Nicholas Piggin,
Christophe Leroy (CS GROUP), Ritesh Harjani (IBM),
Shrikanth Hegde, Nathan Chancellor, Nick Desaulniers,
Bill Wendling, Justin Stitt, Andrew Morton,
David Hildenbrand (Arm),
SJ Park, Usama Arif, Kevin Brodsky, Kiryl Shutsemau (Meta),
linux-kernel, llvm
This series adds arch_cmpxchg128() and arch_cmpxchg128_local() for
little-endian POWER8 and later, using lqarx/stqcx. This enables
HAVE_CMPXCHG_DOUBLE and HAVE_ALIGNED_STRUCT_PAGE, which gives us:
* SLUB's lockless __update_freelist_fast() path (avoiding slab_lock()).
* kmalloc_nolock(), without which BPF task-local storage creation fails
with -ENOMEM, and the selftests task_local_data_race and
struct_ops_assoc (timer subtests) fail. BPF Arena kfuncs were changed
to be callable from any context. kmalloc_nolock() replaced kvcalloc()
in the arena allocation paths because kvcalloc() may sleep and take
ordinary allocator locks, while kmalloc_nolock() can run in NMI or
non-sleepable contexts.
The lqarx and stqcx. instructions require the 128-bit operand to reside in
a consecutive even/odd register pair. In the Power ISA, the operand uses
big-endian register layout where the most significant and least
significant 64 bits are in the even and odd registers respectively.
Until now this could not be expressed in inline asm without hard
register constraints [1].
GCC now supports this through the PTI mode attribute [2,3] for PowerPC
and it will be available in the upcoming GCC 17 release. Backports have
landed in the GCC 14, 15 and 16 branches. GCC 16.2 is the first release
which has PTI mode support. For older GCC releases and clang there is a
fallback implementation with hard register constraints.
Additionally, this series enables PPC_HAS_LBARX_LHARX for Power10, which
was previously omitted in the Kconfig.
[1] https://github.com/linuxppc/issues/issues/463
[2] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106895
[3] https://gcc.gnu.org/pipermail/gcc-patches/2026-April/712831.html
Nysal Jan K.A. (3):
powerpc: Add support for 128-bit cmpxchg
powerpc: Implement 128-bit cmpxchg fallback
powerpc: Enable lbarx/lharx for Power10
arch/powerpc/Kconfig | 8 ++
arch/powerpc/include/asm/cmpxchg.h | 100 +++++++++++++++++++++++++
arch/powerpc/platforms/Kconfig.cputype | 4 +
3 files changed, 112 insertions(+)
base-commit: 65e4fd942f72a9dba0e4e257290c4fa1aa756854
--
2.52.0
^ permalink raw reply [flat|nested] 12+ messages in thread* [PATCH 1/3] powerpc: Add support for 128-bit cmpxchg 2026-11-25 17:40 [PATCH 0/3] powerpc: Implement cmpxchg128() Nysal Jan K.A. @ 2026-11-25 17:40 ` Nysal Jan K.A. 2026-11-25 20:44 ` Nathan Chancellor ` (2 more replies) 2026-11-25 17:40 ` [PATCH 2/3] powerpc: Implement 128-bit cmpxchg fallback Nysal Jan K.A. ` (2 subsequent siblings) 3 siblings, 3 replies; 12+ messages in thread From: Nysal Jan K.A. @ 2026-11-25 17:40 UTC (permalink / raw) To: linuxppc-dev Cc: Jeevitha Palanisamy, Nysal Jan K.A., Madhavan Srinivasan, Michael Ellerman, Nicholas Piggin, Christophe Leroy (CS GROUP), Ritesh Harjani (IBM), Shrikanth Hegde, Nathan Chancellor, Nick Desaulniers, Bill Wendling, Justin Stitt, Andrew Morton, David Hildenbrand (Arm), Ryan Roberts, Andreas Larsson, Usama Arif, Kevin Brodsky, Kiryl Shutsemau (Meta), linux-kernel, llvm 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@linux.ibm.com> --- arch/powerpc/Kconfig | 8 ++++ arch/powerpc/include/asm/cmpxchg.h | 60 ++++++++++++++++++++++++++ arch/powerpc/platforms/Kconfig.cputype | 3 ++ 3 files changed, 71 insertions(+) diff --git a/arch/powerpc/Kconfig b/arch/powerpc/Kconfig index c3b6cd655606..79cda056dfc4 100644 --- a/arch/powerpc/Kconfig +++ b/arch/powerpc/Kconfig @@ -15,6 +15,9 @@ config CC_HAS_PCREL # do pcrel yet. def_bool PPC64 && CC_IS_GCC && $(cc-option, -mcpu=power10 -mpcrel) +config CC_HAS_PTI_MODE_VAR_ATTR + def_bool $(success,echo 'void foo(void) { unsigned __int128 u128 __attribute__((__mode__(PTI))); }' | $(CC) -x c - -c -o /dev/null) + config 32BIT bool default y if PPC32 @@ -214,6 +217,7 @@ config PPC select GENERIC_PCI_IOMAP if PCI select GENERIC_SMP_IDLE_THREAD select HAS_IOPORT if PCI + select HAVE_ALIGNED_STRUCT_PAGE if PPC_HAS_LQARX && CPU_LITTLE_ENDIAN && CC_HAS_PTI_MODE_VAR_ATTR select HAVE_ARCH_AUDITSYSCALL select HAVE_ARCH_HUGE_VMALLOC if HAVE_ARCH_HUGE_VMAP select HAVE_ARCH_HUGE_VMAP if PPC_RADIX_MMU || PPC_8xx @@ -234,6 +238,7 @@ config PPC select HAVE_ARCH_SECCOMP_FILTER select HAVE_ARCH_TRACEHOOK select HAVE_ASM_MODVERSIONS + select HAVE_CMPXCHG_DOUBLE if PPC_HAS_LQARX && CPU_LITTLE_ENDIAN && CC_HAS_PTI_MODE_VAR_ATTR select HAVE_CONTEXT_TRACKING_USER select HAVE_C_RECORDMCOUNT select HAVE_DEBUG_KMEMLEAK @@ -341,6 +346,9 @@ config PPC_BARRIER_NOSPEC config PPC_HAS_LBARX_LHARX bool +config PPC_HAS_LQARX + bool + config EARLY_PRINTK bool default y diff --git a/arch/powerpc/include/asm/cmpxchg.h b/arch/powerpc/include/asm/cmpxchg.h 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; + }; +}; + +#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) }; \ + \ + __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" \ + : [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; \ +} + +__CMPXCHG128(, PPC_ATOMIC_ENTRY_BARRIER, PPC_ATOMIC_EXIT_BARRIER) +__CMPXCHG128(_local, ,) + +#undef __CMPXCHG128 + +#define system_has_cmpxchg128() 1 + +#define arch_cmpxchg128(ptr, o, n) \ +({ \ + BUILD_BUG_ON(sizeof(*(ptr)) != 16); \ + __cmpxchg_u128((ptr), (o), (n)); \ +}) + +#define arch_cmpxchg128_local(ptr, o, n) \ +({ \ + BUILD_BUG_ON(sizeof(*(ptr)) != 16); \ + __cmpxchg_u128_local((ptr), (o), (n)); \ +}) +#endif + static __always_inline unsigned long __cmpxchg(volatile void *ptr, unsigned long old, unsigned long new, unsigned int size) diff --git a/arch/powerpc/platforms/Kconfig.cputype b/arch/powerpc/platforms/Kconfig.cputype index f0adbf7ecb7f..a42fd4c64159 100644 --- a/arch/powerpc/platforms/Kconfig.cputype +++ b/arch/powerpc/platforms/Kconfig.cputype @@ -163,12 +163,14 @@ config POWER8_CPU select ARCH_HAS_FAST_MULTIPLIER select PPC_64S_HASH_MMU select PPC_HAS_LBARX_LHARX + select PPC_HAS_LQARX config POWER9_CPU bool "POWER9" depends on PPC_BOOK3S_64 select ARCH_HAS_FAST_MULTIPLIER select PPC_HAS_LBARX_LHARX + select PPC_HAS_LQARX config POWER10_CPU bool "POWER10" @@ -176,6 +178,7 @@ config POWER10_CPU select ARCH_HAS_FAST_MULTIPLIER select PPC_HAVE_PREFIXED_SUPPORT select PPC_HAVE_PCREL_SUPPORT + select PPC_HAS_LQARX config E5500_CPU bool "Freescale e5500" -- 2.52.0 ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH 1/3] powerpc: Add support for 128-bit cmpxchg 2026-11-25 17:40 ` [PATCH 1/3] powerpc: Add support for 128-bit cmpxchg Nysal Jan K.A. @ 2026-11-25 20:44 ` Nathan Chancellor 2026-11-26 10:12 ` Nysal Jan K.A. 2026-11-26 11:27 ` David Laight 2026-11-30 17:58 ` Christophe Leroy (CS GROUP) 2 siblings, 1 reply; 12+ messages in thread From: Nathan Chancellor @ 2026-11-25 20:44 UTC (permalink / raw) To: Nysal Jan K.A. Cc: linuxppc-dev, Jeevitha Palanisamy, Madhavan Srinivasan, Michael Ellerman, Nicholas Piggin, Christophe Leroy (CS GROUP), Ritesh Harjani (IBM), Shrikanth Hegde, Nick Desaulniers, Bill Wendling, Justin Stitt, Andrew Morton, David Hildenbrand (Arm), Ryan Roberts, Andreas Larsson, Usama Arif, Kevin Brodsky, Kiryl Shutsemau (Meta), linux-kernel, llvm On Wed, Nov 25, 2026 at 11:10:22PM +0530, Nysal Jan K.A. wrote: > diff --git a/arch/powerpc/Kconfig b/arch/powerpc/Kconfig > index c3b6cd655606..79cda056dfc4 100644 > --- a/arch/powerpc/Kconfig > +++ b/arch/powerpc/Kconfig > @@ -15,6 +15,9 @@ config CC_HAS_PCREL > # do pcrel yet. > def_bool PPC64 && CC_IS_GCC && $(cc-option, -mcpu=power10 -mpcrel) > > +config CC_HAS_PTI_MODE_VAR_ATTR > + def_bool $(success,echo 'void foo(void) { unsigned __int128 u128 __attribute__((__mode__(PTI))); }' | $(CC) -x c - -c -o /dev/null) Please use '$(CC) $(CLANG_FLAGS)' so that this works if clang adds support for this, which will depend on the target information in CLANG_FLAGS. Also, consider following the recently added guidance to scripts/Kconfig.toolchain and add some information about when this check can go away: https://git.kernel.org/kbuild/c/c285fe817a92f9c747931134f468d2aefeae6bce It would probably be something like # gcc: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106895 - first fixed in 17.0.0, backported in 16.2.0, 15.4.0, and 14.5.0 # clang: not supported - <bug link like Nick requested> -- Cheers, Nathan ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH 1/3] powerpc: Add support for 128-bit cmpxchg 2026-11-25 20:44 ` Nathan Chancellor @ 2026-11-26 10:12 ` Nysal Jan K.A. 0 siblings, 0 replies; 12+ messages in thread From: Nysal Jan K.A. @ 2026-11-26 10:12 UTC (permalink / raw) To: Nathan Chancellor Cc: linuxppc-dev, Jeevitha Palanisamy, Madhavan Srinivasan, Michael Ellerman, Nicholas Piggin, Christophe Leroy (CS GROUP), Ritesh Harjani (IBM), Shrikanth Hegde, Nick Desaulniers, Bill Wendling, Justin Stitt, Andrew Morton, David Hildenbrand (Arm), Ryan Roberts, Andreas Larsson, Usama Arif, Kevin Brodsky, Kiryl Shutsemau (Meta), linux-kernel, llvm On Wed, Nov 25, 2026 at 01:44:53PM -0700, Nathan Chancellor wrote: > On Wed, Nov 25, 2026 at 11:10:22PM +0530, Nysal Jan K.A. wrote: > > diff --git a/arch/powerpc/Kconfig b/arch/powerpc/Kconfig > > index c3b6cd655606..79cda056dfc4 100644 > > --- a/arch/powerpc/Kconfig > > +++ b/arch/powerpc/Kconfig > > @@ -15,6 +15,9 @@ config CC_HAS_PCREL > > # do pcrel yet. > > def_bool PPC64 && CC_IS_GCC && $(cc-option, -mcpu=power10 -mpcrel) > > > > +config CC_HAS_PTI_MODE_VAR_ATTR > > + def_bool $(success,echo 'void foo(void) { unsigned __int128 u128 __attribute__((__mode__(PTI))); }' | $(CC) -x c - -c -o /dev/null) > > Please use '$(CC) $(CLANG_FLAGS)' so that this works if clang adds > support for this, which will depend on the target information in > CLANG_FLAGS. > > Also, consider following the recently added guidance to > scripts/Kconfig.toolchain and add some information about when this check > can go away: > > https://git.kernel.org/kbuild/c/c285fe817a92f9c747931134f468d2aefeae6bce > > It would probably be something like > > # gcc: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106895 - first fixed in 17.0.0, backported in 16.2.0, 15.4.0, and 14.5.0 > # clang: not supported - <bug link like Nick requested> I've opened a feature request - https://github.com/llvm/llvm-project/issues/246292 Will make both the suggested changes in v2. Thanks --Nysal ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH 1/3] powerpc: Add support for 128-bit cmpxchg 2026-11-25 17:40 ` [PATCH 1/3] powerpc: Add support for 128-bit cmpxchg Nysal Jan K.A. 2026-11-25 20:44 ` Nathan Chancellor @ 2026-11-26 11:27 ` David Laight 2026-11-30 17:58 ` Christophe Leroy (CS GROUP) 2 siblings, 0 replies; 12+ messages in thread From: David Laight @ 2026-11-26 11:27 UTC (permalink / raw) To: Nysal Jan K.A. Cc: linuxppc-dev, Jeevitha Palanisamy, Madhavan Srinivasan, Michael Ellerman, Nicholas Piggin, Christophe Leroy (CS GROUP), Ritesh Harjani (IBM), Shrikanth Hegde, Nathan Chancellor, Nick Desaulniers, Bill Wendling, Justin Stitt, Andrew Morton, David Hildenbrand (Arm), Ryan Roberts, Andreas Larsson, Usama Arif, Kevin Brodsky, Kiryl Shutsemau (Meta), linux-kernel, llvm On Wed, 25 Nov 2026 23:10:22 +0530 "Nysal Jan K.A." <nysal@linux.ibm.com> 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@linux.ibm.com> > --- ... > 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; \ > +} ... ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH 1/3] powerpc: Add support for 128-bit cmpxchg 2026-11-25 17:40 ` [PATCH 1/3] powerpc: Add support for 128-bit cmpxchg Nysal Jan K.A. 2026-11-25 20:44 ` Nathan Chancellor 2026-11-26 11:27 ` David Laight @ 2026-11-30 17:58 ` Christophe Leroy (CS GROUP) 2026-11-30 22:01 ` David Laight 2 siblings, 1 reply; 12+ messages in thread From: Christophe Leroy (CS GROUP) @ 2026-11-30 17:58 UTC (permalink / raw) To: Nysal Jan K.A., linuxppc-dev Cc: Jeevitha Palanisamy, Madhavan Srinivasan, Michael Ellerman, Nicholas Piggin, Ritesh Harjani (IBM), Shrikanth Hegde, Nathan Chancellor, Nick Desaulniers, Bill Wendling, Justin Stitt, Andrew Morton, David Hildenbrand (Arm), Ryan Roberts, Andreas Larsson, Usama Arif, Kevin Brodsky, Kiryl Shutsemau (Meta), linux-kernel, llvm Le 25/11/2026 à 18:40, Nysal Jan K.A. a écrit : > 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@linux.ibm.com> > --- > arch/powerpc/Kconfig | 8 ++++ > arch/powerpc/include/asm/cmpxchg.h | 60 ++++++++++++++++++++++++++ > arch/powerpc/platforms/Kconfig.cputype | 3 ++ > 3 files changed, 71 insertions(+) > > diff --git a/arch/powerpc/Kconfig b/arch/powerpc/Kconfig > index c3b6cd655606..79cda056dfc4 100644 > --- a/arch/powerpc/Kconfig > +++ b/arch/powerpc/Kconfig > @@ -15,6 +15,9 @@ config CC_HAS_PCREL > # do pcrel yet. > def_bool PPC64 && CC_IS_GCC && $(cc-option, -mcpu=power10 -mpcrel) > > +config CC_HAS_PTI_MODE_VAR_ATTR > + def_bool $(success,echo 'void foo(void) { unsigned __int128 u128 __attribute__((__mode__(PTI))); }' | $(CC) -x c - -c -o /dev/null) > + > config 32BIT > bool > default y if PPC32 > @@ -214,6 +217,7 @@ config PPC > select GENERIC_PCI_IOMAP if PCI > select GENERIC_SMP_IDLE_THREAD > select HAS_IOPORT if PCI > + select HAVE_ALIGNED_STRUCT_PAGE if PPC_HAS_LQARX && CPU_LITTLE_ENDIAN && CC_HAS_PTI_MODE_VAR_ATTR > select HAVE_ARCH_AUDITSYSCALL > select HAVE_ARCH_HUGE_VMALLOC if HAVE_ARCH_HUGE_VMAP > select HAVE_ARCH_HUGE_VMAP if PPC_RADIX_MMU || PPC_8xx > @@ -234,6 +238,7 @@ config PPC > select HAVE_ARCH_SECCOMP_FILTER > select HAVE_ARCH_TRACEHOOK > select HAVE_ASM_MODVERSIONS > + select HAVE_CMPXCHG_DOUBLE if PPC_HAS_LQARX && CPU_LITTLE_ENDIAN && CC_HAS_PTI_MODE_VAR_ATTR > select HAVE_CONTEXT_TRACKING_USER > select HAVE_C_RECORDMCOUNT > select HAVE_DEBUG_KMEMLEAK > @@ -341,6 +346,9 @@ config PPC_BARRIER_NOSPEC > config PPC_HAS_LBARX_LHARX > bool > > +config PPC_HAS_LQARX > + bool > + > config EARLY_PRINTK > bool > default y > diff --git a/arch/powerpc/include/asm/cmpxchg.h b/arch/powerpc/include/asm/cmpxchg.h > 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; > + }; > +}; > + > +#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) }; \ > + \ > + __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" \ > + : [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. > + \ > + return r.full; \ > +} 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 > + > +__CMPXCHG128(, PPC_ATOMIC_ENTRY_BARRIER, PPC_ATOMIC_EXIT_BARRIER) > +__CMPXCHG128(_local, ,) > + > +#undef __CMPXCHG128 > + > +#define system_has_cmpxchg128() 1 > + > +#define arch_cmpxchg128(ptr, o, n) \ > +({ \ > + BUILD_BUG_ON(sizeof(*(ptr)) != 16); \ > + __cmpxchg_u128((ptr), (o), (n)); \ > +}) > + > +#define arch_cmpxchg128_local(ptr, o, n) \ > +({ \ > + BUILD_BUG_ON(sizeof(*(ptr)) != 16); \ > + __cmpxchg_u128_local((ptr), (o), (n)); \ > +}) > +#endif > + > static __always_inline unsigned long > __cmpxchg(volatile void *ptr, unsigned long old, unsigned long new, > unsigned int size) > diff --git a/arch/powerpc/platforms/Kconfig.cputype b/arch/powerpc/platforms/Kconfig.cputype > index f0adbf7ecb7f..a42fd4c64159 100644 > --- a/arch/powerpc/platforms/Kconfig.cputype > +++ b/arch/powerpc/platforms/Kconfig.cputype > @@ -163,12 +163,14 @@ config POWER8_CPU > select ARCH_HAS_FAST_MULTIPLIER > select PPC_64S_HASH_MMU > select PPC_HAS_LBARX_LHARX > + select PPC_HAS_LQARX > > config POWER9_CPU > bool "POWER9" > depends on PPC_BOOK3S_64 > select ARCH_HAS_FAST_MULTIPLIER > select PPC_HAS_LBARX_LHARX > + select PPC_HAS_LQARX > > config POWER10_CPU > bool "POWER10" > @@ -176,6 +178,7 @@ config POWER10_CPU > select ARCH_HAS_FAST_MULTIPLIER > select PPC_HAVE_PREFIXED_SUPPORT > select PPC_HAVE_PCREL_SUPPORT > + select PPC_HAS_LQARX > > config E5500_CPU > bool "Freescale e5500" ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH 1/3] powerpc: Add support for 128-bit cmpxchg 2026-11-30 17:58 ` Christophe Leroy (CS GROUP) @ 2026-11-30 22:01 ` David Laight 0 siblings, 0 replies; 12+ messages in thread From: David Laight @ 2026-11-30 22:01 UTC (permalink / raw) To: Christophe Leroy (CS GROUP) Cc: Nysal Jan K.A., linuxppc-dev, Jeevitha Palanisamy, Madhavan Srinivasan, Michael Ellerman, Nicholas Piggin, Ritesh Harjani (IBM), Shrikanth Hegde, Nathan Chancellor, Nick Desaulniers, Bill Wendling, Justin Stitt, Andrew Morton, David Hildenbrand (Arm), Ryan Roberts, Andreas Larsson, Usama Arif, Kevin Brodsky, Kiryl Shutsemau (Meta), linux-kernel, llvm On Mon, 30 Nov 2026 18:58:11 +0100 "Christophe Leroy (CS GROUP)" <chleroy@kernel.org> wrote: > Le 25/11/2026 à 18:40, Nysal Jan K.A. a écrit : > > 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@linux.ibm.com> > > --- > > arch/powerpc/Kconfig | 8 ++++ > > arch/powerpc/include/asm/cmpxchg.h | 60 ++++++++++++++++++++++++++ > > arch/powerpc/platforms/Kconfig.cputype | 3 ++ > > 3 files changed, 71 insertions(+) > > > > diff --git a/arch/powerpc/Kconfig b/arch/powerpc/Kconfig > > index c3b6cd655606..79cda056dfc4 100644 > > --- a/arch/powerpc/Kconfig > > +++ b/arch/powerpc/Kconfig > > @@ -15,6 +15,9 @@ config CC_HAS_PCREL > > # do pcrel yet. > > def_bool PPC64 && CC_IS_GCC && $(cc-option, -mcpu=power10 -mpcrel) > > > > +config CC_HAS_PTI_MODE_VAR_ATTR > > + def_bool $(success,echo 'void foo(void) { unsigned __int128 u128 __attribute__((__mode__(PTI))); }' | $(CC) -x c - -c -o /dev/null) > > + > > config 32BIT > > bool > > default y if PPC32 > > @@ -214,6 +217,7 @@ config PPC > > select GENERIC_PCI_IOMAP if PCI > > select GENERIC_SMP_IDLE_THREAD > > select HAS_IOPORT if PCI > > + select HAVE_ALIGNED_STRUCT_PAGE if PPC_HAS_LQARX && CPU_LITTLE_ENDIAN && CC_HAS_PTI_MODE_VAR_ATTR > > select HAVE_ARCH_AUDITSYSCALL > > select HAVE_ARCH_HUGE_VMALLOC if HAVE_ARCH_HUGE_VMAP > > select HAVE_ARCH_HUGE_VMAP if PPC_RADIX_MMU || PPC_8xx > > @@ -234,6 +238,7 @@ config PPC > > select HAVE_ARCH_SECCOMP_FILTER > > select HAVE_ARCH_TRACEHOOK > > select HAVE_ASM_MODVERSIONS > > + select HAVE_CMPXCHG_DOUBLE if PPC_HAS_LQARX && CPU_LITTLE_ENDIAN && CC_HAS_PTI_MODE_VAR_ATTR > > select HAVE_CONTEXT_TRACKING_USER > > select HAVE_C_RECORDMCOUNT > > select HAVE_DEBUG_KMEMLEAK > > @@ -341,6 +346,9 @@ config PPC_BARRIER_NOSPEC > > config PPC_HAS_LBARX_LHARX > > bool > > > > +config PPC_HAS_LQARX > > + bool > > + > > config EARLY_PRINTK > > bool > > default y > > diff --git a/arch/powerpc/include/asm/cmpxchg.h b/arch/powerpc/include/asm/cmpxchg.h > > 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; > > + }; > > +}; > > + > > +#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) }; \ > > + \ > > + __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" \ > > + : [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. > > > + \ > > + return r.full; \ > > +} > > 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; \ Or even just: return (u128)r.low << 64 | r.high; I missed it earlier, but you don't need the instructions to word-swap [n] (aka new) either. Just get the compiler to allocate them the correct way around (aka swapped). It does look as though your struct/union naming is wrong. I think the first u64 is the low numbered register and the high 64 bits (so doesn't really match a u128 for LE). So you need to do o = { .low = old, .high = old >> 64 } This is also a real function: u128 f(void *p, u128 old, u128 new). It looks like that uses r3 for p, r4 for old.lo, r5 for old.hi and r6/r7 for new. That doesn't help code generation, I think you need new.hi in an even numbered register. Actually is using _PTI here even worth the effort at all. Since it is a real function you know where the values start and which registers can be used - so just use asm to define register numbers that minimise the number of register-register moves and sort out an order for the arguments that makes life easy. It might mean you need a C wrapper that swaps things around. Something like: u128 _cmpxchg(u64 o_hi, u64 o_lo, u64 n_hi, u64 n_lo, u128 *p); #define cmpxchg128(p, old, new) ({ \ u128 _old = old, _new = new; \ _cmpxchg(old >> 64, old, new >> 64, new, p);}) But maybe that isn't quite right. David > } > #else > > > > + > > +__CMPXCHG128(, PPC_ATOMIC_ENTRY_BARRIER, PPC_ATOMIC_EXIT_BARRIER) > > +__CMPXCHG128(_local, ,) > > + > > +#undef __CMPXCHG128 > > + > > +#define system_has_cmpxchg128() 1 > > + > > +#define arch_cmpxchg128(ptr, o, n) \ > > +({ \ > > + BUILD_BUG_ON(sizeof(*(ptr)) != 16); \ > > + __cmpxchg_u128((ptr), (o), (n)); \ > > +}) > > + > > +#define arch_cmpxchg128_local(ptr, o, n) \ > > +({ \ > > + BUILD_BUG_ON(sizeof(*(ptr)) != 16); \ > > + __cmpxchg_u128_local((ptr), (o), (n)); \ > > +}) > > +#endif > > + > > static __always_inline unsigned long > > __cmpxchg(volatile void *ptr, unsigned long old, unsigned long new, > > unsigned int size) > > diff --git a/arch/powerpc/platforms/Kconfig.cputype b/arch/powerpc/platforms/Kconfig.cputype > > index f0adbf7ecb7f..a42fd4c64159 100644 > > --- a/arch/powerpc/platforms/Kconfig.cputype > > +++ b/arch/powerpc/platforms/Kconfig.cputype > > @@ -163,12 +163,14 @@ config POWER8_CPU > > select ARCH_HAS_FAST_MULTIPLIER > > select PPC_64S_HASH_MMU > > select PPC_HAS_LBARX_LHARX > > + select PPC_HAS_LQARX > > > > config POWER9_CPU > > bool "POWER9" > > depends on PPC_BOOK3S_64 > > select ARCH_HAS_FAST_MULTIPLIER > > select PPC_HAS_LBARX_LHARX > > + select PPC_HAS_LQARX > > > > config POWER10_CPU > > bool "POWER10" > > @@ -176,6 +178,7 @@ config POWER10_CPU > > select ARCH_HAS_FAST_MULTIPLIER > > select PPC_HAVE_PREFIXED_SUPPORT > > select PPC_HAVE_PCREL_SUPPORT > > + select PPC_HAS_LQARX > > > > config E5500_CPU > > bool "Freescale e5500" > > ^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH 2/3] powerpc: Implement 128-bit cmpxchg fallback 2026-11-25 17:40 [PATCH 0/3] powerpc: Implement cmpxchg128() Nysal Jan K.A. 2026-11-25 17:40 ` [PATCH 1/3] powerpc: Add support for 128-bit cmpxchg Nysal Jan K.A. @ 2026-11-25 17:40 ` Nysal Jan K.A. 2026-11-30 18:55 ` Christophe Leroy (CS GROUP) 2026-11-25 17:40 ` [PATCH 3/3] powerpc: Enable lbarx/lharx for Power10 Nysal Jan K.A. 2026-11-25 17:51 ` [PATCH 0/3] powerpc: Implement cmpxchg128() Nick Desaulniers 3 siblings, 1 reply; 12+ messages in thread From: Nysal Jan K.A. @ 2026-11-25 17:40 UTC (permalink / raw) To: linuxppc-dev Cc: Jeevitha Palanisamy, Nysal Jan K.A., Madhavan Srinivasan, Michael Ellerman, Nicholas Piggin, Christophe Leroy (CS GROUP), Ritesh Harjani (IBM), Shrikanth Hegde, Nathan Chancellor, Nick Desaulniers, Bill Wendling, Justin Stitt, Andrew Morton, David Hildenbrand (Arm), Yeoreum Yun, SJ Park, Kiryl Shutsemau (Meta), Kevin Brodsky, Usama Arif, linux-kernel, llvm Compilers that lack support for the PTI mode attribute (such as Clang and older GCC versions) cannot dynamically allocate consecutive even/odd register pairs for inline assembly operands. Implement a fallback path for 128-bit cmpxchg using hard register constraints: - r14/r15: pair for lqarx target (old value read from memory) - r16/r17: pair for stqcx. source (new value to store) With this fallback in place, remove the compiler dependency for selecting HAVE_CMPXCHG_DOUBLE and HAVE_ALIGNED_STRUCT_PAGE on supported CPUs. Signed-off-by: Nysal Jan K.A. <nysal@linux.ibm.com> --- arch/powerpc/Kconfig | 4 +-- arch/powerpc/include/asm/cmpxchg.h | 42 +++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/arch/powerpc/Kconfig b/arch/powerpc/Kconfig index 79cda056dfc4..0b9071b16b21 100644 --- a/arch/powerpc/Kconfig +++ b/arch/powerpc/Kconfig @@ -217,7 +217,7 @@ config PPC select GENERIC_PCI_IOMAP if PCI select GENERIC_SMP_IDLE_THREAD select HAS_IOPORT if PCI - select HAVE_ALIGNED_STRUCT_PAGE if PPC_HAS_LQARX && CPU_LITTLE_ENDIAN && CC_HAS_PTI_MODE_VAR_ATTR + select HAVE_ALIGNED_STRUCT_PAGE if PPC_HAS_LQARX && CPU_LITTLE_ENDIAN select HAVE_ARCH_AUDITSYSCALL select HAVE_ARCH_HUGE_VMALLOC if HAVE_ARCH_HUGE_VMAP select HAVE_ARCH_HUGE_VMAP if PPC_RADIX_MMU || PPC_8xx @@ -238,7 +238,7 @@ config PPC select HAVE_ARCH_SECCOMP_FILTER select HAVE_ARCH_TRACEHOOK select HAVE_ASM_MODVERSIONS - select HAVE_CMPXCHG_DOUBLE if PPC_HAS_LQARX && CPU_LITTLE_ENDIAN && CC_HAS_PTI_MODE_VAR_ATTR + select HAVE_CMPXCHG_DOUBLE if PPC_HAS_LQARX && CPU_LITTLE_ENDIAN select HAVE_CONTEXT_TRACKING_USER select HAVE_C_RECORDMCOUNT select HAVE_DEBUG_KMEMLEAK diff --git a/arch/powerpc/include/asm/cmpxchg.h b/arch/powerpc/include/asm/cmpxchg.h index b3164c3c0b81..efd7261a4a53 100644 --- a/arch/powerpc/include/asm/cmpxchg.h +++ b/arch/powerpc/include/asm/cmpxchg.h @@ -619,13 +619,20 @@ __cmpxchg_u64_acquire(u64 *p, unsigned long old, unsigned long new) #endif #ifdef CONFIG_HAVE_CMPXCHG_DOUBLE +#ifdef CONFIG_CC_HAS_PTI_MODE_VAR_ATTR +#define __PPC_PTI_MODE __mode(PTI) +#else +#define __PPC_PTI_MODE +#endif + union __u128_halves { - u128 full __mode(PTI); + u128 full __PPC_PTI_MODE; struct { u64 low, high; }; }; +#ifdef CONFIG_CC_HAS_PTI_MODE_VAR_ATTR #define __CMPXCHG128(name, entry_mb, exit_mb) \ static __always_inline u128 \ __cmpxchg_u128##name(volatile u128 *p, u128 old, u128 new) \ @@ -656,6 +663,39 @@ __cmpxchg_u128##name(volatile u128 *p, u128 old, u128 new) \ \ return r.full; \ } +#else +#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) }, \ + n = { .full = (new) }; \ + \ + __asm__ __volatile__ ( \ + entry_mb \ +"1: lqarx 14,0,%[ptr]\n" \ +" xor %[tl],15,%[ol]\n" \ +" xor %[th],14,%[oh]\n" \ +" or. %[th],%[th],%[tl]\n" \ +" bne- 2f\n" \ +" mr 17,%[nl]\n" \ +" mr 16,%[nh]\n" \ +" stqcx. 16,0,%[ptr]\n" \ +" bne- 1b\n" \ + exit_mb \ +"2: mr %[rh],14\n" \ +" mr %[rl],15\n" \ + : [rl] "=&r" (r.low), [rh] "=&r" (r.high), \ + [tl] "=&r" (tmp.low), [th] "=&r" (tmp.high), \ + "+m" (*p) \ + : [ptr] "r" (p), \ + [ol] "r" (o.low), [oh] "r" (o.high), \ + [nl] "r" (n.low), [nh] "r" (n.high) \ + : "r14", "r15", "r16", "r17", "cc", "memory"); \ + \ + return r.full; \ +} +#endif __CMPXCHG128(, PPC_ATOMIC_ENTRY_BARRIER, PPC_ATOMIC_EXIT_BARRIER) __CMPXCHG128(_local, ,) -- 2.52.0 ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH 2/3] powerpc: Implement 128-bit cmpxchg fallback 2026-11-25 17:40 ` [PATCH 2/3] powerpc: Implement 128-bit cmpxchg fallback Nysal Jan K.A. @ 2026-11-30 18:55 ` Christophe Leroy (CS GROUP) 0 siblings, 0 replies; 12+ messages in thread From: Christophe Leroy (CS GROUP) @ 2026-11-30 18:55 UTC (permalink / raw) To: Nysal Jan K.A., linuxppc-dev Cc: Jeevitha Palanisamy, Madhavan Srinivasan, Michael Ellerman, Nicholas Piggin, Ritesh Harjani (IBM), Shrikanth Hegde, Nathan Chancellor, Nick Desaulniers, Bill Wendling, Justin Stitt, Andrew Morton, David Hildenbrand (Arm), Yeoreum Yun, SJ Park, Kiryl Shutsemau (Meta), Kevin Brodsky, Usama Arif, linux-kernel, llvm Le 25/11/2026 à 18:40, Nysal Jan K.A. a écrit : > Compilers that lack support for the PTI mode attribute (such as Clang > and older GCC versions) cannot dynamically allocate consecutive even/odd > register pairs for inline assembly operands. Implement a fallback path > for 128-bit cmpxchg using hard register constraints: > - r14/r15: pair for lqarx target (old value read from memory) > - r16/r17: pair for stqcx. source (new value to store) > > With this fallback in place, remove the compiler dependency for > selecting HAVE_CMPXCHG_DOUBLE and HAVE_ALIGNED_STRUCT_PAGE on > supported CPUs. > > Signed-off-by: Nysal Jan K.A. <nysal@linux.ibm.com> > --- > arch/powerpc/Kconfig | 4 +-- > arch/powerpc/include/asm/cmpxchg.h | 42 +++++++++++++++++++++++++++++- > 2 files changed, 43 insertions(+), 3 deletions(-) > > diff --git a/arch/powerpc/Kconfig b/arch/powerpc/Kconfig > index 79cda056dfc4..0b9071b16b21 100644 > --- a/arch/powerpc/Kconfig > +++ b/arch/powerpc/Kconfig > @@ -217,7 +217,7 @@ config PPC > select GENERIC_PCI_IOMAP if PCI > select GENERIC_SMP_IDLE_THREAD > select HAS_IOPORT if PCI > - select HAVE_ALIGNED_STRUCT_PAGE if PPC_HAS_LQARX && CPU_LITTLE_ENDIAN && CC_HAS_PTI_MODE_VAR_ATTR > + select HAVE_ALIGNED_STRUCT_PAGE if PPC_HAS_LQARX && CPU_LITTLE_ENDIAN > select HAVE_ARCH_AUDITSYSCALL > select HAVE_ARCH_HUGE_VMALLOC if HAVE_ARCH_HUGE_VMAP > select HAVE_ARCH_HUGE_VMAP if PPC_RADIX_MMU || PPC_8xx > @@ -238,7 +238,7 @@ config PPC > select HAVE_ARCH_SECCOMP_FILTER > select HAVE_ARCH_TRACEHOOK > select HAVE_ASM_MODVERSIONS > - select HAVE_CMPXCHG_DOUBLE if PPC_HAS_LQARX && CPU_LITTLE_ENDIAN && CC_HAS_PTI_MODE_VAR_ATTR > + select HAVE_CMPXCHG_DOUBLE if PPC_HAS_LQARX && CPU_LITTLE_ENDIAN > select HAVE_CONTEXT_TRACKING_USER > select HAVE_C_RECORDMCOUNT > select HAVE_DEBUG_KMEMLEAK > diff --git a/arch/powerpc/include/asm/cmpxchg.h b/arch/powerpc/include/asm/cmpxchg.h > index b3164c3c0b81..efd7261a4a53 100644 > --- a/arch/powerpc/include/asm/cmpxchg.h > +++ b/arch/powerpc/include/asm/cmpxchg.h > @@ -619,13 +619,20 @@ __cmpxchg_u64_acquire(u64 *p, unsigned long old, unsigned long new) > #endif > > #ifdef CONFIG_HAVE_CMPXCHG_DOUBLE > +#ifdef CONFIG_CC_HAS_PTI_MODE_VAR_ATTR > +#define __PPC_PTI_MODE __mode(PTI) > +#else > +#define __PPC_PTI_MODE > +#endif > + > union __u128_halves { > - u128 full __mode(PTI); > + u128 full __PPC_PTI_MODE; > struct { > u64 low, high; > }; > }; > > +#ifdef CONFIG_CC_HAS_PTI_MODE_VAR_ATTR > #define __CMPXCHG128(name, entry_mb, exit_mb) \ > static __always_inline u128 \ > __cmpxchg_u128##name(volatile u128 *p, u128 old, u128 new) \ > @@ -656,6 +663,39 @@ __cmpxchg_u128##name(volatile u128 *p, u128 old, u128 new) \ > \ > return r.full; \ > } > +#else > +#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) }, \ > + n = { .full = (new) }; \ > + \ > + __asm__ __volatile__ ( \ > + entry_mb \ > +"1: lqarx 14,0,%[ptr]\n" \ > +" xor %[tl],15,%[ol]\n" \ > +" xor %[th],14,%[oh]\n" \ > +" or. %[th],%[th],%[tl]\n" \ > +" bne- 2f\n" \ > +" mr 17,%[nl]\n" \ > +" mr 16,%[nh]\n" \ > +" stqcx. 16,0,%[ptr]\n" \ > +" bne- 1b\n" \ > + exit_mb \ > +"2: mr %[rh],14\n" \ > +" mr %[rl],15\n" \ > + : [rl] "=&r" (r.low), [rh] "=&r" (r.high), \ > + [tl] "=&r" (tmp.low), [th] "=&r" (tmp.high), \ > + "+m" (*p) \ > + : [ptr] "r" (p), \ > + [ol] "r" (o.low), [oh] "r" (o.high), \ > + [nl] "r" (n.low), [nh] "r" (n.high) \ > + : "r14", "r15", "r16", "r17", "cc", "memory"); \ Would probably be better to declare some local variable as asm("r14") and asm("r15") instead of using clobber, something like: register u64 pair_hi asm("r14"); register u64 pair_lo asm("r15"); Also, why using non-volatile registers, forcing GCC to save them and restore them ? Why not use r10/r11 instead which are volatile ? And do we really need r16/r17 at all ? Can't we reuse the same pair as lqarx ? > + \ > + return r.full; \ > +} > +#endif > > __CMPXCHG128(, PPC_ATOMIC_ENTRY_BARRIER, PPC_ATOMIC_EXIT_BARRIER) > __CMPXCHG128(_local, ,) ^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH 3/3] powerpc: Enable lbarx/lharx for Power10 2026-11-25 17:40 [PATCH 0/3] powerpc: Implement cmpxchg128() Nysal Jan K.A. 2026-11-25 17:40 ` [PATCH 1/3] powerpc: Add support for 128-bit cmpxchg Nysal Jan K.A. 2026-11-25 17:40 ` [PATCH 2/3] powerpc: Implement 128-bit cmpxchg fallback Nysal Jan K.A. @ 2026-11-25 17:40 ` Nysal Jan K.A. 2026-11-25 17:51 ` [PATCH 0/3] powerpc: Implement cmpxchg128() Nick Desaulniers 3 siblings, 0 replies; 12+ messages in thread From: Nysal Jan K.A. @ 2026-11-25 17:40 UTC (permalink / raw) To: linuxppc-dev Cc: Jeevitha Palanisamy, Nysal Jan K.A., Madhavan Srinivasan, Michael Ellerman, Nicholas Piggin, Christophe Leroy (CS GROUP), Ritesh Harjani (IBM), Shrikanth Hegde, Nathan Chancellor, Nick Desaulniers, Bill Wendling, Justin Stitt, Andrew Morton, David Hildenbrand (Arm), SJ Park, Mike Rapoport (Microsoft), Kiryl Shutsemau (Meta), Usama Arif, Kevin Brodsky, linux-kernel, llvm Byte (lbarx/stbcx.) and half word (lharx/sthcx.) atomic instructions were introduced in Power ISA 2.06 and are implemented on POWER7 and later CPUs. Select PPC_HAS_LBARX_LHARX for Power10 as well. Signed-off-by: Nysal Jan K.A. <nysal@linux.ibm.com> --- arch/powerpc/platforms/Kconfig.cputype | 1 + 1 file changed, 1 insertion(+) diff --git a/arch/powerpc/platforms/Kconfig.cputype b/arch/powerpc/platforms/Kconfig.cputype index a42fd4c64159..25d4a5eb3216 100644 --- a/arch/powerpc/platforms/Kconfig.cputype +++ b/arch/powerpc/platforms/Kconfig.cputype @@ -178,6 +178,7 @@ config POWER10_CPU select ARCH_HAS_FAST_MULTIPLIER select PPC_HAVE_PREFIXED_SUPPORT select PPC_HAVE_PCREL_SUPPORT + select PPC_HAS_LBARX_LHARX select PPC_HAS_LQARX config E5500_CPU -- 2.52.0 ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH 0/3] powerpc: Implement cmpxchg128() 2026-11-25 17:40 [PATCH 0/3] powerpc: Implement cmpxchg128() Nysal Jan K.A. ` (2 preceding siblings ...) 2026-11-25 17:40 ` [PATCH 3/3] powerpc: Enable lbarx/lharx for Power10 Nysal Jan K.A. @ 2026-11-25 17:51 ` Nick Desaulniers 2026-11-26 10:07 ` Nysal Jan K.A. 3 siblings, 1 reply; 12+ messages in thread From: Nick Desaulniers @ 2026-11-25 17:51 UTC (permalink / raw) To: Nysal Jan K.A. Cc: linuxppc-dev, Jeevitha Palanisamy, Madhavan Srinivasan, Michael Ellerman, Nicholas Piggin, Christophe Leroy (CS GROUP), Ritesh Harjani (IBM), Shrikanth Hegde, Nathan Chancellor, Bill Wendling, Justin Stitt, Andrew Morton, David Hildenbrand (Arm), SJ Park, Usama Arif, Kevin Brodsky, Kiryl Shutsemau (Meta), linux-kernel, llvm On Wed, Nov 25, 2026 at 9:41 AM Nysal Jan K.A. <nysal@linux.ibm.com> wrote: > > This series adds arch_cmpxchg128() and arch_cmpxchg128_local() for > little-endian POWER8 and later, using lqarx/stqcx. This enables > HAVE_CMPXCHG_DOUBLE and HAVE_ALIGNED_STRUCT_PAGE, which gives us: > > * SLUB's lockless __update_freelist_fast() path (avoiding slab_lock()). > * kmalloc_nolock(), without which BPF task-local storage creation fails > with -ENOMEM, and the selftests task_local_data_race and > struct_ops_assoc (timer subtests) fail. BPF Arena kfuncs were changed > to be callable from any context. kmalloc_nolock() replaced kvcalloc() > in the arena allocation paths because kvcalloc() may sleep and take > ordinary allocator locks, while kmalloc_nolock() can run in NMI or > non-sleepable contexts. > > The lqarx and stqcx. instructions require the 128-bit operand to reside in > a consecutive even/odd register pair. In the Power ISA, the operand uses > big-endian register layout where the most significant and least > significant 64 bits are in the even and odd registers respectively. > Until now this could not be expressed in inline asm without hard > register constraints [1]. > > GCC now supports this through the PTI mode attribute [2,3] for PowerPC > and it will be available in the upcoming GCC 17 release. Backports have > landed in the GCC 14, 15 and 16 branches. GCC 16.2 is the first release > which has PTI mode support. For older GCC releases and clang there is a > fallback implementation with hard register constraints. Is there a feature request in llvm's bug tracker? If so, can you link to it? If not, can you please file one, then link to it? https://github.com/llvm/llvm-project/issues > > Additionally, this series enables PPC_HAS_LBARX_LHARX for Power10, which > was previously omitted in the Kconfig. > > [1] https://github.com/linuxppc/issues/issues/463 > [2] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106895 > [3] https://gcc.gnu.org/pipermail/gcc-patches/2026-April/712831.html > > Nysal Jan K.A. (3): > powerpc: Add support for 128-bit cmpxchg > powerpc: Implement 128-bit cmpxchg fallback > powerpc: Enable lbarx/lharx for Power10 > > arch/powerpc/Kconfig | 8 ++ > arch/powerpc/include/asm/cmpxchg.h | 100 +++++++++++++++++++++++++ > arch/powerpc/platforms/Kconfig.cputype | 4 + > 3 files changed, 112 insertions(+) > > > base-commit: 65e4fd942f72a9dba0e4e257290c4fa1aa756854 > -- > 2.52.0 > -- Thanks, ~Nick Desaulniers ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH 0/3] powerpc: Implement cmpxchg128() 2026-11-25 17:51 ` [PATCH 0/3] powerpc: Implement cmpxchg128() Nick Desaulniers @ 2026-11-26 10:07 ` Nysal Jan K.A. 0 siblings, 0 replies; 12+ messages in thread From: Nysal Jan K.A. @ 2026-11-26 10:07 UTC (permalink / raw) To: Nick Desaulniers Cc: linuxppc-dev, Jeevitha Palanisamy, Madhavan Srinivasan, Michael Ellerman, Nicholas Piggin, Christophe Leroy (CS GROUP), Ritesh Harjani (IBM), Shrikanth Hegde, Nathan Chancellor, Bill Wendling, Justin Stitt, Andrew Morton, David Hildenbrand (Arm), SJ Park, Usama Arif, Kevin Brodsky, Kiryl Shutsemau (Meta), linux-kernel, llvm On Wed, Nov 25, 2026 at 09:51:30AM -0800, Nick Desaulniers wrote: > On Wed, Nov 25, 2026 at 9:41 AM Nysal Jan K.A. <nysal@linux.ibm.com> wrote: > > > > This series adds arch_cmpxchg128() and arch_cmpxchg128_local() for > > little-endian POWER8 and later, using lqarx/stqcx. This enables > > HAVE_CMPXCHG_DOUBLE and HAVE_ALIGNED_STRUCT_PAGE, which gives us: > > > > * SLUB's lockless __update_freelist_fast() path (avoiding slab_lock()). > > * kmalloc_nolock(), without which BPF task-local storage creation fails > > with -ENOMEM, and the selftests task_local_data_race and > > struct_ops_assoc (timer subtests) fail. BPF Arena kfuncs were changed > > to be callable from any context. kmalloc_nolock() replaced kvcalloc() > > in the arena allocation paths because kvcalloc() may sleep and take > > ordinary allocator locks, while kmalloc_nolock() can run in NMI or > > non-sleepable contexts. > > > > The lqarx and stqcx. instructions require the 128-bit operand to reside in > > a consecutive even/odd register pair. In the Power ISA, the operand uses > > big-endian register layout where the most significant and least > > significant 64 bits are in the even and odd registers respectively. > > Until now this could not be expressed in inline asm without hard > > register constraints [1]. > > > > GCC now supports this through the PTI mode attribute [2,3] for PowerPC > > and it will be available in the upcoming GCC 17 release. Backports have > > landed in the GCC 14, 15 and 16 branches. GCC 16.2 is the first release > > which has PTI mode support. For older GCC releases and clang there is a > > fallback implementation with hard register constraints. > > Is there a feature request in llvm's bug tracker? If so, can you link > to it? If not, can you please file one, then link to it? > https://github.com/llvm/llvm-project/issues I've opened one now and will add the link in v2 - https://github.com/llvm/llvm-project/issues/246292 Thanks --Nysal ^ permalink raw reply [flat|nested] 12+ messages in thread
end of thread, other threads:[~2026-11-30 22:01 UTC | newest] Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2026-11-25 17:40 [PATCH 0/3] powerpc: Implement cmpxchg128() Nysal Jan K.A. 2026-11-25 17:40 ` [PATCH 1/3] powerpc: Add support for 128-bit cmpxchg Nysal Jan K.A. 2026-11-25 20:44 ` Nathan Chancellor 2026-11-26 10:12 ` Nysal Jan K.A. 2026-11-26 11:27 ` David Laight 2026-11-30 17:58 ` Christophe Leroy (CS GROUP) 2026-11-30 22:01 ` David Laight 2026-11-25 17:40 ` [PATCH 2/3] powerpc: Implement 128-bit cmpxchg fallback Nysal Jan K.A. 2026-11-30 18:55 ` Christophe Leroy (CS GROUP) 2026-11-25 17:40 ` [PATCH 3/3] powerpc: Enable lbarx/lharx for Power10 Nysal Jan K.A. 2026-11-25 17:51 ` [PATCH 0/3] powerpc: Implement cmpxchg128() Nick Desaulniers 2026-11-26 10:07 ` Nysal Jan K.A.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®