mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Christophe Leroy <christophe.leroy@csgroup.eu>
To: Alexey Klimov <aklimov@redhat.com>,
	"linux-watchdog@vger.kernel.org" <linux-watchdog@vger.kernel.org>
Cc: "wim@linux-watchdog.org" <wim@linux-watchdog.org>,
	"linux@roeck-us.net" <linux@roeck-us.net>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"klimov.linux@gmail.com" <klimov.linux@gmail.com>
Subject: Re: [PATCH REVIEW 2/2] watchdog: add wd_data->lock mutex locking to watchdog_open()
Date: Thu, 18 Aug 2022 09:09:03 +0000	[thread overview]
Message-ID: <368f894f-e433-6a9b-31c6-35dfa2842576@csgroup.eu> (raw)
In-Reply-To: <20220818020624.2386475-2-aklimov@redhat.com>



Le 18/08/2022 à 04:06, Alexey Klimov a écrit :
> [Vous ne recevez pas souvent de courriers de aklimov@redhat.com. Découvrez pourquoi ceci est important à https://aka.ms/LearnAboutSenderIdentification ]
> 
> watchdog_open() does not have wd_data->lock locks at all unlike
> the watchdog_release() for instance. Also watchdog_open() calls
> watchdog_start() that should be called with wd_data->lock acquired.
> The mutex lock should be acquired in the beginning of the function
> after getting struct watchdog_core_data wd_data pointer to deal with
> different status fields and be able to call watchdog_start(); and
> released on exit and on different error paths.

Why do you need the mutex for the open ?

open is protected via atomic operation test_and_set_bit(), so what ?

If the mutex is hold while calling open, it means that the device is 
already open so test_and_set_bit will fail.

> 
> Signed-off-by: Alexey Klimov <aklimov@redhat.com>
> ---
>   drivers/watchdog/watchdog_dev.c | 12 +++++++-----
>   1 file changed, 7 insertions(+), 5 deletions(-)
> 
> diff --git a/drivers/watchdog/watchdog_dev.c b/drivers/watchdog/watchdog_dev.c
> index 804236a094f6..d07a864036d9 100644
> --- a/drivers/watchdog/watchdog_dev.c
> +++ b/drivers/watchdog/watchdog_dev.c
> @@ -836,7 +836,7 @@ static int watchdog_open(struct inode *inode, struct file *file)
>          struct watchdog_core_data *wd_data;
>          struct watchdog_device *wdd;
>          bool hw_running;
> -       int err;
> +       int err = -EBUSY;
> 
>          /* Get the corresponding watchdog device */
>          if (imajor(inode) == MISC_MAJOR)
> @@ -845,9 +845,10 @@ static int watchdog_open(struct inode *inode, struct file *file)
>                  wd_data = container_of(inode->i_cdev, struct watchdog_core_data,
>                                         cdev);
> 
> +       mutex_lock(&wd_data->lock);
>          /* the watchdog is single open! */
>          if (test_and_set_bit(_WDOG_DEV_OPEN, &wd_data->status))
> -               return -EBUSY;
> +               goto out_unlock;
> 
>          wdd = wd_data->wdd;
> 
> @@ -856,10 +857,8 @@ static int watchdog_open(struct inode *inode, struct file *file)
>           * to be unloaded.
>           */
>          hw_running = watchdog_hw_running(wdd);
> -       if (!hw_running && !try_module_get(wdd->ops->owner)) {
> -               err = -EBUSY;
> +       if (!hw_running && !try_module_get(wdd->ops->owner))
>                  goto out_clear;
> -       }
> 
>          err = watchdog_start(wdd);
>          if (err < 0)
> @@ -878,6 +877,7 @@ static int watchdog_open(struct inode *inode, struct file *file)
>           * applied.
>           */
>          wd_data->open_deadline = KTIME_MAX;
> +       mutex_unlock(&wd_data->lock);
> 
>          /* dev/watchdog is a virtual (and thus non-seekable) filesystem */
>          return stream_open(inode, file);
> @@ -886,6 +886,8 @@ static int watchdog_open(struct inode *inode, struct file *file)
>          module_put(wd_data->wdd->ops->owner);
>   out_clear:
>          clear_bit(_WDOG_DEV_OPEN, &wd_data->status);
> +out_unlock:
> +       mutex_unlock(&wd_data->lock);
>          return err;
>   }
> 
> --
> 2.37.2
> 

  reply	other threads:[~2022-08-18  9:09 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-18  2:06 [PATCH REVIEW 1/2] watchdog: extend the mutex-protected section in watchdog_cdev_unregister() Alexey Klimov
2022-08-18  2:06 ` [PATCH REVIEW 2/2] watchdog: add wd_data->lock mutex locking to watchdog_open() Alexey Klimov
2022-08-18  9:09   ` Christophe Leroy [this message]
2022-08-18 16:57   ` Guenter Roeck
2022-08-18 16:55 ` [PATCH REVIEW 1/2] watchdog: extend the mutex-protected section in watchdog_cdev_unregister() 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=368f894f-e433-6a9b-31c6-35dfa2842576@csgroup.eu \
    --to=christophe.leroy@csgroup.eu \
    --cc=aklimov@redhat.com \
    --cc=klimov.linux@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-watchdog@vger.kernel.org \
    --cc=linux@roeck-us.net \
    --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®