]>
Commit | Line | Data |
---|---|---|
58687acb DZ |
1 | /* |
2 | * Detect hard and soft lockups on a system | |
3 | * | |
4 | * started by Don Zickus, Copyright (C) 2010 Red Hat, Inc. | |
5 | * | |
86f5e6a7 FLVC |
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 | |
58687acb DZ |
9 | * to those contributors as well. |
10 | */ | |
11 | ||
4501980a AM |
12 | #define pr_fmt(fmt) "NMI watchdog: " fmt |
13 | ||
58687acb DZ |
14 | #include <linux/mm.h> |
15 | #include <linux/cpu.h> | |
16 | #include <linux/nmi.h> | |
17 | #include <linux/init.h> | |
58687acb DZ |
18 | #include <linux/module.h> |
19 | #include <linux/sysctl.h> | |
bcd951cf | 20 | #include <linux/smpboot.h> |
8bd75c77 | 21 | #include <linux/sched/rt.h> |
58687acb DZ |
22 | |
23 | #include <asm/irq_regs.h> | |
5d1c0f4a | 24 | #include <linux/kvm_para.h> |
58687acb DZ |
25 | #include <linux/perf_event.h> |
26 | ||
3c00ea82 | 27 | int watchdog_user_enabled = 1; |
4eec42f3 | 28 | int __read_mostly watchdog_thresh = 10; |
ed235875 AT |
29 | #ifdef CONFIG_SMP |
30 | int __read_mostly sysctl_softlockup_all_cpu_backtrace; | |
31 | #else | |
32 | #define sysctl_softlockup_all_cpu_backtrace 0 | |
33 | #endif | |
34 | ||
3c00ea82 | 35 | static int __read_mostly watchdog_running; |
0f34c400 | 36 | static u64 __read_mostly sample_period; |
58687acb DZ |
37 | |
38 | static DEFINE_PER_CPU(unsigned long, watchdog_touch_ts); | |
39 | static DEFINE_PER_CPU(struct task_struct *, softlockup_watchdog); | |
40 | static DEFINE_PER_CPU(struct hrtimer, watchdog_hrtimer); | |
41 | static DEFINE_PER_CPU(bool, softlockup_touch_sync); | |
58687acb | 42 | static DEFINE_PER_CPU(bool, soft_watchdog_warn); |
bcd951cf TG |
43 | static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts); |
44 | static DEFINE_PER_CPU(unsigned long, soft_lockup_hrtimer_cnt); | |
b1a8de1f | 45 | static DEFINE_PER_CPU(struct task_struct *, softlockup_task_ptr_saved); |
23637d47 | 46 | #ifdef CONFIG_HARDLOCKUP_DETECTOR |
cafcd80d DZ |
47 | static DEFINE_PER_CPU(bool, hard_watchdog_warn); |
48 | static DEFINE_PER_CPU(bool, watchdog_nmi_touch); | |
58687acb DZ |
49 | static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts_saved); |
50 | static DEFINE_PER_CPU(struct perf_event *, watchdog_ev); | |
51 | #endif | |
ed235875 | 52 | static unsigned long soft_lockup_nmi_warn; |
58687acb | 53 | |
58687acb DZ |
54 | /* boot commands */ |
55 | /* | |
56 | * Should we panic when a soft-lockup or hard-lockup occurs: | |
57 | */ | |
23637d47 | 58 | #ifdef CONFIG_HARDLOCKUP_DETECTOR |
fef2c9bc DZ |
59 | static int hardlockup_panic = |
60 | CONFIG_BOOTPARAM_HARDLOCKUP_PANIC_VALUE; | |
58687acb DZ |
61 | |
62 | static int __init hardlockup_panic_setup(char *str) | |
63 | { | |
64 | if (!strncmp(str, "panic", 5)) | |
65 | hardlockup_panic = 1; | |
fef2c9bc DZ |
66 | else if (!strncmp(str, "nopanic", 7)) |
67 | hardlockup_panic = 0; | |
5dc30558 | 68 | else if (!strncmp(str, "0", 1)) |
3c00ea82 | 69 | watchdog_user_enabled = 0; |
58687acb DZ |
70 | return 1; |
71 | } | |
72 | __setup("nmi_watchdog=", hardlockup_panic_setup); | |
73 | #endif | |
74 | ||
75 | unsigned int __read_mostly softlockup_panic = | |
76 | CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC_VALUE; | |
77 | ||
78 | static int __init softlockup_panic_setup(char *str) | |
79 | { | |
80 | softlockup_panic = simple_strtoul(str, NULL, 0); | |
81 | ||
82 | return 1; | |
83 | } | |
84 | __setup("softlockup_panic=", softlockup_panic_setup); | |
85 | ||
86 | static int __init nowatchdog_setup(char *str) | |
87 | { | |
3c00ea82 | 88 | watchdog_user_enabled = 0; |
58687acb DZ |
89 | return 1; |
90 | } | |
91 | __setup("nowatchdog", nowatchdog_setup); | |
92 | ||
93 | /* deprecated */ | |
94 | static int __init nosoftlockup_setup(char *str) | |
95 | { | |
3c00ea82 | 96 | watchdog_user_enabled = 0; |
58687acb DZ |
97 | return 1; |
98 | } | |
99 | __setup("nosoftlockup", nosoftlockup_setup); | |
100 | /* */ | |
ed235875 AT |
101 | #ifdef CONFIG_SMP |
102 | static int __init softlockup_all_cpu_backtrace_setup(char *str) | |
103 | { | |
104 | sysctl_softlockup_all_cpu_backtrace = | |
105 | !!simple_strtol(str, NULL, 0); | |
106 | return 1; | |
107 | } | |
108 | __setup("softlockup_all_cpu_backtrace=", softlockup_all_cpu_backtrace_setup); | |
109 | #endif | |
58687acb | 110 | |
4eec42f3 MSB |
111 | /* |
112 | * Hard-lockup warnings should be triggered after just a few seconds. Soft- | |
113 | * lockups can have false positives under extreme conditions. So we generally | |
114 | * want a higher threshold for soft lockups than for hard lockups. So we couple | |
115 | * the thresholds with a factor: we make the soft threshold twice the amount of | |
116 | * time the hard threshold is. | |
117 | */ | |
6e9101ae | 118 | static int get_softlockup_thresh(void) |
4eec42f3 MSB |
119 | { |
120 | return watchdog_thresh * 2; | |
121 | } | |
58687acb DZ |
122 | |
123 | /* | |
124 | * Returns seconds, approximately. We don't need nanosecond | |
125 | * resolution, and we don't need to waste time with a big divide when | |
126 | * 2^30ns == 1.074s. | |
127 | */ | |
c06b4f19 | 128 | static unsigned long get_timestamp(void) |
58687acb | 129 | { |
c06b4f19 | 130 | return local_clock() >> 30LL; /* 2^30 ~= 10^9 */ |
58687acb DZ |
131 | } |
132 | ||
0f34c400 | 133 | static void set_sample_period(void) |
58687acb DZ |
134 | { |
135 | /* | |
586692a5 | 136 | * convert watchdog_thresh from seconds to ns |
86f5e6a7 FLVC |
137 | * the divide by 5 is to give hrtimer several chances (two |
138 | * or three with the current relation between the soft | |
139 | * and hard thresholds) to increment before the | |
140 | * hardlockup detector generates a warning | |
58687acb | 141 | */ |
0f34c400 | 142 | sample_period = get_softlockup_thresh() * ((u64)NSEC_PER_SEC / 5); |
58687acb DZ |
143 | } |
144 | ||
145 | /* Commands for resetting the watchdog */ | |
146 | static void __touch_watchdog(void) | |
147 | { | |
c06b4f19 | 148 | __this_cpu_write(watchdog_touch_ts, get_timestamp()); |
58687acb DZ |
149 | } |
150 | ||
332fbdbc | 151 | void touch_softlockup_watchdog(void) |
58687acb | 152 | { |
7861144b AM |
153 | /* |
154 | * Preemption can be enabled. It doesn't matter which CPU's timestamp | |
155 | * gets zeroed here, so use the raw_ operation. | |
156 | */ | |
157 | raw_cpu_write(watchdog_touch_ts, 0); | |
58687acb | 158 | } |
0167c781 | 159 | EXPORT_SYMBOL(touch_softlockup_watchdog); |
58687acb | 160 | |
332fbdbc | 161 | void touch_all_softlockup_watchdogs(void) |
58687acb DZ |
162 | { |
163 | int cpu; | |
164 | ||
165 | /* | |
166 | * this is done lockless | |
167 | * do we care if a 0 races with a timestamp? | |
168 | * all it means is the softlock check starts one cycle later | |
169 | */ | |
170 | for_each_online_cpu(cpu) | |
171 | per_cpu(watchdog_touch_ts, cpu) = 0; | |
172 | } | |
173 | ||
cafcd80d | 174 | #ifdef CONFIG_HARDLOCKUP_DETECTOR |
58687acb DZ |
175 | void touch_nmi_watchdog(void) |
176 | { | |
62572e29 BZ |
177 | /* |
178 | * Using __raw here because some code paths have | |
179 | * preemption enabled. If preemption is enabled | |
180 | * then interrupts should be enabled too, in which | |
181 | * case we shouldn't have to worry about the watchdog | |
182 | * going off. | |
183 | */ | |
184 | __raw_get_cpu_var(watchdog_nmi_touch) = true; | |
332fbdbc | 185 | touch_softlockup_watchdog(); |
58687acb DZ |
186 | } |
187 | EXPORT_SYMBOL(touch_nmi_watchdog); | |
188 | ||
cafcd80d DZ |
189 | #endif |
190 | ||
58687acb DZ |
191 | void touch_softlockup_watchdog_sync(void) |
192 | { | |
193 | __raw_get_cpu_var(softlockup_touch_sync) = true; | |
194 | __raw_get_cpu_var(watchdog_touch_ts) = 0; | |
195 | } | |
196 | ||
23637d47 | 197 | #ifdef CONFIG_HARDLOCKUP_DETECTOR |
58687acb | 198 | /* watchdog detector functions */ |
26e09c6e | 199 | static int is_hardlockup(void) |
58687acb | 200 | { |
909ea964 | 201 | unsigned long hrint = __this_cpu_read(hrtimer_interrupts); |
58687acb | 202 | |
909ea964 | 203 | if (__this_cpu_read(hrtimer_interrupts_saved) == hrint) |
58687acb DZ |
204 | return 1; |
205 | ||
909ea964 | 206 | __this_cpu_write(hrtimer_interrupts_saved, hrint); |
58687acb DZ |
207 | return 0; |
208 | } | |
209 | #endif | |
210 | ||
26e09c6e | 211 | static int is_softlockup(unsigned long touch_ts) |
58687acb | 212 | { |
c06b4f19 | 213 | unsigned long now = get_timestamp(); |
58687acb DZ |
214 | |
215 | /* Warn about unreasonable delays: */ | |
4eec42f3 | 216 | if (time_after(now, touch_ts + get_softlockup_thresh())) |
58687acb DZ |
217 | return now - touch_ts; |
218 | ||
219 | return 0; | |
220 | } | |
221 | ||
23637d47 | 222 | #ifdef CONFIG_HARDLOCKUP_DETECTOR |
1880c4ae | 223 | |
58687acb DZ |
224 | static struct perf_event_attr wd_hw_attr = { |
225 | .type = PERF_TYPE_HARDWARE, | |
226 | .config = PERF_COUNT_HW_CPU_CYCLES, | |
227 | .size = sizeof(struct perf_event_attr), | |
228 | .pinned = 1, | |
229 | .disabled = 1, | |
230 | }; | |
231 | ||
232 | /* Callback function for perf event subsystem */ | |
a8b0ca17 | 233 | static void watchdog_overflow_callback(struct perf_event *event, |
58687acb DZ |
234 | struct perf_sample_data *data, |
235 | struct pt_regs *regs) | |
236 | { | |
c6db67cd PZ |
237 | /* Ensure the watchdog never gets throttled */ |
238 | event->hw.interrupts = 0; | |
239 | ||
909ea964 CL |
240 | if (__this_cpu_read(watchdog_nmi_touch) == true) { |
241 | __this_cpu_write(watchdog_nmi_touch, false); | |
58687acb DZ |
242 | return; |
243 | } | |
244 | ||
245 | /* check for a hardlockup | |
246 | * This is done by making sure our timer interrupt | |
247 | * is incrementing. The timer interrupt should have | |
248 | * fired multiple times before we overflow'd. If it hasn't | |
249 | * then this is a good indication the cpu is stuck | |
250 | */ | |
26e09c6e DZ |
251 | if (is_hardlockup()) { |
252 | int this_cpu = smp_processor_id(); | |
253 | ||
58687acb | 254 | /* only print hardlockups once */ |
909ea964 | 255 | if (__this_cpu_read(hard_watchdog_warn) == true) |
58687acb DZ |
256 | return; |
257 | ||
258 | if (hardlockup_panic) | |
656c3b79 FF |
259 | panic("Watchdog detected hard LOCKUP on cpu %d", |
260 | this_cpu); | |
58687acb | 261 | else |
656c3b79 FF |
262 | WARN(1, "Watchdog detected hard LOCKUP on cpu %d", |
263 | this_cpu); | |
58687acb | 264 | |
909ea964 | 265 | __this_cpu_write(hard_watchdog_warn, true); |
58687acb DZ |
266 | return; |
267 | } | |
268 | ||
909ea964 | 269 | __this_cpu_write(hard_watchdog_warn, false); |
58687acb DZ |
270 | return; |
271 | } | |
bcd951cf TG |
272 | #endif /* CONFIG_HARDLOCKUP_DETECTOR */ |
273 | ||
58687acb DZ |
274 | static void watchdog_interrupt_count(void) |
275 | { | |
909ea964 | 276 | __this_cpu_inc(hrtimer_interrupts); |
58687acb | 277 | } |
bcd951cf TG |
278 | |
279 | static int watchdog_nmi_enable(unsigned int cpu); | |
280 | static void watchdog_nmi_disable(unsigned int cpu); | |
58687acb DZ |
281 | |
282 | /* watchdog kicker functions */ | |
283 | static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer) | |
284 | { | |
909ea964 | 285 | unsigned long touch_ts = __this_cpu_read(watchdog_touch_ts); |
58687acb DZ |
286 | struct pt_regs *regs = get_irq_regs(); |
287 | int duration; | |
ed235875 | 288 | int softlockup_all_cpu_backtrace = sysctl_softlockup_all_cpu_backtrace; |
58687acb DZ |
289 | |
290 | /* kick the hardlockup detector */ | |
291 | watchdog_interrupt_count(); | |
292 | ||
293 | /* kick the softlockup detector */ | |
909ea964 | 294 | wake_up_process(__this_cpu_read(softlockup_watchdog)); |
58687acb DZ |
295 | |
296 | /* .. and repeat */ | |
0f34c400 | 297 | hrtimer_forward_now(hrtimer, ns_to_ktime(sample_period)); |
58687acb DZ |
298 | |
299 | if (touch_ts == 0) { | |
909ea964 | 300 | if (unlikely(__this_cpu_read(softlockup_touch_sync))) { |
58687acb DZ |
301 | /* |
302 | * If the time stamp was touched atomically | |
303 | * make sure the scheduler tick is up to date. | |
304 | */ | |
909ea964 | 305 | __this_cpu_write(softlockup_touch_sync, false); |
58687acb DZ |
306 | sched_clock_tick(); |
307 | } | |
5d1c0f4a EM |
308 | |
309 | /* Clear the guest paused flag on watchdog reset */ | |
310 | kvm_check_and_clear_guest_paused(); | |
58687acb DZ |
311 | __touch_watchdog(); |
312 | return HRTIMER_RESTART; | |
313 | } | |
314 | ||
315 | /* check for a softlockup | |
316 | * This is done by making sure a high priority task is | |
317 | * being scheduled. The task touches the watchdog to | |
318 | * indicate it is getting cpu time. If it hasn't then | |
319 | * this is a good indication some task is hogging the cpu | |
320 | */ | |
26e09c6e | 321 | duration = is_softlockup(touch_ts); |
58687acb | 322 | if (unlikely(duration)) { |
5d1c0f4a EM |
323 | /* |
324 | * If a virtual machine is stopped by the host it can look to | |
325 | * the watchdog like a soft lockup, check to see if the host | |
326 | * stopped the vm before we issue the warning | |
327 | */ | |
328 | if (kvm_check_and_clear_guest_paused()) | |
329 | return HRTIMER_RESTART; | |
330 | ||
58687acb | 331 | /* only warn once */ |
b1a8de1f | 332 | if (__this_cpu_read(soft_watchdog_warn) == true) { |
333 | /* | |
334 | * When multiple processes are causing softlockups the | |
335 | * softlockup detector only warns on the first one | |
336 | * because the code relies on a full quiet cycle to | |
337 | * re-arm. The second process prevents the quiet cycle | |
338 | * and never gets reported. Use task pointers to detect | |
339 | * this. | |
340 | */ | |
341 | if (__this_cpu_read(softlockup_task_ptr_saved) != | |
342 | current) { | |
343 | __this_cpu_write(soft_watchdog_warn, false); | |
344 | __touch_watchdog(); | |
345 | } | |
58687acb | 346 | return HRTIMER_RESTART; |
b1a8de1f | 347 | } |
58687acb | 348 | |
ed235875 AT |
349 | if (softlockup_all_cpu_backtrace) { |
350 | /* Prevent multiple soft-lockup reports if one cpu is already | |
351 | * engaged in dumping cpu back traces | |
352 | */ | |
353 | if (test_and_set_bit(0, &soft_lockup_nmi_warn)) { | |
354 | /* Someone else will report us. Let's give up */ | |
355 | __this_cpu_write(soft_watchdog_warn, true); | |
356 | return HRTIMER_RESTART; | |
357 | } | |
358 | } | |
359 | ||
656c3b79 | 360 | pr_emerg("BUG: soft lockup - CPU#%d stuck for %us! [%s:%d]\n", |
26e09c6e | 361 | smp_processor_id(), duration, |
58687acb | 362 | current->comm, task_pid_nr(current)); |
b1a8de1f | 363 | __this_cpu_write(softlockup_task_ptr_saved, current); |
58687acb DZ |
364 | print_modules(); |
365 | print_irqtrace_events(current); | |
366 | if (regs) | |
367 | show_regs(regs); | |
368 | else | |
369 | dump_stack(); | |
370 | ||
ed235875 AT |
371 | if (softlockup_all_cpu_backtrace) { |
372 | /* Avoid generating two back traces for current | |
373 | * given that one is already made above | |
374 | */ | |
375 | trigger_allbutself_cpu_backtrace(); | |
376 | ||
377 | clear_bit(0, &soft_lockup_nmi_warn); | |
378 | /* Barrier to sync with other cpus */ | |
379 | smp_mb__after_atomic(); | |
380 | } | |
381 | ||
69361eef | 382 | add_taint(TAINT_SOFTLOCKUP, LOCKDEP_STILL_OK); |
58687acb DZ |
383 | if (softlockup_panic) |
384 | panic("softlockup: hung tasks"); | |
909ea964 | 385 | __this_cpu_write(soft_watchdog_warn, true); |
58687acb | 386 | } else |
909ea964 | 387 | __this_cpu_write(soft_watchdog_warn, false); |
58687acb DZ |
388 | |
389 | return HRTIMER_RESTART; | |
390 | } | |
391 | ||
bcd951cf TG |
392 | static void watchdog_set_prio(unsigned int policy, unsigned int prio) |
393 | { | |
394 | struct sched_param param = { .sched_priority = prio }; | |
58687acb | 395 | |
bcd951cf TG |
396 | sched_setscheduler(current, policy, ¶m); |
397 | } | |
398 | ||
399 | static void watchdog_enable(unsigned int cpu) | |
58687acb | 400 | { |
26e09c6e | 401 | struct hrtimer *hrtimer = &__raw_get_cpu_var(watchdog_hrtimer); |
58687acb | 402 | |
3935e895 BM |
403 | /* kick off the timer for the hardlockup detector */ |
404 | hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); | |
405 | hrtimer->function = watchdog_timer_fn; | |
406 | ||
bcd951cf TG |
407 | /* Enable the perf event */ |
408 | watchdog_nmi_enable(cpu); | |
58687acb | 409 | |
58687acb | 410 | /* done here because hrtimer_start can only pin to smp_processor_id() */ |
0f34c400 | 411 | hrtimer_start(hrtimer, ns_to_ktime(sample_period), |
58687acb DZ |
412 | HRTIMER_MODE_REL_PINNED); |
413 | ||
bcd951cf TG |
414 | /* initialize timestamp */ |
415 | watchdog_set_prio(SCHED_FIFO, MAX_RT_PRIO - 1); | |
416 | __touch_watchdog(); | |
417 | } | |
58687acb | 418 | |
bcd951cf TG |
419 | static void watchdog_disable(unsigned int cpu) |
420 | { | |
421 | struct hrtimer *hrtimer = &__raw_get_cpu_var(watchdog_hrtimer); | |
58687acb | 422 | |
bcd951cf TG |
423 | watchdog_set_prio(SCHED_NORMAL, 0); |
424 | hrtimer_cancel(hrtimer); | |
425 | /* disable the perf event */ | |
426 | watchdog_nmi_disable(cpu); | |
58687acb DZ |
427 | } |
428 | ||
b8900bc0 FW |
429 | static void watchdog_cleanup(unsigned int cpu, bool online) |
430 | { | |
431 | watchdog_disable(cpu); | |
432 | } | |
433 | ||
bcd951cf TG |
434 | static int watchdog_should_run(unsigned int cpu) |
435 | { | |
436 | return __this_cpu_read(hrtimer_interrupts) != | |
437 | __this_cpu_read(soft_lockup_hrtimer_cnt); | |
438 | } | |
439 | ||
440 | /* | |
441 | * The watchdog thread function - touches the timestamp. | |
442 | * | |
0f34c400 | 443 | * It only runs once every sample_period seconds (4 seconds by |
bcd951cf TG |
444 | * default) to reset the softlockup timestamp. If this gets delayed |
445 | * for more than 2*watchdog_thresh seconds then the debug-printout | |
446 | * triggers in watchdog_timer_fn(). | |
447 | */ | |
448 | static void watchdog(unsigned int cpu) | |
449 | { | |
450 | __this_cpu_write(soft_lockup_hrtimer_cnt, | |
451 | __this_cpu_read(hrtimer_interrupts)); | |
452 | __touch_watchdog(); | |
453 | } | |
58687acb | 454 | |
23637d47 | 455 | #ifdef CONFIG_HARDLOCKUP_DETECTOR |
a7027046 DZ |
456 | /* |
457 | * People like the simple clean cpu node info on boot. | |
458 | * Reduce the watchdog noise by only printing messages | |
459 | * that are different from what cpu0 displayed. | |
460 | */ | |
461 | static unsigned long cpu0_err; | |
462 | ||
bcd951cf | 463 | static int watchdog_nmi_enable(unsigned int cpu) |
58687acb DZ |
464 | { |
465 | struct perf_event_attr *wd_attr; | |
466 | struct perf_event *event = per_cpu(watchdog_ev, cpu); | |
467 | ||
468 | /* is it already setup and enabled? */ | |
469 | if (event && event->state > PERF_EVENT_STATE_OFF) | |
470 | goto out; | |
471 | ||
472 | /* it is setup but not enabled */ | |
473 | if (event != NULL) | |
474 | goto out_enable; | |
475 | ||
58687acb | 476 | wd_attr = &wd_hw_attr; |
4eec42f3 | 477 | wd_attr->sample_period = hw_nmi_get_sample_period(watchdog_thresh); |
1880c4ae CG |
478 | |
479 | /* Try to register using hardware perf events */ | |
4dc0da86 | 480 | event = perf_event_create_kernel_counter(wd_attr, cpu, NULL, watchdog_overflow_callback, NULL); |
a7027046 DZ |
481 | |
482 | /* save cpu0 error for future comparision */ | |
483 | if (cpu == 0 && IS_ERR(event)) | |
484 | cpu0_err = PTR_ERR(event); | |
485 | ||
58687acb | 486 | if (!IS_ERR(event)) { |
a7027046 DZ |
487 | /* only print for cpu0 or different than cpu0 */ |
488 | if (cpu == 0 || cpu0_err) | |
489 | pr_info("enabled on all CPUs, permanently consumes one hw-PMU counter.\n"); | |
58687acb DZ |
490 | goto out_save; |
491 | } | |
492 | ||
a7027046 DZ |
493 | /* skip displaying the same error again */ |
494 | if (cpu > 0 && (PTR_ERR(event) == cpu0_err)) | |
495 | return PTR_ERR(event); | |
5651f7f4 DZ |
496 | |
497 | /* vary the KERN level based on the returned errno */ | |
498 | if (PTR_ERR(event) == -EOPNOTSUPP) | |
4501980a | 499 | pr_info("disabled (cpu%i): not supported (no LAPIC?)\n", cpu); |
5651f7f4 | 500 | else if (PTR_ERR(event) == -ENOENT) |
656c3b79 | 501 | pr_warn("disabled (cpu%i): hardware events not enabled\n", |
4501980a | 502 | cpu); |
5651f7f4 | 503 | else |
4501980a AM |
504 | pr_err("disabled (cpu%i): unable to create perf event: %ld\n", |
505 | cpu, PTR_ERR(event)); | |
eac24335 | 506 | return PTR_ERR(event); |
58687acb DZ |
507 | |
508 | /* success path */ | |
509 | out_save: | |
510 | per_cpu(watchdog_ev, cpu) = event; | |
511 | out_enable: | |
512 | perf_event_enable(per_cpu(watchdog_ev, cpu)); | |
513 | out: | |
514 | return 0; | |
515 | } | |
516 | ||
bcd951cf | 517 | static void watchdog_nmi_disable(unsigned int cpu) |
58687acb DZ |
518 | { |
519 | struct perf_event *event = per_cpu(watchdog_ev, cpu); | |
520 | ||
521 | if (event) { | |
522 | perf_event_disable(event); | |
523 | per_cpu(watchdog_ev, cpu) = NULL; | |
524 | ||
525 | /* should be in cleanup, but blocks oprofile */ | |
526 | perf_event_release_kernel(event); | |
527 | } | |
df577149 UO |
528 | if (cpu == 0) { |
529 | /* watchdog_nmi_enable() expects this to be zero initially. */ | |
530 | cpu0_err = 0; | |
531 | } | |
58687acb DZ |
532 | } |
533 | #else | |
bcd951cf TG |
534 | static int watchdog_nmi_enable(unsigned int cpu) { return 0; } |
535 | static void watchdog_nmi_disable(unsigned int cpu) { return; } | |
23637d47 | 536 | #endif /* CONFIG_HARDLOCKUP_DETECTOR */ |
58687acb | 537 | |
b8900bc0 FW |
538 | static struct smp_hotplug_thread watchdog_threads = { |
539 | .store = &softlockup_watchdog, | |
540 | .thread_should_run = watchdog_should_run, | |
541 | .thread_fn = watchdog, | |
542 | .thread_comm = "watchdog/%u", | |
543 | .setup = watchdog_enable, | |
544 | .cleanup = watchdog_cleanup, | |
545 | .park = watchdog_disable, | |
546 | .unpark = watchdog_enable, | |
547 | }; | |
548 | ||
9809b18f MH |
549 | static void restart_watchdog_hrtimer(void *info) |
550 | { | |
551 | struct hrtimer *hrtimer = &__raw_get_cpu_var(watchdog_hrtimer); | |
552 | int ret; | |
553 | ||
554 | /* | |
555 | * No need to cancel and restart hrtimer if it is currently executing | |
556 | * because it will reprogram itself with the new period now. | |
557 | * We should never see it unqueued here because we are running per-cpu | |
558 | * with interrupts disabled. | |
559 | */ | |
560 | ret = hrtimer_try_to_cancel(hrtimer); | |
561 | if (ret == 1) | |
562 | hrtimer_start(hrtimer, ns_to_ktime(sample_period), | |
563 | HRTIMER_MODE_REL_PINNED); | |
564 | } | |
565 | ||
566 | static void update_timers(int cpu) | |
567 | { | |
9809b18f MH |
568 | /* |
569 | * Make sure that perf event counter will adopt to a new | |
570 | * sampling period. Updating the sampling period directly would | |
571 | * be much nicer but we do not have an API for that now so | |
572 | * let's use a big hammer. | |
573 | * Hrtimer will adopt the new period on the next tick but this | |
574 | * might be late already so we have to restart the timer as well. | |
575 | */ | |
576 | watchdog_nmi_disable(cpu); | |
e0a23b06 | 577 | smp_call_function_single(cpu, restart_watchdog_hrtimer, NULL, 1); |
9809b18f MH |
578 | watchdog_nmi_enable(cpu); |
579 | } | |
580 | ||
581 | static void update_timers_all_cpus(void) | |
582 | { | |
583 | int cpu; | |
584 | ||
585 | get_online_cpus(); | |
9809b18f MH |
586 | for_each_online_cpu(cpu) |
587 | update_timers(cpu); | |
9809b18f MH |
588 | put_online_cpus(); |
589 | } | |
590 | ||
591 | static int watchdog_enable_all_cpus(bool sample_period_changed) | |
58687acb | 592 | { |
b8900bc0 | 593 | int err = 0; |
58687acb | 594 | |
3c00ea82 | 595 | if (!watchdog_running) { |
b8900bc0 FW |
596 | err = smpboot_register_percpu_thread(&watchdog_threads); |
597 | if (err) | |
598 | pr_err("Failed to create watchdog threads, disabled\n"); | |
599 | else | |
3c00ea82 | 600 | watchdog_running = 1; |
9809b18f MH |
601 | } else if (sample_period_changed) { |
602 | update_timers_all_cpus(); | |
bcd951cf | 603 | } |
b8900bc0 FW |
604 | |
605 | return err; | |
58687acb DZ |
606 | } |
607 | ||
b8900bc0 FW |
608 | /* prepare/enable/disable routines */ |
609 | /* sysctl functions */ | |
610 | #ifdef CONFIG_SYSCTL | |
58687acb DZ |
611 | static void watchdog_disable_all_cpus(void) |
612 | { | |
3c00ea82 FW |
613 | if (watchdog_running) { |
614 | watchdog_running = 0; | |
b8900bc0 | 615 | smpboot_unregister_percpu_thread(&watchdog_threads); |
bcd951cf | 616 | } |
58687acb DZ |
617 | } |
618 | ||
58687acb | 619 | /* |
586692a5 | 620 | * proc handler for /proc/sys/kernel/nmi_watchdog,watchdog_thresh |
58687acb DZ |
621 | */ |
622 | ||
586692a5 MSB |
623 | int proc_dowatchdog(struct ctl_table *table, int write, |
624 | void __user *buffer, size_t *lenp, loff_t *ppos) | |
58687acb | 625 | { |
b8900bc0 | 626 | int err, old_thresh, old_enabled; |
359e6fab | 627 | static DEFINE_MUTEX(watchdog_proc_mutex); |
58687acb | 628 | |
359e6fab | 629 | mutex_lock(&watchdog_proc_mutex); |
b8900bc0 | 630 | old_thresh = ACCESS_ONCE(watchdog_thresh); |
3c00ea82 | 631 | old_enabled = ACCESS_ONCE(watchdog_user_enabled); |
bcd951cf | 632 | |
b8900bc0 FW |
633 | err = proc_dointvec_minmax(table, write, buffer, lenp, ppos); |
634 | if (err || !write) | |
359e6fab | 635 | goto out; |
e04ab2bc | 636 | |
0f34c400 | 637 | set_sample_period(); |
b66a2356 | 638 | /* |
639 | * Watchdog threads shouldn't be enabled if they are | |
3c00ea82 | 640 | * disabled. The 'watchdog_running' variable check in |
b66a2356 | 641 | * watchdog_*_all_cpus() function takes care of this. |
642 | */ | |
3c00ea82 | 643 | if (watchdog_user_enabled && watchdog_thresh) |
9809b18f | 644 | err = watchdog_enable_all_cpus(old_thresh != watchdog_thresh); |
e04ab2bc MSB |
645 | else |
646 | watchdog_disable_all_cpus(); | |
647 | ||
b8900bc0 FW |
648 | /* Restore old values on failure */ |
649 | if (err) { | |
650 | watchdog_thresh = old_thresh; | |
3c00ea82 | 651 | watchdog_user_enabled = old_enabled; |
b8900bc0 | 652 | } |
359e6fab MH |
653 | out: |
654 | mutex_unlock(&watchdog_proc_mutex); | |
b8900bc0 | 655 | return err; |
58687acb | 656 | } |
58687acb DZ |
657 | #endif /* CONFIG_SYSCTL */ |
658 | ||
004417a6 | 659 | void __init lockup_detector_init(void) |
58687acb | 660 | { |
0f34c400 | 661 | set_sample_period(); |
b8900bc0 | 662 | |
3c00ea82 | 663 | if (watchdog_user_enabled) |
9809b18f | 664 | watchdog_enable_all_cpus(false); |
58687acb | 665 | } |