linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] i2c: altera: Use 64-bit arithmetic instead of 32-bit
@ 2020-02-10 19:26 Gustavo A. R. Silva
  2020-02-10 20:42 ` Thor Thayer
  2020-02-11 11:44 ` David Laight
  0 siblings, 2 replies; 3+ messages in thread
From: Gustavo A. R. Silva @ 2020-02-10 19:26 UTC (permalink / raw)
  To: Thor Thayer; +Cc: linux-i2c, linux-kernel, Gustavo A. R. Silva

Add suffix ULL to constant 300 in order to avoid a potential integer
overflow and give the compiler complete information about the proper
arithmetic to use. Notice that this constant is being used in a context
that expects an expression of type u64, but it's currently evaluated
using 32-bit arithmetic.

Addresses-Coverity: 1458369 ("Unintentional integer overflow")
Fixes: 0560ad576268 ("i2c: altera: Add Altera I2C Controller driver")
Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
---
 drivers/i2c/busses/i2c-altera.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/i2c/busses/i2c-altera.c b/drivers/i2c/busses/i2c-altera.c
index 5255d3755411..526f453f0ff7 100644
--- a/drivers/i2c/busses/i2c-altera.c
+++ b/drivers/i2c/busses/i2c-altera.c
@@ -171,7 +171,8 @@ 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(div_u64(300ULL * clk_mhz, 1000),
+	       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] 3+ messages in thread

* Re: [PATCH] i2c: altera: Use 64-bit arithmetic instead of 32-bit
  2020-02-10 19:26 [PATCH] i2c: altera: Use 64-bit arithmetic instead of 32-bit Gustavo A. R. Silva
@ 2020-02-10 20:42 ` Thor Thayer
  2020-02-11 11:44 ` David Laight
  1 sibling, 0 replies; 3+ messages in thread
From: Thor Thayer @ 2020-02-10 20:42 UTC (permalink / raw)
  To: Gustavo A. R. Silva; +Cc: linux-i2c, linux-kernel

On 2/10/20 1:26 PM, Gustavo A. R. Silva wrote:
> Add suffix ULL to constant 300 in order to avoid a potential integer
> overflow and give the compiler complete information about the proper
> arithmetic to use. Notice that this constant is being used in a context
> that expects an expression of type u64, but it's currently evaluated
> using 32-bit arithmetic.
> 
> Addresses-Coverity: 1458369 ("Unintentional integer overflow")
> Fixes: 0560ad576268 ("i2c: altera: Add Altera I2C Controller driver")
> Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
> ---
>   drivers/i2c/busses/i2c-altera.c | 3 ++-
>   1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/i2c/busses/i2c-altera.c b/drivers/i2c/busses/i2c-altera.c
> index 5255d3755411..526f453f0ff7 100644
> --- a/drivers/i2c/busses/i2c-altera.c
> +++ b/drivers/i2c/busses/i2c-altera.c
> @@ -171,7 +171,8 @@ 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(div_u64(300ULL * clk_mhz, 1000),
> +	       idev->base + ALTR_I2C_SDA_HOLD);
>   
>   	/* Mask all master interrupt bits */
>   	altr_i2c_int_enable(idev, ALTR_I2C_ALL_IRQ, false);
> 
Thank you.

Reviewed-by: Thor Thayer <thor.thayer@linux.intel.com>

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

* RE: [PATCH] i2c: altera: Use 64-bit arithmetic instead of 32-bit
  2020-02-10 19:26 [PATCH] i2c: altera: Use 64-bit arithmetic instead of 32-bit Gustavo A. R. Silva
  2020-02-10 20:42 ` Thor Thayer
@ 2020-02-11 11:44 ` David Laight
  1 sibling, 0 replies; 3+ messages in thread
From: David Laight @ 2020-02-11 11:44 UTC (permalink / raw)
  To: 'Gustavo A. R. Silva', Thor Thayer; +Cc: linux-i2c, linux-kernel

From: Gustavo A. R. Silva
> Sent: 10 February 2020 19:27
> 
> Add suffix ULL to constant 300 in order to avoid a potential integer
> overflow and give the compiler complete information about the proper
> arithmetic to use. Notice that this constant is being used in a context
> that expects an expression of type u64, but it's currently evaluated
> using 32-bit arithmetic.
> 
> Addresses-Coverity: 1458369 ("Unintentional integer overflow")
> Fixes: 0560ad576268 ("i2c: altera: Add Altera I2C Controller driver")
> Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
> ---
>  drivers/i2c/busses/i2c-altera.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/i2c/busses/i2c-altera.c b/drivers/i2c/busses/i2c-altera.c
> index 5255d3755411..526f453f0ff7 100644
> --- a/drivers/i2c/busses/i2c-altera.c
> +++ b/drivers/i2c/busses/i2c-altera.c
> @@ -171,7 +171,8 @@ 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(div_u64(300ULL * clk_mhz, 1000),
> +	       idev->base + ALTR_I2C_SDA_HOLD);

Why not factor out the 100?
It may then be you can do 32bit arithmetic (3 * clk_mhz / 10).
If clk_mhz is MHz (not mHz) then the sum will never wrap 32 bits.

	David

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


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

end of thread, other threads:[~2020-02-11 11:45 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-10 19:26 [PATCH] i2c: altera: Use 64-bit arithmetic instead of 32-bit Gustavo A. R. Silva
2020-02-10 20:42 ` Thor Thayer
2020-02-11 11:44 ` David Laight

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