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 | ||
5f92a7b0 | 12 | #define pr_fmt(fmt) "watchdog: " fmt |
4501980a | 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> |
ae7e81c0 | 22 | #include <uapi/linux/sched/types.h> |
fe4ba3c3 | 23 | #include <linux/tick.h> |
82607adc | 24 | #include <linux/workqueue.h> |
e6017571 | 25 | #include <linux/sched/clock.h> |
b17b0153 | 26 | #include <linux/sched/debug.h> |
58687acb DZ |
27 | |
28 | #include <asm/irq_regs.h> | |
5d1c0f4a | 29 | #include <linux/kvm_para.h> |
81a4beef | 30 | #include <linux/kthread.h> |
58687acb | 31 | |
946d1977 | 32 | static DEFINE_MUTEX(watchdog_mutex); |
ab992dc3 | 33 | |
05a4a952 NP |
34 | int __read_mostly nmi_watchdog_enabled; |
35 | ||
36 | #if defined(CONFIG_HARDLOCKUP_DETECTOR) || defined(CONFIG_HAVE_NMI_WATCHDOG) | |
37 | unsigned long __read_mostly watchdog_enabled = SOFT_WATCHDOG_ENABLED | | |
38 | NMI_WATCHDOG_ENABLED; | |
84d56e66 | 39 | #else |
249e52e3 | 40 | unsigned long __read_mostly watchdog_enabled = SOFT_WATCHDOG_ENABLED; |
84d56e66 | 41 | #endif |
05a4a952 NP |
42 | |
43 | #ifdef CONFIG_HARDLOCKUP_DETECTOR | |
44 | /* boot commands */ | |
45 | /* | |
46 | * Should we panic when a soft-lockup or hard-lockup occurs: | |
47 | */ | |
48 | unsigned int __read_mostly hardlockup_panic = | |
49 | CONFIG_BOOTPARAM_HARDLOCKUP_PANIC_VALUE; | |
50 | /* | |
51 | * We may not want to enable hard lockup detection by default in all cases, | |
52 | * for example when running the kernel as a guest on a hypervisor. In these | |
53 | * cases this function can be called to disable hard lockup detection. This | |
54 | * function should only be executed once by the boot processor before the | |
55 | * kernel command line parameters are parsed, because otherwise it is not | |
56 | * possible to override this in hardlockup_panic_setup(). | |
57 | */ | |
7a355820 | 58 | void __init hardlockup_detector_disable(void) |
05a4a952 NP |
59 | { |
60 | watchdog_enabled &= ~NMI_WATCHDOG_ENABLED; | |
61 | } | |
62 | ||
63 | static int __init hardlockup_panic_setup(char *str) | |
64 | { | |
65 | if (!strncmp(str, "panic", 5)) | |
66 | hardlockup_panic = 1; | |
67 | else if (!strncmp(str, "nopanic", 7)) | |
68 | hardlockup_panic = 0; | |
69 | else if (!strncmp(str, "0", 1)) | |
70 | watchdog_enabled &= ~NMI_WATCHDOG_ENABLED; | |
71 | else if (!strncmp(str, "1", 1)) | |
72 | watchdog_enabled |= NMI_WATCHDOG_ENABLED; | |
73 | return 1; | |
74 | } | |
75 | __setup("nmi_watchdog=", hardlockup_panic_setup); | |
76 | ||
77 | #endif | |
78 | ||
79 | #ifdef CONFIG_SOFTLOCKUP_DETECTOR | |
84d56e66 | 80 | int __read_mostly soft_watchdog_enabled; |
05a4a952 NP |
81 | #endif |
82 | ||
84d56e66 | 83 | int __read_mostly watchdog_user_enabled; |
4eec42f3 | 84 | int __read_mostly watchdog_thresh = 10; |
84d56e66 | 85 | |
ed235875 AT |
86 | #ifdef CONFIG_SMP |
87 | int __read_mostly sysctl_softlockup_all_cpu_backtrace; | |
55537871 | 88 | int __read_mostly sysctl_hardlockup_all_cpu_backtrace; |
ed235875 | 89 | #endif |
05a4a952 | 90 | struct cpumask watchdog_cpumask __read_mostly; |
fe4ba3c3 CM |
91 | unsigned long *watchdog_cpumask_bits = cpumask_bits(&watchdog_cpumask); |
92 | ||
ec6a9066 UO |
93 | /* |
94 | * The 'watchdog_running' variable is set to 1 when the watchdog threads | |
95 | * are registered/started and is set to 0 when the watchdog threads are | |
96 | * unregistered/stopped, so it is an indicator whether the threads exist. | |
97 | */ | |
3c00ea82 | 98 | static int __read_mostly watchdog_running; |
05a4a952 NP |
99 | |
100 | /* | |
101 | * These functions can be overridden if an architecture implements its | |
102 | * own hardlockup detector. | |
a10a842f NP |
103 | * |
104 | * watchdog_nmi_enable/disable can be implemented to start and stop when | |
105 | * softlockup watchdog threads start and stop. The arch must select the | |
106 | * SOFTLOCKUP_DETECTOR Kconfig. | |
05a4a952 NP |
107 | */ |
108 | int __weak watchdog_nmi_enable(unsigned int cpu) | |
109 | { | |
110 | return 0; | |
111 | } | |
112 | void __weak watchdog_nmi_disable(unsigned int cpu) | |
113 | { | |
114 | } | |
115 | ||
a10a842f NP |
116 | /* |
117 | * watchdog_nmi_reconfigure can be implemented to be notified after any | |
118 | * watchdog configuration change. The arch hardlockup watchdog should | |
119 | * respond to the following variables: | |
120 | * - nmi_watchdog_enabled | |
121 | * - watchdog_thresh | |
122 | * - watchdog_cpumask | |
123 | * - sysctl_hardlockup_all_cpu_backtrace | |
124 | * - hardlockup_panic | |
a10a842f NP |
125 | */ |
126 | void __weak watchdog_nmi_reconfigure(void) | |
127 | { | |
128 | } | |
129 | ||
130 | ||
05a4a952 NP |
131 | #ifdef CONFIG_SOFTLOCKUP_DETECTOR |
132 | ||
133 | /* Helper for online, unparked cpus. */ | |
134 | #define for_each_watchdog_cpu(cpu) \ | |
135 | for_each_cpu_and((cpu), cpu_online_mask, &watchdog_cpumask) | |
136 | ||
137 | atomic_t watchdog_park_in_progress = ATOMIC_INIT(0); | |
ec6a9066 | 138 | |
0f34c400 | 139 | static u64 __read_mostly sample_period; |
58687acb DZ |
140 | |
141 | static DEFINE_PER_CPU(unsigned long, watchdog_touch_ts); | |
142 | static DEFINE_PER_CPU(struct task_struct *, softlockup_watchdog); | |
143 | static DEFINE_PER_CPU(struct hrtimer, watchdog_hrtimer); | |
144 | static DEFINE_PER_CPU(bool, softlockup_touch_sync); | |
58687acb | 145 | static DEFINE_PER_CPU(bool, soft_watchdog_warn); |
bcd951cf TG |
146 | static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts); |
147 | static DEFINE_PER_CPU(unsigned long, soft_lockup_hrtimer_cnt); | |
b1a8de1f | 148 | static DEFINE_PER_CPU(struct task_struct *, softlockup_task_ptr_saved); |
58687acb | 149 | static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts_saved); |
ed235875 | 150 | static unsigned long soft_lockup_nmi_warn; |
58687acb | 151 | |
58687acb DZ |
152 | unsigned int __read_mostly softlockup_panic = |
153 | CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC_VALUE; | |
154 | ||
155 | static int __init softlockup_panic_setup(char *str) | |
156 | { | |
157 | softlockup_panic = simple_strtoul(str, NULL, 0); | |
158 | ||
159 | return 1; | |
160 | } | |
161 | __setup("softlockup_panic=", softlockup_panic_setup); | |
162 | ||
163 | static int __init nowatchdog_setup(char *str) | |
164 | { | |
195daf66 | 165 | watchdog_enabled = 0; |
58687acb DZ |
166 | return 1; |
167 | } | |
168 | __setup("nowatchdog", nowatchdog_setup); | |
169 | ||
58687acb DZ |
170 | static int __init nosoftlockup_setup(char *str) |
171 | { | |
195daf66 | 172 | watchdog_enabled &= ~SOFT_WATCHDOG_ENABLED; |
58687acb DZ |
173 | return 1; |
174 | } | |
175 | __setup("nosoftlockup", nosoftlockup_setup); | |
195daf66 | 176 | |
ed235875 AT |
177 | #ifdef CONFIG_SMP |
178 | static int __init softlockup_all_cpu_backtrace_setup(char *str) | |
179 | { | |
180 | sysctl_softlockup_all_cpu_backtrace = | |
181 | !!simple_strtol(str, NULL, 0); | |
182 | return 1; | |
183 | } | |
184 | __setup("softlockup_all_cpu_backtrace=", softlockup_all_cpu_backtrace_setup); | |
05a4a952 | 185 | #ifdef CONFIG_HARDLOCKUP_DETECTOR |
55537871 JK |
186 | static int __init hardlockup_all_cpu_backtrace_setup(char *str) |
187 | { | |
188 | sysctl_hardlockup_all_cpu_backtrace = | |
189 | !!simple_strtol(str, NULL, 0); | |
190 | return 1; | |
191 | } | |
192 | __setup("hardlockup_all_cpu_backtrace=", hardlockup_all_cpu_backtrace_setup); | |
ed235875 | 193 | #endif |
05a4a952 | 194 | #endif |
58687acb | 195 | |
4eec42f3 MSB |
196 | /* |
197 | * Hard-lockup warnings should be triggered after just a few seconds. Soft- | |
198 | * lockups can have false positives under extreme conditions. So we generally | |
199 | * want a higher threshold for soft lockups than for hard lockups. So we couple | |
200 | * the thresholds with a factor: we make the soft threshold twice the amount of | |
201 | * time the hard threshold is. | |
202 | */ | |
6e9101ae | 203 | static int get_softlockup_thresh(void) |
4eec42f3 MSB |
204 | { |
205 | return watchdog_thresh * 2; | |
206 | } | |
58687acb DZ |
207 | |
208 | /* | |
209 | * Returns seconds, approximately. We don't need nanosecond | |
210 | * resolution, and we don't need to waste time with a big divide when | |
211 | * 2^30ns == 1.074s. | |
212 | */ | |
c06b4f19 | 213 | static unsigned long get_timestamp(void) |
58687acb | 214 | { |
545a2bf7 | 215 | return running_clock() >> 30LL; /* 2^30 ~= 10^9 */ |
58687acb DZ |
216 | } |
217 | ||
0f34c400 | 218 | static void set_sample_period(void) |
58687acb DZ |
219 | { |
220 | /* | |
586692a5 | 221 | * convert watchdog_thresh from seconds to ns |
86f5e6a7 FLVC |
222 | * the divide by 5 is to give hrtimer several chances (two |
223 | * or three with the current relation between the soft | |
224 | * and hard thresholds) to increment before the | |
225 | * hardlockup detector generates a warning | |
58687acb | 226 | */ |
0f34c400 | 227 | sample_period = get_softlockup_thresh() * ((u64)NSEC_PER_SEC / 5); |
7edaeb68 | 228 | watchdog_update_hrtimer_threshold(sample_period); |
58687acb DZ |
229 | } |
230 | ||
231 | /* Commands for resetting the watchdog */ | |
232 | static void __touch_watchdog(void) | |
233 | { | |
c06b4f19 | 234 | __this_cpu_write(watchdog_touch_ts, get_timestamp()); |
58687acb DZ |
235 | } |
236 | ||
03e0d461 TH |
237 | /** |
238 | * touch_softlockup_watchdog_sched - touch watchdog on scheduler stalls | |
239 | * | |
240 | * Call when the scheduler may have stalled for legitimate reasons | |
241 | * preventing the watchdog task from executing - e.g. the scheduler | |
242 | * entering idle state. This should only be used for scheduler events. | |
243 | * Use touch_softlockup_watchdog() for everything else. | |
244 | */ | |
245 | void touch_softlockup_watchdog_sched(void) | |
58687acb | 246 | { |
7861144b AM |
247 | /* |
248 | * Preemption can be enabled. It doesn't matter which CPU's timestamp | |
249 | * gets zeroed here, so use the raw_ operation. | |
250 | */ | |
251 | raw_cpu_write(watchdog_touch_ts, 0); | |
58687acb | 252 | } |
03e0d461 TH |
253 | |
254 | void touch_softlockup_watchdog(void) | |
255 | { | |
256 | touch_softlockup_watchdog_sched(); | |
82607adc | 257 | wq_watchdog_touch(raw_smp_processor_id()); |
03e0d461 | 258 | } |
0167c781 | 259 | EXPORT_SYMBOL(touch_softlockup_watchdog); |
58687acb | 260 | |
332fbdbc | 261 | void touch_all_softlockup_watchdogs(void) |
58687acb DZ |
262 | { |
263 | int cpu; | |
264 | ||
265 | /* | |
266 | * this is done lockless | |
267 | * do we care if a 0 races with a timestamp? | |
268 | * all it means is the softlock check starts one cycle later | |
269 | */ | |
fe4ba3c3 | 270 | for_each_watchdog_cpu(cpu) |
58687acb | 271 | per_cpu(watchdog_touch_ts, cpu) = 0; |
82607adc | 272 | wq_watchdog_touch(-1); |
58687acb DZ |
273 | } |
274 | ||
58687acb DZ |
275 | void touch_softlockup_watchdog_sync(void) |
276 | { | |
f7f66b05 CL |
277 | __this_cpu_write(softlockup_touch_sync, true); |
278 | __this_cpu_write(watchdog_touch_ts, 0); | |
58687acb DZ |
279 | } |
280 | ||
26e09c6e | 281 | static int is_softlockup(unsigned long touch_ts) |
58687acb | 282 | { |
c06b4f19 | 283 | unsigned long now = get_timestamp(); |
58687acb | 284 | |
39d2da21 | 285 | if ((watchdog_enabled & SOFT_WATCHDOG_ENABLED) && watchdog_thresh){ |
195daf66 UO |
286 | /* Warn about unreasonable delays. */ |
287 | if (time_after(now, touch_ts + get_softlockup_thresh())) | |
288 | return now - touch_ts; | |
289 | } | |
58687acb DZ |
290 | return 0; |
291 | } | |
292 | ||
05a4a952 NP |
293 | /* watchdog detector functions */ |
294 | bool is_hardlockup(void) | |
58687acb | 295 | { |
05a4a952 | 296 | unsigned long hrint = __this_cpu_read(hrtimer_interrupts); |
bcd951cf | 297 | |
05a4a952 NP |
298 | if (__this_cpu_read(hrtimer_interrupts_saved) == hrint) |
299 | return true; | |
300 | ||
301 | __this_cpu_write(hrtimer_interrupts_saved, hrint); | |
302 | return false; | |
73ce0511 | 303 | } |
05a4a952 NP |
304 | |
305 | static void watchdog_interrupt_count(void) | |
73ce0511 | 306 | { |
05a4a952 | 307 | __this_cpu_inc(hrtimer_interrupts); |
73ce0511 | 308 | } |
58687acb | 309 | |
58cf690a UO |
310 | static int watchdog_enable_all_cpus(void); |
311 | static void watchdog_disable_all_cpus(void); | |
312 | ||
58687acb DZ |
313 | /* watchdog kicker functions */ |
314 | static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer) | |
315 | { | |
909ea964 | 316 | unsigned long touch_ts = __this_cpu_read(watchdog_touch_ts); |
58687acb DZ |
317 | struct pt_regs *regs = get_irq_regs(); |
318 | int duration; | |
ed235875 | 319 | int softlockup_all_cpu_backtrace = sysctl_softlockup_all_cpu_backtrace; |
58687acb | 320 | |
6554fd8c TG |
321 | if (!watchdog_enabled || |
322 | atomic_read(&watchdog_park_in_progress) != 0) | |
b94f5118 DZ |
323 | return HRTIMER_NORESTART; |
324 | ||
58687acb DZ |
325 | /* kick the hardlockup detector */ |
326 | watchdog_interrupt_count(); | |
327 | ||
328 | /* kick the softlockup detector */ | |
909ea964 | 329 | wake_up_process(__this_cpu_read(softlockup_watchdog)); |
58687acb DZ |
330 | |
331 | /* .. and repeat */ | |
0f34c400 | 332 | hrtimer_forward_now(hrtimer, ns_to_ktime(sample_period)); |
58687acb DZ |
333 | |
334 | if (touch_ts == 0) { | |
909ea964 | 335 | if (unlikely(__this_cpu_read(softlockup_touch_sync))) { |
58687acb DZ |
336 | /* |
337 | * If the time stamp was touched atomically | |
338 | * make sure the scheduler tick is up to date. | |
339 | */ | |
909ea964 | 340 | __this_cpu_write(softlockup_touch_sync, false); |
58687acb DZ |
341 | sched_clock_tick(); |
342 | } | |
5d1c0f4a EM |
343 | |
344 | /* Clear the guest paused flag on watchdog reset */ | |
345 | kvm_check_and_clear_guest_paused(); | |
58687acb DZ |
346 | __touch_watchdog(); |
347 | return HRTIMER_RESTART; | |
348 | } | |
349 | ||
350 | /* check for a softlockup | |
351 | * This is done by making sure a high priority task is | |
352 | * being scheduled. The task touches the watchdog to | |
353 | * indicate it is getting cpu time. If it hasn't then | |
354 | * this is a good indication some task is hogging the cpu | |
355 | */ | |
26e09c6e | 356 | duration = is_softlockup(touch_ts); |
58687acb | 357 | if (unlikely(duration)) { |
5d1c0f4a EM |
358 | /* |
359 | * If a virtual machine is stopped by the host it can look to | |
360 | * the watchdog like a soft lockup, check to see if the host | |
361 | * stopped the vm before we issue the warning | |
362 | */ | |
363 | if (kvm_check_and_clear_guest_paused()) | |
364 | return HRTIMER_RESTART; | |
365 | ||
58687acb | 366 | /* only warn once */ |
b1a8de1f | 367 | if (__this_cpu_read(soft_watchdog_warn) == true) { |
368 | /* | |
369 | * When multiple processes are causing softlockups the | |
370 | * softlockup detector only warns on the first one | |
371 | * because the code relies on a full quiet cycle to | |
372 | * re-arm. The second process prevents the quiet cycle | |
373 | * and never gets reported. Use task pointers to detect | |
374 | * this. | |
375 | */ | |
376 | if (__this_cpu_read(softlockup_task_ptr_saved) != | |
377 | current) { | |
378 | __this_cpu_write(soft_watchdog_warn, false); | |
379 | __touch_watchdog(); | |
380 | } | |
58687acb | 381 | return HRTIMER_RESTART; |
b1a8de1f | 382 | } |
58687acb | 383 | |
ed235875 AT |
384 | if (softlockup_all_cpu_backtrace) { |
385 | /* Prevent multiple soft-lockup reports if one cpu is already | |
386 | * engaged in dumping cpu back traces | |
387 | */ | |
388 | if (test_and_set_bit(0, &soft_lockup_nmi_warn)) { | |
389 | /* Someone else will report us. Let's give up */ | |
390 | __this_cpu_write(soft_watchdog_warn, true); | |
391 | return HRTIMER_RESTART; | |
392 | } | |
393 | } | |
394 | ||
656c3b79 | 395 | pr_emerg("BUG: soft lockup - CPU#%d stuck for %us! [%s:%d]\n", |
26e09c6e | 396 | smp_processor_id(), duration, |
58687acb | 397 | current->comm, task_pid_nr(current)); |
b1a8de1f | 398 | __this_cpu_write(softlockup_task_ptr_saved, current); |
58687acb DZ |
399 | print_modules(); |
400 | print_irqtrace_events(current); | |
401 | if (regs) | |
402 | show_regs(regs); | |
403 | else | |
404 | dump_stack(); | |
405 | ||
ed235875 AT |
406 | if (softlockup_all_cpu_backtrace) { |
407 | /* Avoid generating two back traces for current | |
408 | * given that one is already made above | |
409 | */ | |
410 | trigger_allbutself_cpu_backtrace(); | |
411 | ||
412 | clear_bit(0, &soft_lockup_nmi_warn); | |
413 | /* Barrier to sync with other cpus */ | |
414 | smp_mb__after_atomic(); | |
415 | } | |
416 | ||
69361eef | 417 | add_taint(TAINT_SOFTLOCKUP, LOCKDEP_STILL_OK); |
58687acb DZ |
418 | if (softlockup_panic) |
419 | panic("softlockup: hung tasks"); | |
909ea964 | 420 | __this_cpu_write(soft_watchdog_warn, true); |
58687acb | 421 | } else |
909ea964 | 422 | __this_cpu_write(soft_watchdog_warn, false); |
58687acb DZ |
423 | |
424 | return HRTIMER_RESTART; | |
425 | } | |
426 | ||
bcd951cf TG |
427 | static void watchdog_set_prio(unsigned int policy, unsigned int prio) |
428 | { | |
429 | struct sched_param param = { .sched_priority = prio }; | |
58687acb | 430 | |
bcd951cf TG |
431 | sched_setscheduler(current, policy, ¶m); |
432 | } | |
433 | ||
434 | static void watchdog_enable(unsigned int cpu) | |
58687acb | 435 | { |
f7f66b05 | 436 | struct hrtimer *hrtimer = raw_cpu_ptr(&watchdog_hrtimer); |
58687acb | 437 | |
3935e895 BM |
438 | /* kick off the timer for the hardlockup detector */ |
439 | hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); | |
440 | hrtimer->function = watchdog_timer_fn; | |
441 | ||
bcd951cf TG |
442 | /* Enable the perf event */ |
443 | watchdog_nmi_enable(cpu); | |
58687acb | 444 | |
58687acb | 445 | /* done here because hrtimer_start can only pin to smp_processor_id() */ |
0f34c400 | 446 | hrtimer_start(hrtimer, ns_to_ktime(sample_period), |
58687acb DZ |
447 | HRTIMER_MODE_REL_PINNED); |
448 | ||
bcd951cf TG |
449 | /* initialize timestamp */ |
450 | watchdog_set_prio(SCHED_FIFO, MAX_RT_PRIO - 1); | |
451 | __touch_watchdog(); | |
452 | } | |
58687acb | 453 | |
bcd951cf TG |
454 | static void watchdog_disable(unsigned int cpu) |
455 | { | |
f7f66b05 | 456 | struct hrtimer *hrtimer = raw_cpu_ptr(&watchdog_hrtimer); |
58687acb | 457 | |
bcd951cf TG |
458 | watchdog_set_prio(SCHED_NORMAL, 0); |
459 | hrtimer_cancel(hrtimer); | |
460 | /* disable the perf event */ | |
461 | watchdog_nmi_disable(cpu); | |
58687acb DZ |
462 | } |
463 | ||
b8900bc0 FW |
464 | static void watchdog_cleanup(unsigned int cpu, bool online) |
465 | { | |
466 | watchdog_disable(cpu); | |
467 | } | |
468 | ||
bcd951cf TG |
469 | static int watchdog_should_run(unsigned int cpu) |
470 | { | |
471 | return __this_cpu_read(hrtimer_interrupts) != | |
472 | __this_cpu_read(soft_lockup_hrtimer_cnt); | |
473 | } | |
474 | ||
475 | /* | |
476 | * The watchdog thread function - touches the timestamp. | |
477 | * | |
0f34c400 | 478 | * It only runs once every sample_period seconds (4 seconds by |
bcd951cf TG |
479 | * default) to reset the softlockup timestamp. If this gets delayed |
480 | * for more than 2*watchdog_thresh seconds then the debug-printout | |
481 | * triggers in watchdog_timer_fn(). | |
482 | */ | |
483 | static void watchdog(unsigned int cpu) | |
484 | { | |
485 | __this_cpu_write(soft_lockup_hrtimer_cnt, | |
486 | __this_cpu_read(hrtimer_interrupts)); | |
487 | __touch_watchdog(); | |
488 | } | |
58687acb | 489 | |
b8900bc0 FW |
490 | static struct smp_hotplug_thread watchdog_threads = { |
491 | .store = &softlockup_watchdog, | |
492 | .thread_should_run = watchdog_should_run, | |
493 | .thread_fn = watchdog, | |
494 | .thread_comm = "watchdog/%u", | |
495 | .setup = watchdog_enable, | |
496 | .cleanup = watchdog_cleanup, | |
497 | .park = watchdog_disable, | |
498 | .unpark = watchdog_enable, | |
499 | }; | |
500 | ||
81a4beef UO |
501 | /* |
502 | * park all watchdog threads that are specified in 'watchdog_cpumask' | |
ee7fed54 UO |
503 | * |
504 | * This function returns an error if kthread_park() of a watchdog thread | |
505 | * fails. In this situation, the watchdog threads of some CPUs can already | |
506 | * be parked and the watchdog threads of other CPUs can still be runnable. | |
507 | * Callers are expected to handle this special condition as appropriate in | |
508 | * their context. | |
a2a45b85 UO |
509 | * |
510 | * This function may only be called in a context that is protected against | |
511 | * races with CPU hotplug - for example, via get_online_cpus(). | |
81a4beef UO |
512 | */ |
513 | static int watchdog_park_threads(void) | |
514 | { | |
515 | int cpu, ret = 0; | |
516 | ||
b94f5118 DZ |
517 | atomic_set(&watchdog_park_in_progress, 1); |
518 | ||
81a4beef UO |
519 | for_each_watchdog_cpu(cpu) { |
520 | ret = kthread_park(per_cpu(softlockup_watchdog, cpu)); | |
521 | if (ret) | |
522 | break; | |
523 | } | |
81a4beef | 524 | |
b94f5118 DZ |
525 | atomic_set(&watchdog_park_in_progress, 0); |
526 | ||
81a4beef UO |
527 | return ret; |
528 | } | |
529 | ||
530 | /* | |
531 | * unpark all watchdog threads that are specified in 'watchdog_cpumask' | |
a2a45b85 UO |
532 | * |
533 | * This function may only be called in a context that is protected against | |
534 | * races with CPU hotplug - for example, via get_online_cpus(). | |
81a4beef UO |
535 | */ |
536 | static void watchdog_unpark_threads(void) | |
537 | { | |
538 | int cpu; | |
539 | ||
81a4beef UO |
540 | for_each_watchdog_cpu(cpu) |
541 | kthread_unpark(per_cpu(softlockup_watchdog, cpu)); | |
81a4beef UO |
542 | } |
543 | ||
b43cb43c | 544 | static int update_watchdog_all_cpus(void) |
9809b18f | 545 | { |
b43cb43c UO |
546 | int ret; |
547 | ||
548 | ret = watchdog_park_threads(); | |
549 | if (ret) | |
550 | return ret; | |
551 | ||
d4bdd0b2 | 552 | watchdog_unpark_threads(); |
b43cb43c UO |
553 | |
554 | return 0; | |
9809b18f MH |
555 | } |
556 | ||
b2f57c3a | 557 | static int watchdog_enable_all_cpus(void) |
58687acb | 558 | { |
b8900bc0 | 559 | int err = 0; |
58687acb | 560 | |
3c00ea82 | 561 | if (!watchdog_running) { |
230ec939 FW |
562 | err = smpboot_register_percpu_thread_cpumask(&watchdog_threads, |
563 | &watchdog_cpumask); | |
b8900bc0 FW |
564 | if (err) |
565 | pr_err("Failed to create watchdog threads, disabled\n"); | |
230ec939 | 566 | else |
3c00ea82 | 567 | watchdog_running = 1; |
b2f57c3a UO |
568 | } else { |
569 | /* | |
570 | * Enable/disable the lockup detectors or | |
571 | * change the sample period 'on the fly'. | |
572 | */ | |
b43cb43c UO |
573 | err = update_watchdog_all_cpus(); |
574 | ||
575 | if (err) { | |
576 | watchdog_disable_all_cpus(); | |
577 | pr_err("Failed to update lockup detectors, disabled\n"); | |
578 | } | |
bcd951cf | 579 | } |
b8900bc0 | 580 | |
b43cb43c UO |
581 | if (err) |
582 | watchdog_enabled = 0; | |
583 | ||
b8900bc0 | 584 | return err; |
58687acb DZ |
585 | } |
586 | ||
587 | static void watchdog_disable_all_cpus(void) | |
588 | { | |
3c00ea82 FW |
589 | if (watchdog_running) { |
590 | watchdog_running = 0; | |
b8900bc0 | 591 | smpboot_unregister_percpu_thread(&watchdog_threads); |
bcd951cf | 592 | } |
58687acb DZ |
593 | } |
594 | ||
a10a842f NP |
595 | #ifdef CONFIG_SYSCTL |
596 | static int watchdog_update_cpus(void) | |
597 | { | |
598 | return smpboot_update_cpumask_percpu_thread( | |
599 | &watchdog_threads, &watchdog_cpumask); | |
600 | } | |
601 | #endif | |
602 | ||
05a4a952 NP |
603 | #else /* SOFTLOCKUP */ |
604 | static int watchdog_park_threads(void) | |
605 | { | |
606 | return 0; | |
607 | } | |
608 | ||
609 | static void watchdog_unpark_threads(void) | |
610 | { | |
611 | } | |
612 | ||
613 | static int watchdog_enable_all_cpus(void) | |
614 | { | |
615 | return 0; | |
616 | } | |
617 | ||
618 | static void watchdog_disable_all_cpus(void) | |
619 | { | |
620 | } | |
621 | ||
a10a842f NP |
622 | #ifdef CONFIG_SYSCTL |
623 | static int watchdog_update_cpus(void) | |
624 | { | |
625 | return 0; | |
626 | } | |
627 | #endif | |
628 | ||
05a4a952 NP |
629 | static void set_sample_period(void) |
630 | { | |
631 | } | |
632 | #endif /* SOFTLOCKUP */ | |
633 | ||
6554fd8c TG |
634 | /** |
635 | * lockup_detector_soft_poweroff - Interface to stop lockup detector(s) | |
636 | * | |
637 | * Special interface for parisc. It prevents lockup detector warnings from | |
638 | * the default pm_poweroff() function which busy loops forever. | |
639 | */ | |
640 | void lockup_detector_soft_poweroff(void) | |
641 | { | |
642 | watchdog_enabled = 0; | |
643 | } | |
644 | ||
58cf690a UO |
645 | #ifdef CONFIG_SYSCTL |
646 | ||
58687acb | 647 | /* |
a0c9cbb9 UO |
648 | * Update the run state of the lockup detectors. |
649 | */ | |
650 | static int proc_watchdog_update(void) | |
651 | { | |
652 | int err = 0; | |
653 | ||
654 | /* | |
655 | * Watchdog threads won't be started if they are already active. | |
656 | * The 'watchdog_running' variable in watchdog_*_all_cpus() takes | |
657 | * care of this. If those threads are already active, the sample | |
658 | * period will be updated and the lockup detectors will be enabled | |
659 | * or disabled 'on the fly'. | |
660 | */ | |
661 | if (watchdog_enabled && watchdog_thresh) | |
b2f57c3a | 662 | err = watchdog_enable_all_cpus(); |
a0c9cbb9 UO |
663 | else |
664 | watchdog_disable_all_cpus(); | |
665 | ||
a10a842f NP |
666 | watchdog_nmi_reconfigure(); |
667 | ||
a0c9cbb9 UO |
668 | return err; |
669 | ||
670 | } | |
671 | ||
ef246a21 UO |
672 | /* |
673 | * common function for watchdog, nmi_watchdog and soft_watchdog parameter | |
674 | * | |
675 | * caller | table->data points to | 'which' contains the flag(s) | |
676 | * -------------------|-----------------------|----------------------------- | |
677 | * proc_watchdog | watchdog_user_enabled | NMI_WATCHDOG_ENABLED or'ed | |
678 | * | | with SOFT_WATCHDOG_ENABLED | |
679 | * -------------------|-----------------------|----------------------------- | |
680 | * proc_nmi_watchdog | nmi_watchdog_enabled | NMI_WATCHDOG_ENABLED | |
681 | * -------------------|-----------------------|----------------------------- | |
682 | * proc_soft_watchdog | soft_watchdog_enabled | SOFT_WATCHDOG_ENABLED | |
683 | */ | |
684 | static int proc_watchdog_common(int which, struct ctl_table *table, int write, | |
685 | void __user *buffer, size_t *lenp, loff_t *ppos) | |
686 | { | |
687 | int err, old, new; | |
688 | int *watchdog_param = (int *)table->data; | |
689 | ||
b7a34981 | 690 | cpu_hotplug_disable(); |
946d1977 | 691 | mutex_lock(&watchdog_mutex); |
ef246a21 UO |
692 | |
693 | /* | |
694 | * If the parameter is being read return the state of the corresponding | |
695 | * bit(s) in 'watchdog_enabled', else update 'watchdog_enabled' and the | |
696 | * run state of the lockup detectors. | |
697 | */ | |
698 | if (!write) { | |
699 | *watchdog_param = (watchdog_enabled & which) != 0; | |
700 | err = proc_dointvec_minmax(table, write, buffer, lenp, ppos); | |
701 | } else { | |
702 | err = proc_dointvec_minmax(table, write, buffer, lenp, ppos); | |
703 | if (err) | |
704 | goto out; | |
705 | ||
706 | /* | |
707 | * There is a race window between fetching the current value | |
708 | * from 'watchdog_enabled' and storing the new value. During | |
709 | * this race window, watchdog_nmi_enable() can sneak in and | |
710 | * clear the NMI_WATCHDOG_ENABLED bit in 'watchdog_enabled'. | |
711 | * The 'cmpxchg' detects this race and the loop retries. | |
712 | */ | |
713 | do { | |
714 | old = watchdog_enabled; | |
715 | /* | |
716 | * If the parameter value is not zero set the | |
717 | * corresponding bit(s), else clear it(them). | |
718 | */ | |
719 | if (*watchdog_param) | |
720 | new = old | which; | |
721 | else | |
722 | new = old & ~which; | |
723 | } while (cmpxchg(&watchdog_enabled, old, new) != old); | |
724 | ||
725 | /* | |
b43cb43c UO |
726 | * Update the run state of the lockup detectors. There is _no_ |
727 | * need to check the value returned by proc_watchdog_update() | |
728 | * and to restore the previous value of 'watchdog_enabled' as | |
729 | * both lockup detectors are disabled if proc_watchdog_update() | |
730 | * returns an error. | |
ef246a21 | 731 | */ |
a1ee1932 JH |
732 | if (old == new) |
733 | goto out; | |
734 | ||
ef246a21 | 735 | err = proc_watchdog_update(); |
ef246a21 UO |
736 | } |
737 | out: | |
946d1977 | 738 | mutex_unlock(&watchdog_mutex); |
b7a34981 | 739 | cpu_hotplug_enable(); |
ef246a21 UO |
740 | return err; |
741 | } | |
742 | ||
83a80a39 UO |
743 | /* |
744 | * /proc/sys/kernel/watchdog | |
745 | */ | |
746 | int proc_watchdog(struct ctl_table *table, int write, | |
747 | void __user *buffer, size_t *lenp, loff_t *ppos) | |
748 | { | |
749 | return proc_watchdog_common(NMI_WATCHDOG_ENABLED|SOFT_WATCHDOG_ENABLED, | |
750 | table, write, buffer, lenp, ppos); | |
751 | } | |
752 | ||
753 | /* | |
754 | * /proc/sys/kernel/nmi_watchdog | |
58687acb | 755 | */ |
83a80a39 UO |
756 | int proc_nmi_watchdog(struct ctl_table *table, int write, |
757 | void __user *buffer, size_t *lenp, loff_t *ppos) | |
758 | { | |
759 | return proc_watchdog_common(NMI_WATCHDOG_ENABLED, | |
760 | table, write, buffer, lenp, ppos); | |
761 | } | |
762 | ||
763 | /* | |
764 | * /proc/sys/kernel/soft_watchdog | |
765 | */ | |
766 | int proc_soft_watchdog(struct ctl_table *table, int write, | |
767 | void __user *buffer, size_t *lenp, loff_t *ppos) | |
768 | { | |
769 | return proc_watchdog_common(SOFT_WATCHDOG_ENABLED, | |
770 | table, write, buffer, lenp, ppos); | |
771 | } | |
58687acb | 772 | |
83a80a39 UO |
773 | /* |
774 | * /proc/sys/kernel/watchdog_thresh | |
775 | */ | |
776 | int proc_watchdog_thresh(struct ctl_table *table, int write, | |
777 | void __user *buffer, size_t *lenp, loff_t *ppos) | |
58687acb | 778 | { |
a1ee1932 | 779 | int err, old, new; |
58687acb | 780 | |
b7a34981 | 781 | cpu_hotplug_disable(); |
946d1977 | 782 | mutex_lock(&watchdog_mutex); |
bcd951cf | 783 | |
83a80a39 | 784 | old = ACCESS_ONCE(watchdog_thresh); |
b8900bc0 | 785 | err = proc_dointvec_minmax(table, write, buffer, lenp, ppos); |
83a80a39 | 786 | |
b8900bc0 | 787 | if (err || !write) |
359e6fab | 788 | goto out; |
e04ab2bc | 789 | |
b66a2356 | 790 | /* |
d283c640 | 791 | * Update the sample period. Restore on failure. |
b66a2356 | 792 | */ |
a1ee1932 JH |
793 | new = ACCESS_ONCE(watchdog_thresh); |
794 | if (old == new) | |
795 | goto out; | |
796 | ||
83a80a39 UO |
797 | set_sample_period(); |
798 | err = proc_watchdog_update(); | |
d283c640 | 799 | if (err) { |
83a80a39 | 800 | watchdog_thresh = old; |
d283c640 UO |
801 | set_sample_period(); |
802 | } | |
359e6fab | 803 | out: |
946d1977 | 804 | mutex_unlock(&watchdog_mutex); |
b7a34981 | 805 | cpu_hotplug_enable(); |
b8900bc0 | 806 | return err; |
58687acb | 807 | } |
fe4ba3c3 CM |
808 | |
809 | /* | |
810 | * The cpumask is the mask of possible cpus that the watchdog can run | |
811 | * on, not the mask of cpus it is actually running on. This allows the | |
812 | * user to specify a mask that will include cpus that have not yet | |
813 | * been brought online, if desired. | |
814 | */ | |
815 | int proc_watchdog_cpumask(struct ctl_table *table, int write, | |
816 | void __user *buffer, size_t *lenp, loff_t *ppos) | |
817 | { | |
818 | int err; | |
819 | ||
b7a34981 | 820 | cpu_hotplug_disable(); |
946d1977 | 821 | mutex_lock(&watchdog_mutex); |
8c073d27 | 822 | |
fe4ba3c3 CM |
823 | err = proc_do_large_bitmap(table, write, buffer, lenp, ppos); |
824 | if (!err && write) { | |
825 | /* Remove impossible cpus to keep sysctl output cleaner. */ | |
826 | cpumask_and(&watchdog_cpumask, &watchdog_cpumask, | |
827 | cpu_possible_mask); | |
828 | ||
829 | if (watchdog_running) { | |
830 | /* | |
831 | * Failure would be due to being unable to allocate | |
832 | * a temporary cpumask, so we are likely not in a | |
833 | * position to do much else to make things better. | |
834 | */ | |
a10a842f | 835 | if (watchdog_update_cpus() != 0) |
fe4ba3c3 CM |
836 | pr_err("cpumask update failed\n"); |
837 | } | |
a10a842f NP |
838 | |
839 | watchdog_nmi_reconfigure(); | |
fe4ba3c3 | 840 | } |
5490125d | 841 | |
946d1977 | 842 | mutex_unlock(&watchdog_mutex); |
b7a34981 | 843 | cpu_hotplug_enable(); |
fe4ba3c3 CM |
844 | return err; |
845 | } | |
846 | ||
58687acb DZ |
847 | #endif /* CONFIG_SYSCTL */ |
848 | ||
004417a6 | 849 | void __init lockup_detector_init(void) |
58687acb | 850 | { |
0f34c400 | 851 | set_sample_period(); |
b8900bc0 | 852 | |
fe4ba3c3 CM |
853 | #ifdef CONFIG_NO_HZ_FULL |
854 | if (tick_nohz_full_enabled()) { | |
314b08ff FW |
855 | pr_info("Disabling watchdog on nohz_full cores by default\n"); |
856 | cpumask_copy(&watchdog_cpumask, housekeeping_mask); | |
fe4ba3c3 CM |
857 | } else |
858 | cpumask_copy(&watchdog_cpumask, cpu_possible_mask); | |
859 | #else | |
860 | cpumask_copy(&watchdog_cpumask, cpu_possible_mask); | |
861 | #endif | |
862 | ||
195daf66 | 863 | if (watchdog_enabled) |
b2f57c3a | 864 | watchdog_enable_all_cpus(); |
58687acb | 865 | } |