nmi_32.c 11 KB

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