From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.42 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796135655; b=dM64U7QqXfQFuu04BATYXcgzIFRMObGIaeyDo0x2uZQYC8724ynM5eV7ldkzuwipODVs 9xD+23kRoXhxJ5WqTpnB0uResAIu+3W1e60gkqGzL5gI6A+ncIoG/Q1q9Q2poqSj1QWpP qGk5fnaH7VOAdIKM5GbQqASTglEV83JYyHGSsRzsNOMq5RmuEJWLFmxMqYJbMayVLHDW8 ajzxgEq7jUpdLPCZFDaaO36OwuYLEsBQyvI2j19g8slimyRhahoUxPrEDTNdFArIIwAhm M0KFdcHx8xCnY9R8Wy9+ewuRMCKyxS7DZVp4XWm1AKctCAqEQyC1wsAVtS/Pgpq+Gww== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796135655; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References:X-Mailer: MIME-Version:Content-Type:Content-Transfer-Encoding; bh=OLlCtOqaNAl0Rp8544mqW3QYvYhZSggHv6GP0SizPXU=; b=or+65ln33mraMz1CLSRe1QWbQm9ztvkLtw9gQD/QevYXGAwwVU0I/5XgPhpNx4JPPg7Z uild108Cgn6A5jHtq5iUiRKBJin6vxkNmnK+dEIBxfVmfnLBCFe+Y1AyO81why/cSVhAw BQIJA9FNOfXsXojZbD70Doeyhj8EhkQ8Oq3a+soDtwjItNBPcgyCIzaUp6PQoY6EP1OOn QKpWqHu77MxDdX0o5z9Go11EjAQETKt2of8Oc1MwflCTFElUgYRec0fDydWYCGpiZqS/3 wF+TcEGVBfXq8l1QWX+L1JXL9G5AMkpDuAGAgDVV9MRqreXgjfB5IfSOYQjbZuES68A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=DaOgGSL8; arc=none smtp.remote-ip=209.85.128.42 Received: from mail-wm1-f42.google.com (mail-wm1-f42.google.com [209.85.128.42]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 927F2606A320 for ; Tue, 1 Dec 2026 14:34:15 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 927F2606A320 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com 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=DaOgGSL8 Received: by mail-wm1-f42.google.com with SMTP id 5b1f17b1804b1-4bb5b8ef642so98313035e9.1 for ; Tue, 01 Dec 2026 06:34:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796135654; x=1796740454; darn=lists.linux.dev; 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=OLlCtOqaNAl0Rp8544mqW3QYvYhZSggHv6GP0SizPXU=; b=DaOgGSL8wFG7jTbcb9UaUcwhLNFbfc9VaDlvhWUsgR4kObxmhCvqrmL7PlWHQyT98r 3eDDW9eQ6bT/hpv/X3NomPcgwMm8OInidbI5xTyV41ZsauFGvp3iMI45QDAC2R1+eCh1 5/7ypNfWI2PiO1zPeERAo6XhvBs7YjkBA5QaEgXmJYHGSFEKlarSeLTp7plmNhNrQe4L MJCEI8bJ52Ofd7O7fdkZ+5EkT4aL0ps546d/yFqVNf4qbOLQEQgjjaeOPJSlW3Gxfma0 X6coryxWoBEGL6JjceXHKJW5K3qD9YV5Rv9XN3GQrD5saZjoE3SO9zESLuBkLzpAlE8E HafA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796135654; x=1796740454; 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=OLlCtOqaNAl0Rp8544mqW3QYvYhZSggHv6GP0SizPXU=; b=Fl+psooRATX8GMQeIVIGFd+MK3JcJRXV/oZhT0p70gQlfi2qsV1MgF63+K6NI1R0KY mX0sI4xuxsGQmMlgCW08Hj8WYs+QZRzprNjfi4C3xGokpPUenJS2xFpf63HgyHWNJfl2 GyQKd7dqXgg5yqQad5NUqKy4mMZLh42+bluBPJZVTHxMa0R8w1RSz0wLvodrJ7ov3ReW DIQmJDgiNTeJhGttp8j3cbDiyxGMsc2WwjKlR4lgz9MJbkIlmLKLaISzYaT2/TSzlEWh ET6PVaTrpBRObVyp8lkwjEh09KhU1F8g3q5wdyWJG8e6TekuHZo8xapLfjEfXbNoarO2 18cg== X-Forwarded-Encrypted: i=1; AL+hErHZhyPAih6Cc9szekcQvi3QVSJtNNcjqYUNPwdzRHyoRcv8/S7RpOsnwEQWOggyPk3HVcbc@lists.linux.dev X-Gm-Message-State: APuU4Z9yYXzSvg41btNulkAg2FhwDB0mb+rL5FW3JGXCBqvITuyuAjjv wBD/rEMVL4/k8Jhzr3D5Ge8bkyWBoRXkKxxJDgekhf//4ROkb74cpual X-Gm-Gg: AZFdTZfUnoYanCuF49QEGStqY34UofU9fsh13oDrBaryp61t7by5mhygZN/YbS2eqa/ uItXfbPKqJrMmLlq7v7wbYS8dgfMK8/fEZrXN8O0k/47xn+/qUrowwQQ0mQR3TsjCoPkPbbq+67 STmmWQcv3uEI6ptp4Bp3qWa/BqcihIA9iU5X1K1iiyywJ5xsitU/SRpDMh+C+Do5Q7bmOgOWzjO 00eTvpHhkTDZN0Ao5n1Ar+4UnSKJU/TANnMyVRj1NamXVJIgqcLBN4lGmWFr9tubRrejdCmQS5C YwWOOEDhKsTC8Mg3McuO6fcHOAwFbwC0VxbgHaFIb94CbbBIa/nJPshWqkn29pkivk58bNndZCP 1Xj/frh1rUKQMIuUeGgkgWUvnkk8Zs0KVw4RZ1Qhd+5vWA7gJmGv/AbG5e+Kf2UGIVIhTHGNO94 xGDXW33vQqkKsqzcR8k3uVLkxTZGSEvL3cRVSKVFeH0rKVOzIiksVBUD9QSBCzrDM3D4mt/aFCQ OpWqqLi+BlpeSFbKNwvxASQ/VzQwKYGBclIZggQBdjSEmKInmt4kyIzvUKsoVrZvJejH/DZJonD XiNcvRkYbBxUReXYG2vQE/c4/WcZZCXmqXStpqvuJC3MSMNBeY5iFmKSZms8o427qAddtxleL2s onMbgZHRLuw== X-Received: by 2002:a05:600c:3e1a:b0:4bb:6814:7391 with SMTP id 5b1f17b1804b1-4bb8881921cmr731408625e9.25.1796135653734; Tue, 01 Dec 2026 06:34:13 -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 5b1f17b1804b1-4bbc184d8dcsm164232855e9.1.2026.12.01.06.34.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2026 06:34:13 -0800 (PST) Date: Tue, 1 Dec 2026 14:34:11 +0000 From: David Laight To: "Nysal Jan K.A." Cc: "Christophe Leroy (CS GROUP)" , 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: <20261201143411.18a66710@pumpkin> In-Reply-To: References: <20261125174035.1412724-1-nysal@linux.ibm.com> <20261125174035.1412724-2-nysal@linux.ibm.com> <82516881-5ffa-40f6-9d6a-549fcbb0ab00@kernel.org> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; arm-unknown-linux-gnueabihf) X-Mailing-List: llvm@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On Tue, 1 Dec 2026 18:22:33 +0530 "Nysal Jan K.A." wrote: > On Mon, Nov 30, 2026 at 06:58:11PM +0100, Christophe Leroy (CS GROUP) wro= te: > >=20 > >=20 > > Le 25/11/2026 =C3=A0 18:40, Nysal Jan K.A. a =C3=A9crit=C2=A0: =20 > > > + 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 dire= ct > > hidden memory modification in the sequence. > > =20 >=20 > Yes, to act like a compiler barrier. I see a similar clobber for other > cmpxchg implementations in this header file, so took the conservative pat= h. I think you need to use "=3DZ" for the memory operand (allows reg+reg but n= ot reg+constant). All the register swaps shouldn't be needed. (I suspect the compiler is only passing the low 64bits? and you are picking up the high bits from r+1 because that is how they get passed on a function call.)=20 AFAICT lq and stq put the high bits into the even register and the low bits into the odd register (thus matching %L[xx]). But you may need to ensure both halves of a 128bit value are explicitly passed in (probably except for the __mode(PTI) ones). Since with: register long r4 asm("r4"), r5 asm("r5"); and [r4] "=3D&r" (r4), "=3D&r" (r5) you can use %L[r4] to reference r5; the asm instructions can be the same with and without PTI. You just need to tweak the variable definitions and constraints (you need the extra one for the odd register containing the low data. I'm also not sure it is worth the two scratch registers to avoid a conditional branch. You expect the compares to succeed, if not you are already on a slow path and a mispredect won't do any extra harm. The asm code might then be (I was going to try testing compiling it...): 1: lqarx %[rslt],%[mem] cmp %L[rslt],%[old_lo] bne- 2f: cmp %[rslt],%[old_hi] bne- 2f stqcx. %[new],%[mem] bne- 1b 2: For non-PTI would need something like: register u64 rslt_hi asm("r6"), rslt_lo("r7"); with the matching: [rslt] "=3D&r" (rslt_hi), "=3D&r" (rslt_lo) (instead of [rslt] "=3D&r" (rslt.full)) David >=20 > >=20 > > You get much better code if you leave the last three 'mr' to the compil= er, > > something like the following IIUC: > >=20 > > 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, u1= 28 > > 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, u= 128 > > new) \ > > [n] "r" (new) \ > > : "cc", "memory"); \ > > \ > > + tmp.low =3D r.low; \ > > + r.low =3D r.high; \ > > + r.high =3D tmp.low; \ > > + \ > > return r.full; \ > > } > > #else > >=20 > > =20 >=20 > I have something close to this in v2 that I'm working on. David suggested > something similar as well (to use arithmetic, I assume a shift left + or > operation to merge the low and high 64-bits). I'll test both and see what > works better. The swap in C does indeed produce better code when I did a = quick > check on godbolt. I'll do something similar for the fallback path as well. >=20 > --Nysal