arm64: PCI: free all allocated memory in case of failure
PATCH 1/1 arm64: PCI: free all allocated memory in case of failure
There are some memory allocations in pci_acpi_scan_root(). But ri, root_ops and ri->cfg are not freed properly in failure cases, which results in memory leaks. This patch fixes the potential memory leaks.
--- arch/arm64/kernel/pci.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-)
arch/arm64/kernel/pci.c +14 -8
@@ -188,25 +188,22 @@ struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root) ri =kzalloc_node (sizeof (*ri),GFP_KERNEL , node);if (!ri) -return NULL ; + goto err_allocri; root_ops =kzalloc_node (sizeof (*root_ops),GFP_KERNEL , node);if (!root_ops) -return NULL ; + goto err_allocops; ri->cfg =pci_acpi_setup_ecam_mapping (root); -if (!ri->cfg) { -kfree (ri); -kfree (root_ops); -return NULL ; - } +if (!ri->cfg) + goto err_ecam; root_ops->release_info = pci_acpi_generic_release_info; root_ops->prepare_resources = pci_acpi_root_prepare_resources; root_ops->pci_ops = &ri->cfg->ops->pci_ops; bus =acpi_pci_root_create (root, root_ops, &ri->common, ri->cfg);if (!bus) -return NULL ; + goto err_rootcreate;pci_bus_size_bridges (bus);pci_bus_assign_resources (bus);
@@ -215,6 +212,15 @@ struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root)pcie_bus_configure_settings (child);return bus; + +err_rootcreate: +pci_ecam_free (ri->cfg); +err_ecam: +kfree (root_ops); +err_allocops: +kfree (ri); +err_allocri: +return NULL ; }void pcibios_add_bus (struct pci_bus *bus)
Signature · 1 line
1.9.1
Quoted 26 lines · Timmy Li
There are some memory allocations in
pci_acpi_scan_root(). But ri,root_opsandri->cfgare not freed properly in failure cases, which results in memory leaks. This patch fixes the potential memory leaks.Signed-off-by Timmy Li <[email protected]>--- arch/arm64/kernel/pci.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-)arch/arm64/kernel/pci.c +2
-2@@ -188,25 +188,22 @@ struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root) ri =kzalloc_node (sizeof (*ri),GFP_KERNEL , node);if (!ri) -return NULL ; + goto err_allocri; root_ops =kzalloc_node (sizeof (*root_ops),GFP_KERNEL , node);if (!root_ops) -return NULL ; + goto err_allocops;
arch/arm64/kernel/pci.c struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root) · line 201
Quoted 10 lines
arch/arm64/kernel/pci.c +2
-5ri->cfg =pci_acpi_setup_ecam_mapping (root); -if (!ri->cfg) { -kfree (ri); -kfree (root_ops); -return NULL ; - } +if (!ri->cfg) + goto err_ecam; root_ops->release_info = pci_acpi_generic_release_info;
You are missing this ^^^^^^^^^
arch/arm64/kernel/pci.c struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root) · line 205
Quoted 4 lines
arch/arm64/kernel/pci.c +0
-0root_ops->prepare_resources = pci_acpi_root_prepare_resources; root_ops->pci_ops = &ri->cfg->ops->pci_ops; bus =acpi_pci_root_create (root, root_ops, &ri->common, ri->cfg);if (!bus)
And how it works if (bus == NULL) here.
Lorenzo
Quoted 25 lines
arch/arm64/kernel/pci.c +10
-1-return NULL ; + goto err_rootcreate;pci_bus_size_bridges (bus);pci_bus_assign_resources (bus);@@ -215,6 +212,15 @@ struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root)pcie_bus_configure_settings (child);return bus; + +err_rootcreate: +pci_ecam_free (ri->cfg); +err_ecam: +kfree (root_ops); +err_allocops: +kfree (ri); +err_allocri: +return NULL ; }void pcibios_add_bus (struct pci_bus *bus)Signature · 1 line
1.9.1
Hi Lorenzo,
Thanks for your review and sorry for the late reply.
Quoted 22 lines · Lorenzo Pieralisi
Quoted 21 lines · Timmy Li
There are some memory allocations in
pci_acpi_scan_root(). But ri,root_opsandri->cfgare not freed properly in failure cases, which results in memory leaks. This patch fixes the potential memory leaks.Signed-off-by Timmy Li <[email protected]>--- arch/arm64/kernel/pci.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-)arch/arm64/kernel/pci.c +1
-1@@ -188,25 +188,22 @@ struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root) ri =kzalloc_node (sizeof (*ri),GFP_KERNEL , node);if (!ri) -return NULL ; + goto err_allocri;
Quoted 4 lines
arch/arm64/kernel/pci.c struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root) · line 195
Quoted 4 lines
arch/arm64/kernel/pci.c +1
-1root_ops =kzalloc_node (sizeof (*root_ops),GFP_KERNEL , node);if (!root_ops) -return NULL ; + goto err_allocops;
I think it is still needed to free ri here When ri is allocated successfully and root_ops is not. Considering this is the only remain place that need to fix, a simple way to do this would be:
if (!root_ops) {
+ kfree(ri); return NULL; }
Quoted 14 lines
arch/arm64/kernel/pci.c struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root) · line 201
Quoted 11 lines
arch/arm64/kernel/pci.c +2
-5ri->cfg =pci_acpi_setup_ecam_mapping (root); -if (!ri->cfg) { -kfree (ri); -kfree (root_ops); -return NULL ; - } +if (!ri->cfg) + goto err_ecam; root_ops->release_info = pci_acpi_generic_release_info;You are missing this ^^^^^^^^^
Quoted 6 lines
arch/arm64/kernel/pci.c struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root) · line 205
Quoted 4 lines
arch/arm64/kernel/pci.c +0
-0root_ops->prepare_resources = pci_acpi_root_prepare_resources; root_ops->pci_ops = &ri->cfg->ops->pci_ops; bus =acpi_pci_root_create (root, root_ops, &ri->common, ri->cfg);if (!bus)And how it works if (bus == NULL) here.
You are correct. There is no need to do anything When acpi_pci_root_create() fails.
Quoted 31 lines
Lorenzo
Quoted 25 lines
arch/arm64/kernel/pci.c +10
-1-return NULL ; + goto err_rootcreate;pci_bus_size_bridges (bus);pci_bus_assign_resources (bus);@@ -215,6 +212,15 @@ struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root)pcie_bus_configure_settings (child);return bus; + +err_rootcreate: +pci_ecam_free (ri->cfg); +err_ecam: +kfree (root_ops); +err_allocops: +kfree (ri); +err_allocri: +return NULL ; }void pcibios_add_bus (struct pci_bus *bus)Signature · 1 line
1.9.1.
Quoted 37 lines · Timmy Li
Hi Lorenzo,
Thanks for your review and sorry for the late reply.
Quoted 22 lines · Lorenzo Pieralisi
Quoted 21 lines · Timmy Li
There are some memory allocations in
pci_acpi_scan_root(). But ri,root_opsandri->cfgare not freed properly in failure cases, which results in memory leaks. This patch fixes the potential memory leaks.Signed-off-by Timmy Li <[email protected]>--- arch/arm64/kernel/pci.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-)arch/arm64/kernel/pci.c +1
-1@@ -188,25 +188,22 @@ struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root) ri =kzalloc_node (sizeof (*ri),GFP_KERNEL , node);if (!ri) -return NULL ; + goto err_allocri;Quoted 4 lines
arch/arm64/kernel/pci.c struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root) · line 195
Quoted 4 lines
arch/arm64/kernel/pci.c +1
-1root_ops =kzalloc_node (sizeof (*root_ops),GFP_KERNEL , node);if (!root_ops) -return NULL ; + goto err_allocops;I think it is still needed to free ri here When ri is allocated successfully and
root_opsis not. Considering this is the only remain place that need to fix, a simple way to do this would be:
if (!root_ops) {arch/arm64/kernel/pci.c +1
-0+kfree (ri);
Quoted 2 lines
return NULL; }
Ok, I will send a fix for that, thanks.
Lorenzo
Quoted 56 lines
Quoted 15 lines
arch/arm64/kernel/pci.c struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root) · line 201
Quoted 12 lines
arch/arm64/kernel/pci.c +2
-5ri->cfg =pci_acpi_setup_ecam_mapping (root); -if (!ri->cfg) { -kfree (ri); -kfree (root_ops); -return NULL ; - } +if (!ri->cfg) + goto err_ecam; root_ops->release_info = pci_acpi_generic_release_info;You are missing this ^^^^^^^^^
Quoted 6 lines
arch/arm64/kernel/pci.c struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root) · line 205
Quoted 4 lines
arch/arm64/kernel/pci.c +0
-0root_ops->prepare_resources = pci_acpi_root_prepare_resources; root_ops->pci_ops = &ri->cfg->ops->pci_ops; bus =acpi_pci_root_create (root, root_ops, &ri->common, ri->cfg);if (!bus)And how it works if (bus == NULL) here.
You are correct. There is no need to do anything When
acpi_pci_root_create()fails.Quoted 31 lines
Lorenzo
Quoted 25 lines
arch/arm64/kernel/pci.c +10
-1-return NULL ; + goto err_rootcreate;pci_bus_size_bridges (bus);pci_bus_assign_resources (bus);@@ -215,6 +212,15 @@ struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root)pcie_bus_configure_settings (child);return bus; + +err_rootcreate: +pci_ecam_free (ri->cfg); +err_ecam: +kfree (root_ops); +err_allocops: +kfree (ri); +err_allocri: +return NULL ; }void pcibios_add_bus (struct pci_bus *bus)Signature · 1 line
1.9.1.