[PATCH v4 13/58] mtd: rawnand: mxic: retain open-handle state on removal
From: James Hilliard
Date: Fri Nov 13 2026 - 13:24:32 EST
A retained MTD handle must not access controller storage freed by
unbind. Disconnect and clean up NAND operations before disabling clocks.
Retain the copied layout and controller storage until the final MTD
reference.
Release scanned NAND metadata on registration failure as well, using
retained ownership once publication has been attempted.
Fixes: 738b0ca55f4f ("mtd: rawnand: Add Macronix raw NAND controller driver")
Cc: Mason Yang <masonccyang@xxxxxxxxxxx>
Signed-off-by: James Hilliard <james.hilliard1@xxxxxxxxx>
---
drivers/mtd/nand/raw/mxic_nand.c | 21 ++++++++++++++++-----
1 file changed, 16 insertions(+), 5 deletions(-)
diff --git a/drivers/mtd/nand/raw/mxic_nand.c b/drivers/mtd/nand/raw/mxic_nand.c
index ce9fe27fb4ac..9b666a2c0a8d 100644
--- a/drivers/mtd/nand/raw/mxic_nand.c
+++ b/drivers/mtd/nand/raw/mxic_nand.c
@@ -15,6 +15,7 @@
#include <linux/mtd/nand-ecc-sw-hamming.h>
#include <linux/mtd/rawnand.h>
#include <linux/platform_device.h>
+#include <linux/slab.h>
#include "internals.h"
@@ -492,10 +493,12 @@ static int mxic_nfc_probe(struct platform_device *pdev)
int err;
int irq;
- nfc = devm_kzalloc(&pdev->dev, sizeof(struct mxic_nand_ctlr),
- GFP_KERNEL);
+ nfc = kzalloc_obj(*nfc);
if (!nfc)
return -ENOMEM;
+ err = devm_nand_controller_init(&pdev->dev, &nfc->controller, nfc);
+ if (err)
+ return err;
nfc->ps_clk = devm_clk_get(&pdev->dev, "ps");
if (IS_ERR(nfc->ps_clk))
@@ -523,7 +526,6 @@ static int mxic_nfc_probe(struct platform_device *pdev)
nand_chip->priv = nfc;
nfc->dev = &pdev->dev;
nfc->controller.ops = &mxic_nand_controller_ops;
- nand_controller_init(&nfc->controller);
nand_chip->controller = &nfc->controller;
irq = platform_get_irq(pdev, 0);
@@ -541,9 +543,17 @@ static int mxic_nfc_probe(struct platform_device *pdev)
if (err)
goto fail;
+ err = nand_set_lifetime(nand_chip, NULL);
+ if (err) {
+ nand_cleanup(nand_chip);
+ goto fail;
+ }
err = mtd_device_register(mtd, NULL, 0);
- if (err)
+ if (err) {
+ nand_disconnect(nand_chip);
+ mtd_put_master(mtd);
goto fail;
+ }
platform_set_drvdata(pdev, nfc);
return 0;
@@ -561,9 +571,10 @@ static void mxic_nfc_remove(struct platform_device *pdev)
ret = mtd_device_unregister(nand_to_mtd(chip));
WARN_ON(ret);
- nand_cleanup(chip);
+ nand_disconnect(chip);
mxic_nfc_clk_disable(nfc);
+ mtd_put_master(nand_to_mtd(chip));
}
static const struct of_device_id mxic_nfc_of_ids[] = {
--
2.53.0