xref: /linux/lib/nmi_backtrace.c (revision 59e6295fac26b8e85c1ea859cdd89fa1e47519d7)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  NMI backtrace support
4  *
5  * Gratuitously copied from arch/x86/kernel/apic/hw_nmi.c by Russell King,
6  * with the following header:
7  *
8  *  HW NMI watchdog support
9  *
10  *  started by Don Zickus, Copyright (C) 2010 Red Hat, Inc.
11  *
12  *  Arch specific calls to support NMI watchdog
13  *
14  *  Bits copied from original nmi.c file
15  */
16 #include <linux/cpumask.h>
17 #include <linux/delay.h>
18 #include <linux/kprobes.h>
19 #include <linux/stringify.h>
20 #include <linux/nmi.h>
21 #include <linux/cpu.h>
22 #include <linux/export.h>
23 #include <linux/sched/debug.h>
24 
25 #ifdef arch_trigger_cpumask_backtrace
26 /* For reliability, we're prepared to waste bits here. */
27 static DECLARE_BITMAP(backtrace_mask, NR_CPUS) __read_mostly;
28 
29 /* "in progress" flag of arch_trigger_cpumask_backtrace */
30 static unsigned long backtrace_flag;
31 
32 #define NMI_BT_TIMEOUT_SEC	10
33 
34 /*
35  * When raise() is called it will be passed a pointer to the
36  * backtrace_mask. Architectures that call nmi_cpu_backtrace()
37  * directly from their raise() functions may rely on the mask
38  * they are passed being updated as a side effect of this call.
39  */
40 void nmi_trigger_cpumask_backtrace(const cpumask_t *mask,
41 				   int exclude_cpu,
42 				   void (*raise)(cpumask_t *mask))
43 {
44 	int i, this_cpu = get_cpu();
45 
46 	if (test_and_set_bit(0, &backtrace_flag)) {
47 		/*
48 		 * If there is already a trigger_all_cpu_backtrace() in progress
49 		 * (backtrace_flag == 1), don't output double cpu dump infos.
50 		 */
51 		put_cpu();
52 		return;
53 	}
54 
55 	cpumask_copy(to_cpumask(backtrace_mask), mask);
56 	if (exclude_cpu != -1)
57 		cpumask_clear_cpu(exclude_cpu, to_cpumask(backtrace_mask));
58 
59 	/*
60 	 * Don't try to send an NMI to this cpu; it may work on some
61 	 * architectures, but on others it may not, and we'll get
62 	 * information at least as useful just by doing a dump_stack() here.
63 	 * Note that nmi_cpu_backtrace(NULL) will clear the cpu bit.
64 	 */
65 	if (cpumask_test_cpu(this_cpu, to_cpumask(backtrace_mask)))
66 		nmi_cpu_backtrace(NULL);
67 
68 	if (!cpumask_empty(to_cpumask(backtrace_mask))) {
69 		pr_info("Sending NMI from CPU %d to CPUs %*pbl:\n",
70 			this_cpu, nr_cpumask_bits, to_cpumask(backtrace_mask));
71 		nmi_backtrace_stall_snap(to_cpumask(backtrace_mask));
72 		raise(to_cpumask(backtrace_mask));
73 	}
74 
75 	/* Wait for up to NMI_BT_TIMEOUT_SEC seconds for all CPUs to do the backtrace */
76 	for (i = 0; i < NMI_BT_TIMEOUT_SEC * 1000; i++) {
77 		if (cpumask_empty(to_cpumask(backtrace_mask)))
78 			break;
79 		mdelay(1);
80 		touch_softlockup_watchdog();
81 	}
82 
83 	if (!cpumask_empty(to_cpumask(backtrace_mask))) {
84 		pr_warn("After " __stringify(NMI_BT_TIMEOUT_SEC) " seconds, these CPUS still haven't responded to the NMI: %*pbl\n",
85 			cpumask_pr_args(to_cpumask(backtrace_mask)));
86 
87 		nmi_backtrace_stall_check(to_cpumask(backtrace_mask));
88 	}
89 
90 	/*
91 	 * Force flush any remote buffers that might be stuck in IRQ context
92 	 * and therefore could not run their irq_work.
93 	 */
94 	printk_trigger_flush();
95 
96 	clear_bit_unlock(0, &backtrace_flag);
97 	put_cpu();
98 }
99 
100 // Dump stacks even for idle CPUs.
101 static bool backtrace_idle;
102 module_param(backtrace_idle, bool, 0644);
103 
104 bool nmi_cpu_backtrace(struct pt_regs *regs)
105 {
106 	int cpu = smp_processor_id();
107 	unsigned long flags;
108 
109 	if (cpumask_test_cpu(cpu, to_cpumask(backtrace_mask))) {
110 		/*
111 		 * Allow nested NMI backtraces while serializing
112 		 * against other CPUs.
113 		 */
114 		printk_cpu_sync_get_irqsave(flags);
115 		if (!READ_ONCE(backtrace_idle) && regs && cpu_in_idle(instruction_pointer(regs))) {
116 			pr_warn("NMI backtrace for cpu %d skipped: idling at %pS\n",
117 				cpu, (void *)instruction_pointer(regs));
118 		} else {
119 			pr_warn("NMI backtrace for cpu %d\n", cpu);
120 			if (regs)
121 				show_regs(regs);
122 			else
123 				dump_stack();
124 		}
125 		printk_cpu_sync_put_irqrestore(flags);
126 		cpumask_clear_cpu(cpu, to_cpumask(backtrace_mask));
127 		return true;
128 	}
129 
130 	return false;
131 }
132 NOKPROBE_SYMBOL(nmi_cpu_backtrace);
133 
134 void cpumask_backtrace(const cpumask_t *mask, int exclude_cpu)
135 {
136 	arch_trigger_cpumask_backtrace(mask, exclude_cpu);
137 }
138 EXPORT_SYMBOL_GPL(cpumask_backtrace);
139 #endif
140