Linux PCI subsystem development
 help / color / mirror / Atom feed
* [PATCH] PCI: qcom: Do not power off a PHY whose power-on failed
@ 2026-11-21 11:37 Muhammad Huda Nugroho
  2026-11-21 11:41 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Muhammad Huda Nugroho @ 2026-11-21 11:37 UTC (permalink / raw)
  To: Manivannan Sadhasivam, Lorenzo Pieralisi, Krzysztof Wilczynski,
	Rob Herring, Bjorn Helgaas
  Cc: linux-pci, linux-arm-msm, linux-kernel

qcom_pcie_phy_power_on() unwinds a failed phy_power_on() by calling
qcom_pcie_phy_power_off(), which walks the whole port list - including
the port that just failed, and every port the loop has not reached yet.
The phy_set_mode_ext() failure path returns without unwinding at all,
leaking the ports already powered on.

The first of those is not a leak but a refcount underflow.
phy_power_off() decrements phy->power_count unconditionally and invokes
the provider's ->power_off only at a count of 1, while phy_power_on()
invokes ->power_on only at a count of 0. Releasing a reference that was
never acquired therefore drives the count to -1, and the next
phy_power_on() returns success having silently skipped powering the PHY
up: no error, no message, just a PHY that is never initialised again.

The damage outlives the failed attempt, which is what makes it worth
fixing. On a single-port controller the very first failure is enough to
reach -1. It is normally invisible because a failed host init at probe
time is fatal anyway, but it becomes reachable as soon as anything
retries host init at runtime, as system resume does - at which point the
controller comes back with a PHY the core believes is already on.

Unwind with list_for_each_entry_continue_reverse() so that only the
ports successfully powered on are powered off, and route the
phy_set_mode_ext() failure through the same unwind. The PHY core already
releases the failed acquisition itself, so the failing port must not be
released here.

Signed-off-by: Muhammad Huda Nugroho <mhudan21@gmail.com>
---
Notes for reviewers, not for the commit message:

A structurally similar unwind fix for the tegra host bridge was declined in
August 2026 ("PCI: tegra: Unwind partial PHY ...", Pengpeng Hou) on the
grounds that a phy_power_on() failure means a broken system that an unwind
will not save. That reasoning answers a leak argument, and it is fair
against one - a PHY left powered after a fatal probe failure changes
nothing. It is not the argument here. The problem in this driver is the
opposite direction: a reference released that was never taken, leaving
power_count below zero, so the next phy_power_on() succeeds without
powering the PHY. That survives the failed attempt and breaks the next
legitimate one.

The same underflow was described by Vladimir Oltean in March 2026 while
reviewing "scsi: ufs: qcom: Drop the PHY power_count check" - "If 0, the
power_count would become negative after phy_power_off(), and a subsequent
phy_power_on() would merely bump it back to 0" - and the ufs patch was
withdrawn in favour of per-driver state tracking. This patch takes the
other available route for a consumer that must not inspect power_count:
only release what it acquired.

Against mainline master as of 2026-11-21.

Found by reading the error path, not by observing it fire. The underflow
mechanism is from the PHY core's own accounting in phy_power_on() and
phy_power_off(); no hardware reproduction of the resume case is claimed.
I would rather say so than imply a bug report I do not have.

 drivers/pci/controller/dwc/pcie-qcom.c | 23 ++++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)

--- a/drivers/pci/controller/dwc/pcie-qcom.c
+++ b/drivers/pci/controller/dwc/pcie-qcom.c
@@ -1445,16 +1445,29 @@
 	list_for_each_entry(port, &pcie->ports, list) {
 		ret = phy_set_mode_ext(port->phy, PHY_MODE_PCIE, PHY_MODE_PCIE_RC);
 		if (ret)
-			return ret;
+			goto err_power_off;
 
 		ret = phy_power_on(port->phy);
-		if (ret) {
-			qcom_pcie_phy_power_off(pcie);
-			return ret;
-		}
+		if (ret)
+			goto err_power_off;
 	}
 
 	return 0;
+
+err_power_off:
+	/*
+	 * Only release what was actually acquired. phy_power_off() decrements
+	 * phy->power_count unconditionally and invokes the provider's callback
+	 * only at 1, while phy_power_on() invokes it only at 0, so releasing a
+	 * port whose phy_power_on() failed (or was never reached) drives the
+	 * count negative - after which the next phy_power_on() silently skips
+	 * powering the PHY up at all. The PHY core already unwinds the failed
+	 * acquisition itself.
+	 */
+	list_for_each_entry_continue_reverse(port, &pcie->ports, list)
+		phy_power_off(port->phy);
+
+	return ret;
 }
 
 static void qcom_pcie_configure_ports(struct qcom_pcie *pcie)
-- 
2.51.0

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

* Re: [PATCH] PCI: qcom: Do not power off a PHY whose power-on failed
  2026-11-21 11:37 [PATCH] PCI: qcom: Do not power off a PHY whose power-on failed Muhammad Huda Nugroho
@ 2026-11-21 11:41 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-21 11:41 UTC (permalink / raw)
  To: Muhammad Huda Nugroho; +Cc: linux-pci

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] PCI: qcom: missing Fixes trailer for qcom_pcie_phy_power_on()
  unwind fix
--

commit e8e6c447938093b506280d02a806164618cf331c
Author: Muhammad Huda Nugroho <mhudan21@gmail.com>

> PCI: qcom: Do not power off a PHY whose power-on failed
>
> qcom_pcie_phy_power_on() unwinds a failed phy_power_on() by calling
> qcom_pcie_phy_power_off(), which walks the whole port list - including
> the port that just failed, and every port the loop has not reached yet.
> The phy_set_mode_ext() failure path returns without unwinding at all,
> leaking the ports already powered on.
>
> The first of those is not a leak but a refcount underflow.
> phy_power_off() decrements phy->power_count unconditionally and invokes
> the provider's ->power_off only at a count of 1, while phy_power_on()
> invokes ->power_on only at a count of 0. Releasing a reference that was
> never acquired therefore drives the count to -1, and the next
> phy_power_on() returns success having silently skipped powering the PHY
> up: no error, no message, just a PHY that is never initialised again.
>
> The damage outlives the failed attempt, which is what makes it worth
> fixing. On a single-port controller the very first failure is enough to
> reach -1. It is normally invisible because a failed host init at probe
> time is fatal anyway, but it becomes reachable as soon as anything
> retries host init at runtime, as system resume does - at which point the
> controller comes back with a PHY the core believes is already on.
>
> Unwind with list_for_each_entry_continue_reverse() so that only the
> ports successfully powered on are powered off, and route the
> phy_set_mode_ext() failure through the same unwind. The PHY core already
> releases the failed acquisition itself, so the failing port must not be
> released here.
>
> Signed-off-by: Muhammad Huda Nugroho <mhudan21@gmail.com>

[Severity: Low]
This fixes the error unwind in qcom_pcie_phy_power_on().  Should this
include:

  Fixes: a2fbecdbbb9d ("PCI: qcom: Add support for parsing the new Root Port binding")

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261121113721.7OL6u%mhudan21@gmail.com?part=1

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

end of thread, other threads:[~2026-11-21 11:41 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-21 11:37 [PATCH] PCI: qcom: Do not power off a PHY whose power-on failed Muhammad Huda Nugroho
2026-11-21 11:41 ` sashiko-bot

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