Skip to contentSkip to conversation

wifi: rtw88: properly drop usb interface reference on error

PATCH In review Greg Kroah-Hartman 2 participants 7 messages Feb 23 – Mar 9 Realtek wireless driver (rtw88)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 wifi: rtw88: properly drop usb interface reference on error

If an error happens in the usb probe path, in rtw_usb_intf_init(), the usb interface reference needs to be properly dropped, otherwise is is incorrectly increased when returning to the USB core.

Cc Ping-Ke Shih <[email protected]>
Cc stable <[email protected]>
Assisted-by gkh_clanker_2000
---
 drivers/net/wireless/realtek/rtw88/usb.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/net/wireless/realtek/rtw88/usb.c +6 -2
index 433b06c8d8a6..36ac20039ce2 100644
@@ -1046,13 +1046,17 @@ static int rtw_usb_intf_init(struct rtw_dev *rtwdev,
 
 	rtwusb->udev = udev;
 	ret = rtw_usb_parse(rtwdev, intf);
-	if (ret)
+	if (ret) {
+		usb_put_dev(udev);
 		return ret;
+	}
 
 	rtwusb->usb_data = kcalloc(RTW_USB_MAX_RXTX_COUNT, sizeof(u32),
 				   GFP_KERNEL);
-	if (!rtwusb->usb_data)
+	if (!rtwusb->usb_data) {
+		usb_put_dev(udev);
 		return -ENOMEM;
+	}
 
 	usb_set_intfdata(intf, rtwdev->hw);
 
Signature · 1 line
2.53.0
maintainer
#1 lore raw
Quoted 26 lines · Greg Kroah-Hartman

If an error happens in the usb probe path, in rtw_usb_intf_init(), the usb interface reference needs to be properly dropped, otherwise is is incorrectly increased when returning to the USB core.

Cc Ping-Ke Shih <[email protected]>
Cc stable <[email protected]>
Assisted-by gkh_clanker_2000
---
 drivers/net/wireless/realtek/rtw88/usb.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/net/wireless/realtek/rtw88/usb.c +3 -1
index 433b06c8d8a6..36ac20039ce2 100644
@@ -1046,13 +1046,17 @@ static int rtw_usb_intf_init(struct rtw_dev *rtwdev,
 
        rtwusb->udev = udev;
        ret = rtw_usb_parse(rtwdev, intf);
-       if (ret)
+       if (ret) {
+               usb_put_dev(udev);
                return ret;
+       }
 

drivers/net/wireless/realtek/rtw88/usb.c static int rtw_usb_intf_init(struct rtw_dev *rtwdev, · line 1062

Quoted 10 lines
drivers/net/wireless/realtek/rtw88/usb.c +3 -1
        rtwusb->usb_data = kcalloc(RTW_USB_MAX_RXTX_COUNT, sizeof(u32),
                                   GFP_KERNEL);
-       if (!rtwusb->usb_data)
+       if (!rtwusb->usb_data) {
+               usb_put_dev(udev);
                return -ENOMEM;
+       }
 
        usb_set_intfdata(intf, rtwdev->hw);
 

Since rtwusb->udev isn't used right after assignment in this function. Would it be simpler that moving usb_get_dev() downward like below?

drivers/net/wireless/realtek/rtw88/usb.c +2 -2
index db60e142268d..6e5c9c6f3e00 100644
@@ -1041,10 +1041,8 @@ static int rtw_usb_intf_init(struct rtw_dev *rtwdev,
                             struct usb_interface *intf)
 {
        struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev);
-       struct usb_device *udev = usb_get_dev(interface_to_usbdev(intf));
        int ret;
 
-       rtwusb->udev = udev;
        ret = rtw_usb_parse(rtwdev, intf);
        if (ret)
                return ret;
@@ -1054,6 +1052,8 @@ static int rtw_usb_intf_init(struct rtw_dev *rtwdev,
        if (!rtwusb->usb_data)
                return -ENOMEM;
 
+       rtwusb->udev = usb_get_dev(interface_to_usbdev(intf));
+
        usb_set_intfdata(intf, rtwdev->hw);
 
        SET_IEEE80211_DEV(rtwdev->hw, &intf->dev);
#2 lore raw
Quoted 35 lines · Ping-Ke Shih
Quoted 34 lines · Greg Kroah-Hartman

If an error happens in the usb probe path, in rtw_usb_intf_init(), the usb interface reference needs to be properly dropped, otherwise is is incorrectly increased when returning to the USB core.

Cc Ping-Ke Shih <[email protected]>
Cc stable <[email protected]>
Assisted-by gkh_clanker_2000
---
 drivers/net/wireless/realtek/rtw88/usb.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/net/wireless/realtek/rtw88/usb.c +6 -2
index 433b06c8d8a6..36ac20039ce2 100644
@@ -1046,13 +1046,17 @@ static int rtw_usb_intf_init(struct rtw_dev *rtwdev,
 
        rtwusb->udev = udev;
        ret = rtw_usb_parse(rtwdev, intf);
-       if (ret)
+       if (ret) {
+               usb_put_dev(udev);
                return ret;
+       }
 
        rtwusb->usb_data = kcalloc(RTW_USB_MAX_RXTX_COUNT, sizeof(u32),
                                   GFP_KERNEL);
-       if (!rtwusb->usb_data)
+       if (!rtwusb->usb_data) {
+               usb_put_dev(udev);
                return -ENOMEM;
+       }
 
Quoted 5 lines

drivers/net/wireless/realtek/rtw88/usb.c static int rtw_usb_intf_init(struct rtw_dev *rtwdev, · line 1058

Quoted 2 lines
drivers/net/wireless/realtek/rtw88/usb.c +0 -0
        usb_set_intfdata(intf, rtwdev->hw);
 

Since rtwusb->udev isn't used right after assignment in this function. Would it be simpler that moving usb_get_dev() downward like below?

What is even simpler, and easier, is to never call usb_get_dev() at all anyway as it's not needed :)

I created that pattern a few decades ago when we thought that it was going to be required, but as long as the usb interface is bound to the driver, that pointer is going to be valid so there's no real need to increment the reference count, except to feel good about doing it.

I'll gladly do that fix instead, if you want me to, I was just trying to follow the style of the existing code and fix up the current bug.

Quoted 16 lines
drivers/net/wireless/realtek/rtw88/usb.c +0 -2
index db60e142268d..6e5c9c6f3e00 100644
@@ -1041,10 +1041,8 @@ static int rtw_usb_intf_init(struct rtw_dev *rtwdev,
                             struct usb_interface *intf)
 {
        struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev);
-       struct usb_device *udev = usb_get_dev(interface_to_usbdev(intf));
        int ret;
 
-       rtwusb->udev = udev;
        ret = rtw_usb_parse(rtwdev, intf);
        if (ret)
                return ret;

drivers/net/wireless/realtek/rtw88/usb.c static int rtw_usb_intf_init(struct rtw_dev *rtwdev, · line 1055

Quoted 4 lines
drivers/net/wireless/realtek/rtw88/usb.c +1 -0
@@ -1054,6 +1052,8 @@ static int rtw_usb_intf_init(struct rtw_dev *rtwdev,
        if (!rtwusb->usb_data)
                return -ENOMEM;
 
+       rtwusb->udev = usb_get_dev(interface_to_usbdev(intf));

That too works, or again, just drop the usb_get_dev() and usb_put_dev() calls entirely.

thanks,

greg k-h

maintainer Acked-by
#3 lore raw
Quoted 45 lines · Greg Kroah-Hartman
Quoted 35 lines · Ping-Ke Shih
Quoted 34 lines · Greg Kroah-Hartman

If an error happens in the usb probe path, in rtw_usb_intf_init(), the usb interface reference needs to be properly dropped, otherwise is is incorrectly increased when returning to the USB core.

Cc Ping-Ke Shih <[email protected]>
Cc stable <[email protected]>
Assisted-by gkh_clanker_2000
---
 drivers/net/wireless/realtek/rtw88/usb.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/net/wireless/realtek/rtw88/usb.c +6 -2
index 433b06c8d8a6..36ac20039ce2 100644
@@ -1046,13 +1046,17 @@ static int rtw_usb_intf_init(struct rtw_dev *rtwdev,
 
        rtwusb->udev = udev;
        ret = rtw_usb_parse(rtwdev, intf);
-       if (ret)
+       if (ret) {
+               usb_put_dev(udev);
                return ret;
+       }
 
        rtwusb->usb_data = kcalloc(RTW_USB_MAX_RXTX_COUNT, sizeof(u32),
                                   GFP_KERNEL);
-       if (!rtwusb->usb_data)
+       if (!rtwusb->usb_data) {
+               usb_put_dev(udev);
                return -ENOMEM;
+       }
 
Quoted 5 lines

drivers/net/wireless/realtek/rtw88/usb.c static int rtw_usb_intf_init(struct rtw_dev *rtwdev, · line 1058

Quoted 2 lines
drivers/net/wireless/realtek/rtw88/usb.c +0 -0
        usb_set_intfdata(intf, rtwdev->hw);
 

Since rtwusb->udev isn't used right after assignment in this function. Would it be simpler that moving usb_get_dev() downward like below?

What is even simpler, and easier, is to never call usb_get_dev() at all anyway as it's not needed :)

Quoted 7 lines

I created that pattern a few decades ago when we thought that it was going to be required, but as long as the usb interface is bound to the driver, that pointer is going to be valid so there's no real need to increment the reference count, except to feel good about doing it.

I'll gladly do that fix instead, if you want me to, I was just trying to follow the style of the existing code and fix up the current bug.

Because I'm not much familiar with USB devices, I can't afford to the change that is too big to me. :)

Let's take your version, so

This patch can go via rtw-next tree (subtree of wireless-next), right?

#4 lore raw
Quoted 54 lines · Ping-Ke Shih
Quoted 45 lines · Greg Kroah-Hartman
Quoted 35 lines · Ping-Ke Shih
Quoted 34 lines · Greg Kroah-Hartman

If an error happens in the usb probe path, in rtw_usb_intf_init(), the usb interface reference needs to be properly dropped, otherwise is is incorrectly increased when returning to the USB core.

Cc Ping-Ke Shih <[email protected]>
Cc stable <[email protected]>
Assisted-by gkh_clanker_2000
---
 drivers/net/wireless/realtek/rtw88/usb.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/net/wireless/realtek/rtw88/usb.c +6 -2
index 433b06c8d8a6..36ac20039ce2 100644
@@ -1046,13 +1046,17 @@ static int rtw_usb_intf_init(struct rtw_dev *rtwdev,
 
        rtwusb->udev = udev;
        ret = rtw_usb_parse(rtwdev, intf);
-       if (ret)
+       if (ret) {
+               usb_put_dev(udev);
                return ret;
+       }
 
        rtwusb->usb_data = kcalloc(RTW_USB_MAX_RXTX_COUNT, sizeof(u32),
                                   GFP_KERNEL);
-       if (!rtwusb->usb_data)
+       if (!rtwusb->usb_data) {
+               usb_put_dev(udev);
                return -ENOMEM;
+       }
 
Quoted 5 lines

drivers/net/wireless/realtek/rtw88/usb.c static int rtw_usb_intf_init(struct rtw_dev *rtwdev, · line 1058

Quoted 2 lines
drivers/net/wireless/realtek/rtw88/usb.c +0 -0
        usb_set_intfdata(intf, rtwdev->hw);
 

Since rtwusb->udev isn't used right after assignment in this function. Would it be simpler that moving usb_get_dev() downward like below?

What is even simpler, and easier, is to never call usb_get_dev() at all anyway as it's not needed :)

Quoted 7 lines

I created that pattern a few decades ago when we thought that it was going to be required, but as long as the usb interface is bound to the driver, that pointer is going to be valid so there's no real need to increment the reference count, except to feel good about doing it.

I'll gladly do that fix instead, if you want me to, I was just trying to follow the style of the existing code and fix up the current bug.

Quoted 6 lines

Because I'm not much familiar with USB devices, I can't afford to the change that is too big to me. :)

Let's take your version, so

Great, thanks!

Quoted 1 line

This patch can go via rtw-next tree (subtree of wireless-next), right?

Sure, please do so.

thanks,

greg k-h

maintainer
#5 lore raw
In reply to Greg Kroah-Hartman
Quoted 9 lines · Greg Kroah-Hartman

If an error happens in the usb probe path, in rtw_usb_intf_init(), the usb interface reference needs to be properly dropped, otherwise is is incorrectly increased when returning to the USB core.

Cc Ping-Ke Shih <[email protected]>
Cc stable <[email protected]>
Assisted-by gkh_clanker_2000

1 patch(es) applied to rtw-next branch of rtw.git, thanks.

c413737effd6 wifi: rtw88: properly drop usb interface reference on error

---
https://github.com/pkshih/rtw.git

maintainer
#6 lore raw
Quoted 13 lines · Ping-Ke Shih
Quoted 9 lines · Greg Kroah-Hartman

If an error happens in the usb probe path, in rtw_usb_intf_init(), the usb interface reference needs to be properly dropped, otherwise is is incorrectly increased when returning to the USB core.

Cc Ping-Ke Shih <[email protected]>
Cc stable <[email protected]>
Assisted-by gkh_clanker_2000
Quoted 4 lines

1 patch(es) applied to rtw-next branch of rtw.git, thanks.

c413737effd6 wifi: rtw88: properly drop usb interface reference on error

As Johan shared patchset and discussion in [1], drop this patch.

[1] https://lore.kernel.org/linux-wireless/2026030625-wrongness-preorder-4e1e@gregkh/