kexec: use atomic_try_cmpxchg in crash_kexec
authorUros Bizjak <ubizjak@gmail.com>
Tue, 14 Nov 2023 16:12:01 +0000 (17:12 +0100)
committerAndrew Morton <akpm@linux-foundation.org>
Mon, 11 Dec 2023 01:21:33 +0000 (17:21 -0800)
Use atomic_try_cmpxchg instead of cmpxchg (*ptr, old, new) == old in
crash_kexec().  x86 CMPXCHG instruction returns success in ZF flag,
so this change saves a compare after cmpxchg.

No functional change intended.

Link: https://lkml.kernel.org/r/20231114161228.108516-1-ubizjak@gmail.com
Signed-off-by: Uros Bizjak <ubizjak@gmail.com>
Acked-by: Baoquan He <bhe@redhat.com>
Cc: Eric Biederman <ebiederm@xmission.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
kernel/kexec_core.c

index be5642a4ec4906ee262b2e892c89045411b49218..bc4c096ab1f3e41d5375224c5b698559921130c8 100644 (file)
@@ -1063,9 +1063,10 @@ __bpf_kfunc void crash_kexec(struct pt_regs *regs)
         * panic().  Otherwise parallel calls of panic() and crash_kexec()
         * may stop each other.  To exclude them, we use panic_cpu here too.
         */
+       old_cpu = PANIC_CPU_INVALID;
        this_cpu = raw_smp_processor_id();
-       old_cpu = atomic_cmpxchg(&panic_cpu, PANIC_CPU_INVALID, this_cpu);
-       if (old_cpu == PANIC_CPU_INVALID) {
+
+       if (atomic_try_cmpxchg(&panic_cpu, &old_cpu, this_cpu)) {
                /* This is the 1st CPU which comes here, so go ahead. */
                __crash_kexec(regs);