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