* [PATCH] watchdog: core: Fix module refcount imbalance when open fails
@ 2026-11-23 20:46 Artur Kowalski
2026-11-23 21:15 ` Guenter Roeck
0 siblings, 1 reply; 2+ messages in thread
From: Artur Kowalski @ 2026-11-23 20:46 UTC (permalink / raw)
To: Wim Van Sebroeck, Guenter Roeck
Cc: linux-watchdog, linux-kernel, Wim Van Sebroeck, Artur Kowalski
watchdog_open() doesn't take a module reference for a watchdog that is
running in hardware, as watchdog_cdev_register() already holds one for
it. If watchdog_start() then fails, which for such a watchdog means the
driver's ping failed, the error path drops a module reference anyway.
This drops the reference held for the running watchdog, so the module
can be unloaded while the watchdog is running and the core still uses
it. Stopping the watchdog through the core later drops the reference a
second time, underflowing the module refcount.
Only drop the module reference on the error path if it was taken.
This was found with the help of Sashiko and Claude. It can be triggered
by any modular driver whose ping can fail on a watchdog that was running
when it was registered. No in-tree driver was checked for that. The fix
was build tested only, and no reproducer was run.
Fixes: ee142889e32f ("watchdog: Introduce WDOG_HW_RUNNING flag")
Signed-off-by: Artur Kowalski <arturkow2000@gmail.com>
---
This was found while developing a Tegra20 watchdog driver. A watchdog
reset hangs Tegra20's boot ROM once the VDE power partition is off, so
the driver stops the watchdog behind the core's back when that happens.
Failing pings afterwards would trigger this bug when opening a watchdog
left running by the bootloader, so the driver keeps them succeeding
until user space has started the watchdog.
---
drivers/watchdog/watchdog_dev.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/drivers/watchdog/watchdog_dev.c b/drivers/watchdog/watchdog_dev.c
index e8c1fcb53c56..9ad715d4d46d 100644
--- a/drivers/watchdog/watchdog_dev.c
+++ b/drivers/watchdog/watchdog_dev.c
@@ -913,7 +913,8 @@ static int watchdog_open(struct inode *inode, struct file *file)
return stream_open(inode, file);
out_mod:
- module_put(wd_data->wdd->ops->owner);
+ if (!hw_running)
+ module_put(wd_data->wdd->ops->owner);
out_clear:
clear_bit(_WDOG_DEV_OPEN, &wd_data->status);
return err;
---
base-commit: 05441d697f7f9a927264f4058cbbc6a03dfcac9d
change-id: 20261123-core-wdt-fix-898327dc0ca2
Best regards,
--
Artur Kowalski <arturkow2000@gmail.com>
^ permalink raw reply [flat|nested] 2+ messages in thread* Re: [PATCH] watchdog: core: Fix module refcount imbalance when open fails
2026-11-23 20:46 [PATCH] watchdog: core: Fix module refcount imbalance when open fails Artur Kowalski
@ 2026-11-23 21:15 ` Guenter Roeck
0 siblings, 0 replies; 2+ messages in thread
From: Guenter Roeck @ 2026-11-23 21:15 UTC (permalink / raw)
To: Artur Kowalski, Wim Van Sebroeck
Cc: linux-watchdog, linux-kernel, Wim Van Sebroeck
On 11/23/26 12:46, Artur Kowalski wrote:
> watchdog_open() doesn't take a module reference for a watchdog that is
> running in hardware, as watchdog_cdev_register() already holds one for
> it. If watchdog_start() then fails, which for such a watchdog means the
> driver's ping failed, the error path drops a module reference anyway.
>
> This drops the reference held for the running watchdog, so the module
> can be unloaded while the watchdog is running and the core still uses
> it. Stopping the watchdog through the core later drops the reference a
> second time, underflowing the module refcount.
>
> Only drop the module reference on the error path if it was taken.
>
> This was found with the help of Sashiko and Claude. It can be triggered
> by any modular driver whose ping can fail on a watchdog that was running
> when it was registered. No in-tree driver was checked for that. The fix
> was build tested only, and no reproducer was run.
>
> Fixes: ee142889e32f ("watchdog: Introduce WDOG_HW_RUNNING flag")
> Signed-off-by: Artur Kowalski <arturkow2000@gmail.com>
> ---
> This was found while developing a Tegra20 watchdog driver. A watchdog
> reset hangs Tegra20's boot ROM once the VDE power partition is off, so
> the driver stops the watchdog behind the core's back when that happens.
> Failing pings afterwards would trigger this bug when opening a watchdog
> left running by the bootloader, so the driver keeps them succeeding
> until user space has started the watchdog.
> ---
> drivers/watchdog/watchdog_dev.c | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/watchdog/watchdog_dev.c b/drivers/watchdog/watchdog_dev.c
> index e8c1fcb53c56..9ad715d4d46d 100644
> --- a/drivers/watchdog/watchdog_dev.c
> +++ b/drivers/watchdog/watchdog_dev.c
> @@ -913,7 +913,8 @@ static int watchdog_open(struct inode *inode, struct file *file)
> return stream_open(inode, file);
>
> out_mod:
> - module_put(wd_data->wdd->ops->owner);
> + if (!hw_running)
> + module_put(wd_data->wdd->ops->owner);
That seems to be identical to upstream commit e1d91be1ec2e6 ("watchdog:
core: Fix unbalanced module_put() in watchdog_open()").
Id suggest to update your tree to v7.4-rc4 or later to avoid hitting
a number of other recently fixed issues in the watchdog subsystem core.
Thanks,
Guenter
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-23 21:15 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-23 20:46 [PATCH] watchdog: core: Fix module refcount imbalance when open fails Artur Kowalski
2026-11-23 21:15 ` Guenter Roeck
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®