Skip to contentSkip to conversation

PCI/pwrctrl: Fix device node leak in pci_pwrctrl_is_required()

PATCH In review Felix Gu 4 participants 6 messages Mar 23 – Mar 24

Subsystems (2)
lore
Share & tools
lore

Felix Gu

author
#0 lore raw

PATCH 1/1 PCI/pwrctrl: Fix device node leak in pci_pwrctrl_is_required()

The for_each_endpoint_of_node() macro requires calling of_node_put() on the endpoint node when breaking out of the loop early.

Add of_node_put(endpoint) before the early return to properly release the reference.

Fixes cf3287fb2c1f ("PCI/pwrctrl: Ensure that remote endpoint node parent has supply requirement")
---
 drivers/pci/pwrctrl/core.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/pci/pwrctrl/core.c +3 -1
index 7754baed67f2..97cff5b8ca88 100644
@@ -299,8 +299,10 @@ static bool pci_pwrctrl_is_required(struct device_node *np)
 			struct device_node *remote __free(device_node) =
 				of_graph_get_remote_port_parent(endpoint);
 			if (remote) {
-				if (of_pci_supply_present(remote))
+				if (of_pci_supply_present(remote)) {
+					of_node_put(endpoint);
 					return true;
+				}
 			}
 		}
 	}

---
base-commit: 785f0eb2f85decbe7c1ef9ae922931f0194ffc2e
change-id: 20260323-pwctrl-fe840d733443

Best regards,

Signature · 1 line
Felix Gu <[email protected]>
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Felix Gu

The for_each_endpoint_of_node() macro requires calling of_node_put() on the endpoint node when breaking out of the loop early.

Add of_node_put(endpoint) before the early return to properly release the reference.

Fixes cf3287fb2c1f ("PCI/pwrctrl: Ensure that remote endpoint node parent has supply requirement")

Err...

- Mani

Quoted 29 lines
---
 drivers/pci/pwrctrl/core.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/pci/pwrctrl/core.c +3 -2
index 7754baed67f2..97cff5b8ca88 100644
@@ -299,8 +299,10 @@ static bool pci_pwrctrl_is_required(struct device_node *np)
 			struct device_node *remote __free(device_node) =
 				of_graph_get_remote_port_parent(endpoint);
 			if (remote) {
-				if (of_pci_supply_present(remote))
+				if (of_pci_supply_present(remote)) {
+					of_node_put(endpoint);
 					return true;
+				}
 			}
 		}
 	}
 
---

base-commit: 785f0eb2f85decbe7c1ef9ae922931f0194ffc2e
change-id: 20260323-pwctrl-fe840d733443

Best regards,

Signature · 1 line
Felix Gu <[email protected]>
Signature · 1 line
மணிவண்ணன் சதாசிவம்
maintainer Reviewed-by on v1 1/1
#2 lore raw
In reply to Felix Gu
Quoted 33 lines · Felix Gu

The for_each_endpoint_of_node() macro requires calling of_node_put() on the endpoint node when breaking out of the loop early.

Add of_node_put(endpoint) before the early return to properly release the reference.

Fixes cf3287fb2c1f ("PCI/pwrctrl: Ensure that remote endpoint node parent has supply requirement")
---
 drivers/pci/pwrctrl/core.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/pci/pwrctrl/core.c +3 -2
index 7754baed67f2..97cff5b8ca88 100644
@@ -299,8 +299,10 @@ static bool pci_pwrctrl_is_required(struct device_node *np)
 			struct device_node *remote __free(device_node) =
 				of_graph_get_remote_port_parent(endpoint);
 			if (remote) {
-				if (of_pci_supply_present(remote))
+				if (of_pci_supply_present(remote)) {
+					of_node_put(endpoint);
 					return true;
+				}
 			}
 		}
 	}
 
---

base-commit: 785f0eb2f85decbe7c1ef9ae922931f0194ffc2e
change-id: 20260323-pwctrl-fe840d733443

Quoted 5 lines

Best regards,
--
Felix Gu <[email protected]>

#3 lore raw
In reply to Felix Gu
Quoted 8 lines · Felix Gu

The for_each_endpoint_of_node() macro requires calling of_node_put() on the endpoint node when breaking out of the loop early.

Add of_node_put(endpoint) before the early return to properly release the reference.

Fixes cf3287fb2c1f ("PCI/pwrctrl: Ensure that remote endpoint node parent has supply requirement")

cf3287fb2c1f appeared in v7.0-rc5, so I put this on pci/for-linus for v7.0, thanks!

Would you also take a look at pwrseq_pcie_m2_match()? It can similarly return early from a for_each_endpoint_of_node() loop, so I'm concerned it may have a similar issue.

Quoted 29 lines
---
 drivers/pci/pwrctrl/core.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/pci/pwrctrl/core.c +3 -2
index 7754baed67f2..97cff5b8ca88 100644
@@ -299,8 +299,10 @@ static bool pci_pwrctrl_is_required(struct device_node *np)
 			struct device_node *remote __free(device_node) =
 				of_graph_get_remote_port_parent(endpoint);
 			if (remote) {
-				if (of_pci_supply_present(remote))
+				if (of_pci_supply_present(remote)) {
+					of_node_put(endpoint);
 					return true;
+				}
 			}
 		}
 	}
 
---

base-commit: 785f0eb2f85decbe7c1ef9ae922931f0194ffc2e
change-id: 20260323-pwctrl-fe840d733443

Best regards,

Signature · 1 line
Felix Gu <[email protected]>
author
#4 lore raw
Quoted 11 lines · Bjorn Helgaas
Quoted 8 lines · Felix Gu

The for_each_endpoint_of_node() macro requires calling of_node_put() on the endpoint node when breaking out of the loop early.

Add of_node_put(endpoint) before the early return to properly release the reference.

Fixes cf3287fb2c1f ("PCI/pwrctrl: Ensure that remote endpoint node parent has supply requirement")
Quoted 6 lines

cf3287fb2c1f appeared in v7.0-rc5, so I put this on pci/for-linus for v7.0, thanks!

Would you also take a look at pwrseq_pcie_m2_match()? It can similarly return early from a for_each_endpoint_of_node() loop, so I'm concerned it may have a similar issue.

Hi Bjorn,
pwrseq_pcie_m2_match should be fine, endpoint is marked as __free(device_node), it will be called of_node_put() automatically when returning early.

Best regards,
Felix

#5 lore raw
Quoted 15 lines · Felix Gu
Quoted 14 lines · Bjorn Helgaas
Quoted 8 lines · Felix Gu

The for_each_endpoint_of_node() macro requires calling of_node_put() on the endpoint node when breaking out of the loop early.

Add of_node_put(endpoint) before the early return to properly release the reference.

Fixes cf3287fb2c1f ("PCI/pwrctrl: Ensure that remote endpoint node parent has supply requirement")

cf3287fb2c1f appeared in v7.0-rc5, so I put this on pci/for-linus for v7.0, thanks!

Quoted 7 lines
Quoted 3 lines

Would you also take a look at pwrseq_pcie_m2_match()? It can similarly return early from a for_each_endpoint_of_node() loop, so I'm concerned it may have a similar issue.

pwrseq_pcie_m2_match should be fine, endpoint is marked as __free(device_node), it will be called of_node_put() automatically when returning early.

Thanks, Felix, I indeed missed the __free() on the declaration.

For anyone else following along (and me the next time I trip over this, since the connection from __free() to of_node_put() is somewhat obscure), here's the connection:

  pwrseq_pcie_m2_match(...)
  {
    struct device_node *endpoint __free(device_node) = NULL;
    for_each_endpoint_of_node(ctx->of_node, endpoint) {
      if (...)
        return PWRSEQ_MATCH_OK;
    }
  }
  $ git grep "DEFINE_FREE(device_node"
  include/linux/of.h:DEFINE_FREE(device_node, struct device_node *, if (_T) of_node_put(_T))