Skip to contentSkip to conversation

FAILED: patch "[PATCH] spi: jcore: fix divide-by-zero when speed_hz is zero" failed to apply to 5.10-stable tree

PATCH New Sasha Levin 1 participant 3 messages Nov 7 – Nov 10

Subsystems (2)
lore
Share & tools
lore

Sasha Levin

author maintainer
#0 lore raw

The patch below does not apply to the 5.10-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.

To reproduce the conflict and resubmit, you may use the following commands:

git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-5.10.y
git checkout FETCH_HEAD
git cherry-pick -x 7db773075501248893c22b11bedc6745d6d0be45
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '[email protected]' --subject-prefix 'PATCH 5.10.y' 'HEAD^..'

Thanks,
Sasha

------------------ original commit in Linus's tree ------------------

From 7db773075501248893c22b11bedc6745d6d0be45 Mon Sep 17 00:00:00 2001
From: Weibin Liu <[email protected]>
Date: Wed, 14 Oct 2026 22:18:50 +0800
Subject: [PATCH] spi: jcore: fix divide-by-zero when speed_hz is zero

jcore_spi_baudrate() computes hw->clock_freq / 2 / speed without validating speed. The SPI core fills transfer.speed_hz from spi->max_speed_hz when it is zero, and jcore_spi_probe() never sets host->max_speed_hz, so the clamp in __spi_setup() is skipped. A spi_device created via the sysfs new_device interface (or a DT node without spi-max-frequency) therefore has max_speed_hz == 0, and any SPI_IOC_MESSAGE transfer reaches jcore_spi_baudrate() with speed == 0, causing a division by zero and a kernel panic.

Clamp speed to a minimum of 1 Hz before the division so the divisor is never zero. The computed divider then truncates to the register field width and the transfer simply runs at the slowest supported rate.

Verified by inspection of the __spi_setup() clamp and __spi_validate()'s speed_hz handling; the division-by-zero path requires the J-Core controller, which was not available.

Fixes 2cb1b3b3ac0a ("spi: add driver for J-Core SPI controller")
---
 drivers/spi/spi-jcore.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/spi/spi-jcore.c +2 -0
index df82e8adb7c61..347f14f7568a0 100644
@@ -84,6 +84,8 @@ static void jcore_spi_baudrate(struct jcore_spi *hw, int speed)
 {
 	if (speed == hw->speed_hz)
 		return;
+	if (speed <= 0)
+		speed = 1;
 	hw->speed_hz = speed;
 	if (speed >= hw->clock_freq / 2)
 		hw->speed_reg = 0;
Signature · 1 line
2.53.0
author maintainer
#1 lore raw

PATCH 1/2 spi: jcore: Fix trailing statements should be on next line

From: Jay Fang <[email protected]>

[ Upstream commit 45793de7bf89fbd0fd1e2db9dda4e58a9c1395ee ]

Fix checkpatch error:

ERROR: trailing statements should be on next line #85: FILE: spi-jcore.c:85:
+ if (speed == hw->speed_hz) return;

Stable-dep-of: 7db773075501 ("spi: jcore: fix divide-by-zero when speed_hz is zero")

---
 drivers/spi/spi-jcore.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/spi/spi-jcore.c +2 -1
index bba10f030e33d..74c8319c29f17 100644
@@ -82,7 +82,8 @@ static void jcore_spi_chipsel(struct spi_device *spi, bool value)
 
 static void jcore_spi_baudrate(struct jcore_spi *hw, int speed)
 {
-	if (speed == hw->speed_hz) return;
+	if (speed == hw->speed_hz)
+		return;
 	hw->speed_hz = speed;
 	if (speed >= hw->clock_freq / 2)
 		hw->speed_reg = 0;
Signature · 1 line
2.53.0
author maintainer
#2 lore raw

PATCH 2/2 spi: jcore: fix divide-by-zero when speed_hz is zero

From: Weibin Liu <[email protected]>

[ Upstream commit 7db773075501248893c22b11bedc6745d6d0be45 ]

jcore_spi_baudrate() computes hw->clock_freq / 2 / speed without validating speed. The SPI core fills transfer.speed_hz from spi->max_speed_hz when it is zero, and jcore_spi_probe() never sets host->max_speed_hz, so the clamp in __spi_setup() is skipped. A spi_device created via the sysfs new_device interface (or a DT node without spi-max-frequency) therefore has max_speed_hz == 0, and any SPI_IOC_MESSAGE transfer reaches jcore_spi_baudrate() with speed == 0, causing a division by zero and a kernel panic.

Clamp speed to a minimum of 1 Hz before the division so the divisor is never zero. The computed divider then truncates to the register field width and the transfer simply runs at the slowest supported rate.

Verified by inspection of the __spi_setup() clamp and __spi_validate()'s speed_hz handling; the division-by-zero path requires the J-Core controller, which was not available.

Fixes 2cb1b3b3ac0a ("spi: add driver for J-Core SPI controller")
---
 drivers/spi/spi-jcore.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/spi/spi-jcore.c +2 -0
index 74c8319c29f17..e8b8e160d2e34 100644
@@ -84,6 +84,8 @@ static void jcore_spi_baudrate(struct jcore_spi *hw, int speed)
 {
 	if (speed == hw->speed_hz)
 		return;
+	if (speed <= 0)
+		speed = 1;
 	hw->speed_hz = speed;
 	if (speed >= hw->clock_freq / 2)
 		hw->speed_reg = 0;
Signature · 1 line
2.53.0