From: David Laight <david.laight.linux@gmail.com>
To: Uros Bizjak <ubizjak@gmail.com>
Cc: x86@kernel.org, linux-kernel@vger.kernel.org,
"Peter Zijlstra (Intel)" <peterz@infradead.org>,
Thomas Gleixner <tglx@kernel.org>, Ingo Molnar <mingo@kernel.org>,
Borislav Petkov <bp@alien8.de>,
Dave Hansen <dave.hansen@linux.intel.com>,
"H. Peter Anvin" <hpa@zytor.com>
Subject: Re: [PATCH] x86/asm: Switch clflush alternatives to use %a address operand modifier
Date: Thu, 19 Mar 2026 11:21:31 +0000 [thread overview]
Message-ID: <20260319112131.0ba42dc7@pumpkin> (raw)
In-Reply-To: <CAFULd4a36axjza=cbKKn0bQqEStLaGFynaZ5CvEjxvYC_XN+sg@mail.gmail.com>
On Thu, 19 Mar 2026 11:45:59 +0100
Uros Bizjak <ubizjak@gmail.com> wrote:
> On Thu, Mar 19, 2026 at 11:20 AM David Laight
> <david.laight.linux@gmail.com> wrote:
> >
> > On Wed, 18 Mar 2026 16:45:28 +0100
> > Uros Bizjak <ubizjak@gmail.com> wrote:
> >
> > > On Wed, Mar 18, 2026 at 4:03 PM David Laight
> > > <david.laight.linux@gmail.com> wrote:
> > > >
> > > > On Wed, 18 Mar 2026 10:08:11 +0100
> > > > Uros Bizjak <ubizjak@gmail.com> wrote:
> > > >
> > > > > The inline asm used with alternative_input() specifies the address
> > > > > operand for clflush with the "a" input operand constraint and
> > > > > explicit "(%[addr])" dereference:
> > > > >
> > > > > "clflush (%[addr])", [addr] "a" (addr)
> > > > >
> > > > > This forces the pointer into %rax and manually encodes the memory
> > > > > operand in the template. Instead, use the %a address operand
> > > > > modifier and relax the constraint from "a" to "r":
> > > > >
> > > > > "clflush %a[addr]", [addr] "r" (addr)
> > > > >
> > > > > This lets the compiler choose the register while generating the
> > > > > correct addressing mode.
> > > >
> > > > Aren't these two independent changes?
> > >
> > > I was hoping I can put a trivial "a" -> "r" change under the "also
> > > ..." change. OTOH, let's change the summary to "x86/asm: Improve
> > > clflush alternatives assembly", that will also handle your proposed
> > > addition of "memory" clobber.
> > >
> > > > %a saves you having to know how to write the memory reference for the
> > > > architecture - so is the same as (%[addr]) (assuming att syntax).
> > > > I think the assembler handles the one 'odd' case of (%rbp).
> > >
> > > Yes, it does, and also fixes another 'odd' case of (%r13).
> > >
> > > > Was there ever a reason for using "a" rather than "r" - it seems an
> > > > unusual choice.
> > >
> > > Probably just an oversight due to a follow-up __monitor() that wants
> > > its operand in %rax.
> >
> > Actually gcc can be quite bad are reverse tracking register requirements.
>
> This must be a very old GCC as I'm not aware of this deficiency.
>
> --cut here--
> void foo (int a)
> {
> asm volatile ("# 1" : : "r" (a));
> asm volatile ("# 2" : : "a" (a));
> }
>
> void bar (int a)
> {
> asm volatile ("# 1" : : "a" (a));
> asm volatile ("# 2" : : "a" (a));
> }
> --cut here--
>
> foo:
> movl %edi, %eax
> # 1
> # 2
> ret
>
> bar:
> movl %edi, %eax
> # 1
> # 2
> ret
>
> Do you perhaps have a testcase to illustrate your claim?
If you look at enough gcc output you'll see places where there are register
moves that look like they could be removed by adjusting the register
assignments.
I'm pretty sure Linus has commented about that as well.
Whether it can happen in this trivial case is another matter.
Oh - I can't see anything in the gcc 15.2 doc that says that the order
of 'asm volatile' statements can't get swapped.
I'm also pretty sure that some older (possibly very much older) versions
definitely would swap them over.
There might have been a post from someone saying that 'it doesn't do that
any more', but it isn't documented.
David
>
> > So forcing 'addr' into %rax for the cflush might actually remove
> > a register move before the monitor.
> > Indeed, were it to pick a different register there will always be a
> > extra register move.
> > If the value is in a different register (eg from a function call)
> > then you'll move the register move instruction - but there'll still
> > be one.
> >
> > So I suspect this change can never improve the code.
>
> Of course, there will always be a register move in the above case, but
> please look at [1].
>
> [1] https://claude.ai/share/cf559f66-dfcf-451a-8260-6f687aead052
>
> Uros.
next prev parent reply other threads:[~2026-03-19 11:21 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-18 9:08 [PATCH] x86/asm: Switch clflush alternatives to use %a address operand modifier Uros Bizjak
2026-03-18 9:17 ` Uros Bizjak
2026-03-18 15:03 ` David Laight
2026-03-18 15:45 ` Uros Bizjak
2026-03-19 10:20 ` David Laight
2026-03-19 10:45 ` Uros Bizjak
2026-03-19 11:21 ` David Laight [this message]
2026-03-19 12:28 ` Uros Bizjak
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=20260319112131.0ba42dc7@pumpkin \
--to=david.laight.linux@gmail.com \
--cc=bp@alien8.de \
--cc=dave.hansen@linux.intel.com \
--cc=hpa@zytor.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@kernel.org \
--cc=peterz@infradead.org \
--cc=tglx@kernel.org \
--cc=ubizjak@gmail.com \
--cc=x86@kernel.org \
/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