nmi_64.c 11 KB

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