Skip to contentSkip to conversation

memory: Fix Critical and High issues reported by Sashiko

PATCH In review Lee Jones 2 participants 14 messages Dec 2

Subsystems (7)
lore
Share & tools
lore

Lee Jones

author
#0 lore raw

cover memory: Fix Critical and High issues reported by Sashiko

These issues were reported by Sashiko.

This series addresses all open Critical and High severity reports in the memory controller subsystem, rebased onto mem-ctrl/for-next. It fixes a devm lifetime use-after-free in of_update_property(), timer teardown and re-arm races during suspend/resume, child depopulation ordering, REQ2ACK bitfield and divide-by-zero bugs, AMCR bitfield shifting/masking, __fls(0) undefined behaviour, and stale invalid custom_configs pointers.

Cc Alexandre Belloni <[email protected]>
Cc Alexandre Torgue <[email protected]>
Cc Aneesh V <[email protected]>
Cc Benoit Cousson <[email protected]>
Cc Boris Brezillon <[email protected]>
Cc Christophe Kerello <[email protected]>
Cc Claudiu Beznea <[email protected]>
Cc Grant Likely <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Jean-Jacques Hiblot <[email protected]>
Cc Jonathan Hunter <[email protected]>
Cc Joseph Lo <[email protected]>
Cc Krzysztof Kozlowski <[email protected]>
Cc Maxime Coquelin <[email protected]>
Cc Nicolas Ferre <[email protected]>
Cc Patrice Chotard <[email protected]>
Cc Santosh Shilimkar <[email protected]>
Cc Thierry Reding <[email protected]>

Lee Jones (8):
memory: atmel-ebi: Use non-devm allocations for of_update_property()
memory: tegra210-emc: Stop timers on suspend and prevent training
re-arm
memory: stm32_omm: Depopulate child devices before disabling clocks
memory: stm32_omm: Fix double FIELD_PREP() and clamp for REQ2ACK
memory: stm32_omm: Avoid division by zero in req2ack calculation
memory: stm32_omm: Shift and mask AMCR value in stm32_omm_set_amcr()
memory: emif: Avoid __fls(0) and underflow in of_get_ddr_info()
memory: emif: Clear invalid custom_configs in get_device_details()

 drivers/memory/atmel-ebi.c               | 40 +++++++++++++++++-------
 drivers/memory/emif.c                    |  6 ++--
 drivers/memory/stm32_omm.c               | 29 +++++++++++------
 drivers/memory/tegra/tegra210-emc-core.c | 37 +++++++++++++++++-----
 drivers/memory/tegra/tegra210-emc.h      |  2 ++
 5 files changed, 84 insertions(+), 30 deletions(-)
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
author
#1 lore raw

PATCH 1/8 memory: atmel-ebi: Use non-devm allocations for of_update_property()

atmel_ebi_dev_disable() allocates the struct property and its name and value strings using devm_kzalloc() and devm_kstrdup(), then installs the property into the live device tree via of_update_property(). Because device tree properties outlive the platform device, a subsequent probe failure or device unbind frees those devm-managed allocations while the property remains linked in np->properties, causing a use-after-free on later device tree accesses.

Switch atmel_ebi_dev_disable() to kzalloc_obj() and kstrdup() with proper kfree() unwinding on error, and drop the now-unused ebi parameter.

Fixes 6a4ec4cd0888 ("memory: add Atmel EBI (External Bus Interface) driver")
---
 drivers/memory/atmel-ebi.c | 40 +++++++++++++++++++++++++++-----------
 1 file changed, 29 insertions(+), 11 deletions(-)
drivers/memory/atmel-ebi.c +29 -11
index 33d7007d9d0c..89a5b5e8a334 100644
@@ -18,6 +18,7 @@
 #include <linux/platform_device.h>
 #include <linux/property.h>
 #include <linux/regmap.h>
+#include <linux/slab.h>
 #include <soc/at91/atmel-sfr.h>
 
 #define AT91_EBI_NUM_CS		8
@@ -493,26 +494,43 @@ static const struct of_device_id atmel_ebi_id_table[] = {
 	{ /* sentinel */ }
 };
 
-static int atmel_ebi_dev_disable(struct atmel_ebi *ebi, struct device_node *np)
+static int atmel_ebi_dev_disable(struct device_node *np)
 {
-	struct device *dev = ebi->dev;
 	struct property *newprop;
+	int ret;
 
-	newprop = devm_kzalloc(dev, sizeof(*newprop), GFP_KERNEL);
+	newprop = kzalloc_obj(*newprop);
 	if (!newprop)
 		return -ENOMEM;
 
-	newprop->name = devm_kstrdup(dev, "status", GFP_KERNEL);
-	if (!newprop->name)
-		return -ENOMEM;
+	newprop->name = kstrdup("status", GFP_KERNEL);
+	if (!newprop->name) {
+		ret = -ENOMEM;
+		goto err_free_prop;
+	}
 
-	newprop->value = devm_kstrdup(dev, "disabled", GFP_KERNEL);
-	if (!newprop->value)
-		return -ENOMEM;
+	newprop->value = kstrdup("disabled", GFP_KERNEL);
+	if (!newprop->value) {
+		ret = -ENOMEM;
+		goto err_free_name;
+	}
 
 	newprop->length = sizeof("disabled");
 
-	return of_update_property(np, newprop);
+	ret = of_update_property(np, newprop);
+	if (ret)
+		goto err_free_value;
+
+	return 0;
+
+err_free_value:
+	kfree(newprop->value);
+err_free_name:
+	kfree(newprop->name);
+err_free_prop:
+	kfree(newprop);
+
+	return ret;
 }
 
 static int atmel_ebi_probe(struct platform_device *pdev)
@@ -603,7 +621,7 @@ static int atmel_ebi_probe(struct platform_device *pdev)
 			dev_err(dev, "failed to configure EBI bus for %pOF, disabling the device",
 				child);
 
-			ret = atmel_ebi_dev_disable(ebi, child);
+			ret = atmel_ebi_dev_disable(child);
 			if (ret)
 				goto err_disable_clk;
 		}
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
author
#2 lore raw

PATCH 2/8 memory: tegra210-emc: Stop timers on suspend and prevent training re-arm

In reply to Lee Jones

tegra210_emc_train() unconditionally calls mod_timer(&emc->training, ...) after releasing emc->lock. When tegra210_emc_training_stop() deletes the timer under emc->lock while tegra210_emc_train() is already running on another CPU, the callback can race with the stop, execute periodic_compensation() on an untrained rate, and re-arm itself.

In addition, tegra210_emc_suspend() detaches the EMC clock without synchronously stopping the refresh polling timer or the periodic training timer, allowing timer callbacks to touch EMC hardware or re-enable training after suspend.

Track whether periodic training is enabled under emc->lock so tegra210_emc_train() only runs and re-arms while training is active. Synchronously stop refresh polling and periodic training in tegra210_emc_suspend() before detaching the EMC clock, and restart periodic training (if required by the resumed timing and not already restarted by clk_set_rate()) and refresh polling in tegra210_emc_resume().

Fixes 10de21148f7d ("memory: tegra: Add EMC scaling support code for Tegra210")
---
 drivers/memory/tegra/tegra210-emc-core.c | 37 +++++++++++++++++++-----
 drivers/memory/tegra/tegra210-emc.h      |  2 ++
 2 files changed, 31 insertions(+), 8 deletions(-)
drivers/memory/tegra/tegra210-emc-core.c +29 -8
index 044ec6f1bc32..06aba71d8fb6 100644
@@ -560,28 +560,29 @@ static void tegra210_emc_train(struct timer_list *timer)
 	struct tegra210_emc *emc = timer_container_of(emc, timer, training);
 	unsigned long flags;
 
-	if (!emc->last)
-		return;
-
 	spin_lock_irqsave(&emc->lock, flags);
 
-	if (emc->sequence->periodic_compensation)
-		emc->sequence->periodic_compensation(emc);
+	if (emc->training_enabled && emc->last) {
+		if (emc->sequence->periodic_compensation)
+			emc->sequence->periodic_compensation(emc);
+
+		mod_timer(&emc->training,
+			  jiffies + msecs_to_jiffies(emc->training_interval));
+	}
 
 	spin_unlock_irqrestore(&emc->lock, flags);
-
-	mod_timer(&emc->training,
-		  jiffies + msecs_to_jiffies(emc->training_interval));
 }
 
 static void tegra210_emc_training_start(struct tegra210_emc *emc)
 {
+	emc->training_enabled = true;
 	mod_timer(&emc->training,
 		  jiffies + msecs_to_jiffies(emc->training_interval));
 }
 
 static void tegra210_emc_training_stop(struct tegra210_emc *emc)
 {
+	emc->training_enabled = false;
 	timer_delete(&emc->training);
 }
 
@@ -2001,6 +2002,7 @@ static void tegra210_emc_remove(struct platform_device *pdev)
 static int __maybe_unused tegra210_emc_suspend(struct device *dev)
 {
 	struct tegra210_emc *emc = dev_get_drvdata(dev);
+	unsigned long flags;
 	int err;
 
 	err = clk_rate_exclusive_get(emc->clk);
@@ -2009,9 +2011,19 @@ static int __maybe_unused tegra210_emc_suspend(struct device *dev)
 		return err;
 	}
 
+	emc->resume_refresh_poll = atomic_read(&emc->refresh_poll) > 0;
+	if (emc->resume_refresh_poll)
+		tegra210_emc_poll_refresh_stop(emc);
+
 	emc->resume_rate = clk_get_rate(emc->clk);
 
 	clk_set_rate(emc->clk, 204000000);
+
+	spin_lock_irqsave(&emc->lock, flags);
+	tegra210_emc_training_stop(emc);
+	spin_unlock_irqrestore(&emc->lock, flags);
+	timer_delete_sync(&emc->training);
+
 	tegra210_clk_emc_detach(emc->clk);
 
 	dev_dbg(dev, "suspending at %lu Hz\n", clk_get_rate(emc->clk));
@@ -2022,6 +2034,7 @@ static int __maybe_unused tegra210_emc_suspend(struct device *dev)
 static int __maybe_unused tegra210_emc_resume(struct device *dev)
 {
 	struct tegra210_emc *emc = dev_get_drvdata(dev);
+	unsigned long flags;
 	int err;
 
 	err = tegra210_clk_emc_attach(emc->clk, &emc->provider);
@@ -2033,6 +2046,14 @@ static int __maybe_unused tegra210_emc_resume(struct device *dev)
 	clk_set_rate(emc->clk, emc->resume_rate);
 	clk_rate_exclusive_put(emc->clk);
 
+	spin_lock_irqsave(&emc->lock, flags);
+	if (emc->last && emc->last->periodic_training && !emc->training_enabled)
+		tegra210_emc_training_start(emc);
+	spin_unlock_irqrestore(&emc->lock, flags);
+
+	if (emc->resume_refresh_poll)
+		tegra210_emc_poll_refresh_start(emc);
+
 	dev_dbg(dev, "resuming at %lu Hz\n", clk_get_rate(emc->clk));
 
 	return 0;
drivers/memory/tegra/tegra210-emc.h +2 -0
index 8988bcf15290..44379034da8b 100644
@@ -912,6 +912,7 @@ struct tegra210_emc {
 
 	unsigned int training_interval;
 	struct timer_list training;
+	bool training_enabled;
 
 	enum tegra210_emc_refresh refresh;
 	unsigned int refresh_poll_interval;
@@ -923,6 +924,7 @@ struct tegra210_emc {
 	int clkchange_delay;
 
 	unsigned long resume_rate;
+	bool resume_refresh_poll;
 
 	struct {
 		struct dentry *root;
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
author
#3 lore raw

PATCH 3/8 memory: stm32_omm: Depopulate child devices before disabling clocks

In reply to Lee Jones

stm32_omm_probe() registers child OSPI devices via devm_of_platform_populate(), whose devres cleanup runs after stm32_omm_remove() returns. When CR_MUXEN is set, stm32_omm_remove() disables the OSPI child clocks while the child drivers are still bound and may access their hardware during removal.

Populate child devices with of_platform_populate() and explicitly call of_platform_depopulate() on partial population failure and at the start of stm32_omm_remove() before disabling the child clocks.

Fixes 8181d061dcff ("memory: Add STM32 Octo Memory Manager driver")
---
 drivers/memory/stm32_omm.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/memory/stm32_omm.c +4 -1
index 3599683ab04a..5ff63870421f 100644
@@ -368,8 +368,9 @@ static int stm32_omm_probe(struct platform_device *pdev)
 			return ret;
 	}
 
-	ret = devm_of_platform_populate(dev);
+	ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
 	if (ret) {
+		of_platform_depopulate(dev);
 		if (omm->cr & CR_MUXEN)
 			stm32_omm_toggle_child_clock(&pdev->dev, false);
 
@@ -383,6 +384,8 @@ static void stm32_omm_remove(struct platform_device *pdev)
 {
 	struct stm32_omm *omm = platform_get_drvdata(pdev);
 
+	of_platform_depopulate(&pdev->dev);
+
 	if (omm->cr & CR_MUXEN)
 		stm32_omm_toggle_child_clock(&pdev->dev, false);
 }
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
author
#6 lore raw

PATCH 4/8 memory: stm32_omm: Fix double FIELD_PREP() and clamp for REQ2ACK

In reply to Lee Jones

stm32_omm_configure() applies FIELD_PREP(CR_REQ2ACK_MASK, req2ack) twice in succession. The first call shifts the value into bits 23:16 leaving bits 7:0 zero, so the second FIELD_PREP() always masks and writes zero into OMM_CR. In addition, CR_REQ2ACK_MASK is an 8-bit field (maximum value 255), so clamping req2ack to 256 overflows the field mask.

Drop the redundant FIELD_PREP() call and clamp req2ack against FIELD_MAX(CR_REQ2ACK_MASK).

Fixes 8181d061dcff ("memory: Add STM32 Octo Memory Manager driver")
---
 drivers/memory/stm32_omm.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/memory/stm32_omm.c +2 -4
index 5ff63870421f..588e67bb1cc9 100644
@@ -240,12 +240,10 @@ static int stm32_omm_configure(struct device *dev)
 			if (!ret && req2ack) {
 				req2ack = DIV_ROUND_UP(req2ack, NSEC_PER_SEC / clk_rate_max) - 1;
 
-				if (req2ack > 256)
-					req2ack = 256;
+				if (req2ack > FIELD_MAX(CR_REQ2ACK_MASK))
+					req2ack = FIELD_MAX(CR_REQ2ACK_MASK);
 			}
 
-			req2ack = FIELD_PREP(CR_REQ2ACK_MASK, req2ack);
-
 			omm->cr &= ~CR_REQ2ACK_MASK;
 			omm->cr |= FIELD_PREP(CR_REQ2ACK_MASK, req2ack);
 
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
author
#8 lore raw

PATCH 5/8 memory: stm32_omm: Avoid division by zero in req2ack calculation

In reply to Lee Jones

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")
---
 drivers/memory/stm32_omm.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/memory/stm32_omm.c +6 -3
index 588e67bb1cc9..c6f605531239 100644
@@ -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;
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
author
#10 lore raw

PATCH 6/8 memory: stm32_omm: Shift and mask AMCR value in stm32_omm_set_amcr()

In reply to Lee Jones

stm32_omm_set_amcr() passes the unshifted amcr value to regmap_update_bits() and reads back the register without masking against syscon_args[1] before shifting right. In addition, if syscon_args[1] is zero, ffs(syscon_args[1]) - 1 evaluates to -1 and causes an undefined shift.

Reject a zero AMCR mask, shift amcr by ffs(syscon_args[1]) - 1 when updating the register, and mask read_amcr with syscon_args[1] before shifting it right.

Fixes 8181d061dcff ("memory: Add STM32 Octo Memory Manager driver")
---
 drivers/memory/stm32_omm.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/memory/stm32_omm.c +10 -3
index c6f605531239..211ef9bc59b6 100644
@@ -49,7 +49,7 @@ static int stm32_omm_set_amcr(struct device *dev, bool set)
 	struct resource res, res1;
 	unsigned int syscon_args[2];
 	int ret = 0, idx;
-	unsigned int i, amcr, read_amcr;
+	unsigned int i, amcr, read_amcr, shift;
 
 	for (i = 0; i < omm->nb_child; i++) {
 		idx = of_property_match_string(dev->of_node,
@@ -105,14 +105,21 @@ static int stm32_omm_set_amcr(struct device *dev, bool set)
 		return dev_err_probe(dev, PTR_ERR(syscfg_regmap),
 				     "Failed to get st,syscfg-amcr property\n");
 
+	if (!syscon_args[1]) {
+		dev_err(dev, "Invalid AMCR mask\n");
+		return -EINVAL;
+	}
+
+	shift = ffs(syscon_args[1]) - 1;
 	amcr = mm_ospi2_size / SZ_64M;
 
 	if (set)
-		regmap_update_bits(syscfg_regmap, syscon_args[0], syscon_args[1], amcr);
+		regmap_update_bits(syscfg_regmap, syscon_args[0], syscon_args[1],
+				   amcr << shift);
 
 	/* read AMCR and check coherency with memory-map areas defined in DT */
 	regmap_read(syscfg_regmap, syscon_args[0], &read_amcr);
-	read_amcr = read_amcr >> (ffs(syscon_args[1]) - 1);
+	read_amcr = (read_amcr & syscon_args[1]) >> shift;
 
 	if (amcr != read_amcr) {
 		dev_err(dev, "AMCR value not coherent with DT memory-map areas\n");
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
author
#12 lore raw

PATCH 7/8 memory: emif: Avoid __fls(0) and underflow in of_get_ddr_info()

In reply to Lee Jones

of_get_ddr_info() uses x & (x - 1) to test whether the "density" and "io-width" device tree properties are powers of two before calling __fls(). When either property is missing or zero, 0 & -1 evaluates to zero and __fls(0) is called, which has undefined behaviour. Small power-of-two values (density < 64 or io-width < 4) also underflow when subtracting 5 or 1 from the bit index.

Use is_power_of_2() and check the minimum valid values (64 Mb for density and 4 bits for io-width) before calling __fls().

Fixes e6b42eb6a66c ("memory: emif: add device tree support to emif driver")
---
 drivers/memory/emif.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/memory/emif.c +3 -2
index 886c2ca0dc19..cb2c49028d6c 100644
@@ -10,6 +10,7 @@
 #include <linux/cleanup.h>
 #include <linux/err.h>
 #include <linux/kernel.h>
+#include <linux/log2.h>
 #include <linux/reboot.h>
 #include <linux/platform_data/emif_plat.h>
 #include <linux/io.h>
@@ -892,13 +893,13 @@ static void of_get_ddr_info(struct device_node *np_emif,
 	of_property_read_u32(np_ddr, "io-width", &io_width);
 
 	/* Convert from density in Mb to the density encoding in jedc_ddr.h */
-	if (density & (density - 1))
+	if (!is_power_of_2(density) || density < 64)
 		dev_info->density = 0;
 	else
 		dev_info->density = __fls(density) - 5;
 
 	/* Convert from io_width in bits to io_width encoding in jedc_ddr.h */
-	if (io_width & (io_width - 1))
+	if (!is_power_of_2(io_width) || io_width < 4)
 		dev_info->io_width = 0;
 	else
 		dev_info->io_width = __fls(io_width) - 1;
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
author
#13 lore raw

PATCH 8/8 memory: emif: Clear invalid custom_configs in get_device_details()

In reply to Lee Jones

get_device_details() copies the entire platform_data structure, including its custom_configs pointer, into a devm-allocated copy. If is_custom_config_valid() rejects the platform data's custom_configs, the pointer is never cleared and subsequent driver paths continue to dereference the invalid configuration.

Reset pd->custom_configs to NULL before validating and copying cust_cfgs.

Fixes 7ec944538dde ("memory: emif: add basic infrastructure for EMIF driver")
---
 drivers/memory/emif.c | 1 +
 1 file changed, 1 insertion(+)
drivers/memory/emif.c +1 -0
index cb2c49028d6c..6d5a74a0ab8b 100644
@@ -1040,6 +1040,7 @@ static struct emif_data *get_device_details(
 	 * custom_configs is not very critical
 	 */
 	cust_cfgs = pd->custom_configs;
+	pd->custom_configs = NULL;
 	if (cust_cfgs && is_custom_config_valid(cust_cfgs, dev)) {
 		temp = devm_kzalloc(dev, sizeof(*cust_cfgs), GFP_KERNEL);
 		if (temp)
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
maintainer Reviewed-by on v1 3/8
#4 lore raw

Re: [PATCH 3/8] memory: stm32_omm: Depopulate child devices before disabling clocks

In reply to Lee Jones
Quoted 34 lines · Lee Jones

stm32_omm_probe() registers child OSPI devices via devm_of_platform_populate(), whose devres cleanup runs after stm32_omm_remove() returns. When CR_MUXEN is set, stm32_omm_remove() disables the OSPI child clocks while the child drivers are still bound and may access their hardware during removal.

Populate child devices with of_platform_populate() and explicitly call of_platform_depopulate() on partial population failure and at the start of stm32_omm_remove() before disabling the child clocks.

Fixes 8181d061dcff ("memory: Add STM32 Octo Memory Manager driver")
---
 drivers/memory/stm32_omm.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/memory/stm32_omm.c +2 -1
index 3599683ab04a..5ff63870421f 100644
@@ -368,8 +368,9 @@ static int stm32_omm_probe(struct platform_device *pdev)
 			return ret;
 	}
 
-	ret = devm_of_platform_populate(dev);
+	ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
 	if (ret) {
+		of_platform_depopulate(dev);
 		if (omm->cr & CR_MUXEN)
 			stm32_omm_toggle_child_clock(&pdev->dev, false);
 

drivers/memory/stm32_omm.c static void stm32_omm_remove(struct platform_device *pdev) · line 391

Quoted 8 lines
drivers/memory/stm32_omm.c +2 -0
@@ -383,6 +384,8 @@ static void stm32_omm_remove(struct platform_device *pdev)
 {
 	struct stm32_omm *omm = platform_get_drvdata(pdev);
 
+	of_platform_depopulate(&pdev->dev);
+
 	if (omm->cr & CR_MUXEN)
 		stm32_omm_toggle_child_clock(&pdev->dev, false);
 }

Hi Lee

Thanks
Patrice

maintainer Reviewed-by on v1 4/8
#7 lore raw

Re: [PATCH 4/8] memory: stm32_omm: Fix double FIELD_PREP() and clamp for REQ2ACK

In reply to Lee Jones
Quoted 27 lines · Lee Jones

stm32_omm_configure() applies FIELD_PREP(CR_REQ2ACK_MASK, req2ack) twice in succession. The first call shifts the value into bits 23:16 leaving bits 7:0 zero, so the second FIELD_PREP() always masks and writes zero into OMM_CR. In addition, CR_REQ2ACK_MASK is an 8-bit field (maximum value 255), so clamping req2ack to 256 overflows the field mask.

Drop the redundant FIELD_PREP() call and clamp req2ack against FIELD_MAX(CR_REQ2ACK_MASK).

Fixes 8181d061dcff ("memory: Add STM32 Octo Memory Manager driver")
---
 drivers/memory/stm32_omm.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/memory/stm32_omm.c +0 -1
index 5ff63870421f..588e67bb1cc9 100644
@@ -240,12 +240,10 @@ static int stm32_omm_configure(struct device *dev)
 			if (!ret && req2ack) {
 				req2ack = DIV_ROUND_UP(req2ack, NSEC_PER_SEC / clk_rate_max) - 1;
 
-				if (req2ack > 256)

drivers/memory/stm32_omm.c static int stm32_omm_configure(struct device *dev) · line 249

Quoted 10 lines
drivers/memory/stm32_omm.c +2 -3
-					req2ack = 256;
+				if (req2ack > FIELD_MAX(CR_REQ2ACK_MASK))
+					req2ack = FIELD_MAX(CR_REQ2ACK_MASK);
 			}
 
-			req2ack = FIELD_PREP(CR_REQ2ACK_MASK, req2ack);
-
 			omm->cr &= ~CR_REQ2ACK_MASK;
 			omm->cr |= FIELD_PREP(CR_REQ2ACK_MASK, req2ack);
 

Hi Lee

Thanks
Patrice

maintainer Reviewed-by on v1 5/8
#9 lore raw

Re: [PATCH 5/8] memory: stm32_omm: Avoid division by zero in req2ack calculation

In reply to Lee Jones
Quoted 38 lines · Lee Jones

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")
---
 drivers/memory/stm32_omm.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/memory/stm32_omm.c +2 -1
index 588e67bb1cc9..c6f605531239 100644
@@ -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;

drivers/memory/stm32_omm.c static int stm32_omm_configure(struct device *dev) · line 250

Quoted 10 lines
drivers/memory/stm32_omm.c +4 -2
+				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;

Hi Lee

Thanks
Patrice

maintainer Reviewed-by on v1 6/8
#11 lore raw

Re: [PATCH 6/8] memory: stm32_omm: Shift and mask AMCR value in stm32_omm_set_amcr()

In reply to Lee Jones
Quoted 45 lines · Lee Jones

stm32_omm_set_amcr() passes the unshifted amcr value to regmap_update_bits() and reads back the register without masking against syscon_args[1] before shifting right. In addition, if syscon_args[1] is zero, ffs(syscon_args[1]) - 1 evaluates to -1 and causes an undefined shift.

Reject a zero AMCR mask, shift amcr by ffs(syscon_args[1]) - 1 when updating the register, and mask read_amcr with syscon_args[1] before shifting it right.

Fixes 8181d061dcff ("memory: Add STM32 Octo Memory Manager driver")
---
 drivers/memory/stm32_omm.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/memory/stm32_omm.c +7 -2
index c6f605531239..211ef9bc59b6 100644
@@ -49,7 +49,7 @@ static int stm32_omm_set_amcr(struct device *dev, bool set)
 	struct resource res, res1;
 	unsigned int syscon_args[2];
 	int ret = 0, idx;
-	unsigned int i, amcr, read_amcr;
+	unsigned int i, amcr, read_amcr, shift;
 
 	for (i = 0; i < omm->nb_child; i++) {
 		idx = of_property_match_string(dev->of_node,
@@ -105,14 +105,21 @@ static int stm32_omm_set_amcr(struct device *dev, bool set)
 		return dev_err_probe(dev, PTR_ERR(syscfg_regmap),
 				     "Failed to get st,syscfg-amcr property\n");
 
+	if (!syscon_args[1]) {
+		dev_err(dev, "Invalid AMCR mask\n");
+		return -EINVAL;
+	}
+
+	shift = ffs(syscon_args[1]) - 1;
 	amcr = mm_ospi2_size / SZ_64M;
 
 	if (set)
-		regmap_update_bits(syscfg_regmap, syscon_args[0], syscon_args[1], amcr);

drivers/memory/stm32_omm.c static int stm32_omm_set_amcr(struct device *dev, bool set) · line 125

Quoted 10 lines
drivers/memory/stm32_omm.c +3 -1
+		regmap_update_bits(syscfg_regmap, syscon_args[0], syscon_args[1],
+				   amcr << shift);
 
 	/* read AMCR and check coherency with memory-map areas defined in DT */
 	regmap_read(syscfg_regmap, syscon_args[0], &read_amcr);
-	read_amcr = read_amcr >> (ffs(syscon_args[1]) - 1);
+	read_amcr = (read_amcr & syscon_args[1]) >> shift;
 
 	if (amcr != read_amcr) {
 		dev_err(dev, "AMCR value not coherent with DT memory-map areas\n");

Hi Lee

Thanks
Patrice

author
#5 lore raw

Re: [PATCH 3/8] memory: stm32_omm: Depopulate child devices before disabling clocks

In reply to Patrice CHOTARD
Quoted 40 lines · Patrice CHOTARD
Quoted 37 lines · Lee Jones

stm32_omm_probe() registers child OSPI devices via devm_of_platform_populate(), whose devres cleanup runs after stm32_omm_remove() returns. When CR_MUXEN is set, stm32_omm_remove() disables the OSPI child clocks while the child drivers are still bound and may access their hardware during removal.

Populate child devices with of_platform_populate() and explicitly call of_platform_depopulate() on partial population failure and at the start of stm32_omm_remove() before disabling the child clocks.

Fixes 8181d061dcff ("memory: Add STM32 Octo Memory Manager driver")
---
 drivers/memory/stm32_omm.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/memory/stm32_omm.c +2 -1
index 3599683ab04a..5ff63870421f 100644
@@ -368,8 +368,9 @@ static int stm32_omm_probe(struct platform_device *pdev)
 			return ret;
 	}
 
-	ret = devm_of_platform_populate(dev);
+	ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
 	if (ret) {
+		of_platform_depopulate(dev);
 		if (omm->cr & CR_MUXEN)
 			stm32_omm_toggle_child_clock(&pdev->dev, false);
 
@@ -383,6 +384,8 @@ static void stm32_omm_remove(struct platform_device *pdev)
 {
 	struct stm32_omm *omm = platform_get_drvdata(pdev);
 
Quoted 7 lines

drivers/memory/stm32_omm.c static void stm32_omm_remove(struct platform_device *pdev) · line 391

Quoted 5 lines
drivers/memory/stm32_omm.c +2 -0
+	of_platform_depopulate(&pdev->dev);
+
 	if (omm->cr & CR_MUXEN)
 		stm32_omm_toggle_child_clock(&pdev->dev, false);
 }

Hi Lee

Hey bud. I hope that you are well.

Quoted 1 line

Thanks for these.

Signature · 1 line
Lee Jones