watchdog_hld.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. /* boot commands */
  21. /*
  22. * Should we panic when a soft-lockup or hard-lockup occurs:
  23. */
  24. unsigned int __read_mostly hardlockup_panic =
  25. CONFIG_BOOTPARAM_HARDLOCKUP_PANIC_VALUE;
  26. static unsigned long hardlockup_allcpu_dumped;
  27. /*
  28. * We may not want to enable hard lockup detection by default in all cases,
  29. * for example when running the kernel as a guest on a hypervisor. In these
  30. * cases this function can be called to disable hard lockup detection. This
  31. * function should only be executed once by the boot processor before the
  32. * kernel command line parameters are parsed, because otherwise it is not
  33. * possible to override this in hardlockup_panic_setup().
  34. */
  35. void hardlockup_detector_disable(void)
  36. {
  37. watchdog_enabled &= ~NMI_WATCHDOG_ENABLED;
  38. }
  39. static int __init hardlockup_panic_setup(char *str)
  40. {
  41. if (!strncmp(str, "panic", 5))
  42. hardlockup_panic = 1;
  43. else if (!strncmp(str, "nopanic", 7))
  44. hardlockup_panic = 0;
  45. else if (!strncmp(str, "0", 1))
  46. watchdog_enabled &= ~NMI_WATCHDOG_ENABLED;
  47. else if (!strncmp(str, "1", 1))
  48. watchdog_enabled |= NMI_WATCHDOG_ENABLED;
  49. return 1;
  50. }
  51. __setup("nmi_watchdog=", hardlockup_panic_setup);
  52. void touch_nmi_watchdog(void)
  53. {
  54. /*
  55. * Using __raw here because some code paths have
  56. * preemption enabled. If preemption is enabled
  57. * then interrupts should be enabled too, in which
  58. * case we shouldn't have to worry about the watchdog
  59. * going off.
  60. */
  61. raw_cpu_write(watchdog_nmi_touch, true);
  62. touch_softlockup_watchdog();
  63. }
  64. EXPORT_SYMBOL(touch_nmi_watchdog);
  65. static struct perf_event_attr wd_hw_attr = {
  66. .type = PERF_TYPE_HARDWARE,
  67. .config = PERF_COUNT_HW_CPU_CYCLES,
  68. .size = sizeof(struct perf_event_attr),
  69. .pinned = 1,
  70. .disabled = 1,
  71. };
  72. /* Callback function for perf event subsystem */
  73. static void watchdog_overflow_callback(struct perf_event *event,
  74. struct perf_sample_data *data,
  75. struct pt_regs *regs)
  76. {
  77. /* Ensure the watchdog never gets throttled */
  78. event->hw.interrupts = 0;
  79. if (atomic_read(&watchdog_park_in_progress) != 0)
  80. return;
  81. if (__this_cpu_read(watchdog_nmi_touch) == true) {
  82. __this_cpu_write(watchdog_nmi_touch, false);
  83. return;
  84. }
  85. /* check for a hardlockup
  86. * This is done by making sure our timer interrupt
  87. * is incrementing. The timer interrupt should have
  88. * fired multiple times before we overflow'd. If it hasn't
  89. * then this is a good indication the cpu is stuck
  90. */
  91. if (is_hardlockup()) {
  92. int this_cpu = smp_processor_id();
  93. /* only print hardlockups once */
  94. if (__this_cpu_read(hard_watchdog_warn) == true)
  95. return;
  96. pr_emerg("Watchdog detected hard LOCKUP on cpu %d", this_cpu);
  97. print_modules();
  98. print_irqtrace_events(current);
  99. if (regs)
  100. show_regs(regs);
  101. else
  102. dump_stack();
  103. /*
  104. * Perform all-CPU dump only once to avoid multiple hardlockups
  105. * generating interleaving traces
  106. */
  107. if (sysctl_hardlockup_all_cpu_backtrace &&
  108. !test_and_set_bit(0, &hardlockup_allcpu_dumped))
  109. trigger_allbutself_cpu_backtrace();
  110. if (hardlockup_panic)
  111. nmi_panic(regs, "Hard LOCKUP");
  112. __this_cpu_write(hard_watchdog_warn, true);
  113. return;
  114. }
  115. __this_cpu_write(hard_watchdog_warn, false);
  116. return;
  117. }
  118. /*
  119. * People like the simple clean cpu node info on boot.
  120. * Reduce the watchdog noise by only printing messages
  121. * that are different from what cpu0 displayed.
  122. */
  123. static unsigned long firstcpu_err;
  124. static atomic_t watchdog_cpus;
  125. int watchdog_nmi_enable(unsigned int cpu)
  126. {
  127. struct perf_event_attr *wd_attr;
  128. struct perf_event *event = per_cpu(watchdog_ev, cpu);
  129. int firstcpu = 0;
  130. /* nothing to do if the hard lockup detector is disabled */
  131. if (!(watchdog_enabled & NMI_WATCHDOG_ENABLED))
  132. goto out;
  133. /* is it already setup and enabled? */
  134. if (event && event->state > PERF_EVENT_STATE_OFF)
  135. goto out;
  136. /* it is setup but not enabled */
  137. if (event != NULL)
  138. goto out_enable;
  139. if (atomic_inc_return(&watchdog_cpus) == 1)
  140. firstcpu = 1;
  141. wd_attr = &wd_hw_attr;
  142. wd_attr->sample_period = hw_nmi_get_sample_period(watchdog_thresh);
  143. /* Try to register using hardware perf events */
  144. event = perf_event_create_kernel_counter(wd_attr, cpu, NULL, watchdog_overflow_callback, NULL);
  145. /* save the first cpu's error for future comparision */
  146. if (firstcpu && IS_ERR(event))
  147. firstcpu_err = PTR_ERR(event);
  148. if (!IS_ERR(event)) {
  149. /* only print for the first cpu initialized */
  150. if (firstcpu || firstcpu_err)
  151. pr_info("enabled on all CPUs, permanently consumes one hw-PMU counter.\n");
  152. goto out_save;
  153. }
  154. /*
  155. * Disable the hard lockup detector if _any_ CPU fails to set up
  156. * set up the hardware perf event. The watchdog() function checks
  157. * the NMI_WATCHDOG_ENABLED bit periodically.
  158. *
  159. * The barriers are for syncing up watchdog_enabled across all the
  160. * cpus, as clear_bit() does not use barriers.
  161. */
  162. smp_mb__before_atomic();
  163. clear_bit(NMI_WATCHDOG_ENABLED_BIT, &watchdog_enabled);
  164. smp_mb__after_atomic();
  165. /* skip displaying the same error again */
  166. if (!firstcpu && (PTR_ERR(event) == firstcpu_err))
  167. return PTR_ERR(event);
  168. /* vary the KERN level based on the returned errno */
  169. if (PTR_ERR(event) == -EOPNOTSUPP)
  170. pr_info("disabled (cpu%i): not supported (no LAPIC?)\n", cpu);
  171. else if (PTR_ERR(event) == -ENOENT)
  172. pr_warn("disabled (cpu%i): hardware events not enabled\n",
  173. cpu);
  174. else
  175. pr_err("disabled (cpu%i): unable to create perf event: %ld\n",
  176. cpu, PTR_ERR(event));
  177. pr_info("Shutting down hard lockup detector on all cpus\n");
  178. return PTR_ERR(event);
  179. /* success path */
  180. out_save:
  181. per_cpu(watchdog_ev, cpu) = event;
  182. out_enable:
  183. perf_event_enable(per_cpu(watchdog_ev, cpu));
  184. out:
  185. return 0;
  186. }
  187. void watchdog_nmi_disable(unsigned int cpu)
  188. {
  189. struct perf_event *event = per_cpu(watchdog_ev, cpu);
  190. if (event) {
  191. perf_event_disable(event);
  192. per_cpu(watchdog_ev, cpu) = NULL;
  193. /* should be in cleanup, but blocks oprofile */
  194. perf_event_release_kernel(event);
  195. /* watchdog_nmi_enable() expects this to be zero initially. */
  196. if (atomic_dec_and_test(&watchdog_cpus))
  197. firstcpu_err = 0;
  198. }
  199. }