blob: bf143f191fb19bd3b1aa2f99cfccc206ec8a686a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +040014#include <linux/nmi.h>
15#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/sysdev.h>
20#include <linux/sysctl.h>
Don Zickus3e4ff112006-06-26 13:57:01 +020021#include <linux/percpu.h>
Fernando Luis Vázquez Cao06039752006-09-26 10:52:36 +020022#include <linux/kprobes.h>
Andrew Mortonbb81a092006-12-07 02:14:01 +010023#include <linux/cpumask.h>
Thomas Gleixnerf8b50352007-02-16 01:28:09 -080024#include <linux/kernel_stat.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070025#include <linux/kdebug.h>
Hiroshi Shimamoto88ff0a42008-05-27 18:49:39 -070026#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +040028#include <asm/proto.h>
Ingo Molnar6e908942008-03-21 14:32:36 +010029#include <asm/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +040031#include <asm/mce.h>
32
33#include <mach_traps.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Andi Kleen29cbc782006-09-30 01:47:55 +020035int unknown_nmi_panic;
36int nmi_watchdog_enabled;
37
Andi Kleen1714f9b2007-04-16 10:30:27 +020038static cpumask_t backtrace_mask = CPU_MASK_NONE;
Andi Kleen09198e62007-05-02 19:27:20 +020039
Linus Torvalds1da177e2005-04-16 15:20:36 -070040/* nmi_active:
Don Zickusb7471c62006-09-26 10:52:26 +020041 * >0: the lapic NMI watchdog is active, but can be disabled
42 * <0: the lapic NMI watchdog has not been set up, and cannot
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 * be enabled
Don Zickusb7471c62006-09-26 10:52:26 +020044 * 0: the lapic NMI watchdog is disabled, but can be enabled
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 */
Don Zickusb7471c62006-09-26 10:52:26 +020046atomic_t nmi_active = ATOMIC_INIT(0); /* oprofile uses this */
Hiroshi Shimamoto88ff0a42008-05-27 18:49:39 -070047EXPORT_SYMBOL(nmi_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Don Zickusb7471c62006-09-26 10:52:26 +020049unsigned int nmi_watchdog = NMI_DEFAULT;
Hiroshi Shimamoto88ff0a42008-05-27 18:49:39 -070050EXPORT_SYMBOL(nmi_watchdog);
51
52static int panic_on_timeout;
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +040053
Don Zickusb7471c62006-09-26 10:52:26 +020054static unsigned int nmi_hz = HZ;
Andi Kleen09198e62007-05-02 19:27:20 +020055static DEFINE_PER_CPU(short, wd_enabled);
Hiroshi Shimamoto88ff0a42008-05-27 18:49:39 -070056static int endflag __initdata;
Ravikiran G Thirumalai92715e22006-12-09 21:33:35 +010057
Cyrill Gorcunovfd5cea02008-05-24 19:36:40 +040058static inline unsigned int get_nmi_count(int cpu)
59{
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +040060#ifdef CONFIG_X86_64
61 return cpu_pda(cpu)->__nmi_count;
62#else
Cyrill Gorcunovfd5cea02008-05-24 19:36:40 +040063 return nmi_count(cpu);
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +040064#endif
Cyrill Gorcunovfd5cea02008-05-24 19:36:40 +040065}
66
67static inline int mce_in_progress(void)
68{
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +040069#if defined(CONFIX_X86_64) && defined(CONFIG_X86_MCE)
70 return atomic_read(&mce_entry) > 0;
71#endif
Cyrill Gorcunovfd5cea02008-05-24 19:36:40 +040072 return 0;
73}
74
75/*
76 * Take the local apic timer and PIT/HPET into account. We don't
77 * know which one is active, when we have highres/dyntick on
78 */
79static inline unsigned int get_timer_irqs(int cpu)
80{
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +040081#ifdef CONFIG_X86_64
82 return read_pda(apic_timer_irqs) + read_pda(irq0_irqs);
83#else
Cyrill Gorcunovfd5cea02008-05-24 19:36:40 +040084 return per_cpu(irq_stat, cpu).apic_timer_irqs +
85 per_cpu(irq_stat, cpu).irq0_irqs;
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +040086#endif
Cyrill Gorcunovfd5cea02008-05-24 19:36:40 +040087}
88
Cyrill Gorcunov19ec6732008-05-28 23:00:47 +040089#ifdef CONFIG_X86_64
Cyrill Gorcunov4b82b272008-05-24 19:36:35 +040090/* Run after command line and cpu_init init, but before all other checks */
91void nmi_watchdog_default(void)
92{
93 if (nmi_watchdog != NMI_DEFAULT)
94 return;
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +040095 nmi_watchdog = NMI_NONE;
Cyrill Gorcunov4b82b272008-05-24 19:36:35 +040096}
Cyrill Gorcunov19ec6732008-05-28 23:00:47 +040097#endif
Cyrill Gorcunov4b82b272008-05-24 19:36:35 +040098
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +010099#ifdef CONFIG_SMP
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +0400100/*
101 * The performance counters used by NMI_LOCAL_APIC don't trigger when
Eric W. Biederman29b70082005-10-30 14:59:40 -0800102 * the CPU is idle. To make sure the NMI watchdog really ticks on all
103 * CPUs during the test make them busy.
104 */
105static __init void nmi_cpu_busy(void *data)
106{
Ingo Molnar366c7f52006-07-03 00:25:25 -0700107 local_irq_enable_in_hardirq();
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +0400108 /*
109 * Intentionally don't use cpu_relax here. This is
110 * to make sure that the performance counter really ticks,
111 * even if there is a simulator or similar that catches the
112 * pause instruction. On a real HT machine this is fine because
113 * all other CPUs are busy with "useless" delay loops and don't
114 * care if they get somewhat less cycles.
115 */
Ravikiran G Thirumalai92715e22006-12-09 21:33:35 +0100116 while (endflag == 0)
117 mb();
Ingo Molnar04920072007-11-09 22:39:38 +0100118}
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100119#endif
Eric W. Biederman29b70082005-10-30 14:59:40 -0800120
Glauber de Oliveira Costa6d60cd52008-03-19 14:25:36 -0300121int __init check_nmi_watchdog(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Eric W. Biederman29b70082005-10-30 14:59:40 -0800123 unsigned int *prev_nmi_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 int cpu;
125
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +0400126 if (nmi_watchdog == NMI_NONE || nmi_watchdog == NMI_DISABLED)
Don Zickusb7471c62006-09-26 10:52:26 +0200127 return 0;
128
129 if (!atomic_read(&nmi_active))
Jack F Vogel67701ae2005-05-01 08:58:48 -0700130 return 0;
131
Eric W. Biederman29b70082005-10-30 14:59:40 -0800132 prev_nmi_count = kmalloc(NR_CPUS * sizeof(int), GFP_KERNEL);
133 if (!prev_nmi_count)
Ingo Molnar6e908942008-03-21 14:32:36 +0100134 goto error;
Eric W. Biederman29b70082005-10-30 14:59:40 -0800135
Jack F Vogel67701ae2005-05-01 08:58:48 -0700136 printk(KERN_INFO "Testing NMI watchdog ... ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100138#ifdef CONFIG_SMP
Eric W. Biederman29b70082005-10-30 14:59:40 -0800139 if (nmi_watchdog == NMI_LOCAL_APIC)
140 smp_call_function(nmi_cpu_busy, (void *)&endflag, 0, 0);
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100141#endif
Eric W. Biederman29b70082005-10-30 14:59:40 -0800142
KAMEZAWA Hiroyukic8912592006-03-28 01:56:39 -0800143 for_each_possible_cpu(cpu)
Cyrill Gorcunovfd5cea02008-05-24 19:36:40 +0400144 prev_nmi_count[cpu] = get_nmi_count(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 local_irq_enable();
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +0400146 mdelay((20 * 1000) / nmi_hz); /* wait 20 ticks */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Cyrill Gorcunov7c2ba832008-05-24 19:36:39 +0400148 for_each_online_cpu(cpu) {
Andi Kleen09198e62007-05-02 19:27:20 +0200149 if (!per_cpu(wd_enabled, cpu))
Don Zickusb7471c62006-09-26 10:52:26 +0200150 continue;
Cyrill Gorcunovfd5cea02008-05-24 19:36:40 +0400151 if (get_nmi_count(cpu) - prev_nmi_count[cpu] <= 5) {
Don Zickus75bc1222007-12-04 17:19:07 +0100152 printk(KERN_WARNING "WARNING: CPU#%d: NMI "
153 "appears to be stuck (%d->%d)!\n",
Eric W. Biederman29b70082005-10-30 14:59:40 -0800154 cpu,
155 prev_nmi_count[cpu],
Cyrill Gorcunovfd5cea02008-05-24 19:36:40 +0400156 get_nmi_count(cpu));
Andi Kleen09198e62007-05-02 19:27:20 +0200157 per_cpu(wd_enabled, cpu) = 0;
Don Zickusb7471c62006-09-26 10:52:26 +0200158 atomic_dec(&nmi_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 }
160 }
Daniel Walkerd9586542007-09-06 16:59:54 +0200161 endflag = 1;
Don Zickusb7471c62006-09-26 10:52:26 +0200162 if (!atomic_read(&nmi_active)) {
163 kfree(prev_nmi_count);
164 atomic_set(&nmi_active, -1);
Ingo Molnar6e908942008-03-21 14:32:36 +0100165 goto error;
Don Zickusb7471c62006-09-26 10:52:26 +0200166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 printk("OK.\n");
168
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +0400169 /*
170 * now that we know it works we can reduce NMI frequency to
171 * something more reasonable; makes a difference in some configs
172 */
Andi Kleen09198e62007-05-02 19:27:20 +0200173 if (nmi_watchdog == NMI_LOCAL_APIC)
174 nmi_hz = lapic_adjust_nmi_hz(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Eric W. Biederman29b70082005-10-30 14:59:40 -0800176 kfree(prev_nmi_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 return 0;
Ingo Molnar6e908942008-03-21 14:32:36 +0100178
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +0400179error:
180#ifdef CONFIG_X86_32
181 timer_ack = !cpu_has_tsc;
182#endif
Ingo Molnar6e908942008-03-21 14:32:36 +0100183 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
186static int __init setup_nmi_watchdog(char *str)
187{
188 int nmi;
189
Cyrill Gorcunovd1b946b2008-05-24 19:36:34 +0400190 if (!strncmp(str, "panic", 5)) {
191 panic_on_timeout = 1;
192 str = strchr(str, ',');
193 if (!str)
194 return 1;
195 ++str;
196 }
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 get_option(&str, &nmi);
199
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +0400200 if (nmi >= NMI_INVALID || nmi < NMI_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 return 0;
Venkatesh Pallipadi58d9ce7d2007-01-22 20:40:34 -0800202
Don Zickusb7471c62006-09-26 10:52:26 +0200203 nmi_watchdog = nmi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 return 1;
205}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206__setup("nmi_watchdog=", setup_nmi_watchdog);
207
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +0400208/*
209 * Suspend/resume support
210 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211#ifdef CONFIG_PM
212
213static int nmi_pm_active; /* nmi_active before suspend */
214
Pavel Machek438510f2005-04-16 15:25:24 -0700215static int lapic_nmi_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
Shaohua Li4038f902006-09-26 10:52:27 +0200217 /* only CPU0 goes here, other CPUs should be offline */
Don Zickusb7471c62006-09-26 10:52:26 +0200218 nmi_pm_active = atomic_read(&nmi_active);
Shaohua Li4038f902006-09-26 10:52:27 +0200219 stop_apic_nmi_watchdog(NULL);
220 BUG_ON(atomic_read(&nmi_active) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return 0;
222}
223
224static int lapic_nmi_resume(struct sys_device *dev)
225{
Shaohua Li4038f902006-09-26 10:52:27 +0200226 /* only CPU0 goes here, other CPUs should be offline */
227 if (nmi_pm_active > 0) {
228 setup_apic_nmi_watchdog(NULL);
229 touch_nmi_watchdog();
230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 return 0;
232}
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234static struct sysdev_class nmi_sysclass = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +0100235 .name = "lapic_nmi",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 .resume = lapic_nmi_resume,
237 .suspend = lapic_nmi_suspend,
238};
239
240static struct sys_device device_lapic_nmi = {
241 .id = 0,
242 .cls = &nmi_sysclass,
243};
244
245static int __init init_lapic_nmi_sysfs(void)
246{
247 int error;
248
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +0400249 /*
250 * should really be a BUG_ON but b/c this is an
Don Zickusb7471c62006-09-26 10:52:26 +0200251 * init call, it just doesn't work. -dcz
252 */
253 if (nmi_watchdog != NMI_LOCAL_APIC)
254 return 0;
255
Andi Kleen09198e62007-05-02 19:27:20 +0200256 if (atomic_read(&nmi_active) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 return 0;
258
259 error = sysdev_class_register(&nmi_sysclass);
260 if (!error)
261 error = sysdev_register(&device_lapic_nmi);
262 return error;
263}
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +0400264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265/* must come after the local APIC's device_initcall() */
266late_initcall(init_lapic_nmi_sysfs);
267
268#endif /* CONFIG_PM */
269
Andi Kleen09198e62007-05-02 19:27:20 +0200270static void __acpi_nmi_enable(void *__unused)
271{
272 apic_write_around(APIC_LVT0, APIC_DM_NMI);
273}
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275/*
Andi Kleen09198e62007-05-02 19:27:20 +0200276 * Enable timer based NMIs on all CPUs:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 */
Andi Kleen09198e62007-05-02 19:27:20 +0200278void acpi_nmi_enable(void)
Jan Beulich7fbb4f62005-06-23 00:08:23 -0700279{
Andi Kleen09198e62007-05-02 19:27:20 +0200280 if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC)
281 on_each_cpu(__acpi_nmi_enable, NULL, 0, 1);
Jan Beulich7fbb4f62005-06-23 00:08:23 -0700282}
283
Andi Kleen09198e62007-05-02 19:27:20 +0200284static void __acpi_nmi_disable(void *__unused)
Venkatesh Pallipadi90ce4bc2007-02-13 13:26:22 +0100285{
Cyrill Gorcunovad63ba12008-05-24 19:36:36 +0400286 apic_write_around(APIC_LVT0, APIC_DM_NMI | APIC_LVT_MASKED);
Venkatesh Pallipadi90ce4bc2007-02-13 13:26:22 +0100287}
288
Andi Kleen09198e62007-05-02 19:27:20 +0200289/*
290 * Disable timer based NMIs on all CPUs:
291 */
292void acpi_nmi_disable(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
Andi Kleen09198e62007-05-02 19:27:20 +0200294 if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC)
295 on_each_cpu(__acpi_nmi_disable, NULL, 0, 1);
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +0200296}
297
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100298void setup_apic_nmi_watchdog(void *unused)
Don Zickusb7471c62006-09-26 10:52:26 +0200299{
Andi Kleen09198e62007-05-02 19:27:20 +0200300 if (__get_cpu_var(wd_enabled))
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100301 return;
Shaohua Li4038f902006-09-26 10:52:27 +0200302
303 /* cheap hack to support suspend/resume */
304 /* if cpu0 is not active neither should the other cpus */
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +0400305 if (smp_processor_id() != 0 && atomic_read(&nmi_active) <= 0)
Shaohua Li4038f902006-09-26 10:52:27 +0200306 return;
307
Andi Kleen09198e62007-05-02 19:27:20 +0200308 switch (nmi_watchdog) {
309 case NMI_LOCAL_APIC:
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +0400310 /* enable it before to avoid race with handler */
311 __get_cpu_var(wd_enabled) = 1;
Andi Kleen09198e62007-05-02 19:27:20 +0200312 if (lapic_watchdog_init(nmi_hz) < 0) {
313 __get_cpu_var(wd_enabled) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 return;
315 }
Andi Kleen09198e62007-05-02 19:27:20 +0200316 /* FALL THROUGH */
317 case NMI_IO_APIC:
318 __get_cpu_var(wd_enabled) = 1;
319 atomic_inc(&nmi_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
Don Zickusb7471c62006-09-26 10:52:26 +0200321}
322
Shaohua Li4038f902006-09-26 10:52:27 +0200323void stop_apic_nmi_watchdog(void *unused)
Don Zickusb7471c62006-09-26 10:52:26 +0200324{
325 /* only support LOCAL and IO APICs for now */
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +0400326 if (nmi_watchdog != NMI_LOCAL_APIC &&
327 nmi_watchdog != NMI_IO_APIC)
328 return;
Andi Kleen09198e62007-05-02 19:27:20 +0200329 if (__get_cpu_var(wd_enabled) == 0)
Shaohua Li4038f902006-09-26 10:52:27 +0200330 return;
Andi Kleen09198e62007-05-02 19:27:20 +0200331 if (nmi_watchdog == NMI_LOCAL_APIC)
332 lapic_watchdog_stop();
333 __get_cpu_var(wd_enabled) = 0;
Don Zickusb7471c62006-09-26 10:52:26 +0200334 atomic_dec(&nmi_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
337/*
338 * the best way to detect whether a CPU has a 'hard lockup' problem
339 * is to check it's local APIC timer IRQ counts. If they are not
340 * changing then that CPU has some problem.
341 *
342 * as these watchdog NMI IRQs are generated on every CPU, we only
343 * have to check the current processor.
344 *
345 * since NMIs don't listen to _any_ locks, we have to be extremely
346 * careful not to rely on unsafe variables. The printk might lock
347 * up though, so we have to break up any console locks first ...
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +0400348 * [when there will be more tty-related locks, break them up here too!]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 */
350
Hiroshi Shimamotof7849462008-05-02 16:45:08 -0700351static DEFINE_PER_CPU(unsigned, last_irq_sum);
352static DEFINE_PER_CPU(local_t, alert_counter);
353static DEFINE_PER_CPU(int, nmi_touch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Andrew Mortonf2890252007-07-17 04:03:57 -0700355void touch_nmi_watchdog(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
Jan Beulichc6ea3962006-12-07 02:14:09 +0100357 if (nmi_watchdog > 0) {
358 unsigned cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Jan Beulichc6ea3962006-12-07 02:14:09 +0100360 /*
Hiroshi Shimamotof7849462008-05-02 16:45:08 -0700361 * Tell other CPUs to reset their alert counters. We cannot
362 * do it ourselves because the alert count increase is not
363 * atomic.
Jan Beulichc6ea3962006-12-07 02:14:09 +0100364 */
Andrew Mortonf2890252007-07-17 04:03:57 -0700365 for_each_present_cpu(cpu) {
Hiroshi Shimamotof7849462008-05-02 16:45:08 -0700366 if (per_cpu(nmi_touch, cpu) != 1)
367 per_cpu(nmi_touch, cpu) = 1;
Andrew Mortonf2890252007-07-17 04:03:57 -0700368 }
Jan Beulichc6ea3962006-12-07 02:14:09 +0100369 }
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700370
371 /*
372 * Tickle the softlockup detector too:
373 */
374 touch_softlockup_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
Michal Schmidt1e862402006-07-30 03:03:29 -0700376EXPORT_SYMBOL(touch_nmi_watchdog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Steven Rostedt5deb45e2008-04-19 19:19:55 +0200378notrace __kprobes int
379nmi_watchdog_tick(struct pt_regs *regs, unsigned reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 /*
382 * Since current_thread_info()-> is always on the stack, and we
383 * always switch the stack NMI-atomically, it's safe to use
384 * smp_processor_id().
385 */
Jesper Juhlb791cce2006-03-28 01:56:52 -0800386 unsigned int sum;
Don Zickusb7471c62006-09-26 10:52:26 +0200387 int touched = 0;
Jesper Juhlb791cce2006-03-28 01:56:52 -0800388 int cpu = smp_processor_id();
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100389 int rc = 0;
Don Zickusb7471c62006-09-26 10:52:26 +0200390
391 /* check for other users first */
392 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT)
393 == NOTIFY_STOP) {
Don Zickus3adbbcce2006-09-26 10:52:26 +0200394 rc = 1;
Don Zickusb7471c62006-09-26 10:52:26 +0200395 touched = 1;
396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Cyrill Gorcunovfd5cea02008-05-24 19:36:40 +0400398 sum = get_timer_irqs(cpu);
399
400 if (__get_cpu_var(nmi_touch)) {
401 __get_cpu_var(nmi_touch) = 0;
402 touched = 1;
403 }
404
Andrew Mortonbb81a092006-12-07 02:14:01 +0100405 if (cpu_isset(cpu, backtrace_mask)) {
406 static DEFINE_SPINLOCK(lock); /* Serialise the printks */
407
408 spin_lock(&lock);
Hiroshi Shimamoto88ff0a42008-05-27 18:49:39 -0700409 printk(KERN_WARNING "NMI backtrace for cpu %d\n", cpu);
Andrew Mortonbb81a092006-12-07 02:14:01 +0100410 dump_stack();
411 spin_unlock(&lock);
412 cpu_clear(cpu, backtrace_mask);
413 }
414
Cyrill Gorcunovfd5cea02008-05-24 19:36:40 +0400415 /* Could check oops_in_progress here too, but it's safer not to */
416 if (mce_in_progress())
Hiroshi Shimamotof7849462008-05-02 16:45:08 -0700417 touched = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Thomas Gleixnerf8b50352007-02-16 01:28:09 -0800419 /* if the none of the timers isn't firing, this cpu isn't doing much */
Hiroshi Shimamotof7849462008-05-02 16:45:08 -0700420 if (!touched && __get_cpu_var(last_irq_sum) == sum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 /*
422 * Ayiee, looks like this CPU is stuck ...
423 * wait a few IRQs (5 seconds) before doing the oops ...
424 */
Hiroshi Shimamotof7849462008-05-02 16:45:08 -0700425 local_inc(&__get_cpu_var(alert_counter));
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +0400426 if (local_read(&__get_cpu_var(alert_counter)) == 5 * nmi_hz)
George Anzinger748f2ed2005-09-03 15:56:48 -0700427 /*
428 * die_nmi will return ONLY if NOTIFY_STOP happens..
429 */
Cyrill Gorcunovddca03c2008-05-24 19:36:31 +0400430 die_nmi("BUG: NMI Watchdog detected LOCKUP",
Cyrill Gorcunovd1b946b2008-05-24 19:36:34 +0400431 regs, panic_on_timeout);
GOTO Masanorib884e252006-03-07 21:55:29 -0800432 } else {
Hiroshi Shimamotof7849462008-05-02 16:45:08 -0700433 __get_cpu_var(last_irq_sum) = sum;
434 local_set(&__get_cpu_var(alert_counter), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +0400436
Don Zickusb7471c62006-09-26 10:52:26 +0200437 /* see if the nmi watchdog went off */
Andi Kleen09198e62007-05-02 19:27:20 +0200438 if (!__get_cpu_var(wd_enabled))
439 return rc;
440 switch (nmi_watchdog) {
441 case NMI_LOCAL_APIC:
442 rc |= lapic_wd_event(nmi_hz);
443 break;
444 case NMI_IO_APIC:
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +0400445 /*
446 * don't know how to accurately check for this.
Andi Kleen09198e62007-05-02 19:27:20 +0200447 * just assume it was a watchdog timer interrupt
448 * This matches the old behaviour.
449 */
450 rc = 1;
451 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
Don Zickus3adbbcce2006-09-26 10:52:26 +0200453 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
456#ifdef CONFIG_SYSCTL
457
458static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu)
459{
460 unsigned char reason = get_nmi_reason();
461 char buf[64];
462
Don Zickus2fbe7b22006-09-26 10:52:27 +0200463 sprintf(buf, "NMI received for unknown reason %02x\n", reason);
Cyrill Gorcunov6c8decd2008-05-24 19:36:37 +0400464 die_nmi(buf, regs, 1); /* Always panic here */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return 0;
466}
467
Don Zickus407984f2006-09-26 10:52:27 +0200468/*
Don Zickuse33e89a2006-09-26 10:52:27 +0200469 * proc handler for /proc/sys/kernel/nmi
Don Zickus407984f2006-09-26 10:52:27 +0200470 */
471int proc_nmi_enabled(struct ctl_table *table, int write, struct file *file,
472 void __user *buffer, size_t *length, loff_t *ppos)
473{
474 int old_state;
475
476 nmi_watchdog_enabled = (atomic_read(&nmi_active) > 0) ? 1 : 0;
477 old_state = nmi_watchdog_enabled;
478 proc_dointvec(table, write, file, buffer, length, ppos);
479 if (!!old_state == !!nmi_watchdog_enabled)
480 return 0;
481
Daniel Gollub0328ece2007-08-15 02:40:35 +0200482 if (atomic_read(&nmi_active) < 0 || nmi_watchdog == NMI_DISABLED) {
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +0400483 printk(KERN_WARNING
484 "NMI watchdog is permanently disabled\n");
Don Zickuse33e89a2006-09-26 10:52:27 +0200485 return -EIO;
Don Zickus407984f2006-09-26 10:52:27 +0200486 }
487
Cyrill Gorcunov19ec6732008-05-28 23:00:47 +0400488#ifdef CONFIG_X86_64
Cyrill Gorcunov4b82b272008-05-24 19:36:35 +0400489 /* if nmi_watchdog is not set yet, then set it */
490 nmi_watchdog_default();
Cyrill Gorcunov19ec6732008-05-28 23:00:47 +0400491#else
492 if (lapic_watchdog_ok())
493 nmi_watchdog = NMI_LOCAL_APIC;
494 else
495 nmi_watchdog = NMI_IO_APIC;
496#endif
Don Zickus407984f2006-09-26 10:52:27 +0200497
Don Zickuse33e89a2006-09-26 10:52:27 +0200498 if (nmi_watchdog == NMI_LOCAL_APIC) {
Don Zickus407984f2006-09-26 10:52:27 +0200499 if (nmi_watchdog_enabled)
500 enable_lapic_nmi_watchdog();
501 else
502 disable_lapic_nmi_watchdog();
Don Zickus407984f2006-09-26 10:52:27 +0200503 } else {
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +0400504 printk(KERN_WARNING
Don Zickus407984f2006-09-26 10:52:27 +0200505 "NMI watchdog doesn't know what hardware to touch\n");
506 return -EIO;
507 }
508 return 0;
509}
510
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +0400511#endif /* CONFIG_SYSCTL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Li Zefana062bae2008-02-03 15:40:30 +0800513int do_nmi_callback(struct pt_regs *regs, int cpu)
514{
515#ifdef CONFIG_SYSCTL
516 if (unknown_nmi_panic)
517 return unknown_nmi_panic_callback(regs, cpu);
518#endif
519 return 0;
520}
521
Andrew Mortonbb81a092006-12-07 02:14:01 +0100522void __trigger_all_cpu_backtrace(void)
523{
524 int i;
525
526 backtrace_mask = cpu_online_map;
527 /* Wait for up to 10 seconds for all CPUs to do the backtrace */
528 for (i = 0; i < 10 * 1000; i++) {
529 if (cpus_empty(backtrace_mask))
530 break;
531 mdelay(1);
532 }
533}