[PATCH 05/58] mtd: rawnand: share retained chip and controller ownership
James Hilliard
james.hilliard1 at gmail.com
Sat Nov 7 02:16:02 PST 2026
Controller drivers need to keep chip metadata and serialization locks
alive after unregistering MTD, without retaining their hardware
resources. Share the name, firmware-node and parent-device references,
the final NAND cleanup, and the disconnected-device open check.
Provide reference-counted controller storage with an initial
device-managed reference. Register that reference before managed
hardware resources so their teardown finishes before the controller can
be freed. Each retained chip pins the controller independently, allowing
multi-chip controllers and embedded chip structures to use the same
lifetime rules.
Signed-off-by: James Hilliard <james.hilliard1 at gmail.com>
---
drivers/mtd/nand/raw/Makefile | 1 +
drivers/mtd/nand/raw/nand_lifetime.c | 118 +++++++++++++++++++++++++++++++++++
include/linux/mtd/rawnand.h | 12 ++++
3 files changed, 131 insertions(+)
diff --git a/drivers/mtd/nand/raw/Makefile b/drivers/mtd/nand/raw/Makefile
index 619760138d32..7ea275a40fcd 100644
--- a/drivers/mtd/nand/raw/Makefile
+++ b/drivers/mtd/nand/raw/Makefile
@@ -61,6 +61,7 @@ obj-$(CONFIG_MTD_NAND_NUVOTON_MA35) += nuvoton-ma35d1-nand-controller.o
obj-$(CONFIG_MTD_NAND_LOONGSON) += loongson-nand-controller.o
nand-objs := nand_base.o nand_legacy.o nand_bbt.o nand_timings.o nand_ids.o
+nand-objs += nand_lifetime.o
nand-objs += nand_onfi.o
nand-objs += nand_jedec.o
nand-objs += nand_amd.o
diff --git a/drivers/mtd/nand/raw/nand_lifetime.c b/drivers/mtd/nand/raw/nand_lifetime.c
new file mode 100644
index 000000000000..9518c9a4bc63
--- /dev/null
+++ b/drivers/mtd/nand/raw/nand_lifetime.c
@@ -0,0 +1,118 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Storage lifetime for disconnected NAND controllers and chips. */
+
+#include <linux/device.h>
+#include <linux/kref.h>
+#include <linux/module.h>
+#include <linux/mtd/rawnand.h>
+#include <linux/slab.h>
+
+static void nand_controller_release(struct kref *ref)
+{
+ struct nand_controller *controller;
+
+ controller = container_of(ref, struct nand_controller, refcnt);
+ controller->release(controller);
+}
+
+static void nand_controller_put(void *data)
+{
+ struct nand_controller *controller = data;
+
+ kref_put(&controller->refcnt, nand_controller_release);
+}
+
+/**
+ * devm_nand_controller_init - initialize reference-counted controller storage
+ * @dev: controller device
+ * @controller: controller embedded in driver-owned, non-devm storage
+ * @release: callback freeing that storage, without accessing hardware
+ *
+ * Call before acquiring managed hardware resources. Device teardown drops its
+ * storage reference after releasing those resources. Chips prepared with
+ * nand_set_release() hold additional references until their final MTD put.
+ *
+ * Takes ownership of the storage even on failure.
+ * Return: zero on success or a negative error code.
+ */
+int devm_nand_controller_init(struct device *dev, struct nand_controller *controller,
+ void (*release)(struct nand_controller *controller))
+{
+ nand_controller_init(controller);
+ kref_init(&controller->refcnt);
+ controller->release = release;
+
+ return devm_add_action_or_reset(dev, nand_controller_put, controller);
+}
+EXPORT_SYMBOL_GPL(devm_nand_controller_init);
+
+static int nand_mtd_get_device(struct mtd_info *mtd)
+{
+ struct nand_chip *chip = mtd_to_nand(mtd_get_master(mtd));
+
+ guard(mutex)(&chip->lock);
+ return chip->disconnected ? -ENODEV : 0;
+}
+
+static void nand_mtd_release(struct mtd_info *mtd)
+{
+ struct nand_chip *chip = mtd_to_nand(mtd);
+ struct nand_controller *controller = chip->controller;
+ struct device *parent = mtd->dev.parent;
+ struct device_node *np = nand_get_flash_node(chip);
+ const char *name = mtd->name;
+
+ if (!controller->release)
+ controller = NULL;
+ nand_cleanup(chip);
+ of_node_put(np);
+ kfree(name);
+ if (chip->release)
+ chip->release(chip);
+ if (controller)
+ nand_controller_put(controller);
+ put_device(parent);
+}
+
+/**
+ * nand_set_release - retain a scanned chip until its final MTD reference
+ * @chip: successfully scanned, unpublished NAND chip
+ * @release: callback freeing chip storage, or NULL for a controller-embedded chip
+ *
+ * Copies the MTD name and pins the parent device, flash node and, when managed
+ * with devm_nand_controller_init(), controller storage. The original name stays
+ * owned by its original allocator. The caller must set the name or parent before
+ * calling this function, and preserve any other metadata used by MTD callbacks.
+ *
+ * After success, unregister MTD, call nand_disconnect(), and drop the driver's
+ * initial reference with mtd_put_master(). Do the same disconnect/put on MTD
+ * registration failure. nand_cleanup() runs at final release, so controller
+ * detach callbacks must not depend on resources released during removal.
+ *
+ * Return: zero on success or -ENOMEM, leaving ownership unchanged on failure.
+ */
+int nand_set_release(struct nand_chip *chip,
+ void (*release)(struct nand_chip *chip))
+{
+ struct mtd_info *mtd = nand_to_mtd(chip);
+ const char *name = mtd->name;
+ char *copy;
+
+ if (!name)
+ name = dev_name(mtd->dev.parent);
+ copy = kstrdup(name, GFP_KERNEL);
+ if (!copy)
+ return -ENOMEM;
+
+ mtd->name = copy;
+ mtd->_get_device = nand_mtd_get_device;
+ get_device(mtd->dev.parent);
+ of_node_get(nand_get_flash_node(chip));
+ if (chip->controller->release)
+ kref_get(&chip->controller->refcnt);
+ chip->release = release;
+ mtd_set_release(mtd, nand_mtd_release);
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(nand_set_release);
diff --git a/include/linux/mtd/rawnand.h b/include/linux/mtd/rawnand.h
index 90b2169cc745..313e0d88e19a 100644
--- a/include/linux/mtd/rawnand.h
+++ b/include/linux/mtd/rawnand.h
@@ -22,6 +22,7 @@
#include <linux/mutex.h>
#include <linux/of.h>
#include <linux/types.h>
+#include <linux/kref.h>
struct nand_chip;
struct gpio_desc;
@@ -1116,6 +1117,8 @@ struct nand_controller_ops {
* changing the column.
* @supported_op.cont_read: The controller supports sequential cache reads.
* @controller_wp: the controller is in charge of handling the WP pin.
+ * @refcnt: controller storage references for managed lifetimes
+ * @release: free controller storage after the last chip is released
*/
struct nand_controller {
struct mutex lock;
@@ -1125,6 +1128,8 @@ struct nand_controller {
unsigned int cont_read: 1;
} supported_op;
bool controller_wp;
+ struct kref refcnt;
+ void (*release)(struct nand_controller *controller);
};
static inline void nand_controller_init(struct nand_controller *nfc)
@@ -1132,6 +1137,9 @@ static inline void nand_controller_init(struct nand_controller *nfc)
mutex_init(&nfc->lock);
}
+int devm_nand_controller_init(struct device *dev, struct nand_controller *controller,
+ void (*release)(struct nand_controller *controller));
+
/**
* struct nand_legacy - NAND chip legacy fields/hooks
* @IO_ADDR_R: address to read the 8 I/O lines of the flash device
@@ -1285,6 +1293,7 @@ struct nand_secure_region {
* independent devices
* @ecc: The ECC controller structure
* @priv: Chip private data
+ * @release: Free chip storage after the last MTD reference is dropped
*/
struct nand_chip {
struct nand_device base;
@@ -1345,6 +1354,7 @@ struct nand_chip {
struct nand_controller *controller;
struct nand_ecc_ctrl ecc;
void *priv;
+ void (*release)(struct nand_chip *chip);
};
static inline struct nand_chip *mtd_to_nand(struct mtd_info *mtd)
@@ -1598,6 +1608,8 @@ void nand_wait_ready(struct nand_chip *chip);
*/
void nand_cleanup(struct nand_chip *chip);
void nand_disconnect(struct nand_chip *chip);
+int nand_set_release(struct nand_chip *chip,
+ void (*release)(struct nand_chip *chip));
/*
* External helper for controller drivers that have to implement the WAITRDY
--
2.53.0
More information about the linux-mtd
mailing list