[PATCH] net: mdio: mvusb: drop redundant device reference

Johan Hovold posted 1 patch 1 month ago
There is a newer version of this series
drivers/net/mdio/mdio-mvusb.c | 10 ++--------
1 file changed, 2 insertions(+), 8 deletions(-)
[PATCH] net: mdio: mvusb: drop redundant device reference
Posted by Johan Hovold 1 month ago
Driver core holds a reference to the USB interface and its parent USB
device while the interface is bound to a driver and there is no need to
take additional references unless the structures are needed after
disconnect.

Drop the redundant device reference to reduce cargo culting, make it
easier to spot drivers where an extra reference is needed, and reduce
the risk of memory leaks when drivers fail to release it.

Signed-off-by: Johan Hovold <johan@kernel.org>
---
 drivers/net/mdio/mdio-mvusb.c | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)

diff --git a/drivers/net/mdio/mdio-mvusb.c b/drivers/net/mdio/mdio-mvusb.c
index 554837c21e73..f9c16c731c73 100644
--- a/drivers/net/mdio/mdio-mvusb.c
+++ b/drivers/net/mdio/mdio-mvusb.c
@@ -75,7 +75,7 @@ static int mvusb_mdio_probe(struct usb_interface *interface,
 
 	mvusb = mdio->priv;
 	mvusb->mdio = mdio;
-	mvusb->udev = usb_get_dev(interface_to_usbdev(interface));
+	mvusb->udev = interface_to_usbdev(interface);
 
 	/* Reversed from USB PCAPs, no idea what these mean. */
 	mvusb->buf[MVUSB_CMD_PREAMBLE0] = cpu_to_le16(0xe800);
@@ -90,23 +90,17 @@ static int mvusb_mdio_probe(struct usb_interface *interface,
 	usb_set_intfdata(interface, mvusb);
 	ret = of_mdiobus_register(mdio, dev->of_node);
 	if (ret)
-		goto put_dev;
+		return ret;
 
 	return 0;
-
-put_dev:
-	usb_put_dev(mvusb->udev);
-	return ret;
 }
 
 static void mvusb_mdio_disconnect(struct usb_interface *interface)
 {
 	struct mvusb_mdio *mvusb = usb_get_intfdata(interface);
-	struct usb_device *udev = mvusb->udev;
 
 	mdiobus_unregister(mvusb->mdio);
 	usb_set_intfdata(interface, NULL);
-	usb_put_dev(udev);
 }
 
 static struct usb_driver mvusb_mdio_driver = {
-- 
2.52.0
Re: [PATCH] net: mdio: mvusb: drop redundant device reference
Posted by Jakub Kicinski 1 month ago
On Thu,  5 Mar 2026 11:47:22 +0100 Johan Hovold wrote:
>  	ret = of_mdiobus_register(mdio, dev->of_node);
>  	if (ret)
> -		goto put_dev;
> +		return ret;
>  
>  	return 0;
> -
> -put_dev:
> -	usb_put_dev(mvusb->udev);
> -	return ret;

better still:

	return of_mdiobus_register(mdio, dev->of_node);

and you can remove int ret; as well
Re: [PATCH] net: mdio: mvusb: drop redundant device reference
Posted by Johan Hovold 1 month ago
On Fri, Mar 06, 2026 at 07:19:22PM -0800, Jakub Kicinski wrote:
> On Thu,  5 Mar 2026 11:47:22 +0100 Johan Hovold wrote:
> >  	ret = of_mdiobus_register(mdio, dev->of_node);
> >  	if (ret)
> > -		goto put_dev;
> > +		return ret;
> >  
> >  	return 0;
> > -
> > -put_dev:
> > -	usb_put_dev(mvusb->udev);
> > -	return ret;
> 
> better still:
> 
> 	return of_mdiobus_register(mdio, dev->of_node);
> 
> and you can remove int ret; as well

I tend to prefer explicit error paths as combining the two at the end of
a function is a common source of errors where drivers fail to clean up
before returning.

But I'll drop ret in a v2.

Johan