[14/58] mtd: rawnand: intel: retain open-handle state on removal

Message ID 20261107-submit-rawnand-retained-handle-lifetime-v1-v1-14-0181ef0cc4c8@gmail.com (mailing list archive)
State New
Headers
Series mtd: rawnand: retain chip and controller state across removal |

Commit Message

James Hilliard Nov. 7, 2026, 10:16 a.m. UTC
Disconnect NAND operations after unregistering MTD and before disabling
the controller or releasing DMA channels. Retain the embedded chip,
controller lock and NAND metadata until the last MTD reference is
released, including references obtained during a partially successful
probe.

Fixes: 0b1039f016e8 ("mtd: rawnand: Add NAND controller support on Intel LGM SoC")
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
 drivers/mtd/nand/raw/intel-nand-controller.c | 22 ++++++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)
  

Comments

sashiko-bot@kernel.org Nov. 7, 2026, 8:31 p.m. UTC | #1
Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 1 related pre-existing issue it does not
introduce:
- [Medium] mtd: rawnand: intel: missing hardware disable in
  ebu_nand_probe() [1]
--

commit 42115b2b815dc0a43fa249f1ae95d7eeefbb02a5
Author: James Hilliard <james.hilliard1@gmail.com>

mtd: rawnand: intel: retain open-handle state on removal

This commit disconnects NAND operations on removal and ties the lifetime of
the Intel EBU NAND controller and chip state to the last MTD reference.

> diff --git a/drivers/mtd/nand/raw/intel-nand-controller.c b/drivers/mtd/nand/raw/intel-nand-controller.c
> index 477a427dac1f0..e4a24e69ebbcf 100644
> --- a/drivers/mtd/nand/raw/intel-nand-controller.c
> +++ b/drivers/mtd/nand/raw/intel-nand-controller.c

[ ... ]

> @@ -692,9 +699,15 @@ static int ebu_nand_probe(struct platform_device *pdev)
>  	if (ret)
>  		goto err_cleanup_dma;
>  
> -	ret = mtd_device_register(mtd, NULL, 0);
> +	ret = nand_set_release(nand, NULL);
>  	if (ret)
>  		goto err_clean_nand;
> +	ret = mtd_device_register(mtd, NULL, 0);
> +	if (ret) {
> +		nand_disconnect(nand);
> +		mtd_put_master(mtd);
> +		goto err_cleanup_dma;
> +	}

[Severity: Medium]
This problem wasn't introduced by this patch, but if probe fails during or
after nand_scan() [1], doesn't jumping to err_clean_nand or
err_cleanup_dma leave EBU_CON_NANDM_EN enabled in EBU_CON?

nand_scan() runs ebu_nand_exec_op() -> ebu_select_chip(), which enables the
controller in EBU_CON, but unlike ebu_nand_remove(), the probe error paths
never call ebu_nand_disable(&ebu_host->chip) before cleaning up DMA and
returning.

[1] https://sashiko.dev/#/bug/linux-34984827-527c-4bf7-baa8-10dffee6916d
  

Patch

diff --git a/drivers/mtd/nand/raw/intel-nand-controller.c b/drivers/mtd/nand/raw/intel-nand-controller.c
index 477a427dac1f..e4a24e69ebbc 100644
--- a/drivers/mtd/nand/raw/intel-nand-controller.c
+++ b/drivers/mtd/nand/raw/intel-nand-controller.c
@@ -573,6 +573,11 @@  static void ebu_dma_cleanup(struct ebu_nand_controller *ebu_host)
 		dma_release_channel(ebu_host->dma_tx);
 }
 
+static void ebu_nand_release(struct nand_controller *controller)
+{
+	kfree(container_of(controller, struct ebu_nand_controller, controller));
+}
+
 static int ebu_nand_probe(struct platform_device *pdev)
 {
 	struct device *dev = &pdev->dev;
@@ -585,12 +590,14 @@  static int ebu_nand_probe(struct platform_device *pdev)
 	int ret;
 	u32 cs;
 
-	ebu_host = devm_kzalloc(dev, sizeof(*ebu_host), GFP_KERNEL);
+	ebu_host = kzalloc_obj(*ebu_host);
 	if (!ebu_host)
 		return -ENOMEM;
+	ret = devm_nand_controller_init(dev, &ebu_host->controller, ebu_nand_release);
+	if (ret)
+		return ret;
 
 	ebu_host->dev = dev;
-	nand_controller_init(&ebu_host->controller);
 
 	ebu_host->ebu = devm_platform_ioremap_resource_byname(pdev, "ebunand");
 	if (IS_ERR(ebu_host->ebu))
@@ -692,9 +699,15 @@  static int ebu_nand_probe(struct platform_device *pdev)
 	if (ret)
 		goto err_cleanup_dma;
 
-	ret = mtd_device_register(mtd, NULL, 0);
+	ret = nand_set_release(nand, NULL);
 	if (ret)
 		goto err_clean_nand;
+	ret = mtd_device_register(mtd, NULL, 0);
+	if (ret) {
+		nand_disconnect(nand);
+		mtd_put_master(mtd);
+		goto err_cleanup_dma;
+	}
 
 	return 0;
 
@@ -715,9 +728,10 @@  static void ebu_nand_remove(struct platform_device *pdev)
 
 	ret = mtd_device_unregister(nand_to_mtd(&ebu_host->chip));
 	WARN_ON(ret);
-	nand_cleanup(&ebu_host->chip);
+	nand_disconnect(&ebu_host->chip);
 	ebu_nand_disable(&ebu_host->chip);
 	ebu_dma_cleanup(ebu_host);
+	mtd_put_master(nand_to_mtd(&ebu_host->chip));
 }
 
 static const struct of_device_id ebu_nand_match[] = {