1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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>
22
23#ifdef arch_trigger_cpumask_backtrace
24
25static DECLARE_BITMAP(backtrace_mask, NR_CPUS) __read_mostly;
26
27
28static unsigned long backtrace_flag;
29
30
31
32
33
34
35
36void nmi_trigger_cpumask_backtrace(const cpumask_t *mask,
37 bool exclude_self,
38 void (*raise)(cpumask_t *mask))
39{
40 int i, this_cpu = get_cpu();
41
42 if (test_and_set_bit(0, &backtrace_flag)) {
43
44
45
46
47 put_cpu();
48 return;
49 }
50
51 cpumask_copy(to_cpumask(backtrace_mask), mask);
52 if (exclude_self)
53 cpumask_clear_cpu(this_cpu, to_cpumask(backtrace_mask));
54
55
56
57
58
59
60
61 if (cpumask_test_cpu(this_cpu, to_cpumask(backtrace_mask)))
62 nmi_cpu_backtrace(NULL);
63
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));
68 }
69
70
71 for (i = 0; i < 10 * 1000; i++) {
72 if (cpumask_empty(to_cpumask(backtrace_mask)))
73 break;
74 mdelay(1);
75 touch_softlockup_watchdog();
76 }
77
78
79
80
81
82 printk_safe_flush();
83
84 clear_bit_unlock(0, &backtrace_flag);
85 put_cpu();
86}
87
88bool nmi_cpu_backtrace(struct pt_regs *regs)
89{
90 int cpu = smp_processor_id();
91
92 if (cpumask_test_cpu(cpu, to_cpumask(backtrace_mask))) {
93 if (regs && cpu_in_idle(instruction_pointer(regs))) {
94 pr_warn("NMI backtrace for cpu %d skipped: idling at %pS\n",
95 cpu, (void *)instruction_pointer(regs));
96 } else {
97 pr_warn("NMI backtrace for cpu %d\n", cpu);
98 if (regs)
99 show_regs(regs);
100 else
101 dump_stack();
102 }
103 cpumask_clear_cpu(cpu, to_cpumask(backtrace_mask));
104 return true;
105 }
106
107 return false;
108}
109NOKPROBE_SYMBOL(nmi_cpu_backtrace);
110#endif
111