* [PATCH] spi: spacemit: avoid 32-bit integer overflow warning
@ 2026-05-05 17:57 Arnd Bergmann
2026-05-05 18:21 ` Alex Elder
0 siblings, 1 reply; 2+ messages in thread
From: Arnd Bergmann @ 2026-05-05 17:57 UTC (permalink / raw)
To: Mark Brown, Yixun Lan, Alex Elder, Guodong Xu
Cc: Arnd Bergmann, linux-spi, linux-riscv, spacemit, linux-kernel
From: Arnd Bergmann <arnd@arndb.de>
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")
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
----
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(-)
diff --git a/drivers/spi/spi-spacemit-k1.c b/drivers/spi/spi-spacemit-k1.c
index 99db429db0b2..215fe66d27b4 100644
--- a/drivers/spi/spi-spacemit-k1.c
+++ b/drivers/spi/spi-spacemit-k1.c
@@ -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);
/*
--
2.39.5
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] spi: spacemit: avoid 32-bit integer overflow warning
2026-05-05 17:57 [PATCH] spi: spacemit: avoid 32-bit integer overflow warning Arnd Bergmann
@ 2026-05-05 18:21 ` Alex Elder
0 siblings, 0 replies; 2+ messages in thread
From: Alex Elder @ 2026-05-05 18:21 UTC (permalink / raw)
To: Arnd Bergmann, Mark Brown, Yixun Lan, Guodong Xu
Cc: Arnd Bergmann, linux-spi, linux-riscv, spacemit, linux-kernel
On 5/5/26 12:57 PM, Arnd Bergmann wrote:
> From: Arnd Bergmann <arnd@arndb.de>
>
> 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")
> Signed-off-by: Arnd Bergmann <arnd@arndb.de>
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
> ----
> 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(-)
>
> diff --git a/drivers/spi/spi-spacemit-k1.c b/drivers/spi/spi-spacemit-k1.c
> index 99db429db0b2..215fe66d27b4 100644
> --- a/drivers/spi/spi-spacemit-k1.c
> +++ b/drivers/spi/spi-spacemit-k1.c
> @@ -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);
>
> /*
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-05-05 18:21 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-05 17:57 [PATCH] spi: spacemit: avoid 32-bit integer overflow warning Arnd Bergmann
2026-05-05 18:21 ` Alex Elder
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox