All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] Bluetooth: btusb: Use a PM reference during QCA memory dumps
@ 2026-12-04 18:50 Praneeth Balasubramanian
  2026-12-04 18:50 ` [PATCH 2/2] Bluetooth: btusb: mediatek: Use a PM reference when the firmware dumps Praneeth Balasubramanian
  2026-12-04 22:16 ` [1/2] Bluetooth: btusb: Use a PM reference during QCA memory dumps bluez.test.bot
  0 siblings, 2 replies; 3+ messages in thread
From: Praneeth Balasubramanian @ 2026-12-04 18:50 UTC (permalink / raw)
  To: Marcel Holtmann, Luiz Augusto von Dentz
  Cc: Matthias Brugger, AngeloGioacchino Del Regno, linux-bluetooth,
	linux-mediatek, linux-kernel, Praneeth Balasubramanian, stable

handle_dump_pkt_qca() calls usb_disable_autosuspend() when a QCA memory
dump starts and usb_enable_autosuspend() when it ends. Both change the
autosuspend policy that user space sets through power/control, and
their kernel-doc says that the caller must hold the udev device lock.
handle_dump_pkt_qca() runs in the receive path, from URB completion,
where that lock cannot be taken. The enable at the end is also
unconditional: after a dump, autosuspend is on even if the user set
power/control to "on" or loaded btusb with enable_autosuspend=0.

Keep the device awake with a runtime PM reference on the interface
instead, as the reset path does with BTUSB_RESET: take it with
usb_autopm_get_interface_no_resume() when the dump starts and drop it
with usb_autopm_put_interface_no_suspend() when it ends. Both may be
called in atomic context. A new BTUSB_DUMP_AWAKE flag makes sure that
the reference is taken only once, and btusb_disconnect() drops it if a
dump never completed, since the USB core does not reset the usage count
of an unbound interface.

The missing lock was found by a static check that turns the locking rule
in a function's comment into a Clang context-analysis requirement on its
callers.

Fixes: 20981ce2d5a5 ("Bluetooth: btusb: Add WCN6855 devcoredump support")
Cc: stable@vger.kernel.org
Signed-off-by: Praneeth Balasubramanian <praneethb2002@gmail.com>
---

Notes:
    This was found by a compiler-based check, part of research at UCSB SecLab.
    The locking rule stated in the kernel-doc comment of the called function
    was turned into a Clang context-analysis requirement (__must_hold) on that
    function, and Clang 23 with CONFIG_WARN_CONTEXT_ANALYSIS then checked every
    caller. An LLM was used only to turn the comment into a machine-readable
    rule; the report itself comes from the compiler. The patch and changelog
    were drafted with Claude Opus 5.5. I reviewed the patch and take
    responsibility for it.
    
    Testing: builds without new warnings with W=1 (x86_64, clang 23,
    allmodconfig). Not runtime-tested; I do not have this hardware, and I
    have not observed the race myself.

 drivers/bluetooth/btusb_main.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/drivers/bluetooth/btusb_main.c b/drivers/bluetooth/btusb_main.c
index 95398e2ca098..468415530e3e 100644
--- a/drivers/bluetooth/btusb_main.c
+++ b/drivers/bluetooth/btusb_main.c
@@ -1016,6 +1016,7 @@ struct btqca_data {
 #define BTUSB_HW_SSR_ACTIVE	17
 #define BTUSB_WAKEUP_BROKEN	18
 #define BTUSB_RESET		19
+#define BTUSB_DUMP_AWAKE	20
 
 #define BTUSB_PROTO_LEGACY	0x00
 #define BTUSB_PROTO_H4		0x01
@@ -3412,7 +3413,6 @@ static int handle_dump_pkt_qca(struct hci_dev *hdev, struct sk_buff *skb)
 	struct btusb_data *btdata = hci_get_drvdata(hdev);
 	struct btqca_data *btqca_data = hci_get_priv(hdev);
 	struct qca_dump_info *qca_dump_ptr = &btqca_data->qca_dump;
-	struct usb_device *udev = btdata->udev;
 
 	pkt_type = hci_skb_pkt_type(skb);
 	skip = sizeof(struct hci_event_hdr);
@@ -3444,7 +3444,12 @@ static int handle_dump_pkt_qca(struct hci_dev *hdev, struct sk_buff *skb)
 
 		skb_pull(skb, offsetof(struct qca_dump_hdr, data0));
 
-		usb_disable_autosuspend(udev);
+		/* Keep the device awake until the dump is done. This runs in
+		 * URB completion context, so hold a PM reference rather than
+		 * change the autosuspend policy, which needs the device lock.
+		 */
+		if (!test_and_set_bit(BTUSB_DUMP_AWAKE, &btdata->flags))
+			usb_autopm_get_interface_no_resume(btdata->intf);
 		bt_dev_info(hdev, "%s memdump size(%u)\n",
 			    (pkt_type == HCI_ACLDATA_PKT) ? "ACL" : "event",
 			    dump_size);
@@ -3482,8 +3487,8 @@ static int handle_dump_pkt_qca(struct hci_dev *hdev, struct sk_buff *skb)
 	return ret;
 
 out:
-	if (qca_dump_ptr->ram_dump_size)
-		usb_enable_autosuspend(udev);
+	if (test_and_clear_bit(BTUSB_DUMP_AWAKE, &btdata->flags))
+		usb_autopm_put_interface_no_suspend(btdata->intf);
 	qca_dump_ptr->ram_dump_size = 0;
 	qca_dump_ptr->ram_dump_seqno = 0;
 	clear_bit(BTUSB_HW_SSR_ACTIVE, &btdata->flags);
@@ -4893,6 +4898,10 @@ static void btusb_disconnect(struct usb_interface *intf)
 	if (test_and_clear_bit(BTUSB_RESET, &data->flags))
 		usb_autopm_put_interface_no_suspend(data->intf);
 
+	/* Drop the reference of a firmware dump that did not complete */
+	if (test_and_clear_bit(BTUSB_DUMP_AWAKE, &data->flags))
+		usb_autopm_put_interface_no_suspend(data->intf);
+
 	if (intf == data->intf) {
 		if (data->isoc)
 			usb_driver_release_interface(&btusb_driver, data->isoc);
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-04 22:16 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 18:50 [PATCH 1/2] Bluetooth: btusb: Use a PM reference during QCA memory dumps Praneeth Balasubramanian
2026-12-04 18:50 ` [PATCH 2/2] Bluetooth: btusb: mediatek: Use a PM reference when the firmware dumps Praneeth Balasubramanian
2026-12-04 22:16 ` [1/2] Bluetooth: btusb: Use a PM reference during QCA memory dumps bluez.test.bot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.