Skip to contentSkip to conversation

PCI: fix reference leak in pci_alloc_child_bus()

PATCH In review Ma Ke 3 participants 3 messages Feb 2, 2025 – Feb 27, 2025

Subsystems (2)
lore
Share & tools
lore

Ma Ke

author
#0 lore raw

PATCH v3 1/1 PCI: fix reference leak in pci_alloc_child_bus()

When device_register(&child->dev) failed, we should call put_device() to explicitly release child->dev.

As comment of device_register() says, 'NOTE: _Never_ directly free @dev after calling this function, even if it returned an error! Always use put_device() to give up the reference initialized in this function instead.'

Found by code review.

Fixes 4f535093cf8f ("PCI: Put pci_dev in device tree as early as possible")

---
Changes in v3:
- modified the description as suggestions.
Changes in v2:
- added the bug description about the comment of device_add();
- fixed the patch as suggestions;
- added Cc and Fixes table.

---
 drivers/pci/probe.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/pci/probe.c +4 -1
index 2e81ab0f5a25..51b78fcda4eb 100644
@@ -1174,7 +1174,10 @@ static struct pci_bus *pci_alloc_child_bus(struct pci_bus *parent,
 add_dev:
 	pci_set_bus_msi_domain(child);
 	ret = device_register(&child->dev);
-	WARN_ON(ret < 0);
+	if (WARN_ON(ret < 0)) {
+		put_device(&child->dev);
+		return NULL;
+	}
 
 	pcibios_add_bus(child);
 
Signature · 1 line
2.25.1
Reviewed-by on v3 1/1
#1 lore raw
Quoted 29 lines · Ma Ke

When device_register(&child->dev) failed, we should call put_device() to explicitly release child->dev.

As comment of device_register() says, 'NOTE: _Never_ directly free @dev after calling this function, even if it returned an error! Always use put_device() to give up the reference initialized in this function instead.'

Found by code review.

Fixes 4f535093cf8f ("PCI: Put pci_dev in device tree as early as possible")

---
Changes in v3:
- modified the description as suggestions.
Changes in v2:
- added the bug description about the comment of device_add();
- fixed the patch as suggestions;
- added Cc and Fixes table.

---
 drivers/pci/probe.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/pci/probe.c +0 -0
index 2e81ab0f5a25..51b78fcda4eb 100644

drivers/pci/probe.c static struct pci_bus *pci_alloc_child_bus(struct pci_bus *parent, · line 1182

Quoted 10 lines
drivers/pci/probe.c +4 -1
@@ -1174,7 +1174,10 @@ static struct pci_bus *pci_alloc_child_bus(struct pci_bus *parent,
 add_dev:
 	pci_set_bus_msi_domain(child);
 	ret = device_register(&child->dev);
-	WARN_ON(ret < 0);
+	if (WARN_ON(ret < 0)) {
+		put_device(&child->dev);
+		return NULL;
+	}
 
 	pcibios_add_bus(child);

Unrelated to this fix, IMO that WARN_ON() is overkill and I'm skeptical that printing a stack trace on a failure in device_register() is helpful. IMO, a simple error print would suffice to tell something (unexpectedly) went wrong here.

Signature · 1 line
 i.
#2 lore raw
In reply to Ma Ke
Quoted 8 lines · Ma Ke

When device_register(&child->dev) failed, we should call put_device() to explicitly release child->dev.

As comment of device_register() says, 'NOTE: _Never_ directly free @dev after calling this function, even if it returned an error! Always use put_device() to give up the reference initialized in this function instead.'

Quoted 5 lines

Found by code review.

Fixes 4f535093cf8f ("PCI: Put pci_dev in device tree as early as possible")

Applied with Ilpo's reviewed-by to pci/enumeration for v6.15, thanks!

Quoted 30 lines

---
Changes in v3:
- modified the description as suggestions.
Changes in v2:
- added the bug description about the comment of device_add();
- fixed the patch as suggestions;
- added Cc and Fixes table.

---
 drivers/pci/probe.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/pci/probe.c +4 -1
index 2e81ab0f5a25..51b78fcda4eb 100644
@@ -1174,7 +1174,10 @@ static struct pci_bus *pci_alloc_child_bus(struct pci_bus *parent,
 add_dev:
 	pci_set_bus_msi_domain(child);
 	ret = device_register(&child->dev);
-	WARN_ON(ret < 0);
+	if (WARN_ON(ret < 0)) {
+		put_device(&child->dev);
+		return NULL;
+	}
 
 	pcibios_add_bus(child);
 
Signature · 1 line
2.25.1