diff mbox series

net: mdio: fix unbalanced fwnode reference count in mdio_device_release()

Message ID 20221201092202.3394119-1-zengheng4@huawei.com (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series net: mdio: fix unbalanced fwnode reference count in mdio_device_release() | expand

Checks

Context Check Description
netdev/tree_selection success Guessed tree name to be net-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix warning Target tree name not specified in the subject
netdev/cover_letter success Single patches do not need cover letters
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers success CCed 9 of 9 maintainers
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 21 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Zeng Heng Dec. 1, 2022, 9:22 a.m. UTC
There is warning report about of_node refcount leak
while probing mdio device:

OF: ERROR: memory leak, expected refcount 1 instead of 2,
of_node_get()/of_node_put() unbalanced - destroy cset entry:
attach overlay node /spi/soc@0/mdio@710700c0/ethernet@4

In of_mdiobus_register_device(), we increase fwnode refcount
by fwnode_handle_get() before associating the of_node with
mdio device, but it has never been decreased after that.
Since that, in mdio_device_release(), it needs to call
fwnode_handle_put() in addition instead of calling kfree()
directly.

After above, just calling mdio_device_free() in the error handle
path of of_mdiobus_register_device() is enough to keep the
refcount balanced.

Fixes: a9049e0c513c ("mdio: Add support for mdio drivers.")
Signed-off-by: Zeng Heng <zengheng4@huawei.com>
---
 drivers/net/mdio/of_mdio.c    | 1 -
 drivers/net/phy/mdio_device.c | 2 ++
 2 files changed, 2 insertions(+), 1 deletion(-)

Comments

Andrew Lunn Dec. 1, 2022, 3:27 p.m. UTC | #1
On Thu, Dec 01, 2022 at 05:22:02PM +0800, Zeng Heng wrote:
> There is warning report about of_node refcount leak
> while probing mdio device:
> 
> OF: ERROR: memory leak, expected refcount 1 instead of 2,
> of_node_get()/of_node_put() unbalanced - destroy cset entry:
> attach overlay node /spi/soc@0/mdio@710700c0/ethernet@4
> 
> In of_mdiobus_register_device(), we increase fwnode refcount
> by fwnode_handle_get() before associating the of_node with
> mdio device, but it has never been decreased after that.
> Since that, in mdio_device_release(), it needs to call
> fwnode_handle_put() in addition instead of calling kfree()
> directly.
> 
> After above, just calling mdio_device_free() in the error handle
> path of of_mdiobus_register_device() is enough to keep the
> refcount balanced.

How does this interact with:

https://lore.kernel.org/netdev/20221201033838.1938765-1-yangyingliang@huawei.com/T/

	Andrew
Zeng Heng Dec. 2, 2022, 2:14 a.m. UTC | #2
On 2022/12/1 23:27, Andrew Lunn wrote:
> On Thu, Dec 01, 2022 at 05:22:02PM +0800, Zeng Heng wrote:
>> There is warning report about of_node refcount leak
>> while probing mdio device:
>>
>> OF: ERROR: memory leak, expected refcount 1 instead of 2,
>> of_node_get()/of_node_put() unbalanced - destroy cset entry:
>> attach overlay node /spi/soc@0/mdio@710700c0/ethernet@4
>>
>> In of_mdiobus_register_device(), we increase fwnode refcount
>> by fwnode_handle_get() before associating the of_node with
>> mdio device, but it has never been decreased after that.
>> Since that, in mdio_device_release(), it needs to call
>> fwnode_handle_put() in addition instead of calling kfree()
>> directly.
>>
>> After above, just calling mdio_device_free() in the error handle
>> path of of_mdiobus_register_device() is enough to keep the
>> refcount balanced.
> How does this interact with:
>
> https://lore.kernel.org/netdev/20221201033838.1938765-1-yangyingliang@huawei.com/T/
>
> 	Andrew

No, they don't interact with each other, because they fix different 
issues respectively.


The patch sent by me is about eliminating refcount warning in the normal 
and error

handling route of mdio_device_register(), while the one sent by 
Yingliang (as you concern about)

is fixing refcount warning in the error handle path of 
phy_device_register().


Yingliang and I work on cleaning the warning report and enhancing the 
quality of kernel.

I am not sure, for your convenience, shall I need to send my patch to 
Yingliang and let him

edit them into a set of patches?


With best regards,

Zeng Heng
Andrew Lunn Dec. 2, 2022, 12:56 p.m. UTC | #3
> No, they don't interact with each other, because they fix different issues
> respectively.

I'm wanted to ensure you know about each others work and that the
fixes don't clash. I could not see a Cc: between you, nor a
reviewed-by: etc to indicate you are working together on this.

The patches can go separately, but maybe you can review v3 of his
patch? Ask him to review yours.

       Andrew
Yang Yingliang Dec. 2, 2022, 1:12 p.m. UTC | #4
On 2022/12/2 20:56, Andrew Lunn wrote:
>> No, they don't interact with each other, because they fix different issues
>> respectively.
> I'm wanted to ensure you know about each others work and that the
> fixes don't clash. I could not see a Cc: between you, nor a
> reviewed-by: etc to indicate you are working together on this.
Yeah, we know each others work, he run test with both patches merged.
>
> The patches can go separately, but maybe you can review v3 of his
> patch? Ask him to review yours.
I can review his patch.

Thanks,
Yang
>
>         Andrew
> .
Yang Yingliang Dec. 2, 2022, 1:36 p.m. UTC | #5
On 2022/12/1 17:22, Zeng Heng wrote:
> There is warning report about of_node refcount leak
> while probing mdio device:
>
> OF: ERROR: memory leak, expected refcount 1 instead of 2,
> of_node_get()/of_node_put() unbalanced - destroy cset entry:
> attach overlay node /spi/soc@0/mdio@710700c0/ethernet@4
>
> In of_mdiobus_register_device(), we increase fwnode refcount
> by fwnode_handle_get() before associating the of_node with
> mdio device, but it has never been decreased after that.
> Since that, in mdio_device_release(), it needs to call
> fwnode_handle_put() in addition instead of calling kfree()
> directly.
>
> After above, just calling mdio_device_free() in the error handle
> path of of_mdiobus_register_device() is enough to keep the
> refcount balanced.
>
> Fixes: a9049e0c513c ("mdio: Add support for mdio drivers.")
> Signed-off-by: Zeng Heng <zengheng4@huawei.com>
> ---
>   drivers/net/mdio/of_mdio.c    | 1 -
>   drivers/net/phy/mdio_device.c | 2 ++
>   2 files changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/net/mdio/of_mdio.c b/drivers/net/mdio/of_mdio.c
> index 796e9c7857d0..296317a6b333 100644
> --- a/drivers/net/mdio/of_mdio.c
> +++ b/drivers/net/mdio/of_mdio.c
> @@ -69,7 +69,6 @@ static int of_mdiobus_register_device(struct mii_bus *mdio,
>   	rc = mdio_device_register(mdiodev);
>   	if (rc) {
>   		mdio_device_free(mdiodev);
> -		of_node_put(child);
device_set_node() is called to set fwnode and of_node, for undoing this,
calling device_set_node(NULL) is better, and then call the put function.

Thanks,
Yang
>   		return rc;
>   	}
>   
> diff --git a/drivers/net/phy/mdio_device.c b/drivers/net/phy/mdio_device.c
> index 250742ffdfd9..044828d081d2 100644
> --- a/drivers/net/phy/mdio_device.c
> +++ b/drivers/net/phy/mdio_device.c
> @@ -21,6 +21,7 @@
>   #include <linux/slab.h>
>   #include <linux/string.h>
>   #include <linux/unistd.h>
> +#include <linux/property.h>
>   
>   void mdio_device_free(struct mdio_device *mdiodev)
>   {
> @@ -30,6 +31,7 @@ EXPORT_SYMBOL(mdio_device_free);
>   
>   static void mdio_device_release(struct device *dev)
>   {
> +	fwnode_handle_put(dev->fwnode);
>   	kfree(to_mdio_device(dev));
>   }
>
diff mbox series

Patch

diff --git a/drivers/net/mdio/of_mdio.c b/drivers/net/mdio/of_mdio.c
index 796e9c7857d0..296317a6b333 100644
--- a/drivers/net/mdio/of_mdio.c
+++ b/drivers/net/mdio/of_mdio.c
@@ -69,7 +69,6 @@  static int of_mdiobus_register_device(struct mii_bus *mdio,
 	rc = mdio_device_register(mdiodev);
 	if (rc) {
 		mdio_device_free(mdiodev);
-		of_node_put(child);
 		return rc;
 	}
 
diff --git a/drivers/net/phy/mdio_device.c b/drivers/net/phy/mdio_device.c
index 250742ffdfd9..044828d081d2 100644
--- a/drivers/net/phy/mdio_device.c
+++ b/drivers/net/phy/mdio_device.c
@@ -21,6 +21,7 @@ 
 #include <linux/slab.h>
 #include <linux/string.h>
 #include <linux/unistd.h>
+#include <linux/property.h>
 
 void mdio_device_free(struct mdio_device *mdiodev)
 {
@@ -30,6 +31,7 @@  EXPORT_SYMBOL(mdio_device_free);
 
 static void mdio_device_release(struct device *dev)
 {
+	fwnode_handle_put(dev->fwnode);
 	kfree(to_mdio_device(dev));
 }