From: "Bilal Yazıcıoğlu" <ahmetbilalyazicioglu@gmail.com>
To: linux-bluetooth@vger.kernel.org
Cc: "Marcel Holtmann" <marcel@holtmann.org>,
"Luiz Augusto von Dentz" <luiz.dentz@gmail.com>,
linux-kernel@vger.kernel.org,
"Bilal Yazıcıoğlu" <ahmetbilalyazicioglu@gmail.com>
Subject: [PATCH] Bluetooth: btusb: free skb and setup packet of deferred TX URBs
Date: Mon, 30 Nov 2026 09:47:51 +0300 [thread overview]
Message-ID: <20261130064751.50405-1-ahmetbilalyazicioglu@gmail.com> (raw)
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
next reply other threads:[~2026-11-30 6:47 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-30 6:47 Bilal Yazıcıoğlu [this message]
2026-11-30 16:27 ` Bluetooth: btusb: free skb and setup packet of deferred TX URBs bluez.test.bot
2026-11-30 20:00 ` [PATCH] " patchwork-bot+bluetooth
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261130064751.50405-1-ahmetbilalyazicioglu@gmail.com \
--to=ahmetbilalyazicioglu@gmail.com \
--cc=linux-bluetooth@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=luiz.dentz@gmail.com \
--cc=marcel@holtmann.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox