Skip to contentSkip to conversation

powerpc: Implement cmpxchg128()

PATCH In review Nysal Jan K.A. 2 participants 5 messages Nov 25 PowerPC

lore
Share & tools
lore
#0 lore raw

cover powerpc: Implement cmpxchg128()

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

Signature · 1 line
2.52.0
#1 lore raw

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

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.

---
 arch/powerpc/Kconfig                   |  8 ++++
 arch/powerpc/include/asm/cmpxchg.h     | 60 ++++++++++++++++++++++++++
 arch/powerpc/platforms/Kconfig.cputype |  3 ++
 3 files changed, 71 insertions(+)
arch/powerpc/Kconfig +8 -0
index c3b6cd655606..79cda056dfc4 100644
@@ -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
arch/powerpc/include/asm/cmpxchg.h +60 -0

72 lines not on this page · raw message

arch/powerpc/platforms/Kconfig.cputype +3 -0
index f0adbf7ecb7f..a42fd4c64159 100644
@@ -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"
Signature · 1 line
2.52.0
#2 lore raw

PATCH 2/3 powerpc: Implement 128-bit cmpxchg fallback

In reply to Nysal Jan K.A.

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.

---
 arch/powerpc/Kconfig               |  4 +--
 arch/powerpc/include/asm/cmpxchg.h | 42 +++++++++++++++++++++++++++++-
 2 files changed, 43 insertions(+), 3 deletions(-)
arch/powerpc/Kconfig +2 -2
index 79cda056dfc4..0b9071b16b21 100644
@@ -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
arch/powerpc/include/asm/cmpxchg.h +41 -1

60 lines not on this page · raw message

Signature · 1 line
2.52.0
#3 lore raw

PATCH 3/3 powerpc: Enable lbarx/lharx for Power10

In reply to Nysal Jan K.A.

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.

---
 arch/powerpc/platforms/Kconfig.cputype | 1 +
 1 file changed, 1 insertion(+)
arch/powerpc/platforms/Kconfig.cputype +1 -0
index a42fd4c64159..25d4a5eb3216 100644
@@ -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
Signature · 1 line
2.52.0
#4 lore raw
In reply to Nysal Jan K.A.
Quoted 21 lines · Nysal Jan K.A.

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

Quoted 5 lines

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

Quoted 23 lines

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

Signature · 2 lines
Thanks,
~Nick Desaulniers