Linux kernel -stable discussions
 help / color / mirror / Atom feed
* [PATCH] memory: emif: Unlink controllers before releasing their data
@ 2026-12-01 16:50 Yige Jiang
  0 siblings, 0 replies; only message in thread
From: Yige Jiang @ 2026-12-01 16:50 UTC (permalink / raw)
  To: Krzysztof Kozlowski; +Cc: Yige Jiang, Santosh Shilimkar, linux-kernel, stable

emif_probe() adds a controller to device_list before probe can fail.
Neither a subsequent error nor driver removal deletes the entry. The
list then points into released device-managed storage. The pointer to
the first controller, emif1, also outlives that controller on removal.

Link the controller after its registers are ready and before requesting
its interrupt. Unlink it if the IRQ request fails and when the driver
is removed. Use the same spinlock as the interrupt-side list walkers.

Serialize probe, unlink and register-cache dumps so that another
controller cannot use emif1 during teardown. When emif1 goes away,
stop its peers from looking up their cache through it. Create the
debugfs files only after registration succeeds.

Fixes: 7ec944538dde ("memory: emif: add basic infrastructure for EMIF driver")
Cc: stable@vger.kernel.org
Signed-off-by: Yige Jiang <yigejiang86@gmail.com>
---
Compile-tested on arm64 with GCC 14.2 and W=1 against v7.4-rc5.
No hardware or runtime test was performed for this revision.
Related DDR-node cleanup in the same probe function:
https://lore.kernel.org/r/confirmed-20261105-11-5d830ffabcd8@gmail.com/

 drivers/memory/emif.c | 48 +++++++++++++++++++++++++++++++++----------
 1 file changed, 37 insertions(+), 11 deletions(-)

diff --git a/drivers/memory/emif.c b/drivers/memory/emif.c
index b5d41ebb19f3..a829d7532fe6 100644
--- a/drivers/memory/emif.c
+++ b/drivers/memory/emif.c
@@ -23,6 +23,7 @@
 #include <linux/module.h>
 #include <linux/list.h>
 #include <linux/spinlock.h>
+#include <linux/mutex.h>
 #include <linux/pm.h>
 
 #include "emif.h"
@@ -68,6 +69,8 @@ struct emif_data {
 	struct device_node		*np_ddr;
 };
 
+/* Protect emif1 and the instances that share its register cache. */
+static DEFINE_MUTEX(emif_instance_lock);
 static struct emif_data *emif1;
 static DEFINE_SPINLOCK(emif_lock);
 static LIST_HEAD(device_list);
@@ -114,6 +117,7 @@ static int emif_regdump_show(struct seq_file *s, void *unused)
 	struct emif_regs	**regs_cache;
 	int			i;
 
+	guard(mutex)(&emif_instance_lock);
 	if (emif->duplicate)
 		regs_cache = emif1->regs_cache;
 	else
@@ -670,9 +674,26 @@ static void disable_and_clear_all_interrupts(struct emif_data *emif)
 	clear_all_interrupts(emif);
 }
 
-static int setup_interrupts(struct emif_data *emif, u32 irq)
+static void emif_unlink(struct emif_data *emif)
+{
+	struct emif_data *peer;
+
+	lockdep_assert_held(&emif_instance_lock);
+	scoped_guard(spinlock_irqsave, &emif_lock)
+		list_del(&emif->node);
+
+	if (emif1 == emif) {
+		emif1 = NULL;
+		/* A later first instance may have different DDR geometry. */
+		list_for_each_entry(peer, &device_list, node)
+			peer->duplicate = false;
+	}
+}
+
+static int emif_register_instance(struct emif_data *emif, u32 irq)
 {
 	u32		interrupts, type;
+	int ret;
 	void __iomem	*base = emif->base;
 
 	type = emif->plat_data->device_info->type;
@@ -692,13 +713,17 @@ static int setup_interrupts(struct emif_data *emif, u32 irq)
 		writel(interrupts, base + EMIF_LL_OCP_INTERRUPT_ENABLE_SET);
 	}
 
-	/* setup IRQ handlers */
-	return devm_request_threaded_irq(emif->dev, irq,
-				    emif_interrupt_handler,
-				    emif_threaded_isr,
-				    0, dev_name(emif->dev),
-				    emif);
+	/* The IRQ handlers can walk the list as soon as they are registered. */
+	scoped_guard(spinlock_irqsave, &emif_lock)
+		list_add(&emif->node, &device_list);
 
+	ret = devm_request_threaded_irq(emif->dev, irq, emif_interrupt_handler,
+					emif_threaded_isr, 0, dev_name(emif->dev),
+					emif);
+	if (ret)
+		emif_unlink(emif);
+
+	return ret;
 }
 
 static void emif_onetime_settings(struct emif_data *emif)
@@ -1086,6 +1111,7 @@ static int emif_probe(struct platform_device *pdev)
 	struct emif_data	*emif;
 	int			irq, ret;
 
+	guard(mutex)(&emif_instance_lock);
 	if (pdev->dev.of_node)
 		emif = of_get_memory_device_details(pdev->dev.of_node, &pdev->dev);
 	else
@@ -1096,8 +1122,6 @@ static int emif_probe(struct platform_device *pdev)
 		goto error;
 	}
 
-	list_add(&emif->node, &device_list);
-
 	/* Save pointers to each other in emif and device structures */
 	emif->dev = &pdev->dev;
 	platform_set_drvdata(pdev, emif);
@@ -1111,9 +1135,8 @@ static int emif_probe(struct platform_device *pdev)
 		goto error;
 
 	emif_onetime_settings(emif);
-	emif_debugfs_init(emif);
 	disable_and_clear_all_interrupts(emif);
-	ret = setup_interrupts(emif, irq);
+	ret = emif_register_instance(emif, irq);
 	if (ret)
 		goto error;
 
@@ -1128,6 +1151,7 @@ static int emif_probe(struct platform_device *pdev)
 		 */
 	}
 
+	emif_debugfs_init(emif);
 	dev_info(&pdev->dev, "%s: device configured with addr = %p and IRQ%d\n",
 		__func__, emif->base, irq);
 
@@ -1141,6 +1165,8 @@ static void emif_remove(struct platform_device *pdev)
 	struct emif_data *emif = platform_get_drvdata(pdev);
 
 	emif_debugfs_exit(emif);
+	scoped_guard(mutex, &emif_instance_lock)
+		emif_unlink(emif);
 	of_node_put(emif->np_ddr);
 }
 

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-12-01 16:50 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01 16:50 [PATCH] memory: emif: Unlink controllers before releasing their data Yige Jiang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox