[PATCH v2] watchdog: core: Don't report time left for a stopped watchdog

From: Nhat Tran

Date: Sat Nov 21 2026 - 00:31:02 EST


watchdog_get_timeleft() calls the driver's ->get_timeleft() regardless
of whether the watchdog is running. It is reachable through the sysfs
timeleft attribute, which is world readable with CONFIG_WATCHDOG_SYSFS=y,
and through WDIOC_GETTIMELEFT, also after the watchdog was stopped with
WDIOS_DISABLECARD while the device is still open.

Drivers may not be able to access the hardware while the watchdog is
stopped. omap_wdt only holds a runtime PM reference while the watchdog
is running, so on a BeagleBone Black (AM335x) reading the counter of a
stopped watchdog triggers an external abort. Any unprivileged user can
hit it through sysfs:

$ cat /sys/class/watchdog/watchdog0/timeleft
Unhandled fault: external abort on non-linefetch (0x1028) at 0xf9e35028
Internal error: : 1028 [#1] SMP ARM
PC is at omap_wdt_get_timeleft+0x4/0x14
LR is at watchdog_get_timeleft+0x24/0x130
Call trace:
omap_wdt_get_timeleft from watchdog_get_timeleft+0x24/0x130
watchdog_get_timeleft from timeleft_show+0x3c/0x80
timeleft_show from dev_attr_show+0x18/0x4c
dev_attr_show from sysfs_kf_seq_show+0xa0/0x138
...

and the same happens with WDIOS_DISABLECARD followed by
WDIOC_GETTIMELEFT on the open device, once the module has idled:

Call trace:
omap_wdt_get_timeleft from watchdog_get_timeleft+0x24/0x130
watchdog_get_timeleft from watchdog_ioctl+0x494/0x668
watchdog_ioctl from sys_ioctl+0x128/0xbcc
sys_ioctl from ret_fast_syscall+0x0/0x54

If the ioctl comes before the module has idled, the read succeeds but
reports 59 seconds left for a watchdog that is stopped.

A stopped watchdog will not reboot the system, so report 0 time left
when neither WDOG_ACTIVE nor WDOG_HW_RUNNING is set, without calling
into the driver. This matches the software fallback for drivers without
->get_timeleft(), which already decays to 0 once the watchdog is no
longer pinged.

Tested on a BeagleBone Black: with the watchdog stopped, both the sysfs
attribute and WDIOC_GETTIMELEFT after WDIOS_DISABLECARD return 0; with
the watchdog started through /dev/watchdog0, timeleft counts down as
before (59, then 56 three seconds later).

Fixes: 452fafed839e ("watchdog: omap_wdt: implement get_timeleft")
Cc: stable@xxxxxxxxxxxxxxx
Suggested-by: Guenter Roeck <linux@xxxxxxxxxxxx>
Signed-off-by: Nhat Tran <nhat.tranminhk2@xxxxxxxxx>
---
v1->v2:
- Moved the check from omap_wdt into watchdog_get_timeleft(), as
suggested by Guenter, so it covers all drivers
- Return 0 for a stopped watchdog; this also covers WDIOC_GETTIMELEFT
after WDIOS_DISABLECARD on an open device

v1: https://lore.kernel.org/all/20261120124009.24264-1-nhat.tranminhk2@xxxxxxxxx/
---
drivers/watchdog/watchdog_dev.c | 3 +++
1 file changed, 3 insertions(+)

diff --git a/drivers/watchdog/watchdog_dev.c b/drivers/watchdog/watchdog_dev.c
index e8c1fcb53c56..b67a68316bda 100644
--- a/drivers/watchdog/watchdog_dev.c
+++ b/drivers/watchdog/watchdog_dev.c
@@ -435,6 +435,9 @@ static void watchdog_get_timeleft(struct watchdog_device *wdd,
{
*timeleft = 0;

+ if (!watchdog_active(wdd) && !watchdog_hw_running(wdd))
+ return;
+
if (wdd->ops->get_timeleft) {
*timeleft = wdd->ops->get_timeleft(wdd);
} else {
--
2.25.1