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> |
fe4ba3c3 | 22 | #include <linux/tick.h> |
58687acb DZ |
23 | |
24 | #include <asm/irq_regs.h> | |
5d1c0f4a | 25 | #include <linux/kvm_para.h> |
58687acb | 26 | #include <linux/perf_event.h> |
81a4beef | 27 | #include <linux/kthread.h> |
58687acb | 28 | |
84d56e66 UO |
29 | /* |
30 | * The run state of the lockup detectors is controlled by the content of the | |
31 | * 'watchdog_enabled' variable. Each lockup detector has its dedicated bit - | |
32 | * bit 0 for the hard lockup detector and bit 1 for the soft lockup detector. | |
33 | * | |
34 | * 'watchdog_user_enabled', 'nmi_watchdog_enabled' and 'soft_watchdog_enabled' | |
35 | * are variables that are only used as an 'interface' between the parameters | |
36 | * in /proc/sys/kernel and the internal state bits in 'watchdog_enabled'. The | |
37 | * 'watchdog_thresh' variable is handled differently because its value is not | |
38 | * boolean, and the lockup detectors are 'suspended' while 'watchdog_thresh' | |
39 | * is equal zero. | |
40 | */ | |
41 | #define NMI_WATCHDOG_ENABLED_BIT 0 | |
42 | #define SOFT_WATCHDOG_ENABLED_BIT 1 | |
43 | #define NMI_WATCHDOG_ENABLED (1 << NMI_WATCHDOG_ENABLED_BIT) | |
44 | #define SOFT_WATCHDOG_ENABLED (1 << SOFT_WATCHDOG_ENABLED_BIT) | |
45 | ||
ab992dc3 PZ |
46 | static DEFINE_MUTEX(watchdog_proc_mutex); |
47 | ||
84d56e66 UO |
48 | #ifdef CONFIG_HARDLOCKUP_DETECTOR |
49 | static unsigned long __read_mostly watchdog_enabled = SOFT_WATCHDOG_ENABLED|NMI_WATCHDOG_ENABLED; | |
50 | #else | |
51 | static unsigned long __read_mostly watchdog_enabled = SOFT_WATCHDOG_ENABLED; | |
52 | #endif | |
53 | int __read_mostly nmi_watchdog_enabled; | |
54 | int __read_mostly soft_watchdog_enabled; | |
55 | int __read_mostly watchdog_user_enabled; | |
4eec42f3 | 56 | int __read_mostly watchdog_thresh = 10; |
84d56e66 | 57 | |
ed235875 AT |
58 | #ifdef CONFIG_SMP |
59 | int __read_mostly sysctl_softlockup_all_cpu_backtrace; | |
60 | #else | |
61 | #define sysctl_softlockup_all_cpu_backtrace 0 | |
62 | #endif | |
fe4ba3c3 CM |
63 | static struct cpumask watchdog_cpumask __read_mostly; |
64 | unsigned long *watchdog_cpumask_bits = cpumask_bits(&watchdog_cpumask); | |
65 | ||
66 | /* Helper for online, unparked cpus. */ | |
67 | #define for_each_watchdog_cpu(cpu) \ | |
68 | for_each_cpu_and((cpu), cpu_online_mask, &watchdog_cpumask) | |
ed235875 | 69 | |
8c073d27 | 70 | static int __read_mostly watchdog_suspended; |
3c00ea82 | 71 | static int __read_mostly watchdog_running; |
0f34c400 | 72 | static u64 __read_mostly sample_period; |
58687acb DZ |
73 | |
74 | static DEFINE_PER_CPU(unsigned long, watchdog_touch_ts); | |
75 | static DEFINE_PER_CPU(struct task_struct *, softlockup_watchdog); | |
76 | static DEFINE_PER_CPU(struct hrtimer, watchdog_hrtimer); | |
77 | static DEFINE_PER_CPU(bool, softlockup_touch_sync); | |
58687acb | 78 | static DEFINE_PER_CPU(bool, soft_watchdog_warn); |
bcd951cf TG |
79 | static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts); |
80 | static DEFINE_PER_CPU(unsigned long, soft_lockup_hrtimer_cnt); | |
b1a8de1f | 81 | static DEFINE_PER_CPU(struct task_struct *, softlockup_task_ptr_saved); |
23637d47 | 82 | #ifdef CONFIG_HARDLOCKUP_DETECTOR |
cafcd80d DZ |
83 | static DEFINE_PER_CPU(bool, hard_watchdog_warn); |
84 | static DEFINE_PER_CPU(bool, watchdog_nmi_touch); | |
58687acb DZ |
85 | static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts_saved); |
86 | static DEFINE_PER_CPU(struct perf_event *, watchdog_ev); | |
87 | #endif | |
ed235875 | 88 | static unsigned long soft_lockup_nmi_warn; |
58687acb | 89 | |
58687acb DZ |
90 | /* boot commands */ |
91 | /* | |
92 | * Should we panic when a soft-lockup or hard-lockup occurs: | |
93 | */ | |
23637d47 | 94 | #ifdef CONFIG_HARDLOCKUP_DETECTOR |
fef2c9bc DZ |
95 | static int hardlockup_panic = |
96 | CONFIG_BOOTPARAM_HARDLOCKUP_PANIC_VALUE; | |
6e7458a6 UO |
97 | /* |
98 | * We may not want to enable hard lockup detection by default in all cases, | |
99 | * for example when running the kernel as a guest on a hypervisor. In these | |
100 | * cases this function can be called to disable hard lockup detection. This | |
101 | * function should only be executed once by the boot processor before the | |
102 | * kernel command line parameters are parsed, because otherwise it is not | |
103 | * possible to override this in hardlockup_panic_setup(). | |
104 | */ | |
692297d8 | 105 | void hardlockup_detector_disable(void) |
6e7458a6 | 106 | { |
692297d8 | 107 | watchdog_enabled &= ~NMI_WATCHDOG_ENABLED; |
6e7458a6 UO |
108 | } |
109 | ||
58687acb DZ |
110 | static int __init hardlockup_panic_setup(char *str) |
111 | { | |
112 | if (!strncmp(str, "panic", 5)) | |
113 | hardlockup_panic = 1; | |
fef2c9bc DZ |
114 | else if (!strncmp(str, "nopanic", 7)) |
115 | hardlockup_panic = 0; | |
5dc30558 | 116 | else if (!strncmp(str, "0", 1)) |
195daf66 UO |
117 | watchdog_enabled &= ~NMI_WATCHDOG_ENABLED; |
118 | else if (!strncmp(str, "1", 1)) | |
119 | watchdog_enabled |= NMI_WATCHDOG_ENABLED; | |
58687acb DZ |
120 | return 1; |
121 | } | |
122 | __setup("nmi_watchdog=", hardlockup_panic_setup); | |
123 | #endif | |
124 | ||
125 | unsigned int __read_mostly softlockup_panic = | |
126 | CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC_VALUE; | |
127 | ||
128 | static int __init softlockup_panic_setup(char *str) | |
129 | { | |
130 | softlockup_panic = simple_strtoul(str, NULL, 0); | |
131 | ||
132 | return 1; | |
133 | } | |
134 | __setup("softlockup_panic=", softlockup_panic_setup); | |
135 | ||
136 | static int __init nowatchdog_setup(char *str) | |
137 | { | |
195daf66 | 138 | watchdog_enabled = 0; |
58687acb DZ |
139 | return 1; |
140 | } | |
141 | __setup("nowatchdog", nowatchdog_setup); | |
142 | ||
58687acb DZ |
143 | static int __init nosoftlockup_setup(char *str) |
144 | { | |
195daf66 | 145 | watchdog_enabled &= ~SOFT_WATCHDOG_ENABLED; |
58687acb DZ |
146 | return 1; |
147 | } | |
148 | __setup("nosoftlockup", nosoftlockup_setup); | |
195daf66 | 149 | |
ed235875 AT |
150 | #ifdef CONFIG_SMP |
151 | static int __init softlockup_all_cpu_backtrace_setup(char *str) | |
152 | { | |
153 | sysctl_softlockup_all_cpu_backtrace = | |
154 | !!simple_strtol(str, NULL, 0); | |
155 | return 1; | |
156 | } | |
157 | __setup("softlockup_all_cpu_backtrace=", softlockup_all_cpu_backtrace_setup); | |
158 | #endif | |
58687acb | 159 | |
4eec42f3 MSB |
160 | /* |
161 | * Hard-lockup warnings should be triggered after just a few seconds. Soft- | |
162 | * lockups can have false positives under extreme conditions. So we generally | |
163 | * want a higher threshold for soft lockups than for hard lockups. So we couple | |
164 | * the thresholds with a factor: we make the soft threshold twice the amount of | |
165 | * time the hard threshold is. | |
166 | */ | |
6e9101ae | 167 | static int get_softlockup_thresh(void) |
4eec42f3 MSB |
168 | { |
169 | return watchdog_thresh * 2; | |
170 | } | |
58687acb DZ |
171 | |
172 | /* | |
173 | * Returns seconds, approximately. We don't need nanosecond | |
174 | * resolution, and we don't need to waste time with a big divide when | |
175 | * 2^30ns == 1.074s. | |
176 | */ | |
c06b4f19 | 177 | static unsigned long get_timestamp(void) |
58687acb | 178 | { |
545a2bf7 | 179 | return running_clock() >> 30LL; /* 2^30 ~= 10^9 */ |
58687acb DZ |
180 | } |
181 | ||
0f34c400 | 182 | static void set_sample_period(void) |
58687acb DZ |
183 | { |
184 | /* | |
586692a5 | 185 | * convert watchdog_thresh from seconds to ns |
86f5e6a7 FLVC |
186 | * the divide by 5 is to give hrtimer several chances (two |
187 | * or three with the current relation between the soft | |
188 | * and hard thresholds) to increment before the | |
189 | * hardlockup detector generates a warning | |
58687acb | 190 | */ |
0f34c400 | 191 | sample_period = get_softlockup_thresh() * ((u64)NSEC_PER_SEC / 5); |
58687acb DZ |
192 | } |
193 | ||
194 | /* Commands for resetting the watchdog */ | |
195 | static void __touch_watchdog(void) | |
196 | { | |
c06b4f19 | 197 | __this_cpu_write(watchdog_touch_ts, get_timestamp()); |
58687acb DZ |
198 | } |
199 | ||
332fbdbc | 200 | void touch_softlockup_watchdog(void) |
58687acb | 201 | { |
7861144b AM |
202 | /* |
203 | * Preemption can be enabled. It doesn't matter which CPU's timestamp | |
204 | * gets zeroed here, so use the raw_ operation. | |
205 | */ | |
206 | raw_cpu_write(watchdog_touch_ts, 0); | |
58687acb | 207 | } |
0167c781 | 208 | EXPORT_SYMBOL(touch_softlockup_watchdog); |
58687acb | 209 | |
332fbdbc | 210 | void touch_all_softlockup_watchdogs(void) |
58687acb DZ |
211 | { |
212 | int cpu; | |
213 | ||
214 | /* | |
215 | * this is done lockless | |
216 | * do we care if a 0 races with a timestamp? | |
217 | * all it means is the softlock check starts one cycle later | |
218 | */ | |
fe4ba3c3 | 219 | for_each_watchdog_cpu(cpu) |
58687acb DZ |
220 | per_cpu(watchdog_touch_ts, cpu) = 0; |
221 | } | |
222 | ||
cafcd80d | 223 | #ifdef CONFIG_HARDLOCKUP_DETECTOR |
58687acb DZ |
224 | void touch_nmi_watchdog(void) |
225 | { | |
62572e29 BZ |
226 | /* |
227 | * Using __raw here because some code paths have | |
228 | * preemption enabled. If preemption is enabled | |
229 | * then interrupts should be enabled too, in which | |
230 | * case we shouldn't have to worry about the watchdog | |
231 | * going off. | |
232 | */ | |
f7f66b05 | 233 | raw_cpu_write(watchdog_nmi_touch, true); |
332fbdbc | 234 | touch_softlockup_watchdog(); |
58687acb DZ |
235 | } |
236 | EXPORT_SYMBOL(touch_nmi_watchdog); | |
237 | ||
cafcd80d DZ |
238 | #endif |
239 | ||
58687acb DZ |
240 | void touch_softlockup_watchdog_sync(void) |
241 | { | |
f7f66b05 CL |
242 | __this_cpu_write(softlockup_touch_sync, true); |
243 | __this_cpu_write(watchdog_touch_ts, 0); | |
58687acb DZ |
244 | } |
245 | ||
23637d47 | 246 | #ifdef CONFIG_HARDLOCKUP_DETECTOR |
58687acb | 247 | /* watchdog detector functions */ |
26e09c6e | 248 | static int is_hardlockup(void) |
58687acb | 249 | { |
909ea964 | 250 | unsigned long hrint = __this_cpu_read(hrtimer_interrupts); |
58687acb | 251 | |
909ea964 | 252 | if (__this_cpu_read(hrtimer_interrupts_saved) == hrint) |
58687acb DZ |
253 | return 1; |
254 | ||
909ea964 | 255 | __this_cpu_write(hrtimer_interrupts_saved, hrint); |
58687acb DZ |
256 | return 0; |
257 | } | |
258 | #endif | |
259 | ||
26e09c6e | 260 | static int is_softlockup(unsigned long touch_ts) |
58687acb | 261 | { |
c06b4f19 | 262 | unsigned long now = get_timestamp(); |
58687acb | 263 | |
195daf66 UO |
264 | if (watchdog_enabled & SOFT_WATCHDOG_ENABLED) { |
265 | /* Warn about unreasonable delays. */ | |
266 | if (time_after(now, touch_ts + get_softlockup_thresh())) | |
267 | return now - touch_ts; | |
268 | } | |
58687acb DZ |
269 | return 0; |
270 | } | |
271 | ||
23637d47 | 272 | #ifdef CONFIG_HARDLOCKUP_DETECTOR |
1880c4ae | 273 | |
58687acb DZ |
274 | static struct perf_event_attr wd_hw_attr = { |
275 | .type = PERF_TYPE_HARDWARE, | |
276 | .config = PERF_COUNT_HW_CPU_CYCLES, | |
277 | .size = sizeof(struct perf_event_attr), | |
278 | .pinned = 1, | |
279 | .disabled = 1, | |
280 | }; | |
281 | ||
282 | /* Callback function for perf event subsystem */ | |
a8b0ca17 | 283 | static void watchdog_overflow_callback(struct perf_event *event, |
58687acb DZ |
284 | struct perf_sample_data *data, |
285 | struct pt_regs *regs) | |
286 | { | |
c6db67cd PZ |
287 | /* Ensure the watchdog never gets throttled */ |
288 | event->hw.interrupts = 0; | |
289 | ||
909ea964 CL |
290 | if (__this_cpu_read(watchdog_nmi_touch) == true) { |
291 | __this_cpu_write(watchdog_nmi_touch, false); | |
58687acb DZ |
292 | return; |
293 | } | |
294 | ||
295 | /* check for a hardlockup | |
296 | * This is done by making sure our timer interrupt | |
297 | * is incrementing. The timer interrupt should have | |
298 | * fired multiple times before we overflow'd. If it hasn't | |
299 | * then this is a good indication the cpu is stuck | |
300 | */ | |
26e09c6e DZ |
301 | if (is_hardlockup()) { |
302 | int this_cpu = smp_processor_id(); | |
303 | ||
58687acb | 304 | /* only print hardlockups once */ |
909ea964 | 305 | if (__this_cpu_read(hard_watchdog_warn) == true) |
58687acb DZ |
306 | return; |
307 | ||
308 | if (hardlockup_panic) | |
656c3b79 FF |
309 | panic("Watchdog detected hard LOCKUP on cpu %d", |
310 | this_cpu); | |
58687acb | 311 | else |
656c3b79 FF |
312 | WARN(1, "Watchdog detected hard LOCKUP on cpu %d", |
313 | this_cpu); | |
58687acb | 314 | |
909ea964 | 315 | __this_cpu_write(hard_watchdog_warn, true); |
58687acb DZ |
316 | return; |
317 | } | |
318 | ||
909ea964 | 319 | __this_cpu_write(hard_watchdog_warn, false); |
58687acb DZ |
320 | return; |
321 | } | |
bcd951cf TG |
322 | #endif /* CONFIG_HARDLOCKUP_DETECTOR */ |
323 | ||
58687acb DZ |
324 | static void watchdog_interrupt_count(void) |
325 | { | |
909ea964 | 326 | __this_cpu_inc(hrtimer_interrupts); |
58687acb | 327 | } |
bcd951cf TG |
328 | |
329 | static int watchdog_nmi_enable(unsigned int cpu); | |
330 | static void watchdog_nmi_disable(unsigned int cpu); | |
58687acb DZ |
331 | |
332 | /* watchdog kicker functions */ | |
333 | static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer) | |
334 | { | |
909ea964 | 335 | unsigned long touch_ts = __this_cpu_read(watchdog_touch_ts); |
58687acb DZ |
336 | struct pt_regs *regs = get_irq_regs(); |
337 | int duration; | |
ed235875 | 338 | int softlockup_all_cpu_backtrace = sysctl_softlockup_all_cpu_backtrace; |
58687acb DZ |
339 | |
340 | /* kick the hardlockup detector */ | |
341 | watchdog_interrupt_count(); | |
342 | ||
343 | /* kick the softlockup detector */ | |
909ea964 | 344 | wake_up_process(__this_cpu_read(softlockup_watchdog)); |
58687acb DZ |
345 | |
346 | /* .. and repeat */ | |
0f34c400 | 347 | hrtimer_forward_now(hrtimer, ns_to_ktime(sample_period)); |
58687acb DZ |
348 | |
349 | if (touch_ts == 0) { | |
909ea964 | 350 | if (unlikely(__this_cpu_read(softlockup_touch_sync))) { |
58687acb DZ |
351 | /* |
352 | * If the time stamp was touched atomically | |
353 | * make sure the scheduler tick is up to date. | |
354 | */ | |
909ea964 | 355 | __this_cpu_write(softlockup_touch_sync, false); |
58687acb DZ |
356 | sched_clock_tick(); |
357 | } | |
5d1c0f4a EM |
358 | |
359 | /* Clear the guest paused flag on watchdog reset */ | |
360 | kvm_check_and_clear_guest_paused(); | |
58687acb DZ |
361 | __touch_watchdog(); |
362 | return HRTIMER_RESTART; | |
363 | } | |
364 | ||
365 | /* check for a softlockup | |
366 | * This is done by making sure a high priority task is | |
367 | * being scheduled. The task touches the watchdog to | |
368 | * indicate it is getting cpu time. If it hasn't then | |
369 | * this is a good indication some task is hogging the cpu | |
370 | */ | |
26e09c6e | 371 | duration = is_softlockup(touch_ts); |
58687acb | 372 | if (unlikely(duration)) { |
5d1c0f4a EM |
373 | /* |
374 | * If a virtual machine is stopped by the host it can look to | |
375 | * the watchdog like a soft lockup, check to see if the host | |
376 | * stopped the vm before we issue the warning | |
377 | */ | |
378 | if (kvm_check_and_clear_guest_paused()) | |
379 | return HRTIMER_RESTART; | |
380 | ||
58687acb | 381 | /* only warn once */ |
b1a8de1f | 382 | if (__this_cpu_read(soft_watchdog_warn) == true) { |
383 | /* | |
384 | * When multiple processes are causing softlockups the | |
385 | * softlockup detector only warns on the first one | |
386 | * because the code relies on a full quiet cycle to | |
387 | * re-arm. The second process prevents the quiet cycle | |
388 | * and never gets reported. Use task pointers to detect | |
389 | * this. | |
390 | */ | |
391 | if (__this_cpu_read(softlockup_task_ptr_saved) != | |
392 | current) { | |
393 | __this_cpu_write(soft_watchdog_warn, false); | |
394 | __touch_watchdog(); | |
395 | } | |
58687acb | 396 | return HRTIMER_RESTART; |
b1a8de1f | 397 | } |
58687acb | 398 | |
ed235875 AT |
399 | if (softlockup_all_cpu_backtrace) { |
400 | /* Prevent multiple soft-lockup reports if one cpu is already | |
401 | * engaged in dumping cpu back traces | |
402 | */ | |
403 | if (test_and_set_bit(0, &soft_lockup_nmi_warn)) { | |
404 | /* Someone else will report us. Let's give up */ | |
405 | __this_cpu_write(soft_watchdog_warn, true); | |
406 | return HRTIMER_RESTART; | |
407 | } | |
408 | } | |
409 | ||
656c3b79 | 410 | pr_emerg("BUG: soft lockup - CPU#%d stuck for %us! [%s:%d]\n", |
26e09c6e | 411 | smp_processor_id(), duration, |
58687acb | 412 | current->comm, task_pid_nr(current)); |
b1a8de1f | 413 | __this_cpu_write(softlockup_task_ptr_saved, current); |
58687acb DZ |
414 | print_modules(); |
415 | print_irqtrace_events(current); | |
416 | if (regs) | |
417 | show_regs(regs); | |
418 | else | |
419 | dump_stack(); | |
420 | ||
ed235875 AT |
421 | if (softlockup_all_cpu_backtrace) { |
422 | /* Avoid generating two back traces for current | |
423 | * given that one is already made above | |
424 | */ | |
425 | trigger_allbutself_cpu_backtrace(); | |
426 | ||
427 | clear_bit(0, &soft_lockup_nmi_warn); | |
428 | /* Barrier to sync with other cpus */ | |
429 | smp_mb__after_atomic(); | |
430 | } | |
431 | ||
69361eef | 432 | add_taint(TAINT_SOFTLOCKUP, LOCKDEP_STILL_OK); |
58687acb DZ |
433 | if (softlockup_panic) |
434 | panic("softlockup: hung tasks"); | |
909ea964 | 435 | __this_cpu_write(soft_watchdog_warn, true); |
58687acb | 436 | } else |
909ea964 | 437 | __this_cpu_write(soft_watchdog_warn, false); |
58687acb DZ |
438 | |
439 | return HRTIMER_RESTART; | |
440 | } | |
441 | ||
bcd951cf TG |
442 | static void watchdog_set_prio(unsigned int policy, unsigned int prio) |
443 | { | |
444 | struct sched_param param = { .sched_priority = prio }; | |
58687acb | 445 | |
bcd951cf TG |
446 | sched_setscheduler(current, policy, ¶m); |
447 | } | |
448 | ||
449 | static void watchdog_enable(unsigned int cpu) | |
58687acb | 450 | { |
f7f66b05 | 451 | struct hrtimer *hrtimer = raw_cpu_ptr(&watchdog_hrtimer); |
58687acb | 452 | |
3935e895 BM |
453 | /* kick off the timer for the hardlockup detector */ |
454 | hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); | |
455 | hrtimer->function = watchdog_timer_fn; | |
456 | ||
bcd951cf TG |
457 | /* Enable the perf event */ |
458 | watchdog_nmi_enable(cpu); | |
58687acb | 459 | |
58687acb | 460 | /* done here because hrtimer_start can only pin to smp_processor_id() */ |
0f34c400 | 461 | hrtimer_start(hrtimer, ns_to_ktime(sample_period), |
58687acb DZ |
462 | HRTIMER_MODE_REL_PINNED); |
463 | ||
bcd951cf TG |
464 | /* initialize timestamp */ |
465 | watchdog_set_prio(SCHED_FIFO, MAX_RT_PRIO - 1); | |
466 | __touch_watchdog(); | |
467 | } | |
58687acb | 468 | |
bcd951cf TG |
469 | static void watchdog_disable(unsigned int cpu) |
470 | { | |
f7f66b05 | 471 | struct hrtimer *hrtimer = raw_cpu_ptr(&watchdog_hrtimer); |
58687acb | 472 | |
bcd951cf TG |
473 | watchdog_set_prio(SCHED_NORMAL, 0); |
474 | hrtimer_cancel(hrtimer); | |
475 | /* disable the perf event */ | |
476 | watchdog_nmi_disable(cpu); | |
58687acb DZ |
477 | } |
478 | ||
b8900bc0 FW |
479 | static void watchdog_cleanup(unsigned int cpu, bool online) |
480 | { | |
481 | watchdog_disable(cpu); | |
482 | } | |
483 | ||
bcd951cf TG |
484 | static int watchdog_should_run(unsigned int cpu) |
485 | { | |
486 | return __this_cpu_read(hrtimer_interrupts) != | |
487 | __this_cpu_read(soft_lockup_hrtimer_cnt); | |
488 | } | |
489 | ||
490 | /* | |
491 | * The watchdog thread function - touches the timestamp. | |
492 | * | |
0f34c400 | 493 | * It only runs once every sample_period seconds (4 seconds by |
bcd951cf TG |
494 | * default) to reset the softlockup timestamp. If this gets delayed |
495 | * for more than 2*watchdog_thresh seconds then the debug-printout | |
496 | * triggers in watchdog_timer_fn(). | |
497 | */ | |
498 | static void watchdog(unsigned int cpu) | |
499 | { | |
500 | __this_cpu_write(soft_lockup_hrtimer_cnt, | |
501 | __this_cpu_read(hrtimer_interrupts)); | |
502 | __touch_watchdog(); | |
bcfba4f4 UO |
503 | |
504 | /* | |
505 | * watchdog_nmi_enable() clears the NMI_WATCHDOG_ENABLED bit in the | |
506 | * failure path. Check for failures that can occur asynchronously - | |
507 | * for example, when CPUs are on-lined - and shut down the hardware | |
508 | * perf event on each CPU accordingly. | |
509 | * | |
510 | * The only non-obvious place this bit can be cleared is through | |
511 | * watchdog_nmi_enable(), so a pr_info() is placed there. Placing a | |
512 | * pr_info here would be too noisy as it would result in a message | |
513 | * every few seconds if the hardlockup was disabled but the softlockup | |
514 | * enabled. | |
515 | */ | |
516 | if (!(watchdog_enabled & NMI_WATCHDOG_ENABLED)) | |
517 | watchdog_nmi_disable(cpu); | |
bcd951cf | 518 | } |
58687acb | 519 | |
23637d47 | 520 | #ifdef CONFIG_HARDLOCKUP_DETECTOR |
a7027046 DZ |
521 | /* |
522 | * People like the simple clean cpu node info on boot. | |
523 | * Reduce the watchdog noise by only printing messages | |
524 | * that are different from what cpu0 displayed. | |
525 | */ | |
526 | static unsigned long cpu0_err; | |
527 | ||
bcd951cf | 528 | static int watchdog_nmi_enable(unsigned int cpu) |
58687acb DZ |
529 | { |
530 | struct perf_event_attr *wd_attr; | |
531 | struct perf_event *event = per_cpu(watchdog_ev, cpu); | |
532 | ||
195daf66 UO |
533 | /* nothing to do if the hard lockup detector is disabled */ |
534 | if (!(watchdog_enabled & NMI_WATCHDOG_ENABLED)) | |
535 | goto out; | |
6e7458a6 | 536 | |
58687acb DZ |
537 | /* is it already setup and enabled? */ |
538 | if (event && event->state > PERF_EVENT_STATE_OFF) | |
539 | goto out; | |
540 | ||
541 | /* it is setup but not enabled */ | |
542 | if (event != NULL) | |
543 | goto out_enable; | |
544 | ||
58687acb | 545 | wd_attr = &wd_hw_attr; |
4eec42f3 | 546 | wd_attr->sample_period = hw_nmi_get_sample_period(watchdog_thresh); |
1880c4ae CG |
547 | |
548 | /* Try to register using hardware perf events */ | |
4dc0da86 | 549 | event = perf_event_create_kernel_counter(wd_attr, cpu, NULL, watchdog_overflow_callback, NULL); |
a7027046 DZ |
550 | |
551 | /* save cpu0 error for future comparision */ | |
552 | if (cpu == 0 && IS_ERR(event)) | |
553 | cpu0_err = PTR_ERR(event); | |
554 | ||
58687acb | 555 | if (!IS_ERR(event)) { |
a7027046 DZ |
556 | /* only print for cpu0 or different than cpu0 */ |
557 | if (cpu == 0 || cpu0_err) | |
558 | pr_info("enabled on all CPUs, permanently consumes one hw-PMU counter.\n"); | |
58687acb DZ |
559 | goto out_save; |
560 | } | |
561 | ||
bcfba4f4 UO |
562 | /* |
563 | * Disable the hard lockup detector if _any_ CPU fails to set up | |
564 | * set up the hardware perf event. The watchdog() function checks | |
565 | * the NMI_WATCHDOG_ENABLED bit periodically. | |
566 | * | |
567 | * The barriers are for syncing up watchdog_enabled across all the | |
568 | * cpus, as clear_bit() does not use barriers. | |
569 | */ | |
570 | smp_mb__before_atomic(); | |
571 | clear_bit(NMI_WATCHDOG_ENABLED_BIT, &watchdog_enabled); | |
572 | smp_mb__after_atomic(); | |
573 | ||
a7027046 DZ |
574 | /* skip displaying the same error again */ |
575 | if (cpu > 0 && (PTR_ERR(event) == cpu0_err)) | |
576 | return PTR_ERR(event); | |
5651f7f4 DZ |
577 | |
578 | /* vary the KERN level based on the returned errno */ | |
579 | if (PTR_ERR(event) == -EOPNOTSUPP) | |
4501980a | 580 | pr_info("disabled (cpu%i): not supported (no LAPIC?)\n", cpu); |
5651f7f4 | 581 | else if (PTR_ERR(event) == -ENOENT) |
656c3b79 | 582 | pr_warn("disabled (cpu%i): hardware events not enabled\n", |
4501980a | 583 | cpu); |
5651f7f4 | 584 | else |
4501980a AM |
585 | pr_err("disabled (cpu%i): unable to create perf event: %ld\n", |
586 | cpu, PTR_ERR(event)); | |
bcfba4f4 UO |
587 | |
588 | pr_info("Shutting down hard lockup detector on all cpus\n"); | |
589 | ||
eac24335 | 590 | return PTR_ERR(event); |
58687acb DZ |
591 | |
592 | /* success path */ | |
593 | out_save: | |
594 | per_cpu(watchdog_ev, cpu) = event; | |
595 | out_enable: | |
596 | perf_event_enable(per_cpu(watchdog_ev, cpu)); | |
597 | out: | |
598 | return 0; | |
599 | } | |
600 | ||
bcd951cf | 601 | static void watchdog_nmi_disable(unsigned int cpu) |
58687acb DZ |
602 | { |
603 | struct perf_event *event = per_cpu(watchdog_ev, cpu); | |
604 | ||
605 | if (event) { | |
606 | perf_event_disable(event); | |
607 | per_cpu(watchdog_ev, cpu) = NULL; | |
608 | ||
609 | /* should be in cleanup, but blocks oprofile */ | |
610 | perf_event_release_kernel(event); | |
611 | } | |
df577149 UO |
612 | if (cpu == 0) { |
613 | /* watchdog_nmi_enable() expects this to be zero initially. */ | |
614 | cpu0_err = 0; | |
615 | } | |
58687acb | 616 | } |
b3738d29 | 617 | |
58687acb | 618 | #else |
bcd951cf TG |
619 | static int watchdog_nmi_enable(unsigned int cpu) { return 0; } |
620 | static void watchdog_nmi_disable(unsigned int cpu) { return; } | |
23637d47 | 621 | #endif /* CONFIG_HARDLOCKUP_DETECTOR */ |
58687acb | 622 | |
b8900bc0 FW |
623 | static struct smp_hotplug_thread watchdog_threads = { |
624 | .store = &softlockup_watchdog, | |
625 | .thread_should_run = watchdog_should_run, | |
626 | .thread_fn = watchdog, | |
627 | .thread_comm = "watchdog/%u", | |
628 | .setup = watchdog_enable, | |
629 | .cleanup = watchdog_cleanup, | |
630 | .park = watchdog_disable, | |
631 | .unpark = watchdog_enable, | |
632 | }; | |
633 | ||
81a4beef UO |
634 | /* |
635 | * park all watchdog threads that are specified in 'watchdog_cpumask' | |
636 | */ | |
637 | static int watchdog_park_threads(void) | |
638 | { | |
639 | int cpu, ret = 0; | |
640 | ||
641 | get_online_cpus(); | |
642 | for_each_watchdog_cpu(cpu) { | |
643 | ret = kthread_park(per_cpu(softlockup_watchdog, cpu)); | |
644 | if (ret) | |
645 | break; | |
646 | } | |
647 | if (ret) { | |
648 | for_each_watchdog_cpu(cpu) | |
649 | kthread_unpark(per_cpu(softlockup_watchdog, cpu)); | |
650 | } | |
651 | put_online_cpus(); | |
652 | ||
653 | return ret; | |
654 | } | |
655 | ||
656 | /* | |
657 | * unpark all watchdog threads that are specified in 'watchdog_cpumask' | |
658 | */ | |
659 | static void watchdog_unpark_threads(void) | |
660 | { | |
661 | int cpu; | |
662 | ||
663 | get_online_cpus(); | |
664 | for_each_watchdog_cpu(cpu) | |
665 | kthread_unpark(per_cpu(softlockup_watchdog, cpu)); | |
666 | put_online_cpus(); | |
667 | } | |
668 | ||
8c073d27 UO |
669 | /* |
670 | * Suspend the hard and soft lockup detector by parking the watchdog threads. | |
671 | */ | |
672 | int watchdog_suspend(void) | |
673 | { | |
674 | int ret = 0; | |
675 | ||
676 | mutex_lock(&watchdog_proc_mutex); | |
677 | /* | |
678 | * Multiple suspend requests can be active in parallel (counted by | |
679 | * the 'watchdog_suspended' variable). If the watchdog threads are | |
680 | * running, the first caller takes care that they will be parked. | |
681 | * The state of 'watchdog_running' cannot change while a suspend | |
682 | * request is active (see related changes in 'proc' handlers). | |
683 | */ | |
684 | if (watchdog_running && !watchdog_suspended) | |
685 | ret = watchdog_park_threads(); | |
686 | ||
687 | if (ret == 0) | |
688 | watchdog_suspended++; | |
689 | ||
690 | mutex_unlock(&watchdog_proc_mutex); | |
691 | ||
692 | return ret; | |
693 | } | |
694 | ||
695 | /* | |
696 | * Resume the hard and soft lockup detector by unparking the watchdog threads. | |
697 | */ | |
698 | void watchdog_resume(void) | |
699 | { | |
700 | mutex_lock(&watchdog_proc_mutex); | |
701 | ||
702 | watchdog_suspended--; | |
703 | /* | |
704 | * The watchdog threads are unparked if they were previously running | |
705 | * and if there is no more active suspend request. | |
706 | */ | |
707 | if (watchdog_running && !watchdog_suspended) | |
708 | watchdog_unpark_threads(); | |
709 | ||
710 | mutex_unlock(&watchdog_proc_mutex); | |
711 | } | |
712 | ||
b2f57c3a | 713 | static void update_watchdog_all_cpus(void) |
9809b18f | 714 | { |
d4bdd0b2 UO |
715 | watchdog_park_threads(); |
716 | watchdog_unpark_threads(); | |
9809b18f MH |
717 | } |
718 | ||
b2f57c3a | 719 | static int watchdog_enable_all_cpus(void) |
58687acb | 720 | { |
b8900bc0 | 721 | int err = 0; |
58687acb | 722 | |
3c00ea82 | 723 | if (!watchdog_running) { |
230ec939 FW |
724 | err = smpboot_register_percpu_thread_cpumask(&watchdog_threads, |
725 | &watchdog_cpumask); | |
b8900bc0 FW |
726 | if (err) |
727 | pr_err("Failed to create watchdog threads, disabled\n"); | |
230ec939 | 728 | else |
3c00ea82 | 729 | watchdog_running = 1; |
b2f57c3a UO |
730 | } else { |
731 | /* | |
732 | * Enable/disable the lockup detectors or | |
733 | * change the sample period 'on the fly'. | |
734 | */ | |
735 | update_watchdog_all_cpus(); | |
bcd951cf | 736 | } |
b8900bc0 FW |
737 | |
738 | return err; | |
58687acb DZ |
739 | } |
740 | ||
b8900bc0 FW |
741 | /* prepare/enable/disable routines */ |
742 | /* sysctl functions */ | |
743 | #ifdef CONFIG_SYSCTL | |
58687acb DZ |
744 | static void watchdog_disable_all_cpus(void) |
745 | { | |
3c00ea82 FW |
746 | if (watchdog_running) { |
747 | watchdog_running = 0; | |
b8900bc0 | 748 | smpboot_unregister_percpu_thread(&watchdog_threads); |
bcd951cf | 749 | } |
58687acb DZ |
750 | } |
751 | ||
58687acb | 752 | /* |
a0c9cbb9 UO |
753 | * Update the run state of the lockup detectors. |
754 | */ | |
755 | static int proc_watchdog_update(void) | |
756 | { | |
757 | int err = 0; | |
758 | ||
759 | /* | |
760 | * Watchdog threads won't be started if they are already active. | |
761 | * The 'watchdog_running' variable in watchdog_*_all_cpus() takes | |
762 | * care of this. If those threads are already active, the sample | |
763 | * period will be updated and the lockup detectors will be enabled | |
764 | * or disabled 'on the fly'. | |
765 | */ | |
766 | if (watchdog_enabled && watchdog_thresh) | |
b2f57c3a | 767 | err = watchdog_enable_all_cpus(); |
a0c9cbb9 UO |
768 | else |
769 | watchdog_disable_all_cpus(); | |
770 | ||
771 | return err; | |
772 | ||
773 | } | |
774 | ||
ef246a21 UO |
775 | /* |
776 | * common function for watchdog, nmi_watchdog and soft_watchdog parameter | |
777 | * | |
778 | * caller | table->data points to | 'which' contains the flag(s) | |
779 | * -------------------|-----------------------|----------------------------- | |
780 | * proc_watchdog | watchdog_user_enabled | NMI_WATCHDOG_ENABLED or'ed | |
781 | * | | with SOFT_WATCHDOG_ENABLED | |
782 | * -------------------|-----------------------|----------------------------- | |
783 | * proc_nmi_watchdog | nmi_watchdog_enabled | NMI_WATCHDOG_ENABLED | |
784 | * -------------------|-----------------------|----------------------------- | |
785 | * proc_soft_watchdog | soft_watchdog_enabled | SOFT_WATCHDOG_ENABLED | |
786 | */ | |
787 | static int proc_watchdog_common(int which, struct ctl_table *table, int write, | |
788 | void __user *buffer, size_t *lenp, loff_t *ppos) | |
789 | { | |
790 | int err, old, new; | |
791 | int *watchdog_param = (int *)table->data; | |
792 | ||
793 | mutex_lock(&watchdog_proc_mutex); | |
794 | ||
8c073d27 UO |
795 | if (watchdog_suspended) { |
796 | /* no parameter changes allowed while watchdog is suspended */ | |
797 | err = -EAGAIN; | |
798 | goto out; | |
799 | } | |
800 | ||
ef246a21 UO |
801 | /* |
802 | * If the parameter is being read return the state of the corresponding | |
803 | * bit(s) in 'watchdog_enabled', else update 'watchdog_enabled' and the | |
804 | * run state of the lockup detectors. | |
805 | */ | |
806 | if (!write) { | |
807 | *watchdog_param = (watchdog_enabled & which) != 0; | |
808 | err = proc_dointvec_minmax(table, write, buffer, lenp, ppos); | |
809 | } else { | |
810 | err = proc_dointvec_minmax(table, write, buffer, lenp, ppos); | |
811 | if (err) | |
812 | goto out; | |
813 | ||
814 | /* | |
815 | * There is a race window between fetching the current value | |
816 | * from 'watchdog_enabled' and storing the new value. During | |
817 | * this race window, watchdog_nmi_enable() can sneak in and | |
818 | * clear the NMI_WATCHDOG_ENABLED bit in 'watchdog_enabled'. | |
819 | * The 'cmpxchg' detects this race and the loop retries. | |
820 | */ | |
821 | do { | |
822 | old = watchdog_enabled; | |
823 | /* | |
824 | * If the parameter value is not zero set the | |
825 | * corresponding bit(s), else clear it(them). | |
826 | */ | |
827 | if (*watchdog_param) | |
828 | new = old | which; | |
829 | else | |
830 | new = old & ~which; | |
831 | } while (cmpxchg(&watchdog_enabled, old, new) != old); | |
832 | ||
833 | /* | |
834 | * Update the run state of the lockup detectors. | |
835 | * Restore 'watchdog_enabled' on failure. | |
836 | */ | |
837 | err = proc_watchdog_update(); | |
838 | if (err) | |
839 | watchdog_enabled = old; | |
840 | } | |
841 | out: | |
842 | mutex_unlock(&watchdog_proc_mutex); | |
843 | return err; | |
844 | } | |
845 | ||
83a80a39 UO |
846 | /* |
847 | * /proc/sys/kernel/watchdog | |
848 | */ | |
849 | int proc_watchdog(struct ctl_table *table, int write, | |
850 | void __user *buffer, size_t *lenp, loff_t *ppos) | |
851 | { | |
852 | return proc_watchdog_common(NMI_WATCHDOG_ENABLED|SOFT_WATCHDOG_ENABLED, | |
853 | table, write, buffer, lenp, ppos); | |
854 | } | |
855 | ||
856 | /* | |
857 | * /proc/sys/kernel/nmi_watchdog | |
58687acb | 858 | */ |
83a80a39 UO |
859 | int proc_nmi_watchdog(struct ctl_table *table, int write, |
860 | void __user *buffer, size_t *lenp, loff_t *ppos) | |
861 | { | |
862 | return proc_watchdog_common(NMI_WATCHDOG_ENABLED, | |
863 | table, write, buffer, lenp, ppos); | |
864 | } | |
865 | ||
866 | /* | |
867 | * /proc/sys/kernel/soft_watchdog | |
868 | */ | |
869 | int proc_soft_watchdog(struct ctl_table *table, int write, | |
870 | void __user *buffer, size_t *lenp, loff_t *ppos) | |
871 | { | |
872 | return proc_watchdog_common(SOFT_WATCHDOG_ENABLED, | |
873 | table, write, buffer, lenp, ppos); | |
874 | } | |
58687acb | 875 | |
83a80a39 UO |
876 | /* |
877 | * /proc/sys/kernel/watchdog_thresh | |
878 | */ | |
879 | int proc_watchdog_thresh(struct ctl_table *table, int write, | |
880 | void __user *buffer, size_t *lenp, loff_t *ppos) | |
58687acb | 881 | { |
83a80a39 | 882 | int err, old; |
58687acb | 883 | |
359e6fab | 884 | mutex_lock(&watchdog_proc_mutex); |
bcd951cf | 885 | |
8c073d27 UO |
886 | if (watchdog_suspended) { |
887 | /* no parameter changes allowed while watchdog is suspended */ | |
888 | err = -EAGAIN; | |
889 | goto out; | |
890 | } | |
891 | ||
83a80a39 | 892 | old = ACCESS_ONCE(watchdog_thresh); |
b8900bc0 | 893 | err = proc_dointvec_minmax(table, write, buffer, lenp, ppos); |
83a80a39 | 894 | |
b8900bc0 | 895 | if (err || !write) |
359e6fab | 896 | goto out; |
e04ab2bc | 897 | |
b66a2356 | 898 | /* |
83a80a39 UO |
899 | * Update the sample period. |
900 | * Restore 'watchdog_thresh' on failure. | |
b66a2356 | 901 | */ |
83a80a39 UO |
902 | set_sample_period(); |
903 | err = proc_watchdog_update(); | |
904 | if (err) | |
905 | watchdog_thresh = old; | |
359e6fab MH |
906 | out: |
907 | mutex_unlock(&watchdog_proc_mutex); | |
b8900bc0 | 908 | return err; |
58687acb | 909 | } |
fe4ba3c3 CM |
910 | |
911 | /* | |
912 | * The cpumask is the mask of possible cpus that the watchdog can run | |
913 | * on, not the mask of cpus it is actually running on. This allows the | |
914 | * user to specify a mask that will include cpus that have not yet | |
915 | * been brought online, if desired. | |
916 | */ | |
917 | int proc_watchdog_cpumask(struct ctl_table *table, int write, | |
918 | void __user *buffer, size_t *lenp, loff_t *ppos) | |
919 | { | |
920 | int err; | |
921 | ||
922 | mutex_lock(&watchdog_proc_mutex); | |
8c073d27 UO |
923 | |
924 | if (watchdog_suspended) { | |
925 | /* no parameter changes allowed while watchdog is suspended */ | |
926 | err = -EAGAIN; | |
927 | goto out; | |
928 | } | |
929 | ||
fe4ba3c3 CM |
930 | err = proc_do_large_bitmap(table, write, buffer, lenp, ppos); |
931 | if (!err && write) { | |
932 | /* Remove impossible cpus to keep sysctl output cleaner. */ | |
933 | cpumask_and(&watchdog_cpumask, &watchdog_cpumask, | |
934 | cpu_possible_mask); | |
935 | ||
936 | if (watchdog_running) { | |
937 | /* | |
938 | * Failure would be due to being unable to allocate | |
939 | * a temporary cpumask, so we are likely not in a | |
940 | * position to do much else to make things better. | |
941 | */ | |
942 | if (smpboot_update_cpumask_percpu_thread( | |
943 | &watchdog_threads, &watchdog_cpumask) != 0) | |
944 | pr_err("cpumask update failed\n"); | |
945 | } | |
946 | } | |
8c073d27 | 947 | out: |
fe4ba3c3 CM |
948 | mutex_unlock(&watchdog_proc_mutex); |
949 | return err; | |
950 | } | |
951 | ||
58687acb DZ |
952 | #endif /* CONFIG_SYSCTL */ |
953 | ||
004417a6 | 954 | void __init lockup_detector_init(void) |
58687acb | 955 | { |
0f34c400 | 956 | set_sample_period(); |
b8900bc0 | 957 | |
fe4ba3c3 CM |
958 | #ifdef CONFIG_NO_HZ_FULL |
959 | if (tick_nohz_full_enabled()) { | |
314b08ff FW |
960 | pr_info("Disabling watchdog on nohz_full cores by default\n"); |
961 | cpumask_copy(&watchdog_cpumask, housekeeping_mask); | |
fe4ba3c3 CM |
962 | } else |
963 | cpumask_copy(&watchdog_cpumask, cpu_possible_mask); | |
964 | #else | |
965 | cpumask_copy(&watchdog_cpumask, cpu_possible_mask); | |
966 | #endif | |
967 | ||
195daf66 | 968 | if (watchdog_enabled) |
b2f57c3a | 969 | watchdog_enable_all_cpus(); |
58687acb | 970 | } |