Skip to content

Commit 1828688

Browse files
ubizjakliuw
authored andcommitted
x86/hyperv: Use atomic_try_cmpxchg() to micro-optimize hv_nmi_unknown()
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" <[email protected]> Cc: Haiyang Zhang <[email protected]> Cc: Wei Liu <[email protected]> Cc: Dexuan Cui <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Borislav Petkov <[email protected]> Cc: Dave Hansen <[email protected]> Cc: "H. Peter Anvin" <[email protected]> Signed-off-by: Uros Bizjak <[email protected]> Reviewed-by: Michael Kelley <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Wei Liu <[email protected]> Message-ID: <[email protected]>
1 parent 7e8037b commit 1828688

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

arch/x86/kernel/cpu/mshyperv.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,11 +262,14 @@ static uint32_t __init ms_hyperv_platform(void)
262262
static int hv_nmi_unknown(unsigned int val, struct pt_regs *regs)
263263
{
264264
static atomic_t nmi_cpu = ATOMIC_INIT(-1);
265+
unsigned int old_cpu, this_cpu;
265266

266267
if (!unknown_nmi_panic)
267268
return NMI_DONE;
268269

269-
if (atomic_cmpxchg(&nmi_cpu, -1, raw_smp_processor_id()) != -1)
270+
old_cpu = -1;
271+
this_cpu = raw_smp_processor_id();
272+
if (!atomic_try_cmpxchg(&nmi_cpu, &old_cpu, this_cpu))
270273
return NMI_HANDLED;
271274

272275
return NMI_DONE;

0 commit comments

Comments
 (0)