blob: 75554754eadfeac67e7ab160ad8e6aa45b2392ce [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>
Chris Metcalf6727ad92016-10-07 17:02:55 -070019#include <linux/cpu.h>
Russell Kingb2c0b2c2014-09-03 23:57:13 +010020
Chris Metcalf9a01c3e2016-10-07 17:02:45 -070021#ifdef arch_trigger_cpumask_backtrace
Russell Kingb2c0b2c2014-09-03 23:57:13 +010022/* For reliability, we're prepared to waste bits here. */
23static DECLARE_BITMAP(backtrace_mask, NR_CPUS) __read_mostly;
Russell Kingb2c0b2c2014-09-03 23:57:13 +010024
Chris Metcalf9a01c3e2016-10-07 17:02:45 -070025/* "in progress" flag of arch_trigger_cpumask_backtrace */
Russell Kingb2c0b2c2014-09-03 23:57:13 +010026static unsigned long backtrace_flag;
27
Daniel Thompson07683302015-09-22 17:12:10 +010028/*
Chris Metcalf9a01c3e2016-10-07 17:02:45 -070029 * When raise() is called it will be passed a pointer to the
Daniel Thompson07683302015-09-22 17:12:10 +010030 * backtrace_mask. Architectures that call nmi_cpu_backtrace()
31 * directly from their raise() functions may rely on the mask
32 * they are passed being updated as a side effect of this call.
33 */
Chris Metcalf9a01c3e2016-10-07 17:02:45 -070034void nmi_trigger_cpumask_backtrace(const cpumask_t *mask,
35 bool exclude_self,
Russell Kingb2c0b2c2014-09-03 23:57:13 +010036 void (*raise)(cpumask_t *mask))
37{
Petr Mladek42a0bb32016-05-20 17:00:33 -070038 int i, this_cpu = get_cpu();
Russell Kingb2c0b2c2014-09-03 23:57:13 +010039
40 if (test_and_set_bit(0, &backtrace_flag)) {
41 /*
42 * If there is already a trigger_all_cpu_backtrace() in progress
43 * (backtrace_flag == 1), don't output double cpu dump infos.
44 */
45 put_cpu();
46 return;
47 }
48
Chris Metcalf9a01c3e2016-10-07 17:02:45 -070049 cpumask_copy(to_cpumask(backtrace_mask), mask);
50 if (exclude_self)
Russell Kingb2c0b2c2014-09-03 23:57:13 +010051 cpumask_clear_cpu(this_cpu, to_cpumask(backtrace_mask));
52
Chris Metcalf67766482016-10-07 17:02:49 -070053 /*
54 * Don't try to send an NMI to this cpu; it may work on some
55 * architectures, but on others it may not, and we'll get
56 * information at least as useful just by doing a dump_stack() here.
57 * Note that nmi_cpu_backtrace(NULL) will clear the cpu bit.
58 */
59 if (cpumask_test_cpu(this_cpu, to_cpumask(backtrace_mask)))
60 nmi_cpu_backtrace(NULL);
61
Russell Kingb2c0b2c2014-09-03 23:57:13 +010062 if (!cpumask_empty(to_cpumask(backtrace_mask))) {
Chris Metcalf9a01c3e2016-10-07 17:02:45 -070063 pr_info("Sending NMI from CPU %d to CPUs %*pbl:\n",
64 this_cpu, nr_cpumask_bits, to_cpumask(backtrace_mask));
Russell Kingb2c0b2c2014-09-03 23:57:13 +010065 raise(to_cpumask(backtrace_mask));
66 }
67
68 /* Wait for up to 10 seconds for all CPUs to do the backtrace */
69 for (i = 0; i < 10 * 1000; i++) {
70 if (cpumask_empty(to_cpumask(backtrace_mask)))
71 break;
72 mdelay(1);
73 touch_softlockup_watchdog();
74 }
75
76 /*
Petr Mladek42a0bb32016-05-20 17:00:33 -070077 * Force flush any remote buffers that might be stuck in IRQ context
78 * and therefore could not run their irq_work.
Russell Kingb2c0b2c2014-09-03 23:57:13 +010079 */
Petr Mladek42a0bb32016-05-20 17:00:33 -070080 printk_nmi_flush();
Russell Kingb2c0b2c2014-09-03 23:57:13 +010081
Petr Mladek42a0bb32016-05-20 17:00:33 -070082 clear_bit_unlock(0, &backtrace_flag);
Russell Kingb2c0b2c2014-09-03 23:57:13 +010083 put_cpu();
84}
85
Russell Kingb2c0b2c2014-09-03 23:57:13 +010086bool nmi_cpu_backtrace(struct pt_regs *regs)
87{
88 int cpu = smp_processor_id();
89
90 if (cpumask_test_cpu(cpu, to_cpumask(backtrace_mask))) {
Chris Metcalf6727ad92016-10-07 17:02:55 -070091 if (regs && cpu_in_idle(instruction_pointer(regs))) {
92 pr_warn("NMI backtrace for cpu %d skipped: idling at pc %#lx\n",
93 cpu, instruction_pointer(regs));
94 } else {
95 pr_warn("NMI backtrace for cpu %d\n", cpu);
96 if (regs)
97 show_regs(regs);
98 else
99 dump_stack();
100 }
Russell Kingb2c0b2c2014-09-03 23:57:13 +0100101 cpumask_clear_cpu(cpu, to_cpumask(backtrace_mask));
102 return true;
103 }
104
105 return false;
106}
107NOKPROBE_SYMBOL(nmi_cpu_backtrace);
108#endif