[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <2038e5bd.42407.187b62983e3.Coremail.u201911681@hust.edu.cn>
Date: Tue, 25 Apr 2023 10:06:26 +0800 (GMT+08:00)
From: 周师德 <u201911681@...t.edu.cn>
To: "abel vesa" <abelvesa@...nel.org>, "peng fan" <peng.fan@....com>,
"michael turquette" <mturquette@...libre.com>,
"stephen boyd" <sboyd@...nel.org>,
"shawn guo" <shawnguo@...nel.org>,
"sascha hauer" <s.hauer@...gutronix.de>,
"pengutronix kernel team" <kernel@...gutronix.de>,
"fabio estevam" <festevam@...il.com>,
"nxp linux team" <linux-imx@....com>, "bai ping" <ping.bai@....com>
Cc: hust-os-kernel-patches@...glegroups.com, linux-clk@...r.kernel.org,
linux-arm-kernel@...ts.infradead.org, linux-kernel@...r.kernel.org
Subject: Re: [PATCH] clk: imx: clk-imx8mm: fix memory leak issue in
'imx8mm_clocks_probe'
> -----原始邮件-----
> 发件人: "Zhou Shide" <u201911681@...t.edu.cn>
> 发送时间: 2023-04-13 11:24:39 (星期四)
> 收件人: "Abel Vesa" <abelvesa@...nel.org>, "Peng Fan" <peng.fan@....com>, "Michael Turquette" <mturquette@...libre.com>, "Stephen Boyd" <sboyd@...nel.org>, "Shawn Guo" <shawnguo@...nel.org>, "Sascha Hauer" <s.hauer@...gutronix.de>, "Pengutronix Kernel Team" <kernel@...gutronix.de>, "Fabio Estevam" <festevam@...il.com>, "NXP Linux Team" <linux-imx@....com>, "Bai Ping" <ping.bai@....com>
> 抄送: hust-os-kernel-patches@...glegroups.com, "Zhou Shide" <u201911681@...t.edu.cn>, linux-clk@...r.kernel.org, linux-arm-kernel@...ts.infradead.org, linux-kernel@...r.kernel.org
> 主题: [PATCH] clk: imx: clk-imx8mm: fix memory leak issue in 'imx8mm_clocks_probe'
>
> The function imx8mm_clocks_probe() has two main issues:
> - The of_iomap() function may cause a memory leak.
> - Memory allocated for 'clk_hw_data' may not be freed properly
> in some paths.
>
> To fix these issues, this commit replaces the use of of_iomap()
> with devm_of_iomap() and replaces kzalloc() with devm_kzalloc().
> This ensures that all memory is properly managed and automatically
> freed when the device is removed.
>
> In addition, when devm_of_iomap() allocates memory with an error,
> it will first jump to label "unregister_hws" and
> then return PTR_ ERR(base).
>
> Fixes: 9c71f9ea35d7 ("clk: imx: imx8mm: Switch to clk_hw based API")
> Fixes: ba5625c3e272 ("clk: imx: Add clock driver support for imx8mm")
> Signed-off-by: Zhou Shide <u201911681@...t.edu.cn>
> ---
> The issue is discovered by static analysis, and the patch is not tested yet.
>
> drivers/clk/imx/clk-imx8mm.c | 16 ++++++++++------
> 1 file changed, 10 insertions(+), 6 deletions(-)
>
> diff --git a/drivers/clk/imx/clk-imx8mm.c b/drivers/clk/imx/clk-imx8mm.c
> index b618892170f2..e6e0bb4805a6 100644
> --- a/drivers/clk/imx/clk-imx8mm.c
> +++ b/drivers/clk/imx/clk-imx8mm.c
> @@ -303,7 +303,7 @@ static int imx8mm_clocks_probe(struct platform_device *pdev)
> void __iomem *base;
> int ret;
>
> - clk_hw_data = kzalloc(struct_size(clk_hw_data, hws,
> + clk_hw_data = devm_kzalloc(dev, struct_size(clk_hw_data, hws,
> IMX8MM_CLK_END), GFP_KERNEL);
> if (WARN_ON(!clk_hw_data))
> return -ENOMEM;
> @@ -320,10 +320,12 @@ static int imx8mm_clocks_probe(struct platform_device *pdev)
> hws[IMX8MM_CLK_EXT4] = imx_get_clk_hw_by_name(np, "clk_ext4");
>
> np = of_find_compatible_node(NULL, NULL, "fsl,imx8mm-anatop");
> - base = of_iomap(np, 0);
> + base = devm_of_iomap(dev, np, 0, NULL);
> of_node_put(np);
> - if (WARN_ON(!base))
> - return -ENOMEM;
> + if (WARN_ON(IS_ERR(base))) {
> + ret = PTR_ERR(base);
> + goto unregister_hws;
> + }
>
> hws[IMX8MM_AUDIO_PLL1_REF_SEL] = imx_clk_hw_mux("audio_pll1_ref_sel", base + 0x0, 0, 2, pll_ref_sels, ARRAY_SIZE(pll_ref_sels));
> hws[IMX8MM_AUDIO_PLL2_REF_SEL] = imx_clk_hw_mux("audio_pll2_ref_sel", base + 0x14, 0, 2, pll_ref_sels, ARRAY_SIZE(pll_ref_sels));
> @@ -399,8 +401,10 @@ static int imx8mm_clocks_probe(struct platform_device *pdev)
>
> np = dev->of_node;
> base = devm_platform_ioremap_resource(pdev, 0);
> - if (WARN_ON(IS_ERR(base)))
> - return PTR_ERR(base);
> + if (WARN_ON(IS_ERR(base))) {
> + ret = PTR_ERR(base);
> + goto unregister_hws;
> + }
>
> /* Core Slice */
> hws[IMX8MM_CLK_A53_DIV] = imx8m_clk_hw_composite_core("arm_a53_div", imx8mm_a53_sels, base + 0x8000);
> --
> 2.34.1
ping?
Powered by blists - more mailing lists