]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - lib/nmi_backtrace.c
Merge tag 'xfs-4.13-fixes-5' of git://git.kernel.org/pub/scm/fs/xfs/xfs-linux
[mirror_ubuntu-artful-kernel.git] / lib / nmi_backtrace.c
CommitLineData
b2c0b2cb
RK
1/*
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>
6727ad9e 19#include <linux/cpu.h>
b17b0153 20#include <linux/sched/debug.h>
b2c0b2cb 21
9a01c3ed 22#ifdef arch_trigger_cpumask_backtrace
b2c0b2cb
RK
23/* For reliability, we're prepared to waste bits here. */
24static DECLARE_BITMAP(backtrace_mask, NR_CPUS) __read_mostly;
b2c0b2cb 25
9a01c3ed 26/* "in progress" flag of arch_trigger_cpumask_backtrace */
b2c0b2cb
RK
27static unsigned long backtrace_flag;
28
0768330d 29/*
9a01c3ed 30 * When raise() is called it will be passed a pointer to the
0768330d
DT
31 * backtrace_mask. Architectures that call nmi_cpu_backtrace()
32 * directly from their raise() functions may rely on the mask
33 * they are passed being updated as a side effect of this call.
34 */
9a01c3ed
CM
35void nmi_trigger_cpumask_backtrace(const cpumask_t *mask,
36 bool exclude_self,
b2c0b2cb
RK
37 void (*raise)(cpumask_t *mask))
38{
42a0bb3f 39 int i, this_cpu = get_cpu();
b2c0b2cb
RK
40
41 if (test_and_set_bit(0, &backtrace_flag)) {
42 /*
43 * If there is already a trigger_all_cpu_backtrace() in progress
44 * (backtrace_flag == 1), don't output double cpu dump infos.
45 */
46 put_cpu();
47 return;
48 }
49
9a01c3ed
CM
50 cpumask_copy(to_cpumask(backtrace_mask), mask);
51 if (exclude_self)
b2c0b2cb
RK
52 cpumask_clear_cpu(this_cpu, to_cpumask(backtrace_mask));
53
67766489
CM
54 /*
55 * Don't try to send an NMI to this cpu; it may work on some
56 * architectures, but on others it may not, and we'll get
57 * information at least as useful just by doing a dump_stack() here.
58 * Note that nmi_cpu_backtrace(NULL) will clear the cpu bit.
59 */
60 if (cpumask_test_cpu(this_cpu, to_cpumask(backtrace_mask)))
61 nmi_cpu_backtrace(NULL);
62
b2c0b2cb 63 if (!cpumask_empty(to_cpumask(backtrace_mask))) {
9a01c3ed
CM
64 pr_info("Sending NMI from CPU %d to CPUs %*pbl:\n",
65 this_cpu, nr_cpumask_bits, to_cpumask(backtrace_mask));
b2c0b2cb
RK
66 raise(to_cpumask(backtrace_mask));
67 }
68
69 /* Wait for up to 10 seconds for all CPUs to do the backtrace */
70 for (i = 0; i < 10 * 1000; i++) {
71 if (cpumask_empty(to_cpumask(backtrace_mask)))
72 break;
73 mdelay(1);
74 touch_softlockup_watchdog();
75 }
76
77 /*
42a0bb3f
PM
78 * Force flush any remote buffers that might be stuck in IRQ context
79 * and therefore could not run their irq_work.
b2c0b2cb 80 */
f92bac3b 81 printk_safe_flush();
b2c0b2cb 82
42a0bb3f 83 clear_bit_unlock(0, &backtrace_flag);
b2c0b2cb
RK
84 put_cpu();
85}
86
b2c0b2cb
RK
87bool nmi_cpu_backtrace(struct pt_regs *regs)
88{
719f6a70 89 static arch_spinlock_t lock = __ARCH_SPIN_LOCK_UNLOCKED;
b2c0b2cb
RK
90 int cpu = smp_processor_id();
91
92 if (cpumask_test_cpu(cpu, to_cpumask(backtrace_mask))) {
719f6a70 93 arch_spin_lock(&lock);
6727ad9e
CM
94 if (regs && cpu_in_idle(instruction_pointer(regs))) {
95 pr_warn("NMI backtrace for cpu %d skipped: idling at pc %#lx\n",
96 cpu, instruction_pointer(regs));
97 } else {
98 pr_warn("NMI backtrace for cpu %d\n", cpu);
99 if (regs)
100 show_regs(regs);
101 else
102 dump_stack();
103 }
719f6a70 104 arch_spin_unlock(&lock);
b2c0b2cb
RK
105 cpumask_clear_cpu(cpu, to_cpumask(backtrace_mask));
106 return true;
107 }
108
109 return false;
110}
111NOKPROBE_SYMBOL(nmi_cpu_backtrace);
112#endif