watchdog: dw_wdt: fix signedness bug in dw_wdt_top_in_seconds()
PATCH 1/1 watchdog: dw_wdt: fix signedness bug in dw_wdt_top_in_seconds()
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
--- drivers/watchdog/dw_wdt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/watchdog/dw_wdt.c +1 -1
@@ -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 )
Signature · 1 line
2.6.4
Quoted 9 lines · 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 to0xffffffff80000000, then converted to unsigned0xffffffff80000000, 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.
Quoted 8 lines
Let's assume clk rate is 25MHZ,
Before the patch:dw_wdt_top_in_seconds(15)= -864612050After the patch:
dw_wdt_top_in_seconds(15)= 85Signed-off-by Jisheng Zhang <[email protected]>
Nice catch.
Quoted 18 lines
--- drivers/watchdog/dw_wdt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/watchdog/dw_wdt.c +1
-1@@ -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 )
Hi Jisheng,
Quoted 37 lines
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 to0xffffffff80000000, then converted to unsigned0xffffffff80000000, 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)= -864612050After the patch:
dw_wdt_top_in_seconds(15)= 85Signed-off-by Jisheng Zhang <[email protected]>--- drivers/watchdog/dw_wdt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/watchdog/dw_wdt.c +1
-1@@ -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 )Signature · 1 line
2.6.4
Patch is added to linux-watchdog-next.
Kind regards,
Wim.