nmi_backtrace.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. #ifdef arch_trigger_cpumask_backtrace
  23. /* For reliability, we're prepared to waste bits here. */
  24. static DECLARE_BITMAP(backtrace_mask, NR_CPUS) __read_mostly;
  25. /* "in progress" flag of arch_trigger_cpumask_backtrace */
  26. static unsigned long backtrace_flag;
  27. /*
  28. * When raise() is called it will be 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. */
  33. void nmi_trigger_cpumask_backtrace(const cpumask_t *mask,
  34. bool exclude_self,
  35. void (*raise)(cpumask_t *mask))
  36. {
  37. int i, this_cpu = get_cpu();
  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. cpumask_copy(to_cpumask(backtrace_mask), mask);
  47. if (exclude_self)
  48. cpumask_clear_cpu(this_cpu, to_cpumask(backtrace_mask));
  49. /*
  50. * Don't try to send an NMI to this cpu; it may work on some
  51. * architectures, but on others it may not, and we'll get
  52. * information at least as useful just by doing a dump_stack() here.
  53. * Note that nmi_cpu_backtrace(NULL) will clear the cpu bit.
  54. */
  55. if (cpumask_test_cpu(this_cpu, to_cpumask(backtrace_mask)))
  56. nmi_cpu_backtrace(NULL);
  57. if (!cpumask_empty(to_cpumask(backtrace_mask))) {
  58. pr_info("Sending NMI from CPU %d to CPUs %*pbl:\n",
  59. this_cpu, nr_cpumask_bits, to_cpumask(backtrace_mask));
  60. raise(to_cpumask(backtrace_mask));
  61. }
  62. /* Wait for up to 10 seconds for all CPUs to do the backtrace */
  63. for (i = 0; i < 10 * 1000; i++) {
  64. if (cpumask_empty(to_cpumask(backtrace_mask)))
  65. break;
  66. mdelay(1);
  67. touch_softlockup_watchdog();
  68. }
  69. /*
  70. * Force flush any remote buffers that might be stuck in IRQ context
  71. * and therefore could not run their irq_work.
  72. */
  73. printk_trigger_flush();
  74. clear_bit_unlock(0, &backtrace_flag);
  75. put_cpu();
  76. }
  77. // Dump stacks even for idle CPUs.
  78. static bool backtrace_idle;
  79. module_param(backtrace_idle, bool, 0644);
  80. bool nmi_cpu_backtrace(struct pt_regs *regs)
  81. {
  82. int cpu = smp_processor_id();
  83. unsigned long flags;
  84. if (cpumask_test_cpu(cpu, to_cpumask(backtrace_mask))) {
  85. /*
  86. * Allow nested NMI backtraces while serializing
  87. * against other CPUs.
  88. */
  89. printk_cpu_sync_get_irqsave(flags);
  90. if (!READ_ONCE(backtrace_idle) && regs && cpu_in_idle(instruction_pointer(regs))) {
  91. pr_warn("NMI backtrace for cpu %d skipped: idling at %pS\n",
  92. cpu, (void *)instruction_pointer(regs));
  93. } else {
  94. pr_warn("NMI backtrace for cpu %d\n", cpu);
  95. if (regs)
  96. show_regs(regs);
  97. else
  98. dump_stack();
  99. }
  100. printk_cpu_sync_put_irqrestore(flags);
  101. cpumask_clear_cpu(cpu, to_cpumask(backtrace_mask));
  102. return true;
  103. }
  104. return false;
  105. }
  106. NOKPROBE_SYMBOL(nmi_cpu_backtrace);
  107. #endif