Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1607239 > unrolled thread

[PATCH v2 0/2] Fix memory leak in of_pci_get_host_bridge_resources

Started byJeffy Chen <jeffy.chen@rock-chips.com>
First post2017-03-23 09:20 +0100
Last post2017-03-23 10:10 +0100
Articles 7 — 5 participants

Back to article view | Back to linux.kernel


Contents

  [PATCH v2 0/2] Fix memory leak in of_pci_get_host_bridge_resources Jeffy Chen <jeffy.chen@rock-chips.com> - 2017-03-23 09:20 +0100
    [PATCH v2 2/2] of/pci: Fix memory leak in of_pci_get_host_bridge_resources Jeffy Chen <jeffy.chen@rock-chips.com> - 2017-03-23 09:20 +0100
      Re: [PATCH v2 2/2] of/pci: Fix memory leak in of_pci_get_host_bridge_resources Rob Herring <robh@kernel.org> - 2017-03-23 23:10 +0100
        Re: [PATCH v2 2/2] of/pci: Fix memory leak in of_pci_get_host_bridge_resources Dmitry Torokhov <dtor@chromium.org> - 2017-03-24 00:00 +0100
          Re: [PATCH v2 2/2] of/pci: Fix memory leak in of_pci_get_host_bridge_resources jeffy <jeffy.chen@rock-chips.com> - 2017-03-24 02:50 +0100
    [PATCH v2 1/2] PCI: return resource_entry in pci_add_resource helpers Jeffy Chen <jeffy.chen@rock-chips.com> - 2017-03-23 09:20 +0100
    Re: [PATCH v2 0/2] Fix memory leak in  of_pci_get_host_bridge_resources Shawn Lin <shawn.lin@rock-chips.com> - 2017-03-23 10:10 +0100

#1607239 — [PATCH v2 0/2] Fix memory leak in of_pci_get_host_bridge_resources

FromJeffy Chen <jeffy.chen@rock-chips.com>
Date2017-03-23 09:20 +0100
Subject[PATCH v2 0/2] Fix memory leak in of_pci_get_host_bridge_resources
Message-ID<to68p-7Yi-1@gated-at.bofh.it>
In of_pci_get_host_bridge_resources, we alloced some struct resource
variables, and they would cause memory leak since no where to free them.

Changes in v2:
Don't change the resource_list_create_entry's behavior.

Jeffy Chen (2):
  PCI: return resource_entry in pci_add_resource helpers
  of/pci: Fix memory leak in of_pci_get_host_bridge_resources

 drivers/of/of_pci.c | 57 +++++++++++++++++++++++------------------------------
 drivers/pci/bus.c   | 13 +++++++-----
 include/linux/pci.h |  8 +++++---
 3 files changed, 38 insertions(+), 40 deletions(-)

-- 
2.1.4

[toc] | [next] | [standalone]


#1607240 — [PATCH v2 2/2] of/pci: Fix memory leak in of_pci_get_host_bridge_resources

FromJeffy Chen <jeffy.chen@rock-chips.com>
Date2017-03-23 09:20 +0100
Subject[PATCH v2 2/2] of/pci: Fix memory leak in of_pci_get_host_bridge_resources
Message-ID<to68p-7Yi-3@gated-at.bofh.it>
In reply to#1607239
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 <jeffy.chen@rock-chips.com>
---

Changes in v2:
Don't change the resource_list_create_entry's behavior.

 drivers/of/of_pci.c | 57 +++++++++++++++++++++++------------------------------
 1 file changed, 25 insertions(+), 32 deletions(-)

diff --git a/drivers/of/of_pci.c b/drivers/of/of_pci.c
index 0ee42c3..a0ec246 100644
--- a/drivers/of/of_pci.c
+++ b/drivers/of/of_pci.c
@@ -190,8 +190,7 @@ int of_pci_get_host_bridge_resources(struct device_node *dev,
 			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 +199,24 @@ 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;
+	}
+	window = pci_add_resource(resources, NULL);
+	if (!window) {
+		err = -ENOMEM;
+		goto parse_failed;
 	}
-	pci_add_resource(resources, bus_range);
+	*window->res = res;
 
 	/* Check for ranges property */
 	err = of_pci_range_parser_init(&parser, dev);
@@ -244,24 +243,16 @@ int of_pci_get_host_bridge_resources(struct device_node *dev,
 		if (range.cpu_addr == OF_BAD_ADDR || range.size == 0)
 			continue;
 
-		res = kzalloc(sizeof(struct resource), GFP_KERNEL);
-		if (!res) {
-			err = -ENOMEM;
-			goto parse_failed;
-		}
-
-		err = of_pci_range_to_resource(&range, dev, res);
-		if (err) {
-			kfree(res);
+		err = of_pci_range_to_resource(&range, dev, &res);
+		if (err)
 			continue;
-		}
 
-		if (resource_type(res) == IORESOURCE_IO) {
+		if (resource_type(&res) == IORESOURCE_IO) {
 			if (!io_base) {
 				pr_err("I/O range found for %s. Please provide an io_base pointer to save CPU base address\n",
 					dev->full_name);
 				err = -EINVAL;
-				goto conversion_failed;
+				goto parse_failed;
 			}
 			if (*io_base != (resource_size_t)OF_BAD_ADDR)
 				pr_warn("More than one I/O resource converted for %s. CPU base address for old range lost!\n",
@@ -269,16 +260,18 @@ int of_pci_get_host_bridge_resources(struct device_node *dev,
 			*io_base = range.cpu_addr;
 		}
 
-		pci_add_resource_offset(resources, res,	res->start - range.pci_addr);
+		window = pci_add_resource(resources, NULL);
+		if (!window) {
+			err = -ENOMEM;
+			goto parse_failed;
+		}
+		*window->res = res;
+		window->offset = res.start - range.pci_addr;
 	}
 
 	return 0;
 
-conversion_failed:
-	kfree(res);
 parse_failed:
-	resource_list_for_each_entry(window, resources)
-		kfree(window->res);
 	pci_free_resource_list(resources);
 	return err;
 }
-- 
2.1.4

[toc] | [prev] | [next] | [standalone]


#1607970 — Re: [PATCH v2 2/2] of/pci: Fix memory leak in of_pci_get_host_bridge_resources

FromRob Herring <robh@kernel.org>
Date2017-03-23 23:10 +0100
SubjectRe: [PATCH v2 2/2] of/pci: Fix memory leak in of_pci_get_host_bridge_resources
Message-ID<toj5D-ic-9@gated-at.bofh.it>
In reply to#1607240
On Thu, Mar 23, 2017 at 3:12 AM, Jeffy Chen <jeffy.chen@rock-chips.com> 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 <jeffy.chen@rock-chips.com>
> ---
>
> Changes in v2:
> Don't change the resource_list_create_entry's behavior.
>
>  drivers/of/of_pci.c | 57 +++++++++++++++++++++++------------------------------
>  1 file changed, 25 insertions(+), 32 deletions(-)
>
> diff --git a/drivers/of/of_pci.c b/drivers/of/of_pci.c
> index 0ee42c3..a0ec246 100644
> --- a/drivers/of/of_pci.c
> +++ b/drivers/of/of_pci.c
> @@ -190,8 +190,7 @@ int of_pci_get_host_bridge_resources(struct device_node *dev,
>                         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 +199,24 @@ 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;
> +       }
> +       window = pci_add_resource(resources, NULL);
> +       if (!window) {
> +               err = -ENOMEM;
> +               goto parse_failed;
>         }
> -       pci_add_resource(resources, bus_range);
> +       *window->res = res;

Well, now this seems racy. You add a blank resource to the list first
and then fill it in.

Rob

[toc] | [prev] | [next] | [standalone]


#1608005 — Re: [PATCH v2 2/2] of/pci: Fix memory leak in of_pci_get_host_bridge_resources

FromDmitry Torokhov <dtor@chromium.org>
Date2017-03-24 00:00 +0100
SubjectRe: [PATCH v2 2/2] of/pci: Fix memory leak in of_pci_get_host_bridge_resources
Message-ID<tojS1-ER-11@gated-at.bofh.it>
In reply to#1607970
On Thu, Mar 23, 2017 at 3:07 PM, Rob Herring <robh@kernel.org> wrote:
> On Thu, Mar 23, 2017 at 3:12 AM, Jeffy Chen <jeffy.chen@rock-chips.com> 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 <jeffy.chen@rock-chips.com>
>> ---
>>
>> Changes in v2:
>> Don't change the resource_list_create_entry's behavior.
>>
>>  drivers/of/of_pci.c | 57 +++++++++++++++++++++++------------------------------
>>  1 file changed, 25 insertions(+), 32 deletions(-)
>>
>> diff --git a/drivers/of/of_pci.c b/drivers/of/of_pci.c
>> index 0ee42c3..a0ec246 100644
>> --- a/drivers/of/of_pci.c
>> +++ b/drivers/of/of_pci.c
>> @@ -190,8 +190,7 @@ int of_pci_get_host_bridge_resources(struct device_node *dev,
>>                         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 +199,24 @@ 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;
>> +       }
>> +       window = pci_add_resource(resources, NULL);
>> +       if (!window) {
>> +               err = -ENOMEM;
>> +               goto parse_failed;
>>         }
>> -       pci_add_resource(resources, bus_range);
>> +       *window->res = res;
>
> Well, now this seems racy. You add a blank resource to the list first
> and then fill it in.
>

Huh? There is absolutely no guarantees for concurrent access here.
pcI_add_resource_offset() first adds a resource and then modifies
offset. Here we add an empty resource and then fill it in.

Thanks.

-- 
Dmitry

[toc] | [prev] | [next] | [standalone]


#1608066 — Re: [PATCH v2 2/2] of/pci: Fix memory leak in of_pci_get_host_bridge_resources

Fromjeffy <jeffy.chen@rock-chips.com>
Date2017-03-24 02:50 +0100
SubjectRe: [PATCH v2 2/2] of/pci: Fix memory leak in of_pci_get_host_bridge_resources
Message-ID<tomwx-2zj-7@gated-at.bofh.it>
In reply to#1608005
Hi Rob & Dmitry,

On 03/24/2017 06:58 AM, Dmitry Torokhov wrote:
> On Thu, Mar 23, 2017 at 3:07 PM, Rob Herring <robh@kernel.org> wrote:
>> On Thu, Mar 23, 2017 at 3:12 AM, Jeffy Chen <jeffy.chen@rock-chips.com> 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 <jeffy.chen@rock-chips.com>
>>> ---
>>>
>>> Changes in v2:
>>> Don't change the resource_list_create_entry's behavior.
>>>
>>>   drivers/of/of_pci.c | 57 +++++++++++++++++++++++------------------------------
>>>   1 file changed, 25 insertions(+), 32 deletions(-)
>>>
>>> diff --git a/drivers/of/of_pci.c b/drivers/of/of_pci.c
>>> index 0ee42c3..a0ec246 100644
>>> --- a/drivers/of/of_pci.c
>>> +++ b/drivers/of/of_pci.c
>>> @@ -190,8 +190,7 @@ int of_pci_get_host_bridge_resources(struct device_node *dev,
>>>                          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 +199,24 @@ 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;
>>> +       }
>>> +       window = pci_add_resource(resources, NULL);
>>> +       if (!window) {
>>> +               err = -ENOMEM;
>>> +               goto parse_failed;
>>>          }
>>> -       pci_add_resource(resources, bus_range);
>>> +       *window->res = res;
>>
>> Well, now this seems racy. You add a blank resource to the list first
>> and then fill it in.
>>
>
> Huh? There is absolutely no guarantees for concurrent access here.
> pcI_add_resource_offset() first adds a resource and then modifies
> offset. Here we add an empty resource and then fill it in.
currently, we are using of_pci_get_host_bridge_resources in this pattern:

create resource list:
         LIST_HEAD(res);
...
add resources into the list:
         err = of_pci_get_host_bridge_resources(dev->of_node, 0, 0xff,
                                                &res, &io_base);
...
walk over the list:
         /* Get the I/O and memory ranges from DT */
         resource_list_for_each_entry(win, &res) {

so only of_pci_get_host_bridge_resources is accessing this list at that 
time.

and an empty resource is harmless i think(with zero size and flags) ;)

maybe i should add some comments in the patch

>
> Thanks.
>

[toc] | [prev] | [next] | [standalone]


#1607243 — [PATCH v2 1/2] PCI: return resource_entry in pci_add_resource helpers

FromJeffy Chen <jeffy.chen@rock-chips.com>
Date2017-03-23 09:20 +0100
Subject[PATCH v2 1/2] PCI: return resource_entry in pci_add_resource helpers
Message-ID<to68p-7Yi-15@gated-at.bofh.it>
In reply to#1607239
Signed-off-by: Jeffy Chen <jeffy.chen@rock-chips.com>
---

Changes in v2: None

 drivers/pci/bus.c   | 13 ++++++++-----
 include/linux/pci.h |  8 +++++---
 2 files changed, 13 insertions(+), 8 deletions(-)

diff --git a/drivers/pci/bus.c b/drivers/pci/bus.c
index bc56cf1..36a1861 100644
--- a/drivers/pci/bus.c
+++ b/drivers/pci/bus.c
@@ -17,25 +17,28 @@
 
 #include "pci.h"
 
-void pci_add_resource_offset(struct list_head *resources, struct resource *res,
-			     resource_size_t offset)
+struct resource_entry *pci_add_resource_offset(struct list_head *resources,
+					       struct resource *res,
+					       resource_size_t offset)
 {
 	struct resource_entry *entry;
 
 	entry = resource_list_create_entry(res, 0);
 	if (!entry) {
 		printk(KERN_ERR "PCI: can't add host bridge window %pR\n", res);
-		return;
+		return NULL;
 	}
 
 	entry->offset = offset;
 	resource_list_add_tail(entry, resources);
+	return entry;
 }
 EXPORT_SYMBOL(pci_add_resource_offset);
 
-void pci_add_resource(struct list_head *resources, struct resource *res)
+struct resource_entry *pci_add_resource(struct list_head *resources,
+					struct resource *res)
 {
-	pci_add_resource_offset(resources, res, 0);
+	return pci_add_resource_offset(resources, res, 0);
 }
 EXPORT_SYMBOL(pci_add_resource);
 
diff --git a/include/linux/pci.h b/include/linux/pci.h
index eb3da1a..ab16abe 100644
--- a/include/linux/pci.h
+++ b/include/linux/pci.h
@@ -1167,9 +1167,11 @@ void pci_release_selected_regions(struct pci_dev *, int);
 /* drivers/pci/bus.c */
 struct pci_bus *pci_bus_get(struct pci_bus *bus);
 void pci_bus_put(struct pci_bus *bus);
-void pci_add_resource(struct list_head *resources, struct resource *res);
-void pci_add_resource_offset(struct list_head *resources, struct resource *res,
-			     resource_size_t offset);
+struct resource_entry *pci_add_resource(struct list_head *resources,
+					struct resource *res);
+struct resource_entry *pci_add_resource_offset(struct list_head *resources,
+					       struct resource *res,
+					       resource_size_t offset);
 void pci_free_resource_list(struct list_head *resources);
 void pci_bus_add_resource(struct pci_bus *bus, struct resource *res,
 			  unsigned int flags);
-- 
2.1.4

[toc] | [prev] | [next] | [standalone]


#1607276 — Re: [PATCH v2 0/2] Fix memory leak in of_pci_get_host_bridge_resources

FromShawn Lin <shawn.lin@rock-chips.com>
Date2017-03-23 10:10 +0100
SubjectRe: [PATCH v2 0/2] Fix memory leak in of_pci_get_host_bridge_resources
Message-ID<to6UO-89-23@gated-at.bofh.it>
In reply to#1607239
Hi Jeffy,

On 2017/3/23 16:12, Jeffy Chen wrote:
> In of_pci_get_host_bridge_resources, we alloced some struct resource
> variables, and they would cause memory leak since no where to free them.
>

Tested-by: Shawn Lin <shawn.lin@rock-chips.com>

> Changes in v2:
> Don't change the resource_list_create_entry's behavior.
>
> Jeffy Chen (2):
>   PCI: return resource_entry in pci_add_resource helpers
>   of/pci: Fix memory leak in of_pci_get_host_bridge_resources
>
>  drivers/of/of_pci.c | 57 +++++++++++++++++++++++------------------------------
>  drivers/pci/bus.c   | 13 +++++++-----
>  include/linux/pci.h |  8 +++++---
>  3 files changed, 38 insertions(+), 40 deletions(-)
>

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web