diff mbox series

watchdog: make nowayout sysfs file writable

Message ID 20191105123125.25985-1-linux@rasmusvillemoes.dk (mailing list archive)
State Changes Requested
Headers show
Series watchdog: make nowayout sysfs file writable | expand

Commit Message

Rasmus Villemoes Nov. 5, 2019, 12:31 p.m. UTC
It can be useful to delay setting the nowayout feature for a watchdog
device. Moreover, not every driver (notably gpio_wdt) implements a
nowayout module parameter/otherwise respects CONFIG_WATCHDOG_NOWAYOUT,
and modifying those drivers carries a risk of causing a regression for
someone who has two watchdog devices, sets CONFIG_WATCHDOG_NOWAYOUT
and somehow relies on the gpio_wdt driver being ignorant of
that (i.e., allowing one to gracefully close a gpio_wdt but not the
other watchdog in the system).

So instead, simply make the nowayout sysfs file writable. Obviously,
setting nowayout is a one-way street.

Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
---
 .../ABI/testing/sysfs-class-watchdog          |  9 ++++++--
 drivers/watchdog/watchdog_dev.c               | 22 ++++++++++++++++++-
 2 files changed, 28 insertions(+), 3 deletions(-)

Comments

Guenter Roeck Nov. 5, 2019, 2:22 p.m. UTC | #1
On 11/5/19 4:31 AM, Rasmus Villemoes wrote:
> It can be useful to delay setting the nowayout feature for a watchdog
> device. Moreover, not every driver (notably gpio_wdt) implements a
> nowayout module parameter/otherwise respects CONFIG_WATCHDOG_NOWAYOUT,
> and modifying those drivers carries a risk of causing a regression for
> someone who has two watchdog devices, sets CONFIG_WATCHDOG_NOWAYOUT
> and somehow relies on the gpio_wdt driver being ignorant of
> that (i.e., allowing one to gracefully close a gpio_wdt but not the
> other watchdog in the system).
> 
> So instead, simply make the nowayout sysfs file writable. Obviously,
> setting nowayout is a one-way street.
> 
> Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
> ---
>   .../ABI/testing/sysfs-class-watchdog          |  9 ++++++--
>   drivers/watchdog/watchdog_dev.c               | 22 ++++++++++++++++++-
>   2 files changed, 28 insertions(+), 3 deletions(-)
> 
> diff --git a/Documentation/ABI/testing/sysfs-class-watchdog b/Documentation/ABI/testing/sysfs-class-watchdog
> index 675f9b537661..9860a8b2ba75 100644
> --- a/Documentation/ABI/testing/sysfs-class-watchdog
> +++ b/Documentation/ABI/testing/sysfs-class-watchdog
> @@ -17,8 +17,13 @@ What:		/sys/class/watchdog/watchdogn/nowayout
>   Date:		August 2015
>   Contact:	Wim Van Sebroeck <wim@iguana.be>
>   Description:
> -		It is a read only file. While reading, it gives '1' if that
> -		device supports nowayout feature else, it gives '0'.
> +		It is a read/write file. While reading, it gives '1'
> +		if the device has the nowayout feature set, otherwise
> +		it gives '0'. Writing a '1' to the file enables the
> +		nowayout feature. Once set, the nowayout feature
> +		cannot be disabled, so writing a '0' either has no
> +		effect (if the feature was already disabled) or
> +		results in a permission error.
>    >   What:		/sys/class/watchdog/watchdogn/state
>   Date:		August 2015
> diff --git a/drivers/watchdog/watchdog_dev.c b/drivers/watchdog/watchdog_dev.c
> index dbd2ad4c9294..0c478b8f8d5a 100644
> --- a/drivers/watchdog/watchdog_dev.c
> +++ b/drivers/watchdog/watchdog_dev.c
> @@ -452,7 +452,27 @@ static ssize_t nowayout_show(struct device *dev, struct device_attribute *attr,
>   
>   	return sprintf(buf, "%d\n", !!test_bit(WDOG_NO_WAY_OUT, &wdd->status));
>   }
> -static DEVICE_ATTR_RO(nowayout);
> +
> +static ssize_t nowayout_store(struct device *dev, struct device_attribute *attr,
> +				const char *buf, size_t len)
> +{
> +	struct watchdog_device *wdd = dev_get_drvdata(dev);
> +	unsigned int value, current;
> +	int ret;
> +
> +	ret = kstrtouint(buf, 0, &value);
> +	if (ret)
> +		return ret;
> +	if (value > 1)
> +		return -EINVAL;
> +	current = !!test_bit(WDOG_NO_WAY_OUT, &wdd->status);

"!!" on test_bit is unnecessary, and assigning the result to an unsigned int
doesn't really make sense. Might as well drop the variable and use test_bit
in the expression below directly.

Guenter

> +	/* nowayout cannot be disabled once set */
> +	if (current && !value)
> +		return -EPERM;
> +	watchdog_set_nowayout(wdd, value);
> +	return len;
> +}
> +static DEVICE_ATTR_RW(nowayout);
>   
>   static ssize_t status_show(struct device *dev, struct device_attribute *attr,
>   				char *buf)
>
Rasmus Villemoes Nov. 6, 2019, 7:44 a.m. UTC | #2
On 05/11/2019 22.16, kbuild test robot wrote:

>    In file included from include/linux/mutex.h:14:0,
>                     from include/linux/kernfs.h:12,
>                     from include/linux/sysfs.h:16,
>                     from include/linux/kobject.h:20,
>                     from include/linux/cdev.h:5,
>                     from drivers/watchdog/watchdog_dev.c:31:
>    drivers/watchdog/watchdog_dev.c: In function 'nowayout_store':
>>> arch/ia64/include/asm/current.h:16:19: error: expected identifier or '(' before 'struct'
>     #define current ((struct task_struct *) ia64_getreg(_IA64_REG_TP))
>                       ^
>    drivers/watchdog/watchdog_dev.c:460:22: note: in expansion of macro 'current'
>      unsigned int value, current;
>                          ^~~~~~~

:facecpalm:

And it happened to work just fine in my test because I was targeting
ppc32 where unlike most other arches, current is not a macro but a
(more-or-less) ordinary global declaration

register struct task_struct *current asm ("r2");

Oh well, already fixed in v2 which dropped current for other reasons.

Rasmus
diff mbox series

Patch

diff --git a/Documentation/ABI/testing/sysfs-class-watchdog b/Documentation/ABI/testing/sysfs-class-watchdog
index 675f9b537661..9860a8b2ba75 100644
--- a/Documentation/ABI/testing/sysfs-class-watchdog
+++ b/Documentation/ABI/testing/sysfs-class-watchdog
@@ -17,8 +17,13 @@  What:		/sys/class/watchdog/watchdogn/nowayout
 Date:		August 2015
 Contact:	Wim Van Sebroeck <wim@iguana.be>
 Description:
-		It is a read only file. While reading, it gives '1' if that
-		device supports nowayout feature else, it gives '0'.
+		It is a read/write file. While reading, it gives '1'
+		if the device has the nowayout feature set, otherwise
+		it gives '0'. Writing a '1' to the file enables the
+		nowayout feature. Once set, the nowayout feature
+		cannot be disabled, so writing a '0' either has no
+		effect (if the feature was already disabled) or
+		results in a permission error.
 
 What:		/sys/class/watchdog/watchdogn/state
 Date:		August 2015
diff --git a/drivers/watchdog/watchdog_dev.c b/drivers/watchdog/watchdog_dev.c
index dbd2ad4c9294..0c478b8f8d5a 100644
--- a/drivers/watchdog/watchdog_dev.c
+++ b/drivers/watchdog/watchdog_dev.c
@@ -452,7 +452,27 @@  static ssize_t nowayout_show(struct device *dev, struct device_attribute *attr,
 
 	return sprintf(buf, "%d\n", !!test_bit(WDOG_NO_WAY_OUT, &wdd->status));
 }
-static DEVICE_ATTR_RO(nowayout);
+
+static ssize_t nowayout_store(struct device *dev, struct device_attribute *attr,
+				const char *buf, size_t len)
+{
+	struct watchdog_device *wdd = dev_get_drvdata(dev);
+	unsigned int value, current;
+	int ret;
+
+	ret = kstrtouint(buf, 0, &value);
+	if (ret)
+		return ret;
+	if (value > 1)
+		return -EINVAL;
+	current = !!test_bit(WDOG_NO_WAY_OUT, &wdd->status);
+	/* nowayout cannot be disabled once set */
+	if (current && !value)
+		return -EPERM;
+	watchdog_set_nowayout(wdd, value);
+	return len;
+}
+static DEVICE_ATTR_RW(nowayout);
 
 static ssize_t status_show(struct device *dev, struct device_attribute *attr,
 				char *buf)