Skip to contentSkip to conversation

watchdog: i6300esb: do not stop an already running watchdog

PATCH Applied Jan Kiszka 3 participants 3 messages Sep 9 Watchdog device drivers

lore
Share & tools
lore

Jan Kiszka

author
#0 lore raw

PATCH 1/1 watchdog: i6300esb: do not stop an already running watchdog

From: Jan Kiszka <[email protected]>

esb_initdevice() unconditionally disabled the WDT, stopping a watchdog that firmware had left running instead of taking over its care. Detect the enable bit in the lock register, set WDOG_HW_RUNNING like the other watchdog drivers do, and simply reset the timeout to the configured value.

Assisted-by opencode:Qwen3.8-27B

---

Background to patch this: The i6300esb is the only watchdog model that QEMU provides for non-x86 virt machines. It can serve there to test complex system integrations. Patches to add a U-Boot driver were sent as well:
https://lore.kernel.org/u-boot/[email protected]/

Background for this unusual as assistance: While crafting this with own hands or using some cloud-hosted frontier model would have been much quicker, I used this practical case to try out what could be done in the background on my ordinary notebook already. Reviewing, testing (it worked immediately) and a bit shortening was done by me, analysis and patch fell out of the opencode after some half-day of thinking.

 drivers/watchdog/i6300esb.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)
drivers/watchdog/i6300esb.c +15 -4
index a30835f547b3..13daafe05c43 100644
@@ -264,13 +264,21 @@ static void esb_initdevice(struct esb_dev *edev)
 	 */
 	pci_write_config_word(edev->pdev, ESB_CONFIG_REG, 0x0003);
 
-	/* Check that the WDT isn't already locked */
+	/* Check the current state of the WDT */
 	pci_read_config_byte(edev->pdev, ESB_LOCK_REG, &val1);
 	if (val1 & ESB_WDT_LOCK)
 		dev_warn(&edev->pdev->dev, "nowayout already set\n");
 
-	/* Set the timer to watchdog mode and disable it for now */
-	pci_write_config_byte(edev->pdev, ESB_LOCK_REG, 0x00);
+	if (val1 & ESB_WDT_ENABLE) {
+		/*
+		 * The watchdog is already running, e.g. enabled by
+		 * firmware. Do not stop it, just mark it as running.
+		 */
+		set_bit(WDOG_HW_RUNNING, &edev->wdd.status);
+	} else {
+		/* Set the timer to watchdog mode and disable it for now */
+		pci_write_config_byte(edev->pdev, ESB_LOCK_REG, 0x00);
+	}
 
 	/* Check if the watchdog was previously triggered */
 	esb_unlock_registers(edev);
@@ -301,7 +309,10 @@ static int esb_probe(struct pci_dev *pdev,
 	if (!esb_getdevice(edev))
 		return -ENODEV;
 
-	/* Initialize the watchdog and make sure it does not run */
+	/*
+	 * Initialize the watchdog, keeping it running if it was already
+	 * started, e.g. by firmware.
+	 */
 	edev->wdd.info = &esb_info;
 	edev->wdd.ops = &esb_ops;
 	edev->wdd.min_timeout = ESB_HEARTBEAT_MIN;
Signature · 1 line
2.47.3
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Pre-existing issues:
- [High] esb_timer_stop() returns a positive integer on failure instead of a negative error code, silently breaking watchdog core error handling.
- [High] Driver fails to enforce WDOG_NO_WAY_OUT when the hardware watchdog is locked by firmware.
--

commit 558955a28d6872fa0e35bdd2eed150583051de56
Author: Jan Kiszka <[email protected]>

watchdog: i6300esb: do not stop an already running watchdog

This commit updates the i6300esb driver to detect if the watchdog was already enabled by firmware. Instead of unconditionally disabling it, the driver sets WDOG_HW_RUNNING and resets the timeout, allowing the watchdog to continue running correctly.

drivers/watchdog/i6300esb.c static void esb_initdevice(struct esb_dev *edev) · line 270

Quoted 13 lines
drivers/watchdog/i6300esb.c +1 -1
index a30835f547b32..13daafe05c43c 100644
@@ -264,13 +264,21 @@ static void esb_initdevice(struct esb_dev *edev)
 	 */
 	pci_write_config_word(edev->pdev, ESB_CONFIG_REG, 0x0003);
 
-	/* Check that the WDT isn't already locked */
+	/* Check the current state of the WDT */
 	pci_read_config_byte(edev->pdev, ESB_LOCK_REG, &val1);
 	if (val1 & ESB_WDT_LOCK)
 		dev_warn(&edev->pdev->dev, "nowayout already set\n");

[Severity: High]
This is a pre-existing issue, but does this code fail to enforce WDOG_NO_WAY_OUT when the hardware watchdog is locked by firmware?

If ESB_WDT_LOCK is set, the driver prints a warning but doesn't set WDOG_NO_WAY_OUT. Later, when the character device is closed, watchdog_stop() tries to stop the device, incorrectly clears WDOG_HW_RUNNING, and fails.

Because WDOG_NO_WAY_OUT is absent, the core doesn't abort with -EBUSY. Since WDOG_HW_RUNNING is cleared, watchdog_release() assumes the hardware is stopped and calls module_put(), allowing the driver to be removed and I/O regions unmapped while the timer keeps running, leading to an unexpected system reset.

Additionally, this is a pre-existing issue, but does esb_timer_stop() return a positive integer on failure instead of a negative error code, silently breaking watchdog core error handling?

Looking at esb_timer_stop():

drivers/watchdog/i6300esb.c:esb_timer_stop() {
	...
	pci_read_config_byte(edev->pdev, ESB_LOCK_REG, &val);

	/* Returns 0 if the timer was disabled, non-zero otherwise */
	return val & ESB_WDT_ENABLE;
}

When the watchdog cannot be stopped (e.g. locked by hardware), this returns val & ESB_WDT_ENABLE, which evaluates to 2.

The watchdog core's watchdog_release() checks for err < 0 to detect stop failures. Since 2 is positive, the core assumes the device stopped successfully, fails to issue the critical warning, misses the fallback keepalive ping, and cancels the background worker, leading to an unexpected system reboot.

Quoted 14 lines
drivers/watchdog/i6300esb.c +10 -2
-	/* Set the timer to watchdog mode and disable it for now */
-	pci_write_config_byte(edev->pdev, ESB_LOCK_REG, 0x00);
+	if (val1 & ESB_WDT_ENABLE) {
+		/*
+		 * The watchdog is already running, e.g. enabled by
+		 * firmware. Do not stop it, just mark it as running.
+		 */
+		set_bit(WDOG_HW_RUNNING, &edev->wdd.status);
+	} else {
+		/* Set the timer to watchdog mode and disable it for now */
+		pci_write_config_byte(edev->pdev, ESB_LOCK_REG, 0x00);
+	}
 
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer Applied
#2 lore raw
In reply to Jan Kiszka
Quoted 10 lines · Jan Kiszka

From: Jan Kiszka <[email protected]>

esb_initdevice() unconditionally disabled the WDT, stopping a watchdog that firmware had left running instead of taking over its care. Detect the enable bit in the lock register, set WDOG_HW_RUNNING like the other watchdog drivers do, and simply reset the timeout to the configured value.

Assisted-by opencode:Qwen3.8-27B

Applied.

Thanks,
Guenter