PCI/ACPI: Fix allocated memory release on error in pci_acpi_scan_root()
authorZhe Qiao <qiaozhe@iscas.ac.cn>
Wed, 30 Apr 2025 06:06:03 +0000 (14:06 +0800)
committerKrzysztof Wilczyński <kwilczynski@kernel.org>
Thu, 22 May 2025 11:00:03 +0000 (11:00 +0000)
In the pci_acpi_scan_root() function, when creating a PCI bus fails,
we need to free up the previously allocated memory, which can avoid
invalid memory usage and save resources.

Fixes: 789befdfa389 ("arm64: PCI: Migrate ACPI related functions to pci-acpi.c")
Signed-off-by: Zhe Qiao <qiaozhe@iscas.ac.cn>
[kwilczynski: commit log]
Signed-off-by: Krzysztof Wilczyński <kwilczynski@kernel.org>
Acked-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Link: https://lore.kernel.org/r/20250430060603.381504-1-qiaozhe@iscas.ac.cn
drivers/pci/pci-acpi.c

index af370628e58393aa0cbdf6d283b3afe33e5effb5..b78e0e4173244516bca77907aaeb1a0ee1c5d37a 100644 (file)
@@ -1676,24 +1676,19 @@ struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root)
                return NULL;
 
        root_ops = kzalloc(sizeof(*root_ops), GFP_KERNEL);
-       if (!root_ops) {
-               kfree(ri);
-               return NULL;
-       }
+       if (!root_ops)
+               goto free_ri;
 
        ri->cfg = pci_acpi_setup_ecam_mapping(root);
-       if (!ri->cfg) {
-               kfree(ri);
-               kfree(root_ops);
-               return NULL;
-       }
+       if (!ri->cfg)
+               goto free_root_ops;
 
        root_ops->release_info = pci_acpi_generic_release_info;
        root_ops->prepare_resources = pci_acpi_root_prepare_resources;
        root_ops->pci_ops = (struct pci_ops *)&ri->cfg->ops->pci_ops;
        bus = acpi_pci_root_create(root, root_ops, &ri->common, ri->cfg);
        if (!bus)
-               return NULL;
+               goto free_cfg;
 
        /* If we must preserve the resource configuration, claim now */
        host = pci_find_host_bridge(bus);
@@ -1710,6 +1705,14 @@ struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root)
                pcie_bus_configure_settings(child);
 
        return bus;
+
+free_cfg:
+       pci_ecam_free(ri->cfg);
+free_root_ops:
+       kfree(root_ops);
+free_ri:
+       kfree(ri);
+       return NULL;
 }
 
 void pcibios_add_bus(struct pci_bus *bus)