blob: 6a0aa70386859ae9aa7235b1572505ffee21f055 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#include <asm/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/nmi.h>
28
29#include "mach_traps.h"
30
Andi Kleen29cbc782006-09-30 01:47:55 +020031int unknown_nmi_panic;
32int nmi_watchdog_enabled;
33
Andi Kleen1714f9b2007-04-16 10:30:27 +020034static cpumask_t backtrace_mask = CPU_MASK_NONE;
Andi Kleen09198e62007-05-02 19:27:20 +020035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036/* nmi_active:
Don Zickusb7471c62006-09-26 10:52:26 +020037 * >0: the lapic NMI watchdog is active, but can be disabled
38 * <0: the lapic NMI watchdog has not been set up, and cannot
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 * be enabled
Don Zickusb7471c62006-09-26 10:52:26 +020040 * 0: the lapic NMI watchdog is disabled, but can be enabled
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 */
Don Zickusb7471c62006-09-26 10:52:26 +020042atomic_t nmi_active = ATOMIC_INIT(0); /* oprofile uses this */
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Don Zickusb7471c62006-09-26 10:52:26 +020044unsigned int nmi_watchdog = NMI_DEFAULT;
45static unsigned int nmi_hz = HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Andi Kleen09198e62007-05-02 19:27:20 +020047static DEFINE_PER_CPU(short, wd_enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Ravikiran G Thirumalai92715e22006-12-09 21:33:35 +010049static int endflag __initdata = 0;
50
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +010051#ifdef CONFIG_SMP
Eric W. Biederman29b70082005-10-30 14:59:40 -080052/* The performance counters used by NMI_LOCAL_APIC don't trigger when
53 * the CPU is idle. To make sure the NMI watchdog really ticks on all
54 * CPUs during the test make them busy.
55 */
56static __init void nmi_cpu_busy(void *data)
57{
Ingo Molnar366c7f52006-07-03 00:25:25 -070058 local_irq_enable_in_hardirq();
Eric W. Biederman29b70082005-10-30 14:59:40 -080059 /* Intentionally don't use cpu_relax here. This is
60 to make sure that the performance counter really ticks,
61 even if there is a simulator or similar that catches the
62 pause instruction. On a real HT machine this is fine because
63 all other CPUs are busy with "useless" delay loops and don't
64 care if they get somewhat less cycles. */
Ravikiran G Thirumalai92715e22006-12-09 21:33:35 +010065 while (endflag == 0)
66 mb();
Ingo Molnar04920072007-11-09 22:39:38 +010067}
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +010068#endif
Eric W. Biederman29b70082005-10-30 14:59:40 -080069
Jack F Vogel67701ae2005-05-01 08:58:48 -070070static int __init check_nmi_watchdog(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Eric W. Biederman29b70082005-10-30 14:59:40 -080072 unsigned int *prev_nmi_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 int cpu;
74
Daniel Gollub0328ece2007-08-15 02:40:35 +020075 if ((nmi_watchdog == NMI_NONE) || (nmi_watchdog == NMI_DISABLED))
Don Zickusb7471c62006-09-26 10:52:26 +020076 return 0;
77
78 if (!atomic_read(&nmi_active))
Jack F Vogel67701ae2005-05-01 08:58:48 -070079 return 0;
80
Eric W. Biederman29b70082005-10-30 14:59:40 -080081 prev_nmi_count = kmalloc(NR_CPUS * sizeof(int), GFP_KERNEL);
82 if (!prev_nmi_count)
Thomas Gleixner4960c9d2008-01-22 10:23:01 +010083 return -1;
Eric W. Biederman29b70082005-10-30 14:59:40 -080084
Jack F Vogel67701ae2005-05-01 08:58:48 -070085 printk(KERN_INFO "Testing NMI watchdog ... ");
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +010087#ifdef CONFIG_SMP
Eric W. Biederman29b70082005-10-30 14:59:40 -080088 if (nmi_watchdog == NMI_LOCAL_APIC)
89 smp_call_function(nmi_cpu_busy, (void *)&endflag, 0, 0);
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +010090#endif
Eric W. Biederman29b70082005-10-30 14:59:40 -080091
KAMEZAWA Hiroyukic8912592006-03-28 01:56:39 -080092 for_each_possible_cpu(cpu)
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +010093 prev_nmi_count[cpu] = nmi_count(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 local_irq_enable();
Andi Kleen0fb2ebf2007-04-02 12:14:12 +020095 mdelay((20*1000)/nmi_hz); // wait 20 ticks
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
KAMEZAWA Hiroyukic8912592006-03-28 01:56:39 -080097 for_each_possible_cpu(cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#ifdef CONFIG_SMP
99 /* Check cpu_callin_map here because that is set
100 after the timer is started. */
101 if (!cpu_isset(cpu, cpu_callin_map))
102 continue;
103#endif
Andi Kleen09198e62007-05-02 19:27:20 +0200104 if (!per_cpu(wd_enabled, cpu))
Don Zickusb7471c62006-09-26 10:52:26 +0200105 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 if (nmi_count(cpu) - prev_nmi_count[cpu] <= 5) {
Don Zickus75bc1222007-12-04 17:19:07 +0100107 printk(KERN_WARNING "WARNING: CPU#%d: NMI "
108 "appears to be stuck (%d->%d)!\n",
Eric W. Biederman29b70082005-10-30 14:59:40 -0800109 cpu,
110 prev_nmi_count[cpu],
111 nmi_count(cpu));
Andi Kleen09198e62007-05-02 19:27:20 +0200112 per_cpu(wd_enabled, cpu) = 0;
Don Zickusb7471c62006-09-26 10:52:26 +0200113 atomic_dec(&nmi_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 }
115 }
Daniel Walkerd9586542007-09-06 16:59:54 +0200116 endflag = 1;
Don Zickusb7471c62006-09-26 10:52:26 +0200117 if (!atomic_read(&nmi_active)) {
118 kfree(prev_nmi_count);
119 atomic_set(&nmi_active, -1);
Thomas Gleixner4960c9d2008-01-22 10:23:01 +0100120 return -1;
Don Zickusb7471c62006-09-26 10:52:26 +0200121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 printk("OK.\n");
123
124 /* now that we know it works we can reduce NMI frequency to
125 something more reasonable; makes a difference in some configs */
Andi Kleen09198e62007-05-02 19:27:20 +0200126 if (nmi_watchdog == NMI_LOCAL_APIC)
127 nmi_hz = lapic_adjust_nmi_hz(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Eric W. Biederman29b70082005-10-30 14:59:40 -0800129 kfree(prev_nmi_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 return 0;
131}
Jack F Vogel67701ae2005-05-01 08:58:48 -0700132/* This needs to happen later in boot so counters are working */
133late_initcall(check_nmi_watchdog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135static int __init setup_nmi_watchdog(char *str)
136{
137 int nmi;
138
139 get_option(&str, &nmi);
140
Don Zickusb7471c62006-09-26 10:52:26 +0200141 if ((nmi >= NMI_INVALID) || (nmi < NMI_NONE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return 0;
Venkatesh Pallipadi58d9ce7d2007-01-22 20:40:34 -0800143
Don Zickusb7471c62006-09-26 10:52:26 +0200144 nmi_watchdog = nmi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 return 1;
146}
147
148__setup("nmi_watchdog=", setup_nmi_watchdog);
149
Don Zickusb7471c62006-09-26 10:52:26 +0200150
Andi Kleen09198e62007-05-02 19:27:20 +0200151/* Suspend/resume support */
Ingo Molnar5d0e6002007-02-13 13:26:24 +0100152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153#ifdef CONFIG_PM
154
155static int nmi_pm_active; /* nmi_active before suspend */
156
Pavel Machek438510f2005-04-16 15:25:24 -0700157static int lapic_nmi_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Shaohua Li4038f902006-09-26 10:52:27 +0200159 /* only CPU0 goes here, other CPUs should be offline */
Don Zickusb7471c62006-09-26 10:52:26 +0200160 nmi_pm_active = atomic_read(&nmi_active);
Shaohua Li4038f902006-09-26 10:52:27 +0200161 stop_apic_nmi_watchdog(NULL);
162 BUG_ON(atomic_read(&nmi_active) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 return 0;
164}
165
166static int lapic_nmi_resume(struct sys_device *dev)
167{
Shaohua Li4038f902006-09-26 10:52:27 +0200168 /* only CPU0 goes here, other CPUs should be offline */
169 if (nmi_pm_active > 0) {
170 setup_apic_nmi_watchdog(NULL);
171 touch_nmi_watchdog();
172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return 0;
174}
175
176
177static struct sysdev_class nmi_sysclass = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +0100178 .name = "lapic_nmi",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 .resume = lapic_nmi_resume,
180 .suspend = lapic_nmi_suspend,
181};
182
183static struct sys_device device_lapic_nmi = {
184 .id = 0,
185 .cls = &nmi_sysclass,
186};
187
188static int __init init_lapic_nmi_sysfs(void)
189{
190 int error;
191
Don Zickusb7471c62006-09-26 10:52:26 +0200192 /* should really be a BUG_ON but b/c this is an
193 * init call, it just doesn't work. -dcz
194 */
195 if (nmi_watchdog != NMI_LOCAL_APIC)
196 return 0;
197
Andi Kleen09198e62007-05-02 19:27:20 +0200198 if (atomic_read(&nmi_active) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return 0;
200
201 error = sysdev_class_register(&nmi_sysclass);
202 if (!error)
203 error = sysdev_register(&device_lapic_nmi);
204 return error;
205}
206/* must come after the local APIC's device_initcall() */
207late_initcall(init_lapic_nmi_sysfs);
208
209#endif /* CONFIG_PM */
210
Andi Kleen09198e62007-05-02 19:27:20 +0200211static void __acpi_nmi_enable(void *__unused)
212{
213 apic_write_around(APIC_LVT0, APIC_DM_NMI);
214}
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216/*
Andi Kleen09198e62007-05-02 19:27:20 +0200217 * Enable timer based NMIs on all CPUs:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 */
Andi Kleen09198e62007-05-02 19:27:20 +0200219void acpi_nmi_enable(void)
Jan Beulich7fbb4f62005-06-23 00:08:23 -0700220{
Andi Kleen09198e62007-05-02 19:27:20 +0200221 if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC)
222 on_each_cpu(__acpi_nmi_enable, NULL, 0, 1);
Jan Beulich7fbb4f62005-06-23 00:08:23 -0700223}
224
Andi Kleen09198e62007-05-02 19:27:20 +0200225static void __acpi_nmi_disable(void *__unused)
Venkatesh Pallipadi90ce4bc2007-02-13 13:26:22 +0100226{
Andi Kleen09198e62007-05-02 19:27:20 +0200227 apic_write(APIC_LVT0, APIC_DM_NMI | APIC_LVT_MASKED);
Venkatesh Pallipadi90ce4bc2007-02-13 13:26:22 +0100228}
229
Andi Kleen09198e62007-05-02 19:27:20 +0200230/*
231 * Disable timer based NMIs on all CPUs:
232 */
233void acpi_nmi_disable(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
Andi Kleen09198e62007-05-02 19:27:20 +0200235 if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC)
236 on_each_cpu(__acpi_nmi_disable, NULL, 0, 1);
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +0200237}
238
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100239void setup_apic_nmi_watchdog(void *unused)
Don Zickusb7471c62006-09-26 10:52:26 +0200240{
Andi Kleen09198e62007-05-02 19:27:20 +0200241 if (__get_cpu_var(wd_enabled))
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100242 return;
Shaohua Li4038f902006-09-26 10:52:27 +0200243
244 /* cheap hack to support suspend/resume */
245 /* if cpu0 is not active neither should the other cpus */
246 if ((smp_processor_id() != 0) && (atomic_read(&nmi_active) <= 0))
247 return;
248
Andi Kleen09198e62007-05-02 19:27:20 +0200249 switch (nmi_watchdog) {
250 case NMI_LOCAL_APIC:
251 __get_cpu_var(wd_enabled) = 1; /* enable it before to avoid race with handler */
252 if (lapic_watchdog_init(nmi_hz) < 0) {
253 __get_cpu_var(wd_enabled) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return;
255 }
Andi Kleen09198e62007-05-02 19:27:20 +0200256 /* FALL THROUGH */
257 case NMI_IO_APIC:
258 __get_cpu_var(wd_enabled) = 1;
259 atomic_inc(&nmi_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
Don Zickusb7471c62006-09-26 10:52:26 +0200261}
262
Shaohua Li4038f902006-09-26 10:52:27 +0200263void stop_apic_nmi_watchdog(void *unused)
Don Zickusb7471c62006-09-26 10:52:26 +0200264{
265 /* only support LOCAL and IO APICs for now */
266 if ((nmi_watchdog != NMI_LOCAL_APIC) &&
267 (nmi_watchdog != NMI_IO_APIC))
268 return;
Andi Kleen09198e62007-05-02 19:27:20 +0200269 if (__get_cpu_var(wd_enabled) == 0)
Shaohua Li4038f902006-09-26 10:52:27 +0200270 return;
Andi Kleen09198e62007-05-02 19:27:20 +0200271 if (nmi_watchdog == NMI_LOCAL_APIC)
272 lapic_watchdog_stop();
273 __get_cpu_var(wd_enabled) = 0;
Don Zickusb7471c62006-09-26 10:52:26 +0200274 atomic_dec(&nmi_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
277/*
278 * the best way to detect whether a CPU has a 'hard lockup' problem
279 * is to check it's local APIC timer IRQ counts. If they are not
280 * changing then that CPU has some problem.
281 *
282 * as these watchdog NMI IRQs are generated on every CPU, we only
283 * have to check the current processor.
284 *
285 * since NMIs don't listen to _any_ locks, we have to be extremely
286 * careful not to rely on unsafe variables. The printk might lock
287 * up though, so we have to break up any console locks first ...
288 * [when there will be more tty-related locks, break them up
289 * here too!]
290 */
291
292static unsigned int
293 last_irq_sums [NR_CPUS],
294 alert_counter [NR_CPUS];
295
Andrew Mortonf2890252007-07-17 04:03:57 -0700296void touch_nmi_watchdog(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Jan Beulichc6ea3962006-12-07 02:14:09 +0100298 if (nmi_watchdog > 0) {
299 unsigned cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Jan Beulichc6ea3962006-12-07 02:14:09 +0100301 /*
302 * Just reset the alert counters, (other CPUs might be
303 * spinning on locks we hold):
304 */
Andrew Mortonf2890252007-07-17 04:03:57 -0700305 for_each_present_cpu(cpu) {
306 if (alert_counter[cpu])
307 alert_counter[cpu] = 0;
308 }
Jan Beulichc6ea3962006-12-07 02:14:09 +0100309 }
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700310
311 /*
312 * Tickle the softlockup detector too:
313 */
314 touch_softlockup_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
Michal Schmidt1e862402006-07-30 03:03:29 -0700316EXPORT_SYMBOL(touch_nmi_watchdog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318extern void die_nmi(struct pt_regs *, const char *msg);
319
Fernando Luis Vázquez Cao06039752006-09-26 10:52:36 +0200320__kprobes int nmi_watchdog_tick(struct pt_regs * regs, unsigned reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
322
323 /*
324 * Since current_thread_info()-> is always on the stack, and we
325 * always switch the stack NMI-atomically, it's safe to use
326 * smp_processor_id().
327 */
Jesper Juhlb791cce2006-03-28 01:56:52 -0800328 unsigned int sum;
Don Zickusb7471c62006-09-26 10:52:26 +0200329 int touched = 0;
Jesper Juhlb791cce2006-03-28 01:56:52 -0800330 int cpu = smp_processor_id();
Hiroshi Shimamoto416b7212008-01-30 13:30:33 +0100331 int rc = 0;
Don Zickusb7471c62006-09-26 10:52:26 +0200332
333 /* check for other users first */
334 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT)
335 == NOTIFY_STOP) {
Don Zickus3adbbcce2006-09-26 10:52:26 +0200336 rc = 1;
Don Zickusb7471c62006-09-26 10:52:26 +0200337 touched = 1;
338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Andrew Mortonbb81a092006-12-07 02:14:01 +0100340 if (cpu_isset(cpu, backtrace_mask)) {
341 static DEFINE_SPINLOCK(lock); /* Serialise the printks */
342
343 spin_lock(&lock);
344 printk("NMI backtrace for cpu %d\n", cpu);
345 dump_stack();
346 spin_unlock(&lock);
347 cpu_clear(cpu, backtrace_mask);
348 }
349
Thomas Gleixnerf8b50352007-02-16 01:28:09 -0800350 /*
351 * Take the local apic timer and PIT/HPET into account. We don't
352 * know which one is active, when we have highres/dyntick on
353 */
Thomas Gleixner3c9aea42007-10-12 23:04:06 +0200354 sum = per_cpu(irq_stat, cpu).apic_timer_irqs +
355 per_cpu(irq_stat, cpu).irq0_irqs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Thomas Gleixnerf8b50352007-02-16 01:28:09 -0800357 /* if the none of the timers isn't firing, this cpu isn't doing much */
Don Zickusb7471c62006-09-26 10:52:26 +0200358 if (!touched && last_irq_sums[cpu] == sum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 /*
360 * Ayiee, looks like this CPU is stuck ...
361 * wait a few IRQs (5 seconds) before doing the oops ...
362 */
363 alert_counter[cpu]++;
364 if (alert_counter[cpu] == 5*nmi_hz)
George Anzinger748f2ed2005-09-03 15:56:48 -0700365 /*
366 * die_nmi will return ONLY if NOTIFY_STOP happens..
367 */
Ingo Molnar91368d72006-03-23 03:00:54 -0800368 die_nmi(regs, "BUG: NMI Watchdog detected LOCKUP");
GOTO Masanorib884e252006-03-07 21:55:29 -0800369 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 last_irq_sums[cpu] = sum;
371 alert_counter[cpu] = 0;
372 }
Don Zickusb7471c62006-09-26 10:52:26 +0200373 /* see if the nmi watchdog went off */
Andi Kleen09198e62007-05-02 19:27:20 +0200374 if (!__get_cpu_var(wd_enabled))
375 return rc;
376 switch (nmi_watchdog) {
377 case NMI_LOCAL_APIC:
378 rc |= lapic_wd_event(nmi_hz);
379 break;
380 case NMI_IO_APIC:
381 /* don't know how to accurately check for this.
382 * just assume it was a watchdog timer interrupt
383 * This matches the old behaviour.
384 */
385 rc = 1;
386 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
Don Zickus3adbbcce2006-09-26 10:52:26 +0200388 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389}
390
391#ifdef CONFIG_SYSCTL
392
393static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu)
394{
395 unsigned char reason = get_nmi_reason();
396 char buf[64];
397
Don Zickus2fbe7b22006-09-26 10:52:27 +0200398 sprintf(buf, "NMI received for unknown reason %02x\n", reason);
399 die_nmi(regs, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 return 0;
401}
402
Don Zickus407984f2006-09-26 10:52:27 +0200403/*
Don Zickuse33e89a2006-09-26 10:52:27 +0200404 * proc handler for /proc/sys/kernel/nmi
Don Zickus407984f2006-09-26 10:52:27 +0200405 */
406int proc_nmi_enabled(struct ctl_table *table, int write, struct file *file,
407 void __user *buffer, size_t *length, loff_t *ppos)
408{
409 int old_state;
410
411 nmi_watchdog_enabled = (atomic_read(&nmi_active) > 0) ? 1 : 0;
412 old_state = nmi_watchdog_enabled;
413 proc_dointvec(table, write, file, buffer, length, ppos);
414 if (!!old_state == !!nmi_watchdog_enabled)
415 return 0;
416
Daniel Gollub0328ece2007-08-15 02:40:35 +0200417 if (atomic_read(&nmi_active) < 0 || nmi_watchdog == NMI_DISABLED) {
Don Zickuse33e89a2006-09-26 10:52:27 +0200418 printk( KERN_WARNING "NMI watchdog is permanently disabled\n");
419 return -EIO;
Don Zickus407984f2006-09-26 10:52:27 +0200420 }
421
422 if (nmi_watchdog == NMI_DEFAULT) {
Andi Kleen09198e62007-05-02 19:27:20 +0200423 if (lapic_watchdog_ok())
Don Zickus407984f2006-09-26 10:52:27 +0200424 nmi_watchdog = NMI_LOCAL_APIC;
425 else
426 nmi_watchdog = NMI_IO_APIC;
427 }
428
Don Zickuse33e89a2006-09-26 10:52:27 +0200429 if (nmi_watchdog == NMI_LOCAL_APIC) {
Don Zickus407984f2006-09-26 10:52:27 +0200430 if (nmi_watchdog_enabled)
431 enable_lapic_nmi_watchdog();
432 else
433 disable_lapic_nmi_watchdog();
Don Zickus407984f2006-09-26 10:52:27 +0200434 } else {
435 printk( KERN_WARNING
436 "NMI watchdog doesn't know what hardware to touch\n");
437 return -EIO;
438 }
439 return 0;
440}
441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442#endif
443
Li Zefana062bae2008-02-03 15:40:30 +0800444int do_nmi_callback(struct pt_regs *regs, int cpu)
445{
446#ifdef CONFIG_SYSCTL
447 if (unknown_nmi_panic)
448 return unknown_nmi_panic_callback(regs, cpu);
449#endif
450 return 0;
451}
452
Andrew Mortonbb81a092006-12-07 02:14:01 +0100453void __trigger_all_cpu_backtrace(void)
454{
455 int i;
456
457 backtrace_mask = cpu_online_map;
458 /* Wait for up to 10 seconds for all CPUs to do the backtrace */
459 for (i = 0; i < 10 * 1000; i++) {
460 if (cpus_empty(backtrace_mask))
461 break;
462 mdelay(1);
463 }
464}
465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466EXPORT_SYMBOL(nmi_active);
467EXPORT_SYMBOL(nmi_watchdog);