nmi.c 11 KB

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