nmi_32.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /*
  2. * NMI watchdog support on APIC systems
  3. *
  4. * Started by Ingo Molnar <mingo@redhat.com>
  5. *
  6. * Fixes:
  7. * Mikael Pettersson : AMD K7 support for local APIC NMI watchdog.
  8. * Mikael Pettersson : Power Management for local APIC NMI watchdog.
  9. * Mikael Pettersson : Pentium 4 support for local APIC NMI watchdog.
  10. * Pavel Machek and
  11. * Mikael Pettersson : PM converted to driver model. Disable/enable API.
  12. */
  13. #include <linux/delay.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/module.h>
  16. #include <linux/nmi.h>
  17. #include <linux/sysdev.h>
  18. #include <linux/sysctl.h>
  19. #include <linux/percpu.h>
  20. #include <linux/kprobes.h>
  21. #include <linux/cpumask.h>
  22. #include <linux/kernel_stat.h>
  23. #include <linux/kdebug.h>
  24. #include <linux/slab.h>
  25. #include <asm/smp.h>
  26. #include <asm/nmi.h>
  27. #include <asm/timer.h>
  28. #include "mach_traps.h"
  29. int unknown_nmi_panic;
  30. int nmi_watchdog_enabled;
  31. static cpumask_t backtrace_mask = CPU_MASK_NONE;
  32. /* nmi_active:
  33. * >0: the lapic NMI watchdog is active, but can be disabled
  34. * <0: the lapic NMI watchdog has not been set up, and cannot
  35. * be enabled
  36. * 0: the lapic NMI watchdog is disabled, but can be enabled
  37. */
  38. atomic_t nmi_active = ATOMIC_INIT(0); /* oprofile uses this */
  39. unsigned int nmi_watchdog = NMI_DEFAULT;
  40. static unsigned int nmi_hz = HZ;
  41. static DEFINE_PER_CPU(short, wd_enabled);
  42. static int endflag __initdata = 0;
  43. #ifdef CONFIG_SMP
  44. /* The performance counters used by NMI_LOCAL_APIC don't trigger when
  45. * the CPU is idle. To make sure the NMI watchdog really ticks on all
  46. * CPUs during the test make them busy.
  47. */
  48. static __init void nmi_cpu_busy(void *data)
  49. {
  50. local_irq_enable_in_hardirq();
  51. /* Intentionally don't use cpu_relax here. This is
  52. to make sure that the performance counter really ticks,
  53. even if there is a simulator or similar that catches the
  54. pause instruction. On a real HT machine this is fine because
  55. all other CPUs are busy with "useless" delay loops and don't
  56. care if they get somewhat less cycles. */
  57. while (endflag == 0)
  58. mb();
  59. }
  60. #endif
  61. int __init check_nmi_watchdog(void)
  62. {
  63. unsigned int *prev_nmi_count;
  64. int cpu;
  65. if ((nmi_watchdog == NMI_NONE) || (nmi_watchdog == NMI_DISABLED))
  66. return 0;
  67. if (!atomic_read(&nmi_active))
  68. return 0;
  69. prev_nmi_count = kmalloc(NR_CPUS * sizeof(int), GFP_KERNEL);
  70. if (!prev_nmi_count)
  71. goto error;
  72. printk(KERN_INFO "Testing NMI watchdog ... ");
  73. #ifdef CONFIG_SMP
  74. if (nmi_watchdog == NMI_LOCAL_APIC)
  75. smp_call_function(nmi_cpu_busy, (void *)&endflag, 0, 0);
  76. #endif
  77. for_each_possible_cpu(cpu)
  78. prev_nmi_count[cpu] = nmi_count(cpu);
  79. local_irq_enable();
  80. mdelay((20*1000)/nmi_hz); // wait 20 ticks
  81. for_each_possible_cpu(cpu) {
  82. #ifdef CONFIG_SMP
  83. /* Check cpu_callin_map here because that is set
  84. after the timer is started. */
  85. if (!cpu_isset(cpu, cpu_callin_map))
  86. continue;
  87. #endif
  88. if (!per_cpu(wd_enabled, cpu))
  89. continue;
  90. if (nmi_count(cpu) - prev_nmi_count[cpu] <= 5) {
  91. printk(KERN_WARNING "WARNING: CPU#%d: NMI "
  92. "appears to be stuck (%d->%d)!\n",
  93. cpu,
  94. prev_nmi_count[cpu],
  95. nmi_count(cpu));
  96. per_cpu(wd_enabled, cpu) = 0;
  97. atomic_dec(&nmi_active);
  98. }
  99. }
  100. endflag = 1;
  101. if (!atomic_read(&nmi_active)) {
  102. kfree(prev_nmi_count);
  103. atomic_set(&nmi_active, -1);
  104. goto error;
  105. }
  106. printk("OK.\n");
  107. /* now that we know it works we can reduce NMI frequency to
  108. something more reasonable; makes a difference in some configs */
  109. if (nmi_watchdog == NMI_LOCAL_APIC)
  110. nmi_hz = lapic_adjust_nmi_hz(1);
  111. kfree(prev_nmi_count);
  112. return 0;
  113. error:
  114. timer_ack = !cpu_has_tsc;
  115. return -1;
  116. }
  117. static int __init setup_nmi_watchdog(char *str)
  118. {
  119. int nmi;
  120. get_option(&str, &nmi);
  121. if ((nmi >= NMI_INVALID) || (nmi < NMI_NONE))
  122. return 0;
  123. nmi_watchdog = nmi;
  124. return 1;
  125. }
  126. __setup("nmi_watchdog=", setup_nmi_watchdog);
  127. /* Suspend/resume support */
  128. #ifdef CONFIG_PM
  129. static int nmi_pm_active; /* nmi_active before suspend */
  130. static int lapic_nmi_suspend(struct sys_device *dev, pm_message_t state)
  131. {
  132. /* only CPU0 goes here, other CPUs should be offline */
  133. nmi_pm_active = atomic_read(&nmi_active);
  134. stop_apic_nmi_watchdog(NULL);
  135. BUG_ON(atomic_read(&nmi_active) != 0);
  136. return 0;
  137. }
  138. static int lapic_nmi_resume(struct sys_device *dev)
  139. {
  140. /* only CPU0 goes here, other CPUs should be offline */
  141. if (nmi_pm_active > 0) {
  142. setup_apic_nmi_watchdog(NULL);
  143. touch_nmi_watchdog();
  144. }
  145. return 0;
  146. }
  147. static struct sysdev_class nmi_sysclass = {
  148. .name = "lapic_nmi",
  149. .resume = lapic_nmi_resume,
  150. .suspend = lapic_nmi_suspend,
  151. };
  152. static struct sys_device device_lapic_nmi = {
  153. .id = 0,
  154. .cls = &nmi_sysclass,
  155. };
  156. static int __init init_lapic_nmi_sysfs(void)
  157. {
  158. int error;
  159. /* should really be a BUG_ON but b/c this is an
  160. * init call, it just doesn't work. -dcz
  161. */
  162. if (nmi_watchdog != NMI_LOCAL_APIC)
  163. return 0;
  164. if (atomic_read(&nmi_active) < 0)
  165. return 0;
  166. error = sysdev_class_register(&nmi_sysclass);
  167. if (!error)
  168. error = sysdev_register(&device_lapic_nmi);
  169. return error;
  170. }
  171. /* must come after the local APIC's device_initcall() */
  172. late_initcall(init_lapic_nmi_sysfs);
  173. #endif /* CONFIG_PM */
  174. static void __acpi_nmi_enable(void *__unused)
  175. {
  176. apic_write_around(APIC_LVT0, APIC_DM_NMI);
  177. }
  178. /*
  179. * Enable timer based NMIs on all CPUs:
  180. */
  181. void acpi_nmi_enable(void)
  182. {
  183. if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC)
  184. on_each_cpu(__acpi_nmi_enable, NULL, 0, 1);
  185. }
  186. static void __acpi_nmi_disable(void *__unused)
  187. {
  188. apic_write(APIC_LVT0, APIC_DM_NMI | APIC_LVT_MASKED);
  189. }
  190. /*
  191. * Disable timer based NMIs on all CPUs:
  192. */
  193. void acpi_nmi_disable(void)
  194. {
  195. if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC)
  196. on_each_cpu(__acpi_nmi_disable, NULL, 0, 1);
  197. }
  198. void setup_apic_nmi_watchdog(void *unused)
  199. {
  200. if (__get_cpu_var(wd_enabled))
  201. return;
  202. /* cheap hack to support suspend/resume */
  203. /* if cpu0 is not active neither should the other cpus */
  204. if ((smp_processor_id() != 0) && (atomic_read(&nmi_active) <= 0))
  205. return;
  206. switch (nmi_watchdog) {
  207. case NMI_LOCAL_APIC:
  208. __get_cpu_var(wd_enabled) = 1; /* enable it before to avoid race with handler */
  209. if (lapic_watchdog_init(nmi_hz) < 0) {
  210. __get_cpu_var(wd_enabled) = 0;
  211. return;
  212. }
  213. /* FALL THROUGH */
  214. case NMI_IO_APIC:
  215. __get_cpu_var(wd_enabled) = 1;
  216. atomic_inc(&nmi_active);
  217. }
  218. }
  219. void stop_apic_nmi_watchdog(void *unused)
  220. {
  221. /* only support LOCAL and IO APICs for now */
  222. if ((nmi_watchdog != NMI_LOCAL_APIC) &&
  223. (nmi_watchdog != NMI_IO_APIC))
  224. return;
  225. if (__get_cpu_var(wd_enabled) == 0)
  226. return;
  227. if (nmi_watchdog == NMI_LOCAL_APIC)
  228. lapic_watchdog_stop();
  229. __get_cpu_var(wd_enabled) = 0;
  230. atomic_dec(&nmi_active);
  231. }
  232. /*
  233. * the best way to detect whether a CPU has a 'hard lockup' problem
  234. * is to check it's local APIC timer IRQ counts. If they are not
  235. * changing then that CPU has some problem.
  236. *
  237. * as these watchdog NMI IRQs are generated on every CPU, we only
  238. * have to check the current processor.
  239. *
  240. * since NMIs don't listen to _any_ locks, we have to be extremely
  241. * careful not to rely on unsafe variables. The printk might lock
  242. * up though, so we have to break up any console locks first ...
  243. * [when there will be more tty-related locks, break them up
  244. * here too!]
  245. */
  246. static DEFINE_PER_CPU(unsigned, last_irq_sum);
  247. static DEFINE_PER_CPU(local_t, alert_counter);
  248. static DEFINE_PER_CPU(int, nmi_touch);
  249. void touch_nmi_watchdog(void)
  250. {
  251. if (nmi_watchdog > 0) {
  252. unsigned cpu;
  253. /*
  254. * Tell other CPUs to reset their alert counters. We cannot
  255. * do it ourselves because the alert count increase is not
  256. * atomic.
  257. */
  258. for_each_present_cpu(cpu) {
  259. if (per_cpu(nmi_touch, cpu) != 1)
  260. per_cpu(nmi_touch, cpu) = 1;
  261. }
  262. }
  263. /*
  264. * Tickle the softlockup detector too:
  265. */
  266. touch_softlockup_watchdog();
  267. }
  268. EXPORT_SYMBOL(touch_nmi_watchdog);
  269. notrace __kprobes int
  270. nmi_watchdog_tick(struct pt_regs *regs, unsigned reason)
  271. {
  272. /*
  273. * Since current_thread_info()-> is always on the stack, and we
  274. * always switch the stack NMI-atomically, it's safe to use
  275. * smp_processor_id().
  276. */
  277. unsigned int sum;
  278. int touched = 0;
  279. int cpu = smp_processor_id();
  280. int rc = 0;
  281. /* check for other users first */
  282. if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT)
  283. == NOTIFY_STOP) {
  284. rc = 1;
  285. touched = 1;
  286. }
  287. if (cpu_isset(cpu, backtrace_mask)) {
  288. static DEFINE_SPINLOCK(lock); /* Serialise the printks */
  289. spin_lock(&lock);
  290. printk("NMI backtrace for cpu %d\n", cpu);
  291. dump_stack();
  292. spin_unlock(&lock);
  293. cpu_clear(cpu, backtrace_mask);
  294. }
  295. /*
  296. * Take the local apic timer and PIT/HPET into account. We don't
  297. * know which one is active, when we have highres/dyntick on
  298. */
  299. sum = per_cpu(irq_stat, cpu).apic_timer_irqs +
  300. per_cpu(irq_stat, cpu).irq0_irqs;
  301. if (__get_cpu_var(nmi_touch)) {
  302. __get_cpu_var(nmi_touch) = 0;
  303. touched = 1;
  304. }
  305. /* if the none of the timers isn't firing, this cpu isn't doing much */
  306. if (!touched && __get_cpu_var(last_irq_sum) == sum) {
  307. /*
  308. * Ayiee, looks like this CPU is stuck ...
  309. * wait a few IRQs (5 seconds) before doing the oops ...
  310. */
  311. local_inc(&__get_cpu_var(alert_counter));
  312. if (local_read(&__get_cpu_var(alert_counter)) == 5*nmi_hz)
  313. /*
  314. * die_nmi will return ONLY if NOTIFY_STOP happens..
  315. */
  316. die_nmi("BUG: NMI Watchdog detected LOCKUP",
  317. regs, 0);
  318. } else {
  319. __get_cpu_var(last_irq_sum) = sum;
  320. local_set(&__get_cpu_var(alert_counter), 0);
  321. }
  322. /* see if the nmi watchdog went off */
  323. if (!__get_cpu_var(wd_enabled))
  324. return rc;
  325. switch (nmi_watchdog) {
  326. case NMI_LOCAL_APIC:
  327. rc |= lapic_wd_event(nmi_hz);
  328. break;
  329. case NMI_IO_APIC:
  330. /* don't know how to accurately check for this.
  331. * just assume it was a watchdog timer interrupt
  332. * This matches the old behaviour.
  333. */
  334. rc = 1;
  335. break;
  336. }
  337. return rc;
  338. }
  339. #ifdef CONFIG_SYSCTL
  340. static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu)
  341. {
  342. unsigned char reason = get_nmi_reason();
  343. char buf[64];
  344. sprintf(buf, "NMI received for unknown reason %02x\n", reason);
  345. die_nmi(buf, regs, 0);
  346. return 0;
  347. }
  348. /*
  349. * proc handler for /proc/sys/kernel/nmi
  350. */
  351. int proc_nmi_enabled(struct ctl_table *table, int write, struct file *file,
  352. void __user *buffer, size_t *length, loff_t *ppos)
  353. {
  354. int old_state;
  355. nmi_watchdog_enabled = (atomic_read(&nmi_active) > 0) ? 1 : 0;
  356. old_state = nmi_watchdog_enabled;
  357. proc_dointvec(table, write, file, buffer, length, ppos);
  358. if (!!old_state == !!nmi_watchdog_enabled)
  359. return 0;
  360. if (atomic_read(&nmi_active) < 0 || nmi_watchdog == NMI_DISABLED) {
  361. printk( KERN_WARNING "NMI watchdog is permanently disabled\n");
  362. return -EIO;
  363. }
  364. if (nmi_watchdog == NMI_DEFAULT) {
  365. if (lapic_watchdog_ok())
  366. nmi_watchdog = NMI_LOCAL_APIC;
  367. else
  368. nmi_watchdog = NMI_IO_APIC;
  369. }
  370. if (nmi_watchdog == NMI_LOCAL_APIC) {
  371. if (nmi_watchdog_enabled)
  372. enable_lapic_nmi_watchdog();
  373. else
  374. disable_lapic_nmi_watchdog();
  375. } else {
  376. printk( KERN_WARNING
  377. "NMI watchdog doesn't know what hardware to touch\n");
  378. return -EIO;
  379. }
  380. return 0;
  381. }
  382. #endif
  383. int do_nmi_callback(struct pt_regs *regs, int cpu)
  384. {
  385. #ifdef CONFIG_SYSCTL
  386. if (unknown_nmi_panic)
  387. return unknown_nmi_panic_callback(regs, cpu);
  388. #endif
  389. return 0;
  390. }
  391. void __trigger_all_cpu_backtrace(void)
  392. {
  393. int i;
  394. backtrace_mask = cpu_online_map;
  395. /* Wait for up to 10 seconds for all CPUs to do the backtrace */
  396. for (i = 0; i < 10 * 1000; i++) {
  397. if (cpus_empty(backtrace_mask))
  398. break;
  399. mdelay(1);
  400. }
  401. }
  402. EXPORT_SYMBOL(nmi_active);
  403. EXPORT_SYMBOL(nmi_watchdog);