Skip to contentSkip to conversation

wifi: drop redundant USB device references

PATCH In review Johan Hovold 5 participants 24 messages Mar 5 – Mar 6

Subsystems (11)
lore
Share & tools
lore
#0 lore raw

cover wifi: drop redundant USB device references

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 redundant device references 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 them.

Note that I sent an ath9k patch separately here:

	https://lore.kernel.org/all/[email protected]/

but I guess these can all be picked up by Johannes.

Johan

Johan Hovold (13):
wifi: at76c50x: drop redundant device reference
wifi: libertas: drop redundant device reference
wifi: libertas_tf: drop redundant device reference
wifi: mwifiex: drop redundant device reference
wifi: mt76: drop redundant device reference
wifi: mt76x0u: drop redundant device reference
wifi: mt76x2u: drop redundant device reference
wifi: mt76: mt792xu: drop redundant device reference
wifi: mt7601u: drop redundant device reference
wifi: rt2x00: drop redundant device reference
wifi: rtl818x: drop redundant device reference
wifi: rtl8xxxu: drop redundant device reference
wifi: rtlwifi: usb: drop redundant device reference

 drivers/net/wireless/atmel/at76c50x-usb.c          | 12 ++++--------
 drivers/net/wireless/marvell/libertas/if_usb.c     |  3 ---
 drivers/net/wireless/marvell/libertas_tf/if_usb.c  |  2 --
 drivers/net/wireless/marvell/mwifiex/usb.c         |  4 ----
 drivers/net/wireless/mediatek/mt76/mt7615/usb.c    |  3 ---
 drivers/net/wireless/mediatek/mt76/mt76x0/usb.c    |  3 ---
 drivers/net/wireless/mediatek/mt76/mt76x2/usb.c    |  4 ----
 drivers/net/wireless/mediatek/mt76/mt7921/usb.c    |  2 --
 drivers/net/wireless/mediatek/mt76/mt7925/usb.c    |  2 --
 drivers/net/wireless/mediatek/mt76/mt792x_usb.c    |  1 -
 drivers/net/wireless/mediatek/mt7601u/usb.c        |  3 ---
 drivers/net/wireless/ralink/rt2x00/rt2x00usb.c     | 12 +-----------
 drivers/net/wireless/realtek/rtl818x/rtl8187/dev.c |  4 ----
 drivers/net/wireless/realtek/rtl8xxxu/core.c       | 11 +++--------
 drivers/net/wireless/realtek/rtlwifi/usb.c         |  4 ----
 15 files changed, 8 insertions(+), 62 deletions(-)
Signature · 1 line
2.52.0
#1 lore raw

PATCH 1/13 wifi: at76c50x: drop redundant device reference

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.

---
 drivers/net/wireless/atmel/at76c50x-usb.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
drivers/net/wireless/atmel/at76c50x-usb.c +4 -8
index 6445332801a4..44b04ea3cc8b 100644
@@ -2440,13 +2440,11 @@ static int at76_probe(struct usb_interface *interface,
 	struct mib_fw_version *fwv;
 	int board_type = (int)id->driver_info;
 
-	udev = usb_get_dev(interface_to_usbdev(interface));
+	udev = interface_to_usbdev(interface);
 
 	fwv = kmalloc_obj(*fwv);
-	if (!fwv) {
-		ret = -ENOMEM;
-		goto exit;
-	}
+	if (!fwv)
+		return -ENOMEM;
 
 	/* Load firmware into kernel memory */
 	fwe = at76_load_firmware(udev, board_type);
@@ -2534,8 +2532,7 @@ static int at76_probe(struct usb_interface *interface,
 
 exit:
 	kfree(fwv);
-	if (ret < 0)
-		usb_put_dev(udev);
+
 	return ret;
 }
 
@@ -2552,7 +2549,6 @@ static void at76_disconnect(struct usb_interface *interface)
 
 	wiphy_info(priv->hw->wiphy, "disconnecting\n");
 	at76_delete_device(priv);
-	usb_put_dev(interface_to_usbdev(interface));
 	dev_info(&interface->dev, "disconnected\n");
 }
 
Signature · 1 line
2.52.0
#2 lore raw

PATCH 2/13 wifi: libertas: drop redundant device reference

In reply to Johan Hovold

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.

---
 drivers/net/wireless/marvell/libertas/if_usb.c | 3 ---
 1 file changed, 3 deletions(-)
drivers/net/wireless/marvell/libertas/if_usb.c +0 -3
index 8a6bf1365cfa..05fcf9cc28fa 100644
@@ -276,7 +276,6 @@ static int if_usb_probe(struct usb_interface *intf,
 
 	cardp->boot2_version = udev->descriptor.bcdDevice;
 
-	usb_get_dev(udev);
 	usb_set_intfdata(intf, cardp);
 
 	r = lbs_get_firmware_async(priv, &udev->dev, cardp->model,
@@ -287,7 +286,6 @@ static int if_usb_probe(struct usb_interface *intf,
 	return 0;
 
 err_get_fw:
-	usb_put_dev(udev);
 	lbs_remove_card(priv);
 err_add_card:
 	if_usb_reset_device(cardp);
@@ -321,7 +319,6 @@ static void if_usb_disconnect(struct usb_interface *intf)
 	kfree(cardp);
 
 	usb_set_intfdata(intf, NULL);
-	usb_put_dev(interface_to_usbdev(intf));
 }
 
 /**
Signature · 1 line
2.52.0
#3 lore raw

PATCH 3/13 wifi: libertas_tf: drop redundant device reference

In reply to Johan Hovold

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.

---
 drivers/net/wireless/marvell/libertas_tf/if_usb.c | 2 --
 1 file changed, 2 deletions(-)
drivers/net/wireless/marvell/libertas_tf/if_usb.c +0 -2
index f49151c18b79..07b38f2b8f58 100644
@@ -223,7 +223,6 @@ static int if_usb_probe(struct usb_interface *intf,
 	if (!priv)
 		goto dealloc;
 
-	usb_get_dev(udev);
 	usb_set_intfdata(intf, cardp);
 
 	return 0;
@@ -258,7 +257,6 @@ static void if_usb_disconnect(struct usb_interface *intf)
 	kfree(cardp);
 
 	usb_set_intfdata(intf, NULL);
-	usb_put_dev(interface_to_usbdev(intf));
 
 	lbtf_deb_leave(LBTF_DEB_MAIN);
 }
Signature · 1 line
2.52.0
#4 lore raw

PATCH 4/13 wifi: mwifiex: drop redundant device reference

In reply to Johan Hovold

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.

---
 drivers/net/wireless/marvell/mwifiex/usb.c | 4 ----
 1 file changed, 4 deletions(-)
drivers/net/wireless/marvell/mwifiex/usb.c +0 -4
index 947ecb0a7b40..f4b94a1054f6 100644
@@ -520,8 +520,6 @@ static int mwifiex_usb_probe(struct usb_interface *intf,
 		return ret;
 	}
 
-	usb_get_dev(udev);
-
 	return 0;
 }
 
@@ -666,8 +664,6 @@ static void mwifiex_usb_disconnect(struct usb_interface *intf)
 	mwifiex_dbg(adapter, FATAL,
 		    "%s: removing card\n", __func__);
 	mwifiex_remove_card(adapter);
-
-	usb_put_dev(interface_to_usbdev(intf));
 }
 
 static void mwifiex_usb_coredump(struct device *dev)
Signature · 1 line
2.52.0
reviewer Acked-by on v1 4/13
#5 lore raw

Re: [PATCH 04/13] wifi: mwifiex: drop redundant device reference

Quoted 10 lines · Johan Hovold

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.

#6 lore raw

PATCH 5/13 wifi: mt76: drop redundant device reference

In reply to Johan Hovold

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.

---
 drivers/net/wireless/mediatek/mt76/mt7615/usb.c | 3 ---
 1 file changed, 3 deletions(-)
drivers/net/wireless/mediatek/mt76/mt7615/usb.c +0 -3
index d91feffadda9..bab7b91f14be 100644
@@ -151,7 +151,6 @@ static int mt7663u_probe(struct usb_interface *usb_intf,
 		return -ENOMEM;
 
 	dev = container_of(mdev, struct mt7615_dev, mt76);
-	udev = usb_get_dev(udev);
 	usb_reset_device(udev);
 
 	usb_set_intfdata(usb_intf, dev);
@@ -193,7 +192,6 @@ static int mt7663u_probe(struct usb_interface *usb_intf,
 error:
 	mt76u_queues_deinit(&dev->mt76);
 	usb_set_intfdata(usb_intf, NULL);
-	usb_put_dev(interface_to_usbdev(usb_intf));
 
 	mt76_free_device(&dev->mt76);
 
@@ -211,7 +209,6 @@ static void mt7663u_disconnect(struct usb_interface *usb_intf)
 	mt7663u_cleanup(dev);
 
 	usb_set_intfdata(usb_intf, NULL);
-	usb_put_dev(interface_to_usbdev(usb_intf));
 
 	mt76_free_device(&dev->mt76);
 }
Signature · 1 line
2.52.0
#7 lore raw

PATCH 6/13 wifi: mt76x0u: drop redundant device reference

In reply to Johan Hovold

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.

---
 drivers/net/wireless/mediatek/mt76/mt76x0/usb.c | 3 ---
 1 file changed, 3 deletions(-)
drivers/net/wireless/mediatek/mt76/mt76x0/usb.c +0 -3
index 90e5666c0857..2acce121fb14 100644
@@ -245,7 +245,6 @@ static int mt76x0u_probe(struct usb_interface *usb_intf,
 	if (id->driver_info)
 		dev->no_2ghz = true;
 
-	usb_dev = usb_get_dev(usb_dev);
 	usb_reset_device(usb_dev);
 
 	usb_set_intfdata(usb_intf, dev);
@@ -284,7 +283,6 @@ static int mt76x0u_probe(struct usb_interface *usb_intf,
 
 err:
 	usb_set_intfdata(usb_intf, NULL);
-	usb_put_dev(interface_to_usbdev(usb_intf));
 	mt76u_queues_deinit(&dev->mt76);
 	mt76_free_device(&dev->mt76);
 
@@ -303,7 +301,6 @@ static void mt76x0_disconnect(struct usb_interface *usb_intf)
 	mt76x0u_cleanup(dev);
 
 	usb_set_intfdata(usb_intf, NULL);
-	usb_put_dev(interface_to_usbdev(usb_intf));
 
 	mt76_free_device(&dev->mt76);
 }
Signature · 1 line
2.52.0
#8 lore raw

PATCH 7/13 wifi: mt76x2u: drop redundant device reference

In reply to Johan Hovold

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.

---
 drivers/net/wireless/mediatek/mt76/mt76x2/usb.c | 4 ----
 1 file changed, 4 deletions(-)
drivers/net/wireless/mediatek/mt76/mt76x2/usb.c +0 -4
index 01cb3b2830f3..8af360bca643 100644
@@ -57,7 +57,6 @@ static int mt76x2u_probe(struct usb_interface *intf,
 
 	dev = container_of(mdev, struct mt76x02_dev, mt76);
 
-	udev = usb_get_dev(udev);
 	usb_reset_device(udev);
 
 	usb_set_intfdata(intf, dev);
@@ -84,14 +83,12 @@ static int mt76x2u_probe(struct usb_interface *intf,
 	mt76u_queues_deinit(&dev->mt76);
 	mt76_free_device(&dev->mt76);
 	usb_set_intfdata(intf, NULL);
-	usb_put_dev(udev);
 
 	return err;
 }
 
 static void mt76x2u_disconnect(struct usb_interface *intf)
 {
-	struct usb_device *udev = interface_to_usbdev(intf);
 	struct mt76x02_dev *dev = usb_get_intfdata(intf);
 	struct ieee80211_hw *hw = mt76_hw(dev);
 
@@ -100,7 +97,6 @@ static void mt76x2u_disconnect(struct usb_interface *intf)
 	mt76x2u_cleanup(dev);
 	mt76_free_device(&dev->mt76);
 	usb_set_intfdata(intf, NULL);
-	usb_put_dev(udev);
 }
 
 static int __maybe_unused mt76x2u_suspend(struct usb_interface *intf,
Signature · 1 line
2.52.0
#9 lore raw

PATCH 8/13 wifi: mt76: mt792xu: drop redundant device reference

In reply to Johan Hovold

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.

---
 drivers/net/wireless/mediatek/mt76/mt7921/usb.c | 2 --
 drivers/net/wireless/mediatek/mt76/mt7925/usb.c | 2 --
 drivers/net/wireless/mediatek/mt76/mt792x_usb.c | 1 -
 3 files changed, 5 deletions(-)
drivers/net/wireless/mediatek/mt76/mt7921/usb.c +0 -2
index 17057e68bf21..9bfc234f306f 100644
@@ -197,7 +197,6 @@ static int mt7921u_probe(struct usb_interface *usb_intf,
 	dev->fw_features = features;
 	dev->hif_ops = &hif_ops;
 
-	udev = usb_get_dev(udev);
 	usb_reset_device(udev);
 
 	usb_set_intfdata(usb_intf, dev);
@@ -246,7 +245,6 @@ static int mt7921u_probe(struct usb_interface *usb_intf,
 	mt76u_queues_deinit(&dev->mt76);
 
 	usb_set_intfdata(usb_intf, NULL);
-	usb_put_dev(interface_to_usbdev(usb_intf));
 
 	mt76_free_device(&dev->mt76);
 
drivers/net/wireless/mediatek/mt76/mt7925/usb.c +0 -2
index d9968f03856d..84bcebbf009a 100644
@@ -185,7 +185,6 @@ static int mt7925u_probe(struct usb_interface *usb_intf,
 	dev->fw_features = features;
 	dev->hif_ops = &hif_ops;
 
-	udev = usb_get_dev(udev);
 	usb_reset_device(udev);
 
 	usb_set_intfdata(usb_intf, dev);
@@ -234,7 +233,6 @@ static int mt7925u_probe(struct usb_interface *usb_intf,
 	mt76u_queues_deinit(&dev->mt76);
 
 	usb_set_intfdata(usb_intf, NULL);
-	usb_put_dev(interface_to_usbdev(usb_intf));
 
 	mt76_free_device(&dev->mt76);
 
drivers/net/wireless/mediatek/mt76/mt792x_usb.c +0 -1
index 552808458138..f827e8a56a18 100644
@@ -306,7 +306,6 @@ void mt792xu_disconnect(struct usb_interface *usb_intf)
 	mt792xu_cleanup(dev);
 
 	usb_set_intfdata(usb_intf, NULL);
-	usb_put_dev(interface_to_usbdev(usb_intf));
 
 	mt76_free_device(&dev->mt76);
 }
Signature · 1 line
2.52.0
#10 lore raw

PATCH 9/13 wifi: mt7601u: drop redundant device reference

In reply to Johan Hovold

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.

---
 drivers/net/wireless/mediatek/mt7601u/usb.c | 3 ---
 1 file changed, 3 deletions(-)
drivers/net/wireless/mediatek/mt7601u/usb.c +0 -3
index c41ae251cb95..9306870cbc91 100644
@@ -274,7 +274,6 @@ static int mt7601u_probe(struct usb_interface *usb_intf,
 	if (!dev)
 		return -ENOMEM;
 
-	usb_dev = usb_get_dev(usb_dev);
 	usb_reset_device(usb_dev);
 
 	usb_set_intfdata(usb_intf, dev);
@@ -319,7 +318,6 @@ static int mt7601u_probe(struct usb_interface *usb_intf,
 	mt7601u_cleanup(dev);
 err:
 	usb_set_intfdata(usb_intf, NULL);
-	usb_put_dev(interface_to_usbdev(usb_intf));
 
 	destroy_workqueue(dev->stat_wq);
 	ieee80211_free_hw(dev->hw);
@@ -334,7 +332,6 @@ static void mt7601u_disconnect(struct usb_interface *usb_intf)
 	mt7601u_cleanup(dev);
 
 	usb_set_intfdata(usb_intf, NULL);
-	usb_put_dev(interface_to_usbdev(usb_intf));
 
 	destroy_workqueue(dev->stat_wq);
 	ieee80211_free_hw(dev->hw);
Signature · 1 line
2.52.0
#11 lore raw

PATCH 10/13 wifi: rt2x00: drop redundant device reference

In reply to Johan Hovold

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.

---
 drivers/net/wireless/ralink/rt2x00/rt2x00usb.c | 12 +-----------
 1 file changed, 1 insertion(+), 11 deletions(-)
drivers/net/wireless/ralink/rt2x00/rt2x00usb.c +1 -11
index 54599cad78f9..83d00b6baf64 100644
@@ -802,14 +802,12 @@ int rt2x00usb_probe(struct usb_interface *usb_intf,
 	struct rt2x00_dev *rt2x00dev;
 	int retval;
 
-	usb_dev = usb_get_dev(usb_dev);
 	usb_reset_device(usb_dev);
 
 	hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw);
 	if (!hw) {
 		rt2x00_probe_err("Failed to allocate hardware\n");
-		retval = -ENOMEM;
-		goto exit_put_device;
+		return -ENOMEM;
 	}
 
 	usb_set_intfdata(usb_intf, hw);
@@ -851,10 +849,6 @@ int rt2x00usb_probe(struct usb_interface *usb_intf,
 
 exit_free_device:
 	ieee80211_free_hw(hw);
-
-exit_put_device:
-	usb_put_dev(usb_dev);
-
 	usb_set_intfdata(usb_intf, NULL);
 
 	return retval;
@@ -873,11 +867,7 @@ void rt2x00usb_disconnect(struct usb_interface *usb_intf)
 	rt2x00usb_free_reg(rt2x00dev);
 	ieee80211_free_hw(hw);
 
-	/*
-	 * Free the USB device data.
-	 */
 	usb_set_intfdata(usb_intf, NULL);
-	usb_put_dev(interface_to_usbdev(usb_intf));
 }
 EXPORT_SYMBOL_GPL(rt2x00usb_disconnect);
 
Signature · 1 line
2.52.0
#12 lore raw

Re: [PATCH 10/13] wifi: rt2x00: drop redundant device reference

Quoted 6 lines · Johan Hovold

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

Getting the reference in probe() and drop it in disconnect() was not a cargo cult. That was requirement from usb_get_dev() comment, that later it was changed by below commit:

commit f6a9a2d64dd168b7d71076c0e6b2be7db7cb7399

Author: Alan Stern <[email protected]>
Date:   Fri Feb 25 09:38:25 2022 -0500

USB: core: Update kerneldoc for usb_get_dev() and usb_get_intf()

The kerneldoc for usb_get_dev() and usb_get_intf() says that drivers should always refcount the references they hold for the usb_device or usb_interface structure, respectively. But this is an overstatement:
In many cases drivers do not access these references after they have been unbound, and in such cases refcounting is unnecessary.

This patch updates the kerneldoc for the two routines, explaining when a driver does not need to increment and decrement the refcount. This should help dispel misconceptions which might otherwise afflict programmers new to the USB subsystem.

Regards
Stanislaw

Quoted 54 lines

easier to spot drivers where an extra reference is needed, and reduce the risk of memory leaks when drivers fail to release it.

---
 drivers/net/wireless/ralink/rt2x00/rt2x00usb.c | 12 +-----------
 1 file changed, 1 insertion(+), 11 deletions(-)
drivers/net/wireless/ralink/rt2x00/rt2x00usb.c +1 -11
index 54599cad78f9..83d00b6baf64 100644
@@ -802,14 +802,12 @@ int rt2x00usb_probe(struct usb_interface *usb_intf,
 	struct rt2x00_dev *rt2x00dev;
 	int retval;
 
-	usb_dev = usb_get_dev(usb_dev);
 	usb_reset_device(usb_dev);
 
 	hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw);
 	if (!hw) {
 		rt2x00_probe_err("Failed to allocate hardware\n");
-		retval = -ENOMEM;
-		goto exit_put_device;
+		return -ENOMEM;
 	}
 
 	usb_set_intfdata(usb_intf, hw);
@@ -851,10 +849,6 @@ int rt2x00usb_probe(struct usb_interface *usb_intf,
 
 exit_free_device:
 	ieee80211_free_hw(hw);
-
-exit_put_device:
-	usb_put_dev(usb_dev);
-
 	usb_set_intfdata(usb_intf, NULL);
 
 	return retval;
@@ -873,11 +867,7 @@ void rt2x00usb_disconnect(struct usb_interface *usb_intf)
 	rt2x00usb_free_reg(rt2x00dev);
 	ieee80211_free_hw(hw);
 
-	/*
-	 * Free the USB device data.
-	 */
 	usb_set_intfdata(usb_intf, NULL);
-	usb_put_dev(interface_to_usbdev(usb_intf));
 }
 EXPORT_SYMBOL_GPL(rt2x00usb_disconnect);
 
Signature · 1 line
2.52.0
#13 lore raw

Re: [PATCH 10/13] wifi: rt2x00: drop redundant device reference

Quoted 24 lines · Stanislaw Gruszka
Quoted 6 lines · Johan Hovold

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

Getting the reference in probe() and drop it in disconnect() was not a cargo cult. That was requirement from usb_get_dev() comment, that later it was changed by below commit:

commit f6a9a2d64dd168b7d71076c0e6b2be7db7cb7399

Author: Alan Stern <[email protected]>
Date:   Fri Feb 25 09:38:25 2022 -0500

USB: core: Update kerneldoc for usb_get_dev() and usb_get_intf()

The kerneldoc for usb_get_dev() and usb_get_intf() says that drivers should always refcount the references they hold for the usb_device or usb_interface structure, respectively. But this is an overstatement:
In many cases drivers do not access these references after they have been unbound, and in such cases refcounting is unnecessary.

Quoted 4 lines

This patch updates the kerneldoc for the two routines, explaining when a driver does not need to increment and decrement the refcount. This should help dispel misconceptions which might otherwise afflict programmers new to the USB subsystem.

Yeah, the documentation indeed was misleading for a very long time. That reference comment even predates the driver model.

Many authors base their work on existing drivers so we still get new ones that take such references even after the docs were amended.

Johan

maintainer Acked-by on v1 10/13
#14 lore raw

Re: [PATCH 10/13] wifi: rt2x00: drop redundant device reference

In reply to Johan Hovold
Quoted 10 lines · Johan Hovold

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.

Quoted 50 lines
---
 drivers/net/wireless/ralink/rt2x00/rt2x00usb.c | 12 +-----------
 1 file changed, 1 insertion(+), 11 deletions(-)
drivers/net/wireless/ralink/rt2x00/rt2x00usb.c +1 -11
index 54599cad78f9..83d00b6baf64 100644
@@ -802,14 +802,12 @@ int rt2x00usb_probe(struct usb_interface *usb_intf,
 	struct rt2x00_dev *rt2x00dev;
 	int retval;
 
-	usb_dev = usb_get_dev(usb_dev);
 	usb_reset_device(usb_dev);
 
 	hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw);
 	if (!hw) {
 		rt2x00_probe_err("Failed to allocate hardware\n");
-		retval = -ENOMEM;
-		goto exit_put_device;
+		return -ENOMEM;
 	}
 
 	usb_set_intfdata(usb_intf, hw);
@@ -851,10 +849,6 @@ int rt2x00usb_probe(struct usb_interface *usb_intf,
 
 exit_free_device:
 	ieee80211_free_hw(hw);
-
-exit_put_device:
-	usb_put_dev(usb_dev);
-
 	usb_set_intfdata(usb_intf, NULL);
 
 	return retval;
@@ -873,11 +867,7 @@ void rt2x00usb_disconnect(struct usb_interface *usb_intf)
 	rt2x00usb_free_reg(rt2x00dev);
 	ieee80211_free_hw(hw);
 
-	/*
-	 * Free the USB device data.
-	 */
 	usb_set_intfdata(usb_intf, NULL);
-	usb_put_dev(interface_to_usbdev(usb_intf));
 }
 EXPORT_SYMBOL_GPL(rt2x00usb_disconnect);
 
Signature · 1 line
2.52.0
#15 lore raw

PATCH 11/13 wifi: rtl818x: drop redundant device reference

In reply to Johan Hovold

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.

---
 drivers/net/wireless/realtek/rtl818x/rtl8187/dev.c | 4 ----
 1 file changed, 4 deletions(-)
drivers/net/wireless/realtek/rtl818x/rtl8187/dev.c +0 -4
index f7e0f6573180..1d21c468a236 100644
@@ -1475,8 +1475,6 @@ static int rtl8187_probe(struct usb_interface *intf,
 	usb_set_intfdata(intf, dev);
 	priv->udev = udev;
 
-	usb_get_dev(udev);
-
 	skb_queue_head_init(&priv->rx_queue);
 
 	BUILD_BUG_ON(sizeof(priv->channels) != sizeof(rtl818x_channels));
@@ -1663,7 +1661,6 @@ static int rtl8187_probe(struct usb_interface *intf,
  err_free_dmabuf:
 	kfree(priv->io_dmabuf);
 	usb_set_intfdata(intf, NULL);
-	usb_put_dev(udev);
  err_free_dev:
 	ieee80211_free_hw(dev);
 	return err;
@@ -1685,7 +1682,6 @@ static void rtl8187_disconnect(struct usb_interface *intf)
 
 	priv = dev->priv;
 	usb_reset_device(priv->udev);
-	usb_put_dev(interface_to_usbdev(intf));
 	kfree(priv->io_dmabuf);
 	ieee80211_free_hw(dev);
 }
Signature · 1 line
2.52.0
#16 lore raw

PATCH 12/13 wifi: rtl8xxxu: drop redundant device reference

In reply to Johan Hovold

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.

---
 drivers/net/wireless/realtek/rtl8xxxu/core.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)
drivers/net/wireless/realtek/rtl8xxxu/core.c +3 -8
index 794187d28caa..f131ca09ae43 100644
@@ -7698,7 +7698,7 @@ static int rtl8xxxu_probe(struct usb_interface *interface,
 	int ret;
 	int untested = 1;
 
-	udev = usb_get_dev(interface_to_usbdev(interface));
+	udev = interface_to_usbdev(interface);
 
 	switch (id->idVendor) {
 	case USB_VENDOR_ID_REALTEK:
@@ -7756,10 +7756,8 @@ static int rtl8xxxu_probe(struct usb_interface *interface,
 	}
 
 	hw = ieee80211_alloc_hw(sizeof(struct rtl8xxxu_priv), &rtl8xxxu_ops);
-	if (!hw) {
-		ret = -ENOMEM;
-		goto err_put_dev;
-	}
+	if (!hw)
+		return -ENOMEM;
 
 	priv = hw->priv;
 	priv->hw = hw;
@@ -7901,8 +7899,6 @@ static int rtl8xxxu_probe(struct usb_interface *interface,
 	mutex_destroy(&priv->h2c_mutex);
 
 	ieee80211_free_hw(hw);
-err_put_dev:
-	usb_put_dev(udev);
 
 	return ret;
 }
@@ -7935,7 +7931,6 @@ static void rtl8xxxu_disconnect(struct usb_interface *interface)
 			 "Device still attached, trying to reset\n");
 		usb_reset_device(priv->udev);
 	}
-	usb_put_dev(priv->udev);
 	ieee80211_free_hw(hw);
 }
 
Signature · 1 line
2.52.0
#17 lore raw

PATCH 13/13 wifi: rtlwifi: usb: drop redundant device reference

In reply to Johan Hovold

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.

---
 drivers/net/wireless/realtek/rtlwifi/usb.c | 4 ----
 1 file changed, 4 deletions(-)
drivers/net/wireless/realtek/rtlwifi/usb.c +0 -4
index d35ed56d6db9..9a64df9eed39 100644
@@ -986,7 +986,6 @@ int rtl_usb_probe(struct usb_interface *intf,
 	init_completion(&rtlpriv->firmware_loading_complete);
 	SET_IEEE80211_DEV(hw, &intf->dev);
 	udev = interface_to_usbdev(intf);
-	usb_get_dev(udev);
 	usb_priv = rtl_usbpriv(hw);
 	memset(usb_priv, 0, sizeof(*usb_priv));
 	usb_priv->dev.intf = intf;
@@ -1038,7 +1037,6 @@ int rtl_usb_probe(struct usb_interface *intf,
 	rtl_deinit_core(hw);
 error_out2:
 	_rtl_usb_io_handler_release(hw);
-	usb_put_dev(udev);
 	kfree(rtlpriv->usb_data);
 	ieee80211_free_hw(hw);
 	return -ENODEV;
@@ -1050,7 +1048,6 @@ void rtl_usb_disconnect(struct usb_interface *intf)
 	struct ieee80211_hw *hw = usb_get_intfdata(intf);
 	struct rtl_priv *rtlpriv = rtl_priv(hw);
 	struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
-	struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
 
 	if (unlikely(!rtlpriv))
 		return;
@@ -1072,7 +1069,6 @@ void rtl_usb_disconnect(struct usb_interface *intf)
 	kfree(rtlpriv->usb_data);
 	rtlpriv->cfg->ops->deinit_sw_vars(hw);
 	_rtl_usb_io_handler_release(hw);
-	usb_put_dev(rtlusb->udev);
 	usb_set_intfdata(intf, NULL);
 	ieee80211_free_hw(hw);
 }
Signature · 1 line
2.52.0
maintainer
#18 lore raw
In reply to Johan Hovold

// +Cc Greg

Hi Johan,

Johan Hovold <[email protected]> wrote:

[...]

Quoted 3 lines
 drivers/net/wireless/realtek/rtl818x/rtl8187/dev.c |  4 ----
 drivers/net/wireless/realtek/rtl8xxxu/core.c       | 11 +++--------
 drivers/net/wireless/realtek/rtlwifi/usb.c         |  4 ----

Acked to changes of these Realtek WiFi drivers. But I'd like to know why rtw88/rtw89 aren't included in this patchset? Greg sent a patch [1] to correct error path of USB probe for rtw88. In the discussion, he also mentioned the simplest way is to drop usb_get_dev()/usb_put_dev() like this patchset does. Will you share patches for rtw88/rtw89? I so, I'd drop Greg's patch and apply yours.

[1] https://lore.kernel.org/linux-wireless/2026022333-periscope-unusual-f0a0@gregkh/

Ping-Ke

#19 lore raw
Quoted 6 lines · Ping-Ke Shih

// +Cc Greg

Hi Johan,

Johan Hovold <[email protected]> wrote:

Quoted 8 lines

[...]

Quoted 3 lines
 drivers/net/wireless/realtek/rtl818x/rtl8187/dev.c |  4 ----
 drivers/net/wireless/realtek/rtl8xxxu/core.c       | 11 +++--------
 drivers/net/wireless/realtek/rtlwifi/usb.c         |  4 ----

Acked to changes of these Realtek WiFi drivers. But I'd like to know why rtw88/rtw89 aren't included in this patchset?

I used a more specific grep pattern to catch driver releasing references in disconnect() and therefore missed a few that dropped the reference in helper functions (or used non-standard names for their disconnect function such as ath10k, I see now).

Quoted 7 lines

Greg sent a patch [1] to
correct error path of USB probe for rtw88. In the discussion, he also mentioned the simplest way is to drop usb_get_dev()/usb_put_dev() like this patchset does. Will you share patches for rtw88/rtw89? I so, I'd drop Greg's patch and apply yours.

[1] https://lore.kernel.org/linux-wireless/2026022333-periscope-unusual-f0a0@gregkh/

Sure, I can include rtw88 (and ath10k and ath6kl) in a v2.

Do you really prefer replacing Greg's fix or shall I send an incremental patch on top?

Johan

maintainer
#20 lore raw
Quoted 27 lines · Johan Hovold
Quoted 6 lines · Ping-Ke Shih

// +Cc Greg

Hi Johan,

Johan Hovold <[email protected]> wrote:

Quoted 8 lines

[...]

Quoted 3 lines
 drivers/net/wireless/realtek/rtl818x/rtl8187/dev.c |  4 ----
 drivers/net/wireless/realtek/rtl8xxxu/core.c       | 11 +++--------
 drivers/net/wireless/realtek/rtlwifi/usb.c         |  4 ----

Acked to changes of these Realtek WiFi drivers. But I'd like to know why rtw88/rtw89 aren't included in this patchset?

I used a more specific grep pattern to catch driver releasing references in disconnect() and therefore missed a few that dropped the reference in helper functions (or used non-standard names for their disconnect function such as ath10k, I see now).

Quoted 6 lines

Greg sent a patch [1] to
correct error path of USB probe for rtw88. In the discussion, he also mentioned the simplest way is to drop usb_get_dev()/usb_put_dev() like this patchset does. Will you share patches for rtw88/rtw89? I so, I'd drop Greg's patch and apply yours.

Quoted 3 lines
Quoted 1 line

[1] https://lore.kernel.org/linux-wireless/2026022333-periscope-unusual-f0a0@gregkh/

Sure, I can include rtw88 (and ath10k and ath6kl) in a v2.

Can you please also include rtw89? (I saw you have sent v2 though)

Quoted 3 lines

Do you really prefer replacing Greg's fix or shall I send an incremental patch on top?

No. I'd drop Gerg's fix from my tree.

Ping-Ke

#21 lore raw
Quoted 5 lines · Ping-Ke Shih
Quoted 1 line · Johan Hovold

Sure, I can include rtw88 (and ath10k and ath6kl) in a v2.

Can you please also include rtw89? (I saw you have sent v2 though)

Ah, sorry about that. I was sure I had grepped for usb_get_dev in wireless before sending v2 but apparently I did not.

I just sent a separate follow-up for rtw89 here:

	https://lore.kernel.org/r/[email protected]

Johan

maintainer
#22 lore raw
Quoted 5 lines · Johan Hovold
Quoted 4 lines · Ping-Ke Shih
Quoted 1 line · Johan Hovold

Sure, I can include rtw88 (and ath10k and ath6kl) in a v2.

Quoted 8 lines
Quoted 1 line

Can you please also include rtw89? (I saw you have sent v2 though)

Ah, sorry about that. I was sure I had grepped for usb_get_dev in wireless before sending v2 but apparently I did not.

I just sent a separate follow-up for rtw89 here:

        https://lore.kernel.org/r/[email protected]

Got it & thank you. :)

Ping-Ke

#23 lore raw
In reply to Ping-Ke Shih
Quoted 32 lines · Ping-Ke Shih
Quoted 27 lines · Johan Hovold
Quoted 6 lines · Ping-Ke Shih

// +Cc Greg

Hi Johan,

Johan Hovold <[email protected]> wrote:

Quoted 8 lines

[...]

Quoted 3 lines
 drivers/net/wireless/realtek/rtl818x/rtl8187/dev.c |  4 ----
 drivers/net/wireless/realtek/rtl8xxxu/core.c       | 11 +++--------
 drivers/net/wireless/realtek/rtlwifi/usb.c         |  4 ----

Acked to changes of these Realtek WiFi drivers. But I'd like to know why rtw88/rtw89 aren't included in this patchset?

I used a more specific grep pattern to catch driver releasing references in disconnect() and therefore missed a few that dropped the reference in helper functions (or used non-standard names for their disconnect function such as ath10k, I see now).

Quoted 6 lines

Greg sent a patch [1] to
correct error path of USB probe for rtw88. In the discussion, he also mentioned the simplest way is to drop usb_get_dev()/usb_put_dev() like this patchset does. Will you share patches for rtw88/rtw89? I so, I'd drop Greg's patch and apply yours.

Quoted 3 lines
Quoted 1 line

[1] https://lore.kernel.org/linux-wireless/2026022333-periscope-unusual-f0a0@gregkh/

Sure, I can include rtw88 (and ath10k and ath6kl) in a v2.

Quoted 7 lines

Can you please also include rtw89? (I saw you have sent v2 though)

Quoted 3 lines

Do you really prefer replacing Greg's fix or shall I send an incremental patch on top?

No. I'd drop Gerg's fix from my tree.

I have no objection for my changes being dropped, thanks for making these changes.

greg k-h