Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH net] net: stmmac: rk: Fix clock reference leak on probe failure
@ 2026-12-01  8:29 Wanhui Fu
  2026-12-02 18:56 ` Maxime Chevallier
  2026-12-03 23:31 ` netdev-bot+sashiko
  0 siblings, 2 replies; 4+ messages in thread
From: Wanhui Fu @ 2026-12-01  8:29 UTC (permalink / raw)
  To: heiko, maxime.chevallier, andrew+netdev, davem, edumazet, kuba,
	pabeni, mcoquelin.stm32, alexandre.torgue, david.wu
  Cc: linux-arm-kernel, linux-rockchip, netdev, linux-stm32, Wanhui Fu

rk_gmac_clk_init() acquires a clock reference using of_clk_get().
If the subsequent platform init callback fails, stmmac_dvr_probe()
returns without calling rk_gmac_exit(), leaking the clock reference.

Fix this by using devm_get_clk_from_child() and removing the
explicit clk_put() from rk_gmac_exit().

Found it by self-developed static analysis tool and compile-test
passed.

Fixes: fecd4d7eef8b ("net: stmmac: dwmac-rk: Add integrated PHY support")
Signed-off-by: Wanhui Fu <fuwh1120@163.com>
---
 drivers/net/ethernet/stmicro/stmmac/dwmac-rk.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/drivers/net/ethernet/stmicro/stmmac/dwmac-rk.c b/drivers/net/ethernet/stmicro/stmmac/dwmac-rk.c
index a37df5767d84..82aed5df1eb5 100644
--- a/drivers/net/ethernet/stmicro/stmmac/dwmac-rk.c
+++ b/drivers/net/ethernet/stmicro/stmmac/dwmac-rk.c
@@ -1168,7 +1168,8 @@ static int rk_gmac_clk_init(struct plat_stmmacenet_data *plat)
 	}
 
 	if (plat->phy_node && bsp_priv->integrated_phy) {
-		bsp_priv->clk_phy = of_clk_get(plat->phy_node, 0);
+		bsp_priv->clk_phy = devm_get_clk_from_child(dev,
+							    plat->phy_node, NULL);
 		ret = PTR_ERR_OR_ZERO(bsp_priv->clk_phy);
 		if (ret)
 			return dev_err_probe(dev, ret, "Cannot get PHY clock\n");
@@ -1637,14 +1638,10 @@ static int rk_gmac_init(struct device *dev, void *bsp_priv)
 
 static void rk_gmac_exit(struct device *dev, void *bsp_priv_)
 {
-	struct stmmac_priv *priv = netdev_priv(dev_get_drvdata(dev));
 	struct rk_priv_data *bsp_priv = bsp_priv_;
 
 	rk_gmac_powerdown(bsp_priv);
 
-	if (priv->plat->phy_node && bsp_priv->integrated_phy)
-		clk_put(bsp_priv->clk_phy);
-
 	reset_control_put(bsp_priv->phy_reset);
 }
 
-- 
2.25.1



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

* Re: [PATCH net] net: stmmac: rk: Fix clock reference leak on probe failure
  2026-12-01  8:29 [PATCH net] net: stmmac: rk: Fix clock reference leak on probe failure Wanhui Fu
@ 2026-12-02 18:56 ` Maxime Chevallier
  2026-12-03  2:32   ` fuwh1120
  2026-12-03 23:31 ` netdev-bot+sashiko
  1 sibling, 1 reply; 4+ messages in thread
From: Maxime Chevallier @ 2026-12-02 18:56 UTC (permalink / raw)
  To: Wanhui Fu, heiko, andrew+netdev, davem, edumazet, kuba, pabeni,
	mcoquelin.stm32, alexandre.torgue, david.wu
  Cc: linux-arm-kernel, linux-rockchip, netdev, linux-stm32

Hi,

On 12/1/26 09:29, Wanhui Fu wrote:
> rk_gmac_clk_init() acquires a clock reference using of_clk_get().
> If the subsequent platform init callback fails, stmmac_dvr_probe()
> returns without calling rk_gmac_exit(), leaking the clock reference.
> 
> Fix this by using devm_get_clk_from_child() and removing the
> explicit clk_put() from rk_gmac_exit().
> 
> Found it by self-developed static analysis tool and compile-test
> passed.
> 
> Fixes: fecd4d7eef8b ("net: stmmac: dwmac-rk: Add integrated PHY support")
> Signed-off-by: Wanhui Fu <fuwh1120@163.com>

This should go to net-next, it's not a regression and only happens in
the failure path, likely not easy to hit in real life.

Please repost,

Maxime

pw-bot: cr

> ---
>  drivers/net/ethernet/stmicro/stmmac/dwmac-rk.c | 7 ++-----
>  1 file changed, 2 insertions(+), 5 deletions(-)
> 
> diff --git a/drivers/net/ethernet/stmicro/stmmac/dwmac-rk.c b/drivers/net/ethernet/stmicro/stmmac/dwmac-rk.c
> index a37df5767d84..82aed5df1eb5 100644
> --- a/drivers/net/ethernet/stmicro/stmmac/dwmac-rk.c
> +++ b/drivers/net/ethernet/stmicro/stmmac/dwmac-rk.c
> @@ -1168,7 +1168,8 @@ static int rk_gmac_clk_init(struct plat_stmmacenet_data *plat)
>  	}
>  
>  	if (plat->phy_node && bsp_priv->integrated_phy) {
> -		bsp_priv->clk_phy = of_clk_get(plat->phy_node, 0);
> +		bsp_priv->clk_phy = devm_get_clk_from_child(dev,
> +							    plat->phy_node, NULL);
>  		ret = PTR_ERR_OR_ZERO(bsp_priv->clk_phy);
>  		if (ret)
>  			return dev_err_probe(dev, ret, "Cannot get PHY clock\n");
> @@ -1637,14 +1638,10 @@ static int rk_gmac_init(struct device *dev, void *bsp_priv)
>  
>  static void rk_gmac_exit(struct device *dev, void *bsp_priv_)
>  {
> -	struct stmmac_priv *priv = netdev_priv(dev_get_drvdata(dev));
>  	struct rk_priv_data *bsp_priv = bsp_priv_;
>  
>  	rk_gmac_powerdown(bsp_priv);
>  
> -	if (priv->plat->phy_node && bsp_priv->integrated_phy)
> -		clk_put(bsp_priv->clk_phy);
> -
>  	reset_control_put(bsp_priv->phy_reset);
>  }
>  



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

* Re: [PATCH net] net: stmmac: rk: Fix clock reference leak on probe failure
  2026-12-02 18:56 ` Maxime Chevallier
@ 2026-12-03  2:32   ` fuwh1120
  0 siblings, 0 replies; 4+ messages in thread
From: fuwh1120 @ 2026-12-03  2:32 UTC (permalink / raw)
  To: Maxime Chevallier
  Cc: heiko, andrew+netdev, davem, edumazet, kuba, pabeni,
	mcoquelin.stm32, alexandre.torgue, david.wu, linux-arm-kernel,
	linux-rockchip, netdev, linux-stm32


Hi,

At 2026-12-03 02:56:22, "Maxime Chevallier" <maxime.chevallier@bootlin.com> wrote:
>
>This should go to net-next, it's not a regression and only happens in
>the failure path, likely not easy to hit in real life.
>
>Please repost,
>

Thanks for suggestion! I will repost it.

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

* Re: [PATCH net] net: stmmac: rk: Fix clock reference leak on probe failure
  2026-12-01  8:29 [PATCH net] net: stmmac: rk: Fix clock reference leak on probe failure Wanhui Fu
  2026-12-02 18:56 ` Maxime Chevallier
@ 2026-12-03 23:31 ` netdev-bot+sashiko
  1 sibling, 0 replies; 4+ messages in thread
From: netdev-bot+sashiko @ 2026-12-03 23:31 UTC (permalink / raw)
  To: fuwh1120
  Cc: heiko, maxime.chevallier, andrew+netdev, davem, edumazet, kuba,
	pabeni, mcoquelin.stm32, alexandre.torgue, david.wu,
	linux-arm-kernel, linux-rockchip, netdev, linux-stm32

Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.

Critical: 0 · High: 1 · Medium: 0 · Low: 1

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [High] The patch is incomplete.
- [Low] The patch silently fixes a crash that the commit message never
  mentions.

--

net: stmmac: rk: Fix clock reference leak on probe failure

This patch changes rk_gmac_clk_init() to get the integrated PHY clock
with devm_get_clk_from_child() instead of of_clk_get(). It also drops
the matching clk_put() from rk_gmac_exit(). The goal is to stop leaking
the clock reference when the platform init callback fails in
stmmac_dvr_probe().

> diff --git a/drivers/net/ethernet/stmicro/stmmac/dwmac-rk.c b/drivers/net/ethernet/stmicro/stmmac/dwmac-rk.c
> index a37df5767d84e..67561252224ae 100644
> --- a/drivers/net/ethernet/stmicro/stmmac/dwmac-rk.c
> +++ b/drivers/net/ethernet/stmicro/stmmac/dwmac-rk.c

[ ... ]

> @@ -1637,14 +1638,10 @@ static int rk_gmac_init(struct device *dev, void *bsp_priv)
>  
>  static void rk_gmac_exit(struct device *dev, void *bsp_priv_)
>  {
> -	struct stmmac_priv *priv = netdev_priv(dev_get_drvdata(dev));
>  	struct rk_priv_data *bsp_priv = bsp_priv_;
>  
>  	rk_gmac_powerdown(bsp_priv);
>  
> -	if (priv->plat->phy_node && bsp_priv->integrated_phy)
> -		clk_put(bsp_priv->clk_phy);
> -

[Severity: Low]
This isn't a bug, but removing this dereference also changes behavior in
a way the commit message doesn't mention.

stmmac_dvr_probe() calls ->exit() whenever __stmmac_dvr_probe() fails:

	ret = __stmmac_dvr_probe(dev, plat_dat, res);
	if (ret && plat_dat->exit)
		plat_dat->exit(dev, plat_dat->bsp_priv);

Some of those failures happen before __stmmac_dvr_probe() reaches
dev_set_drvdata(device, priv->dev):

- an invalid DMA config
- a devm_alloc_etherdev_mqs() allocation failure
- a devm_netdev_alloc_pcpu_stats() allocation failure
- a stmmac_msi_init() failure

In those cases drvdata is still NULL. The old rk_gmac_exit() then
computed netdev_priv(NULL) and read priv->plat, which would fault.

That dereference came from 1a62894e040d ("net: stmmac: rk: convert to
init()/exit() methods"), not from fecd4d7eef8b.

Could the commit message say that this also fixes an oops on early probe
failure? A Fixes: tag for 1a62894e040d would also show stable
backporters which trees need it.

>  	reset_control_put(bsp_priv->phy_reset);
>  }

[Severity: High]
Does bsp_priv->phy_reset still leak on the same failure path the commit
message describes?

rk_gmac_setup() takes phy_reset with the non-devm, exclusive getter. It
does this under the same integrated_phy condition as clk_phy, and both
came from fecd4d7eef8b:

rk_gmac_setup() {
    ...
		if (bsp_priv->integrated_phy) {
			bsp_priv->phy_reset = of_reset_control_get(plat->phy_node, NULL);
    ...
}

rk_gmac_exit() is still the only place that releases it at runtime. When
rk_gmac_init() fails, stmmac_dvr_probe() returns without calling
->exit():

stmmac_dvr_probe() {
	if (plat_dat->init) {
		ret = plat_dat->init(dev, plat_dat->bsp_priv);
		if (ret)
			return ret;
	}
    ...
}

The same thing happens when rk_gmac_clk_init() fails in
rk_gmac_probe(). That includes -EPROBE_DEFER from
devm_clk_bulk_get_optional() or from the new devm_get_clk_from_child():

rk_gmac_probe() {
    ...
	ret = rk_gmac_clk_init(plat_dat);
	if (ret)
		return ret;
    ...
}

On the next probe attempt, could __reset_control_get_internal() find
the leaked exclusive reset control and hit this?

	if (WARN_ON(!rstc->shared || !shared))
		return ERR_PTR(-EBUSY);

If so, rk_gmac_setup() would print "No PHY reset control found." and set
phy_reset to NULL.

With phy_reset NULL, rk_gmac_integrated_ephy_powerup() skips its whole
if (priv->phy_reset) block. That block holds the only write of
RK_MACPHY_ENABLE. rk_gmac_integrated_ephy_powerdown() still writes
RK_MACPHY_DISABLE.

After the first powerdown (suspend or remove), would the integrated PHY
then stay disabled?

Could phy_reset also be devm-managed here, for example with a devm
reset getter or devm_add_action_or_reset()? That would cover both
integrated PHY resources on these failure paths.

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261201082932.3199297-1-fuwh1120%40163.com


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

end of thread, other threads:[~2026-12-03 23:32 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01  8:29 [PATCH net] net: stmmac: rk: Fix clock reference leak on probe failure Wanhui Fu
2026-12-02 18:56 ` Maxime Chevallier
2026-12-03  2:32   ` fuwh1120
2026-12-03 23:31 ` netdev-bot+sashiko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox