blob: c6d0777aaf90775f54b055de96fa6a91179f78f7 [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
Cyrill Gorcunov7c2ba832008-05-24 19:36:39 +0400111 for_each_online_cpu(cpu) {
Andi Kleen09198e62007-05-02 19:27:20 +0200112 if (!per_cpu(wd_enabled, cpu))
Don Zickusb7471c62006-09-26 10:52:26 +0200113 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 if (nmi_count(cpu) - prev_nmi_count[cpu] <= 5) {
Don Zickus75bc1222007-12-04 17:19:07 +0100115 printk(KERN_WARNING "WARNING: CPU#%d: NMI "
116 "appears to be stuck (%d->%d)!\n",
Eric W. Biederman29b70082005-10-30 14:59:40 -0800117 cpu,
118 prev_nmi_count[cpu],
119 nmi_count(cpu));
Andi Kleen09198e62007-05-02 19:27:20 +0200120 per_cpu(wd_enabled, cpu) = 0;
Don Zickusb7471c62006-09-26 10:52:26 +0200121 atomic_dec(&nmi_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 }
123 }
Daniel Walkerd9586542007-09-06 16:59:54 +0200124 endflag = 1;
Don Zickusb7471c62006-09-26 10:52:26 +0200125 if (!atomic_read(&nmi_active)) {
126 kfree(prev_nmi_count);
127 atomic_set(&nmi_active, -1);
Ingo Molnar6e908942008-03-21 14:32:36 +0100128 goto error;
Don Zickusb7471c62006-09-26 10:52:26 +0200129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 printk("OK.\n");
131
132 /* now that we know it works we can reduce NMI frequency to
133 something more reasonable; makes a difference in some configs */
Andi Kleen09198e62007-05-02 19:27:20 +0200134 if (nmi_watchdog == NMI_LOCAL_APIC)
135 nmi_hz = lapic_adjust_nmi_hz(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Eric W. Biederman29b70082005-10-30 14:59:40 -0800137 kfree(prev_nmi_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 return 0;
Ingo Molnar6e908942008-03-21 14:32:36 +0100139error:
140 timer_ack = !cpu_has_tsc;
141
142 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
145static int __init setup_nmi_watchdog(char *str)
146{
147 int nmi;
148
Cyrill Gorcunovd1b946b2008-05-24 19:36:34 +0400149 if (!strncmp(str, "panic", 5)) {
150 panic_on_timeout = 1;
151 str = strchr(str, ',');
152 if (!str)
153 return 1;
154 ++str;
155 }
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 get_option(&str, &nmi);
158
Don Zickusb7471c62006-09-26 10:52:26 +0200159 if ((nmi >= NMI_INVALID) || (nmi < NMI_NONE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 return 0;
Venkatesh Pallipadi58d9ce7d2007-01-22 20:40:34 -0800161
Don Zickusb7471c62006-09-26 10:52:26 +0200162 nmi_watchdog = nmi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 return 1;
164}
165
166__setup("nmi_watchdog=", setup_nmi_watchdog);
167
Don Zickusb7471c62006-09-26 10:52:26 +0200168
Andi Kleen09198e62007-05-02 19:27:20 +0200169/* Suspend/resume support */
Ingo Molnar5d0e6002007-02-13 13:26:24 +0100170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171#ifdef CONFIG_PM
172
173static int nmi_pm_active; /* nmi_active before suspend */
174
Pavel Machek438510f2005-04-16 15:25:24 -0700175static int lapic_nmi_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Shaohua Li4038f902006-09-26 10:52:27 +0200177 /* only CPU0 goes here, other CPUs should be offline */
Don Zickusb7471c62006-09-26 10:52:26 +0200178 nmi_pm_active = atomic_read(&nmi_active);
Shaohua Li4038f902006-09-26 10:52:27 +0200179 stop_apic_nmi_watchdog(NULL);
180 BUG_ON(atomic_read(&nmi_active) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 return 0;
182}
183
184static int lapic_nmi_resume(struct sys_device *dev)
185{
Shaohua Li4038f902006-09-26 10:52:27 +0200186 /* only CPU0 goes here, other CPUs should be offline */
187 if (nmi_pm_active > 0) {
188 setup_apic_nmi_watchdog(NULL);
189 touch_nmi_watchdog();
190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return 0;
192}
193
194
195static struct sysdev_class nmi_sysclass = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +0100196 .name = "lapic_nmi",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 .resume = lapic_nmi_resume,
198 .suspend = lapic_nmi_suspend,
199};
200
201static struct sys_device device_lapic_nmi = {
202 .id = 0,
203 .cls = &nmi_sysclass,
204};
205
206static int __init init_lapic_nmi_sysfs(void)
207{
208 int error;
209
Don Zickusb7471c62006-09-26 10:52:26 +0200210 /* should really be a BUG_ON but b/c this is an
211 * init call, it just doesn't work. -dcz
212 */
213 if (nmi_watchdog != NMI_LOCAL_APIC)
214 return 0;
215
Andi Kleen09198e62007-05-02 19:27:20 +0200216 if (atomic_read(&nmi_active) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return 0;
218
219 error = sysdev_class_register(&nmi_sysclass);
220 if (!error)
221 error = sysdev_register(&device_lapic_nmi);
222 return error;
223}
224/* must come after the local APIC's device_initcall() */
225late_initcall(init_lapic_nmi_sysfs);
226
227#endif /* CONFIG_PM */
228
Andi Kleen09198e62007-05-02 19:27:20 +0200229static void __acpi_nmi_enable(void *__unused)
230{
231 apic_write_around(APIC_LVT0, APIC_DM_NMI);
232}
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234/*
Andi Kleen09198e62007-05-02 19:27:20 +0200235 * Enable timer based NMIs on all CPUs:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 */
Andi Kleen09198e62007-05-02 19:27:20 +0200237void acpi_nmi_enable(void)
Jan Beulich7fbb4f62005-06-23 00:08:23 -0700238{
Andi Kleen09198e62007-05-02 19:27:20 +0200239 if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC)
240 on_each_cpu(__acpi_nmi_enable, NULL, 0, 1);
Jan Beulich7fbb4f62005-06-23 00:08:23 -0700241}
242
Andi Kleen09198e62007-05-02 19:27:20 +0200243static void __acpi_nmi_disable(void *__unused)
Venkatesh Pallipadi90ce4bc2007-02-13 13:26:22 +0100244{
Cyrill Gorcunovad63ba12008-05-24 19:36:36 +0400245 apic_write_around(APIC_LVT0, APIC_DM_NMI | APIC_LVT_MASKED);
Venkatesh Pallipadi90ce4bc2007-02-13 13:26:22 +0100246}
247
Andi Kleen09198e62007-05-02 19:27:20 +0200248/*
249 * Disable timer based NMIs on all CPUs:
250 */
251void acpi_nmi_disable(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
Andi Kleen09198e62007-05-02 19:27:20 +0200253 if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC)
254 on_each_cpu(__acpi_nmi_disable, NULL, 0, 1);
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +0200255}
256
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100257void setup_apic_nmi_watchdog(void *unused)
Don Zickusb7471c62006-09-26 10:52:26 +0200258{
Andi Kleen09198e62007-05-02 19:27:20 +0200259 if (__get_cpu_var(wd_enabled))
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100260 return;
Shaohua Li4038f902006-09-26 10:52:27 +0200261
262 /* cheap hack to support suspend/resume */
263 /* if cpu0 is not active neither should the other cpus */
264 if ((smp_processor_id() != 0) && (atomic_read(&nmi_active) <= 0))
265 return;
266
Andi Kleen09198e62007-05-02 19:27:20 +0200267 switch (nmi_watchdog) {
268 case NMI_LOCAL_APIC:
269 __get_cpu_var(wd_enabled) = 1; /* enable it before to avoid race with handler */
270 if (lapic_watchdog_init(nmi_hz) < 0) {
271 __get_cpu_var(wd_enabled) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 return;
273 }
Andi Kleen09198e62007-05-02 19:27:20 +0200274 /* FALL THROUGH */
275 case NMI_IO_APIC:
276 __get_cpu_var(wd_enabled) = 1;
277 atomic_inc(&nmi_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 }
Don Zickusb7471c62006-09-26 10:52:26 +0200279}
280
Shaohua Li4038f902006-09-26 10:52:27 +0200281void stop_apic_nmi_watchdog(void *unused)
Don Zickusb7471c62006-09-26 10:52:26 +0200282{
283 /* only support LOCAL and IO APICs for now */
284 if ((nmi_watchdog != NMI_LOCAL_APIC) &&
285 (nmi_watchdog != NMI_IO_APIC))
286 return;
Andi Kleen09198e62007-05-02 19:27:20 +0200287 if (__get_cpu_var(wd_enabled) == 0)
Shaohua Li4038f902006-09-26 10:52:27 +0200288 return;
Andi Kleen09198e62007-05-02 19:27:20 +0200289 if (nmi_watchdog == NMI_LOCAL_APIC)
290 lapic_watchdog_stop();
291 __get_cpu_var(wd_enabled) = 0;
Don Zickusb7471c62006-09-26 10:52:26 +0200292 atomic_dec(&nmi_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293}
294
295/*
296 * the best way to detect whether a CPU has a 'hard lockup' problem
297 * is to check it's local APIC timer IRQ counts. If they are not
298 * changing then that CPU has some problem.
299 *
300 * as these watchdog NMI IRQs are generated on every CPU, we only
301 * have to check the current processor.
302 *
303 * since NMIs don't listen to _any_ locks, we have to be extremely
304 * careful not to rely on unsafe variables. The printk might lock
305 * up though, so we have to break up any console locks first ...
306 * [when there will be more tty-related locks, break them up
307 * here too!]
308 */
309
Hiroshi Shimamotof7849462008-05-02 16:45:08 -0700310static DEFINE_PER_CPU(unsigned, last_irq_sum);
311static DEFINE_PER_CPU(local_t, alert_counter);
312static DEFINE_PER_CPU(int, nmi_touch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Andrew Mortonf2890252007-07-17 04:03:57 -0700314void touch_nmi_watchdog(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Jan Beulichc6ea3962006-12-07 02:14:09 +0100316 if (nmi_watchdog > 0) {
317 unsigned cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Jan Beulichc6ea3962006-12-07 02:14:09 +0100319 /*
Hiroshi Shimamotof7849462008-05-02 16:45:08 -0700320 * Tell other CPUs to reset their alert counters. We cannot
321 * do it ourselves because the alert count increase is not
322 * atomic.
Jan Beulichc6ea3962006-12-07 02:14:09 +0100323 */
Andrew Mortonf2890252007-07-17 04:03:57 -0700324 for_each_present_cpu(cpu) {
Hiroshi Shimamotof7849462008-05-02 16:45:08 -0700325 if (per_cpu(nmi_touch, cpu) != 1)
326 per_cpu(nmi_touch, cpu) = 1;
Andrew Mortonf2890252007-07-17 04:03:57 -0700327 }
Jan Beulichc6ea3962006-12-07 02:14:09 +0100328 }
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700329
330 /*
331 * Tickle the softlockup detector too:
332 */
333 touch_softlockup_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
Michal Schmidt1e862402006-07-30 03:03:29 -0700335EXPORT_SYMBOL(touch_nmi_watchdog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Steven Rostedt5deb45e2008-04-19 19:19:55 +0200337notrace __kprobes int
338nmi_watchdog_tick(struct pt_regs *regs, unsigned reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
340
341 /*
342 * Since current_thread_info()-> is always on the stack, and we
343 * always switch the stack NMI-atomically, it's safe to use
344 * smp_processor_id().
345 */
Jesper Juhlb791cce2006-03-28 01:56:52 -0800346 unsigned int sum;
Don Zickusb7471c62006-09-26 10:52:26 +0200347 int touched = 0;
Jesper Juhlb791cce2006-03-28 01:56:52 -0800348 int cpu = smp_processor_id();
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100349 int rc = 0;
Don Zickusb7471c62006-09-26 10:52:26 +0200350
351 /* check for other users first */
352 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT)
353 == NOTIFY_STOP) {
Don Zickus3adbbcce2006-09-26 10:52:26 +0200354 rc = 1;
Don Zickusb7471c62006-09-26 10:52:26 +0200355 touched = 1;
356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Andrew Mortonbb81a092006-12-07 02:14:01 +0100358 if (cpu_isset(cpu, backtrace_mask)) {
359 static DEFINE_SPINLOCK(lock); /* Serialise the printks */
360
361 spin_lock(&lock);
362 printk("NMI backtrace for cpu %d\n", cpu);
363 dump_stack();
364 spin_unlock(&lock);
365 cpu_clear(cpu, backtrace_mask);
366 }
367
Thomas Gleixnerf8b50352007-02-16 01:28:09 -0800368 /*
369 * Take the local apic timer and PIT/HPET into account. We don't
370 * know which one is active, when we have highres/dyntick on
371 */
Thomas Gleixner3c9aea42007-10-12 23:04:06 +0200372 sum = per_cpu(irq_stat, cpu).apic_timer_irqs +
373 per_cpu(irq_stat, cpu).irq0_irqs;
Hiroshi Shimamotof7849462008-05-02 16:45:08 -0700374 if (__get_cpu_var(nmi_touch)) {
375 __get_cpu_var(nmi_touch) = 0;
376 touched = 1;
377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Thomas Gleixnerf8b50352007-02-16 01:28:09 -0800379 /* if the none of the timers isn't firing, this cpu isn't doing much */
Hiroshi Shimamotof7849462008-05-02 16:45:08 -0700380 if (!touched && __get_cpu_var(last_irq_sum) == sum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 /*
382 * Ayiee, looks like this CPU is stuck ...
383 * wait a few IRQs (5 seconds) before doing the oops ...
384 */
Hiroshi Shimamotof7849462008-05-02 16:45:08 -0700385 local_inc(&__get_cpu_var(alert_counter));
386 if (local_read(&__get_cpu_var(alert_counter)) == 5*nmi_hz)
George Anzinger748f2ed2005-09-03 15:56:48 -0700387 /*
388 * die_nmi will return ONLY if NOTIFY_STOP happens..
389 */
Cyrill Gorcunovddca03c2008-05-24 19:36:31 +0400390 die_nmi("BUG: NMI Watchdog detected LOCKUP",
Cyrill Gorcunovd1b946b2008-05-24 19:36:34 +0400391 regs, panic_on_timeout);
GOTO Masanorib884e252006-03-07 21:55:29 -0800392 } else {
Hiroshi Shimamotof7849462008-05-02 16:45:08 -0700393 __get_cpu_var(last_irq_sum) = sum;
394 local_set(&__get_cpu_var(alert_counter), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
Don Zickusb7471c62006-09-26 10:52:26 +0200396 /* see if the nmi watchdog went off */
Andi Kleen09198e62007-05-02 19:27:20 +0200397 if (!__get_cpu_var(wd_enabled))
398 return rc;
399 switch (nmi_watchdog) {
400 case NMI_LOCAL_APIC:
401 rc |= lapic_wd_event(nmi_hz);
402 break;
403 case NMI_IO_APIC:
404 /* don't know how to accurately check for this.
405 * just assume it was a watchdog timer interrupt
406 * This matches the old behaviour.
407 */
408 rc = 1;
409 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
Don Zickus3adbbcce2006-09-26 10:52:26 +0200411 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
413
414#ifdef CONFIG_SYSCTL
415
416static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu)
417{
418 unsigned char reason = get_nmi_reason();
419 char buf[64];
420
Don Zickus2fbe7b22006-09-26 10:52:27 +0200421 sprintf(buf, "NMI received for unknown reason %02x\n", reason);
Cyrill Gorcunov6c8decd2008-05-24 19:36:37 +0400422 die_nmi(buf, regs, 1); /* Always panic here */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return 0;
424}
425
Don Zickus407984f2006-09-26 10:52:27 +0200426/*
Don Zickuse33e89a2006-09-26 10:52:27 +0200427 * proc handler for /proc/sys/kernel/nmi
Don Zickus407984f2006-09-26 10:52:27 +0200428 */
429int proc_nmi_enabled(struct ctl_table *table, int write, struct file *file,
430 void __user *buffer, size_t *length, loff_t *ppos)
431{
432 int old_state;
433
434 nmi_watchdog_enabled = (atomic_read(&nmi_active) > 0) ? 1 : 0;
435 old_state = nmi_watchdog_enabled;
436 proc_dointvec(table, write, file, buffer, length, ppos);
437 if (!!old_state == !!nmi_watchdog_enabled)
438 return 0;
439
Daniel Gollub0328ece2007-08-15 02:40:35 +0200440 if (atomic_read(&nmi_active) < 0 || nmi_watchdog == NMI_DISABLED) {
Don Zickuse33e89a2006-09-26 10:52:27 +0200441 printk( KERN_WARNING "NMI watchdog is permanently disabled\n");
442 return -EIO;
Don Zickus407984f2006-09-26 10:52:27 +0200443 }
444
Cyrill Gorcunov4b82b272008-05-24 19:36:35 +0400445 /* if nmi_watchdog is not set yet, then set it */
446 nmi_watchdog_default();
Don Zickus407984f2006-09-26 10:52:27 +0200447
Don Zickuse33e89a2006-09-26 10:52:27 +0200448 if (nmi_watchdog == NMI_LOCAL_APIC) {
Don Zickus407984f2006-09-26 10:52:27 +0200449 if (nmi_watchdog_enabled)
450 enable_lapic_nmi_watchdog();
451 else
452 disable_lapic_nmi_watchdog();
Don Zickus407984f2006-09-26 10:52:27 +0200453 } else {
454 printk( KERN_WARNING
455 "NMI watchdog doesn't know what hardware to touch\n");
456 return -EIO;
457 }
458 return 0;
459}
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461#endif
462
Li Zefana062bae2008-02-03 15:40:30 +0800463int do_nmi_callback(struct pt_regs *regs, int cpu)
464{
465#ifdef CONFIG_SYSCTL
466 if (unknown_nmi_panic)
467 return unknown_nmi_panic_callback(regs, cpu);
468#endif
469 return 0;
470}
471
Andrew Mortonbb81a092006-12-07 02:14:01 +0100472void __trigger_all_cpu_backtrace(void)
473{
474 int i;
475
476 backtrace_mask = cpu_online_map;
477 /* Wait for up to 10 seconds for all CPUs to do the backtrace */
478 for (i = 0; i < 10 * 1000; i++) {
479 if (cpus_empty(backtrace_mask))
480 break;
481 mdelay(1);
482 }
483}
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485EXPORT_SYMBOL(nmi_active);
486EXPORT_SYMBOL(nmi_watchdog);