Netdev List
 help / color / mirror / Atom feed
* [PATCH v2] net: phy: defer PHY probe when running in async probe context
@ 2026-11-25  8:09 Hans Zhang
  2026-11-29  8:40 ` netdev-bot+sashiko
  0 siblings, 1 reply; 2+ messages in thread
From: Hans Zhang @ 2026-11-25  8:09 UTC (permalink / raw)
  To: andrew, hkallweit1, davem, edumazet, kuba, pabeni
  Cc: linux, netdev, Hans Zhang, Kai Xia

phy_request_driver_module() synchronously calls request_module() so that
the PHY driver is loaded before phy_device_create() returns.  That call is
not safe from async probe context: module init may invoke
async_synchronize_full(), which waits for the current async worker and can
deadlock.

When this path is reached from an async worker, return -EPROBE_DEFER
instead.  The parent device probe is then retried later from the deferred
probe workqueue, where request_module() can run synchronously and the
guarantee that the PHY driver is loaded before device registration is
preserved.

This fixes the WARN below and the possible deadlock, observed on an arm64
board whose PCIe host controller uses PROBE_PREFER_ASYNCHRONOUS while
probing an RTL8126A:

  WARNING: kernel/module/kmod.c:145 at __request_module+0x198/0x200, CPU#5: kworker/u49:0/77
  Workqueue: async async_run_entry_fn
  Call trace:
   __request_module+0x198/0x200 (P)
   phy_request_driver_module+0x11c/0x17c
   phy_device_create+0x234/0x260
   get_phy_device+0x74/0x150
   mdiobus_scan+0x34/0x114
   __mdiobus_register+0x1ec/0x450
   __devm_mdiobus_register+0x70/0xe8
   rtl_init_one+0x1350/0x16b0
   local_pci_probe+0x40/0xa8
   pci_call_probe+0x64/0x174
   pci_device_probe+0xb4/0x144
   really_probe+0xbc/0x298
   __driver_probe_device+0x16c/0x19c
   driver_probe_device+0x3c/0x114
   __device_attach_driver+0xb8/0x118
   bus_for_each_drv+0x84/0xe0
   __device_attach+0x9c/0x194
   device_initial_probe+0x50/0x68
   pci_bus_add_device+0x6c/0xf8
   pci_bus_add_devices+0x3c/0x88
   pci_bus_add_devices+0x68/0x88
   pci_host_probe+0x90/0x108
   cdns_pcie_hpa_host_setup+0x300/0x684
   sky1_pcie_probe+0x230/0x360
   platform_probe+0x5c/0x9c
   really_probe+0xbc/0x298
   __driver_probe_device+0x16c/0x19c
   driver_probe_device+0x3c/0x114
   __driver_attach_async_helper+0x58/0xcc
   async_run_entry_fn+0x34/0xe0
   process_one_work+0x180/0x2dc
   worker_thread+0x188/0x304
   kthread+0x110/0x124
   ret_from_fork+0x10/0x20
   ---[ end trace 0000000000000000 ]---

Fixes: 0fdff3ec6d87 ("async, kmod: warn on synchronous request_module() from async workers")
Co-developed-by: Kai Xia <503536682@qq.com>
Signed-off-by: Kai Xia <503536682@qq.com>
Signed-off-by: Hans Zhang <18255117159@163.com>
---
 drivers/net/phy/phy_device.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c
index b39e945cebd7..9f88da87df97 100644
--- a/drivers/net/phy/phy_device.c
+++ b/drivers/net/phy/phy_device.c
@@ -10,6 +10,7 @@
 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 
 #include <linux/acpi.h>
+#include <linux/async.h>
 #include <linux/bitmap.h>
 #include <linux/delay.h>
 #include <linux/errno.h>
@@ -639,6 +640,9 @@ static int phy_request_driver_module(struct phy_device *dev, u32 phy_id)
 {
 	int ret;
 
+	if (current_is_async())
+		return -EPROBE_DEFER;
+
 	ret = request_module(MDIO_MODULE_PREFIX MDIO_ID_FMT,
 			     MDIO_ID_ARGS(phy_id));
 	/* We only check for failures in executing the usermode binary,

base-commit: 4dce97c6c265544342e716e62fd24bbb232590c5
-- 
2.43.0


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

* Re: [PATCH v2] net: phy: defer PHY probe when running in async probe context
  2026-11-25  8:09 [PATCH v2] net: phy: defer PHY probe when running in async probe context Hans Zhang
@ 2026-11-29  8:40 ` netdev-bot+sashiko
  0 siblings, 0 replies; 2+ messages in thread
From: netdev-bot+sashiko @ 2026-11-29  8:40 UTC (permalink / raw)
  To: 18255117159
  Cc: andrew, hkallweit1, davem, edumazet, kuba, pabeni, linux, netdev,
	503536682

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

Critical: 0 · High: 2 · Medium: 1 · 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 commit message assumes the deferred retry runs synchronously
  from the deferred probe workqueue.
- [High] get_phy_device(), and through it phy_device_create(), can now
  return -EPROBE_DEFER.
- [Medium] The current_is_async() check in phy_request_driver_module()
  (phy_device.c:643-644) runs before, and regardless of, the…
- [Low] The block comment in phy_device_create() (phy_device.c:720-729)
  still says "Request the appropriate module unconditionally".

--

net: phy: defer PHY probe when running in async probe context

This commit makes phy_request_driver_module() return -EPROBE_DEFER when
it is called from an async worker, instead of calling request_module()
synchronously. The idea is that the parent device probe is retried later
from the deferred probe workqueue, where request_module() can run
synchronously.

> diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c
> index b39e945cebd73..9f88da87df971 100644
> --- a/drivers/net/phy/phy_device.c
> +++ b/drivers/net/phy/phy_device.c

[ ... ]

> @@ -639,6 +640,9 @@ static int phy_request_driver_module(struct phy_device *dev, u32 phy_id)
>  {
>  	int ret;
>  
> +	if (current_is_async())
> +		return -EPROBE_DEFER;
> +

[Severity: High]
The commit message says the retry happens "from the deferred probe
workqueue, where request_module() can run synchronously". Does that hold
when the driver that registers the MDIO bus is itself allowed to probe
asynchronously?

The deferred retry path is:

deferred_probe_work_func()
  bus_probe_device_deferred()
    __bus_probe_device()
      device_initial_probe()
        __device_attach(dev, true)

In the synchronous pass, __device_attach_driver() skips drivers that
allow async probing:

drivers/base/dd.c:__device_attach_driver() {
    ...
	if (async_allowed)
		data->have_async = true;

	if (data->check_async && async_allowed != data->want_async)
		return 0;
    ...
}

__device_attach() then queues the probe through
async_schedule_dev_nocall(__device_attach_async_helper, dev). The retry
runs inside async_run_entry_fn again, so current_is_async() is true again
and -EPROBE_DEFER comes back again.

driver_allows_async_probing() returns true in several cases:

  PROBE_PREFER_ASYNCHRONOUS
  driver_async_probe=<name> or driver_async_probe=* on the command line
  the per-module async_probe parameter, or module.async_probe=1

In any of these setups, could a driver like r8169 stay deferred forever
and never bind? It scans PHYs through
devm_mdiobus_register()->mdiobus_scan_bus_c22()->get_phy_device().

With module.async_probe=1, do_init_module() already skips the
synchronization:

kernel/module/main.c:do_init_module() {
    ...
	if (!mod->async_probe_requested)
		async_synchronize_full();
    ...
}

So the old code could not deadlock in that configuration. Before this
patch those systems got a WARN_ON_ONCE() and a working NIC. With this
patch, wouldn't the NIC stay stuck in deferred probe for good?

[Severity: High]
With this change, get_phy_device() and phy_device_create() can return
-EPROBE_DEFER. The get_phy_device() kernel-doc only lists -ENODEV and
-EIO. Do all callers pass the new error up?

fixed_phy_register() in drivers/net/phy/fixed_phy.c rewrites it:

	phy = get_phy_device(fmb_mii_bus, phy_addr, false);
	if (IS_ERR(phy)) {
		fixed_phy_del(phy_addr);
		return ERR_PTR(-EINVAL);
	}

swphy_read_reg() returns 0 for MII_PHYSID1 and MII_PHYSID2. That makes
phy_id 0, so phy_device_create() always reaches
phy_request_driver_module(). In async context that now returns
-EPROBE_DEFER, and fixed_phy_register() turns it into -EINVAL.

Some MAC probes call of_phy_register_fixed_link() or fixed_phy_register()
from an async worker, for example fec, gianfar, bcmgenet, ravb, cpsw,
ftgmac100 and hibmcge. Wouldn't those now fail with -EINVAL and never be
retried?

Before this patch, __request_module() only did WARN_ON_ONCE(wait &&
current_is_async()), and the fixed PHY was still created.

Other callers collapse the error in a similar way:

  xgbe_phy_find_phy_device() maps it to -ENODEV
  hns_mac_register_phydev() maps it to -EIO
  xgene_enet_phy_register() maps it to NULL

It hasn't been checked whether these three can run in async context.

>  	ret = request_module(MDIO_MODULE_PREFIX MDIO_ID_FMT,
>  			     MDIO_ID_ARGS(phy_id));
>  	/* We only check for failures in executing the usermode binary,

[Severity: Medium]
Should the new check apply only when a module load can actually happen?

With CONFIG_MODULES=n, include/linux/kmod.h has:

static inline int request_module(const char *name, ...) { return -ENOSYS; }

The existing IS_ENABLED(CONFIG_MODULES) && ret < 0 test then makes
phy_request_driver_module() return 0. In that configuration nothing is
loaded, there is no warning, and async_synchronize_full() can't deadlock.

current_is_async() is always built in. So non-modular kernels now also
defer every PHY creation in async context, and they hit the permanent
deferral and fixed_phy -EINVAL cases above. The same is true when the
PHY driver is built in or already loaded.

The commit message only covers the modular case.

[Severity: Low]
This isn't a bug, but the block comment in phy_device_create() still
says:

	/* Request the appropriate module unconditionally; don't
	 * bother trying to do so only if it isn't already loaded,
	...
	 * enough for the driver to get loaded. With MDIO, the NIC
	 * driver will get bored and give up as soon as it finds that
	 * there's no driver _already_ loaded.
	 */

After this change the module request is skipped in async context, and
PHY creation returns -EPROBE_DEFER instead. Should the comment mention
the async restriction and the reliance on deferred probing?

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

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

end of thread, other threads:[~2026-11-29  8:40 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25  8:09 [PATCH v2] net: phy: defer PHY probe when running in async probe context Hans Zhang
2026-11-29  8:40 ` 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