Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp1846764rwl; Wed, 12 Apr 2023 20:30:48 -0700 (PDT) X-Google-Smtp-Source: AKy350aThZVyatNkwAvEA5NANmzwyLCcDAwxVnf/aBbEazA4a+ZEQ5+cua4c9A9LvlfJI+Crces7 X-Received: by 2002:a17:90b:685:b0:233:c301:32b3 with SMTP id m5-20020a17090b068500b00233c30132b3mr457711pjz.3.1681356647722; Wed, 12 Apr 2023 20:30:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681356647; cv=none; d=google.com; s=arc-20160816; b=P3hWzo2w1mT3gfp93tpposXm3cdEPWq7KlPCcQ2Vvdo6m+NL+fG/nDVItd4BkByD4Y WB4sYVIQsl9X3lbfRinLTJ3LyUZlAQZyH918Zxt9fXus8jWnbVipHz/WnzyOhU8r6F0o B6cOTBkp/Rw4f5A8cwEJaSiry2pVeHEL4SxF6sVs9QodxnHwXsP2VgAdUT3UFXU4nAW/ 5I5t1A78hydK91zgf/h01tHxRul5tvXps6TAPP/OdnkI9vzhq6pR1x8uEPoHE5uxOEtl +2IlEcud0++MmdDrbGULTLhArdFl9+Farp4QeBw7qTfaN3jkBFydzy6c9Rflx+lEmP7g 0zkw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=/GVVcSJub3/rooyq8tuxiZqi3WHBgOy09mqp9wCkGBU=; b=usMoFx01QV8Z2XgRghOaBvTDuwo6SgD0we3KgIRdQeZNeu5YuCAp/FSnboz/N1dLtp L3q6rDjO4hZ2PkPV0bVs/hp3RW/Blt6LPt0ECSvdyFX/iUkkSS2A2FyGA9ti2AYWEfH+ Y1ThW5n3z61MsSnrsxXQfGq5ATD6t+cayAxOuT/snBonqBHeqIWPSh0Hlx6DA6Dnly3T ULRZA0hMbXKNwitZuW5+l2btqincUDEuPJS4nGr4O1YySZP506bNtmY6PBNhE93x9X75 KPeAqB3rcmOVJgkVp2gM9TENvF52r9TkPVkdMP0UQIdd7nVRHk9eWrYpoU94lGslAfo8 n1bA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p17-20020a170902e75100b0019f34a15738si921036plf.139.2023.04.12.20.30.36; Wed, 12 Apr 2023 20:30:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229597AbjDMD2r (ORCPT + 99 others); Wed, 12 Apr 2023 23:28:47 -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: 8bit X-FEAS-AUTH-USER: u201911681@hust.edu.cn X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_PASS, SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- 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