[PATCHv4,net] net: asix: fix modprobe "sysfs: cannot create duplicate filename"

Message ID 20230314055410.3329480-1-grundler@chromium.org
State New
Headers
Series [PATCHv4,net] net: asix: fix modprobe "sysfs: cannot create duplicate filename" |

Commit Message

Grant Grundler March 14, 2023, 5:54 a.m. UTC
  "modprobe asix ; rmmod asix ; modprobe asix" fails with:
   sysfs: cannot create duplicate filename \
   	'/devices/virtual/mdio_bus/usb-003:004'

Issue was originally reported by Anton Lundin on 2022-06-22 (link below).

Chrome OS team hit the same issue in Feb, 2023 when trying to find
work arounds for other issues with AX88172 devices.

The use of devm_mdiobus_register() with usbnet devices results in the
MDIO data being associated with the USB device. When the asix driver
is unloaded, the USB device continues to exist and the corresponding
"mdiobus_unregister()" is NOT called until the USB device is unplugged
or unauthorized. So the next "modprobe asix" will fail because the MDIO
phy sysfs attributes still exist.

The 'easy' (from a design PoV) fix is to use the non-devm variants of
mdiobus_* functions and explicitly manage this use in the asix_bind
and asix_unbind function calls. I've not explored trying to fix usbnet
initialization so devm_* stuff will work.

Fixes: e532a096be0e5 ("net: usb: asix: ax88772: add phylib support")
Reported-by: Anton Lundin <glance@acc.umu.se>
Link: https://lore.kernel.org/netdev/20220623063649.GD23685@pengutronix.de/T/
Tested-by: Eizan Miyamoto <eizan@chromium.org>
Signed-off-by: Grant Grundler <grundler@chromium.org>

---
 drivers/net/usb/asix_devices.c | 33 ++++++++++++++++++++++++++++-----
 1 file changed, 28 insertions(+), 5 deletions(-)

V4: add mdio_unregister to ax88172_bind() error handling paths

V3: rebase against netdev/net.git
    remove "TEST" prefix in subject line
    added Link: tag for Reported-by tag

V2: moved mdiobus_get_phy() call back into ax88772_init_phy()
   (Lukas Wunner is entirely correct this patch is much easier
   to backport without this patch hunk.)
   Added "Fixes:" tag per request from Florian Fainelli
  

Comments

Michal Swiatkowski March 14, 2023, 1:22 p.m. UTC | #1
On Mon, Mar 13, 2023 at 10:54:10PM -0700, Grant Grundler wrote:
> "modprobe asix ; rmmod asix ; modprobe asix" fails with:
>    sysfs: cannot create duplicate filename \
>    	'/devices/virtual/mdio_bus/usb-003:004'
> 
> Issue was originally reported by Anton Lundin on 2022-06-22 (link below).
> 
> Chrome OS team hit the same issue in Feb, 2023 when trying to find
> work arounds for other issues with AX88172 devices.
> 
> The use of devm_mdiobus_register() with usbnet devices results in the
> MDIO data being associated with the USB device. When the asix driver
> is unloaded, the USB device continues to exist and the corresponding
> "mdiobus_unregister()" is NOT called until the USB device is unplugged
> or unauthorized. So the next "modprobe asix" will fail because the MDIO
> phy sysfs attributes still exist.
> 
> The 'easy' (from a design PoV) fix is to use the non-devm variants of
> mdiobus_* functions and explicitly manage this use in the asix_bind
> and asix_unbind function calls. I've not explored trying to fix usbnet
> initialization so devm_* stuff will work.
> 
> Fixes: e532a096be0e5 ("net: usb: asix: ax88772: add phylib support")
> Reported-by: Anton Lundin <glance@acc.umu.se>
> Link: https://lore.kernel.org/netdev/20220623063649.GD23685@pengutronix.de/T/
> Tested-by: Eizan Miyamoto <eizan@chromium.org>
> Signed-off-by: Grant Grundler <grundler@chromium.org>
> 
> ---
>  drivers/net/usb/asix_devices.c | 33 ++++++++++++++++++++++++++++-----
>  1 file changed, 28 insertions(+), 5 deletions(-)
> 
> V4: add mdio_unregister to ax88172_bind() error handling paths
> 
> V3: rebase against netdev/net.git
>     remove "TEST" prefix in subject line
>     added Link: tag for Reported-by tag
> 
> V2: moved mdiobus_get_phy() call back into ax88772_init_phy()
>    (Lukas Wunner is entirely correct this patch is much easier
>    to backport without this patch hunk.)
>    Added "Fixes:" tag per request from Florian Fainelli
> 
> diff --git a/drivers/net/usb/asix_devices.c b/drivers/net/usb/asix_devices.c
> index 743cbf5d662c..b758010bab36 100644
> --- a/drivers/net/usb/asix_devices.c
> +++ b/drivers/net/usb/asix_devices.c
> @@ -666,8 +666,9 @@ static int asix_resume(struct usb_interface *intf)
>  static int ax88772_init_mdio(struct usbnet *dev)
>  {
>  	struct asix_common_private *priv = dev->driver_priv;
> +	int ret;
>  
> -	priv->mdio = devm_mdiobus_alloc(&dev->udev->dev);

Hi,

Patch looks good, one question, are the devm_mdiobus_alloc() and
devm_mdiobus_register functions used anywhere after removing? Maybe it
is worth to remove also the implementation if it isn't used.

Otherwise
Reviewed-by: Michal Swiatkowski <michal.swiatkowski@linux.intel.com>

> +	priv->mdio = mdiobus_alloc();
>  	if (!priv->mdio)
>  		return -ENOMEM;
>  
> @@ -679,7 +680,20 @@ static int ax88772_init_mdio(struct usbnet *dev)
>  	snprintf(priv->mdio->id, MII_BUS_ID_SIZE, "usb-%03d:%03d",
>  		 dev->udev->bus->busnum, dev->udev->devnum);
>  
> -	return devm_mdiobus_register(&dev->udev->dev, priv->mdio);
> +	ret = mdiobus_register(priv->mdio);
> +	if (ret) {
> +		netdev_err(dev->net, "Could not register MDIO bus (err %d)\n", ret);
> +		mdiobus_free(priv->mdio);
> +		priv->mdio = NULL;
> +	}
> +
> +	return ret;
> +}
> +
> +static void ax88772_mdio_unregister(struct asix_common_private *priv)
> +{
> +	mdiobus_unregister(priv->mdio);
> +	mdiobus_free(priv->mdio);
>  }
>  
>  static int ax88772_init_phy(struct usbnet *dev)
> @@ -690,6 +704,7 @@ static int ax88772_init_phy(struct usbnet *dev)
>  	priv->phydev = mdiobus_get_phy(priv->mdio, priv->phy_addr);
>  	if (!priv->phydev) {
>  		netdev_err(dev->net, "Could not find PHY\n");
> +		ax88772_mdio_unregister(priv);
>  		return -ENODEV;
>  	}
>  
> @@ -896,16 +911,23 @@ static int ax88772_bind(struct usbnet *dev, struct usb_interface *intf)
>  
>  	ret = ax88772_init_mdio(dev);
>  	if (ret)
> -		return ret;
> +		goto mdio_err;
>  
>  	ret = ax88772_phylink_setup(dev);
>  	if (ret)
> -		return ret;
> +		goto phylink_err;
>  
>  	ret = ax88772_init_phy(dev);
>  	if (ret)
> -		phylink_destroy(priv->phylink);
> +		goto initphy_err;
>  
> +	return 0;
> +
> +initphy_err:
> +	phylink_destroy(priv->phylink);
> +phylink_err:
> +	ax88772_mdio_unregister(priv);
> +mdio_err:
>  	return ret;
>  }
>  
> @@ -926,6 +948,7 @@ static void ax88772_unbind(struct usbnet *dev, struct usb_interface *intf)
>  	phylink_disconnect_phy(priv->phylink);
>  	rtnl_unlock();
>  	phylink_destroy(priv->phylink);
> +	ax88772_mdio_unregister(priv);
>  	asix_rx_fixup_common_free(dev->driver_priv);
>  }
>  
> -- 
> 2.40.0.rc1.284.g88254d51c5-goog
>
  
Jakub Kicinski March 16, 2023, 5:08 a.m. UTC | #2
On Mon, 13 Mar 2023 22:54:10 -0700 Grant Grundler wrote:
> @@ -690,6 +704,7 @@ static int ax88772_init_phy(struct usbnet *dev)
>  	priv->phydev = mdiobus_get_phy(priv->mdio, priv->phy_addr);
>  	if (!priv->phydev) {
>  		netdev_err(dev->net, "Could not find PHY\n");
> +		ax88772_mdio_unregister(priv);

this line needs to go now..

>  		return -ENODEV;

.. since if we return error here ..

>  	}
>  
> @@ -896,16 +911,23 @@ static int ax88772_bind(struct usbnet *dev, struct usb_interface *intf)
>  
>  	ret = ax88772_init_mdio(dev);
>  	if (ret)
> -		return ret;
> +		goto mdio_err;
>  
>  	ret = ax88772_phylink_setup(dev);
>  	if (ret)
> -		return ret;
> +		goto phylink_err;
>  
>  	ret = ax88772_init_phy(dev);

.. it will pop out here ..

>  	if (ret)
> -		phylink_destroy(priv->phylink);
> +		goto initphy_err;
>  
> +	return 0;
> +
> +initphy_err:
> +	phylink_destroy(priv->phylink);
> +phylink_err:
> +	ax88772_mdio_unregister(priv);

.. and then call ax88772_mdio_unregister() a second time.

> +mdio_err:
>  	return ret;
>  }
  
Grant Grundler March 17, 2023, 5:58 p.m. UTC | #3
On Wed, Mar 15, 2023 at 10:08 PM Jakub Kicinski <kuba@kernel.org> wrote:
>
> On Mon, 13 Mar 2023 22:54:10 -0700 Grant Grundler wrote:
> > @@ -690,6 +704,7 @@ static int ax88772_init_phy(struct usbnet *dev)
> >       priv->phydev = mdiobus_get_phy(priv->mdio, priv->phy_addr);
> >       if (!priv->phydev) {
> >               netdev_err(dev->net, "Could not find PHY\n");
> > +             ax88772_mdio_unregister(priv);
>
> this line needs to go now..
>
> >               return -ENODEV;
>
> .. since if we return error here ..
>
> >       }
> >
> > @@ -896,16 +911,23 @@ static int ax88772_bind(struct usbnet *dev, struct usb_interface *intf)
> >
> >       ret = ax88772_init_mdio(dev);
> >       if (ret)
> > -             return ret;
> > +             goto mdio_err;
> >
> >       ret = ax88772_phylink_setup(dev);
> >       if (ret)
> > -             return ret;
> > +             goto phylink_err;
> >
> >       ret = ax88772_init_phy(dev);
>
> .. it will pop out here ..
>
> >       if (ret)
> > -             phylink_destroy(priv->phylink);
> > +             goto initphy_err;
> >
> > +     return 0;
> > +
> > +initphy_err:
> > +     phylink_destroy(priv->phylink);
> > +phylink_err:
> > +     ax88772_mdio_unregister(priv);
>
> .. and then call ax88772_mdio_unregister() a second time.

Doh! Yes - good catch. Let me fix that.

cheers,
grant

>
> > +mdio_err:
> >       return ret;
> >  }
  

Patch

diff --git a/drivers/net/usb/asix_devices.c b/drivers/net/usb/asix_devices.c
index 743cbf5d662c..b758010bab36 100644
--- a/drivers/net/usb/asix_devices.c
+++ b/drivers/net/usb/asix_devices.c
@@ -666,8 +666,9 @@  static int asix_resume(struct usb_interface *intf)
 static int ax88772_init_mdio(struct usbnet *dev)
 {
 	struct asix_common_private *priv = dev->driver_priv;
+	int ret;
 
-	priv->mdio = devm_mdiobus_alloc(&dev->udev->dev);
+	priv->mdio = mdiobus_alloc();
 	if (!priv->mdio)
 		return -ENOMEM;
 
@@ -679,7 +680,20 @@  static int ax88772_init_mdio(struct usbnet *dev)
 	snprintf(priv->mdio->id, MII_BUS_ID_SIZE, "usb-%03d:%03d",
 		 dev->udev->bus->busnum, dev->udev->devnum);
 
-	return devm_mdiobus_register(&dev->udev->dev, priv->mdio);
+	ret = mdiobus_register(priv->mdio);
+	if (ret) {
+		netdev_err(dev->net, "Could not register MDIO bus (err %d)\n", ret);
+		mdiobus_free(priv->mdio);
+		priv->mdio = NULL;
+	}
+
+	return ret;
+}
+
+static void ax88772_mdio_unregister(struct asix_common_private *priv)
+{
+	mdiobus_unregister(priv->mdio);
+	mdiobus_free(priv->mdio);
 }
 
 static int ax88772_init_phy(struct usbnet *dev)
@@ -690,6 +704,7 @@  static int ax88772_init_phy(struct usbnet *dev)
 	priv->phydev = mdiobus_get_phy(priv->mdio, priv->phy_addr);
 	if (!priv->phydev) {
 		netdev_err(dev->net, "Could not find PHY\n");
+		ax88772_mdio_unregister(priv);
 		return -ENODEV;
 	}
 
@@ -896,16 +911,23 @@  static int ax88772_bind(struct usbnet *dev, struct usb_interface *intf)
 
 	ret = ax88772_init_mdio(dev);
 	if (ret)
-		return ret;
+		goto mdio_err;
 
 	ret = ax88772_phylink_setup(dev);
 	if (ret)
-		return ret;
+		goto phylink_err;
 
 	ret = ax88772_init_phy(dev);
 	if (ret)
-		phylink_destroy(priv->phylink);
+		goto initphy_err;
 
+	return 0;
+
+initphy_err:
+	phylink_destroy(priv->phylink);
+phylink_err:
+	ax88772_mdio_unregister(priv);
+mdio_err:
 	return ret;
 }
 
@@ -926,6 +948,7 @@  static void ax88772_unbind(struct usbnet *dev, struct usb_interface *intf)
 	phylink_disconnect_phy(priv->phylink);
 	rtnl_unlock();
 	phylink_destroy(priv->phylink);
+	ax88772_mdio_unregister(priv);
 	asix_rx_fixup_common_free(dev->driver_priv);
 }