[PATCH v4 30/58] mtd: rawnand: pl35x: retain open-handle state on removal
From: James Hilliard
Date: Fri Nov 13 2026 - 16:21:39 EST
Keep chip metadata and controller storage alive until the final MTD
reference is released. Unregister and disconnect each published chip
before releasing the resources used by its hardware operations.
Unwind chip ownership on registration failures as well as removal.
Run NAND cleanup during disconnection; the final core-owned put only
releases retained allocation and layout data.
Cc: Michal Simek <michal.simek@xxxxxxx>
Signed-off-by: James Hilliard <james.hilliard1@xxxxxxxxx>
---
drivers/mtd/nand/raw/pl35x-nand-controller.c | 36 ++++++++++++++++++++--------
1 file changed, 26 insertions(+), 10 deletions(-)
diff --git a/drivers/mtd/nand/raw/pl35x-nand-controller.c b/drivers/mtd/nand/raw/pl35x-nand-controller.c
index 537387014e2a..e5d86d72faf3 100644
--- a/drivers/mtd/nand/raw/pl35x-nand-controller.c
+++ b/drivers/mtd/nand/raw/pl35x-nand-controller.c
@@ -1057,22 +1057,24 @@ static int pl35x_nand_chip_init(struct pl35x_nandc *nfc,
struct mtd_info *mtd;
int cs, ret;
- plnand = devm_kzalloc(nfc->dev, sizeof(*plnand), GFP_KERNEL);
+ plnand = kzalloc_obj(*plnand);
if (!plnand)
return -ENOMEM;
ret = of_property_read_u32(np, "reg", &cs);
if (ret)
- return ret;
+ goto free_chip;
if (cs >= PL35X_NAND_MAX_CS) {
dev_err(nfc->dev, "Wrong CS %d\n", cs);
- return -EINVAL;
+ ret = -EINVAL;
+ goto free_chip;
}
if (test_and_set_bit(cs, &nfc->assigned_cs)) {
dev_err(nfc->dev, "Already assigned CS %d\n", cs);
- return -EINVAL;
+ ret = -EINVAL;
+ goto free_chip;
}
plnand->cs = cs;
@@ -1089,23 +1091,34 @@ static int pl35x_nand_chip_init(struct pl35x_nandc *nfc,
"%s", PL35X_NANDC_DRIVER_NAME);
if (!mtd->name) {
dev_err(nfc->dev, "Failed to allocate mtd->name\n");
- return -ENOMEM;
+ ret = -ENOMEM;
+ goto free_chip;
}
}
ret = nand_scan(chip, 1);
if (ret)
- return ret;
+ goto free_chip;
+ ret = nand_set_lifetime(chip, plnand);
+ if (ret) {
+ nand_cleanup(chip);
+ goto free_chip;
+ }
ret = mtd_device_register(mtd, NULL, 0);
if (ret) {
- nand_cleanup(chip);
+ nand_disconnect(chip);
+ mtd_put_master(mtd);
return ret;
}
list_add_tail(&plnand->node, &nfc->chips);
return ret;
+
+free_chip:
+ kfree(plnand);
+ return ret;
}
static void pl35x_nand_chips_cleanup(struct pl35x_nandc *nfc)
@@ -1118,8 +1131,9 @@ static void pl35x_nand_chips_cleanup(struct pl35x_nandc *nfc)
chip = &plnand->chip;
ret = mtd_device_unregister(nand_to_mtd(chip));
WARN_ON(ret);
- nand_cleanup(chip);
+ nand_disconnect(chip);
list_del(&plnand->node);
+ mtd_put_master(nand_to_mtd(chip));
}
}
@@ -1153,12 +1167,14 @@ static int pl35x_nand_probe(struct platform_device *pdev)
struct pl35x_nandc *nfc;
int ret;
- nfc = devm_kzalloc(&pdev->dev, sizeof(*nfc), GFP_KERNEL);
+ nfc = kzalloc_obj(*nfc);
if (!nfc)
return -ENOMEM;
+ ret = devm_nand_controller_init(&pdev->dev, &nfc->controller, nfc);
+ if (ret)
+ return ret;
nfc->dev = &pdev->dev;
- nand_controller_init(&nfc->controller);
nfc->controller.ops = &pl35x_nandc_ops;
INIT_LIST_HEAD(&nfc->chips);
--
2.53.0