Linux-Watchdog Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH] watchdog: imx2_wdt: fix min() calculation in imx2_wdt_set_timeout
@ 2019-08-12 13:13 Rasmus Villemoes
  2019-08-12 13:28 ` Guenter Roeck
  0 siblings, 1 reply; 2+ messages in thread
From: Rasmus Villemoes @ 2019-08-12 13:13 UTC (permalink / raw)
  To: Wim Van Sebroeck, Guenter Roeck
  Cc: Georg Hofmann, linux-watchdog, linux-kernel, Rasmus Villemoes, stable

Converting from ms to s requires dividing by 1000, not multiplying. So
this is currently taking the smaller of new_timeout and 1.28e8,
i.e. effectively new_timeout.

The driver knows what it set max_hw_heartbeat_ms to, so use that
value instead of doing a division at run-time.

FWIW, this can easily be tested by booting into a busybox shell and
doing "watchdog -t 5 -T 130 /dev/watchdog" - without this patch, the
watchdog fires after 130&127 == 2 seconds.

Fixes: b07e228eee69 "watchdog: imx2_wdt: Fix set_timeout for big timeout values"
Cc: stable@vger.kernel.org # 5.2 plus anything the above got backported to
Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
---
This should really be handled in the watchdog core for any driver that
reports max_hw_heartbeat_ms.

The same pattern appears in aspeed_wdt.c. I don't have the hardware, but
s#wdd->max_hw_heartbeat_ms * 1000#WDT_MAX_TIMEOUT_MS/1000U# should fix that one.


 drivers/watchdog/imx2_wdt.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/watchdog/imx2_wdt.c b/drivers/watchdog/imx2_wdt.c
index 32af3974e6bb..8d019a961ccc 100644
--- a/drivers/watchdog/imx2_wdt.c
+++ b/drivers/watchdog/imx2_wdt.c
@@ -55,7 +55,7 @@
 
 #define IMX2_WDT_WMCR		0x08		/* Misc Register */
 
-#define IMX2_WDT_MAX_TIME	128
+#define IMX2_WDT_MAX_TIME	128U
 #define IMX2_WDT_DEFAULT_TIME	60		/* in seconds */
 
 #define WDOG_SEC_TO_COUNT(s)	((s * 2 - 1) << 8)
@@ -180,7 +180,7 @@ static int imx2_wdt_set_timeout(struct watchdog_device *wdog,
 {
 	unsigned int actual;
 
-	actual = min(new_timeout, wdog->max_hw_heartbeat_ms * 1000);
+	actual = min(new_timeout, IMX2_WDT_MAX_TIME);
 	__imx2_wdt_set_timeout(wdog, actual);
 	wdog->timeout = new_timeout;
 	return 0;
-- 
2.20.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] watchdog: imx2_wdt: fix min() calculation in imx2_wdt_set_timeout
  2019-08-12 13:13 [PATCH] watchdog: imx2_wdt: fix min() calculation in imx2_wdt_set_timeout Rasmus Villemoes
@ 2019-08-12 13:28 ` Guenter Roeck
  0 siblings, 0 replies; 2+ messages in thread
From: Guenter Roeck @ 2019-08-12 13:28 UTC (permalink / raw)
  To: Rasmus Villemoes, Wim Van Sebroeck
  Cc: Georg Hofmann, linux-watchdog, linux-kernel, stable

On 8/12/19 6:13 AM, Rasmus Villemoes wrote:
> Converting from ms to s requires dividing by 1000, not multiplying. So
> this is currently taking the smaller of new_timeout and 1.28e8,
> i.e. effectively new_timeout.
> 
> The driver knows what it set max_hw_heartbeat_ms to, so use that
> value instead of doing a division at run-time.
> 
> FWIW, this can easily be tested by booting into a busybox shell and
> doing "watchdog -t 5 -T 130 /dev/watchdog" - without this patch, the
> watchdog fires after 130&127 == 2 seconds.
> 
> Fixes: b07e228eee69 "watchdog: imx2_wdt: Fix set_timeout for big timeout values"
> Cc: stable@vger.kernel.org # 5.2 plus anything the above got backported to
> Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>

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

> ---
> This should really be handled in the watchdog core for any driver that
> reports max_hw_heartbeat_ms.
> 
Good point. I'll see if I can write a patch.

Guenter

> The same pattern appears in aspeed_wdt.c. I don't have the hardware, but
> s#wdd->max_hw_heartbeat_ms * 1000#WDT_MAX_TIMEOUT_MS/1000U# should fix that one.
> 
> 
>   drivers/watchdog/imx2_wdt.c | 4 ++--
>   1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/watchdog/imx2_wdt.c b/drivers/watchdog/imx2_wdt.c
> index 32af3974e6bb..8d019a961ccc 100644
> --- a/drivers/watchdog/imx2_wdt.c
> +++ b/drivers/watchdog/imx2_wdt.c
> @@ -55,7 +55,7 @@
>   
>   #define IMX2_WDT_WMCR		0x08		/* Misc Register */
>   
> -#define IMX2_WDT_MAX_TIME	128
> +#define IMX2_WDT_MAX_TIME	128U
>   #define IMX2_WDT_DEFAULT_TIME	60		/* in seconds */
>   
>   #define WDOG_SEC_TO_COUNT(s)	((s * 2 - 1) << 8)
> @@ -180,7 +180,7 @@ static int imx2_wdt_set_timeout(struct watchdog_device *wdog,
>   {
>   	unsigned int actual;
>   
> -	actual = min(new_timeout, wdog->max_hw_heartbeat_ms * 1000);
> +	actual = min(new_timeout, IMX2_WDT_MAX_TIME);
>   	__imx2_wdt_set_timeout(wdog, actual);
>   	wdog->timeout = new_timeout;
>   	return 0;
> 


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, back to index

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-08-12 13:13 [PATCH] watchdog: imx2_wdt: fix min() calculation in imx2_wdt_set_timeout Rasmus Villemoes
2019-08-12 13:28 ` Guenter Roeck

Linux-Watchdog Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-watchdog/0 linux-watchdog/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-watchdog linux-watchdog/ https://lore.kernel.org/linux-watchdog \
		linux-watchdog@vger.kernel.org linux-watchdog@archiver.kernel.org
	public-inbox-index linux-watchdog


Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-watchdog


AGPL code for this site: git clone https://public-inbox.org/ public-inbox