Skip to contentSkip to conversation

PCI: starfive: Fix unchecked pm_runtime_get_sync() in probe

PATCH Superseded Ali Tariq 3 participants 8 messages Jul 12 – Jul 29

Subsystems (3)
lore
Share & tools
lore

Ali Tariq

author
#0 lore raw

PATCH 1/1 PCI: starfive: Fix unchecked pm_runtime_get_sync() in probe

pm_runtime_get_sync() is called in starfive_pcie_probe() without checking its return value. If runtime resume fails, the driver proceeds to configure PCIe hardware through regmap_update_bits(), enable clocks and resets, and power on the PHY, even though the device may not actually be powered.

pm_runtime_get_sync() also increments the usage counter even when resume fails, which would leave the counter unbalanced if this error path were later handled without additional cleanup.

Switch to pm_runtime_resume_and_get(), which balances the usage counter internally on failure, and bail out of probe before any hardware is touched if resume does not succeed.

Tested on StarFive VisionFive 2 v1.2A board.

---
 drivers/pci/controller/plda/pcie-starfive.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/pci/controller/plda/pcie-starfive.c +5 -1
index 298036c3e7f9..e9a472985b8a 100644
@@ -410,7 +410,11 @@ static int starfive_pcie_probe(struct platform_device *pdev)
 		return ret;
 
 	pm_runtime_enable(&pdev->dev);
-	pm_runtime_get_sync(&pdev->dev);
+	ret = pm_runtime_resume_and_get(&pdev->dev);
+	if (ret < 0) {
+		pm_runtime_disable(&pdev->dev);
+		return dev_err_probe(dev, ret, "failed to resume device\n");
+	}
 
 	plda->host_ops = &sf_host_ops;
 	plda->num_events = PLDA_MAX_EVENT_NUM;
Signature · 1 line
2.34.1
#1 lore raw
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

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

Pre-existing issues:
- [High] The driver's removal path incorrectly calls asynchronous pm_runtime_put() before pm_runtime_disable(), and tears down PM references before disabling the hardware.
- [High] Failure in starfive_pcie_clk_rst_init() returns directly without powering off the PHY, causing a resource leak.
- [High] Missing return statement after regulator_enable() failure causes a silent fallthrough, later leading to an unbalanced regulator_disable(). --

--- Patch [1]: [PATCH] PCI: starfive: Fix unchecked pm_runtime_get_sync() in probe --- commit 83a329f8aee98c4aec071468fc96ecc89497124e
Author: Ali Tariq <[email protected]>

PCI: starfive: Fix unchecked pm_runtime_get_sync() in probe

This patch replaces pm_runtime_get_sync() with pm_runtime_resume_and_get() in starfive_pcie_probe() to properly handle runtime resume failures. It adds error handling to disable runtime PM and bail out of the probe function if the device fails to resume, preventing unbalanced usage counters and hardware configuration on an unpowered device.

Quoted 7 lines
drivers/pci/controller/plda/pcie-starfive.c +0 -0
index 298036c3e7f94..e9a472985b8ac 100644
@@ -410,7 +410,11 @@ static int starfive_pcie_probe(struct platform_device *pdev)
 		return ret;
 

drivers/pci/controller/plda/pcie-starfive.c static int starfive_pcie_probe(struct platform_device *pdev) · line 420

Quoted 10 lines
drivers/pci/controller/plda/pcie-starfive.c +5 -1
 	pm_runtime_enable(&pdev->dev);
-	pm_runtime_get_sync(&pdev->dev);
+	ret = pm_runtime_resume_and_get(&pdev->dev);
+	if (ret < 0) {
+		pm_runtime_disable(&pdev->dev);
+		return dev_err_probe(dev, ret, "failed to resume device\n");
+	}
 
 	plda->host_ops = &sf_host_ops;
 	plda->num_events = PLDA_MAX_EVENT_NUM;

[Severity: High]
This isn't a bug introduced by this patch, but looking at starfive_pcie_remove():

drivers/pci/controller/plda/pcie-starfive.c:starfive_pcie_remove() {
    ...
	pm_runtime_put(&pdev->dev);
	pm_runtime_disable(&pdev->dev);
	plda_pcie_host_deinit(&pcie->plda);
    ...
}

Does this leave the hardware in an active power state upon module unload?

Since pm_runtime_put() is asynchronous, wouldn't calling pm_runtime_disable() immediately afterward risk canceling the idle/suspend work?

Also, does tearing down the PM reference before stopping the PCIe host controller via plda_pcie_host_deinit() reverse the proper teardown order? Could this cause the device to power off while still being actively accessed by the core?

[Severity: High]
This is a pre-existing issue, but I noticed a potential resource leak in starfive_pcie_host_init():

drivers/pci/controller/plda/pcie-starfive.c:starfive_pcie_host_init() {
    ...
	ret = starfive_pcie_enable_phy(dev, pcie);
	if (ret)
		return ret;
    ...
	ret = starfive_pcie_clk_rst_init(pcie);
	if (ret)
		return ret;
    ...
}

If starfive_pcie_clk_rst_init() fails, it returns the error code directly. Does this fail to clean up the successfully initialized PHY?

Since plda_pcie_host_init() detects this initialization failure and returns early without invoking host_deinit, does this result in permanently leaked PHY resources and an unbalanced power state?

[Severity: High]
This is a pre-existing issue, but does this code swallow the regulator error?

drivers/pci/controller/plda/pcie-starfive.c:starfive_pcie_host_init() {
    ...
	if (pcie->vpcie3v3) {
		ret = regulator_enable(pcie->vpcie3v3);
		if (ret)
			dev_err_probe(dev, ret, "failed to enable vpcie3v3 regulator\n");
	}
    ...
}

Because there is no return statement after dev_err_probe(), the function proceeds and eventually returns success at the end. Could this cause the driver to continue operating with unpowered hardware?

Later on, starfive_pcie_host_deinit() unconditionally calls regulator_disable(). Since the regulator was not successfully enabled, would this trigger an unbalanced disable warning in the regulator core and corrupt regulator use counts?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author
#2 lore raw

Thanks for the review. These are all pre-existing issues separate from this patch, but worth fixing. I'll send a follow-up series addressing each of them separately.

author
#3 lore raw
In reply to [email protected]
Quoted 51 lines · On 7/12/26 11:04 PM, [email protected] wrote:

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

Pre-existing issues:
- [High] The driver's removal path incorrectly calls asynchronous pm_runtime_put() before pm_runtime_disable(), and tears down PM references before disabling the hardware.
- [High] Failure in starfive_pcie_clk_rst_init() returns directly without powering off the PHY, causing a resource leak.
- [High] Missing return statement after regulator_enable() failure causes a silent fallthrough, later leading to an unbalanced regulator_disable(). --

--- Patch [1]: [PATCH] PCI: starfive: Fix unchecked pm_runtime_get_sync() in probe --- commit 83a329f8aee98c4aec071468fc96ecc89497124e
Author: Ali Tariq <[email protected]>

PCI: starfive: Fix unchecked pm_runtime_get_sync() in probe

This patch replaces pm_runtime_get_sync() with pm_runtime_resume_and_get() in starfive_pcie_probe() to properly handle runtime resume failures. It adds error handling to disable runtime PM and bail out of the probe function if the device fails to resume, preventing unbalanced usage counters and hardware configuration on an unpowered device.

Quoted 7 lines
drivers/pci/controller/plda/pcie-starfive.c +0 -0
index 298036c3e7f94..e9a472985b8ac 100644
@@ -410,7 +410,11 @@ static int starfive_pcie_probe(struct platform_device *pdev)
  		return ret;
 

drivers/pci/controller/plda/pcie-starfive.c static int starfive_pcie_probe(struct platform_device *pdev) · line 420

Quoted 10 lines
drivers/pci/controller/plda/pcie-starfive.c +5 -1
  	pm_runtime_enable(&pdev->dev);
-	pm_runtime_get_sync(&pdev->dev);
+	ret = pm_runtime_resume_and_get(&pdev->dev);
+	if (ret < 0) {
+		pm_runtime_disable(&pdev->dev);
+		return dev_err_probe(dev, ret, "failed to resume device\n");
+	}
 
  	plda->host_ops = &sf_host_ops;
  	plda->num_events = PLDA_MAX_EVENT_NUM;

[Severity: High]
This isn't a bug introduced by this patch, but looking at starfive_pcie_remove():

drivers/pci/controller/plda/pcie-starfive.c:starfive_pcie_remove() {
     ...
	pm_runtime_put(&pdev->dev);
	pm_runtime_disable(&pdev->dev);
	plda_pcie_host_deinit(&pcie->plda);
     ...
}

Does this leave the hardware in an active power state upon module unload?

Quoted 7 lines

Since pm_runtime_put() is asynchronous, wouldn't calling pm_runtime_disable() immediately afterward risk canceling the idle/suspend work?

Also, does tearing down the PM reference before stopping the PCIe host controller via plda_pcie_host_deinit() reverse the proper teardown order? Could this cause the device to power off while still being actively accessed by the core?

This is fixed in a separate patch [1].

Quoted 9 lines

[Severity: High]
This is a pre-existing issue, but I noticed a potential resource leak in starfive_pcie_host_init():

drivers/pci/controller/plda/pcie-starfive.c:starfive_pcie_host_init() {
     ...
	ret = starfive_pcie_enable_phy(dev, pcie);
	if (ret)
		return ret;

I do not think we need to call starfive_pcie_disable_phy() here inside
the error handling. If starfive_pcie_enable_phy() fails, its internal error path already cleans up by calling phy_exit() via the 'err_phy_on' label. Calling starfive_pcie_disable_phy() here would cause a redundant, unbalanced phy_exit() call.

Quoted 10 lines
     ...
	ret = starfive_pcie_clk_rst_init(pcie);
	if (ret)
		return ret;
     ...
}

If starfive_pcie_clk_rst_init() fails, it returns the error code directly. Does this fail to clean up the successfully initialized PHY?

This is fixed in a separate patch [2].

Quoted 13 lines

[Severity: High]
This is a pre-existing issue, but does this code swallow the regulator error?

drivers/pci/controller/plda/pcie-starfive.c:starfive_pcie_host_init() {
     ...
	if (pcie->vpcie3v3) {
		ret = regulator_enable(pcie->vpcie3v3);
		if (ret)
			dev_err_probe(dev, ret, "failed to enable vpcie3v3 regulator\n");
	}
     ...
}
Quoted 9 lines

Because there is no return statement after dev_err_probe(), the function proceeds and eventually returns success at the end. Could this cause the driver to continue operating with unpowered hardware?

Later on, starfive_pcie_host_deinit() unconditionally calls regulator_disable(). Since the regulator was not successfully enabled, would this trigger an unbalanced disable warning in the regulator core and corrupt regulator use counts?

This is also fixed in a separate patch [2].

[1] https://lore.kernel.org/linux-pci/[email protected]/T/#u
[2] https://lore.kernel.org/linux-pci/[email protected]/T/#u

author
#4 lore raw

PATCH v2 1/1 PCI: starfive: Fix unchecked pm_runtime_get_sync() in probe

In reply to Ali Tariq

pm_runtime_get_sync() is called in starfive_pcie_probe() without checking its return value. If runtime resume fails, the driver proceeds to configure PCIe hardware through regmap_update_bits(), enable clocks and resets, and power on the PHY, even though the device may not actually be powered.

pm_runtime_get_sync() also increments the usage counter even when resume fails, which would leave the counter unbalanced if this error path were later handled without additional cleanup.

Switch to pm_runtime_resume_and_get(), which balances the usage counter internally on failure, and bail out of probe before any hardware is touched if resume does not succeed.

Tested on StarFive VisionFive 2 v1.2A board.

Fixes 6168efbebace ("PCI: starfive: Enable controller runtime PM before probing host bridge")

---
Changes in v2:
- Added Fixes tag to commit message

---
 drivers/pci/controller/plda/pcie-starfive.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/pci/controller/plda/pcie-starfive.c +5 -1
index 298036c3e7f9..e9a472985b8a 100644
@@ -410,7 +410,11 @@ static int starfive_pcie_probe(struct platform_device *pdev)
 		return ret;
 
 	pm_runtime_enable(&pdev->dev);
-	pm_runtime_get_sync(&pdev->dev);
+	ret = pm_runtime_resume_and_get(&pdev->dev);
+	if (ret < 0) {
+		pm_runtime_disable(&pdev->dev);
+		return dev_err_probe(dev, ret, "failed to resume device\n");
+	}
 
 	plda->host_ops = &sf_host_ops;
 	plda->num_events = PLDA_MAX_EVENT_NUM;
Signature · 1 line
2.34.1
#5 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

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

Pre-existing issues:
- [High] Runtime PM is disabled asynchronously and prematurely during driver removal, leading to an incorrect power state and violating PM guidelines.
- [High] IRQ domains are destroyed during host deinit while their associated interrupts are still mapped and active.
--

--- Patch [1]: [PATCH v2] PCI: starfive: Fix unchecked pm_runtime_get_sync() in probe --- commit e9cae95ac8bfa924ebc45ae6433e2c2961f70890
Author: Ali Tariq <[email protected]>

PCI: starfive: Fix unchecked pm_runtime_get_sync() in probe

pm_runtime_get_sync() is called in starfive_pcie_probe() without checking its return value. If runtime resume fails, the driver proceeds to configure PCIe hardware through regmap_update_bits(), enable clocks and resets, and power on the PHY, even though the device may not actually be powered.

Quoted 7 lines
drivers/pci/controller/plda/pcie-starfive.c +0 -0
index 298036c3e7f94..e9a472985b8ac 100644
@@ -410,7 +410,11 @@ static int starfive_pcie_probe(struct platform_device *pdev)
 		return ret;
 

drivers/pci/controller/plda/pcie-starfive.c static int starfive_pcie_probe(struct platform_device *pdev) · line 420

Quoted 10 lines
drivers/pci/controller/plda/pcie-starfive.c +5 -1
 	pm_runtime_enable(&pdev->dev);
-	pm_runtime_get_sync(&pdev->dev);
+	ret = pm_runtime_resume_and_get(&pdev->dev);
+	if (ret < 0) {
+		pm_runtime_disable(&pdev->dev);
+		return dev_err_probe(dev, ret, "failed to resume device\n");
+	}
 
 	plda->host_ops = &sf_host_ops;
 	plda->num_events = PLDA_MAX_EVENT_NUM;

While reviewing this patch, I noticed a couple of pre-existing issues in related code:

Quoted 9 lines

static void starfive_pcie_remove(struct platform_device *pdev)

{
	struct starfive_jh7110_pcie *pcie = platform_get_drvdata(pdev);

	pm_runtime_put(&pdev->dev);
	pm_runtime_disable(&pdev->dev);
	plda_pcie_host_deinit(&pcie->plda);
	platform_set_drvdata(pdev, NULL);
}

[Severity: High]
This isn't a bug introduced by this patch, but should this use pm_runtime_put_sync() instead?

In starfive_pcie_remove(), pm_runtime_put() queues an asynchronous idle request, but pm_runtime_disable() is called immediately after. This cancels the pending request, leaving the device in an active power state indefinitely.

Additionally, this teardown happens before plda_pcie_host_deinit(). If pm_runtime_put_sync() were used, the hardware would drop power immediately, causing plda_pcie_host_deinit() to access unpowered registers and potentially crash the system.

Should the host bridge be torn down first, and only then synchronously power off the device?

Also in drivers/pci/controller/plda/pcie-plda-host.c:

Quoted 11 lines

static void plda_pcie_irq_domain_deinit(struct plda_pcie_rp *pcie)

{
	irq_set_chained_handler_and_data(pcie->irq, NULL, NULL);
	irq_set_chained_handler_and_data(pcie->msi_irq, NULL, NULL);
	irq_set_chained_handler_and_data(pcie->intx_irq, NULL, NULL);

	irq_domain_remove(pcie->msi.dev_domain);

	irq_domain_remove(pcie->intx_domain);
	irq_domain_remove(pcie->event_domain);
}

[Severity: High]
This is a pre-existing issue, but are we destroying the IRQ domains while their associated interrupts are still mapped and active?

During driver removal, starfive_pcie_remove() calls plda_pcie_host_deinit(), which synchronously removes the event and INTx IRQ domains via plda_pcie_irq_domain_deinit().

However, the interrupts mapped from these domains are never unmapped via irq_dispose_mapping(). The actual interrupt handlers requested via devm_request_irq() in plda_init_interrupts() haven't been freed yet, because devm teardown occurs strictly after the driver remove() callback returns.

Is it safe to destroy these domains with active mappings and bound interrupts, or could this lead to issues during subsequent devm teardown?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#7 lore raw
In reply to Ali Tariq
Quoted 11 lines · Ali Tariq

pm_runtime_get_sync() is called in starfive_pcie_probe() without checking its return value. If runtime resume fails, the driver proceeds to configure PCIe hardware through regmap_update_bits(), enable clocks and resets, and power on the PHY, even though the device may not actually be powered.

pm_runtime_get_sync() also increments the usage counter even when resume fails, which would leave the counter unbalanced if this error path were later handled without additional cleanup.

[...]

Applied, thanks!

[1/1] PCI: starfive: Fix unchecked pm_runtime_get_sync() in probe

      commit: aaae917990623a6ca6b638557056606a1ae4a8d6

Best regards,

Signature · 1 line
மணிவண்ணன் சதாசிவம்