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