2013-04-08 09:12:25

by Hector Palacios

[permalink] [raw]
Subject: [PATCH v2] watchdog: core: don't try to stop device if not running

A watchdog device may be stopped from userspace using WDIOC_SETOPTIONS
ioctl and flag WDIOS_DISABLECARD. If the device is closed after this
operation, watchdog_release() is called and status bits checked for
stopping it. Besides, if the device has not been unregistered a critical
message "watchdog did not stop!" is printed, although the ioctl may have
successfully stopped it already.

Without the patch a user application sample code like this will successfully
stop the watchdog, but the kernel will output the message
"watchdog did not stop!":

wd_fd = open("/dev/watchdog", O_RDWR);

flags = WDIOS_DISABLECARD;
ioctl(wd_fd, WDIOC_SETOPTIONS, &flags);

close(wd_fd);

Signed-off-by: Hector Palacios <[email protected]>
---

Changes from v1:
- Make it less intrusive with 'if/else if' clauses
- Change the logic of 'err' variable for better readability of code
- Remove one-liner if brackets that broke coding sytle

drivers/watchdog/watchdog_dev.c | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/drivers/watchdog/watchdog_dev.c b/drivers/watchdog/watchdog_dev.c
index ef8edec..8e0f1b8 100644
--- a/drivers/watchdog/watchdog_dev.c
+++ b/drivers/watchdog/watchdog_dev.c
@@ -463,15 +463,17 @@ out:
static int watchdog_release(struct inode *inode, struct file *file)
{
struct watchdog_device *wdd = file->private_data;
- int err = -EBUSY;
+ int err = 0;

/*
* We only stop the watchdog if we received the magic character
* or if WDIOF_MAGICCLOSE is not set. If nowayout was set then
* watchdog_stop will fail.
*/
- if (test_and_clear_bit(WDOG_ALLOW_RELEASE, &wdd->status) ||
- !(wdd->info->options & WDIOF_MAGICCLOSE))
+ if (test_bit(WDOG_ACTIVE, &wdd->status))
+ err = -EBUSY;
+ else if (test_and_clear_bit(WDOG_ALLOW_RELEASE, &wdd->status) ||
+ !(wdd->info->options & WDIOF_MAGICCLOSE))
err = watchdog_stop(wdd);

/* If the watchdog was not stopped, send a keepalive ping */
--
1.8.2


2013-04-08 13:41:16

by Guenter Roeck

[permalink] [raw]
Subject: Re: [PATCH v2] watchdog: core: don't try to stop device if not running

On Mon, Apr 08, 2013 at 11:10:58AM +0200, Hector Palacios wrote:
> A watchdog device may be stopped from userspace using WDIOC_SETOPTIONS
> ioctl and flag WDIOS_DISABLECARD. If the device is closed after this
> operation, watchdog_release() is called and status bits checked for
> stopping it. Besides, if the device has not been unregistered a critical
> message "watchdog did not stop!" is printed, although the ioctl may have
> successfully stopped it already.
>
> Without the patch a user application sample code like this will successfully
> stop the watchdog, but the kernel will output the message
> "watchdog did not stop!":
>
> wd_fd = open("/dev/watchdog", O_RDWR);
>
> flags = WDIOS_DISABLECARD;
> ioctl(wd_fd, WDIOC_SETOPTIONS, &flags);
>
> close(wd_fd);
>
> Signed-off-by: Hector Palacios <[email protected]>
> ---
>
> Changes from v1:
> - Make it less intrusive with 'if/else if' clauses
> - Change the logic of 'err' variable for better readability of code
> - Remove one-liner if brackets that broke coding sytle
>
> drivers/watchdog/watchdog_dev.c | 8 +++++---
> 1 file changed, 5 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/watchdog/watchdog_dev.c b/drivers/watchdog/watchdog_dev.c
> index ef8edec..8e0f1b8 100644
> --- a/drivers/watchdog/watchdog_dev.c
> +++ b/drivers/watchdog/watchdog_dev.c
> @@ -463,15 +463,17 @@ out:
> static int watchdog_release(struct inode *inode, struct file *file)
> {
> struct watchdog_device *wdd = file->private_data;
> - int err = -EBUSY;
> + int err = 0;
>
> /*
> * We only stop the watchdog if we received the magic character
> * or if WDIOF_MAGICCLOSE is not set. If nowayout was set then
> * watchdog_stop will fail.
> */
> - if (test_and_clear_bit(WDOG_ALLOW_RELEASE, &wdd->status) ||
> - !(wdd->info->options & WDIOF_MAGICCLOSE))
> + if (test_bit(WDOG_ACTIVE, &wdd->status))
> + err = -EBUSY;
> + else if (test_and_clear_bit(WDOG_ALLOW_RELEASE, &wdd->status) ||
> + !(wdd->info->options & WDIOF_MAGICCLOSE))
> err = watchdog_stop(wdd);
>
Looking at it again, it is now broken and fails with EBUSY if WDOG_ACTIVE is
set, and it tries to stop the watchdog if it is already stopped.

Guenter

2013-04-08 14:15:44

by Hector Palacios

[permalink] [raw]
Subject: Re: [PATCH v2] watchdog: core: don't try to stop device if not running

On 04/08/2013 03:41 PM, Guenter Roeck wrote:
> On Mon, Apr 08, 2013 at 11:10:58AM +0200, Hector Palacios wrote:
>> A watchdog device may be stopped from userspace using WDIOC_SETOPTIONS
>> ioctl and flag WDIOS_DISABLECARD. If the device is closed after this
>> operation, watchdog_release() is called and status bits checked for
>> stopping it. Besides, if the device has not been unregistered a critical
>> message "watchdog did not stop!" is printed, although the ioctl may have
>> successfully stopped it already.
>>
>> Without the patch a user application sample code like this will successfully
>> stop the watchdog, but the kernel will output the message
>> "watchdog did not stop!":
>>
>> wd_fd = open("/dev/watchdog", O_RDWR);
>>
>> flags = WDIOS_DISABLECARD;
>> ioctl(wd_fd, WDIOC_SETOPTIONS, &flags);
>>
>> close(wd_fd);
>>
>> Signed-off-by: Hector Palacios <[email protected]>
>> ---
>>
>> Changes from v1:
>> - Make it less intrusive with 'if/else if' clauses
>> - Change the logic of 'err' variable for better readability of code
>> - Remove one-liner if brackets that broke coding sytle
>>
>> drivers/watchdog/watchdog_dev.c | 8 +++++---
>> 1 file changed, 5 insertions(+), 3 deletions(-)
>>
>> diff --git a/drivers/watchdog/watchdog_dev.c b/drivers/watchdog/watchdog_dev.c
>> index ef8edec..8e0f1b8 100644
>> --- a/drivers/watchdog/watchdog_dev.c
>> +++ b/drivers/watchdog/watchdog_dev.c
>> @@ -463,15 +463,17 @@ out:
>> static int watchdog_release(struct inode *inode, struct file *file)
>> {
>> struct watchdog_device *wdd = file->private_data;
>> - int err = -EBUSY;
>> + int err = 0;
>>
>> /*
>> * We only stop the watchdog if we received the magic character
>> * or if WDIOF_MAGICCLOSE is not set. If nowayout was set then
>> * watchdog_stop will fail.
>> */
>> - if (test_and_clear_bit(WDOG_ALLOW_RELEASE, &wdd->status) ||
>> - !(wdd->info->options & WDIOF_MAGICCLOSE))
>> + if (test_bit(WDOG_ACTIVE, &wdd->status))
>> + err = -EBUSY;
>> + else if (test_and_clear_bit(WDOG_ALLOW_RELEASE, &wdd->status) ||
>> + !(wdd->info->options & WDIOF_MAGICCLOSE))
>> err = watchdog_stop(wdd);
>>
> Looking at it again, it is now broken and fails with EBUSY if WDOG_ACTIVE is
> set, and it tries to stop the watchdog if it is already stopped.

You are right, good catch! So your patch version wins.
Thank you
--
H?ctor Palacios