Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  *  NMI backtrace support
0004  *
0005  * Gratuitously copied from arch/x86/kernel/apic/hw_nmi.c by Russell King,
0006  * with the following header:
0007  *
0008  *  HW NMI watchdog support
0009  *
0010  *  started by Don Zickus, Copyright (C) 2010 Red Hat, Inc.
0011  *
0012  *  Arch specific calls to support NMI watchdog
0013  *
0014  *  Bits copied from original nmi.c file
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 /* For reliability, we're prepared to waste bits here. */
0025 static DECLARE_BITMAP(backtrace_mask, NR_CPUS) __read_mostly;
0026 
0027 /* "in progress" flag of arch_trigger_cpumask_backtrace */
0028 static unsigned long backtrace_flag;
0029 
0030 /*
0031  * When raise() is called it will be passed a pointer to the
0032  * backtrace_mask. Architectures that call nmi_cpu_backtrace()
0033  * directly from their raise() functions may rely on the mask
0034  * they are passed being updated as a side effect of this call.
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          * If there is already a trigger_all_cpu_backtrace() in progress
0045          * (backtrace_flag == 1), don't output double cpu dump infos.
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      * Don't try to send an NMI to this cpu; it may work on some
0057      * architectures, but on others it may not, and we'll get
0058      * information at least as useful just by doing a dump_stack() here.
0059      * Note that nmi_cpu_backtrace(NULL) will clear the cpu bit.
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     /* Wait for up to 10 seconds for all CPUs to do the backtrace */
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      * Force flush any remote buffers that might be stuck in IRQ context
0080      * and therefore could not run their irq_work.
0081      */
0082     printk_trigger_flush();
0083 
0084     clear_bit_unlock(0, &backtrace_flag);
0085     put_cpu();
0086 }
0087 
0088 // Dump stacks even for idle CPUs.
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          * Allow nested NMI backtraces while serializing
0100          * against other CPUs.
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