* [PATCH v2] Bluetooth: hci_sysfs: Wait for setup to finish before system sleep
@ 2026-12-01 9:48 Jens Willmer
2026-12-02 3:08 ` [v2] " bluez.test.bot
0 siblings, 1 reply; 2+ messages in thread
From: Jens Willmer @ 2026-12-01 9:48 UTC (permalink / raw)
To: linux-bluetooth; +Cc: marcel, luiz.dentz
The driver setup runs in hci_power_on() before the device is powered.
hci_suspend_dev() returns early for a device that is not powered, so
nothing waits for a setup in flight when the system goes to sleep, and
the driver aborts it in its suspend callback.
For a Realtek USB controller this can leave the controller unusable
until it is power cycled. It loses its firmware in system sleep, so
when wakeup is not enabled btusb sets reset_resume, is probed again on
resume and downloads the firmware from hci_power_on(). On an RTL8852CU
(0bda:0852) this takes about two seconds. If the system goes to sleep
again during that time, for example when systemd's
suspend-then-hibernate fails to hibernate and suspends again right
away, btusb_suspend() kills the pending command:
PM: suspend exit
PM: suspend entry (deep)
Bluetooth: hci0: RTL: download fw command failed (-2)
After resume from S3 the controller kept the partial download and a
USB reset did not clear it. Every command timed out, and the device was
reset and probed in a loop until it was power cycled:
Bluetooth: hci0: command 0xfc20 tx timeout
Bluetooth: hci0: Resetting usb device.
Bluetooth: hci1: command 0xfc61 tx timeout
Bluetooth: hci1: RTL: Failed to generate devcoredump
Give the hci device a ->prepare PM callback that, for a device that is
not up, waits for the work queued on hdev->req_workqueue so far.
dpm_prepare() runs after user space is frozen and after it has waited
for and blocked device probing, and before any device is suspended.
This covers the setup run from hdev->power_on, including that of a
controller probed again during the transition, and a setup that
HCI_QUIRK_NON_PERSISTENT_SETUP repeats from hdev->cmd_sync_work.
The wait is bounded to 10 seconds, after which the transition goes on
as before. A setup can depend on kernel threads, which are already
frozen at this point on suspend, for example when it reads its firmware
from a file system that completes reads on a freezable workqueue, and a
controller that does not respond can make a setup take longer. Neither
should hang or prevent system sleep. Without work in flight the wait
returns at once.
Fixes: 04b8c8143d46 ("Bluetooth: btusb: fix Realtek suspend/resume")
Cc: stable@vger.kernel.org
Signed-off-by: Jens Willmer <info@jwillmer.de>
---
Notes:
Changes in v2:
- The wait moved from a new PM notifier in btusb to a ->prepare PM
callback of the hci device. It runs after user space is frozen and
device probing is blocked, so work queued after the old notifier
(hibernation thaw, a device probed during the transition, mgmt) is
waited for as well, and so is a setup that
HCI_QUIRK_NON_PERSISTENT_SETUP repeats from cmd_sync_work, which runs
on the same workqueue. This answers the netdev Sashiko findings.
- The wait is bounded (10 s); when it expires the transition goes on
as without the wait, so a setup that cannot progress while kernel
threads are frozen, or a controller that does not respond, cannot
hang or prevent system sleep. Devices that are up are not waited for.
- Commit message rewritten for the new place of the wait.
v1: https://lore.kernel.org/linux-bluetooth/VE1PR08MB5247BE2C05F27B1A2675FA25D5712@VE1PR08MB5247.eurprd08.prod.outlook.com/
Not covered: a Hardware Error event that arrives after the wait queues
error_reset, which reopens the device and, with
HCI_QUIRK_NON_PERSISTENT_SETUP, runs the setup again. The same is true
without this patch. With uswsusp, SNAPSHOT_POWER_OFF does not require
frozen tasks, so user space can still queue work there.
A setup that completes during the wait leaves the device up with
HCI_AUTO_OFF set, so hdev->power_off runs 2 s later, possibly while the
transport is suspending; after a reset_resume the device is unbound on
resume anyway. Waits for several controllers add up, one after another.
On the review's suggestion to wait in btusb_suspend() instead: at that
point other devices, including the disk the firmware is read from, may
already be suspended, which can stall the suspend; in the prepare phase
no device is suspended yet. An error from a USB interface suspend
callback also does not stop system suspend.
On the locking report from sashiko.dev for v1: this version registers no
notifier and does not wait under the PM notifier chain. The wait runs
under the device lock of the hci device, which the PM core holds for
->prepare. No work on req_workqueue takes that lock or unregisters the
device, so the work waited for does not depend on it. A concurrent
hci_unregister_dev() disables hdev->power_on and then blocks in
device_del() until the wait has ended.
Lockdep does not validate device locks (novalidate class), so the
lockdep run below does not cover this; the argument rests on reading
the code.
Testing:
1) This patch on bluetooth-next (f57f78086ab5), i386, btusb built in,
CONFIG_PROVE_LOCKING=y, in a QEMU/KVM guest with the RTL8852CU
(0bda:0852) passed through from the host to an emulated xHCI
controller. /sys/power/pm_test set to "devices", so the devices are
suspended and resumed but the controller keeps its power. The kernel
is unchanged apart from the patch; kprobes give the times. btusb sets
reset_resume, and the emulated xHCI also reports a reset on resume, so
btusb is probed again after each resume and downloads the firmware.
A second suspend was started about 0.2 s after the first one
returned, three times. Each time ->prepare waited 1.94 s for the
setup, the download completed and the controller came up afterwards:
[ 23.190288] Bluetooth: hci0: RTL: cfg_sz 6, total sz 71983
[ 23.194064] PM: suspend entry (deep)
[ 23.210417] Freezing remaining freezable tasks completed
[ 23.214579] bt_host_prepare_enter
[ 24.983204] Bluetooth: hci0: RTL: fw version 0x040d7225
[ 25.156506] hci_power_on_exit
[ 25.157635] bt_host_prepare_exit ret=0x0
[ 25.164565] btusb_suspend_enter
With the controller up and idle ->prepare returned within 1 ms. No
lockdep report and no warning in the run.
The same test with the same tree and config without the patch:
[ 23.186671] Bluetooth: hci0: RTL: cfg_sz 6, total sz 71983
[ 23.191414] PM: suspend entry (deep)
[ 23.208155] Freezing remaining freezable tasks completed
[ 23.217676] btusb_suspend_enter
[ 23.219678] Bluetooth: hci0: RTL: download fw command failed (-2)
[ 29.312178] PM: suspend exit
[ 31.311629] Bluetooth: hci0: command 0xfc61 tx timeout
[ 31.311664] Bluetooth: hci0: RTL: Failed to generate devcoredump
[ 31.311679] Bluetooth: hci0: Resetting usb device.
The last three lines repeated until the run ended 30 s later. Given
back to the host afterwards, the controller did not come up there
either until the machine was powered off.
2) The same kernel with hci_vhci instead of the real controller, same
guest setup. Five cycles as in 1) with hci_power_on() in flight (an
HCI reset that times out; three "mem", one "disk") or with no work:
->prepare waited 1.8 s, or 2 ms without work. Timeout path, with a
command queued on cmd_sync_work that keeps the workqueue busy for
about 20 s: after 10.4 s "setup still running after 10000 ms,
suspending anyway", the transition completed, and the next cycle
waited for the rest of that work (5.9 s). Two runs, no lockdep report,
no warning, no hung task. Not exercised: a barrier still queued from a
timed out wait when the next wait starts; in the run it had been
cancelled.
Not tested with this version: a real system sleep (suspend to RAM,
suspend-to-idle, hibernation with image and power off), the restore
path, CONFIG_DPM_WATCHDOG, and controllers other than the RTL8852CU.
An earlier version of this patch, with an unbounded flush in
->prepare, was run on kernel 7.2.15 with two RTC-woken suspend-to-idle
cycles back to back and waited in the same way. That the controller
stays unusable until it is power cycled was seen after S3 with the
unpatched kernel and in the v1 tests, and after the control run in 1).
Tool use: the analysis of the logs, the patch and this text were
written with the AI assistant Claude (Anthropic), working from my
problem report and logs on my machine. I reviewed the change and ran
the tests above on my hardware.
include/net/bluetooth/hci_core.h | 2 ++
net/bluetooth/hci_sysfs.c | 56 ++++++++++++++++++++++++++++++++
2 files changed, 58 insertions(+)
diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h
index 4aa0b49ae..dabfe09d2 100644
--- a/include/net/bluetooth/hci_core.h
+++ b/include/net/bluetooth/hci_core.h
@@ -511,6 +511,8 @@ struct hci_dev {
struct work_struct power_on;
struct delayed_work power_off;
+ struct work_struct pm_barrier;
+ struct completion pm_barrier_done;
struct work_struct error_reset;
struct work_struct cmd_sync_work;
struct list_head cmd_sync_work_list;
diff --git a/net/bluetooth/hci_sysfs.c b/net/bluetooth/hci_sysfs.c
index c2065abf7..f59e72233 100644
--- a/net/bluetooth/hci_sysfs.c
+++ b/net/bluetooth/hci_sysfs.c
@@ -123,10 +123,63 @@ static struct attribute *bt_host_attrs[] = {
};
ATTRIBUTE_GROUPS(bt_host);
+/* Upper bound for the wait in bt_host_prepare(). A setup normally ends
+ * within a few seconds.
+ */
+#define BT_HOST_PREPARE_TIMEOUT_MS 10000
+
+static void bt_host_pm_barrier(struct work_struct *work)
+{
+ struct hci_dev *hdev = container_of(work, struct hci_dev, pm_barrier);
+
+ complete(&hdev->pm_barrier_done);
+}
+
+static int bt_host_prepare(struct device *dev)
+{
+ struct hci_dev *hdev = to_hci_dev(dev);
+ unsigned long timeout = msecs_to_jiffies(BT_HOST_PREPARE_TIMEOUT_MS);
+
+ /* The driver setup (e.g. a firmware download) runs from
+ * hdev->req_workqueue before the device is up, so hci_suspend_dev()
+ * does not wait for it and the driver aborts it when it suspends.
+ * A device that is up has no setup in flight.
+ */
+ if (test_bit(HCI_UP, &hdev->flags))
+ return 0;
+
+ /* User space is frozen and probing is blocked here, so wait for
+ * the work queued so far. The workqueue is ordered, so the barrier
+ * runs after it. A barrier left from an earlier timed out wait is
+ * retired first, so that it cannot end this wait.
+ *
+ * The wait is bounded: kernel threads may be frozen already and a
+ * setup can depend on them (e.g. a firmware read from a file system
+ * that completes reads on a freezable workqueue), or a controller
+ * may not respond. Then go on as without the wait.
+ */
+ cancel_work_sync(&hdev->pm_barrier);
+ reinit_completion(&hdev->pm_barrier_done);
+ queue_work(hdev->req_workqueue, &hdev->pm_barrier);
+
+ if (!wait_for_completion_timeout(&hdev->pm_barrier_done, timeout)) {
+ cancel_work(&hdev->pm_barrier);
+ bt_dev_warn(hdev, "setup still running after %u ms, suspending anyway",
+ BT_HOST_PREPARE_TIMEOUT_MS);
+ }
+
+ return 0;
+}
+
+static const struct dev_pm_ops bt_host_pm_ops = {
+ .prepare = pm_sleep_ptr(bt_host_prepare),
+};
+
static const struct device_type bt_host = {
.name = "host",
.release = bt_host_release,
.groups = bt_host_groups,
+ .pm = pm_sleep_ptr(&bt_host_pm_ops),
};
void hci_init_sysfs(struct hci_dev *hdev)
@@ -136,6 +189,9 @@ void hci_init_sysfs(struct hci_dev *hdev)
dev->type = &bt_host;
dev->class = &bt_class;
+ INIT_WORK(&hdev->pm_barrier, bt_host_pm_barrier);
+ init_completion(&hdev->pm_barrier_done);
+
__module_get(THIS_MODULE);
device_initialize(dev);
}
--
2.43.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* RE: [v2] Bluetooth: hci_sysfs: Wait for setup to finish before system sleep
2026-12-01 9:48 [PATCH v2] Bluetooth: hci_sysfs: Wait for setup to finish before system sleep Jens Willmer
@ 2026-12-02 3:08 ` bluez.test.bot
0 siblings, 0 replies; 2+ messages in thread
From: bluez.test.bot @ 2026-12-02 3:08 UTC (permalink / raw)
To: linux-bluetooth, info
[-- Attachment #1: Type: text/plain, Size: 1884 bytes --]
This is automated email and please do not reply to this email!
Dear submitter,
Thank you for submitting the patches to the linux bluetooth mailing list.
This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/series/1227920/
---Test result---
Test Summary:
CheckPatch PASS 0.92 seconds
VerifyFixes PASS 0.08 seconds
VerifySignedoff PASS 0.08 seconds
GitLint PASS 0.25 seconds
SubjectPrefix PASS 0.07 seconds
BuildKernel PASS 31.53 seconds
CheckAllWarning PASS 35.27 seconds
CheckSparse PASS 38.04 seconds
BuildKernel32 PASS 31.05 seconds
CheckKernelLLVM PASS 32.39 seconds
TestRunnerSetup PASS 751.93 seconds
TestRunner_l2cap-tester FAIL 14.96 seconds
TestRunner_iso-tester PASS 13.43 seconds
TestRunner_bnep-tester PASS 3.14 seconds
TestRunner_mgmt-tester PASS 89.46 seconds
TestRunner_rfcomm-tester PASS 4.49 seconds
TestRunner_sco-tester PASS 5.62 seconds
TestRunner_ioctl-tester PASS 4.67 seconds
TestRunner_mesh-tester PASS 4.51 seconds
TestRunner_smp-tester PASS 4.38 seconds
TestRunner_userchan-tester PASS 3.21 seconds
TestRunner_6lowpan-tester PASS 4.45 seconds
IncrementalBuild PASS 29.56 seconds
Details
##############################
Test: TestRunner_l2cap-tester - FAIL
Desc: Run l2cap-tester with test-runner
Output:
Total: 144, Passed: 143 (99.3%), Failed: 1, Not Run: 0
Failed Test Cases
L2CAP BR/EDR Client SSP - Success 2 Failed 0.026 seconds
https://github.com/bluez/bluetooth-next/pull/1269
---
Regards,
Linux Bluetooth
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-02 3:08 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01 9:48 [PATCH v2] Bluetooth: hci_sysfs: Wait for setup to finish before system sleep Jens Willmer
2026-12-02 3:08 ` [v2] " bluez.test.bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox