nmi_32.c 11 KB

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