PCI: fix reference leak in pci_alloc_child_bus()
Subsystems (2)
PATCH v2 1/1 PCI: fix reference leak in pci_alloc_child_bus()
When device_register(&child->dev) failed, calling put_device() to explicitly release child->dev. Otherwise, it could cause double free problem.
device_register() includes device_add(). As comment of device_add() says, 'if device_add() succeeds, you should call device_del() when you want to get rid of it. If device_add() has not succeeded, use only put_device() to drop the reference count'.
Found by code review.
Fixes 4f535093cf8f ("PCI: Put pci_dev in device tree as early as possible")
Signed-off-by Ma Ke <[email protected]>
---
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
@@ -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