xref: /openbmc/linux/kernel/watchdog.c (revision e31d6883)
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 /**
1266b9dc480SThomas Gleixner  * watchdog_nmi_stop - Stop the watchdog for reconfiguration
1276592ad2fSThomas Gleixner  *
1286b9dc480SThomas Gleixner  * The reconfiguration steps are:
1296b9dc480SThomas Gleixner  * watchdog_nmi_stop();
1306592ad2fSThomas Gleixner  * update_variables();
1316b9dc480SThomas Gleixner  * watchdog_nmi_start();
1326b9dc480SThomas Gleixner  */
1336b9dc480SThomas Gleixner void __weak watchdog_nmi_stop(void) { }
1346b9dc480SThomas Gleixner 
1356b9dc480SThomas Gleixner /**
1366b9dc480SThomas Gleixner  * watchdog_nmi_start - Start the watchdog after reconfiguration
1376592ad2fSThomas Gleixner  *
1386b9dc480SThomas Gleixner  * Counterpart to watchdog_nmi_stop().
1396b9dc480SThomas Gleixner  *
1406b9dc480SThomas Gleixner  * The following variables have been updated in update_variables() and
1416b9dc480SThomas Gleixner  * contain the currently valid configuration:
1427feeb9cdSThomas Gleixner  * - watchdog_enabled
143a10a842fSNicholas Piggin  * - watchdog_thresh
144a10a842fSNicholas Piggin  * - watchdog_cpumask
145a10a842fSNicholas Piggin  */
1466b9dc480SThomas Gleixner void __weak watchdog_nmi_start(void) { }
147a10a842fSNicholas Piggin 
14809154985SThomas Gleixner /**
14909154985SThomas Gleixner  * lockup_detector_update_enable - Update the sysctl enable bit
15009154985SThomas Gleixner  *
15109154985SThomas Gleixner  * Caller needs to make sure that the NMI/perf watchdogs are off, so this
15209154985SThomas Gleixner  * can't race with watchdog_nmi_disable().
15309154985SThomas Gleixner  */
15409154985SThomas Gleixner static void lockup_detector_update_enable(void)
15509154985SThomas Gleixner {
15609154985SThomas Gleixner 	watchdog_enabled = 0;
15709154985SThomas Gleixner 	if (!watchdog_user_enabled)
15809154985SThomas Gleixner 		return;
159a994a314SThomas Gleixner 	if (nmi_watchdog_available && nmi_watchdog_user_enabled)
16009154985SThomas Gleixner 		watchdog_enabled |= NMI_WATCHDOG_ENABLED;
16109154985SThomas Gleixner 	if (soft_watchdog_user_enabled)
16209154985SThomas Gleixner 		watchdog_enabled |= SOFT_WATCHDOG_ENABLED;
16309154985SThomas Gleixner }
16409154985SThomas Gleixner 
16505a4a952SNicholas Piggin #ifdef CONFIG_SOFTLOCKUP_DETECTOR
16605a4a952SNicholas Piggin 
1672b9d7f23SThomas Gleixner /* Global variables, exported for sysctl */
1682b9d7f23SThomas Gleixner unsigned int __read_mostly softlockup_panic =
1692b9d7f23SThomas Gleixner 			CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC_VALUE;
1702eb2527fSThomas Gleixner 
1710f34c400SChuansheng Liu static u64 __read_mostly sample_period;
17258687acbSDon Zickus 
17358687acbSDon Zickus static DEFINE_PER_CPU(unsigned long, watchdog_touch_ts);
17458687acbSDon Zickus static DEFINE_PER_CPU(struct task_struct *, softlockup_watchdog);
17558687acbSDon Zickus static DEFINE_PER_CPU(struct hrtimer, watchdog_hrtimer);
17658687acbSDon Zickus static DEFINE_PER_CPU(bool, softlockup_touch_sync);
17758687acbSDon Zickus static DEFINE_PER_CPU(bool, soft_watchdog_warn);
178bcd951cfSThomas Gleixner static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts);
179bcd951cfSThomas Gleixner static DEFINE_PER_CPU(unsigned long, soft_lockup_hrtimer_cnt);
180b1a8de1fSchai wen static DEFINE_PER_CPU(struct task_struct *, softlockup_task_ptr_saved);
18158687acbSDon Zickus static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts_saved);
182ed235875SAaron Tomlin static unsigned long soft_lockup_nmi_warn;
18358687acbSDon Zickus 
18458687acbSDon Zickus static int __init softlockup_panic_setup(char *str)
18558687acbSDon Zickus {
18658687acbSDon Zickus 	softlockup_panic = simple_strtoul(str, NULL, 0);
18758687acbSDon Zickus 	return 1;
18858687acbSDon Zickus }
18958687acbSDon Zickus __setup("softlockup_panic=", softlockup_panic_setup);
19058687acbSDon Zickus 
19158687acbSDon Zickus static int __init nowatchdog_setup(char *str)
19258687acbSDon Zickus {
19309154985SThomas Gleixner 	watchdog_user_enabled = 0;
19458687acbSDon Zickus 	return 1;
19558687acbSDon Zickus }
19658687acbSDon Zickus __setup("nowatchdog", nowatchdog_setup);
19758687acbSDon Zickus 
19858687acbSDon Zickus static int __init nosoftlockup_setup(char *str)
19958687acbSDon Zickus {
20009154985SThomas Gleixner 	soft_watchdog_user_enabled = 0;
20158687acbSDon Zickus 	return 1;
20258687acbSDon Zickus }
20358687acbSDon Zickus __setup("nosoftlockup", nosoftlockup_setup);
204195daf66SUlrich Obergfell 
205ed235875SAaron Tomlin #ifdef CONFIG_SMP
206368a7e2cSThomas Gleixner int __read_mostly sysctl_softlockup_all_cpu_backtrace;
207368a7e2cSThomas Gleixner 
208ed235875SAaron Tomlin static int __init softlockup_all_cpu_backtrace_setup(char *str)
209ed235875SAaron Tomlin {
210368a7e2cSThomas Gleixner 	sysctl_softlockup_all_cpu_backtrace = !!simple_strtol(str, NULL, 0);
211ed235875SAaron Tomlin 	return 1;
212ed235875SAaron Tomlin }
213ed235875SAaron Tomlin __setup("softlockup_all_cpu_backtrace=", softlockup_all_cpu_backtrace_setup);
21405a4a952SNicholas Piggin #endif
21558687acbSDon Zickus 
216941154bdSThomas Gleixner static void __lockup_detector_cleanup(void);
217941154bdSThomas Gleixner 
2184eec42f3SMandeep Singh Baines /*
2194eec42f3SMandeep Singh Baines  * Hard-lockup warnings should be triggered after just a few seconds. Soft-
2204eec42f3SMandeep Singh Baines  * lockups can have false positives under extreme conditions. So we generally
2214eec42f3SMandeep Singh Baines  * want a higher threshold for soft lockups than for hard lockups. So we couple
2224eec42f3SMandeep Singh Baines  * the thresholds with a factor: we make the soft threshold twice the amount of
2234eec42f3SMandeep Singh Baines  * time the hard threshold is.
2244eec42f3SMandeep Singh Baines  */
2256e9101aeSIngo Molnar static int get_softlockup_thresh(void)
2264eec42f3SMandeep Singh Baines {
2274eec42f3SMandeep Singh Baines 	return watchdog_thresh * 2;
2284eec42f3SMandeep Singh Baines }
22958687acbSDon Zickus 
23058687acbSDon Zickus /*
23158687acbSDon Zickus  * Returns seconds, approximately.  We don't need nanosecond
23258687acbSDon Zickus  * resolution, and we don't need to waste time with a big divide when
23358687acbSDon Zickus  * 2^30ns == 1.074s.
23458687acbSDon Zickus  */
235c06b4f19SNamhyung Kim static unsigned long get_timestamp(void)
23658687acbSDon Zickus {
237545a2bf7SCyril Bur 	return running_clock() >> 30LL;  /* 2^30 ~= 10^9 */
23858687acbSDon Zickus }
23958687acbSDon Zickus 
2400f34c400SChuansheng Liu static void set_sample_period(void)
24158687acbSDon Zickus {
24258687acbSDon Zickus 	/*
243586692a5SMandeep Singh Baines 	 * convert watchdog_thresh from seconds to ns
24486f5e6a7SFernando Luis Vázquez Cao 	 * the divide by 5 is to give hrtimer several chances (two
24586f5e6a7SFernando Luis Vázquez Cao 	 * or three with the current relation between the soft
24686f5e6a7SFernando Luis Vázquez Cao 	 * and hard thresholds) to increment before the
24786f5e6a7SFernando Luis Vázquez Cao 	 * hardlockup detector generates a warning
24858687acbSDon Zickus 	 */
2490f34c400SChuansheng Liu 	sample_period = get_softlockup_thresh() * ((u64)NSEC_PER_SEC / 5);
2507edaeb68SThomas Gleixner 	watchdog_update_hrtimer_threshold(sample_period);
25158687acbSDon Zickus }
25258687acbSDon Zickus 
25358687acbSDon Zickus /* Commands for resetting the watchdog */
25458687acbSDon Zickus static void __touch_watchdog(void)
25558687acbSDon Zickus {
256c06b4f19SNamhyung Kim 	__this_cpu_write(watchdog_touch_ts, get_timestamp());
25758687acbSDon Zickus }
25858687acbSDon Zickus 
25903e0d461STejun Heo /**
26003e0d461STejun Heo  * touch_softlockup_watchdog_sched - touch watchdog on scheduler stalls
26103e0d461STejun Heo  *
26203e0d461STejun Heo  * Call when the scheduler may have stalled for legitimate reasons
26303e0d461STejun Heo  * preventing the watchdog task from executing - e.g. the scheduler
26403e0d461STejun Heo  * entering idle state.  This should only be used for scheduler events.
26503e0d461STejun Heo  * Use touch_softlockup_watchdog() for everything else.
26603e0d461STejun Heo  */
26703e0d461STejun Heo void touch_softlockup_watchdog_sched(void)
26858687acbSDon Zickus {
2697861144bSAndrew Morton 	/*
2707861144bSAndrew Morton 	 * Preemption can be enabled.  It doesn't matter which CPU's timestamp
2717861144bSAndrew Morton 	 * gets zeroed here, so use the raw_ operation.
2727861144bSAndrew Morton 	 */
2737861144bSAndrew Morton 	raw_cpu_write(watchdog_touch_ts, 0);
27458687acbSDon Zickus }
27503e0d461STejun Heo 
27603e0d461STejun Heo void touch_softlockup_watchdog(void)
27703e0d461STejun Heo {
27803e0d461STejun Heo 	touch_softlockup_watchdog_sched();
27982607adcSTejun Heo 	wq_watchdog_touch(raw_smp_processor_id());
28003e0d461STejun Heo }
2810167c781SIngo Molnar EXPORT_SYMBOL(touch_softlockup_watchdog);
28258687acbSDon Zickus 
283332fbdbcSDon Zickus void touch_all_softlockup_watchdogs(void)
28458687acbSDon Zickus {
28558687acbSDon Zickus 	int cpu;
28658687acbSDon Zickus 
28758687acbSDon Zickus 	/*
288d57108d4SThomas Gleixner 	 * watchdog_mutex cannpt be taken here, as this might be called
289d57108d4SThomas Gleixner 	 * from (soft)interrupt context, so the access to
290d57108d4SThomas Gleixner 	 * watchdog_allowed_cpumask might race with a concurrent update.
291d57108d4SThomas Gleixner 	 *
292d57108d4SThomas Gleixner 	 * The watchdog time stamp can race against a concurrent real
293d57108d4SThomas Gleixner 	 * update as well, the only side effect might be a cycle delay for
294d57108d4SThomas Gleixner 	 * the softlockup check.
29558687acbSDon Zickus 	 */
296d57108d4SThomas Gleixner 	for_each_cpu(cpu, &watchdog_allowed_mask)
29758687acbSDon Zickus 		per_cpu(watchdog_touch_ts, cpu) = 0;
29882607adcSTejun Heo 	wq_watchdog_touch(-1);
29958687acbSDon Zickus }
30058687acbSDon Zickus 
30158687acbSDon Zickus void touch_softlockup_watchdog_sync(void)
30258687acbSDon Zickus {
303f7f66b05SChristoph Lameter 	__this_cpu_write(softlockup_touch_sync, true);
304f7f66b05SChristoph Lameter 	__this_cpu_write(watchdog_touch_ts, 0);
30558687acbSDon Zickus }
30658687acbSDon Zickus 
30726e09c6eSDon Zickus static int is_softlockup(unsigned long touch_ts)
30858687acbSDon Zickus {
309c06b4f19SNamhyung Kim 	unsigned long now = get_timestamp();
31058687acbSDon Zickus 
31139d2da21SUlrich Obergfell 	if ((watchdog_enabled & SOFT_WATCHDOG_ENABLED) && watchdog_thresh){
312195daf66SUlrich Obergfell 		/* Warn about unreasonable delays. */
3134eec42f3SMandeep Singh Baines 		if (time_after(now, touch_ts + get_softlockup_thresh()))
31458687acbSDon Zickus 			return now - touch_ts;
315195daf66SUlrich Obergfell 	}
31658687acbSDon Zickus 	return 0;
31758687acbSDon Zickus }
31858687acbSDon Zickus 
31905a4a952SNicholas Piggin /* watchdog detector functions */
32005a4a952SNicholas Piggin bool is_hardlockup(void)
32105a4a952SNicholas Piggin {
32205a4a952SNicholas Piggin 	unsigned long hrint = __this_cpu_read(hrtimer_interrupts);
32305a4a952SNicholas Piggin 
32405a4a952SNicholas Piggin 	if (__this_cpu_read(hrtimer_interrupts_saved) == hrint)
32505a4a952SNicholas Piggin 		return true;
32605a4a952SNicholas Piggin 
32705a4a952SNicholas Piggin 	__this_cpu_write(hrtimer_interrupts_saved, hrint);
32805a4a952SNicholas Piggin 	return false;
32905a4a952SNicholas Piggin }
33005a4a952SNicholas Piggin 
33158687acbSDon Zickus static void watchdog_interrupt_count(void)
33258687acbSDon Zickus {
333909ea964SChristoph Lameter 	__this_cpu_inc(hrtimer_interrupts);
33458687acbSDon Zickus }
335bcd951cfSThomas Gleixner 
33658687acbSDon Zickus /* watchdog kicker functions */
33758687acbSDon Zickus static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer)
33858687acbSDon Zickus {
339909ea964SChristoph Lameter 	unsigned long touch_ts = __this_cpu_read(watchdog_touch_ts);
34058687acbSDon Zickus 	struct pt_regs *regs = get_irq_regs();
34158687acbSDon Zickus 	int duration;
342ed235875SAaron Tomlin 	int softlockup_all_cpu_backtrace = sysctl_softlockup_all_cpu_backtrace;
34358687acbSDon Zickus 
34401f0a027SThomas Gleixner 	if (!watchdog_enabled)
345b94f5118SDon Zickus 		return HRTIMER_NORESTART;
346b94f5118SDon Zickus 
34758687acbSDon Zickus 	/* kick the hardlockup detector */
34858687acbSDon Zickus 	watchdog_interrupt_count();
34958687acbSDon Zickus 
35058687acbSDon Zickus 	/* kick the softlockup detector */
351909ea964SChristoph Lameter 	wake_up_process(__this_cpu_read(softlockup_watchdog));
35258687acbSDon Zickus 
35358687acbSDon Zickus 	/* .. and repeat */
3540f34c400SChuansheng Liu 	hrtimer_forward_now(hrtimer, ns_to_ktime(sample_period));
35558687acbSDon Zickus 
35658687acbSDon Zickus 	if (touch_ts == 0) {
357909ea964SChristoph Lameter 		if (unlikely(__this_cpu_read(softlockup_touch_sync))) {
35858687acbSDon Zickus 			/*
35958687acbSDon Zickus 			 * If the time stamp was touched atomically
36058687acbSDon Zickus 			 * make sure the scheduler tick is up to date.
36158687acbSDon Zickus 			 */
362909ea964SChristoph Lameter 			__this_cpu_write(softlockup_touch_sync, false);
36358687acbSDon Zickus 			sched_clock_tick();
36458687acbSDon Zickus 		}
3655d1c0f4aSEric B Munson 
3665d1c0f4aSEric B Munson 		/* Clear the guest paused flag on watchdog reset */
3675d1c0f4aSEric B Munson 		kvm_check_and_clear_guest_paused();
36858687acbSDon Zickus 		__touch_watchdog();
36958687acbSDon Zickus 		return HRTIMER_RESTART;
37058687acbSDon Zickus 	}
37158687acbSDon Zickus 
37258687acbSDon Zickus 	/* check for a softlockup
37358687acbSDon Zickus 	 * This is done by making sure a high priority task is
37458687acbSDon Zickus 	 * being scheduled.  The task touches the watchdog to
37558687acbSDon Zickus 	 * indicate it is getting cpu time.  If it hasn't then
37658687acbSDon Zickus 	 * this is a good indication some task is hogging the cpu
37758687acbSDon Zickus 	 */
37826e09c6eSDon Zickus 	duration = is_softlockup(touch_ts);
37958687acbSDon Zickus 	if (unlikely(duration)) {
3805d1c0f4aSEric B Munson 		/*
3815d1c0f4aSEric B Munson 		 * If a virtual machine is stopped by the host it can look to
3825d1c0f4aSEric B Munson 		 * the watchdog like a soft lockup, check to see if the host
3835d1c0f4aSEric B Munson 		 * stopped the vm before we issue the warning
3845d1c0f4aSEric B Munson 		 */
3855d1c0f4aSEric B Munson 		if (kvm_check_and_clear_guest_paused())
3865d1c0f4aSEric B Munson 			return HRTIMER_RESTART;
3875d1c0f4aSEric B Munson 
38858687acbSDon Zickus 		/* only warn once */
389b1a8de1fSchai wen 		if (__this_cpu_read(soft_watchdog_warn) == true) {
390b1a8de1fSchai wen 			/*
391b1a8de1fSchai wen 			 * When multiple processes are causing softlockups the
392b1a8de1fSchai wen 			 * softlockup detector only warns on the first one
393b1a8de1fSchai wen 			 * because the code relies on a full quiet cycle to
394b1a8de1fSchai wen 			 * re-arm.  The second process prevents the quiet cycle
395b1a8de1fSchai wen 			 * and never gets reported.  Use task pointers to detect
396b1a8de1fSchai wen 			 * this.
397b1a8de1fSchai wen 			 */
398b1a8de1fSchai wen 			if (__this_cpu_read(softlockup_task_ptr_saved) !=
399b1a8de1fSchai wen 			    current) {
400b1a8de1fSchai wen 				__this_cpu_write(soft_watchdog_warn, false);
401b1a8de1fSchai wen 				__touch_watchdog();
402b1a8de1fSchai wen 			}
40358687acbSDon Zickus 			return HRTIMER_RESTART;
404b1a8de1fSchai wen 		}
40558687acbSDon Zickus 
406ed235875SAaron Tomlin 		if (softlockup_all_cpu_backtrace) {
407ed235875SAaron Tomlin 			/* Prevent multiple soft-lockup reports if one cpu is already
408ed235875SAaron Tomlin 			 * engaged in dumping cpu back traces
409ed235875SAaron Tomlin 			 */
410ed235875SAaron Tomlin 			if (test_and_set_bit(0, &soft_lockup_nmi_warn)) {
411ed235875SAaron Tomlin 				/* Someone else will report us. Let's give up */
412ed235875SAaron Tomlin 				__this_cpu_write(soft_watchdog_warn, true);
413ed235875SAaron Tomlin 				return HRTIMER_RESTART;
414ed235875SAaron Tomlin 			}
415ed235875SAaron Tomlin 		}
416ed235875SAaron Tomlin 
417656c3b79SFabian Frederick 		pr_emerg("BUG: soft lockup - CPU#%d stuck for %us! [%s:%d]\n",
41826e09c6eSDon Zickus 			smp_processor_id(), duration,
41958687acbSDon Zickus 			current->comm, task_pid_nr(current));
420b1a8de1fSchai wen 		__this_cpu_write(softlockup_task_ptr_saved, current);
42158687acbSDon Zickus 		print_modules();
42258687acbSDon Zickus 		print_irqtrace_events(current);
42358687acbSDon Zickus 		if (regs)
42458687acbSDon Zickus 			show_regs(regs);
42558687acbSDon Zickus 		else
42658687acbSDon Zickus 			dump_stack();
42758687acbSDon Zickus 
428ed235875SAaron Tomlin 		if (softlockup_all_cpu_backtrace) {
429ed235875SAaron Tomlin 			/* Avoid generating two back traces for current
430ed235875SAaron Tomlin 			 * given that one is already made above
431ed235875SAaron Tomlin 			 */
432ed235875SAaron Tomlin 			trigger_allbutself_cpu_backtrace();
433ed235875SAaron Tomlin 
434ed235875SAaron Tomlin 			clear_bit(0, &soft_lockup_nmi_warn);
435ed235875SAaron Tomlin 			/* Barrier to sync with other cpus */
436ed235875SAaron Tomlin 			smp_mb__after_atomic();
437ed235875SAaron Tomlin 		}
438ed235875SAaron Tomlin 
43969361eefSJosh Hunt 		add_taint(TAINT_SOFTLOCKUP, LOCKDEP_STILL_OK);
44058687acbSDon Zickus 		if (softlockup_panic)
44158687acbSDon Zickus 			panic("softlockup: hung tasks");
442909ea964SChristoph Lameter 		__this_cpu_write(soft_watchdog_warn, true);
44358687acbSDon Zickus 	} else
444909ea964SChristoph Lameter 		__this_cpu_write(soft_watchdog_warn, false);
44558687acbSDon Zickus 
44658687acbSDon Zickus 	return HRTIMER_RESTART;
44758687acbSDon Zickus }
44858687acbSDon Zickus 
449bcd951cfSThomas Gleixner static void watchdog_set_prio(unsigned int policy, unsigned int prio)
45058687acbSDon Zickus {
451bcd951cfSThomas Gleixner 	struct sched_param param = { .sched_priority = prio };
452bcd951cfSThomas Gleixner 
453bcd951cfSThomas Gleixner 	sched_setscheduler(current, policy, &param);
454bcd951cfSThomas Gleixner }
455bcd951cfSThomas Gleixner 
456bcd951cfSThomas Gleixner static void watchdog_enable(unsigned int cpu)
457bcd951cfSThomas Gleixner {
45801f0a027SThomas Gleixner 	struct hrtimer *hrtimer = this_cpu_ptr(&watchdog_hrtimer);
45958687acbSDon Zickus 
46001f0a027SThomas Gleixner 	/*
46101f0a027SThomas Gleixner 	 * Start the timer first to prevent the NMI watchdog triggering
46201f0a027SThomas Gleixner 	 * before the timer has a chance to fire.
46301f0a027SThomas Gleixner 	 */
4643935e895SBjørn Mork 	hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
4653935e895SBjørn Mork 	hrtimer->function = watchdog_timer_fn;
4660f34c400SChuansheng Liu 	hrtimer_start(hrtimer, ns_to_ktime(sample_period),
46758687acbSDon Zickus 		      HRTIMER_MODE_REL_PINNED);
46858687acbSDon Zickus 
46901f0a027SThomas Gleixner 	/* Initialize timestamp */
47058687acbSDon Zickus 	__touch_watchdog();
47101f0a027SThomas Gleixner 	/* Enable the perf event */
472146c9d0eSThomas Gleixner 	if (watchdog_enabled & NMI_WATCHDOG_ENABLED)
47301f0a027SThomas Gleixner 		watchdog_nmi_enable(cpu);
47401f0a027SThomas Gleixner 
47501f0a027SThomas Gleixner 	watchdog_set_prio(SCHED_FIFO, MAX_RT_PRIO - 1);
47658687acbSDon Zickus }
477bcd951cfSThomas Gleixner 
478bcd951cfSThomas Gleixner static void watchdog_disable(unsigned int cpu)
479bcd951cfSThomas Gleixner {
48001f0a027SThomas Gleixner 	struct hrtimer *hrtimer = this_cpu_ptr(&watchdog_hrtimer);
481bcd951cfSThomas Gleixner 
482bcd951cfSThomas Gleixner 	watchdog_set_prio(SCHED_NORMAL, 0);
48301f0a027SThomas Gleixner 	/*
48401f0a027SThomas Gleixner 	 * Disable the perf event first. That prevents that a large delay
48501f0a027SThomas Gleixner 	 * between disabling the timer and disabling the perf event causes
48601f0a027SThomas Gleixner 	 * the perf NMI to detect a false positive.
48701f0a027SThomas Gleixner 	 */
488bcd951cfSThomas Gleixner 	watchdog_nmi_disable(cpu);
48901f0a027SThomas Gleixner 	hrtimer_cancel(hrtimer);
490bcd951cfSThomas Gleixner }
491bcd951cfSThomas Gleixner 
492b8900bc0SFrederic Weisbecker static void watchdog_cleanup(unsigned int cpu, bool online)
493b8900bc0SFrederic Weisbecker {
494b8900bc0SFrederic Weisbecker 	watchdog_disable(cpu);
495b8900bc0SFrederic Weisbecker }
496b8900bc0SFrederic Weisbecker 
497bcd951cfSThomas Gleixner static int watchdog_should_run(unsigned int cpu)
498bcd951cfSThomas Gleixner {
499bcd951cfSThomas Gleixner 	return __this_cpu_read(hrtimer_interrupts) !=
500bcd951cfSThomas Gleixner 		__this_cpu_read(soft_lockup_hrtimer_cnt);
501bcd951cfSThomas Gleixner }
502bcd951cfSThomas Gleixner 
503b60f796cSAndrew Morton /*
504bcd951cfSThomas Gleixner  * The watchdog thread function - touches the timestamp.
505bcd951cfSThomas Gleixner  *
5060f34c400SChuansheng Liu  * It only runs once every sample_period seconds (4 seconds by
507bcd951cfSThomas Gleixner  * default) to reset the softlockup timestamp. If this gets delayed
508bcd951cfSThomas Gleixner  * for more than 2*watchdog_thresh seconds then the debug-printout
509bcd951cfSThomas Gleixner  * triggers in watchdog_timer_fn().
510b60f796cSAndrew Morton  */
511bcd951cfSThomas Gleixner static void watchdog(unsigned int cpu)
512bcd951cfSThomas Gleixner {
513bcd951cfSThomas Gleixner 	__this_cpu_write(soft_lockup_hrtimer_cnt,
514bcd951cfSThomas Gleixner 			 __this_cpu_read(hrtimer_interrupts));
515bcd951cfSThomas Gleixner 	__touch_watchdog();
51658687acbSDon Zickus }
51758687acbSDon Zickus 
518b8900bc0SFrederic Weisbecker static struct smp_hotplug_thread watchdog_threads = {
519b8900bc0SFrederic Weisbecker 	.store			= &softlockup_watchdog,
520b8900bc0SFrederic Weisbecker 	.thread_should_run	= watchdog_should_run,
521b8900bc0SFrederic Weisbecker 	.thread_fn		= watchdog,
522b8900bc0SFrederic Weisbecker 	.thread_comm		= "watchdog/%u",
523b8900bc0SFrederic Weisbecker 	.setup			= watchdog_enable,
524b8900bc0SFrederic Weisbecker 	.cleanup		= watchdog_cleanup,
525b8900bc0SFrederic Weisbecker 	.park			= watchdog_disable,
526b8900bc0SFrederic Weisbecker 	.unpark			= watchdog_enable,
527b8900bc0SFrederic Weisbecker };
528b8900bc0SFrederic Weisbecker 
5292eb2527fSThomas Gleixner static void softlockup_update_smpboot_threads(void)
5302eb2527fSThomas Gleixner {
5312eb2527fSThomas Gleixner 	lockdep_assert_held(&watchdog_mutex);
5322eb2527fSThomas Gleixner 
5332eb2527fSThomas Gleixner 	if (!softlockup_threads_initialized)
5342eb2527fSThomas Gleixner 		return;
5352eb2527fSThomas Gleixner 
5362eb2527fSThomas Gleixner 	smpboot_update_cpumask_percpu_thread(&watchdog_threads,
5372eb2527fSThomas Gleixner 					     &watchdog_allowed_mask);
5382eb2527fSThomas Gleixner }
5392eb2527fSThomas Gleixner 
5402eb2527fSThomas Gleixner /* Temporarily park all watchdog threads */
5412eb2527fSThomas Gleixner static void softlockup_park_all_threads(void)
5422eb2527fSThomas Gleixner {
5432eb2527fSThomas Gleixner 	cpumask_clear(&watchdog_allowed_mask);
5442eb2527fSThomas Gleixner 	softlockup_update_smpboot_threads();
5452eb2527fSThomas Gleixner }
5462eb2527fSThomas Gleixner 
547e8b62b2dSThomas Gleixner /* Unpark enabled threads */
548e8b62b2dSThomas Gleixner static void softlockup_unpark_threads(void)
5492eb2527fSThomas Gleixner {
5502eb2527fSThomas Gleixner 	cpumask_copy(&watchdog_allowed_mask, &watchdog_cpumask);
5512eb2527fSThomas Gleixner 	softlockup_update_smpboot_threads();
5522eb2527fSThomas Gleixner }
5532eb2527fSThomas Gleixner 
55409154985SThomas Gleixner static void softlockup_reconfigure_threads(void)
5552eb2527fSThomas Gleixner {
556e31d6883SThomas Gleixner 	cpus_read_lock();
5576b9dc480SThomas Gleixner 	watchdog_nmi_stop();
5582eb2527fSThomas Gleixner 	softlockup_park_all_threads();
5592eb2527fSThomas Gleixner 	set_sample_period();
56009154985SThomas Gleixner 	lockup_detector_update_enable();
56109154985SThomas Gleixner 	if (watchdog_enabled && watchdog_thresh)
562e8b62b2dSThomas Gleixner 		softlockup_unpark_threads();
5636b9dc480SThomas Gleixner 	watchdog_nmi_start();
564e31d6883SThomas Gleixner 	cpus_read_unlock();
565e31d6883SThomas Gleixner 	/*
566e31d6883SThomas Gleixner 	 * Must be called outside the cpus locked section to prevent
567e31d6883SThomas Gleixner 	 * recursive locking in the perf code.
568e31d6883SThomas Gleixner 	 */
569e31d6883SThomas Gleixner 	__lockup_detector_cleanup();
5702eb2527fSThomas Gleixner }
5712eb2527fSThomas Gleixner 
5722eb2527fSThomas Gleixner /*
5732eb2527fSThomas Gleixner  * Create the watchdog thread infrastructure.
5742eb2527fSThomas Gleixner  *
5752eb2527fSThomas Gleixner  * The threads are not unparked as watchdog_allowed_mask is empty.  When
5762eb2527fSThomas Gleixner  * the threads are sucessfully initialized, take the proper locks and
5772eb2527fSThomas Gleixner  * unpark the threads in the watchdog_cpumask if the watchdog is enabled.
5782eb2527fSThomas Gleixner  */
5792eb2527fSThomas Gleixner static __init void softlockup_init_threads(void)
5802eb2527fSThomas Gleixner {
5812eb2527fSThomas Gleixner 	int ret;
5822eb2527fSThomas Gleixner 
5832eb2527fSThomas Gleixner 	/*
5842eb2527fSThomas Gleixner 	 * If sysctl is off and watchdog got disabled on the command line,
5852eb2527fSThomas Gleixner 	 * nothing to do here.
5862eb2527fSThomas Gleixner 	 */
58709154985SThomas Gleixner 	lockup_detector_update_enable();
58809154985SThomas Gleixner 
5892eb2527fSThomas Gleixner 	if (!IS_ENABLED(CONFIG_SYSCTL) &&
5902eb2527fSThomas Gleixner 	    !(watchdog_enabled && watchdog_thresh))
5912eb2527fSThomas Gleixner 		return;
5922eb2527fSThomas Gleixner 
5932eb2527fSThomas Gleixner 	ret = smpboot_register_percpu_thread_cpumask(&watchdog_threads,
5942eb2527fSThomas Gleixner 						     &watchdog_allowed_mask);
5952eb2527fSThomas Gleixner 	if (ret) {
5962eb2527fSThomas Gleixner 		pr_err("Failed to initialize soft lockup detector threads\n");
5972eb2527fSThomas Gleixner 		return;
5982eb2527fSThomas Gleixner 	}
5992eb2527fSThomas Gleixner 
6002eb2527fSThomas Gleixner 	mutex_lock(&watchdog_mutex);
6012eb2527fSThomas Gleixner 	softlockup_threads_initialized = true;
60209154985SThomas Gleixner 	softlockup_reconfigure_threads();
6032eb2527fSThomas Gleixner 	mutex_unlock(&watchdog_mutex);
6042eb2527fSThomas Gleixner }
6052eb2527fSThomas Gleixner 
6062b9d7f23SThomas Gleixner #else /* CONFIG_SOFTLOCKUP_DETECTOR */
6072b9d7f23SThomas Gleixner static inline int watchdog_park_threads(void) { return 0; }
6082b9d7f23SThomas Gleixner static inline void watchdog_unpark_threads(void) { }
6092b9d7f23SThomas Gleixner static inline int watchdog_enable_all_cpus(void) { return 0; }
6102b9d7f23SThomas Gleixner static inline void watchdog_disable_all_cpus(void) { }
6112eb2527fSThomas Gleixner static inline void softlockup_init_threads(void) { }
61209154985SThomas Gleixner static void softlockup_reconfigure_threads(void)
6136592ad2fSThomas Gleixner {
614e31d6883SThomas Gleixner 	cpus_read_lock();
6156b9dc480SThomas Gleixner 	watchdog_nmi_stop();
61609154985SThomas Gleixner 	lockup_detector_update_enable();
6176b9dc480SThomas Gleixner 	watchdog_nmi_start();
618e31d6883SThomas Gleixner 	cpus_read_unlock();
6196592ad2fSThomas Gleixner }
6202b9d7f23SThomas Gleixner #endif /* !CONFIG_SOFTLOCKUP_DETECTOR */
62105a4a952SNicholas Piggin 
622941154bdSThomas Gleixner static void __lockup_detector_cleanup(void)
623941154bdSThomas Gleixner {
624941154bdSThomas Gleixner 	lockdep_assert_held(&watchdog_mutex);
625941154bdSThomas Gleixner 	hardlockup_detector_perf_cleanup();
626941154bdSThomas Gleixner }
627941154bdSThomas Gleixner 
628941154bdSThomas Gleixner /**
629941154bdSThomas Gleixner  * lockup_detector_cleanup - Cleanup after cpu hotplug or sysctl changes
630941154bdSThomas Gleixner  *
631941154bdSThomas Gleixner  * Caller must not hold the cpu hotplug rwsem.
632941154bdSThomas Gleixner  */
633941154bdSThomas Gleixner void lockup_detector_cleanup(void)
634941154bdSThomas Gleixner {
635941154bdSThomas Gleixner 	mutex_lock(&watchdog_mutex);
636941154bdSThomas Gleixner 	__lockup_detector_cleanup();
637941154bdSThomas Gleixner 	mutex_unlock(&watchdog_mutex);
638941154bdSThomas Gleixner }
639941154bdSThomas Gleixner 
6406554fd8cSThomas Gleixner /**
6416554fd8cSThomas Gleixner  * lockup_detector_soft_poweroff - Interface to stop lockup detector(s)
6426554fd8cSThomas Gleixner  *
6436554fd8cSThomas Gleixner  * Special interface for parisc. It prevents lockup detector warnings from
6446554fd8cSThomas Gleixner  * the default pm_poweroff() function which busy loops forever.
6456554fd8cSThomas Gleixner  */
6466554fd8cSThomas Gleixner void lockup_detector_soft_poweroff(void)
6476554fd8cSThomas Gleixner {
6486554fd8cSThomas Gleixner 	watchdog_enabled = 0;
6496554fd8cSThomas Gleixner }
6506554fd8cSThomas Gleixner 
65158cf690aSUlrich Obergfell #ifdef CONFIG_SYSCTL
65258cf690aSUlrich Obergfell 
653e8b62b2dSThomas Gleixner /* Propagate any changes to the watchdog threads */
654d57108d4SThomas Gleixner static void proc_watchdog_update(void)
65558687acbSDon Zickus {
656e8b62b2dSThomas Gleixner 	/* Remove impossible cpus to keep sysctl output clean. */
657e8b62b2dSThomas Gleixner 	cpumask_and(&watchdog_cpumask, &watchdog_cpumask, cpu_possible_mask);
65809154985SThomas Gleixner 	softlockup_reconfigure_threads();
659a0c9cbb9SUlrich Obergfell }
660a0c9cbb9SUlrich Obergfell 
661a0c9cbb9SUlrich Obergfell /*
662ef246a21SUlrich Obergfell  * common function for watchdog, nmi_watchdog and soft_watchdog parameter
663ef246a21SUlrich Obergfell  *
6647feeb9cdSThomas Gleixner  * caller             | table->data points to      | 'which'
6657feeb9cdSThomas Gleixner  * -------------------|----------------------------|--------------------------
6667feeb9cdSThomas Gleixner  * proc_watchdog      | watchdog_user_enabled      | NMI_WATCHDOG_ENABLED |
6677feeb9cdSThomas Gleixner  *                    |                            | SOFT_WATCHDOG_ENABLED
6687feeb9cdSThomas Gleixner  * -------------------|----------------------------|--------------------------
6697feeb9cdSThomas Gleixner  * proc_nmi_watchdog  | nmi_watchdog_user_enabled  | NMI_WATCHDOG_ENABLED
6707feeb9cdSThomas Gleixner  * -------------------|----------------------------|--------------------------
6717feeb9cdSThomas Gleixner  * proc_soft_watchdog | soft_watchdog_user_enabled | SOFT_WATCHDOG_ENABLED
672ef246a21SUlrich Obergfell  */
673ef246a21SUlrich Obergfell static int proc_watchdog_common(int which, struct ctl_table *table, int write,
674ef246a21SUlrich Obergfell 				void __user *buffer, size_t *lenp, loff_t *ppos)
675ef246a21SUlrich Obergfell {
67609154985SThomas Gleixner 	int err, old, *param = table->data;
677bcd951cfSThomas Gleixner 
678946d1977SThomas Gleixner 	mutex_lock(&watchdog_mutex);
679ef246a21SUlrich Obergfell 
680ef246a21SUlrich Obergfell 	if (!write) {
68109154985SThomas Gleixner 		/*
68209154985SThomas Gleixner 		 * On read synchronize the userspace interface. This is a
68309154985SThomas Gleixner 		 * racy snapshot.
68409154985SThomas Gleixner 		 */
68509154985SThomas Gleixner 		*param = (watchdog_enabled & which) != 0;
686b8900bc0SFrederic Weisbecker 		err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
687ef246a21SUlrich Obergfell 	} else {
68809154985SThomas Gleixner 		old = READ_ONCE(*param);
689ef246a21SUlrich Obergfell 		err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
69009154985SThomas Gleixner 		if (!err && old != READ_ONCE(*param))
691d57108d4SThomas Gleixner 			proc_watchdog_update();
692ef246a21SUlrich Obergfell 	}
693946d1977SThomas Gleixner 	mutex_unlock(&watchdog_mutex);
694ef246a21SUlrich Obergfell 	return err;
695ef246a21SUlrich Obergfell }
696ef246a21SUlrich Obergfell 
697ef246a21SUlrich Obergfell /*
69883a80a39SUlrich Obergfell  * /proc/sys/kernel/watchdog
69983a80a39SUlrich Obergfell  */
70083a80a39SUlrich Obergfell int proc_watchdog(struct ctl_table *table, int write,
70183a80a39SUlrich Obergfell 		  void __user *buffer, size_t *lenp, loff_t *ppos)
70283a80a39SUlrich Obergfell {
70383a80a39SUlrich Obergfell 	return proc_watchdog_common(NMI_WATCHDOG_ENABLED|SOFT_WATCHDOG_ENABLED,
70483a80a39SUlrich Obergfell 				    table, write, buffer, lenp, ppos);
70583a80a39SUlrich Obergfell }
70683a80a39SUlrich Obergfell 
70783a80a39SUlrich Obergfell /*
70883a80a39SUlrich Obergfell  * /proc/sys/kernel/nmi_watchdog
70983a80a39SUlrich Obergfell  */
71083a80a39SUlrich Obergfell int proc_nmi_watchdog(struct ctl_table *table, int write,
71183a80a39SUlrich Obergfell 		      void __user *buffer, size_t *lenp, loff_t *ppos)
71283a80a39SUlrich Obergfell {
713a994a314SThomas Gleixner 	if (!nmi_watchdog_available && write)
714a994a314SThomas Gleixner 		return -ENOTSUPP;
71583a80a39SUlrich Obergfell 	return proc_watchdog_common(NMI_WATCHDOG_ENABLED,
71683a80a39SUlrich Obergfell 				    table, write, buffer, lenp, ppos);
71783a80a39SUlrich Obergfell }
71883a80a39SUlrich Obergfell 
71983a80a39SUlrich Obergfell /*
72083a80a39SUlrich Obergfell  * /proc/sys/kernel/soft_watchdog
72183a80a39SUlrich Obergfell  */
72283a80a39SUlrich Obergfell int proc_soft_watchdog(struct ctl_table *table, int write,
72383a80a39SUlrich Obergfell 			void __user *buffer, size_t *lenp, loff_t *ppos)
72483a80a39SUlrich Obergfell {
72583a80a39SUlrich Obergfell 	return proc_watchdog_common(SOFT_WATCHDOG_ENABLED,
72683a80a39SUlrich Obergfell 				    table, write, buffer, lenp, ppos);
72783a80a39SUlrich Obergfell }
72883a80a39SUlrich Obergfell 
72983a80a39SUlrich Obergfell /*
73083a80a39SUlrich Obergfell  * /proc/sys/kernel/watchdog_thresh
73183a80a39SUlrich Obergfell  */
73283a80a39SUlrich Obergfell int proc_watchdog_thresh(struct ctl_table *table, int write,
73383a80a39SUlrich Obergfell 			 void __user *buffer, size_t *lenp, loff_t *ppos)
73483a80a39SUlrich Obergfell {
735d57108d4SThomas Gleixner 	int err, old;
73683a80a39SUlrich Obergfell 
737946d1977SThomas Gleixner 	mutex_lock(&watchdog_mutex);
73883a80a39SUlrich Obergfell 
739d57108d4SThomas Gleixner 	old = READ_ONCE(watchdog_thresh);
74083a80a39SUlrich Obergfell 	err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
74183a80a39SUlrich Obergfell 
742d57108d4SThomas Gleixner 	if (!err && write && old != READ_ONCE(watchdog_thresh))
743d57108d4SThomas Gleixner 		proc_watchdog_update();
744e04ab2bcSMandeep Singh Baines 
745946d1977SThomas Gleixner 	mutex_unlock(&watchdog_mutex);
746b8900bc0SFrederic Weisbecker 	return err;
74758687acbSDon Zickus }
748fe4ba3c3SChris Metcalf 
749fe4ba3c3SChris Metcalf /*
750fe4ba3c3SChris Metcalf  * The cpumask is the mask of possible cpus that the watchdog can run
751fe4ba3c3SChris Metcalf  * on, not the mask of cpus it is actually running on.  This allows the
752fe4ba3c3SChris Metcalf  * user to specify a mask that will include cpus that have not yet
753fe4ba3c3SChris Metcalf  * been brought online, if desired.
754fe4ba3c3SChris Metcalf  */
755fe4ba3c3SChris Metcalf int proc_watchdog_cpumask(struct ctl_table *table, int write,
756fe4ba3c3SChris Metcalf 			  void __user *buffer, size_t *lenp, loff_t *ppos)
757fe4ba3c3SChris Metcalf {
758fe4ba3c3SChris Metcalf 	int err;
759fe4ba3c3SChris Metcalf 
760946d1977SThomas Gleixner 	mutex_lock(&watchdog_mutex);
7618c073d27SUlrich Obergfell 
762fe4ba3c3SChris Metcalf 	err = proc_do_large_bitmap(table, write, buffer, lenp, ppos);
76305ba3de7SThomas Gleixner 	if (!err && write)
764e8b62b2dSThomas Gleixner 		proc_watchdog_update();
7655490125dSThomas Gleixner 
766946d1977SThomas Gleixner 	mutex_unlock(&watchdog_mutex);
767fe4ba3c3SChris Metcalf 	return err;
768fe4ba3c3SChris Metcalf }
76958687acbSDon Zickus #endif /* CONFIG_SYSCTL */
77058687acbSDon Zickus 
771004417a6SPeter Zijlstra void __init lockup_detector_init(void)
77258687acbSDon Zickus {
773fe4ba3c3SChris Metcalf #ifdef CONFIG_NO_HZ_FULL
774fe4ba3c3SChris Metcalf 	if (tick_nohz_full_enabled()) {
775fe4ba3c3SChris Metcalf 		pr_info("Disabling watchdog on nohz_full cores by default\n");
776314b08ffSFrederic Weisbecker 		cpumask_copy(&watchdog_cpumask, housekeeping_mask);
777fe4ba3c3SChris Metcalf 	} else
778fe4ba3c3SChris Metcalf 		cpumask_copy(&watchdog_cpumask, cpu_possible_mask);
779fe4ba3c3SChris Metcalf #else
780fe4ba3c3SChris Metcalf 	cpumask_copy(&watchdog_cpumask, cpu_possible_mask);
781fe4ba3c3SChris Metcalf #endif
782fe4ba3c3SChris Metcalf 
783a994a314SThomas Gleixner 	if (!watchdog_nmi_probe())
784a994a314SThomas Gleixner 		nmi_watchdog_available = true;
785d57108d4SThomas Gleixner 	softlockup_init_threads();
78658687acbSDon Zickus }
787