Linux-Watchdog Archive mirror
 help / color / mirror / Atom feed
From: Guenter Roeck <linux@roeck-us.net>
To: Curtis Klein <curtis.klein@hpe.com>,
	wim@linux-watchdog.org, haokexin@gmail.com,
	linux-watchdog@vger.kernel.org
Subject: Re: [PATCH] watchdog: set cdev owner before adding
Date: Tue, 5 Dec 2023 11:13:28 -0800	[thread overview]
Message-ID: <a1d11e74-9c8b-463a-a237-dae651f4cda7@roeck-us.net> (raw)
In-Reply-To: <20231205190522.55153-1-curtis.klein@hpe.com>

On 12/5/23 11:05, Curtis Klein wrote:
> When the new watchdog character device is registered, it becomes
> available for opening. This creates a race where userspace may open the
> device before the character device's owner is set. This results in an
> imbalance in module_get calls as the cdev_get in cdev_open will not
> increment the reference count on the watchdog driver module.
> 
> This causes problems when the watchdog character device is released as
> the module loader's reference will also be released. This makes it
> impossible to open the watchdog device later on as it now appears that
> the module is being unloaded. The open will fail with -ENXIO from
> chrdev_open.
> 
> The legacy watchdog device will fail with -EBUSY from the try_module_get
> in watchdog_open because it's module owner is the watchdog core module
> so it can still be opened but it will fail to get a refcount on the
> underlying watchdog device driver.
> 
> Fixes: 72139dfa2464 ("watchdog: Fix the race between the release of watchdog_core_data and cdev")
> Signed-off-by: Curtis Klein <curtis.klein@hpe.com>

Reviewed-by: Guenter Roeck <linux@roeck-us.net>

> ---
>   drivers/watchdog/watchdog_dev.c | 3 +--
>   1 file changed, 1 insertion(+), 2 deletions(-)
> 
> diff --git a/drivers/watchdog/watchdog_dev.c b/drivers/watchdog/watchdog_dev.c
> index 15df74e11a59..e2bd266b1b5b 100644
> --- a/drivers/watchdog/watchdog_dev.c
> +++ b/drivers/watchdog/watchdog_dev.c
> @@ -1073,6 +1073,7 @@ static int watchdog_cdev_register(struct watchdog_device *wdd)
>   
>   	/* Fill in the data structures */
>   	cdev_init(&wd_data->cdev, &watchdog_fops);
> +	wd_data->cdev.owner = wdd->ops->owner;
>   
>   	/* Add the device */
>   	err = cdev_device_add(&wd_data->cdev, &wd_data->dev);
> @@ -1087,8 +1088,6 @@ static int watchdog_cdev_register(struct watchdog_device *wdd)
>   		return err;
>   	}
>   
> -	wd_data->cdev.owner = wdd->ops->owner;
> -
>   	/* Record time of most recent heartbeat as 'just before now'. */
>   	wd_data->last_hw_keepalive = ktime_sub(ktime_get(), 1);
>   	watchdog_set_open_deadline(wd_data);


      reply	other threads:[~2023-12-05 19:13 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-05 19:05 [PATCH] watchdog: set cdev owner before adding Curtis Klein
2023-12-05 19:13 ` Guenter Roeck [this message]

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=a1d11e74-9c8b-463a-a237-dae651f4cda7@roeck-us.net \
    --to=linux@roeck-us.net \
    --cc=curtis.klein@hpe.com \
    --cc=haokexin@gmail.com \
    --cc=linux-watchdog@vger.kernel.org \
    --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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).