phy: hisilicon: Fix OF node reference leak
PATCH 1/1 phy: hisilicon: Fix OF node reference leak
hi3670_pcie_get_resources_from_pcie() leaks an OF node reference obtained by of_get_child_by_name(). The reference is not released on any of the error paths or on successful return, causing a reference count leak.
Fix this by declaring the device node with the __free(device_node) cleanup construct to ensure the reference is automatically released.
--- drivers/phy/hisilicon/phy-hi3670-pcie.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/phy/hisilicon/phy-hi3670-pcie.c +2 -3
@@ -558,11 +558,10 @@ static int hi3670_pcie_noc_power(struct hi3670_pcie_phy *phy, bool enable)static int hi3670_pcie_get_resources_from_pcie (struct hi3670_pcie_phy *phy) { -struct device_node *pcie_port;struct device *dev = phy->dev;struct device *pcie_dev; - - pcie_port =of_get_child_by_name (dev->parent->of_node,"pcie" ); + struct device_node *pcie_port __free(device_node) = +of_get_child_by_name (dev->parent->of_node,"pcie" );if (!pcie_port) {dev_err (dev,"no pcie node found in %s\n" , dev->parent->of_node->full_name);
Signature · 7 lines
2.50.1.windows.1 -- linux-phy mailing list [email protected] https://lists.infradead.org/mailman/listinfo/linux-phy
Hello Haotian,
Quoted 36 lines · Haotian Zhang
hi3670_pcie_get_resources_from_pcie()leaks an OF node reference obtained byof_get_child_by_name(). The reference is not released on any of the error paths or on successful return, causing a reference count leak.Fix this by declaring the device node with the
__free(device_node)cleanup construct to ensure the reference is automatically released.Fixes 73075011ffff ("phy: HiSilicon: Add driver for Kirin 970 PCIe PHY")Signed-off-by Haotian Zhang <[email protected]>--- drivers/phy/hisilicon/phy-hi3670-pcie.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)drivers/phy/hisilicon/phy-hi3670-pcie.c +2
-3@@ -558,11 +558,10 @@ static int hi3670_pcie_noc_power(struct hi3670_pcie_phy *phy, bool enable)static int hi3670_pcie_get_resources_from_pcie (struct hi3670_pcie_phy *phy) { -struct device_node *pcie_port;struct device *dev = phy->dev;struct device *pcie_dev; - - pcie_port =of_get_child_by_name (dev->parent->of_node,"pcie" ); + struct device_node *pcie_port __free(device_node) = +of_get_child_by_name (dev->parent->of_node,"pcie" );if (!pcie_port) {dev_err (dev,"no pcie node found in %s\n" , dev->parent->of_node->full_name);Signature · 1 line
2.50.1.windows.1
Sorry for the delay, and thank you for the patch.
Please do not complicate the solution more than necessary, and more importantly, do not use cleanup.h infrastructure added in 2023 to fix a bug from 2021 (will be difficult to backport).
In this case, it is sufficient to free the OF node after bus_find_device_by_of_node():
pcie_port = of_get_child_by_name(dev->parent->of_node, "pcie"); if (!pcie_port) {
dev_err(dev, "no pcie node found in %s\n",
dev->parent->of_node->full_name);
return -ENODEV;
}
pcie_dev = bus_find_device_by_of_node(&platform_bus_type, pcie_port);
+ of_node_put(pcie_port);
if (!pcie_dev) {
dev_err(dev, "Didn't find pcie device\n");
return -ENODEV;
}
Note that there exists a second reference leak bug in the same function. bus_find_device_by_of_node() requires put_device(pcie_dev) after it is no longer needed.
Can you resubmit a patch set addressing both issues?
Quoted 1 line · Vladimir Oltean
On Wed, Nov 12, 2025 at 02:22:46PM +0800, Haotian Zhang wrote:
...
Quoted 9 lines
(quoted diff) +1
-0pcie_dev = bus_find_device_by_of_node(&platform_bus_type, pcie_port); + of_node_put(pcie_port); if (!pcie_dev) { dev_err(dev, "Didn't find pcie device\n"); return -ENODEV; }Note that there exists a second reference leak bug in the same function.
bus_find_device_by_of_node()requiresput_device(pcie_dev)
Note, there is a pci_* wrapper for that.
Quoted 1 line
after it is no longer needed.
Is it only a local variable? If so, it's probably okay to put it, but that action needs more investigations of the how the pcie_dev is being used. Also, it might be (but I don't think it is) a (double) put_device() call somewhere else. TL;DR: the summary of this investigation should be present in the commit message.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Vladimir Oltean
On Wed, Nov 12, 2025 at 02:22:46PM +0800, Haotian Zhang wrote:
...
Quoted 11 lines
Quoted 9 lines
(quoted diff) +1
-0pcie_dev = bus_find_device_by_of_node(&platform_bus_type, pcie_port); + of_node_put(pcie_port); if (!pcie_dev) { dev_err(dev, "Didn't find pcie device\n"); return -ENODEV; }Note that there exists a second reference leak bug in the same function.
bus_find_device_by_of_node()requiresput_device(pcie_dev)Note, there is a pci_* wrapper for that.
Now I re-read this and found confusing comment, please, disregard this.
Quoted 7 lines
Quoted 1 line
after it is no longer needed.
Is it only a local variable? If so, it's probably okay to put it, but that action needs more investigations of the how the
pcie_devis being used. Also, it might be (but I don't think it is) a (double)put_device()call somewhere else. TL;DR: the summary of this investigation should be present in the commit message.
Signature · 2 lines
With Best Regards, Andy Shevchenko
PATCH v2 1/1 phy: hisilicon: hi3670: Fix OF node and device reference leaks
In reply to Haotian Zhanghi3670_pcie_get_resources_from_pcie() leaks two references:
1) The OF node returned by of_get_child_by_name() is never released.
2) The device returned by bus_find_device_by_of_node() is never put.
Fix both leaks by calling of_node_put() right after bus_find_device_by_of_node(), and put_device() after dev_get_regmap() when the temporary pcie_dev reference is no longer needed.
---
Changes in v2:
- Use explicit release instead of cleanup.h infrastructure.
- Add put_device() for bus_find_device_by_of_node().
--- drivers/phy/hisilicon/phy-hi3670-pcie.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/phy/hisilicon/phy-hi3670-pcie.c +2 -0
@@ -570,6 +570,7 @@ static int hi3670_pcie_get_resources_from_pcie(struct hi3670_pcie_phy *phy) } pcie_dev =bus_find_device_by_of_node (&platform_bus_type, pcie_port); +of_node_put (pcie_port);if (!pcie_dev) {dev_err (dev,"Didn't find pcie device\n" );return -ENODEV ;
@@ -584,6 +585,7 @@ static int hi3670_pcie_get_resources_from_pcie(struct hi3670_pcie_phy *phy)* right regmap. So, let's use the named version. */ phy->apb =dev_get_regmap (pcie_dev,"kirin_pcie_apb" ); +put_device (pcie_dev);if (!phy->apb) {dev_err (dev,"Failed to get APB regmap\n" );return -ENODEV ;
Signature · 7 lines
2.43.0 -- linux-phy mailing list [email protected] https://lists.infradead.org/mailman/listinfo/linux-phy
Re: [PATCH v2] phy: hisilicon: hi3670: Fix OF node and device reference leaks
Quoted 11 lines · Haotian Zhang
hi3670_pcie_get_resources_from_pcie()leaks two references:
1) The OF node returned byof_get_child_by_name()is never released.
2) The device returned bybus_find_device_by_of_node()is never put.Fix both leaks by calling
of_node_put()right afterbus_find_device_by_of_node(), andput_device()afterdev_get_regmap()when the temporarypcie_devreference is no longer needed.Fixes 73075011ffff ("phy: HiSilicon: Add driver for Kirin 970 PCIe PHY")Suggested-by Vladimir Oltean <[email protected]>Signed-off-by Haotian Zhang <[email protected]>
- Mani
Signature · 1 line
மணிவண்ணன் சதாசிவம்
Re: [PATCH v2] phy: hisilicon: hi3670: Fix OF node and device reference leaks
In reply to Haotian ZhangQuoted 9 lines · Haotian Zhang
hi3670_pcie_get_resources_from_pcie()leaks two references:
1) The OF node returned byof_get_child_by_name()is never released.
2) The device returned bybus_find_device_by_of_node()is never put.Fix both leaks by calling
of_node_put()right afterbus_find_device_by_of_node(), andput_device()afterdev_get_regmap()when the temporarypcie_devreference is no longer needed.[...]
Applied, thanks!
[1/1] phy: hisilicon: hi3670: Fix OF node and device reference leaks
commit: 2638a0e60f65e38bbd28400340ab92fa67b562b3
Best regards,
Signature · 1 line
~Vinod