nmi.c 12 KB

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