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>
26 #include <asm/proto.h>
29 int unknown_nmi_panic
;
30 int nmi_watchdog_enabled
;
31 int panic_on_unrecovered_nmi
;
33 static cpumask_t backtrace_mask
= CPU_MASK_NONE
;
36 * >0: the lapic NMI watchdog is active, but can be disabled
37 * <0: the lapic NMI watchdog has not been set up, and cannot
39 * 0: the lapic NMI watchdog is disabled, but can be enabled
41 atomic_t nmi_active
= ATOMIC_INIT(0); /* oprofile uses this */
42 static int panic_on_timeout
;
44 unsigned int nmi_watchdog
= NMI_DEFAULT
;
45 static unsigned int nmi_hz
= HZ
;
47 static DEFINE_PER_CPU(short, wd_enabled
);
49 /* Run after command line and cpu_init init, but before all other checks */
50 void nmi_watchdog_default(void)
52 if (nmi_watchdog
!= NMI_DEFAULT
)
54 nmi_watchdog
= NMI_NONE
;
57 static int endflag __initdata
= 0;
60 /* The performance counters used by NMI_LOCAL_APIC don't trigger when
61 * the CPU is idle. To make sure the NMI watchdog really ticks on all
62 * CPUs during the test make them busy.
64 static __init
void nmi_cpu_busy(void *data
)
66 local_irq_enable_in_hardirq();
67 /* Intentionally don't use cpu_relax here. This is
68 to make sure that the performance counter really ticks,
69 even if there is a simulator or similar that catches the
70 pause instruction. On a real HT machine this is fine because
71 all other CPUs are busy with "useless" delay loops and don't
72 care if they get somewhat less cycles. */
78 int __init
check_nmi_watchdog(void)
83 if ((nmi_watchdog
== NMI_NONE
) || (nmi_watchdog
== NMI_DISABLED
))
86 if (!atomic_read(&nmi_active
))
89 prev_nmi_count
= kmalloc(NR_CPUS
* sizeof(int), GFP_KERNEL
);
93 printk(KERN_INFO
"Testing NMI watchdog ... ");
96 if (nmi_watchdog
== NMI_LOCAL_APIC
)
97 smp_call_function(nmi_cpu_busy
, (void *)&endflag
, 0, 0);
100 for (cpu
= 0; cpu
< NR_CPUS
; cpu
++)
101 prev_nmi_count
[cpu
] = cpu_pda(cpu
)->__nmi_count
;
103 mdelay((20*1000)/nmi_hz
); // wait 20 ticks
105 for_each_online_cpu(cpu
) {
106 if (!per_cpu(wd_enabled
, cpu
))
108 if (cpu_pda(cpu
)->__nmi_count
- prev_nmi_count
[cpu
] <= 5) {
109 printk(KERN_WARNING
"WARNING: CPU#%d: NMI "
110 "appears to be stuck (%d->%d)!\n",
113 cpu_pda(cpu
)->__nmi_count
);
114 per_cpu(wd_enabled
, cpu
) = 0;
115 atomic_dec(&nmi_active
);
119 if (!atomic_read(&nmi_active
)) {
120 kfree(prev_nmi_count
);
121 atomic_set(&nmi_active
, -1);
126 /* now that we know it works we can reduce NMI frequency to
127 something more reasonable; makes a difference in some configs */
128 if (nmi_watchdog
== NMI_LOCAL_APIC
)
129 nmi_hz
= lapic_adjust_nmi_hz(1);
131 kfree(prev_nmi_count
);
135 static int __init
setup_nmi_watchdog(char *str
)
139 if (!strncmp(str
,"panic",5)) {
140 panic_on_timeout
= 1;
141 str
= strchr(str
, ',');
147 get_option(&str
, &nmi
);
149 if ((nmi
>= NMI_INVALID
) || (nmi
< NMI_NONE
))
156 __setup("nmi_watchdog=", setup_nmi_watchdog
);
160 static int nmi_pm_active
; /* nmi_active before suspend */
162 static int lapic_nmi_suspend(struct sys_device
*dev
, pm_message_t state
)
164 /* only CPU0 goes here, other CPUs should be offline */
165 nmi_pm_active
= atomic_read(&nmi_active
);
166 stop_apic_nmi_watchdog(NULL
);
167 BUG_ON(atomic_read(&nmi_active
) != 0);
171 static int lapic_nmi_resume(struct sys_device
*dev
)
173 /* only CPU0 goes here, other CPUs should be offline */
174 if (nmi_pm_active
> 0) {
175 setup_apic_nmi_watchdog(NULL
);
176 touch_nmi_watchdog();
181 static struct sysdev_class nmi_sysclass
= {
183 .resume
= lapic_nmi_resume
,
184 .suspend
= lapic_nmi_suspend
,
187 static struct sys_device device_lapic_nmi
= {
189 .cls
= &nmi_sysclass
,
192 static int __init
init_lapic_nmi_sysfs(void)
196 /* should really be a BUG_ON but b/c this is an
197 * init call, it just doesn't work. -dcz
199 if (nmi_watchdog
!= NMI_LOCAL_APIC
)
202 if (atomic_read(&nmi_active
) < 0)
205 error
= sysdev_class_register(&nmi_sysclass
);
207 error
= sysdev_register(&device_lapic_nmi
);
210 /* must come after the local APIC's device_initcall() */
211 late_initcall(init_lapic_nmi_sysfs
);
213 #endif /* CONFIG_PM */
215 static void __acpi_nmi_enable(void *__unused
)
217 apic_write(APIC_LVT0
, APIC_DM_NMI
);
221 * Enable timer based NMIs on all CPUs:
223 void acpi_nmi_enable(void)
225 if (atomic_read(&nmi_active
) && nmi_watchdog
== NMI_IO_APIC
)
226 on_each_cpu(__acpi_nmi_enable
, NULL
, 0, 1);
229 static void __acpi_nmi_disable(void *__unused
)
231 apic_write(APIC_LVT0
, APIC_DM_NMI
| APIC_LVT_MASKED
);
235 * Disable timer based NMIs on all CPUs:
237 void acpi_nmi_disable(void)
239 if (atomic_read(&nmi_active
) && nmi_watchdog
== NMI_IO_APIC
)
240 on_each_cpu(__acpi_nmi_disable
, NULL
, 0, 1);
243 void setup_apic_nmi_watchdog(void *unused
)
245 if (__get_cpu_var(wd_enabled
))
248 /* cheap hack to support suspend/resume */
249 /* if cpu0 is not active neither should the other cpus */
250 if ((smp_processor_id() != 0) && (atomic_read(&nmi_active
) <= 0))
253 switch (nmi_watchdog
) {
255 __get_cpu_var(wd_enabled
) = 1;
256 if (lapic_watchdog_init(nmi_hz
) < 0) {
257 __get_cpu_var(wd_enabled
) = 0;
262 __get_cpu_var(wd_enabled
) = 1;
263 atomic_inc(&nmi_active
);
267 void stop_apic_nmi_watchdog(void *unused
)
269 /* only support LOCAL and IO APICs for now */
270 if ((nmi_watchdog
!= NMI_LOCAL_APIC
) &&
271 (nmi_watchdog
!= NMI_IO_APIC
))
273 if (__get_cpu_var(wd_enabled
) == 0)
275 if (nmi_watchdog
== NMI_LOCAL_APIC
)
276 lapic_watchdog_stop();
277 __get_cpu_var(wd_enabled
) = 0;
278 atomic_dec(&nmi_active
);
282 * the best way to detect whether a CPU has a 'hard lockup' problem
283 * is to check it's local APIC timer IRQ counts. If they are not
284 * changing then that CPU has some problem.
286 * as these watchdog NMI IRQs are generated on every CPU, we only
287 * have to check the current processor.
290 static DEFINE_PER_CPU(unsigned, last_irq_sum
);
291 static DEFINE_PER_CPU(local_t
, alert_counter
);
292 static DEFINE_PER_CPU(int, nmi_touch
);
294 void touch_nmi_watchdog(void)
296 if (nmi_watchdog
> 0) {
300 * Tell other CPUs to reset their alert counters. We cannot
301 * do it ourselves because the alert count increase is not
304 for_each_present_cpu(cpu
) {
305 if (per_cpu(nmi_touch
, cpu
) != 1)
306 per_cpu(nmi_touch
, cpu
) = 1;
310 touch_softlockup_watchdog();
312 EXPORT_SYMBOL(touch_nmi_watchdog
);
314 int __kprobes
nmi_watchdog_tick(struct pt_regs
* regs
, unsigned reason
)
318 int cpu
= smp_processor_id();
321 /* check for other users first */
322 if (notify_die(DIE_NMI
, "nmi", regs
, reason
, 2, SIGINT
)
328 sum
= read_pda(apic_timer_irqs
) + read_pda(irq0_irqs
);
329 if (__get_cpu_var(nmi_touch
)) {
330 __get_cpu_var(nmi_touch
) = 0;
334 if (cpu_isset(cpu
, backtrace_mask
)) {
335 static DEFINE_SPINLOCK(lock
); /* Serialise the printks */
338 printk("NMI backtrace for cpu %d\n", cpu
);
341 cpu_clear(cpu
, backtrace_mask
);
344 #ifdef CONFIG_X86_MCE
345 /* Could check oops_in_progress here too, but it's safer
347 if (atomic_read(&mce_entry
) > 0)
350 /* if the apic timer isn't firing, this cpu isn't doing much */
351 if (!touched
&& __get_cpu_var(last_irq_sum
) == sum
) {
353 * Ayiee, looks like this CPU is stuck ...
354 * wait a few IRQs (5 seconds) before doing the oops ...
356 local_inc(&__get_cpu_var(alert_counter
));
357 if (local_read(&__get_cpu_var(alert_counter
)) == 5*nmi_hz
)
358 die_nmi("NMI Watchdog detected LOCKUP on CPU %d\n", regs
,
361 __get_cpu_var(last_irq_sum
) = sum
;
362 local_set(&__get_cpu_var(alert_counter
), 0);
365 /* see if the nmi watchdog went off */
366 if (!__get_cpu_var(wd_enabled
))
368 switch (nmi_watchdog
) {
370 rc
|= lapic_wd_event(nmi_hz
);
373 /* don't know how to accurately check for this.
374 * just assume it was a watchdog timer interrupt
375 * This matches the old behaviour.
383 static unsigned ignore_nmis
;
385 asmlinkage __kprobes
void do_nmi(struct pt_regs
* regs
, long error_code
)
388 add_pda(__nmi_count
,1);
390 default_do_nmi(regs
);
400 void restart_nmi(void)
408 static int unknown_nmi_panic_callback(struct pt_regs
*regs
, int cpu
)
410 unsigned char reason
= get_nmi_reason();
413 sprintf(buf
, "NMI received for unknown reason %02x\n", reason
);
414 die_nmi(buf
, regs
, 1); /* Always panic here */
419 * proc handler for /proc/sys/kernel/nmi
421 int proc_nmi_enabled(struct ctl_table
*table
, int write
, struct file
*file
,
422 void __user
*buffer
, size_t *length
, loff_t
*ppos
)
426 nmi_watchdog_enabled
= (atomic_read(&nmi_active
) > 0) ? 1 : 0;
427 old_state
= nmi_watchdog_enabled
;
428 proc_dointvec(table
, write
, file
, buffer
, length
, ppos
);
429 if (!!old_state
== !!nmi_watchdog_enabled
)
432 if (atomic_read(&nmi_active
) < 0 || nmi_watchdog
== NMI_DISABLED
) {
433 printk( KERN_WARNING
"NMI watchdog is permanently disabled\n");
437 /* if nmi_watchdog is not set yet, then set it */
438 nmi_watchdog_default();
440 if (nmi_watchdog
== NMI_LOCAL_APIC
) {
441 if (nmi_watchdog_enabled
)
442 enable_lapic_nmi_watchdog();
444 disable_lapic_nmi_watchdog();
447 "NMI watchdog doesn't know what hardware to touch\n");
455 int do_nmi_callback(struct pt_regs
*regs
, int cpu
)
458 if (unknown_nmi_panic
)
459 return unknown_nmi_panic_callback(regs
, cpu
);
464 void __trigger_all_cpu_backtrace(void)
468 backtrace_mask
= cpu_online_map
;
469 /* Wait for up to 10 seconds for all CPUs to do the backtrace */
470 for (i
= 0; i
< 10 * 1000; i
++) {
471 if (cpus_empty(backtrace_mask
))
477 EXPORT_SYMBOL(nmi_active
);
478 EXPORT_SYMBOL(nmi_watchdog
);