From: Guenter Roeck <linux@roeck-us.net>
To: Alexey Klimov <klimov.linux@gmail.com>, wim@linux-watchdog.org
Cc: linux-watchdog@vger.kernel.org, gregkh@linuxfoundation.org,
oneukum@suse.com, linux-usb@vger.kernel.org,
linux-kernel@vger.kernel.org, atishp@rivosinc.com,
atishp@atishpatra.org, yury.norov@gmail.com, aklimov@redhat.com,
atomlin@redhat.com, stern@rowland.harvard.edu
Subject: Re: [PATCH v6] watchdog: add driver for StreamLabs USB watchdog device
Date: Thu, 22 Sep 2022 07:37:16 -0700 [thread overview]
Message-ID: <052ab7b7-ef09-a751-bb03-2cd5742083af@roeck-us.net> (raw)
In-Reply-To: <20220917031502.372319-1-klimov.linux@gmail.com>
On 9/16/22 20:15, Alexey Klimov wrote:
> Hi Wim/Guenter,
>
> For me it seems that there could be a potential race condition. I have to rely
> on watchdog_active(&streamlabs_wdt->wdt_dev) function which tests the WDOG_ACTIVE
> bit in struct watchdog_device->status member.
> The watchdog_dev changes the state of the device with ->start() or ->ping() and
> ->stop() methods and updates the WDOG_ACTIVE accordingly.
> In {pre,post}_reset methods here I have to change the state of the device from
> running to stopped and back to running conditionally, however WDOG_ACTIVE bit
> could be updated in between these callbacks execution or starting/stopping
> the device can race.
> For instance, I see the potential dangerous race like this:
>
> CPUX CPUY
>
> .. watchdog_stop() {
> .. if (wdd->ops->stop) {
> ...
> err = wdd->ops->stop(wdd)
> }
> usb_streamlabs_wdt_pre_reset() {
> if (watchdog_active())
> stop_command(); /* WDOG_ACTIVE bit is still set
> ... here indicating that watchdog is
> } started, but ->stop() has already
> finished */
> ...
> usb_streamlabs_wdt_post_reset() {
> if (watchdog_active())
> start_command();
> }
> ... /* WDOG_ACTIVE is updated here */
> clear_bit(WDOG_ACTIVE, &wdd->status);
> }
>
> As a result, the watchdog subsystem "thinks" that watchdog is not active and should
> not be pinged. However, the driver observed using watchdog_active() that watchdog
> was active during {pre,post}_reset and restarted the device which will lead to
> unexpected reset. It is very unlikely race to happen but consequence is fatal.
> In other words, there are two independent paths leading to driver changing
> the state of the watchdog device and one path relies on status that can be changed
> by another path.
>
> Thinking about that I see the following approaches:
>
> 1. Introduce a variable in struct streamlabs_wdt that tracks the state of the
> watchdog device itself and checking/updating the state of a device happens under
> semaphore lock.
> Obviously, this "internal" to the driver state variable should be used in
> {pre,post}_reset. In case there will be other drivers (say, USB ones) they also
> need to implement this.
>
> or
>
> 2. The updates to wdd->status should happen under wd_data->lock.
> Currently, it is mutex-based. The acquiring and releasing the lock could be
> exported for the drivers to use. The mutex lock probably should be switched
> to a binary semaphore for that.
>
> In such case, in pre_reset() for example, I would need to do:
> static int pre_reset()
> {
> lock_wdd();
> acquire_internal_driver_lock();
>
> if (watchdog_active())
> stop_command();
> }
>
> static int post_reset()
> {
>
> if (watchdog_active())
> start_command();
>
> release_internal_driver_lock();
> unlock_wdd();
> }
>
> There should be an order that we have to acquire subsystem wdd lock first, then
> internal driver lock. Otherwise there could be deadlocks.
>
> This could be done if you think it's more wiser move.
>
> or
>
> 3. The {pre,post}_reset callbacks should execute watchdog_dev.c subsystem functions
> (not sure which functions exactly). Eventually, it will look similar to what is
> described in the previous point with respect to locks order.
> I meant something like this:
>
> static int pre_reset()
> {
> watchdog_dev_pre_reset_prepare();
> }
>
> static int post_reset()
> {
> watchdog_dev_post_reset_done();
> }
>
> In watchdog_dev.c:
> void watchdog_dev_pre_reset_prepare()
> {
> mutex_lock(&wd_data->lock); <-- should be changed to semaphore too?
>
> watchdog_stop(wdd); <-- without updating WDOG_ACTIVE bit?
> or there should be a way to indicate
> to watchdog_dev_post_reset_done() if
> watchdog should be started or not
> }
>
> void watchdog_dev_post_reset_done()
> {
> if (watchdog_active())
> watchdog_start(wdd);
>
> mutex_unlock(&wd_data->lock);
> }
>
> I didn't really thought about point 3 yet. For me personally the point 2 seems
> the like right way to go but you have more experience with that. The exported
> locks could be re-used by other drivers if needed in future.
> In case of point 1 each USB driver should deal with {pre,post}_reset by themselves.
>
> Any thoughts?
>
Please go with 1). pre_reset/post_reset functionality is a first in the watchdog
subsystem and the first to require locking outside the scope of a function or set
of functions. I'd rather avoid having to deal with the potential consequences
in the watchdog core. We can do that if/when it becomes more common and after
we have a good understanding of the potential consequences.
Thanks,
Guenter
> Thanks,
> Alexey
next prev parent reply other threads:[~2022-09-22 14:38 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-09-17 3:05 Alexey Klimov
2022-09-17 3:15 ` Alexey Klimov
2022-09-22 14:37 ` Guenter Roeck [this message]
2022-09-22 14:41 ` 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=052ab7b7-ef09-a751-bb03-2cd5742083af@roeck-us.net \
--to=linux@roeck-us.net \
--cc=aklimov@redhat.com \
--cc=atishp@atishpatra.org \
--cc=atishp@rivosinc.com \
--cc=atomlin@redhat.com \
--cc=gregkh@linuxfoundation.org \
--cc=klimov.linux@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=linux-watchdog@vger.kernel.org \
--cc=oneukum@suse.com \
--cc=stern@rowland.harvard.edu \
--cc=wim@linux-watchdog.org \
--cc=yury.norov@gmail.com \
/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®