Linux bluetooth development
 help / color / mirror / Atom feed
* [RFC PATCH] Bluetooth: btusb: Try the device lock when the CSR quirk re-enables autosuspend
@ 2026-12-04 18:50 Praneeth Balasubramanian
  0 siblings, 0 replies; only message in thread
From: Praneeth Balasubramanian @ 2026-12-04 18:50 UTC (permalink / raw)
  To: Marcel Holtmann, Luiz Augusto von Dentz
  Cc: Hans de Goede, linux-bluetooth, linux-kernel,
	Praneeth Balasubramanian, stable

btusb_setup_csr() ends its workaround for fake CSR controllers by
calling usb_enable_autosuspend() when btusb was loaded with
enable_autosuspend. usb_enable_autosuspend() changes the policy that
user space sets through power/control, and its kernel-doc says that the
caller must hold the udev device lock, which serializes it with writes
to power/control. btusb_setup_csr() runs from the HCI power-on work
without that lock.

A plain usb_lock_device() would deadlock: USB interface drivers are
unbound with the parent device locked, and btusb_disconnect() ->
hci_unregister_dev() waits for the power-on work with
disable_work_sync(). So take the lock with usb_trylock_device(). If it
is busy, which mostly means that the device is going away, leave
autosuspend off and warn; it can still be enabled through power/control.

pm_runtime_allow() and pm_runtime_forbid(), which the workaround calls
before, have no such requirement and are left as they are.

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: 0671c0662383 ("Bluetooth: btusb: Add workaround for remote-wakeup issues with Barrot 8041a02 fake CSR controllers")
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 | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/drivers/bluetooth/btusb_main.c b/drivers/bluetooth/btusb_main.c
index 95398e2ca098..d1d4b9a70606 100644
--- a/drivers/bluetooth/btusb_main.c
+++ b/drivers/bluetooth/btusb_main.c
@@ -2838,9 +2838,19 @@ static int btusb_setup_csr(struct hci_dev *hdev)
 
 		device_set_wakeup_capable(&data->udev->dev, false);
 
-		/* Re-enable autosuspend if this was requested */
-		if (enable_autosuspend)
-			usb_enable_autosuspend(data->udev);
+		/*
+		 * Re-enable autosuspend if this was requested. This needs the
+		 * device lock, but btusb_disconnect() holds it while it waits
+		 * for the power-on work that runs this setup, so only try it.
+		 */
+		if (enable_autosuspend) {
+			if (usb_trylock_device(data->udev)) {
+				usb_enable_autosuspend(data->udev);
+				usb_unlock_device(data->udev);
+			} else {
+				bt_dev_warn(hdev, "CSR: Couldn't re-enable autosuspend");
+			}
+		}
 	}
 
 	kfree_skb(skb);
-- 
2.25.1


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-12-04 18:50 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 18:50 [RFC PATCH] Bluetooth: btusb: Try the device lock when the CSR quirk re-enables autosuspend Praneeth Balasubramanian

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