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