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