blob: a94dbedf2b74213e8703d051144925ad35301e0c [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/module.h>
17#include <linux/nmi.h>
18#include <linux/sysdev.h>
19#include <linux/sysctl.h>
Don Zickus3e4ff112006-06-26 13:57:01 +020020#include <linux/percpu.h>
Fernando Luis Vázquez Cao06039752006-09-26 10:52:36 +020021#include <linux/kprobes.h>
Andrew Mortonbb81a092006-12-07 02:14:01 +010022#include <linux/cpumask.h>
Thomas Gleixnerf8b50352007-02-16 01:28:09 -080023#include <linux/kernel_stat.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070024#include <linux/kdebug.h>
Robert P. J. Day0054f4b2008-03-13 21:47:32 -040025#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#include <asm/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/nmi.h>
Ingo Molnar6e908942008-03-21 14:32:36 +010029#include <asm/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31#include "mach_traps.h"
32
Andi Kleen29cbc782006-09-30 01:47:55 +020033int unknown_nmi_panic;
34int nmi_watchdog_enabled;
35
Andi Kleen1714f9b2007-04-16 10:30:27 +020036static cpumask_t backtrace_mask = CPU_MASK_NONE;
Andi Kleen09198e62007-05-02 19:27:20 +020037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038/* nmi_active:
Don Zickusb7471c62006-09-26 10:52:26 +020039 * >0: the lapic NMI watchdog is active, but can be disabled
40 * <0: the lapic NMI watchdog has not been set up, and cannot
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 * be enabled
Don Zickusb7471c62006-09-26 10:52:26 +020042 * 0: the lapic NMI watchdog is disabled, but can be enabled
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 */
Don Zickusb7471c62006-09-26 10:52:26 +020044atomic_t nmi_active = ATOMIC_INIT(0); /* oprofile uses this */
Cyrill Gorcunovd1b946b2008-05-24 19:36:34 +040045static int panic_on_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Don Zickusb7471c62006-09-26 10:52:26 +020047unsigned int nmi_watchdog = NMI_DEFAULT;
48static unsigned int nmi_hz = HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Andi Kleen09198e62007-05-02 19:27:20 +020050static DEFINE_PER_CPU(short, wd_enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Ravikiran G Thirumalai92715e22006-12-09 21:33:35 +010052static int endflag __initdata = 0;
53
Cyrill Gorcunovfd5cea02008-05-24 19:36:40 +040054static inline unsigned int get_nmi_count(int cpu)
55{
56 return nmi_count(cpu);
57}
58
59static inline int mce_in_progress(void)
60{
61 return 0;
62}
63
64/*
65 * Take the local apic timer and PIT/HPET into account. We don't
66 * know which one is active, when we have highres/dyntick on
67 */
68static inline unsigned int get_timer_irqs(int cpu)
69{
70 return per_cpu(irq_stat, cpu).apic_timer_irqs +
71 per_cpu(irq_stat, cpu).irq0_irqs;
72}
73
Cyrill Gorcunov4b82b272008-05-24 19:36:35 +040074/* Run after command line and cpu_init init, but before all other checks */
75void nmi_watchdog_default(void)
76{
77 if (nmi_watchdog != NMI_DEFAULT)
78 return;
79 if (lapic_watchdog_ok())
80 nmi_watchdog = NMI_LOCAL_APIC;
81 else
82 nmi_watchdog = NMI_IO_APIC;
83}
84
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +010085#ifdef CONFIG_SMP
Eric W. Biederman29b70082005-10-30 14:59:40 -080086/* The performance counters used by NMI_LOCAL_APIC don't trigger when
87 * the CPU is idle. To make sure the NMI watchdog really ticks on all
88 * CPUs during the test make them busy.
89 */
90static __init void nmi_cpu_busy(void *data)
91{
Ingo Molnar366c7f52006-07-03 00:25:25 -070092 local_irq_enable_in_hardirq();
Eric W. Biederman29b70082005-10-30 14:59:40 -080093 /* Intentionally don't use cpu_relax here. This is
94 to make sure that the performance counter really ticks,
95 even if there is a simulator or similar that catches the
96 pause instruction. On a real HT machine this is fine because
97 all other CPUs are busy with "useless" delay loops and don't
98 care if they get somewhat less cycles. */
Ravikiran G Thirumalai92715e22006-12-09 21:33:35 +010099 while (endflag == 0)
100 mb();
Ingo Molnar04920072007-11-09 22:39:38 +0100101}
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100102#endif
Eric W. Biederman29b70082005-10-30 14:59:40 -0800103
Glauber de Oliveira Costa6d60cd52008-03-19 14:25:36 -0300104int __init check_nmi_watchdog(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
Eric W. Biederman29b70082005-10-30 14:59:40 -0800106 unsigned int *prev_nmi_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 int cpu;
108
Daniel Gollub0328ece2007-08-15 02:40:35 +0200109 if ((nmi_watchdog == NMI_NONE) || (nmi_watchdog == NMI_DISABLED))
Don Zickusb7471c62006-09-26 10:52:26 +0200110 return 0;
111
112 if (!atomic_read(&nmi_active))
Jack F Vogel67701ae2005-05-01 08:58:48 -0700113 return 0;
114
Eric W. Biederman29b70082005-10-30 14:59:40 -0800115 prev_nmi_count = kmalloc(NR_CPUS * sizeof(int), GFP_KERNEL);
116 if (!prev_nmi_count)
Ingo Molnar6e908942008-03-21 14:32:36 +0100117 goto error;
Eric W. Biederman29b70082005-10-30 14:59:40 -0800118
Jack F Vogel67701ae2005-05-01 08:58:48 -0700119 printk(KERN_INFO "Testing NMI watchdog ... ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100121#ifdef CONFIG_SMP
Eric W. Biederman29b70082005-10-30 14:59:40 -0800122 if (nmi_watchdog == NMI_LOCAL_APIC)
123 smp_call_function(nmi_cpu_busy, (void *)&endflag, 0, 0);
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100124#endif
Eric W. Biederman29b70082005-10-30 14:59:40 -0800125
KAMEZAWA Hiroyukic8912592006-03-28 01:56:39 -0800126 for_each_possible_cpu(cpu)
Cyrill Gorcunovfd5cea02008-05-24 19:36:40 +0400127 prev_nmi_count[cpu] = get_nmi_count(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 local_irq_enable();
Andi Kleen0fb2ebf2007-04-02 12:14:12 +0200129 mdelay((20*1000)/nmi_hz); // wait 20 ticks
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Cyrill Gorcunov7c2ba832008-05-24 19:36:39 +0400131 for_each_online_cpu(cpu) {
Andi Kleen09198e62007-05-02 19:27:20 +0200132 if (!per_cpu(wd_enabled, cpu))
Don Zickusb7471c62006-09-26 10:52:26 +0200133 continue;
Cyrill Gorcunovfd5cea02008-05-24 19:36:40 +0400134 if (get_nmi_count(cpu) - prev_nmi_count[cpu] <= 5) {
Don Zickus75bc1222007-12-04 17:19:07 +0100135 printk(KERN_WARNING "WARNING: CPU#%d: NMI "
136 "appears to be stuck (%d->%d)!\n",
Eric W. Biederman29b70082005-10-30 14:59:40 -0800137 cpu,
138 prev_nmi_count[cpu],
Cyrill Gorcunovfd5cea02008-05-24 19:36:40 +0400139 get_nmi_count(cpu));
Andi Kleen09198e62007-05-02 19:27:20 +0200140 per_cpu(wd_enabled, cpu) = 0;
Don Zickusb7471c62006-09-26 10:52:26 +0200141 atomic_dec(&nmi_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 }
143 }
Daniel Walkerd9586542007-09-06 16:59:54 +0200144 endflag = 1;
Don Zickusb7471c62006-09-26 10:52:26 +0200145 if (!atomic_read(&nmi_active)) {
146 kfree(prev_nmi_count);
147 atomic_set(&nmi_active, -1);
Ingo Molnar6e908942008-03-21 14:32:36 +0100148 goto error;
Don Zickusb7471c62006-09-26 10:52:26 +0200149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 printk("OK.\n");
151
152 /* now that we know it works we can reduce NMI frequency to
153 something more reasonable; makes a difference in some configs */
Andi Kleen09198e62007-05-02 19:27:20 +0200154 if (nmi_watchdog == NMI_LOCAL_APIC)
155 nmi_hz = lapic_adjust_nmi_hz(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Eric W. Biederman29b70082005-10-30 14:59:40 -0800157 kfree(prev_nmi_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 return 0;
Ingo Molnar6e908942008-03-21 14:32:36 +0100159error:
160 timer_ack = !cpu_has_tsc;
161
162 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
165static int __init setup_nmi_watchdog(char *str)
166{
167 int nmi;
168
Cyrill Gorcunovd1b946b2008-05-24 19:36:34 +0400169 if (!strncmp(str, "panic", 5)) {
170 panic_on_timeout = 1;
171 str = strchr(str, ',');
172 if (!str)
173 return 1;
174 ++str;
175 }
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 get_option(&str, &nmi);
178
Don Zickusb7471c62006-09-26 10:52:26 +0200179 if ((nmi >= NMI_INVALID) || (nmi < NMI_NONE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 return 0;
Venkatesh Pallipadi58d9ce7d2007-01-22 20:40:34 -0800181
Don Zickusb7471c62006-09-26 10:52:26 +0200182 nmi_watchdog = nmi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 return 1;
184}
185
186__setup("nmi_watchdog=", setup_nmi_watchdog);
187
Don Zickusb7471c62006-09-26 10:52:26 +0200188
Andi Kleen09198e62007-05-02 19:27:20 +0200189/* Suspend/resume support */
Ingo Molnar5d0e6002007-02-13 13:26:24 +0100190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191#ifdef CONFIG_PM
192
193static int nmi_pm_active; /* nmi_active before suspend */
194
Pavel Machek438510f2005-04-16 15:25:24 -0700195static int lapic_nmi_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
Shaohua Li4038f902006-09-26 10:52:27 +0200197 /* only CPU0 goes here, other CPUs should be offline */
Don Zickusb7471c62006-09-26 10:52:26 +0200198 nmi_pm_active = atomic_read(&nmi_active);
Shaohua Li4038f902006-09-26 10:52:27 +0200199 stop_apic_nmi_watchdog(NULL);
200 BUG_ON(atomic_read(&nmi_active) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 return 0;
202}
203
204static int lapic_nmi_resume(struct sys_device *dev)
205{
Shaohua Li4038f902006-09-26 10:52:27 +0200206 /* only CPU0 goes here, other CPUs should be offline */
207 if (nmi_pm_active > 0) {
208 setup_apic_nmi_watchdog(NULL);
209 touch_nmi_watchdog();
210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return 0;
212}
213
214
215static struct sysdev_class nmi_sysclass = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +0100216 .name = "lapic_nmi",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 .resume = lapic_nmi_resume,
218 .suspend = lapic_nmi_suspend,
219};
220
221static struct sys_device device_lapic_nmi = {
222 .id = 0,
223 .cls = &nmi_sysclass,
224};
225
226static int __init init_lapic_nmi_sysfs(void)
227{
228 int error;
229
Don Zickusb7471c62006-09-26 10:52:26 +0200230 /* should really be a BUG_ON but b/c this is an
231 * init call, it just doesn't work. -dcz
232 */
233 if (nmi_watchdog != NMI_LOCAL_APIC)
234 return 0;
235
Andi Kleen09198e62007-05-02 19:27:20 +0200236 if (atomic_read(&nmi_active) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 return 0;
238
239 error = sysdev_class_register(&nmi_sysclass);
240 if (!error)
241 error = sysdev_register(&device_lapic_nmi);
242 return error;
243}
244/* must come after the local APIC's device_initcall() */
245late_initcall(init_lapic_nmi_sysfs);
246
247#endif /* CONFIG_PM */
248
Andi Kleen09198e62007-05-02 19:27:20 +0200249static void __acpi_nmi_enable(void *__unused)
250{
251 apic_write_around(APIC_LVT0, APIC_DM_NMI);
252}
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254/*
Andi Kleen09198e62007-05-02 19:27:20 +0200255 * Enable timer based NMIs on all CPUs:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 */
Andi Kleen09198e62007-05-02 19:27:20 +0200257void acpi_nmi_enable(void)
Jan Beulich7fbb4f62005-06-23 00:08:23 -0700258{
Andi Kleen09198e62007-05-02 19:27:20 +0200259 if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC)
260 on_each_cpu(__acpi_nmi_enable, NULL, 0, 1);
Jan Beulich7fbb4f62005-06-23 00:08:23 -0700261}
262
Andi Kleen09198e62007-05-02 19:27:20 +0200263static void __acpi_nmi_disable(void *__unused)
Venkatesh Pallipadi90ce4bc2007-02-13 13:26:22 +0100264{
Cyrill Gorcunovad63ba12008-05-24 19:36:36 +0400265 apic_write_around(APIC_LVT0, APIC_DM_NMI | APIC_LVT_MASKED);
Venkatesh Pallipadi90ce4bc2007-02-13 13:26:22 +0100266}
267
Andi Kleen09198e62007-05-02 19:27:20 +0200268/*
269 * Disable timer based NMIs on all CPUs:
270 */
271void acpi_nmi_disable(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Andi Kleen09198e62007-05-02 19:27:20 +0200273 if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC)
274 on_each_cpu(__acpi_nmi_disable, NULL, 0, 1);
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +0200275}
276
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100277void setup_apic_nmi_watchdog(void *unused)
Don Zickusb7471c62006-09-26 10:52:26 +0200278{
Andi Kleen09198e62007-05-02 19:27:20 +0200279 if (__get_cpu_var(wd_enabled))
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100280 return;
Shaohua Li4038f902006-09-26 10:52:27 +0200281
282 /* cheap hack to support suspend/resume */
283 /* if cpu0 is not active neither should the other cpus */
284 if ((smp_processor_id() != 0) && (atomic_read(&nmi_active) <= 0))
285 return;
286
Andi Kleen09198e62007-05-02 19:27:20 +0200287 switch (nmi_watchdog) {
288 case NMI_LOCAL_APIC:
289 __get_cpu_var(wd_enabled) = 1; /* enable it before to avoid race with handler */
290 if (lapic_watchdog_init(nmi_hz) < 0) {
291 __get_cpu_var(wd_enabled) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 return;
293 }
Andi Kleen09198e62007-05-02 19:27:20 +0200294 /* FALL THROUGH */
295 case NMI_IO_APIC:
296 __get_cpu_var(wd_enabled) = 1;
297 atomic_inc(&nmi_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
Don Zickusb7471c62006-09-26 10:52:26 +0200299}
300
Shaohua Li4038f902006-09-26 10:52:27 +0200301void stop_apic_nmi_watchdog(void *unused)
Don Zickusb7471c62006-09-26 10:52:26 +0200302{
303 /* only support LOCAL and IO APICs for now */
304 if ((nmi_watchdog != NMI_LOCAL_APIC) &&
305 (nmi_watchdog != NMI_IO_APIC))
306 return;
Andi Kleen09198e62007-05-02 19:27:20 +0200307 if (__get_cpu_var(wd_enabled) == 0)
Shaohua Li4038f902006-09-26 10:52:27 +0200308 return;
Andi Kleen09198e62007-05-02 19:27:20 +0200309 if (nmi_watchdog == NMI_LOCAL_APIC)
310 lapic_watchdog_stop();
311 __get_cpu_var(wd_enabled) = 0;
Don Zickusb7471c62006-09-26 10:52:26 +0200312 atomic_dec(&nmi_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
315/*
316 * the best way to detect whether a CPU has a 'hard lockup' problem
317 * is to check it's local APIC timer IRQ counts. If they are not
318 * changing then that CPU has some problem.
319 *
320 * as these watchdog NMI IRQs are generated on every CPU, we only
321 * have to check the current processor.
322 *
323 * since NMIs don't listen to _any_ locks, we have to be extremely
324 * careful not to rely on unsafe variables. The printk might lock
325 * up though, so we have to break up any console locks first ...
326 * [when there will be more tty-related locks, break them up
327 * here too!]
328 */
329
Hiroshi Shimamotof7849462008-05-02 16:45:08 -0700330static DEFINE_PER_CPU(unsigned, last_irq_sum);
331static DEFINE_PER_CPU(local_t, alert_counter);
332static DEFINE_PER_CPU(int, nmi_touch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Andrew Mortonf2890252007-07-17 04:03:57 -0700334void touch_nmi_watchdog(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
Jan Beulichc6ea3962006-12-07 02:14:09 +0100336 if (nmi_watchdog > 0) {
337 unsigned cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Jan Beulichc6ea3962006-12-07 02:14:09 +0100339 /*
Hiroshi Shimamotof7849462008-05-02 16:45:08 -0700340 * Tell other CPUs to reset their alert counters. We cannot
341 * do it ourselves because the alert count increase is not
342 * atomic.
Jan Beulichc6ea3962006-12-07 02:14:09 +0100343 */
Andrew Mortonf2890252007-07-17 04:03:57 -0700344 for_each_present_cpu(cpu) {
Hiroshi Shimamotof7849462008-05-02 16:45:08 -0700345 if (per_cpu(nmi_touch, cpu) != 1)
346 per_cpu(nmi_touch, cpu) = 1;
Andrew Mortonf2890252007-07-17 04:03:57 -0700347 }
Jan Beulichc6ea3962006-12-07 02:14:09 +0100348 }
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700349
350 /*
351 * Tickle the softlockup detector too:
352 */
353 touch_softlockup_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
Michal Schmidt1e862402006-07-30 03:03:29 -0700355EXPORT_SYMBOL(touch_nmi_watchdog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Steven Rostedt5deb45e2008-04-19 19:19:55 +0200357notrace __kprobes int
358nmi_watchdog_tick(struct pt_regs *regs, unsigned reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
360
361 /*
362 * Since current_thread_info()-> is always on the stack, and we
363 * always switch the stack NMI-atomically, it's safe to use
364 * smp_processor_id().
365 */
Jesper Juhlb791cce2006-03-28 01:56:52 -0800366 unsigned int sum;
Don Zickusb7471c62006-09-26 10:52:26 +0200367 int touched = 0;
Jesper Juhlb791cce2006-03-28 01:56:52 -0800368 int cpu = smp_processor_id();
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100369 int rc = 0;
Don Zickusb7471c62006-09-26 10:52:26 +0200370
371 /* check for other users first */
372 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT)
373 == NOTIFY_STOP) {
Don Zickus3adbbcc2006-09-26 10:52:26 +0200374 rc = 1;
Don Zickusb7471c62006-09-26 10:52:26 +0200375 touched = 1;
376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Cyrill Gorcunovfd5cea02008-05-24 19:36:40 +0400378 sum = get_timer_irqs(cpu);
379
380 if (__get_cpu_var(nmi_touch)) {
381 __get_cpu_var(nmi_touch) = 0;
382 touched = 1;
383 }
384
Andrew Mortonbb81a092006-12-07 02:14:01 +0100385 if (cpu_isset(cpu, backtrace_mask)) {
386 static DEFINE_SPINLOCK(lock); /* Serialise the printks */
387
388 spin_lock(&lock);
389 printk("NMI backtrace for cpu %d\n", cpu);
390 dump_stack();
391 spin_unlock(&lock);
392 cpu_clear(cpu, backtrace_mask);
393 }
394
Cyrill Gorcunovfd5cea02008-05-24 19:36:40 +0400395 /* Could check oops_in_progress here too, but it's safer not to */
396 if (mce_in_progress())
Hiroshi Shimamotof7849462008-05-02 16:45:08 -0700397 touched = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Thomas Gleixnerf8b50352007-02-16 01:28:09 -0800399 /* if the none of the timers isn't firing, this cpu isn't doing much */
Hiroshi Shimamotof7849462008-05-02 16:45:08 -0700400 if (!touched && __get_cpu_var(last_irq_sum) == sum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 /*
402 * Ayiee, looks like this CPU is stuck ...
403 * wait a few IRQs (5 seconds) before doing the oops ...
404 */
Hiroshi Shimamotof7849462008-05-02 16:45:08 -0700405 local_inc(&__get_cpu_var(alert_counter));
406 if (local_read(&__get_cpu_var(alert_counter)) == 5*nmi_hz)
George Anzinger748f2ed2005-09-03 15:56:48 -0700407 /*
408 * die_nmi will return ONLY if NOTIFY_STOP happens..
409 */
Cyrill Gorcunovddca03c2008-05-24 19:36:31 +0400410 die_nmi("BUG: NMI Watchdog detected LOCKUP",
Cyrill Gorcunovd1b946b2008-05-24 19:36:34 +0400411 regs, panic_on_timeout);
GOTO Masanorib884e252006-03-07 21:55:29 -0800412 } else {
Hiroshi Shimamotof7849462008-05-02 16:45:08 -0700413 __get_cpu_var(last_irq_sum) = sum;
414 local_set(&__get_cpu_var(alert_counter), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
Don Zickusb7471c62006-09-26 10:52:26 +0200416 /* see if the nmi watchdog went off */
Andi Kleen09198e62007-05-02 19:27:20 +0200417 if (!__get_cpu_var(wd_enabled))
418 return rc;
419 switch (nmi_watchdog) {
420 case NMI_LOCAL_APIC:
421 rc |= lapic_wd_event(nmi_hz);
422 break;
423 case NMI_IO_APIC:
424 /* don't know how to accurately check for this.
425 * just assume it was a watchdog timer interrupt
426 * This matches the old behaviour.
427 */
428 rc = 1;
429 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
Don Zickus3adbbcc2006-09-26 10:52:26 +0200431 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
434#ifdef CONFIG_SYSCTL
435
436static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu)
437{
438 unsigned char reason = get_nmi_reason();
439 char buf[64];
440
Don Zickus2fbe7b22006-09-26 10:52:27 +0200441 sprintf(buf, "NMI received for unknown reason %02x\n", reason);
Cyrill Gorcunov6c8decd2008-05-24 19:36:37 +0400442 die_nmi(buf, regs, 1); /* Always panic here */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 return 0;
444}
445
Don Zickus407984f2006-09-26 10:52:27 +0200446/*
Don Zickuse33e89a2006-09-26 10:52:27 +0200447 * proc handler for /proc/sys/kernel/nmi
Don Zickus407984f2006-09-26 10:52:27 +0200448 */
449int proc_nmi_enabled(struct ctl_table *table, int write, struct file *file,
450 void __user *buffer, size_t *length, loff_t *ppos)
451{
452 int old_state;
453
454 nmi_watchdog_enabled = (atomic_read(&nmi_active) > 0) ? 1 : 0;
455 old_state = nmi_watchdog_enabled;
456 proc_dointvec(table, write, file, buffer, length, ppos);
457 if (!!old_state == !!nmi_watchdog_enabled)
458 return 0;
459
Daniel Gollub0328ece2007-08-15 02:40:35 +0200460 if (atomic_read(&nmi_active) < 0 || nmi_watchdog == NMI_DISABLED) {
Don Zickuse33e89a2006-09-26 10:52:27 +0200461 printk( KERN_WARNING "NMI watchdog is permanently disabled\n");
462 return -EIO;
Don Zickus407984f2006-09-26 10:52:27 +0200463 }
464
Cyrill Gorcunov4b82b272008-05-24 19:36:35 +0400465 /* if nmi_watchdog is not set yet, then set it */
466 nmi_watchdog_default();
Don Zickus407984f2006-09-26 10:52:27 +0200467
Don Zickuse33e89a2006-09-26 10:52:27 +0200468 if (nmi_watchdog == NMI_LOCAL_APIC) {
Don Zickus407984f2006-09-26 10:52:27 +0200469 if (nmi_watchdog_enabled)
470 enable_lapic_nmi_watchdog();
471 else
472 disable_lapic_nmi_watchdog();
Don Zickus407984f2006-09-26 10:52:27 +0200473 } else {
474 printk( KERN_WARNING
475 "NMI watchdog doesn't know what hardware to touch\n");
476 return -EIO;
477 }
478 return 0;
479}
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481#endif
482
Li Zefana062bae2008-02-03 15:40:30 +0800483int do_nmi_callback(struct pt_regs *regs, int cpu)
484{
485#ifdef CONFIG_SYSCTL
486 if (unknown_nmi_panic)
487 return unknown_nmi_panic_callback(regs, cpu);
488#endif
489 return 0;
490}
491
Andrew Mortonbb81a092006-12-07 02:14:01 +0100492void __trigger_all_cpu_backtrace(void)
493{
494 int i;
495
496 backtrace_mask = cpu_online_map;
497 /* Wait for up to 10 seconds for all CPUs to do the backtrace */
498 for (i = 0; i < 10 * 1000; i++) {
499 if (cpus_empty(backtrace_mask))
500 break;
501 mdelay(1);
502 }
503}
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505EXPORT_SYMBOL(nmi_active);
506EXPORT_SYMBOL(nmi_watchdog);