[RFC PATCH 3/8] PCI/PM: Convert pci_bridge_d3_update() recursion to iteration
From: Pavol Sakac
Date: Fri Sep 11 2026 - 08:34:58 EST
pci_bridge_d3_update() propagates a bridge_d3 change to upstream bridges
by tail recursion: when a bridge's bridge_d3 value changes, the function
calls itself with that bridge as the new device.
Convert the tail recursion into an iterative loop. Each level recomputes
"remove" and d3cold_ok exactly as the recursive call did for its own
device, and the early returns become loop exits. No functional change
intended.
An upcoming change serializes this update with a mutex; the iterative
form lets that mutex be taken once per external call instead of once per
bridge level.
Assisted-by: LLM
Signed-off-by: Pavol Sakac <sakacpav@xxxxxxxxx>
---
drivers/pci/pci.c | 67 +++++++++++++++++++++++++----------------------
1 file changed, 35 insertions(+), 32 deletions(-)
diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
index b2879a6be5f8..c62a315c0b4c 100644
--- a/drivers/pci/pci.c
+++ b/drivers/pci/pci.c
@@ -3104,46 +3104,49 @@ static int pci_dev_check_d3cold(struct pci_dev *dev, void *data)
*/
void pci_bridge_d3_update(struct pci_dev *dev)
{
- bool remove = !device_is_registered(&dev->dev);
struct pci_dev *bridge;
- bool d3cold_ok = true;
- bridge = pci_upstream_bridge(dev);
- if (!bridge || !pci_bridge_d3_possible(bridge))
- return;
+ while ((bridge = pci_upstream_bridge(dev)) &&
+ pci_bridge_d3_possible(bridge)) {
+ bool remove = !device_is_registered(&dev->dev);
+ bool d3cold_ok = true;
- /*
- * If D3 is currently allowed for the bridge, removing one of its
- * children won't change that.
- */
- if (remove && bridge->bridge_d3)
- return;
+ /*
+ * If D3 is currently allowed for the bridge, removing one of
+ * its children won't change that.
+ */
+ if (remove && bridge->bridge_d3)
+ break;
- /*
- * If D3 is currently allowed for the bridge and a child is added or
- * changed, disallowance of D3 can only be caused by that child, so
- * we only need to check that single device, not any of its siblings.
- *
- * If D3 is currently not allowed for the bridge, checking the device
- * first may allow us to skip checking its siblings.
- */
- if (!remove)
- pci_dev_check_d3cold(dev, &d3cold_ok);
+ /*
+ * If D3 is currently allowed for the bridge and a child is
+ * added or changed, disallowance of D3 can only be caused by
+ * that child, so we only need to check that single device,
+ * not any of its siblings.
+ *
+ * If D3 is currently not allowed for the bridge, checking the
+ * device first may allow us to skip checking its siblings.
+ */
+ if (!remove)
+ pci_dev_check_d3cold(dev, &d3cold_ok);
- /*
- * If D3 is currently not allowed for the bridge, this may be caused
- * either by the device being changed/removed or any of its siblings,
- * so we need to go through all children to find out if one of them
- * continues to block D3.
- */
- if (d3cold_ok && !bridge->bridge_d3)
- pci_walk_bus(bridge->subordinate, pci_dev_check_d3cold,
- &d3cold_ok);
+ /*
+ * If D3 is currently not allowed for the bridge, this may be
+ * caused either by the device being changed/removed or any of
+ * its siblings, so we need to go through all children to find
+ * out if one of them continues to block D3.
+ */
+ if (d3cold_ok && !bridge->bridge_d3)
+ pci_walk_bus(bridge->subordinate, pci_dev_check_d3cold,
+ &d3cold_ok);
+
+ if (bridge->bridge_d3 == d3cold_ok)
+ break;
- if (bridge->bridge_d3 != d3cold_ok) {
bridge->bridge_d3 = d3cold_ok;
+
/* Propagate change to upstream bridges */
- pci_bridge_d3_update(bridge);
+ dev = bridge;
}
}
--
2.47.3