All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] i2c: altera: Fix potential integer overflow
@ 2020-02-11 14:47 Gustavo A. R. Silva
  2020-02-11 19:54 ` Thor Thayer
  2020-02-13  9:09 ` Wolfram Sang
  0 siblings, 2 replies; 4+ messages in thread
From: Gustavo A. R. Silva @ 2020-02-11 14:47 UTC (permalink / raw)
  To: Thor Thayer, Andy Shevchenko, Wolfram Sang, David Laight
  Cc: linux-i2c, linux-kernel, Gustavo A. R. Silva

Factor out 100 from the equation and do 32-bit arithmetic (3 * clk_mhz / 10)
instead of 64-bit.

Notice that clk_mhz is MHz, so the multiplication will never wrap 32 bits
and there is no need for div_u64().

Addresses-Coverity: 1458369 ("Unintentional integer overflow")
Fixes: 0560ad576268 ("i2c: altera: Add Altera I2C Controller driver")
Suggested-by: David Laight <David.Laight@ACULAB.COM>
Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
---
Changes in v2:
 - Update subject and changelog text.
 - Avoid the need for 64-bit arithmetic at all.

 drivers/i2c/busses/i2c-altera.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/i2c/busses/i2c-altera.c b/drivers/i2c/busses/i2c-altera.c
index 5255d3755411..1de23b4f3809 100644
--- a/drivers/i2c/busses/i2c-altera.c
+++ b/drivers/i2c/busses/i2c-altera.c
@@ -171,7 +171,7 @@ static void altr_i2c_init(struct altr_i2c_dev *idev)
 	/* SCL Low Time */
 	writel(t_low, idev->base + ALTR_I2C_SCL_LOW);
 	/* SDA Hold Time, 300ns */
-	writel(div_u64(300 * clk_mhz, 1000), idev->base + ALTR_I2C_SDA_HOLD);
+	writel(3 * clk_mhz / 10, idev->base + ALTR_I2C_SDA_HOLD);
 
 	/* Mask all master interrupt bits */
 	altr_i2c_int_enable(idev, ALTR_I2C_ALL_IRQ, false);
-- 
2.25.0


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

* Re: [PATCH v2] i2c: altera: Fix potential integer overflow
  2020-02-11 14:47 [PATCH v2] i2c: altera: Fix potential integer overflow Gustavo A. R. Silva
@ 2020-02-11 19:54 ` Thor Thayer
  2020-02-13  9:09 ` Wolfram Sang
  1 sibling, 0 replies; 4+ messages in thread
From: Thor Thayer @ 2020-02-11 19:54 UTC (permalink / raw)
  To: Gustavo A. R. Silva, Andy Shevchenko, Wolfram Sang, David Laight
  Cc: linux-i2c, linux-kernel

On 2/11/20 8:47 AM, Gustavo A. R. Silva wrote:
> Factor out 100 from the equation and do 32-bit arithmetic (3 * clk_mhz / 10)
> instead of 64-bit.
> 
> Notice that clk_mhz is MHz, so the multiplication will never wrap 32 bits
> and there is no need for div_u64().
> 
> Addresses-Coverity: 1458369 ("Unintentional integer overflow")
> Fixes: 0560ad576268 ("i2c: altera: Add Altera I2C Controller driver")
> Suggested-by: David Laight <David.Laight@ACULAB.COM>
> Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
> ---
> Changes in v2:
>   - Update subject and changelog text.
>   - Avoid the need for 64-bit arithmetic at all.
> 
>   drivers/i2c/busses/i2c-altera.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/i2c/busses/i2c-altera.c b/drivers/i2c/busses/i2c-altera.c
> index 5255d3755411..1de23b4f3809 100644
> --- a/drivers/i2c/busses/i2c-altera.c
> +++ b/drivers/i2c/busses/i2c-altera.c
> @@ -171,7 +171,7 @@ static void altr_i2c_init(struct altr_i2c_dev *idev)
>   	/* SCL Low Time */
>   	writel(t_low, idev->base + ALTR_I2C_SCL_LOW);
>   	/* SDA Hold Time, 300ns */
> -	writel(div_u64(300 * clk_mhz, 1000), idev->base + ALTR_I2C_SDA_HOLD);
> +	writel(3 * clk_mhz / 10, idev->base + ALTR_I2C_SDA_HOLD);
>   
>   	/* Mask all master interrupt bits */
>   	altr_i2c_int_enable(idev, ALTR_I2C_ALL_IRQ, false);
> 
Reviewed-by: Thor Thayer <thor.thayer@linux.intel.com>

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

* Re: [PATCH v2] i2c: altera: Fix potential integer overflow
  2020-02-11 14:47 [PATCH v2] i2c: altera: Fix potential integer overflow Gustavo A. R. Silva
  2020-02-11 19:54 ` Thor Thayer
@ 2020-02-13  9:09 ` Wolfram Sang
  2020-02-13  9:58   ` David Laight
  1 sibling, 1 reply; 4+ messages in thread
From: Wolfram Sang @ 2020-02-13  9:09 UTC (permalink / raw)
  To: Gustavo A. R. Silva
  Cc: Thor Thayer, Andy Shevchenko, David Laight, linux-i2c, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 724 bytes --]

On Tue, Feb 11, 2020 at 08:47:04AM -0600, Gustavo A. R. Silva wrote:
> Factor out 100 from the equation and do 32-bit arithmetic (3 * clk_mhz / 10)
> instead of 64-bit.
> 
> Notice that clk_mhz is MHz, so the multiplication will never wrap 32 bits
> and there is no need for div_u64().

Was there ever? With

	u32 clk_mhz = clk_get_rate(idev->i2c_clk) / 1000000;

a later multiplication with 300 should not wrap u32?

>  	/* SDA Hold Time, 300ns */
> -	writel(div_u64(300 * clk_mhz, 1000), idev->base + ALTR_I2C_SDA_HOLD);
> +	writel(3 * clk_mhz / 10, idev->base + ALTR_I2C_SDA_HOLD);

The change itself is OK, yet I wonder about the comment above:

'clk_mhz * 0.3' will not give a constant 300ns, or?


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

* RE: [PATCH v2] i2c: altera: Fix potential integer overflow
  2020-02-13  9:09 ` Wolfram Sang
@ 2020-02-13  9:58   ` David Laight
  0 siblings, 0 replies; 4+ messages in thread
From: David Laight @ 2020-02-13  9:58 UTC (permalink / raw)
  To: 'Wolfram Sang', Gustavo A. R. Silva
  Cc: Thor Thayer, Andy Shevchenko, linux-i2c, linux-kernel

From: Wolfram Sang
> Sent: 13 February 2020 09:10
> 
> On Tue, Feb 11, 2020 at 08:47:04AM -0600, Gustavo A. R. Silva wrote:
> > Factor out 100 from the equation and do 32-bit arithmetic (3 * clk_mhz / 10)
> > instead of 64-bit.
> >
> > Notice that clk_mhz is MHz, so the multiplication will never wrap 32 bits
> > and there is no need for div_u64().
> 
> Was there ever? With
> 
> 	u32 clk_mhz = clk_get_rate(idev->i2c_clk) / 1000000;
> 
> a later multiplication with 300 should not wrap u32?
> 
> >  	/* SDA Hold Time, 300ns */
> > -	writel(div_u64(300 * clk_mhz, 1000), idev->base + ALTR_I2C_SDA_HOLD);
> > +	writel(3 * clk_mhz / 10, idev->base + ALTR_I2C_SDA_HOLD);
> 
> The change itself is OK, yet I wonder about the comment above:
> 
> 'clk_mhz * 0.3' will not give a constant 300ns, or?

Depends on the definition of the register.
A count of zero may mean one clock period.
So maybe it could have (3 * clk - 1)/10 instead of (3 * clk + 9)/10.
OTOH nothing probably requires that much hold time.

If that is the 'standard' Altera Avalon slave I2C 'megafunction' I2C
master then it is probably so slow to use it can't matter.
Most of those blocks are crap, they aren't even small.

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)


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

end of thread, other threads:[~2020-02-13  9:58 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-11 14:47 [PATCH v2] i2c: altera: Fix potential integer overflow Gustavo A. R. Silva
2020-02-11 19:54 ` Thor Thayer
2020-02-13  9:09 ` Wolfram Sang
2020-02-13  9:58   ` David Laight

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.