Skip to contentSkip to conversation

PCI: mobiveil: Fix device_node leak in ls_g4_pcie_probe()

PATCH In review Vishal Satle 3 participants 5 messages Oct 27 – Nov 18

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

PATCH 1/1 PCI: mobiveil: Fix device_node leak in ls_g4_pcie_probe()

ls_g4_pcie_probe() calls of_parse_phandle() to verify the presence of the "msi-parent" device node. of_parse_phandle() increments the reference count of the returned device_node.

Because the return value was evaluated directly as a boolean condition, the acquired reference was never released, leaking the MSI controller node reference count on probe.

Assign the resolved node to a local variable and call of_node_put() once the existence check is complete.

---
 drivers/pci/controller/mobiveil/pcie-layerscape-gen4.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/pci/controller/mobiveil/pcie-layerscape-gen4.c +4 -1
index 4919b27ea..5b1862fe0 100644
@@ -199,12 +199,15 @@ static int __init ls_g4_pcie_probe(struct platform_device *pdev)
 	struct mobiveil_pcie *mv_pci;
 	struct ls_g4_pcie *pcie;
 	struct device_node *np = dev->of_node;
+	struct device_node *msi_node;
 	int ret;
 
-	if (!of_parse_phandle(np, "msi-parent", 0)) {
+	msi_node = of_parse_phandle(np, "msi-parent", 0);
+	if (!msi_node) {
 		dev_err(dev, "Failed to find msi-parent\n");
 		return -EINVAL;
 	}
+	of_node_put(msi_node);
 
 	bridge = devm_pci_alloc_host_bridge(dev, sizeof(*pcie));
 	if (!bridge)
Signature · 1 line
2.53.0
#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 8 lines

ls_g4_pcie_probe() calls of_parse_phandle() to verify the presence of the "msi-parent" device node. of_parse_phandle() increments the reference count of the returned device_node.

Because the return value was evaluated directly as a boolean condition, the acquired reference was never released, leaking the MSI controller node reference count on probe.

Quoted 6 lines

Assign the resolved node to a local variable and call of_node_put() once the existence check is complete.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Vishal Satle
Quoted 4 lines · Vishal Satle

ls_g4_pcie_probe() calls of_parse_phandle() to verify the presence of the "msi-parent" device node. of_parse_phandle() increments the reference count of the returned device_node.

Quoted 9 lines

Because the return value was evaluated directly as a boolean condition, the acquired reference was never released, leaking the MSI controller node reference count on probe.

Assign the resolved node to a local variable and call of_node_put() once the existence check is complete.

Why? This is not a critical bug fix to backport.

Quoted 10 lines
---
 drivers/pci/controller/mobiveil/pcie-layerscape-gen4.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/pci/controller/mobiveil/pcie-layerscape-gen4.c +0 -0
index 4919b27ea..5b1862fe0 100644

drivers/pci/controller/mobiveil/pcie-layerscape-gen4.c static int __init ls_g4_pcie_probe(struct platform_device *pdev) · line 206

Quoted 9 lines
drivers/pci/controller/mobiveil/pcie-layerscape-gen4.c +3 -1
@@ -199,12 +199,15 @@ static int __init ls_g4_pcie_probe(struct platform_device *pdev)
 	struct mobiveil_pcie *mv_pci;
 	struct ls_g4_pcie *pcie;
 	struct device_node *np = dev->of_node;
+	struct device_node *msi_node;
 	int ret;
 
-	if (!of_parse_phandle(np, "msi-parent", 0)) {
+	msi_node = of_parse_phandle(np, "msi-parent", 0);
+	if (!msi_node) {

You should just use of_property_present() and be done with it. The driver is not using the phandle and it shouldn't care if it resolves to a proper node or not as it will be verified by the irqchip layer.

But that being said, I really doubt whether we need this check or not. None of the other controller drivers are having this check. If this property is absent, then MSIs won't work, but INTx will continue to work. So the controller wouldn't be broken altogether.

So this check can be dropped.

- Mani

#3 lore raw

PATCH v2 1/1 PCI: mobiveil: Drop unnecessary msi-parent check in ls_g4_pcie_probe()

In reply to Vishal Satle

ls_g4_pcie_probe() calls of_parse_phandle(np, "msi-parent", 0) solely as a boolean existence check without releasing the returned device_node via of_node_put(), leaking a reference count on probe.

Instead of keeping the check with of_node_put(), drop the check altogether. The driver does not use the phandle directly (the irqchip layer handles it), other PCI controller drivers do not enforce this check, and without "msi-parent" INTx still works even though MSIs will not, so probing does not need to fail.

Also remove the now-unused local variable np.

---
Changes in v2:
- Drop the msi-parent check and the now-unused np variable altogether
instead of adding of_node_put(), as suggested by Manivannan.
- Link to v1: https://lore.kernel.org/all/[email protected]/

 drivers/pci/controller/mobiveil/pcie-layerscape-gen4.c | 6 ------
 1 file changed, 6 deletions(-)
drivers/pci/controller/mobiveil/pcie-layerscape-gen4.c +0 -6
index 4919b27ea..f31be6327 100644
@@ -198,14 +198,8 @@ static int __init ls_g4_pcie_probe(struct platform_device *pdev)
 	struct pci_host_bridge *bridge;
 	struct mobiveil_pcie *mv_pci;
 	struct ls_g4_pcie *pcie;
-	struct device_node *np = dev->of_node;
 	int ret;
 
-	if (!of_parse_phandle(np, "msi-parent", 0)) {
-		dev_err(dev, "Failed to find msi-parent\n");
-		return -EINVAL;
-	}
-
 	bridge = devm_pci_alloc_host_bridge(dev, sizeof(*pcie));
 	if (!bridge)
 		return -ENOMEM;
Signature · 1 line
2.53.0
#4 lore raw

Re: [PATCH v2] PCI: mobiveil: Drop unnecessary msi-parent check in ls_g4_pcie_probe()

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] PCI: mobiveil: missing Fixes trailer for device_node leak in ls_g4_pcie_probe() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] PCI: mobiveil: missing Fixes trailer for device_node leak in ls_g4_pcie_probe()
--

commit f42a1868186c1406c426daa13adc92dc71419de8
Author: Vishal Satle <[email protected]>

Quoted 12 lines

PCI: mobiveil: Drop unnecessary msi-parent check in ls_g4_pcie_probe()

ls_g4_pcie_probe() calls of_parse_phandle(np, "msi-parent", 0) solely as a boolean existence check without releasing the returned device_node via of_node_put(), leaking a reference count on probe.

Instead of keeping the check with of_node_put(), drop the check altogether. The driver does not use the phandle directly (the irqchip layer handles it), other PCI controller drivers do not enforce this check, and without "msi-parent" INTx still works even though MSIs will not, so probing does not need to fail.

Quoted 5 lines

Also remove the now-unused local variable np.

[Severity: Low]
Since this fixes the device_node reference count leak in ls_g4_pcie_probe(), should this include:

Fixes d29ad70a813b ("PCI: mobiveil: Add PCIe Gen4 RC driver for Layerscape SoCs")
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1