Skip to contentSkip to conversation

i2c: altera: Use 64-bit arithmetic instead of 32-bit

PATCH In review Gustavo A. R. Silva 3 participants 3 messages Feb 10, 2020 – Feb 11, 2020 I2C subsystem host drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 i2c: altera: Use 64-bit arithmetic instead of 32-bit

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")
---
 drivers/i2c/busses/i2c-altera.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/i2c/busses/i2c-altera.c +2 -1
index 5255d3755411..526f453f0ff7 100644
@@ -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);
Signature · 1 line
2.25.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 19 lines · 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")
---
  drivers/i2c/busses/i2c-altera.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
drivers/i2c/busses/i2c-altera.c +0 -0
index 5255d3755411..526f453f0ff7 100644

drivers/i2c/busses/i2c-altera.c static void altr_i2c_init(struct altr_i2c_dev *idev) · line 178

Quoted 9 lines
drivers/i2c/busses/i2c-altera.c +2 -1
@@ -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.

#2 lore raw
In reply to Gustavo A. R. Silva

From: Gustavo A. R. Silva

Quoted 20 lines

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")
---
 drivers/i2c/busses/i2c-altera.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/i2c/busses/i2c-altera.c +0 -0
index 5255d3755411..526f453f0ff7 100644

drivers/i2c/busses/i2c-altera.c static void altr_i2c_init(struct altr_i2c_dev *idev) · line 175

Quoted 6 lines
drivers/i2c/busses/i2c-altera.c +2 -1
@@ -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)