2 * Detect Hard Lockups using the NMI
4 * started by Don Zickus, Copyright (C) 2010 Red Hat, Inc.
6 * this code detects hard lockups: incidents in where on a CPU
7 * the kernel does not respond to anything except NMI.
9 * Note: Most of this code is borrowed heavily from softlockup.c,
10 * so thanks to Ingo for the initial implementation.
11 * Some chunks also taken from arch/x86/kernel/apic/nmi.c, thanks
12 * to those contributors as well.
16 #include <linux/cpu.h>
17 #include <linux/nmi.h>
18 #include <linux/init.h>
19 #include <linux/delay.h>
20 #include <linux/freezer.h>
21 #include <linux/lockdep.h>
22 #include <linux/notifier.h>
23 #include <linux/module.h>
24 #include <linux/sysctl.h>
26 #include <asm/irq_regs.h>
27 #include <linux/perf_event.h>
29 static DEFINE_PER_CPU(struct perf_event *, nmi_watchdog_ev);
30 static DEFINE_PER_CPU(int, nmi_watchdog_touch);
31 static DEFINE_PER_CPU(long, alert_counter);
33 void touch_nmi_watchdog(void)
35 __raw_get_cpu_var(nmi_watchdog_touch) = 1;
36 touch_softlockup_watchdog();
38 EXPORT_SYMBOL(touch_nmi_watchdog);
40 void touch_all_nmi_watchdog(void)
44 for_each_online_cpu(cpu)
45 per_cpu(nmi_watchdog_touch, cpu) = 1;
46 touch_softlockup_watchdog();
51 * proc handler for /proc/sys/kernel/nmi_watchdog
53 int proc_nmi_enabled(struct ctl_table *table, int write,
54 void __user *buffer, size_t *length, loff_t *ppos)
58 if (per_cpu(nmi_watchdog_ev, smp_processor_id()) == NULL)
59 nmi_watchdog_enabled = 0;
61 nmi_watchdog_enabled = 1;
63 touch_all_nmi_watchdog();
64 proc_dointvec(table, write, buffer, length, ppos);
65 if (nmi_watchdog_enabled)
66 for_each_online_cpu(cpu)
67 perf_event_enable(per_cpu(nmi_watchdog_ev, cpu));
69 for_each_online_cpu(cpu)
70 perf_event_disable(per_cpu(nmi_watchdog_ev, cpu));
74 #endif /* CONFIG_SYSCTL */
76 struct perf_event_attr wd_attr = {
77 .type = PERF_TYPE_HARDWARE,
78 .config = PERF_COUNT_HW_CPU_CYCLES,
79 .size = sizeof(struct perf_event_attr),
84 static int panic_on_timeout;
86 void wd_overflow(struct perf_event *event, int nmi,
87 struct perf_sample_data *data,
90 int cpu = smp_processor_id();
93 if (__get_cpu_var(nmi_watchdog_touch)) {
94 per_cpu(nmi_watchdog_touch, cpu) = 0;
98 /* check to see if the cpu is doing anything */
99 if (!touched && hw_nmi_is_cpu_stuck(regs)) {
101 * Ayiee, looks like this CPU is stuck ...
102 * wait a few IRQs (5 seconds) before doing the oops ...
104 per_cpu(alert_counter,cpu) += 1;
105 if (per_cpu(alert_counter,cpu) == 5) {
107 * die_nmi will return ONLY if NOTIFY_STOP happens..
109 die_nmi("BUG: NMI Watchdog detected LOCKUP",
110 regs, panic_on_timeout);
113 per_cpu(alert_counter,cpu) = 0;
120 * Create/destroy watchdog threads as CPUs come and go:
123 cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
125 int hotcpu = (unsigned long)hcpu;
126 struct perf_event *event;
130 case CPU_UP_PREPARE_FROZEN:
131 per_cpu(nmi_watchdog_touch, hotcpu) = 0;
134 case CPU_ONLINE_FROZEN:
135 /* originally wanted the below chunk to be in CPU_UP_PREPARE, but caps is unpriv for non-CPU0 */
136 wd_attr.sample_period = cpu_khz * 1000;
137 event = perf_event_create_kernel_counter(&wd_attr, hotcpu, -1, wd_overflow);
139 printk(KERN_ERR "nmi watchdog failed to create perf event on %i: %p\n", hotcpu, event);
142 per_cpu(nmi_watchdog_ev, hotcpu) = event;
143 perf_event_enable(per_cpu(nmi_watchdog_ev, hotcpu));
145 #ifdef CONFIG_HOTPLUG_CPU
146 case CPU_UP_CANCELED:
147 case CPU_UP_CANCELED_FROZEN:
148 perf_event_disable(per_cpu(nmi_watchdog_ev, hotcpu));
150 case CPU_DEAD_FROZEN:
151 event = per_cpu(nmi_watchdog_ev, hotcpu);
152 per_cpu(nmi_watchdog_ev, hotcpu) = NULL;
153 perf_event_release_kernel(event);
155 #endif /* CONFIG_HOTPLUG_CPU */
160 static struct notifier_block __cpuinitdata cpu_nfb = {
161 .notifier_call = cpu_callback
164 static int __initdata nonmi_watchdog;
166 static int __init nonmi_watchdog_setup(char *str)
171 __setup("nonmi_watchdog", nonmi_watchdog_setup);
173 static int __init spawn_nmi_watchdog_task(void)
175 void *cpu = (void *)(long)smp_processor_id();
181 err = cpu_callback(&cpu_nfb, CPU_UP_PREPARE, cpu);
182 if (err == NOTIFY_BAD) {
186 cpu_callback(&cpu_nfb, CPU_ONLINE, cpu);
187 register_cpu_notifier(&cpu_nfb);
191 early_initcall(spawn_nmi_watchdog_task);