netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v1] net:thunder_bgx:Fix resource leaks in device_for_each_child_node() loops
@ 2023-07-04 14:14 Wang Ming
  2023-07-04 20:30 ` Simon Horman
  0 siblings, 1 reply; 3+ messages in thread
From: Wang Ming @ 2023-07-04 14:14 UTC (permalink / raw)
  To: Sunil Goutham, David S. Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, linux-arm-kernel, netdev, linux-kernel
  Cc: opensource.kernel, Wang Ming

The device_for_each_child_node() loop in
bgx_init_of_phy() function should have
fwnode_handle_put() before break which could
avoid resource leaks. 
This patch could fix this bug.

Signed-off-by: Wang Ming <machel@vivo.com>
---
 drivers/net/ethernet/cavium/thunder/thunder_bgx.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/drivers/net/ethernet/cavium/thunder/thunder_bgx.c b/drivers/net/ethernet/cavium/thunder/thunder_bgx.c
index a317feb8d..dad32d36a 100644
--- a/drivers/net/ethernet/cavium/thunder/thunder_bgx.c
+++ b/drivers/net/ethernet/cavium/thunder/thunder_bgx.c
@@ -1478,8 +1478,10 @@ static int bgx_init_of_phy(struct bgx *bgx)
 		 * cannot handle it, so exit the loop.
 		 */
 		node = to_of_node(fwn);
-		if (!node)
+		if (!node) {
+			fwnode_handle_put(fwn);
 			break;
+		}
 
 		of_get_mac_address(node, bgx->lmac[lmac].mac);
 
@@ -1503,6 +1505,7 @@ static int bgx_init_of_phy(struct bgx *bgx)
 		lmac++;
 		if (lmac == bgx->max_lmac) {
 			of_node_put(node);
+			fwnode_handle_put(fwn);
 			break;
 		}
 	}
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v1] net:thunder_bgx:Fix resource leaks in device_for_each_child_node() loops
  2023-07-04 14:14 [PATCH v1] net:thunder_bgx:Fix resource leaks in device_for_each_child_node() loops Wang Ming
@ 2023-07-04 20:30 ` Simon Horman
  2023-07-05  1:20   ` 回复: " 王明-软件底层技术部
  0 siblings, 1 reply; 3+ messages in thread
From: Simon Horman @ 2023-07-04 20:30 UTC (permalink / raw)
  To: Wang Ming
  Cc: Sunil Goutham, David S. Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, linux-arm-kernel, netdev, linux-kernel,
	opensource.kernel

On Tue, Jul 04, 2023 at 10:14:47PM +0800, Wang Ming wrote:
> The device_for_each_child_node() loop in
> bgx_init_of_phy() function should have
> fwnode_handle_put() before break which could
> avoid resource leaks. 
> This patch could fix this bug.

Hi Wang Ming,

If this fixes a bug then it probably warrants a fixes tag.
And it should also probably be targeted at the next tree:

	[PATCH net v2] ...

If so, please allow 24h to elapse before posting v2.

Else it should be targeted at the net-next tree,
and posted after net-next reopens after July 10th:

	[PATCH net-next v2] ...

Also, looking at git history, I think a more appropriate subject prefix
would be 'net: thunderx: '

	[PATCH net v2] net: thunderx: ...

Link: https://www.kernel.org/doc/html/next/process/maintainer-netdev.html

> Signed-off-by: Wang Ming <machel@vivo.com>
> ---
>  drivers/net/ethernet/cavium/thunder/thunder_bgx.c | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/net/ethernet/cavium/thunder/thunder_bgx.c b/drivers/net/ethernet/cavium/thunder/thunder_bgx.c
> index a317feb8d..dad32d36a 100644
> --- a/drivers/net/ethernet/cavium/thunder/thunder_bgx.c
> +++ b/drivers/net/ethernet/cavium/thunder/thunder_bgx.c
> @@ -1478,8 +1478,10 @@ static int bgx_init_of_phy(struct bgx *bgx)
>  		 * cannot handle it, so exit the loop.
>  		 */
>  		node = to_of_node(fwn);
> -		if (!node)
> +		if (!node) {
> +			fwnode_handle_put(fwn);
>  			break;
> +		}
>  
>  		of_get_mac_address(node, bgx->lmac[lmac].mac);
>  
> @@ -1503,6 +1505,7 @@ static int bgx_init_of_phy(struct bgx *bgx)
>  		lmac++;
>  		if (lmac == bgx->max_lmac) {
>  			of_node_put(node);
> +			fwnode_handle_put(fwn);
>  			break;
>  		}
>  	}

Should this change also apply to the 'goto defer' case in the same loop?
If so, perhaps a more idiomatic approach of releasing resources
in a ladder of goto labels is appropriate. Something like this (untested!):

diff --git a/drivers/net/ethernet/cavium/thunder/thunder_bgx.c b/drivers/net/ethernet/cavium/thunder/thunder_bgx.c
index a317feb8decb..3091c96134e4 100644
--- a/drivers/net/ethernet/cavium/thunder/thunder_bgx.c
+++ b/drivers/net/ethernet/cavium/thunder/thunder_bgx.c
@@ -1469,6 +1469,7 @@ static int bgx_init_of_phy(struct bgx *bgx)
 	struct fwnode_handle *fwn;
 	struct device_node *node = NULL;
 	u8 lmac = 0;
+	int err = 0;
 
 	device_for_each_child_node(&bgx->pdev->dev, fwn) {
 		struct phy_device *pd;
@@ -1479,7 +1480,7 @@ static int bgx_init_of_phy(struct bgx *bgx)
 		 */
 		node = to_of_node(fwn);
 		if (!node)
-			break;
+			goto out_handle_put;
 
 		of_get_mac_address(node, bgx->lmac[lmac].mac);
 
@@ -1501,10 +1502,8 @@ static int bgx_init_of_phy(struct bgx *bgx)
 		}
 
 		lmac++;
-		if (lmac == bgx->max_lmac) {
-			of_node_put(node);
-			break;
-		}
+		if (lmac == bgx->max_lmac)
+			goto out_node_put;
 	}
 	return 0;
 
@@ -1519,8 +1518,12 @@ static int bgx_init_of_phy(struct bgx *bgx)
 		}
 		lmac--;
 	}
+	err = -EPROBE_DEFER;
+out_node_put:
 	of_node_put(node);
-	return -EPROBE_DEFER;
+out_handle_put:
+	fwnode_handle_put(fwn);
+	return err;
 }
 
 #else

-- 
pw-bot: changes-requested


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* 回复: [PATCH v1] net:thunder_bgx:Fix resource leaks in device_for_each_child_node() loops
  2023-07-04 20:30 ` Simon Horman
@ 2023-07-05  1:20   ` 王明-软件底层技术部
  0 siblings, 0 replies; 3+ messages in thread
From: 王明-软件底层技术部 @ 2023-07-05  1:20 UTC (permalink / raw)
  To: Simon Horman
  Cc: Sunil Goutham, David S. Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, linux-arm-kernel@lists.infradead.org,
	netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
	opensource.kernel

Thank you.Ok, I will repost [PATCH net v2] net: thunderx:... in 24 hours. 
-----邮件原件-----
发件人: Simon Horman <simon.horman@corigine.com> 
发送时间: 2023年7月5日 4:30
收件人: 王明-软件底层技术部 <machel@vivo.com>
抄送: Sunil Goutham <sgoutham@marvell.com>; David S. Miller <davem@davemloft.net>; Eric Dumazet <edumazet@google.com>; Jakub Kicinski <kuba@kernel.org>; Paolo Abeni <pabeni@redhat.com>; linux-arm-kernel@lists.infradead.org; netdev@vger.kernel.org; linux-kernel@vger.kernel.org; opensource.kernel <opensource.kernel@vivo.com>
主题: Re: [PATCH v1] net:thunder_bgx:Fix resource leaks in device_for_each_child_node() loops

On Tue, Jul 04, 2023 at 10:14:47PM +0800, Wang Ming wrote:
> The device_for_each_child_node() loop in
> bgx_init_of_phy() function should have
> fwnode_handle_put() before break which could avoid resource leaks.
> This patch could fix this bug.

Hi Wang Ming,

If this fixes a bug then it probably warrants a fixes tag.
And it should also probably be targeted at the next tree:

	[PATCH net v2] ...

If so, please allow 24h to elapse before posting v2.

Else it should be targeted at the net-next tree, and posted after net-next reopens after July 10th:

	[PATCH net-next v2] ...

Also, looking at git history, I think a more appropriate subject prefix would be 'net: thunderx: '

	[PATCH net v2] net: thunderx: ...

Link: https://www.kernel.org/doc/html/next/process/maintainer-netdev.html

> Signed-off-by: Wang Ming <machel@vivo.com>
> ---
>  drivers/net/ethernet/cavium/thunder/thunder_bgx.c | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/net/ethernet/cavium/thunder/thunder_bgx.c 
> b/drivers/net/ethernet/cavium/thunder/thunder_bgx.c
> index a317feb8d..dad32d36a 100644
> --- a/drivers/net/ethernet/cavium/thunder/thunder_bgx.c
> +++ b/drivers/net/ethernet/cavium/thunder/thunder_bgx.c
> @@ -1478,8 +1478,10 @@ static int bgx_init_of_phy(struct bgx *bgx)
>  		 * cannot handle it, so exit the loop.
>  		 */
>  		node = to_of_node(fwn);
> -		if (!node)
> +		if (!node) {
> +			fwnode_handle_put(fwn);
>  			break;
> +		}
>  
>  		of_get_mac_address(node, bgx->lmac[lmac].mac);
>  
> @@ -1503,6 +1505,7 @@ static int bgx_init_of_phy(struct bgx *bgx)
>  		lmac++;
>  		if (lmac == bgx->max_lmac) {
>  			of_node_put(node);
> +			fwnode_handle_put(fwn);
>  			break;
>  		}
>  	}

Should this change also apply to the 'goto defer' case in the same loop?
If so, perhaps a more idiomatic approach of releasing resources in a ladder of goto labels is appropriate. Something like this (untested!):

diff --git a/drivers/net/ethernet/cavium/thunder/thunder_bgx.c b/drivers/net/ethernet/cavium/thunder/thunder_bgx.c
index a317feb8decb..3091c96134e4 100644
--- a/drivers/net/ethernet/cavium/thunder/thunder_bgx.c
+++ b/drivers/net/ethernet/cavium/thunder/thunder_bgx.c
@@ -1469,6 +1469,7 @@ static int bgx_init_of_phy(struct bgx *bgx)
 	struct fwnode_handle *fwn;
 	struct device_node *node = NULL;
 	u8 lmac = 0;
+	int err = 0;
 
 	device_for_each_child_node(&bgx->pdev->dev, fwn) {
 		struct phy_device *pd;
@@ -1479,7 +1480,7 @@ static int bgx_init_of_phy(struct bgx *bgx)
 		 */
 		node = to_of_node(fwn);
 		if (!node)
-			break;
+			goto out_handle_put;
 
 		of_get_mac_address(node, bgx->lmac[lmac].mac);
 
@@ -1501,10 +1502,8 @@ static int bgx_init_of_phy(struct bgx *bgx)
 		}
 
 		lmac++;
-		if (lmac == bgx->max_lmac) {
-			of_node_put(node);
-			break;
-		}
+		if (lmac == bgx->max_lmac)
+			goto out_node_put;
 	}
 	return 0;
 
@@ -1519,8 +1518,12 @@ static int bgx_init_of_phy(struct bgx *bgx)
 		}
 		lmac--;
 	}
+	err = -EPROBE_DEFER;
+out_node_put:
 	of_node_put(node);
-	return -EPROBE_DEFER;
+out_handle_put:
+	fwnode_handle_put(fwn);
+	return err;
 }
 
 #else

--
pw-bot: changes-requested


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-07-05  1:20 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-07-04 14:14 [PATCH v1] net:thunder_bgx:Fix resource leaks in device_for_each_child_node() loops Wang Ming
2023-07-04 20:30 ` Simon Horman
2023-07-05  1:20   ` 回复: " 王明-软件底层技术部

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).