nmi_64.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. 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. /* local prototypes */
  42. static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu);
  43. /* Run after command line and cpu_init init, but before all other checks */
  44. void nmi_watchdog_default(void)
  45. {
  46. if (nmi_watchdog != NMI_DEFAULT)
  47. return;
  48. nmi_watchdog = NMI_NONE;
  49. }
  50. static int endflag __initdata = 0;
  51. #ifdef CONFIG_SMP
  52. /* The performance counters used by NMI_LOCAL_APIC don't trigger when
  53. * the CPU is idle. To make sure the NMI watchdog really ticks on all
  54. * CPUs during the test make them busy.
  55. */
  56. static __init void nmi_cpu_busy(void *data)
  57. {
  58. local_irq_enable_in_hardirq();
  59. /* Intentionally don't use cpu_relax here. This is
  60. to make sure that the performance counter really ticks,
  61. even if there is a simulator or similar that catches the
  62. pause instruction. On a real HT machine this is fine because
  63. all other CPUs are busy with "useless" delay loops and don't
  64. care if they get somewhat less cycles. */
  65. while (endflag == 0)
  66. mb();
  67. }
  68. #endif
  69. int __init check_nmi_watchdog (void)
  70. {
  71. int *counts;
  72. int cpu;
  73. if ((nmi_watchdog == NMI_NONE) || (nmi_watchdog == NMI_DISABLED))
  74. return 0;
  75. if (!atomic_read(&nmi_active))
  76. return 0;
  77. counts = kmalloc(NR_CPUS * sizeof(int), GFP_KERNEL);
  78. if (!counts)
  79. return -1;
  80. printk(KERN_INFO "testing NMI watchdog ... ");
  81. #ifdef CONFIG_SMP
  82. if (nmi_watchdog == NMI_LOCAL_APIC)
  83. smp_call_function(nmi_cpu_busy, (void *)&endflag, 0, 0);
  84. #endif
  85. for (cpu = 0; cpu < NR_CPUS; cpu++)
  86. counts[cpu] = cpu_pda(cpu)->__nmi_count;
  87. local_irq_enable();
  88. mdelay((20*1000)/nmi_hz); // wait 20 ticks
  89. for_each_online_cpu(cpu) {
  90. if (!per_cpu(wd_enabled, cpu))
  91. continue;
  92. if (cpu_pda(cpu)->__nmi_count - counts[cpu] <= 5) {
  93. printk("CPU#%d: NMI appears to be stuck (%d->%d)!\n",
  94. cpu,
  95. counts[cpu],
  96. cpu_pda(cpu)->__nmi_count);
  97. per_cpu(wd_enabled, cpu) = 0;
  98. atomic_dec(&nmi_active);
  99. }
  100. }
  101. if (!atomic_read(&nmi_active)) {
  102. kfree(counts);
  103. atomic_set(&nmi_active, -1);
  104. endflag = 1;
  105. return -1;
  106. }
  107. endflag = 1;
  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(counts);
  114. return 0;
  115. }
  116. int __init setup_nmi_watchdog(char *str)
  117. {
  118. int nmi;
  119. if (!strncmp(str,"panic",5)) {
  120. panic_on_timeout = 1;
  121. str = strchr(str, ',');
  122. if (!str)
  123. return 1;
  124. ++str;
  125. }
  126. get_option(&str, &nmi);
  127. if ((nmi >= NMI_INVALID) || (nmi < NMI_NONE))
  128. return 0;
  129. nmi_watchdog = nmi;
  130. return 1;
  131. }
  132. __setup("nmi_watchdog=", setup_nmi_watchdog);
  133. static void __acpi_nmi_disable(void *__unused)
  134. {
  135. apic_write(APIC_LVT0, APIC_DM_NMI | APIC_LVT_MASKED);
  136. }
  137. /*
  138. * Disable timer based NMIs on all CPUs:
  139. */
  140. void acpi_nmi_disable(void)
  141. {
  142. if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC)
  143. on_each_cpu(__acpi_nmi_disable, NULL, 0, 1);
  144. }
  145. static void __acpi_nmi_enable(void *__unused)
  146. {
  147. apic_write(APIC_LVT0, APIC_DM_NMI);
  148. }
  149. /*
  150. * Enable timer based NMIs on all CPUs:
  151. */
  152. void acpi_nmi_enable(void)
  153. {
  154. if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC)
  155. on_each_cpu(__acpi_nmi_enable, NULL, 0, 1);
  156. }
  157. #ifdef CONFIG_PM
  158. static int nmi_pm_active; /* nmi_active before suspend */
  159. static int lapic_nmi_suspend(struct sys_device *dev, pm_message_t state)
  160. {
  161. /* only CPU0 goes here, other CPUs should be offline */
  162. nmi_pm_active = atomic_read(&nmi_active);
  163. stop_apic_nmi_watchdog(NULL);
  164. BUG_ON(atomic_read(&nmi_active) != 0);
  165. return 0;
  166. }
  167. static int lapic_nmi_resume(struct sys_device *dev)
  168. {
  169. /* only CPU0 goes here, other CPUs should be offline */
  170. if (nmi_pm_active > 0) {
  171. setup_apic_nmi_watchdog(NULL);
  172. touch_nmi_watchdog();
  173. }
  174. return 0;
  175. }
  176. static struct sysdev_class nmi_sysclass = {
  177. set_kset_name("lapic_nmi"),
  178. .resume = lapic_nmi_resume,
  179. .suspend = lapic_nmi_suspend,
  180. };
  181. static struct sys_device device_lapic_nmi = {
  182. .id = 0,
  183. .cls = &nmi_sysclass,
  184. };
  185. static int __init init_lapic_nmi_sysfs(void)
  186. {
  187. int error;
  188. /* should really be a BUG_ON but b/c this is an
  189. * init call, it just doesn't work. -dcz
  190. */
  191. if (nmi_watchdog != NMI_LOCAL_APIC)
  192. return 0;
  193. if ( atomic_read(&nmi_active) < 0 )
  194. return 0;
  195. error = sysdev_class_register(&nmi_sysclass);
  196. if (!error)
  197. error = sysdev_register(&device_lapic_nmi);
  198. return error;
  199. }
  200. /* must come after the local APIC's device_initcall() */
  201. late_initcall(init_lapic_nmi_sysfs);
  202. #endif /* CONFIG_PM */
  203. void setup_apic_nmi_watchdog(void *unused)
  204. {
  205. if (__get_cpu_var(wd_enabled) == 1)
  206. return;
  207. /* cheap hack to support suspend/resume */
  208. /* if cpu0 is not active neither should the other cpus */
  209. if ((smp_processor_id() != 0) && (atomic_read(&nmi_active) <= 0))
  210. return;
  211. switch (nmi_watchdog) {
  212. case NMI_LOCAL_APIC:
  213. __get_cpu_var(wd_enabled) = 1;
  214. if (lapic_watchdog_init(nmi_hz) < 0) {
  215. __get_cpu_var(wd_enabled) = 0;
  216. return;
  217. }
  218. /* FALL THROUGH */
  219. case NMI_IO_APIC:
  220. __get_cpu_var(wd_enabled) = 1;
  221. atomic_inc(&nmi_active);
  222. }
  223. }
  224. void stop_apic_nmi_watchdog(void *unused)
  225. {
  226. /* only support LOCAL and IO APICs for now */
  227. if ((nmi_watchdog != NMI_LOCAL_APIC) &&
  228. (nmi_watchdog != NMI_IO_APIC))
  229. return;
  230. if (__get_cpu_var(wd_enabled) == 0)
  231. return;
  232. if (nmi_watchdog == NMI_LOCAL_APIC)
  233. lapic_watchdog_stop();
  234. __get_cpu_var(wd_enabled) = 0;
  235. atomic_dec(&nmi_active);
  236. }
  237. /*
  238. * the best way to detect whether a CPU has a 'hard lockup' problem
  239. * is to check it's local APIC timer IRQ counts. If they are not
  240. * changing then that CPU has some problem.
  241. *
  242. * as these watchdog NMI IRQs are generated on every CPU, we only
  243. * have to check the current processor.
  244. */
  245. static DEFINE_PER_CPU(unsigned, last_irq_sum);
  246. static DEFINE_PER_CPU(local_t, alert_counter);
  247. static DEFINE_PER_CPU(int, nmi_touch);
  248. void touch_nmi_watchdog(void)
  249. {
  250. if (nmi_watchdog > 0) {
  251. unsigned cpu;
  252. /*
  253. * Tell other CPUs to reset their alert counters. We cannot
  254. * do it ourselves because the alert count increase is not
  255. * atomic.
  256. */
  257. for_each_present_cpu(cpu) {
  258. if (per_cpu(nmi_touch, cpu) != 1)
  259. per_cpu(nmi_touch, cpu) = 1;
  260. }
  261. }
  262. touch_softlockup_watchdog();
  263. }
  264. int __kprobes nmi_watchdog_tick(struct pt_regs * regs, unsigned reason)
  265. {
  266. 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 on CPU %d\n", regs,
  304. 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. static unsigned ignore_nmis;
  327. asmlinkage __kprobes void do_nmi(struct pt_regs * regs, long error_code)
  328. {
  329. nmi_enter();
  330. add_pda(__nmi_count,1);
  331. if (!ignore_nmis)
  332. default_do_nmi(regs);
  333. nmi_exit();
  334. }
  335. int do_nmi_callback(struct pt_regs * regs, int cpu)
  336. {
  337. #ifdef CONFIG_SYSCTL
  338. if (unknown_nmi_panic)
  339. return unknown_nmi_panic_callback(regs, cpu);
  340. #endif
  341. return 0;
  342. }
  343. void stop_nmi(void)
  344. {
  345. acpi_nmi_disable();
  346. ignore_nmis++;
  347. }
  348. void restart_nmi(void)
  349. {
  350. ignore_nmis--;
  351. acpi_nmi_enable();
  352. }
  353. #ifdef CONFIG_SYSCTL
  354. static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu)
  355. {
  356. unsigned char reason = get_nmi_reason();
  357. char buf[64];
  358. sprintf(buf, "NMI received for unknown reason %02x\n", reason);
  359. die_nmi(buf, regs, 1); /* Always panic here */
  360. return 0;
  361. }
  362. /*
  363. * proc handler for /proc/sys/kernel/nmi
  364. */
  365. int proc_nmi_enabled(struct ctl_table *table, int write, struct file *file,
  366. void __user *buffer, size_t *length, loff_t *ppos)
  367. {
  368. int old_state;
  369. nmi_watchdog_enabled = (atomic_read(&nmi_active) > 0) ? 1 : 0;
  370. old_state = nmi_watchdog_enabled;
  371. proc_dointvec(table, write, file, buffer, length, ppos);
  372. if (!!old_state == !!nmi_watchdog_enabled)
  373. return 0;
  374. if (atomic_read(&nmi_active) < 0 || nmi_watchdog == NMI_DISABLED) {
  375. printk( KERN_WARNING "NMI watchdog is permanently disabled\n");
  376. return -EIO;
  377. }
  378. /* if nmi_watchdog is not set yet, then set it */
  379. nmi_watchdog_default();
  380. if (nmi_watchdog == NMI_LOCAL_APIC) {
  381. if (nmi_watchdog_enabled)
  382. enable_lapic_nmi_watchdog();
  383. else
  384. disable_lapic_nmi_watchdog();
  385. } else {
  386. printk( KERN_WARNING
  387. "NMI watchdog doesn't know what hardware to touch\n");
  388. return -EIO;
  389. }
  390. return 0;
  391. }
  392. #endif
  393. void __trigger_all_cpu_backtrace(void)
  394. {
  395. int i;
  396. backtrace_mask = cpu_online_map;
  397. /* Wait for up to 10 seconds for all CPUs to do the backtrace */
  398. for (i = 0; i < 10 * 1000; i++) {
  399. if (cpus_empty(backtrace_mask))
  400. break;
  401. mdelay(1);
  402. }
  403. }
  404. EXPORT_SYMBOL(nmi_active);
  405. EXPORT_SYMBOL(nmi_watchdog);
  406. EXPORT_SYMBOL(touch_nmi_watchdog);