[PATCH 01/11] mtd: bind the block device without leaking on failure
Daniel Golle
daniel at makrotopia.org
Mon Sep 28 17:04:44 PDT 2026
add_mtd_device() allocates a slot to hold the mtd_info pointer that
mtd_bind() keeps, but drops the return value. mtd_bind() only logs and
returns on failure, so the slot leaks. A failed kmalloc() is ignored
too, leaving the master without a block device and no hint why its
partitions are unreachable.
Move the bind into a helper that frees the slot when mtd_bind() fails
and warns with the errno in both cases.
Fixes: e8a7453824b7 ("mtd: bind an mtd_blk device for non-NAND MTD masters")
Signed-off-by: Daniel Golle <daniel at makrotopia.org>
---
drivers/mtd/mtdcore.c | 37 ++++++++++++++++++++++++++-----------
1 file changed, 26 insertions(+), 11 deletions(-)
diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c
index e0d0b5ae2e9..3d3a84df0a6 100644
--- a/drivers/mtd/mtdcore.c
+++ b/drivers/mtd/mtdcore.c
@@ -396,6 +396,31 @@ static struct device_type mtd_devtype = {
};
#endif
+static void mtd_blk_bind_master(struct mtd_info *mtd)
+{
+ struct mtd_info **mtdp;
+ int ret;
+
+ /*
+ * mtd_bind() keeps the passed pointer, so it needs storage that lives
+ * as long as the block device; an MTD master is never removed.
+ */
+ mtdp = kmalloc(sizeof(*mtdp), GFP_KERNEL);
+ if (!mtdp) {
+ ret = -ENOMEM;
+ goto warn;
+ }
+
+ *mtdp = mtd;
+ ret = mtd_bind(mtd->dev, mtdp);
+ if (!ret)
+ return;
+
+ kfree(mtdp);
+warn:
+ pr_warn("mtd: %s: cannot bind a block device: %d\n", mtd->name, ret);
+}
+
/**
* add_mtd_device - register an MTD device
* @mtd: pointer to new MTD device info structure
@@ -504,18 +529,8 @@ int add_mtd_device(struct mtd_info *mtd)
* NAND is excluded: it needs UBI, which brings its own block
* device. Partitions inherit the master's block device via the
* "mtd" partition type, so only masters are bound.
- *
- * mtd_bind() keeps a pointer to the passed mtd_info pointer, so
- * it needs storage that lives as long as the block device; the
- * MTD master is never removed in practice, so a small heap slot
- * is fine.
*/
- struct mtd_info **mtdp = kmalloc(sizeof(*mtdp), GFP_KERNEL);
-
- if (mtdp) {
- *mtdp = mtd;
- mtd_bind(mtd->dev, mtdp);
- }
+ mtd_blk_bind_master(mtd);
}
/* We _know_ we aren't being removed, because
--
2.55.0
More information about the openwrt-devel
mailing list