All of lore.kernel.org
 help / color / mirror / Atom feed
From: Rob Herring <robh@kernel.org>
To: Jeffy Chen <jeffy.chen@rock-chips.com>
Cc: "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 2/2] of/pci: Fix memory leak in of_pci_get_host_bridge_resources
Date: Wed, 22 Mar 2017 09:39:02 -0500	[thread overview]
Message-ID: <CAL_JsqL7eqJCKhsGuCtqoCXW=KpXrjka+4ZX4VCYDyFFY5RYFg@mail.gmail.com> (raw)
In-Reply-To: <1490149546-4062-2-git-send-email-jeffy.chen@rock-chips.com>

On Tue, Mar 21, 2017 at 9:25 PM, 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>
> ---
>
>  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.

If we have a leak, can't you just add a free in the correct spot? That
would be a lot easier to review.

Rob

WARNING: multiple messages have this Message-ID (diff)
From: Rob Herring <robh-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
To: Jeffy Chen <jeffy.chen-TNX95d0MmH7DzftRWevZcw@public.gmane.org>
Cc: "linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org"
	<linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org>,
	toshi.kani-ZPxbGqLxI0U@public.gmane.org,
	Shawn Lin <shawn.lin-TNX95d0MmH7DzftRWevZcw@public.gmane.org>,
	Brian Norris
	<briannorris-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org>,
	Doug Anderson <dianders-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org>,
	"bhelgaas-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org"
	<bhelgaas-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org>,
	Dmitry Torokhov <dtor-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org>,
	Frank Rowand
	<frowand.list-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>,
	"devicetree-u79uwXL29TY76Z2rM5mHXA@public.gmane.org"
	<devicetree-u79uwXL29TY76Z2rM5mHXA@public.gmane.org>
Subject: Re: [PATCH 2/2] of/pci: Fix memory leak in of_pci_get_host_bridge_resources
Date: Wed, 22 Mar 2017 09:39:02 -0500	[thread overview]
Message-ID: <CAL_JsqL7eqJCKhsGuCtqoCXW=KpXrjka+4ZX4VCYDyFFY5RYFg@mail.gmail.com> (raw)
In-Reply-To: <1490149546-4062-2-git-send-email-jeffy.chen-TNX95d0MmH7DzftRWevZcw@public.gmane.org>

On Tue, Mar 21, 2017 at 9:25 PM, Jeffy Chen <jeffy.chen-TNX95d0MmH7DzftRWevZcw@public.gmane.org> 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-TNX95d0MmH7DzftRWevZcw@public.gmane.org>
> ---
>
>  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.

If we have a leak, can't you just add a free in the correct spot? That
would be a lot easier to review.

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

  reply	other threads:[~2017-03-22 14:40 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-22  2:25 [PATCH 1/2] resource: Copy the whole res in resource_list_create_entry Jeffy Chen
2017-03-22  2:25 ` [PATCH 2/2] of/pci: Fix memory leak in of_pci_get_host_bridge_resources Jeffy Chen
2017-03-22 14:39   ` Rob Herring [this message]
2017-03-22 14:39     ` Rob Herring
2017-03-22 14:55     ` Rob Herring
2017-03-22 14:55       ` Rob Herring
2017-03-23  8:22       ` jeffy
2017-03-23  8:22         ` jeffy

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='CAL_JsqL7eqJCKhsGuCtqoCXW=KpXrjka+4ZX4VCYDyFFY5RYFg@mail.gmail.com' \
    --to=robh@kernel.org \
    --cc=bhelgaas@google.com \
    --cc=briannorris@chromium.org \
    --cc=devicetree@vger.kernel.org \
    --cc=dianders@chromium.org \
    --cc=dtor@chromium.org \
    --cc=frowand.list@gmail.com \
    --cc=jeffy.chen@rock-chips.com \
    --cc=linux-kernel@vger.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.