[PATCH] clk: fix memory leak in hisi_clk_init.

Tao Lan posted 1 patch 2 years, 8 months ago
drivers/clk/hisilicon/clk.c | 4 ++++
1 file changed, 4 insertions(+)
[PATCH] clk: fix memory leak in hisi_clk_init.
Posted by Tao Lan 2 years, 8 months ago
From: taolan <taolan@huawei.com>

when clk_data create fail, we also need to release base.

Signed-off-by: taolan <taolan@huawei.com>
---
 drivers/clk/hisilicon/clk.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/drivers/clk/hisilicon/clk.c b/drivers/clk/hisilicon/clk.c
index 54d9fdc93599..9ca4fc05fa57 100644
--- a/drivers/clk/hisilicon/clk.c
+++ b/drivers/clk/hisilicon/clk.c
@@ -82,6 +82,10 @@ struct hisi_clock_data *hisi_clk_init(struct device_node *np,
 	of_clk_add_provider(np, of_clk_src_onecell_get, &clk_data->clk_data);
 	return clk_data;
 err_data:
+	if (base) {
+		iounmap(base);
+		base = NULL;
+	}
 	kfree(clk_data);
 err:
 	return NULL;
-- 
2.17.1
Re: [PATCH] clk: fix memory leak in hisi_clk_init.
Posted by Jonathan Neuschäfer 2 years, 1 month ago
Hello,

sorry for the delay, but I found this unmerged patch while cleaning up
my mailbox.

On Thu, Dec 22, 2022 at 09:12:21AM +0000, Tao Lan wrote:
> From: taolan <taolan@huawei.com>
> 
> when clk_data create fail, we also need to release base.
> 
> Signed-off-by: taolan <taolan@huawei.com>
> ---
>  drivers/clk/hisilicon/clk.c | 4 ++++
>  1 file changed, 4 insertions(+)
> 
> diff --git a/drivers/clk/hisilicon/clk.c b/drivers/clk/hisilicon/clk.c
> index 54d9fdc93599..9ca4fc05fa57 100644
> --- a/drivers/clk/hisilicon/clk.c
> +++ b/drivers/clk/hisilicon/clk.c
> @@ -82,6 +82,10 @@ struct hisi_clock_data *hisi_clk_init(struct device_node *np,
>  	of_clk_add_provider(np, of_clk_src_onecell_get, &clk_data->clk_data);
>  	return clk_data;
>  err_data:
> +	if (base) {
> +		iounmap(base);
> +		base = NULL;
> +	}

This is inaccurate. Consider the case when kzalloc fails:

	clk_data = kzalloc(sizeof(*clk_data), GFP_KERNEL);
	if (!clk_data)
		goto err;

base has already been mapped, but the code jumps to 'err', which doesn't
have the iounmap.

To address this properly, you'd have to add another label between
err_data and err.


>  	kfree(clk_data);
>  err:
>  	return NULL;


Best regards,
jn