* [PATCH] Bluetooth: btusb: free skb and setup packet of deferred TX URBs
@ 2026-11-30 6:47 Bilal Yazıcıoğlu
2026-11-30 16:27 ` bluez.test.bot
0 siblings, 1 reply; 2+ messages in thread
From: Bilal Yazıcıoğlu @ 2026-11-30 6:47 UTC (permalink / raw)
To: linux-bluetooth
Cc: Marcel Holtmann, Luiz Augusto von Dentz, linux-kernel,
Bilal Yazıcıoğlu
While the adapter is runtime suspended, submit_or_queue_tx_urb() does
not submit TX URBs but anchors them on data->deferred, so that
play_deferred() submits them on resume. Until then the driver owns the
skb in urb->context and the control request in urb->setup_packet,
which are normally freed by the completion handlers.
When these URBs are dropped instead of submitted, only the URB itself
is freed:
- btusb_close() and the failed path of btusb_resume() discard them
with usb_scuttle_anchored_urbs(), leaking both the skb and the
setup packet.
- play_deferred() frees the setup packet but not the skb, both when
usb_submit_urb() fails and for the URBs left on the anchor after
that.
On a ThinkPad T480 (Intel 8265, 8087:0a2b) this happens every time
Bluetooth is switched off from the desktop while the adapter is
autosuspended. thinkpad_acpi cuts power to the USB device, the
HCI_Reset sent by btintel_shutdown_combined() gets deferred, the
resume in btusb_close() fails and the queued command is dropped:
usb 1-7: USB disconnect, device number 25
Bluetooth: hci0: HCI reset during shutdown failed
Bluetooth: hci0: Error when powering off device on rfkill (-19)
kmemleak reports three objects per toggle (skb head, skb data and the
8 byte setup packet, 880 bytes in total), allocated from
hci_cmd_sync_alloc() and alloc_ctrl_urb().
Add btusb_free_deferred(), which frees the setup packet, the skb and
the URB of every URB on the deferred anchor, and use it in
btusb_close(), btusb_resume() and play_deferred(). Also free the skb
when usb_submit_urb() fails in play_deferred().
Tested on a ThinkPad T480 with KASAN, kmemleak and lockdep enabled,
switching Bluetooth off and on through blueman 10 times per run:
30 objects leaked without this patch and none with it, in two runs.
There were no KASAN or lockdep reports, and suspend/resume and Wi-Fi
rfkill kept working. Only the btusb_close() path was exercised; the
btusb_resume() and play_deferred() changes are covered by code
inspection only.
I ran a debug kernel with kmemleak on my ThinkPad T480 together with
Claude Code, which ran the tests and found the leak. It also helped
with the analysis and this changelog. I wrote the fix and reviewed the
analysis and the test results.
Fixes: 7bee549e197c ("Bluetooth: Add USB autosuspend support to btusb driver")
Fixes: 7b8e2c1db0f3 ("fix memory leak in fixed btusb_close")
Fixes: 19cfe912c37b ("Bluetooth: btusb: Fix memory leak in play_deferred")
Signed-off-by: Bilal Yazıcıoğlu <ahmetbilalyazicioglu@gmail.com>
---
drivers/bluetooth/btusb_main.c | 22 ++++++++++++++++------
1 file changed, 16 insertions(+), 6 deletions(-)
diff --git a/drivers/bluetooth/btusb_main.c b/drivers/bluetooth/btusb_main.c
index 95398e2ca..12396ed93 100644
--- a/drivers/bluetooth/btusb_main.c
+++ b/drivers/bluetooth/btusb_main.c
@@ -2122,6 +2122,18 @@ static void btusb_isoc_tx_complete(struct urb *urb)
kfree_skb(skb);
}
+static void btusb_free_deferred(struct btusb_data *data)
+{
+ struct urb *urb;
+
+ /* Deferred URBs were never submitted, so their completion won't run. */
+ while ((urb = usb_get_from_anchor(&data->deferred))) {
+ kfree(urb->setup_packet);
+ kfree_skb(urb->context);
+ usb_free_urb(urb);
+ }
+}
+
static int btusb_open(struct hci_dev *hdev)
{
struct btusb_data *data = hci_get_drvdata(hdev);
@@ -2235,7 +2247,7 @@ static int btusb_close(struct hci_dev *hdev)
usb_autopm_put_interface(data->intf);
failed:
- usb_scuttle_anchored_urbs(&data->deferred);
+ btusb_free_deferred(data);
return 0;
}
@@ -5000,6 +5012,7 @@ static void play_deferred(struct btusb_data *data)
BT_ERR("%s urb %p submission failed (%d)",
data->hdev->name, urb, -err);
kfree(urb->setup_packet);
+ kfree_skb(urb->context);
usb_unanchor_urb(urb);
usb_free_urb(urb);
break;
@@ -5010,10 +5023,7 @@ static void play_deferred(struct btusb_data *data)
}
/* Cleanup the rest deferred urbs. */
- while ((urb = usb_get_from_anchor(&data->deferred))) {
- kfree(urb->setup_packet);
- usb_free_urb(urb);
- }
+ btusb_free_deferred(data);
}
static int btusb_resume(struct usb_interface *intf)
@@ -5073,7 +5083,7 @@ static int btusb_resume(struct usb_interface *intf)
return 0;
failed:
- usb_scuttle_anchored_urbs(&data->deferred);
+ btusb_free_deferred(data);
done:
spin_lock_irq(&data->txlock);
clear_bit(BTUSB_SUSPENDING, &data->flags);
--
2.56.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-30 16:27 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 6:47 [PATCH] Bluetooth: btusb: free skb and setup packet of deferred TX URBs Bilal Yazıcıoğlu
2026-11-30 16:27 ` bluez.test.bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox