From: Wei Liu <wei.liu@kernel.org>
To: Uros Bizjak <ubizjak@gmail.com>
Cc: Michael Kelley <mhklinux@outlook.com>,
"linux-hyperv@vger.kernel.org" <linux-hyperv@vger.kernel.org>,
"x86@kernel.org" <x86@kernel.org>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
"K. Y. Srinivasan" <kys@microsoft.com>,
Haiyang Zhang <haiyangz@microsoft.com>,
Wei Liu <wei.liu@kernel.org>, Dexuan Cui <decui@microsoft.com>,
Thomas Gleixner <tglx@linutronix.de>,
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/hyperv: Use atomic_try_cmpxchg() to micro-optimize hv_nmi_unknown()
Date: Wed, 22 Nov 2023 03:51:57 +0000 [thread overview]
Message-ID: <ZV163ePuUQyyeKUj@liuwe-devbox-debian-v2> (raw)
In-Reply-To: <CAFULd4Z3DZh0SoEyNHfz3=DM2CkDGtNP_f1gVx64NJkzmWp-Pw@mail.gmail.com>
On Wed, Nov 15, 2023 at 09:58:29PM +0100, Uros Bizjak wrote:
> On Wed, Nov 15, 2023 at 6:19 PM Michael Kelley <mhklinux@outlook.com> wrote:
> >
> > From: Uros Bizjak <ubizjak@gmail.com> Sent: Tuesday, November 14, 2023 8:59 AM
> > >
> > > Use atomic_try_cmpxchg() instead of atomic_cmpxchg(*ptr, old, new) == old
> > > in hv_nmi_unknown(). On x86 the CMPXCHG instruction returns success in
> > > the ZF flag, so this change saves a compare after CMPXCHG. The generated
> > > asm code improves from:
> > >
> > > 3e: 65 8b 15 00 00 00 00 mov %gs:0x0(%rip),%edx
> > > 45: b8 ff ff ff ff mov $0xffffffff,%eax
> > > 4a: f0 0f b1 15 00 00 00 lock cmpxchg %edx,0x0(%rip)
> > > 51: 00
> > > 52: 83 f8 ff cmp $0xffffffff,%eax
> > > 55: 0f 95 c0 setne %al
> > >
> > > to:
> > >
> > > 3e: 65 8b 15 00 00 00 00 mov %gs:0x0(%rip),%edx
> > > 45: b8 ff ff ff ff mov $0xffffffff,%eax
> > > 4a: f0 0f b1 15 00 00 00 lock cmpxchg %edx,0x0(%rip)
> > > 51: 00
> > > 52: 0f 95 c0 setne %al
> > >
> > > No functional change intended.
> > >
> > > Cc: "K. Y. Srinivasan" <kys@microsoft.com>
> > > Cc: Haiyang Zhang <haiyangz@microsoft.com>
> > > Cc: Wei Liu <wei.liu@kernel.org>
> > > Cc: Dexuan Cui <decui@microsoft.com>
> > > Cc: Thomas Gleixner <tglx@linutronix.de>
> > > Cc: Ingo Molnar <mingo@kernel.org>
> > > Cc: Borislav Petkov <bp@alien8.de>
> > > Cc: Dave Hansen <dave.hansen@linux.intel.com>
> > > Cc: "H. Peter Anvin" <hpa@zytor.com>
> > > Signed-off-by: Uros Bizjak <ubizjak@gmail.com>
> > > ---
> > > arch/x86/kernel/cpu/mshyperv.c | 5 ++++-
> > > 1 file changed, 4 insertions(+), 1 deletion(-)
> > >
> > > diff --git a/arch/x86/kernel/cpu/mshyperv.c
> > > b/arch/x86/kernel/cpu/mshyperv.c index e6bba12c759c..01fa06dd06b6
> > > 100644
> > > --- a/arch/x86/kernel/cpu/mshyperv.c
> > > +++ b/arch/x86/kernel/cpu/mshyperv.c
> > > @@ -262,11 +262,14 @@ static uint32_t __init ms_hyperv_platform(void)
> > > static int hv_nmi_unknown(unsigned int val, struct pt_regs *regs) {
> > > static atomic_t nmi_cpu = ATOMIC_INIT(-1);
> > > + unsigned int old_cpu, this_cpu;
> > >
> > > if (!unknown_nmi_panic)
> > > return NMI_DONE;
> > >
> > > - if (atomic_cmpxchg(&nmi_cpu, -1, raw_smp_processor_id()) != -1)
> > > + old_cpu = -1;
> > > + this_cpu = raw_smp_processor_id();
> > > + if (!atomic_try_cmpxchg(&nmi_cpu, &old_cpu, this_cpu))
> > > return NMI_HANDLED;
> > >
> > > return NMI_DONE;
> > > --
> > > 2.41.0
> >
> > The change looks correct to me. But is there any motivation other
> > than saving 3 bytes of generated code? This is not a performance
> > sensitive path. And the change adds 3 lines of source code. So
> > I wonder if the change is worth the churn.
>
> Yes, I was trying to make the function more easy to understand and
> similar to nmi_panic() from kernel/panic.c. I had also the idea of
> using CPU_INVALID #define instead of -1, but IMO, the above works as
> well.
>
> > In any case,
> >
> > Reviewed-by: Michael Kelley <mhklinux@outlook.com>
Applied to hyperv-fixes.
Uros, just so you know, DKIM verification failed when I used b4 to apply
this patch. You may want to check your email setup.
For such a simple patch I'm not worried about spoofing authorship, and I
also checked the same email address had sent similar patches before.
Thanks,
Wei.
>
> Thanks,
> Uros.
next prev parent reply other threads:[~2023-11-22 3:52 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-11-14 16:59 [PATCH] x86/hyperv: Use atomic_try_cmpxchg() to micro-optimize hv_nmi_unknown() Uros Bizjak
2023-11-15 17:19 ` Michael Kelley
2023-11-15 20:58 ` Uros Bizjak
2023-11-22 3:51 ` Wei Liu [this message]
2023-11-22 12:31 ` Uros Bizjak
2023-11-22 12:38 ` Uros Bizjak
2023-11-22 16:52 ` Konstantin Ryabitsev
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=ZV163ePuUQyyeKUj@liuwe-devbox-debian-v2 \
--to=wei.liu@kernel.org \
--cc=bp@alien8.de \
--cc=dave.hansen@linux.intel.com \
--cc=decui@microsoft.com \
--cc=haiyangz@microsoft.com \
--cc=hpa@zytor.com \
--cc=kys@microsoft.com \
--cc=linux-hyperv@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mhklinux@outlook.com \
--cc=mingo@kernel.org \
--cc=tglx@linutronix.de \
--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;
as well as URLs for NNTP newsgroup(s).