nmi.c 13 KB

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