From: David Laight <david.laight.linux@gmail.com>
To: "Nysal Jan K.A." <nysal@linux.ibm.com>
Cc: 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>,
"Christophe Leroy (CS GROUP)" <chleroy@kernel.org>,
"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 22:20:22 +0000 [thread overview]
Message-ID: <20261201222022.166b43eb@pumpkin> (raw)
In-Reply-To: <20261201170926.39183e2a@pumpkin>
On Tue, 1 Dec 2026 17:09:26 +0000
David Laight <david.laight.linux@gmail.com> wrote:
> On Tue, 1 Dec 2026 14:58:31 +0000
> David Laight <david.laight.linux@gmail.com> wrote:
>
> > On Tue, 1 Dec 2026 14:48:04 +0000
> > David Laight <david.laight.linux@gmail.com> wrote:
> >
> > > On Tue, 1 Dec 2026 19:22:06 +0530
> > > "Nysal Jan K.A." <nysal@linux.ibm.com> wrote:
> > >
> > > > On Thu, Nov 26, 2026 at 11:27:14AM +0000, David Laight wrote:
> > > > > On Wed, 25 Nov 2026 23:10:22 +0530
> > > > > "Nysal Jan K.A." <nysal@linux.ibm.com> wrote:
> > > > >
> > > > > >
> > > > > > +#ifdef CONFIG_HAVE_CMPXCHG_DOUBLE
> > > > > > +union __u128_halves {
> > > > > > + u128 full __mode(PTI);
> > > > > > + struct {
> > > > > > + u64 low, high;
> > > > >
> > > > > There seems to be a mismatch between the register order that PTI
> > > > > assigns and the one that C uses.
> > > > > It might be less confusing to swap low and high in the above
> > > > > and only use 'full' (probably renamed) in the asm statement.
> > > > >
> > > >
> > > > I can swap high and low here to indicate that it maps to the big-endian
> > > > register layout if that makes it less confusing.
> > >
> > > But AFICT it doesn't (if I believe the AI from google for "ppc64le lq word order".
> > > The high bits (from EA + 8) end up in the even register and the low bits
> > > (from EA) in the odd register.
> >
> > But that doesn't match a test I just did on godbolt.
> > If the order is backwards then you do need to swap low and high
> > and only use 'full' (aka full_partially_be) for the asm constraint.
>
> I'm getting more confused. I don't have a cpu to test this on either.
> The above AI says you can do (reg + reg), but godbolt's gcc 16.2.0
> will only assemble (reg + const*16).
> See https://www.godbolt.org/z/KParbvPa7
> (and fiddle with what is assigned to [v])
>
> I also found this comment in a patch to qemu:
> + /* Note that the low part is always in RD+1, even in LE mode. */
> + lo = cpu_gpr[rd + 1];
> + hi = cpu_gpr[rd];
>
> So it might be that gcc is actually getting it wrong.
> Needs testing on a physical cpu.
Answering myself again...
It might 'just' be that the resister pair assignments that gcc uses
for __int128 are the opposite way around the those the cpu uses for
lq/stq instructions.
That means that you can't use lq/stq to read/write an __int128
from memory.
The asm code is all consistent L[x] accesses 'reg+1' to get the low
64 bits (ie the LE mode the cpu is inverting A3 as well as inverting
the lower address bits).
So I think you need something like:
/* gcc assigns the registers in the opposite order to the hardware */
union reg_pair {
u128 reg_pair __mode(PTI);
struct { u64 high, low; };
};
and then only access high/low from C and reg_pair from the asm constraint.
(It might need a bigger comment...)
David
>
> David
>
> >
> > David
> >
> > >
> > > I suspect (as in the other email I just sent) that when you do:
> > > u128 x;
> > >
> > > asm(... : "=r" (x))
> > > you are getting the register that holds the low bits of 'x'.
> > > But you want the one containing the high bits so that you can use L[]
> > > to reference the low bits.
> > > The code might appear to work because on a function call the lower
> > > register contains the low bits; so, if the register isn't reused,
> > > L[x] will be the high bits.
> > >
> > > You might be able to tell which registers gcc thinks are 'live'
> > > by adding one to the clobber list.
> > > You can't add one that is in use; so it should force an alternative
> > > register allocation.
> > >
> > > David
> > >
> > >
> > >
> >
>
next prev parent reply other threads:[~2026-12-01 22:20 UTC|newest]
Thread overview: 21+ 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 [this message]
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-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=20261201222022.166b43eb@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.