diff mbox series

[net] net: mdiobus: remove unneccessary fwnode_handle_put() in the error path

Message ID 20221130091759.682841-1-yangyingliang@huawei.com (mailing list archive)
State Changes Requested
Delegated to: Netdev Maintainers
Headers show
Series [net] net: mdiobus: remove unneccessary fwnode_handle_put() in the error path | expand

Checks

Context Check Description
netdev/tree_selection success Clearly marked for net
netdev/fixes_present success Fixes tag present in non-next series
netdev/subject_prefix success Link
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 warning WARNING: 'unneccessary' may be misspelled - perhaps 'unnecessary'?
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Yang Yingliang Nov. 30, 2022, 9:17 a.m. UTC
If phy_device_register() or fwnode_mdiobus_phy_device_register()
fail, phy_device_free() is called, the device refcount is decreased
to 0, then fwnode_handle_put() is called in phy_device_release(),
so the fwnode_handle_put() in the error path can be removed to avoid
double put.

Fixes: cdde1560118f ("net: mdiobus: fix unbalanced node reference count")
Reported-by: Zeng Heng <zengheng4@huawei.com>
Tested-by: Zeng Heng <zengheng4@huawei.com>
Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
---
 drivers/net/mdio/fwnode_mdio.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)

Comments

Andrew Lunn Dec. 1, 2022, 2:41 a.m. UTC | #1
On Wed, Nov 30, 2022 at 05:17:59PM +0800, Yang Yingliang wrote:
> If phy_device_register() or fwnode_mdiobus_phy_device_register()
> fail, phy_device_free() is called, the device refcount is decreased
> to 0, then fwnode_handle_put() is called in phy_device_release(),
> so the fwnode_handle_put() in the error path can be removed to avoid
> double put.
> 
> Fixes: cdde1560118f ("net: mdiobus: fix unbalanced node reference count")
> Reported-by: Zeng Heng <zengheng4@huawei.com>
> Tested-by: Zeng Heng <zengheng4@huawei.com>
> Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
> ---
>  drivers/net/mdio/fwnode_mdio.c | 8 ++------
>  1 file changed, 2 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/net/mdio/fwnode_mdio.c b/drivers/net/mdio/fwnode_mdio.c
> index eb344f6d4a7b..e584abda585b 100644
> --- a/drivers/net/mdio/fwnode_mdio.c
> +++ b/drivers/net/mdio/fwnode_mdio.c
> @@ -97,10 +97,8 @@ int fwnode_mdiobus_phy_device_register(struct mii_bus *mdio,
>  	 * register it
>  	 */
>  	rc = phy_device_register(phy);
> -	if (rc) {
> -		fwnode_handle_put(child);
> +	if (rc)
>  		return rc;
> -	}

The current code is:

        /* Associate the fwnode with the device structure so it
         * can be looked up later
         */
        fwnode_handle_get(child);
        device_set_node(&phy->mdio.dev, child);

        /* All data is now stored in the phy struct;
         * register it
         */
        rc = phy_device_register(phy);
        if (rc) {
                fwnode_handle_put(child);
                return rc;
        }

The fwnode_handle_put(child) balances the earlier
fwnode_handle_get(child).

The code will look wrong without this balance. Please find a better
fix which actually looks correct.

    Andrew
diff mbox series

Patch

diff --git a/drivers/net/mdio/fwnode_mdio.c b/drivers/net/mdio/fwnode_mdio.c
index eb344f6d4a7b..e584abda585b 100644
--- a/drivers/net/mdio/fwnode_mdio.c
+++ b/drivers/net/mdio/fwnode_mdio.c
@@ -97,10 +97,8 @@  int fwnode_mdiobus_phy_device_register(struct mii_bus *mdio,
 	 * register it
 	 */
 	rc = phy_device_register(phy);
-	if (rc) {
-		fwnode_handle_put(child);
+	if (rc)
 		return rc;
-	}
 
 	dev_dbg(&mdio->dev, "registered phy %p fwnode at address %i\n",
 		child, addr);
@@ -152,10 +150,8 @@  int fwnode_mdiobus_register_phy(struct mii_bus *bus,
 
 		/* All data is now stored in the phy struct, so register it */
 		rc = phy_device_register(phy);
-		if (rc) {
-			fwnode_handle_put(phy->mdio.dev.fwnode);
+		if (rc)
 			goto clean_phy;
-		}
 	} else if (is_of_node(child)) {
 		rc = fwnode_mdiobus_phy_device_register(bus, phy, child, addr);
 		if (rc)