From: Guenter Roeck <linux@roeck-us.net>
To: linux-watchdog@vger.kernel.org
Cc: Tzung-Bi Shih <tzungbi@kernel.org>,
Guenter Roeck <linux@roeck-us.net>,
Xingyu Wu <xingyu.wu@starfivetech.com>,
Ziv Xu <ziv.xu@starfivetech.com>
Subject: [PATCH v2 8/8] watchdog: starfive-wdt: Avoid unclocked MMIO access in starfive_wdt_set_timeout()
Date: Thu, 5 Nov 2026 11:05:29 -0800 [thread overview]
Message-ID: <20261105190530.2371854-9-linux@roeck-us.net> (raw)
In-Reply-To: <20261105190530.2371854-1-linux@roeck-us.net>
When the watchdog is stopped via ioctl(WDIOC_SETOPTIONS, WDIOS_DISABLECARD)
while /dev/watchdog remains open, watchdog_stop() clears WDOG_HW_RUNNING
and WDOG_ACTIVE, and starfive_wdt_pm_stop() halts the hardware counter and
drops the runtime PM reference, gating core_clk and apb_clk.
A subsequent ioctl(WDIOC_SETTIMEOUT) invokes starfive_wdt_set_timeout()
regardless of whether the watchdog is running. Because
starfive_wdt_set_timeout() unconditionally accesses MMIO registers and
calls starfive_wdt_enable(), updating the timeout while stopped triggers an
unclocked MMIO bus fault when runtime-suspended, or unexpectedly starts the
hardware counter without WDOG_ACTIVE or WDOG_HW_RUNNING set when runtime PM
is disabled.
Only program the hardware registers in starfive_wdt_set_timeout() when
watchdog_hw_running() is set. In addition, reload the initial counter value
via starfive_wdt_set_reload_count() in starfive_wdt_start() (preserving
wdt->reload on resume) so JH7100 loads the configured timeout into the
counter when started from probe or after WDIOS_DISABLECARD.
Fixes: db728ea9c7be ("drivers: watchdog: Add StarFive Watchdog driver")
Cc: Xingyu Wu <xingyu.wu@starfivetech.com>
Cc: Ziv Xu <ziv.xu@starfivetech.com>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
---
v2: New patch
drivers/watchdog/starfive-wdt.c | 32 +++++++++++++++-----------------
1 file changed, 15 insertions(+), 17 deletions(-)
diff --git a/drivers/watchdog/starfive-wdt.c b/drivers/watchdog/starfive-wdt.c
index ae2d84ccc76f..758030ad9c65 100644
--- a/drivers/watchdog/starfive-wdt.c
+++ b/drivers/watchdog/starfive-wdt.c
@@ -344,7 +344,7 @@ static int starfive_wdt_keepalive(struct watchdog_device *wdd)
return ret;
}
-static int starfive_wdt_start(struct starfive_wdt *wdt)
+static int starfive_wdt_start(struct starfive_wdt *wdt, u32 count)
{
int ret;
@@ -357,7 +357,9 @@ static int starfive_wdt_start(struct starfive_wdt *wdt)
if (ret)
goto exit;
- starfive_wdt_set_count(wdt, wdt->count);
+ starfive_wdt_set_reload_count(wdt, count);
+ if (count != wdt->count)
+ starfive_wdt_set_count(wdt, wdt->count);
starfive_wdt_enable(wdt);
exit:
@@ -381,7 +383,7 @@ static int starfive_wdt_pm_start(struct watchdog_device *wdd)
if (ret < 0)
return ret;
- ret = starfive_wdt_start(wdt);
+ ret = starfive_wdt_start(wdt, wdt->count);
if (ret)
pm_runtime_put_sync(wdd->parent);
@@ -412,11 +414,13 @@ static int starfive_wdt_set_timeout(struct watchdog_device *wdd,
wdt->count = count;
wdd->timeout = timeout;
- starfive_wdt_unlock(wdt);
- starfive_wdt_disable(wdt);
- starfive_wdt_set_reload_count(wdt, wdt->count);
- starfive_wdt_enable(wdt);
- starfive_wdt_lock(wdt);
+ if (watchdog_hw_running(wdd)) {
+ starfive_wdt_unlock(wdt);
+ starfive_wdt_disable(wdt);
+ starfive_wdt_set_reload_count(wdt, wdt->count);
+ starfive_wdt_enable(wdt);
+ starfive_wdt_lock(wdt);
+ }
return 0;
}
@@ -497,7 +501,7 @@ static int starfive_wdt_probe(struct platform_device *pdev)
watchdog_stop_ping_on_suspend(&wdt->wdd);
if (early_enable) {
- ret = starfive_wdt_start(wdt);
+ ret = starfive_wdt_start(wdt, wdt->count);
if (ret)
goto err_exit;
set_bit(WDOG_HW_RUNNING, &wdt->wdd.status);
@@ -575,14 +579,8 @@ static int starfive_wdt_resume(struct device *dev)
if (ret)
return ret;
- if (watchdog_hw_running(&wdt->wdd)) {
- starfive_wdt_unlock(wdt);
- /* Restore watchdog state. */
- starfive_wdt_set_reload_count(wdt, wdt->reload);
- starfive_wdt_lock(wdt);
-
- return starfive_wdt_start(wdt);
- }
+ if (watchdog_hw_running(&wdt->wdd))
+ return starfive_wdt_start(wdt, wdt->reload);
return 0;
}
--
2.45.2
next prev parent reply other threads:[~2026-11-05 19:05 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-05 19:05 [PATCH v2 0/8] watchdog: starfive-wdt: Fix runtime PM and unclocked register access bugs Guenter Roeck
2026-11-05 19:05 ` [PATCH v2 1/8] watchdog: starfive-wdt: Drop runtime PM reference if starting watchdog fails Guenter Roeck
2026-11-05 19:05 ` [PATCH v2 2/8] watchdog: starfive-wdt: Balance pm_runtime_enable() on probe error paths Guenter Roeck
2026-11-05 19:05 ` [PATCH v2 3/8] watchdog: starfive-wdt: Avoid unclocked register access in remove and PM paths Guenter Roeck
2026-11-06 14:21 ` Tzung-Bi Shih
2026-11-05 19:05 ` [PATCH v2 4/8] watchdog: starfive-wdt: Fix runtime PM usage count leaks in probe and remove Guenter Roeck
2026-11-05 19:05 ` [PATCH v2 5/8] watchdog: starfive-wdt: Drop runtime PM reference before registering watchdog Guenter Roeck
2026-11-05 19:05 ` [PATCH v2 6/8] watchdog: starfive-wdt: Avoid unclocked MMIO read in starfive_wdt_get_timeleft() Guenter Roeck
2026-11-06 14:22 ` Tzung-Bi Shih
2026-11-05 19:05 ` [PATCH v2 7/8] watchdog: starfive-wdt: Stop ping worker on system suspend Guenter Roeck
2026-11-06 14:22 ` Tzung-Bi Shih
2026-11-05 19:05 ` Guenter Roeck [this message]
2026-11-06 14:21 ` [PATCH v2 8/8] watchdog: starfive-wdt: Avoid unclocked MMIO access in starfive_wdt_set_timeout() Tzung-Bi Shih
2026-11-06 15:20 ` Guenter Roeck
2026-11-06 15:51 ` Tzung-Bi Shih
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=20261105190530.2371854-9-linux@roeck-us.net \
--to=linux@roeck-us.net \
--cc=linux-watchdog@vger.kernel.org \
--cc=tzungbi@kernel.org \
--cc=xingyu.wu@starfivetech.com \
--cc=ziv.xu@starfivetech.com \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.