Skip to contentSkip to conversation

PCI: qcom: Do not power off a PHY whose power-on failed

PATCH New Muhammad Huda Nugroho 2 participants 2 messages Nov 21

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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.

---
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(-)
drivers/pci/controller/dwc/pcie-qcom.c +18 -5
@@ -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)
Signature · 1 line
2.51.0
#1 lore raw
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 --

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 <[email protected]>

Quoted 23 lines

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.

Quoted 7 lines

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.

[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")
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/20261121113721.7OL6u%[email protected]?part=1