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