[PATCH] nvmem: fix a reference leak in u_boot_env_probe()

From: Haoxiang Li

Date: Fri Dec 04 2026 - 00:52:19 EST


of_get_mtd_device_by_node() acquires a MTD device reference
but the reference is never released, causing a potential
leak. Use devm_add_action_or_reset() to drop the reference
when device is dropped.

Fixes: d5542923f200 ("nvmem: add driver handling U-Boot environment variables")
Signed-off-by: Haoxiang Li <haoxiang_li2024@xxxxxxx>
---
drivers/nvmem/u-boot-env.c | 10 ++++++++++
1 file changed, 10 insertions(+)

diff --git a/drivers/nvmem/u-boot-env.c b/drivers/nvmem/u-boot-env.c
index a9cb1bc5b779..fddac8b07ee5 100644
--- a/drivers/nvmem/u-boot-env.c
+++ b/drivers/nvmem/u-boot-env.c
@@ -42,6 +42,11 @@ static int u_boot_env_read(void *context, unsigned int offset, void *val,
return 0;
}

+static void u_boot_env_put_mtd(void *data)
+{
+ put_mtd_device(data);
+}
+
static int u_boot_env_probe(struct platform_device *pdev)
{
struct nvmem_config config = {
@@ -51,6 +56,7 @@ static int u_boot_env_probe(struct platform_device *pdev)
struct device *dev = &pdev->dev;
struct device_node *np = dev->of_node;
struct u_boot_env *priv;
+ int ret;

priv = devm_kzalloc_obj(dev, *priv);
if (!priv)
@@ -65,6 +71,10 @@ static int u_boot_env_probe(struct platform_device *pdev)
return PTR_ERR(priv->mtd);
}

+ ret = devm_add_action_or_reset(dev, u_boot_env_put_mtd, priv->mtd);
+ if (ret)
+ return ret;
+
config.dev = dev;
config.priv = priv;
config.size = priv->mtd->size;
--
2.25.1