watchdog_hld.c 6.3 KB

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