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