Building the Linux kernel with Clang and LLVM
 help / color / mirror / Atom feed
From: David Laight <david.laight.linux@gmail.com>
To: "Nysal Jan K.A." <nysal@linux.ibm.com>
Cc: "Christophe Leroy (CS GROUP)" <chleroy@kernel.org>,
	linuxppc-dev@lists.ozlabs.org,
	Jeevitha Palanisamy <jeevitha@linux.ibm.com>,
	Madhavan Srinivasan <maddy@linux.ibm.com>,
	Michael Ellerman <mpe@ellerman.id.au>,
	Nicholas Piggin <npiggin@gmail.com>,
	"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>,
	Ryan Roberts <ryan.roberts@arm.com>,
	Andreas Larsson <andreas@gaisler.com>,
	Usama Arif <usama.arif@linux.dev>,
	Kevin Brodsky <kevin.brodsky@arm.com>,
	"Kiryl Shutsemau (Meta)" <kas@kernel.org>,
	linux-kernel@vger.kernel.org, llvm@lists.linux.dev
Subject: Re: [PATCH 1/3] powerpc: Add support for 128-bit cmpxchg
Date: Tue, 1 Dec 2026 14:34:11 +0000	[thread overview]
Message-ID: <20261201143411.18a66710@pumpkin> (raw)
In-Reply-To: <aw6-J9gtWTeRN-JS@li-398ad3cc-28e7-11b2-a85c-e030a5b10d54.ibm.com>

On Tue, 1 Dec 2026 18:22:33 +0530
"Nysal Jan K.A." <nysal@linux.ibm.com> wrote:

> On Mon, Nov 30, 2026 at 06:58:11PM +0100, Christophe Leroy (CS GROUP) wrote:
> > 
> > 
> > Le 25/11/2026 à 18:40, Nysal Jan K.A. a écrit :  
> > > +	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.
> >   
> 
> Yes, to act like a compiler barrier. I see a similar clobber for other
> cmpxchg implementations in this header file, so took the conservative path.

I think you need to use "=Z" for the memory operand (allows reg+reg but not
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.) 
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] "=&r" (r4), "=&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] "=&r" (rslt_hi), "=&r" (rslt_lo)
(instead of [rslt] "=&r" (rslt.full))

David

> 
> > 
> > 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
> > 
> >   
> 
> 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.
> 
> --Nysal


  reply	other threads:[~2026-12-01 14:34 UTC|newest]

Thread overview: 19+ 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 20:44   ` Nathan Chancellor
2026-11-26 10:12     ` Nysal Jan K.A.
2026-11-26 11:27   ` David Laight
2026-12-01 13:52     ` Nysal Jan K.A.
2026-12-01 14:48       ` David Laight
2026-12-01 14:58         ` David Laight
2026-11-30 17:58   ` Christophe Leroy (CS GROUP)
2026-11-30 22:01     ` David Laight
2026-12-01 10:34       ` David Laight
2026-12-01 12:52     ` Nysal Jan K.A.
2026-12-01 14:34       ` David Laight [this message]
2026-11-25 17:40 ` [PATCH 2/3] powerpc: Implement 128-bit cmpxchg fallback Nysal Jan K.A.
2026-11-30 18:55   ` Christophe Leroy (CS GROUP)
2026-12-01 13:25     ` Nysal Jan K.A.
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
2026-11-26 10:07   ` Nysal Jan K.A.

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=20261201143411.18a66710@pumpkin \
    --to=david.laight.linux@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=andreas@gaisler.com \
    --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=nysal@linux.ibm.com \
    --cc=ritesh.list@gmail.com \
    --cc=ryan.roberts@arm.com \
    --cc=sshegde@linux.ibm.com \
    --cc=usama.arif@linux.dev \
    /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