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 static DEFINE_MUTEX(watchdog_mutex);
31
32 #if defined(CONFIG_HARDLOCKUP_DETECTOR) || defined(CONFIG_HARDLOCKUP_DETECTOR_SPARC64)
33 # define WATCHDOG_HARDLOCKUP_DEFAULT 1
34 #else
35 # define WATCHDOG_HARDLOCKUP_DEFAULT 0
36 #endif
37
38 unsigned long __read_mostly watchdog_enabled;
39 int __read_mostly watchdog_user_enabled = 1;
40 static int __read_mostly watchdog_hardlockup_user_enabled = WATCHDOG_HARDLOCKUP_DEFAULT;
41 static int __read_mostly watchdog_softlockup_user_enabled = 1;
42 int __read_mostly watchdog_thresh = 10;
43 static int __read_mostly watchdog_thresh_next;
44 static int __read_mostly watchdog_hardlockup_available;
45
46 struct cpumask watchdog_cpumask __read_mostly;
47 unsigned long *watchdog_cpumask_bits = cpumask_bits(&watchdog_cpumask);
48
49 #ifdef CONFIG_HARDLOCKUP_DETECTOR
50
51 # ifdef CONFIG_SMP
52 int __read_mostly sysctl_hardlockup_all_cpu_backtrace;
53 # endif /* CONFIG_SMP */
54
55 /*
56 * Should we panic when a soft-lockup or hard-lockup occurs:
57 */
58 unsigned int __read_mostly hardlockup_panic =
59 IS_ENABLED(CONFIG_BOOTPARAM_HARDLOCKUP_PANIC);
60 /*
61 * We may not want to enable hard lockup detection by default in all cases,
62 * for example when running the kernel as a guest on a hypervisor. In these
63 * cases this function can be called to disable hard lockup detection. This
64 * function should only be executed once by the boot processor before the
65 * kernel command line parameters are parsed, because otherwise it is not
66 * possible to override this in hardlockup_panic_setup().
67 */
hardlockup_detector_disable(void)68 void __init hardlockup_detector_disable(void)
69 {
70 watchdog_hardlockup_user_enabled = 0;
71 }
72
hardlockup_panic_setup(char * str)73 static int __init hardlockup_panic_setup(char *str)
74 {
75 if (!strncmp(str, "panic", 5))
76 hardlockup_panic = 1;
77 else if (!strncmp(str, "nopanic", 7))
78 hardlockup_panic = 0;
79 else if (!strncmp(str, "0", 1))
80 watchdog_hardlockup_user_enabled = 0;
81 else if (!strncmp(str, "1", 1))
82 watchdog_hardlockup_user_enabled = 1;
83 return 1;
84 }
85 __setup("nmi_watchdog=", hardlockup_panic_setup);
86
87 #endif /* CONFIG_HARDLOCKUP_DETECTOR */
88
89 #if defined(CONFIG_HARDLOCKUP_DETECTOR_COUNTS_HRTIMER)
90
91 static DEFINE_PER_CPU(atomic_t, hrtimer_interrupts);
92 static DEFINE_PER_CPU(int, hrtimer_interrupts_saved);
93 static DEFINE_PER_CPU(bool, watchdog_hardlockup_warned);
94 static DEFINE_PER_CPU(bool, watchdog_hardlockup_touched);
95 static unsigned long watchdog_hardlockup_all_cpu_dumped;
96
arch_touch_nmi_watchdog(void)97 notrace void arch_touch_nmi_watchdog(void)
98 {
99 /*
100 * Using __raw here because some code paths have
101 * preemption enabled. If preemption is enabled
102 * then interrupts should be enabled too, in which
103 * case we shouldn't have to worry about the watchdog
104 * going off.
105 */
106 raw_cpu_write(watchdog_hardlockup_touched, true);
107 }
108 EXPORT_SYMBOL(arch_touch_nmi_watchdog);
109
watchdog_hardlockup_touch_cpu(unsigned int cpu)110 void watchdog_hardlockup_touch_cpu(unsigned int cpu)
111 {
112 per_cpu(watchdog_hardlockup_touched, cpu) = true;
113 }
114
is_hardlockup(unsigned int cpu)115 static bool is_hardlockup(unsigned int cpu)
116 {
117 int hrint = atomic_read(&per_cpu(hrtimer_interrupts, cpu));
118
119 if (per_cpu(hrtimer_interrupts_saved, cpu) == hrint)
120 return true;
121
122 /*
123 * NOTE: we don't need any fancy atomic_t or READ_ONCE/WRITE_ONCE
124 * for hrtimer_interrupts_saved. hrtimer_interrupts_saved is
125 * written/read by a single CPU.
126 */
127 per_cpu(hrtimer_interrupts_saved, cpu) = hrint;
128
129 return false;
130 }
131
watchdog_hardlockup_kick(void)132 static void watchdog_hardlockup_kick(void)
133 {
134 int new_interrupts;
135
136 new_interrupts = atomic_inc_return(this_cpu_ptr(&hrtimer_interrupts));
137 watchdog_buddy_check_hardlockup(new_interrupts);
138 }
139
watchdog_hardlockup_check(unsigned int cpu,struct pt_regs * regs)140 void watchdog_hardlockup_check(unsigned int cpu, struct pt_regs *regs)
141 {
142 if (per_cpu(watchdog_hardlockup_touched, cpu)) {
143 per_cpu(watchdog_hardlockup_touched, cpu) = false;
144 return;
145 }
146
147 /*
148 * Check for a hardlockup by making sure the CPU's timer
149 * interrupt is incrementing. The timer interrupt should have
150 * fired multiple times before we overflow'd. If it hasn't
151 * then this is a good indication the cpu is stuck
152 */
153 if (is_hardlockup(cpu)) {
154 unsigned int this_cpu = smp_processor_id();
155
156 /* Only print hardlockups once. */
157 if (per_cpu(watchdog_hardlockup_warned, cpu))
158 return;
159
160 pr_emerg("Watchdog detected hard LOCKUP on cpu %d\n", cpu);
161 print_modules();
162 print_irqtrace_events(current);
163 if (cpu == this_cpu) {
164 if (regs)
165 show_regs(regs);
166 else
167 dump_stack();
168 } else {
169 trigger_single_cpu_backtrace(cpu);
170 }
171
172 /*
173 * Perform multi-CPU dump only once to avoid multiple
174 * hardlockups generating interleaving traces
175 */
176 if (sysctl_hardlockup_all_cpu_backtrace &&
177 !test_and_set_bit(0, &watchdog_hardlockup_all_cpu_dumped))
178 trigger_allbutcpu_cpu_backtrace(cpu);
179
180 if (hardlockup_panic)
181 nmi_panic(regs, "Hard LOCKUP");
182
183 per_cpu(watchdog_hardlockup_warned, cpu) = true;
184 } else {
185 per_cpu(watchdog_hardlockup_warned, cpu) = false;
186 }
187 }
188
189 #else /* CONFIG_HARDLOCKUP_DETECTOR_COUNTS_HRTIMER */
190
watchdog_hardlockup_kick(void)191 static inline void watchdog_hardlockup_kick(void) { }
192
193 #endif /* !CONFIG_HARDLOCKUP_DETECTOR_COUNTS_HRTIMER */
194
195 /*
196 * These functions can be overridden based on the configured hardlockdup detector.
197 *
198 * watchdog_hardlockup_enable/disable can be implemented to start and stop when
199 * softlockup watchdog start and stop. The detector must select the
200 * SOFTLOCKUP_DETECTOR Kconfig.
201 */
watchdog_hardlockup_enable(unsigned int cpu)202 void __weak watchdog_hardlockup_enable(unsigned int cpu) { }
203
watchdog_hardlockup_disable(unsigned int cpu)204 void __weak watchdog_hardlockup_disable(unsigned int cpu) { }
205
206 /*
207 * Watchdog-detector specific API.
208 *
209 * Return 0 when hardlockup watchdog is available, negative value otherwise.
210 * Note that the negative value means that a delayed probe might
211 * succeed later.
212 */
watchdog_hardlockup_probe(void)213 int __weak __init watchdog_hardlockup_probe(void)
214 {
215 return -ENODEV;
216 }
217
218 /**
219 * watchdog_hardlockup_stop - Stop the watchdog for reconfiguration
220 *
221 * The reconfiguration steps are:
222 * watchdog_hardlockup_stop();
223 * update_variables();
224 * watchdog_hardlockup_start();
225 */
watchdog_hardlockup_stop(void)226 void __weak watchdog_hardlockup_stop(void) { }
227
228 /**
229 * watchdog_hardlockup_start - Start the watchdog after reconfiguration
230 *
231 * Counterpart to watchdog_hardlockup_stop().
232 *
233 * The following variables have been updated in update_variables() and
234 * contain the currently valid configuration:
235 * - watchdog_enabled
236 * - watchdog_thresh
237 * - watchdog_cpumask
238 */
watchdog_hardlockup_start(void)239 void __weak watchdog_hardlockup_start(void) { }
240
241 /**
242 * lockup_detector_update_enable - Update the sysctl enable bit
243 *
244 * Caller needs to make sure that the hard watchdogs are off, so this
245 * can't race with watchdog_hardlockup_disable().
246 */
lockup_detector_update_enable(void)247 static void lockup_detector_update_enable(void)
248 {
249 watchdog_enabled = 0;
250 if (!watchdog_user_enabled)
251 return;
252 if (watchdog_hardlockup_available && watchdog_hardlockup_user_enabled)
253 watchdog_enabled |= WATCHDOG_HARDLOCKUP_ENABLED;
254 if (watchdog_softlockup_user_enabled)
255 watchdog_enabled |= WATCHDOG_SOFTOCKUP_ENABLED;
256 }
257
258 #ifdef CONFIG_SOFTLOCKUP_DETECTOR
259
260 /*
261 * Delay the soflockup report when running a known slow code.
262 * It does _not_ affect the timestamp of the last successdul reschedule.
263 */
264 #define SOFTLOCKUP_DELAY_REPORT ULONG_MAX
265
266 #ifdef CONFIG_SMP
267 int __read_mostly sysctl_softlockup_all_cpu_backtrace;
268 #endif
269
270 static struct cpumask watchdog_allowed_mask __read_mostly;
271
272 /* Global variables, exported for sysctl */
273 unsigned int __read_mostly softlockup_panic =
274 IS_ENABLED(CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC);
275
276 static bool softlockup_initialized __read_mostly;
277 static u64 __read_mostly sample_period;
278
279 /* Timestamp taken after the last successful reschedule. */
280 static DEFINE_PER_CPU(unsigned long, watchdog_touch_ts);
281 /* Timestamp of the last softlockup report. */
282 static DEFINE_PER_CPU(unsigned long, watchdog_report_ts);
283 static DEFINE_PER_CPU(struct hrtimer, watchdog_hrtimer);
284 static DEFINE_PER_CPU(bool, softlockup_touch_sync);
285 static unsigned long soft_lockup_nmi_warn;
286
softlockup_panic_setup(char * str)287 static int __init softlockup_panic_setup(char *str)
288 {
289 softlockup_panic = simple_strtoul(str, NULL, 0);
290 return 1;
291 }
292 __setup("softlockup_panic=", softlockup_panic_setup);
293
nowatchdog_setup(char * str)294 static int __init nowatchdog_setup(char *str)
295 {
296 watchdog_user_enabled = 0;
297 return 1;
298 }
299 __setup("nowatchdog", nowatchdog_setup);
300
nosoftlockup_setup(char * str)301 static int __init nosoftlockup_setup(char *str)
302 {
303 watchdog_softlockup_user_enabled = 0;
304 return 1;
305 }
306 __setup("nosoftlockup", nosoftlockup_setup);
307
watchdog_thresh_setup(char * str)308 static int __init watchdog_thresh_setup(char *str)
309 {
310 get_option(&str, &watchdog_thresh);
311 return 1;
312 }
313 __setup("watchdog_thresh=", watchdog_thresh_setup);
314
315 static void __lockup_detector_cleanup(void);
316
317 /*
318 * Hard-lockup warnings should be triggered after just a few seconds. Soft-
319 * lockups can have false positives under extreme conditions. So we generally
320 * want a higher threshold for soft lockups than for hard lockups. So we couple
321 * the thresholds with a factor: we make the soft threshold twice the amount of
322 * time the hard threshold is.
323 */
get_softlockup_thresh(void)324 static int get_softlockup_thresh(void)
325 {
326 return watchdog_thresh * 2;
327 }
328
329 /*
330 * Returns seconds, approximately. We don't need nanosecond
331 * resolution, and we don't need to waste time with a big divide when
332 * 2^30ns == 1.074s.
333 */
get_timestamp(void)334 static unsigned long get_timestamp(void)
335 {
336 return running_clock() >> 30LL; /* 2^30 ~= 10^9 */
337 }
338
set_sample_period(void)339 static void set_sample_period(void)
340 {
341 /*
342 * convert watchdog_thresh from seconds to ns
343 * the divide by 5 is to give hrtimer several chances (two
344 * or three with the current relation between the soft
345 * and hard thresholds) to increment before the
346 * hardlockup detector generates a warning
347 */
348 sample_period = get_softlockup_thresh() * ((u64)NSEC_PER_SEC / 5);
349 watchdog_update_hrtimer_threshold(sample_period);
350 }
351
update_report_ts(void)352 static void update_report_ts(void)
353 {
354 __this_cpu_write(watchdog_report_ts, get_timestamp());
355 }
356
357 /* Commands for resetting the watchdog */
update_touch_ts(void)358 static void update_touch_ts(void)
359 {
360 __this_cpu_write(watchdog_touch_ts, get_timestamp());
361 update_report_ts();
362 }
363
364 /**
365 * touch_softlockup_watchdog_sched - touch watchdog on scheduler stalls
366 *
367 * Call when the scheduler may have stalled for legitimate reasons
368 * preventing the watchdog task from executing - e.g. the scheduler
369 * entering idle state. This should only be used for scheduler events.
370 * Use touch_softlockup_watchdog() for everything else.
371 */
touch_softlockup_watchdog_sched(void)372 notrace void touch_softlockup_watchdog_sched(void)
373 {
374 /*
375 * Preemption can be enabled. It doesn't matter which CPU's watchdog
376 * report period gets restarted here, so use the raw_ operation.
377 */
378 raw_cpu_write(watchdog_report_ts, SOFTLOCKUP_DELAY_REPORT);
379 }
380
touch_softlockup_watchdog(void)381 notrace void touch_softlockup_watchdog(void)
382 {
383 touch_softlockup_watchdog_sched();
384 wq_watchdog_touch(raw_smp_processor_id());
385 }
386 EXPORT_SYMBOL(touch_softlockup_watchdog);
387
touch_all_softlockup_watchdogs(void)388 void touch_all_softlockup_watchdogs(void)
389 {
390 int cpu;
391
392 /*
393 * watchdog_mutex cannpt be taken here, as this might be called
394 * from (soft)interrupt context, so the access to
395 * watchdog_allowed_cpumask might race with a concurrent update.
396 *
397 * The watchdog time stamp can race against a concurrent real
398 * update as well, the only side effect might be a cycle delay for
399 * the softlockup check.
400 */
401 for_each_cpu(cpu, &watchdog_allowed_mask) {
402 per_cpu(watchdog_report_ts, cpu) = SOFTLOCKUP_DELAY_REPORT;
403 wq_watchdog_touch(cpu);
404 }
405 }
406
touch_softlockup_watchdog_sync(void)407 void touch_softlockup_watchdog_sync(void)
408 {
409 __this_cpu_write(softlockup_touch_sync, true);
410 __this_cpu_write(watchdog_report_ts, SOFTLOCKUP_DELAY_REPORT);
411 }
412
is_softlockup(unsigned long touch_ts,unsigned long period_ts,unsigned long now)413 static int is_softlockup(unsigned long touch_ts,
414 unsigned long period_ts,
415 unsigned long now)
416 {
417 if ((watchdog_enabled & WATCHDOG_SOFTOCKUP_ENABLED) && watchdog_thresh) {
418 /* Warn about unreasonable delays. */
419 if (time_after(now, period_ts + get_softlockup_thresh()))
420 return now - touch_ts;
421 }
422 return 0;
423 }
424
425 /* watchdog detector functions */
426 static DEFINE_PER_CPU(struct completion, softlockup_completion);
427 static DEFINE_PER_CPU(struct cpu_stop_work, softlockup_stop_work);
428
429 /*
430 * The watchdog feed function - touches the timestamp.
431 *
432 * It only runs once every sample_period seconds (4 seconds by
433 * default) to reset the softlockup timestamp. If this gets delayed
434 * for more than 2*watchdog_thresh seconds then the debug-printout
435 * triggers in watchdog_timer_fn().
436 */
softlockup_fn(void * data)437 static int softlockup_fn(void *data)
438 {
439 update_touch_ts();
440 complete(this_cpu_ptr(&softlockup_completion));
441
442 return 0;
443 }
444
445 /* watchdog kicker functions */
watchdog_timer_fn(struct hrtimer * hrtimer)446 static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer)
447 {
448 unsigned long touch_ts, period_ts, now;
449 struct pt_regs *regs = get_irq_regs();
450 int duration;
451 int softlockup_all_cpu_backtrace = sysctl_softlockup_all_cpu_backtrace;
452
453 if (!watchdog_enabled)
454 return HRTIMER_NORESTART;
455
456 watchdog_hardlockup_kick();
457
458 /* kick the softlockup detector */
459 if (completion_done(this_cpu_ptr(&softlockup_completion))) {
460 reinit_completion(this_cpu_ptr(&softlockup_completion));
461 stop_one_cpu_nowait(smp_processor_id(),
462 softlockup_fn, NULL,
463 this_cpu_ptr(&softlockup_stop_work));
464 }
465
466 /* .. and repeat */
467 hrtimer_forward_now(hrtimer, ns_to_ktime(sample_period));
468
469 /*
470 * Read the current timestamp first. It might become invalid anytime
471 * when a virtual machine is stopped by the host or when the watchog
472 * is touched from NMI.
473 */
474 now = get_timestamp();
475 /*
476 * If a virtual machine is stopped by the host it can look to
477 * the watchdog like a soft lockup. This function touches the watchdog.
478 */
479 kvm_check_and_clear_guest_paused();
480 /*
481 * The stored timestamp is comparable with @now only when not touched.
482 * It might get touched anytime from NMI. Make sure that is_softlockup()
483 * uses the same (valid) value.
484 */
485 period_ts = READ_ONCE(*this_cpu_ptr(&watchdog_report_ts));
486
487 /* Reset the interval when touched by known problematic code. */
488 if (period_ts == SOFTLOCKUP_DELAY_REPORT) {
489 if (unlikely(__this_cpu_read(softlockup_touch_sync))) {
490 /*
491 * If the time stamp was touched atomically
492 * make sure the scheduler tick is up to date.
493 */
494 __this_cpu_write(softlockup_touch_sync, false);
495 sched_clock_tick();
496 }
497
498 update_report_ts();
499 return HRTIMER_RESTART;
500 }
501
502 /* Check for a softlockup. */
503 touch_ts = __this_cpu_read(watchdog_touch_ts);
504 duration = is_softlockup(touch_ts, period_ts, now);
505 if (unlikely(duration)) {
506 /*
507 * Prevent multiple soft-lockup reports if one cpu is already
508 * engaged in dumping all cpu back traces.
509 */
510 if (softlockup_all_cpu_backtrace) {
511 if (test_and_set_bit_lock(0, &soft_lockup_nmi_warn))
512 return HRTIMER_RESTART;
513 }
514
515 /* Start period for the next softlockup warning. */
516 update_report_ts();
517
518 pr_emerg("BUG: soft lockup - CPU#%d stuck for %us! [%s:%d]\n",
519 smp_processor_id(), duration,
520 current->comm, task_pid_nr(current));
521 print_modules();
522 print_irqtrace_events(current);
523 if (regs)
524 show_regs(regs);
525 else
526 dump_stack();
527
528 if (softlockup_all_cpu_backtrace) {
529 trigger_allbutcpu_cpu_backtrace(smp_processor_id());
530 clear_bit_unlock(0, &soft_lockup_nmi_warn);
531 }
532
533 add_taint(TAINT_SOFTLOCKUP, LOCKDEP_STILL_OK);
534 if (softlockup_panic)
535 panic("softlockup: hung tasks");
536 }
537
538 return HRTIMER_RESTART;
539 }
540
watchdog_enable(unsigned int cpu)541 static void watchdog_enable(unsigned int cpu)
542 {
543 struct hrtimer *hrtimer = this_cpu_ptr(&watchdog_hrtimer);
544 struct completion *done = this_cpu_ptr(&softlockup_completion);
545
546 WARN_ON_ONCE(cpu != smp_processor_id());
547
548 init_completion(done);
549 complete(done);
550
551 /*
552 * Start the timer first to prevent the hardlockup watchdog triggering
553 * before the timer has a chance to fire.
554 */
555 hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
556 hrtimer->function = watchdog_timer_fn;
557 hrtimer_start(hrtimer, ns_to_ktime(sample_period),
558 HRTIMER_MODE_REL_PINNED_HARD);
559
560 /* Initialize timestamp */
561 update_touch_ts();
562 /* Enable the hardlockup detector */
563 if (watchdog_enabled & WATCHDOG_HARDLOCKUP_ENABLED)
564 watchdog_hardlockup_enable(cpu);
565 }
566
watchdog_disable(unsigned int cpu)567 static void watchdog_disable(unsigned int cpu)
568 {
569 struct hrtimer *hrtimer = this_cpu_ptr(&watchdog_hrtimer);
570
571 WARN_ON_ONCE(cpu != smp_processor_id());
572
573 /*
574 * Disable the hardlockup detector first. That prevents that a large
575 * delay between disabling the timer and disabling the hardlockup
576 * detector causes a false positive.
577 */
578 watchdog_hardlockup_disable(cpu);
579 hrtimer_cancel(hrtimer);
580 wait_for_completion(this_cpu_ptr(&softlockup_completion));
581 }
582
softlockup_stop_fn(void * data)583 static int softlockup_stop_fn(void *data)
584 {
585 watchdog_disable(smp_processor_id());
586 return 0;
587 }
588
softlockup_stop_all(void)589 static void softlockup_stop_all(void)
590 {
591 int cpu;
592
593 if (!softlockup_initialized)
594 return;
595
596 for_each_cpu(cpu, &watchdog_allowed_mask)
597 smp_call_on_cpu(cpu, softlockup_stop_fn, NULL, false);
598
599 cpumask_clear(&watchdog_allowed_mask);
600 }
601
softlockup_start_fn(void * data)602 static int softlockup_start_fn(void *data)
603 {
604 watchdog_enable(smp_processor_id());
605 return 0;
606 }
607
softlockup_start_all(void)608 static void softlockup_start_all(void)
609 {
610 int cpu;
611
612 cpumask_copy(&watchdog_allowed_mask, &watchdog_cpumask);
613 for_each_cpu(cpu, &watchdog_allowed_mask)
614 smp_call_on_cpu(cpu, softlockup_start_fn, NULL, false);
615 }
616
lockup_detector_online_cpu(unsigned int cpu)617 int lockup_detector_online_cpu(unsigned int cpu)
618 {
619 if (cpumask_test_cpu(cpu, &watchdog_allowed_mask))
620 watchdog_enable(cpu);
621 return 0;
622 }
623
lockup_detector_offline_cpu(unsigned int cpu)624 int lockup_detector_offline_cpu(unsigned int cpu)
625 {
626 if (cpumask_test_cpu(cpu, &watchdog_allowed_mask))
627 watchdog_disable(cpu);
628 return 0;
629 }
630
__lockup_detector_reconfigure(bool thresh_changed)631 static void __lockup_detector_reconfigure(bool thresh_changed)
632 {
633 cpus_read_lock();
634 watchdog_hardlockup_stop();
635
636 softlockup_stop_all();
637 /*
638 * To prevent watchdog_timer_fn from using the old interval and
639 * the new watchdog_thresh at the same time, which could lead to
640 * false softlockup reports, it is necessary to update the
641 * watchdog_thresh after the softlockup is completed.
642 */
643 if (thresh_changed)
644 watchdog_thresh = READ_ONCE(watchdog_thresh_next);
645 set_sample_period();
646 lockup_detector_update_enable();
647 if (watchdog_enabled && watchdog_thresh)
648 softlockup_start_all();
649
650 watchdog_hardlockup_start();
651 cpus_read_unlock();
652 /*
653 * Must be called outside the cpus locked section to prevent
654 * recursive locking in the perf code.
655 */
656 __lockup_detector_cleanup();
657 }
658
lockup_detector_reconfigure(void)659 void lockup_detector_reconfigure(void)
660 {
661 mutex_lock(&watchdog_mutex);
662 __lockup_detector_reconfigure(false);
663 mutex_unlock(&watchdog_mutex);
664 }
665
666 /*
667 * Create the watchdog infrastructure and configure the detector(s).
668 */
lockup_detector_setup(void)669 static __init void lockup_detector_setup(void)
670 {
671 /*
672 * If sysctl is off and watchdog got disabled on the command line,
673 * nothing to do here.
674 */
675 lockup_detector_update_enable();
676
677 if (!IS_ENABLED(CONFIG_SYSCTL) &&
678 !(watchdog_enabled && watchdog_thresh))
679 return;
680
681 mutex_lock(&watchdog_mutex);
682 __lockup_detector_reconfigure(false);
683 softlockup_initialized = true;
684 mutex_unlock(&watchdog_mutex);
685 }
686
687 #else /* CONFIG_SOFTLOCKUP_DETECTOR */
__lockup_detector_reconfigure(bool thresh_changed)688 static void __lockup_detector_reconfigure(bool thresh_changed)
689 {
690 cpus_read_lock();
691 watchdog_hardlockup_stop();
692 if (thresh_changed)
693 watchdog_thresh = READ_ONCE(watchdog_thresh_next);
694 lockup_detector_update_enable();
695 watchdog_hardlockup_start();
696 cpus_read_unlock();
697 }
lockup_detector_reconfigure(void)698 void lockup_detector_reconfigure(void)
699 {
700 __lockup_detector_reconfigure(false);
701 }
lockup_detector_setup(void)702 static inline void lockup_detector_setup(void)
703 {
704 __lockup_detector_reconfigure(false);
705 }
706 #endif /* !CONFIG_SOFTLOCKUP_DETECTOR */
707
__lockup_detector_cleanup(void)708 static void __lockup_detector_cleanup(void)
709 {
710 lockdep_assert_held(&watchdog_mutex);
711 hardlockup_detector_perf_cleanup();
712 }
713
714 /**
715 * lockup_detector_cleanup - Cleanup after cpu hotplug or sysctl changes
716 *
717 * Caller must not hold the cpu hotplug rwsem.
718 */
lockup_detector_cleanup(void)719 void lockup_detector_cleanup(void)
720 {
721 mutex_lock(&watchdog_mutex);
722 __lockup_detector_cleanup();
723 mutex_unlock(&watchdog_mutex);
724 }
725
726 /**
727 * lockup_detector_soft_poweroff - Interface to stop lockup detector(s)
728 *
729 * Special interface for parisc. It prevents lockup detector warnings from
730 * the default pm_poweroff() function which busy loops forever.
731 */
lockup_detector_soft_poweroff(void)732 void lockup_detector_soft_poweroff(void)
733 {
734 watchdog_enabled = 0;
735 }
736
737 #ifdef CONFIG_SYSCTL
738
739 /* Propagate any changes to the watchdog infrastructure */
proc_watchdog_update(bool thresh_changed)740 static void proc_watchdog_update(bool thresh_changed)
741 {
742 /* Remove impossible cpus to keep sysctl output clean. */
743 cpumask_and(&watchdog_cpumask, &watchdog_cpumask, cpu_possible_mask);
744 __lockup_detector_reconfigure(thresh_changed);
745 }
746
747 /*
748 * common function for watchdog, nmi_watchdog and soft_watchdog parameter
749 *
750 * caller | table->data points to | 'which'
751 * -------------------|----------------------------------|-------------------------------
752 * proc_watchdog | watchdog_user_enabled | WATCHDOG_HARDLOCKUP_ENABLED |
753 * | | WATCHDOG_SOFTOCKUP_ENABLED
754 * -------------------|----------------------------------|-------------------------------
755 * proc_nmi_watchdog | watchdog_hardlockup_user_enabled | WATCHDOG_HARDLOCKUP_ENABLED
756 * -------------------|----------------------------------|-------------------------------
757 * proc_soft_watchdog | watchdog_softlockup_user_enabled | WATCHDOG_SOFTOCKUP_ENABLED
758 */
proc_watchdog_common(int which,struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)759 static int proc_watchdog_common(int which, struct ctl_table *table, int write,
760 void *buffer, size_t *lenp, loff_t *ppos)
761 {
762 int err, old, *param = table->data;
763
764 mutex_lock(&watchdog_mutex);
765
766 if (!write) {
767 /*
768 * On read synchronize the userspace interface. This is a
769 * racy snapshot.
770 */
771 *param = (watchdog_enabled & which) != 0;
772 err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
773 } else {
774 old = READ_ONCE(*param);
775 err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
776 if (!err && old != READ_ONCE(*param))
777 proc_watchdog_update(false);
778 }
779 mutex_unlock(&watchdog_mutex);
780 return err;
781 }
782
783 /*
784 * /proc/sys/kernel/watchdog
785 */
proc_watchdog(struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)786 int proc_watchdog(struct ctl_table *table, int write,
787 void *buffer, size_t *lenp, loff_t *ppos)
788 {
789 return proc_watchdog_common(WATCHDOG_HARDLOCKUP_ENABLED |
790 WATCHDOG_SOFTOCKUP_ENABLED,
791 table, write, buffer, lenp, ppos);
792 }
793
794 /*
795 * /proc/sys/kernel/nmi_watchdog
796 */
proc_nmi_watchdog(struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)797 int proc_nmi_watchdog(struct ctl_table *table, int write,
798 void *buffer, size_t *lenp, loff_t *ppos)
799 {
800 if (!watchdog_hardlockup_available && write)
801 return -ENOTSUPP;
802 return proc_watchdog_common(WATCHDOG_HARDLOCKUP_ENABLED,
803 table, write, buffer, lenp, ppos);
804 }
805
806 /*
807 * /proc/sys/kernel/soft_watchdog
808 */
proc_soft_watchdog(struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)809 int proc_soft_watchdog(struct ctl_table *table, int write,
810 void *buffer, size_t *lenp, loff_t *ppos)
811 {
812 return proc_watchdog_common(WATCHDOG_SOFTOCKUP_ENABLED,
813 table, write, buffer, lenp, ppos);
814 }
815
816 /*
817 * /proc/sys/kernel/watchdog_thresh
818 */
proc_watchdog_thresh(struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)819 int proc_watchdog_thresh(struct ctl_table *table, int write,
820 void *buffer, size_t *lenp, loff_t *ppos)
821 {
822 int err, old;
823
824 mutex_lock(&watchdog_mutex);
825
826 watchdog_thresh_next = READ_ONCE(watchdog_thresh);
827
828 old = watchdog_thresh_next;
829 err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
830
831 if (!err && write && old != READ_ONCE(watchdog_thresh_next))
832 proc_watchdog_update(true);
833
834 mutex_unlock(&watchdog_mutex);
835 return err;
836 }
837
838 /*
839 * The cpumask is the mask of possible cpus that the watchdog can run
840 * on, not the mask of cpus it is actually running on. This allows the
841 * user to specify a mask that will include cpus that have not yet
842 * been brought online, if desired.
843 */
proc_watchdog_cpumask(struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)844 int proc_watchdog_cpumask(struct ctl_table *table, int write,
845 void *buffer, size_t *lenp, loff_t *ppos)
846 {
847 int err;
848
849 mutex_lock(&watchdog_mutex);
850
851 err = proc_do_large_bitmap(table, write, buffer, lenp, ppos);
852 if (!err && write)
853 proc_watchdog_update(false);
854
855 mutex_unlock(&watchdog_mutex);
856 return err;
857 }
858
859 static const int sixty = 60;
860
861 static struct ctl_table watchdog_sysctls[] = {
862 {
863 .procname = "watchdog",
864 .data = &watchdog_user_enabled,
865 .maxlen = sizeof(int),
866 .mode = 0644,
867 .proc_handler = proc_watchdog,
868 .extra1 = SYSCTL_ZERO,
869 .extra2 = SYSCTL_ONE,
870 },
871 {
872 .procname = "watchdog_thresh",
873 .data = &watchdog_thresh_next,
874 .maxlen = sizeof(int),
875 .mode = 0644,
876 .proc_handler = proc_watchdog_thresh,
877 .extra1 = SYSCTL_ZERO,
878 .extra2 = (void *)&sixty,
879 },
880 {
881 .procname = "watchdog_cpumask",
882 .data = &watchdog_cpumask_bits,
883 .maxlen = NR_CPUS,
884 .mode = 0644,
885 .proc_handler = proc_watchdog_cpumask,
886 },
887 #ifdef CONFIG_SOFTLOCKUP_DETECTOR
888 {
889 .procname = "soft_watchdog",
890 .data = &watchdog_softlockup_user_enabled,
891 .maxlen = sizeof(int),
892 .mode = 0644,
893 .proc_handler = proc_soft_watchdog,
894 .extra1 = SYSCTL_ZERO,
895 .extra2 = SYSCTL_ONE,
896 },
897 {
898 .procname = "softlockup_panic",
899 .data = &softlockup_panic,
900 .maxlen = sizeof(int),
901 .mode = 0644,
902 .proc_handler = proc_dointvec_minmax,
903 .extra1 = SYSCTL_ZERO,
904 .extra2 = SYSCTL_ONE,
905 },
906 #ifdef CONFIG_SMP
907 {
908 .procname = "softlockup_all_cpu_backtrace",
909 .data = &sysctl_softlockup_all_cpu_backtrace,
910 .maxlen = sizeof(int),
911 .mode = 0644,
912 .proc_handler = proc_dointvec_minmax,
913 .extra1 = SYSCTL_ZERO,
914 .extra2 = SYSCTL_ONE,
915 },
916 #endif /* CONFIG_SMP */
917 #endif
918 #ifdef CONFIG_HARDLOCKUP_DETECTOR
919 {
920 .procname = "hardlockup_panic",
921 .data = &hardlockup_panic,
922 .maxlen = sizeof(int),
923 .mode = 0644,
924 .proc_handler = proc_dointvec_minmax,
925 .extra1 = SYSCTL_ZERO,
926 .extra2 = SYSCTL_ONE,
927 },
928 #ifdef CONFIG_SMP
929 {
930 .procname = "hardlockup_all_cpu_backtrace",
931 .data = &sysctl_hardlockup_all_cpu_backtrace,
932 .maxlen = sizeof(int),
933 .mode = 0644,
934 .proc_handler = proc_dointvec_minmax,
935 .extra1 = SYSCTL_ZERO,
936 .extra2 = SYSCTL_ONE,
937 },
938 #endif /* CONFIG_SMP */
939 #endif
940 {}
941 };
942
943 static struct ctl_table watchdog_hardlockup_sysctl[] = {
944 {
945 .procname = "nmi_watchdog",
946 .data = &watchdog_hardlockup_user_enabled,
947 .maxlen = sizeof(int),
948 .mode = 0444,
949 .proc_handler = proc_nmi_watchdog,
950 .extra1 = SYSCTL_ZERO,
951 .extra2 = SYSCTL_ONE,
952 },
953 {}
954 };
955
watchdog_sysctl_init(void)956 static void __init watchdog_sysctl_init(void)
957 {
958 register_sysctl_init("kernel", watchdog_sysctls);
959
960 if (watchdog_hardlockup_available)
961 watchdog_hardlockup_sysctl[0].mode = 0644;
962 register_sysctl_init("kernel", watchdog_hardlockup_sysctl);
963 }
964
965 #else
966 #define watchdog_sysctl_init() do { } while (0)
967 #endif /* CONFIG_SYSCTL */
968
969 static void __init lockup_detector_delay_init(struct work_struct *work);
970 static bool allow_lockup_detector_init_retry __initdata;
971
972 static struct work_struct detector_work __initdata =
973 __WORK_INITIALIZER(detector_work, lockup_detector_delay_init);
974
lockup_detector_delay_init(struct work_struct * work)975 static void __init lockup_detector_delay_init(struct work_struct *work)
976 {
977 int ret;
978
979 ret = watchdog_hardlockup_probe();
980 if (ret) {
981 pr_info("Delayed init of the lockup detector failed: %d\n", ret);
982 pr_info("Hard watchdog permanently disabled\n");
983 return;
984 }
985
986 allow_lockup_detector_init_retry = false;
987
988 watchdog_hardlockup_available = true;
989 lockup_detector_setup();
990 }
991
992 /*
993 * lockup_detector_retry_init - retry init lockup detector if possible.
994 *
995 * Retry hardlockup detector init. It is useful when it requires some
996 * functionality that has to be initialized later on a particular
997 * platform.
998 */
lockup_detector_retry_init(void)999 void __init lockup_detector_retry_init(void)
1000 {
1001 /* Must be called before late init calls */
1002 if (!allow_lockup_detector_init_retry)
1003 return;
1004
1005 schedule_work(&detector_work);
1006 }
1007
1008 /*
1009 * Ensure that optional delayed hardlockup init is proceed before
1010 * the init code and memory is freed.
1011 */
lockup_detector_check(void)1012 static int __init lockup_detector_check(void)
1013 {
1014 /* Prevent any later retry. */
1015 allow_lockup_detector_init_retry = false;
1016
1017 /* Make sure no work is pending. */
1018 flush_work(&detector_work);
1019
1020 watchdog_sysctl_init();
1021
1022 return 0;
1023
1024 }
1025 late_initcall_sync(lockup_detector_check);
1026
lockup_detector_init(void)1027 void __init lockup_detector_init(void)
1028 {
1029 if (tick_nohz_full_enabled())
1030 pr_info("Disabling watchdog on nohz_full cores by default\n");
1031
1032 cpumask_copy(&watchdog_cpumask,
1033 housekeeping_cpumask(HK_TYPE_TIMER));
1034
1035 if (!watchdog_hardlockup_probe())
1036 watchdog_hardlockup_available = true;
1037 else
1038 allow_lockup_detector_init_retry = true;
1039
1040 lockup_detector_setup();
1041 }
1042