The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Praneeth Balasubramanian <praneethb2002@gmail.com>
To: Marcel Holtmann <marcel@holtmann.org>,
	Luiz Augusto von Dentz <luiz.dentz@gmail.com>
Cc: Matthias Brugger <matthias.bgg@gmail.com>,
	AngeloGioacchino Del Regno
	<angelogioacchino.delregno@collabora.com>,
	linux-bluetooth@vger.kernel.org,
	linux-mediatek@lists.infradead.org, linux-kernel@vger.kernel.org,
	Praneeth Balasubramanian <praneethb2002@gmail.com>,
	stable@vger.kernel.org
Subject: [PATCH 2/2] Bluetooth: btusb: mediatek: Use a PM reference when the firmware dumps
Date: Fri,  4 Dec 2026 10:50:17 -0800	[thread overview]
Message-ID: <20261204185017.1687516-2-praneethb2002@gmail.com> (raw)
In-Reply-To: <20261204185017.1687516-1-praneethb2002@gmail.com>

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


      reply	other threads:[~2026-12-04 18:50 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]

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=20261204185017.1687516-2-praneethb2002@gmail.com \
    --to=praneethb2002@gmail.com \
    --cc=angelogioacchino.delregno@collabora.com \
    --cc=linux-bluetooth@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mediatek@lists.infradead.org \
    --cc=luiz.dentz@gmail.com \
    --cc=marcel@holtmann.org \
    --cc=matthias.bgg@gmail.com \
    --cc=stable@vger.kernel.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