The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: David Laight <david.laight.linux@gmail.com>
To: "Christophe Leroy (CS GROUP)" <chleroy@kernel.org>
Cc: "Nysal Jan K.A." <nysal@linux.ibm.com>,
	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: Thu, 3 Dec 2026 11:28:41 +0000	[thread overview]
Message-ID: <20261203112841.29d4d09b@pumpkin> (raw)
In-Reply-To: <30fbd121-8ada-4619-bb61-08ded9af6268@kernel.org>

On Thu, 3 Dec 2026 10:41:27 +0100
"Christophe Leroy (CS GROUP)" <chleroy@kernel.org> wrote:

> Le 02/12/2026 à 18:52, David Laight a écrit :
> > On Wed, 2 Dec 2026 11:13:24 +0100
> > "Christophe Leroy (CS GROUP)" <chleroy@kernel.org> wrote:
> >   
> >> Le 02/12/2026 à 10:57, David Laight a écrit :  
> >>> On Mon, 30 Nov 2026 18:58:11 +0100
> >>> "Christophe Leroy (CS GROUP)" <chleroy@kernel.org> wrote:
> >>>      
> >>>> Why a memory clobber ? Is it to get a barrier ? Otherwise I see no
> >>>> direct hidden memory modification in the sequence.  
> >>>
> >>> I found something horrid during testing:
> >>> The compiler (gcc) will spill the value of a memory operand to stack.
> >>> So:
> >>> long f(long *p)
> >>> {
> >>>       register long x asm("r3");
> >>>
> >>>       asm( "opcode %[x],%[p]" : [x] "=&r" (x) : [p] "m" (*p));
> >>>       return x;
> >>> }
> >>> compiles to:
> >>> f:
> >>>           ld 9,0(3)
> >>>           std 9,-16(1)
> >>>           ori 2,2,0
> >>>           opcode 3,-16(1)
> >>>           blr  
> >>
> >> What 'opcode' is that ? Because powerpc is a RISC processor, there is no
> >> way ALU operation can operate on something else than registers.  
> > 
> > A memory read would do.
> > I was playing around trying to get the compiler to generate addressing
> > modes that lq doesn't support.
> > The point is that the asm block isn't given a reference to the address
> > it expects.
> > It also means that the size of the variable has to be right.
> > If it can do the same for an output then asm blocks that try to do
> > atomic operations will do them on a copy.
> > 
> > Or consider (with -mno-altivec) this:
> > u128 f(int xx, u128 *p)
> > {
> >      register u128 x asm("r4");
> > 
> >      asm( "lq %[x],%[p]" : [x] "=&r" (x) : [p] "m" (*p));
> >      return x;
> > }
> > generates:
> > f:
> >          ld 9,0(4)
> >          ld 10,8(4)
> >          std 9,-16(1)
> >          std 10,-8(1)
> >          ori 2,2,0
> >          lq 4,-16(1)
> >          mr 3,4
> >          mr 4,5
> >          blr
> > 
> > A little contrived and the byte order is wrong but it is failing to
> > do the expected atomic read of the 128bit value.  
> 
> Looks like the "m" generates the copy.
> When you use "+m" it doesn't.

More likely 'it hasn't been seen to do so'.
If it can spill an input memory argument to stack I suspect the
logic is there to spill an output parameter as well.
If you try really hard I bet you can get it to do the double copy
for "+" as well.

> By the way you should use "+Q" instead of "+m" I think.

For lq/stq you definitely need to use "Q".
Both "Z" and "m" can generate 'reg + reg' which they don't support
but gas doesn't actually reject but treats the one register number
as an immediate offset - which only fails because it didn't use r16.

> 
> With:
> 
> u128 f(int xx, u128 *p)
> {
>      register u128 x asm("r4");
> 
>      asm volatile("lq %[x],%[mem]" : [x] "=&r" (x), [mem] "+Q" (*p));
> 
>      return x;
> }
> 
> I get
> 
> 0000000000005490 <f>:
>      5490:       78 23 89 7c     mr      r9,r4
>      5494:       00 00 89 e0     lq      r4,0(r9)
>      5498:       78 23 83 7c     mr      r3,r4
>      549c:       78 2b a4 7c     mr      r4,r5
>      54a0:       20 00 80 4e     blr

Indeed - I tried all sorts of combinations.
But there isn't anything there that actually stops the value being
spilled.
So constrain the register allocation further and it might still
spill the value.

This also means that code that uses a 'fake' "m" argument to indicate
that some specific memory is accessed by an asm block is fundamentally
broken because that "m" argument can refer to a copy.

If the asm is in a real function it is likely to have been checked once.
But if inlined it will depend on the call site - and 99% could be ok.

David

> 
> Christophe


  reply	other threads:[~2026-12-03 11:28 UTC|newest]

Thread overview: 27+ 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-12-01 17:09           ` David Laight
2026-12-01 22:20             ` 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
2026-12-02  9:57     ` David Laight
2026-12-02 10:13       ` Christophe Leroy (CS GROUP)
2026-12-02 11:01         ` Christophe Leroy (CS GROUP)
2026-12-02 17:52         ` David Laight
2026-12-03  9:41           ` Christophe Leroy (CS GROUP)
2026-12-03 11:28             ` 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=20261203112841.29d4d09b@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