diff mbox

PCI: Clean up resource allocation in devm_of_pci_get_host_bridge_resources()

Message ID f35b2972-aa40-5397-facb-765cafe7c315@siemens.com (mailing list archive)
State New, archived
Headers show

Commit Message

Jan Kiszka May 16, 2018, 12:31 p.m. UTC
Instead of first allocating and then freeing memory for struct resource
in case we cannot parse a PCI resource from the device tree, work
against a local struct and kmemdup it when we decide to go with it.

Suggested-by: Andy Shevchenko <andy.shevchenko@gmail.com>
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
---
 drivers/pci/of.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)

Comments

Vladimir Zapolskiy May 17, 2018, 6:23 a.m. UTC | #1
On 05/16/2018 03:31 PM, Jan Kiszka wrote:
> Instead of first allocating and then freeing memory for struct resource
> in case we cannot parse a PCI resource from the device tree, work
> against a local struct and kmemdup it when we decide to go with it.
> 
> Suggested-by: Andy Shevchenko <andy.shevchenko@gmail.com>
> Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
> ---
>  drivers/pci/of.c | 14 ++++++--------
>  1 file changed, 6 insertions(+), 8 deletions(-)
> 
> diff --git a/drivers/pci/of.c b/drivers/pci/of.c
> index b06585a1da75..fc0f906c5c25 100644
> --- a/drivers/pci/of.c
> +++ b/drivers/pci/of.c
> @@ -266,7 +266,7 @@ int devm_of_pci_get_host_bridge_resources(struct device *dev,
>  			struct list_head *resources, resource_size_t *io_base)
>  {
>  	struct device_node *dev_node = dev->of_node;
> -	struct resource *res;
> +	struct resource *res, tmp_res;
>  	struct resource *bus_range;
>  	struct of_pci_range range;
>  	struct of_pci_range_parser parser;
> @@ -320,18 +320,16 @@ int devm_of_pci_get_host_bridge_resources(struct device *dev,
>  		if (range.cpu_addr == OF_BAD_ADDR || range.size == 0)
>  			continue;
>  
> -		res = devm_kzalloc(dev, sizeof(struct resource), GFP_KERNEL);
> +		err = of_pci_range_to_resource(&range, dev_node, &tmp_res);
> +		if (err)
> +			continue;
> +
> +		res = devm_kmemdup(dev, &tmp_res, sizeof(tmp_res), GFP_KERNEL);

The change looks okay, apparently probable garbage in tmp_res.desc has no impact.

Reviewed-by: Vladimir Zapolskiy <vladimir_zapolskiy@mentor.com>

--
With best wishes,
Vladimir
Andy Shevchenko June 5, 2018, 4:26 p.m. UTC | #2
On Wed, May 16, 2018 at 3:31 PM, Jan Kiszka <jan.kiszka@siemens.com> wrote:
> Instead of first allocating and then freeing memory for struct resource
> in case we cannot parse a PCI resource from the device tree, work
> against a local struct and kmemdup it when we decide to go with it.
>

Bjorn, any comments on this?

> Suggested-by: Andy Shevchenko <andy.shevchenko@gmail.com>
> Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
> ---
>  drivers/pci/of.c | 14 ++++++--------
>  1 file changed, 6 insertions(+), 8 deletions(-)
>
> diff --git a/drivers/pci/of.c b/drivers/pci/of.c
> index b06585a1da75..fc0f906c5c25 100644
> --- a/drivers/pci/of.c
> +++ b/drivers/pci/of.c
> @@ -266,7 +266,7 @@ int devm_of_pci_get_host_bridge_resources(struct device *dev,
>                         struct list_head *resources, resource_size_t *io_base)
>  {
>         struct device_node *dev_node = dev->of_node;
> -       struct resource *res;
> +       struct resource *res, tmp_res;
>         struct resource *bus_range;
>         struct of_pci_range range;
>         struct of_pci_range_parser parser;
> @@ -320,18 +320,16 @@ int devm_of_pci_get_host_bridge_resources(struct device *dev,
>                 if (range.cpu_addr == OF_BAD_ADDR || range.size == 0)
>                         continue;
>
> -               res = devm_kzalloc(dev, sizeof(struct resource), GFP_KERNEL);
> +               err = of_pci_range_to_resource(&range, dev_node, &tmp_res);
> +               if (err)
> +                       continue;
> +
> +               res = devm_kmemdup(dev, &tmp_res, sizeof(tmp_res), GFP_KERNEL);
>                 if (!res) {
>                         err = -ENOMEM;
>                         goto failed;
>                 }
>
> -               err = of_pci_range_to_resource(&range, dev_node, res);
> -               if (err) {
> -                       devm_kfree(dev, res);
> -                       continue;
> -               }
> -
>                 if (resource_type(res) == IORESOURCE_IO) {
>                         if (!io_base) {
>                                 dev_err(dev,
> --
> 2.13.6
Bjorn Helgaas June 5, 2018, 8:44 p.m. UTC | #3
On Tue, Jun 05, 2018 at 07:26:57PM +0300, Andy Shevchenko wrote:
> On Wed, May 16, 2018 at 3:31 PM, Jan Kiszka <jan.kiszka@siemens.com> wrote:
> > Instead of first allocating and then freeing memory for struct resource
> > in case we cannot parse a PCI resource from the device tree, work
> > against a local struct and kmemdup it when we decide to go with it.
> >
> 
> Bjorn, any comments on this?

I'm trying to finish up the v4.18 changes now, but I'll look at this later.
If it's urgent, let me know and I can look sooner.

> > Suggested-by: Andy Shevchenko <andy.shevchenko@gmail.com>
> > Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
> > ---
> >  drivers/pci/of.c | 14 ++++++--------
> >  1 file changed, 6 insertions(+), 8 deletions(-)
> >
> > diff --git a/drivers/pci/of.c b/drivers/pci/of.c
> > index b06585a1da75..fc0f906c5c25 100644
> > --- a/drivers/pci/of.c
> > +++ b/drivers/pci/of.c
> > @@ -266,7 +266,7 @@ int devm_of_pci_get_host_bridge_resources(struct device *dev,
> >                         struct list_head *resources, resource_size_t *io_base)
> >  {
> >         struct device_node *dev_node = dev->of_node;
> > -       struct resource *res;
> > +       struct resource *res, tmp_res;
> >         struct resource *bus_range;
> >         struct of_pci_range range;
> >         struct of_pci_range_parser parser;
> > @@ -320,18 +320,16 @@ int devm_of_pci_get_host_bridge_resources(struct device *dev,
> >                 if (range.cpu_addr == OF_BAD_ADDR || range.size == 0)
> >                         continue;
> >
> > -               res = devm_kzalloc(dev, sizeof(struct resource), GFP_KERNEL);
> > +               err = of_pci_range_to_resource(&range, dev_node, &tmp_res);
> > +               if (err)
> > +                       continue;
> > +
> > +               res = devm_kmemdup(dev, &tmp_res, sizeof(tmp_res), GFP_KERNEL);
> >                 if (!res) {
> >                         err = -ENOMEM;
> >                         goto failed;
> >                 }
> >
> > -               err = of_pci_range_to_resource(&range, dev_node, res);
> > -               if (err) {
> > -                       devm_kfree(dev, res);
> > -                       continue;
> > -               }
> > -
> >                 if (resource_type(res) == IORESOURCE_IO) {
> >                         if (!io_base) {
> >                                 dev_err(dev,
> > --
> > 2.13.6
> 
> 
> 
> -- 
> With Best Regards,
> Andy Shevchenko
Bjorn Helgaas June 29, 2018, 11:30 p.m. UTC | #4
On Wed, May 16, 2018 at 02:31:44PM +0200, Jan Kiszka wrote:
> Instead of first allocating and then freeing memory for struct resource
> in case we cannot parse a PCI resource from the device tree, work
> against a local struct and kmemdup it when we decide to go with it.
> 
> Suggested-by: Andy Shevchenko <andy.shevchenko@gmail.com>
> Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>

Applied with Vladimir's reviewed-by to pci/resource for v4.19, thanks!

> ---
>  drivers/pci/of.c | 14 ++++++--------
>  1 file changed, 6 insertions(+), 8 deletions(-)
> 
> diff --git a/drivers/pci/of.c b/drivers/pci/of.c
> index b06585a1da75..fc0f906c5c25 100644
> --- a/drivers/pci/of.c
> +++ b/drivers/pci/of.c
> @@ -266,7 +266,7 @@ int devm_of_pci_get_host_bridge_resources(struct device *dev,
>  			struct list_head *resources, resource_size_t *io_base)
>  {
>  	struct device_node *dev_node = dev->of_node;
> -	struct resource *res;
> +	struct resource *res, tmp_res;
>  	struct resource *bus_range;
>  	struct of_pci_range range;
>  	struct of_pci_range_parser parser;
> @@ -320,18 +320,16 @@ int devm_of_pci_get_host_bridge_resources(struct device *dev,
>  		if (range.cpu_addr == OF_BAD_ADDR || range.size == 0)
>  			continue;
>  
> -		res = devm_kzalloc(dev, sizeof(struct resource), GFP_KERNEL);
> +		err = of_pci_range_to_resource(&range, dev_node, &tmp_res);
> +		if (err)
> +			continue;
> +
> +		res = devm_kmemdup(dev, &tmp_res, sizeof(tmp_res), GFP_KERNEL);
>  		if (!res) {
>  			err = -ENOMEM;
>  			goto failed;
>  		}
>  
> -		err = of_pci_range_to_resource(&range, dev_node, res);
> -		if (err) {
> -			devm_kfree(dev, res);
> -			continue;
> -		}
> -
>  		if (resource_type(res) == IORESOURCE_IO) {
>  			if (!io_base) {
>  				dev_err(dev,
> -- 
> 2.13.6
diff mbox

Patch

diff --git a/drivers/pci/of.c b/drivers/pci/of.c
index b06585a1da75..fc0f906c5c25 100644
--- a/drivers/pci/of.c
+++ b/drivers/pci/of.c
@@ -266,7 +266,7 @@  int devm_of_pci_get_host_bridge_resources(struct device *dev,
 			struct list_head *resources, resource_size_t *io_base)
 {
 	struct device_node *dev_node = dev->of_node;
-	struct resource *res;
+	struct resource *res, tmp_res;
 	struct resource *bus_range;
 	struct of_pci_range range;
 	struct of_pci_range_parser parser;
@@ -320,18 +320,16 @@  int devm_of_pci_get_host_bridge_resources(struct device *dev,
 		if (range.cpu_addr == OF_BAD_ADDR || range.size == 0)
 			continue;
 
-		res = devm_kzalloc(dev, sizeof(struct resource), GFP_KERNEL);
+		err = of_pci_range_to_resource(&range, dev_node, &tmp_res);
+		if (err)
+			continue;
+
+		res = devm_kmemdup(dev, &tmp_res, sizeof(tmp_res), GFP_KERNEL);
 		if (!res) {
 			err = -ENOMEM;
 			goto failed;
 		}
 
-		err = of_pci_range_to_resource(&range, dev_node, res);
-		if (err) {
-			devm_kfree(dev, res);
-			continue;
-		}
-
 		if (resource_type(res) == IORESOURCE_IO) {
 			if (!io_base) {
 				dev_err(dev,