From: "Nysal Jan K.A." <nysal@linux.ibm.com>
To: linuxppc-dev@lists.ozlabs.org
Cc: Jeevitha Palanisamy <jeevitha@linux.ibm.com>,
"Nysal Jan K.A." <nysal@linux.ibm.com>,
Madhavan Srinivasan <maddy@linux.ibm.com>,
Michael Ellerman <mpe@ellerman.id.au>,
Nicholas Piggin <npiggin@gmail.com>,
"Christophe Leroy (CS GROUP)" <chleroy@kernel.org>,
"Ritesh Harjani (IBM)" <ritesh.list@gmail.com>,
Shrikanth Hegde <sshegde@linux.ibm.com>,
Nathan Chancellor <nathan@kernel.org>,
Nick Desaulniers <ndesaulniers@google.com>,
Bill Wendling <morbo@google.com>,
Justin Stitt <justinstitt@google.com>,
Andrew Morton <akpm@linux-foundation.org>,
"David Hildenbrand (Arm)" <david@kernel.org>,
Yeoreum Yun <yeoreum.yun@arm.com>, SJ Park <sj@kernel.org>,
"Kiryl Shutsemau (Meta)" <kas@kernel.org>,
Kevin Brodsky <kevin.brodsky@arm.com>,
Usama Arif <usama.arif@linux.dev>,
linux-kernel@vger.kernel.org, llvm@lists.linux.dev
Subject: [PATCH 2/3] powerpc: Implement 128-bit cmpxchg fallback
Date: Wed, 25 Nov 2026 23:10:23 +0530 [thread overview]
Message-ID: <20261125174035.1412724-3-nysal@linux.ibm.com> (raw)
In-Reply-To: <20261125174035.1412724-1-nysal@linux.ibm.com>
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
next prev parent reply other threads:[~2026-11-25 17:42 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
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. [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261125174035.1412724-3-nysal@linux.ibm.com \
--to=nysal@linux.ibm.com \
--cc=akpm@linux-foundation.org \
--cc=chleroy@kernel.org \
--cc=david@kernel.org \
--cc=jeevitha@linux.ibm.com \
--cc=justinstitt@google.com \
--cc=kas@kernel.org \
--cc=kevin.brodsky@arm.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linuxppc-dev@lists.ozlabs.org \
--cc=llvm@lists.linux.dev \
--cc=maddy@linux.ibm.com \
--cc=morbo@google.com \
--cc=mpe@ellerman.id.au \
--cc=nathan@kernel.org \
--cc=ndesaulniers@google.com \
--cc=npiggin@gmail.com \
--cc=ritesh.list@gmail.com \
--cc=sj@kernel.org \
--cc=sshegde@linux.ibm.com \
--cc=usama.arif@linux.dev \
--cc=yeoreum.yun@arm.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®