diff mbox

PCI: Fix error cleanup paths in devm_of_pci_get_host_bridge_resources()

Message ID 294ff38c-f8df-f89b-3bac-604e79fe6cfe@siemens.com (mailing list archive)
State New, archived
Delegated to: Bjorn Helgaas
Headers show

Commit Message

Jan Kiszka May 13, 2018, 6:22 p.m. UTC
Fallouts from the conversion to devm_kzalloc: In case the function
fails, we no longer need to clean up managed allocations. In fact, we
must not. Only one case requires explicit freeing, and that is when
of_pci_range_to_resource() fails and we simply skip over the related
resource entry.

Reported-by: Julia Lawall <julia.lawall@lip6.fr>
Fixes: 07adab611304 ("PCI: Add dev parameter to __of_pci_get_host_bridge_resources()")
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
---
 drivers/pci/of.c | 22 +++++-----------------
 1 file changed, 5 insertions(+), 17 deletions(-)
diff mbox

Patch

diff --git a/drivers/pci/of.c b/drivers/pci/of.c
index 38469ffd1d7e..ad8cc1d677f3 100644
--- a/drivers/pci/of.c
+++ b/drivers/pci/of.c
@@ -266,7 +266,6 @@  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_entry *window;
 	struct resource *res;
 	struct resource *bus_range;
 	struct of_pci_range range;
@@ -299,7 +298,7 @@  int devm_of_pci_get_host_bridge_resources(struct device *dev,
 	/* Check for ranges property */
 	err = of_pci_range_parser_init(&parser, dev_node);
 	if (err)
-		goto parse_failed;
+		return err;
 
 	dev_dbg(dev, "Parsing ranges property...\n");
 	for_each_of_pci_range(&parser, &range) {
@@ -322,14 +321,12 @@  int devm_of_pci_get_host_bridge_resources(struct device *dev,
 			continue;
 
 		res = devm_kzalloc(dev, sizeof(struct resource), GFP_KERNEL);
-		if (!res) {
-			err = -ENOMEM;
-			goto parse_failed;
-		}
+		if (!res)
+			return -ENOMEM;
 
 		err = of_pci_range_to_resource(&range, dev_node, res);
 		if (err) {
-			kfree(res);
+			devm_kfree(dev, res);
 			continue;
 		}
 
@@ -338,8 +335,7 @@  int devm_of_pci_get_host_bridge_resources(struct device *dev,
 				dev_err(dev,
 					"I/O range found for %pOF. Please provide an io_base pointer to save CPU base address\n",
 					dev_node);
-				err = -EINVAL;
-				goto conversion_failed;
+				return -EINVAL;
 			}
 			if (*io_base != (resource_size_t)OF_BAD_ADDR)
 				dev_warn(dev,
@@ -352,14 +348,6 @@  int devm_of_pci_get_host_bridge_resources(struct device *dev,
 	}
 
 	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;
 }
 EXPORT_SYMBOL_GPL(devm_of_pci_get_host_bridge_resources);
 #endif /* CONFIG_OF_ADDRESS */