watchdog_hld.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Detect hard lockups on a system
  3. *
  4. * started by Don Zickus, Copyright (C) 2010 Red Hat, Inc.
  5. *
  6. * Note: Most of this code is borrowed heavily from the original softlockup
  7. * detector, so thanks to Ingo for the initial implementation.
  8. * Some chunks also taken from the old x86-specific nmi watchdog code, thanks
  9. * to those contributors as well.
  10. */
  11. #define pr_fmt(fmt) "NMI watchdog: " fmt
  12. #include <linux/nmi.h>
  13. #include <linux/module.h>
  14. #include <linux/sched/debug.h>
  15. #include <asm/irq_regs.h>
  16. #include <linux/perf_event.h>
  17. static DEFINE_PER_CPU(bool, hard_watchdog_warn);
  18. static DEFINE_PER_CPU(bool, watchdog_nmi_touch);
  19. static DEFINE_PER_CPU(struct perf_event *, watchdog_ev);
  20. static unsigned long hardlockup_allcpu_dumped;
  21. void arch_touch_nmi_watchdog(void)
  22. {
  23. /*
  24. * Using __raw here because some code paths have
  25. * preemption enabled. If preemption is enabled
  26. * then interrupts should be enabled too, in which
  27. * case we shouldn't have to worry about the watchdog
  28. * going off.
  29. */
  30. raw_cpu_write(watchdog_nmi_touch, true);
  31. }
  32. EXPORT_SYMBOL(arch_touch_nmi_watchdog);
  33. static struct perf_event_attr wd_hw_attr = {
  34. .type = PERF_TYPE_HARDWARE,
  35. .config = PERF_COUNT_HW_CPU_CYCLES,
  36. .size = sizeof(struct perf_event_attr),
  37. .pinned = 1,
  38. .disabled = 1,
  39. };
  40. /* Callback function for perf event subsystem */
  41. static void watchdog_overflow_callback(struct perf_event *event,
  42. struct perf_sample_data *data,
  43. struct pt_regs *regs)
  44. {
  45. /* Ensure the watchdog never gets throttled */
  46. event->hw.interrupts = 0;
  47. if (atomic_read(&watchdog_park_in_progress) != 0)
  48. return;
  49. if (__this_cpu_read(watchdog_nmi_touch) == true) {
  50. __this_cpu_write(watchdog_nmi_touch, false);
  51. return;
  52. }
  53. /* check for a hardlockup
  54. * This is done by making sure our timer interrupt
  55. * is incrementing. The timer interrupt should have
  56. * fired multiple times before we overflow'd. If it hasn't
  57. * then this is a good indication the cpu is stuck
  58. */
  59. if (is_hardlockup()) {
  60. int this_cpu = smp_processor_id();
  61. /* only print hardlockups once */
  62. if (__this_cpu_read(hard_watchdog_warn) == true)
  63. return;
  64. pr_emerg("Watchdog detected hard LOCKUP on cpu %d", this_cpu);
  65. print_modules();
  66. print_irqtrace_events(current);
  67. if (regs)
  68. show_regs(regs);
  69. else
  70. dump_stack();
  71. /*
  72. * Perform all-CPU dump only once to avoid multiple hardlockups
  73. * generating interleaving traces
  74. */
  75. if (sysctl_hardlockup_all_cpu_backtrace &&
  76. !test_and_set_bit(0, &hardlockup_allcpu_dumped))
  77. trigger_allbutself_cpu_backtrace();
  78. if (hardlockup_panic)
  79. nmi_panic(regs, "Hard LOCKUP");
  80. __this_cpu_write(hard_watchdog_warn, true);
  81. return;
  82. }
  83. __this_cpu_write(hard_watchdog_warn, false);
  84. return;
  85. }
  86. /*
  87. * People like the simple clean cpu node info on boot.
  88. * Reduce the watchdog noise by only printing messages
  89. * that are different from what cpu0 displayed.
  90. */
  91. static unsigned long firstcpu_err;
  92. static atomic_t watchdog_cpus;
  93. int watchdog_nmi_enable(unsigned int cpu)
  94. {
  95. struct perf_event_attr *wd_attr;
  96. struct perf_event *event = per_cpu(watchdog_ev, cpu);
  97. int firstcpu = 0;
  98. /* nothing to do if the hard lockup detector is disabled */
  99. if (!(watchdog_enabled & NMI_WATCHDOG_ENABLED))
  100. goto out;
  101. /* is it already setup and enabled? */
  102. if (event && event->state > PERF_EVENT_STATE_OFF)
  103. goto out;
  104. /* it is setup but not enabled */
  105. if (event != NULL)
  106. goto out_enable;
  107. if (atomic_inc_return(&watchdog_cpus) == 1)
  108. firstcpu = 1;
  109. wd_attr = &wd_hw_attr;
  110. wd_attr->sample_period = hw_nmi_get_sample_period(watchdog_thresh);
  111. /* Try to register using hardware perf events */
  112. event = perf_event_create_kernel_counter(wd_attr, cpu, NULL, watchdog_overflow_callback, NULL);
  113. /* save the first cpu's error for future comparision */
  114. if (firstcpu && IS_ERR(event))
  115. firstcpu_err = PTR_ERR(event);
  116. if (!IS_ERR(event)) {
  117. /* only print for the first cpu initialized */
  118. if (firstcpu || firstcpu_err)
  119. pr_info("enabled on all CPUs, permanently consumes one hw-PMU counter.\n");
  120. goto out_save;
  121. }
  122. /*
  123. * Disable the hard lockup detector if _any_ CPU fails to set up
  124. * set up the hardware perf event. The watchdog() function checks
  125. * the NMI_WATCHDOG_ENABLED bit periodically.
  126. *
  127. * The barriers are for syncing up watchdog_enabled across all the
  128. * cpus, as clear_bit() does not use barriers.
  129. */
  130. smp_mb__before_atomic();
  131. clear_bit(NMI_WATCHDOG_ENABLED_BIT, &watchdog_enabled);
  132. smp_mb__after_atomic();
  133. /* skip displaying the same error again */
  134. if (!firstcpu && (PTR_ERR(event) == firstcpu_err))
  135. return PTR_ERR(event);
  136. /* vary the KERN level based on the returned errno */
  137. if (PTR_ERR(event) == -EOPNOTSUPP)
  138. pr_info("disabled (cpu%i): not supported (no LAPIC?)\n", cpu);
  139. else if (PTR_ERR(event) == -ENOENT)
  140. pr_warn("disabled (cpu%i): hardware events not enabled\n",
  141. cpu);
  142. else
  143. pr_err("disabled (cpu%i): unable to create perf event: %ld\n",
  144. cpu, PTR_ERR(event));
  145. pr_info("Shutting down hard lockup detector on all cpus\n");
  146. return PTR_ERR(event);
  147. /* success path */
  148. out_save:
  149. per_cpu(watchdog_ev, cpu) = event;
  150. out_enable:
  151. perf_event_enable(per_cpu(watchdog_ev, cpu));
  152. out:
  153. return 0;
  154. }
  155. void watchdog_nmi_disable(unsigned int cpu)
  156. {
  157. struct perf_event *event = per_cpu(watchdog_ev, cpu);
  158. if (event) {
  159. perf_event_disable(event);
  160. per_cpu(watchdog_ev, cpu) = NULL;
  161. /* should be in cleanup, but blocks oprofile */
  162. perf_event_release_kernel(event);
  163. /* watchdog_nmi_enable() expects this to be zero initially. */
  164. if (atomic_dec_and_test(&watchdog_cpus))
  165. firstcpu_err = 0;
  166. }
  167. }