• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/reboot.h>
26 #include <linux/types.h>
27 #include <linux/watchdog.h>
28 
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) ")");
35 
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) ")");
41 
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)");
46 
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)");
51 
52 static struct hrtimer softdog_ticktock;
53 static struct hrtimer softdog_preticktock;
54 
softdog_fire(struct hrtimer * timer)55 static enum hrtimer_restart softdog_fire(struct hrtimer *timer)
56 {
57 	module_put(THIS_MODULE);
58 	if (soft_noboot) {
59 		pr_crit("Triggered - Reboot ignored\n");
60 	} else if (soft_panic) {
61 		pr_crit("Initiating panic\n");
62 		panic("Software Watchdog Timer expired");
63 	} else {
64 		pr_crit("Initiating system reboot\n");
65 		emergency_restart();
66 		pr_crit("Reboot didn't ?????\n");
67 	}
68 
69 	return HRTIMER_NORESTART;
70 }
71 
72 static struct watchdog_device softdog_dev;
73 
softdog_pretimeout(struct hrtimer * timer)74 static enum hrtimer_restart softdog_pretimeout(struct hrtimer *timer)
75 {
76 	watchdog_notify_pretimeout(&softdog_dev);
77 
78 	return HRTIMER_NORESTART;
79 }
80 
softdog_ping(struct watchdog_device * w)81 static int softdog_ping(struct watchdog_device *w)
82 {
83 	if (!hrtimer_active(&softdog_ticktock))
84 		__module_get(THIS_MODULE);
85 	hrtimer_start(&softdog_ticktock, ktime_set(w->timeout, 0),
86 		      HRTIMER_MODE_REL);
87 
88 	if (IS_ENABLED(CONFIG_SOFT_WATCHDOG_PRETIMEOUT)) {
89 		if (w->pretimeout)
90 			hrtimer_start(&softdog_preticktock,
91 				      ktime_set(w->timeout - w->pretimeout, 0),
92 				      HRTIMER_MODE_REL);
93 		else
94 			hrtimer_cancel(&softdog_preticktock);
95 	}
96 
97 	return 0;
98 }
99 
softdog_stop(struct watchdog_device * w)100 static int softdog_stop(struct watchdog_device *w)
101 {
102 	if (hrtimer_cancel(&softdog_ticktock))
103 		module_put(THIS_MODULE);
104 
105 	if (IS_ENABLED(CONFIG_SOFT_WATCHDOG_PRETIMEOUT))
106 		hrtimer_cancel(&softdog_preticktock);
107 
108 	return 0;
109 }
110 
111 static struct watchdog_info softdog_info = {
112 	.identity = "Software Watchdog",
113 	.options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
114 };
115 
116 static const struct watchdog_ops softdog_ops = {
117 	.owner = THIS_MODULE,
118 	.start = softdog_ping,
119 	.stop = softdog_stop,
120 };
121 
122 static struct watchdog_device softdog_dev = {
123 	.info = &softdog_info,
124 	.ops = &softdog_ops,
125 	.min_timeout = 1,
126 	.max_timeout = 65535,
127 	.timeout = TIMER_MARGIN,
128 };
129 
softdog_init(void)130 static int __init softdog_init(void)
131 {
132 	int ret;
133 
134 	watchdog_init_timeout(&softdog_dev, soft_margin, NULL);
135 	watchdog_set_nowayout(&softdog_dev, nowayout);
136 	watchdog_stop_on_reboot(&softdog_dev);
137 
138 	hrtimer_init(&softdog_ticktock, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
139 	softdog_ticktock.function = softdog_fire;
140 
141 	if (IS_ENABLED(CONFIG_SOFT_WATCHDOG_PRETIMEOUT)) {
142 		softdog_info.options |= WDIOF_PRETIMEOUT;
143 		hrtimer_init(&softdog_preticktock, CLOCK_MONOTONIC,
144 			     HRTIMER_MODE_REL);
145 		softdog_preticktock.function = softdog_pretimeout;
146 	}
147 
148 	ret = watchdog_register_device(&softdog_dev);
149 	if (ret)
150 		return ret;
151 
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);
154 
155 	return 0;
156 }
157 module_init(softdog_init);
158 
softdog_exit(void)159 static void __exit softdog_exit(void)
160 {
161 	watchdog_unregister_device(&softdog_dev);
162 }
163 module_exit(softdog_exit);
164 
165 MODULE_AUTHOR("Alan Cox");
166 MODULE_DESCRIPTION("Software Watchdog Device Driver");
167 MODULE_LICENSE("GPL");
168