xref: /linux/drivers/watchdog/watchdog_core.c (revision 1fd1dc41724319406b0aff221a352a400b0ddfc5)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *	watchdog_core.c
4  *
5  *	(c) Copyright 2008-2011 Alan Cox <alan@lxorguk.ukuu.org.uk>,
6  *						All Rights Reserved.
7  *
8  *	(c) Copyright 2008-2011 Wim Van Sebroeck <wim@iguana.be>.
9  *
10  *	This source code is part of the generic code that can be used
11  *	by all the watchdog timer drivers.
12  *
13  *	Based on source code of the following authors:
14  *	  Matt Domsch <Matt_Domsch@dell.com>,
15  *	  Rob Radez <rob@osinvestor.com>,
16  *	  Rusty Lynch <rusty@linux.co.intel.com>
17  *	  Satyam Sharma <satyam@infradead.org>
18  *	  Randy Dunlap <randy.dunlap@oracle.com>
19  *
20  *	Neither Alan Cox, CymruNet Ltd., Wim Van Sebroeck nor Iguana vzw.
21  *	admit liability nor provide warranty for any of this software.
22  *	This material is provided "AS-IS" and at no charge.
23  */
24 
25 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26 
27 #include <linux/module.h>	/* For EXPORT_SYMBOL/module stuff/... */
28 #include <linux/types.h>	/* For standard types */
29 #include <linux/errno.h>	/* For the -ENODEV/... values */
30 #include <linux/kernel.h>	/* For printk/panic/... */
31 #include <linux/reboot.h>	/* For restart handler */
32 #include <linux/watchdog.h>	/* For watchdog specific items */
33 #include <linux/init.h>		/* For __init/__exit/... */
34 #include <linux/idr.h>		/* For ida_* macros */
35 #include <linux/err.h>		/* For IS_ERR macros */
36 #include <linux/of.h>		/* For of_alias_get_id */
37 #include <linux/property.h>	/* For device_property_read_u32 */
38 #include <linux/suspend.h>
39 
40 #include "watchdog_core.h"	/* For watchdog_dev_register/... */
41 
42 #define CREATE_TRACE_POINTS
43 #include <trace/events/watchdog.h>
44 
45 static DEFINE_IDA(watchdog_ida);
46 
47 static int stop_on_reboot = -1;
48 module_param(stop_on_reboot, int, 0444);
49 MODULE_PARM_DESC(stop_on_reboot, "Stop watchdogs on reboot (0=keep watching, 1=stop)");
50 
51 /*
52  * Deferred Registration infrastructure.
53  *
54  * Sometimes watchdog drivers needs to be loaded as soon as possible,
55  * for example when it's impossible to disable it. To do so,
56  * raising the initcall level of the watchdog driver is a solution.
57  * But in such case, the miscdev is maybe not ready (subsys_initcall), and
58  * watchdog_core need miscdev to register the watchdog as a char device.
59  *
60  * The deferred registration infrastructure offer a way for the watchdog
61  * subsystem to register a watchdog properly, even before miscdev is ready.
62  */
63 
64 static DEFINE_MUTEX(wtd_deferred_reg_mutex);
65 static LIST_HEAD(wtd_deferred_reg_list);
66 static bool wtd_deferred_reg_done;
67 
68 static void watchdog_deferred_registration_add(struct watchdog_device *wdd)
69 {
70 	list_add_tail(&wdd->deferred,
71 		      &wtd_deferred_reg_list);
72 }
73 
74 static void watchdog_deferred_registration_del(struct watchdog_device *wdd)
75 {
76 	struct list_head *p, *n;
77 	struct watchdog_device *wdd_tmp;
78 
79 	list_for_each_safe(p, n, &wtd_deferred_reg_list) {
80 		wdd_tmp = list_entry(p, struct watchdog_device,
81 				     deferred);
82 		if (wdd_tmp == wdd) {
83 			list_del(&wdd_tmp->deferred);
84 			break;
85 		}
86 	}
87 }
88 
89 static void watchdog_check_min_max_timeout(struct watchdog_device *wdd)
90 {
91 	/*
92 	 * Check that we have valid min and max timeout values, if
93 	 * not reset them both to 0 (=not used or unknown)
94 	 */
95 	if (!wdd->max_hw_heartbeat_ms && wdd->min_timeout > wdd->max_timeout) {
96 		pr_info("Invalid min and max timeout values, resetting to 0!\n");
97 		wdd->min_timeout = 0;
98 		wdd->max_timeout = 0;
99 	}
100 }
101 
102 /**
103  * watchdog_init_timeout() - initialize the timeout field
104  * @wdd: watchdog device
105  * @timeout_parm: timeout module parameter
106  * @dev: Device that stores the timeout-sec property
107  *
108  * Initialize the timeout field of the watchdog_device struct with either the
109  * timeout module parameter (if it is valid value) or the timeout-sec property
110  * (only if it is a valid value and the timeout_parm is out of bounds).
111  * If none of them are valid then we keep the old value (which should normally
112  * be the default timeout value). Note that for the module parameter, '0' means
113  * 'use default' while it is an invalid value for the timeout-sec property.
114  * It should simply be dropped if you want to use the default value then.
115  *
116  * A zero is returned on success or -EINVAL if all provided values are out of
117  * bounds.
118  */
119 int watchdog_init_timeout(struct watchdog_device *wdd,
120 			  unsigned int timeout_parm,
121 			  const struct device *dev)
122 {
123 	const char *dev_str = wdd->parent ? dev_name(wdd->parent) :
124 			      (const char *)wdd->info->identity;
125 	unsigned int t = 0;
126 	int ret = 0;
127 
128 	watchdog_check_min_max_timeout(wdd);
129 
130 	/* check the driver supplied value (likely a module parameter) first */
131 	if (timeout_parm) {
132 		if (!watchdog_timeout_invalid(wdd, timeout_parm)) {
133 			wdd->timeout = timeout_parm;
134 			return 0;
135 		}
136 		pr_err("%s: driver supplied timeout (%u) out of range\n",
137 			dev_str, timeout_parm);
138 		ret = -EINVAL;
139 	}
140 
141 	/* try to get the timeout_sec property */
142 	if (dev && device_property_read_u32(dev, "timeout-sec", &t) == 0) {
143 		if (t && !watchdog_timeout_invalid(wdd, t)) {
144 			wdd->timeout = t;
145 			return 0;
146 		}
147 		pr_err("%s: DT supplied timeout (%u) out of range\n", dev_str, t);
148 		ret = -EINVAL;
149 	}
150 
151 	if (ret < 0 && wdd->timeout)
152 		pr_warn("%s: falling back to default timeout (%u)\n", dev_str,
153 			wdd->timeout);
154 
155 	return ret;
156 }
157 EXPORT_SYMBOL_GPL(watchdog_init_timeout);
158 
159 static int watchdog_reboot_notifier(struct notifier_block *nb,
160 				    unsigned long code, void *data)
161 {
162 	struct watchdog_device *wdd;
163 
164 	wdd = container_of(nb, struct watchdog_device, reboot_nb);
165 	if (code == SYS_DOWN || code == SYS_HALT || code == SYS_POWER_OFF) {
166 		if (watchdog_hw_running(wdd)) {
167 			int ret;
168 
169 			ret = wdd->ops->stop(wdd);
170 			trace_watchdog_stop(wdd, ret);
171 			if (ret)
172 				return NOTIFY_BAD;
173 		}
174 	}
175 
176 	return NOTIFY_DONE;
177 }
178 
179 static int watchdog_restart_notifier(struct notifier_block *nb,
180 				     unsigned long action, void *data)
181 {
182 	struct watchdog_device *wdd = container_of(nb, struct watchdog_device,
183 						   restart_nb);
184 
185 	int ret;
186 
187 	ret = wdd->ops->restart(wdd, action, data);
188 	if (ret)
189 		return NOTIFY_BAD;
190 
191 	return NOTIFY_DONE;
192 }
193 
194 static int watchdog_pm_notifier(struct notifier_block *nb, unsigned long mode,
195 				void *data)
196 {
197 	struct watchdog_device *wdd;
198 	int ret = 0;
199 
200 	wdd = container_of(nb, struct watchdog_device, pm_nb);
201 
202 	switch (mode) {
203 	case PM_HIBERNATION_PREPARE:
204 	case PM_RESTORE_PREPARE:
205 	case PM_SUSPEND_PREPARE:
206 		ret = watchdog_dev_suspend(wdd);
207 		break;
208 	case PM_POST_HIBERNATION:
209 	case PM_POST_RESTORE:
210 	case PM_POST_SUSPEND:
211 		ret = watchdog_dev_resume(wdd);
212 		break;
213 	}
214 
215 	if (ret)
216 		return NOTIFY_BAD;
217 
218 	return NOTIFY_DONE;
219 }
220 
221 /**
222  * watchdog_set_restart_priority - Change priority of restart handler
223  * @wdd: watchdog device
224  * @priority: priority of the restart handler, should follow these guidelines:
225  *   0:   use watchdog's restart function as last resort, has limited restart
226  *        capabilies
227  *   128: default restart handler, use if no other handler is expected to be
228  *        available and/or if restart is sufficient to restart the entire system
229  *   255: preempt all other handlers
230  *
231  * If a wdd->ops->restart function is provided when watchdog_register_device is
232  * called, it will be registered as a restart handler with the priority given
233  * here.
234  */
235 void watchdog_set_restart_priority(struct watchdog_device *wdd, int priority)
236 {
237 	wdd->restart_nb.priority = priority;
238 }
239 EXPORT_SYMBOL_GPL(watchdog_set_restart_priority);
240 
241 static int ___watchdog_register_device(struct watchdog_device *wdd)
242 {
243 	int ret, id = -1;
244 
245 	if (wdd == NULL || wdd->info == NULL || wdd->ops == NULL)
246 		return -EINVAL;
247 
248 	/* Mandatory operations need to be supported */
249 	if (!wdd->ops->start || (!wdd->ops->stop && !wdd->max_hw_heartbeat_ms))
250 		return -EINVAL;
251 
252 	watchdog_check_min_max_timeout(wdd);
253 
254 	/*
255 	 * Note: now that all watchdog_device data has been verified, we
256 	 * will not check this anymore in other functions. If data gets
257 	 * corrupted in a later stage then we expect a kernel panic!
258 	 */
259 
260 	/* Use alias for watchdog id if possible */
261 	if (wdd->parent) {
262 		ret = of_alias_get_id(wdd->parent->of_node, "watchdog");
263 		if (ret >= 0)
264 			id = ida_alloc_range(&watchdog_ida, ret, ret,
265 					     GFP_KERNEL);
266 	}
267 
268 	if (id < 0)
269 		id = ida_alloc_max(&watchdog_ida, MAX_DOGS - 1, GFP_KERNEL);
270 
271 	if (id < 0)
272 		return id;
273 	wdd->id = id;
274 
275 	ret = watchdog_dev_register(wdd);
276 	if (ret) {
277 		ida_free(&watchdog_ida, id);
278 		if (!(id == 0 && ret == -EBUSY))
279 			return ret;
280 
281 		/* Retry in case a legacy watchdog module exists */
282 		id = ida_alloc_range(&watchdog_ida, 1, MAX_DOGS - 1,
283 				     GFP_KERNEL);
284 		if (id < 0)
285 			return id;
286 		wdd->id = id;
287 
288 		ret = watchdog_dev_register(wdd);
289 		if (ret) {
290 			ida_free(&watchdog_ida, id);
291 			return ret;
292 		}
293 	}
294 
295 	/* Module parameter to force watchdog policy on reboot. */
296 	if (stop_on_reboot != -1) {
297 		if (stop_on_reboot)
298 			set_bit(WDOG_STOP_ON_REBOOT, &wdd->status);
299 		else
300 			clear_bit(WDOG_STOP_ON_REBOOT, &wdd->status);
301 	}
302 
303 	if (test_bit(WDOG_STOP_ON_REBOOT, &wdd->status)) {
304 		if (!wdd->ops->stop)
305 			pr_warn("watchdog%d: stop_on_reboot not supported\n", wdd->id);
306 		else {
307 			wdd->reboot_nb.notifier_call = watchdog_reboot_notifier;
308 
309 			ret = register_reboot_notifier(&wdd->reboot_nb);
310 			if (ret) {
311 				pr_err("watchdog%d: Cannot register reboot notifier (%d)\n",
312 					wdd->id, ret);
313 				watchdog_dev_unregister(wdd);
314 				ida_free(&watchdog_ida, id);
315 				return ret;
316 			}
317 		}
318 	}
319 
320 	if (wdd->ops->restart) {
321 		wdd->restart_nb.notifier_call = watchdog_restart_notifier;
322 
323 		ret = register_restart_handler(&wdd->restart_nb);
324 		if (ret)
325 			pr_warn("watchdog%d: Cannot register restart handler (%d)\n",
326 				wdd->id, ret);
327 	}
328 
329 	if (test_bit(WDOG_NO_PING_ON_SUSPEND, &wdd->status)) {
330 		wdd->pm_nb.notifier_call = watchdog_pm_notifier;
331 
332 		ret = register_pm_notifier(&wdd->pm_nb);
333 		if (ret)
334 			pr_warn("watchdog%d: Cannot register pm handler (%d)\n",
335 				wdd->id, ret);
336 	}
337 
338 	return 0;
339 }
340 
341 static int __watchdog_register_device(struct watchdog_device *wdd)
342 {
343 	const char *dev_str;
344 	int ret;
345 
346 	ret = ___watchdog_register_device(wdd);
347 	if (ret) {
348 		dev_str = wdd->parent ? dev_name(wdd->parent) :
349 			  (const char *)wdd->info->identity;
350 		pr_err("%s: failed to register watchdog device (err = %d)\n",
351 			dev_str, ret);
352 	}
353 
354 	return ret;
355 }
356 
357 /**
358  * watchdog_register_device() - register a watchdog device
359  * @wdd: watchdog device
360  *
361  * Register a watchdog device with the kernel so that the
362  * watchdog timer can be accessed from userspace.
363  *
364  * A zero is returned on success and a negative errno code for
365  * failure.
366  */
367 
368 int watchdog_register_device(struct watchdog_device *wdd)
369 {
370 	int ret = 0;
371 
372 	mutex_lock(&wtd_deferred_reg_mutex);
373 	if (wtd_deferred_reg_done)
374 		ret = __watchdog_register_device(wdd);
375 	else
376 		watchdog_deferred_registration_add(wdd);
377 	mutex_unlock(&wtd_deferred_reg_mutex);
378 
379 	return ret;
380 }
381 EXPORT_SYMBOL_GPL(watchdog_register_device);
382 
383 static void __watchdog_unregister_device(struct watchdog_device *wdd)
384 {
385 	if (wdd == NULL)
386 		return;
387 
388 	if (wdd->ops->restart)
389 		unregister_restart_handler(&wdd->restart_nb);
390 
391 	if (test_bit(WDOG_STOP_ON_REBOOT, &wdd->status))
392 		unregister_reboot_notifier(&wdd->reboot_nb);
393 
394 	watchdog_dev_unregister(wdd);
395 	ida_free(&watchdog_ida, wdd->id);
396 }
397 
398 /**
399  * watchdog_unregister_device() - unregister a watchdog device
400  * @wdd: watchdog device to unregister
401  *
402  * Unregister a watchdog device that was previously successfully
403  * registered with watchdog_register_device().
404  */
405 
406 void watchdog_unregister_device(struct watchdog_device *wdd)
407 {
408 	mutex_lock(&wtd_deferred_reg_mutex);
409 	if (wtd_deferred_reg_done)
410 		__watchdog_unregister_device(wdd);
411 	else
412 		watchdog_deferred_registration_del(wdd);
413 	mutex_unlock(&wtd_deferred_reg_mutex);
414 }
415 
416 EXPORT_SYMBOL_GPL(watchdog_unregister_device);
417 
418 static void devm_watchdog_unregister_device(struct device *dev, void *res)
419 {
420 	watchdog_unregister_device(*(struct watchdog_device **)res);
421 }
422 
423 /**
424  * devm_watchdog_register_device() - resource managed watchdog_register_device()
425  * @dev: device that is registering this watchdog device
426  * @wdd: watchdog device
427  *
428  * Managed watchdog_register_device(). For watchdog device registered by this
429  * function,  watchdog_unregister_device() is automatically called on driver
430  * detach. See watchdog_register_device() for more information.
431  */
432 int devm_watchdog_register_device(struct device *dev,
433 				struct watchdog_device *wdd)
434 {
435 	struct watchdog_device **rcwdd;
436 	int ret;
437 
438 	rcwdd = devres_alloc(devm_watchdog_unregister_device, sizeof(*rcwdd),
439 			     GFP_KERNEL);
440 	if (!rcwdd)
441 		return -ENOMEM;
442 
443 	ret = watchdog_register_device(wdd);
444 	if (!ret) {
445 		*rcwdd = wdd;
446 		devres_add(dev, rcwdd);
447 	} else {
448 		devres_free(rcwdd);
449 	}
450 
451 	return ret;
452 }
453 EXPORT_SYMBOL_GPL(devm_watchdog_register_device);
454 
455 static int __init watchdog_deferred_registration(void)
456 {
457 	mutex_lock(&wtd_deferred_reg_mutex);
458 	wtd_deferred_reg_done = true;
459 	while (!list_empty(&wtd_deferred_reg_list)) {
460 		struct watchdog_device *wdd;
461 
462 		wdd = list_first_entry(&wtd_deferred_reg_list,
463 				       struct watchdog_device, deferred);
464 		list_del(&wdd->deferred);
465 		__watchdog_register_device(wdd);
466 	}
467 	mutex_unlock(&wtd_deferred_reg_mutex);
468 	return 0;
469 }
470 
471 static int __init watchdog_init(void)
472 {
473 	int err;
474 
475 	err = watchdog_dev_init();
476 	if (err < 0)
477 		return err;
478 
479 	watchdog_deferred_registration();
480 	return 0;
481 }
482 
483 static void __exit watchdog_exit(void)
484 {
485 	watchdog_dev_exit();
486 	ida_destroy(&watchdog_ida);
487 }
488 
489 subsys_initcall_sync(watchdog_init);
490 module_exit(watchdog_exit);
491 
492 MODULE_AUTHOR("Alan Cox <alan@lxorguk.ukuu.org.uk>");
493 MODULE_AUTHOR("Wim Van Sebroeck <wim@iguana.be>");
494 MODULE_DESCRIPTION("WatchDog Timer Driver Core");
495 MODULE_LICENSE("GPL");
496