Skip to contentSkip to conversation

spi: mt65xx: avoid division by zero when speed_hz is zero

PATCH In review Ilia Snezhkov 2 participants 3 messages Nov 26 – Nov 27

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

PATCH 1/1 spi: mt65xx: avoid division by zero when speed_hz is zero

spi-mt65xx does not set host->max_speed_hz, so if a slave device omits spi-max-frequency in DT and a transfer leaves speed_hz unset, __spi_validate() passes speed_hz == 0 to mtk_spi_prepare_transfer().

Because 0 < mdata->spi_clk_hz / 2 is true, DIV_ROUND_UP(mdata->spi_clk_hz, speed_hz) divides by zero (undefined behavior; on arm64 udiv returns 0, which then underflows sck_time - 1 to 0xffff).

Guard the division on speed_hz != 0 so zero speed falls back to div = 1.

Found and drafted with an AI coding assistant.

Fixes a568231f4632 ("spi: mediatek: Add spi bus for Mediatek MT8173")
---
 drivers/spi/spi-mt65xx.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/spi/spi-mt65xx.c +1 -1
index 8b4deef..2baea0c 100644
@@ -509,7 +509,7 @@ static void mtk_spi_prepare_transfer(struct spi_controller *host,
 	u32 div, sck_time, reg_val;
 	struct mtk_spi *mdata = spi_controller_get_devdata(host);
 
-	if (speed_hz < mdata->spi_clk_hz / 2)
+	if (speed_hz && speed_hz < mdata->spi_clk_hz / 2)
 		div = DIV_ROUND_UP(mdata->spi_clk_hz, speed_hz);
 	else
 		div = 1;
Signature · 1 line
2.54.0 (Apple Git-157)
#1 lore raw
Quoted 4 lines · On Thursday, November 26th, 2026 at 2:33 AM, Ilia Snezhkov via B4 Relay…

From: Ilia Snezhkov <[email protected]>

spi-mt65xx does not set host->max_speed_hz, so if a slave device omits spi-max-frequency in DT and a transfer leaves speed_hz unset,

It's better to set host->max_speed_hz instead. There's another division by zero in the mtk_spi_transfer_wait() for max_speed_hz.

Quoted 9 lines

__spi_validate() passes speed_hz == 0 to mtk_spi_prepare_transfer().

Because 0 < mdata->spi_clk_hz / 2 is true, DIV_ROUND_UP(mdata->spi_clk_hz, speed_hz) divides by zero (undefined behavior; on arm64 udiv returns 0, which then underflows sck_time - 1 to 0xffff).

Guard the division on speed_hz != 0 so zero speed falls back to div = 1.

Found and drafted with an AI coding assistant.

This note should go below ---

Best regards,
Roman

Quoted 26 lines
Fixes a568231f4632 ("spi: mediatek: Add spi bus for Mediatek MT8173")
---
 drivers/spi/spi-mt65xx.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/spi/spi-mt65xx.c +1 -2
index 8b4deef..2baea0c 100644
@@ -509,7 +509,7 @@ static void mtk_spi_prepare_transfer(struct spi_controller *host,
 	u32 div, sck_time, reg_val;
 	struct mtk_spi *mdata = spi_controller_get_devdata(host);
 
-	if (speed_hz < mdata->spi_clk_hz / 2)
+	if (speed_hz && speed_hz < mdata->spi_clk_hz / 2)
 		div = DIV_ROUND_UP(mdata->spi_clk_hz, speed_hz);
 	else
 		div = 1;
--

2.54.0 (Apple Git-157)

#2 lore raw

Hi Roman,

Good catch — I missed the second division by mem->spi->max_speed_hz in mtk_spi_transfer_wait(). Setting host->max_speed_hz = mdata->spi_clk_hz / 2 in mtk_spi_probe() lets the SPI core populate spi->max_speed_hz, xfer->speed_hz, and op->max_freq across all paths cleanly.

I will respin v2 with that approach and move the AI note below ---.

Best regards,
Ilia