diff options
author | Don Zickus <dzickus@redhat.com> | 2010-02-05 21:47:04 -0500 |
---|---|---|
committer | Ingo Molnar <mingo@elte.hu> | 2010-02-08 02:29:02 -0500 |
commit | 1fb9d6ad2766a1dd70d167552988375049a97f21 (patch) | |
tree | cee14f2d49bb40a2bed2f683c5a616990be93454 /kernel | |
parent | e40b17208b6805be50ffe891878662b6076206b9 (diff) |
nmi_watchdog: Add new, generic implementation, using perf events
This is a new generic nmi_watchdog implementation using the perf
events infrastructure as suggested by Ingo.
The implementation is simple, just create an in-kernel perf
event and register an overflow handler to check for cpu lockups.
I created a generic implementation that lives in kernel/ and
the hardware specific part that for now lives in arch/x86.
This approach has a number of advantages:
- It simplifies the x86 PMU implementation in the long run,
in that it removes the hardcoded low-level PMU implementation
that was the NMI watchdog before.
- It allows new NMI watchdog features to be added in a central
place.
- It allows other architectures to enable the NMI watchdog,
as long as they have perf events (that provide NMIs)
implemented.
- It also allows for more graceful co-existence of existing
perf events apps and the NMI watchdog - before these changes
the relationship was exclusive. (The NMI watchdog will 'spend'
a perf event when enabled. In later iterations we might be
able to piggyback from an existing NMI event without having
to allocate a hardware event for the NMI watchdog - turning
this into a no-hardware-cost feature.)
As for compatibility, we'll keep the old NMI watchdog code as
well until the new one can 100% replace it on all CPUs, old and
new alike. That might take some time as the NMI watchdog has
been ported to many CPU models.
I have done light testing to make sure the framework works
correctly and it does.
v2: Set the correct timeout values based on the old nmi
watchdog
Signed-off-by: Don Zickus <dzickus@redhat.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: gorcunov@gmail.com
Cc: aris@redhat.com
Cc: peterz@infradead.org
LKML-Reference: <1265424425-31562-3-git-send-email-dzickus@redhat.com>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
Diffstat (limited to 'kernel')
-rw-r--r-- | kernel/nmi_watchdog.c | 191 |
1 files changed, 191 insertions, 0 deletions
diff --git a/kernel/nmi_watchdog.c b/kernel/nmi_watchdog.c new file mode 100644 index 000000000000..36817b214d69 --- /dev/null +++ b/kernel/nmi_watchdog.c | |||
@@ -0,0 +1,191 @@ | |||
1 | /* | ||
2 | * Detect Hard Lockups using the NMI | ||
3 | * | ||
4 | * started by Don Zickus, Copyright (C) 2010 Red Hat, Inc. | ||
5 | * | ||
6 | * this code detects hard lockups: incidents in where on a CPU | ||
7 | * the kernel does not respond to anything except NMI. | ||
8 | * | ||
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. | ||
13 | */ | ||
14 | |||
15 | #include <linux/mm.h> | ||
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> | ||
25 | |||
26 | #include <asm/irq_regs.h> | ||
27 | #include <linux/perf_event.h> | ||
28 | |||
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); | ||
32 | |||
33 | void touch_nmi_watchdog(void) | ||
34 | { | ||
35 | __raw_get_cpu_var(nmi_watchdog_touch) = 1; | ||
36 | touch_softlockup_watchdog(); | ||
37 | } | ||
38 | EXPORT_SYMBOL(touch_nmi_watchdog); | ||
39 | |||
40 | void touch_all_nmi_watchdog(void) | ||
41 | { | ||
42 | int cpu; | ||
43 | |||
44 | for_each_online_cpu(cpu) | ||
45 | per_cpu(nmi_watchdog_touch, cpu) = 1; | ||
46 | touch_softlockup_watchdog(); | ||
47 | } | ||
48 | |||
49 | #ifdef CONFIG_SYSCTL | ||
50 | /* | ||
51 | * proc handler for /proc/sys/kernel/nmi_watchdog | ||
52 | */ | ||
53 | int proc_nmi_enabled(struct ctl_table *table, int write, | ||
54 | void __user *buffer, size_t *length, loff_t *ppos) | ||
55 | { | ||
56 | int cpu; | ||
57 | |||
58 | if (per_cpu(nmi_watchdog_ev, smp_processor_id()) == NULL) | ||
59 | nmi_watchdog_enabled = 0; | ||
60 | else | ||
61 | nmi_watchdog_enabled = 1; | ||
62 | |||
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)); | ||
68 | else | ||
69 | for_each_online_cpu(cpu) | ||
70 | perf_event_disable(per_cpu(nmi_watchdog_ev, cpu)); | ||
71 | return 0; | ||
72 | } | ||
73 | |||
74 | #endif /* CONFIG_SYSCTL */ | ||
75 | |||
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), | ||
80 | .pinned = 1, | ||
81 | .disabled = 1, | ||
82 | }; | ||
83 | |||
84 | static int panic_on_timeout; | ||
85 | |||
86 | void wd_overflow(struct perf_event *event, int nmi, | ||
87 | struct perf_sample_data *data, | ||
88 | struct pt_regs *regs) | ||
89 | { | ||
90 | int cpu = smp_processor_id(); | ||
91 | int touched = 0; | ||
92 | |||
93 | if (__get_cpu_var(nmi_watchdog_touch)) { | ||
94 | per_cpu(nmi_watchdog_touch, cpu) = 0; | ||
95 | touched = 1; | ||
96 | } | ||
97 | |||
98 | /* check to see if the cpu is doing anything */ | ||
99 | if (!touched && hw_nmi_is_cpu_stuck(regs)) { | ||
100 | /* | ||
101 | * Ayiee, looks like this CPU is stuck ... | ||
102 | * wait a few IRQs (5 seconds) before doing the oops ... | ||
103 | */ | ||
104 | per_cpu(alert_counter,cpu) += 1; | ||
105 | if (per_cpu(alert_counter,cpu) == 5) { | ||
106 | /* | ||
107 | * die_nmi will return ONLY if NOTIFY_STOP happens.. | ||
108 | */ | ||
109 | die_nmi("BUG: NMI Watchdog detected LOCKUP", | ||
110 | regs, panic_on_timeout); | ||
111 | } | ||
112 | } else { | ||
113 | per_cpu(alert_counter,cpu) = 0; | ||
114 | } | ||
115 | |||
116 | return; | ||
117 | } | ||
118 | |||
119 | /* | ||
120 | * Create/destroy watchdog threads as CPUs come and go: | ||
121 | */ | ||
122 | static int __cpuinit | ||
123 | cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu) | ||
124 | { | ||
125 | int hotcpu = (unsigned long)hcpu; | ||
126 | struct perf_event *event; | ||
127 | |||
128 | switch (action) { | ||
129 | case CPU_UP_PREPARE: | ||
130 | case CPU_UP_PREPARE_FROZEN: | ||
131 | per_cpu(nmi_watchdog_touch, hotcpu) = 0; | ||
132 | break; | ||
133 | case CPU_ONLINE: | ||
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); | ||
138 | if (IS_ERR(event)) { | ||
139 | printk(KERN_ERR "nmi watchdog failed to create perf event on %i: %p\n", hotcpu, event); | ||
140 | return NOTIFY_BAD; | ||
141 | } | ||
142 | per_cpu(nmi_watchdog_ev, hotcpu) = event; | ||
143 | perf_event_enable(per_cpu(nmi_watchdog_ev, hotcpu)); | ||
144 | break; | ||
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)); | ||
149 | case CPU_DEAD: | ||
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); | ||
154 | break; | ||
155 | #endif /* CONFIG_HOTPLUG_CPU */ | ||
156 | } | ||
157 | return NOTIFY_OK; | ||
158 | } | ||
159 | |||
160 | static struct notifier_block __cpuinitdata cpu_nfb = { | ||
161 | .notifier_call = cpu_callback | ||
162 | }; | ||
163 | |||
164 | static int __initdata nonmi_watchdog; | ||
165 | |||
166 | static int __init nonmi_watchdog_setup(char *str) | ||
167 | { | ||
168 | nonmi_watchdog = 1; | ||
169 | return 1; | ||
170 | } | ||
171 | __setup("nonmi_watchdog", nonmi_watchdog_setup); | ||
172 | |||
173 | static int __init spawn_nmi_watchdog_task(void) | ||
174 | { | ||
175 | void *cpu = (void *)(long)smp_processor_id(); | ||
176 | int err; | ||
177 | |||
178 | if (nonmi_watchdog) | ||
179 | return 0; | ||
180 | |||
181 | err = cpu_callback(&cpu_nfb, CPU_UP_PREPARE, cpu); | ||
182 | if (err == NOTIFY_BAD) { | ||
183 | BUG(); | ||
184 | return 1; | ||
185 | } | ||
186 | cpu_callback(&cpu_nfb, CPU_ONLINE, cpu); | ||
187 | register_cpu_notifier(&cpu_nfb); | ||
188 | |||
189 | return 0; | ||
190 | } | ||
191 | early_initcall(spawn_nmi_watchdog_task); | ||