Messages in this thread | | | Date | Mon, 11 Feb 2019 15:31:59 +0100 | From | Thomas Petazzoni <> | Subject | Re: [PATCH] net: phy: mdio_bus: add missing device_del() in mdiobus_register() error handling |
| |
Hello Andrew,
On Wed, 16 Jan 2019 16:44:39 +0100 Andrew Lunn <andrew@lunn.ch> wrote:
> > On Wed, 16 Jan 2019 15:48:29 +0100, Andrew Lunn wrote: > > > > > Reviewed-by: Andrew Lunn <andrew@lunn.ch> > > > > > > However, i wounder if it makes sense to add a label before the > > > existing device_del() at the end of the function, and convert this, > > > and the case above into a goto? That might scale better, avoiding the > > > same issue in the future? > > > > That's another option indeed. > > > > Hmm, now that I looked at it, I think we should use device_unregister() > > instead. device_unregister() does both device_del() and put_device(). > > Hi Thomas > > device_unregister() does seem symmetrical with device_register() which > is what we are trying to undo.
Even if DaveM already merged my simple fix, I had a further look at whether we should be using device_unregister(), and in fact we should not, but not really for a good reason: because the mdio API is not very symmetrical.
The typical flow is:
probe() { bus = mdiobus_alloc(); if (!bus) return -ENOMEM;
ret = mdiobus_register(&bus); if (ret) { mdiobus_free(bus);
... }
remove() { mdiobus_unregister(); mdiobus_free(); }
mdiobus_alloc() only does memory allocation, i.e it has no side effects on the device model data structures.
mdiobus_register() does a device_register(). If it fails, it only cleans up with a device_del(), i.e it doesn't do the put_device() that it should do to fully "undo" its effect.
mdiobus_unregister() does a device_del(), i.e it also doesn't do the opposite of mdiobus_register(), which should be device_del() + put_device() (device_unregister() is a shortcut for both).
mdiobus_free() does the put_device()
So:
* mdiobus_alloc() / mdiobus_free() are not symmetrical in terms of their interaction with the device model data structures
* On error, mdiobus_register() leaves a non-zero reference count to the bus->dev structure, which will be freed up by mdiobus_free()
* mdiobus_unregister() leaves a non-zero reference count to the bus->dev structure, which will be freed up by mdiobus_free()
So, if we were to use device_unregister() in the error path of mdiobus_register() and in mdiobus_unregister(), it would break how mdiobus_free() works.
Best regards,
Thomas -- Thomas Petazzoni, CTO, Bootlin Embedded Linux and Kernel engineering https://bootlin.com
| |