From nobody Thu Feb 12 00:25:23 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 01CF9C77B6C for ; Thu, 13 Apr 2023 03:28:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229640AbjDMD2s (ORCPT ); Wed, 12 Apr 2023 23:28:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46010 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229831AbjDMD21 (ORCPT ); Wed, 12 Apr 2023 23:28:27 -0400 Received: from hust.edu.cn (mail.hust.edu.cn [202.114.0.240]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F402E139; Wed, 12 Apr 2023 20:28:25 -0700 (PDT) Received: from localhost.localdomain ([172.16.0.254]) (user=u201911681@hust.edu.cn mech=LOGIN bits=0) by mx1.hust.edu.cn with ESMTP id 33D3PeWP002793-33D3PeWQ002793 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Thu, 13 Apr 2023 11:25:48 +0800 From: Zhou Shide To: Abel Vesa , Peng Fan , Michael Turquette , Stephen Boyd , Shawn Guo , Sascha Hauer , Pengutronix Kernel Team , Fabio Estevam , NXP Linux Team , Bai Ping Cc: hust-os-kernel-patches@googlegroups.com, Zhou Shide , linux-clk@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH] clk: imx: clk-imx8mm: fix memory leak issue in 'imx8mm_clocks_probe' Date: Thu, 13 Apr 2023 03:24:39 +0000 Message-Id: <20230413032439.1706448-1-u201911681@hust.edu.cn> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-FEAS-AUTH-USER: u201911681@hust.edu.cn Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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 Reviewed-by: Peng Fan --- 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; =20 - clk_hw_data =3D kzalloc(struct_size(clk_hw_data, hws, + clk_hw_data =3D 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] =3D imx_get_clk_hw_by_name(np, "clk_ext4"); =20 np =3D of_find_compatible_node(NULL, NULL, "fsl,imx8mm-anatop"); - base =3D of_iomap(np, 0); + base =3D devm_of_iomap(dev, np, 0, NULL); of_node_put(np); - if (WARN_ON(!base)) - return -ENOMEM; + if (WARN_ON(IS_ERR(base))) { + ret =3D PTR_ERR(base); + goto unregister_hws; + } =20 hws[IMX8MM_AUDIO_PLL1_REF_SEL] =3D imx_clk_hw_mux("audio_pll1_ref_sel", b= ase + 0x0, 0, 2, pll_ref_sels, ARRAY_SIZE(pll_ref_sels)); hws[IMX8MM_AUDIO_PLL2_REF_SEL] =3D imx_clk_hw_mux("audio_pll2_ref_sel", b= ase + 0x14, 0, 2, pll_ref_sels, ARRAY_SIZE(pll_ref_sels)); @@ -399,8 +401,10 @@ static int imx8mm_clocks_probe(struct platform_device = *pdev) =20 np =3D dev->of_node; base =3D devm_platform_ioremap_resource(pdev, 0); - if (WARN_ON(IS_ERR(base))) - return PTR_ERR(base); + if (WARN_ON(IS_ERR(base))) { + ret =3D PTR_ERR(base); + goto unregister_hws; + } =20 /* Core Slice */ hws[IMX8MM_CLK_A53_DIV] =3D imx8m_clk_hw_composite_core("arm_a53_div", im= x8mm_a53_sels, base + 0x8000); --=20 2.34.1