Skip to contentSkip to conversation

Loongarch64: pci: fix memleak in pci_acpi_scan_root

PATCH In review Wentao Guan 3 participants 6 messages Aug 31, 2024 LoongArch

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Loongarch64: pci: fix memleak in pci_acpi_scan_root

Add kfree(root_ops) in this case to avoid memleak root_ops, leaks when pci_find_bus() != 0.
Also delay assign root_ops when used for making code read clean. Found by code review

---
 arch/loongarch/pci/acpi.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
arch/loongarch/pci/acpi.c +5 -4
index 3eb61b8cd5e3..7f3539f5fa23 100644
@@ -219,17 +219,18 @@ struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root)
 		return NULL;
 	}
 
-	root_ops->release_info = acpi_release_root_info;
-	root_ops->prepare_resources = acpi_prepare_root_resources;
-	root_ops->pci_ops = (struct pci_ops *)&info->cfg->ops->pci_ops;
-
 	bus = pci_find_bus(domain, busnum);
 	if (bus) {
 		memcpy(bus->sysdata, info->cfg, sizeof(struct pci_config_window));
 		kfree(info);
+		kfree(root_ops);
 	} else {
 		struct pci_bus *child;
 
+		root_ops->release_info = acpi_release_root_info;
+		root_ops->prepare_resources = acpi_prepare_root_resources;
+		root_ops->pci_ops = (struct pci_ops *)&info->cfg->ops->pci_ops;
+
 		bus = acpi_pci_root_create(root, root_ops,
 					   &info->common, info->cfg);
 		if (!bus) {
Signature · 1 line
2.20.1
maintainer
#1 lore raw

Hi, Wentao,

Quoted 7 lines · Wentao Guan

Add kfree(root_ops) in this case to avoid memleak root_ops, leaks when pci_find_bus() != 0.
Also delay assign root_ops when used for making code read clean. Found by code review

This should be Yuli Wang <[email protected]>

Quoted 12 lines
---
 arch/loongarch/pci/acpi.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
arch/loongarch/pci/acpi.c +0 -0
index 3eb61b8cd5e3..7f3539f5fa23 100644
@@ -219,17 +219,18 @@ struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root)
                return NULL;
        }

arch/loongarch/pci/acpi.c struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root) · line 226

Quoted 10 lines
arch/loongarch/pci/acpi.c +1 -4
 
-       root_ops->release_info = acpi_release_root_info;
-       root_ops->prepare_resources = acpi_prepare_root_resources;
-       root_ops->pci_ops = (struct pci_ops *)&info->cfg->ops->pci_ops;
-
        bus = pci_find_bus(domain, busnum);
        if (bus) {
                memcpy(bus->sysdata, info->cfg, sizeof(struct pci_config_window));
                kfree(info);
+               kfree(root_ops);

Add kfree() is fine, but move root_ops assignment is unnecessary.

Huacai

Quoted 14 lines
arch/loongarch/pci/acpi.c +4 -1
        } else {
                struct pci_bus *child;
 
+               root_ops->release_info = acpi_release_root_info;
+               root_ops->prepare_resources = acpi_prepare_root_resources;
+               root_ops->pci_ops = (struct pci_ops *)&info->cfg->ops->pci_ops;
+
                bus = acpi_pci_root_create(root, root_ops,
                                           &info->common, info->cfg);
                if (!bus) {
--

2.20.1

#2 lore raw

Hello Huacai:

Okay... It is my code smell, here is the patch after change.

From 73426019a83a77b4502491fc96d145e666b16936 Mon Sep 17 00:00:00 2001
From: Wentao Guan <[email protected]>
Date: Sat, 31 Aug 2024 00:17:10 +0800
Subject: [PATCH] Loongarch64: pci: fix memleak in pci_acpi_scan_root

Add kfree(root_ops) in this case to avoid memleak root_ops, leaks when pci_find_bus() != 0.

---
 arch/loongarch/pci/acpi.c | 1 +
 1 file changed, 1 insertion(+)
arch/loongarch/pci/acpi.c +1 -0
index 3eb61b8cd5e3..f405409a3491 100644
@@ -227,6 +227,7 @@ struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root)
        if (bus) {
                memcpy(bus->sysdata, info->cfg, sizeof(struct pci_config_window));
                kfree(info);
+               kfree(root_ops);
        } else {
                struct pci_bus *child;
 
Signature · 1 line
2.20.1
#3 lore raw

Hello Huacai:

I found a mistake that my PATCH commit msg need change to "Loongarch: pci: fix memleak in pci_acpi_scan_root" Need to send patch v2?

maintainer
#4 lore raw
Quoted 5 lines · Wentao Guan

Hello Huacai:

I found a mistake that my PATCH commit msg need change to "Loongarch: pci: fix memleak in pci_acpi_scan_root" Need to send patch v2?

No need.

Huacai

#5 lore raw
In reply to Huacai Chen
Quoted 10 lines · Huacai Chen

Hi, Wentao,

Quoted 6 lines · Wentao Guan

Add kfree(root_ops) in this case to avoid memleak root_ops, leaks when pci_find_bus() != 0.
Also delay assign root_ops when used for making code read clean. Found by code review

This should be Yuli Wang <[email protected]>

While I maintain a preference for the traditional Chinese naming order, not particularly like putting my Chinese name in the English surname order,

I acknowledge that flexibility in this matter is often necessary.

It is sufficient for everybody to be aware that both 'WangYuli' and 'Yuli Wang' refer to me.

Quoted 40 lines
Quoted 12 lines
---
  arch/loongarch/pci/acpi.c | 9 +++++----
  1 file changed, 5 insertions(+), 4 deletions(-)
arch/loongarch/pci/acpi.c +0 -0
index 3eb61b8cd5e3..7f3539f5fa23 100644
@@ -219,17 +219,18 @@ struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root)
                 return NULL;
         }

arch/loongarch/pci/acpi.c struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root) · line 226

Quoted 10 lines
arch/loongarch/pci/acpi.c +1 -4
 
-       root_ops->release_info = acpi_release_root_info;
-       root_ops->prepare_resources = acpi_prepare_root_resources;
-       root_ops->pci_ops = (struct pci_ops *)&info->cfg->ops->pci_ops;
-
         bus = pci_find_bus(domain, busnum);
         if (bus) {
                 memcpy(bus->sysdata, info->cfg, sizeof(struct pci_config_window));
                 kfree(info);
+               kfree(root_ops);

Add kfree() is fine, but move root_ops assignment is unnecessary.

Huacai

Quoted 14 lines
arch/loongarch/pci/acpi.c +4 -1
         } else {
                 struct pci_bus *child;
 
+               root_ops->release_info = acpi_release_root_info;
+               root_ops->prepare_resources = acpi_prepare_root_resources;
+               root_ops->pci_ops = (struct pci_ops *)&info->cfg->ops->pci_ops;
+
                 bus = acpi_pci_root_create(root, root_ops,
                                            &info->common, info->cfg);
                 if (!bus) {
--

2.20.1

Signature · 1 line
WangYuli