All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] net: mdiobus: fix device unregistering in mdiobus_register
@ 2020-08-26  9:51 Sascha Hauer
  2020-08-26 16:26 ` Heiner Kallweit
  0 siblings, 1 reply; 3+ messages in thread
From: Sascha Hauer @ 2020-08-26  9:51 UTC (permalink / raw)
  To: netdev
  Cc: Andrew Lunn, Florian Fainelli, Heiner Kallweit, kernel, Sascha Hauer

__mdiobus_register() can fail between calling device_register() and
setting bus->state to MDIOBUS_REGISTERED. When this happens the caller
will call mdiobus_free() which then frees the mdio bus structure. This
is not allowed as the embedded struct device is already registered, thus
must be freed dropping the reference count using put_device(). To
accomplish this set bus->state to MDIOBUS_UNREGISTERED after having
registered the device. With this mdiobus_free() correctly calls
put_device() instead of freeing the mdio bus structure directly.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 drivers/net/phy/mdio_bus.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/net/phy/mdio_bus.c b/drivers/net/phy/mdio_bus.c
index 0af20faad69d..85cbaab4a591 100644
--- a/drivers/net/phy/mdio_bus.c
+++ b/drivers/net/phy/mdio_bus.c
@@ -540,6 +540,8 @@ int __mdiobus_register(struct mii_bus *bus, struct module *owner)
 		return -EINVAL;
 	}
 
+	bus->state = MDIOBUS_UNREGISTERED;
+
 	mutex_init(&bus->mdio_lock);
 	mutex_init(&bus->shared_lock);
 
-- 
2.28.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] net: mdiobus: fix device unregistering in mdiobus_register
  2020-08-26  9:51 [PATCH] net: mdiobus: fix device unregistering in mdiobus_register Sascha Hauer
@ 2020-08-26 16:26 ` Heiner Kallweit
  2020-08-27  6:56   ` Sascha Hauer
  0 siblings, 1 reply; 3+ messages in thread
From: Heiner Kallweit @ 2020-08-26 16:26 UTC (permalink / raw)
  To: Sascha Hauer, netdev; +Cc: Andrew Lunn, Florian Fainelli, kernel

On 26.08.2020 11:51, Sascha Hauer wrote:
> __mdiobus_register() can fail between calling device_register() and
> setting bus->state to MDIOBUS_REGISTERED. When this happens the caller
> will call mdiobus_free() which then frees the mdio bus structure. This
> is not allowed as the embedded struct device is already registered, thus
> must be freed dropping the reference count using put_device(). To
> accomplish this set bus->state to MDIOBUS_UNREGISTERED after having
> registered the device. With this mdiobus_free() correctly calls
> put_device() instead of freeing the mdio bus structure directly.
> 
> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
> ---
>  drivers/net/phy/mdio_bus.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/drivers/net/phy/mdio_bus.c b/drivers/net/phy/mdio_bus.c
> index 0af20faad69d..85cbaab4a591 100644
> --- a/drivers/net/phy/mdio_bus.c
> +++ b/drivers/net/phy/mdio_bus.c
> @@ -540,6 +540,8 @@ int __mdiobus_register(struct mii_bus *bus, struct module *owner)
>  		return -EINVAL;
>  	}
>  
> +	bus->state = MDIOBUS_UNREGISTERED;
> +
>  	mutex_init(&bus->mdio_lock);
>  	mutex_init(&bus->shared_lock);
>  
> 
I see the point. If we bail out after having called device_register()
then put_device() has to be called. This however isn't done by
mdiobus_free() if state is MDIOBUS_ALLOCATED. So I think the idea is
right. However we have to call put_device() even if device_register()
fails, therefore setting state to MDIOBUS_UNREGISTERED should be
moved to before calling device_register().


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] net: mdiobus: fix device unregistering in mdiobus_register
  2020-08-26 16:26 ` Heiner Kallweit
@ 2020-08-27  6:56   ` Sascha Hauer
  0 siblings, 0 replies; 3+ messages in thread
From: Sascha Hauer @ 2020-08-27  6:56 UTC (permalink / raw)
  To: Heiner Kallweit; +Cc: netdev, Andrew Lunn, Florian Fainelli, kernel

On Wed, Aug 26, 2020 at 06:26:36PM +0200, Heiner Kallweit wrote:
> On 26.08.2020 11:51, Sascha Hauer wrote:
> > __mdiobus_register() can fail between calling device_register() and
> > setting bus->state to MDIOBUS_REGISTERED. When this happens the caller
> > will call mdiobus_free() which then frees the mdio bus structure. This
> > is not allowed as the embedded struct device is already registered, thus
> > must be freed dropping the reference count using put_device(). To
> > accomplish this set bus->state to MDIOBUS_UNREGISTERED after having
> > registered the device. With this mdiobus_free() correctly calls
> > put_device() instead of freeing the mdio bus structure directly.
> > 
> > Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
> > ---
> >  drivers/net/phy/mdio_bus.c | 2 ++
> >  1 file changed, 2 insertions(+)
> > 
> > diff --git a/drivers/net/phy/mdio_bus.c b/drivers/net/phy/mdio_bus.c
> > index 0af20faad69d..85cbaab4a591 100644
> > --- a/drivers/net/phy/mdio_bus.c
> > +++ b/drivers/net/phy/mdio_bus.c
> > @@ -540,6 +540,8 @@ int __mdiobus_register(struct mii_bus *bus, struct module *owner)
> >  		return -EINVAL;
> >  	}
> >  
> > +	bus->state = MDIOBUS_UNREGISTERED;
> > +
> >  	mutex_init(&bus->mdio_lock);
> >  	mutex_init(&bus->shared_lock);
> >  
> > 
> I see the point. If we bail out after having called device_register()
> then put_device() has to be called. This however isn't done by
> mdiobus_free() if state is MDIOBUS_ALLOCATED. So I think the idea is
> right. However we have to call put_device() even if device_register()
> fails, therefore setting state to MDIOBUS_UNREGISTERED should be
> moved to before calling device_register().

You're right, the comment above device_register clearly states:

/*
 * NOTE: _Never_ directly free @dev after calling this function, even
 * if it returned an error! Always use put_device() to give up the
 * reference initialized in this function instead.
 */

And I read this just yesterday while preparing this patch.

Will send a v2.

Sascha

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-08-27  6:56 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-26  9:51 [PATCH] net: mdiobus: fix device unregistering in mdiobus_register Sascha Hauer
2020-08-26 16:26 ` Heiner Kallweit
2020-08-27  6:56   ` Sascha Hauer

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.