1 // SPDX-License-Identifier: GPL-2.0+
3 * SoftDog: A Software Watchdog Device
5 * (c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>,
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.
12 * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>
14 * Software only watchdog driver. Unlike its big brother the WDT501P
15 * driver this won't always recover a failed machine.
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20 #include <linux/hrtimer.h>
21 #include <linux/init.h>
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/reboot.h>
26 #include <linux/types.h>
27 #include <linux/watchdog.h>
29 #define TIMER_MARGIN 60 /* Default is 60 seconds */
30 static unsigned int soft_margin
= TIMER_MARGIN
; /* in seconds */
31 module_param(soft_margin
, uint
, 0);
32 MODULE_PARM_DESC(soft_margin
,
33 "Watchdog soft_margin in seconds. (0 < soft_margin < 65536, default="
34 __MODULE_STRING(TIMER_MARGIN
) ")");
36 static bool nowayout
= WATCHDOG_NOWAYOUT
;
37 module_param(nowayout
, bool, 0);
38 MODULE_PARM_DESC(nowayout
,
39 "Watchdog cannot be stopped once started (default="
40 __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
42 static int soft_noboot
;
43 module_param(soft_noboot
, int, 0);
44 MODULE_PARM_DESC(soft_noboot
,
45 "Softdog action, set to 1 to ignore reboots, 0 to reboot (default=0)");
47 static int soft_panic
;
48 module_param(soft_panic
, int, 0);
49 MODULE_PARM_DESC(soft_panic
,
50 "Softdog action, set to 1 to panic, 0 to reboot (default=0)");
52 static struct hrtimer softdog_ticktock
;
53 static struct hrtimer softdog_preticktock
;
55 static enum hrtimer_restart
softdog_fire(struct hrtimer
*timer
)
57 module_put(THIS_MODULE
);
59 pr_crit("Triggered - Reboot ignored\n");
60 } else if (soft_panic
) {
61 pr_crit("Initiating panic\n");
62 panic("Software Watchdog Timer expired");
64 pr_crit("Initiating system reboot\n");
66 pr_crit("Reboot didn't ?????\n");
69 return HRTIMER_NORESTART
;
72 static struct watchdog_device softdog_dev
;
74 static enum hrtimer_restart
softdog_pretimeout(struct hrtimer
*timer
)
76 watchdog_notify_pretimeout(&softdog_dev
);
78 return HRTIMER_NORESTART
;
81 static int softdog_ping(struct watchdog_device
*w
)
83 if (!hrtimer_active(&softdog_ticktock
))
84 __module_get(THIS_MODULE
);
85 hrtimer_start(&softdog_ticktock
, ktime_set(w
->timeout
, 0),
88 if (IS_ENABLED(CONFIG_SOFT_WATCHDOG_PRETIMEOUT
)) {
90 hrtimer_start(&softdog_preticktock
,
91 ktime_set(w
->timeout
- w
->pretimeout
, 0),
94 hrtimer_cancel(&softdog_preticktock
);
100 static int softdog_stop(struct watchdog_device
*w
)
102 if (hrtimer_cancel(&softdog_ticktock
))
103 module_put(THIS_MODULE
);
105 if (IS_ENABLED(CONFIG_SOFT_WATCHDOG_PRETIMEOUT
))
106 hrtimer_cancel(&softdog_preticktock
);
111 static struct watchdog_info softdog_info
= {
112 .identity
= "Software Watchdog",
113 .options
= WDIOF_SETTIMEOUT
| WDIOF_KEEPALIVEPING
| WDIOF_MAGICCLOSE
,
116 static const struct watchdog_ops softdog_ops
= {
117 .owner
= THIS_MODULE
,
118 .start
= softdog_ping
,
119 .stop
= softdog_stop
,
122 static struct watchdog_device softdog_dev
= {
123 .info
= &softdog_info
,
126 .max_timeout
= 65535,
127 .timeout
= TIMER_MARGIN
,
130 static int __init
softdog_init(void)
134 watchdog_init_timeout(&softdog_dev
, soft_margin
, NULL
);
135 watchdog_set_nowayout(&softdog_dev
, nowayout
);
136 watchdog_stop_on_reboot(&softdog_dev
);
138 hrtimer_init(&softdog_ticktock
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
139 softdog_ticktock
.function
= softdog_fire
;
141 if (IS_ENABLED(CONFIG_SOFT_WATCHDOG_PRETIMEOUT
)) {
142 softdog_info
.options
|= WDIOF_PRETIMEOUT
;
143 hrtimer_init(&softdog_preticktock
, CLOCK_MONOTONIC
,
145 softdog_preticktock
.function
= softdog_pretimeout
;
148 ret
= watchdog_register_device(&softdog_dev
);
152 pr_info("initialized. soft_noboot=%d soft_margin=%d sec soft_panic=%d (nowayout=%d)\n",
153 soft_noboot
, softdog_dev
.timeout
, soft_panic
, nowayout
);
157 module_init(softdog_init
);
159 static void __exit
softdog_exit(void)
161 watchdog_unregister_device(&softdog_dev
);
163 module_exit(softdog_exit
);
165 MODULE_AUTHOR("Alan Cox");
166 MODULE_DESCRIPTION("Software Watchdog Device Driver");
167 MODULE_LICENSE("GPL");