[PATCH] PCI: fix possible memory leak in error case in pci_register_host_bridge()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



If device_register() fails in pci_register_host_bridge(), the refcount
of bus device is leaked, so device name that set by dev_set_name() can
not be freed. Fix this by calling put_device() when device_register()
fails, so the device name will be freed in kobject_cleanup().

The 'bus' is freed in release_pcibus_dev(), so set it to NULL to avoid
double free.

The refcount of bridge device is decreased in release_pcibus_dev(), so
remove the put_device() in error path and change error label name.

Signed-off-by: Yang Yingliang <yangyingliang@xxxxxxxxxx>
---
 drivers/pci/probe.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/drivers/pci/probe.c b/drivers/pci/probe.c
index f61754d76aa7..03b926d7c7ec 100644
--- a/drivers/pci/probe.c
+++ b/drivers/pci/probe.c
@@ -946,8 +946,11 @@ static int pci_register_host_bridge(struct pci_host_bridge *bridge)
 	name = dev_name(&bus->dev);
 
 	err = device_register(&bus->dev);
-	if (err)
-		goto unregister;
+	if (err) {
+		put_device(&bus->dev);
+		bus = NULL;
+		goto del_device;
+	}
 
 	pcibios_add_bus(bus);
 
@@ -1023,8 +1026,7 @@ static int pci_register_host_bridge(struct pci_host_bridge *bridge)
 
 	return 0;
 
-unregister:
-	put_device(&bridge->dev);
+del_device:
 	device_del(&bridge->dev);
 
 free:
-- 
2.25.1




[Index of Archives]     [DMA Engine]     [Linux Coverity]     [Linux USB]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [Greybus]

  Powered by Linux