[Concept,14/17] pci: Fix PCI regions array leak on device removal

Message ID 20260316183050.3855921-15-sjg@u-boot.org
State New
Headers
Series Add automatic memory-leak detection to U-Boot tests |

Commit Message

Simon Glass March 16, 2026, 6:30 p.m. UTC
  From: Simon Glass <sjg@chromium.org>

decode_regions() allocates hose->regions in pci_uclass_pre_probe() but
the array is never freed when the PCI bus device is removed.

Add a pre_remove() handler to free it.

Fixes: e002474158d1 ("pci: pci-uclass: Dynamically allocate the PCI regions")
Signed-off-by: Simon Glass <sjg@chromium.org>
---

 drivers/pci/pci-uclass.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
  

Patch

diff --git a/drivers/pci/pci-uclass.c b/drivers/pci/pci-uclass.c
index c370f8c6400..27bc92f0e4c 100644
--- a/drivers/pci/pci-uclass.c
+++ b/drivers/pci/pci-uclass.c
@@ -1874,6 +1874,16 @@  int pci_sriov_get_totalvfs(struct udevice *pdev)
 }
 #endif /* SRIOV */
 
+static int pci_uclass_pre_remove(struct udevice *bus)
+{
+	struct pci_controller *hose = dev_get_uclass_priv(bus);
+
+	free(hose->regions);
+	hose->regions = NULL;
+
+	return 0;
+}
+
 UCLASS_DRIVER(pci) = {
 	.id		= UCLASS_PCI,
 	.name		= "pci",
@@ -1881,6 +1891,7 @@  UCLASS_DRIVER(pci) = {
 	.post_bind	= dm_scan_fdt_dev,
 	.pre_probe	= pci_uclass_pre_probe,
 	.post_probe	= pci_uclass_post_probe,
+	.pre_remove	= pci_uclass_pre_remove,
 	.child_post_bind = pci_uclass_child_post_bind,
 	.per_device_auto	= sizeof(struct pci_controller),
 	.per_child_plat_auto	= sizeof(struct pci_child_plat),