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

Michael Trimarchi michael at amarulasolutions.com
Sat Jun 13 11:04:51 CEST 2026


# Add your code comments below. There is no need to trim or delete
# any existing content -- just insert your comments under the relevant
# lines of code. Lines starting with "> " are quoted diff context and
# lines starting with "| " are comments from other reviewers.
# The final email will be reformatted automatically to include only
# the sections that have your comments.
#
> 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>
>
> 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;

Reviewed-By: Michael Trimarchi <michael at amarulasolutions.com>

-- 
Michael Trimarchi <michael at amarulasolutions.com>


More information about the U-Boot mailing list