nmi.c 13 KB

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