PCI: Fix reference leak in pci_alloc_child_bus()
authorMa Ke <make24@iscas.ac.cn>
Sun, 2 Feb 2025 06:23:57 +0000 (14:23 +0800)
committerBjorn Helgaas <bhelgaas@google.com>
Mon, 10 Mar 2025 18:41:48 +0000 (13:41 -0500)
If device_register(&child->dev) fails, call put_device() to explicitly
release child->dev, per the comment at device_register().

Found by code review.

Link: https://lore.kernel.org/r/20250202062357.872971-1-make24@iscas.ac.cn
Fixes: 4f535093cf8f ("PCI: Put pci_dev in device tree as early as possible")
Signed-off-by: Ma Ke <make24@iscas.ac.cn>
Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
Reviewed-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
Cc: stable@vger.kernel.org
drivers/pci/probe.c

index dc37a3c0a9771a98d0d9eb2ebf8b81cd6a1c62e6..088131a74d7d461eba6c880328eda384c42eed11 100644 (file)
@@ -1222,7 +1222,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);