xref: /openbmc/linux/kernel/watchdog.c (revision 0f90b88d)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
258687acbSDon Zickus /*
358687acbSDon Zickus  * Detect hard and soft lockups on a system
458687acbSDon Zickus  *
558687acbSDon Zickus  * started by Don Zickus, Copyright (C) 2010 Red Hat, Inc.
658687acbSDon Zickus  *
786f5e6a7SFernando Luis Vázquez Cao  * Note: Most of this code is borrowed heavily from the original softlockup
886f5e6a7SFernando Luis Vázquez Cao  * detector, so thanks to Ingo for the initial implementation.
986f5e6a7SFernando Luis Vázquez Cao  * Some chunks also taken from the old x86-specific nmi watchdog code, thanks
1058687acbSDon Zickus  * to those contributors as well.
1158687acbSDon Zickus  */
1258687acbSDon Zickus 
135f92a7b0SKefeng Wang #define pr_fmt(fmt) "watchdog: " fmt
144501980aSAndrew Morton 
1558687acbSDon Zickus #include <linux/mm.h>
1658687acbSDon Zickus #include <linux/cpu.h>
1758687acbSDon Zickus #include <linux/nmi.h>
1858687acbSDon Zickus #include <linux/init.h>
1958687acbSDon Zickus #include <linux/module.h>
2058687acbSDon Zickus #include <linux/sysctl.h>
21fe4ba3c3SChris Metcalf #include <linux/tick.h>
22e6017571SIngo Molnar #include <linux/sched/clock.h>
23b17b0153SIngo Molnar #include <linux/sched/debug.h>
2478634061SFrederic Weisbecker #include <linux/sched/isolation.h>
259cf57731SPeter Zijlstra #include <linux/stop_machine.h>
2658687acbSDon Zickus 
2758687acbSDon Zickus #include <asm/irq_regs.h>
285d1c0f4aSEric B Munson #include <linux/kvm_para.h>
2958687acbSDon Zickus 
30946d1977SThomas Gleixner static DEFINE_MUTEX(watchdog_mutex);
31ab992dc3SPeter Zijlstra 
3205a4a952SNicholas Piggin #if defined(CONFIG_HARDLOCKUP_DETECTOR) || defined(CONFIG_HAVE_NMI_WATCHDOG)
3309154985SThomas Gleixner # define WATCHDOG_DEFAULT	(SOFT_WATCHDOG_ENABLED | NMI_WATCHDOG_ENABLED)
3409154985SThomas Gleixner # define NMI_WATCHDOG_DEFAULT	1
3584d56e66SUlrich Obergfell #else
3609154985SThomas Gleixner # define WATCHDOG_DEFAULT	(SOFT_WATCHDOG_ENABLED)
3709154985SThomas Gleixner # define NMI_WATCHDOG_DEFAULT	0
3884d56e66SUlrich Obergfell #endif
3905a4a952SNicholas Piggin 
4009154985SThomas Gleixner unsigned long __read_mostly watchdog_enabled;
4109154985SThomas Gleixner int __read_mostly watchdog_user_enabled = 1;
4209154985SThomas Gleixner int __read_mostly nmi_watchdog_user_enabled = NMI_WATCHDOG_DEFAULT;
4309154985SThomas Gleixner int __read_mostly soft_watchdog_user_enabled = 1;
447feeb9cdSThomas Gleixner int __read_mostly watchdog_thresh = 10;
4548084abfSValdis Kletnieks static int __read_mostly nmi_watchdog_available;
467feeb9cdSThomas Gleixner 
477feeb9cdSThomas Gleixner struct cpumask watchdog_cpumask __read_mostly;
487feeb9cdSThomas Gleixner unsigned long *watchdog_cpumask_bits = cpumask_bits(&watchdog_cpumask);
497feeb9cdSThomas Gleixner 
5005a4a952SNicholas Piggin #ifdef CONFIG_HARDLOCKUP_DETECTOR
51f117955aSGuilherme G. Piccoli 
52f117955aSGuilherme G. Piccoli # ifdef CONFIG_SMP
53f117955aSGuilherme G. Piccoli int __read_mostly sysctl_hardlockup_all_cpu_backtrace;
54f117955aSGuilherme G. Piccoli # endif /* CONFIG_SMP */
55f117955aSGuilherme G. Piccoli 
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 #endif /* CONFIG_HARDLOCKUP_DETECTOR */
8905a4a952SNicholas Piggin 
90ec6a9066SUlrich Obergfell /*
9105a4a952SNicholas Piggin  * These functions can be overridden if an architecture implements its
9205a4a952SNicholas Piggin  * own hardlockup detector.
93a10a842fSNicholas Piggin  *
94a10a842fSNicholas Piggin  * watchdog_nmi_enable/disable can be implemented to start and stop when
95a10a842fSNicholas Piggin  * softlockup watchdog threads start and stop. The arch must select the
96a10a842fSNicholas Piggin  * SOFTLOCKUP_DETECTOR Kconfig.
9705a4a952SNicholas Piggin  */
9805a4a952SNicholas Piggin int __weak watchdog_nmi_enable(unsigned int cpu)
9905a4a952SNicholas Piggin {
100146c9d0eSThomas Gleixner 	hardlockup_detector_perf_enable();
10105a4a952SNicholas Piggin 	return 0;
10205a4a952SNicholas Piggin }
103941154bdSThomas Gleixner 
10405a4a952SNicholas Piggin void __weak watchdog_nmi_disable(unsigned int cpu)
10505a4a952SNicholas Piggin {
106941154bdSThomas Gleixner 	hardlockup_detector_perf_disable();
10705a4a952SNicholas Piggin }
10805a4a952SNicholas Piggin 
109a994a314SThomas Gleixner /* Return 0, if a NMI watchdog is available. Error code otherwise */
110a994a314SThomas Gleixner int __weak __init watchdog_nmi_probe(void)
111a994a314SThomas Gleixner {
112a994a314SThomas Gleixner 	return hardlockup_detector_perf_init();
113a994a314SThomas Gleixner }
114a994a314SThomas Gleixner 
1156592ad2fSThomas Gleixner /**
1166b9dc480SThomas Gleixner  * watchdog_nmi_stop - Stop the watchdog for reconfiguration
1176592ad2fSThomas Gleixner  *
1186b9dc480SThomas Gleixner  * The reconfiguration steps are:
1196b9dc480SThomas Gleixner  * watchdog_nmi_stop();
1206592ad2fSThomas Gleixner  * update_variables();
1216b9dc480SThomas Gleixner  * watchdog_nmi_start();
1226b9dc480SThomas Gleixner  */
1236b9dc480SThomas Gleixner void __weak watchdog_nmi_stop(void) { }
1246b9dc480SThomas Gleixner 
1256b9dc480SThomas Gleixner /**
1266b9dc480SThomas Gleixner  * watchdog_nmi_start - Start the watchdog after reconfiguration
1276592ad2fSThomas Gleixner  *
1286b9dc480SThomas Gleixner  * Counterpart to watchdog_nmi_stop().
1296b9dc480SThomas Gleixner  *
1306b9dc480SThomas Gleixner  * The following variables have been updated in update_variables() and
1316b9dc480SThomas Gleixner  * contain the currently valid configuration:
1327feeb9cdSThomas Gleixner  * - watchdog_enabled
133a10a842fSNicholas Piggin  * - watchdog_thresh
134a10a842fSNicholas Piggin  * - watchdog_cpumask
135a10a842fSNicholas Piggin  */
1366b9dc480SThomas Gleixner void __weak watchdog_nmi_start(void) { }
137a10a842fSNicholas Piggin 
13809154985SThomas Gleixner /**
13909154985SThomas Gleixner  * lockup_detector_update_enable - Update the sysctl enable bit
14009154985SThomas Gleixner  *
14109154985SThomas Gleixner  * Caller needs to make sure that the NMI/perf watchdogs are off, so this
14209154985SThomas Gleixner  * can't race with watchdog_nmi_disable().
14309154985SThomas Gleixner  */
14409154985SThomas Gleixner static void lockup_detector_update_enable(void)
14509154985SThomas Gleixner {
14609154985SThomas Gleixner 	watchdog_enabled = 0;
14709154985SThomas Gleixner 	if (!watchdog_user_enabled)
14809154985SThomas Gleixner 		return;
149a994a314SThomas Gleixner 	if (nmi_watchdog_available && nmi_watchdog_user_enabled)
15009154985SThomas Gleixner 		watchdog_enabled |= NMI_WATCHDOG_ENABLED;
15109154985SThomas Gleixner 	if (soft_watchdog_user_enabled)
15209154985SThomas Gleixner 		watchdog_enabled |= SOFT_WATCHDOG_ENABLED;
15309154985SThomas Gleixner }
15409154985SThomas Gleixner 
15505a4a952SNicholas Piggin #ifdef CONFIG_SOFTLOCKUP_DETECTOR
15605a4a952SNicholas Piggin 
157fef06efcSPetr Mladek /*
158fef06efcSPetr Mladek  * Delay the soflockup report when running a known slow code.
159fef06efcSPetr Mladek  * It does _not_ affect the timestamp of the last successdul reschedule.
160fef06efcSPetr Mladek  */
161fef06efcSPetr Mladek #define SOFTLOCKUP_DELAY_REPORT	ULONG_MAX
16211e31f60SThomas Gleixner 
163f117955aSGuilherme G. Piccoli #ifdef CONFIG_SMP
164f117955aSGuilherme G. Piccoli int __read_mostly sysctl_softlockup_all_cpu_backtrace;
165f117955aSGuilherme G. Piccoli #endif
166f117955aSGuilherme G. Piccoli 
167e7e04615SSantosh Sivaraj static struct cpumask watchdog_allowed_mask __read_mostly;
168e7e04615SSantosh Sivaraj 
1692b9d7f23SThomas Gleixner /* Global variables, exported for sysctl */
1702b9d7f23SThomas Gleixner unsigned int __read_mostly softlockup_panic =
1712b9d7f23SThomas Gleixner 			CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC_VALUE;
1722eb2527fSThomas Gleixner 
1739cf57731SPeter Zijlstra static bool softlockup_initialized __read_mostly;
1740f34c400SChuansheng Liu static u64 __read_mostly sample_period;
17558687acbSDon Zickus 
176fef06efcSPetr Mladek /* Timestamp taken after the last successful reschedule. */
17758687acbSDon Zickus static DEFINE_PER_CPU(unsigned long, watchdog_touch_ts);
178fef06efcSPetr Mladek /* Timestamp of the last softlockup report. */
179fef06efcSPetr Mladek static DEFINE_PER_CPU(unsigned long, watchdog_report_ts);
18058687acbSDon Zickus static DEFINE_PER_CPU(struct hrtimer, watchdog_hrtimer);
18158687acbSDon Zickus static DEFINE_PER_CPU(bool, softlockup_touch_sync);
182bcd951cfSThomas Gleixner static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts);
18358687acbSDon Zickus static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts_saved);
184ed235875SAaron Tomlin static unsigned long soft_lockup_nmi_warn;
18558687acbSDon Zickus 
18658687acbSDon Zickus static int __init nowatchdog_setup(char *str)
18758687acbSDon Zickus {
18809154985SThomas Gleixner 	watchdog_user_enabled = 0;
18958687acbSDon Zickus 	return 1;
19058687acbSDon Zickus }
19158687acbSDon Zickus __setup("nowatchdog", nowatchdog_setup);
19258687acbSDon Zickus 
19358687acbSDon Zickus static int __init nosoftlockup_setup(char *str)
19458687acbSDon Zickus {
19509154985SThomas Gleixner 	soft_watchdog_user_enabled = 0;
19658687acbSDon Zickus 	return 1;
19758687acbSDon Zickus }
19858687acbSDon Zickus __setup("nosoftlockup", nosoftlockup_setup);
199195daf66SUlrich Obergfell 
20011295055SLaurence Oberman static int __init watchdog_thresh_setup(char *str)
20111295055SLaurence Oberman {
20211295055SLaurence Oberman 	get_option(&str, &watchdog_thresh);
20311295055SLaurence Oberman 	return 1;
20411295055SLaurence Oberman }
20511295055SLaurence Oberman __setup("watchdog_thresh=", watchdog_thresh_setup);
20611295055SLaurence Oberman 
207941154bdSThomas Gleixner static void __lockup_detector_cleanup(void);
208941154bdSThomas Gleixner 
2094eec42f3SMandeep Singh Baines /*
2104eec42f3SMandeep Singh Baines  * Hard-lockup warnings should be triggered after just a few seconds. Soft-
2114eec42f3SMandeep Singh Baines  * lockups can have false positives under extreme conditions. So we generally
2124eec42f3SMandeep Singh Baines  * want a higher threshold for soft lockups than for hard lockups. So we couple
2134eec42f3SMandeep Singh Baines  * the thresholds with a factor: we make the soft threshold twice the amount of
2144eec42f3SMandeep Singh Baines  * time the hard threshold is.
2154eec42f3SMandeep Singh Baines  */
2166e9101aeSIngo Molnar static int get_softlockup_thresh(void)
2174eec42f3SMandeep Singh Baines {
2184eec42f3SMandeep Singh Baines 	return watchdog_thresh * 2;
2194eec42f3SMandeep Singh Baines }
22058687acbSDon Zickus 
22158687acbSDon Zickus /*
22258687acbSDon Zickus  * Returns seconds, approximately.  We don't need nanosecond
22358687acbSDon Zickus  * resolution, and we don't need to waste time with a big divide when
22458687acbSDon Zickus  * 2^30ns == 1.074s.
22558687acbSDon Zickus  */
226c06b4f19SNamhyung Kim static unsigned long get_timestamp(void)
22758687acbSDon Zickus {
228545a2bf7SCyril Bur 	return running_clock() >> 30LL;  /* 2^30 ~= 10^9 */
22958687acbSDon Zickus }
23058687acbSDon Zickus 
2310f34c400SChuansheng Liu static void set_sample_period(void)
23258687acbSDon Zickus {
23358687acbSDon Zickus 	/*
234586692a5SMandeep Singh Baines 	 * convert watchdog_thresh from seconds to ns
23586f5e6a7SFernando Luis Vázquez Cao 	 * the divide by 5 is to give hrtimer several chances (two
23686f5e6a7SFernando Luis Vázquez Cao 	 * or three with the current relation between the soft
23786f5e6a7SFernando Luis Vázquez Cao 	 * and hard thresholds) to increment before the
23886f5e6a7SFernando Luis Vázquez Cao 	 * hardlockup detector generates a warning
23958687acbSDon Zickus 	 */
2400f34c400SChuansheng Liu 	sample_period = get_softlockup_thresh() * ((u64)NSEC_PER_SEC / 5);
2417edaeb68SThomas Gleixner 	watchdog_update_hrtimer_threshold(sample_period);
24258687acbSDon Zickus }
24358687acbSDon Zickus 
244fef06efcSPetr Mladek static void update_report_ts(void)
245fef06efcSPetr Mladek {
246fef06efcSPetr Mladek 	__this_cpu_write(watchdog_report_ts, get_timestamp());
247fef06efcSPetr Mladek }
248fef06efcSPetr Mladek 
24958687acbSDon Zickus /* Commands for resetting the watchdog */
2507c0012f5SPetr Mladek static void update_touch_ts(void)
25158687acbSDon Zickus {
252c06b4f19SNamhyung Kim 	__this_cpu_write(watchdog_touch_ts, get_timestamp());
253fef06efcSPetr Mladek 	update_report_ts();
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  */
264cb9d7fd5SVincent Whitchurch notrace void touch_softlockup_watchdog_sched(void)
26558687acbSDon Zickus {
2667861144bSAndrew Morton 	/*
267fef06efcSPetr Mladek 	 * Preemption can be enabled.  It doesn't matter which CPU's watchdog
268fef06efcSPetr Mladek 	 * report period gets restarted here, so use the raw_ operation.
2697861144bSAndrew Morton 	 */
270fef06efcSPetr Mladek 	raw_cpu_write(watchdog_report_ts, SOFTLOCKUP_DELAY_REPORT);
27158687acbSDon Zickus }
27203e0d461STejun Heo 
273cb9d7fd5SVincent Whitchurch notrace 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 	 */
29389e28ce6SWang Qing 	for_each_cpu(cpu, &watchdog_allowed_mask) {
294fef06efcSPetr Mladek 		per_cpu(watchdog_report_ts, cpu) = SOFTLOCKUP_DELAY_REPORT;
29589e28ce6SWang Qing 		wq_watchdog_touch(cpu);
29689e28ce6SWang Qing 	}
29758687acbSDon Zickus }
29858687acbSDon Zickus 
29958687acbSDon Zickus void touch_softlockup_watchdog_sync(void)
30058687acbSDon Zickus {
301f7f66b05SChristoph Lameter 	__this_cpu_write(softlockup_touch_sync, true);
302fef06efcSPetr Mladek 	__this_cpu_write(watchdog_report_ts, SOFTLOCKUP_DELAY_REPORT);
30358687acbSDon Zickus }
30458687acbSDon Zickus 
305*0f90b88dSPetr Mladek static int is_softlockup(unsigned long touch_ts,
306*0f90b88dSPetr Mladek 			 unsigned long period_ts,
307*0f90b88dSPetr Mladek 			 unsigned long now)
30858687acbSDon Zickus {
30939d2da21SUlrich Obergfell 	if ((watchdog_enabled & SOFT_WATCHDOG_ENABLED) && watchdog_thresh){
310195daf66SUlrich Obergfell 		/* Warn about unreasonable delays. */
311fef06efcSPetr Mladek 		if (time_after(now, period_ts + get_softlockup_thresh()))
31258687acbSDon Zickus 			return now - touch_ts;
313195daf66SUlrich Obergfell 	}
31458687acbSDon Zickus 	return 0;
31558687acbSDon Zickus }
31658687acbSDon Zickus 
31705a4a952SNicholas Piggin /* watchdog detector functions */
31805a4a952SNicholas Piggin bool is_hardlockup(void)
31905a4a952SNicholas Piggin {
32005a4a952SNicholas Piggin 	unsigned long hrint = __this_cpu_read(hrtimer_interrupts);
32105a4a952SNicholas Piggin 
32205a4a952SNicholas Piggin 	if (__this_cpu_read(hrtimer_interrupts_saved) == hrint)
32305a4a952SNicholas Piggin 		return true;
32405a4a952SNicholas Piggin 
32505a4a952SNicholas Piggin 	__this_cpu_write(hrtimer_interrupts_saved, hrint);
32605a4a952SNicholas Piggin 	return false;
32705a4a952SNicholas Piggin }
32805a4a952SNicholas Piggin 
32958687acbSDon Zickus static void watchdog_interrupt_count(void)
33058687acbSDon Zickus {
331909ea964SChristoph Lameter 	__this_cpu_inc(hrtimer_interrupts);
33258687acbSDon Zickus }
333bcd951cfSThomas Gleixner 
334be45bf53SPeter Zijlstra static DEFINE_PER_CPU(struct completion, softlockup_completion);
335be45bf53SPeter Zijlstra static DEFINE_PER_CPU(struct cpu_stop_work, softlockup_stop_work);
336be45bf53SPeter Zijlstra 
3379cf57731SPeter Zijlstra /*
3389cf57731SPeter Zijlstra  * The watchdog thread function - touches the timestamp.
3399cf57731SPeter Zijlstra  *
3409cf57731SPeter Zijlstra  * It only runs once every sample_period seconds (4 seconds by
3419cf57731SPeter Zijlstra  * default) to reset the softlockup timestamp. If this gets delayed
3429cf57731SPeter Zijlstra  * for more than 2*watchdog_thresh seconds then the debug-printout
3439cf57731SPeter Zijlstra  * triggers in watchdog_timer_fn().
3449cf57731SPeter Zijlstra  */
3459cf57731SPeter Zijlstra static int softlockup_fn(void *data)
3469cf57731SPeter Zijlstra {
3477c0012f5SPetr Mladek 	update_touch_ts();
348be45bf53SPeter Zijlstra 	complete(this_cpu_ptr(&softlockup_completion));
3499cf57731SPeter Zijlstra 
3509cf57731SPeter Zijlstra 	return 0;
3519cf57731SPeter Zijlstra }
3529cf57731SPeter Zijlstra 
35358687acbSDon Zickus /* watchdog kicker functions */
35458687acbSDon Zickus static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer)
35558687acbSDon Zickus {
356*0f90b88dSPetr Mladek 	unsigned long touch_ts, period_ts, now;
35758687acbSDon Zickus 	struct pt_regs *regs = get_irq_regs();
35858687acbSDon Zickus 	int duration;
359ed235875SAaron Tomlin 	int softlockup_all_cpu_backtrace = sysctl_softlockup_all_cpu_backtrace;
36058687acbSDon Zickus 
36101f0a027SThomas Gleixner 	if (!watchdog_enabled)
362b94f5118SDon Zickus 		return HRTIMER_NORESTART;
363b94f5118SDon Zickus 
36458687acbSDon Zickus 	/* kick the hardlockup detector */
36558687acbSDon Zickus 	watchdog_interrupt_count();
36658687acbSDon Zickus 
36758687acbSDon Zickus 	/* kick the softlockup detector */
368be45bf53SPeter Zijlstra 	if (completion_done(this_cpu_ptr(&softlockup_completion))) {
369be45bf53SPeter Zijlstra 		reinit_completion(this_cpu_ptr(&softlockup_completion));
3709cf57731SPeter Zijlstra 		stop_one_cpu_nowait(smp_processor_id(),
3719cf57731SPeter Zijlstra 				softlockup_fn, NULL,
3729cf57731SPeter Zijlstra 				this_cpu_ptr(&softlockup_stop_work));
373be45bf53SPeter Zijlstra 	}
37458687acbSDon Zickus 
37558687acbSDon Zickus 	/* .. and repeat */
3760f34c400SChuansheng Liu 	hrtimer_forward_now(hrtimer, ns_to_ktime(sample_period));
37758687acbSDon Zickus 
3789bf3bc94SPetr Mladek 	/*
379*0f90b88dSPetr Mladek 	 * Read the current timestamp first. It might become invalid anytime
380*0f90b88dSPetr Mladek 	 * when a virtual machine is stopped by the host or when the watchog
381*0f90b88dSPetr Mladek 	 * is touched from NMI.
382*0f90b88dSPetr Mladek 	 */
383*0f90b88dSPetr Mladek 	now = get_timestamp();
384*0f90b88dSPetr Mladek 	/*
3859bf3bc94SPetr Mladek 	 * If a virtual machine is stopped by the host it can look to
386*0f90b88dSPetr Mladek 	 * the watchdog like a soft lockup. This function touches the watchdog.
3879bf3bc94SPetr Mladek 	 */
3889bf3bc94SPetr Mladek 	kvm_check_and_clear_guest_paused();
389*0f90b88dSPetr Mladek 	/*
390*0f90b88dSPetr Mladek 	 * The stored timestamp is comparable with @now only when not touched.
391*0f90b88dSPetr Mladek 	 * It might get touched anytime from NMI. Make sure that is_softlockup()
392*0f90b88dSPetr Mladek 	 * uses the same (valid) value.
393*0f90b88dSPetr Mladek 	 */
394*0f90b88dSPetr Mladek 	period_ts = READ_ONCE(*this_cpu_ptr(&watchdog_report_ts));
3959bf3bc94SPetr Mladek 
3969bf3bc94SPetr Mladek 	/* Reset the interval when touched by known problematic code. */
397fef06efcSPetr Mladek 	if (period_ts == SOFTLOCKUP_DELAY_REPORT) {
398909ea964SChristoph Lameter 		if (unlikely(__this_cpu_read(softlockup_touch_sync))) {
39958687acbSDon Zickus 			/*
40058687acbSDon Zickus 			 * If the time stamp was touched atomically
40158687acbSDon Zickus 			 * make sure the scheduler tick is up to date.
40258687acbSDon Zickus 			 */
403909ea964SChristoph Lameter 			__this_cpu_write(softlockup_touch_sync, false);
40458687acbSDon Zickus 			sched_clock_tick();
40558687acbSDon Zickus 		}
4065d1c0f4aSEric B Munson 
407fef06efcSPetr Mladek 		update_report_ts();
40858687acbSDon Zickus 		return HRTIMER_RESTART;
40958687acbSDon Zickus 	}
41058687acbSDon Zickus 
411*0f90b88dSPetr Mladek 	/* Check for a softlockup. */
412*0f90b88dSPetr Mladek 	touch_ts = __this_cpu_read(watchdog_touch_ts);
413*0f90b88dSPetr Mladek 	duration = is_softlockup(touch_ts, period_ts, now);
41458687acbSDon Zickus 	if (unlikely(duration)) {
4155d1c0f4aSEric B Munson 		/*
4169f113bf7SPetr Mladek 		 * Prevent multiple soft-lockup reports if one cpu is already
4179f113bf7SPetr Mladek 		 * engaged in dumping all cpu back traces.
418ed235875SAaron Tomlin 		 */
4199f113bf7SPetr Mladek 		if (softlockup_all_cpu_backtrace) {
4209f113bf7SPetr Mladek 			if (test_and_set_bit_lock(0, &soft_lockup_nmi_warn))
421ed235875SAaron Tomlin 				return HRTIMER_RESTART;
422ed235875SAaron Tomlin 		}
423ed235875SAaron Tomlin 
424c9ad17c9SPetr Mladek 		/* Start period for the next softlockup warning. */
425fef06efcSPetr Mladek 		update_report_ts();
426c9ad17c9SPetr Mladek 
427656c3b79SFabian Frederick 		pr_emerg("BUG: soft lockup - CPU#%d stuck for %us! [%s:%d]\n",
42826e09c6eSDon Zickus 			smp_processor_id(), duration,
42958687acbSDon Zickus 			current->comm, task_pid_nr(current));
43058687acbSDon Zickus 		print_modules();
43158687acbSDon Zickus 		print_irqtrace_events(current);
43258687acbSDon Zickus 		if (regs)
43358687acbSDon Zickus 			show_regs(regs);
43458687acbSDon Zickus 		else
43558687acbSDon Zickus 			dump_stack();
43658687acbSDon Zickus 
437ed235875SAaron Tomlin 		if (softlockup_all_cpu_backtrace) {
438ed235875SAaron Tomlin 			trigger_allbutself_cpu_backtrace();
4399f113bf7SPetr Mladek 			clear_bit_unlock(0, &soft_lockup_nmi_warn);
440ed235875SAaron Tomlin 		}
441ed235875SAaron Tomlin 
44269361eefSJosh Hunt 		add_taint(TAINT_SOFTLOCKUP, LOCKDEP_STILL_OK);
44358687acbSDon Zickus 		if (softlockup_panic)
44458687acbSDon Zickus 			panic("softlockup: hung tasks");
4451bc503cbSPetr Mladek 	}
44658687acbSDon Zickus 
44758687acbSDon Zickus 	return HRTIMER_RESTART;
44858687acbSDon Zickus }
44958687acbSDon Zickus 
450bcd951cfSThomas Gleixner static void watchdog_enable(unsigned int cpu)
451bcd951cfSThomas Gleixner {
45201f0a027SThomas Gleixner 	struct hrtimer *hrtimer = this_cpu_ptr(&watchdog_hrtimer);
453be45bf53SPeter Zijlstra 	struct completion *done = this_cpu_ptr(&softlockup_completion);
45458687acbSDon Zickus 
4559cf57731SPeter Zijlstra 	WARN_ON_ONCE(cpu != smp_processor_id());
4569cf57731SPeter Zijlstra 
457be45bf53SPeter Zijlstra 	init_completion(done);
458be45bf53SPeter Zijlstra 	complete(done);
459be45bf53SPeter Zijlstra 
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 	 */
464d2ab4cf4SSebastian Andrzej Siewior 	hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
4653935e895SBjørn Mork 	hrtimer->function = watchdog_timer_fn;
4660f34c400SChuansheng Liu 	hrtimer_start(hrtimer, ns_to_ktime(sample_period),
467d2ab4cf4SSebastian Andrzej Siewior 		      HRTIMER_MODE_REL_PINNED_HARD);
46858687acbSDon Zickus 
46901f0a027SThomas Gleixner 	/* Initialize timestamp */
4707c0012f5SPetr Mladek 	update_touch_ts();
47101f0a027SThomas Gleixner 	/* Enable the perf event */
472146c9d0eSThomas Gleixner 	if (watchdog_enabled & NMI_WATCHDOG_ENABLED)
47301f0a027SThomas Gleixner 		watchdog_nmi_enable(cpu);
47458687acbSDon Zickus }
475bcd951cfSThomas Gleixner 
476bcd951cfSThomas Gleixner static void watchdog_disable(unsigned int cpu)
477bcd951cfSThomas Gleixner {
47801f0a027SThomas Gleixner 	struct hrtimer *hrtimer = this_cpu_ptr(&watchdog_hrtimer);
479bcd951cfSThomas Gleixner 
4809cf57731SPeter Zijlstra 	WARN_ON_ONCE(cpu != smp_processor_id());
4819cf57731SPeter Zijlstra 
48201f0a027SThomas Gleixner 	/*
48301f0a027SThomas Gleixner 	 * Disable the perf event first. That prevents that a large delay
48401f0a027SThomas Gleixner 	 * between disabling the timer and disabling the perf event causes
48501f0a027SThomas Gleixner 	 * the perf NMI to detect a false positive.
48601f0a027SThomas Gleixner 	 */
487bcd951cfSThomas Gleixner 	watchdog_nmi_disable(cpu);
48801f0a027SThomas Gleixner 	hrtimer_cancel(hrtimer);
489be45bf53SPeter Zijlstra 	wait_for_completion(this_cpu_ptr(&softlockup_completion));
490bcd951cfSThomas Gleixner }
491bcd951cfSThomas Gleixner 
4929cf57731SPeter Zijlstra static int softlockup_stop_fn(void *data)
493b8900bc0SFrederic Weisbecker {
4949cf57731SPeter Zijlstra 	watchdog_disable(smp_processor_id());
4959cf57731SPeter Zijlstra 	return 0;
496b8900bc0SFrederic Weisbecker }
497b8900bc0SFrederic Weisbecker 
4989cf57731SPeter Zijlstra static void softlockup_stop_all(void)
499bcd951cfSThomas Gleixner {
5009cf57731SPeter Zijlstra 	int cpu;
501bcd951cfSThomas Gleixner 
5029cf57731SPeter Zijlstra 	if (!softlockup_initialized)
5032eb2527fSThomas Gleixner 		return;
5042eb2527fSThomas Gleixner 
5059cf57731SPeter Zijlstra 	for_each_cpu(cpu, &watchdog_allowed_mask)
5069cf57731SPeter Zijlstra 		smp_call_on_cpu(cpu, softlockup_stop_fn, NULL, false);
5072eb2527fSThomas Gleixner 
5082eb2527fSThomas Gleixner 	cpumask_clear(&watchdog_allowed_mask);
5092eb2527fSThomas Gleixner }
5102eb2527fSThomas Gleixner 
5119cf57731SPeter Zijlstra static int softlockup_start_fn(void *data)
5122eb2527fSThomas Gleixner {
5139cf57731SPeter Zijlstra 	watchdog_enable(smp_processor_id());
5149cf57731SPeter Zijlstra 	return 0;
5159cf57731SPeter Zijlstra }
5169cf57731SPeter Zijlstra 
5179cf57731SPeter Zijlstra static void softlockup_start_all(void)
5189cf57731SPeter Zijlstra {
5199cf57731SPeter Zijlstra 	int cpu;
5209cf57731SPeter Zijlstra 
5212eb2527fSThomas Gleixner 	cpumask_copy(&watchdog_allowed_mask, &watchdog_cpumask);
5229cf57731SPeter Zijlstra 	for_each_cpu(cpu, &watchdog_allowed_mask)
5239cf57731SPeter Zijlstra 		smp_call_on_cpu(cpu, softlockup_start_fn, NULL, false);
5249cf57731SPeter Zijlstra }
5259cf57731SPeter Zijlstra 
5269cf57731SPeter Zijlstra int lockup_detector_online_cpu(unsigned int cpu)
5279cf57731SPeter Zijlstra {
5287dd47617SThomas Gleixner 	if (cpumask_test_cpu(cpu, &watchdog_allowed_mask))
5299cf57731SPeter Zijlstra 		watchdog_enable(cpu);
5309cf57731SPeter Zijlstra 	return 0;
5319cf57731SPeter Zijlstra }
5329cf57731SPeter Zijlstra 
5339cf57731SPeter Zijlstra int lockup_detector_offline_cpu(unsigned int cpu)
5349cf57731SPeter Zijlstra {
5357dd47617SThomas Gleixner 	if (cpumask_test_cpu(cpu, &watchdog_allowed_mask))
5369cf57731SPeter Zijlstra 		watchdog_disable(cpu);
5379cf57731SPeter Zijlstra 	return 0;
5382eb2527fSThomas Gleixner }
5392eb2527fSThomas Gleixner 
5405587185dSThomas Gleixner static void lockup_detector_reconfigure(void)
5412eb2527fSThomas Gleixner {
542e31d6883SThomas Gleixner 	cpus_read_lock();
5436b9dc480SThomas Gleixner 	watchdog_nmi_stop();
5449cf57731SPeter Zijlstra 
5459cf57731SPeter Zijlstra 	softlockup_stop_all();
5462eb2527fSThomas Gleixner 	set_sample_period();
54709154985SThomas Gleixner 	lockup_detector_update_enable();
54809154985SThomas Gleixner 	if (watchdog_enabled && watchdog_thresh)
5499cf57731SPeter Zijlstra 		softlockup_start_all();
5509cf57731SPeter Zijlstra 
5516b9dc480SThomas Gleixner 	watchdog_nmi_start();
552e31d6883SThomas Gleixner 	cpus_read_unlock();
553e31d6883SThomas Gleixner 	/*
554e31d6883SThomas Gleixner 	 * Must be called outside the cpus locked section to prevent
555e31d6883SThomas Gleixner 	 * recursive locking in the perf code.
556e31d6883SThomas Gleixner 	 */
557e31d6883SThomas Gleixner 	__lockup_detector_cleanup();
5582eb2527fSThomas Gleixner }
5592eb2527fSThomas Gleixner 
5602eb2527fSThomas Gleixner /*
5615587185dSThomas Gleixner  * Create the watchdog thread infrastructure and configure the detector(s).
5622eb2527fSThomas Gleixner  *
5632eb2527fSThomas Gleixner  * The threads are not unparked as watchdog_allowed_mask is empty.  When
56476e15524SArash Fotouhi  * the threads are successfully initialized, take the proper locks and
5652eb2527fSThomas Gleixner  * unpark the threads in the watchdog_cpumask if the watchdog is enabled.
5662eb2527fSThomas Gleixner  */
5675587185dSThomas Gleixner static __init void lockup_detector_setup(void)
5682eb2527fSThomas Gleixner {
5692eb2527fSThomas Gleixner 	/*
5702eb2527fSThomas Gleixner 	 * If sysctl is off and watchdog got disabled on the command line,
5712eb2527fSThomas Gleixner 	 * nothing to do here.
5722eb2527fSThomas Gleixner 	 */
57309154985SThomas Gleixner 	lockup_detector_update_enable();
57409154985SThomas Gleixner 
5752eb2527fSThomas Gleixner 	if (!IS_ENABLED(CONFIG_SYSCTL) &&
5762eb2527fSThomas Gleixner 	    !(watchdog_enabled && watchdog_thresh))
5772eb2527fSThomas Gleixner 		return;
5782eb2527fSThomas Gleixner 
5792eb2527fSThomas Gleixner 	mutex_lock(&watchdog_mutex);
5805587185dSThomas Gleixner 	lockup_detector_reconfigure();
5819cf57731SPeter Zijlstra 	softlockup_initialized = true;
5822eb2527fSThomas Gleixner 	mutex_unlock(&watchdog_mutex);
5832eb2527fSThomas Gleixner }
5842eb2527fSThomas Gleixner 
5852b9d7f23SThomas Gleixner #else /* CONFIG_SOFTLOCKUP_DETECTOR */
5865587185dSThomas Gleixner static void lockup_detector_reconfigure(void)
5876592ad2fSThomas Gleixner {
588e31d6883SThomas Gleixner 	cpus_read_lock();
5896b9dc480SThomas Gleixner 	watchdog_nmi_stop();
59009154985SThomas Gleixner 	lockup_detector_update_enable();
5916b9dc480SThomas Gleixner 	watchdog_nmi_start();
592e31d6883SThomas Gleixner 	cpus_read_unlock();
5936592ad2fSThomas Gleixner }
5945587185dSThomas Gleixner static inline void lockup_detector_setup(void)
59534ddaa3eSThomas Gleixner {
5965587185dSThomas Gleixner 	lockup_detector_reconfigure();
59734ddaa3eSThomas Gleixner }
5982b9d7f23SThomas Gleixner #endif /* !CONFIG_SOFTLOCKUP_DETECTOR */
59905a4a952SNicholas Piggin 
600941154bdSThomas Gleixner static void __lockup_detector_cleanup(void)
601941154bdSThomas Gleixner {
602941154bdSThomas Gleixner 	lockdep_assert_held(&watchdog_mutex);
603941154bdSThomas Gleixner 	hardlockup_detector_perf_cleanup();
604941154bdSThomas Gleixner }
605941154bdSThomas Gleixner 
606941154bdSThomas Gleixner /**
607941154bdSThomas Gleixner  * lockup_detector_cleanup - Cleanup after cpu hotplug or sysctl changes
608941154bdSThomas Gleixner  *
609941154bdSThomas Gleixner  * Caller must not hold the cpu hotplug rwsem.
610941154bdSThomas Gleixner  */
611941154bdSThomas Gleixner void lockup_detector_cleanup(void)
612941154bdSThomas Gleixner {
613941154bdSThomas Gleixner 	mutex_lock(&watchdog_mutex);
614941154bdSThomas Gleixner 	__lockup_detector_cleanup();
615941154bdSThomas Gleixner 	mutex_unlock(&watchdog_mutex);
616941154bdSThomas Gleixner }
617941154bdSThomas Gleixner 
6186554fd8cSThomas Gleixner /**
6196554fd8cSThomas Gleixner  * lockup_detector_soft_poweroff - Interface to stop lockup detector(s)
6206554fd8cSThomas Gleixner  *
6216554fd8cSThomas Gleixner  * Special interface for parisc. It prevents lockup detector warnings from
6226554fd8cSThomas Gleixner  * the default pm_poweroff() function which busy loops forever.
6236554fd8cSThomas Gleixner  */
6246554fd8cSThomas Gleixner void lockup_detector_soft_poweroff(void)
6256554fd8cSThomas Gleixner {
6266554fd8cSThomas Gleixner 	watchdog_enabled = 0;
6276554fd8cSThomas Gleixner }
6286554fd8cSThomas Gleixner 
62958cf690aSUlrich Obergfell #ifdef CONFIG_SYSCTL
63058cf690aSUlrich Obergfell 
631e8b62b2dSThomas Gleixner /* Propagate any changes to the watchdog threads */
632d57108d4SThomas Gleixner static void proc_watchdog_update(void)
63358687acbSDon Zickus {
634e8b62b2dSThomas Gleixner 	/* Remove impossible cpus to keep sysctl output clean. */
635e8b62b2dSThomas Gleixner 	cpumask_and(&watchdog_cpumask, &watchdog_cpumask, cpu_possible_mask);
6365587185dSThomas Gleixner 	lockup_detector_reconfigure();
637a0c9cbb9SUlrich Obergfell }
638a0c9cbb9SUlrich Obergfell 
639a0c9cbb9SUlrich Obergfell /*
640ef246a21SUlrich Obergfell  * common function for watchdog, nmi_watchdog and soft_watchdog parameter
641ef246a21SUlrich Obergfell  *
6427feeb9cdSThomas Gleixner  * caller             | table->data points to      | 'which'
6437feeb9cdSThomas Gleixner  * -------------------|----------------------------|--------------------------
6447feeb9cdSThomas Gleixner  * proc_watchdog      | watchdog_user_enabled      | NMI_WATCHDOG_ENABLED |
6457feeb9cdSThomas Gleixner  *                    |                            | SOFT_WATCHDOG_ENABLED
6467feeb9cdSThomas Gleixner  * -------------------|----------------------------|--------------------------
6477feeb9cdSThomas Gleixner  * proc_nmi_watchdog  | nmi_watchdog_user_enabled  | NMI_WATCHDOG_ENABLED
6487feeb9cdSThomas Gleixner  * -------------------|----------------------------|--------------------------
6497feeb9cdSThomas Gleixner  * proc_soft_watchdog | soft_watchdog_user_enabled | SOFT_WATCHDOG_ENABLED
650ef246a21SUlrich Obergfell  */
651ef246a21SUlrich Obergfell static int proc_watchdog_common(int which, struct ctl_table *table, int write,
65232927393SChristoph Hellwig 				void *buffer, size_t *lenp, loff_t *ppos)
653ef246a21SUlrich Obergfell {
65409154985SThomas Gleixner 	int err, old, *param = table->data;
655bcd951cfSThomas Gleixner 
656946d1977SThomas Gleixner 	mutex_lock(&watchdog_mutex);
657ef246a21SUlrich Obergfell 
658ef246a21SUlrich Obergfell 	if (!write) {
65909154985SThomas Gleixner 		/*
66009154985SThomas Gleixner 		 * On read synchronize the userspace interface. This is a
66109154985SThomas Gleixner 		 * racy snapshot.
66209154985SThomas Gleixner 		 */
66309154985SThomas Gleixner 		*param = (watchdog_enabled & which) != 0;
664b8900bc0SFrederic Weisbecker 		err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
665ef246a21SUlrich Obergfell 	} else {
66609154985SThomas Gleixner 		old = READ_ONCE(*param);
667ef246a21SUlrich Obergfell 		err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
66809154985SThomas Gleixner 		if (!err && old != READ_ONCE(*param))
669d57108d4SThomas Gleixner 			proc_watchdog_update();
670ef246a21SUlrich Obergfell 	}
671946d1977SThomas Gleixner 	mutex_unlock(&watchdog_mutex);
672ef246a21SUlrich Obergfell 	return err;
673ef246a21SUlrich Obergfell }
674ef246a21SUlrich Obergfell 
675ef246a21SUlrich Obergfell /*
67683a80a39SUlrich Obergfell  * /proc/sys/kernel/watchdog
67783a80a39SUlrich Obergfell  */
67883a80a39SUlrich Obergfell int proc_watchdog(struct ctl_table *table, int write,
67932927393SChristoph Hellwig 		  void *buffer, size_t *lenp, loff_t *ppos)
68083a80a39SUlrich Obergfell {
68183a80a39SUlrich Obergfell 	return proc_watchdog_common(NMI_WATCHDOG_ENABLED|SOFT_WATCHDOG_ENABLED,
68283a80a39SUlrich Obergfell 				    table, write, buffer, lenp, ppos);
68383a80a39SUlrich Obergfell }
68483a80a39SUlrich Obergfell 
68583a80a39SUlrich Obergfell /*
68683a80a39SUlrich Obergfell  * /proc/sys/kernel/nmi_watchdog
68783a80a39SUlrich Obergfell  */
68883a80a39SUlrich Obergfell int proc_nmi_watchdog(struct ctl_table *table, int write,
68932927393SChristoph Hellwig 		      void *buffer, size_t *lenp, loff_t *ppos)
69083a80a39SUlrich Obergfell {
691a994a314SThomas Gleixner 	if (!nmi_watchdog_available && write)
692a994a314SThomas Gleixner 		return -ENOTSUPP;
69383a80a39SUlrich Obergfell 	return proc_watchdog_common(NMI_WATCHDOG_ENABLED,
69483a80a39SUlrich Obergfell 				    table, write, buffer, lenp, ppos);
69583a80a39SUlrich Obergfell }
69683a80a39SUlrich Obergfell 
69783a80a39SUlrich Obergfell /*
69883a80a39SUlrich Obergfell  * /proc/sys/kernel/soft_watchdog
69983a80a39SUlrich Obergfell  */
70083a80a39SUlrich Obergfell int proc_soft_watchdog(struct ctl_table *table, int write,
70132927393SChristoph Hellwig 			void *buffer, size_t *lenp, loff_t *ppos)
70283a80a39SUlrich Obergfell {
70383a80a39SUlrich Obergfell 	return proc_watchdog_common(SOFT_WATCHDOG_ENABLED,
70483a80a39SUlrich Obergfell 				    table, write, buffer, lenp, ppos);
70583a80a39SUlrich Obergfell }
70683a80a39SUlrich Obergfell 
70783a80a39SUlrich Obergfell /*
70883a80a39SUlrich Obergfell  * /proc/sys/kernel/watchdog_thresh
70983a80a39SUlrich Obergfell  */
71083a80a39SUlrich Obergfell int proc_watchdog_thresh(struct ctl_table *table, int write,
71132927393SChristoph Hellwig 			 void *buffer, size_t *lenp, loff_t *ppos)
71283a80a39SUlrich Obergfell {
713d57108d4SThomas Gleixner 	int err, old;
71483a80a39SUlrich Obergfell 
715946d1977SThomas Gleixner 	mutex_lock(&watchdog_mutex);
71683a80a39SUlrich Obergfell 
717d57108d4SThomas Gleixner 	old = READ_ONCE(watchdog_thresh);
71883a80a39SUlrich Obergfell 	err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
71983a80a39SUlrich Obergfell 
720d57108d4SThomas Gleixner 	if (!err && write && old != READ_ONCE(watchdog_thresh))
721d57108d4SThomas Gleixner 		proc_watchdog_update();
722e04ab2bcSMandeep Singh Baines 
723946d1977SThomas Gleixner 	mutex_unlock(&watchdog_mutex);
724b8900bc0SFrederic Weisbecker 	return err;
72558687acbSDon Zickus }
726fe4ba3c3SChris Metcalf 
727fe4ba3c3SChris Metcalf /*
728fe4ba3c3SChris Metcalf  * The cpumask is the mask of possible cpus that the watchdog can run
729fe4ba3c3SChris Metcalf  * on, not the mask of cpus it is actually running on.  This allows the
730fe4ba3c3SChris Metcalf  * user to specify a mask that will include cpus that have not yet
731fe4ba3c3SChris Metcalf  * been brought online, if desired.
732fe4ba3c3SChris Metcalf  */
733fe4ba3c3SChris Metcalf int proc_watchdog_cpumask(struct ctl_table *table, int write,
73432927393SChristoph Hellwig 			  void *buffer, size_t *lenp, loff_t *ppos)
735fe4ba3c3SChris Metcalf {
736fe4ba3c3SChris Metcalf 	int err;
737fe4ba3c3SChris Metcalf 
738946d1977SThomas Gleixner 	mutex_lock(&watchdog_mutex);
7398c073d27SUlrich Obergfell 
740fe4ba3c3SChris Metcalf 	err = proc_do_large_bitmap(table, write, buffer, lenp, ppos);
74105ba3de7SThomas Gleixner 	if (!err && write)
742e8b62b2dSThomas Gleixner 		proc_watchdog_update();
7435490125dSThomas Gleixner 
744946d1977SThomas Gleixner 	mutex_unlock(&watchdog_mutex);
745fe4ba3c3SChris Metcalf 	return err;
746fe4ba3c3SChris Metcalf }
74758687acbSDon Zickus #endif /* CONFIG_SYSCTL */
74858687acbSDon Zickus 
749004417a6SPeter Zijlstra void __init lockup_detector_init(void)
75058687acbSDon Zickus {
75113316b31SFrederic Weisbecker 	if (tick_nohz_full_enabled())
752fe4ba3c3SChris Metcalf 		pr_info("Disabling watchdog on nohz_full cores by default\n");
75313316b31SFrederic Weisbecker 
754de201559SFrederic Weisbecker 	cpumask_copy(&watchdog_cpumask,
755de201559SFrederic Weisbecker 		     housekeeping_cpumask(HK_FLAG_TIMER));
756fe4ba3c3SChris Metcalf 
757a994a314SThomas Gleixner 	if (!watchdog_nmi_probe())
758a994a314SThomas Gleixner 		nmi_watchdog_available = true;
7595587185dSThomas Gleixner 	lockup_detector_setup();
76058687acbSDon Zickus }
761