From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796061501; b=DMKwInPJZTbKieqxLmYqeLJ6vKmCiINadrpDhvfmbWQMo9ZlsgKQdTg6/IbaXrW0e06T mA4yO4ylWZOM8x4yGi2hzgiovXso+PS6D2AF5u3IRHGL8pT8Vy4HeszCmAQdWth29l1Dy es+B7dUbyiXOyfWGlRtSSMqQpd4sR+CuWb15akX6xfgqrbv+uAA7XDddNplsfvBB3Fb67 q1xpEbtQuoJ0U02XOPDDzf4d8ji9yqNgl6c+a4+t1ifewCt1qKuRq/QYALeBZZBnaAAhS 6gcXcXh6qaLkssOlCH3CC2jxVvY/WDsGk6OUcdIXbDbNWYP4O2wN3LUjvJTkEym19JA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796061501; h=Received:DKIM-Signature:Message-ID:Date:MIME-Version:User-Agent: Subject:To:Cc:References:Content-Language:From:In-Reply-To: Content-Type:Content-Transfer-Encoding; bh=SutIdhMJ6LWmZlLrsLpRnOgZQErK3NS9u6EWac18GSU=; b=SzJqVXMzkVIE3HJyq9lTKSAS7sPFhj01NnZMckWo/r2p5pKkqWgeFaYh38EomsJ3N+7S 5PM3qt0ad0abqCFYBuDKJZUk9Si4hMC+1lpZUa2k2iQIU6W71QsKjMiQgXdVYAsrMfdIR mSoRsDthOmtxeKjJEiMayMTzhRuAOQA3aMSEKuQbN90EoIlXRuhhETCPkvwxswZhiXFE8 qUtScVjXSIGZQZ3RDc77kEZPUYa+iJtT0X0uGi6AQQcn+af35mTAMgwAbgqZ9IOwwA3NX A9bKV792bdUlLhEKLMyJE/w/iZagRHd9wB4ocGvBI0hZ1h0FYR4Uq/5cKrwFIxavuzQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=GSK5adSz; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C146F606A33A; Mon, 30 Nov 2026 17:58:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=GSK5adSz Received: by smtp.kernel.org (Postfix) with ESMTPSA id 75B1C1F000C1; Mon, 30 Nov 2026 17:58:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796061500; bh=SutIdhMJ6LWmZlLrsLpRnOgZQErK3NS9u6EWac18GSU=; h=Date:Subject:To:Cc:References:From:In-Reply-To; b=GSK5adSzFiU1ocuzs8DGICXFDs4QjkTnVPQ/1K9j7zs/0vjPT4vPHyxjECoeTa5NB G4rRI4gQ6P+C3PCBQpLY66ZpSMiFmA0dNw64rAVK8MiYSNFR0Mo/dssiVKvXU2WoZx 3cwEKNvQYMvODex37gAKbF/axpKd+qxNhgmN4Xba2n8T+gVGyX5xGhY0Jo+4TsQmQp ef7LGgmG1myzLGUhlN4WKEfkfrl4MX+jZ9kez1VVIg4a5OrkK/kjYQzL8sjdassYgz XgovE/zrXQOBiBtn/ZUkYi1Tb8B3qtnJgufyOrBEYlGtLQ++2aGI8vhPYLbobBii1S oKl4qB9BdxjzQ== Message-ID: <82516881-5ffa-40f6-9d6a-549fcbb0ab00@kernel.org> Date: Mon, 30 Nov 2026 18:58:11 +0100 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 1/3] powerpc: Add support for 128-bit cmpxchg To: "Nysal Jan K.A." , linuxppc-dev@lists.ozlabs.org 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@vger.kernel.org, llvm@lists.linux.dev References: <20261125174035.1412724-1-nysal@linux.ibm.com> <20261125174035.1412724-2-nysal@linux.ibm.com> Content-Language: fr-FR From: "Christophe Leroy (CS GROUP)" In-Reply-To: <20261125174035.1412724-2-nysal@linux.ibm.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit 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. > --- > 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 > #include > #include > +#include > > #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"