Skip to content

watchdog: starfive-wdt: Fix runtime PM and unclocked register access bugs

Guenter Roeck linux-watchdog 2 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v2 Latest8 patches 6 repliesIn review
  2. v15 patches 10 repliesSuperseded

Changes in v2 from the cover letter

- Patch 1, 2, 4, and 5: Added Reviewed-by: tags
- Patch 3: Remove the redundant starfive_wdt_shutdown() callback instead of guarding it with !pm_runtime_suspended(), avoiding unbalanced pm_runtime_put_sync() calls when runtime suspend is forbidden or after watchdog_reboot_notifier() already stopped the watchdog, avoiding unclocked MMIO during hibernation shutdown, and respecting the watchdog.stop_on_reboot module parameter.
- Patch 6 (new): Return 0 in starfive_wdt_get_timeleft() when !watchdog_hw_running() to avoid unclocked MMIO reads.
- Patch 7 (new): Call watchdog_stop_ping_on_suspend() in starfive_wdt_probe() so the ping worker does not call starfive_wdt_keepalive() while clocks are gated during suspend.
- Patch 8 (new): Only program hardware registers in starfive_wdt_set_timeout() when watchdog_hw_running() is set, and reload the preset counter in starfive_wdt_start() when starting from probe or after WDIOS_DISABLECARD.

Patches in v2 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/8watchdog: starfive-wdt: Drop runtime PM reference if starting watchdog failsdrivers/watchdog/starfive-wdt.c+7 -20 replies
2/8watchdog: starfive-wdt: Balance pm_runtime_enable() on probe error pathsdrivers/watchdog/starfive-wdt.c+3 -20 replies
3/8watchdog: starfive-wdt: Avoid unclocked register access in remove and PM pathsdrivers/watchdog/starfive-wdt.c+19 -191 reply
4/8watchdog: starfive-wdt: Fix runtime PM usage count leaks in probe and removedrivers/watchdog/starfive-wdt.c+11 -20 replies
5/8watchdog: starfive-wdt: Drop runtime PM reference before registering watchdogdrivers/watchdog/starfive-wdt.c+10 -130 replies
6/8watchdog: starfive-wdt: Avoid unclocked MMIO read in starfive_wdt_get_timeleft()drivers/watchdog/starfive-wdt.c+3 -01 reply
7/8watchdog: starfive-wdt: Stop ping worker on system suspenddrivers/watchdog/starfive-wdt.c+1 -01 reply
8/8watchdog: starfive-wdt: Avoid unclocked MMIO access in starfive_wdt_set_timeout()drivers/watchdog/starfive-wdt.c+15 -173 replies

Reviews trailers per patch

All 8 patches have a Reviewed-by or Acked-by.

PatchTzung-Bi Shih
1/8R-b
2/8R-b
3/8R-b
4/8R-b
5/8R-b
6/8R-b
7/8R-b
8/8R-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v2 thread

Cover letter [PATCH v2 0/8] watchdog: starfive-wdt: Fix runtime PM and unclocked register access bugs

The StarFive watchdog driver (drivers/watchdog/starfive-wdt.c) gates its APB and core clocks via runtime PM callbacks, but has several runtime PM reference-counting, ordering, and unclocked MMIO access bugs across probe, remove, start/stop, shutdown, ioctl/sysfs, and system sleep paths:

1. In starfive_wdt_pm_start(), if starfive_wdt_start() fails after pm_runtime_resume_and_get() succeeds, the acquired runtime PM reference is leaked because the watchdog core does not call the stop callback when start fails.
2. In starfive_wdt_probe(), early returns on pm_runtime_resume_and_get() or starfive_wdt_enable_clock() failure bypass pm_runtime_disable(), leaving the runtime PM disable_depth counter unbalanced.
3. When runtime PM has suspended an idle watchdog and gated its clocks, starfive_wdt_remove(), starfive_wdt_shutdown(), starfive_wdt_suspend(), and starfive_wdt_resume() unconditionally read and write hardware registers, causing unclocked MMIO bus faults. In addition, starfive_wdt_shutdown() duplicates watchdog_stop_on_reboot() (causing unbalanced runtime PM puts when not runtime-suspended and ignoring watchdog.stop_on_reboot=0), and starfive_wdt_pm_stop() propagates non-zero idle return codes from pm_runtime_put_sync() as errors to watchdog_stop().
4. Probe error paths after pm_runtime_resume_and_get() disable clocks manually without releasing the runtime PM reference (and without stopping an early_enable watchdog if registration fails), and starfive_wdt_remove() never drops the active runtime PM reference retained when watchdog_hw_running() is still set (e.g. early_enable).
5. In starfive_wdt_probe(), watchdog_register_device() is called before pm_runtime_put_sync() drops the probe-time reference, and non-zero idle return values such as -EAGAIN (triggered if userspace opens /dev/watchdog immediately upon registration) are treated as fatal probe errors.
6. In starfive_wdt_get_timeleft(), reading the sysfs timeleft attribute or issuing ioctl(WDIOC_GETTIMELEFT) while the watchdog is inactive reads hardware registers while clocks are gated off.
7. In starfive_wdt_probe(), omitting watchdog_stop_ping_on_suspend() leaves the non-freezable watchdog ping worker active across system suspend when early_enable=1 is used, risking unclocked MMIO in starfive_wdt_keepalive() after starfive_wdt_suspend() gates clocks.
8. In starfive_wdt_set_timeout(), issuing ioctl(WDIOC_SETTIMEOUT) after stopping the watchdog via WDIOS_DISABLECARD accesses unclocked MMIO registers (when runtime-suspended) or unexpectedly re-enables the hardware counter while inactive (when runtime PM is disabled).

This 8-patch series fixes each of these issues in sequence.

Changes in v2:
- Patch 1, 2, 4, and 5: Added Reviewed-by: tags
- Patch 3: Remove the redundant starfive_wdt_shutdown() callback instead of guarding it with !pm_runtime_suspended(), avoiding unbalanced pm_runtime_put_sync() calls when runtime suspend is forbidden or after watchdog_reboot_notifier() already stopped the watchdog, avoiding unclocked MMIO during hibernation shutdown, and respecting the watchdog.stop_on_reboot module parameter.
- Patch 6 (new): Return 0 in starfive_wdt_get_timeleft() when !watchdog_hw_running() to avoid unclocked MMIO reads.
- Patch 7 (new): Call watchdog_stop_ping_on_suspend() in starfive_wdt_probe() so the ping worker does not call starfive_wdt_keepalive() while clocks are gated during suspend.
- Patch 8 (new): Only program hardware registers in starfive_wdt_set_timeout() when watchdog_hw_running() is set, and reload the preset counter in starfive_wdt_start() when starting from probe or after WDIOS_DISABLECARD.

Note on tooling and testing:
This series was generated with an AI coding assistant (Jetski / Gemini) following analysis of reported runtime PM and lifecycle bugs in starfive-wdt.c. All patches were build-tested with make allmodconfig and make W=1 with zero warnings, verified with scripts/checkpatch.pl --strict, and validated against a state-machine simulation harness covering runtime PM, clock gating, ioctl/sysfs, suspend/shutdown, and concurrent open paths; they were not tested on physical StarFive JH7100/JH7110 hardware.

Cc Xingyu Wu <[email protected]>
Cc Ziv Xu <[email protected]>

Guenter Roeck (8):
watchdog: starfive-wdt: Drop runtime PM reference if starting watchdog
fails
watchdog: starfive-wdt: Balance pm_runtime_enable() on probe error
paths
watchdog: starfive-wdt: Avoid unclocked register access in remove and
PM paths
watchdog: starfive-wdt: Fix runtime PM usage count leaks in probe and
remove
watchdog: starfive-wdt: Drop runtime PM reference before registering
watchdog
watchdog: starfive-wdt: Avoid unclocked MMIO read in
starfive_wdt_get_timeleft()
watchdog: starfive-wdt: Stop ping worker on system suspend
watchdog: starfive-wdt: Avoid unclocked MMIO access in
starfive_wdt_set_timeout()

 drivers/watchdog/starfive-wdt.c | 102 ++++++++++++++++++--------------
 1 file changed, 58 insertions(+), 44 deletions(-)
Signature · 1 line
2.56.0