mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] PCI: starfive: Fix resource leaks on error paths in host_init()
@ 2026-07-14 11:30 Ali Tariq
  2026-07-15 10:26 ` Markus Elfring
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Ali Tariq @ 2026-07-14 11:30 UTC (permalink / raw)
  To: Kevin Xie
  Cc: Ali Tariq, Lorenzo Pieralisi, Krzysztof Wilczyński,
	Manivannan Sadhasivam, Rob Herring, Bjorn Helgaas,
	open list:PCIE DRIVER FOR STARFIVE JH71x0, open list

starfive_pcie_host_init() acquires the PHY, clocks/resets, and an
optional regulator in sequence, but does not correctly unwind these
resources when a later step fails.

If starfive_pcie_clk_rst_init() fails after the PHY has already been
successfully enabled, the function returns directly without disabling
the PHY, leaking it and leaving it powered.

If regulator_enable() fails for the optional vpcie3v3 regulator, the
failure is only logged; the function falls through and returns
success, leaving the driver believing the regulator is enabled while
continuing to configure PCIe hardware that may be unpowered. This
also leaves the clocks and PHY enabled with nothing to clean them up.

Disable the PHY on the clk/reset failure path, and disable the
clocks/resets and PHY, then return the error, if the regulator fails
to enable.

Build-tested and boot-tested on StarFive VisionFive 2 v1.2A

Signed-off-by: Ali Tariq <alitariq45892@gmail.com>
---
 drivers/pci/controller/plda/pcie-starfive.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/drivers/pci/controller/plda/pcie-starfive.c b/drivers/pci/controller/plda/pcie-starfive.c
index ba8ef7842e35..156b31eef868 100644
--- a/drivers/pci/controller/plda/pcie-starfive.c
+++ b/drivers/pci/controller/plda/pcie-starfive.c
@@ -303,13 +303,19 @@ static int starfive_pcie_host_init(struct plda_pcie_rp *plda)
 			   STG_SYSCON_CLKREQ, STG_SYSCON_CLKREQ);
 
 	ret = starfive_pcie_clk_rst_init(pcie);
-	if (ret)
+	if (ret) {
+		starfive_pcie_disable_phy(pcie);
 		return ret;
+	}
 
 	if (pcie->vpcie3v3) {
 		ret = regulator_enable(pcie->vpcie3v3);
-		if (ret)
+		if (ret) {
 			dev_err_probe(dev, ret, "failed to enable vpcie3v3 regulator\n");
+			starfive_pcie_clk_rst_deinit(pcie);
+			starfive_pcie_disable_phy(pcie);
+			return ret;
+		}
 	}
 
 	if (pcie->reset_gpio)
-- 
2.34.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] PCI: starfive: Fix resource leaks on error paths in host_init()
  2026-07-14 11:30 [PATCH] PCI: starfive: Fix resource leaks on error paths in host_init() Ali Tariq
@ 2026-07-15 10:26 ` Markus Elfring
  2026-07-15 13:45 ` [PATCH v2] " Ali Tariq
  2026-07-16 10:20 ` [PATCH v3] " Ali Tariq
  2 siblings, 0 replies; 6+ messages in thread
From: Markus Elfring @ 2026-07-15 10:26 UTC (permalink / raw)
  To: Ali Tariq, linux-pci, Kevin Xie
  Cc: LKML, Bjorn Helgaas, Krzysztof Wilczyński,
	Lorenzo Pieralisi, Manivannan Sadhasivam, Rob Herring

…
> Disable the PHY on the clk/reset failure path, and disable the
> clocks/resets and PHY, then return the error, if the regulator fails
> to enable.
…

Please avoid a bit of duplicate source code by using an additional label.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?h=v7.2-rc3#n526

Regards,
Markus

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v2] PCI: starfive: Fix resource leaks on error paths in host_init()
  2026-07-14 11:30 [PATCH] PCI: starfive: Fix resource leaks on error paths in host_init() Ali Tariq
  2026-07-15 10:26 ` Markus Elfring
@ 2026-07-15 13:45 ` Ali Tariq
  2026-07-15 18:21   ` Markus Elfring
  2026-07-16 10:20 ` [PATCH v3] " Ali Tariq
  2 siblings, 1 reply; 6+ messages in thread
From: Ali Tariq @ 2026-07-15 13:45 UTC (permalink / raw)
  To: kevin.xie
  Cc: lpieralisi, kwilczynski, mani, robh, bhelgaas, Markus.Elfring,
	linux-pci, linux-kernel, Ali Tariq

starfive_pcie_host_init() acquires the PHY, clocks/resets, and an
optional regulator in sequence, but does not correctly unwind these
resources when a later step fails.

If starfive_pcie_clk_rst_init() fails after the PHY has already been
successfully enabled, the function returns directly without disabling
the PHY, leaking it and leaving it powered.

If regulator_enable() fails for the optional vpcie3v3 regulator, the
failure is only logged; the function falls through and returns
success, leaving the driver believing the regulator is enabled while
continuing to configure PCIe hardware that may be unpowered. This
also leaves the clocks and PHY enabled with nothing to clean them up.

Disable the PHY on the clk/reset failure path, and disable the
clocks/resets and PHY, then return the error, if the regulator fails
to enable.

Build-tested and boot-tested on StarFive VisionFive 2 v1.2A

Signed-off-by: Ali Tariq <alitariq45892@gmail.com>
---
Changes in v2:
- Use a shared error-handling label instead of duplicating cleanup code
---
 drivers/pci/controller/plda/pcie-starfive.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/drivers/pci/controller/plda/pcie-starfive.c b/drivers/pci/controller/plda/pcie-starfive.c
index ba8ef7842e35..fab44054a5de 100644
--- a/drivers/pci/controller/plda/pcie-starfive.c
+++ b/drivers/pci/controller/plda/pcie-starfive.c
@@ -304,12 +304,14 @@ static int starfive_pcie_host_init(struct plda_pcie_rp *plda)
 
 	ret = starfive_pcie_clk_rst_init(pcie);
 	if (ret)
-		return ret;
+		goto err_disable_phy;
 
 	if (pcie->vpcie3v3) {
 		ret = regulator_enable(pcie->vpcie3v3);
-		if (ret)
+		if (ret) {
 			dev_err_probe(dev, ret, "failed to enable vpcie3v3 regulator\n");
+			goto err_clk_rst;
+		}
 	}
 
 	if (pcie->reset_gpio)
@@ -379,6 +381,13 @@ static int starfive_pcie_host_init(struct plda_pcie_rp *plda)
 		dev_info(dev, "port link down\n");
 
 	return 0;
+
+err_clk_rst:
+	starfive_pcie_clk_rst_deinit(pcie);
+err_disable_phy:
+	starfive_pcie_disable_phy(pcie);
+
+	return ret;
 }
 
 static const struct plda_pcie_host_ops sf_host_ops = {
-- 
2.34.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] PCI: starfive: Fix resource leaks on error paths in host_init()
  2026-07-15 13:45 ` [PATCH v2] " Ali Tariq
@ 2026-07-15 18:21   ` Markus Elfring
  0 siblings, 0 replies; 6+ messages in thread
From: Markus Elfring @ 2026-07-15 18:21 UTC (permalink / raw)
  To: Ali Tariq, linux-pci, Kevin Xie
  Cc: LKML, Bjorn Helgaas, Krzysztof Wilczyński,
	Lorenzo Pieralisi, Manivannan Sadhasivam, Rob Herring

…
> Disable the PHY on the clk/reset failure path, and disable the
> clocks/resets and PHY, then return the error, if the regulator fails
> to enable.…

How do you think about to add any tags (like “Fixes” and “Cc”) accordingly?

See also:
* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v7.2-rc3#n145
* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/stable-kernel-rules.rst?h=v7.2-rc3#n34


Regards,
Markus

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v3] PCI: starfive: Fix resource leaks on error paths in host_init()
  2026-07-14 11:30 [PATCH] PCI: starfive: Fix resource leaks on error paths in host_init() Ali Tariq
  2026-07-15 10:26 ` Markus Elfring
  2026-07-15 13:45 ` [PATCH v2] " Ali Tariq
@ 2026-07-16 10:20 ` Ali Tariq
  2026-07-16 16:53   ` Manivannan Sadhasivam
  2 siblings, 1 reply; 6+ messages in thread
From: Ali Tariq @ 2026-07-16 10:20 UTC (permalink / raw)
  To: Kevin Xie
  Cc: Lorenzo Pieralisi, Krzysztof Wilczyński,
	Manivannan Sadhasivam, Rob Herring, Bjorn Helgaas, linux-pci,
	linux-kernel, Markus Elfring, Ali Tariq, stable

starfive_pcie_host_init() acquires the PHY, clocks/resets, and an
optional regulator in sequence, but does not correctly unwind these
resources when a later step fails.

If starfive_pcie_clk_rst_init() fails after the PHY has already been
successfully enabled, the function returns directly without disabling
the PHY, leaking it and leaving it powered.

If regulator_enable() fails for the optional vpcie3v3 regulator, the
failure is only logged; the function falls through and returns
success, leaving the driver believing the regulator is enabled while
continuing to configure PCIe hardware that may be unpowered. This
also leaves the clocks and PHY enabled with nothing to clean them up.

Disable the PHY on the clk/reset failure path, and disable the
clocks/resets and PHY, then return the error, if the regulator fails
to enable.

Build-tested and boot-tested on StarFive VisionFive 2 v1.2A

Fixes: 05a75df4182e ("PCI: starfive: Use regulator APIs to control the 3v3 power supply of PCIe slots")
Fixes: 39b91eb40c6a ("PCI: starfive: Add JH7110 PCIe controller")
Signed-off-by: Ali Tariq <alitariq45892@gmail.com>
Cc: stable@vger.kernel.org
---
Changes in v3:
- Add Fixes: tags for both introducing commits
- Add Cc: stable@vger.kernel.org

Changes in v2:
- Use a shared error-handling label instead of duplicating cleanup code
---
 drivers/pci/controller/plda/pcie-starfive.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/drivers/pci/controller/plda/pcie-starfive.c b/drivers/pci/controller/plda/pcie-starfive.c
index ba8ef7842e35..fab44054a5de 100644
--- a/drivers/pci/controller/plda/pcie-starfive.c
+++ b/drivers/pci/controller/plda/pcie-starfive.c
@@ -304,12 +304,14 @@ static int starfive_pcie_host_init(struct plda_pcie_rp *plda)
 
 	ret = starfive_pcie_clk_rst_init(pcie);
 	if (ret)
-		return ret;
+		goto err_disable_phy;
 
 	if (pcie->vpcie3v3) {
 		ret = regulator_enable(pcie->vpcie3v3);
-		if (ret)
+		if (ret) {
 			dev_err_probe(dev, ret, "failed to enable vpcie3v3 regulator\n");
+			goto err_clk_rst;
+		}
 	}
 
 	if (pcie->reset_gpio)
@@ -379,6 +381,13 @@ static int starfive_pcie_host_init(struct plda_pcie_rp *plda)
 		dev_info(dev, "port link down\n");
 
 	return 0;
+
+err_clk_rst:
+	starfive_pcie_clk_rst_deinit(pcie);
+err_disable_phy:
+	starfive_pcie_disable_phy(pcie);
+
+	return ret;
 }
 
 static const struct plda_pcie_host_ops sf_host_ops = {
-- 
2.34.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v3] PCI: starfive: Fix resource leaks on error paths in host_init()
  2026-07-16 10:20 ` [PATCH v3] " Ali Tariq
@ 2026-07-16 16:53   ` Manivannan Sadhasivam
  0 siblings, 0 replies; 6+ messages in thread
From: Manivannan Sadhasivam @ 2026-07-16 16:53 UTC (permalink / raw)
  To: Kevin Xie, Ali Tariq
  Cc: Lorenzo Pieralisi, Krzysztof Wilczyński, Rob Herring,
	Bjorn Helgaas, linux-pci, linux-kernel, Markus Elfring, stable


On Thu, 16 Jul 2026 15:20:53 +0500, Ali Tariq wrote:
> starfive_pcie_host_init() acquires the PHY, clocks/resets, and an
> optional regulator in sequence, but does not correctly unwind these
> resources when a later step fails.
> 
> If starfive_pcie_clk_rst_init() fails after the PHY has already been
> successfully enabled, the function returns directly without disabling
> the PHY, leaking it and leaving it powered.
> 
> [...]

Applied, thanks!

[1/1] PCI: starfive: Fix resource leaks on error paths in host_init()
      commit: 22877a061f81c5d58041e384b3131684bec636b9

Best regards,
-- 
மணிவண்ணன் சதாசிவம்



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-07-16 16:53 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-07-14 11:30 [PATCH] PCI: starfive: Fix resource leaks on error paths in host_init() Ali Tariq
2026-07-15 10:26 ` Markus Elfring
2026-07-15 13:45 ` [PATCH v2] " Ali Tariq
2026-07-15 18:21   ` Markus Elfring
2026-07-16 10:20 ` [PATCH v3] " Ali Tariq
2026-07-16 16:53   ` Manivannan Sadhasivam

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox