1 // SPDX-License-Identifier: GPL-2.0
3 * NMI backtrace support
5 * Gratuitously copied from arch/x86/kernel/apic/hw_nmi.c by Russell King,
6 * with the following header:
8 * HW NMI watchdog support
10 * started by Don Zickus, Copyright (C) 2010 Red Hat, Inc.
12 * Arch specific calls to support NMI watchdog
14 * Bits copied from original nmi.c file
16 #include <linux/cpumask.h>
17 #include <linux/delay.h>
18 #include <linux/kprobes.h>
19 #include <linux/nmi.h>
20 #include <linux/cpu.h>
21 #include <linux/sched/debug.h>
23 #ifdef arch_trigger_cpumask_backtrace
24 /* For reliability, we're prepared to waste bits here. */
25 static DECLARE_BITMAP(backtrace_mask, NR_CPUS) __read_mostly;
27 /* "in progress" flag of arch_trigger_cpumask_backtrace */
28 static unsigned long backtrace_flag;
31 * When raise() is called it will be passed a pointer to the
32 * backtrace_mask. Architectures that call nmi_cpu_backtrace()
33 * directly from their raise() functions may rely on the mask
34 * they are passed being updated as a side effect of this call.
36 void nmi_trigger_cpumask_backtrace(const cpumask_t *mask,
38 void (*raise)(cpumask_t *mask))
40 int i, this_cpu = get_cpu();
42 if (test_and_set_bit(0, &backtrace_flag)) {
44 * If there is already a trigger_all_cpu_backtrace() in progress
45 * (backtrace_flag == 1), don't output double cpu dump infos.
51 cpumask_copy(to_cpumask(backtrace_mask), mask);
53 cpumask_clear_cpu(this_cpu, to_cpumask(backtrace_mask));
56 * Don't try to send an NMI to this cpu; it may work on some
57 * architectures, but on others it may not, and we'll get
58 * information at least as useful just by doing a dump_stack() here.
59 * Note that nmi_cpu_backtrace(NULL) will clear the cpu bit.
61 if (cpumask_test_cpu(this_cpu, to_cpumask(backtrace_mask)))
62 nmi_cpu_backtrace(NULL);
64 if (!cpumask_empty(to_cpumask(backtrace_mask))) {
65 pr_info("Sending NMI from CPU %d to CPUs %*pbl:\n",
66 this_cpu, nr_cpumask_bits, to_cpumask(backtrace_mask));
67 raise(to_cpumask(backtrace_mask));
70 /* Wait for up to 10 seconds for all CPUs to do the backtrace */
71 for (i = 0; i < 10 * 1000; i++) {
72 if (cpumask_empty(to_cpumask(backtrace_mask)))
75 touch_softlockup_watchdog();
79 * Force flush any remote buffers that might be stuck in IRQ context
80 * and therefore could not run their irq_work.
84 clear_bit_unlock(0, &backtrace_flag);
88 bool nmi_cpu_backtrace(struct pt_regs *regs)
90 static arch_spinlock_t lock = __ARCH_SPIN_LOCK_UNLOCKED;
91 int cpu = smp_processor_id();
93 if (cpumask_test_cpu(cpu, to_cpumask(backtrace_mask))) {
94 arch_spin_lock(&lock);
95 if (regs && cpu_in_idle(instruction_pointer(regs))) {
96 pr_warn("NMI backtrace for cpu %d skipped: idling at pc %#lx\n",
97 cpu, instruction_pointer(regs));
99 pr_warn("NMI backtrace for cpu %d\n", cpu);
105 arch_spin_unlock(&lock);
106 cpumask_clear_cpu(cpu, to_cpumask(backtrace_mask));
112 NOKPROBE_SYMBOL(nmi_cpu_backtrace);