Skip to contentSkip to conversation

watchdog: core: Cancel timer if cdev_device_add() fails

PATCH In review Guenter Roeck 1 participant 3 messages Oct 27 Watchdog device drivers

lore
Share & tools
lore

Guenter Roeck

author maintainer
#0 lore raw

PATCH v2 6/8 watchdog: core: Cancel timer if cdev_device_add() fails

If misc_register() exposed the device to userspace before cdev_device_add() is called, a concurrent watchdog_open() could start the watchdog and arm wd_data->timer as well as the pretimeout timer. Also, if the hardware watchdog was already running, watchdog_open() expects the device and module references to have been acquired prior to opening.

If cdev_device_add() then fails, the error path drops the device reference but fails to stop the watchdog, cancel the timers, and stop the worker, leaving the watchdog active and timers armed that can later fire and dereference freed memory. Furthermore, if a concurrent watchdog_open() saw hw_running == true before cdev_device_add() was called, it skipped taking its own device reference, allowing put_device() on the error path to free wd_data while the file descriptor is still open. Similarly, when unregistering a running watchdog that is not currently open, the extra hw_running module and device references were never released.

Fix the problem by initializing wd_data and taking the running-watchdog references before exposing the device via misc_register(), stopping the watchdog and canceling both the heartbeat and pretimeout timers and stopping the worker on registration failure, and releasing unclaimed running-watchdog references on registration failure and unregistration.

Fixes ee142889e32f ("watchdog: Introduce WDOG_HW_RUNNING flag")
Assisted-by LLM

---
v2: Added Reviewed-by: tag

 drivers/watchdog/watchdog_dev.c | 91 ++++++++++++++++++++-------------
 1 file changed, 55 insertions(+), 36 deletions(-)
drivers/watchdog/watchdog_dev.c +55 -36
index 47b0735ed6ac..ef2254188f7f 100644
@@ -1046,6 +1046,7 @@ static const struct class watchdog_class = {
 static int watchdog_cdev_register(struct watchdog_device *wdd)
 {
 	struct watchdog_core_data *wd_data;
+	bool hw_running;
 	int err;
 
 	wd_data = kzalloc_obj(struct watchdog_core_data);
@@ -1081,46 +1082,10 @@ static int watchdog_cdev_register(struct watchdog_device *wdd)
 		      HRTIMER_MODE_REL_HARD);
 	watchdog_hrtimer_pretimeout_init(wdd);
 
-	if (wdd->id == 0) {
-		old_wd_data = wd_data;
-		watchdog_miscdev.parent = wdd->parent;
-		err = misc_register(&watchdog_miscdev);
-		if (err != 0) {
-			pr_err("%s: cannot register miscdev on minor=%d (err=%d).\n",
-				wdd->info->identity, WATCHDOG_MINOR, err);
-			if (err == -EBUSY)
-				pr_err("%s: a legacy watchdog module is probably present.\n",
-					wdd->info->identity);
-			old_wd_data = NULL;
-			wdd->wd_data = NULL;
-			put_device(&wd_data->dev);
-			return err;
-		}
-	}
-
 	/* Fill in the data structures */
 	cdev_init(&wd_data->cdev, &watchdog_fops);
 	wd_data->cdev.owner = wdd->ops->owner;
 
-	/* Add the device */
-	err = cdev_device_add(&wd_data->cdev, &wd_data->dev);
-	if (err) {
-		pr_err("watchdog%d unable to add device %d:%d\n",
-			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;
-		wdd->wd_data = NULL;
-		mutex_unlock(&wd_data->lock);
-		put_device(&wd_data->dev);
-		return err;
-	}
-
 	/* Record time of most recent heartbeat as 'just before now'. */
 	wd_data->last_hw_keepalive = ktime_sub(ktime_get(), 1);
 	watchdog_set_open_deadline(wd_data);
@@ -1140,7 +1105,55 @@ static int watchdog_cdev_register(struct watchdog_device *wdd)
 				wdd->id);
 	}
 
+	if (wdd->id == 0) {
+		old_wd_data = wd_data;
+		watchdog_miscdev.parent = wdd->parent;
+		err = misc_register(&watchdog_miscdev);
+		if (err != 0) {
+			pr_err("%s: cannot register miscdev on minor=%d (err=%d).\n",
+				wdd->info->identity, WATCHDOG_MINOR, err);
+			if (err == -EBUSY)
+				pr_err("%s: a legacy watchdog module is probably present.\n",
+					wdd->info->identity);
+			old_wd_data = NULL;
+			goto err_clear;
+		}
+	}
+
+	/* Add the device */
+	err = cdev_device_add(&wd_data->cdev, &wd_data->dev);
+	if (err) {
+		pr_err("watchdog%d unable to add device %d:%d\n",
+			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);
+		}
+		goto err_clear;
+	}
+
 	return 0;
+
+err_clear:
+	mutex_lock(&wd_data->lock);
+	hw_running = watchdog_hw_running(wdd);
+	if (watchdog_active(wdd))
+		watchdog_stop(wdd);
+	watchdog_hrtimer_pretimeout_stop(wdd);
+	if (hw_running && !test_bit(_WDOG_DEV_OPEN, &wd_data->status)) {
+		module_put(wdd->ops->owner);
+		put_device(&wd_data->dev);
+	}
+	wd_data->wdd = NULL;
+	wdd->wd_data = NULL;
+	mutex_unlock(&wd_data->lock);
+
+	hrtimer_cancel(&wd_data->timer);
+	kthread_cancel_work_sync(&wd_data->work);
+	put_device(&wd_data->dev);
+	return err;
 }
 
 /**
@@ -1153,6 +1166,7 @@ static int watchdog_cdev_register(struct watchdog_device *wdd)
 static void watchdog_cdev_unregister(struct watchdog_device *wdd)
 {
 	struct watchdog_core_data *wd_data = wdd->wd_data;
+	bool hw_running;
 
 	cdev_device_del(&wd_data->cdev, &wd_data->dev);
 	if (wdd->id == 0) {
@@ -1163,6 +1177,7 @@ static void watchdog_cdev_unregister(struct watchdog_device *wdd)
 	}
 
 	mutex_lock(&wd_data->lock);
+	hw_running = watchdog_hw_running(wdd);
 	if (watchdog_active(wdd) &&
 	    test_bit(WDOG_STOP_ON_UNREGISTER, &wdd->status)) {
 		watchdog_stop(wdd);
@@ -1170,6 +1185,10 @@ static void watchdog_cdev_unregister(struct watchdog_device *wdd)
 
 	watchdog_hrtimer_pretimeout_stop(wdd);
 
+	if (hw_running && !test_bit(_WDOG_DEV_OPEN, &wd_data->status)) {
+		module_put(wdd->ops->owner);
+		put_device(&wd_data->dev);
+	}
 	wd_data->wdd = NULL;
 	wdd->wd_data = NULL;
 	mutex_unlock(&wd_data->lock);
Signature · 1 line
2.45.2
author maintainer
#1 lore raw

PATCH v2 7/8 watchdog: core: Fix unbalanced module_put() in watchdog_open()

If the hardware watchdog is running (hw_running is true), try_module_get() is skipped. However, if watchdog_start() then fails, the code jumps to out_mod and unconditionally calls module_put().

This can cause a premature driver module unload while hardware timers or interrupts are active by dropping a reference that was never acquired.

Only call module_put() if the hardware watchdog is not running to fix the problem.

Fixes ee142889e32f ("watchdog: Introduce WDOG_HW_RUNNING flag")
Assisted-by LLM

---
v2: Added Reviewed-by: tag

 drivers/watchdog/watchdog_dev.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/watchdog/watchdog_dev.c +2 -1
index ef2254188f7f..7c80d3753252 100644
@@ -936,7 +936,8 @@ static int watchdog_open(struct inode *inode, struct file *file)
 	return stream_open(inode, file);
 
 out_mod:
-	module_put(wd_data->wdd->ops->owner);
+	if (!hw_running)
+		module_put(wd_data->wdd->ops->owner);
 out_clear:
 	clear_bit(_WDOG_DEV_OPEN, &wd_data->status);
 	mutex_unlock(&wd_data->lock);
Signature · 1 line
2.45.2
author maintainer
#2 lore raw

PATCH v2 8/8 watchdog: core: Update last_keepalive in watchdog_start()

In reply to Guenter Roeck

When starting a watchdog whose hardware is already running, watchdog_start() calls __watchdog_ping() before marking the watchdog active, and fails to update wd_data->last_keepalive. As a result:

1. wd_data->last_keepalive remains 0, causing watchdog_get_timeleft() to calculate the elapsed time since the epoch and return 0 time left.
2. watchdog_update_worker() inside __watchdog_ping() evaluates watchdog_need_worker() and watchdog_next_keepalive() while WDOG_ACTIVE is still clear, using wd_data->open_deadline instead of wd_data->last_keepalive. If open_deadline has expired, the background keepalive timer is not armed; conversely, if the active watchdog does not need a worker, the boot keepalive timer is not canceled.

Set WDOG_ACTIVE and wd_data->last_keepalive to started_at before calling __watchdog_ping(), and revert WDOG_ACTIVE and update the worker if __watchdog_ping() fails.

Fixes fbbe35dfcf94 ("watchdog: use __watchdog_ping in startup")
Assisted-by LLM

---
v2: Added Reviewed-by: tag

 drivers/watchdog/watchdog_dev.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/watchdog/watchdog_dev.c +5 -1
index 7c80d3753252..aad3f1f1b4ce 100644
@@ -264,10 +264,14 @@ static int watchdog_start(struct watchdog_device *wdd)
 
 	started_at = ktime_get();
 	if (watchdog_hw_running(wdd) && wdd->ops->ping) {
+		set_bit(WDOG_ACTIVE, &wdd->status);
+		wd_data->last_keepalive = started_at;
 		err = __watchdog_ping(wdd);
 		if (err == 0) {
-			set_bit(WDOG_ACTIVE, &wdd->status);
 			watchdog_hrtimer_pretimeout_start(wdd);
+		} else {
+			clear_bit(WDOG_ACTIVE, &wdd->status);
+			watchdog_update_worker(wdd);
 		}
 	} else {
 		err = wdd->ops->start(wdd);
Signature · 1 line
2.45.2