[PATCH v2] mtd: retain the partition node reference across device registration
James Hilliard
james.hilliard1 at gmail.com
Sun Nov 8 14:48:16 PST 2026
mtd_check_of_node() obtains a child node reference while looking up a
partition, stores the node in the MTD device, and drops the reference.
add_mtd_device() only takes its own reference after the helper returns.
A concurrent dynamic DT removal can release the node in that gap.
Return a referenced node from the lookup helper and transfer that reference
to the MTD device. For an already supplied node, acquire the reference in
the same helper. The existing registration unwind and device release paths
remain responsible for putting it.
Fixes: 5ab9bbf6c678 ("mtd: Fix possible refcounting issue when going through partition nodes")
Cc: stable at vger.kernel.org
Signed-off-by: James Hilliard <james.hilliard1 at gmail.com>
---
Changes in v2:
- Correct the Fixes tag to identify the commit that dropped the
partition-node reference before registration.
- Link to v1: https://patch.msgid.link/20261108-submit-mtd-partition-node-reference-v1-v1-1-ad30359569d4@gmail.com
To: Miquel Raynal <miquel.raynal at bootlin.com>
To: Richard Weinberger <richard at nod.at>
To: Vignesh Raghavendra <vigneshr at ti.com>
Cc: linux-mtd at lists.infradead.org
Cc: linux-kernel at vger.kernel.org
---
drivers/mtd/mtdcore.c | 17 +++++++----------
1 file changed, 7 insertions(+), 10 deletions(-)
diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c
index 491a27d0315f..f7fa895ede35 100644
--- a/drivers/mtd/mtdcore.c
+++ b/drivers/mtd/mtdcore.c
@@ -636,7 +636,7 @@ static int mtd_nvmem_add(struct mtd_info *mtd)
return 0;
}
-static void mtd_check_of_node(struct mtd_info *mtd)
+static struct device_node *mtd_find_of_node(struct mtd_info *mtd)
{
struct device_node *partitions, *parent_dn, *mtd_dn = NULL;
const char *pname, *prefix = "partition-";
@@ -644,14 +644,14 @@ static void mtd_check_of_node(struct mtd_info *mtd)
/* Check if MTD already has a device node */
if (mtd_get_of_node(mtd))
- return;
+ return of_node_get(mtd_get_of_node(mtd));
if (!mtd_is_partition(mtd))
- return;
+ return NULL;
parent_dn = of_node_get(mtd_get_of_node(mtd->parent));
if (!parent_dn)
- return;
+ return NULL;
if (mtd_is_partition(mtd->parent))
partitions = of_node_get(parent_dn);
@@ -679,16 +679,14 @@ static void mtd_check_of_node(struct mtd_info *mtd)
plen = strlen(pname) - offset;
if (plen == mtd_name_len &&
- !strncmp(mtd->name, pname + offset, plen)) {
- mtd_set_of_node(mtd, mtd_dn);
- of_node_put(mtd_dn);
+ !strncmp(mtd->name, pname + offset, plen))
break;
- }
}
of_node_put(partitions);
exit_parent:
of_node_put(parent_dn);
+ return mtd_dn;
}
/**
@@ -805,8 +803,7 @@ int add_mtd_device(struct mtd_info *mtd)
if (error)
goto fail_devname;
dev_set_drvdata(&mtd->dev, mtd);
- mtd_check_of_node(mtd);
- of_node_get(mtd_get_of_node(mtd));
+ mtd_set_of_node(mtd, mtd_find_of_node(mtd));
error = device_register(&mtd->dev);
if (error)
goto fail_added;
---
base-commit: 1c1a342aceec79528b3ba51f376eca2be5928fe7
change-id: 20261107-submit-mtd-partition-node-reference-v1-99ecfce4b351
Best regards,
--
James Hilliard <james.hilliard1 at gmail.com>
More information about the linux-mtd
mailing list