[net-next] net: stmmac: dwmac-sun8i: Defer probe when the EPHY clock is not ready

Message ID 20261113-b4-sun8i-emac-ephy-defer-v1-1-12a8591702fe@gmail.com (mailing list archive)
State New
Headers
Series [net-next] net: stmmac: dwmac-sun8i: Defer probe when the EPHY clock is not ready |

Commit Message

Arun Iyengar via B4 Relay Nov. 13, 2026, 8:04 a.m. UTC
From: Arun Iyengar <aiyengar1990@gmail.com>

get_ephy_nodes() skips an internal PHY node on any of_clk_get() error.
If the EPHY clock provider is not registered yet, the node is skipped,
the function returns -ENODEV and the probe fails for good. The reset
lookup right below already returns -EPROBE_DEFER in this case.

Return -EPROBE_DEFER for the clock too. Other clock errors still skip
the node, as before.

Fixes: 9f93ac8d4085 ("net-next: stmmac: Add dwmac-sun8i")
Signed-off-by: Arun Iyengar <aiyengar1990@gmail.com>
---
Reported by the Sashiko review bot while it reviewed my MDIO mux error
path fix for this driver:
https://sashiko.dev/#/bug/linux-98384c14-541e-410e-a074-fff5e98b3820

Only the H3/H5 and V3s variants (soc_has_internal_phy) reach this code.
With the in-tree device trees the EPHY clock comes from the same CCU as
the EMAC clock and reset, so the EMAC probe already waits for that CCU
and this cannot fail there. It matters when the EPHY clock comes
from a provider that probes later. Build-tested only (W=1, arm64 and
arm); I have no board with the internal PHY.

This patch was developed with Claude (Anthropic) as a coding assistant:
it checked the report against the code and wrote the fix and the commit
message. I reviewed it.
---
 drivers/net/ethernet/stmicro/stmmac/dwmac-sun8i.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)


---
base-commit: 573af801552999954caa8347e4490b04cf4763f0
change-id: 20261113-b4-sun8i-emac-ephy-defer-480c39394e6a

Best regards,
--  
Arun Iyengar <aiyengar1990@gmail.com>
  

Comments

netdev-bot+sashiko@kernel.org Nov. 17, 2026, 8:13 a.m. UTC | #1
Thank you for your contribution! Sashiko AI review found 1 potential
issue(s) to consider.

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

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 makes get_ephy_nodes() in
  drivers/net/ethernet/stmicro/stmmac/dwmac-sun8i.c return -EPROBE_DEFER…

--

net: stmmac: dwmac-sun8i: Defer probe when the EPHY clock is not ready

With this patch, get_ephy_nodes() returns -EPROBE_DEFER when of_clk_get()
on the internal PHY node returns -EPROBE_DEFER. Before, it skipped the
node and the probe ended with -ENODEV. Other clock errors still skip the
node.

> diff --git a/drivers/net/ethernet/stmicro/stmmac/dwmac-sun8i.c b/drivers/net/ethernet/stmicro/stmmac/dwmac-sun8i.c
> index 2ece7ba9dd73b..8163e49fa7ad0 100644
> --- a/drivers/net/ethernet/stmicro/stmmac/dwmac-sun8i.c
> +++ b/drivers/net/ethernet/stmicro/stmmac/dwmac-sun8i.c
> @@ -801,8 +801,13 @@ static int get_ephy_nodes(struct stmmac_priv *priv)
>  		struct clk *ephy_clk;
>  
>  		ephy_clk = of_clk_get(iphynode, 0);
> -		if (IS_ERR(ephy_clk))
> +		if (IS_ERR(ephy_clk)) {
> +			if (PTR_ERR(ephy_clk) == -EPROBE_DEFER) {
> +				of_node_put(mdio_internal);
> +				return -EPROBE_DEFER;
> +			}
>  			continue;
> +		}
>  		gmac->rst_ephy = of_reset_control_get_exclusive(iphynode, NULL);
>  		if (IS_ERR(gmac->rst_ephy)) {
>  			ret = PTR_ERR(gmac->rst_ephy);

[Severity: High]
Does this deferral come too late in the probe?

The only caller of get_ephy_nodes() is sun8i_dwmac_probe(), and it calls
it after stmmac_pltfr_probe() has already run:

sun8i_dwmac_probe()
  stmmac_pltfr_probe()
    sun8i_dwmac_init()          (plat->init, enables the phy regulator)
    __stmmac_dvr_probe()
      deasserts the stmmac reset
      stmmac_mdio_register(ndev)
      register_netdev(ndev)
  pm_runtime_get_sync()
  get_ephy_nodes()
    return -EPROBE_DEFER
  goto dwmac_remove
    stmmac_pltfr_remove()
    sun8i_dwmac_unset_syscon()

So when the new -EPROBE_DEFER is returned, the MDIO bus and an ethN
netdev already exist as children of the platform device.

Documentation/driver-api/driver-model/driver.rst says to defer as early
as possible, and it also says:

  -EPROBE_DEFER must not be returned if probe() has already created
  child devices, even if those child devices are removed again
  in a cleanup path.

The commit message describes an EPHY clock provider that probes later.
In that case, wouldn't every deferred probe retry do all of the
following?

  - power up the phy regulator
  - toggle the MAC reset
  - register and unregister the MDIO bus and an ethN interface that
    userspace can see
  - rewrite the syscon EMAC clock register

driver_probe_device() also re-triggers deferred probing if
deferred_trigger_count changes during a probe:

drivers/base/dd.c:driver_probe_device() {
    ...
	if (trigger_count != atomic_read(&deferred_trigger_count) &&
	    !defer_all_probes)
		driver_deferred_probe_trigger();
    ...
}

If another device binds during any attempt, this starts another full
cycle. That can become the endless .probe() loop the documentation warns
about.

get_ephy_nodes() only uses priv->device->of_node and the sunxi_priv_data.
None of the mdio-mux lookup, of_clk_get() or
of_reset_control_get_exclusive() on the iphy node needs the MAC to be up.
The "mux must be registered after parent MDIO" comment only seems to
constrain sun8i_dwmac_register_mdio_mux().

Could the EPHY clock and reset lookup move into sun8i_dwmac_probe(),
before stmmac_pltfr_probe()?

The commit message uses the existing reset lookup deferral as precedent.
That path returns -EPROBE_DEFER from the same late point. Would moving the
lookup earlier fix both cases, instead of adding a second late deferral?
  
Maxime Chevallier Nov. 17, 2026, 9:35 a.m. UTC | #2
Hi,

On 11/17/26 09:13, netdev-bot+sashiko@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 1 potential
> issue(s) to consider.
> 
> Critical: 0 · High: 1 · Medium: 0 · Low: 0
> 
> 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 makes get_ephy_nodes() in
>   drivers/net/ethernet/stmicro/stmmac/dwmac-sun8i.c return -EPROBE_DEFER…
> 
> --
> 
> net: stmmac: dwmac-sun8i: Defer probe when the EPHY clock is not ready
> 
> With this patch, get_ephy_nodes() returns -EPROBE_DEFER when of_clk_get()
> on the internal PHY node returns -EPROBE_DEFER. Before, it skipped the
> node and the probe ended with -ENODEV. Other clock errors still skip the
> node.
> 
>> diff --git a/drivers/net/ethernet/stmicro/stmmac/dwmac-sun8i.c b/drivers/net/ethernet/stmicro/stmmac/dwmac-sun8i.c
>> index 2ece7ba9dd73b..8163e49fa7ad0 100644
>> --- a/drivers/net/ethernet/stmicro/stmmac/dwmac-sun8i.c
>> +++ b/drivers/net/ethernet/stmicro/stmmac/dwmac-sun8i.c
>> @@ -801,8 +801,13 @@ static int get_ephy_nodes(struct stmmac_priv *priv)
>>  		struct clk *ephy_clk;
>>  
>>  		ephy_clk = of_clk_get(iphynode, 0);
>> -		if (IS_ERR(ephy_clk))
>> +		if (IS_ERR(ephy_clk)) {
>> +			if (PTR_ERR(ephy_clk) == -EPROBE_DEFER) {
>> +				of_node_put(mdio_internal);
>> +				return -EPROBE_DEFER;
>> +			}
>>  			continue;
>> +		}
>>  		gmac->rst_ephy = of_reset_control_get_exclusive(iphynode, NULL);
>>  		if (IS_ERR(gmac->rst_ephy)) {
>>  			ret = PTR_ERR(gmac->rst_ephy);
> 
> [Severity: High]
> Does this deferral come too late in the probe?
I agree with the bot here, this defer happens after the netdev is registered,
which isn't great... And it's likely the PHY is on that mux'd bus, no ?

Was this tested ? Doesn't it really work to return a probedefer so late ?

This mdio mux logic should likely be reworked so that we register the mux in due
time (i.e during the mdio init sequence, may be by hooking into stmmac_mdio_setup
or something like that ?)

Maxime
  
Arun Iyengar Nov. 18, 2026, 3:19 a.m. UTC | #3
Hi Maxime,

You and the bot are right: get_ephy_nodes() runs after stmmac_pltfr_probe()
has registered the netdev, so the deferral comes too late.

Yes, the internal and the external PHY are both behind the mux. stmmac
attaches the PHY at open, so today the mux is registered only after the
netdev is visible.

It was build-tested only, as the notes said; I have no H3, H5 or V3s board
with the internal PHY.

I will drop this patch and move the mux registration into the MDIO setup,
together with the probe and remove ordering that Chen-Yu raised on the mux
use-after-free patch.
  

Patch

diff --git a/drivers/net/ethernet/stmicro/stmmac/dwmac-sun8i.c b/drivers/net/ethernet/stmicro/stmmac/dwmac-sun8i.c
index 2ece7ba9dd73..8163e49fa7ad 100644
--- a/drivers/net/ethernet/stmicro/stmmac/dwmac-sun8i.c
+++ b/drivers/net/ethernet/stmicro/stmmac/dwmac-sun8i.c
@@ -801,8 +801,13 @@  static int get_ephy_nodes(struct stmmac_priv *priv)
 		struct clk *ephy_clk;
 
 		ephy_clk = of_clk_get(iphynode, 0);
-		if (IS_ERR(ephy_clk))
+		if (IS_ERR(ephy_clk)) {
+			if (PTR_ERR(ephy_clk) == -EPROBE_DEFER) {
+				of_node_put(mdio_internal);
+				return -EPROBE_DEFER;
+			}
 			continue;
+		}
 		gmac->rst_ephy = of_reset_control_get_exclusive(iphynode, NULL);
 		if (IS_ERR(gmac->rst_ephy)) {
 			ret = PTR_ERR(gmac->rst_ephy);