blob: fd680c73ba77ee5a53c9c3edc017c6f92d3fe2b8 [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
Ingo Molnar3d1ba1d2008-06-02 13:50:10 +020014#include <asm/apic.h>
15
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +040016#include <linux/nmi.h>
17#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/sysdev.h>
22#include <linux/sysctl.h>
Don Zickus3e4ff112006-06-26 13:57:01 +020023#include <linux/percpu.h>
Fernando Luis Vázquez Cao06039752006-09-26 10:52:36 +020024#include <linux/kprobes.h>
Andrew Mortonbb81a092006-12-07 02:14:01 +010025#include <linux/cpumask.h>
Thomas Gleixnerf8b50352007-02-16 01:28:09 -080026#include <linux/kernel_stat.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070027#include <linux/kdebug.h>
Hiroshi Shimamoto88ff0a42008-05-27 18:49:39 -070028#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +040030#include <asm/proto.h>
Ingo Molnar6e908942008-03-21 14:32:36 +010031#include <asm/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +040033#include <asm/mce.h>
34
35#include <mach_traps.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Andi Kleen29cbc782006-09-30 01:47:55 +020037int unknown_nmi_panic;
38int nmi_watchdog_enabled;
39
Andi Kleen1714f9b2007-04-16 10:30:27 +020040static cpumask_t backtrace_mask = CPU_MASK_NONE;
Andi Kleen09198e62007-05-02 19:27:20 +020041
Linus Torvalds1da177e2005-04-16 15:20:36 -070042/* nmi_active:
Don Zickusb7471c62006-09-26 10:52:26 +020043 * >0: the lapic NMI watchdog is active, but can be disabled
44 * <0: the lapic NMI watchdog has not been set up, and cannot
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 * be enabled
Don Zickusb7471c62006-09-26 10:52:26 +020046 * 0: the lapic NMI watchdog is disabled, but can be enabled
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 */
Don Zickusb7471c62006-09-26 10:52:26 +020048atomic_t nmi_active = ATOMIC_INIT(0); /* oprofile uses this */
Hiroshi Shimamoto88ff0a42008-05-27 18:49:39 -070049EXPORT_SYMBOL(nmi_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Don Zickusb7471c62006-09-26 10:52:26 +020051unsigned int nmi_watchdog = NMI_DEFAULT;
Hiroshi Shimamoto88ff0a42008-05-27 18:49:39 -070052EXPORT_SYMBOL(nmi_watchdog);
53
54static int panic_on_timeout;
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +040055
Don Zickusb7471c62006-09-26 10:52:26 +020056static unsigned int nmi_hz = HZ;
Andi Kleen09198e62007-05-02 19:27:20 +020057static DEFINE_PER_CPU(short, wd_enabled);
Hiroshi Shimamoto88ff0a42008-05-27 18:49:39 -070058static int endflag __initdata;
Ravikiran G Thirumalai92715e22006-12-09 21:33:35 +010059
Cyrill Gorcunovfd5cea02008-05-24 19:36:40 +040060static inline unsigned int get_nmi_count(int cpu)
61{
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +040062#ifdef CONFIG_X86_64
63 return cpu_pda(cpu)->__nmi_count;
64#else
Cyrill Gorcunovfd5cea02008-05-24 19:36:40 +040065 return nmi_count(cpu);
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +040066#endif
Cyrill Gorcunovfd5cea02008-05-24 19:36:40 +040067}
68
69static inline int mce_in_progress(void)
70{
Glauber Costab8e0418b2008-06-16 19:59:08 -030071#if defined(CONFIG_X86_64) && defined(CONFIG_X86_MCE)
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +040072 return atomic_read(&mce_entry) > 0;
73#endif
Cyrill Gorcunovfd5cea02008-05-24 19:36:40 +040074 return 0;
75}
76
77/*
78 * Take the local apic timer and PIT/HPET into account. We don't
79 * know which one is active, when we have highres/dyntick on
80 */
81static inline unsigned int get_timer_irqs(int cpu)
82{
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +040083#ifdef CONFIG_X86_64
84 return read_pda(apic_timer_irqs) + read_pda(irq0_irqs);
85#else
Cyrill Gorcunovfd5cea02008-05-24 19:36:40 +040086 return per_cpu(irq_stat, cpu).apic_timer_irqs +
87 per_cpu(irq_stat, cpu).irq0_irqs;
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +040088#endif
Cyrill Gorcunovfd5cea02008-05-24 19:36:40 +040089}
90
Cyrill Gorcunov4b82b272008-05-24 19:36:35 +040091/* Run after command line and cpu_init init, but before all other checks */
92void nmi_watchdog_default(void)
93{
94 if (nmi_watchdog != NMI_DEFAULT)
95 return;
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +040096 nmi_watchdog = NMI_NONE;
Cyrill Gorcunov4b82b272008-05-24 19:36:35 +040097}
98
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 Vogel67701ae92005-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 Vogel67701ae92005-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 Hiroyukic89125992006-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 Sieversaf5ca3f42007-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 Pallipadi90ce4bc42007-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 Pallipadi90ce4bc42007-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{
Cyrill Gorcunovf781b032008-06-12 17:08:16 +0400357 if (nmi_watchdog == NMI_LOCAL_APIC ||
358 nmi_watchdog == NMI_IO_APIC) {
Jan Beulichc6ea3962006-12-07 02:14:09 +0100359 unsigned cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Jan Beulichc6ea3962006-12-07 02:14:09 +0100361 /*
Hiroshi Shimamotof7849462008-05-02 16:45:08 -0700362 * Tell other CPUs to reset their alert counters. We cannot
363 * do it ourselves because the alert count increase is not
364 * atomic.
Jan Beulichc6ea3962006-12-07 02:14:09 +0100365 */
Andrew Mortonf2890252007-07-17 04:03:57 -0700366 for_each_present_cpu(cpu) {
Hiroshi Shimamotof7849462008-05-02 16:45:08 -0700367 if (per_cpu(nmi_touch, cpu) != 1)
368 per_cpu(nmi_touch, cpu) = 1;
Andrew Mortonf2890252007-07-17 04:03:57 -0700369 }
Jan Beulichc6ea3962006-12-07 02:14:09 +0100370 }
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700371
372 /*
373 * Tickle the softlockup detector too:
374 */
375 touch_softlockup_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376}
Michal Schmidt1e862402006-07-30 03:03:29 -0700377EXPORT_SYMBOL(touch_nmi_watchdog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Steven Rostedt5deb45e2008-04-19 19:19:55 +0200379notrace __kprobes int
380nmi_watchdog_tick(struct pt_regs *regs, unsigned reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 /*
383 * Since current_thread_info()-> is always on the stack, and we
384 * always switch the stack NMI-atomically, it's safe to use
385 * smp_processor_id().
386 */
Jesper Juhlb791cce2006-03-28 01:56:52 -0800387 unsigned int sum;
Don Zickusb7471c62006-09-26 10:52:26 +0200388 int touched = 0;
Jesper Juhlb791cce2006-03-28 01:56:52 -0800389 int cpu = smp_processor_id();
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100390 int rc = 0;
Don Zickusb7471c62006-09-26 10:52:26 +0200391
392 /* check for other users first */
393 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT)
394 == NOTIFY_STOP) {
Don Zickus3adbbcc2006-09-26 10:52:26 +0200395 rc = 1;
Don Zickusb7471c62006-09-26 10:52:26 +0200396 touched = 1;
397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Cyrill Gorcunovfd5cea02008-05-24 19:36:40 +0400399 sum = get_timer_irqs(cpu);
400
401 if (__get_cpu_var(nmi_touch)) {
402 __get_cpu_var(nmi_touch) = 0;
403 touched = 1;
404 }
405
Andrew Mortonbb81a092006-12-07 02:14:01 +0100406 if (cpu_isset(cpu, backtrace_mask)) {
407 static DEFINE_SPINLOCK(lock); /* Serialise the printks */
408
409 spin_lock(&lock);
Hiroshi Shimamoto88ff0a42008-05-27 18:49:39 -0700410 printk(KERN_WARNING "NMI backtrace for cpu %d\n", cpu);
Andrew Mortonbb81a092006-12-07 02:14:01 +0100411 dump_stack();
412 spin_unlock(&lock);
413 cpu_clear(cpu, backtrace_mask);
414 }
415
Cyrill Gorcunovfd5cea02008-05-24 19:36:40 +0400416 /* Could check oops_in_progress here too, but it's safer not to */
417 if (mce_in_progress())
Hiroshi Shimamotof7849462008-05-02 16:45:08 -0700418 touched = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Thomas Gleixnerf8b50352007-02-16 01:28:09 -0800420 /* if the none of the timers isn't firing, this cpu isn't doing much */
Hiroshi Shimamotof7849462008-05-02 16:45:08 -0700421 if (!touched && __get_cpu_var(last_irq_sum) == sum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 /*
423 * Ayiee, looks like this CPU is stuck ...
424 * wait a few IRQs (5 seconds) before doing the oops ...
425 */
Hiroshi Shimamotof7849462008-05-02 16:45:08 -0700426 local_inc(&__get_cpu_var(alert_counter));
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +0400427 if (local_read(&__get_cpu_var(alert_counter)) == 5 * nmi_hz)
George Anzinger748f2ed2005-09-03 15:56:48 -0700428 /*
429 * die_nmi will return ONLY if NOTIFY_STOP happens..
430 */
Cyrill Gorcunovddca03c2008-05-24 19:36:31 +0400431 die_nmi("BUG: NMI Watchdog detected LOCKUP",
Cyrill Gorcunovd1b946b2008-05-24 19:36:34 +0400432 regs, panic_on_timeout);
GOTO Masanorib884e252006-03-07 21:55:29 -0800433 } else {
Hiroshi Shimamotof7849462008-05-02 16:45:08 -0700434 __get_cpu_var(last_irq_sum) = sum;
435 local_set(&__get_cpu_var(alert_counter), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 }
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +0400437
Don Zickusb7471c62006-09-26 10:52:26 +0200438 /* see if the nmi watchdog went off */
Andi Kleen09198e62007-05-02 19:27:20 +0200439 if (!__get_cpu_var(wd_enabled))
440 return rc;
441 switch (nmi_watchdog) {
442 case NMI_LOCAL_APIC:
443 rc |= lapic_wd_event(nmi_hz);
444 break;
445 case NMI_IO_APIC:
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +0400446 /*
447 * don't know how to accurately check for this.
Andi Kleen09198e62007-05-02 19:27:20 +0200448 * just assume it was a watchdog timer interrupt
449 * This matches the old behaviour.
450 */
451 rc = 1;
452 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
Don Zickus3adbbcc2006-09-26 10:52:26 +0200454 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
457#ifdef CONFIG_SYSCTL
458
459static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu)
460{
461 unsigned char reason = get_nmi_reason();
462 char buf[64];
463
Don Zickus2fbe7b22006-09-26 10:52:27 +0200464 sprintf(buf, "NMI received for unknown reason %02x\n", reason);
Cyrill Gorcunov6c8decdf2008-05-24 19:36:37 +0400465 die_nmi(buf, regs, 1); /* Always panic here */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return 0;
467}
468
Don Zickus407984f2006-09-26 10:52:27 +0200469/*
Don Zickuse33e89a2006-09-26 10:52:27 +0200470 * proc handler for /proc/sys/kernel/nmi
Don Zickus407984f2006-09-26 10:52:27 +0200471 */
472int proc_nmi_enabled(struct ctl_table *table, int write, struct file *file,
473 void __user *buffer, size_t *length, loff_t *ppos)
474{
475 int old_state;
476
477 nmi_watchdog_enabled = (atomic_read(&nmi_active) > 0) ? 1 : 0;
478 old_state = nmi_watchdog_enabled;
479 proc_dointvec(table, write, file, buffer, length, ppos);
480 if (!!old_state == !!nmi_watchdog_enabled)
481 return 0;
482
Daniel Gollub0328ece2007-08-15 02:40:35 +0200483 if (atomic_read(&nmi_active) < 0 || nmi_watchdog == NMI_DISABLED) {
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +0400484 printk(KERN_WARNING
485 "NMI watchdog is permanently disabled\n");
Don Zickuse33e89a2006-09-26 10:52:27 +0200486 return -EIO;
Don Zickus407984f2006-09-26 10:52:27 +0200487 }
488
Cyrill Gorcunov4b82b272008-05-24 19:36:35 +0400489 /* if nmi_watchdog is not set yet, then set it */
490 nmi_watchdog_default();
Cyrill Gorcunov3ed3f062008-06-04 01:00:47 +0400491
492#ifdef CONFIG_X86_32
493 if (nmi_watchdog == NMI_NONE) {
494 if (lapic_watchdog_ok())
495 nmi_watchdog = NMI_LOCAL_APIC;
496 else
497 nmi_watchdog = NMI_IO_APIC;
498 }
Cyrill Gorcunov19ec6732008-05-28 23:00:47 +0400499#endif
Don Zickus407984f2006-09-26 10:52:27 +0200500
Don Zickuse33e89a2006-09-26 10:52:27 +0200501 if (nmi_watchdog == NMI_LOCAL_APIC) {
Don Zickus407984f2006-09-26 10:52:27 +0200502 if (nmi_watchdog_enabled)
503 enable_lapic_nmi_watchdog();
504 else
505 disable_lapic_nmi_watchdog();
Don Zickus407984f2006-09-26 10:52:27 +0200506 } else {
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +0400507 printk(KERN_WARNING
Don Zickus407984f2006-09-26 10:52:27 +0200508 "NMI watchdog doesn't know what hardware to touch\n");
509 return -EIO;
510 }
511 return 0;
512}
513
Cyrill Gorcunov1798bc22008-05-24 19:36:41 +0400514#endif /* CONFIG_SYSCTL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Li Zefana062bae2008-02-03 15:40:30 +0800516int do_nmi_callback(struct pt_regs *regs, int cpu)
517{
518#ifdef CONFIG_SYSCTL
519 if (unknown_nmi_panic)
520 return unknown_nmi_panic_callback(regs, cpu);
521#endif
522 return 0;
523}
524
Andrew Mortonbb81a092006-12-07 02:14:01 +0100525void __trigger_all_cpu_backtrace(void)
526{
527 int i;
528
529 backtrace_mask = cpu_online_map;
530 /* Wait for up to 10 seconds for all CPUs to do the backtrace */
531 for (i = 0; i < 10 * 1000; i++) {
532 if (cpus_empty(backtrace_mask))
533 break;
534 mdelay(1);
535 }
536}