nmi_backtrace.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * NMI backtrace support
  3. *
  4. * Gratuitously copied from arch/x86/kernel/apic/hw_nmi.c by Russell King,
  5. * with the following header:
  6. *
  7. * HW NMI watchdog support
  8. *
  9. * started by Don Zickus, Copyright (C) 2010 Red Hat, Inc.
  10. *
  11. * Arch specific calls to support NMI watchdog
  12. *
  13. * Bits copied from original nmi.c file
  14. */
  15. #include <linux/cpumask.h>
  16. #include <linux/delay.h>
  17. #include <linux/kprobes.h>
  18. #include <linux/nmi.h>
  19. #include <linux/cpu.h>
  20. #ifdef arch_trigger_cpumask_backtrace
  21. /* For reliability, we're prepared to waste bits here. */
  22. static DECLARE_BITMAP(backtrace_mask, NR_CPUS) __read_mostly;
  23. /* "in progress" flag of arch_trigger_cpumask_backtrace */
  24. static unsigned long backtrace_flag;
  25. /*
  26. * When raise() is called it will be passed a pointer to the
  27. * backtrace_mask. Architectures that call nmi_cpu_backtrace()
  28. * directly from their raise() functions may rely on the mask
  29. * they are passed being updated as a side effect of this call.
  30. */
  31. void nmi_trigger_cpumask_backtrace(const cpumask_t *mask,
  32. bool exclude_self,
  33. void (*raise)(cpumask_t *mask))
  34. {
  35. int i, this_cpu = get_cpu();
  36. if (test_and_set_bit(0, &backtrace_flag)) {
  37. /*
  38. * If there is already a trigger_all_cpu_backtrace() in progress
  39. * (backtrace_flag == 1), don't output double cpu dump infos.
  40. */
  41. put_cpu();
  42. return;
  43. }
  44. cpumask_copy(to_cpumask(backtrace_mask), mask);
  45. if (exclude_self)
  46. cpumask_clear_cpu(this_cpu, to_cpumask(backtrace_mask));
  47. /*
  48. * Don't try to send an NMI to this cpu; it may work on some
  49. * architectures, but on others it may not, and we'll get
  50. * information at least as useful just by doing a dump_stack() here.
  51. * Note that nmi_cpu_backtrace(NULL) will clear the cpu bit.
  52. */
  53. if (cpumask_test_cpu(this_cpu, to_cpumask(backtrace_mask)))
  54. nmi_cpu_backtrace(NULL);
  55. if (!cpumask_empty(to_cpumask(backtrace_mask))) {
  56. pr_info("Sending NMI from CPU %d to CPUs %*pbl:\n",
  57. this_cpu, nr_cpumask_bits, to_cpumask(backtrace_mask));
  58. raise(to_cpumask(backtrace_mask));
  59. }
  60. /* Wait for up to 10 seconds for all CPUs to do the backtrace */
  61. for (i = 0; i < 10 * 1000; i++) {
  62. if (cpumask_empty(to_cpumask(backtrace_mask)))
  63. break;
  64. mdelay(1);
  65. touch_softlockup_watchdog();
  66. }
  67. /*
  68. * Force flush any remote buffers that might be stuck in IRQ context
  69. * and therefore could not run their irq_work.
  70. */
  71. printk_nmi_flush();
  72. clear_bit_unlock(0, &backtrace_flag);
  73. put_cpu();
  74. }
  75. bool nmi_cpu_backtrace(struct pt_regs *regs)
  76. {
  77. int cpu = smp_processor_id();
  78. if (cpumask_test_cpu(cpu, to_cpumask(backtrace_mask))) {
  79. if (regs && cpu_in_idle(instruction_pointer(regs))) {
  80. pr_warn("NMI backtrace for cpu %d skipped: idling at pc %#lx\n",
  81. cpu, instruction_pointer(regs));
  82. } else {
  83. pr_warn("NMI backtrace for cpu %d\n", cpu);
  84. if (regs)
  85. show_regs(regs);
  86. else
  87. dump_stack();
  88. }
  89. cpumask_clear_cpu(cpu, to_cpumask(backtrace_mask));
  90. return true;
  91. }
  92. return false;
  93. }
  94. NOKPROBE_SYMBOL(nmi_cpu_backtrace);
  95. #endif