1d0173278SGuenter Roeck // SPDX-License-Identifier: GPL-2.0+
2b7e04f8cSWim Van Sebroeck /*
3a5132cafSAlan Cox * SoftDog: A Software Watchdog Device
4b7e04f8cSWim Van Sebroeck *
5143a2e54SWim Van Sebroeck * (c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>,
6143a2e54SWim Van Sebroeck * All Rights Reserved.
7b7e04f8cSWim Van Sebroeck *
8b7e04f8cSWim Van Sebroeck * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
9b7e04f8cSWim Van Sebroeck * warranty for any of this software. This material is provided
10b7e04f8cSWim Van Sebroeck * "AS-IS" and at no charge.
11b7e04f8cSWim Van Sebroeck *
12b7e04f8cSWim Van Sebroeck * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>
13b7e04f8cSWim Van Sebroeck *
14b7e04f8cSWim Van Sebroeck * Software only watchdog driver. Unlike its big brother the WDT501P
15b7e04f8cSWim Van Sebroeck * driver this won't always recover a failed machine.
16b7e04f8cSWim Van Sebroeck */
17b7e04f8cSWim Van Sebroeck
1827c766aaSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
1927c766aaSJoe Perches
208d5755b3SNiklas Cassel #include <linux/hrtimer.h>
21b7e04f8cSWim Van Sebroeck #include <linux/init.h>
227fff4bebSAnithra P Janakiraman #include <linux/kernel.h>
23*36a8947cSWoody Lin #include <linux/kthread.h>
24e65c5825SWolfram Sang #include <linux/module.h>
25e65c5825SWolfram Sang #include <linux/moduleparam.h>
26e65c5825SWolfram Sang #include <linux/reboot.h>
27e65c5825SWolfram Sang #include <linux/types.h>
28e65c5825SWolfram Sang #include <linux/watchdog.h>
29*36a8947cSWoody Lin #include <linux/workqueue.h>
30b7e04f8cSWim Van Sebroeck
31b7e04f8cSWim Van Sebroeck #define TIMER_MARGIN 60 /* Default is 60 seconds */
32a5132cafSAlan Cox static unsigned int soft_margin = TIMER_MARGIN; /* in seconds */
33a5132cafSAlan Cox module_param(soft_margin, uint, 0);
34f92d3749SAlan Cox MODULE_PARM_DESC(soft_margin,
35f92d3749SAlan Cox "Watchdog soft_margin in seconds. (0 < soft_margin < 65536, default="
36f92d3749SAlan Cox __MODULE_STRING(TIMER_MARGIN) ")");
37b7e04f8cSWim Van Sebroeck
3886a1e189SWim Van Sebroeck static bool nowayout = WATCHDOG_NOWAYOUT;
3986a1e189SWim Van Sebroeck module_param(nowayout, bool, 0);
40f92d3749SAlan Cox MODULE_PARM_DESC(nowayout,
41f92d3749SAlan Cox "Watchdog cannot be stopped once started (default="
42f92d3749SAlan Cox __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
43b7e04f8cSWim Van Sebroeck
445f5e1909SJingoo Han static int soft_noboot;
45b7e04f8cSWim Van Sebroeck module_param(soft_noboot, int, 0);
46a77dba7eSWim Van Sebroeck MODULE_PARM_DESC(soft_noboot,
47a5132cafSAlan Cox "Softdog action, set to 1 to ignore reboots, 0 to reboot (default=0)");
48b7e04f8cSWim Van Sebroeck
497fff4bebSAnithra P Janakiraman static int soft_panic;
507fff4bebSAnithra P Janakiraman module_param(soft_panic, int, 0);
517fff4bebSAnithra P Janakiraman MODULE_PARM_DESC(soft_panic,
527fff4bebSAnithra P Janakiraman "Softdog action, set to 1 to panic, 0 to reboot (default=0)");
537fff4bebSAnithra P Janakiraman
54*36a8947cSWoody Lin static char *soft_reboot_cmd;
55*36a8947cSWoody Lin module_param(soft_reboot_cmd, charp, 0000);
56*36a8947cSWoody Lin MODULE_PARM_DESC(soft_reboot_cmd,
57*36a8947cSWoody Lin "Set reboot command. Emergency reboot takes place if unset");
58*36a8947cSWoody Lin
59*36a8947cSWoody Lin static bool soft_active_on_boot;
60*36a8947cSWoody Lin module_param(soft_active_on_boot, bool, 0000);
61*36a8947cSWoody Lin MODULE_PARM_DESC(soft_active_on_boot,
62*36a8947cSWoody Lin "Set to true to active Softdog on boot (default=false)");
63*36a8947cSWoody Lin
648d5755b3SNiklas Cassel static struct hrtimer softdog_ticktock;
658d5755b3SNiklas Cassel static struct hrtimer softdog_preticktock;
668d5755b3SNiklas Cassel
reboot_kthread_fn(void * data)67*36a8947cSWoody Lin static int reboot_kthread_fn(void *data)
68*36a8947cSWoody Lin {
69*36a8947cSWoody Lin kernel_restart(soft_reboot_cmd);
70*36a8947cSWoody Lin return -EPERM; /* Should not reach here */
71*36a8947cSWoody Lin }
72*36a8947cSWoody Lin
reboot_work_fn(struct work_struct * unused)73*36a8947cSWoody Lin static void reboot_work_fn(struct work_struct *unused)
74*36a8947cSWoody Lin {
75*36a8947cSWoody Lin kthread_run(reboot_kthread_fn, NULL, "softdog_reboot");
76*36a8947cSWoody Lin }
77*36a8947cSWoody Lin
softdog_fire(struct hrtimer * timer)788d5755b3SNiklas Cassel static enum hrtimer_restart softdog_fire(struct hrtimer *timer)
79b7e04f8cSWim Van Sebroeck {
80*36a8947cSWoody Lin static bool soft_reboot_fired;
81*36a8947cSWoody Lin
825889f06bSLi RongQing module_put(THIS_MODULE);
834a23e2bfSWolfram Sang if (soft_noboot) {
8427c766aaSJoe Perches pr_crit("Triggered - Reboot ignored\n");
854a23e2bfSWolfram Sang } else if (soft_panic) {
8627c766aaSJoe Perches pr_crit("Initiating panic\n");
8727c766aaSJoe Perches panic("Software Watchdog Timer expired");
887fff4bebSAnithra P Janakiraman } else {
8927c766aaSJoe Perches pr_crit("Initiating system reboot\n");
90*36a8947cSWoody Lin if (!soft_reboot_fired && soft_reboot_cmd != NULL) {
91*36a8947cSWoody Lin static DECLARE_WORK(reboot_work, reboot_work_fn);
92*36a8947cSWoody Lin /*
93*36a8947cSWoody Lin * The 'kernel_restart' is a 'might-sleep' operation.
94*36a8947cSWoody Lin * Also, executing it in system-wide workqueues blocks
95*36a8947cSWoody Lin * any driver from using the same workqueue in its
96*36a8947cSWoody Lin * shutdown callback function. Thus, we should execute
97*36a8947cSWoody Lin * the 'kernel_restart' in a standalone kernel thread.
98*36a8947cSWoody Lin * But since starting a kernel thread is also a
99*36a8947cSWoody Lin * 'might-sleep' operation, so the 'reboot_work' is
100*36a8947cSWoody Lin * required as a launcher of the kernel thread.
101*36a8947cSWoody Lin *
102*36a8947cSWoody Lin * After request the reboot, restart the timer to
103*36a8947cSWoody Lin * schedule an 'emergency_restart' reboot after
104*36a8947cSWoody Lin * 'TIMER_MARGIN' seconds. It's because if the softdog
105*36a8947cSWoody Lin * hangs, it might be because of scheduling issues. And
106*36a8947cSWoody Lin * if that is the case, both 'schedule_work' and
107*36a8947cSWoody Lin * 'kernel_restart' may possibly be malfunctional at the
108*36a8947cSWoody Lin * same time.
109*36a8947cSWoody Lin */
110*36a8947cSWoody Lin soft_reboot_fired = true;
111*36a8947cSWoody Lin schedule_work(&reboot_work);
112*36a8947cSWoody Lin hrtimer_add_expires_ns(timer,
113*36a8947cSWoody Lin (u64)TIMER_MARGIN * NSEC_PER_SEC);
114*36a8947cSWoody Lin
115*36a8947cSWoody Lin return HRTIMER_RESTART;
116*36a8947cSWoody Lin }
117b7e04f8cSWim Van Sebroeck emergency_restart();
11827c766aaSJoe Perches pr_crit("Reboot didn't ?????\n");
119b7e04f8cSWim Van Sebroeck }
120b7e04f8cSWim Van Sebroeck
1218d5755b3SNiklas Cassel return HRTIMER_NORESTART;
1228d5755b3SNiklas Cassel }
12344ba0f04SWolfram Sang
1242accf320SWolfram Sang static struct watchdog_device softdog_dev;
1252accf320SWolfram Sang
softdog_pretimeout(struct hrtimer * timer)1268d5755b3SNiklas Cassel static enum hrtimer_restart softdog_pretimeout(struct hrtimer *timer)
1272accf320SWolfram Sang {
1282accf320SWolfram Sang watchdog_notify_pretimeout(&softdog_dev);
1292accf320SWolfram Sang
1308d5755b3SNiklas Cassel return HRTIMER_NORESTART;
1318d5755b3SNiklas Cassel }
1322accf320SWolfram Sang
softdog_ping(struct watchdog_device * w)133a5132cafSAlan Cox static int softdog_ping(struct watchdog_device *w)
134b7e04f8cSWim Van Sebroeck {
1358d5755b3SNiklas Cassel if (!hrtimer_active(&softdog_ticktock))
1365889f06bSLi RongQing __module_get(THIS_MODULE);
1378d5755b3SNiklas Cassel hrtimer_start(&softdog_ticktock, ktime_set(w->timeout, 0),
1388d5755b3SNiklas Cassel HRTIMER_MODE_REL);
1392accf320SWolfram Sang
1404cbc6902SWolfram Sang if (IS_ENABLED(CONFIG_SOFT_WATCHDOG_PRETIMEOUT)) {
1412accf320SWolfram Sang if (w->pretimeout)
1428d5755b3SNiklas Cassel hrtimer_start(&softdog_preticktock,
1438d5755b3SNiklas Cassel ktime_set(w->timeout - w->pretimeout, 0),
1448d5755b3SNiklas Cassel HRTIMER_MODE_REL);
1452accf320SWolfram Sang else
1468d5755b3SNiklas Cassel hrtimer_cancel(&softdog_preticktock);
1474cbc6902SWolfram Sang }
1482accf320SWolfram Sang
149b7e04f8cSWim Van Sebroeck return 0;
150b7e04f8cSWim Van Sebroeck }
151b7e04f8cSWim Van Sebroeck
softdog_stop(struct watchdog_device * w)152a5132cafSAlan Cox static int softdog_stop(struct watchdog_device *w)
153b7e04f8cSWim Van Sebroeck {
1548d5755b3SNiklas Cassel if (hrtimer_cancel(&softdog_ticktock))
1555889f06bSLi RongQing module_put(THIS_MODULE);
1565889f06bSLi RongQing
1574cbc6902SWolfram Sang if (IS_ENABLED(CONFIG_SOFT_WATCHDOG_PRETIMEOUT))
1588d5755b3SNiklas Cassel hrtimer_cancel(&softdog_preticktock);
1592accf320SWolfram Sang
160b7e04f8cSWim Van Sebroeck return 0;
161b7e04f8cSWim Van Sebroeck }
162b7e04f8cSWim Van Sebroeck
163a5132cafSAlan Cox static struct watchdog_info softdog_info = {
164a5132cafSAlan Cox .identity = "Software Watchdog",
1654cbc6902SWolfram Sang .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
166a5132cafSAlan Cox };
167a5132cafSAlan Cox
16885f15cfcSJulia Lawall static const struct watchdog_ops softdog_ops = {
169a5132cafSAlan Cox .owner = THIS_MODULE,
170a5132cafSAlan Cox .start = softdog_ping,
171a5132cafSAlan Cox .stop = softdog_stop,
172a5132cafSAlan Cox };
173a5132cafSAlan Cox
174a5132cafSAlan Cox static struct watchdog_device softdog_dev = {
175a5132cafSAlan Cox .info = &softdog_info,
176a5132cafSAlan Cox .ops = &softdog_ops,
177a5132cafSAlan Cox .min_timeout = 1,
178e8cf96abSWolfram Sang .max_timeout = 65535,
179e8cf96abSWolfram Sang .timeout = TIMER_MARGIN,
180a5132cafSAlan Cox };
181a5132cafSAlan Cox
softdog_init(void)1820efc70b8SWolfram Sang static int __init softdog_init(void)
183b7e04f8cSWim Van Sebroeck {
184b7e04f8cSWim Van Sebroeck int ret;
185b7e04f8cSWim Van Sebroeck
186e8cf96abSWolfram Sang watchdog_init_timeout(&softdog_dev, soft_margin, NULL);
187a5132cafSAlan Cox watchdog_set_nowayout(&softdog_dev, nowayout);
18884ebcc17SDamien Riegel watchdog_stop_on_reboot(&softdog_dev);
189b7e04f8cSWim Van Sebroeck
1908d5755b3SNiklas Cassel hrtimer_init(&softdog_ticktock, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1918d5755b3SNiklas Cassel softdog_ticktock.function = softdog_fire;
1928d5755b3SNiklas Cassel
1938d5755b3SNiklas Cassel if (IS_ENABLED(CONFIG_SOFT_WATCHDOG_PRETIMEOUT)) {
1944cbc6902SWolfram Sang softdog_info.options |= WDIOF_PRETIMEOUT;
1958d5755b3SNiklas Cassel hrtimer_init(&softdog_preticktock, CLOCK_MONOTONIC,
1968d5755b3SNiklas Cassel HRTIMER_MODE_REL);
1978d5755b3SNiklas Cassel softdog_preticktock.function = softdog_pretimeout;
1988d5755b3SNiklas Cassel }
1994cbc6902SWolfram Sang
200*36a8947cSWoody Lin if (soft_active_on_boot)
201*36a8947cSWoody Lin softdog_ping(&softdog_dev);
202*36a8947cSWoody Lin
203a5132cafSAlan Cox ret = watchdog_register_device(&softdog_dev);
20484ebcc17SDamien Riegel if (ret)
205b7e04f8cSWim Van Sebroeck return ret;
206b7e04f8cSWim Van Sebroeck
207e8cf96abSWolfram Sang pr_info("initialized. soft_noboot=%d soft_margin=%d sec soft_panic=%d (nowayout=%d)\n",
208e8cf96abSWolfram Sang soft_noboot, softdog_dev.timeout, soft_panic, nowayout);
209*36a8947cSWoody Lin pr_info(" soft_reboot_cmd=%s soft_active_on_boot=%d\n",
210*36a8947cSWoody Lin soft_reboot_cmd ?: "<not set>", soft_active_on_boot);
211b7e04f8cSWim Van Sebroeck
212b7e04f8cSWim Van Sebroeck return 0;
213b7e04f8cSWim Van Sebroeck }
2140efc70b8SWolfram Sang module_init(softdog_init);
215b7e04f8cSWim Van Sebroeck
softdog_exit(void)2160efc70b8SWolfram Sang static void __exit softdog_exit(void)
217b7e04f8cSWim Van Sebroeck {
218a5132cafSAlan Cox watchdog_unregister_device(&softdog_dev);
219b7e04f8cSWim Van Sebroeck }
2200efc70b8SWolfram Sang module_exit(softdog_exit);
221b7e04f8cSWim Van Sebroeck
222b7e04f8cSWim Van Sebroeck MODULE_AUTHOR("Alan Cox");
223b7e04f8cSWim Van Sebroeck MODULE_DESCRIPTION("Software Watchdog Device Driver");
224b7e04f8cSWim Van Sebroeck MODULE_LICENSE("GPL");
225