From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.41 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796135655; b=B6latnfKkYsmAAg2glVBV6b55S3cvAC0ox8Gi7RIRxoCdhD/CbcyzqS+SEIjmJT96PnE keadw334yd/S/k2661nq3N2pYYDKYzN6YdJYHlVW06USjvCOGWqKEvZwv2+s/+/QWFpPH r5SrB+M0hcTD21CpPkAz8vnpdySGpiyW3XGBTTMWexLs6xGY9sOXSw020jozrR8zA3z+y ZQrPVuPHNpUuF38Q46/8ExYSSg/F59EYy9uMDk/tUIMdr3oZq+GSqf/Ht4WcREER8D8oW 3knY8WR8WI0uNiPpvh8Kw5EeG2J+wqRGBwTEg2w18rE8hq6ljDgrb87mXpT51u4ntiQ== 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=ntHivwBK+YhSx5RwqubZRzZ/DGwhF+1DUVG3+gj8MYVGIZvUuRjhA/bw99K3Mm+wdbdO v1kAgNNIdZ0u0z/rgFpWXBJSZGUfs5l/smx8g1SBs2WqrBE+wCwok+x4BWbwKX9+ICmE1 QSOJgzOlX5bV98q6anZcrJEsUmGyBwOhqQ99PQ/w4dLj9qOSc/xcfBvfGXIpxgTG24mvF JEg3tBiMWUHplxOiaihcJSKutJM9rDIJSfzEvBxC1+tf8ewS7xaViJp7xWWk6wMjs5lQp TiAf59T5xqNqIgtb8r+nlIGkboY307dbxykw/UjziEaInauZktwLcG8kkqsYhEaRczA== 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=blmiGajY; arc=none smtp.remote-ip=209.85.128.41 Received: from mail-wm1-f41.google.com (mail-wm1-f41.google.com [209.85.128.41]) (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 8332E606A31A for ; Tue, 1 Dec 2026 14:34:15 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 8332E606A31A 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, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=blmiGajY Received: by mail-wm1-f41.google.com with SMTP id 5b1f17b1804b1-4bb5b8ef642so98313025e9.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=vger.kernel.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=OLlCtOqaNAl0Rp8544mqW3QYvYhZSggHv6GP0SizPXU=; b=blmiGajYNeBu5UryP3TZ2VxLEOLY1Bfk56Vws5fpKBsKCDDkoVXegfo85Rke1i8+cc VD8qfZWCUmdDKTHPw7VvTlXgXfUxuJGX5WO+9kVYkpeJAsHS4wI3dXpVpUeY0r8haBxo csGfAv/gJpRDma++sAx3sb223sX2D1QS3hTtdOCfruD8DEysSCs/m8kTqGw0DobLndAp FKgJQWchOIGdXmjGVCKzJjC7FR4CU0IJA31y0cipwwoDTPvzN/ov0RlnD9CadrolJCPQ JZGDQmVekB4pGB6TKElsOPs/DSk0YnuJOTalxUAavQY6sKV7/gl9UCefMl4ZYN1BN2Cg ZN4g== 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=kk6BUEAkEo5jpOtwLDy4dEiKm1cdGdGA4xldGxx2ictrPatkjF0snUXTrR7GN9gDe0 o2PyT3j4lrGZhMCqifVJea/2LVMOpSmAFLUB0bK96xwhPk1YdvkEewDNUODPR+9sHakg vvOSDPBpwqcf/EW4Iq14XGfKov1hK6isDUzIzrpf2Q8aXnuoX6jNSjn0BF4MoiVIYPdx eynU8DSNr25l1zb0kG6rVSsw+9rRwSzSCstVuTteBRlgeb2CmGQYeO0x7wMuXiAj8k54 aQVwkZSsdIle3N7CVQgJu2/NugYr7dGAnFOh2jpAleqWLGVINb8sUQ8XkZJhtBqq93SN xKuQ== X-Forwarded-Encrypted: i=1; AL+hErGdnjDGI8jStdGBCnvdgrTH3cO5h8MUk1PwxMIpwRI/otHvJSefvcEyeEV4qz7vInhGIZcHAbc4XscKVJY=@vger.kernel.org X-Gm-Message-State: APuU4Z9p/vgg7MnKocK2aCtfMCQV4H8zH34FzKT1XOhlk9MrarQNfQUW wwJoczLD7ggHG2E8q7SU5vUBS2H0tW5hdsibVlkIKwf4e792d2iDt6XU X-Gm-Gg: AZFdTZdXieCvYqz2JAGd6vHkhh/gxo3Exn8fkV35PEY75dG4ObvSVIQyBiif5/kURVf s+nUpYN2iV8AtgAab75Dw+fOGOhSWadt/IGykml8t+uJyU66csYW1C7uJrflvs4QaOPr0U7jnc0 wh+XcL9W3LUpZzED3U2s+IRkte494ycERK8Ff5XPFgUBPxyKcIxPB6g8ZP2jg8HZcR8kK8cKbp+ vuuoa5sdLwpUznXz4tuX2HA4fCwXjWox1Sz4nhMnh4ihcGxkvgGZash3bWfnbpyKG25fTT9Dg9g 55a4kq/Oq8CX+qPSSBs9G+yc+yj/Lr8ed5Il4+D7vnCh3jKP2+Fw0yxvxW6tMSDygW+5pY0Iplv bQy9oM5ccfqZBHthJt1qSIXf+W3CFPNdZLMZSAzXfXxr9QvpGu8R94IGq3kI5V2g1NsNIxyqYNU DKnqq2gcvHPmxrJBGP06+1pIArmY0mFDeIUWqxNvJduLucb56iVzutfri+jNbMIObLv8KWH7D/L xxV8+zfwfc/UE1UPHTYddkzGOSmMTRhmp4GkKJL0kZjWpPvjCN/ksS5cNC0G/wBwtMkZQCL3vLF W9IQsK9s7lHsslogNN+r1DfzQmzxpLqjKpjypQ/NGqopCFvKfJ+OozlIJRQ4FXc/TBz5HtH04Oj 9Ge+K01YIVg== 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: linux-kernel@vger.kernel.org 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