[PATCH v2 5/6] watchdog: starfive: avoid PM refcount underflow in shutdown

From: William Theesfeld

Date: Fri Jun 05 2026 - 09:14:48 EST


starfive_wdt_shutdown() unconditionally calls starfive_wdt_pm_stop(),
which drops a runtime PM reference via pm_runtime_put_sync(). If the
device was never opened (so there was no matching pm_start()), and
early_enable is also off (so probe already balanced its own get/put),
the counter is at zero and put_sync() drives it negative.

Only call pm_stop() when WDOG_ACTIVE is set (meaning a paired
pm_start() happened on open). For the early_enable-and-never-opened
case, the framework state is WDOG_HW_RUNNING without WDOG_ACTIVE; in
that case stop just the hardware so the watchdog does not keep
ticking through shutdown, while leaving the PM refcount for the
remove path to release.

Signed-off-by: William Theesfeld <william@xxxxxxxxxxxxx>
---
drivers/watchdog/starfive-wdt.c | 14 +++++++++++++-
1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/drivers/watchdog/starfive-wdt.c b/drivers/watchdog/starfive-wdt.c
index 4926984bf..848935dc6 100644
--- a/drivers/watchdog/starfive-wdt.c
+++ b/drivers/watchdog/starfive-wdt.c
@@ -549,7 +549,19 @@ static void starfive_wdt_shutdown(struct platform_device *pdev)
{
struct starfive_wdt *wdt = platform_get_drvdata(pdev);

- starfive_wdt_pm_stop(&wdt->wdd);
+ /*
+ * Only drop a runtime PM reference when we actually hold one. The
+ * watchdog framework increments it via pm_start() on open; when
+ * early_enable started the hardware without an open, the reference
+ * was taken at probe time and the framework state reads as
+ * WDOG_HW_RUNNING without WDOG_ACTIVE. In the inactive-and-not-
+ * running case there is nothing for us to do and dropping the
+ * counter unconditionally would underflow it.
+ */
+ if (watchdog_active(&wdt->wdd))
+ starfive_wdt_pm_stop(&wdt->wdd);
+ else if (test_bit(WDOG_HW_RUNNING, &wdt->wdd.status))
+ starfive_wdt_stop(wdt);
}

static int starfive_wdt_suspend(struct device *dev)
--
2.54.0