linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: linux@roeck-us.net (Guenter Roeck)
To: linux-arm-kernel@lists.infradead.org
Subject: [PATCH] watchdog: dw_wdt: fix signedness bug in dw_wdt_top_in_seconds()
Date: Tue, 15 Dec 2015 08:15:18 -0800	[thread overview]
Message-ID: <56703C96.90105@roeck-us.net> (raw)
In-Reply-To: <1450189527-1015-1-git-send-email-jszhang@marvell.com>

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)
>

      reply	other threads:[~2015-12-15 16:15 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-12-15 14:25 [PATCH] watchdog: dw_wdt: fix signedness bug in dw_wdt_top_in_seconds() Jisheng Zhang
2015-12-15 16:15 ` Guenter Roeck [this message]

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=56703C96.90105@roeck-us.net \
    --to=linux@roeck-us.net \
    --cc=linux-arm-kernel@lists.infradead.org \
    /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;
as well as URLs for NNTP newsgroup(s).