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

Nhat Tran posted 1 patch 3 days, 7 hours ago
drivers/watchdog/watchdog_dev.c | 3 +++
1 file changed, 3 insertions(+)
[PATCH v2] watchdog: core: Don't report time left for a stopped watchdog
Posted by Nhat Tran 3 days, 7 hours ago
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@vger.kernel.org
Suggested-by: Guenter Roeck <linux@roeck-us.net>
Signed-off-by: Nhat Tran <nhat.tranminhk2@gmail.com>
---
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@gmail.com/
---
 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
Re: [PATCH v2] watchdog: core: Don't report time left for a stopped watchdog
Posted by Guenter Roeck 2 days, 21 hours ago
On Sat, Nov 21, 2026 at 12:29:50PM +0700, Nhat Tran wrote:
> 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@vger.kernel.org
> Suggested-by: Guenter Roeck <linux@roeck-us.net>
> Signed-off-by: Nhat Tran <nhat.tranminhk2@gmail.com>

Applied, and thanks for testing with WDIOS_DISABLECARD/WDIOC_GETTIMELEFT.

Thanks,
Guenter