Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

net: asix: fix modprobe "sysfs: cannot create duplicate filename"

"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>
Link: https://lore.kernel.org/r/20230321170539.732147-1-grundler@chromium.org
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

authored by

Grant Grundler and committed by
Jakub Kicinski
8eac0095 68c3e4fc

+27 -5
+27 -5
drivers/net/usb/asix_devices.c
··· 666 666 static int ax88772_init_mdio(struct usbnet *dev) 667 667 { 668 668 struct asix_common_private *priv = dev->driver_priv; 669 + int ret; 669 670 670 - priv->mdio = devm_mdiobus_alloc(&dev->udev->dev); 671 + priv->mdio = mdiobus_alloc(); 671 672 if (!priv->mdio) 672 673 return -ENOMEM; 673 674 ··· 680 679 snprintf(priv->mdio->id, MII_BUS_ID_SIZE, "usb-%03d:%03d", 681 680 dev->udev->bus->busnum, dev->udev->devnum); 682 681 683 - return devm_mdiobus_register(&dev->udev->dev, priv->mdio); 682 + ret = mdiobus_register(priv->mdio); 683 + if (ret) { 684 + netdev_err(dev->net, "Could not register MDIO bus (err %d)\n", ret); 685 + mdiobus_free(priv->mdio); 686 + priv->mdio = NULL; 687 + } 688 + 689 + return ret; 690 + } 691 + 692 + static void ax88772_mdio_unregister(struct asix_common_private *priv) 693 + { 694 + mdiobus_unregister(priv->mdio); 695 + mdiobus_free(priv->mdio); 684 696 } 685 697 686 698 static int ax88772_init_phy(struct usbnet *dev) ··· 910 896 911 897 ret = ax88772_init_mdio(dev); 912 898 if (ret) 913 - return ret; 899 + goto mdio_err; 914 900 915 901 ret = ax88772_phylink_setup(dev); 916 902 if (ret) 917 - return ret; 903 + goto phylink_err; 918 904 919 905 ret = ax88772_init_phy(dev); 920 906 if (ret) 921 - phylink_destroy(priv->phylink); 907 + goto initphy_err; 922 908 909 + return 0; 910 + 911 + initphy_err: 912 + phylink_destroy(priv->phylink); 913 + phylink_err: 914 + ax88772_mdio_unregister(priv); 915 + mdio_err: 923 916 return ret; 924 917 } 925 918 ··· 947 926 phylink_disconnect_phy(priv->phylink); 948 927 rtnl_unlock(); 949 928 phylink_destroy(priv->phylink); 929 + ax88772_mdio_unregister(priv); 950 930 asix_rx_fixup_common_free(dev->driver_priv); 951 931 } 952 932