Received: by 2002:a25:824b:0:0:0:0:0 with SMTP id d11csp1897354ybn; Thu, 26 Sep 2019 04:00:09 -0700 (PDT) X-Google-Smtp-Source: APXvYqwoVyMC3WxkHE9bC4seVy9dPydnDHK2ffuGRNBWKF4p6O9QqoEjsbVCyRaFmWXfEjlhu7xF X-Received: by 2002:a50:8961:: with SMTP id f30mr2772092edf.144.1569495609459; Thu, 26 Sep 2019 04:00:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1569495609; cv=none; d=google.com; s=arc-20160816; b=hpE8dn7UibWukvEtSp5JcOaI6DBnYIUXdpUT+zHqCvHbDlpYlaWQ6GJRs1ZZ9FvMmX XToIqhRQAzwiyI4qxMsTK3+lDTpAKfhYtUK94znBRKIZGO2+I5a1qm6EiD0WGOtOViUN sBbXLEli8njz/D+LWKnG2D47JnWm88nJDod6I5KY7UAjBPqTWj/LqUx7BaUWYunucDWV 2HJUvF5rIYzB6jtuQav/8EdfCpQeua9qxBqeo9mMvPT9RcDBGz6LtbBEjyT827cptOVh rwN2V51akaSemgmI/qlnGCDUxfj6/TgLNAO+M5RjB8o6F31j3xgLHCRTn1VofXQBXgi3 6pFg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:organisation:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=0DPxUvLqTeaXj+veDPxWoCYXJOHjU3PdWMUvfj149Mk=; b=O5rreCL4itTXXIwVS979x8GwwtivrOmWqWMe4zlvDcZqHmZWld65KmqubRm5EoETfZ fZtbvAHv3hRXZzqZOpfuLxF0lTGFBu18SNcPnlF24/ag7x+CQ7KTdlhxujDM2p1GZaDL T8A0f2pgwHgJzBL+Ii5PdL4VWPFLvDjVpIIO3jvb2T7OaF7rxTx8MoOwELUfzOxZNmSa MJuZCvJ9lKBvF0geXgAOULVpqQq0XtmHFylq0YzsPjMA00SUKnXhOvRNoZGL0dPP1bDc HexDAcv6s4P8SkMIHgH1kAr6VcSMlXJh4ia19YNWxtmSsaF0+Q6vqNJKY7r8FggHikRN 4EkA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n17si1000164edo.143.2019.09.26.03.59.46; Thu, 26 Sep 2019 04:00:09 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727898AbfIZIXG (ORCPT + 99 others); Thu, 26 Sep 2019 04:23:06 -0400 Received: from kirsty.vergenet.net ([202.4.237.240]:50626 "EHLO kirsty.vergenet.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725815AbfIZIXG (ORCPT ); Thu, 26 Sep 2019 04:23:06 -0400 Received: from reginn.horms.nl (watermunt.horms.nl [80.127.179.77]) by kirsty.vergenet.net (Postfix) with ESMTPA id E6F8D25AF0D; Thu, 26 Sep 2019 18:23:04 +1000 (AEST) Received: by reginn.horms.nl (Postfix, from userid 7100) id ECA80943750; Thu, 26 Sep 2019 10:23:02 +0200 (CEST) Date: Thu, 26 Sep 2019 10:23:02 +0200 From: Simon Horman To: Navid Emamdoost Cc: emamd001@umn.edu, smccaman@umn.edu, kjlu@umn.edu, Geert Uytterhoeven , Magnus Damm , linux-renesas-soc@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] soc: renesas: rcar-sysc: fix memory leak in rcar_sysc_pd_init Message-ID: <20190926082302.smaruxtgamgwoxad@verge.net.au> References: <20190925210354.8845-1-navid.emamdoost@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190925210354.8845-1-navid.emamdoost@gmail.com> Organisation: Horms Solutions BV User-Agent: NeoMutt/20170113 (1.7.2) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Ni Navid, thanks for your patch. On Wed, Sep 25, 2019 at 04:03:53PM -0500, Navid Emamdoost wrote: > In rcar_sysc_pd_init when looping over info->areas errors may happen but > the error handling path does not clean up the intermediate allocated > memories. > > This patch changes the error handling path in major and a little the loop > itself. Inside the loop if an error happens the current pd will be > released and then it goes to error handling path where it releases any > previously allocated domains. > > Signed-off-by: Navid Emamdoost > --- > drivers/soc/renesas/rcar-sysc.c | 27 ++++++++++++++++++++++++--- > 1 file changed, 24 insertions(+), 3 deletions(-) > > diff --git a/drivers/soc/renesas/rcar-sysc.c b/drivers/soc/renesas/rcar-sysc.c > index 59b5e6b10272..f9613c1ee0a0 100644 > --- a/drivers/soc/renesas/rcar-sysc.c > +++ b/drivers/soc/renesas/rcar-sysc.c > @@ -330,10 +330,10 @@ static int __init rcar_sysc_pd_init(void) > { > const struct rcar_sysc_info *info; > const struct of_device_id *match; > - struct rcar_pm_domains *domains; > + struct rcar_pm_domains *domains = NULL; > struct device_node *np; > void __iomem *base; > - unsigned int i; > + unsigned int i, num_areas = 0; > int error; Please preserve reverse xmas tree sorting of local variables. > np = of_find_matching_node_and_match(NULL, rcar_sysc_matches, &match); > @@ -382,6 +382,7 @@ static int __init rcar_sysc_pd_init(void) > pd = kzalloc(sizeof(*pd) + strlen(area->name) + 1, GFP_KERNEL); > if (!pd) { > error = -ENOMEM; > + num_areas = i; > goto out_put; > } > > @@ -393,8 +394,11 @@ static int __init rcar_sysc_pd_init(void) > pd->flags = area->flags; > > error = rcar_sysc_pd_setup(pd); > - if (error) > + if (error) { > + kfree(pd); > + num_areas = i; > goto out_put; > + } > > domains->domains[area->isr_bit] = &pd->genpd; > > @@ -406,13 +410,30 @@ static int __init rcar_sysc_pd_init(void) > if (error) { > pr_warn("Failed to add PM subdomain %s to parent %u\n", > area->name, area->parent); > + kfree(pd); > + num_areas = i; > goto out_put; > } > } > > error = of_genpd_add_provider_onecell(np, &domains->onecell_data); > + of_node_put(np); > + > + return error; > > out_put: > + if (domains) { > + for (i = 0; i < num_areas; i++) { > + const struct rcar_sysc_area *area = &info->areas[i]; > + > + if (!area->name) { > + /* Skip NULLified area */ > + continue; > + } > + kfree(domains->domains[area->isr_bit]); This cleanup doesn't feel correct to me. For one I think the allocated memory is at to_rcar_pd(domains->domains[area->isr_bit]); And for antoher I wonder if it is also necessary to unwind initialisation done by rcar_sysc_pd_setup() and pm_genpd_add_subdomain(); I think this leads us to the heart of why such unwinding is not present and that is, I suspect, that its reasonably complex and in the event of failure the system is very likely unusable. So leaking a bit of memory, while unpleasent, doesn't effect the user experience. > + } > + kfree(domains); > + } > of_node_put(np); > return error; I think it would be more in keeping with kernel coding style to add some extra labels for different error paths. I also think you can utilise the fact that i is already set to the number of allocated areas. Something like this (completely untested): out_free_areas: while (--i > 0) { /* Cleanup of 'i' goes here */ } out_free_domains: kfree(domains); out_put: of_node_put(np); return error; > } > -- > 2.17.1 >