[PATCH] spi: spacemit: add u64 cast to NSEC_PER_SEC to avoid 32-bit overflow

Guodong Xu posted 1 patch 1 month, 1 week ago
drivers/spi/spi-spacemit-k1.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] spi: spacemit: add u64 cast to NSEC_PER_SEC to avoid 32-bit overflow
Posted by Guodong Xu 1 month, 1 week ago
NSEC_PER_SEC expands to the long constant 1000000000L, so NSEC_PER_SEC *
BITS_PER_BYTE (8 * 10^9) overflows on 32-bit-long architectures
before the result reaches the u64 nsec_per_word.

Promote the multiplication to u64 by casting the first operand, which is
NSEC_PER_SEC.

Fixes: efcd8b9d1111 ("spi: spacemit: introduce SpacemiT K1 SPI controller driver")
Suggested-by: Alex Elder <elder@riscstar.com>
Reported-by: kernel test robot <lkp@intel.com>
Closes: https://lore.kernel.org/oe-kbuild-all/202605050437.RS6mmV2b-lkp@intel.com/
Closes: https://lore.kernel.org/oe-kbuild-all/202605050317.Tf9j487w-lkp@intel.com/
Signed-off-by: Guodong Xu <guodong@riscstar.com>
---
Hi, Mark

This is an incremental fix on top of your for-7.2, addressing the
overflow warning.

Verified on BPI-F3 with a GD25Q64E SPI NOR, read/write/erase still work.

Thank you.
Guodong
---
 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 99db429db0b26..215fe66d27b4d 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);
 
 	/*

---
base-commit: efcd8b9d111177d48c841d09beca43b15d5b9e5f
change-id: 20260505-spi-spacemit-k1-fix-overflow-8799dbc2ae97

Best regards,
--  
Guodong Xu <guodong@riscstar.com>
Re: [PATCH] spi: spacemit: add u64 cast to NSEC_PER_SEC to avoid 32-bit overflow
Posted by Mark Brown 1 month, 1 week ago
On Tue, 05 May 2026 09:53:34 -0400, Guodong Xu wrote:
> spi: spacemit: add u64 cast to NSEC_PER_SEC to avoid 32-bit overflow

Applied to

   https://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi.git for-7.2

Thanks!

[1/1] spi: spacemit: add u64 cast to NSEC_PER_SEC to avoid 32-bit overflow
      https://git.kernel.org/broonie/spi/c/00cef7eb08c8

All being well this means that it will be integrated into the linux-next
tree (usually sometime in the next 24 hours) and sent to Linus during
the next merge window (or sooner if it is a bug fix), however if
problems are discovered then the patch may be dropped or reverted.

You may get further e-mails resulting from automated or manual testing
and review of the tree, please engage with people reporting problems and
send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they
should be sent as incremental updates against current git, existing
patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying
to this mail.

Thanks,
Mark