blob: 4437fe1edabcbb565e556b4b1d3f5e407deddbe3 [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
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +010054#ifdef CONFIG_SMP
Eric W. Biederman29b70082005-10-30 14:59:40 -080055/* The performance counters used by NMI_LOCAL_APIC don't trigger when
56 * the CPU is idle. To make sure the NMI watchdog really ticks on all
57 * CPUs during the test make them busy.
58 */
59static __init void nmi_cpu_busy(void *data)
60{
Ingo Molnar366c7f52006-07-03 00:25:25 -070061 local_irq_enable_in_hardirq();
Eric W. Biederman29b70082005-10-30 14:59:40 -080062 /* Intentionally don't use cpu_relax here. This is
63 to make sure that the performance counter really ticks,
64 even if there is a simulator or similar that catches the
65 pause instruction. On a real HT machine this is fine because
66 all other CPUs are busy with "useless" delay loops and don't
67 care if they get somewhat less cycles. */
Ravikiran G Thirumalai92715e22006-12-09 21:33:35 +010068 while (endflag == 0)
69 mb();
Ingo Molnar04920072007-11-09 22:39:38 +010070}
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +010071#endif
Eric W. Biederman29b70082005-10-30 14:59:40 -080072
Glauber de Oliveira Costa6d60cd52008-03-19 14:25:36 -030073int __init check_nmi_watchdog(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Eric W. Biederman29b70082005-10-30 14:59:40 -080075 unsigned int *prev_nmi_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 int cpu;
77
Daniel Gollub0328ece2007-08-15 02:40:35 +020078 if ((nmi_watchdog == NMI_NONE) || (nmi_watchdog == NMI_DISABLED))
Don Zickusb7471c62006-09-26 10:52:26 +020079 return 0;
80
81 if (!atomic_read(&nmi_active))
Jack F Vogel67701ae2005-05-01 08:58:48 -070082 return 0;
83
Eric W. Biederman29b70082005-10-30 14:59:40 -080084 prev_nmi_count = kmalloc(NR_CPUS * sizeof(int), GFP_KERNEL);
85 if (!prev_nmi_count)
Ingo Molnar6e908942008-03-21 14:32:36 +010086 goto error;
Eric W. Biederman29b70082005-10-30 14:59:40 -080087
Jack F Vogel67701ae2005-05-01 08:58:48 -070088 printk(KERN_INFO "Testing NMI watchdog ... ");
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +010090#ifdef CONFIG_SMP
Eric W. Biederman29b70082005-10-30 14:59:40 -080091 if (nmi_watchdog == NMI_LOCAL_APIC)
92 smp_call_function(nmi_cpu_busy, (void *)&endflag, 0, 0);
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +010093#endif
Eric W. Biederman29b70082005-10-30 14:59:40 -080094
KAMEZAWA Hiroyukic8912592006-03-28 01:56:39 -080095 for_each_possible_cpu(cpu)
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +010096 prev_nmi_count[cpu] = nmi_count(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 local_irq_enable();
Andi Kleen0fb2ebf2007-04-02 12:14:12 +020098 mdelay((20*1000)/nmi_hz); // wait 20 ticks
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
KAMEZAWA Hiroyukic8912592006-03-28 01:56:39 -0800100 for_each_possible_cpu(cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#ifdef CONFIG_SMP
102 /* Check cpu_callin_map here because that is set
103 after the timer is started. */
104 if (!cpu_isset(cpu, cpu_callin_map))
105 continue;
106#endif
Andi Kleen09198e62007-05-02 19:27:20 +0200107 if (!per_cpu(wd_enabled, cpu))
Don Zickusb7471c62006-09-26 10:52:26 +0200108 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 if (nmi_count(cpu) - prev_nmi_count[cpu] <= 5) {
Don Zickus75bc1222007-12-04 17:19:07 +0100110 printk(KERN_WARNING "WARNING: CPU#%d: NMI "
111 "appears to be stuck (%d->%d)!\n",
Eric W. Biederman29b70082005-10-30 14:59:40 -0800112 cpu,
113 prev_nmi_count[cpu],
114 nmi_count(cpu));
Andi Kleen09198e62007-05-02 19:27:20 +0200115 per_cpu(wd_enabled, cpu) = 0;
Don Zickusb7471c62006-09-26 10:52:26 +0200116 atomic_dec(&nmi_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 }
118 }
Daniel Walkerd9586542007-09-06 16:59:54 +0200119 endflag = 1;
Don Zickusb7471c62006-09-26 10:52:26 +0200120 if (!atomic_read(&nmi_active)) {
121 kfree(prev_nmi_count);
122 atomic_set(&nmi_active, -1);
Ingo Molnar6e908942008-03-21 14:32:36 +0100123 goto error;
Don Zickusb7471c62006-09-26 10:52:26 +0200124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 printk("OK.\n");
126
127 /* now that we know it works we can reduce NMI frequency to
128 something more reasonable; makes a difference in some configs */
Andi Kleen09198e62007-05-02 19:27:20 +0200129 if (nmi_watchdog == NMI_LOCAL_APIC)
130 nmi_hz = lapic_adjust_nmi_hz(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Eric W. Biederman29b70082005-10-30 14:59:40 -0800132 kfree(prev_nmi_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 return 0;
Ingo Molnar6e908942008-03-21 14:32:36 +0100134error:
135 timer_ack = !cpu_has_tsc;
136
137 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
140static int __init setup_nmi_watchdog(char *str)
141{
142 int nmi;
143
Cyrill Gorcunovd1b946b2008-05-24 19:36:34 +0400144 if (!strncmp(str, "panic", 5)) {
145 panic_on_timeout = 1;
146 str = strchr(str, ',');
147 if (!str)
148 return 1;
149 ++str;
150 }
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 get_option(&str, &nmi);
153
Don Zickusb7471c62006-09-26 10:52:26 +0200154 if ((nmi >= NMI_INVALID) || (nmi < NMI_NONE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 return 0;
Venkatesh Pallipadi58d9ce7d2007-01-22 20:40:34 -0800156
Don Zickusb7471c62006-09-26 10:52:26 +0200157 nmi_watchdog = nmi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 return 1;
159}
160
161__setup("nmi_watchdog=", setup_nmi_watchdog);
162
Don Zickusb7471c62006-09-26 10:52:26 +0200163
Andi Kleen09198e62007-05-02 19:27:20 +0200164/* Suspend/resume support */
Ingo Molnar5d0e6002007-02-13 13:26:24 +0100165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166#ifdef CONFIG_PM
167
168static int nmi_pm_active; /* nmi_active before suspend */
169
Pavel Machek438510f2005-04-16 15:25:24 -0700170static int lapic_nmi_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
Shaohua Li4038f902006-09-26 10:52:27 +0200172 /* only CPU0 goes here, other CPUs should be offline */
Don Zickusb7471c62006-09-26 10:52:26 +0200173 nmi_pm_active = atomic_read(&nmi_active);
Shaohua Li4038f902006-09-26 10:52:27 +0200174 stop_apic_nmi_watchdog(NULL);
175 BUG_ON(atomic_read(&nmi_active) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 return 0;
177}
178
179static int lapic_nmi_resume(struct sys_device *dev)
180{
Shaohua Li4038f902006-09-26 10:52:27 +0200181 /* only CPU0 goes here, other CPUs should be offline */
182 if (nmi_pm_active > 0) {
183 setup_apic_nmi_watchdog(NULL);
184 touch_nmi_watchdog();
185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return 0;
187}
188
189
190static struct sysdev_class nmi_sysclass = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +0100191 .name = "lapic_nmi",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 .resume = lapic_nmi_resume,
193 .suspend = lapic_nmi_suspend,
194};
195
196static struct sys_device device_lapic_nmi = {
197 .id = 0,
198 .cls = &nmi_sysclass,
199};
200
201static int __init init_lapic_nmi_sysfs(void)
202{
203 int error;
204
Don Zickusb7471c62006-09-26 10:52:26 +0200205 /* should really be a BUG_ON but b/c this is an
206 * init call, it just doesn't work. -dcz
207 */
208 if (nmi_watchdog != NMI_LOCAL_APIC)
209 return 0;
210
Andi Kleen09198e62007-05-02 19:27:20 +0200211 if (atomic_read(&nmi_active) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 return 0;
213
214 error = sysdev_class_register(&nmi_sysclass);
215 if (!error)
216 error = sysdev_register(&device_lapic_nmi);
217 return error;
218}
219/* must come after the local APIC's device_initcall() */
220late_initcall(init_lapic_nmi_sysfs);
221
222#endif /* CONFIG_PM */
223
Andi Kleen09198e62007-05-02 19:27:20 +0200224static void __acpi_nmi_enable(void *__unused)
225{
226 apic_write_around(APIC_LVT0, APIC_DM_NMI);
227}
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229/*
Andi Kleen09198e62007-05-02 19:27:20 +0200230 * Enable timer based NMIs on all CPUs:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 */
Andi Kleen09198e62007-05-02 19:27:20 +0200232void acpi_nmi_enable(void)
Jan Beulich7fbb4f62005-06-23 00:08:23 -0700233{
Andi Kleen09198e62007-05-02 19:27:20 +0200234 if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC)
235 on_each_cpu(__acpi_nmi_enable, NULL, 0, 1);
Jan Beulich7fbb4f62005-06-23 00:08:23 -0700236}
237
Andi Kleen09198e62007-05-02 19:27:20 +0200238static void __acpi_nmi_disable(void *__unused)
Venkatesh Pallipadi90ce4bc2007-02-13 13:26:22 +0100239{
Andi Kleen09198e62007-05-02 19:27:20 +0200240 apic_write(APIC_LVT0, APIC_DM_NMI | APIC_LVT_MASKED);
Venkatesh Pallipadi90ce4bc2007-02-13 13:26:22 +0100241}
242
Andi Kleen09198e62007-05-02 19:27:20 +0200243/*
244 * Disable timer based NMIs on all CPUs:
245 */
246void acpi_nmi_disable(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Andi Kleen09198e62007-05-02 19:27:20 +0200248 if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC)
249 on_each_cpu(__acpi_nmi_disable, NULL, 0, 1);
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +0200250}
251
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100252void setup_apic_nmi_watchdog(void *unused)
Don Zickusb7471c62006-09-26 10:52:26 +0200253{
Andi Kleen09198e62007-05-02 19:27:20 +0200254 if (__get_cpu_var(wd_enabled))
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100255 return;
Shaohua Li4038f902006-09-26 10:52:27 +0200256
257 /* cheap hack to support suspend/resume */
258 /* if cpu0 is not active neither should the other cpus */
259 if ((smp_processor_id() != 0) && (atomic_read(&nmi_active) <= 0))
260 return;
261
Andi Kleen09198e62007-05-02 19:27:20 +0200262 switch (nmi_watchdog) {
263 case NMI_LOCAL_APIC:
264 __get_cpu_var(wd_enabled) = 1; /* enable it before to avoid race with handler */
265 if (lapic_watchdog_init(nmi_hz) < 0) {
266 __get_cpu_var(wd_enabled) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 return;
268 }
Andi Kleen09198e62007-05-02 19:27:20 +0200269 /* FALL THROUGH */
270 case NMI_IO_APIC:
271 __get_cpu_var(wd_enabled) = 1;
272 atomic_inc(&nmi_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 }
Don Zickusb7471c62006-09-26 10:52:26 +0200274}
275
Shaohua Li4038f902006-09-26 10:52:27 +0200276void stop_apic_nmi_watchdog(void *unused)
Don Zickusb7471c62006-09-26 10:52:26 +0200277{
278 /* only support LOCAL and IO APICs for now */
279 if ((nmi_watchdog != NMI_LOCAL_APIC) &&
280 (nmi_watchdog != NMI_IO_APIC))
281 return;
Andi Kleen09198e62007-05-02 19:27:20 +0200282 if (__get_cpu_var(wd_enabled) == 0)
Shaohua Li4038f902006-09-26 10:52:27 +0200283 return;
Andi Kleen09198e62007-05-02 19:27:20 +0200284 if (nmi_watchdog == NMI_LOCAL_APIC)
285 lapic_watchdog_stop();
286 __get_cpu_var(wd_enabled) = 0;
Don Zickusb7471c62006-09-26 10:52:26 +0200287 atomic_dec(&nmi_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
290/*
291 * the best way to detect whether a CPU has a 'hard lockup' problem
292 * is to check it's local APIC timer IRQ counts. If they are not
293 * changing then that CPU has some problem.
294 *
295 * as these watchdog NMI IRQs are generated on every CPU, we only
296 * have to check the current processor.
297 *
298 * since NMIs don't listen to _any_ locks, we have to be extremely
299 * careful not to rely on unsafe variables. The printk might lock
300 * up though, so we have to break up any console locks first ...
301 * [when there will be more tty-related locks, break them up
302 * here too!]
303 */
304
Hiroshi Shimamotof7849462008-05-02 16:45:08 -0700305static DEFINE_PER_CPU(unsigned, last_irq_sum);
306static DEFINE_PER_CPU(local_t, alert_counter);
307static DEFINE_PER_CPU(int, nmi_touch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Andrew Mortonf2890252007-07-17 04:03:57 -0700309void touch_nmi_watchdog(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Jan Beulichc6ea3962006-12-07 02:14:09 +0100311 if (nmi_watchdog > 0) {
312 unsigned cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Jan Beulichc6ea3962006-12-07 02:14:09 +0100314 /*
Hiroshi Shimamotof7849462008-05-02 16:45:08 -0700315 * Tell other CPUs to reset their alert counters. We cannot
316 * do it ourselves because the alert count increase is not
317 * atomic.
Jan Beulichc6ea3962006-12-07 02:14:09 +0100318 */
Andrew Mortonf2890252007-07-17 04:03:57 -0700319 for_each_present_cpu(cpu) {
Hiroshi Shimamotof7849462008-05-02 16:45:08 -0700320 if (per_cpu(nmi_touch, cpu) != 1)
321 per_cpu(nmi_touch, cpu) = 1;
Andrew Mortonf2890252007-07-17 04:03:57 -0700322 }
Jan Beulichc6ea3962006-12-07 02:14:09 +0100323 }
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700324
325 /*
326 * Tickle the softlockup detector too:
327 */
328 touch_softlockup_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
Michal Schmidt1e862402006-07-30 03:03:29 -0700330EXPORT_SYMBOL(touch_nmi_watchdog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Steven Rostedt5deb45e2008-04-19 19:19:55 +0200332notrace __kprobes int
333nmi_watchdog_tick(struct pt_regs *regs, unsigned reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
335
336 /*
337 * Since current_thread_info()-> is always on the stack, and we
338 * always switch the stack NMI-atomically, it's safe to use
339 * smp_processor_id().
340 */
Jesper Juhlb791cce2006-03-28 01:56:52 -0800341 unsigned int sum;
Don Zickusb7471c62006-09-26 10:52:26 +0200342 int touched = 0;
Jesper Juhlb791cce2006-03-28 01:56:52 -0800343 int cpu = smp_processor_id();
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100344 int rc = 0;
Don Zickusb7471c62006-09-26 10:52:26 +0200345
346 /* check for other users first */
347 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT)
348 == NOTIFY_STOP) {
Don Zickus3adbbcce2006-09-26 10:52:26 +0200349 rc = 1;
Don Zickusb7471c62006-09-26 10:52:26 +0200350 touched = 1;
351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Andrew Mortonbb81a092006-12-07 02:14:01 +0100353 if (cpu_isset(cpu, backtrace_mask)) {
354 static DEFINE_SPINLOCK(lock); /* Serialise the printks */
355
356 spin_lock(&lock);
357 printk("NMI backtrace for cpu %d\n", cpu);
358 dump_stack();
359 spin_unlock(&lock);
360 cpu_clear(cpu, backtrace_mask);
361 }
362
Thomas Gleixnerf8b50352007-02-16 01:28:09 -0800363 /*
364 * Take the local apic timer and PIT/HPET into account. We don't
365 * know which one is active, when we have highres/dyntick on
366 */
Thomas Gleixner3c9aea42007-10-12 23:04:06 +0200367 sum = per_cpu(irq_stat, cpu).apic_timer_irqs +
368 per_cpu(irq_stat, cpu).irq0_irqs;
Hiroshi Shimamotof7849462008-05-02 16:45:08 -0700369 if (__get_cpu_var(nmi_touch)) {
370 __get_cpu_var(nmi_touch) = 0;
371 touched = 1;
372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Thomas Gleixnerf8b50352007-02-16 01:28:09 -0800374 /* if the none of the timers isn't firing, this cpu isn't doing much */
Hiroshi Shimamotof7849462008-05-02 16:45:08 -0700375 if (!touched && __get_cpu_var(last_irq_sum) == sum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 /*
377 * Ayiee, looks like this CPU is stuck ...
378 * wait a few IRQs (5 seconds) before doing the oops ...
379 */
Hiroshi Shimamotof7849462008-05-02 16:45:08 -0700380 local_inc(&__get_cpu_var(alert_counter));
381 if (local_read(&__get_cpu_var(alert_counter)) == 5*nmi_hz)
George Anzinger748f2ed2005-09-03 15:56:48 -0700382 /*
383 * die_nmi will return ONLY if NOTIFY_STOP happens..
384 */
Cyrill Gorcunovddca03c2008-05-24 19:36:31 +0400385 die_nmi("BUG: NMI Watchdog detected LOCKUP",
Cyrill Gorcunovd1b946b2008-05-24 19:36:34 +0400386 regs, panic_on_timeout);
GOTO Masanorib884e252006-03-07 21:55:29 -0800387 } else {
Hiroshi Shimamotof7849462008-05-02 16:45:08 -0700388 __get_cpu_var(last_irq_sum) = sum;
389 local_set(&__get_cpu_var(alert_counter), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
Don Zickusb7471c62006-09-26 10:52:26 +0200391 /* see if the nmi watchdog went off */
Andi Kleen09198e62007-05-02 19:27:20 +0200392 if (!__get_cpu_var(wd_enabled))
393 return rc;
394 switch (nmi_watchdog) {
395 case NMI_LOCAL_APIC:
396 rc |= lapic_wd_event(nmi_hz);
397 break;
398 case NMI_IO_APIC:
399 /* don't know how to accurately check for this.
400 * just assume it was a watchdog timer interrupt
401 * This matches the old behaviour.
402 */
403 rc = 1;
404 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
Don Zickus3adbbcce2006-09-26 10:52:26 +0200406 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
409#ifdef CONFIG_SYSCTL
410
411static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu)
412{
413 unsigned char reason = get_nmi_reason();
414 char buf[64];
415
Don Zickus2fbe7b22006-09-26 10:52:27 +0200416 sprintf(buf, "NMI received for unknown reason %02x\n", reason);
Cyrill Gorcunovddca03c2008-05-24 19:36:31 +0400417 die_nmi(buf, regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 return 0;
419}
420
Don Zickus407984f2006-09-26 10:52:27 +0200421/*
Don Zickuse33e89a2006-09-26 10:52:27 +0200422 * proc handler for /proc/sys/kernel/nmi
Don Zickus407984f2006-09-26 10:52:27 +0200423 */
424int proc_nmi_enabled(struct ctl_table *table, int write, struct file *file,
425 void __user *buffer, size_t *length, loff_t *ppos)
426{
427 int old_state;
428
429 nmi_watchdog_enabled = (atomic_read(&nmi_active) > 0) ? 1 : 0;
430 old_state = nmi_watchdog_enabled;
431 proc_dointvec(table, write, file, buffer, length, ppos);
432 if (!!old_state == !!nmi_watchdog_enabled)
433 return 0;
434
Daniel Gollub0328ece2007-08-15 02:40:35 +0200435 if (atomic_read(&nmi_active) < 0 || nmi_watchdog == NMI_DISABLED) {
Don Zickuse33e89a2006-09-26 10:52:27 +0200436 printk( KERN_WARNING "NMI watchdog is permanently disabled\n");
437 return -EIO;
Don Zickus407984f2006-09-26 10:52:27 +0200438 }
439
440 if (nmi_watchdog == NMI_DEFAULT) {
Andi Kleen09198e62007-05-02 19:27:20 +0200441 if (lapic_watchdog_ok())
Don Zickus407984f2006-09-26 10:52:27 +0200442 nmi_watchdog = NMI_LOCAL_APIC;
443 else
444 nmi_watchdog = NMI_IO_APIC;
445 }
446
Don Zickuse33e89a2006-09-26 10:52:27 +0200447 if (nmi_watchdog == NMI_LOCAL_APIC) {
Don Zickus407984f2006-09-26 10:52:27 +0200448 if (nmi_watchdog_enabled)
449 enable_lapic_nmi_watchdog();
450 else
451 disable_lapic_nmi_watchdog();
Don Zickus407984f2006-09-26 10:52:27 +0200452 } else {
453 printk( KERN_WARNING
454 "NMI watchdog doesn't know what hardware to touch\n");
455 return -EIO;
456 }
457 return 0;
458}
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460#endif
461
Li Zefana062bae2008-02-03 15:40:30 +0800462int do_nmi_callback(struct pt_regs *regs, int cpu)
463{
464#ifdef CONFIG_SYSCTL
465 if (unknown_nmi_panic)
466 return unknown_nmi_panic_callback(regs, cpu);
467#endif
468 return 0;
469}
470
Andrew Mortonbb81a092006-12-07 02:14:01 +0100471void __trigger_all_cpu_backtrace(void)
472{
473 int i;
474
475 backtrace_mask = cpu_online_map;
476 /* Wait for up to 10 seconds for all CPUs to do the backtrace */
477 for (i = 0; i < 10 * 1000; i++) {
478 if (cpus_empty(backtrace_mask))
479 break;
480 mdelay(1);
481 }
482}
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484EXPORT_SYMBOL(nmi_active);
485EXPORT_SYMBOL(nmi_watchdog);