[PATCH v2] clk: starfive: jh7110: fix memory leak in jh7110_reset_controller_register() error path

Guangshuo Li posted 1 patch 2 months, 1 week ago
There is a newer version of this series
drivers/clk/starfive/clk-starfive-jh7110-sys.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
[PATCH v2] clk: starfive: jh7110: fix memory leak in jh7110_reset_controller_register() error path
Posted by Guangshuo Li 2 months, 1 week ago
jh7110_reset_controller_register() allocates a jh71x0_reset_adev with
kzalloc() before calling auxiliary_device_init().

When auxiliary_device_init() returns an error, the function exits
without freeing rdev. Since the release callback is only expected to
handle cleanup after successful initialization, rdev should be freed
explicitly in this path.

Add the missing kfree(rdev) before returning from the
auxiliary_device_init() error path.

Fixes: edab7204afe5 ("clk: starfive: Add StarFive JH7110 system clock driver")
Cc: stable@vger.kernel.org
Signed-off-by: Guangshuo Li <lgs201920130244@gmail.com>
---
 drivers/clk/starfive/clk-starfive-jh7110-sys.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/clk/starfive/clk-starfive-jh7110-sys.c b/drivers/clk/starfive/clk-starfive-jh7110-sys.c
index 52833d4241c5..55cd0ccbdb84 100644
--- a/drivers/clk/starfive/clk-starfive-jh7110-sys.c
+++ b/drivers/clk/starfive/clk-starfive-jh7110-sys.c
@@ -360,8 +360,10 @@ int jh7110_reset_controller_register(struct jh71x0_clk_priv *priv,
 	adev->id = adev_id;
 
 	ret = auxiliary_device_init(adev);
-	if (ret)
+	if (ret) {
+		kfree(rdev);
 		return ret;
+	}
 
 	ret = auxiliary_device_add(adev);
 	if (ret) {
-- 
2.43.0
Re: [PATCH v2] clk: starfive: jh7110: fix memory leak in jh7110_reset_controller_register() error path
Posted by Brian Masney 2 months, 1 week ago
Hi Guangshuo,

On Sun, Apr 12, 2026 at 08:54:50PM +0800, Guangshuo Li wrote:
> jh7110_reset_controller_register() allocates a jh71x0_reset_adev with
> kzalloc() before calling auxiliary_device_init().
> 
> When auxiliary_device_init() returns an error, the function exits
> without freeing rdev. Since the release callback is only expected to
> handle cleanup after successful initialization, rdev should be freed
> explicitly in this path.
> 
> Add the missing kfree(rdev) before returning from the
> auxiliary_device_init() error path.
> 
> Fixes: edab7204afe5 ("clk: starfive: Add StarFive JH7110 system clock driver")
> Cc: stable@vger.kernel.org
> Signed-off-by: Guangshuo Li <lgs201920130244@gmail.com>
> ---
>  drivers/clk/starfive/clk-starfive-jh7110-sys.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/clk/starfive/clk-starfive-jh7110-sys.c b/drivers/clk/starfive/clk-starfive-jh7110-sys.c
> index 52833d4241c5..55cd0ccbdb84 100644
> --- a/drivers/clk/starfive/clk-starfive-jh7110-sys.c
> +++ b/drivers/clk/starfive/clk-starfive-jh7110-sys.c
> @@ -360,8 +360,10 @@ int jh7110_reset_controller_register(struct jh71x0_clk_priv *priv,
>  	adev->id = adev_id;
>  
>  	ret = auxiliary_device_init(adev);
> -	if (ret)
> +	if (ret) {
> +		kfree(rdev);
>  		return ret;
> +	}
>  
>  	ret = auxiliary_device_add(adev);
>  	if (ret) {

There's actually another leak in the error path for
auxiliary_device_add(). I think this code should be
converted to devm_kzalloc().

There is no devm_kzalloc_obj() yet, however according to [1] that should
be coming soon.

[1] https://lore.kernel.org/lkml/20260330154108.GA3389518@killaraus.ideasonboard.com/

Brian