net: lpc_eth: Fix a possible memory leak in lpc_mii_probe()
Subsystems (4)
PATCH 1/1 net: lpc_eth: Fix a possible memory leak in lpc_mii_probe()
lpc_mii_probe() calls of_phy_find_device() to obtain a phy_device pointer. of_phy_find_device() increments the refcount of the device. The current implementation does not decrement the refcount after using the pointer, which leads to a memory leak.
Add phy_device_free() to balance the refcount.
Found by code review.
--- drivers/net/ethernet/nxp/lpc_eth.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
drivers/net/ethernet/nxp/lpc_eth.c +6 -5
@@ -751,7 +751,7 @@ static void lpc_handle_link_change(struct net_device *ndev)static int lpc_mii_probe (struct net_device *ndev) {struct netdata_local *pldat =netdev_priv (ndev); -struct phy_device *phydev; +struct phy_device *phydev, *phydev_tmp;/* Attach to the PHY */ if (lpc_phy_interface_mode (&pldat->pdev->dev) ==PHY_INTERFACE_MODE_MII )
@@ -760,17 +760,18 @@ static int lpc_mii_probe(struct net_device *ndev)netdev_info (ndev,"using RMII interface\n" );if (pldat->phy_node) - phydev =of_phy_find_device (pldat->phy_node); + phydev_tmp =of_phy_find_device (pldat->phy_node);else - phydev =phy_find_first (pldat->mii_bus); -if (!phydev) { + phydev_tmp =phy_find_first (pldat->mii_bus); +if (!phydev_tmp) {netdev_err (ndev,"no PHY found\n" );return -ENODEV ; } - phydev =phy_connect (ndev,phydev_name (phydev), + phydev =phy_connect (ndev,phydev_name (phydev_tmp), &lpc_handle_link_change,lpc_phy_interface_mode (&pldat->pdev->dev)); +phy_device_free (phydev_tmp);if (IS_ERR (phydev)) {netdev_err (ndev,"Could not attach to PHY\n" );return PTR_ERR (phydev);
Signature · 1 line
2.43.0
Vladimir Zapolskiy
maintainerHello Ma Ke,
Quoted 4 lines · Ma Ke
lpc_mii_probe()callsof_phy_find_device()to obtain aphy_devicepointer.of_phy_find_device()increments the refcount of the device. The current implementation does not decrement the refcount after using the pointer, which leads to a memory leak.
this is correct, there is an actual detected bug.
Quoted 2 lines
Add
phy_device_free()to balance the refcount.
But this does not sound right, you shoud use of_node_put(pldat->phy_node).
Quoted 24 lines
Found by code review.
Signed-off-by Ma Ke <[email protected]>Fixes 3503bf024b3e ("net: lpc_eth: parse phy nodes from device tree")--- drivers/net/ethernet/nxp/lpc_eth.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)drivers/net/ethernet/nxp/lpc_eth.c +1
-1@@ -751,7 +751,7 @@ static void lpc_handle_link_change(struct net_device *ndev)static int lpc_mii_probe (struct net_device *ndev) {struct netdata_local *pldat =netdev_priv (ndev); -struct phy_device *phydev; +struct phy_device *phydev, *phydev_tmp;/* Attach to the PHY */ if (lpc_phy_interface_mode (&pldat->pdev->dev) ==PHY_INTERFACE_MODE_MII )
drivers/net/ethernet/nxp/lpc_eth.c static int lpc_mii_probe(struct net_device *ndev) · line 766
Quoted 10 lines
drivers/net/ethernet/nxp/lpc_eth.c +3
-3@@ -760,17 +760,18 @@ static int lpc_mii_probe(struct net_device *ndev)netdev_info (ndev,"using RMII interface\n" );if (pldat->phy_node) - phydev =of_phy_find_device (pldat->phy_node); + phydev_tmp =of_phy_find_device (pldat->phy_node);else - phydev =phy_find_first (pldat->mii_bus); -if (!phydev) { + phydev_tmp =phy_find_first (pldat->mii_bus); +if (!phydev_tmp) {
I didn't get it, why the new phydev_tmp is needed above, please restore the original code above.
drivers/net/ethernet/nxp/lpc_eth.c static int lpc_mii_probe(struct net_device *ndev) · line 774
Quoted 9 lines
drivers/net/ethernet/nxp/lpc_eth.c +2
-1netdev_err (ndev,"no PHY found\n" );return -ENODEV ; } - phydev =phy_connect (ndev,phydev_name (phydev), + phydev =phy_connect (ndev,phydev_name (phydev_tmp), &lpc_handle_link_change,lpc_phy_interface_mode (&pldat->pdev->dev)); +phy_device_free (phydev_tmp);
This is plainly wrong and has to be dropped or changed to
if (pldat->phy_node) of_node_put(pldat->phy_node);
drivers/net/ethernet/nxp/lpc_eth.c static int lpc_mii_probe(struct net_device *ndev) · line 777
Quoted 3 lines
drivers/net/ethernet/nxp/lpc_eth.c +0
-0if (IS_ERR (phydev)) {netdev_err (ndev,"Could not attach to PHY\n" );return PTR_ERR (phydev);
Is it AI generated fix or what?.. The change looks bad, it introduces more severe issues than it fixes.
If you think you cannot create a proper change, let me know.
Signature · 2 lines
Best wishes, Vladimir
Quoted 66 lines · Vladimir Zapolskiy
Quoted 4 lines · Ma Ke
lpc_mii_probe()callsof_phy_find_device()to obtain aphy_devicepointer.of_phy_find_device()increments the refcount of the device. The current implementation does not decrement the refcount after using the pointer, which leads to a memory leak.this is correct, there is an actual detected bug.
Quoted 2 lines
Add
phy_device_free()to balance the refcount.But this does not sound right, you shoud use
of_node_put(pldat->phy_node).Quoted 24 lines
Found by code review.
Signed-off-by Ma Ke <[email protected]>Fixes 3503bf024b3e ("net: lpc_eth: parse phy nodes from device tree")--- drivers/net/ethernet/nxp/lpc_eth.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)drivers/net/ethernet/nxp/lpc_eth.c +1
-1@@ -751,7 +751,7 @@ static void lpc_handle_link_change(struct net_device *ndev)static int lpc_mii_probe (struct net_device *ndev) {struct netdata_local *pldat =netdev_priv (ndev); -struct phy_device *phydev; +struct phy_device *phydev, *phydev_tmp;/* Attach to the PHY */ if (lpc_phy_interface_mode (&pldat->pdev->dev) ==PHY_INTERFACE_MODE_MII )drivers/net/ethernet/nxp/lpc_eth.c static int lpc_mii_probe(struct net_device *ndev) · line 766
Quoted 10 lines
drivers/net/ethernet/nxp/lpc_eth.c +3
-3@@ -760,17 +760,18 @@ static int lpc_mii_probe(struct net_device *ndev)netdev_info (ndev,"using RMII interface\n" );if (pldat->phy_node) - phydev =of_phy_find_device (pldat->phy_node); + phydev_tmp =of_phy_find_device (pldat->phy_node);else - phydev =phy_find_first (pldat->mii_bus); -if (!phydev) { + phydev_tmp =phy_find_first (pldat->mii_bus); +if (!phydev_tmp) {I didn't get it, why the new
phydev_tmpis needed above, please restore the original code above.drivers/net/ethernet/nxp/lpc_eth.c static int lpc_mii_probe(struct net_device *ndev) · line 774
Quoted 9 lines
drivers/net/ethernet/nxp/lpc_eth.c +2
-1netdev_err (ndev,"no PHY found\n" );return -ENODEV ; } - phydev =phy_connect (ndev,phydev_name (phydev), + phydev =phy_connect (ndev,phydev_name (phydev_tmp), &lpc_handle_link_change,lpc_phy_interface_mode (&pldat->pdev->dev)); +phy_device_free (phydev_tmp);This is plainly wrong and has to be dropped or changed to
if (pldat->phy_node) of_node_put(pldat->phy_node);
Quoted 12 lines
drivers/net/ethernet/nxp/lpc_eth.c static int lpc_mii_probe(struct net_device *ndev) · line 777
Quoted 3 lines
drivers/net/ethernet/nxp/lpc_eth.c +0
-0if (IS_ERR (phydev)) {netdev_err (ndev,"Could not attach to PHY\n" );return PTR_ERR (phydev);Is it AI generated fix or what?.. The change looks bad, it introduces more severe issues than it fixes.
If you think you cannot create a proper change, let me know.
Signature · 2 lines
Best wishes, Vladimir
Thank you very much for your detailed review and guidance.
Now I think your point probably is: you are saying that the real leak is not from of_phy_find_device(), but from the device node pldat->phy_node which was obtained earlier (probably by of_parse_phandle()) and never freed by of_node_put(). And you suggest to add of_node_put(pldat->phy_node) instead of my wrong phy_device_free().
However, I am still a little confused. In lpc_mii_probe(), of_phy_find_device() is called. From my understanding, this function increases the reference count of the device. To balance it, I thought phy_device_free() (which calls put_device()) should be used.
Could you please kindly advise the correct patch? I will follow your guidance and submit a proper fix.
I apologize again for my previous wrong patch. Thank you very much for your help.
Best regards,
Ma Ke
Quoted 66 lines · Vladimir Zapolskiy
Quoted 4 lines · Ma Ke
lpc_mii_probe()callsof_phy_find_device()to obtain aphy_devicepointer.of_phy_find_device()increments the refcount of the device. The current implementation does not decrement the refcount after using the pointer, which leads to a memory leak.this is correct, there is an actual detected bug.
Quoted 2 lines
Add
phy_device_free()to balance the refcount.But this does not sound right, you shoud use
of_node_put(pldat->phy_node).Quoted 24 lines
Found by code review.
Signed-off-by Ma Ke <[email protected]>Fixes 3503bf024b3e ("net: lpc_eth: parse phy nodes from device tree")--- drivers/net/ethernet/nxp/lpc_eth.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)drivers/net/ethernet/nxp/lpc_eth.c +1
-1@@ -751,7 +751,7 @@ static void lpc_handle_link_change(struct net_device *ndev)static int lpc_mii_probe (struct net_device *ndev) {struct netdata_local *pldat =netdev_priv (ndev); -struct phy_device *phydev; +struct phy_device *phydev, *phydev_tmp;/* Attach to the PHY */ if (lpc_phy_interface_mode (&pldat->pdev->dev) ==PHY_INTERFACE_MODE_MII )drivers/net/ethernet/nxp/lpc_eth.c static int lpc_mii_probe(struct net_device *ndev) · line 766
Quoted 10 lines
drivers/net/ethernet/nxp/lpc_eth.c +3
-3@@ -760,17 +760,18 @@ static int lpc_mii_probe(struct net_device *ndev)netdev_info (ndev,"using RMII interface\n" );if (pldat->phy_node) - phydev =of_phy_find_device (pldat->phy_node); + phydev_tmp =of_phy_find_device (pldat->phy_node);else - phydev =phy_find_first (pldat->mii_bus); -if (!phydev) { + phydev_tmp =phy_find_first (pldat->mii_bus); +if (!phydev_tmp) {I didn't get it, why the new
phydev_tmpis needed above, please restore the original code above.drivers/net/ethernet/nxp/lpc_eth.c static int lpc_mii_probe(struct net_device *ndev) · line 774
Quoted 9 lines
drivers/net/ethernet/nxp/lpc_eth.c +2
-1netdev_err (ndev,"no PHY found\n" );return -ENODEV ; } - phydev =phy_connect (ndev,phydev_name (phydev), + phydev =phy_connect (ndev,phydev_name (phydev_tmp), &lpc_handle_link_change,lpc_phy_interface_mode (&pldat->pdev->dev)); +phy_device_free (phydev_tmp);This is plainly wrong and has to be dropped or changed to
if (pldat->phy_node) of_node_put(pldat->phy_node);
Quoted 12 lines
drivers/net/ethernet/nxp/lpc_eth.c static int lpc_mii_probe(struct net_device *ndev) · line 777
Quoted 3 lines
drivers/net/ethernet/nxp/lpc_eth.c +0
-0if (IS_ERR (phydev)) {netdev_err (ndev,"Could not attach to PHY\n" );return PTR_ERR (phydev);Is it AI generated fix or what?.. The change looks bad, it introduces more severe issues than it fixes.
If you think you cannot create a proper change, let me know.
Signature · 2 lines
Best wishes, Vladimir
Thank you very much for your detailed review and guidance.
Now I think your point probably is: you are saying that the real leak is not from of_phy_find_device(), but from the device node pldat->phy_node which was obtained earlier (probably by of_parse_phandle()) and never freed by of_node_put(). And you suggest to add of_node_put(pldat->phy_node) instead of my wrong phy_device_free().
However, I am still a little confused. In lpc_mii_probe(), of_phy_find_device() is called. From my understanding, this function increases the reference count of the device. To balance it, I thought phy_device_free() (which calls put_device()) should be used.
Could you please kindly advise the correct patch? I will follow your guidance and submit a proper fix.
I apologize again for my previous wrong patch. Thank you very much for your help.
Best regards,
Ma Ke
Vladimir Zapolskiy
maintainerHello Ma Ke.
Quoted 74 lines · Ma Ke
Quoted 73 lines · Vladimir Zapolskiy
Quoted 4 lines · Ma Ke
lpc_mii_probe()callsof_phy_find_device()to obtain aphy_devicepointer.of_phy_find_device()increments the refcount of the device. The current implementation does not decrement the refcount after using the pointer, which leads to a memory leak.this is correct, there is an actual detected bug.
Quoted 2 lines
Add
phy_device_free()to balance the refcount.But this does not sound right, you shoud use
of_node_put(pldat->phy_node).Quoted 24 lines
Found by code review.
Signed-off-by Ma Ke <[email protected]>Fixes 3503bf024b3e ("net: lpc_eth: parse phy nodes from device tree")--- drivers/net/ethernet/nxp/lpc_eth.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)drivers/net/ethernet/nxp/lpc_eth.c +1
-1@@ -751,7 +751,7 @@ static void lpc_handle_link_change(struct net_device *ndev)static int lpc_mii_probe (struct net_device *ndev) {struct netdata_local *pldat =netdev_priv (ndev); -struct phy_device *phydev; +struct phy_device *phydev, *phydev_tmp;/* Attach to the PHY */ if (lpc_phy_interface_mode (&pldat->pdev->dev) ==PHY_INTERFACE_MODE_MII )drivers/net/ethernet/nxp/lpc_eth.c static int lpc_mii_probe(struct net_device *ndev) · line 766
Quoted 10 lines
drivers/net/ethernet/nxp/lpc_eth.c +3
-3@@ -760,17 +760,18 @@ static int lpc_mii_probe(struct net_device *ndev)netdev_info (ndev,"using RMII interface\n" );if (pldat->phy_node) - phydev =of_phy_find_device (pldat->phy_node); + phydev_tmp =of_phy_find_device (pldat->phy_node);else - phydev =phy_find_first (pldat->mii_bus); -if (!phydev) { + phydev_tmp =phy_find_first (pldat->mii_bus); +if (!phydev_tmp) {I didn't get it, why the new
phydev_tmpis needed above, please restore the original code above.drivers/net/ethernet/nxp/lpc_eth.c static int lpc_mii_probe(struct net_device *ndev) · line 774
Quoted 9 lines
drivers/net/ethernet/nxp/lpc_eth.c +2
-1netdev_err (ndev,"no PHY found\n" );return -ENODEV ; } - phydev =phy_connect (ndev,phydev_name (phydev), + phydev =phy_connect (ndev,phydev_name (phydev_tmp), &lpc_handle_link_change,lpc_phy_interface_mode (&pldat->pdev->dev)); +phy_device_free (phydev_tmp);This is plainly wrong and has to be dropped or changed to
if (pldat->phy_node) of_node_put(pldat->phy_node);drivers/net/ethernet/nxp/lpc_eth.c static int lpc_mii_probe(struct net_device *ndev) · line 777
Quoted 3 lines
drivers/net/ethernet/nxp/lpc_eth.c +0
-0if (IS_ERR (phydev)) {netdev_err (ndev,"Could not attach to PHY\n" );return PTR_ERR (phydev);Is it AI generated fix or what?.. The change looks bad, it introduces more severe issues than it fixes.
Quoted 6 lines
Quoted 2 lines
If you think you cannot create a proper change, let me know.
Thank you very much for your detailed review and guidance.
Now I think your point probably is: you are saying that the real leak is not from
of_phy_find_device(), but from the device node
I was pretty indelicate in my comment, let's split the change into parts.
1) I still do not understand, why phydev_tmp is introduced, please explain or remove this part of the change;
2) phydev = of_phy_find_device() requires phy_device_free(phydev), but I do not see why phy_find_first() requires it, while it was added in your change.
Let's start from resolving these two points.
Quoted 15 lines
pldat->phy_nodewhich was obtained earlier (probably byof_parse_phandle()) and never freed byof_node_put(). And you suggest to addof_node_put(pldat->phy_node)instead of my wrongphy_device_free().However, I am still a little confused. In
lpc_mii_probe(),of_phy_find_device()is called. From my understanding, this function increases the reference count of the device. To balance it, I thoughtphy_device_free()(which callsput_device()) should be used.Could you please kindly advise the correct patch? I will follow your guidance and submit a proper fix.
I apologize again for my previous wrong patch. Thank you very much for your help.
Signature · 2 lines
Best wishes, Vladimir
Quoted 105 lines
Hello Ma Ke.
Quoted 74 lines · Ma Ke
Quoted 73 lines · Vladimir Zapolskiy
Quoted 4 lines · Ma Ke
lpc_mii_probe()callsof_phy_find_device()to obtain aphy_devicepointer.of_phy_find_device()increments the refcount of the device. The current implementation does not decrement the refcount after using the pointer, which leads to a memory leak.this is correct, there is an actual detected bug.
Quoted 2 lines
Add
phy_device_free()to balance the refcount.But this does not sound right, you shoud use
of_node_put(pldat->phy_node).Quoted 24 lines
Found by code review.
Signed-off-by Ma Ke <[email protected]>Fixes 3503bf024b3e ("net: lpc_eth: parse phy nodes from device tree")--- drivers/net/ethernet/nxp/lpc_eth.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)drivers/net/ethernet/nxp/lpc_eth.c +1
-1@@ -751,7 +751,7 @@ static void lpc_handle_link_change(struct net_device *ndev)static int lpc_mii_probe (struct net_device *ndev) {struct netdata_local *pldat =netdev_priv (ndev); -struct phy_device *phydev; +struct phy_device *phydev, *phydev_tmp;/* Attach to the PHY */ if (lpc_phy_interface_mode (&pldat->pdev->dev) ==PHY_INTERFACE_MODE_MII )drivers/net/ethernet/nxp/lpc_eth.c static int lpc_mii_probe(struct net_device *ndev) · line 766
Quoted 10 lines
drivers/net/ethernet/nxp/lpc_eth.c +3
-3@@ -760,17 +760,18 @@ static int lpc_mii_probe(struct net_device *ndev)netdev_info (ndev,"using RMII interface\n" );if (pldat->phy_node) - phydev =of_phy_find_device (pldat->phy_node); + phydev_tmp =of_phy_find_device (pldat->phy_node);else - phydev =phy_find_first (pldat->mii_bus); -if (!phydev) { + phydev_tmp =phy_find_first (pldat->mii_bus); +if (!phydev_tmp) {I didn't get it, why the new
phydev_tmpis needed above, please restore the original code above.drivers/net/ethernet/nxp/lpc_eth.c static int lpc_mii_probe(struct net_device *ndev) · line 774
Quoted 9 lines
drivers/net/ethernet/nxp/lpc_eth.c +2
-1netdev_err (ndev,"no PHY found\n" );return -ENODEV ; } - phydev =phy_connect (ndev,phydev_name (phydev), + phydev =phy_connect (ndev,phydev_name (phydev_tmp), &lpc_handle_link_change,lpc_phy_interface_mode (&pldat->pdev->dev)); +phy_device_free (phydev_tmp);This is plainly wrong and has to be dropped or changed to
if (pldat->phy_node) of_node_put(pldat->phy_node);drivers/net/ethernet/nxp/lpc_eth.c static int lpc_mii_probe(struct net_device *ndev) · line 777
Quoted 3 lines
drivers/net/ethernet/nxp/lpc_eth.c +0
-0if (IS_ERR (phydev)) {netdev_err (ndev,"Could not attach to PHY\n" );return PTR_ERR (phydev);Is it AI generated fix or what?.. The change looks bad, it introduces more severe issues than it fixes.
Quoted 6 lines
Quoted 2 lines
If you think you cannot create a proper change, let me know.
Thank you very much for your detailed review and guidance.
Now I think your point probably is: you are saying that the real leak is not from
of_phy_find_device(), but from the device nodeI was pretty indelicate in my comment, let's split the change into parts.
1) I still do not understand, why
phydev_tmpis introduced, please explain or remove this part of the change;2) phydev =
of_phy_find_device()requiresphy_device_free(phydev), but I do not see whyphy_find_first()requires it, while it was added in your change.Let's start from resolving these two points.
Quoted 10 lines
pldat->phy_nodewhich was obtained earlier (probably byof_parse_phandle()) and never freed byof_node_put(). And you suggest to addof_node_put(pldat->phy_node)instead of my wrongphy_device_free().However, I am still a little confused. In
lpc_mii_probe(),of_phy_find_device()is called. From my understanding, this function increases the reference count of the device. To balance it, I thoughtphy_device_free()(which callsput_device()) should be used.
Quoted 9 lines
Quoted 5 lines
Could you please kindly advise the correct patch? I will follow your guidance and submit a proper fix.
I apologize again for my previous wrong patch. Thank you very much for your help.
Signature · 2 lines
Best wishes, Vladimir
Hello Vladimir,
Thank you for the detailed explanation and for pointing out my mistakes.
Quoted 2 lines
1) I still do not understand, why
phydev_tmpis introduced, please explain or remove this part of the change;
I added phydev_tmp because I thought I needed to keep the original phy_device pointer for releasing after phy_connect(). But as you implied, it's perhaps unnecessary and only makes the code less readable. I will drop this change completely in the next version.
Quoted 3 lines
2) phydev =
of_phy_find_device()requiresphy_device_free(phydev), but I do not see whyphy_find_first()requires it, while it was added in your change.
You are absolutely right. I mistakenly assumed that both functions return a reference-counted pointer. phy_find_first() does not increment the refcount, so calling phy_device_free() on it is wrong and dangerous. My patch introduced a new bug there.
Now I understand that only the of_phy_find_device() branch needs a corresponding put_device(). I will prepare a corrected patch that only releases the reference in that specific path (including on the error path after phy_connect() failure). I will also look at the phy_node reference leak you hinted at.
Thank you again for your guidance. I will send a v2 after fixing it properly.
Best regards,
Ma Ke