[PATCH] mtd: ubi: fix nvmem-layout reference leak

Zhiqi Zhang zhiqizhangg at gmail.com
Wed Nov 25 19:00:33 PST 2026


While reviewing the UBI NVMEM provider, I found that ubi_nvmem_add()
only uses of_get_child_by_name() to check whether the UBI volume has an
nvmem-layout child node. The helper returns a referenced device_node on
success, so the current code leaks that reference when the child exists.

I fixed this by storing the child node pointer and dropping the reference
once the existence check has completed.

Fixes: 3ce485803da1 ("mtd: ubi: provide NVMEM layer over UBI volumes")
Signed-off-by: Zhiqi Zhang <zhiqizhangg at gmail.com>
---
 drivers/mtd/ubi/nvmem.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/drivers/mtd/ubi/nvmem.c b/drivers/mtd/ubi/nvmem.c
index 2dade9727..d6c5f191d 100644
--- a/drivers/mtd/ubi/nvmem.c
+++ b/drivers/mtd/ubi/nvmem.c
@@ -61,6 +61,7 @@ static int ubi_nvmem_reg_read(void *priv, unsigned int from,
 static int ubi_nvmem_add(struct ubi_volume_info *vi)
 {
 	struct device_node *np = dev_of_node(vi->dev);
+	struct device_node *layout_np;
 	struct nvmem_config config = {};
 	struct ubi_nvmem *unv;
 	int ret;
@@ -68,8 +69,10 @@ static int ubi_nvmem_add(struct ubi_volume_info *vi)
 	if (!np)
 		return 0;
 
-	if (!of_get_child_by_name(np, "nvmem-layout"))
+	layout_np = of_get_child_by_name(np, "nvmem-layout");
+	if (!layout_np)
 		return 0;
+	of_node_put(layout_np);
 
 	if (WARN_ON_ONCE(vi->usable_leb_size <= 0) ||
 	    WARN_ON_ONCE(vi->size <= 0))
-- 
2.54.0 (Apple Git-157)




More information about the linux-mtd mailing list