[PATCH] env: mtd: fix unbalanced put_mtd_device()

Weijie Gao weijie.gao at mediatek.com
Thu Jun 11 10:35:17 CEST 2026


In env_mtd_load(), put_mtd_device(mtd_env) should only be called after
setup_mtd_device(&mtd_env) returned success. Failing to do so will trigger
BUG_ON(mtd->usecount < 0) crash in __put_mtd_device().

Adding an early error out path for setup_mtd_device() to skip
put_mtd_device() will solve this issue.

Fixes: 39ae954b04ef ("env: mtd: add the missing put_mtd_device()")
Signed-off-by: Weijie Gao <weijie.gao at mediatek.com>
---
 env/mtd.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/env/mtd.c b/env/mtd.c
index b26ee80985a..67d207272d8 100644
--- a/env/mtd.c
+++ b/env/mtd.c
@@ -157,7 +157,7 @@ static int env_mtd_load(void)
 
 	ret = setup_mtd_device(&mtd_env);
 	if (ret)
-		goto out;
+		goto early_out;
 
 	sect_size = mtd_env->erasesize;
 
@@ -192,6 +192,7 @@ static int env_mtd_load(void)
 out:
 	put_mtd_device(mtd_env);
 
+early_out:
 	free(buf);
 
 	return ret;
-- 
2.45.2



More information about the U-Boot mailing list