nmi_64.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. int unknown_nmi_panic;
  27. int nmi_watchdog_enabled;
  28. int panic_on_unrecovered_nmi;
  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. 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 (cpu = 0; cpu < NR_CPUS; 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(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(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. int __kprobes nmi_watchdog_tick(struct pt_regs * regs, unsigned reason)
  264. {
  265. int sum;
  266. int touched = 0;
  267. int cpu = smp_processor_id();
  268. int rc = 0;
  269. /* check for other users first */
  270. if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT)
  271. == NOTIFY_STOP) {
  272. rc = 1;
  273. touched = 1;
  274. }
  275. sum = read_pda(apic_timer_irqs) + read_pda(irq0_irqs);
  276. if (__get_cpu_var(nmi_touch)) {
  277. __get_cpu_var(nmi_touch) = 0;
  278. touched = 1;
  279. }
  280. if (cpu_isset(cpu, backtrace_mask)) {
  281. static DEFINE_SPINLOCK(lock); /* Serialise the printks */
  282. spin_lock(&lock);
  283. printk("NMI backtrace for cpu %d\n", cpu);
  284. dump_stack();
  285. spin_unlock(&lock);
  286. cpu_clear(cpu, backtrace_mask);
  287. }
  288. #ifdef CONFIG_X86_MCE
  289. /* Could check oops_in_progress here too, but it's safer
  290. not too */
  291. if (atomic_read(&mce_entry) > 0)
  292. touched = 1;
  293. #endif
  294. /* if the apic timer isn't firing, this cpu isn't doing much */
  295. if (!touched && __get_cpu_var(last_irq_sum) == sum) {
  296. /*
  297. * Ayiee, looks like this CPU is stuck ...
  298. * wait a few IRQs (5 seconds) before doing the oops ...
  299. */
  300. local_inc(&__get_cpu_var(alert_counter));
  301. if (local_read(&__get_cpu_var(alert_counter)) == 5*nmi_hz)
  302. die_nmi("NMI Watchdog detected LOCKUP on CPU %d\n", regs,
  303. panic_on_timeout);
  304. } else {
  305. __get_cpu_var(last_irq_sum) = sum;
  306. local_set(&__get_cpu_var(alert_counter), 0);
  307. }
  308. /* see if the nmi watchdog went off */
  309. if (!__get_cpu_var(wd_enabled))
  310. return rc;
  311. switch (nmi_watchdog) {
  312. case NMI_LOCAL_APIC:
  313. rc |= lapic_wd_event(nmi_hz);
  314. break;
  315. case NMI_IO_APIC:
  316. /* don't know how to accurately check for this.
  317. * just assume it was a watchdog timer interrupt
  318. * This matches the old behaviour.
  319. */
  320. rc = 1;
  321. break;
  322. }
  323. return rc;
  324. }
  325. static unsigned ignore_nmis;
  326. asmlinkage __kprobes void do_nmi(struct pt_regs * regs, long error_code)
  327. {
  328. nmi_enter();
  329. add_pda(__nmi_count,1);
  330. if (!ignore_nmis)
  331. default_do_nmi(regs);
  332. nmi_exit();
  333. }
  334. void stop_nmi(void)
  335. {
  336. acpi_nmi_disable();
  337. ignore_nmis++;
  338. }
  339. void restart_nmi(void)
  340. {
  341. ignore_nmis--;
  342. acpi_nmi_enable();
  343. }
  344. #ifdef CONFIG_SYSCTL
  345. static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu)
  346. {
  347. unsigned char reason = get_nmi_reason();
  348. char buf[64];
  349. sprintf(buf, "NMI received for unknown reason %02x\n", reason);
  350. die_nmi(buf, regs, 1); /* Always panic here */
  351. return 0;
  352. }
  353. /*
  354. * proc handler for /proc/sys/kernel/nmi
  355. */
  356. int proc_nmi_enabled(struct ctl_table *table, int write, struct file *file,
  357. void __user *buffer, size_t *length, loff_t *ppos)
  358. {
  359. int old_state;
  360. nmi_watchdog_enabled = (atomic_read(&nmi_active) > 0) ? 1 : 0;
  361. old_state = nmi_watchdog_enabled;
  362. proc_dointvec(table, write, file, buffer, length, ppos);
  363. if (!!old_state == !!nmi_watchdog_enabled)
  364. return 0;
  365. if (atomic_read(&nmi_active) < 0 || nmi_watchdog == NMI_DISABLED) {
  366. printk( KERN_WARNING "NMI watchdog is permanently disabled\n");
  367. return -EIO;
  368. }
  369. /* if nmi_watchdog is not set yet, then set it */
  370. nmi_watchdog_default();
  371. if (nmi_watchdog == NMI_LOCAL_APIC) {
  372. if (nmi_watchdog_enabled)
  373. enable_lapic_nmi_watchdog();
  374. else
  375. disable_lapic_nmi_watchdog();
  376. } else {
  377. printk( KERN_WARNING
  378. "NMI watchdog doesn't know what hardware to touch\n");
  379. return -EIO;
  380. }
  381. return 0;
  382. }
  383. #endif
  384. int do_nmi_callback(struct pt_regs *regs, int cpu)
  385. {
  386. #ifdef CONFIG_SYSCTL
  387. if (unknown_nmi_panic)
  388. return unknown_nmi_panic_callback(regs, cpu);
  389. #endif
  390. return 0;
  391. }
  392. void __trigger_all_cpu_backtrace(void)
  393. {
  394. int i;
  395. backtrace_mask = cpu_online_map;
  396. /* Wait for up to 10 seconds for all CPUs to do the backtrace */
  397. for (i = 0; i < 10 * 1000; i++) {
  398. if (cpus_empty(backtrace_mask))
  399. break;
  400. mdelay(1);
  401. }
  402. }
  403. EXPORT_SYMBOL(nmi_active);
  404. EXPORT_SYMBOL(nmi_watchdog);