All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] watchdog: dw_wdt: fix signedness bug in dw_wdt_top_in_seconds()
@ 2015-12-15 14:25 ` Jisheng Zhang
  0 siblings, 0 replies; 5+ messages in thread
From: Jisheng Zhang @ 2015-12-15 14:25 UTC (permalink / raw)
  To: wim
  Cc: linux-watchdog, linux-kernel, linux-arm-kernel,
	sebastian.hesselbarth, Jisheng Zhang

On 64bit platforms, "(1 << (16 + top)) / clk_get_rate(dw_wdt.clk)" is
sign-extended to 64bit then converted to unsigned 64bit, finally divide
the clk rate. If the top is the maximum TOP i.e 15, "(1 << (16 +15))"
will be sign-extended to 0xffffffff80000000, then converted to unsigned
0xffffffff80000000, which is a huge number, thus the final result is
wrong.

We fix this issue by giving usigned value(1U in this case) at first.

Let's assume clk rate is 25MHZ,
Before the patch:
dw_wdt_top_in_seconds(15) = -864612050

After the patch:
dw_wdt_top_in_seconds(15) = 85

Signed-off-by: Jisheng Zhang <jszhang@marvell.com>
---
 drivers/watchdog/dw_wdt.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/watchdog/dw_wdt.c b/drivers/watchdog/dw_wdt.c
index 6ea0634..8fefa4ad 100644
--- a/drivers/watchdog/dw_wdt.c
+++ b/drivers/watchdog/dw_wdt.c
@@ -81,7 +81,7 @@ static inline int dw_wdt_top_in_seconds(unsigned top)
 	 * There are 16 possible timeout values in 0..15 where the number of
 	 * cycles is 2 ^ (16 + i) and the watchdog counts down.
 	 */
-	return (1 << (16 + top)) / clk_get_rate(dw_wdt.clk);
+	return (1U << (16 + top)) / clk_get_rate(dw_wdt.clk);
 }
 
 static int dw_wdt_get_top(void)
-- 
2.6.4


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

* [PATCH] watchdog: dw_wdt: fix signedness bug in dw_wdt_top_in_seconds()
@ 2015-12-15 14:25 ` Jisheng Zhang
  0 siblings, 0 replies; 5+ messages in thread
From: Jisheng Zhang @ 2015-12-15 14:25 UTC (permalink / raw)
  To: linux-arm-kernel

On 64bit platforms, "(1 << (16 + top)) / clk_get_rate(dw_wdt.clk)" is
sign-extended to 64bit then converted to unsigned 64bit, finally divide
the clk rate. If the top is the maximum TOP i.e 15, "(1 << (16 +15))"
will be sign-extended to 0xffffffff80000000, then converted to unsigned
0xffffffff80000000, which is a huge number, thus the final result is
wrong.

We fix this issue by giving usigned value(1U in this case) at first.

Let's assume clk rate is 25MHZ,
Before the patch:
dw_wdt_top_in_seconds(15) = -864612050

After the patch:
dw_wdt_top_in_seconds(15) = 85

Signed-off-by: Jisheng Zhang <jszhang@marvell.com>
---
 drivers/watchdog/dw_wdt.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/watchdog/dw_wdt.c b/drivers/watchdog/dw_wdt.c
index 6ea0634..8fefa4ad 100644
--- a/drivers/watchdog/dw_wdt.c
+++ b/drivers/watchdog/dw_wdt.c
@@ -81,7 +81,7 @@ static inline int dw_wdt_top_in_seconds(unsigned top)
 	 * There are 16 possible timeout values in 0..15 where the number of
 	 * cycles is 2 ^ (16 + i) and the watchdog counts down.
 	 */
-	return (1 << (16 + top)) / clk_get_rate(dw_wdt.clk);
+	return (1U << (16 + top)) / clk_get_rate(dw_wdt.clk);
 }
 
 static int dw_wdt_get_top(void)
-- 
2.6.4

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

* Re: [PATCH] watchdog: dw_wdt: fix signedness bug in dw_wdt_top_in_seconds()
  2015-12-15 14:25 ` Jisheng Zhang
@ 2015-12-15 16:15   ` Guenter Roeck
  -1 siblings, 0 replies; 5+ messages in thread
From: Guenter Roeck @ 2015-12-15 16:15 UTC (permalink / raw)
  To: Jisheng Zhang, wim
  Cc: linux-watchdog, linux-kernel, linux-arm-kernel, sebastian.hesselbarth

On 12/15/2015 06:25 AM, Jisheng Zhang wrote:
> On 64bit platforms, "(1 << (16 + top)) / clk_get_rate(dw_wdt.clk)" is
> sign-extended to 64bit then converted to unsigned 64bit, finally divide
> the clk rate. If the top is the maximum TOP i.e 15, "(1 << (16 +15))"
> will be sign-extended to 0xffffffff80000000, then converted to unsigned
> 0xffffffff80000000, which is a huge number, thus the final result is
> wrong.
>
> We fix this issue by giving usigned value(1U in this case) at first.
>
> Let's assume clk rate is 25MHZ,
> Before the patch:
> dw_wdt_top_in_seconds(15) = -864612050
>
> After the patch:
> dw_wdt_top_in_seconds(15) = 85
>
> Signed-off-by: Jisheng Zhang <jszhang@marvell.com>

Nice catch.

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

> ---
>   drivers/watchdog/dw_wdt.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/watchdog/dw_wdt.c b/drivers/watchdog/dw_wdt.c
> index 6ea0634..8fefa4ad 100644
> --- a/drivers/watchdog/dw_wdt.c
> +++ b/drivers/watchdog/dw_wdt.c
> @@ -81,7 +81,7 @@ static inline int dw_wdt_top_in_seconds(unsigned top)
>   	 * There are 16 possible timeout values in 0..15 where the number of
>   	 * cycles is 2 ^ (16 + i) and the watchdog counts down.
>   	 */
> -	return (1 << (16 + top)) / clk_get_rate(dw_wdt.clk);
> +	return (1U << (16 + top)) / clk_get_rate(dw_wdt.clk);
>   }
>
>   static int dw_wdt_get_top(void)
>


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

* [PATCH] watchdog: dw_wdt: fix signedness bug in dw_wdt_top_in_seconds()
@ 2015-12-15 16:15   ` Guenter Roeck
  0 siblings, 0 replies; 5+ messages in thread
From: Guenter Roeck @ 2015-12-15 16:15 UTC (permalink / raw)
  To: linux-arm-kernel

On 12/15/2015 06:25 AM, Jisheng Zhang wrote:
> On 64bit platforms, "(1 << (16 + top)) / clk_get_rate(dw_wdt.clk)" is
> sign-extended to 64bit then converted to unsigned 64bit, finally divide
> the clk rate. If the top is the maximum TOP i.e 15, "(1 << (16 +15))"
> will be sign-extended to 0xffffffff80000000, then converted to unsigned
> 0xffffffff80000000, which is a huge number, thus the final result is
> wrong.
>
> We fix this issue by giving usigned value(1U in this case) at first.
>
> Let's assume clk rate is 25MHZ,
> Before the patch:
> dw_wdt_top_in_seconds(15) = -864612050
>
> After the patch:
> dw_wdt_top_in_seconds(15) = 85
>
> Signed-off-by: Jisheng Zhang <jszhang@marvell.com>

Nice catch.

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

> ---
>   drivers/watchdog/dw_wdt.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/watchdog/dw_wdt.c b/drivers/watchdog/dw_wdt.c
> index 6ea0634..8fefa4ad 100644
> --- a/drivers/watchdog/dw_wdt.c
> +++ b/drivers/watchdog/dw_wdt.c
> @@ -81,7 +81,7 @@ static inline int dw_wdt_top_in_seconds(unsigned top)
>   	 * There are 16 possible timeout values in 0..15 where the number of
>   	 * cycles is 2 ^ (16 + i) and the watchdog counts down.
>   	 */
> -	return (1 << (16 + top)) / clk_get_rate(dw_wdt.clk);
> +	return (1U << (16 + top)) / clk_get_rate(dw_wdt.clk);
>   }
>
>   static int dw_wdt_get_top(void)
>

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

* Re: [PATCH] watchdog: dw_wdt: fix signedness bug in dw_wdt_top_in_seconds()
  2015-12-15 14:25 ` Jisheng Zhang
  (?)
  (?)
@ 2015-12-27 20:06 ` Wim Van Sebroeck
  -1 siblings, 0 replies; 5+ messages in thread
From: Wim Van Sebroeck @ 2015-12-27 20:06 UTC (permalink / raw)
  To: Jisheng Zhang
  Cc: linux-watchdog, linux-kernel, linux-arm-kernel, sebastian.hesselbarth

Hi Jisheng,

> On 64bit platforms, "(1 << (16 + top)) / clk_get_rate(dw_wdt.clk)" is
> sign-extended to 64bit then converted to unsigned 64bit, finally divide
> the clk rate. If the top is the maximum TOP i.e 15, "(1 << (16 +15))"
> will be sign-extended to 0xffffffff80000000, then converted to unsigned
> 0xffffffff80000000, which is a huge number, thus the final result is
> wrong.
> 
> We fix this issue by giving usigned value(1U in this case) at first.
> 
> Let's assume clk rate is 25MHZ,
> Before the patch:
> dw_wdt_top_in_seconds(15) = -864612050
> 
> After the patch:
> dw_wdt_top_in_seconds(15) = 85
> 
> Signed-off-by: Jisheng Zhang <jszhang@marvell.com>
> ---
>  drivers/watchdog/dw_wdt.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/watchdog/dw_wdt.c b/drivers/watchdog/dw_wdt.c
> index 6ea0634..8fefa4ad 100644
> --- a/drivers/watchdog/dw_wdt.c
> +++ b/drivers/watchdog/dw_wdt.c
> @@ -81,7 +81,7 @@ static inline int dw_wdt_top_in_seconds(unsigned top)
>  	 * There are 16 possible timeout values in 0..15 where the number of
>  	 * cycles is 2 ^ (16 + i) and the watchdog counts down.
>  	 */
> -	return (1 << (16 + top)) / clk_get_rate(dw_wdt.clk);
> +	return (1U << (16 + top)) / clk_get_rate(dw_wdt.clk);
>  }
>  
>  static int dw_wdt_get_top(void)
> -- 
> 2.6.4
> 

Patch is added to linux-watchdog-next.

Kind regards,
Wim.


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

end of thread, other threads:[~2015-12-27 20:06 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-12-15 14:25 [PATCH] watchdog: dw_wdt: fix signedness bug in dw_wdt_top_in_seconds() Jisheng Zhang
2015-12-15 14:25 ` Jisheng Zhang
2015-12-15 16:15 ` Guenter Roeck
2015-12-15 16:15   ` Guenter Roeck
2015-12-27 20:06 ` Wim Van Sebroeck

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.