Linux-mediatek Archive on 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
  0 siblings, 1 reply; 2+ 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] 2+ messages in thread

* [PATCH 2/2] Bluetooth: btusb: mediatek: Use a PM reference when the firmware dumps
  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 ` Praneeth Balasubramanian
  0 siblings, 0 replies; 2+ 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

btmtk_usb_recv_acl() calls usb_disable_autosuspend() for every firmware
dump packet (handle 0xfc6f), because a hung firmware can no longer
suspend. usb_disable_autosuspend() changes the autosuspend policy that
user space sets through power/control, and its kernel-doc says that the
caller must hold the udev device lock. btmtk_usb_recv_acl() runs in the
receive path, from URB completion, where that lock cannot be taken.

Keep the device awake with a runtime PM reference on the interface
instead, using the BTUSB_DUMP_AWAKE flag from the previous patch. btusb
now takes the reference on the first dump packet, in a small wrapper
around btmtk_usb_recv_acl(), and btusb_disconnect() drops it when the
device is unbound, for example after a USB reset. As before, the device
stays awake until then, but the user's autosuspend setting is left
alone. btusb is the only user of btmtk_usb_recv_acl().

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: 0b10c8c84c0c ("Bluetooth: btusb: Record debug log for Mediatek Chip.")
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/btmtk.c      |  6 +-----
 drivers/bluetooth/btusb_main.c | 19 ++++++++++++++++++-
 2 files changed, 19 insertions(+), 6 deletions(-)

diff --git a/drivers/bluetooth/btmtk.c b/drivers/bluetooth/btmtk.c
index 1b3dab0d10bb..d868e7ba4745 100644
--- a/drivers/bluetooth/btmtk.c
+++ b/drivers/bluetooth/btmtk.c
@@ -1439,7 +1439,6 @@ EXPORT_SYMBOL_GPL(btmtk_usb_subsys_reset);
 
 int btmtk_usb_recv_acl(struct hci_dev *hdev, struct sk_buff *skb)
 {
-	struct btmtk_data *data = hci_get_priv(hdev);
 	u16 handle = le16_to_cpu(hci_acl_hdr(skb)->handle);
 
 	/* The handles below are vendor-reserved values MTK firmware uses to
@@ -1451,10 +1450,7 @@ int btmtk_usb_recv_acl(struct hci_dev *hdev, struct sk_buff *skb)
 	 */
 	switch (handle) {
 	case 0xfc6f:		/* Firmware dump from device */
-		/* When the firmware hangs, the device can no longer
-		 * suspend and thus disable auto-suspend.
-		 */
-		usb_disable_autosuspend(data->udev);
+		/* btusb keeps the device awake, see btusb_recv_acl_mtk() */
 
 		/* We need to forward the diagnostic packet to userspace daemon
 		 * for backward compatibility, so we have to clone the packet
diff --git a/drivers/bluetooth/btusb_main.c b/drivers/bluetooth/btusb_main.c
index 468415530e3e..088bea4bb594 100644
--- a/drivers/bluetooth/btusb_main.c
+++ b/drivers/bluetooth/btusb_main.c
@@ -3063,6 +3063,23 @@ static void btusb_mtk_release_iso_intf(struct hci_dev *hdev)
 	clear_bit(BTMTK_ISOPKT_OVER_INTR, &btmtk_data->flags);
 }
 
+static int btusb_recv_acl_mtk(struct hci_dev *hdev, struct sk_buff *skb)
+{
+	struct btusb_data *data = hci_get_drvdata(hdev);
+
+	/* When the firmware hangs, it sends a dump on handle 0xfc6f and the
+	 * device can no longer suspend. Keep it awake until it is reset and
+	 * rebound; btusb_disconnect() drops the reference. This runs in URB
+	 * completion context, so hold a PM reference rather than change the
+	 * autosuspend policy, which needs the device lock.
+	 */
+	if (le16_to_cpu(hci_acl_hdr(skb)->handle) == 0xfc6f &&
+	    !test_and_set_bit(BTUSB_DUMP_AWAKE, &data->flags))
+		usb_autopm_get_interface_no_resume(data->intf);
+
+	return btmtk_usb_recv_acl(hdev, skb);
+}
+
 static int btusb_mtk_disconnect(struct hci_dev *hdev)
 {
 	/* This function describes the specific additional steps taken by MediaTek
@@ -4628,7 +4645,7 @@ static int btusb_probe(struct usb_interface *intf,
 		hdev->send = btusb_send_frame_mtk;
 		hci_set_quirk(hdev, HCI_QUIRK_BROKEN_ENHANCED_SETUP_SYNC_CONN);
 		hci_set_quirk(hdev, HCI_QUIRK_NON_PERSISTENT_SETUP);
-		data->recv_acl = btmtk_usb_recv_acl;
+		data->recv_acl = btusb_recv_acl_mtk;
 		data->recv_event = btmtk_recv_event;
 		data->suspend = btmtk_usb_suspend;
 		data->resume = btmtk_usb_resume;
-- 
2.25.1



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

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

Thread overview: 2+ 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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox