public inbox for netdev@vger.kernel.org
 help / color / mirror / Atom feed
From: Wei Fang <wei.fang@nxp.com>
To: andrew@lunn.ch, hkallweit1@gmail.com, linux@armlinux.org.uk,
	davem@davemloft.net, edumazet@google.com, kuba@kernel.org,
	pabeni@redhat.com, florian.fainelli@broadcom.com,
	xiaolei.wang@windriver.com, maxime.chevallier@bootlin.com,
	quic_abchauha@quicinc.com, quic_sarohasa@quicinc.com
Cc: imx@lists.linux.dev, netdev@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH v2 net] net: phy: change devlink flag to AUTOREMOVE_SUPPLIER for non-SFP PHYs
Date: Mon,  2 Feb 2026 13:45:33 +0800	[thread overview]
Message-ID: <20260202054533.539883-1-wei.fang@nxp.com> (raw)

For the shared MDIO bus use case, multiple MACs will share the same MDIO
bus. Therefore, these MACs all depend on this MDIO bus. If this shared
MDIO bus is removed, all the PHY devices attached to this MDIO bus will
also be removed. Consequently, the MAC driver should not access the PHY
device, otherwise, it will lead to some potential crashes. Because the
corresponding phydev and the mii_bus have been freed, some pointers have
become invalid.

For example. Abhishek reported a crash issue that occurred if the MDIO
bus driver was removed first, followed by the MAC driver. The crash log
is as below.

Call trace:
 __list_del_entry_valid_or_report+0xa8/0xe0
 __device_link_del+0x40/0xf0
 device_link_put_kref+0xb4/0xc8
 device_link_del+0x38/0x58
 phy_detach+0x2c/0x170
 phy_disconnect+0x4c/0x70
 phylink_disconnect_phy+0x6c/0xc0 [phylink]
 stmmac_release+0x60/0x358 [stmmac]

Another example is the i.MX95-15x15 platform which has two ENETC ports.
When all the external PHYs are managed the EMDIO (the MDIO controller),
if the enetc driver is removed after the EMDIO driver. Users will see
the below crash log and the console is hanged.

Call trace:
 _phy_state_machine+0x230/0x36c (P)
 phy_stop+0x74/0x190
 phylink_stop+0x28/0xb8
 enetc_close+0x28/0x8c
 __dev_close_many+0xb4/0x1d8
 netif_close_many+0x8c/0x13c
 enetc4_pf_remove+0x2c/0x84
 pci_device_remove+0x44/0xe8

To address this issue, Sarosh Hasan tried to change the devlink flag to
DL_FLAG_AUTOREMOVE_SUPPLIER [1], so that the MAC driver will be removed
along with the PHY driver. However, the solution does not take into
account the hot-swappable PHY devices (SFP PHYs), so when the PHY device
is unplugged, the MAC driver will automatically be removed, which is not
the expected behavior. This issue should not exist for SFP PHYs, so based
on the Sarosh's patch, the flag is changed to DL_FLAG_AUTOREMOVE_SUPPLIER
for non-SFP PHYs.

Reported-by: Abhishek Chauhan (ABC) <quic_abchauha@quicinc.com>
Closes: https://lore.kernel.org/all/d696a426-40bb-4c1a-b42d-990fb690de5e@quicinc.com/
Link: https://lore.kernel.org/imx/20250703090041.23137-1-quic_sarohasa@quicinc.com/ # [1]
Fixes: bc66fa87d4fd ("net: phy: Add link between phy dev and mac dev")
Suggested-by: Maxime Chevallier <maxime.chevallier@bootlin.com>
Signed-off-by: Wei Fang <wei.fang@nxp.com>
---
v2:
1. Change the subject and update the commit message
2. Based on Maxime's suggestion, only set DL_FLAG_AUTOREMOVE_SUPPLIER
flag for non-SFP PHYs.
v1 link: https://lore.kernel.org/imx/20260126104409.1070403-1-wei.fang@nxp.com/
---
 drivers/net/phy/phy_device.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c
index 81984d4ebb7c..0494ab58ceaf 100644
--- a/drivers/net/phy/phy_device.c
+++ b/drivers/net/phy/phy_device.c
@@ -1771,9 +1771,17 @@ int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
 	 * another mac interface, so we should create a device link between
 	 * phy dev and mac dev.
 	 */
-	if (dev && phydev->mdio.bus->parent && dev->dev.parent != phydev->mdio.bus->parent)
-		phydev->devlink = device_link_add(dev->dev.parent, &phydev->mdio.dev,
-						  DL_FLAG_PM_RUNTIME | DL_FLAG_STATELESS);
+	if (dev && bus->parent && dev->dev.parent != bus->parent) {
+		if (phy_on_sfp(phydev))
+			phydev->devlink = device_link_add(dev->dev.parent,
+							  &phydev->mdio.dev,
+							  DL_FLAG_PM_RUNTIME |
+							  DL_FLAG_STATELESS);
+		else
+			device_link_add(dev->dev.parent, &phydev->mdio.dev,
+					DL_FLAG_PM_RUNTIME |
+					DL_FLAG_AUTOREMOVE_SUPPLIER);
+	}
 
 	return err;
 
-- 
2.34.1


             reply	other threads:[~2026-02-02  5:44 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-02  5:45 Wei Fang [this message]
2026-02-02 11:10 ` [PATCH v2 net] net: phy: change devlink flag to AUTOREMOVE_SUPPLIER for non-SFP PHYs Maxime Chevallier
2026-02-02 14:25   ` Russell King (Oracle)
2026-02-02 17:38     ` Maxime Chevallier
2026-02-02 18:00       ` Russell King (Oracle)
2026-02-02 18:37         ` Maxime Chevallier
2026-02-03  5:14           ` Wei Fang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260202054533.539883-1-wei.fang@nxp.com \
    --to=wei.fang@nxp.com \
    --cc=andrew@lunn.ch \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=florian.fainelli@broadcom.com \
    --cc=hkallweit1@gmail.com \
    --cc=imx@lists.linux.dev \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@armlinux.org.uk \
    --cc=maxime.chevallier@bootlin.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=quic_abchauha@quicinc.com \
    --cc=quic_sarohasa@quicinc.com \
    --cc=xiaolei.wang@windriver.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox