Skip to contentSkip to conversation

PCI: imx6: fix resource leaks in probe error paths

PATCH Superseded Zhijian Han 5 participants 5 messages Aug 24 – Sep 2

Subsystems (4)

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 PCI: imx6: fix resource leaks in probe error paths

imx_pcie_probe() leaks both pwrctrl devices and power domains on failure:

- imx_pcie_attach_pd() attaches the "pcie" and "pcie_phy" power domains and adds device links to them, but nothing detaches the domains on probe failure or deferral, so they leak.

- A failure of devm_pm_runtime_set_active_enabled() returns directly without destroying the pwrctrl devices.

- A partial failure inside imx_pcie_attach_pd() leaks the power domains that were already attached.

Add imx_pcie_detach_pd() to detach the power domains in reverse order of acquisition and call it from the probe error paths. Add DL_FLAG_AUTOREMOVE_CONSUMER to the device links so the driver core removes them automatically when probe fails, instead of tracking and deleting them manually.

Reported-by [email protected]
Fixes 2c5768344f88 ("PCI: imx6: Move pci_pwrctrl_create_devices() to imx_pcie_probe()")

---
Changes in v4:
- Use DL_FLAG_AUTOREMOVE_CONSUMER so the driver core removes the device links automatically, instead of tracking and deleting them manually
- Add a Fixes tag

 drivers/pci/controller/dwc/pci-imx6.c | 37 +++++++++++++++++++++------
 1 file changed, 29 insertions(+), 8 deletions(-)
drivers/pci/controller/dwc/pci-imx6.c +29 -8
index 39790e66b..0b4209365 100644
@@ -639,6 +639,18 @@ static int imx6q_pcie_abort_handler(unsigned long addr,
 }
 #endif
 
+static void imx_pcie_detach_pd(struct imx_pcie *imx_pcie)
+{
+	if (!IS_ERR_OR_NULL(imx_pcie->pd_pcie_phy)) {
+		dev_pm_domain_detach(imx_pcie->pd_pcie_phy, true);
+		imx_pcie->pd_pcie_phy = NULL;
+	}
+	if (!IS_ERR_OR_NULL(imx_pcie->pd_pcie)) {
+		dev_pm_domain_detach(imx_pcie->pd_pcie, true);
+		imx_pcie->pd_pcie = NULL;
+	}
+}
+
 static int imx_pcie_attach_pd(struct device *dev)
 {
 	struct imx_pcie *imx_pcie = dev_get_drvdata(dev);
@@ -655,24 +667,30 @@ static int imx_pcie_attach_pd(struct device *dev)
 	if (!imx_pcie->pd_pcie)
 		return 0;
 	link = device_link_add(dev, imx_pcie->pd_pcie,
-			DL_FLAG_STATELESS |
 			DL_FLAG_PM_RUNTIME |
-			DL_FLAG_RPM_ACTIVE);
+			DL_FLAG_RPM_ACTIVE |
+			DL_FLAG_AUTOREMOVE_CONSUMER);
 	if (!link) {
 		dev_err(dev, "Failed to add device_link to pcie pd\n");
+		imx_pcie_detach_pd(imx_pcie);
 		return -EINVAL;
 	}
 
 	imx_pcie->pd_pcie_phy = dev_pm_domain_attach_by_name(dev, "pcie_phy");
-	if (IS_ERR(imx_pcie->pd_pcie_phy))
-		return PTR_ERR(imx_pcie->pd_pcie_phy);
+	if (IS_ERR(imx_pcie->pd_pcie_phy)) {
+		int ret = PTR_ERR(imx_pcie->pd_pcie_phy);
+
+		imx_pcie_detach_pd(imx_pcie);
+		return ret;
+	}
 
 	link = device_link_add(dev, imx_pcie->pd_pcie_phy,
-			DL_FLAG_STATELESS |
 			DL_FLAG_PM_RUNTIME |
-			DL_FLAG_RPM_ACTIVE);
+			DL_FLAG_RPM_ACTIVE |
+			DL_FLAG_AUTOREMOVE_CONSUMER);
 	if (!link) {
 		dev_err(dev, "Failed to add device_link to pcie_phy pd\n");
+		imx_pcie_detach_pd(imx_pcie);
 		return -EINVAL;
 	}
 
@@ -1956,8 +1974,10 @@ static int imx_pcie_probe(struct platform_device *pdev)
 		return ret;
 
 	ret = pci_pwrctrl_create_devices(dev);
-	if (ret)
+	if (ret) {
+		imx_pcie_detach_pd(imx_pcie);
 		return dev_err_probe(dev, ret, "failed to create pwrctrl devices\n");
+	}
 
 	pci->use_parent_dt_ranges = true;
 	if (imx_pcie->drvdata->mode == DW_PCIE_EP_TYPE) {
@@ -1975,7 +1995,7 @@ static int imx_pcie_probe(struct platform_device *pdev)
 			pm_runtime_no_callbacks(dev);
 			ret = devm_pm_runtime_set_active_enabled(dev);
 			if (ret < 0)
-				return ret;
+				goto err_pwrctrl_destroy;
 		}
 
 		if (imx_check_flag(imx_pcie, IMX_PCIE_FLAG_SKIP_L23_READY))
@@ -2001,6 +2021,7 @@ static int imx_pcie_probe(struct platform_device *pdev)
 err_pwrctrl_destroy:
 	if (ret != -EPROBE_DEFER)
 		pci_pwrctrl_destroy_devices(dev);
+	imx_pcie_detach_pd(imx_pcie);
 	return ret;
 }
 
Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Unbounded resource leak (clocks, regulators, PHYs) during repeated endpoint probe deferrals. --

--- Patch [1]: [PATCH v4] PCI: imx6: fix resource leaks in probe error paths --- commit 4146a6183d1b75bd09bf97a22116274151dfc0ca
Author: Zhijian Han <[email protected]>

PCI: imx6: fix resource leaks in probe error paths

This commit addresses resource leaks in the imx6 pcie probe error paths by introducing imx_pcie_detach_pd() to clean up power domains and pwrctrl devices. It also adds DL_FLAG_AUTOREMOVE_CONSUMER to device links so they are cleaned up automatically.

Quoted 4 lines
drivers/pci/controller/dwc/pci-imx6.c +0 -0
index 39790e66b98dc..0b42093654f04 100644

[ ... ]

drivers/pci/controller/dwc/pci-imx6.c static int imx_pcie_probe(struct platform_device *pdev) · line 1983

Quoted 12 lines
drivers/pci/controller/dwc/pci-imx6.c +3 -1
@@ -1956,8 +1974,10 @@ static int imx_pcie_probe(struct platform_device *pdev)
 		return ret;
 
 	ret = pci_pwrctrl_create_devices(dev);
-	if (ret)
+	if (ret) {
+		imx_pcie_detach_pd(imx_pcie);
 		return dev_err_probe(dev, ret, "failed to create pwrctrl devices\n");
+	}
 
 	pci->use_parent_dt_ranges = true;
 	if (imx_pcie->drvdata->mode == DW_PCIE_EP_TYPE) {

[Severity: High]
This is a pre-existing issue, but does imx_add_pcie_ep() leak hardware resources such as clocks, regulators, and PHYs if endpoint initialization fails or defers?

Looking at imx_add_pcie_ep(), it allocates hardware resources via imx_pcie_host_init(pp), but if dw_pcie_ep_init(ep) returns an error (like -EPROBE_DEFER), it returns directly without cleaning them up:

drivers/pci/controller/dwc/pci-imx6.c:imx_add_pcie_ep() {
    ...
	ret = dw_pcie_ep_init(ep);
	if (ret) {
		dev_err(dev, "failed to initialize endpoint\n");
		return ret;
	}
    ...
}

Similarly, if dw_pcie_ep_init_registers(ep) fails, it calls dw_pcie_ep_deinit(ep) but still misses cleaning up the host resources:

drivers/pci/controller/dwc/pci-imx6.c:imx_add_pcie_ep() {
    ...
	ret = dw_pcie_ep_init_registers(ep);
	if (ret) {
		dev_err(dev, "Failed to initialize DWC endpoint registers\n");
		dw_pcie_ep_deinit(ep);
		return ret;
	}
    ...
}

Should these error paths call imx_pcie_host_exit(pp) to avoid permanently leaking power and resource references during repeated probe deferrals?

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Zhijian Han
Quoted 18 lines · Zhijian Han

imx_pcie_probe() leaks both pwrctrl devices and power domains on failure:

- imx_pcie_attach_pd() attaches the "pcie" and "pcie_phy" power domains and adds device links to them, but nothing detaches the domains on probe failure or deferral, so they leak.

- A failure of devm_pm_runtime_set_active_enabled() returns directly without destroying the pwrctrl devices.

- A partial failure inside imx_pcie_attach_pd() leaks the power domains that were already attached.

Add imx_pcie_detach_pd() to detach the power domains in reverse order of acquisition and call it from the probe error paths. Add DL_FLAG_AUTOREMOVE_CONSUMER to the device links so the driver core removes them automatically when probe fails, instead of tracking and deleting them manually.

Quoted 4 lines
Reported-by [email protected]
Fixes 2c5768344f88 ("PCI: imx6: Move pci_pwrctrl_create_devices() to imx_pcie_probe()")

Frank, Richard, can I get your review for this patch?

- Mani

maintainer Reviewed-by
#3 lore raw
Quoted 50 lines

-----Original Message-----
From: Manivannan Sadhasivam <[email protected]>
Sent: Wednesday, September 2, 2026 3:53 PM
To: Hongxing Zhu <[email protected]>; Frank Li <[email protected]>; Zhijian Han <[email protected]>
Cc: Hongxing Zhu <[email protected]>; Lucas Stach <[email protected]>; Lorenzo Pieralisi <[email protected]>; Krzysztof Wilczyński <[email protected]>; Bjorn Helgaas <[email protected]>; Frank Li <[email protected]>; Sascha Hauer <[email protected]>; Rob Herring <[email protected]>; Pengutronix Kernel Team <[email protected]>; Fabio Estevam <[email protected]>; linux- [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v4] PCI: imx6: fix resource leaks in probe error paths

Quoted 18 lines · Zhijian Han

imx_pcie_probe() leaks both pwrctrl devices and power domains on failure:

- imx_pcie_attach_pd() attaches the "pcie" and "pcie_phy" power domains and adds device links to them, but nothing detaches the domains on probe failure or deferral, so they leak.

- A failure of devm_pm_runtime_set_active_enabled() returns directly without destroying the pwrctrl devices.

- A partial failure inside imx_pcie_attach_pd() leaks the power domains that were already attached.

Add imx_pcie_detach_pd() to detach the power domains in reverse order of acquisition and call it from the probe error paths. Add DL_FLAG_AUTOREMOVE_CONSUMER to the device links so the driver core removes them automatically when probe fails, instead of tracking and deleting them manually.

Quoted 4 lines
Reported-by [email protected]

Link:
https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Flore
.kernel.org%2Fall%2F20260822013640.182C01F000E9%40smtp.kernel.org%2

F&d

Quoted 1 line

ata=05%7C02%7Chongxing.zhu%40nxp.com%7C8285e77d3eaf45f25a0608df08 c73e1

Quoted 1 line

2%7C686ea1d3bc2b4c6fa92cd99c5c301635%7C0%7C0%7C6392393239841792 60%7CUn

Quoted 1 line

known%7CTWFpbGZsb3d8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwM CIsIlAiOi

Quoted 1 line
Quoted 8 lines

JXaW4zMiIsIkFOIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C0%7C%7C%7C&sdata=gcw b63Hn

Quoted 4 lines

dHcOrDQnwD37rUN%2F0EUDcnIZOOGrZ7Ylzio%3D&reserved=0

Fixes 2c5768344f88 ("PCI: imx6: Move pci_pwrctrl_create_devices() to

imx_pcie_probe()")

Frank, Richard, can I get your review for this patch?

Apologies for missing this earlier.

Best Regards
Richard Zhu

Quoted 2 lines

- Mani

#4 lore raw
In reply to Zhijian Han
Quoted 94 lines · Zhijian Han

[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

imx_pcie_probe() leaks both pwrctrl devices and power domains on failure:

- imx_pcie_attach_pd() attaches the "pcie" and "pcie_phy" power domains and adds device links to them, but nothing detaches the domains on probe failure or deferral, so they leak.

- A failure of devm_pm_runtime_set_active_enabled() returns directly without destroying the pwrctrl devices.

- A partial failure inside imx_pcie_attach_pd() leaks the power domains that were already attached.

Add imx_pcie_detach_pd() to detach the power domains in reverse order of acquisition and call it from the probe error paths. Add DL_FLAG_AUTOREMOVE_CONSUMER to the device links so the driver core removes them automatically when probe fails, instead of tracking and deleting them manually.

Reported-by [email protected]
Fixes 2c5768344f88 ("PCI: imx6: Move pci_pwrctrl_create_devices() to imx_pcie_probe()")

---
Changes in v4:
- Use DL_FLAG_AUTOREMOVE_CONSUMER so the driver core removes the device links automatically, instead of tracking and deleting them manually
- Add a Fixes tag

 drivers/pci/controller/dwc/pci-imx6.c | 37 +++++++++++++++++++++------
 1 file changed, 29 insertions(+), 8 deletions(-)
drivers/pci/controller/dwc/pci-imx6.c +24 -6
index 39790e66b..0b4209365 100644
@@ -639,6 +639,18 @@ static int imx6q_pcie_abort_handler(unsigned long addr,
 }
 #endif
 
+static void imx_pcie_detach_pd(struct imx_pcie *imx_pcie)
+{
+       if (!IS_ERR_OR_NULL(imx_pcie->pd_pcie_phy)) {
+               dev_pm_domain_detach(imx_pcie->pd_pcie_phy, true);
+               imx_pcie->pd_pcie_phy = NULL;
+       }
+       if (!IS_ERR_OR_NULL(imx_pcie->pd_pcie)) {
+               dev_pm_domain_detach(imx_pcie->pd_pcie, true);
+               imx_pcie->pd_pcie = NULL;
+       }
+}
+
 static int imx_pcie_attach_pd(struct device *dev)
 {
        struct imx_pcie *imx_pcie = dev_get_drvdata(dev);
@@ -655,24 +667,30 @@ static int imx_pcie_attach_pd(struct device *dev)
        if (!imx_pcie->pd_pcie)
                return 0;
        link = device_link_add(dev, imx_pcie->pd_pcie,
-                       DL_FLAG_STATELESS |
                        DL_FLAG_PM_RUNTIME |
-                       DL_FLAG_RPM_ACTIVE);
+                       DL_FLAG_RPM_ACTIVE |
+                       DL_FLAG_AUTOREMOVE_CONSUMER);
        if (!link) {
                dev_err(dev, "Failed to add device_link to pcie pd\n");
+               imx_pcie_detach_pd(imx_pcie);
                return -EINVAL;
        }
 
        imx_pcie->pd_pcie_phy = dev_pm_domain_attach_by_name(dev, "pcie_phy");
-       if (IS_ERR(imx_pcie->pd_pcie_phy))
-               return PTR_ERR(imx_pcie->pd_pcie_phy);
+       if (IS_ERR(imx_pcie->pd_pcie_phy)) {
+               int ret = PTR_ERR(imx_pcie->pd_pcie_phy);
+
+               imx_pcie_detach_pd(imx_pcie);
+               return ret;
+       }
 
        link = device_link_add(dev, imx_pcie->pd_pcie_phy,
-                       DL_FLAG_STATELESS |
                        DL_FLAG_PM_RUNTIME |
-                       DL_FLAG_RPM_ACTIVE);
+                       DL_FLAG_RPM_ACTIVE |
+                       DL_FLAG_AUTOREMOVE_CONSUMER);
        if (!link) {
                dev_err(dev, "Failed to add device_link to pcie_phy pd\n");
+               imx_pcie_detach_pd(imx_pcie);
                return -EINVAL;
        }
 

drivers/pci/controller/dwc/pci-imx6.c static int imx_pcie_probe(struct platform_device *pdev) · line 1978

Quoted 6 lines
drivers/pci/controller/dwc/pci-imx6.c +2 -1
@@ -1956,8 +1974,10 @@ static int imx_pcie_probe(struct platform_device *pdev)
                return ret;
 
        ret = pci_pwrctrl_create_devices(dev);
-       if (ret)
+       if (ret) {
+               imx_pcie_detach_pd(imx_pcie);

use devm_add_action_or_reset() to do tear down.

Frank

Quoted 26 lines
drivers/pci/controller/dwc/pci-imx6.c +3 -2
                return dev_err_probe(dev, ret, "failed to create pwrctrl devices\n");
+       }
 
        pci->use_parent_dt_ranges = true;
        if (imx_pcie->drvdata->mode == DW_PCIE_EP_TYPE) {
@@ -1975,7 +1995,7 @@ static int imx_pcie_probe(struct platform_device *pdev)
                        pm_runtime_no_callbacks(dev);
                        ret = devm_pm_runtime_set_active_enabled(dev);
                        if (ret < 0)
-                               return ret;
+                               goto err_pwrctrl_destroy;
                }
 
                if (imx_check_flag(imx_pcie, IMX_PCIE_FLAG_SKIP_L23_READY))
@@ -2001,6 +2021,7 @@ static int imx_pcie_probe(struct platform_device *pdev)
 err_pwrctrl_destroy:
        if (ret != -EPROBE_DEFER)
                pci_pwrctrl_destroy_devices(dev);
+       imx_pcie_detach_pd(imx_pcie);
        return ret;
 }
 
--

2.43.0