blob: df347e355267b176330aabc746b88787032e88e3 [file] [log] [blame]
Russell Kingb2c0b2c2014-09-03 23:57:13 +01001/*
2 * NMI backtrace support
3 *
4 * Gratuitously copied from arch/x86/kernel/apic/hw_nmi.c by Russell King,
5 * with the following header:
6 *
7 * HW NMI watchdog support
8 *
9 * started by Don Zickus, Copyright (C) 2010 Red Hat, Inc.
10 *
11 * Arch specific calls to support NMI watchdog
12 *
13 * Bits copied from original nmi.c file
14 */
15#include <linux/cpumask.h>
16#include <linux/delay.h>
17#include <linux/kprobes.h>
18#include <linux/nmi.h>
Russell Kingb2c0b2c2014-09-03 23:57:13 +010019
Chris Metcalf9a01c3e2016-10-07 17:02:45 -070020#ifdef arch_trigger_cpumask_backtrace
Russell Kingb2c0b2c2014-09-03 23:57:13 +010021/* For reliability, we're prepared to waste bits here. */
22static DECLARE_BITMAP(backtrace_mask, NR_CPUS) __read_mostly;
Russell Kingb2c0b2c2014-09-03 23:57:13 +010023
Chris Metcalf9a01c3e2016-10-07 17:02:45 -070024/* "in progress" flag of arch_trigger_cpumask_backtrace */
Russell Kingb2c0b2c2014-09-03 23:57:13 +010025static unsigned long backtrace_flag;
26
Daniel Thompson07683302015-09-22 17:12:10 +010027/*
Chris Metcalf9a01c3e2016-10-07 17:02:45 -070028 * When raise() is called it will be passed a pointer to the
Daniel Thompson07683302015-09-22 17:12:10 +010029 * backtrace_mask. Architectures that call nmi_cpu_backtrace()
30 * directly from their raise() functions may rely on the mask
31 * they are passed being updated as a side effect of this call.
32 */
Chris Metcalf9a01c3e2016-10-07 17:02:45 -070033void nmi_trigger_cpumask_backtrace(const cpumask_t *mask,
34 bool exclude_self,
Russell Kingb2c0b2c2014-09-03 23:57:13 +010035 void (*raise)(cpumask_t *mask))
36{
Petr Mladek42a0bb32016-05-20 17:00:33 -070037 int i, this_cpu = get_cpu();
Russell Kingb2c0b2c2014-09-03 23:57:13 +010038
39 if (test_and_set_bit(0, &backtrace_flag)) {
40 /*
41 * If there is already a trigger_all_cpu_backtrace() in progress
42 * (backtrace_flag == 1), don't output double cpu dump infos.
43 */
44 put_cpu();
45 return;
46 }
47
Chris Metcalf9a01c3e2016-10-07 17:02:45 -070048 cpumask_copy(to_cpumask(backtrace_mask), mask);
49 if (exclude_self)
Russell Kingb2c0b2c2014-09-03 23:57:13 +010050 cpumask_clear_cpu(this_cpu, to_cpumask(backtrace_mask));
51
Russell Kingb2c0b2c2014-09-03 23:57:13 +010052 if (!cpumask_empty(to_cpumask(backtrace_mask))) {
Chris Metcalf9a01c3e2016-10-07 17:02:45 -070053 pr_info("Sending NMI from CPU %d to CPUs %*pbl:\n",
54 this_cpu, nr_cpumask_bits, to_cpumask(backtrace_mask));
Russell Kingb2c0b2c2014-09-03 23:57:13 +010055 raise(to_cpumask(backtrace_mask));
56 }
57
58 /* Wait for up to 10 seconds for all CPUs to do the backtrace */
59 for (i = 0; i < 10 * 1000; i++) {
60 if (cpumask_empty(to_cpumask(backtrace_mask)))
61 break;
62 mdelay(1);
63 touch_softlockup_watchdog();
64 }
65
66 /*
Petr Mladek42a0bb32016-05-20 17:00:33 -070067 * Force flush any remote buffers that might be stuck in IRQ context
68 * and therefore could not run their irq_work.
Russell Kingb2c0b2c2014-09-03 23:57:13 +010069 */
Petr Mladek42a0bb32016-05-20 17:00:33 -070070 printk_nmi_flush();
Russell Kingb2c0b2c2014-09-03 23:57:13 +010071
Petr Mladek42a0bb32016-05-20 17:00:33 -070072 clear_bit_unlock(0, &backtrace_flag);
Russell Kingb2c0b2c2014-09-03 23:57:13 +010073 put_cpu();
74}
75
Russell Kingb2c0b2c2014-09-03 23:57:13 +010076bool nmi_cpu_backtrace(struct pt_regs *regs)
77{
78 int cpu = smp_processor_id();
79
80 if (cpumask_test_cpu(cpu, to_cpumask(backtrace_mask))) {
Russell Kingb2c0b2c2014-09-03 23:57:13 +010081 pr_warn("NMI backtrace for cpu %d\n", cpu);
Daniel Thompson07683302015-09-22 17:12:10 +010082 if (regs)
83 show_regs(regs);
84 else
85 dump_stack();
Russell Kingb2c0b2c2014-09-03 23:57:13 +010086 cpumask_clear_cpu(cpu, to_cpumask(backtrace_mask));
87 return true;
88 }
89
90 return false;
91}
92NOKPROBE_SYMBOL(nmi_cpu_backtrace);
93#endif