mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Artur Kowalski <arturkow2000@gmail.com>
To: Wim Van Sebroeck <wim@linux-watchdog.org>,
	 Guenter Roeck <linux@roeck-us.net>
Cc: linux-watchdog@vger.kernel.org, linux-kernel@vger.kernel.org,
	 Wim Van Sebroeck <wim@iguana.be>,
	Artur Kowalski <arturkow2000@gmail.com>
Subject: [PATCH] watchdog: core: Fix module refcount imbalance when open fails
Date: Mon, 23 Nov 2026 21:46:49 +0100	[thread overview]
Message-ID: <20261123-core-wdt-fix-v1-1-a315269dce4b@gmail.com> (raw)

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>


             reply	other threads:[~2026-11-23 20:47 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23 20:46 Artur Kowalski [this message]
2026-11-23 21:15 ` Guenter Roeck

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=20261123-core-wdt-fix-v1-1-a315269dce4b@gmail.com \
    --to=arturkow2000@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-watchdog@vger.kernel.org \
    --cc=linux@roeck-us.net \
    --cc=wim@iguana.be \
    --cc=wim@linux-watchdog.org \
    /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 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®