nmi_watchdog.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. void touch_nmi_watchdog(void)
  30. {
  31. __raw_get_cpu_var(nmi_watchdog_touch) = 1;
  32. touch_softlockup_watchdog();
  33. }
  34. EXPORT_SYMBOL(touch_nmi_watchdog);
  35. void touch_all_nmi_watchdog(void)
  36. {
  37. int cpu;
  38. for_each_online_cpu(cpu)
  39. per_cpu(nmi_watchdog_touch, cpu) = 1;
  40. touch_softlockup_watchdog();
  41. }
  42. #ifdef CONFIG_SYSCTL
  43. /*
  44. * proc handler for /proc/sys/kernel/nmi_watchdog
  45. */
  46. int proc_nmi_enabled(struct ctl_table *table, int write,
  47. void __user *buffer, size_t *length, loff_t *ppos)
  48. {
  49. int cpu;
  50. if (per_cpu(nmi_watchdog_ev, smp_processor_id()) == NULL)
  51. nmi_watchdog_enabled = 0;
  52. else
  53. nmi_watchdog_enabled = 1;
  54. touch_all_nmi_watchdog();
  55. proc_dointvec(table, write, buffer, length, ppos);
  56. if (nmi_watchdog_enabled)
  57. for_each_online_cpu(cpu)
  58. perf_event_enable(per_cpu(nmi_watchdog_ev, cpu));
  59. else
  60. for_each_online_cpu(cpu)
  61. perf_event_disable(per_cpu(nmi_watchdog_ev, cpu));
  62. return 0;
  63. }
  64. #endif /* CONFIG_SYSCTL */
  65. struct perf_event_attr wd_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. static int panic_on_timeout;
  73. void wd_overflow(struct perf_event *event, int nmi,
  74. struct perf_sample_data *data,
  75. struct pt_regs *regs)
  76. {
  77. int cpu = smp_processor_id();
  78. int touched = 0;
  79. if (__get_cpu_var(nmi_watchdog_touch)) {
  80. per_cpu(nmi_watchdog_touch, cpu) = 0;
  81. touched = 1;
  82. }
  83. /* check to see if the cpu is doing anything */
  84. if (!touched && hw_nmi_is_cpu_stuck(regs)) {
  85. /*
  86. * Ayiee, looks like this CPU is stuck ...
  87. * wait a few IRQs (5 seconds) before doing the oops ...
  88. */
  89. per_cpu(alert_counter,cpu) += 1;
  90. if (per_cpu(alert_counter,cpu) == 5) {
  91. /*
  92. * die_nmi will return ONLY if NOTIFY_STOP happens..
  93. */
  94. die_nmi("BUG: NMI Watchdog detected LOCKUP",
  95. regs, panic_on_timeout);
  96. }
  97. } else {
  98. per_cpu(alert_counter,cpu) = 0;
  99. }
  100. return;
  101. }
  102. /*
  103. * Create/destroy watchdog threads as CPUs come and go:
  104. */
  105. static int __cpuinit
  106. cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
  107. {
  108. int hotcpu = (unsigned long)hcpu;
  109. struct perf_event *event;
  110. switch (action) {
  111. case CPU_UP_PREPARE:
  112. case CPU_UP_PREPARE_FROZEN:
  113. per_cpu(nmi_watchdog_touch, hotcpu) = 0;
  114. break;
  115. case CPU_ONLINE:
  116. case CPU_ONLINE_FROZEN:
  117. /* originally wanted the below chunk to be in CPU_UP_PREPARE, but caps is unpriv for non-CPU0 */
  118. wd_attr.sample_period = cpu_khz * 1000;
  119. event = perf_event_create_kernel_counter(&wd_attr, hotcpu, -1, wd_overflow);
  120. if (IS_ERR(event)) {
  121. printk(KERN_ERR "nmi watchdog failed to create perf event on %i: %p\n", hotcpu, event);
  122. return NOTIFY_BAD;
  123. }
  124. per_cpu(nmi_watchdog_ev, hotcpu) = event;
  125. perf_event_enable(per_cpu(nmi_watchdog_ev, hotcpu));
  126. break;
  127. #ifdef CONFIG_HOTPLUG_CPU
  128. case CPU_UP_CANCELED:
  129. case CPU_UP_CANCELED_FROZEN:
  130. perf_event_disable(per_cpu(nmi_watchdog_ev, hotcpu));
  131. case CPU_DEAD:
  132. case CPU_DEAD_FROZEN:
  133. event = per_cpu(nmi_watchdog_ev, hotcpu);
  134. per_cpu(nmi_watchdog_ev, hotcpu) = NULL;
  135. perf_event_release_kernel(event);
  136. break;
  137. #endif /* CONFIG_HOTPLUG_CPU */
  138. }
  139. return NOTIFY_OK;
  140. }
  141. static struct notifier_block __cpuinitdata cpu_nfb = {
  142. .notifier_call = cpu_callback
  143. };
  144. static int __initdata nonmi_watchdog;
  145. static int __init nonmi_watchdog_setup(char *str)
  146. {
  147. nonmi_watchdog = 1;
  148. return 1;
  149. }
  150. __setup("nonmi_watchdog", nonmi_watchdog_setup);
  151. static int __init spawn_nmi_watchdog_task(void)
  152. {
  153. void *cpu = (void *)(long)smp_processor_id();
  154. int err;
  155. if (nonmi_watchdog)
  156. return 0;
  157. err = cpu_callback(&cpu_nfb, CPU_UP_PREPARE, cpu);
  158. if (err == NOTIFY_BAD) {
  159. BUG();
  160. return 1;
  161. }
  162. cpu_callback(&cpu_nfb, CPU_ONLINE, cpu);
  163. register_cpu_notifier(&cpu_nfb);
  164. return 0;
  165. }
  166. early_initcall(spawn_nmi_watchdog_task);