Skip to contentSkip to conversation

staging: rtl8712: Fix memory leak in r8712_init_recv_priv

PATCH Superseded Dongliang Mu 2 participants 6 messages May 21, 2021 Staging

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 staging: rtl8712: Fix memory leak in r8712_init_recv_priv

r871xu_dev_remove failed to call r8712_free_drv_sw() and free the resource (e.g., struct urb) due to the failure of firmware loading.

Fix this by invoking r8712_free_drv_sw at the failure site.

Reported-by [email protected]
Fixes b4383c971bc5 ("staging: rtl8712: handle firmware load failure")
---
 drivers/staging/rtl8712/usb_intf.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/staging/rtl8712/usb_intf.c +10 -3
index dc21e7743349..a5190b4250ce 100644
@@ -589,7 +589,7 @@ static int r871xu_drv_init(struct usb_interface *pusb_intf,
  */
 static void r871xu_dev_remove(struct usb_interface *pusb_intf)
 {
-	struct net_device *pnetdev = usb_get_intfdata(pusb_intf);
+	struct net_device *pnetdev, *newpnetdev = usb_get_intfdata(pusb_intf);
 	struct usb_device *udev = interface_to_usbdev(pusb_intf);
 
 	if (pnetdev) {
@@ -597,9 +597,9 @@ static void r871xu_dev_remove(struct usb_interface *pusb_intf)
 
 		/* never exit with a firmware callback pending */
 		wait_for_completion(&padapter->rtl8712_fw_ready);
-		pnetdev = usb_get_intfdata(pusb_intf);
+		newpnetdev = usb_get_intfdata(pusb_intf);
 		usb_set_intfdata(pusb_intf, NULL);
-		if (!pnetdev)
+		if (!newpnetdev)
 			goto firmware_load_fail;
 		release_firmware(padapter->fw);
 		if (drvpriv.drv_registered)
@@ -625,6 +625,13 @@ static void r871xu_dev_remove(struct usb_interface *pusb_intf)
 	 */
 	if (udev->state != USB_STATE_NOTATTACHED)
 		usb_reset_device(udev);
+	if (pnetdev) {
+		struct _adapter *padapter = netdev_priv(pnetdev);
+		/* Stop driver mlme relation timer */
+		//r8712_stop_drv_timers(padapter);
+		//r871x_dev_unload(padapter);
+		r8712_free_drv_sw(padapter);
+	}
 }
 
 static int __init r8712u_drv_entry(void)
Signature · 1 line
2.25.1
author
#1 lore raw
Quoted 39 lines · Dongliang Mu

r871xu_dev_remove failed to call r8712_free_drv_sw() and free the resource (e.g., struct urb) due to the failure of firmware loading.

Fix this by invoking r8712_free_drv_sw at the failure site.

Reported-by [email protected]
Fixes b4383c971bc5 ("staging: rtl8712: handle firmware load failure")
---
 drivers/staging/rtl8712/usb_intf.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/staging/rtl8712/usb_intf.c +3 -3
index dc21e7743349..a5190b4250ce 100644
@@ -589,7 +589,7 @@ static int r871xu_drv_init(struct usb_interface *pusb_intf,
  */
 static void r871xu_dev_remove(struct usb_interface *pusb_intf)
 {
-       struct net_device *pnetdev = usb_get_intfdata(pusb_intf);
+       struct net_device *pnetdev, *newpnetdev = usb_get_intfdata(pusb_intf);
        struct usb_device *udev = interface_to_usbdev(pusb_intf);
 
        if (pnetdev) {
@@ -597,9 +597,9 @@ static void r871xu_dev_remove(struct usb_interface *pusb_intf)
 
                /* never exit with a firmware callback pending */
                wait_for_completion(&padapter->rtl8712_fw_ready);
-               pnetdev = usb_get_intfdata(pusb_intf);
+               newpnetdev = usb_get_intfdata(pusb_intf);
                usb_set_intfdata(pusb_intf, NULL);
-               if (!pnetdev)
+               if (!newpnetdev)
                        goto firmware_load_fail;
                release_firmware(padapter->fw);
                if (drvpriv.drv_registered)

drivers/staging/rtl8712/usb_intf.c static void r871xu_dev_remove(struct usb_interface *pusb_intf) · line 632

Quoted 8 lines
drivers/staging/rtl8712/usb_intf.c +5 -0
@@ -625,6 +625,13 @@ static void r871xu_dev_remove(struct usb_interface *pusb_intf)
         */
        if (udev->state != USB_STATE_NOTATTACHED)
                usb_reset_device(udev);
+       if (pnetdev) {
+               struct _adapter *padapter = netdev_priv(pnetdev);
+               /* Stop driver mlme relation timer */
+               //r8712_stop_drv_timers(padapter);
+               //r871x_dev_unload(padapter);

I am not sure if I should add those deallocation functions in this branch. I will appreciate it if anyone can give some advice here.

Quoted 8 lines
drivers/staging/rtl8712/usb_intf.c +2 -1
+               r8712_free_drv_sw(padapter);
+       }
 }
 
 static int __init r8712u_drv_entry(void)
--

2.25.1

maintainer
#2 lore raw
In reply to Dongliang Mu
Quoted 17 lines · Dongliang Mu

r871xu_dev_remove failed to call r8712_free_drv_sw() and free the resource (e.g., struct urb) due to the failure of firmware loading.

Fix this by invoking r8712_free_drv_sw at the failure site.

Reported-by [email protected]
Fixes b4383c971bc5 ("staging: rtl8712: handle firmware load failure")
---
 drivers/staging/rtl8712/usb_intf.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/staging/rtl8712/usb_intf.c +0 -0
index dc21e7743349..a5190b4250ce 100644

drivers/staging/rtl8712/usb_intf.c static int r871xu_drv_init(struct usb_interface *pusb_intf, · line 595

Quoted 8 lines
drivers/staging/rtl8712/usb_intf.c +1 -1
@@ -589,7 +589,7 @@ static int r871xu_drv_init(struct usb_interface *pusb_intf,
  */
 static void r871xu_dev_remove(struct usb_interface *pusb_intf)
 {
-	struct net_device *pnetdev = usb_get_intfdata(pusb_intf);
+	struct net_device *pnetdev, *newpnetdev = usb_get_intfdata(pusb_intf);
 	struct usb_device *udev = interface_to_usbdev(pusb_intf);
 
 	if (pnetdev) {

Did you test this?

I think you just broke the code right here :(

thanks,

greg k-h

author
#3 lore raw
Quoted 26 lines · Greg KH
Quoted 24 lines · Dongliang Mu

r871xu_dev_remove failed to call r8712_free_drv_sw() and free the resource (e.g., struct urb) due to the failure of firmware loading.

Fix this by invoking r8712_free_drv_sw at the failure site.

Reported-by [email protected]
Fixes b4383c971bc5 ("staging: rtl8712: handle firmware load failure")
---
 drivers/staging/rtl8712/usb_intf.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/staging/rtl8712/usb_intf.c +1 -1
index dc21e7743349..a5190b4250ce 100644
@@ -589,7 +589,7 @@ static int r871xu_drv_init(struct usb_interface *pusb_intf,
  */
 static void r871xu_dev_remove(struct usb_interface *pusb_intf)
 {
-     struct net_device *pnetdev = usb_get_intfdata(pusb_intf);
+     struct net_device *pnetdev, *newpnetdev = usb_get_intfdata(pusb_intf);
      struct usb_device *udev = interface_to_usbdev(pusb_intf);
 
Quoted 3 lines

drivers/staging/rtl8712/usb_intf.c static int r871xu_drv_init(struct usb_interface *pusb_intf, · line 595

Quoted 1 line
drivers/staging/rtl8712/usb_intf.c +0 -0
      if (pnetdev) {

Did you test this?

For now, I only tested this patch in my local workspace. The memory leak does not occur any more.

I have pushed a patch testing onto the syzbot dashboard [1]. Now it is in the pending state.

[1] https://syzkaller.appspot.com/bug?id=3a325b8389fc41c1bc94de0f4ac437ed13cce584

Quoted 2 lines

I think you just broke the code right here :(

If I broke any code logic, I am sorry. However, this patch only adds some code to deallocate some resources when failing to load firmware.

Do you mean that I replace pnetdev with the variable - newpnetdev?

Quoted 4 lines

thanks,

greg k-h

maintainer
#4 lore raw
Quoted 40 lines · 慕冬亮
Quoted 26 lines · Greg KH
Quoted 24 lines · Dongliang Mu

r871xu_dev_remove failed to call r8712_free_drv_sw() and free the resource (e.g., struct urb) due to the failure of firmware loading.

Fix this by invoking r8712_free_drv_sw at the failure site.

Reported-by [email protected]
Fixes b4383c971bc5 ("staging: rtl8712: handle firmware load failure")
---
 drivers/staging/rtl8712/usb_intf.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/staging/rtl8712/usb_intf.c +1 -1
index dc21e7743349..a5190b4250ce 100644
@@ -589,7 +589,7 @@ static int r871xu_drv_init(struct usb_interface *pusb_intf,
  */
 static void r871xu_dev_remove(struct usb_interface *pusb_intf)
 {
-     struct net_device *pnetdev = usb_get_intfdata(pusb_intf);
+     struct net_device *pnetdev, *newpnetdev = usb_get_intfdata(pusb_intf);
      struct usb_device *udev = interface_to_usbdev(pusb_intf);
 
Quoted 3 lines

drivers/staging/rtl8712/usb_intf.c static int r871xu_drv_init(struct usb_interface *pusb_intf, · line 595

Quoted 1 line
drivers/staging/rtl8712/usb_intf.c +0 -0
      if (pnetdev) {

Did you test this?

For now, I only tested this patch in my local workspace. The memory leak does not occur any more.

I have pushed a patch testing onto the syzbot dashboard [1]. Now it is in the pending state.

[1] https://syzkaller.appspot.com/bug?id=3a325b8389fc41c1bc94de0f4ac437ed13cce584

Quoted 1 line
Quoted 6 lines
Quoted 1 line

I think you just broke the code right here :(

If I broke any code logic, I am sorry. However, this patch only adds some code to deallocate some resources when failing to load firmware.

Do you mean that I replace pnetdev with the variable - newpnetdev?

Yes, and then the first thing the code does is check the value of pnetdev which is totally undefined :(

author
#5 lore raw
Quoted 44 lines · Greg KH
Quoted 42 lines · 慕冬亮
Quoted 26 lines · Greg KH
Quoted 24 lines · Dongliang Mu

r871xu_dev_remove failed to call r8712_free_drv_sw() and free the resource (e.g., struct urb) due to the failure of firmware loading.

Fix this by invoking r8712_free_drv_sw at the failure site.

Reported-by [email protected]
Fixes b4383c971bc5 ("staging: rtl8712: handle firmware load failure")
---
 drivers/staging/rtl8712/usb_intf.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/staging/rtl8712/usb_intf.c +1 -1
index dc21e7743349..a5190b4250ce 100644
@@ -589,7 +589,7 @@ static int r871xu_drv_init(struct usb_interface *pusb_intf,
  */
 static void r871xu_dev_remove(struct usb_interface *pusb_intf)
 {
-     struct net_device *pnetdev = usb_get_intfdata(pusb_intf);
+     struct net_device *pnetdev, *newpnetdev = usb_get_intfdata(pusb_intf);
      struct usb_device *udev = interface_to_usbdev(pusb_intf);
 
Quoted 3 lines

drivers/staging/rtl8712/usb_intf.c static int r871xu_drv_init(struct usb_interface *pusb_intf, · line 595

Quoted 1 line
drivers/staging/rtl8712/usb_intf.c +0 -0
      if (pnetdev) {

Did you test this?

For now, I only tested this patch in my local workspace. The memory leak does not occur any more.

I have pushed a patch testing onto the syzbot dashboard [1]. Now it is in the pending state.

[1] https://syzkaller.appspot.com/bug?id=3a325b8389fc41c1bc94de0f4ac437ed13cce584

Quoted 2 lines

I think you just broke the code right here :(

Quoted 7 lines
Quoted 4 lines

If I broke any code logic, I am sorry. However, this patch only adds some code to deallocate some resources when failing to load firmware.

Do you mean that I replace pnetdev with the variable - newpnetdev?

Yes, and then the first thing the code does is check the value of pnetdev which is totally undefined :(

You are right. Apology for the previous patch. I test my old patch below in the local workspace, it works.

------------------------------------------------------------------------------------------------------------------------

drivers/staging/rtl8712/usb_intf.c +0 -0
@@ -597,9 +597,9 @@ static void r871xu_dev_remove(struct usb_interface

*pusb_intf)

/* never exit with a firmware callback pending */ wait_for_completion(&padapter->rtl8712_fw_ready);
- pnetdev = usb_get_intfdata(pusb_intf);
+ struct net_device *newpnetdev = usb_get_intfdata(pusb_intf); usb_set_intfdata(pusb_intf, NULL);
- if (!pnetdev)
+ if (!newpnetdev)
goto firmware_load_fail; release_firmware(padapter->fw);
if (drvpriv.drv_registered)
@@ -625,6 +625,14 @@ static void r871xu_dev_remove(struct usb_interface *pusb_intf)
*/
if (udev->state != USB_STATE_NOTATTACHED) usb_reset_device(udev);
+ if (pnetdev) {
+ struct _adapter *padapter = netdev_priv(pnetdev);
+ /* Stop driver mlme relation timer */
+ //r8712_stop_drv_timers(padapter);
+ //r871x_dev_unload(padapter);
+ r8712_free_drv_sw(padapter);
+ /* udev is already freed in failed fireware loading */
+ }
}
------------------------------------------------------------------------------------------------------------------------

However, the compiler complains the declaration of newpnetdev. So I moved the declaration to the beginning, but I forget to initialize both two variables. :(

I will revise this problem and test it in my local workspace. If it works, I will resend a v2 patch.

BTW, should I uncomment "r8712_stop_drv_timers" and "r871x_dev_unload"? I am not very sure about its functionability.

Quoted 1 line