From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from lists.ozlabs.org (lists.ozlabs.org [112.213.38.117]) (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 317DF606A31A for ; Tue, 1 Dec 2026 10:35:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=hJ2utynI Received: from boromir.ozlabs.org (localhost [127.0.0.1]) by lists.ozlabs.org (Postfix) with ESMTP id 4jdy2L6JVdz30Vm; Tue, 01 Dec 2026 21:35:06 +1100 (AEDT) Authentication-Results: lists.ozlabs.org; arc=none smtp.remote-ip="2a00:1450:4864:20::330" ARC-Seal: i=1; a=rsa-sha256; d=lists.ozlabs.org; s=201707; t=1796121306; cv=none; b=T7dW5PgOOlJFxkFIZ6zNY6pGeCKRgff/AN8uiuH9h9pplyIr65cWoBOCy6/x61Qk03+AYxy0dioD3WMtx72hAaMqMUQa/AUw0hC8rx3H3irdl7eQCNTbL5JocWmUscsamSGz12hfFvT0pZBH4JypymtPPcZWBhaX5X3mVftGHFwKxLqforVl8MuU91iUwBpu3Q6ppOleywmC4sgR57cALzfEAXx13+bif/6ldCA8y90YHLHKGOleZ/IpxQ8wviNcWKmml+QFMFyQmfHfpwOH5+lD700Q/YFu/ocuSAVEDgCCZkR1mhMn6rGzdpQs+lNFjSmd+RU6tRzm3bUJV9imrg== ARC-Message-Signature: i=1; a=rsa-sha256; d=lists.ozlabs.org; s=201707; t=1796121306; c=relaxed/relaxed; bh=ELk3vSt+82dHwi2Y8un4aJk8hHOKfWlmWdGmtw4G1H8=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=TXaiQzj31gj+BANKX1D1q4w3KuKkBwt/4Q6Gx8XmnGLIUCMr504x8I6yh6rzxbPysF1QsVt+rNqh2UHlUiqFMi/hYpVjJlpl38P5bHoY9jCXf79qsK38KNwevGrZy8bCmcVHyyGJLinde7UbC/Zp/z3Z3WmoTXj+Y2l3N9YwAGTPCvRLuKK4XU1bXxN6aCiPiWH5uLU4I4IAux/wQF6rnBiR8KCrMS/vCEUj8lmfgaY8h8CGH+dV74v/ikYR9AQ7tQ7/30uJ42Ad9QO47MBshBSE4jz4bBj04Ggwxbp5lfobbiMNUnBvaBhrCwF5BG0SbY5U+EdcIPxXWwJo4pDRdw== ARC-Authentication-Results: i=1; lists.ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=hJ2utynI; dkim-atps=neutral; spf=pass (client-ip=2a00:1450:4864:20::330; helo=mail-wm1-x330.google.com; envelope-from=david.laight.linux@gmail.com; receiver=lists.ozlabs.org) smtp.mailfrom=gmail.com Authentication-Results: lists.ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=hJ2utynI; dkim-atps=neutral Authentication-Results: lists.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gmail.com (client-ip=2a00:1450:4864:20::330; helo=mail-wm1-x330.google.com; envelope-from=david.laight.linux@gmail.com; receiver=lists.ozlabs.org) Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 4jdy2J1bg8z2yMK for ; Tue, 01 Dec 2026 21:35:03 +1100 (AEDT) Received: by mail-wm1-x330.google.com with SMTP id 5b1f17b1804b1-4b79502c27aso116080805e9.1 for ; Tue, 01 Dec 2026 02:35:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796121298; x=1796726098; darn=lists.ozlabs.org; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:subject:cc:to:from:date:from:to:cc:subject :date:message-id:reply-to:content-type; bh=ELk3vSt+82dHwi2Y8un4aJk8hHOKfWlmWdGmtw4G1H8=; b=hJ2utynITjYgspQ5QPQCz6+Csd57/SJjW/8yRCl3P0BRYUCmCOYmJXbWbABE6wkvgd 3L/xwUsZxJvxXnKM2dOozxAh/SFI761vrn7atrwKc54YJDmOv5FE4nVD2mSgFLYif/gk WBtLqsidWHqwwNB2v6M1S9Zv3DmXQHCV3zlb79soooC/Z1bjKawQIOvo5UH40B3rYLpV JC9t/Xqp2NsIsrfCi++FvKF/OtdqvdCkJlAKyw5cSFqkDCeKTq8q2SS/zA3hcVOlc+eg 59dNN73ZFHi+XoaTKimdkkKAVlkYx+tPr8+nGk/kRgHyx2oo4yNN5w6AC7vqwguJ8K6x 7Caw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796121298; x=1796726098; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:subject:cc:to:from:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=ELk3vSt+82dHwi2Y8un4aJk8hHOKfWlmWdGmtw4G1H8=; b=MUiRwT3sML6b6Yi0z3WucbJG+ZHN7XzY+zgo8DA/9aQ0YpktNii4h6sabpLnmpg4/u TLQ+JxuRhVv/ByxdRtIMFnGrkezRrEne9lB1FIrtKyuWOESpHmcg51rKclP2bcd/PuVJ ZbBbSMj+KRNufMpb+xFXnv2e5ZyIJQIDCe9B3XORBilAGPd9PWYsln0tROHJETqXu+Fb u0XVJo18PW/R5Z+bVUJyl2tLyg+E91uVIGtht2Ky0YaU71tyXFnQJ3Ct8lpm4FV5vVOB oo4ssEUeNSK0Lkh7e0VpkB+JzhtkiLx9nts6HDgCzMlFJCjVb1vzPr2EAbPRCp0D5tKS 8Wdw== X-Forwarded-Encrypted: i=1; AL+hErFZLymBz7R8emlc+95sETfR0l0ef8ZJrS1LqJWEUbx2eanj/jHZWep0fF4DLuXx2uzfmozLEVy791jYO3A=@lists.ozlabs.org X-Gm-Message-State: APuU4Z9dJOqW7T6wfepwx05UHLY+vKVdSxkZ6mospKWyyzpDm8LO57vu cmit/fysNtf0xxdA13c6/Hl1jcLYC0leArSiNL2dJ3IgJTGYXjf5oNms X-Gm-Gg: AZFdTZfBgtO2BLC/JB/SwsyXzdU7xzsMDAjVqm4C5IKKmMk/+ICQz9RjOBQ1t8ggf7d aI72/I+2zatsSoHlFzCzp6nCGDxk8QEoWTXlDQKsfnBWRDaDo4FdMwt8JXMLRKu3lgLT0y4B9W8 7NemyRjMqAGtd0WCLaRo12HAiz0xE6h/Kzi6+og2H6e6pwwyIk37QCEXUBVH+28OCfGKh1UQUhy 21IlNHINKNmbYBk7qJYh/a93zKnCoSxpwEcMfpCXtRYXFUo5Qoj+hnTUEGetMcf8Aoi+ki6zG5k UOgjdL7+crTI+iWU4XTdqb0upGJ4gckIRarYjnuCI1P3epkzi+EidX3iUY74iPvfJY2+2/5fNMK 0NaxtTQO70Pu9iZcor13wXxEgsYEQtFBY0Uqlro4O3qyzLb0/9bbOLZlpWfCntfpyPRA+MJq1sw CoSzQHUTwp//88oO7g2+ZiXM7BBP2RWSL8lMX/gfdmp2f/YKkpuXtuIKyJgSr4t5qp4j37ZL8yq tlc4Y/dZsqcslTuXl0lGjMSfeqVprll6sxNFJoKq4QKRrR3hw8oOCOB+1IppGy6GJN//e5Smb6P xGqy04nNTWqcMgug0UPpo3wNbe3AiU9pV3WgzMe6Z/rizbUh+uHKjQIH9Jp2r+a1 X-Received: by 2002:a05:600c:483:b0:4bb:a9f7:681 with SMTP id 5b1f17b1804b1-4bba9f70685mr252792045e9.3.1796121298284; Tue, 01 Dec 2026 02:34:58 -0800 (PST) Received: from pumpkin (82-69-66-36.dsl.in-addr.zen.co.uk. [82.69.66.36]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4ab69bf630esm72101474f8f.16.2026.12.01.02.34.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2026 02:34:57 -0800 (PST) Date: Tue, 1 Dec 2026 10:34:56 +0000 From: David Laight To: "Christophe Leroy (CS GROUP)" Cc: "Nysal Jan K.A." , linuxppc-dev@lists.ozlabs.org, 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 Subject: Re: [PATCH 1/3] powerpc: Add support for 128-bit cmpxchg Message-ID: <20261201103456.57c4a5ab@pumpkin> In-Reply-To: <20261130220123.1cb380db@pumpkin> References: <20261125174035.1412724-1-nysal@linux.ibm.com> <20261125174035.1412724-2-nysal@linux.ibm.com> <82516881-5ffa-40f6-9d6a-549fcbb0ab00@kernel.org> <20261130220123.1cb380db@pumpkin> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; arm-unknown-linux-gnueabihf) X-Mailing-List: linuxppc-dev@lists.ozlabs.org List-Id: List-Help: List-Owner: List-Post: List-Archive: , List-Subscribe: , , Precedence: list X-Archived-By: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-19.3 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,MENTIONS_GIT_HOSTING,SPF_HELO_PASS,SPF_PASS, USER_AGENT_SANE_2 autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 On Mon, 30 Nov 2026 22:01:23 +0000 David Laight wrote: > On Mon, 30 Nov 2026 18:58:11 +0100 > "Christophe Leroy (CS GROUP)" wrote: >=20 > > Le 25/11/2026 =C3=A0 18:40, Nysal Jan K.A. a =C3=A9crit=C2=A0: =20 > > > Implement arch_cmpxchg128() and arch_cmpxchg128_local() for 64-bit > > > little-endian PowerPC (POWER8 and later) using the lqarx and stqcx. > > > instructions. > > >=20 > > > This enables HAVE_CMPXCHG_DOUBLE, which allows SLUB to use the lockle= ss > > > __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. > > >=20 > > > 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. > > >=20 > > > Link: https://github.com/linuxppc/issues/issues/463 > > > Link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D106895 > > > 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(+) > > >=20 > > > 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=3Dpower10 -mpcre= l) > > > =20 > > > +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_END= IAN && 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 > > > =20 > > > +config PPC_HAS_LQARX > > > + bool > > > + > > > config EARLY_PRINTK > > > bool > > > default y > > > diff --git a/arch/powerpc/include/asm/cmpxchg.h b/arch/powerpc/includ= e/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 > > > =20 > > > #ifdef __BIG_ENDIAN > > > #define BITOFF_CAL(size, off) ((sizeof(u32) - size - off) * BITS_PE= R_BYTE) > > > @@ -617,6 +618,65 @@ __cmpxchg_u64_acquire(u64 *p, unsigned long old,= unsigned long new) > > > } > > > #endif > > > =20 > > > +#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 =3D { .full =3D (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] "=3D&r" (r.full), \ > > > + [t] "=3D&r" (tmp.full), "+m" (*p) \ > > > + : [ptr] "r" (p), \ > > > + [ol] "r" (o.low), [oh] "r" (o.high), \ > > > + [n] "r" (new) \ > > > + : "cc", "memory"); \ =20 > >=20 > > Why a memory clobber ? Is it to get a barrier ? Otherwise I see no=20 > > direct hidden memory modification in the sequence. > > =20 > > > + \ > > > + return r.full; \ > > > +} =20 > >=20 > > You get much better code if you leave the last three 'mr' to the=20 > > compiler, something like the following IIUC: > >=20 > > diff --git a/arch/powerpc/include/asm/cmpxchg.h=20 > > 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,=20 > > 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] "=3D&r" (r.full), \ > > [t] "=3D&r" (tmp.full), "+m" (*p) \ > > : [ptr] "r" (p), \ > > @@ -661,6 +659,10 @@ __cmpxchg_u128##name(volatile u128 *p, u128 old,=20 > > u128 new) \ > > [n] "r" (new) \ > > : "cc", "memory"); \ > > \ > > + tmp.low =3D r.low; \ > > + r.low =3D r.high; \ > > + r.high =3D tmp.low; \ > > + \ > > return r.full; \ =20 >=20 > Or even just: > return (u128)r.low << 64 | r.high; >=20 > 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 swappe= d). >=20 > 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 =3D { .low =3D old, .high =3D old >> 64 } >=20 > 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 num= bered > 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 =3D old, _new =3D new; \ > _cmpxchg(old >> 64, old, new >> 64, new, p);}) >=20 > But maybe that isn't quite right. I wrote that while falling asleep last night... Let me think some more while awake. David >=20 >=20 > David >=20 > > } > > #else > >=20 > > =20 > > > + > > > +__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)) !=3D 16); \ > > > + __cmpxchg_u128((ptr), (o), (n)); \ > > > +}) > > > + > > > +#define arch_cmpxchg128_local(ptr, o, n) \ > > > +({ \ > > > + BUILD_BUG_ON(sizeof(*(ptr)) !=3D 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/pl= atforms/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 > > > =20 > > > config POWER9_CPU > > > bool "POWER9" > > > depends on PPC_BOOK3S_64 > > > select ARCH_HAS_FAST_MULTIPLIER > > > select PPC_HAS_LBARX_LHARX > > > + select PPC_HAS_LQARX > > > =20 > > > 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 > > > =20 > > > config E5500_CPU > > > bool "Freescale e5500" =20 > >=20 > > =20 >=20 >=20