From: Dmitry Torokhov <dtor@chromium.org>
To: Rob Herring <robh@kernel.org>
Cc: Jeffy Chen <jeffy.chen@rock-chips.com>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
toshi.kani@hpe.com, Shawn Lin <shawn.lin@rock-chips.com>,
Brian Norris <briannorris@chromium.org>,
Doug Anderson <dianders@chromium.org>,
"bhelgaas@google.com" <bhelgaas@google.com>,
Dmitry Torokhov <dtor@chromium.org>,
Frank Rowand <frowand.list@gmail.com>,
"devicetree@vger.kernel.org" <devicetree@vger.kernel.org>
Subject: Re: [PATCH v2 2/2] of/pci: Fix memory leak in of_pci_get_host_bridge_resources
Date: Thu, 23 Mar 2017 15:58:56 -0700 [thread overview]
Message-ID: <CAE_wzQ9ZVDiDGP4k_2i2KL4JxtRn_S7gjFXUTYZncMLx2m77gQ@mail.gmail.com> (raw)
In-Reply-To: <CAL_Jsq+xxG6w6J+Zz-kcJ+rA9jJomyHFiazHzcPHuQizn-4-wg@mail.gmail.com>
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
next prev parent reply other threads:[~2017-03-23 22:58 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-03-23 8:12 [PATCH v2 0/2] Fix memory leak in of_pci_get_host_bridge_resources Jeffy Chen
2017-03-23 8:12 ` [PATCH v2 2/2] of/pci: " Jeffy Chen
2017-03-23 22:07 ` Rob Herring
2017-03-23 22:58 ` Dmitry Torokhov [this message]
[not found] ` <CAE_wzQ9ZVDiDGP4k_2i2KL4JxtRn_S7gjFXUTYZncMLx2m77gQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2017-03-24 1:39 ` jeffy
2017-04-04 19:18 ` Bjorn Helgaas
[not found] ` <CAErSpo6CZ1hoHyzEnWBi7VDr51bLurF4-4t=-v4jH27kOpzYrg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2017-04-05 2:22 ` jeffy
2017-04-05 13:21 ` Rob Herring
2017-03-23 9:00 ` [PATCH v2 0/2] " Shawn Lin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CAE_wzQ9ZVDiDGP4k_2i2KL4JxtRn_S7gjFXUTYZncMLx2m77gQ@mail.gmail.com \
--to=dtor@chromium.org \
--cc=bhelgaas@google.com \
--cc=briannorris@chromium.org \
--cc=devicetree@vger.kernel.org \
--cc=dianders@chromium.org \
--cc=frowand.list@gmail.com \
--cc=jeffy.chen@rock-chips.com \
--cc=linux-kernel@vger.kernel.org \
--cc=robh@kernel.org \
--cc=shawn.lin@rock-chips.com \
--cc=toshi.kani@hpe.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).