blob: 46e4c749e4eba64da193d0e1f221fa65eee4802a [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Russell Kingb2c0b2c2014-09-03 23:57:13 +01002/*
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/nmi.h>
Chris Metcalf6727ad92016-10-07 17:02:55 -070020#include <linux/cpu.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +010021#include <linux/sched/debug.h>
Russell Kingb2c0b2c2014-09-03 23:57:13 +010022
Chris Metcalf9a01c3e2016-10-07 17:02:45 -070023#ifdef arch_trigger_cpumask_backtrace
Russell Kingb2c0b2c2014-09-03 23:57:13 +010024/* For reliability, we're prepared to waste bits here. */
25static DECLARE_BITMAP(backtrace_mask, NR_CPUS) __read_mostly;
Russell Kingb2c0b2c2014-09-03 23:57:13 +010026
Chris Metcalf9a01c3e2016-10-07 17:02:45 -070027/* "in progress" flag of arch_trigger_cpumask_backtrace */
Russell Kingb2c0b2c2014-09-03 23:57:13 +010028static unsigned long backtrace_flag;
29
Daniel Thompson07683302015-09-22 17:12:10 +010030/*
Chris Metcalf9a01c3e2016-10-07 17:02:45 -070031 * When raise() is called it will be passed a pointer to the
Daniel Thompson07683302015-09-22 17:12:10 +010032 * 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.
35 */
Chris Metcalf9a01c3e2016-10-07 17:02:45 -070036void nmi_trigger_cpumask_backtrace(const cpumask_t *mask,
37 bool exclude_self,
Russell Kingb2c0b2c2014-09-03 23:57:13 +010038 void (*raise)(cpumask_t *mask))
39{
Petr Mladek42a0bb32016-05-20 17:00:33 -070040 int i, this_cpu = get_cpu();
Russell Kingb2c0b2c2014-09-03 23:57:13 +010041
42 if (test_and_set_bit(0, &backtrace_flag)) {
43 /*
44 * If there is already a trigger_all_cpu_backtrace() in progress
45 * (backtrace_flag == 1), don't output double cpu dump infos.
46 */
47 put_cpu();
48 return;
49 }
50
Chris Metcalf9a01c3e2016-10-07 17:02:45 -070051 cpumask_copy(to_cpumask(backtrace_mask), mask);
52 if (exclude_self)
Russell Kingb2c0b2c2014-09-03 23:57:13 +010053 cpumask_clear_cpu(this_cpu, to_cpumask(backtrace_mask));
54
Chris Metcalf67766482016-10-07 17:02:49 -070055 /*
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.
60 */
61 if (cpumask_test_cpu(this_cpu, to_cpumask(backtrace_mask)))
62 nmi_cpu_backtrace(NULL);
63
Russell Kingb2c0b2c2014-09-03 23:57:13 +010064 if (!cpumask_empty(to_cpumask(backtrace_mask))) {
Chris Metcalf9a01c3e2016-10-07 17:02:45 -070065 pr_info("Sending NMI from CPU %d to CPUs %*pbl:\n",
66 this_cpu, nr_cpumask_bits, to_cpumask(backtrace_mask));
Russell Kingb2c0b2c2014-09-03 23:57:13 +010067 raise(to_cpumask(backtrace_mask));
68 }
69
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)))
73 break;
74 mdelay(1);
75 touch_softlockup_watchdog();
76 }
77
78 /*
Petr Mladek42a0bb32016-05-20 17:00:33 -070079 * Force flush any remote buffers that might be stuck in IRQ context
80 * and therefore could not run their irq_work.
Russell Kingb2c0b2c2014-09-03 23:57:13 +010081 */
Sergey Senozhatskyf92bac32016-12-27 23:16:05 +090082 printk_safe_flush();
Russell Kingb2c0b2c2014-09-03 23:57:13 +010083
Petr Mladek42a0bb32016-05-20 17:00:33 -070084 clear_bit_unlock(0, &backtrace_flag);
Russell Kingb2c0b2c2014-09-03 23:57:13 +010085 put_cpu();
86}
87
Russell Kingb2c0b2c2014-09-03 23:57:13 +010088bool nmi_cpu_backtrace(struct pt_regs *regs)
89{
Petr Mladek719f6a72017-04-20 10:52:31 +020090 static arch_spinlock_t lock = __ARCH_SPIN_LOCK_UNLOCKED;
Russell Kingb2c0b2c2014-09-03 23:57:13 +010091 int cpu = smp_processor_id();
92
93 if (cpumask_test_cpu(cpu, to_cpumask(backtrace_mask))) {
Petr Mladek719f6a72017-04-20 10:52:31 +020094 arch_spin_lock(&lock);
Chris Metcalf6727ad92016-10-07 17:02:55 -070095 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));
98 } else {
99 pr_warn("NMI backtrace for cpu %d\n", cpu);
100 if (regs)
101 show_regs(regs);
102 else
103 dump_stack();
104 }
Petr Mladek719f6a72017-04-20 10:52:31 +0200105 arch_spin_unlock(&lock);
Russell Kingb2c0b2c2014-09-03 23:57:13 +0100106 cpumask_clear_cpu(cpu, to_cpumask(backtrace_mask));
107 return true;
108 }
109
110 return false;
111}
112NOKPROBE_SYMBOL(nmi_cpu_backtrace);
113#endif