Russell King | b2c0b2c | 2014-09-03 23:57:13 +0100 | [diff] [blame] | 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> |
Russell King | b2c0b2c | 2014-09-03 23:57:13 +0100 | [diff] [blame] | 19 | |
| 20 | #ifdef arch_trigger_all_cpu_backtrace |
| 21 | /* For reliability, we're prepared to waste bits here. */ |
| 22 | static DECLARE_BITMAP(backtrace_mask, NR_CPUS) __read_mostly; |
Russell King | b2c0b2c | 2014-09-03 23:57:13 +0100 | [diff] [blame] | 23 | |
| 24 | /* "in progress" flag of arch_trigger_all_cpu_backtrace */ |
| 25 | static unsigned long backtrace_flag; |
| 26 | |
Daniel Thompson | 0768330 | 2015-09-22 17:12:10 +0100 | [diff] [blame] | 27 | /* |
| 28 | * When raise() is called it will be is passed a pointer to the |
| 29 | * 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 | */ |
Russell King | b2c0b2c | 2014-09-03 23:57:13 +0100 | [diff] [blame] | 33 | void nmi_trigger_all_cpu_backtrace(bool include_self, |
| 34 | void (*raise)(cpumask_t *mask)) |
| 35 | { |
Petr Mladek | 42a0bb3 | 2016-05-20 17:00:33 -0700 | [diff] [blame] | 36 | int i, this_cpu = get_cpu(); |
Russell King | b2c0b2c | 2014-09-03 23:57:13 +0100 | [diff] [blame] | 37 | |
| 38 | if (test_and_set_bit(0, &backtrace_flag)) { |
| 39 | /* |
| 40 | * If there is already a trigger_all_cpu_backtrace() in progress |
| 41 | * (backtrace_flag == 1), don't output double cpu dump infos. |
| 42 | */ |
| 43 | put_cpu(); |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | cpumask_copy(to_cpumask(backtrace_mask), cpu_online_mask); |
| 48 | if (!include_self) |
| 49 | cpumask_clear_cpu(this_cpu, to_cpumask(backtrace_mask)); |
| 50 | |
Russell King | b2c0b2c | 2014-09-03 23:57:13 +0100 | [diff] [blame] | 51 | if (!cpumask_empty(to_cpumask(backtrace_mask))) { |
| 52 | pr_info("Sending NMI to %s CPUs:\n", |
| 53 | (include_self ? "all" : "other")); |
| 54 | raise(to_cpumask(backtrace_mask)); |
| 55 | } |
| 56 | |
| 57 | /* Wait for up to 10 seconds for all CPUs to do the backtrace */ |
| 58 | for (i = 0; i < 10 * 1000; i++) { |
| 59 | if (cpumask_empty(to_cpumask(backtrace_mask))) |
| 60 | break; |
| 61 | mdelay(1); |
| 62 | touch_softlockup_watchdog(); |
| 63 | } |
| 64 | |
| 65 | /* |
Petr Mladek | 42a0bb3 | 2016-05-20 17:00:33 -0700 | [diff] [blame] | 66 | * Force flush any remote buffers that might be stuck in IRQ context |
| 67 | * and therefore could not run their irq_work. |
Russell King | b2c0b2c | 2014-09-03 23:57:13 +0100 | [diff] [blame] | 68 | */ |
Petr Mladek | 42a0bb3 | 2016-05-20 17:00:33 -0700 | [diff] [blame] | 69 | printk_nmi_flush(); |
Russell King | b2c0b2c | 2014-09-03 23:57:13 +0100 | [diff] [blame] | 70 | |
Petr Mladek | 42a0bb3 | 2016-05-20 17:00:33 -0700 | [diff] [blame] | 71 | clear_bit_unlock(0, &backtrace_flag); |
Russell King | b2c0b2c | 2014-09-03 23:57:13 +0100 | [diff] [blame] | 72 | put_cpu(); |
| 73 | } |
| 74 | |
Russell King | b2c0b2c | 2014-09-03 23:57:13 +0100 | [diff] [blame] | 75 | bool nmi_cpu_backtrace(struct pt_regs *regs) |
| 76 | { |
| 77 | int cpu = smp_processor_id(); |
| 78 | |
| 79 | if (cpumask_test_cpu(cpu, to_cpumask(backtrace_mask))) { |
Russell King | b2c0b2c | 2014-09-03 23:57:13 +0100 | [diff] [blame] | 80 | pr_warn("NMI backtrace for cpu %d\n", cpu); |
Daniel Thompson | 0768330 | 2015-09-22 17:12:10 +0100 | [diff] [blame] | 81 | if (regs) |
| 82 | show_regs(regs); |
| 83 | else |
| 84 | dump_stack(); |
Russell King | b2c0b2c | 2014-09-03 23:57:13 +0100 | [diff] [blame] | 85 | cpumask_clear_cpu(cpu, to_cpumask(backtrace_mask)); |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | return false; |
| 90 | } |
| 91 | NOKPROBE_SYMBOL(nmi_cpu_backtrace); |
| 92 | #endif |