mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Lee Jones <lee@kernel.org>
To: lee@kernel.org, Patrice Chotard <patrice.chotard@foss.st.com>,
	Krzysztof Kozlowski <krzk@kernel.org>,
	Maxime Coquelin <mcoquelin.stm32@gmail.com>,
	Alexandre Torgue <alexandre.torgue@foss.st.com>,
	Christophe Kerello <christophe.kerello@foss.st.com>,
	linux-kernel@vger.kernel.org,
	linux-stm32@st-md-mailman.stormreply.com,
	linux-arm-kernel@lists.infradead.org
Cc: stable@vger.kernel.org
Subject: [PATCH 5/8] memory: stm32_omm: Avoid division by zero in req2ack calculation
Date: Wed,  2 Dec 2026 09:33:52 +0000	[thread overview]
Message-ID: <407e9b75952f7c3b59a7ded80a8516da9a58ea2a.1796203994.git.lee@kernel.org> (raw)
In-Reply-To: <cover.1796203994.git.lee@kernel.org>

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@vger.kernel.org
Signed-off-by: Lee Jones <lee@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


  parent reply	other threads:[~2026-12-02  9:35 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02  9:33 [PATCH 0/8] memory: Fix Critical and High issues reported by Sashiko Lee Jones
2026-12-02  9:33 ` [PATCH 1/8] memory: atmel-ebi: Use non-devm allocations for of_update_property() Lee Jones
2026-12-02  9:33 ` [PATCH 2/8] memory: tegra210-emc: Stop timers on suspend and prevent training re-arm Lee Jones
2026-12-02  9:33 ` [PATCH 3/8] memory: stm32_omm: Depopulate child devices before disabling clocks Lee Jones
2026-12-02 13:54   ` Patrice CHOTARD
2026-12-02  9:33 ` [PATCH 4/8] memory: stm32_omm: Fix double FIELD_PREP() and clamp for REQ2ACK Lee Jones
2026-12-02 14:08   ` Patrice CHOTARD
2026-12-02  9:33 ` Lee Jones [this message]
2026-12-02 14:17   ` [PATCH 5/8] memory: stm32_omm: Avoid division by zero in req2ack calculation Patrice CHOTARD
2026-12-02  9:33 ` [PATCH 6/8] memory: stm32_omm: Shift and mask AMCR value in stm32_omm_set_amcr() Lee Jones
2026-12-02 14:33   ` Patrice CHOTARD
2026-12-02  9:33 ` [PATCH 7/8] memory: emif: Avoid __fls(0) and underflow in of_get_ddr_info() Lee Jones
2026-12-02  9:33 ` [PATCH 8/8] memory: emif: Clear invalid custom_configs in get_device_details() Lee Jones

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=407e9b75952f7c3b59a7ded80a8516da9a58ea2a.1796203994.git.lee@kernel.org \
    --to=lee@kernel.org \
    --cc=alexandre.torgue@foss.st.com \
    --cc=christophe.kerello@foss.st.com \
    --cc=krzk@kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-stm32@st-md-mailman.stormreply.com \
    --cc=mcoquelin.stm32@gmail.com \
    --cc=patrice.chotard@foss.st.com \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®