Skip to contentSkip to conversation

spi: spacemit: avoid 32-bit integer overflow warning

PATCH In review Arnd Bergmann 2 participants 2 messages May 5

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 spi: spacemit: avoid 32-bit integer overflow warning

From: Arnd Bergmann <[email protected]>

Calculating the bit rate in nanoseconds does not fit into a 32-bit 'int' type, as gcc-16 points out:

drivers/spi/spi-spacemit-k1.c: In function 'k1_spi_set_speed':
drivers/spi/spi-spacemit-k1.c:393:38: error: integer overflow in expression of type 'long int' results in '-589934592' [-Werror=overflow]
393 | nsec_per_word = NSEC_PER_SEC * BITS_PER_BYTE * drv_data->bytes;

Cast the value to a 64-bit constant to force the longer type for the calculation.

Fixes efcd8b9d1111 ("spi: spacemit: introduce SpacemiT K1 SPI controller driver")

----
There is probably a way to rework the algorithm to avoid both the 64-bit multiplication and the following division, but I kept this as a minimal change.

---
 drivers/spi/spi-spacemit-k1.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/spi/spi-spacemit-k1.c +1 -1
index 99db429db0b2..215fe66d27b4 100644
@@ -390,7 +390,7 @@ static int k1_spi_set_speed(struct k1_spi_driver_data *drv_data,
 	 *   ticks_per_word = BITS_PER_BYTE * drv_data->bytes;
 	 * We do the divide last for better accuracy.
 	 */
-	nsec_per_word = NSEC_PER_SEC * BITS_PER_BYTE * drv_data->bytes;
+	nsec_per_word = (u64)NSEC_PER_SEC * BITS_PER_BYTE * drv_data->bytes;
 	nsec_per_word = DIV_ROUND_UP_ULL(nsec_per_word, drv_data->rate);
 
 	/*
Signature · 1 line
2.39.5
#1 lore raw
Quoted 9 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

Calculating the bit rate in nanoseconds does not fit into a 32-bit 'int' type, as gcc-16 points out:

drivers/spi/spi-spacemit-k1.c: In function 'k1_spi_set_speed':
drivers/spi/spi-spacemit-k1.c:393:38: error: integer overflow in expression of type 'long int' results in '-589934592' [-Werror=overflow]
393 | nsec_per_word = NSEC_PER_SEC * BITS_PER_BYTE * drv_data->bytes;

Quoted 5 lines

Cast the value to a 64-bit constant to force the longer type for the calculation.

Fixes efcd8b9d1111 ("spi: spacemit: introduce SpacemiT K1 SPI controller driver")

Thanks Arnd. I diagnosed that when I first saw the warning and my fix was identical to yours. I believe Mark already merged the fix.

-Alex

Quoted 21 lines

----
There is probably a way to rework the algorithm to avoid both the 64-bit multiplication and the following division, but I kept this as a minimal change.

---
  drivers/spi/spi-spacemit-k1.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/spi/spi-spacemit-k1.c +1 -1
index 99db429db0b2..215fe66d27b4 100644
@@ -390,7 +390,7 @@ static int k1_spi_set_speed(struct k1_spi_driver_data *drv_data,
  	 *   ticks_per_word = BITS_PER_BYTE * drv_data->bytes;
  	 * We do the divide last for better accuracy.
  	 */
-	nsec_per_word = NSEC_PER_SEC * BITS_PER_BYTE * drv_data->bytes;
+	nsec_per_word = (u64)NSEC_PER_SEC * BITS_PER_BYTE * drv_data->bytes;
  	nsec_per_word = DIV_ROUND_UP_ULL(nsec_per_word, drv_data->rate);
 
  	/*