* [PATCH] nvmem: u-boot-env: Fix mtd reference leak in u_boot_env_probe()
@ 2026-09-17 12:38 Wentao Liang
2026-09-18 21:44 ` Srinivas Kandagatla
0 siblings, 1 reply; 2+ messages in thread
From: Wentao Liang @ 2026-09-17 12:38 UTC (permalink / raw)
To: a.fatoum; +Cc: gregkh, linux-kernel, rafal, srini, Wentao Liang, stable
of_get_mtd_device_by_node() takes a reference on the returned MTD device
that the caller has to drop with put_mtd_device(). The probe function
returns early when devm_nvmem_register() or u_boot_env_parse() fails
without releasing that reference, so add the missing put_mtd_device()
calls on both error paths.
Fixes: d5542923f200 ("nvmem: add driver handling U-Boot environment variables")
Cc: stable@vger.kernel.org
Signed-off-by: Wentao Liang <vulab@iscas.ac.cn>
---
drivers/nvmem/u-boot-env.c | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)
diff --git a/drivers/nvmem/u-boot-env.c b/drivers/nvmem/u-boot-env.c
index ced414fc9e60..893fbd2a8520 100644
--- a/drivers/nvmem/u-boot-env.c
+++ b/drivers/nvmem/u-boot-env.c
@@ -52,6 +52,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(dev, sizeof(*priv), GFP_KERNEL);
if (!priv)
@@ -71,10 +72,16 @@ static int u_boot_env_probe(struct platform_device *pdev)
config.size = priv->mtd->size;
priv->nvmem = devm_nvmem_register(dev, &config);
- if (IS_ERR(priv->nvmem))
+ if (IS_ERR(priv->nvmem)) {
+ put_mtd_device(priv->mtd);
return PTR_ERR(priv->nvmem);
+ }
+
+ ret = u_boot_env_parse(dev, priv->nvmem, priv->format);
+ if (ret)
+ put_mtd_device(priv->mtd);
- return u_boot_env_parse(dev, priv->nvmem, priv->format);
+ return ret;
}
static const struct of_device_id u_boot_env_of_match_table[] = {
--
2.34.1
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] nvmem: u-boot-env: Fix mtd reference leak in u_boot_env_probe()
2026-09-17 12:38 [PATCH] nvmem: u-boot-env: Fix mtd reference leak in u_boot_env_probe() Wentao Liang
@ 2026-09-18 21:44 ` Srinivas Kandagatla
0 siblings, 0 replies; 2+ messages in thread
From: Srinivas Kandagatla @ 2026-09-18 21:44 UTC (permalink / raw)
To: Wentao Liang, a.fatoum; +Cc: gregkh, linux-kernel, rafal, srini, stable
On 9/17/26 1:38 PM, Wentao Liang wrote:
> of_get_mtd_device_by_node() takes a reference on the returned MTD device
> that the caller has to drop with put_mtd_device(). The probe function
> returns early when devm_nvmem_register() or u_boot_env_parse() fails
> without releasing that reference, so add the missing put_mtd_device()
> calls on both error paths.
>
> Fixes: d5542923f200 ("nvmem: add driver handling U-Boot environment variables")
> Cc: stable@vger.kernel.org
> Signed-off-by: Wentao Liang <vulab@iscas.ac.cn>
> ---
> drivers/nvmem/u-boot-env.c | 11 +++++++++--
> 1 file changed, 9 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/nvmem/u-boot-env.c b/drivers/nvmem/u-boot-env.c
> index ced414fc9e60..893fbd2a8520 100644
> --- a/drivers/nvmem/u-boot-env.c
> +++ b/drivers/nvmem/u-boot-env.c
> @@ -52,6 +52,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(dev, sizeof(*priv), GFP_KERNEL);
> if (!priv)
> @@ -71,10 +72,16 @@ static int u_boot_env_probe(struct platform_device *pdev)
> config.size = priv->mtd->size;
>
> priv->nvmem = devm_nvmem_register(dev, &config);
> - if (IS_ERR(priv->nvmem))
> + if (IS_ERR(priv->nvmem)) {
> + put_mtd_device(priv->mtd);
> return PTR_ERR(priv->nvmem);
> + }
> +
> + ret = u_boot_env_parse(dev, priv->nvmem, priv->format);
> + if (ret)
> + put_mtd_device(priv->mtd);
>
> - return u_boot_env_parse(dev, priv->nvmem, priv->format);
> + return ret;
consider devm_add_action_or_reset() that should cover one more case this
patch missed.
--srini
> }
>
> static const struct of_device_id u_boot_env_of_match_table[] = {
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-09-18 21:44 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-17 12:38 [PATCH] nvmem: u-boot-env: Fix mtd reference leak in u_boot_env_probe() Wentao Liang
2026-09-18 21:44 ` Srinivas Kandagatla
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®