[PATCH 5/8] memory: stm32_omm: Avoid division by zero in req2ack calculation
Lee Jones
lee at kernel.org
Wed Dec 2 01:33:52 PST 2026
stm32_omm_configure() converts st,omm-req2ack-ns into clock cycles via
DIV_ROUND_UP(req2ack, NSEC_PER_SEC / clk_rate_max) - 1. If
clk_rate_max exceeds NSEC_PER_SEC (1 GHz), NSEC_PER_SEC / clk_rate_max
evaluates to zero and triggers a division by zero. For lower rates,
truncating the clock period to an integer number of nanoseconds also
loses precision.
Compute the cycle count directly in 64-bit arithmetic using
mul_u64_u64_div_u64_roundup() before subtracting one and clamping to
FIELD_MAX(CR_REQ2ACK_MASK).
Fixes: 8181d061dcff ("memory: Add STM32 Octo Memory Manager driver")
Closes: https://sashiko.dev/#/bug/linux-18fbeb54-8161-4b1a-a1b9-1556a0bf7999
Cc: stable at vger.kernel.org
Signed-off-by: Lee Jones <lee at kernel.org>
---
drivers/memory/stm32_omm.c | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)
diff --git a/drivers/memory/stm32_omm.c b/drivers/memory/stm32_omm.c
index 588e67bb1cc9..c6f605531239 100644
--- a/drivers/memory/stm32_omm.c
+++ b/drivers/memory/stm32_omm.c
@@ -8,7 +8,9 @@
#include <linux/bus/stm32_firewall_device.h>
#include <linux/clk.h>
#include <linux/err.h>
+#include <linux/math64.h>
#include <linux/mfd/syscon.h>
+#include <linux/minmax.h>
#include <linux/module.h>
#include <linux/of_address.h>
#include <linux/of_platform.h>
@@ -238,10 +240,11 @@ static int stm32_omm_configure(struct device *dev)
ret = of_property_read_u32(dev->of_node, "st,omm-req2ack-ns",
&req2ack);
if (!ret && req2ack) {
- req2ack = DIV_ROUND_UP(req2ack, NSEC_PER_SEC / clk_rate_max) - 1;
+ u64 cycles;
- if (req2ack > FIELD_MAX(CR_REQ2ACK_MASK))
- req2ack = FIELD_MAX(CR_REQ2ACK_MASK);
+ cycles = mul_u64_u64_div_u64_roundup(req2ack, clk_rate_max,
+ NSEC_PER_SEC);
+ req2ack = min_t(u64, cycles - 1, FIELD_MAX(CR_REQ2ACK_MASK));
}
omm->cr &= ~CR_REQ2ACK_MASK;
--
2.56.1.911.g883d8bcb20-goog
More information about the linux-arm-kernel
mailing list