public inbox for linux-clk@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH][next] clk: imx: pll14xx: Fix potential integer overflow on multiplication
@ 2024-10-07  8:48 Colin Ian King
  2024-10-07  9:54 ` Dan Carpenter
  0 siblings, 1 reply; 3+ messages in thread
From: Colin Ian King @ 2024-10-07  8:48 UTC (permalink / raw)
  To: Abel Vesa, Peng Fan, Michael Turquette, Stephen Boyd, Shawn Guo,
	Sascha Hauer, Pengutronix Kernel Team, Fabio Estevam, linux-clk,
	imx, linux-arm-kernel
  Cc: kernel-janitors, linux-kernel

The calculation of fout is using int multiplication and assigning
the result to a u64, this can potentially overflow if the int variable
mdiv is too large. Fix this by making the 65536 a u64 value to ensure a
u64 multiplication is being performed to avoid the overflow.

Fixes: 53990cf9d5b4 ("clk: imx: pll14xx: consolidate rate calculation")
Signed-off-by: Colin Ian King <colin.i.king@gmail.com>
---
 drivers/clk/imx/clk-pll14xx.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/clk/imx/clk-pll14xx.c b/drivers/clk/imx/clk-pll14xx.c
index d63564dbb12c..2afe361fc711 100644
--- a/drivers/clk/imx/clk-pll14xx.c
+++ b/drivers/clk/imx/clk-pll14xx.c
@@ -107,7 +107,7 @@ static long pll14xx_calc_rate(struct clk_pll14xx *pll, int mdiv, int pdiv,
 	u64 fout = prate;
 
 	/* fout = (m * 65536 + k) * Fin / (p * 65536) / (1 << sdiv) */
-	fout *= (mdiv * 65536 + kdiv);
+	fout *= (mdiv * 65536ULL + kdiv);
 	pdiv *= 65536;
 
 	do_div(fout, pdiv << sdiv);
-- 
2.46.2


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

* Re: [PATCH][next] clk: imx: pll14xx: Fix potential integer overflow on multiplication
  2024-10-07  8:48 [PATCH][next] clk: imx: pll14xx: Fix potential integer overflow on multiplication Colin Ian King
@ 2024-10-07  9:54 ` Dan Carpenter
  2024-10-07 10:00   ` Colin King (gmail)
  0 siblings, 1 reply; 3+ messages in thread
From: Dan Carpenter @ 2024-10-07  9:54 UTC (permalink / raw)
  To: Colin Ian King
  Cc: Abel Vesa, Peng Fan, Michael Turquette, Stephen Boyd, Shawn Guo,
	Sascha Hauer, Pengutronix Kernel Team, Fabio Estevam, linux-clk,
	imx, linux-arm-kernel, kernel-janitors, linux-kernel

On Mon, Oct 07, 2024 at 09:48:40AM +0100, Colin Ian King wrote:
> The calculation of fout is using int multiplication and assigning
> the result to a u64, this can potentially overflow if the int variable
> mdiv is too large. Fix this by making the 65536 a u64 value to ensure a
> u64 multiplication is being performed to avoid the overflow.
> 
> Fixes: 53990cf9d5b4 ("clk: imx: pll14xx: consolidate rate calculation")
> Signed-off-by: Colin Ian King <colin.i.king@gmail.com>

mdiv is always clamped in then 0-1023 range by one of these:

	mdiv = FIELD_GET(MDIV_MASK, pll_div_ctl0);
	mdiv = clamp(mdiv, 64, 1023);

so it can't overflow and the Fixes tag is unnecessary.

I think the reason why "fout" is declared as a u64 is because we were worried
that on 32 bit systems the "fout *=" operation could overflow.  That looks
reasonable to me.

regards,
dan carpenter


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

* Re: [PATCH][next] clk: imx: pll14xx: Fix potential integer overflow on multiplication
  2024-10-07  9:54 ` Dan Carpenter
@ 2024-10-07 10:00   ` Colin King (gmail)
  0 siblings, 0 replies; 3+ messages in thread
From: Colin King (gmail) @ 2024-10-07 10:00 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: Abel Vesa, Peng Fan, Michael Turquette, Stephen Boyd, Shawn Guo,
	Sascha Hauer, Pengutronix Kernel Team, Fabio Estevam, linux-clk,
	imx, linux-arm-kernel, kernel-janitors, linux-kernel

On 07/10/2024 10:54, Dan Carpenter wrote:
> On Mon, Oct 07, 2024 at 09:48:40AM +0100, Colin Ian King wrote:
>> The calculation of fout is using int multiplication and assigning
>> the result to a u64, this can potentially overflow if the int variable
>> mdiv is too large. Fix this by making the 65536 a u64 value to ensure a
>> u64 multiplication is being performed to avoid the overflow.
>>
>> Fixes: 53990cf9d5b4 ("clk: imx: pll14xx: consolidate rate calculation")
>> Signed-off-by: Colin Ian King <colin.i.king@gmail.com>
> 
> mdiv is always clamped in then 0-1023 range by one of these:
> 
> 	mdiv = FIELD_GET(MDIV_MASK, pll_div_ctl0);
> 	mdiv = clamp(mdiv, 64, 1023);
> 
> so it can't overflow and the Fixes tag is unnecessary.

Good point.

> 
> I think the reason why "fout" is declared as a u64 is because we were worried
> that on 32 bit systems the "fout *=" operation could overflow.  That looks
> reasonable to me.

Yes, that makes perfect sense. NAK my patch. Apologies for the noise.

Colin

> 
> regards,
> dan carpenter
> 


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

end of thread, other threads:[~2024-10-07 10:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-07  8:48 [PATCH][next] clk: imx: pll14xx: Fix potential integer overflow on multiplication Colin Ian King
2024-10-07  9:54 ` Dan Carpenter
2024-10-07 10:00   ` Colin King (gmail)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox