1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Detect hard and soft lockups on a system
4 *
5 * started by Don Zickus, Copyright (C) 2010 Red Hat, Inc.
6 *
7 * Note: Most of this code is borrowed heavily from the original softlockup
8 * detector, so thanks to Ingo for the initial implementation.
9 * Some chunks also taken from the old x86-specific nmi watchdog code, thanks
10 * to those contributors as well.
11 */
12
13 #define pr_fmt(fmt) "watchdog: " fmt
14
15 #include <linux/mm.h>
16 #include <linux/cpu.h>
17 #include <linux/nmi.h>
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/sysctl.h>
21 #include <linux/tick.h>
22 #include <linux/sched/clock.h>
23 #include <linux/sched/debug.h>
24 #include <linux/sched/isolation.h>
25 #include <linux/stop_machine.h>
26
27 #include <asm/irq_regs.h>
28 #include <linux/kvm_para.h>
29
30 #include <trace/hooks/softlockup.h>
31
32 static DEFINE_MUTEX(watchdog_mutex);
33
34 #if defined(CONFIG_HARDLOCKUP_DETECTOR) || defined(CONFIG_HAVE_NMI_WATCHDOG)
35 # define WATCHDOG_DEFAULT (SOFT_WATCHDOG_ENABLED | NMI_WATCHDOG_ENABLED)
36 # define NMI_WATCHDOG_DEFAULT 1
37 #else
38 # define WATCHDOG_DEFAULT (SOFT_WATCHDOG_ENABLED)
39 # define NMI_WATCHDOG_DEFAULT 0
40 #endif
41
42 unsigned long __read_mostly watchdog_enabled;
43 int __read_mostly watchdog_user_enabled = 1;
44 int __read_mostly nmi_watchdog_user_enabled = NMI_WATCHDOG_DEFAULT;
45 int __read_mostly soft_watchdog_user_enabled = 1;
46 int __read_mostly watchdog_thresh = 10;
47 static int __read_mostly nmi_watchdog_available;
48
49 struct cpumask watchdog_cpumask __read_mostly;
50 unsigned long *watchdog_cpumask_bits = cpumask_bits(&watchdog_cpumask);
51
52 #ifdef CONFIG_HARDLOCKUP_DETECTOR
53
54 # ifdef CONFIG_SMP
55 int __read_mostly sysctl_hardlockup_all_cpu_backtrace;
56 # endif /* CONFIG_SMP */
57
58 ATOMIC_NOTIFIER_HEAD(hardlock_notifier_list);
59
60 /*
61 * Should we panic when a soft-lockup or hard-lockup occurs:
62 */
63 unsigned int __read_mostly hardlockup_panic =
64 CONFIG_BOOTPARAM_HARDLOCKUP_PANIC_VALUE;
65 /*
66 * We may not want to enable hard lockup detection by default in all cases,
67 * for example when running the kernel as a guest on a hypervisor. In these
68 * cases this function can be called to disable hard lockup detection. This
69 * function should only be executed once by the boot processor before the
70 * kernel command line parameters are parsed, because otherwise it is not
71 * possible to override this in hardlockup_panic_setup().
72 */
hardlockup_detector_disable(void)73 void __init hardlockup_detector_disable(void)
74 {
75 nmi_watchdog_user_enabled = 0;
76 }
77
hardlockup_panic_setup(char * str)78 static int __init hardlockup_panic_setup(char *str)
79 {
80 if (!strncmp(str, "panic", 5))
81 hardlockup_panic = 1;
82 else if (!strncmp(str, "nopanic", 7))
83 hardlockup_panic = 0;
84 else if (!strncmp(str, "0", 1))
85 nmi_watchdog_user_enabled = 0;
86 else if (!strncmp(str, "1", 1))
87 nmi_watchdog_user_enabled = 1;
88 return 1;
89 }
90 __setup("nmi_watchdog=", hardlockup_panic_setup);
91
92 #endif /* CONFIG_HARDLOCKUP_DETECTOR */
93
94 #ifdef CONFIG_HARDLOCKUP_DETECTOR_OTHER_CPU
95 static cpumask_t __read_mostly watchdog_cpus;
96 static DEFINE_PER_CPU(bool, hard_watchdog_warn);
97 static DEFINE_PER_CPU(bool, watchdog_nmi_touch);
98 static unsigned int watchdog_next_cpu(unsigned int cpu);
99
watchdog_nmi_enable(unsigned int cpu)100 int watchdog_nmi_enable(unsigned int cpu)
101 {
102 /*
103 * The new cpu will be marked online before the first hrtimer interrupt
104 * runs on it. If another cpu tests for a hardlockup on the new cpu
105 * before it has run its first hrtimer, it will get a false positive.
106 * Touch the watchdog on the new cpu to delay the first check for at
107 * least 3 sampling periods to guarantee one hrtimer has run on the new
108 * cpu.
109 */
110 per_cpu(watchdog_nmi_touch, cpu) = true;
111 smp_wmb();
112 cpumask_set_cpu(cpu, &watchdog_cpus);
113 return 0;
114 }
115
watchdog_nmi_disable(unsigned int cpu)116 void watchdog_nmi_disable(unsigned int cpu)
117 {
118 unsigned int next_cpu = watchdog_next_cpu(cpu);
119
120 /*
121 * Offlining this cpu will cause the cpu before this one to start
122 * checking the one after this one. If this cpu just finished checking
123 * the next cpu and updating hrtimer_interrupts_saved, and then the
124 * previous cpu checks it within one sample period, it will trigger a
125 * false positive. Touch the watchdog on the next cpu to prevent it.
126 */
127 if (next_cpu < nr_cpu_ids)
128 per_cpu(watchdog_nmi_touch, next_cpu) = true;
129 smp_wmb();
130 cpumask_clear_cpu(cpu, &watchdog_cpus);
131 }
132 #else
133 /*
134 * These functions can be overridden if an architecture implements its
135 * own hardlockup detector.
136 *
137 * watchdog_nmi_enable/disable can be implemented to start and stop when
138 * softlockup watchdog threads start and stop. The arch must select the
139 * SOFTLOCKUP_DETECTOR Kconfig.
140 */
watchdog_nmi_enable(unsigned int cpu)141 int __weak watchdog_nmi_enable(unsigned int cpu)
142 {
143 hardlockup_detector_perf_enable();
144 return 0;
145 }
146
watchdog_nmi_disable(unsigned int cpu)147 void __weak watchdog_nmi_disable(unsigned int cpu)
148 {
149 hardlockup_detector_perf_disable();
150 }
151 #endif /* CONFIG_HARDLOCKUP_DETECTOR_OTHER_CPU */
152
153 /* Return 0, if a NMI watchdog is available. Error code otherwise */
watchdog_nmi_probe(void)154 int __weak __init watchdog_nmi_probe(void)
155 {
156 return hardlockup_detector_perf_init();
157 }
158
159 /**
160 * watchdog_nmi_stop - Stop the watchdog for reconfiguration
161 *
162 * The reconfiguration steps are:
163 * watchdog_nmi_stop();
164 * update_variables();
165 * watchdog_nmi_start();
166 */
watchdog_nmi_stop(void)167 void __weak watchdog_nmi_stop(void) { }
168
169 /**
170 * watchdog_nmi_start - Start the watchdog after reconfiguration
171 *
172 * Counterpart to watchdog_nmi_stop().
173 *
174 * The following variables have been updated in update_variables() and
175 * contain the currently valid configuration:
176 * - watchdog_enabled
177 * - watchdog_thresh
178 * - watchdog_cpumask
179 */
watchdog_nmi_start(void)180 void __weak watchdog_nmi_start(void) { }
181
182 /**
183 * lockup_detector_update_enable - Update the sysctl enable bit
184 *
185 * Caller needs to make sure that the NMI/perf watchdogs are off, so this
186 * can't race with watchdog_nmi_disable().
187 */
lockup_detector_update_enable(void)188 static void lockup_detector_update_enable(void)
189 {
190 watchdog_enabled = 0;
191 if (!watchdog_user_enabled)
192 return;
193 if (nmi_watchdog_available && nmi_watchdog_user_enabled)
194 watchdog_enabled |= NMI_WATCHDOG_ENABLED;
195 if (soft_watchdog_user_enabled)
196 watchdog_enabled |= SOFT_WATCHDOG_ENABLED;
197 }
198
199 #ifdef CONFIG_SOFTLOCKUP_DETECTOR
200
201 #define SOFTLOCKUP_RESET ULONG_MAX
202
203 #ifdef CONFIG_SMP
204 int __read_mostly sysctl_softlockup_all_cpu_backtrace;
205 #endif
206
207 static struct cpumask watchdog_allowed_mask __read_mostly;
208
209 /* Global variables, exported for sysctl */
210 unsigned int __read_mostly softlockup_panic =
211 CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC_VALUE;
212
213 static bool softlockup_initialized __read_mostly;
214 static u64 __read_mostly sample_period;
215
216 static DEFINE_PER_CPU(unsigned long, watchdog_touch_ts);
217 static DEFINE_PER_CPU(struct hrtimer, watchdog_hrtimer);
218 static DEFINE_PER_CPU(bool, softlockup_touch_sync);
219 static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts);
220 static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts_saved);
221 static unsigned long soft_lockup_nmi_warn;
222
nowatchdog_setup(char * str)223 static int __init nowatchdog_setup(char *str)
224 {
225 watchdog_user_enabled = 0;
226 return 1;
227 }
228 __setup("nowatchdog", nowatchdog_setup);
229
nosoftlockup_setup(char * str)230 static int __init nosoftlockup_setup(char *str)
231 {
232 soft_watchdog_user_enabled = 0;
233 return 1;
234 }
235 __setup("nosoftlockup", nosoftlockup_setup);
236
watchdog_thresh_setup(char * str)237 static int __init watchdog_thresh_setup(char *str)
238 {
239 get_option(&str, &watchdog_thresh);
240 return 1;
241 }
242 __setup("watchdog_thresh=", watchdog_thresh_setup);
243
244 static void __lockup_detector_cleanup(void);
245
246 /*
247 * Hard-lockup warnings should be triggered after just a few seconds. Soft-
248 * lockups can have false positives under extreme conditions. So we generally
249 * want a higher threshold for soft lockups than for hard lockups. So we couple
250 * the thresholds with a factor: we make the soft threshold twice the amount of
251 * time the hard threshold is.
252 */
get_softlockup_thresh(void)253 static int get_softlockup_thresh(void)
254 {
255 return watchdog_thresh * 2;
256 }
257
258 /*
259 * Returns seconds, approximately. We don't need nanosecond
260 * resolution, and we don't need to waste time with a big divide when
261 * 2^30ns == 1.074s.
262 */
get_timestamp(void)263 static unsigned long get_timestamp(void)
264 {
265 return running_clock() >> 30LL; /* 2^30 ~= 10^9 */
266 }
267
set_sample_period(void)268 static void set_sample_period(void)
269 {
270 /*
271 * convert watchdog_thresh from seconds to ns
272 * the divide by 5 is to give hrtimer several chances (two
273 * or three with the current relation between the soft
274 * and hard thresholds) to increment before the
275 * hardlockup detector generates a warning
276 */
277 sample_period = get_softlockup_thresh() * ((u64)NSEC_PER_SEC / 5);
278 watchdog_update_hrtimer_threshold(sample_period);
279 }
280
281 /* Commands for resetting the watchdog */
update_touch_ts(void)282 static void update_touch_ts(void)
283 {
284 __this_cpu_write(watchdog_touch_ts, get_timestamp());
285 }
286
287 /**
288 * touch_softlockup_watchdog_sched - touch watchdog on scheduler stalls
289 *
290 * Call when the scheduler may have stalled for legitimate reasons
291 * preventing the watchdog task from executing - e.g. the scheduler
292 * entering idle state. This should only be used for scheduler events.
293 * Use touch_softlockup_watchdog() for everything else.
294 */
touch_softlockup_watchdog_sched(void)295 notrace void touch_softlockup_watchdog_sched(void)
296 {
297 /*
298 * Preemption can be enabled. It doesn't matter which CPU's timestamp
299 * gets zeroed here, so use the raw_ operation.
300 */
301 raw_cpu_write(watchdog_touch_ts, SOFTLOCKUP_RESET);
302 }
303
touch_softlockup_watchdog(void)304 notrace void touch_softlockup_watchdog(void)
305 {
306 touch_softlockup_watchdog_sched();
307 wq_watchdog_touch(raw_smp_processor_id());
308 }
309 EXPORT_SYMBOL(touch_softlockup_watchdog);
310
touch_all_softlockup_watchdogs(void)311 void touch_all_softlockup_watchdogs(void)
312 {
313 int cpu;
314
315 /*
316 * watchdog_mutex cannpt be taken here, as this might be called
317 * from (soft)interrupt context, so the access to
318 * watchdog_allowed_cpumask might race with a concurrent update.
319 *
320 * The watchdog time stamp can race against a concurrent real
321 * update as well, the only side effect might be a cycle delay for
322 * the softlockup check.
323 */
324 for_each_cpu(cpu, &watchdog_allowed_mask)
325 per_cpu(watchdog_touch_ts, cpu) = SOFTLOCKUP_RESET;
326 wq_watchdog_touch(-1);
327 }
328
touch_softlockup_watchdog_sync(void)329 void touch_softlockup_watchdog_sync(void)
330 {
331 __this_cpu_write(softlockup_touch_sync, true);
332 __this_cpu_write(watchdog_touch_ts, SOFTLOCKUP_RESET);
333 }
334
is_softlockup(unsigned long touch_ts)335 static int is_softlockup(unsigned long touch_ts)
336 {
337 unsigned long now = get_timestamp();
338
339 if ((watchdog_enabled & SOFT_WATCHDOG_ENABLED) && watchdog_thresh){
340 /* Warn about unreasonable delays. */
341 if (time_after(now, touch_ts + get_softlockup_thresh()))
342 return now - touch_ts;
343 }
344 return 0;
345 }
346
347 /* watchdog detector functions */
is_hardlockup(void)348 bool is_hardlockup(void)
349 {
350 unsigned long hrint = __this_cpu_read(hrtimer_interrupts);
351
352 if (__this_cpu_read(hrtimer_interrupts_saved) == hrint)
353 return true;
354
355 __this_cpu_write(hrtimer_interrupts_saved, hrint);
356 return false;
357 }
358
359 #ifdef CONFIG_HARDLOCKUP_DETECTOR_OTHER_CPU
watchdog_next_cpu(unsigned int cpu)360 static unsigned int watchdog_next_cpu(unsigned int cpu)
361 {
362 cpumask_t cpus = watchdog_cpus;
363 unsigned int next_cpu;
364
365 next_cpu = cpumask_next(cpu, &cpus);
366 if (next_cpu >= nr_cpu_ids)
367 next_cpu = cpumask_first(&cpus);
368
369 if (next_cpu == cpu)
370 return nr_cpu_ids;
371
372 return next_cpu;
373 }
374
is_hardlockup_other_cpu(unsigned int cpu)375 static int is_hardlockup_other_cpu(unsigned int cpu)
376 {
377 unsigned long hrint = per_cpu(hrtimer_interrupts, cpu);
378
379 if (per_cpu(hrtimer_interrupts_saved, cpu) == hrint)
380 return 1;
381
382 per_cpu(hrtimer_interrupts_saved, cpu) = hrint;
383 return 0;
384 }
385
watchdog_check_hardlockup_other_cpu(void)386 static void watchdog_check_hardlockup_other_cpu(void)
387 {
388 unsigned int next_cpu;
389
390 /*
391 * Test for hardlockups every 3 samples. The sample period is
392 * watchdog_thresh * 2 / 5, so 3 samples gets us back to slightly over
393 * watchdog_thresh (over by 20%).
394 */
395 if (__this_cpu_read(hrtimer_interrupts) % 3 != 0)
396 return;
397
398 /* check for a hardlockup on the next cpu */
399 next_cpu = watchdog_next_cpu(smp_processor_id());
400 if (next_cpu >= nr_cpu_ids)
401 return;
402
403 smp_rmb();
404
405 if (per_cpu(watchdog_nmi_touch, next_cpu) == true) {
406 per_cpu(watchdog_nmi_touch, next_cpu) = false;
407 return;
408 }
409
410 if (is_hardlockup_other_cpu(next_cpu)) {
411 /* only warn once */
412 if (per_cpu(hard_watchdog_warn, next_cpu) == true)
413 return;
414
415 if (hardlockup_panic)
416 panic("Watchdog detected hard LOCKUP on cpu %u", next_cpu);
417 else
418 WARN(1, "Watchdog detected hard LOCKUP on cpu %u", next_cpu);
419
420 atomic_notifier_call_chain(&hardlock_notifier_list, 0, NULL);
421 per_cpu(hard_watchdog_warn, next_cpu) = true;
422 } else {
423 per_cpu(hard_watchdog_warn, next_cpu) = false;
424 }
425 }
426 #else
watchdog_check_hardlockup_other_cpu(void)427 static inline void watchdog_check_hardlockup_other_cpu(void) { return; }
428 #endif
429
watchdog_interrupt_count(void)430 static void watchdog_interrupt_count(void)
431 {
432 __this_cpu_inc(hrtimer_interrupts);
433 }
434
435 static DEFINE_PER_CPU(struct completion, softlockup_completion);
436 static DEFINE_PER_CPU(struct cpu_stop_work, softlockup_stop_work);
437
438 /*
439 * The watchdog thread function - touches the timestamp.
440 *
441 * It only runs once every sample_period seconds (4 seconds by
442 * default) to reset the softlockup timestamp. If this gets delayed
443 * for more than 2*watchdog_thresh seconds then the debug-printout
444 * triggers in watchdog_timer_fn().
445 */
softlockup_fn(void * data)446 static int softlockup_fn(void *data)
447 {
448 update_touch_ts();
449 complete(this_cpu_ptr(&softlockup_completion));
450
451 return 0;
452 }
453
454 /* watchdog kicker functions */
watchdog_timer_fn(struct hrtimer * hrtimer)455 static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer)
456 {
457 unsigned long touch_ts = __this_cpu_read(watchdog_touch_ts);
458 struct pt_regs *regs = get_irq_regs();
459 int duration;
460 int softlockup_all_cpu_backtrace = sysctl_softlockup_all_cpu_backtrace;
461
462 if (!watchdog_enabled)
463 return HRTIMER_NORESTART;
464
465 /* test for hardlockups on the next cpu */
466 if (IS_ENABLED(CONFIG_HARDLOCKUP_DETECTOR_OTHER_CPU))
467 watchdog_check_hardlockup_other_cpu();
468
469 /* kick the hardlockup detector */
470 watchdog_interrupt_count();
471
472 /* kick the softlockup detector */
473 if (completion_done(this_cpu_ptr(&softlockup_completion))) {
474 reinit_completion(this_cpu_ptr(&softlockup_completion));
475 stop_one_cpu_nowait(smp_processor_id(),
476 softlockup_fn, NULL,
477 this_cpu_ptr(&softlockup_stop_work));
478 }
479
480 /* .. and repeat */
481 hrtimer_forward_now(hrtimer, ns_to_ktime(sample_period));
482
483 if (touch_ts == SOFTLOCKUP_RESET) {
484 if (unlikely(__this_cpu_read(softlockup_touch_sync))) {
485 /*
486 * If the time stamp was touched atomically
487 * make sure the scheduler tick is up to date.
488 */
489 __this_cpu_write(softlockup_touch_sync, false);
490 sched_clock_tick();
491 }
492
493 /* Clear the guest paused flag on watchdog reset */
494 kvm_check_and_clear_guest_paused();
495 update_touch_ts();
496 return HRTIMER_RESTART;
497 }
498
499 /* check for a softlockup
500 * This is done by making sure a high priority task is
501 * being scheduled. The task touches the watchdog to
502 * indicate it is getting cpu time. If it hasn't then
503 * this is a good indication some task is hogging the cpu
504 */
505 duration = is_softlockup(touch_ts);
506 if (unlikely(duration)) {
507 /*
508 * If a virtual machine is stopped by the host it can look to
509 * the watchdog like a soft lockup, check to see if the host
510 * stopped the vm before we issue the warning
511 */
512 if (kvm_check_and_clear_guest_paused())
513 return HRTIMER_RESTART;
514
515 /*
516 * Prevent multiple soft-lockup reports if one cpu is already
517 * engaged in dumping all cpu back traces.
518 */
519 if (softlockup_all_cpu_backtrace) {
520 if (test_and_set_bit_lock(0, &soft_lockup_nmi_warn))
521 return HRTIMER_RESTART;
522 }
523
524 /* Start period for the next softlockup warning. */
525 update_touch_ts();
526
527 pr_emerg("BUG: soft lockup - CPU#%d stuck for %us! [%s:%d]\n",
528 smp_processor_id(), duration,
529 current->comm, task_pid_nr(current));
530 print_modules();
531 print_irqtrace_events(current);
532 if (regs)
533 show_regs(regs);
534 else
535 dump_stack();
536
537 if (softlockup_all_cpu_backtrace) {
538 trigger_allbutself_cpu_backtrace();
539 clear_bit_unlock(0, &soft_lockup_nmi_warn);
540 }
541
542 trace_android_vh_watchdog_timer_softlockup(duration, regs, !!softlockup_panic);
543 add_taint(TAINT_SOFTLOCKUP, LOCKDEP_STILL_OK);
544 if (softlockup_panic)
545 panic("softlockup: hung tasks");
546 }
547
548 return HRTIMER_RESTART;
549 }
550
watchdog_enable(unsigned int cpu)551 static void watchdog_enable(unsigned int cpu)
552 {
553 struct hrtimer *hrtimer = this_cpu_ptr(&watchdog_hrtimer);
554 struct completion *done = this_cpu_ptr(&softlockup_completion);
555
556 WARN_ON_ONCE(cpu != smp_processor_id());
557
558 init_completion(done);
559 complete(done);
560
561 /*
562 * Start the timer first to prevent the NMI watchdog triggering
563 * before the timer has a chance to fire.
564 */
565 hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
566 hrtimer->function = watchdog_timer_fn;
567 hrtimer_start(hrtimer, ns_to_ktime(sample_period),
568 HRTIMER_MODE_REL_PINNED_HARD);
569
570 /* Initialize timestamp */
571 update_touch_ts();
572 /* Enable the perf event */
573 if (watchdog_enabled & NMI_WATCHDOG_ENABLED)
574 watchdog_nmi_enable(cpu);
575 else if (IS_ENABLED(CONFIG_HARDLOCKUP_DETECTOR_OTHER_CPU))
576 watchdog_nmi_enable(cpu);
577 }
578
watchdog_disable(unsigned int cpu)579 static void watchdog_disable(unsigned int cpu)
580 {
581 struct hrtimer *hrtimer = this_cpu_ptr(&watchdog_hrtimer);
582
583 WARN_ON_ONCE(cpu != smp_processor_id());
584
585 /*
586 * Disable the perf event first. That prevents that a large delay
587 * between disabling the timer and disabling the perf event causes
588 * the perf NMI to detect a false positive.
589 */
590 watchdog_nmi_disable(cpu);
591 hrtimer_cancel(hrtimer);
592 wait_for_completion(this_cpu_ptr(&softlockup_completion));
593 }
594
softlockup_stop_fn(void * data)595 static int softlockup_stop_fn(void *data)
596 {
597 watchdog_disable(smp_processor_id());
598 return 0;
599 }
600
softlockup_stop_all(void)601 static void softlockup_stop_all(void)
602 {
603 int cpu;
604
605 if (!softlockup_initialized)
606 return;
607
608 for_each_cpu(cpu, &watchdog_allowed_mask)
609 smp_call_on_cpu(cpu, softlockup_stop_fn, NULL, false);
610
611 cpumask_clear(&watchdog_allowed_mask);
612 }
613
softlockup_start_fn(void * data)614 static int softlockup_start_fn(void *data)
615 {
616 watchdog_enable(smp_processor_id());
617 return 0;
618 }
619
softlockup_start_all(void)620 static void softlockup_start_all(void)
621 {
622 int cpu;
623
624 cpumask_copy(&watchdog_allowed_mask, &watchdog_cpumask);
625 for_each_cpu(cpu, &watchdog_allowed_mask)
626 smp_call_on_cpu(cpu, softlockup_start_fn, NULL, false);
627 }
628
lockup_detector_online_cpu(unsigned int cpu)629 int lockup_detector_online_cpu(unsigned int cpu)
630 {
631 if (cpumask_test_cpu(cpu, &watchdog_allowed_mask))
632 watchdog_enable(cpu);
633 return 0;
634 }
635
lockup_detector_offline_cpu(unsigned int cpu)636 int lockup_detector_offline_cpu(unsigned int cpu)
637 {
638 if (cpumask_test_cpu(cpu, &watchdog_allowed_mask))
639 watchdog_disable(cpu);
640 return 0;
641 }
642
__lockup_detector_reconfigure(void)643 static void __lockup_detector_reconfigure(void)
644 {
645 cpus_read_lock();
646 watchdog_nmi_stop();
647
648 softlockup_stop_all();
649 set_sample_period();
650 lockup_detector_update_enable();
651 if (watchdog_enabled && watchdog_thresh)
652 softlockup_start_all();
653
654 watchdog_nmi_start();
655 cpus_read_unlock();
656 /*
657 * Must be called outside the cpus locked section to prevent
658 * recursive locking in the perf code.
659 */
660 __lockup_detector_cleanup();
661 }
662
lockup_detector_reconfigure(void)663 void lockup_detector_reconfigure(void)
664 {
665 mutex_lock(&watchdog_mutex);
666 __lockup_detector_reconfigure();
667 mutex_unlock(&watchdog_mutex);
668 }
669
670 /*
671 * Create the watchdog thread infrastructure and configure the detector(s).
672 *
673 * The threads are not unparked as watchdog_allowed_mask is empty. When
674 * the threads are successfully initialized, take the proper locks and
675 * unpark the threads in the watchdog_cpumask if the watchdog is enabled.
676 */
lockup_detector_setup(void)677 static __init void lockup_detector_setup(void)
678 {
679 /*
680 * If sysctl is off and watchdog got disabled on the command line,
681 * nothing to do here.
682 */
683 lockup_detector_update_enable();
684
685 if (!IS_ENABLED(CONFIG_SYSCTL) &&
686 !(watchdog_enabled && watchdog_thresh))
687 return;
688
689 mutex_lock(&watchdog_mutex);
690 __lockup_detector_reconfigure();
691 softlockup_initialized = true;
692 mutex_unlock(&watchdog_mutex);
693 }
694
695 #else /* CONFIG_SOFTLOCKUP_DETECTOR */
__lockup_detector_reconfigure(void)696 static void __lockup_detector_reconfigure(void)
697 {
698 cpus_read_lock();
699 watchdog_nmi_stop();
700 lockup_detector_update_enable();
701 watchdog_nmi_start();
702 cpus_read_unlock();
703 }
lockup_detector_reconfigure(void)704 void lockup_detector_reconfigure(void)
705 {
706 __lockup_detector_reconfigure();
707 }
lockup_detector_setup(void)708 static inline void lockup_detector_setup(void)
709 {
710 __lockup_detector_reconfigure();
711 }
712 #endif /* !CONFIG_SOFTLOCKUP_DETECTOR */
713
__lockup_detector_cleanup(void)714 static void __lockup_detector_cleanup(void)
715 {
716 lockdep_assert_held(&watchdog_mutex);
717 hardlockup_detector_perf_cleanup();
718 }
719
720 /**
721 * lockup_detector_cleanup - Cleanup after cpu hotplug or sysctl changes
722 *
723 * Caller must not hold the cpu hotplug rwsem.
724 */
lockup_detector_cleanup(void)725 void lockup_detector_cleanup(void)
726 {
727 mutex_lock(&watchdog_mutex);
728 __lockup_detector_cleanup();
729 mutex_unlock(&watchdog_mutex);
730 }
731
732 /**
733 * lockup_detector_soft_poweroff - Interface to stop lockup detector(s)
734 *
735 * Special interface for parisc. It prevents lockup detector warnings from
736 * the default pm_poweroff() function which busy loops forever.
737 */
lockup_detector_soft_poweroff(void)738 void lockup_detector_soft_poweroff(void)
739 {
740 watchdog_enabled = 0;
741 }
742
743 #ifdef CONFIG_SYSCTL
744
745 /* Propagate any changes to the watchdog threads */
proc_watchdog_update(void)746 static void proc_watchdog_update(void)
747 {
748 /* Remove impossible cpus to keep sysctl output clean. */
749 cpumask_and(&watchdog_cpumask, &watchdog_cpumask, cpu_possible_mask);
750 __lockup_detector_reconfigure();
751 }
752
753 /*
754 * common function for watchdog, nmi_watchdog and soft_watchdog parameter
755 *
756 * caller | table->data points to | 'which'
757 * -------------------|----------------------------|--------------------------
758 * proc_watchdog | watchdog_user_enabled | NMI_WATCHDOG_ENABLED |
759 * | | SOFT_WATCHDOG_ENABLED
760 * -------------------|----------------------------|--------------------------
761 * proc_nmi_watchdog | nmi_watchdog_user_enabled | NMI_WATCHDOG_ENABLED
762 * -------------------|----------------------------|--------------------------
763 * proc_soft_watchdog | soft_watchdog_user_enabled | SOFT_WATCHDOG_ENABLED
764 */
proc_watchdog_common(int which,struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)765 static int proc_watchdog_common(int which, struct ctl_table *table, int write,
766 void *buffer, size_t *lenp, loff_t *ppos)
767 {
768 int err, old, *param = table->data;
769
770 mutex_lock(&watchdog_mutex);
771
772 if (!write) {
773 /*
774 * On read synchronize the userspace interface. This is a
775 * racy snapshot.
776 */
777 *param = (watchdog_enabled & which) != 0;
778 err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
779 } else {
780 old = READ_ONCE(*param);
781 err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
782 if (!err && old != READ_ONCE(*param))
783 proc_watchdog_update();
784 }
785 mutex_unlock(&watchdog_mutex);
786 return err;
787 }
788
789 /*
790 * /proc/sys/kernel/watchdog
791 */
proc_watchdog(struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)792 int proc_watchdog(struct ctl_table *table, int write,
793 void *buffer, size_t *lenp, loff_t *ppos)
794 {
795 return proc_watchdog_common(NMI_WATCHDOG_ENABLED|SOFT_WATCHDOG_ENABLED,
796 table, write, buffer, lenp, ppos);
797 }
798
799 /*
800 * /proc/sys/kernel/nmi_watchdog
801 */
proc_nmi_watchdog(struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)802 int proc_nmi_watchdog(struct ctl_table *table, int write,
803 void *buffer, size_t *lenp, loff_t *ppos)
804 {
805 if (!nmi_watchdog_available && write)
806 return -ENOTSUPP;
807 return proc_watchdog_common(NMI_WATCHDOG_ENABLED,
808 table, write, buffer, lenp, ppos);
809 }
810
811 /*
812 * /proc/sys/kernel/soft_watchdog
813 */
proc_soft_watchdog(struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)814 int proc_soft_watchdog(struct ctl_table *table, int write,
815 void *buffer, size_t *lenp, loff_t *ppos)
816 {
817 return proc_watchdog_common(SOFT_WATCHDOG_ENABLED,
818 table, write, buffer, lenp, ppos);
819 }
820
821 /*
822 * /proc/sys/kernel/watchdog_thresh
823 */
proc_watchdog_thresh(struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)824 int proc_watchdog_thresh(struct ctl_table *table, int write,
825 void *buffer, size_t *lenp, loff_t *ppos)
826 {
827 int err, old;
828
829 mutex_lock(&watchdog_mutex);
830
831 old = READ_ONCE(watchdog_thresh);
832 err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
833
834 if (!err && write && old != READ_ONCE(watchdog_thresh))
835 proc_watchdog_update();
836
837 mutex_unlock(&watchdog_mutex);
838 return err;
839 }
840
841 /*
842 * The cpumask is the mask of possible cpus that the watchdog can run
843 * on, not the mask of cpus it is actually running on. This allows the
844 * user to specify a mask that will include cpus that have not yet
845 * been brought online, if desired.
846 */
proc_watchdog_cpumask(struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)847 int proc_watchdog_cpumask(struct ctl_table *table, int write,
848 void *buffer, size_t *lenp, loff_t *ppos)
849 {
850 int err;
851
852 mutex_lock(&watchdog_mutex);
853
854 err = proc_do_large_bitmap(table, write, buffer, lenp, ppos);
855 if (!err && write)
856 proc_watchdog_update();
857
858 mutex_unlock(&watchdog_mutex);
859 return err;
860 }
861 #endif /* CONFIG_SYSCTL */
862
lockup_detector_init(void)863 void __init lockup_detector_init(void)
864 {
865 if (tick_nohz_full_enabled())
866 pr_info("Disabling watchdog on nohz_full cores by default\n");
867
868 cpumask_copy(&watchdog_cpumask,
869 housekeeping_cpumask(HK_FLAG_TIMER));
870
871 if (!watchdog_nmi_probe())
872 nmi_watchdog_available = true;
873 lockup_detector_setup();
874 }
875