Skip to contentSkip to conversation

net: phy: allow phy_detach() before netdev registration

PATCH In review Chenguang Zhao 2 participants 2 messages Sep 18

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 net: phy: allow phy_detach() before netdev registration

From: Chenguang Zhao <[email protected]>

phylink_connect_phy() may be called from probe without RTNL while the net_device is still NETREG_UNINITIALIZED. If phylink_bringup_phy() fails, phy_detach() uses rtnl_dereference(dev->hwprov) and lockdep reports suspicious RCU usage.

hwprov cannot have been assigned before register_netdevice(). Relax the RCU check for that state only; registered devices still require RTNL.

Fixes 35f7cad1743e ("net: Add the possibility to support a selected hwtstamp in netdevice")
Reported-by [email protected]

---
v2:
phy_attach() does not set dev->hwprov. It is only written by ethtool TSCONFIG after register_netdevice(). when calling phy_detach(), we need to check whether hwprov is NULL to clean up dangling pointers.

This patch uses NETREG_UNINITIALIZED as a flag. When PHY registration fails, TSCONFIG remains unset; hence dev->hwprov should not be accessed.

v1:
https://lore.kernel.org/all/[email protected]/

 drivers/net/phy/phy_device.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
drivers/net/phy/phy_device.c +7 -5
index 94b2e85e00a3..a3a7d5c75970 100644
@@ -1941,11 +1941,13 @@ void phy_detach(struct phy_device *phydev)
 		/* hwprov may technically be protected by ops lock but
 		 * not for devices with a phydev, see phy_link_topo_add_phy()
 		 */
-		hwprov = rtnl_dereference(dev->hwprov);
-		/* Disable timestamp if it is the one selected */
-		if (hwprov && hwprov->phydev == phydev) {
-			rcu_assign_pointer(dev->hwprov, NULL);
-			kfree_rcu(hwprov, rcu_head);
+		if (dev->reg_state != NETREG_UNINITIALIZED) {
+			hwprov = rtnl_dereference(dev->hwprov);
+			/* Disable timestamp if it is the one selected */
+			if (hwprov && hwprov->phydev == phydev) {
+				rcu_assign_pointer(dev->hwprov, NULL);
+				kfree_rcu(hwprov, rcu_head);
+			}
 		}
 
 		phydev->attached_dev->phydev = NULL;
Signature · 1 line
2.25.1
maintainer
#1 lore raw
Quoted 2 lines

phy_attach() does not set dev->hwprov. It is only written by ethtool TSCONFIG after register_netdevice().

Please could you point me at this code.

Andrew