From: Guenter Roeck <linux@roeck-us.net>
To: linux-watchdog@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, Guenter Roeck <linux@roeck-us.net>
Subject: [PATCH 2/8] watchdog: core: Add missing locks
Date: Tue, 29 Sep 2026 06:46:29 -0700 [thread overview]
Message-ID: <20260929134635.2567137-3-linux@roeck-us.net> (raw)
In-Reply-To: <20260929134635.2567137-1-linux@roeck-us.net>
Add missing locks to watchdog_open(), watchdog_cdev_unregister(), and
watchdog_set_last_hw_keepalive(). Also protect old_wd_data with its own
lock to prevent its modification while in use, acquiring wd_data->lock
before releasing old_wd_data_lock in watchdog_open() to avoid a TOCTOU
race, and keeping misc_deregister() outside old_wd_data_lock to avoid
lock ordering inversion with misc_mtx.
Fixes: b4ffb1909843 ("watchdog: Separate and maintain variables based on variable lifetime")
Assisted-by: LLM
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
---
drivers/watchdog/watchdog_dev.c | 42 ++++++++++++++++++++++++++++-----
1 file changed, 36 insertions(+), 6 deletions(-)
diff --git a/drivers/watchdog/watchdog_dev.c b/drivers/watchdog/watchdog_dev.c
index ed5b35184271..50224877ff49 100644
--- a/drivers/watchdog/watchdog_dev.c
+++ b/drivers/watchdog/watchdog_dev.c
@@ -55,6 +55,7 @@
static dev_t watchdog_devt;
/* Reference to watchdog device behind /dev/watchdog */
static struct watchdog_core_data *old_wd_data;
+static DEFINE_MUTEX(old_wd_data_lock);
static struct kthread_worker *watchdog_kworker;
@@ -869,17 +870,32 @@ static int watchdog_open(struct inode *inode, struct file *file)
int err;
/* Get the corresponding watchdog device */
- if (imajor(inode) == MISC_MAJOR)
+ if (imajor(inode) == MISC_MAJOR) {
+ mutex_lock(&old_wd_data_lock);
wd_data = old_wd_data;
- else
+ if (!wd_data) {
+ mutex_unlock(&old_wd_data_lock);
+ return -ENODEV;
+ }
+ mutex_lock(&wd_data->lock);
+ mutex_unlock(&old_wd_data_lock);
+ } else {
wd_data = container_of(inode->i_cdev, struct watchdog_core_data,
cdev);
+ mutex_lock(&wd_data->lock);
+ }
/* the watchdog is single open! */
- if (test_and_set_bit(_WDOG_DEV_OPEN, &wd_data->status))
+ if (test_and_set_bit(_WDOG_DEV_OPEN, &wd_data->status)) {
+ mutex_unlock(&wd_data->lock);
return -EBUSY;
+ }
wdd = wd_data->wdd;
+ if (!wdd) {
+ err = -ENODEV;
+ goto out_clear;
+ }
/*
* If the /dev/watchdog device is open, we don't want the module
@@ -909,6 +925,8 @@ static int watchdog_open(struct inode *inode, struct file *file)
*/
wd_data->open_deadline = KTIME_MAX;
+ mutex_unlock(&wd_data->lock);
+
/* dev/watchdog is a virtual (and thus non-seekable) filesystem */
return stream_open(inode, file);
@@ -916,6 +934,7 @@ static int watchdog_open(struct inode *inode, struct file *file)
module_put(wd_data->wdd->ops->owner);
out_clear:
clear_bit(_WDOG_DEV_OPEN, &wd_data->status);
+ mutex_unlock(&wd_data->lock);
return err;
}
@@ -1085,7 +1104,9 @@ static int watchdog_cdev_register(struct watchdog_device *wdd)
wdd->id, MAJOR(watchdog_devt), wdd->id);
if (wdd->id == 0) {
misc_deregister(&watchdog_miscdev);
+ mutex_lock(&old_wd_data_lock);
old_wd_data = NULL;
+ mutex_unlock(&old_wd_data_lock);
}
mutex_lock(&wd_data->lock);
wd_data->wdd = NULL;
@@ -1131,9 +1152,12 @@ static void watchdog_cdev_unregister(struct watchdog_device *wdd)
cdev_device_del(&wd_data->cdev, &wd_data->dev);
if (wdd->id == 0) {
misc_deregister(&watchdog_miscdev);
+ mutex_lock(&old_wd_data_lock);
old_wd_data = NULL;
+ mutex_unlock(&old_wd_data_lock);
}
+ mutex_lock(&wd_data->lock);
if (watchdog_active(wdd) &&
test_bit(WDOG_STOP_ON_UNREGISTER, &wdd->status)) {
watchdog_stop(wdd);
@@ -1141,7 +1165,6 @@ static void watchdog_cdev_unregister(struct watchdog_device *wdd)
watchdog_hrtimer_pretimeout_stop(wdd);
- mutex_lock(&wd_data->lock);
wd_data->wdd = NULL;
wdd->wd_data = NULL;
mutex_unlock(&wd_data->lock);
@@ -1208,20 +1231,27 @@ int watchdog_set_last_hw_keepalive(struct watchdog_device *wdd,
{
struct watchdog_core_data *wd_data;
ktime_t now;
+ int ret = 0;
if (!wdd)
return -EINVAL;
wd_data = wdd->wd_data;
+ if (!wd_data)
+ return -ENODEV;
+
+ mutex_lock(&wd_data->lock);
now = ktime_get();
wd_data->last_hw_keepalive = ktime_sub(now, ms_to_ktime(last_ping_ms));
if (watchdog_hw_running(wdd) && handle_boot_enabled)
- return __watchdog_ping(wdd);
+ ret = __watchdog_ping(wdd);
- return 0;
+ mutex_unlock(&wd_data->lock);
+
+ return ret;
}
EXPORT_SYMBOL_GPL(watchdog_set_last_hw_keepalive);
--
2.45.2
next prev parent reply other threads:[~2026-09-29 13:46 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-29 13:46 [PATCH 0/8] watchdog: core: Fix locking, lifetime, suspend, and state management bugs Guenter Roeck
2026-09-29 13:46 ` [PATCH 1/8] watchdog: core: Clear wd_data pointer on errors Guenter Roeck
2026-09-29 13:46 ` Guenter Roeck [this message]
2026-09-29 13:46 ` [PATCH 3/8] watchdog: core: Prevent ping worker from re-arming timer on suspend Guenter Roeck
2026-09-29 13:46 ` [PATCH 4/8] watchdog: core: Stop pretimeout hrtimer " Guenter Roeck
2026-09-29 13:46 ` [PATCH 5/8] watchdog: core: Restore WDOG_HW_RUNNING if stopping watchdog fails Guenter Roeck
2026-09-29 13:46 ` [PATCH 6/8] watchdog: core: Cancel timer if cdev_device_add() fails Guenter Roeck
2026-09-29 13:46 ` [PATCH 7/8] watchdog: core: Fix unbalanced module_put() in watchdog_open() Guenter Roeck
2026-09-29 13:46 ` [PATCH 8/8] watchdog: core: Update last_keepalive in watchdog_start() Guenter Roeck
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260929134635.2567137-3-linux@roeck-us.net \
--to=linux@roeck-us.net \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-watchdog@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®