๐Ÿš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: linux.kernel.netdev
2 messages
2 total messages Started by javen Wed, 25 Nov 2026 09:02
[PATCH net-next v4 1/3] r8169: hold MDIO bus lock while loading firmware
#22405
Author: javen
Date: Wed, 25 Nov 2026 09:02
40 lines
10298 bytes
From: Javen Xu <javen_xu@realsil.com.cn>

rtl_fw_write_firmware() accesses PHY registers directly, bypassing
phylib. Hold MDIO bus lock meanwhile, so other phylib users cannot
interleave their accesses with the firmware sequence.

Signed-off-by: Javen Xu <javen_xu@realsil.com.cn>
---
Changes in v3:
 - New patch

Changes in v4:
 - Skip the bus lock on PHY-less RTL8116af
 - Restore tp->ocp_base before releasing the bus lock
---
 drivers/net/ethernet/realtek/r8169_main.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/drivers/net/ethernet/realtek/r8169_main.c b/drivers/net/ethernet/realtek/r8169_main.c
index f151ab681393..b289ff6c3064 100644
--- a/drivers/net/ethernet/realtek/r8169_main.c
+++ b/drivers/net/ethernet/realtek/r8169_main.c
@@ -2686,9 +2686,14 @@ void r8169_apply_firmware(struct rtl8169_private *tp)
 
 	/* TODO: release firmware if rtl_fw_write_firmware signals failure. */
 	if (tp->rtl_fw) {
+		/* Keep phylib users (hwmon, PTP) off the bus meanwhile. */
+		if (tp->phydev)
+			phy_lock_mdio_bus(tp->phydev);
 		rtl_fw_write_firmware(tp, tp->rtl_fw);
 		/* At least one firmware doesn't reset tp->ocp_base. */
 		tp->ocp_base = OCP_STD_PHY_BASE;
+		if (tp->phydev)
+			phy_unlock_mdio_bus(tp->phydev);
 
 		/* PHY soft reset may still be in progress */
 		if (tp->phydev)
-- 
2.43.0
Re: [PATCH net-next v4 1/3] r8169: hold MDIO bus lock while loading firmware
#24111
Author: netdev-bot+sashiko
Date: Sun, 29 Nov 2026 09:50
89 lines
11145 bytes
Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] The commit message presents this patch as groundwork for the PTP
  patches later in the series.

--

r8169: hold MDIO bus lock while loading firmware

This patch takes the MDIO bus lock around rtl_fw_write_firmware() in
r8169_apply_firmware(). Other phylib users then cannot interleave their
PHY accesses with the firmware sequence. The lock is skipped when there
is no phydev, and tp->ocp_base is restored before the lock is dropped.

> diff --git a/drivers/net/ethernet/realtek/r8169_main.c b/drivers/net/ethernet/realtek/r8169_main.c
> index f151ab6813932..b289ff6c30645 100644
> --- a/drivers/net/ethernet/realtek/r8169_main.c
> +++ b/drivers/net/ethernet/realtek/r8169_main.c
> @@ -2686,9 +2686,14 @@ void r8169_apply_firmware(struct rtl8169_private *tp)
>  
>  	/* TODO: release firmware if rtl_fw_write_firmware signals failure. */
>  	if (tp->rtl_fw) {
> +		/* Keep phylib users (hwmon, PTP) off the bus meanwhile. */
> +		if (tp->phydev)
> +			phy_lock_mdio_bus(tp->phydev);
>  		rtl_fw_write_firmware(tp, tp->rtl_fw);

[Severity: Low]
The commit message doesn't say so, but does this also fix a race that
mainline users can already hit?

Since commit 33700ca45b7d ("net: phy: realtek: add hwmon support for temp
sensor on RTL822x"), rtl822x_probe() registers a hwmon device for the
internal NBASE-T PHY. Any user can read its sysfs attributes. A
temperature read takes only the MDIO bus lock:

rtl822x_hwmon_read()
  phy_read_mmd(phydev, MDIO_MMD_VEND2, RTL822X_VND2_TSRR)   <- bus lock
    rtl822x_read_mmd()
      rtlgen_read_vend2()
        __mdiobus_c45_read()
          r8169_mdio_read_reg_c45()
            r8168_phy_ocp_read()

r8168_phy_ocp_read() writes the GPHY_OCP command register and polls it,
and it takes no driver lock of its own:

	RTL_W32(tp, GPHY_OCP, reg << 15);

	return rtl_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
		(RTL_R32(tp, GPHY_OCP) & 0xffff) : -ETIMEDOUT;

Before this patch, rtl_fw_write_firmware() reached the same GPHY_OCP
sequence without the bus lock:

rtl_readphy()/rtl_writephy()
  r8168g_mdio_read()/r8168g_mdio_write()
    r8168_phy_ocp_read()/r8168_phy_ocp_write()

Firmware loads during open, resume or reset:

rtl8169_init_phy()->r8169_hw_phy_config()->r8169_apply_firmware()

If a hwmon read runs during a firmware load, can the two command/poll
sequences overlap? A firmware PHY write could then be lost or go to the
wrong register.

Paged C22 accesses that write register 0x1f also change tp->ocp_base,
and the firmware sequence depends on that value.

If so, could the commit message describe this race and add a Fixes: tag
so that stable kernels get the fix? For example:

Fixes: 33700ca45b7d ("net: phy: realtek: add hwmon support for temp sensor on RTL822x")

>  		/* At least one firmware doesn't reset tp->ocp_base. */
>  		tp->ocp_base = OCP_STD_PHY_BASE;
> +		if (tp->phydev)
> +			phy_unlock_mdio_bus(tp->phydev);

[ ... ]

-- 
Sashiko AI review ยท https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261125090217.1198-1-javen_xu%40realsil.com.cn
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads