0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 #include <linux/cpumask.h>
0017 #include <linux/delay.h>
0018 #include <linux/kprobes.h>
0019 #include <linux/nmi.h>
0020 #include <linux/cpu.h>
0021 #include <linux/sched/debug.h>
0022
0023 #ifdef arch_trigger_cpumask_backtrace
0024
0025 static DECLARE_BITMAP(backtrace_mask, NR_CPUS) __read_mostly;
0026
0027
0028 static unsigned long backtrace_flag;
0029
0030
0031
0032
0033
0034
0035
0036 void nmi_trigger_cpumask_backtrace(const cpumask_t *mask,
0037 bool exclude_self,
0038 void (*raise)(cpumask_t *mask))
0039 {
0040 int i, this_cpu = get_cpu();
0041
0042 if (test_and_set_bit(0, &backtrace_flag)) {
0043
0044
0045
0046
0047 put_cpu();
0048 return;
0049 }
0050
0051 cpumask_copy(to_cpumask(backtrace_mask), mask);
0052 if (exclude_self)
0053 cpumask_clear_cpu(this_cpu, to_cpumask(backtrace_mask));
0054
0055
0056
0057
0058
0059
0060
0061 if (cpumask_test_cpu(this_cpu, to_cpumask(backtrace_mask)))
0062 nmi_cpu_backtrace(NULL);
0063
0064 if (!cpumask_empty(to_cpumask(backtrace_mask))) {
0065 pr_info("Sending NMI from CPU %d to CPUs %*pbl:\n",
0066 this_cpu, nr_cpumask_bits, to_cpumask(backtrace_mask));
0067 raise(to_cpumask(backtrace_mask));
0068 }
0069
0070
0071 for (i = 0; i < 10 * 1000; i++) {
0072 if (cpumask_empty(to_cpumask(backtrace_mask)))
0073 break;
0074 mdelay(1);
0075 touch_softlockup_watchdog();
0076 }
0077
0078
0079
0080
0081
0082 printk_trigger_flush();
0083
0084 clear_bit_unlock(0, &backtrace_flag);
0085 put_cpu();
0086 }
0087
0088
0089 static bool backtrace_idle;
0090 module_param(backtrace_idle, bool, 0644);
0091
0092 bool nmi_cpu_backtrace(struct pt_regs *regs)
0093 {
0094 int cpu = smp_processor_id();
0095 unsigned long flags;
0096
0097 if (cpumask_test_cpu(cpu, to_cpumask(backtrace_mask))) {
0098
0099
0100
0101
0102 printk_cpu_sync_get_irqsave(flags);
0103 if (!READ_ONCE(backtrace_idle) && regs && cpu_in_idle(instruction_pointer(regs))) {
0104 pr_warn("NMI backtrace for cpu %d skipped: idling at %pS\n",
0105 cpu, (void *)instruction_pointer(regs));
0106 } else {
0107 pr_warn("NMI backtrace for cpu %d\n", cpu);
0108 if (regs)
0109 show_regs(regs);
0110 else
0111 dump_stack();
0112 }
0113 printk_cpu_sync_put_irqrestore(flags);
0114 cpumask_clear_cpu(cpu, to_cpumask(backtrace_mask));
0115 return true;
0116 }
0117
0118 return false;
0119 }
0120 NOKPROBE_SYMBOL(nmi_cpu_backtrace);
0121 #endif