Back to home page

LXR

 
 

    


0001 /*
0002  *  NMI backtrace support
0003  *
0004  * Gratuitously copied from arch/x86/kernel/apic/hw_nmi.c by Russell King,
0005  * with the following header:
0006  *
0007  *  HW NMI watchdog support
0008  *
0009  *  started by Don Zickus, Copyright (C) 2010 Red Hat, Inc.
0010  *
0011  *  Arch specific calls to support NMI watchdog
0012  *
0013  *  Bits copied from original nmi.c file
0014  */
0015 #include <linux/cpumask.h>
0016 #include <linux/delay.h>
0017 #include <linux/kprobes.h>
0018 #include <linux/nmi.h>
0019 #include <linux/cpu.h>
0020 
0021 #ifdef arch_trigger_cpumask_backtrace
0022 /* For reliability, we're prepared to waste bits here. */
0023 static DECLARE_BITMAP(backtrace_mask, NR_CPUS) __read_mostly;
0024 
0025 /* "in progress" flag of arch_trigger_cpumask_backtrace */
0026 static unsigned long backtrace_flag;
0027 
0028 /*
0029  * When raise() is called it will be passed a pointer to the
0030  * backtrace_mask. Architectures that call nmi_cpu_backtrace()
0031  * directly from their raise() functions may rely on the mask
0032  * they are passed being updated as a side effect of this call.
0033  */
0034 void nmi_trigger_cpumask_backtrace(const cpumask_t *mask,
0035                    bool exclude_self,
0036                    void (*raise)(cpumask_t *mask))
0037 {
0038     int i, this_cpu = get_cpu();
0039 
0040     if (test_and_set_bit(0, &backtrace_flag)) {
0041         /*
0042          * If there is already a trigger_all_cpu_backtrace() in progress
0043          * (backtrace_flag == 1), don't output double cpu dump infos.
0044          */
0045         put_cpu();
0046         return;
0047     }
0048 
0049     cpumask_copy(to_cpumask(backtrace_mask), mask);
0050     if (exclude_self)
0051         cpumask_clear_cpu(this_cpu, to_cpumask(backtrace_mask));
0052 
0053     /*
0054      * Don't try to send an NMI to this cpu; it may work on some
0055      * architectures, but on others it may not, and we'll get
0056      * information at least as useful just by doing a dump_stack() here.
0057      * Note that nmi_cpu_backtrace(NULL) will clear the cpu bit.
0058      */
0059     if (cpumask_test_cpu(this_cpu, to_cpumask(backtrace_mask)))
0060         nmi_cpu_backtrace(NULL);
0061 
0062     if (!cpumask_empty(to_cpumask(backtrace_mask))) {
0063         pr_info("Sending NMI from CPU %d to CPUs %*pbl:\n",
0064             this_cpu, nr_cpumask_bits, to_cpumask(backtrace_mask));
0065         raise(to_cpumask(backtrace_mask));
0066     }
0067 
0068     /* Wait for up to 10 seconds for all CPUs to do the backtrace */
0069     for (i = 0; i < 10 * 1000; i++) {
0070         if (cpumask_empty(to_cpumask(backtrace_mask)))
0071             break;
0072         mdelay(1);
0073         touch_softlockup_watchdog();
0074     }
0075 
0076     /*
0077      * Force flush any remote buffers that might be stuck in IRQ context
0078      * and therefore could not run their irq_work.
0079      */
0080     printk_nmi_flush();
0081 
0082     clear_bit_unlock(0, &backtrace_flag);
0083     put_cpu();
0084 }
0085 
0086 bool nmi_cpu_backtrace(struct pt_regs *regs)
0087 {
0088     int cpu = smp_processor_id();
0089 
0090     if (cpumask_test_cpu(cpu, to_cpumask(backtrace_mask))) {
0091         if (regs && cpu_in_idle(instruction_pointer(regs))) {
0092             pr_warn("NMI backtrace for cpu %d skipped: idling at pc %#lx\n",
0093                 cpu, instruction_pointer(regs));
0094         } else {
0095             pr_warn("NMI backtrace for cpu %d\n", cpu);
0096             if (regs)
0097                 show_regs(regs);
0098             else
0099                 dump_stack();
0100         }
0101         cpumask_clear_cpu(cpu, to_cpumask(backtrace_mask));
0102         return true;
0103     }
0104 
0105     return false;
0106 }
0107 NOKPROBE_SYMBOL(nmi_cpu_backtrace);
0108 #endif