[PATCH] mtdcore: Infrastructure for the device release API

Martin Habets mhabets at solarflare.com
Thu May 18 02:23:11 PDT 2017

This API adds a cleanup callback for the user of an MTD device.
Normally they will have allocated the mtd_info structure, and using
this callback they can free that memory when the last reference is

CONFIG_DEBUG_KOBJECT_RELEASE can be used to detect users that free
the memory too early.

Signed-off-by: Martin Habets <mhabets at solarflare.com>
 drivers/mtd/mtdcore.c   |    7 ++++---
 include/linux/mtd/mtd.h |    1 +
 2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c
index 1517da3ddd7d..c36752206edc 100644
--- a/drivers/mtd/mtdcore.c
+++ b/drivers/mtd/mtdcore.c
@@ -96,9 +96,7 @@ static LIST_HEAD(mtd_notifiers);

 #define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2)

-/* REVISIT once MTD uses the driver model better, whoever allocates
- * the mtd_info will probably want to use the release() hook...
- */
+/* Whoever allocates the mtd_info should populate the release() hook. */
 static void mtd_release(struct device *dev)
        struct mtd_info *mtd = dev_get_drvdata(dev);
@@ -106,6 +104,9 @@ static void mtd_release(struct device *dev)

        /* remove /dev/mtdXro node */
        device_destroy(&mtd_class, index + 1);
+       if (mtd->_release)
+               (mtd->_release)(mtd);

 static ssize_t mtd_type_show(struct device *dev,
diff --git a/include/linux/mtd/mtd.h b/include/linux/mtd/mtd.h
index 79b176eca04a..e61c30ac8fb6 100644
--- a/include/linux/mtd/mtd.h
+++ b/include/linux/mtd/mtd.h
@@ -327,6 +327,7 @@ struct mtd_info {
        int (*_suspend) (struct mtd_info *mtd);
        void (*_resume) (struct mtd_info *mtd);
        void (*_reboot) (struct mtd_info *mtd);
+       void (*_release) (struct mtd_info *mtd);
         * If the driver is something smart, like UBI, it may need to maintain
         * its own reference counting. The below functions are only for driver.

The information contained in this message is confidential and is intended for the addressee(s) only. If you have received this message in error, please notify the sender immediately and delete the message. Unless you are an addressee (or authorized to receive for an addressee), you may not use, copy or disclose to anyone this message or any information contained in this message. The unauthorized use, disclosure, copying or alteration of this message is strictly prohibited.

More information about the linux-mtd mailing list