]>
Commit | Line | Data |
---|---|---|
73ce0511 BM |
1 | /* |
2 | * Detect hard lockups on a system | |
3 | * | |
4 | * started by Don Zickus, Copyright (C) 2010 Red Hat, Inc. | |
5 | * | |
6 | * Note: Most of this code is borrowed heavily from the original softlockup | |
7 | * detector, so thanks to Ingo for the initial implementation. | |
8 | * Some chunks also taken from the old x86-specific nmi watchdog code, thanks | |
9 | * to those contributors as well. | |
10 | */ | |
11 | ||
12 | #define pr_fmt(fmt) "NMI watchdog: " fmt | |
13 | ||
14 | #include <linux/nmi.h> | |
15 | #include <linux/module.h> | |
b17b0153 IM |
16 | #include <linux/sched/debug.h> |
17 | ||
73ce0511 BM |
18 | #include <asm/irq_regs.h> |
19 | #include <linux/perf_event.h> | |
20 | ||
21 | static DEFINE_PER_CPU(bool, hard_watchdog_warn); | |
22 | static DEFINE_PER_CPU(bool, watchdog_nmi_touch); | |
23 | static DEFINE_PER_CPU(struct perf_event *, watchdog_ev); | |
941154bd TG |
24 | static DEFINE_PER_CPU(struct perf_event *, dead_event); |
25 | static struct cpumask dead_events_mask; | |
73ce0511 | 26 | |
73ce0511 | 27 | static unsigned long hardlockup_allcpu_dumped; |
20d853fd | 28 | static bool hardlockup_detector_disabled; |
73ce0511 | 29 | |
f2e0cff8 | 30 | void arch_touch_nmi_watchdog(void) |
73ce0511 BM |
31 | { |
32 | /* | |
33 | * Using __raw here because some code paths have | |
34 | * preemption enabled. If preemption is enabled | |
35 | * then interrupts should be enabled too, in which | |
36 | * case we shouldn't have to worry about the watchdog | |
37 | * going off. | |
38 | */ | |
39 | raw_cpu_write(watchdog_nmi_touch, true); | |
73ce0511 | 40 | } |
f2e0cff8 | 41 | EXPORT_SYMBOL(arch_touch_nmi_watchdog); |
73ce0511 | 42 | |
7edaeb68 TG |
43 | #ifdef CONFIG_HARDLOCKUP_CHECK_TIMESTAMP |
44 | static DEFINE_PER_CPU(ktime_t, last_timestamp); | |
45 | static DEFINE_PER_CPU(unsigned int, nmi_rearmed); | |
46 | static ktime_t watchdog_hrtimer_sample_threshold __read_mostly; | |
47 | ||
48 | void watchdog_update_hrtimer_threshold(u64 period) | |
49 | { | |
50 | /* | |
51 | * The hrtimer runs with a period of (watchdog_threshold * 2) / 5 | |
52 | * | |
53 | * So it runs effectively with 2.5 times the rate of the NMI | |
54 | * watchdog. That means the hrtimer should fire 2-3 times before | |
55 | * the NMI watchdog expires. The NMI watchdog on x86 is based on | |
56 | * unhalted CPU cycles, so if Turbo-Mode is enabled the CPU cycles | |
57 | * might run way faster than expected and the NMI fires in a | |
58 | * smaller period than the one deduced from the nominal CPU | |
59 | * frequency. Depending on the Turbo-Mode factor this might be fast | |
60 | * enough to get the NMI period smaller than the hrtimer watchdog | |
61 | * period and trigger false positives. | |
62 | * | |
63 | * The sample threshold is used to check in the NMI handler whether | |
64 | * the minimum time between two NMI samples has elapsed. That | |
65 | * prevents false positives. | |
66 | * | |
67 | * Set this to 4/5 of the actual watchdog threshold period so the | |
68 | * hrtimer is guaranteed to fire at least once within the real | |
69 | * watchdog threshold. | |
70 | */ | |
71 | watchdog_hrtimer_sample_threshold = period * 2; | |
72 | } | |
73 | ||
74 | static bool watchdog_check_timestamp(void) | |
75 | { | |
76 | ktime_t delta, now = ktime_get_mono_fast_ns(); | |
77 | ||
78 | delta = now - __this_cpu_read(last_timestamp); | |
79 | if (delta < watchdog_hrtimer_sample_threshold) { | |
80 | /* | |
81 | * If ktime is jiffies based, a stalled timer would prevent | |
82 | * jiffies from being incremented and the filter would look | |
83 | * at a stale timestamp and never trigger. | |
84 | */ | |
85 | if (__this_cpu_inc_return(nmi_rearmed) < 10) | |
86 | return false; | |
87 | } | |
88 | __this_cpu_write(nmi_rearmed, 0); | |
89 | __this_cpu_write(last_timestamp, now); | |
90 | return true; | |
91 | } | |
92 | #else | |
93 | static inline bool watchdog_check_timestamp(void) | |
94 | { | |
95 | return true; | |
96 | } | |
97 | #endif | |
98 | ||
73ce0511 BM |
99 | static struct perf_event_attr wd_hw_attr = { |
100 | .type = PERF_TYPE_HARDWARE, | |
101 | .config = PERF_COUNT_HW_CPU_CYCLES, | |
102 | .size = sizeof(struct perf_event_attr), | |
103 | .pinned = 1, | |
104 | .disabled = 1, | |
105 | }; | |
106 | ||
107 | /* Callback function for perf event subsystem */ | |
108 | static void watchdog_overflow_callback(struct perf_event *event, | |
109 | struct perf_sample_data *data, | |
110 | struct pt_regs *regs) | |
111 | { | |
112 | /* Ensure the watchdog never gets throttled */ | |
113 | event->hw.interrupts = 0; | |
114 | ||
b94f5118 DZ |
115 | if (atomic_read(&watchdog_park_in_progress) != 0) |
116 | return; | |
117 | ||
73ce0511 BM |
118 | if (__this_cpu_read(watchdog_nmi_touch) == true) { |
119 | __this_cpu_write(watchdog_nmi_touch, false); | |
120 | return; | |
121 | } | |
122 | ||
7edaeb68 TG |
123 | if (!watchdog_check_timestamp()) |
124 | return; | |
125 | ||
73ce0511 BM |
126 | /* check for a hardlockup |
127 | * This is done by making sure our timer interrupt | |
128 | * is incrementing. The timer interrupt should have | |
129 | * fired multiple times before we overflow'd. If it hasn't | |
130 | * then this is a good indication the cpu is stuck | |
131 | */ | |
132 | if (is_hardlockup()) { | |
133 | int this_cpu = smp_processor_id(); | |
134 | ||
135 | /* only print hardlockups once */ | |
136 | if (__this_cpu_read(hard_watchdog_warn) == true) | |
137 | return; | |
138 | ||
139 | pr_emerg("Watchdog detected hard LOCKUP on cpu %d", this_cpu); | |
140 | print_modules(); | |
141 | print_irqtrace_events(current); | |
142 | if (regs) | |
143 | show_regs(regs); | |
144 | else | |
145 | dump_stack(); | |
146 | ||
147 | /* | |
148 | * Perform all-CPU dump only once to avoid multiple hardlockups | |
149 | * generating interleaving traces | |
150 | */ | |
151 | if (sysctl_hardlockup_all_cpu_backtrace && | |
152 | !test_and_set_bit(0, &hardlockup_allcpu_dumped)) | |
153 | trigger_allbutself_cpu_backtrace(); | |
154 | ||
155 | if (hardlockup_panic) | |
156 | nmi_panic(regs, "Hard LOCKUP"); | |
157 | ||
158 | __this_cpu_write(hard_watchdog_warn, true); | |
159 | return; | |
160 | } | |
161 | ||
162 | __this_cpu_write(hard_watchdog_warn, false); | |
163 | return; | |
164 | } | |
165 | ||
166 | /* | |
167 | * People like the simple clean cpu node info on boot. | |
168 | * Reduce the watchdog noise by only printing messages | |
169 | * that are different from what cpu0 displayed. | |
170 | */ | |
8dcde9de PB |
171 | static unsigned long firstcpu_err; |
172 | static atomic_t watchdog_cpus; | |
73ce0511 BM |
173 | |
174 | int watchdog_nmi_enable(unsigned int cpu) | |
175 | { | |
176 | struct perf_event_attr *wd_attr; | |
177 | struct perf_event *event = per_cpu(watchdog_ev, cpu); | |
8dcde9de | 178 | int firstcpu = 0; |
73ce0511 BM |
179 | |
180 | /* nothing to do if the hard lockup detector is disabled */ | |
181 | if (!(watchdog_enabled & NMI_WATCHDOG_ENABLED)) | |
182 | goto out; | |
183 | ||
20d853fd TG |
184 | /* A failure disabled the hardlockup detector permanently */ |
185 | if (hardlockup_detector_disabled) | |
186 | return -ENODEV; | |
187 | ||
73ce0511 BM |
188 | /* is it already setup and enabled? */ |
189 | if (event && event->state > PERF_EVENT_STATE_OFF) | |
190 | goto out; | |
191 | ||
192 | /* it is setup but not enabled */ | |
193 | if (event != NULL) | |
194 | goto out_enable; | |
195 | ||
8dcde9de PB |
196 | if (atomic_inc_return(&watchdog_cpus) == 1) |
197 | firstcpu = 1; | |
198 | ||
73ce0511 BM |
199 | wd_attr = &wd_hw_attr; |
200 | wd_attr->sample_period = hw_nmi_get_sample_period(watchdog_thresh); | |
201 | ||
202 | /* Try to register using hardware perf events */ | |
203 | event = perf_event_create_kernel_counter(wd_attr, cpu, NULL, watchdog_overflow_callback, NULL); | |
204 | ||
8dcde9de PB |
205 | /* save the first cpu's error for future comparision */ |
206 | if (firstcpu && IS_ERR(event)) | |
207 | firstcpu_err = PTR_ERR(event); | |
73ce0511 BM |
208 | |
209 | if (!IS_ERR(event)) { | |
8dcde9de PB |
210 | /* only print for the first cpu initialized */ |
211 | if (firstcpu || firstcpu_err) | |
73ce0511 BM |
212 | pr_info("enabled on all CPUs, permanently consumes one hw-PMU counter.\n"); |
213 | goto out_save; | |
214 | } | |
215 | ||
73ce0511 | 216 | /* skip displaying the same error again */ |
8dcde9de | 217 | if (!firstcpu && (PTR_ERR(event) == firstcpu_err)) |
73ce0511 BM |
218 | return PTR_ERR(event); |
219 | ||
220 | /* vary the KERN level based on the returned errno */ | |
221 | if (PTR_ERR(event) == -EOPNOTSUPP) | |
222 | pr_info("disabled (cpu%i): not supported (no LAPIC?)\n", cpu); | |
223 | else if (PTR_ERR(event) == -ENOENT) | |
224 | pr_warn("disabled (cpu%i): hardware events not enabled\n", | |
225 | cpu); | |
226 | else | |
227 | pr_err("disabled (cpu%i): unable to create perf event: %ld\n", | |
228 | cpu, PTR_ERR(event)); | |
229 | ||
20d853fd TG |
230 | pr_info("Disabling hard lockup detector permanently\n"); |
231 | hardlockup_detector_disabled = true; | |
73ce0511 BM |
232 | |
233 | return PTR_ERR(event); | |
234 | ||
235 | /* success path */ | |
236 | out_save: | |
237 | per_cpu(watchdog_ev, cpu) = event; | |
238 | out_enable: | |
239 | perf_event_enable(per_cpu(watchdog_ev, cpu)); | |
240 | out: | |
241 | return 0; | |
242 | } | |
243 | ||
941154bd TG |
244 | /** |
245 | * hardlockup_detector_perf_disable - Disable the local event | |
246 | */ | |
247 | void hardlockup_detector_perf_disable(void) | |
73ce0511 | 248 | { |
941154bd | 249 | struct perf_event *event = this_cpu_read(watchdog_ev); |
73ce0511 BM |
250 | |
251 | if (event) { | |
252 | perf_event_disable(event); | |
941154bd TG |
253 | this_cpu_write(watchdog_ev, NULL); |
254 | this_cpu_write(dead_event, event); | |
255 | cpumask_set_cpu(smp_processor_id(), &dead_events_mask); | |
8dcde9de | 256 | |
73ce0511 | 257 | /* watchdog_nmi_enable() expects this to be zero initially. */ |
8dcde9de PB |
258 | if (atomic_dec_and_test(&watchdog_cpus)) |
259 | firstcpu_err = 0; | |
73ce0511 BM |
260 | } |
261 | } | |
d0b6e0a8 | 262 | |
941154bd TG |
263 | /** |
264 | * hardlockup_detector_perf_cleanup - Cleanup disabled events and destroy them | |
265 | * | |
266 | * Called from lockup_detector_cleanup(). Serialized by the caller. | |
267 | */ | |
268 | void hardlockup_detector_perf_cleanup(void) | |
269 | { | |
270 | int cpu; | |
271 | ||
272 | for_each_cpu(cpu, &dead_events_mask) { | |
273 | struct perf_event *event = per_cpu(dead_event, cpu); | |
274 | ||
275 | per_cpu(dead_event, cpu) = NULL; | |
276 | perf_event_release_kernel(event); | |
277 | } | |
278 | cpumask_clear(&dead_events_mask); | |
279 | } | |
280 | ||
d0b6e0a8 PZ |
281 | /** |
282 | * hardlockup_detector_perf_stop - Globally stop watchdog events | |
283 | * | |
284 | * Special interface for x86 to handle the perf HT bug. | |
285 | */ | |
286 | void __init hardlockup_detector_perf_stop(void) | |
287 | { | |
288 | int cpu; | |
289 | ||
290 | lockdep_assert_cpus_held(); | |
291 | ||
292 | for_each_online_cpu(cpu) { | |
293 | struct perf_event *event = per_cpu(watchdog_ev, cpu); | |
294 | ||
295 | if (event) | |
296 | perf_event_disable(event); | |
297 | } | |
298 | } | |
299 | ||
300 | /** | |
301 | * hardlockup_detector_perf_restart - Globally restart watchdog events | |
302 | * | |
303 | * Special interface for x86 to handle the perf HT bug. | |
304 | */ | |
305 | void __init hardlockup_detector_perf_restart(void) | |
306 | { | |
307 | int cpu; | |
308 | ||
309 | lockdep_assert_cpus_held(); | |
310 | ||
311 | if (!(watchdog_enabled & NMI_WATCHDOG_ENABLED)) | |
312 | return; | |
313 | ||
314 | for_each_online_cpu(cpu) { | |
315 | struct perf_event *event = per_cpu(watchdog_ev, cpu); | |
316 | ||
317 | if (event) | |
318 | perf_event_enable(event); | |
319 | } | |
320 | } |