blob: ec5842b4dd68b2d4f5795ab74f7603f3af527b13 [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 Gorcunov4b82b272008-05-24 19:36:35 +040054/* Run after command line and cpu_init init, but before all other checks */
55void nmi_watchdog_default(void)
56{
57 if (nmi_watchdog != NMI_DEFAULT)
58 return;
59 if (lapic_watchdog_ok())
60 nmi_watchdog = NMI_LOCAL_APIC;
61 else
62 nmi_watchdog = NMI_IO_APIC;
63}
64
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +010065#ifdef CONFIG_SMP
Eric W. Biederman29b70082005-10-30 14:59:40 -080066/* The performance counters used by NMI_LOCAL_APIC don't trigger when
67 * the CPU is idle. To make sure the NMI watchdog really ticks on all
68 * CPUs during the test make them busy.
69 */
70static __init void nmi_cpu_busy(void *data)
71{
Ingo Molnar366c7f52006-07-03 00:25:25 -070072 local_irq_enable_in_hardirq();
Eric W. Biederman29b70082005-10-30 14:59:40 -080073 /* Intentionally don't use cpu_relax here. This is
74 to make sure that the performance counter really ticks,
75 even if there is a simulator or similar that catches the
76 pause instruction. On a real HT machine this is fine because
77 all other CPUs are busy with "useless" delay loops and don't
78 care if they get somewhat less cycles. */
Ravikiran G Thirumalai92715e22006-12-09 21:33:35 +010079 while (endflag == 0)
80 mb();
Ingo Molnar04920072007-11-09 22:39:38 +010081}
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +010082#endif
Eric W. Biederman29b70082005-10-30 14:59:40 -080083
Glauber de Oliveira Costa6d60cd52008-03-19 14:25:36 -030084int __init check_nmi_watchdog(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
Eric W. Biederman29b70082005-10-30 14:59:40 -080086 unsigned int *prev_nmi_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 int cpu;
88
Daniel Gollub0328ece2007-08-15 02:40:35 +020089 if ((nmi_watchdog == NMI_NONE) || (nmi_watchdog == NMI_DISABLED))
Don Zickusb7471c62006-09-26 10:52:26 +020090 return 0;
91
92 if (!atomic_read(&nmi_active))
Jack F Vogel67701ae2005-05-01 08:58:48 -070093 return 0;
94
Eric W. Biederman29b70082005-10-30 14:59:40 -080095 prev_nmi_count = kmalloc(NR_CPUS * sizeof(int), GFP_KERNEL);
96 if (!prev_nmi_count)
Ingo Molnar6e908942008-03-21 14:32:36 +010097 goto error;
Eric W. Biederman29b70082005-10-30 14:59:40 -080098
Jack F Vogel67701ae2005-05-01 08:58:48 -070099 printk(KERN_INFO "Testing NMI watchdog ... ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100101#ifdef CONFIG_SMP
Eric W. Biederman29b70082005-10-30 14:59:40 -0800102 if (nmi_watchdog == NMI_LOCAL_APIC)
103 smp_call_function(nmi_cpu_busy, (void *)&endflag, 0, 0);
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100104#endif
Eric W. Biederman29b70082005-10-30 14:59:40 -0800105
KAMEZAWA Hiroyukic8912592006-03-28 01:56:39 -0800106 for_each_possible_cpu(cpu)
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100107 prev_nmi_count[cpu] = nmi_count(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 local_irq_enable();
Andi Kleen0fb2ebf2007-04-02 12:14:12 +0200109 mdelay((20*1000)/nmi_hz); // wait 20 ticks
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
KAMEZAWA Hiroyukic8912592006-03-28 01:56:39 -0800111 for_each_possible_cpu(cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#ifdef CONFIG_SMP
113 /* Check cpu_callin_map here because that is set
114 after the timer is started. */
115 if (!cpu_isset(cpu, cpu_callin_map))
116 continue;
117#endif
Andi Kleen09198e62007-05-02 19:27:20 +0200118 if (!per_cpu(wd_enabled, cpu))
Don Zickusb7471c62006-09-26 10:52:26 +0200119 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 if (nmi_count(cpu) - prev_nmi_count[cpu] <= 5) {
Don Zickus75bc1222007-12-04 17:19:07 +0100121 printk(KERN_WARNING "WARNING: CPU#%d: NMI "
122 "appears to be stuck (%d->%d)!\n",
Eric W. Biederman29b70082005-10-30 14:59:40 -0800123 cpu,
124 prev_nmi_count[cpu],
125 nmi_count(cpu));
Andi Kleen09198e62007-05-02 19:27:20 +0200126 per_cpu(wd_enabled, cpu) = 0;
Don Zickusb7471c62006-09-26 10:52:26 +0200127 atomic_dec(&nmi_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 }
129 }
Daniel Walkerd9586542007-09-06 16:59:54 +0200130 endflag = 1;
Don Zickusb7471c62006-09-26 10:52:26 +0200131 if (!atomic_read(&nmi_active)) {
132 kfree(prev_nmi_count);
133 atomic_set(&nmi_active, -1);
Ingo Molnar6e908942008-03-21 14:32:36 +0100134 goto error;
Don Zickusb7471c62006-09-26 10:52:26 +0200135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 printk("OK.\n");
137
138 /* now that we know it works we can reduce NMI frequency to
139 something more reasonable; makes a difference in some configs */
Andi Kleen09198e62007-05-02 19:27:20 +0200140 if (nmi_watchdog == NMI_LOCAL_APIC)
141 nmi_hz = lapic_adjust_nmi_hz(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Eric W. Biederman29b70082005-10-30 14:59:40 -0800143 kfree(prev_nmi_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 return 0;
Ingo Molnar6e908942008-03-21 14:32:36 +0100145error:
146 timer_ack = !cpu_has_tsc;
147
148 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
151static int __init setup_nmi_watchdog(char *str)
152{
153 int nmi;
154
Cyrill Gorcunovd1b946b2008-05-24 19:36:34 +0400155 if (!strncmp(str, "panic", 5)) {
156 panic_on_timeout = 1;
157 str = strchr(str, ',');
158 if (!str)
159 return 1;
160 ++str;
161 }
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 get_option(&str, &nmi);
164
Don Zickusb7471c62006-09-26 10:52:26 +0200165 if ((nmi >= NMI_INVALID) || (nmi < NMI_NONE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 return 0;
Venkatesh Pallipadi58d9ce7d2007-01-22 20:40:34 -0800167
Don Zickusb7471c62006-09-26 10:52:26 +0200168 nmi_watchdog = nmi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 return 1;
170}
171
172__setup("nmi_watchdog=", setup_nmi_watchdog);
173
Don Zickusb7471c62006-09-26 10:52:26 +0200174
Andi Kleen09198e62007-05-02 19:27:20 +0200175/* Suspend/resume support */
Ingo Molnar5d0e6002007-02-13 13:26:24 +0100176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177#ifdef CONFIG_PM
178
179static int nmi_pm_active; /* nmi_active before suspend */
180
Pavel Machek438510f2005-04-16 15:25:24 -0700181static int lapic_nmi_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Shaohua Li4038f902006-09-26 10:52:27 +0200183 /* only CPU0 goes here, other CPUs should be offline */
Don Zickusb7471c62006-09-26 10:52:26 +0200184 nmi_pm_active = atomic_read(&nmi_active);
Shaohua Li4038f902006-09-26 10:52:27 +0200185 stop_apic_nmi_watchdog(NULL);
186 BUG_ON(atomic_read(&nmi_active) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 return 0;
188}
189
190static int lapic_nmi_resume(struct sys_device *dev)
191{
Shaohua Li4038f902006-09-26 10:52:27 +0200192 /* only CPU0 goes here, other CPUs should be offline */
193 if (nmi_pm_active > 0) {
194 setup_apic_nmi_watchdog(NULL);
195 touch_nmi_watchdog();
196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 return 0;
198}
199
200
201static struct sysdev_class nmi_sysclass = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +0100202 .name = "lapic_nmi",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 .resume = lapic_nmi_resume,
204 .suspend = lapic_nmi_suspend,
205};
206
207static struct sys_device device_lapic_nmi = {
208 .id = 0,
209 .cls = &nmi_sysclass,
210};
211
212static int __init init_lapic_nmi_sysfs(void)
213{
214 int error;
215
Don Zickusb7471c62006-09-26 10:52:26 +0200216 /* should really be a BUG_ON but b/c this is an
217 * init call, it just doesn't work. -dcz
218 */
219 if (nmi_watchdog != NMI_LOCAL_APIC)
220 return 0;
221
Andi Kleen09198e62007-05-02 19:27:20 +0200222 if (atomic_read(&nmi_active) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return 0;
224
225 error = sysdev_class_register(&nmi_sysclass);
226 if (!error)
227 error = sysdev_register(&device_lapic_nmi);
228 return error;
229}
230/* must come after the local APIC's device_initcall() */
231late_initcall(init_lapic_nmi_sysfs);
232
233#endif /* CONFIG_PM */
234
Andi Kleen09198e62007-05-02 19:27:20 +0200235static void __acpi_nmi_enable(void *__unused)
236{
237 apic_write_around(APIC_LVT0, APIC_DM_NMI);
238}
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240/*
Andi Kleen09198e62007-05-02 19:27:20 +0200241 * Enable timer based NMIs on all CPUs:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 */
Andi Kleen09198e62007-05-02 19:27:20 +0200243void acpi_nmi_enable(void)
Jan Beulich7fbb4f62005-06-23 00:08:23 -0700244{
Andi Kleen09198e62007-05-02 19:27:20 +0200245 if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC)
246 on_each_cpu(__acpi_nmi_enable, NULL, 0, 1);
Jan Beulich7fbb4f62005-06-23 00:08:23 -0700247}
248
Andi Kleen09198e62007-05-02 19:27:20 +0200249static void __acpi_nmi_disable(void *__unused)
Venkatesh Pallipadi90ce4bc2007-02-13 13:26:22 +0100250{
Cyrill Gorcunovad63ba12008-05-24 19:36:36 +0400251 apic_write_around(APIC_LVT0, APIC_DM_NMI | APIC_LVT_MASKED);
Venkatesh Pallipadi90ce4bc2007-02-13 13:26:22 +0100252}
253
Andi Kleen09198e62007-05-02 19:27:20 +0200254/*
255 * Disable timer based NMIs on all CPUs:
256 */
257void acpi_nmi_disable(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -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_disable, NULL, 0, 1);
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +0200261}
262
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100263void setup_apic_nmi_watchdog(void *unused)
Don Zickusb7471c62006-09-26 10:52:26 +0200264{
Andi Kleen09198e62007-05-02 19:27:20 +0200265 if (__get_cpu_var(wd_enabled))
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100266 return;
Shaohua Li4038f902006-09-26 10:52:27 +0200267
268 /* cheap hack to support suspend/resume */
269 /* if cpu0 is not active neither should the other cpus */
270 if ((smp_processor_id() != 0) && (atomic_read(&nmi_active) <= 0))
271 return;
272
Andi Kleen09198e62007-05-02 19:27:20 +0200273 switch (nmi_watchdog) {
274 case NMI_LOCAL_APIC:
275 __get_cpu_var(wd_enabled) = 1; /* enable it before to avoid race with handler */
276 if (lapic_watchdog_init(nmi_hz) < 0) {
277 __get_cpu_var(wd_enabled) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 return;
279 }
Andi Kleen09198e62007-05-02 19:27:20 +0200280 /* FALL THROUGH */
281 case NMI_IO_APIC:
282 __get_cpu_var(wd_enabled) = 1;
283 atomic_inc(&nmi_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
Don Zickusb7471c62006-09-26 10:52:26 +0200285}
286
Shaohua Li4038f902006-09-26 10:52:27 +0200287void stop_apic_nmi_watchdog(void *unused)
Don Zickusb7471c62006-09-26 10:52:26 +0200288{
289 /* only support LOCAL and IO APICs for now */
290 if ((nmi_watchdog != NMI_LOCAL_APIC) &&
291 (nmi_watchdog != NMI_IO_APIC))
292 return;
Andi Kleen09198e62007-05-02 19:27:20 +0200293 if (__get_cpu_var(wd_enabled) == 0)
Shaohua Li4038f902006-09-26 10:52:27 +0200294 return;
Andi Kleen09198e62007-05-02 19:27:20 +0200295 if (nmi_watchdog == NMI_LOCAL_APIC)
296 lapic_watchdog_stop();
297 __get_cpu_var(wd_enabled) = 0;
Don Zickusb7471c62006-09-26 10:52:26 +0200298 atomic_dec(&nmi_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
301/*
302 * the best way to detect whether a CPU has a 'hard lockup' problem
303 * is to check it's local APIC timer IRQ counts. If they are not
304 * changing then that CPU has some problem.
305 *
306 * as these watchdog NMI IRQs are generated on every CPU, we only
307 * have to check the current processor.
308 *
309 * since NMIs don't listen to _any_ locks, we have to be extremely
310 * careful not to rely on unsafe variables. The printk might lock
311 * up though, so we have to break up any console locks first ...
312 * [when there will be more tty-related locks, break them up
313 * here too!]
314 */
315
Hiroshi Shimamotof7849462008-05-02 16:45:08 -0700316static DEFINE_PER_CPU(unsigned, last_irq_sum);
317static DEFINE_PER_CPU(local_t, alert_counter);
318static DEFINE_PER_CPU(int, nmi_touch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Andrew Mortonf2890252007-07-17 04:03:57 -0700320void touch_nmi_watchdog(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
Jan Beulichc6ea3962006-12-07 02:14:09 +0100322 if (nmi_watchdog > 0) {
323 unsigned cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Jan Beulichc6ea3962006-12-07 02:14:09 +0100325 /*
Hiroshi Shimamotof7849462008-05-02 16:45:08 -0700326 * Tell other CPUs to reset their alert counters. We cannot
327 * do it ourselves because the alert count increase is not
328 * atomic.
Jan Beulichc6ea3962006-12-07 02:14:09 +0100329 */
Andrew Mortonf2890252007-07-17 04:03:57 -0700330 for_each_present_cpu(cpu) {
Hiroshi Shimamotof7849462008-05-02 16:45:08 -0700331 if (per_cpu(nmi_touch, cpu) != 1)
332 per_cpu(nmi_touch, cpu) = 1;
Andrew Mortonf2890252007-07-17 04:03:57 -0700333 }
Jan Beulichc6ea3962006-12-07 02:14:09 +0100334 }
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700335
336 /*
337 * Tickle the softlockup detector too:
338 */
339 touch_softlockup_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
Michal Schmidt1e862402006-07-30 03:03:29 -0700341EXPORT_SYMBOL(touch_nmi_watchdog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Steven Rostedt5deb45e2008-04-19 19:19:55 +0200343notrace __kprobes int
344nmi_watchdog_tick(struct pt_regs *regs, unsigned reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
346
347 /*
348 * Since current_thread_info()-> is always on the stack, and we
349 * always switch the stack NMI-atomically, it's safe to use
350 * smp_processor_id().
351 */
Jesper Juhlb791cce2006-03-28 01:56:52 -0800352 unsigned int sum;
Don Zickusb7471c62006-09-26 10:52:26 +0200353 int touched = 0;
Jesper Juhlb791cce2006-03-28 01:56:52 -0800354 int cpu = smp_processor_id();
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100355 int rc = 0;
Don Zickusb7471c62006-09-26 10:52:26 +0200356
357 /* check for other users first */
358 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT)
359 == NOTIFY_STOP) {
Don Zickus3adbbcc2006-09-26 10:52:26 +0200360 rc = 1;
Don Zickusb7471c62006-09-26 10:52:26 +0200361 touched = 1;
362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Andrew Mortonbb81a092006-12-07 02:14:01 +0100364 if (cpu_isset(cpu, backtrace_mask)) {
365 static DEFINE_SPINLOCK(lock); /* Serialise the printks */
366
367 spin_lock(&lock);
368 printk("NMI backtrace for cpu %d\n", cpu);
369 dump_stack();
370 spin_unlock(&lock);
371 cpu_clear(cpu, backtrace_mask);
372 }
373
Thomas Gleixnerf8b50352007-02-16 01:28:09 -0800374 /*
375 * Take the local apic timer and PIT/HPET into account. We don't
376 * know which one is active, when we have highres/dyntick on
377 */
Thomas Gleixner3c9aea42007-10-12 23:04:06 +0200378 sum = per_cpu(irq_stat, cpu).apic_timer_irqs +
379 per_cpu(irq_stat, cpu).irq0_irqs;
Hiroshi Shimamotof7849462008-05-02 16:45:08 -0700380 if (__get_cpu_var(nmi_touch)) {
381 __get_cpu_var(nmi_touch) = 0;
382 touched = 1;
383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Thomas Gleixnerf8b50352007-02-16 01:28:09 -0800385 /* if the none of the timers isn't firing, this cpu isn't doing much */
Hiroshi Shimamotof7849462008-05-02 16:45:08 -0700386 if (!touched && __get_cpu_var(last_irq_sum) == sum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 /*
388 * Ayiee, looks like this CPU is stuck ...
389 * wait a few IRQs (5 seconds) before doing the oops ...
390 */
Hiroshi Shimamotof7849462008-05-02 16:45:08 -0700391 local_inc(&__get_cpu_var(alert_counter));
392 if (local_read(&__get_cpu_var(alert_counter)) == 5*nmi_hz)
George Anzinger748f2ed2005-09-03 15:56:48 -0700393 /*
394 * die_nmi will return ONLY if NOTIFY_STOP happens..
395 */
Cyrill Gorcunovddca03c2008-05-24 19:36:31 +0400396 die_nmi("BUG: NMI Watchdog detected LOCKUP",
Cyrill Gorcunovd1b946b2008-05-24 19:36:34 +0400397 regs, panic_on_timeout);
GOTO Masanorib884e252006-03-07 21:55:29 -0800398 } else {
Hiroshi Shimamotof7849462008-05-02 16:45:08 -0700399 __get_cpu_var(last_irq_sum) = sum;
400 local_set(&__get_cpu_var(alert_counter), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
Don Zickusb7471c62006-09-26 10:52:26 +0200402 /* see if the nmi watchdog went off */
Andi Kleen09198e62007-05-02 19:27:20 +0200403 if (!__get_cpu_var(wd_enabled))
404 return rc;
405 switch (nmi_watchdog) {
406 case NMI_LOCAL_APIC:
407 rc |= lapic_wd_event(nmi_hz);
408 break;
409 case NMI_IO_APIC:
410 /* don't know how to accurately check for this.
411 * just assume it was a watchdog timer interrupt
412 * This matches the old behaviour.
413 */
414 rc = 1;
415 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
Don Zickus3adbbcc2006-09-26 10:52:26 +0200417 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
420#ifdef CONFIG_SYSCTL
421
422static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu)
423{
424 unsigned char reason = get_nmi_reason();
425 char buf[64];
426
Don Zickus2fbe7b22006-09-26 10:52:27 +0200427 sprintf(buf, "NMI received for unknown reason %02x\n", reason);
Cyrill Gorcunovddca03c2008-05-24 19:36:31 +0400428 die_nmi(buf, regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return 0;
430}
431
Don Zickus407984f2006-09-26 10:52:27 +0200432/*
Don Zickuse33e89a2006-09-26 10:52:27 +0200433 * proc handler for /proc/sys/kernel/nmi
Don Zickus407984f2006-09-26 10:52:27 +0200434 */
435int proc_nmi_enabled(struct ctl_table *table, int write, struct file *file,
436 void __user *buffer, size_t *length, loff_t *ppos)
437{
438 int old_state;
439
440 nmi_watchdog_enabled = (atomic_read(&nmi_active) > 0) ? 1 : 0;
441 old_state = nmi_watchdog_enabled;
442 proc_dointvec(table, write, file, buffer, length, ppos);
443 if (!!old_state == !!nmi_watchdog_enabled)
444 return 0;
445
Daniel Gollub0328ece2007-08-15 02:40:35 +0200446 if (atomic_read(&nmi_active) < 0 || nmi_watchdog == NMI_DISABLED) {
Don Zickuse33e89a2006-09-26 10:52:27 +0200447 printk( KERN_WARNING "NMI watchdog is permanently disabled\n");
448 return -EIO;
Don Zickus407984f2006-09-26 10:52:27 +0200449 }
450
Cyrill Gorcunov4b82b272008-05-24 19:36:35 +0400451 /* if nmi_watchdog is not set yet, then set it */
452 nmi_watchdog_default();
Don Zickus407984f2006-09-26 10:52:27 +0200453
Don Zickuse33e89a2006-09-26 10:52:27 +0200454 if (nmi_watchdog == NMI_LOCAL_APIC) {
Don Zickus407984f2006-09-26 10:52:27 +0200455 if (nmi_watchdog_enabled)
456 enable_lapic_nmi_watchdog();
457 else
458 disable_lapic_nmi_watchdog();
Don Zickus407984f2006-09-26 10:52:27 +0200459 } else {
460 printk( KERN_WARNING
461 "NMI watchdog doesn't know what hardware to touch\n");
462 return -EIO;
463 }
464 return 0;
465}
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467#endif
468
Li Zefana062bae2008-02-03 15:40:30 +0800469int do_nmi_callback(struct pt_regs *regs, int cpu)
470{
471#ifdef CONFIG_SYSCTL
472 if (unknown_nmi_panic)
473 return unknown_nmi_panic_callback(regs, cpu);
474#endif
475 return 0;
476}
477
Andrew Mortonbb81a092006-12-07 02:14:01 +0100478void __trigger_all_cpu_backtrace(void)
479{
480 int i;
481
482 backtrace_mask = cpu_online_map;
483 /* Wait for up to 10 seconds for all CPUs to do the backtrace */
484 for (i = 0; i < 10 * 1000; i++) {
485 if (cpus_empty(backtrace_mask))
486 break;
487 mdelay(1);
488 }
489}
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491EXPORT_SYMBOL(nmi_active);
492EXPORT_SYMBOL(nmi_watchdog);