From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754113AbdCWIXH (ORCPT ); Thu, 23 Mar 2017 04:23:07 -0400 Received: from regular1.263xmail.com ([211.150.99.131]:51717 "EHLO regular1.263xmail.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751410AbdCWIXF (ORCPT ); Thu, 23 Mar 2017 04:23:05 -0400 X-263anti-spam: KSV:0; X-MAIL-GRAY: 0 X-MAIL-DELIVERY: 1 X-KSVirus-check: 0 X-ABS-CHECKED: 4 X-RL-SENDER: jeffy.chen@rock-chips.com X-FST-TO: robh@kernel.org X-SENDER-IP: 103.29.142.67 X-LOGIN-NAME: jeffy.chen@rock-chips.com X-UNIQUE-TAG: <89401ddf3b219c823a3f90db6b0b69f3> X-ATTACHMENT-NUM: 0 X-DNS-TYPE: 0 Message-ID: <58D385BB.8010502@rock-chips.com> Date: Thu, 23 Mar 2017 16:22:19 +0800 From: jeffy User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:19.0) Gecko/20130126 Thunderbird/19.0 MIME-Version: 1.0 To: Rob Herring CC: "linux-kernel@vger.kernel.org" , toshi.kani@hpe.com, Shawn Lin , Brian Norris , Doug Anderson , "bhelgaas@google.com" , Dmitry Torokhov , Frank Rowand , "devicetree@vger.kernel.org" Subject: Re: [PATCH 2/2] of/pci: Fix memory leak in of_pci_get_host_bridge_resources References: <1490149546-4062-1-git-send-email-jeffy.chen@rock-chips.com> <1490149546-4062-2-git-send-email-jeffy.chen@rock-chips.com> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Rob, On 03/22/2017 10:55 PM, Rob Herring wrote: > On Wed, Mar 22, 2017 at 9:39 AM, Rob Herring wrote: >> On Tue, Mar 21, 2017 at 9:25 PM, Jeffy Chen wrote: >>> Currently we only free the allocated resource struct when error. >>> This would cause memory leak after pci_free_resource_list. >>> >>> Signed-off-by: Jeffy Chen >>> --- >>> >>> drivers/of/of_pci.c | 48 +++++++++++++++--------------------------------- >>> 1 file changed, 15 insertions(+), 33 deletions(-) >>> >>> diff --git a/drivers/of/of_pci.c b/drivers/of/of_pci.c >>> index 0ee42c3..269393bc 100644 >>> --- a/drivers/of/of_pci.c >>> +++ b/drivers/of/of_pci.c >>> @@ -189,9 +189,7 @@ int of_pci_get_host_bridge_resources(struct device_node *dev, >>> unsigned char busno, unsigned char bus_max, >>> struct list_head *resources, resource_size_t *io_base) >>> { >>> - struct resource_entry *window; >>> - struct resource *res; >>> - struct resource *bus_range; >>> + struct resource res; >>> struct of_pci_range range; >>> struct of_pci_range_parser parser; >>> char range_type[4]; >>> @@ -200,24 +198,19 @@ int of_pci_get_host_bridge_resources(struct device_node *dev, >>> if (io_base) >>> *io_base = (resource_size_t)OF_BAD_ADDR; >>> >>> - bus_range = kzalloc(sizeof(*bus_range), GFP_KERNEL); >>> - if (!bus_range) >>> - return -ENOMEM; >>> - >>> pr_info("host bridge %s ranges:\n", dev->full_name); >>> >>> - err = of_pci_parse_bus_range(dev, bus_range); >>> + err = of_pci_parse_bus_range(dev, &res); >>> if (err) { >>> - bus_range->start = busno; >>> - bus_range->end = bus_max; >>> - bus_range->flags = IORESOURCE_BUS; >>> - pr_info(" No bus range found for %s, using %pR\n", >>> - dev->full_name, bus_range); >>> + res.start = busno; >>> + res.end = bus_max; >>> + res.flags = IORESOURCE_BUS; >>> + pr_info(" No bus range found for %s\n", dev->full_name); >>> } else { >>> - if (bus_range->end > bus_range->start + bus_max) >>> - bus_range->end = bus_range->start + bus_max; >>> + if (res.end > res.start + bus_max) >>> + res.end = res.start + bus_max; >>> } >>> - pci_add_resource(resources, bus_range); >>> + pci_add_resource(resources, &res); >> >> You are passing a stack variable to pci_add_resource and it doesn't >> make a copy of it. I assume the resource needs to live after you exit >> this function. > > Ah, found your 1st patch changing the behavior. I'm surprised that > change works without affecting anyone else. sorry, i should add a cover-letter first. and you're right, that would affect others(for example the ioport_resource/iomem_resource). > >> If we have a leak, can't you just add a free in the correct spot? That >> would be a lot easier to review. it seems hard to tell which resources in the list need to be freed after all... >> >> Rob > > > From mboxrd@z Thu Jan 1 00:00:00 1970 From: jeffy Subject: Re: [PATCH 2/2] of/pci: Fix memory leak in of_pci_get_host_bridge_resources Date: Thu, 23 Mar 2017 16:22:19 +0800 Message-ID: <58D385BB.8010502@rock-chips.com> References: <1490149546-4062-1-git-send-email-jeffy.chen@rock-chips.com> <1490149546-4062-2-git-send-email-jeffy.chen@rock-chips.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Return-path: In-Reply-To: Sender: devicetree-owner-u79uwXL29TY76Z2rM5mHXA@public.gmane.org To: Rob Herring Cc: "linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org" , toshi.kani-ZPxbGqLxI0U@public.gmane.org, Shawn Lin , Brian Norris , Doug Anderson , "bhelgaas-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org" , Dmitry Torokhov , Frank Rowand , "devicetree-u79uwXL29TY76Z2rM5mHXA@public.gmane.org" List-Id: devicetree@vger.kernel.org Hi Rob, On 03/22/2017 10:55 PM, Rob Herring wrote: > On Wed, Mar 22, 2017 at 9:39 AM, Rob Herring wrote: >> On Tue, Mar 21, 2017 at 9:25 PM, Jeffy Chen wrote: >>> Currently we only free the allocated resource struct when error. >>> This would cause memory leak after pci_free_resource_list. >>> >>> Signed-off-by: Jeffy Chen >>> --- >>> >>> drivers/of/of_pci.c | 48 +++++++++++++++--------------------------------- >>> 1 file changed, 15 insertions(+), 33 deletions(-) >>> >>> diff --git a/drivers/of/of_pci.c b/drivers/of/of_pci.c >>> index 0ee42c3..269393bc 100644 >>> --- a/drivers/of/of_pci.c >>> +++ b/drivers/of/of_pci.c >>> @@ -189,9 +189,7 @@ int of_pci_get_host_bridge_resources(struct device_node *dev, >>> unsigned char busno, unsigned char bus_max, >>> struct list_head *resources, resource_size_t *io_base) >>> { >>> - struct resource_entry *window; >>> - struct resource *res; >>> - struct resource *bus_range; >>> + struct resource res; >>> struct of_pci_range range; >>> struct of_pci_range_parser parser; >>> char range_type[4]; >>> @@ -200,24 +198,19 @@ int of_pci_get_host_bridge_resources(struct device_node *dev, >>> if (io_base) >>> *io_base = (resource_size_t)OF_BAD_ADDR; >>> >>> - bus_range = kzalloc(sizeof(*bus_range), GFP_KERNEL); >>> - if (!bus_range) >>> - return -ENOMEM; >>> - >>> pr_info("host bridge %s ranges:\n", dev->full_name); >>> >>> - err = of_pci_parse_bus_range(dev, bus_range); >>> + err = of_pci_parse_bus_range(dev, &res); >>> if (err) { >>> - bus_range->start = busno; >>> - bus_range->end = bus_max; >>> - bus_range->flags = IORESOURCE_BUS; >>> - pr_info(" No bus range found for %s, using %pR\n", >>> - dev->full_name, bus_range); >>> + res.start = busno; >>> + res.end = bus_max; >>> + res.flags = IORESOURCE_BUS; >>> + pr_info(" No bus range found for %s\n", dev->full_name); >>> } else { >>> - if (bus_range->end > bus_range->start + bus_max) >>> - bus_range->end = bus_range->start + bus_max; >>> + if (res.end > res.start + bus_max) >>> + res.end = res.start + bus_max; >>> } >>> - pci_add_resource(resources, bus_range); >>> + pci_add_resource(resources, &res); >> >> You are passing a stack variable to pci_add_resource and it doesn't >> make a copy of it. I assume the resource needs to live after you exit >> this function. > > Ah, found your 1st patch changing the behavior. I'm surprised that > change works without affecting anyone else. sorry, i should add a cover-letter first. and you're right, that would affect others(for example the ioport_resource/iomem_resource). > >> If we have a leak, can't you just add a free in the correct spot? That >> would be a lot easier to review. it seems hard to tell which resources in the list need to be freed after all... >> >> Rob > > > -- To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org More majordomo info at http://vger.kernel.org/majordomo-info.html