Skip to contentSkip to conversation

Bluetooth: btusb: Fix memory leak in play_deferred

RFC PATCH Superseded Jeffy Chen 3 participants 10 messages Jul 18, 2017 Bluetooth drivers

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Jeffy Chen

author
#0 lore raw

RFC PATCH v2 1/1 Bluetooth: btusb: Fix memory leak in play_deferred

Currently we are calling usb_submit_urb directly to submit deferred tx urbs after unanchor them.

So the usb_giveback_urb_bh would failed to unref it in usb_unanchor_urb and cause memory leak:
unreferenced object 0xffffffc0ce0fa400 (size 256):
...
backtrace:

    [<ffffffc00034a9a8>] __save_stack_trace+0x48/0x6c
    [<ffffffc00034b088>] create_object+0x138/0x254
    [<ffffffc0009d5504>] kmemleak_alloc+0x58/0x8c
    [<ffffffc000345f78>] __kmalloc+0x1d4/0x2a0
    [<ffffffc0006765bc>] usb_alloc_urb+0x30/0x60
    [<ffffffbffc128598>] alloc_ctrl_urb+0x38/0x120 [btusb]
    [<ffffffbffc129e7c>] btusb_send_frame+0x64/0xf8 [btusb]

Free those urbs after completed to avoid the leak, and also fix the error handling.

---

Changes in v2:
Call usb_free_urb instead of reusing submit_tx_urb.

 drivers/bluetooth/btusb.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)
drivers/bluetooth/btusb.c +17 -3
index 0d533b2..a22a08b 100644
@@ -3260,19 +3260,33 @@ static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
 	return 0;
 }
 
+static void btusb_deferred_tx_complete(struct urb *urb)
+{
+	btusb_tx_complete(urb);
+	usb_free_urb(urb);
+}
+
 static void play_deferred(struct btusb_data *data)
 {
 	struct urb *urb;
 	int err;
 
 	while ((urb = usb_get_from_anchor(&data->deferred))) {
+		/* Add a hook to free urb after completed */
+		urb->complete = btusb_deferred_tx_complete;
+
 		err = usb_submit_urb(urb, GFP_ATOMIC);
-		if (err < 0)
-			break;
+		if (err < 0) {
+			if (err != -EPERM && err != -ENODEV)
+				BT_ERR("%s urb %p submission failed (%d)",
+				       data->hdev->name, urb, -err);
+			kfree(urb->setup_packet);
+			usb_free_urb(urb);
+			continue;
+		}
 
 		data->tx_in_flight++;
 	}
-	usb_scuttle_anchored_urbs(&data->deferred);
 }
 
 static int btusb_resume(struct usb_interface *intf)
Signature · 1 line
2.1.4
maintainer
#1 lore raw

Hi Oliver,

Quoted 57 lines

Currently we are calling usb_submit_urb directly to submit deferred tx urbs after unanchor them.

So the usb_giveback_urb_bh would failed to unref it in usb_unanchor_urb and cause memory leak:
unreferenced object 0xffffffc0ce0fa400 (size 256):
...
backtrace:

   [<ffffffc00034a9a8>] __save_stack_trace+0x48/0x6c
   [<ffffffc00034b088>] create_object+0x138/0x254
   [<ffffffc0009d5504>] kmemleak_alloc+0x58/0x8c
   [<ffffffc000345f78>] __kmalloc+0x1d4/0x2a0
   [<ffffffc0006765bc>] usb_alloc_urb+0x30/0x60
   [<ffffffbffc128598>] alloc_ctrl_urb+0x38/0x120 [btusb]
   [<ffffffbffc129e7c>] btusb_send_frame+0x64/0xf8 [btusb]

Free those urbs after completed to avoid the leak, and also fix the error handling.

---

Changes in v2:
Call usb_free_urb instead of reusing submit_tx_urb.

drivers/bluetooth/btusb.c | 20 +++++++++++++++++---

1 file changed, 17 insertions(+), 3 deletions(-)
drivers/bluetooth/btusb.c +12 -2
index 0d533b2..a22a08b 100644
@@ -3260,19 +3260,33 @@ static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
 	return 0;
 }
 
+static void btusb_deferred_tx_complete(struct urb *urb)
+{
+	btusb_tx_complete(urb);
+	usb_free_urb(urb);
+}
+
 static void play_deferred(struct btusb_data *data)
 {
 	struct urb *urb;
 	int err;
 
 	while ((urb = usb_get_from_anchor(&data->deferred))) {
+		/* Add a hook to free urb after completed */
+		urb->complete = btusb_deferred_tx_complete;
+
 		err = usb_submit_urb(urb, GFP_ATOMIC);
-		if (err < 0)
-			break;
+		if (err < 0) {
+			if (err != -EPERM && err != -ENODEV)
+				BT_ERR("%s urb %p submission failed (%d)",

drivers/bluetooth/btusb.c static int btusb_suspend(struct usb_interface *intf, pm_message_t message) · line 3290

Quoted 10 lines
drivers/bluetooth/btusb.c +5 -1
+				       data->hdev->name, urb, -err);
+			kfree(urb->setup_packet);
+			usb_free_urb(urb);
+			continue;
+		}
 
 		data->tx_in_flight++;
 	}
-	usb_scuttle_anchored_urbs(&data->deferred);
 }

can I get an ack from you on this one?

Regards

Marcel

#2 lore raw

Am Dienstag, den 18.07.2017, 08:44 +0200 schrieb Marcel Holtmann:

Quoted 35 lines
Quoted 35 lines
drivers/bluetooth/btusb.c +17 -2
index 0d533b2..a22a08b 100644
@@ -3260,19 +3260,33 @@ static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
        return 0;
 }
 
+static void btusb_deferred_tx_complete(struct urb *urb)
+{
+     btusb_tx_complete(urb);
+     usb_free_urb(urb);
+}
+
 static void play_deferred(struct btusb_data *data)
 {
        struct urb *urb;
        int err;
 
        while ((urb = usb_get_from_anchor(&data->deferred))) {
+             /* Add a hook to free urb after completed */
+             urb->complete = btusb_deferred_tx_complete;
+
                err = usb_submit_urb(urb, GFP_ATOMIC);
-             if (err < 0)
-                     break;
+             if (err < 0) {
+                     if (err != -EPERM && err != -ENODEV)
+                             BT_ERR("%s urb %p submission failed (%d)",
+                                    data->hdev->name, urb, -err);
+                     kfree(urb->setup_packet);
+                     usb_free_urb(urb);
+                     continue;
+             }
 
Quoted 6 lines

drivers/bluetooth/btusb.c static int btusb_suspend(struct usb_interface *intf, pm_message_t message) · line 3290

Quoted 4 lines
drivers/bluetooth/btusb.c +0 -1
                data->tx_in_flight++;
        }
-     usb_scuttle_anchored_urbs(&data->deferred);
 }

can I get an ack from you on this one?

Hi,

I am afraid not. We cannot silently drop one part of a transmission. I am afraid that the correct algorithm, if we encounter an error at that stage, is to abort the operation and report an error.

Regards
Oliver

author
#3 lore raw

Hi Oliver,

Quoted 43 lines · Oliver Neukum

Am Dienstag, den 18.07.2017, 08:44 +0200 schrieb Marcel Holtmann:

Quoted 35 lines
Quoted 35 lines
drivers/bluetooth/btusb.c +17 -2
index 0d533b2..a22a08b 100644
@@ -3260,19 +3260,33 @@ static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
        return 0;
 }
 
+static void btusb_deferred_tx_complete(struct urb *urb)
+{
+     btusb_tx_complete(urb);
+     usb_free_urb(urb);
+}
+
 static void play_deferred(struct btusb_data *data)
 {
        struct urb *urb;
        int err;
 
        while ((urb = usb_get_from_anchor(&data->deferred))) {
+             /* Add a hook to free urb after completed */
+             urb->complete = btusb_deferred_tx_complete;
+
                err = usb_submit_urb(urb, GFP_ATOMIC);
-             if (err < 0)
-                     break;
+             if (err < 0) {
+                     if (err != -EPERM && err != -ENODEV)
+                             BT_ERR("%s urb %p submission failed (%d)",
+                                    data->hdev->name, urb, -err);
+                     kfree(urb->setup_packet);
+                     usb_free_urb(urb);
+                     continue;
+             }
 
Quoted 6 lines

drivers/bluetooth/btusb.c static int btusb_suspend(struct usb_interface *intf, pm_message_t message) · line 3290

Quoted 4 lines
drivers/bluetooth/btusb.c +0 -1
                data->tx_in_flight++;
        }
-     usb_scuttle_anchored_urbs(&data->deferred);
 }

can I get an ack from you on this one?

Quoted 6 lines

Hi,

I am afraid not. We cannot silently drop one part of a transmission. I am afraid that the correct algorithm, if we encounter an error at that stage, is to abort the operation and report an error.

so i should break the loop when error happens right?

and i uploaded 2 version of patches, which one do you prefer to go on?

Quoted 6 lines

Regards
Oliver

#4 lore raw

Am Dienstag, den 18.07.2017, 16:08 +0800 schrieb jeffy:

Quoted 5 lines
Quoted 4 lines

I am afraid not. We cannot silently drop one part of a transmission. I am afraid that the correct algorithm, if we encounter an error at that stage, is to abort the operation and report an error.

so i should break the loop when error happens right?

Yes

Quoted 2 lines

and i uploaded 2 version of patches, which one do you prefer to go on?

Where to?

Regards
Oliver

author
#5 lore raw

Hi Oliver,

Quoted 8 lines · Oliver Neukum

Am Dienstag, den 18.07.2017, 16:08 +0800 schrieb jeffy:

Quoted 5 lines
Quoted 4 lines

I am afraid not. We cannot silently drop one part of a transmission. I am afraid that the correct algorithm, if we encounter an error at that stage, is to abort the operation and report an error.

so i should break the loop when error happens right?

Yes

ok, i'll do that.

Quoted 5 lines
Quoted 3 lines

and i uploaded 2 version of patches, which one do you prefer to go on?

Where to?

https://patchwork.kernel.org/patch/9847037
and
https://patchwork.kernel.org/patch/9846617

Quoted 7 lines

Regards
Oliver

#6 lore raw

Am Dienstag, den 18.07.2017, 17:36 +0800 schrieb jeffy:

Quoted 19 lines

Hi Oliver,

Quoted 11 lines · Oliver Neukum

Am Dienstag, den 18.07.2017, 16:08 +0800 schrieb jeffy:

Quoted 7 lines
Quoted 6 lines

I am afraid not. We cannot silently drop one part of a transmission. I am afraid that the correct algorithm, if we encounter an error at that stage, is to abort the operation and report an error.

so i should break the loop when error happens right?

Yes

ok, i'll do that.

Quoted 4 lines
Quoted 4 lines
Quoted 6 lines
Quoted 3 lines
Quoted 1 line

and i uploaded 2 version of patches, which one do you prefer to go on?

Where to?

https://patchwork.kernel.org/patch/9847037
and
https://patchwork.kernel.org/patch/9846617

I think that as soon as one URB fails, you should not even try to submit any other deferred URBs. You are taking one out from the middle of a sequence. That cannot be right.

Regards
Oliver

author
#7 lore raw

Hi Oliver,

Quoted 22 lines · Oliver Neukum

Am Dienstag, den 18.07.2017, 17:36 +0800 schrieb jeffy:

Quoted 21 lines

Hi Oliver,

Quoted 11 lines · Oliver Neukum

Am Dienstag, den 18.07.2017, 16:08 +0800 schrieb jeffy:

Quoted 7 lines
Quoted 6 lines

I am afraid not. We cannot silently drop one part of a transmission. I am afraid that the correct algorithm, if we encounter an error at that stage, is to abort the operation and report an error.

so i should break the loop when error happens right?

Yes

ok, i'll do that.

Quoted 6 lines
Quoted 5 lines

and i uploaded 2 version of patches, which one do you prefer to go on?

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Where to?

https://patchwork.kernel.org/patch/9847037
and
https://patchwork.kernel.org/patch/9846617

I think that as soon as one URB fails, you should not even try to submit any other deferred URBs. You are taking one out from the middle of a sequence. That cannot be right.

ok, that make sense.

new patch sent :)

Quoted 7 lines

Regards
Oliver

#8 lore raw

Am Dienstag, den 18.07.2017, 17:56 +0800 schrieb jeffy:

Quoted 8 lines

Hi Oliver,

Quoted 3 lines

I think that as soon as one URB fails, you should not even try to submit any other deferred URBs. You are taking one out from the middle of a sequence. That cannot be right.

ok, that make sense.

new patch sent :)

Where to?

Regards
Oliver

author
#9 lore raw

Hi Oliver,

Quoted 12 lines · Oliver Neukum

Am Dienstag, den 18.07.2017, 17:56 +0800 schrieb jeffy:

Quoted 8 lines

Hi Oliver,

Quoted 3 lines

I think that as soon as one URB fails, you should not even try to submit any other deferred URBs. You are taking one out from the middle of a sequence. That cannot be right.

ok, that make sense.

new patch sent :)

Where to?

sorry, just noticed that you're not in the auto-generated CC list by patman...i'll resend it, thanks:)

Quoted 6 lines

Regards
Oliver