Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp575049ybz; Wed, 29 Apr 2020 05:41:07 -0700 (PDT) X-Google-Smtp-Source: APiQypLoubZgHRNZKHKaloJSylsJHsgXBOqB66oKBQBsSjWwHU4PeAwaSiNJvIDGgkb+9tLTwsV7 X-Received: by 2002:a50:8716:: with SMTP id i22mr2229588edb.248.1588164067285; Wed, 29 Apr 2020 05:41:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588164067; cv=none; d=google.com; s=arc-20160816; b=oT3mAOicyItR1tzac+VVc3gMv01qSQkPt5TgRZfBB3qgn2rKLG/DvdXZd1/Vvu0XIc RwniGZCrww1kSeI2p33IatArF8ajcJfFazhBeIL/q8a1sOprobSG8P+Tsb8V2NkRQg5H M22CgTmvApMKsk0M5diZJBcynxaAk3w/dzuy8JxEoeg6x5cNby2YF8NA949cvjM+1/H0 EIie/0vgSAbzovaLMYxzq2ySLhkUyNd2cNJ54bjhyEuir3oeGTEQbkv8cIkDFgxMeFIx w8TPSTVFtFfwEtNFPSi000I9fkMgC5NijZUvLWuotuuG8CvYrJUZMqSfAMpSMNI3dhFZ su8A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:date:in-reply-to :subject:cc:to:from:user-agent:references:dkim-signature; bh=NkpmzV5D80JNy4ts2YZIu3+YM84vR2UzyeLRZ8y2XUk=; b=0EQxntMDehpwjl7smmEY1jglAr83dWT7X8rD1XScIWXX/uNOiV7rIYXblpApdnH4Ds s9V7jGNPz4H0KXmkNxBDepgR9zm8uaEA8XTszwDz44RshAUaC9cYC+GfZ8AWRCcelWuz Rg25jgCuk+LfwVqaAg4LQ5lfT9b0T1oRcnnsWRoXTyMe9OleQOLkYsvldEhWFbiiWSDI M3h7ymDSSc1kY/2HIEyot5812NUUsQeLTLJYw+YoFLJHLBPtD3FzIMRmtNsO3uSGEWRN KFXCBqalwtwUv7729RtXn82UAQGmKR8RfkfXYWruAGKgZKzYvXGz3Lkm7U0MZUzE2exC VUPw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@baylibre-com.20150623.gappssmtp.com header.s=20150623 header.b=RB2S6Kbc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ca3si3142136edb.164.2020.04.29.05.40.43; Wed, 29 Apr 2020 05:41:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=fail header.i=@baylibre-com.20150623.gappssmtp.com header.s=20150623 header.b=RB2S6Kbc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726826AbgD2MhN (ORCPT + 99 others); Wed, 29 Apr 2020 08:37:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43624 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1726740AbgD2MhM (ORCPT ); Wed, 29 Apr 2020 08:37:12 -0400 Received: from mail-wm1-x344.google.com (mail-wm1-x344.google.com [IPv6:2a00:1450:4864:20::344]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3AA92C03C1AD for ; Wed, 29 Apr 2020 05:37:12 -0700 (PDT) Received: by mail-wm1-x344.google.com with SMTP id k12so1842157wmj.3 for ; Wed, 29 Apr 2020 05:37:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20150623.gappssmtp.com; s=20150623; h=references:user-agent:from:to:cc:subject:in-reply-to:date :message-id:mime-version; bh=NkpmzV5D80JNy4ts2YZIu3+YM84vR2UzyeLRZ8y2XUk=; b=RB2S6Kbc+kfXu6qf/y6WSvhN9KPpKce18ars9tpM2GQHKrokwDv6/gMX4kdak20F6j KXNYe2U3QdGzROWkaeE5+5esEhR6IdymhTSFRH5is6PbUfFZjdXEhNZXda8pGH9KVC2H wX/PkvDEyEKQ+60pRvswRIlPgDyTsEywC3MJkTI+LCDEJ//QbLAJ0qmL31Ql306LvGM6 5o+o682PQZk5WTzs0jbP/ePdF4FvqO+2vokdsZ2rnTvaqBsOLpIvsOr3AmoToWMeieYb HoJxSlRteoXodvsrMnkadcgDRcAmpXv/EkkM0q4N6XjFvFN+jDEk1BF908zvESsOTGvR Znrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:references:user-agent:from:to:cc:subject :in-reply-to:date:message-id:mime-version; bh=NkpmzV5D80JNy4ts2YZIu3+YM84vR2UzyeLRZ8y2XUk=; b=X2mhGNbV0Y6v2bMDz71Rbvk2qzz+67hhmdEMoIhcJLpUy1WDdKETS4VVwakAgyJ9l3 dM5y1rpAMAzWGxH7g49kx+tovJAv7+d9q2/hCR6BebhuLdlvJCsr0PLuwpU0HyYBbOZW GhmTkN31E4GHqZY6Pcv6Mq8kBGF+/rYf4WfiT4+LWN4BuJHa4ZemAFBMSJBZheSrlA5s 8rn3/4X329fZJLkVjVE6NshpyduT+B7RIrocNFuZaMHjOMAg3WTw2Vt1AS9TVNvfha9o /W1p0MFSn4dSWCR9Z58LTbRXEEvIM25kJlpcYzJsYEH5dtB+cUr05Hz3vHfRLV/v42I1 Wmzw== X-Gm-Message-State: AGi0PuZsQfDcc68go3GMPbyAyooAR8/6Sht6aw+xGSO2gYhUe1lnLiRu 64nsF0Zk9MtwsI5IhkYCD0Jlvw== X-Received: by 2002:a1c:5403:: with SMTP id i3mr3302337wmb.10.1588163830921; Wed, 29 Apr 2020 05:37:10 -0700 (PDT) Received: from localhost (cag06-3-82-243-161-21.fbx.proxad.net. [82.243.161.21]) by smtp.gmail.com with ESMTPSA id k23sm7597481wmi.46.2020.04.29.05.37.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Apr 2020 05:37:10 -0700 (PDT) References: <20200429031416.3900-1-bernard@vivo.com> User-agent: mu4e 1.3.3; emacs 26.3 From: Jerome Brunet To: Martin Blumenstingl , Bernard Zhao , Neil Armstrong , Stephen Boyd , Kevin Hilman , linux-amlogic@lists.infradead.org, linux-clk@vger.kernel.org, linux-kernel@vger.kernel.org Cc: opensource.kernel@vivo.com Subject: Re: [PATCH] clk/meson: fixes memleak issue in init err branch In-reply-to: <20200429031416.3900-1-bernard@vivo.com> Date: Wed, 29 Apr 2020 14:37:09 +0200 Message-ID: <1jpnbqii2y.fsf@starbuckisacylon.baylibre.com> MIME-Version: 1.0 Content-Type: text/plain Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed 29 Apr 2020 at 05:14, Bernard Zhao wrote: > In common init function, when run into err branch, we didn`t > use kfree to release kzmalloc area, this may bring in memleak Thx for reporting this Bernard. I'm not a fan of adding kfree everywhere. I'd much prefer a label and clear error exit path. That being said, the allocation is probably not the only thing that needs to be undone in case of error. I guess this is due to conversion to CLK_OF_DECLARE_DRIVER() which forced to drop all the devm_ This was done because the clock controller was required early in the boot sequence. There is 2 paths to properly solve this: 1) Old school: manually undo everything with every error exit condition Doable but probably a bit messy 2) Convert back the driver to a real platform driver and use devm_. We would still need the controller to register early but I wonder if we could use the same method as drivers/clk/mediatek/clk-mt2701.c and use arch_initcall() ? Martin, you did the initial conversion, what do you think of option 2 ? Would it still answer the problem you were trying to solve back then ? One added benefit of option 2 is we could drop CLK_OF_DECLARE_DRIVER(). We could even do the same in for the other SoCs, which I suppose would avoid a fair amount of probe deferral. > > Signed-off-by: Bernard Zhao > --- > drivers/clk/meson/meson8b.c | 10 ++++++++-- > 1 file changed, 8 insertions(+), 2 deletions(-) > > diff --git a/drivers/clk/meson/meson8b.c b/drivers/clk/meson/meson8b.c > index 34a70c4b4899..0f07d5a4cd16 100644 > --- a/drivers/clk/meson/meson8b.c > +++ b/drivers/clk/meson/meson8b.c > @@ -3687,6 +3687,7 @@ static void __init meson8b_clkc_init_common(struct device_node *np, > if (ret) { > pr_err("%s: Failed to register clkc reset controller: %d\n", > __func__, ret); > + kfree(rstc); > return; > } > > @@ -3710,8 +3711,10 @@ static void __init meson8b_clkc_init_common(struct device_node *np, > continue; > > ret = of_clk_hw_register(np, clk_hw_onecell_data->hws[i]); > - if (ret) > + if (ret) { > + kfree(rstc); > return; > + } > } > > meson8b_cpu_nb_data.cpu_clk = clk_hw_onecell_data->hws[CLKID_CPUCLK]; > @@ -3727,13 +3730,16 @@ static void __init meson8b_clkc_init_common(struct device_node *np, > if (ret) { > pr_err("%s: failed to register the CPU clock notifier\n", > __func__); > + kfree(rstc); > return; > } > > ret = of_clk_add_hw_provider(np, of_clk_hw_onecell_get, > clk_hw_onecell_data); > - if (ret) > + if (ret) { > pr_err("%s: failed to register clock provider\n", __func__); > + kfree(rstc); > + } > } > > static void __init meson8_clkc_init(struct device_node *np)