Re: [PATCH net-next 06/16] net: pcs: xpcs: Avoid creating dummy XPCS MDIO device

From: Russell King (Oracle)
Date: Tue Dec 12 2023 - 14:07:10 EST


On Tue, Dec 12, 2023 at 06:26:16PM +0300, Serge Semin wrote:
> I would have used in the first place if it was externally visible, but
> it's defined as static. Do you suggest to make it global or ...

That would be one option - I didn't make it visible when I introduced it
beacuse there were no users for it.

> > At some point, we should implement
> > mdiobus_get_mdiodev() which also deals with the refcount.
>
> ... create mdiobus_get_mdiodev() instead?
>
> * Note in the commit message I mentioned that having a getter would be
> * better than directly touching the mii_bus instance guts.

What I'm thinking is:

/**
* mdiobus_get_mdiodev() - get a mdiodev for the specified bus
* @bus: mii_bus to get mdio device from
* @addr: mdio address of mdio device
*
* Return the struct mdio_device attached to the MII bus @bus at MDIO
* address @addr. On success, the refcount on the device will be
* increased, which must be dropped using mdio_device_put(), and the
* mdio device returned. Otherwise, returns NULL.
*/
struct mdio_device *mdiobus_get_mdiodev(struct mii_bus *bus, int addr)
{
struct mdio_device *mdiodev;

mdiodev = mdiobus_find_device(bus, addr);
if (mdiodev)
get_device(&mdiodev->dev);
return mdiodev;
}
EXPORT_SYMBOL(mdiobus_get_mdiodev);

should do it, and will hold a reference on the mdiodev structure (which
won't be freed) and also on the mii_bus (since this device is a child
of the bus device, the parent can't be released until the child has
been, so struct mii_bus should at least stay around.)

What would help the "the bus driver has been unbound" situation is if
we took the mdio_lock on the bus, and then set the {read,write}{,_c45}
functions to dummy stubs when the bus is being unregistered which then
return e.g. -ENXIO. That will probably make unbinding/unloading all
MDIO bus drivers safe from kernel oops, although phylib will spit out
a non-useful backtrace if it tries an access. I don't think there's
much which can be done about that - I did propose a patch to change
that behaviour but apparently folk like having it!

It isn't perfect - it's racy, but then accessing mdio_map[] is
inherently racy due to no locking with mdiobus_.*register_device().
At least if we have everyone using a proper getter function rather
than directly fiddling with bus->mdio_map[]. We only have one driver
that accesses it directly at the moment (mscc_ptp):

dev = phydev->mdio.bus->mdio_map[vsc8531->ts_base_addr];
phydev = container_of(dev, struct phy_device, mdio);

return phydev->priv;

and that should really be using mdiobus_get_phy().

--
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTP is here! 80Mbps down 10Mbps up. Decent connectivity at last!