4 * (c) Copyright 2008-2011 Alan Cox <alan@lxorguk.ukuu.org.uk>,
7 * (c) Copyright 2008-2011 Wim Van Sebroeck <wim@iguana.be>.
10 * This source code is part of the generic code that can be used
11 * by all the watchdog timer drivers.
13 * This part of the generic code takes care of the following
14 * misc device: /dev/watchdog.
16 * Based on source code of the following authors:
17 * Matt Domsch <Matt_Domsch@dell.com>,
18 * Rob Radez <rob@osinvestor.com>,
19 * Rusty Lynch <rusty@linux.co.intel.com>
20 * Satyam Sharma <satyam@infradead.org>
21 * Randy Dunlap <randy.dunlap@oracle.com>
23 * This program is free software; you can redistribute it and/or
24 * modify it under the terms of the GNU General Public License
25 * as published by the Free Software Foundation; either version
26 * 2 of the License, or (at your option) any later version.
28 * Neither Alan Cox, CymruNet Ltd., Wim Van Sebroeck nor Iguana vzw.
29 * admit liability nor provide warranty for any of this software.
30 * This material is provided "AS-IS" and at no charge.
33 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
35 #include <linux/cdev.h> /* For character device */
36 #include <linux/errno.h> /* For the -ENODEV/... values */
37 #include <linux/fs.h> /* For file operations */
38 #include <linux/init.h> /* For __init/__exit/... */
39 #include <linux/hrtimer.h> /* For hrtimers */
40 #include <linux/kernel.h> /* For printk/panic/... */
41 #include <linux/kthread.h> /* For kthread_work */
42 #include <linux/miscdevice.h> /* For handling misc devices */
43 #include <linux/module.h> /* For module stuff/... */
44 #include <linux/mutex.h> /* For mutexes */
45 #include <linux/slab.h> /* For memory functions */
46 #include <linux/types.h> /* For standard types (like size_t) */
47 #include <linux/watchdog.h> /* For watchdog specific items */
48 #include <linux/uaccess.h> /* For copy_to_user/put_user/... */
50 #include <uapi/linux/sched/types.h> /* For struct sched_param */
52 #include "watchdog_core.h"
53 #include "watchdog_pretimeout.h"
56 * struct watchdog_core_data - watchdog core internal data
57 * @dev: The watchdog's internal device
58 * @cdev: The watchdog's Character device.
59 * @wdd: Pointer to watchdog device.
60 * @lock: Lock for watchdog core.
61 * @status: Watchdog core internal status bits.
63 struct watchdog_core_data
{
66 struct watchdog_device
*wdd
;
68 ktime_t last_keepalive
;
69 ktime_t last_hw_keepalive
;
71 struct kthread_work work
;
72 unsigned long status
; /* Internal status bits */
73 #define _WDOG_DEV_OPEN 0 /* Opened ? */
74 #define _WDOG_ALLOW_RELEASE 1 /* Did we receive the magic char ? */
75 #define _WDOG_KEEPALIVE 2 /* Did we receive a keepalive ? */
78 /* the dev_t structure to store the dynamically allocated watchdog devices */
79 static dev_t watchdog_devt
;
80 /* Reference to watchdog device behind /dev/watchdog */
81 static struct watchdog_core_data
*old_wd_data
;
83 static struct kthread_worker
*watchdog_kworker
;
85 static bool handle_boot_enabled
=
86 IS_ENABLED(CONFIG_WATCHDOG_HANDLE_BOOT_ENABLED
);
88 static inline bool watchdog_need_worker(struct watchdog_device
*wdd
)
90 /* All variables in milli-seconds */
91 unsigned int hm
= wdd
->max_hw_heartbeat_ms
;
92 unsigned int t
= wdd
->timeout
* 1000;
95 * A worker to generate heartbeat requests is needed if all of the
96 * following conditions are true.
97 * - Userspace activated the watchdog.
98 * - The driver provided a value for the maximum hardware timeout, and
99 * thus is aware that the framework supports generating heartbeat
101 * - Userspace requests a longer timeout than the hardware can handle.
103 * Alternatively, if userspace has not opened the watchdog
104 * device, we take care of feeding the watchdog if it is
107 return (hm
&& watchdog_active(wdd
) && t
> hm
) ||
108 (t
&& !watchdog_active(wdd
) && watchdog_hw_running(wdd
));
111 static ktime_t
watchdog_next_keepalive(struct watchdog_device
*wdd
)
113 struct watchdog_core_data
*wd_data
= wdd
->wd_data
;
114 unsigned int timeout_ms
= wdd
->timeout
* 1000;
115 ktime_t keepalive_interval
;
116 ktime_t last_heartbeat
, latest_heartbeat
;
117 ktime_t virt_timeout
;
118 unsigned int hw_heartbeat_ms
;
120 virt_timeout
= ktime_add(wd_data
->last_keepalive
,
121 ms_to_ktime(timeout_ms
));
122 hw_heartbeat_ms
= min_not_zero(timeout_ms
, wdd
->max_hw_heartbeat_ms
);
123 keepalive_interval
= ms_to_ktime(hw_heartbeat_ms
/ 2);
125 if (!watchdog_active(wdd
))
126 return keepalive_interval
;
129 * To ensure that the watchdog times out wdd->timeout seconds
130 * after the most recent ping from userspace, the last
131 * worker ping has to come in hw_heartbeat_ms before this timeout.
133 last_heartbeat
= ktime_sub(virt_timeout
, ms_to_ktime(hw_heartbeat_ms
));
134 latest_heartbeat
= ktime_sub(last_heartbeat
, ktime_get());
135 if (ktime_before(latest_heartbeat
, keepalive_interval
))
136 return latest_heartbeat
;
137 return keepalive_interval
;
140 static inline void watchdog_update_worker(struct watchdog_device
*wdd
)
142 struct watchdog_core_data
*wd_data
= wdd
->wd_data
;
144 if (watchdog_need_worker(wdd
)) {
145 ktime_t t
= watchdog_next_keepalive(wdd
);
148 hrtimer_start(&wd_data
->timer
, t
, HRTIMER_MODE_REL
);
150 hrtimer_cancel(&wd_data
->timer
);
154 static int __watchdog_ping(struct watchdog_device
*wdd
)
156 struct watchdog_core_data
*wd_data
= wdd
->wd_data
;
157 ktime_t earliest_keepalive
, now
;
160 earliest_keepalive
= ktime_add(wd_data
->last_hw_keepalive
,
161 ms_to_ktime(wdd
->min_hw_heartbeat_ms
));
164 if (ktime_after(earliest_keepalive
, now
)) {
165 hrtimer_start(&wd_data
->timer
,
166 ktime_sub(earliest_keepalive
, now
),
171 wd_data
->last_hw_keepalive
= now
;
174 err
= wdd
->ops
->ping(wdd
); /* ping the watchdog */
176 err
= wdd
->ops
->start(wdd
); /* restart watchdog */
178 watchdog_update_worker(wdd
);
184 * watchdog_ping: ping the watchdog.
185 * @wdd: the watchdog device to ping
187 * The caller must hold wd_data->lock.
189 * If the watchdog has no own ping operation then it needs to be
190 * restarted via the start operation. This wrapper function does
192 * We only ping when the watchdog device is running.
195 static int watchdog_ping(struct watchdog_device
*wdd
)
197 struct watchdog_core_data
*wd_data
= wdd
->wd_data
;
199 if (!watchdog_active(wdd
) && !watchdog_hw_running(wdd
))
202 set_bit(_WDOG_KEEPALIVE
, &wd_data
->status
);
204 wd_data
->last_keepalive
= ktime_get();
205 return __watchdog_ping(wdd
);
208 static bool watchdog_worker_should_ping(struct watchdog_core_data
*wd_data
)
210 struct watchdog_device
*wdd
= wd_data
->wdd
;
212 return wdd
&& (watchdog_active(wdd
) || watchdog_hw_running(wdd
));
215 static void watchdog_ping_work(struct kthread_work
*work
)
217 struct watchdog_core_data
*wd_data
;
219 wd_data
= container_of(work
, struct watchdog_core_data
, work
);
221 mutex_lock(&wd_data
->lock
);
222 if (watchdog_worker_should_ping(wd_data
))
223 __watchdog_ping(wd_data
->wdd
);
224 mutex_unlock(&wd_data
->lock
);
227 static enum hrtimer_restart
watchdog_timer_expired(struct hrtimer
*timer
)
229 struct watchdog_core_data
*wd_data
;
231 wd_data
= container_of(timer
, struct watchdog_core_data
, timer
);
233 kthread_queue_work(watchdog_kworker
, &wd_data
->work
);
234 return HRTIMER_NORESTART
;
238 * watchdog_start: wrapper to start the watchdog.
239 * @wdd: the watchdog device to start
241 * The caller must hold wd_data->lock.
243 * Start the watchdog if it is not active and mark it active.
244 * This function returns zero on success or a negative errno code for
248 static int watchdog_start(struct watchdog_device
*wdd
)
250 struct watchdog_core_data
*wd_data
= wdd
->wd_data
;
254 if (watchdog_active(wdd
))
257 set_bit(_WDOG_KEEPALIVE
, &wd_data
->status
);
259 started_at
= ktime_get();
260 if (watchdog_hw_running(wdd
) && wdd
->ops
->ping
)
261 err
= wdd
->ops
->ping(wdd
);
263 err
= wdd
->ops
->start(wdd
);
265 set_bit(WDOG_ACTIVE
, &wdd
->status
);
266 wd_data
->last_keepalive
= started_at
;
267 watchdog_update_worker(wdd
);
274 * watchdog_stop: wrapper to stop the watchdog.
275 * @wdd: the watchdog device to stop
277 * The caller must hold wd_data->lock.
279 * Stop the watchdog if it is still active and unmark it active.
280 * This function returns zero on success or a negative errno code for
282 * If the 'nowayout' feature was set, the watchdog cannot be stopped.
285 static int watchdog_stop(struct watchdog_device
*wdd
)
289 if (!watchdog_active(wdd
))
292 if (test_bit(WDOG_NO_WAY_OUT
, &wdd
->status
)) {
293 pr_info("watchdog%d: nowayout prevents watchdog being stopped!\n",
298 if (wdd
->ops
->stop
) {
299 clear_bit(WDOG_HW_RUNNING
, &wdd
->status
);
300 err
= wdd
->ops
->stop(wdd
);
302 set_bit(WDOG_HW_RUNNING
, &wdd
->status
);
306 clear_bit(WDOG_ACTIVE
, &wdd
->status
);
307 watchdog_update_worker(wdd
);
314 * watchdog_get_status: wrapper to get the watchdog status
315 * @wdd: the watchdog device to get the status from
317 * The caller must hold wd_data->lock.
319 * Get the watchdog's status flags.
322 static unsigned int watchdog_get_status(struct watchdog_device
*wdd
)
324 struct watchdog_core_data
*wd_data
= wdd
->wd_data
;
327 if (wdd
->ops
->status
)
328 status
= wdd
->ops
->status(wdd
);
330 status
= wdd
->bootstatus
& (WDIOF_CARDRESET
|
338 if (test_bit(_WDOG_ALLOW_RELEASE
, &wd_data
->status
))
339 status
|= WDIOF_MAGICCLOSE
;
341 if (test_and_clear_bit(_WDOG_KEEPALIVE
, &wd_data
->status
))
342 status
|= WDIOF_KEEPALIVEPING
;
348 * watchdog_set_timeout: set the watchdog timer timeout
349 * @wdd: the watchdog device to set the timeout for
350 * @timeout: timeout to set in seconds
352 * The caller must hold wd_data->lock.
355 static int watchdog_set_timeout(struct watchdog_device
*wdd
,
356 unsigned int timeout
)
360 if (!(wdd
->info
->options
& WDIOF_SETTIMEOUT
))
363 if (watchdog_timeout_invalid(wdd
, timeout
))
366 if (wdd
->ops
->set_timeout
) {
367 err
= wdd
->ops
->set_timeout(wdd
, timeout
);
369 wdd
->timeout
= timeout
;
370 /* Disable pretimeout if it doesn't fit the new timeout */
371 if (wdd
->pretimeout
>= wdd
->timeout
)
375 watchdog_update_worker(wdd
);
381 * watchdog_set_pretimeout: set the watchdog timer pretimeout
382 * @wdd: the watchdog device to set the timeout for
383 * @timeout: pretimeout to set in seconds
386 static int watchdog_set_pretimeout(struct watchdog_device
*wdd
,
387 unsigned int timeout
)
391 if (!(wdd
->info
->options
& WDIOF_PRETIMEOUT
))
394 if (watchdog_pretimeout_invalid(wdd
, timeout
))
397 if (wdd
->ops
->set_pretimeout
)
398 err
= wdd
->ops
->set_pretimeout(wdd
, timeout
);
400 wdd
->pretimeout
= timeout
;
406 * watchdog_get_timeleft: wrapper to get the time left before a reboot
407 * @wdd: the watchdog device to get the remaining time from
408 * @timeleft: the time that's left
410 * The caller must hold wd_data->lock.
412 * Get the time before a watchdog will reboot (if not pinged).
415 static int watchdog_get_timeleft(struct watchdog_device
*wdd
,
416 unsigned int *timeleft
)
420 if (!wdd
->ops
->get_timeleft
)
423 *timeleft
= wdd
->ops
->get_timeleft(wdd
);
428 #ifdef CONFIG_WATCHDOG_SYSFS
429 static ssize_t
nowayout_show(struct device
*dev
, struct device_attribute
*attr
,
432 struct watchdog_device
*wdd
= dev_get_drvdata(dev
);
434 return sprintf(buf
, "%d\n", !!test_bit(WDOG_NO_WAY_OUT
, &wdd
->status
));
436 static DEVICE_ATTR_RO(nowayout
);
438 static ssize_t
status_show(struct device
*dev
, struct device_attribute
*attr
,
441 struct watchdog_device
*wdd
= dev_get_drvdata(dev
);
442 struct watchdog_core_data
*wd_data
= wdd
->wd_data
;
445 mutex_lock(&wd_data
->lock
);
446 status
= watchdog_get_status(wdd
);
447 mutex_unlock(&wd_data
->lock
);
449 return sprintf(buf
, "0x%x\n", status
);
451 static DEVICE_ATTR_RO(status
);
453 static ssize_t
bootstatus_show(struct device
*dev
,
454 struct device_attribute
*attr
, char *buf
)
456 struct watchdog_device
*wdd
= dev_get_drvdata(dev
);
458 return sprintf(buf
, "%u\n", wdd
->bootstatus
);
460 static DEVICE_ATTR_RO(bootstatus
);
462 static ssize_t
timeleft_show(struct device
*dev
, struct device_attribute
*attr
,
465 struct watchdog_device
*wdd
= dev_get_drvdata(dev
);
466 struct watchdog_core_data
*wd_data
= wdd
->wd_data
;
470 mutex_lock(&wd_data
->lock
);
471 status
= watchdog_get_timeleft(wdd
, &val
);
472 mutex_unlock(&wd_data
->lock
);
474 status
= sprintf(buf
, "%u\n", val
);
478 static DEVICE_ATTR_RO(timeleft
);
480 static ssize_t
timeout_show(struct device
*dev
, struct device_attribute
*attr
,
483 struct watchdog_device
*wdd
= dev_get_drvdata(dev
);
485 return sprintf(buf
, "%u\n", wdd
->timeout
);
487 static DEVICE_ATTR_RO(timeout
);
489 static ssize_t
pretimeout_show(struct device
*dev
,
490 struct device_attribute
*attr
, char *buf
)
492 struct watchdog_device
*wdd
= dev_get_drvdata(dev
);
494 return sprintf(buf
, "%u\n", wdd
->pretimeout
);
496 static DEVICE_ATTR_RO(pretimeout
);
498 static ssize_t
identity_show(struct device
*dev
, struct device_attribute
*attr
,
501 struct watchdog_device
*wdd
= dev_get_drvdata(dev
);
503 return sprintf(buf
, "%s\n", wdd
->info
->identity
);
505 static DEVICE_ATTR_RO(identity
);
507 static ssize_t
state_show(struct device
*dev
, struct device_attribute
*attr
,
510 struct watchdog_device
*wdd
= dev_get_drvdata(dev
);
512 if (watchdog_active(wdd
))
513 return sprintf(buf
, "active\n");
515 return sprintf(buf
, "inactive\n");
517 static DEVICE_ATTR_RO(state
);
519 static ssize_t
pretimeout_available_governors_show(struct device
*dev
,
520 struct device_attribute
*attr
, char *buf
)
522 return watchdog_pretimeout_available_governors_get(buf
);
524 static DEVICE_ATTR_RO(pretimeout_available_governors
);
526 static ssize_t
pretimeout_governor_show(struct device
*dev
,
527 struct device_attribute
*attr
,
530 struct watchdog_device
*wdd
= dev_get_drvdata(dev
);
532 return watchdog_pretimeout_governor_get(wdd
, buf
);
535 static ssize_t
pretimeout_governor_store(struct device
*dev
,
536 struct device_attribute
*attr
,
537 const char *buf
, size_t count
)
539 struct watchdog_device
*wdd
= dev_get_drvdata(dev
);
540 int ret
= watchdog_pretimeout_governor_set(wdd
, buf
);
547 static DEVICE_ATTR_RW(pretimeout_governor
);
549 static umode_t
wdt_is_visible(struct kobject
*kobj
, struct attribute
*attr
,
552 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
553 struct watchdog_device
*wdd
= dev_get_drvdata(dev
);
554 umode_t mode
= attr
->mode
;
556 if (attr
== &dev_attr_timeleft
.attr
&& !wdd
->ops
->get_timeleft
)
558 else if (attr
== &dev_attr_pretimeout
.attr
&&
559 !(wdd
->info
->options
& WDIOF_PRETIMEOUT
))
561 else if ((attr
== &dev_attr_pretimeout_governor
.attr
||
562 attr
== &dev_attr_pretimeout_available_governors
.attr
) &&
563 (!(wdd
->info
->options
& WDIOF_PRETIMEOUT
) ||
564 !IS_ENABLED(CONFIG_WATCHDOG_PRETIMEOUT_GOV
)))
569 static struct attribute
*wdt_attrs
[] = {
570 &dev_attr_state
.attr
,
571 &dev_attr_identity
.attr
,
572 &dev_attr_timeout
.attr
,
573 &dev_attr_pretimeout
.attr
,
574 &dev_attr_timeleft
.attr
,
575 &dev_attr_bootstatus
.attr
,
576 &dev_attr_status
.attr
,
577 &dev_attr_nowayout
.attr
,
578 &dev_attr_pretimeout_governor
.attr
,
579 &dev_attr_pretimeout_available_governors
.attr
,
583 static const struct attribute_group wdt_group
= {
585 .is_visible
= wdt_is_visible
,
587 __ATTRIBUTE_GROUPS(wdt
);
589 #define wdt_groups NULL
593 * watchdog_ioctl_op: call the watchdog drivers ioctl op if defined
594 * @wdd: the watchdog device to do the ioctl on
595 * @cmd: watchdog command
596 * @arg: argument pointer
598 * The caller must hold wd_data->lock.
601 static int watchdog_ioctl_op(struct watchdog_device
*wdd
, unsigned int cmd
,
604 if (!wdd
->ops
->ioctl
)
607 return wdd
->ops
->ioctl(wdd
, cmd
, arg
);
611 * watchdog_write: writes to the watchdog.
612 * @file: file from VFS
613 * @data: user address of data
614 * @len: length of data
615 * @ppos: pointer to the file offset
617 * A write to a watchdog device is defined as a keepalive ping.
618 * Writing the magic 'V' sequence allows the next close to turn
619 * off the watchdog (if 'nowayout' is not set).
622 static ssize_t
watchdog_write(struct file
*file
, const char __user
*data
,
623 size_t len
, loff_t
*ppos
)
625 struct watchdog_core_data
*wd_data
= file
->private_data
;
626 struct watchdog_device
*wdd
;
635 * Note: just in case someone wrote the magic character
638 clear_bit(_WDOG_ALLOW_RELEASE
, &wd_data
->status
);
640 /* scan to see whether or not we got the magic character */
641 for (i
= 0; i
!= len
; i
++) {
642 if (get_user(c
, data
+ i
))
645 set_bit(_WDOG_ALLOW_RELEASE
, &wd_data
->status
);
648 /* someone wrote to us, so we send the watchdog a keepalive ping */
651 mutex_lock(&wd_data
->lock
);
654 err
= watchdog_ping(wdd
);
655 mutex_unlock(&wd_data
->lock
);
664 * watchdog_ioctl: handle the different ioctl's for the watchdog device.
665 * @file: file handle to the device
666 * @cmd: watchdog command
667 * @arg: argument pointer
669 * The watchdog API defines a common set of functions for all watchdogs
670 * according to their available features.
673 static long watchdog_ioctl(struct file
*file
, unsigned int cmd
,
676 struct watchdog_core_data
*wd_data
= file
->private_data
;
677 void __user
*argp
= (void __user
*)arg
;
678 struct watchdog_device
*wdd
;
679 int __user
*p
= argp
;
683 mutex_lock(&wd_data
->lock
);
691 err
= watchdog_ioctl_op(wdd
, cmd
, arg
);
692 if (err
!= -ENOIOCTLCMD
)
696 case WDIOC_GETSUPPORT
:
697 err
= copy_to_user(argp
, wdd
->info
,
698 sizeof(struct watchdog_info
)) ? -EFAULT
: 0;
700 case WDIOC_GETSTATUS
:
701 val
= watchdog_get_status(wdd
);
702 err
= put_user(val
, p
);
704 case WDIOC_GETBOOTSTATUS
:
705 err
= put_user(wdd
->bootstatus
, p
);
707 case WDIOC_SETOPTIONS
:
708 if (get_user(val
, p
)) {
712 if (val
& WDIOS_DISABLECARD
) {
713 err
= watchdog_stop(wdd
);
717 if (val
& WDIOS_ENABLECARD
)
718 err
= watchdog_start(wdd
);
720 case WDIOC_KEEPALIVE
:
721 if (!(wdd
->info
->options
& WDIOF_KEEPALIVEPING
)) {
725 err
= watchdog_ping(wdd
);
727 case WDIOC_SETTIMEOUT
:
728 if (get_user(val
, p
)) {
732 err
= watchdog_set_timeout(wdd
, val
);
735 /* If the watchdog is active then we send a keepalive ping
736 * to make sure that the watchdog keep's running (and if
737 * possible that it takes the new timeout) */
738 err
= watchdog_ping(wdd
);
742 case WDIOC_GETTIMEOUT
:
743 /* timeout == 0 means that we don't know the timeout */
744 if (wdd
->timeout
== 0) {
748 err
= put_user(wdd
->timeout
, p
);
750 case WDIOC_GETTIMELEFT
:
751 err
= watchdog_get_timeleft(wdd
, &val
);
754 err
= put_user(val
, p
);
756 case WDIOC_SETPRETIMEOUT
:
757 if (get_user(val
, p
)) {
761 err
= watchdog_set_pretimeout(wdd
, val
);
763 case WDIOC_GETPRETIMEOUT
:
764 err
= put_user(wdd
->pretimeout
, p
);
772 mutex_unlock(&wd_data
->lock
);
777 * watchdog_open: open the /dev/watchdog* devices.
778 * @inode: inode of device
779 * @file: file handle to device
781 * When the /dev/watchdog* device gets opened, we start the watchdog.
782 * Watch out: the /dev/watchdog device is single open, so we make sure
783 * it can only be opened once.
786 static int watchdog_open(struct inode
*inode
, struct file
*file
)
788 struct watchdog_core_data
*wd_data
;
789 struct watchdog_device
*wdd
;
793 /* Get the corresponding watchdog device */
794 if (imajor(inode
) == MISC_MAJOR
)
795 wd_data
= old_wd_data
;
797 wd_data
= container_of(inode
->i_cdev
, struct watchdog_core_data
,
800 /* the watchdog is single open! */
801 if (test_and_set_bit(_WDOG_DEV_OPEN
, &wd_data
->status
))
807 * If the /dev/watchdog device is open, we don't want the module
810 hw_running
= watchdog_hw_running(wdd
);
811 if (!hw_running
&& !try_module_get(wdd
->ops
->owner
)) {
816 err
= watchdog_start(wdd
);
820 file
->private_data
= wd_data
;
823 get_device(&wd_data
->dev
);
825 /* dev/watchdog is a virtual (and thus non-seekable) filesystem */
826 return nonseekable_open(inode
, file
);
829 module_put(wd_data
->wdd
->ops
->owner
);
831 clear_bit(_WDOG_DEV_OPEN
, &wd_data
->status
);
835 static void watchdog_core_data_release(struct device
*dev
)
837 struct watchdog_core_data
*wd_data
;
839 wd_data
= container_of(dev
, struct watchdog_core_data
, dev
);
845 * watchdog_release: release the watchdog device.
846 * @inode: inode of device
847 * @file: file handle to device
849 * This is the code for when /dev/watchdog gets closed. We will only
850 * stop the watchdog when we have received the magic char (and nowayout
851 * was not set), else the watchdog will keep running.
854 static int watchdog_release(struct inode
*inode
, struct file
*file
)
856 struct watchdog_core_data
*wd_data
= file
->private_data
;
857 struct watchdog_device
*wdd
;
861 mutex_lock(&wd_data
->lock
);
868 * We only stop the watchdog if we received the magic character
869 * or if WDIOF_MAGICCLOSE is not set. If nowayout was set then
870 * watchdog_stop will fail.
872 if (!test_bit(WDOG_ACTIVE
, &wdd
->status
))
874 else if (test_and_clear_bit(_WDOG_ALLOW_RELEASE
, &wd_data
->status
) ||
875 !(wdd
->info
->options
& WDIOF_MAGICCLOSE
))
876 err
= watchdog_stop(wdd
);
878 /* If the watchdog was not stopped, send a keepalive ping */
880 pr_crit("watchdog%d: watchdog did not stop!\n", wdd
->id
);
884 watchdog_update_worker(wdd
);
886 /* make sure that /dev/watchdog can be re-opened */
887 clear_bit(_WDOG_DEV_OPEN
, &wd_data
->status
);
890 running
= wdd
&& watchdog_hw_running(wdd
);
891 mutex_unlock(&wd_data
->lock
);
893 * Allow the owner module to be unloaded again unless the watchdog
894 * is still running. If the watchdog is still running, it can not
895 * be stopped, and its driver must not be unloaded.
898 module_put(wd_data
->cdev
.owner
);
899 put_device(&wd_data
->dev
);
904 static const struct file_operations watchdog_fops
= {
905 .owner
= THIS_MODULE
,
906 .write
= watchdog_write
,
907 .unlocked_ioctl
= watchdog_ioctl
,
908 .open
= watchdog_open
,
909 .release
= watchdog_release
,
912 static struct miscdevice watchdog_miscdev
= {
913 .minor
= WATCHDOG_MINOR
,
915 .fops
= &watchdog_fops
,
918 static struct class watchdog_class
= {
920 .owner
= THIS_MODULE
,
921 .dev_groups
= wdt_groups
,
925 * watchdog_cdev_register: register watchdog character device
926 * @wdd: watchdog device
928 * Register a watchdog character device including handling the legacy
929 * /dev/watchdog node. /dev/watchdog is actually a miscdevice and
930 * thus we set it up like that.
933 static int watchdog_cdev_register(struct watchdog_device
*wdd
)
935 struct watchdog_core_data
*wd_data
;
938 wd_data
= kzalloc(sizeof(struct watchdog_core_data
), GFP_KERNEL
);
941 mutex_init(&wd_data
->lock
);
944 wdd
->wd_data
= wd_data
;
946 if (IS_ERR_OR_NULL(watchdog_kworker
))
949 kthread_init_work(&wd_data
->work
, watchdog_ping_work
);
950 hrtimer_init(&wd_data
->timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
951 wd_data
->timer
.function
= watchdog_timer_expired
;
954 old_wd_data
= wd_data
;
955 watchdog_miscdev
.parent
= wdd
->parent
;
956 err
= misc_register(&watchdog_miscdev
);
958 pr_err("%s: cannot register miscdev on minor=%d (err=%d).\n",
959 wdd
->info
->identity
, WATCHDOG_MINOR
, err
);
961 pr_err("%s: a legacy watchdog module is probably present.\n",
962 wdd
->info
->identity
);
969 device_initialize(&wd_data
->dev
);
970 wd_data
->dev
.devt
= MKDEV(MAJOR(watchdog_devt
), wdd
->id
);
971 wd_data
->dev
.class = &watchdog_class
;
972 wd_data
->dev
.parent
= wdd
->parent
;
973 wd_data
->dev
.groups
= wdd
->groups
;
974 wd_data
->dev
.release
= watchdog_core_data_release
;
975 dev_set_drvdata(&wd_data
->dev
, wdd
);
976 dev_set_name(&wd_data
->dev
, "watchdog%d", wdd
->id
);
978 /* Fill in the data structures */
979 cdev_init(&wd_data
->cdev
, &watchdog_fops
);
982 err
= cdev_device_add(&wd_data
->cdev
, &wd_data
->dev
);
984 pr_err("watchdog%d unable to add device %d:%d\n",
985 wdd
->id
, MAJOR(watchdog_devt
), wdd
->id
);
987 misc_deregister(&watchdog_miscdev
);
989 put_device(&wd_data
->dev
);
994 wd_data
->cdev
.owner
= wdd
->ops
->owner
;
996 /* Record time of most recent heartbeat as 'just before now'. */
997 wd_data
->last_hw_keepalive
= ktime_sub(ktime_get(), 1);
1000 * If the watchdog is running, prevent its driver from being unloaded,
1001 * and schedule an immediate ping.
1003 if (watchdog_hw_running(wdd
)) {
1004 __module_get(wdd
->ops
->owner
);
1005 get_device(&wd_data
->dev
);
1006 if (handle_boot_enabled
)
1007 hrtimer_start(&wd_data
->timer
, 0, HRTIMER_MODE_REL
);
1009 pr_info("watchdog%d running and kernel based pre-userspace handler disabled\n",
1017 * watchdog_cdev_unregister: unregister watchdog character device
1018 * @watchdog: watchdog device
1020 * Unregister watchdog character device and if needed the legacy
1021 * /dev/watchdog device.
1024 static void watchdog_cdev_unregister(struct watchdog_device
*wdd
)
1026 struct watchdog_core_data
*wd_data
= wdd
->wd_data
;
1028 cdev_device_del(&wd_data
->cdev
, &wd_data
->dev
);
1030 misc_deregister(&watchdog_miscdev
);
1034 if (watchdog_active(wdd
) &&
1035 test_bit(WDOG_STOP_ON_UNREGISTER
, &wdd
->status
)) {
1039 mutex_lock(&wd_data
->lock
);
1040 wd_data
->wdd
= NULL
;
1041 wdd
->wd_data
= NULL
;
1042 mutex_unlock(&wd_data
->lock
);
1044 hrtimer_cancel(&wd_data
->timer
);
1045 kthread_cancel_work_sync(&wd_data
->work
);
1047 put_device(&wd_data
->dev
);
1051 * watchdog_dev_register: register a watchdog device
1052 * @wdd: watchdog device
1054 * Register a watchdog device including handling the legacy
1055 * /dev/watchdog node. /dev/watchdog is actually a miscdevice and
1056 * thus we set it up like that.
1059 int watchdog_dev_register(struct watchdog_device
*wdd
)
1063 ret
= watchdog_cdev_register(wdd
);
1067 ret
= watchdog_register_pretimeout(wdd
);
1069 watchdog_cdev_unregister(wdd
);
1075 * watchdog_dev_unregister: unregister a watchdog device
1076 * @watchdog: watchdog device
1078 * Unregister watchdog device and if needed the legacy
1079 * /dev/watchdog device.
1082 void watchdog_dev_unregister(struct watchdog_device
*wdd
)
1084 watchdog_unregister_pretimeout(wdd
);
1085 watchdog_cdev_unregister(wdd
);
1089 * watchdog_dev_init: init dev part of watchdog core
1091 * Allocate a range of chardev nodes to use for watchdog devices
1094 int __init
watchdog_dev_init(void)
1097 struct sched_param param
= {.sched_priority
= MAX_RT_PRIO
- 1,};
1099 watchdog_kworker
= kthread_create_worker(0, "watchdogd");
1100 if (IS_ERR(watchdog_kworker
)) {
1101 pr_err("Failed to create watchdog kworker\n");
1102 return PTR_ERR(watchdog_kworker
);
1104 sched_setscheduler(watchdog_kworker
->task
, SCHED_FIFO
, ¶m
);
1106 err
= class_register(&watchdog_class
);
1108 pr_err("couldn't register class\n");
1112 err
= alloc_chrdev_region(&watchdog_devt
, 0, MAX_DOGS
, "watchdog");
1114 pr_err("watchdog: unable to allocate char dev region\n");
1121 class_unregister(&watchdog_class
);
1123 kthread_destroy_worker(watchdog_kworker
);
1128 * watchdog_dev_exit: exit dev part of watchdog core
1130 * Release the range of chardev nodes used for watchdog devices
1133 void __exit
watchdog_dev_exit(void)
1135 unregister_chrdev_region(watchdog_devt
, MAX_DOGS
);
1136 class_unregister(&watchdog_class
);
1137 kthread_destroy_worker(watchdog_kworker
);
1140 module_param(handle_boot_enabled
, bool, 0444);
1141 MODULE_PARM_DESC(handle_boot_enabled
,
1142 "Watchdog core auto-updates boot enabled watchdogs before userspace takes over (default="
1143 __MODULE_STRING(IS_ENABLED(CONFIG_WATCHDOG_HANDLE_BOOT_ENABLED
)) ")");