2 * NMI watchdog support on APIC systems
4 * Started by Ingo Molnar <mingo@redhat.com>
7 * Mikael Pettersson : AMD K7 support for local APIC NMI watchdog.
8 * Mikael Pettersson : Power Management for local APIC NMI watchdog.
10 * Mikael Pettersson : PM converted to driver model. Disable/enable API.
13 #include <linux/nmi.h>
15 #include <linux/delay.h>
16 #include <linux/interrupt.h>
17 #include <linux/module.h>
18 #include <linux/sysdev.h>
19 #include <linux/sysctl.h>
20 #include <linux/kprobes.h>
21 #include <linux/cpumask.h>
22 #include <linux/kdebug.h>
24 #include <asm/i8259.h>
25 #include <asm/io_apic.h>
28 #include <asm/proto.h>
31 #include <mach_traps.h>
33 int unknown_nmi_panic;
34 int nmi_watchdog_enabled;
35 int panic_on_unrecovered_nmi;
37 static cpumask_t backtrace_mask = CPU_MASK_NONE;
40 * >0: the lapic NMI watchdog is active, but can be disabled
41 * <0: the lapic NMI watchdog has not been set up, and cannot
43 * 0: the lapic NMI watchdog is disabled, but can be enabled
45 atomic_t nmi_active = ATOMIC_INIT(0); /* oprofile uses this */
46 static int panic_on_timeout;
48 unsigned int nmi_watchdog = NMI_DEFAULT;
49 static unsigned int nmi_hz = HZ;
51 static DEFINE_PER_CPU(short, wd_enabled);
53 /* Run after command line and cpu_init init, but before all other checks */
54 void nmi_watchdog_default(void)
56 if (nmi_watchdog != NMI_DEFAULT)
58 nmi_watchdog = NMI_NONE;
61 static int endflag __initdata = 0;
64 /* The performance counters used by NMI_LOCAL_APIC don't trigger when
65 * the CPU is idle. To make sure the NMI watchdog really ticks on all
66 * CPUs during the test make them busy.
68 static __init void nmi_cpu_busy(void *data)
70 local_irq_enable_in_hardirq();
71 /* Intentionally don't use cpu_relax here. This is
72 to make sure that the performance counter really ticks,
73 even if there is a simulator or similar that catches the
74 pause instruction. On a real HT machine this is fine because
75 all other CPUs are busy with "useless" delay loops and don't
76 care if they get somewhat less cycles. */
82 int __init check_nmi_watchdog(void)
87 if ((nmi_watchdog == NMI_NONE) || (nmi_watchdog == NMI_DISABLED))
90 if (!atomic_read(&nmi_active))
93 prev_nmi_count = kmalloc(nr_cpu_ids * sizeof(int), GFP_KERNEL);
97 printk(KERN_INFO "Testing NMI watchdog ... ");
100 if (nmi_watchdog == NMI_LOCAL_APIC)
101 smp_call_function(nmi_cpu_busy, (void *)&endflag, 0, 0);
104 for (cpu = 0; cpu < nr_cpu_ids; cpu++)
105 prev_nmi_count[cpu] = cpu_pda(cpu)->__nmi_count;
107 mdelay((20*1000)/nmi_hz); // wait 20 ticks
109 for_each_online_cpu(cpu) {
110 if (!per_cpu(wd_enabled, cpu))
112 if (cpu_pda(cpu)->__nmi_count - prev_nmi_count[cpu] <= 5) {
113 printk(KERN_WARNING "WARNING: CPU#%d: NMI "
114 "appears to be stuck (%d->%d)!\n",
117 cpu_pda(cpu)->__nmi_count);
118 per_cpu(wd_enabled, cpu) = 0;
119 atomic_dec(&nmi_active);
123 if (!atomic_read(&nmi_active)) {
124 kfree(prev_nmi_count);
125 atomic_set(&nmi_active, -1);
130 /* now that we know it works we can reduce NMI frequency to
131 something more reasonable; makes a difference in some configs */
132 if (nmi_watchdog == NMI_LOCAL_APIC)
133 nmi_hz = lapic_adjust_nmi_hz(1);
135 kfree(prev_nmi_count);
138 if (nmi_watchdog == NMI_IO_APIC && !timer_through_8259)
139 disable_8259A_irq(0);
144 static int __init setup_nmi_watchdog(char *str)
148 if (!strncmp(str,"panic",5)) {
149 panic_on_timeout = 1;
150 str = strchr(str, ',');
156 get_option(&str, &nmi);
158 if ((nmi >= NMI_INVALID) || (nmi < NMI_NONE))
165 __setup("nmi_watchdog=", setup_nmi_watchdog);
169 static int nmi_pm_active; /* nmi_active before suspend */
171 static int lapic_nmi_suspend(struct sys_device *dev, pm_message_t state)
173 /* only CPU0 goes here, other CPUs should be offline */
174 nmi_pm_active = atomic_read(&nmi_active);
175 stop_apic_nmi_watchdog(NULL);
176 BUG_ON(atomic_read(&nmi_active) != 0);
180 static int lapic_nmi_resume(struct sys_device *dev)
182 /* only CPU0 goes here, other CPUs should be offline */
183 if (nmi_pm_active > 0) {
184 setup_apic_nmi_watchdog(NULL);
185 touch_nmi_watchdog();
190 static struct sysdev_class nmi_sysclass = {
192 .resume = lapic_nmi_resume,
193 .suspend = lapic_nmi_suspend,
196 static struct sys_device device_lapic_nmi = {
198 .cls = &nmi_sysclass,
201 static int __init init_lapic_nmi_sysfs(void)
205 /* should really be a BUG_ON but b/c this is an
206 * init call, it just doesn't work. -dcz
208 if (nmi_watchdog != NMI_LOCAL_APIC)
211 if (atomic_read(&nmi_active) < 0)
214 error = sysdev_class_register(&nmi_sysclass);
216 error = sysdev_register(&device_lapic_nmi);
219 /* must come after the local APIC's device_initcall() */
220 late_initcall(init_lapic_nmi_sysfs);
222 #endif /* CONFIG_PM */
224 static void __acpi_nmi_enable(void *__unused)
226 apic_write(APIC_LVT0, APIC_DM_NMI);
230 * Enable timer based NMIs on all CPUs:
232 void acpi_nmi_enable(void)
234 if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC)
235 on_each_cpu(__acpi_nmi_enable, NULL, 0, 1);
238 static void __acpi_nmi_disable(void *__unused)
240 apic_write(APIC_LVT0, APIC_DM_NMI | APIC_LVT_MASKED);
244 * Disable timer based NMIs on all CPUs:
246 void acpi_nmi_disable(void)
248 if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC)
249 on_each_cpu(__acpi_nmi_disable, NULL, 0, 1);
252 void setup_apic_nmi_watchdog(void *unused)
254 if (__get_cpu_var(wd_enabled))
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))
262 switch (nmi_watchdog) {
264 __get_cpu_var(wd_enabled) = 1;
265 if (lapic_watchdog_init(nmi_hz) < 0) {
266 __get_cpu_var(wd_enabled) = 0;
271 __get_cpu_var(wd_enabled) = 1;
272 atomic_inc(&nmi_active);
276 void stop_apic_nmi_watchdog(void *unused)
278 /* only support LOCAL and IO APICs for now */
279 if ((nmi_watchdog != NMI_LOCAL_APIC) &&
280 (nmi_watchdog != NMI_IO_APIC))
282 if (__get_cpu_var(wd_enabled) == 0)
284 if (nmi_watchdog == NMI_LOCAL_APIC)
285 lapic_watchdog_stop();
286 __get_cpu_var(wd_enabled) = 0;
287 atomic_dec(&nmi_active);
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.
295 * as these watchdog NMI IRQs are generated on every CPU, we only
296 * have to check the current processor.
299 static DEFINE_PER_CPU(unsigned, last_irq_sum);
300 static DEFINE_PER_CPU(local_t, alert_counter);
301 static DEFINE_PER_CPU(int, nmi_touch);
303 void touch_nmi_watchdog(void)
305 if (nmi_watchdog > 0) {
309 * Tell other CPUs to reset their alert counters. We cannot
310 * do it ourselves because the alert count increase is not
313 for_each_present_cpu(cpu) {
314 if (per_cpu(nmi_touch, cpu) != 1)
315 per_cpu(nmi_touch, cpu) = 1;
319 touch_softlockup_watchdog();
321 EXPORT_SYMBOL(touch_nmi_watchdog);
323 notrace __kprobes int
324 nmi_watchdog_tick(struct pt_regs *regs, unsigned reason)
328 int cpu = smp_processor_id();
331 /* check for other users first */
332 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT)
338 sum = read_pda(apic_timer_irqs) + read_pda(irq0_irqs);
339 if (__get_cpu_var(nmi_touch)) {
340 __get_cpu_var(nmi_touch) = 0;
344 if (cpu_isset(cpu, backtrace_mask)) {
345 static DEFINE_SPINLOCK(lock); /* Serialise the printks */
348 printk("NMI backtrace for cpu %d\n", cpu);
351 cpu_clear(cpu, backtrace_mask);
354 #ifdef CONFIG_X86_MCE
355 /* Could check oops_in_progress here too, but it's safer
357 if (atomic_read(&mce_entry) > 0)
360 /* if the apic timer isn't firing, this cpu isn't doing much */
361 if (!touched && __get_cpu_var(last_irq_sum) == sum) {
363 * Ayiee, looks like this CPU is stuck ...
364 * wait a few IRQs (5 seconds) before doing the oops ...
366 local_inc(&__get_cpu_var(alert_counter));
367 if (local_read(&__get_cpu_var(alert_counter)) == 5*nmi_hz)
368 die_nmi("NMI Watchdog detected LOCKUP on CPU %d\n", regs,
371 __get_cpu_var(last_irq_sum) = sum;
372 local_set(&__get_cpu_var(alert_counter), 0);
375 /* see if the nmi watchdog went off */
376 if (!__get_cpu_var(wd_enabled))
378 switch (nmi_watchdog) {
380 rc |= lapic_wd_event(nmi_hz);
383 /* don't know how to accurately check for this.
384 * just assume it was a watchdog timer interrupt
385 * This matches the old behaviour.
393 static unsigned ignore_nmis;
395 asmlinkage notrace __kprobes void
396 do_nmi(struct pt_regs *regs, long error_code)
399 add_pda(__nmi_count,1);
401 default_do_nmi(regs);
411 void restart_nmi(void)
419 static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu)
421 unsigned char reason = get_nmi_reason();
424 sprintf(buf, "NMI received for unknown reason %02x\n", reason);
425 die_nmi(buf, regs, 1); /* Always panic here */
430 * proc handler for /proc/sys/kernel/nmi
432 int proc_nmi_enabled(struct ctl_table *table, int write, struct file *file,
433 void __user *buffer, size_t *length, loff_t *ppos)
437 nmi_watchdog_enabled = (atomic_read(&nmi_active) > 0) ? 1 : 0;
438 old_state = nmi_watchdog_enabled;
439 proc_dointvec(table, write, file, buffer, length, ppos);
440 if (!!old_state == !!nmi_watchdog_enabled)
443 if (atomic_read(&nmi_active) < 0 || nmi_watchdog == NMI_DISABLED) {
444 printk( KERN_WARNING "NMI watchdog is permanently disabled\n");
448 /* if nmi_watchdog is not set yet, then set it */
449 nmi_watchdog_default();
451 if (nmi_watchdog == NMI_LOCAL_APIC) {
452 if (nmi_watchdog_enabled)
453 enable_lapic_nmi_watchdog();
455 disable_lapic_nmi_watchdog();
458 "NMI watchdog doesn't know what hardware to touch\n");
466 int do_nmi_callback(struct pt_regs *regs, int cpu)
469 if (unknown_nmi_panic)
470 return unknown_nmi_panic_callback(regs, cpu);
475 void __trigger_all_cpu_backtrace(void)
479 backtrace_mask = cpu_online_map;
480 /* Wait for up to 10 seconds for all CPUs to do the backtrace */
481 for (i = 0; i < 10 * 1000; i++) {
482 if (cpus_empty(backtrace_mask))
488 EXPORT_SYMBOL(nmi_active);
489 EXPORT_SYMBOL(nmi_watchdog);