xref: /openbmc/linux/kernel/watchdog.c (revision 146c9d0e)
158687acbSDon Zickus /*
258687acbSDon Zickus  * Detect hard and soft lockups on a system
358687acbSDon Zickus  *
458687acbSDon Zickus  * started by Don Zickus, Copyright (C) 2010 Red Hat, Inc.
558687acbSDon Zickus  *
686f5e6a7SFernando Luis Vázquez Cao  * Note: Most of this code is borrowed heavily from the original softlockup
786f5e6a7SFernando Luis Vázquez Cao  * detector, so thanks to Ingo for the initial implementation.
886f5e6a7SFernando Luis Vázquez Cao  * Some chunks also taken from the old x86-specific nmi watchdog code, thanks
958687acbSDon Zickus  * to those contributors as well.
1058687acbSDon Zickus  */
1158687acbSDon Zickus 
125f92a7b0SKefeng Wang #define pr_fmt(fmt) "watchdog: " fmt
134501980aSAndrew Morton 
1458687acbSDon Zickus #include <linux/mm.h>
1558687acbSDon Zickus #include <linux/cpu.h>
1658687acbSDon Zickus #include <linux/nmi.h>
1758687acbSDon Zickus #include <linux/init.h>
1858687acbSDon Zickus #include <linux/module.h>
1958687acbSDon Zickus #include <linux/sysctl.h>
20bcd951cfSThomas Gleixner #include <linux/smpboot.h>
218bd75c77SClark Williams #include <linux/sched/rt.h>
22ae7e81c0SIngo Molnar #include <uapi/linux/sched/types.h>
23fe4ba3c3SChris Metcalf #include <linux/tick.h>
2482607adcSTejun Heo #include <linux/workqueue.h>
25e6017571SIngo Molnar #include <linux/sched/clock.h>
26b17b0153SIngo Molnar #include <linux/sched/debug.h>
2758687acbSDon Zickus 
2858687acbSDon Zickus #include <asm/irq_regs.h>
295d1c0f4aSEric B Munson #include <linux/kvm_para.h>
3081a4beefSUlrich Obergfell #include <linux/kthread.h>
3158687acbSDon Zickus 
32946d1977SThomas Gleixner static DEFINE_MUTEX(watchdog_mutex);
33ab992dc3SPeter Zijlstra 
3405a4a952SNicholas Piggin #if defined(CONFIG_HARDLOCKUP_DETECTOR) || defined(CONFIG_HAVE_NMI_WATCHDOG)
3509154985SThomas Gleixner # define WATCHDOG_DEFAULT	(SOFT_WATCHDOG_ENABLED | NMI_WATCHDOG_ENABLED)
3609154985SThomas Gleixner # define NMI_WATCHDOG_DEFAULT	1
3784d56e66SUlrich Obergfell #else
3809154985SThomas Gleixner # define WATCHDOG_DEFAULT	(SOFT_WATCHDOG_ENABLED)
3909154985SThomas Gleixner # define NMI_WATCHDOG_DEFAULT	0
4084d56e66SUlrich Obergfell #endif
4105a4a952SNicholas Piggin 
4209154985SThomas Gleixner unsigned long __read_mostly watchdog_enabled;
4309154985SThomas Gleixner int __read_mostly watchdog_user_enabled = 1;
4409154985SThomas Gleixner int __read_mostly nmi_watchdog_user_enabled = NMI_WATCHDOG_DEFAULT;
4509154985SThomas Gleixner int __read_mostly soft_watchdog_user_enabled = 1;
467feeb9cdSThomas Gleixner int __read_mostly watchdog_thresh = 10;
47a994a314SThomas Gleixner int __read_mostly nmi_watchdog_available;
487feeb9cdSThomas Gleixner 
497feeb9cdSThomas Gleixner struct cpumask watchdog_allowed_mask __read_mostly;
507feeb9cdSThomas Gleixner static bool softlockup_threads_initialized __read_mostly;
517feeb9cdSThomas Gleixner 
527feeb9cdSThomas Gleixner struct cpumask watchdog_cpumask __read_mostly;
537feeb9cdSThomas Gleixner unsigned long *watchdog_cpumask_bits = cpumask_bits(&watchdog_cpumask);
547feeb9cdSThomas Gleixner 
5505a4a952SNicholas Piggin #ifdef CONFIG_HARDLOCKUP_DETECTOR
5605a4a952SNicholas Piggin /*
5705a4a952SNicholas Piggin  * Should we panic when a soft-lockup or hard-lockup occurs:
5805a4a952SNicholas Piggin  */
5905a4a952SNicholas Piggin unsigned int __read_mostly hardlockup_panic =
6005a4a952SNicholas Piggin 			CONFIG_BOOTPARAM_HARDLOCKUP_PANIC_VALUE;
6105a4a952SNicholas Piggin /*
6205a4a952SNicholas Piggin  * We may not want to enable hard lockup detection by default in all cases,
6305a4a952SNicholas Piggin  * for example when running the kernel as a guest on a hypervisor. In these
6405a4a952SNicholas Piggin  * cases this function can be called to disable hard lockup detection. This
6505a4a952SNicholas Piggin  * function should only be executed once by the boot processor before the
6605a4a952SNicholas Piggin  * kernel command line parameters are parsed, because otherwise it is not
6705a4a952SNicholas Piggin  * possible to override this in hardlockup_panic_setup().
6805a4a952SNicholas Piggin  */
697a355820SThomas Gleixner void __init hardlockup_detector_disable(void)
7005a4a952SNicholas Piggin {
7109154985SThomas Gleixner 	nmi_watchdog_user_enabled = 0;
7205a4a952SNicholas Piggin }
7305a4a952SNicholas Piggin 
7405a4a952SNicholas Piggin static int __init hardlockup_panic_setup(char *str)
7505a4a952SNicholas Piggin {
7605a4a952SNicholas Piggin 	if (!strncmp(str, "panic", 5))
7705a4a952SNicholas Piggin 		hardlockup_panic = 1;
7805a4a952SNicholas Piggin 	else if (!strncmp(str, "nopanic", 7))
7905a4a952SNicholas Piggin 		hardlockup_panic = 0;
8005a4a952SNicholas Piggin 	else if (!strncmp(str, "0", 1))
8109154985SThomas Gleixner 		nmi_watchdog_user_enabled = 0;
8205a4a952SNicholas Piggin 	else if (!strncmp(str, "1", 1))
8309154985SThomas Gleixner 		nmi_watchdog_user_enabled = 1;
8405a4a952SNicholas Piggin 	return 1;
8505a4a952SNicholas Piggin }
8605a4a952SNicholas Piggin __setup("nmi_watchdog=", hardlockup_panic_setup);
8705a4a952SNicholas Piggin 
88368a7e2cSThomas Gleixner # ifdef CONFIG_SMP
89368a7e2cSThomas Gleixner int __read_mostly sysctl_hardlockup_all_cpu_backtrace;
9005a4a952SNicholas Piggin 
91368a7e2cSThomas Gleixner static int __init hardlockup_all_cpu_backtrace_setup(char *str)
92368a7e2cSThomas Gleixner {
93368a7e2cSThomas Gleixner 	sysctl_hardlockup_all_cpu_backtrace = !!simple_strtol(str, NULL, 0);
94368a7e2cSThomas Gleixner 	return 1;
95368a7e2cSThomas Gleixner }
96368a7e2cSThomas Gleixner __setup("hardlockup_all_cpu_backtrace=", hardlockup_all_cpu_backtrace_setup);
97368a7e2cSThomas Gleixner # endif /* CONFIG_SMP */
98368a7e2cSThomas Gleixner #endif /* CONFIG_HARDLOCKUP_DETECTOR */
9905a4a952SNicholas Piggin 
100ec6a9066SUlrich Obergfell /*
10105a4a952SNicholas Piggin  * These functions can be overridden if an architecture implements its
10205a4a952SNicholas Piggin  * own hardlockup detector.
103a10a842fSNicholas Piggin  *
104a10a842fSNicholas Piggin  * watchdog_nmi_enable/disable can be implemented to start and stop when
105a10a842fSNicholas Piggin  * softlockup watchdog threads start and stop. The arch must select the
106a10a842fSNicholas Piggin  * SOFTLOCKUP_DETECTOR Kconfig.
10705a4a952SNicholas Piggin  */
10805a4a952SNicholas Piggin int __weak watchdog_nmi_enable(unsigned int cpu)
10905a4a952SNicholas Piggin {
110146c9d0eSThomas Gleixner 	hardlockup_detector_perf_enable();
11105a4a952SNicholas Piggin 	return 0;
11205a4a952SNicholas Piggin }
113941154bdSThomas Gleixner 
11405a4a952SNicholas Piggin void __weak watchdog_nmi_disable(unsigned int cpu)
11505a4a952SNicholas Piggin {
116941154bdSThomas Gleixner 	hardlockup_detector_perf_disable();
11705a4a952SNicholas Piggin }
11805a4a952SNicholas Piggin 
119a994a314SThomas Gleixner /* Return 0, if a NMI watchdog is available. Error code otherwise */
120a994a314SThomas Gleixner int __weak __init watchdog_nmi_probe(void)
121a994a314SThomas Gleixner {
122a994a314SThomas Gleixner 	return hardlockup_detector_perf_init();
123a994a314SThomas Gleixner }
124a994a314SThomas Gleixner 
1256592ad2fSThomas Gleixner /**
1266592ad2fSThomas Gleixner  * watchdog_nmi_reconfigure - Optional function to reconfigure NMI watchdogs
1276592ad2fSThomas Gleixner  * @run:	If false stop the watchdogs on all enabled CPUs
1286592ad2fSThomas Gleixner  *		If true start the watchdogs on all enabled CPUs
1296592ad2fSThomas Gleixner  *
1306592ad2fSThomas Gleixner  * The core call order is:
1316592ad2fSThomas Gleixner  * watchdog_nmi_reconfigure(false);
1326592ad2fSThomas Gleixner  * update_variables();
1336592ad2fSThomas Gleixner  * watchdog_nmi_reconfigure(true);
1346592ad2fSThomas Gleixner  *
1356592ad2fSThomas Gleixner  * The second call which starts the watchdogs again guarantees that the
1366592ad2fSThomas Gleixner  * following variables are stable across the call.
1377feeb9cdSThomas Gleixner  * - watchdog_enabled
138a10a842fSNicholas Piggin  * - watchdog_thresh
139a10a842fSNicholas Piggin  * - watchdog_cpumask
1406592ad2fSThomas Gleixner  *
1416592ad2fSThomas Gleixner  * After the call the variables can be changed again.
142a10a842fSNicholas Piggin  */
1436592ad2fSThomas Gleixner void __weak watchdog_nmi_reconfigure(bool run) { }
144a10a842fSNicholas Piggin 
14509154985SThomas Gleixner /**
14609154985SThomas Gleixner  * lockup_detector_update_enable - Update the sysctl enable bit
14709154985SThomas Gleixner  *
14809154985SThomas Gleixner  * Caller needs to make sure that the NMI/perf watchdogs are off, so this
14909154985SThomas Gleixner  * can't race with watchdog_nmi_disable().
15009154985SThomas Gleixner  */
15109154985SThomas Gleixner static void lockup_detector_update_enable(void)
15209154985SThomas Gleixner {
15309154985SThomas Gleixner 	watchdog_enabled = 0;
15409154985SThomas Gleixner 	if (!watchdog_user_enabled)
15509154985SThomas Gleixner 		return;
156a994a314SThomas Gleixner 	if (nmi_watchdog_available && nmi_watchdog_user_enabled)
15709154985SThomas Gleixner 		watchdog_enabled |= NMI_WATCHDOG_ENABLED;
15809154985SThomas Gleixner 	if (soft_watchdog_user_enabled)
15909154985SThomas Gleixner 		watchdog_enabled |= SOFT_WATCHDOG_ENABLED;
16009154985SThomas Gleixner }
16109154985SThomas Gleixner 
16205a4a952SNicholas Piggin #ifdef CONFIG_SOFTLOCKUP_DETECTOR
16305a4a952SNicholas Piggin 
1642b9d7f23SThomas Gleixner /* Global variables, exported for sysctl */
1652b9d7f23SThomas Gleixner unsigned int __read_mostly softlockup_panic =
1662b9d7f23SThomas Gleixner 			CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC_VALUE;
1672eb2527fSThomas Gleixner 
1680f34c400SChuansheng Liu static u64 __read_mostly sample_period;
16958687acbSDon Zickus 
17058687acbSDon Zickus static DEFINE_PER_CPU(unsigned long, watchdog_touch_ts);
17158687acbSDon Zickus static DEFINE_PER_CPU(struct task_struct *, softlockup_watchdog);
17258687acbSDon Zickus static DEFINE_PER_CPU(struct hrtimer, watchdog_hrtimer);
17358687acbSDon Zickus static DEFINE_PER_CPU(bool, softlockup_touch_sync);
17458687acbSDon Zickus static DEFINE_PER_CPU(bool, soft_watchdog_warn);
175bcd951cfSThomas Gleixner static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts);
176bcd951cfSThomas Gleixner static DEFINE_PER_CPU(unsigned long, soft_lockup_hrtimer_cnt);
177b1a8de1fSchai wen static DEFINE_PER_CPU(struct task_struct *, softlockup_task_ptr_saved);
17858687acbSDon Zickus static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts_saved);
179ed235875SAaron Tomlin static unsigned long soft_lockup_nmi_warn;
18058687acbSDon Zickus 
18158687acbSDon Zickus static int __init softlockup_panic_setup(char *str)
18258687acbSDon Zickus {
18358687acbSDon Zickus 	softlockup_panic = simple_strtoul(str, NULL, 0);
18458687acbSDon Zickus 	return 1;
18558687acbSDon Zickus }
18658687acbSDon Zickus __setup("softlockup_panic=", softlockup_panic_setup);
18758687acbSDon Zickus 
18858687acbSDon Zickus static int __init nowatchdog_setup(char *str)
18958687acbSDon Zickus {
19009154985SThomas Gleixner 	watchdog_user_enabled = 0;
19158687acbSDon Zickus 	return 1;
19258687acbSDon Zickus }
19358687acbSDon Zickus __setup("nowatchdog", nowatchdog_setup);
19458687acbSDon Zickus 
19558687acbSDon Zickus static int __init nosoftlockup_setup(char *str)
19658687acbSDon Zickus {
19709154985SThomas Gleixner 	soft_watchdog_user_enabled = 0;
19858687acbSDon Zickus 	return 1;
19958687acbSDon Zickus }
20058687acbSDon Zickus __setup("nosoftlockup", nosoftlockup_setup);
201195daf66SUlrich Obergfell 
202ed235875SAaron Tomlin #ifdef CONFIG_SMP
203368a7e2cSThomas Gleixner int __read_mostly sysctl_softlockup_all_cpu_backtrace;
204368a7e2cSThomas Gleixner 
205ed235875SAaron Tomlin static int __init softlockup_all_cpu_backtrace_setup(char *str)
206ed235875SAaron Tomlin {
207368a7e2cSThomas Gleixner 	sysctl_softlockup_all_cpu_backtrace = !!simple_strtol(str, NULL, 0);
208ed235875SAaron Tomlin 	return 1;
209ed235875SAaron Tomlin }
210ed235875SAaron Tomlin __setup("softlockup_all_cpu_backtrace=", softlockup_all_cpu_backtrace_setup);
21105a4a952SNicholas Piggin #endif
21258687acbSDon Zickus 
213941154bdSThomas Gleixner static void __lockup_detector_cleanup(void);
214941154bdSThomas Gleixner 
2154eec42f3SMandeep Singh Baines /*
2164eec42f3SMandeep Singh Baines  * Hard-lockup warnings should be triggered after just a few seconds. Soft-
2174eec42f3SMandeep Singh Baines  * lockups can have false positives under extreme conditions. So we generally
2184eec42f3SMandeep Singh Baines  * want a higher threshold for soft lockups than for hard lockups. So we couple
2194eec42f3SMandeep Singh Baines  * the thresholds with a factor: we make the soft threshold twice the amount of
2204eec42f3SMandeep Singh Baines  * time the hard threshold is.
2214eec42f3SMandeep Singh Baines  */
2226e9101aeSIngo Molnar static int get_softlockup_thresh(void)
2234eec42f3SMandeep Singh Baines {
2244eec42f3SMandeep Singh Baines 	return watchdog_thresh * 2;
2254eec42f3SMandeep Singh Baines }
22658687acbSDon Zickus 
22758687acbSDon Zickus /*
22858687acbSDon Zickus  * Returns seconds, approximately.  We don't need nanosecond
22958687acbSDon Zickus  * resolution, and we don't need to waste time with a big divide when
23058687acbSDon Zickus  * 2^30ns == 1.074s.
23158687acbSDon Zickus  */
232c06b4f19SNamhyung Kim static unsigned long get_timestamp(void)
23358687acbSDon Zickus {
234545a2bf7SCyril Bur 	return running_clock() >> 30LL;  /* 2^30 ~= 10^9 */
23558687acbSDon Zickus }
23658687acbSDon Zickus 
2370f34c400SChuansheng Liu static void set_sample_period(void)
23858687acbSDon Zickus {
23958687acbSDon Zickus 	/*
240586692a5SMandeep Singh Baines 	 * convert watchdog_thresh from seconds to ns
24186f5e6a7SFernando Luis Vázquez Cao 	 * the divide by 5 is to give hrtimer several chances (two
24286f5e6a7SFernando Luis Vázquez Cao 	 * or three with the current relation between the soft
24386f5e6a7SFernando Luis Vázquez Cao 	 * and hard thresholds) to increment before the
24486f5e6a7SFernando Luis Vázquez Cao 	 * hardlockup detector generates a warning
24558687acbSDon Zickus 	 */
2460f34c400SChuansheng Liu 	sample_period = get_softlockup_thresh() * ((u64)NSEC_PER_SEC / 5);
2477edaeb68SThomas Gleixner 	watchdog_update_hrtimer_threshold(sample_period);
24858687acbSDon Zickus }
24958687acbSDon Zickus 
25058687acbSDon Zickus /* Commands for resetting the watchdog */
25158687acbSDon Zickus static void __touch_watchdog(void)
25258687acbSDon Zickus {
253c06b4f19SNamhyung Kim 	__this_cpu_write(watchdog_touch_ts, get_timestamp());
25458687acbSDon Zickus }
25558687acbSDon Zickus 
25603e0d461STejun Heo /**
25703e0d461STejun Heo  * touch_softlockup_watchdog_sched - touch watchdog on scheduler stalls
25803e0d461STejun Heo  *
25903e0d461STejun Heo  * Call when the scheduler may have stalled for legitimate reasons
26003e0d461STejun Heo  * preventing the watchdog task from executing - e.g. the scheduler
26103e0d461STejun Heo  * entering idle state.  This should only be used for scheduler events.
26203e0d461STejun Heo  * Use touch_softlockup_watchdog() for everything else.
26303e0d461STejun Heo  */
26403e0d461STejun Heo void touch_softlockup_watchdog_sched(void)
26558687acbSDon Zickus {
2667861144bSAndrew Morton 	/*
2677861144bSAndrew Morton 	 * Preemption can be enabled.  It doesn't matter which CPU's timestamp
2687861144bSAndrew Morton 	 * gets zeroed here, so use the raw_ operation.
2697861144bSAndrew Morton 	 */
2707861144bSAndrew Morton 	raw_cpu_write(watchdog_touch_ts, 0);
27158687acbSDon Zickus }
27203e0d461STejun Heo 
27303e0d461STejun Heo void touch_softlockup_watchdog(void)
27403e0d461STejun Heo {
27503e0d461STejun Heo 	touch_softlockup_watchdog_sched();
27682607adcSTejun Heo 	wq_watchdog_touch(raw_smp_processor_id());
27703e0d461STejun Heo }
2780167c781SIngo Molnar EXPORT_SYMBOL(touch_softlockup_watchdog);
27958687acbSDon Zickus 
280332fbdbcSDon Zickus void touch_all_softlockup_watchdogs(void)
28158687acbSDon Zickus {
28258687acbSDon Zickus 	int cpu;
28358687acbSDon Zickus 
28458687acbSDon Zickus 	/*
285d57108d4SThomas Gleixner 	 * watchdog_mutex cannpt be taken here, as this might be called
286d57108d4SThomas Gleixner 	 * from (soft)interrupt context, so the access to
287d57108d4SThomas Gleixner 	 * watchdog_allowed_cpumask might race with a concurrent update.
288d57108d4SThomas Gleixner 	 *
289d57108d4SThomas Gleixner 	 * The watchdog time stamp can race against a concurrent real
290d57108d4SThomas Gleixner 	 * update as well, the only side effect might be a cycle delay for
291d57108d4SThomas Gleixner 	 * the softlockup check.
29258687acbSDon Zickus 	 */
293d57108d4SThomas Gleixner 	for_each_cpu(cpu, &watchdog_allowed_mask)
29458687acbSDon Zickus 		per_cpu(watchdog_touch_ts, cpu) = 0;
29582607adcSTejun Heo 	wq_watchdog_touch(-1);
29658687acbSDon Zickus }
29758687acbSDon Zickus 
29858687acbSDon Zickus void touch_softlockup_watchdog_sync(void)
29958687acbSDon Zickus {
300f7f66b05SChristoph Lameter 	__this_cpu_write(softlockup_touch_sync, true);
301f7f66b05SChristoph Lameter 	__this_cpu_write(watchdog_touch_ts, 0);
30258687acbSDon Zickus }
30358687acbSDon Zickus 
30426e09c6eSDon Zickus static int is_softlockup(unsigned long touch_ts)
30558687acbSDon Zickus {
306c06b4f19SNamhyung Kim 	unsigned long now = get_timestamp();
30758687acbSDon Zickus 
30839d2da21SUlrich Obergfell 	if ((watchdog_enabled & SOFT_WATCHDOG_ENABLED) && watchdog_thresh){
309195daf66SUlrich Obergfell 		/* Warn about unreasonable delays. */
3104eec42f3SMandeep Singh Baines 		if (time_after(now, touch_ts + get_softlockup_thresh()))
31158687acbSDon Zickus 			return now - touch_ts;
312195daf66SUlrich Obergfell 	}
31358687acbSDon Zickus 	return 0;
31458687acbSDon Zickus }
31558687acbSDon Zickus 
31605a4a952SNicholas Piggin /* watchdog detector functions */
31705a4a952SNicholas Piggin bool is_hardlockup(void)
31805a4a952SNicholas Piggin {
31905a4a952SNicholas Piggin 	unsigned long hrint = __this_cpu_read(hrtimer_interrupts);
32005a4a952SNicholas Piggin 
32105a4a952SNicholas Piggin 	if (__this_cpu_read(hrtimer_interrupts_saved) == hrint)
32205a4a952SNicholas Piggin 		return true;
32305a4a952SNicholas Piggin 
32405a4a952SNicholas Piggin 	__this_cpu_write(hrtimer_interrupts_saved, hrint);
32505a4a952SNicholas Piggin 	return false;
32605a4a952SNicholas Piggin }
32705a4a952SNicholas Piggin 
32858687acbSDon Zickus static void watchdog_interrupt_count(void)
32958687acbSDon Zickus {
330909ea964SChristoph Lameter 	__this_cpu_inc(hrtimer_interrupts);
33158687acbSDon Zickus }
332bcd951cfSThomas Gleixner 
33358687acbSDon Zickus /* watchdog kicker functions */
33458687acbSDon Zickus static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer)
33558687acbSDon Zickus {
336909ea964SChristoph Lameter 	unsigned long touch_ts = __this_cpu_read(watchdog_touch_ts);
33758687acbSDon Zickus 	struct pt_regs *regs = get_irq_regs();
33858687acbSDon Zickus 	int duration;
339ed235875SAaron Tomlin 	int softlockup_all_cpu_backtrace = sysctl_softlockup_all_cpu_backtrace;
34058687acbSDon Zickus 
34101f0a027SThomas Gleixner 	if (!watchdog_enabled)
342b94f5118SDon Zickus 		return HRTIMER_NORESTART;
343b94f5118SDon Zickus 
34458687acbSDon Zickus 	/* kick the hardlockup detector */
34558687acbSDon Zickus 	watchdog_interrupt_count();
34658687acbSDon Zickus 
34758687acbSDon Zickus 	/* kick the softlockup detector */
348909ea964SChristoph Lameter 	wake_up_process(__this_cpu_read(softlockup_watchdog));
34958687acbSDon Zickus 
35058687acbSDon Zickus 	/* .. and repeat */
3510f34c400SChuansheng Liu 	hrtimer_forward_now(hrtimer, ns_to_ktime(sample_period));
35258687acbSDon Zickus 
35358687acbSDon Zickus 	if (touch_ts == 0) {
354909ea964SChristoph Lameter 		if (unlikely(__this_cpu_read(softlockup_touch_sync))) {
35558687acbSDon Zickus 			/*
35658687acbSDon Zickus 			 * If the time stamp was touched atomically
35758687acbSDon Zickus 			 * make sure the scheduler tick is up to date.
35858687acbSDon Zickus 			 */
359909ea964SChristoph Lameter 			__this_cpu_write(softlockup_touch_sync, false);
36058687acbSDon Zickus 			sched_clock_tick();
36158687acbSDon Zickus 		}
3625d1c0f4aSEric B Munson 
3635d1c0f4aSEric B Munson 		/* Clear the guest paused flag on watchdog reset */
3645d1c0f4aSEric B Munson 		kvm_check_and_clear_guest_paused();
36558687acbSDon Zickus 		__touch_watchdog();
36658687acbSDon Zickus 		return HRTIMER_RESTART;
36758687acbSDon Zickus 	}
36858687acbSDon Zickus 
36958687acbSDon Zickus 	/* check for a softlockup
37058687acbSDon Zickus 	 * This is done by making sure a high priority task is
37158687acbSDon Zickus 	 * being scheduled.  The task touches the watchdog to
37258687acbSDon Zickus 	 * indicate it is getting cpu time.  If it hasn't then
37358687acbSDon Zickus 	 * this is a good indication some task is hogging the cpu
37458687acbSDon Zickus 	 */
37526e09c6eSDon Zickus 	duration = is_softlockup(touch_ts);
37658687acbSDon Zickus 	if (unlikely(duration)) {
3775d1c0f4aSEric B Munson 		/*
3785d1c0f4aSEric B Munson 		 * If a virtual machine is stopped by the host it can look to
3795d1c0f4aSEric B Munson 		 * the watchdog like a soft lockup, check to see if the host
3805d1c0f4aSEric B Munson 		 * stopped the vm before we issue the warning
3815d1c0f4aSEric B Munson 		 */
3825d1c0f4aSEric B Munson 		if (kvm_check_and_clear_guest_paused())
3835d1c0f4aSEric B Munson 			return HRTIMER_RESTART;
3845d1c0f4aSEric B Munson 
38558687acbSDon Zickus 		/* only warn once */
386b1a8de1fSchai wen 		if (__this_cpu_read(soft_watchdog_warn) == true) {
387b1a8de1fSchai wen 			/*
388b1a8de1fSchai wen 			 * When multiple processes are causing softlockups the
389b1a8de1fSchai wen 			 * softlockup detector only warns on the first one
390b1a8de1fSchai wen 			 * because the code relies on a full quiet cycle to
391b1a8de1fSchai wen 			 * re-arm.  The second process prevents the quiet cycle
392b1a8de1fSchai wen 			 * and never gets reported.  Use task pointers to detect
393b1a8de1fSchai wen 			 * this.
394b1a8de1fSchai wen 			 */
395b1a8de1fSchai wen 			if (__this_cpu_read(softlockup_task_ptr_saved) !=
396b1a8de1fSchai wen 			    current) {
397b1a8de1fSchai wen 				__this_cpu_write(soft_watchdog_warn, false);
398b1a8de1fSchai wen 				__touch_watchdog();
399b1a8de1fSchai wen 			}
40058687acbSDon Zickus 			return HRTIMER_RESTART;
401b1a8de1fSchai wen 		}
40258687acbSDon Zickus 
403ed235875SAaron Tomlin 		if (softlockup_all_cpu_backtrace) {
404ed235875SAaron Tomlin 			/* Prevent multiple soft-lockup reports if one cpu is already
405ed235875SAaron Tomlin 			 * engaged in dumping cpu back traces
406ed235875SAaron Tomlin 			 */
407ed235875SAaron Tomlin 			if (test_and_set_bit(0, &soft_lockup_nmi_warn)) {
408ed235875SAaron Tomlin 				/* Someone else will report us. Let's give up */
409ed235875SAaron Tomlin 				__this_cpu_write(soft_watchdog_warn, true);
410ed235875SAaron Tomlin 				return HRTIMER_RESTART;
411ed235875SAaron Tomlin 			}
412ed235875SAaron Tomlin 		}
413ed235875SAaron Tomlin 
414656c3b79SFabian Frederick 		pr_emerg("BUG: soft lockup - CPU#%d stuck for %us! [%s:%d]\n",
41526e09c6eSDon Zickus 			smp_processor_id(), duration,
41658687acbSDon Zickus 			current->comm, task_pid_nr(current));
417b1a8de1fSchai wen 		__this_cpu_write(softlockup_task_ptr_saved, current);
41858687acbSDon Zickus 		print_modules();
41958687acbSDon Zickus 		print_irqtrace_events(current);
42058687acbSDon Zickus 		if (regs)
42158687acbSDon Zickus 			show_regs(regs);
42258687acbSDon Zickus 		else
42358687acbSDon Zickus 			dump_stack();
42458687acbSDon Zickus 
425ed235875SAaron Tomlin 		if (softlockup_all_cpu_backtrace) {
426ed235875SAaron Tomlin 			/* Avoid generating two back traces for current
427ed235875SAaron Tomlin 			 * given that one is already made above
428ed235875SAaron Tomlin 			 */
429ed235875SAaron Tomlin 			trigger_allbutself_cpu_backtrace();
430ed235875SAaron Tomlin 
431ed235875SAaron Tomlin 			clear_bit(0, &soft_lockup_nmi_warn);
432ed235875SAaron Tomlin 			/* Barrier to sync with other cpus */
433ed235875SAaron Tomlin 			smp_mb__after_atomic();
434ed235875SAaron Tomlin 		}
435ed235875SAaron Tomlin 
43669361eefSJosh Hunt 		add_taint(TAINT_SOFTLOCKUP, LOCKDEP_STILL_OK);
43758687acbSDon Zickus 		if (softlockup_panic)
43858687acbSDon Zickus 			panic("softlockup: hung tasks");
439909ea964SChristoph Lameter 		__this_cpu_write(soft_watchdog_warn, true);
44058687acbSDon Zickus 	} else
441909ea964SChristoph Lameter 		__this_cpu_write(soft_watchdog_warn, false);
44258687acbSDon Zickus 
44358687acbSDon Zickus 	return HRTIMER_RESTART;
44458687acbSDon Zickus }
44558687acbSDon Zickus 
446bcd951cfSThomas Gleixner static void watchdog_set_prio(unsigned int policy, unsigned int prio)
44758687acbSDon Zickus {
448bcd951cfSThomas Gleixner 	struct sched_param param = { .sched_priority = prio };
449bcd951cfSThomas Gleixner 
450bcd951cfSThomas Gleixner 	sched_setscheduler(current, policy, &param);
451bcd951cfSThomas Gleixner }
452bcd951cfSThomas Gleixner 
453bcd951cfSThomas Gleixner static void watchdog_enable(unsigned int cpu)
454bcd951cfSThomas Gleixner {
45501f0a027SThomas Gleixner 	struct hrtimer *hrtimer = this_cpu_ptr(&watchdog_hrtimer);
45658687acbSDon Zickus 
45701f0a027SThomas Gleixner 	/*
45801f0a027SThomas Gleixner 	 * Start the timer first to prevent the NMI watchdog triggering
45901f0a027SThomas Gleixner 	 * before the timer has a chance to fire.
46001f0a027SThomas Gleixner 	 */
4613935e895SBjørn Mork 	hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
4623935e895SBjørn Mork 	hrtimer->function = watchdog_timer_fn;
4630f34c400SChuansheng Liu 	hrtimer_start(hrtimer, ns_to_ktime(sample_period),
46458687acbSDon Zickus 		      HRTIMER_MODE_REL_PINNED);
46558687acbSDon Zickus 
46601f0a027SThomas Gleixner 	/* Initialize timestamp */
46758687acbSDon Zickus 	__touch_watchdog();
46801f0a027SThomas Gleixner 	/* Enable the perf event */
469146c9d0eSThomas Gleixner 	if (watchdog_enabled & NMI_WATCHDOG_ENABLED)
47001f0a027SThomas Gleixner 		watchdog_nmi_enable(cpu);
47101f0a027SThomas Gleixner 
47201f0a027SThomas Gleixner 	watchdog_set_prio(SCHED_FIFO, MAX_RT_PRIO - 1);
47358687acbSDon Zickus }
474bcd951cfSThomas Gleixner 
475bcd951cfSThomas Gleixner static void watchdog_disable(unsigned int cpu)
476bcd951cfSThomas Gleixner {
47701f0a027SThomas Gleixner 	struct hrtimer *hrtimer = this_cpu_ptr(&watchdog_hrtimer);
478bcd951cfSThomas Gleixner 
479bcd951cfSThomas Gleixner 	watchdog_set_prio(SCHED_NORMAL, 0);
48001f0a027SThomas Gleixner 	/*
48101f0a027SThomas Gleixner 	 * Disable the perf event first. That prevents that a large delay
48201f0a027SThomas Gleixner 	 * between disabling the timer and disabling the perf event causes
48301f0a027SThomas Gleixner 	 * the perf NMI to detect a false positive.
48401f0a027SThomas Gleixner 	 */
485bcd951cfSThomas Gleixner 	watchdog_nmi_disable(cpu);
48601f0a027SThomas Gleixner 	hrtimer_cancel(hrtimer);
487bcd951cfSThomas Gleixner }
488bcd951cfSThomas Gleixner 
489b8900bc0SFrederic Weisbecker static void watchdog_cleanup(unsigned int cpu, bool online)
490b8900bc0SFrederic Weisbecker {
491b8900bc0SFrederic Weisbecker 	watchdog_disable(cpu);
492b8900bc0SFrederic Weisbecker }
493b8900bc0SFrederic Weisbecker 
494bcd951cfSThomas Gleixner static int watchdog_should_run(unsigned int cpu)
495bcd951cfSThomas Gleixner {
496bcd951cfSThomas Gleixner 	return __this_cpu_read(hrtimer_interrupts) !=
497bcd951cfSThomas Gleixner 		__this_cpu_read(soft_lockup_hrtimer_cnt);
498bcd951cfSThomas Gleixner }
499bcd951cfSThomas Gleixner 
500b60f796cSAndrew Morton /*
501bcd951cfSThomas Gleixner  * The watchdog thread function - touches the timestamp.
502bcd951cfSThomas Gleixner  *
5030f34c400SChuansheng Liu  * It only runs once every sample_period seconds (4 seconds by
504bcd951cfSThomas Gleixner  * default) to reset the softlockup timestamp. If this gets delayed
505bcd951cfSThomas Gleixner  * for more than 2*watchdog_thresh seconds then the debug-printout
506bcd951cfSThomas Gleixner  * triggers in watchdog_timer_fn().
507b60f796cSAndrew Morton  */
508bcd951cfSThomas Gleixner static void watchdog(unsigned int cpu)
509bcd951cfSThomas Gleixner {
510bcd951cfSThomas Gleixner 	__this_cpu_write(soft_lockup_hrtimer_cnt,
511bcd951cfSThomas Gleixner 			 __this_cpu_read(hrtimer_interrupts));
512bcd951cfSThomas Gleixner 	__touch_watchdog();
51358687acbSDon Zickus }
51458687acbSDon Zickus 
515b8900bc0SFrederic Weisbecker static struct smp_hotplug_thread watchdog_threads = {
516b8900bc0SFrederic Weisbecker 	.store			= &softlockup_watchdog,
517b8900bc0SFrederic Weisbecker 	.thread_should_run	= watchdog_should_run,
518b8900bc0SFrederic Weisbecker 	.thread_fn		= watchdog,
519b8900bc0SFrederic Weisbecker 	.thread_comm		= "watchdog/%u",
520b8900bc0SFrederic Weisbecker 	.setup			= watchdog_enable,
521b8900bc0SFrederic Weisbecker 	.cleanup		= watchdog_cleanup,
522b8900bc0SFrederic Weisbecker 	.park			= watchdog_disable,
523b8900bc0SFrederic Weisbecker 	.unpark			= watchdog_enable,
524b8900bc0SFrederic Weisbecker };
525b8900bc0SFrederic Weisbecker 
5262eb2527fSThomas Gleixner static void softlockup_update_smpboot_threads(void)
5272eb2527fSThomas Gleixner {
5282eb2527fSThomas Gleixner 	lockdep_assert_held(&watchdog_mutex);
5292eb2527fSThomas Gleixner 
5302eb2527fSThomas Gleixner 	if (!softlockup_threads_initialized)
5312eb2527fSThomas Gleixner 		return;
5322eb2527fSThomas Gleixner 
5332eb2527fSThomas Gleixner 	smpboot_update_cpumask_percpu_thread(&watchdog_threads,
5342eb2527fSThomas Gleixner 					     &watchdog_allowed_mask);
5352eb2527fSThomas Gleixner 	__lockup_detector_cleanup();
5362eb2527fSThomas Gleixner }
5372eb2527fSThomas Gleixner 
5382eb2527fSThomas Gleixner /* Temporarily park all watchdog threads */
5392eb2527fSThomas Gleixner static void softlockup_park_all_threads(void)
5402eb2527fSThomas Gleixner {
5412eb2527fSThomas Gleixner 	cpumask_clear(&watchdog_allowed_mask);
5422eb2527fSThomas Gleixner 	softlockup_update_smpboot_threads();
5432eb2527fSThomas Gleixner }
5442eb2527fSThomas Gleixner 
545e8b62b2dSThomas Gleixner /* Unpark enabled threads */
546e8b62b2dSThomas Gleixner static void softlockup_unpark_threads(void)
5472eb2527fSThomas Gleixner {
5482eb2527fSThomas Gleixner 	cpumask_copy(&watchdog_allowed_mask, &watchdog_cpumask);
5492eb2527fSThomas Gleixner 	softlockup_update_smpboot_threads();
5502eb2527fSThomas Gleixner }
5512eb2527fSThomas Gleixner 
55209154985SThomas Gleixner static void softlockup_reconfigure_threads(void)
5532eb2527fSThomas Gleixner {
5546592ad2fSThomas Gleixner 	watchdog_nmi_reconfigure(false);
5552eb2527fSThomas Gleixner 	softlockup_park_all_threads();
5562eb2527fSThomas Gleixner 	set_sample_period();
55709154985SThomas Gleixner 	lockup_detector_update_enable();
55809154985SThomas Gleixner 	if (watchdog_enabled && watchdog_thresh)
559e8b62b2dSThomas Gleixner 		softlockup_unpark_threads();
5606592ad2fSThomas Gleixner 	watchdog_nmi_reconfigure(true);
5612eb2527fSThomas Gleixner }
5622eb2527fSThomas Gleixner 
5632eb2527fSThomas Gleixner /*
5642eb2527fSThomas Gleixner  * Create the watchdog thread infrastructure.
5652eb2527fSThomas Gleixner  *
5662eb2527fSThomas Gleixner  * The threads are not unparked as watchdog_allowed_mask is empty.  When
5672eb2527fSThomas Gleixner  * the threads are sucessfully initialized, take the proper locks and
5682eb2527fSThomas Gleixner  * unpark the threads in the watchdog_cpumask if the watchdog is enabled.
5692eb2527fSThomas Gleixner  */
5702eb2527fSThomas Gleixner static __init void softlockup_init_threads(void)
5712eb2527fSThomas Gleixner {
5722eb2527fSThomas Gleixner 	int ret;
5732eb2527fSThomas Gleixner 
5742eb2527fSThomas Gleixner 	/*
5752eb2527fSThomas Gleixner 	 * If sysctl is off and watchdog got disabled on the command line,
5762eb2527fSThomas Gleixner 	 * nothing to do here.
5772eb2527fSThomas Gleixner 	 */
57809154985SThomas Gleixner 	lockup_detector_update_enable();
57909154985SThomas Gleixner 
5802eb2527fSThomas Gleixner 	if (!IS_ENABLED(CONFIG_SYSCTL) &&
5812eb2527fSThomas Gleixner 	    !(watchdog_enabled && watchdog_thresh))
5822eb2527fSThomas Gleixner 		return;
5832eb2527fSThomas Gleixner 
5842eb2527fSThomas Gleixner 	ret = smpboot_register_percpu_thread_cpumask(&watchdog_threads,
5852eb2527fSThomas Gleixner 						     &watchdog_allowed_mask);
5862eb2527fSThomas Gleixner 	if (ret) {
5872eb2527fSThomas Gleixner 		pr_err("Failed to initialize soft lockup detector threads\n");
5882eb2527fSThomas Gleixner 		return;
5892eb2527fSThomas Gleixner 	}
5902eb2527fSThomas Gleixner 
5912eb2527fSThomas Gleixner 	mutex_lock(&watchdog_mutex);
5922eb2527fSThomas Gleixner 	softlockup_threads_initialized = true;
59309154985SThomas Gleixner 	softlockup_reconfigure_threads();
5942eb2527fSThomas Gleixner 	mutex_unlock(&watchdog_mutex);
5952eb2527fSThomas Gleixner }
5962eb2527fSThomas Gleixner 
5972b9d7f23SThomas Gleixner #else /* CONFIG_SOFTLOCKUP_DETECTOR */
5982b9d7f23SThomas Gleixner static inline int watchdog_park_threads(void) { return 0; }
5992b9d7f23SThomas Gleixner static inline void watchdog_unpark_threads(void) { }
6002b9d7f23SThomas Gleixner static inline int watchdog_enable_all_cpus(void) { return 0; }
6012b9d7f23SThomas Gleixner static inline void watchdog_disable_all_cpus(void) { }
6022eb2527fSThomas Gleixner static inline void softlockup_init_threads(void) { }
60309154985SThomas Gleixner static void softlockup_reconfigure_threads(void)
6046592ad2fSThomas Gleixner {
6056592ad2fSThomas Gleixner 	watchdog_nmi_reconfigure(false);
60609154985SThomas Gleixner 	lockup_detector_update_enable();
6076592ad2fSThomas Gleixner 	watchdog_nmi_reconfigure(true);
6086592ad2fSThomas Gleixner }
6092b9d7f23SThomas Gleixner #endif /* !CONFIG_SOFTLOCKUP_DETECTOR */
61005a4a952SNicholas Piggin 
611941154bdSThomas Gleixner static void __lockup_detector_cleanup(void)
612941154bdSThomas Gleixner {
613941154bdSThomas Gleixner 	lockdep_assert_held(&watchdog_mutex);
614941154bdSThomas Gleixner 	hardlockup_detector_perf_cleanup();
615941154bdSThomas Gleixner }
616941154bdSThomas Gleixner 
617941154bdSThomas Gleixner /**
618941154bdSThomas Gleixner  * lockup_detector_cleanup - Cleanup after cpu hotplug or sysctl changes
619941154bdSThomas Gleixner  *
620941154bdSThomas Gleixner  * Caller must not hold the cpu hotplug rwsem.
621941154bdSThomas Gleixner  */
622941154bdSThomas Gleixner void lockup_detector_cleanup(void)
623941154bdSThomas Gleixner {
624941154bdSThomas Gleixner 	mutex_lock(&watchdog_mutex);
625941154bdSThomas Gleixner 	__lockup_detector_cleanup();
626941154bdSThomas Gleixner 	mutex_unlock(&watchdog_mutex);
627941154bdSThomas Gleixner }
628941154bdSThomas Gleixner 
6296554fd8cSThomas Gleixner /**
6306554fd8cSThomas Gleixner  * lockup_detector_soft_poweroff - Interface to stop lockup detector(s)
6316554fd8cSThomas Gleixner  *
6326554fd8cSThomas Gleixner  * Special interface for parisc. It prevents lockup detector warnings from
6336554fd8cSThomas Gleixner  * the default pm_poweroff() function which busy loops forever.
6346554fd8cSThomas Gleixner  */
6356554fd8cSThomas Gleixner void lockup_detector_soft_poweroff(void)
6366554fd8cSThomas Gleixner {
6376554fd8cSThomas Gleixner 	watchdog_enabled = 0;
6386554fd8cSThomas Gleixner }
6396554fd8cSThomas Gleixner 
64058cf690aSUlrich Obergfell #ifdef CONFIG_SYSCTL
64158cf690aSUlrich Obergfell 
642e8b62b2dSThomas Gleixner /* Propagate any changes to the watchdog threads */
643d57108d4SThomas Gleixner static void proc_watchdog_update(void)
64458687acbSDon Zickus {
645e8b62b2dSThomas Gleixner 	/* Remove impossible cpus to keep sysctl output clean. */
646e8b62b2dSThomas Gleixner 	cpumask_and(&watchdog_cpumask, &watchdog_cpumask, cpu_possible_mask);
64709154985SThomas Gleixner 	softlockup_reconfigure_threads();
648a0c9cbb9SUlrich Obergfell }
649a0c9cbb9SUlrich Obergfell 
650a0c9cbb9SUlrich Obergfell /*
651ef246a21SUlrich Obergfell  * common function for watchdog, nmi_watchdog and soft_watchdog parameter
652ef246a21SUlrich Obergfell  *
6537feeb9cdSThomas Gleixner  * caller             | table->data points to      | 'which'
6547feeb9cdSThomas Gleixner  * -------------------|----------------------------|--------------------------
6557feeb9cdSThomas Gleixner  * proc_watchdog      | watchdog_user_enabled      | NMI_WATCHDOG_ENABLED |
6567feeb9cdSThomas Gleixner  *                    |                            | SOFT_WATCHDOG_ENABLED
6577feeb9cdSThomas Gleixner  * -------------------|----------------------------|--------------------------
6587feeb9cdSThomas Gleixner  * proc_nmi_watchdog  | nmi_watchdog_user_enabled  | NMI_WATCHDOG_ENABLED
6597feeb9cdSThomas Gleixner  * -------------------|----------------------------|--------------------------
6607feeb9cdSThomas Gleixner  * proc_soft_watchdog | soft_watchdog_user_enabled | SOFT_WATCHDOG_ENABLED
661ef246a21SUlrich Obergfell  */
662ef246a21SUlrich Obergfell static int proc_watchdog_common(int which, struct ctl_table *table, int write,
663ef246a21SUlrich Obergfell 				void __user *buffer, size_t *lenp, loff_t *ppos)
664ef246a21SUlrich Obergfell {
66509154985SThomas Gleixner 	int err, old, *param = table->data;
666bcd951cfSThomas Gleixner 
667b7a34981SThomas Gleixner 	cpu_hotplug_disable();
668946d1977SThomas Gleixner 	mutex_lock(&watchdog_mutex);
669ef246a21SUlrich Obergfell 
670ef246a21SUlrich Obergfell 	if (!write) {
67109154985SThomas Gleixner 		/*
67209154985SThomas Gleixner 		 * On read synchronize the userspace interface. This is a
67309154985SThomas Gleixner 		 * racy snapshot.
67409154985SThomas Gleixner 		 */
67509154985SThomas Gleixner 		*param = (watchdog_enabled & which) != 0;
676b8900bc0SFrederic Weisbecker 		err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
677ef246a21SUlrich Obergfell 	} else {
67809154985SThomas Gleixner 		old = READ_ONCE(*param);
679ef246a21SUlrich Obergfell 		err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
68009154985SThomas Gleixner 		if (!err && old != READ_ONCE(*param))
681d57108d4SThomas Gleixner 			proc_watchdog_update();
682ef246a21SUlrich Obergfell 	}
683946d1977SThomas Gleixner 	mutex_unlock(&watchdog_mutex);
684b7a34981SThomas Gleixner 	cpu_hotplug_enable();
685ef246a21SUlrich Obergfell 	return err;
686ef246a21SUlrich Obergfell }
687ef246a21SUlrich Obergfell 
688ef246a21SUlrich Obergfell /*
68983a80a39SUlrich Obergfell  * /proc/sys/kernel/watchdog
69083a80a39SUlrich Obergfell  */
69183a80a39SUlrich Obergfell int proc_watchdog(struct ctl_table *table, int write,
69283a80a39SUlrich Obergfell 		  void __user *buffer, size_t *lenp, loff_t *ppos)
69383a80a39SUlrich Obergfell {
69483a80a39SUlrich Obergfell 	return proc_watchdog_common(NMI_WATCHDOG_ENABLED|SOFT_WATCHDOG_ENABLED,
69583a80a39SUlrich Obergfell 				    table, write, buffer, lenp, ppos);
69683a80a39SUlrich Obergfell }
69783a80a39SUlrich Obergfell 
69883a80a39SUlrich Obergfell /*
69983a80a39SUlrich Obergfell  * /proc/sys/kernel/nmi_watchdog
70083a80a39SUlrich Obergfell  */
70183a80a39SUlrich Obergfell int proc_nmi_watchdog(struct ctl_table *table, int write,
70283a80a39SUlrich Obergfell 		      void __user *buffer, size_t *lenp, loff_t *ppos)
70383a80a39SUlrich Obergfell {
704a994a314SThomas Gleixner 	if (!nmi_watchdog_available && write)
705a994a314SThomas Gleixner 		return -ENOTSUPP;
70683a80a39SUlrich Obergfell 	return proc_watchdog_common(NMI_WATCHDOG_ENABLED,
70783a80a39SUlrich Obergfell 				    table, write, buffer, lenp, ppos);
70883a80a39SUlrich Obergfell }
70983a80a39SUlrich Obergfell 
71083a80a39SUlrich Obergfell /*
71183a80a39SUlrich Obergfell  * /proc/sys/kernel/soft_watchdog
71283a80a39SUlrich Obergfell  */
71383a80a39SUlrich Obergfell int proc_soft_watchdog(struct ctl_table *table, int write,
71483a80a39SUlrich Obergfell 			void __user *buffer, size_t *lenp, loff_t *ppos)
71583a80a39SUlrich Obergfell {
71683a80a39SUlrich Obergfell 	return proc_watchdog_common(SOFT_WATCHDOG_ENABLED,
71783a80a39SUlrich Obergfell 				    table, write, buffer, lenp, ppos);
71883a80a39SUlrich Obergfell }
71983a80a39SUlrich Obergfell 
72083a80a39SUlrich Obergfell /*
72183a80a39SUlrich Obergfell  * /proc/sys/kernel/watchdog_thresh
72283a80a39SUlrich Obergfell  */
72383a80a39SUlrich Obergfell int proc_watchdog_thresh(struct ctl_table *table, int write,
72483a80a39SUlrich Obergfell 			 void __user *buffer, size_t *lenp, loff_t *ppos)
72583a80a39SUlrich Obergfell {
726d57108d4SThomas Gleixner 	int err, old;
72783a80a39SUlrich Obergfell 
728b7a34981SThomas Gleixner 	cpu_hotplug_disable();
729946d1977SThomas Gleixner 	mutex_lock(&watchdog_mutex);
73083a80a39SUlrich Obergfell 
731d57108d4SThomas Gleixner 	old = READ_ONCE(watchdog_thresh);
73283a80a39SUlrich Obergfell 	err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
73383a80a39SUlrich Obergfell 
734d57108d4SThomas Gleixner 	if (!err && write && old != READ_ONCE(watchdog_thresh))
735d57108d4SThomas Gleixner 		proc_watchdog_update();
736e04ab2bcSMandeep Singh Baines 
737946d1977SThomas Gleixner 	mutex_unlock(&watchdog_mutex);
738b7a34981SThomas Gleixner 	cpu_hotplug_enable();
739b8900bc0SFrederic Weisbecker 	return err;
74058687acbSDon Zickus }
741fe4ba3c3SChris Metcalf 
742fe4ba3c3SChris Metcalf /*
743fe4ba3c3SChris Metcalf  * The cpumask is the mask of possible cpus that the watchdog can run
744fe4ba3c3SChris Metcalf  * on, not the mask of cpus it is actually running on.  This allows the
745fe4ba3c3SChris Metcalf  * user to specify a mask that will include cpus that have not yet
746fe4ba3c3SChris Metcalf  * been brought online, if desired.
747fe4ba3c3SChris Metcalf  */
748fe4ba3c3SChris Metcalf int proc_watchdog_cpumask(struct ctl_table *table, int write,
749fe4ba3c3SChris Metcalf 			  void __user *buffer, size_t *lenp, loff_t *ppos)
750fe4ba3c3SChris Metcalf {
751fe4ba3c3SChris Metcalf 	int err;
752fe4ba3c3SChris Metcalf 
753b7a34981SThomas Gleixner 	cpu_hotplug_disable();
754946d1977SThomas Gleixner 	mutex_lock(&watchdog_mutex);
7558c073d27SUlrich Obergfell 
756fe4ba3c3SChris Metcalf 	err = proc_do_large_bitmap(table, write, buffer, lenp, ppos);
75705ba3de7SThomas Gleixner 	if (!err && write)
758e8b62b2dSThomas Gleixner 		proc_watchdog_update();
7595490125dSThomas Gleixner 
760946d1977SThomas Gleixner 	mutex_unlock(&watchdog_mutex);
761b7a34981SThomas Gleixner 	cpu_hotplug_enable();
762fe4ba3c3SChris Metcalf 	return err;
763fe4ba3c3SChris Metcalf }
76458687acbSDon Zickus #endif /* CONFIG_SYSCTL */
76558687acbSDon Zickus 
766004417a6SPeter Zijlstra void __init lockup_detector_init(void)
76758687acbSDon Zickus {
768fe4ba3c3SChris Metcalf #ifdef CONFIG_NO_HZ_FULL
769fe4ba3c3SChris Metcalf 	if (tick_nohz_full_enabled()) {
770fe4ba3c3SChris Metcalf 		pr_info("Disabling watchdog on nohz_full cores by default\n");
771314b08ffSFrederic Weisbecker 		cpumask_copy(&watchdog_cpumask, housekeeping_mask);
772fe4ba3c3SChris Metcalf 	} else
773fe4ba3c3SChris Metcalf 		cpumask_copy(&watchdog_cpumask, cpu_possible_mask);
774fe4ba3c3SChris Metcalf #else
775fe4ba3c3SChris Metcalf 	cpumask_copy(&watchdog_cpumask, cpu_possible_mask);
776fe4ba3c3SChris Metcalf #endif
777fe4ba3c3SChris Metcalf 
778a994a314SThomas Gleixner 	if (!watchdog_nmi_probe())
779a994a314SThomas Gleixner 		nmi_watchdog_available = true;
780d57108d4SThomas Gleixner 	softlockup_init_threads();
78158687acbSDon Zickus }
782