~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/lib/nmi_backtrace.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 /*
  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>
 20 #include <linux/cpu.h>
 21 #include <linux/sched/debug.h>
 22 
 23 #ifdef arch_trigger_cpumask_backtrace
 24 /* For reliability, we're prepared to waste bits here. */
 25 static DECLARE_BITMAP(backtrace_mask, NR_CPUS) __read_mostly;
 26 
 27 /* "in progress" flag of arch_trigger_cpumask_backtrace */
 28 static unsigned long backtrace_flag;
 29 
 30 /*
 31  * When raise() is called it will be passed a pointer to the
 32  * 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  */
 36 void nmi_trigger_cpumask_backtrace(const cpumask_t *mask,
 37                                    int exclude_cpu,
 38                                    void (*raise)(cpumask_t *mask))
 39 {
 40         int i, this_cpu = get_cpu();
 41 
 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 
 51         cpumask_copy(to_cpumask(backtrace_mask), mask);
 52         if (exclude_cpu != -1)
 53                 cpumask_clear_cpu(exclude_cpu, to_cpumask(backtrace_mask));
 54 
 55         /*
 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 
 64         if (!cpumask_empty(to_cpumask(backtrace_mask))) {
 65                 pr_info("Sending NMI from CPU %d to CPUs %*pbl:\n",
 66                         this_cpu, nr_cpumask_bits, to_cpumask(backtrace_mask));
 67                 nmi_backtrace_stall_snap(to_cpumask(backtrace_mask));
 68                 raise(to_cpumask(backtrace_mask));
 69         }
 70 
 71         /* Wait for up to 10 seconds for all CPUs to do the backtrace */
 72         for (i = 0; i < 10 * 1000; i++) {
 73                 if (cpumask_empty(to_cpumask(backtrace_mask)))
 74                         break;
 75                 mdelay(1);
 76                 touch_softlockup_watchdog();
 77         }
 78         nmi_backtrace_stall_check(to_cpumask(backtrace_mask));
 79 
 80         /*
 81          * Force flush any remote buffers that might be stuck in IRQ context
 82          * and therefore could not run their irq_work.
 83          */
 84         printk_trigger_flush();
 85 
 86         clear_bit_unlock(0, &backtrace_flag);
 87         put_cpu();
 88 }
 89 
 90 // Dump stacks even for idle CPUs.
 91 static bool backtrace_idle;
 92 module_param(backtrace_idle, bool, 0644);
 93 
 94 bool nmi_cpu_backtrace(struct pt_regs *regs)
 95 {
 96         int cpu = smp_processor_id();
 97         unsigned long flags;
 98 
 99         if (cpumask_test_cpu(cpu, to_cpumask(backtrace_mask))) {
100                 /*
101                  * Allow nested NMI backtraces while serializing
102                  * against other CPUs.
103                  */
104                 printk_cpu_sync_get_irqsave(flags);
105                 if (!READ_ONCE(backtrace_idle) && regs && cpu_in_idle(instruction_pointer(regs))) {
106                         pr_warn("NMI backtrace for cpu %d skipped: idling at %pS\n",
107                                 cpu, (void *)instruction_pointer(regs));
108                 } else {
109                         pr_warn("NMI backtrace for cpu %d\n", cpu);
110                         if (regs)
111                                 show_regs(regs);
112                         else
113                                 dump_stack();
114                 }
115                 printk_cpu_sync_put_irqrestore(flags);
116                 cpumask_clear_cpu(cpu, to_cpumask(backtrace_mask));
117                 return true;
118         }
119 
120         return false;
121 }
122 NOKPROBE_SYMBOL(nmi_cpu_backtrace);
123 #endif
124 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php