Skip to content

Commit 9e9d673

Browse files
ubizjakIngo Molnar
authored andcommitted
x86/smp: Use atomic_try_cmpxchg in native_stop_other_cpus()
Use atomic_try_cmpxchg() instead of atomic_cmpxchg(*ptr, old, new) == old. X86 CMPXCHG instruction returns success in ZF flag, so this change saves a compare after the CMPXCHG. Tested by building a native Fedora-38 kernel and rebooting a 12-way SMP system using "shutdown -r" command some 100 times. No functional change intended. Signed-off-by: Uros Bizjak <[email protected]> Signed-off-by: Ingo Molnar <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 9d1c8f2 commit 9e9d673

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

arch/x86/kernel/smp.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,16 @@ static int register_stop_handler(void)
148148

149149
static void native_stop_other_cpus(int wait)
150150
{
151-
unsigned int this_cpu;
151+
unsigned int old_cpu, this_cpu;
152152
unsigned long flags, timeout;
153153

154154
if (reboot_force)
155155
return;
156156

157157
/* Only proceed if this is the first CPU to reach this code */
158+
old_cpu = -1;
158159
this_cpu = smp_processor_id();
159-
if (atomic_cmpxchg(&stopping_cpu, -1, this_cpu) != -1)
160+
if (!atomic_try_cmpxchg(&stopping_cpu, &old_cpu, this_cpu))
160161
return;
161162

162163
/* For kexec, ensure that offline CPUs are out of MWAIT and in HLT */

0 commit comments

Comments
 (0)