xref: /OK3568_Linux_fs/kernel/drivers/watchdog/softdog.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  *	SoftDog:	A Software Watchdog Device
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  *	(c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>,
6*4882a593Smuzhiyun  *							All Rights Reserved.
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  *	Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
9*4882a593Smuzhiyun  *	warranty for any of this software. This material is provided
10*4882a593Smuzhiyun  *	"AS-IS" and at no charge.
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  *	(c) Copyright 1995    Alan Cox <alan@lxorguk.ukuu.org.uk>
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  *	Software only watchdog driver. Unlike its big brother the WDT501P
15*4882a593Smuzhiyun  *	driver this won't always recover a failed machine.
16*4882a593Smuzhiyun  */
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun #include <linux/hrtimer.h>
21*4882a593Smuzhiyun #include <linux/init.h>
22*4882a593Smuzhiyun #include <linux/kernel.h>
23*4882a593Smuzhiyun #include <linux/kthread.h>
24*4882a593Smuzhiyun #include <linux/module.h>
25*4882a593Smuzhiyun #include <linux/moduleparam.h>
26*4882a593Smuzhiyun #include <linux/reboot.h>
27*4882a593Smuzhiyun #include <linux/types.h>
28*4882a593Smuzhiyun #include <linux/watchdog.h>
29*4882a593Smuzhiyun #include <linux/workqueue.h>
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun #define TIMER_MARGIN	60		/* Default is 60 seconds */
32*4882a593Smuzhiyun static unsigned int soft_margin = TIMER_MARGIN;	/* in seconds */
33*4882a593Smuzhiyun module_param(soft_margin, uint, 0);
34*4882a593Smuzhiyun MODULE_PARM_DESC(soft_margin,
35*4882a593Smuzhiyun 	"Watchdog soft_margin in seconds. (0 < soft_margin < 65536, default="
36*4882a593Smuzhiyun 					__MODULE_STRING(TIMER_MARGIN) ")");
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun static bool nowayout = WATCHDOG_NOWAYOUT;
39*4882a593Smuzhiyun module_param(nowayout, bool, 0);
40*4882a593Smuzhiyun MODULE_PARM_DESC(nowayout,
41*4882a593Smuzhiyun 		"Watchdog cannot be stopped once started (default="
42*4882a593Smuzhiyun 				__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun static int soft_noboot;
45*4882a593Smuzhiyun module_param(soft_noboot, int, 0);
46*4882a593Smuzhiyun MODULE_PARM_DESC(soft_noboot,
47*4882a593Smuzhiyun 	"Softdog action, set to 1 to ignore reboots, 0 to reboot (default=0)");
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun static int soft_panic;
50*4882a593Smuzhiyun module_param(soft_panic, int, 0);
51*4882a593Smuzhiyun MODULE_PARM_DESC(soft_panic,
52*4882a593Smuzhiyun 	"Softdog action, set to 1 to panic, 0 to reboot (default=0)");
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun static char *soft_reboot_cmd;
55*4882a593Smuzhiyun module_param(soft_reboot_cmd, charp, 0000);
56*4882a593Smuzhiyun MODULE_PARM_DESC(soft_reboot_cmd,
57*4882a593Smuzhiyun 	"Set reboot command. Emergency reboot takes place if unset");
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun static bool soft_active_on_boot;
60*4882a593Smuzhiyun module_param(soft_active_on_boot, bool, 0000);
61*4882a593Smuzhiyun MODULE_PARM_DESC(soft_active_on_boot,
62*4882a593Smuzhiyun 	"Set to true to active Softdog on boot (default=false)");
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun static struct hrtimer softdog_ticktock;
65*4882a593Smuzhiyun static struct hrtimer softdog_preticktock;
66*4882a593Smuzhiyun 
reboot_kthread_fn(void * data)67*4882a593Smuzhiyun static int reboot_kthread_fn(void *data)
68*4882a593Smuzhiyun {
69*4882a593Smuzhiyun 	kernel_restart(soft_reboot_cmd);
70*4882a593Smuzhiyun 	return -EPERM; /* Should not reach here */
71*4882a593Smuzhiyun }
72*4882a593Smuzhiyun 
reboot_work_fn(struct work_struct * unused)73*4882a593Smuzhiyun static void reboot_work_fn(struct work_struct *unused)
74*4882a593Smuzhiyun {
75*4882a593Smuzhiyun 	kthread_run(reboot_kthread_fn, NULL, "softdog_reboot");
76*4882a593Smuzhiyun }
77*4882a593Smuzhiyun 
softdog_fire(struct hrtimer * timer)78*4882a593Smuzhiyun static enum hrtimer_restart softdog_fire(struct hrtimer *timer)
79*4882a593Smuzhiyun {
80*4882a593Smuzhiyun 	static bool soft_reboot_fired;
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	module_put(THIS_MODULE);
83*4882a593Smuzhiyun 	if (soft_noboot) {
84*4882a593Smuzhiyun 		pr_crit("Triggered - Reboot ignored\n");
85*4882a593Smuzhiyun 	} else if (soft_panic) {
86*4882a593Smuzhiyun 		pr_crit("Initiating panic\n");
87*4882a593Smuzhiyun 		panic("Software Watchdog Timer expired");
88*4882a593Smuzhiyun 	} else {
89*4882a593Smuzhiyun 		pr_crit("Initiating system reboot\n");
90*4882a593Smuzhiyun 		if (!soft_reboot_fired && soft_reboot_cmd != NULL) {
91*4882a593Smuzhiyun 			static DECLARE_WORK(reboot_work, reboot_work_fn);
92*4882a593Smuzhiyun 			/*
93*4882a593Smuzhiyun 			 * The 'kernel_restart' is a 'might-sleep' operation.
94*4882a593Smuzhiyun 			 * Also, executing it in system-wide workqueues blocks
95*4882a593Smuzhiyun 			 * any driver from using the same workqueue in its
96*4882a593Smuzhiyun 			 * shutdown callback function. Thus, we should execute
97*4882a593Smuzhiyun 			 * the 'kernel_restart' in a standalone kernel thread.
98*4882a593Smuzhiyun 			 * But since starting a kernel thread is also a
99*4882a593Smuzhiyun 			 * 'might-sleep' operation, so the 'reboot_work' is
100*4882a593Smuzhiyun 			 * required as a launcher of the kernel thread.
101*4882a593Smuzhiyun 			 *
102*4882a593Smuzhiyun 			 * After request the reboot, restart the timer to
103*4882a593Smuzhiyun 			 * schedule an 'emergency_restart' reboot after
104*4882a593Smuzhiyun 			 * 'TIMER_MARGIN' seconds. It's because if the softdog
105*4882a593Smuzhiyun 			 * hangs, it might be because of scheduling issues. And
106*4882a593Smuzhiyun 			 * if that is the case, both 'schedule_work' and
107*4882a593Smuzhiyun 			 * 'kernel_restart' may possibly be malfunctional at the
108*4882a593Smuzhiyun 			 * same time.
109*4882a593Smuzhiyun 			 */
110*4882a593Smuzhiyun 			soft_reboot_fired = true;
111*4882a593Smuzhiyun 			schedule_work(&reboot_work);
112*4882a593Smuzhiyun 			hrtimer_add_expires_ns(timer,
113*4882a593Smuzhiyun 					(u64)TIMER_MARGIN * NSEC_PER_SEC);
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 			return HRTIMER_RESTART;
116*4882a593Smuzhiyun 		}
117*4882a593Smuzhiyun 		emergency_restart();
118*4882a593Smuzhiyun 		pr_crit("Reboot didn't ?????\n");
119*4882a593Smuzhiyun 	}
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	return HRTIMER_NORESTART;
122*4882a593Smuzhiyun }
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun static struct watchdog_device softdog_dev;
125*4882a593Smuzhiyun 
softdog_pretimeout(struct hrtimer * timer)126*4882a593Smuzhiyun static enum hrtimer_restart softdog_pretimeout(struct hrtimer *timer)
127*4882a593Smuzhiyun {
128*4882a593Smuzhiyun 	watchdog_notify_pretimeout(&softdog_dev);
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun 	return HRTIMER_NORESTART;
131*4882a593Smuzhiyun }
132*4882a593Smuzhiyun 
softdog_ping(struct watchdog_device * w)133*4882a593Smuzhiyun static int softdog_ping(struct watchdog_device *w)
134*4882a593Smuzhiyun {
135*4882a593Smuzhiyun 	if (!hrtimer_active(&softdog_ticktock))
136*4882a593Smuzhiyun 		__module_get(THIS_MODULE);
137*4882a593Smuzhiyun 	hrtimer_start(&softdog_ticktock, ktime_set(w->timeout, 0),
138*4882a593Smuzhiyun 		      HRTIMER_MODE_REL);
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun 	if (IS_ENABLED(CONFIG_SOFT_WATCHDOG_PRETIMEOUT)) {
141*4882a593Smuzhiyun 		if (w->pretimeout)
142*4882a593Smuzhiyun 			hrtimer_start(&softdog_preticktock,
143*4882a593Smuzhiyun 				      ktime_set(w->timeout - w->pretimeout, 0),
144*4882a593Smuzhiyun 				      HRTIMER_MODE_REL);
145*4882a593Smuzhiyun 		else
146*4882a593Smuzhiyun 			hrtimer_cancel(&softdog_preticktock);
147*4882a593Smuzhiyun 	}
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 	return 0;
150*4882a593Smuzhiyun }
151*4882a593Smuzhiyun 
softdog_stop(struct watchdog_device * w)152*4882a593Smuzhiyun static int softdog_stop(struct watchdog_device *w)
153*4882a593Smuzhiyun {
154*4882a593Smuzhiyun 	if (hrtimer_cancel(&softdog_ticktock))
155*4882a593Smuzhiyun 		module_put(THIS_MODULE);
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun 	if (IS_ENABLED(CONFIG_SOFT_WATCHDOG_PRETIMEOUT))
158*4882a593Smuzhiyun 		hrtimer_cancel(&softdog_preticktock);
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	return 0;
161*4882a593Smuzhiyun }
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun static struct watchdog_info softdog_info = {
164*4882a593Smuzhiyun 	.identity = "Software Watchdog",
165*4882a593Smuzhiyun 	.options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
166*4882a593Smuzhiyun };
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun static const struct watchdog_ops softdog_ops = {
169*4882a593Smuzhiyun 	.owner = THIS_MODULE,
170*4882a593Smuzhiyun 	.start = softdog_ping,
171*4882a593Smuzhiyun 	.stop = softdog_stop,
172*4882a593Smuzhiyun };
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun static struct watchdog_device softdog_dev = {
175*4882a593Smuzhiyun 	.info = &softdog_info,
176*4882a593Smuzhiyun 	.ops = &softdog_ops,
177*4882a593Smuzhiyun 	.min_timeout = 1,
178*4882a593Smuzhiyun 	.max_timeout = 65535,
179*4882a593Smuzhiyun 	.timeout = TIMER_MARGIN,
180*4882a593Smuzhiyun };
181*4882a593Smuzhiyun 
softdog_init(void)182*4882a593Smuzhiyun static int __init softdog_init(void)
183*4882a593Smuzhiyun {
184*4882a593Smuzhiyun 	int ret;
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 	watchdog_init_timeout(&softdog_dev, soft_margin, NULL);
187*4882a593Smuzhiyun 	watchdog_set_nowayout(&softdog_dev, nowayout);
188*4882a593Smuzhiyun 	watchdog_stop_on_reboot(&softdog_dev);
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun 	hrtimer_init(&softdog_ticktock, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
191*4882a593Smuzhiyun 	softdog_ticktock.function = softdog_fire;
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 	if (IS_ENABLED(CONFIG_SOFT_WATCHDOG_PRETIMEOUT)) {
194*4882a593Smuzhiyun 		softdog_info.options |= WDIOF_PRETIMEOUT;
195*4882a593Smuzhiyun 		hrtimer_init(&softdog_preticktock, CLOCK_MONOTONIC,
196*4882a593Smuzhiyun 			     HRTIMER_MODE_REL);
197*4882a593Smuzhiyun 		softdog_preticktock.function = softdog_pretimeout;
198*4882a593Smuzhiyun 	}
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun 	if (soft_active_on_boot)
201*4882a593Smuzhiyun 		softdog_ping(&softdog_dev);
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun 	ret = watchdog_register_device(&softdog_dev);
204*4882a593Smuzhiyun 	if (ret)
205*4882a593Smuzhiyun 		return ret;
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 	pr_info("initialized. soft_noboot=%d soft_margin=%d sec soft_panic=%d (nowayout=%d)\n",
208*4882a593Smuzhiyun 		soft_noboot, softdog_dev.timeout, soft_panic, nowayout);
209*4882a593Smuzhiyun 	pr_info("             soft_reboot_cmd=%s soft_active_on_boot=%d\n",
210*4882a593Smuzhiyun 		soft_reboot_cmd ?: "<not set>", soft_active_on_boot);
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun 	return 0;
213*4882a593Smuzhiyun }
214*4882a593Smuzhiyun module_init(softdog_init);
215*4882a593Smuzhiyun 
softdog_exit(void)216*4882a593Smuzhiyun static void __exit softdog_exit(void)
217*4882a593Smuzhiyun {
218*4882a593Smuzhiyun 	watchdog_unregister_device(&softdog_dev);
219*4882a593Smuzhiyun }
220*4882a593Smuzhiyun module_exit(softdog_exit);
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun MODULE_AUTHOR("Alan Cox");
223*4882a593Smuzhiyun MODULE_DESCRIPTION("Software Watchdog Device Driver");
224*4882a593Smuzhiyun MODULE_LICENSE("GPL");
225