nmi_watchdog.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * Detect Hard Lockups using the NMI
  3. *
  4. * started by Don Zickus, Copyright (C) 2010 Red Hat, Inc.
  5. *
  6. * this code detects hard lockups: incidents in where on a CPU
  7. * the kernel does not respond to anything except NMI.
  8. *
  9. * Note: Most of this code is borrowed heavily from softlockup.c,
  10. * so thanks to Ingo for the initial implementation.
  11. * Some chunks also taken from arch/x86/kernel/apic/nmi.c, thanks
  12. * to those contributors as well.
  13. */
  14. #include <linux/mm.h>
  15. #include <linux/cpu.h>
  16. #include <linux/nmi.h>
  17. #include <linux/init.h>
  18. #include <linux/delay.h>
  19. #include <linux/freezer.h>
  20. #include <linux/lockdep.h>
  21. #include <linux/notifier.h>
  22. #include <linux/module.h>
  23. #include <linux/sysctl.h>
  24. #include <asm/irq_regs.h>
  25. #include <linux/perf_event.h>
  26. static DEFINE_PER_CPU(struct perf_event *, nmi_watchdog_ev);
  27. static DEFINE_PER_CPU(int, nmi_watchdog_touch);
  28. static DEFINE_PER_CPU(long, alert_counter);
  29. static int panic_on_timeout;
  30. void touch_nmi_watchdog(void)
  31. {
  32. __raw_get_cpu_var(nmi_watchdog_touch) = 1;
  33. touch_softlockup_watchdog();
  34. }
  35. EXPORT_SYMBOL(touch_nmi_watchdog);
  36. void touch_all_nmi_watchdog(void)
  37. {
  38. int cpu;
  39. for_each_online_cpu(cpu)
  40. per_cpu(nmi_watchdog_touch, cpu) = 1;
  41. touch_softlockup_watchdog();
  42. }
  43. static int __init setup_nmi_watchdog(char *str)
  44. {
  45. if (!strncmp(str, "panic", 5)) {
  46. panic_on_timeout = 1;
  47. str = strchr(str, ',');
  48. if (!str)
  49. return 1;
  50. ++str;
  51. }
  52. return 1;
  53. }
  54. __setup("nmi_watchdog=", setup_nmi_watchdog);
  55. #ifdef CONFIG_SYSCTL
  56. /*
  57. * proc handler for /proc/sys/kernel/nmi_watchdog
  58. */
  59. int nmi_watchdog_enabled;
  60. int proc_nmi_enabled(struct ctl_table *table, int write,
  61. void __user *buffer, size_t *length, loff_t *ppos)
  62. {
  63. int cpu;
  64. if (!write) {
  65. struct perf_event *event;
  66. for_each_online_cpu(cpu) {
  67. event = per_cpu(nmi_watchdog_ev, cpu);
  68. if (event->state > PERF_EVENT_STATE_OFF) {
  69. nmi_watchdog_enabled = 1;
  70. break;
  71. }
  72. }
  73. proc_dointvec(table, write, buffer, length, ppos);
  74. return 0;
  75. }
  76. if (per_cpu(nmi_watchdog_ev, smp_processor_id()) == NULL) {
  77. nmi_watchdog_enabled = 0;
  78. proc_dointvec(table, write, buffer, length, ppos);
  79. printk("NMI watchdog failed configuration, can not be enabled\n");
  80. return 0;
  81. }
  82. touch_all_nmi_watchdog();
  83. proc_dointvec(table, write, buffer, length, ppos);
  84. if (nmi_watchdog_enabled)
  85. for_each_online_cpu(cpu)
  86. perf_event_enable(per_cpu(nmi_watchdog_ev, cpu));
  87. else
  88. for_each_online_cpu(cpu)
  89. perf_event_disable(per_cpu(nmi_watchdog_ev, cpu));
  90. return 0;
  91. }
  92. #endif /* CONFIG_SYSCTL */
  93. struct perf_event_attr wd_attr = {
  94. .type = PERF_TYPE_HARDWARE,
  95. .config = PERF_COUNT_HW_CPU_CYCLES,
  96. .size = sizeof(struct perf_event_attr),
  97. .pinned = 1,
  98. .disabled = 1,
  99. };
  100. void wd_overflow(struct perf_event *event, int nmi,
  101. struct perf_sample_data *data,
  102. struct pt_regs *regs)
  103. {
  104. int cpu = smp_processor_id();
  105. int touched = 0;
  106. if (__get_cpu_var(nmi_watchdog_touch)) {
  107. per_cpu(nmi_watchdog_touch, cpu) = 0;
  108. touched = 1;
  109. }
  110. /* check to see if the cpu is doing anything */
  111. if (!touched && hw_nmi_is_cpu_stuck(regs)) {
  112. /*
  113. * Ayiee, looks like this CPU is stuck ...
  114. * wait a few IRQs (5 seconds) before doing the oops ...
  115. */
  116. per_cpu(alert_counter,cpu) += 1;
  117. if (per_cpu(alert_counter,cpu) == 5) {
  118. if (panic_on_timeout) {
  119. panic("NMI Watchdog detected LOCKUP on cpu %d", cpu);
  120. } else {
  121. WARN(1, "NMI Watchdog detected LOCKUP on cpu %d", cpu);
  122. }
  123. }
  124. } else {
  125. per_cpu(alert_counter,cpu) = 0;
  126. }
  127. return;
  128. }
  129. /*
  130. * Create/destroy watchdog threads as CPUs come and go:
  131. */
  132. static int __cpuinit
  133. cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
  134. {
  135. int hotcpu = (unsigned long)hcpu;
  136. struct perf_event *event;
  137. switch (action) {
  138. case CPU_UP_PREPARE:
  139. case CPU_UP_PREPARE_FROZEN:
  140. per_cpu(nmi_watchdog_touch, hotcpu) = 0;
  141. break;
  142. case CPU_ONLINE:
  143. case CPU_ONLINE_FROZEN:
  144. /* originally wanted the below chunk to be in CPU_UP_PREPARE, but caps is unpriv for non-CPU0 */
  145. wd_attr.sample_period = hw_nmi_get_sample_period();
  146. event = perf_event_create_kernel_counter(&wd_attr, hotcpu, -1, wd_overflow);
  147. if (IS_ERR(event)) {
  148. printk(KERN_ERR "nmi watchdog failed to create perf event on %i: %p\n", hotcpu, event);
  149. return NOTIFY_BAD;
  150. }
  151. per_cpu(nmi_watchdog_ev, hotcpu) = event;
  152. perf_event_enable(per_cpu(nmi_watchdog_ev, hotcpu));
  153. break;
  154. #ifdef CONFIG_HOTPLUG_CPU
  155. case CPU_UP_CANCELED:
  156. case CPU_UP_CANCELED_FROZEN:
  157. perf_event_disable(per_cpu(nmi_watchdog_ev, hotcpu));
  158. case CPU_DEAD:
  159. case CPU_DEAD_FROZEN:
  160. event = per_cpu(nmi_watchdog_ev, hotcpu);
  161. per_cpu(nmi_watchdog_ev, hotcpu) = NULL;
  162. perf_event_release_kernel(event);
  163. break;
  164. #endif /* CONFIG_HOTPLUG_CPU */
  165. }
  166. return NOTIFY_OK;
  167. }
  168. static struct notifier_block __cpuinitdata cpu_nfb = {
  169. .notifier_call = cpu_callback
  170. };
  171. static int __initdata nonmi_watchdog;
  172. static int __init nonmi_watchdog_setup(char *str)
  173. {
  174. nonmi_watchdog = 1;
  175. return 1;
  176. }
  177. __setup("nonmi_watchdog", nonmi_watchdog_setup);
  178. static int __init spawn_nmi_watchdog_task(void)
  179. {
  180. void *cpu = (void *)(long)smp_processor_id();
  181. int err;
  182. if (nonmi_watchdog)
  183. return 0;
  184. err = cpu_callback(&cpu_nfb, CPU_UP_PREPARE, cpu);
  185. if (err == NOTIFY_BAD) {
  186. BUG();
  187. return 1;
  188. }
  189. cpu_callback(&cpu_nfb, CPU_ONLINE, cpu);
  190. register_cpu_notifier(&cpu_nfb);
  191. return 0;
  192. }
  193. early_initcall(spawn_nmi_watchdog_task);