diff mbox series

[net,v3] net: mdiobus: fix double put fwnode in the error path

Message ID 20221202051833.699945-1-yangyingliang@huawei.com (mailing list archive)
State Accepted
Commit 165df24186ecea95705505627df3dacf5e7ff6bf
Delegated to: Netdev Maintainers
Headers show
Series [net,v3] net: mdiobus: fix double put fwnode 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 success total: 0 errors, 0 warnings, 0 checks, 16 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Yang Yingliang Dec. 2, 2022, 5:18 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() will be called in phy_device_release(),
but in the error path, fwnode_handle_put() has already been called,
so set fwnode to NULL after fwnode_handle_put() in the error path 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>
---
v2 -> v3:
  set fwnode to NULL before calling fwnode_handle_put()

v1 -> v2:
  Don't remove fwnode_handle_put() in the error path,
  set fwnode to NULL avoid double put.
---
 drivers/net/mdio/fwnode_mdio.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

Comments

Zeng Heng Dec. 3, 2022, 2:14 a.m. UTC | #1
On 2022/12/2 13:18, 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() will be called in phy_device_release(),
> but in the error path, fwnode_handle_put() has already been called,
> so set fwnode to NULL after fwnode_handle_put() in the error path 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>
> ---
> v2 -> v3:
>    set fwnode to NULL before calling fwnode_handle_put()
>
> v1 -> v2:
>    Don't remove fwnode_handle_put() in the error path,
>    set fwnode to NULL avoid double put.
> ---
>   drivers/net/mdio/fwnode_mdio.c | 4 +++-
>   1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/net/mdio/fwnode_mdio.c b/drivers/net/mdio/fwnode_mdio.c
> index eb344f6d4a7b..b782c35c4ac1 100644
> --- a/drivers/net/mdio/fwnode_mdio.c
> +++ b/drivers/net/mdio/fwnode_mdio.c
> @@ -98,6 +98,7 @@ int fwnode_mdiobus_phy_device_register(struct mii_bus *mdio,
>   	 */
>   	rc = phy_device_register(phy);
>   	if (rc) {
> +		device_set_node(&phy->mdio.dev, NULL);
>   		fwnode_handle_put(child);
>   		return rc;
>   	}
> @@ -153,7 +154,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);
> +			phy->mdio.dev.fwnode = NULL;
> +			fwnode_handle_put(child);
>   			goto clean_phy;
>   		}
>   	} else if (is_of_node(child)) {

Reviewed-by: Zeng Heng <zengheng4@huawei.com>

Tested-by: Zeng Heng <zengheng4@huawei.com>
patchwork-bot+netdevbpf@kernel.org Dec. 5, 2022, 11:10 a.m. UTC | #2
Hello:

This patch was applied to netdev/net.git (master)
by David S. Miller <davem@davemloft.net>:

On Fri, 2 Dec 2022 13:18:33 +0800 you 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() will be called in phy_device_release(),
> but in the error path, fwnode_handle_put() has already been called,
> so set fwnode to NULL after fwnode_handle_put() in the error path to
> avoid double put.
> 
> [...]

Here is the summary with links:
  - [net,v3] net: mdiobus: fix double put fwnode in the error path
    https://git.kernel.org/netdev/net/c/165df24186ec

You are awesome, thank you!
diff mbox series

Patch

diff --git a/drivers/net/mdio/fwnode_mdio.c b/drivers/net/mdio/fwnode_mdio.c
index eb344f6d4a7b..b782c35c4ac1 100644
--- a/drivers/net/mdio/fwnode_mdio.c
+++ b/drivers/net/mdio/fwnode_mdio.c
@@ -98,6 +98,7 @@  int fwnode_mdiobus_phy_device_register(struct mii_bus *mdio,
 	 */
 	rc = phy_device_register(phy);
 	if (rc) {
+		device_set_node(&phy->mdio.dev, NULL);
 		fwnode_handle_put(child);
 		return rc;
 	}
@@ -153,7 +154,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);
+			phy->mdio.dev.fwnode = NULL;
+			fwnode_handle_put(child);
 			goto clean_phy;
 		}
 	} else if (is_of_node(child)) {