From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933206AbdEVMhh (ORCPT ); Mon, 22 May 2017 08:37:37 -0400 Received: from szxga01-in.huawei.com ([45.249.212.187]:6798 "EHLO szxga01-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1757679AbdEVMgn (ORCPT ); Mon, 22 May 2017 08:36:43 -0400 From: Timmy Li Subject: Re: [PATCH] arm64: PCI: free all allocated memory in case of failure To: Lorenzo Pieralisi CC: , , , , , References: <20170512095747.744688-1-lixiaoping3@huawei.com> <20170512102825.GA10929@red-moon> Message-ID: <84e06412-9aa9-3fe5-d727-036d62114c27@huawei.com> Date: Mon, 22 May 2017 20:32:09 +0800 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.0.1 MIME-Version: 1.0 In-Reply-To: <20170512102825.GA10929@red-moon> Content-Type: text/plain; charset="utf-8" Content-Language: en-US Content-Transfer-Encoding: 7bit X-Originating-IP: [10.46.39.99] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A0B0208.5922DA92.0192,ss=1,re=0.000,recu=0.000,reip=0.000,cl=1,cld=1,fgs=0, ip=0.0.0.0, so=2014-11-16 11:51:01, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: edefd9affe32ea975aef700cb52c4038 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Lorenzo, Thanks for your review and sorry for the late reply. On 2017/5/12 18:28, Lorenzo Pieralisi wrote: > On Fri, May 12, 2017 at 05:57:47PM +0800, Timmy Li wrote: >> There are some memory allocations in pci_acpi_scan_root(). But >> ri, root_ops and ri->cfg are not freed properly in failure cases, >> which results in memory leaks. This patch fixes the potential >> memory leaks. >> >> Signed-off-by: Timmy Li >> --- >> arch/arm64/kernel/pci.c | 22 ++++++++++++++-------- >> 1 file changed, 14 insertions(+), 8 deletions(-) >> >> diff --git a/arch/arm64/kernel/pci.c b/arch/arm64/kernel/pci.c >> index 4f0e3eb..e7e88ce 100644 >> --- a/arch/arm64/kernel/pci.c >> +++ b/arch/arm64/kernel/pci.c >> @@ -188,25 +188,22 @@ struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root) >> >> ri = kzalloc_node(sizeof(*ri), GFP_KERNEL, node); >> if (!ri) >> - return NULL; >> + goto err_allocri; >> >> root_ops = kzalloc_node(sizeof(*root_ops), GFP_KERNEL, node); >> if (!root_ops) >> - return NULL; >> + goto err_allocops; I think it is still needed to free ri here When ri is allocated successfully and root_ops is not. Considering this is the only remain place that need to fix, a simple way to do this would be: if (!root_ops) { + kfree(ri); return NULL; } >> >> ri->cfg = pci_acpi_setup_ecam_mapping(root); >> - if (!ri->cfg) { >> - kfree(ri); >> - kfree(root_ops); >> - return NULL; >> - } >> + if (!ri->cfg) >> + goto err_ecam; >> >> root_ops->release_info = pci_acpi_generic_release_info; > > You are missing this ^^^^^^^^^ > >> root_ops->prepare_resources = pci_acpi_root_prepare_resources; >> root_ops->pci_ops = &ri->cfg->ops->pci_ops; >> bus = acpi_pci_root_create(root, root_ops, &ri->common, ri->cfg); >> if (!bus) > > And how it works if (bus == NULL) here. You are correct. There is no need to do anything When acpi_pci_root_create() fails. > > Lorenzo > >> - return NULL; >> + goto err_rootcreate; >> >> pci_bus_size_bridges(bus); >> pci_bus_assign_resources(bus); >> @@ -215,6 +212,15 @@ struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root) >> pcie_bus_configure_settings(child); >> >> return bus; >> + >> +err_rootcreate: >> + pci_ecam_free(ri->cfg); >> +err_ecam: >> + kfree(root_ops); >> +err_allocops: >> + kfree(ri); >> +err_allocri: >> + return NULL; >> } >> >> void pcibios_add_bus(struct pci_bus *bus) >> -- >> 1.9.1 >> >> > > . >