Bluetooth: Fix work queued while hci_dev_close_sync() drains
Subsystems (2)
A message that is not in the archive (sent off-list or not yet received): [email protected]
v2 cover Bluetooth: Fix work queued while hci_dev_close_sync() drains
syzbot reports "workqueue: cannot queue hci_conn_timeout on wq hci0" from hci_conn_drop() while hci_dev_close_sync() runs drain_workqueue().
v1 made hci_conn_drop() skip queueing disc_work while HCI_CMD_DRAIN_WORKQUEUE is set. Sashiko pointed out that this misses two cases: a connection delayed work whose timer was armed before close can still fire during the drain, and a connection created after hci_conn_hash_flush() is never deleted.
v2 drops the hci_conn_drop() change and fixes the cause instead:
- Patch 1 frees the unset handle when __hci_conn_add() fails. The leak already exists on the -ECONNREFUSED and -ENOMEM paths, and patch 2 adds another error return. Sashiko found it while reviewing patch 2.
- Patch 2 makes __hci_conn_add() refuse new connections once HCI_UP is cleared, and asserts that hdev->lock is held. I checked every caller and all of them hold the lock.
- Patch 3 disables the per-connection delayed works before drain_workqueue(), so neither the timers nor hci_conn_drop() can queue them during the drain. It depends on patch 2: without it, a connection added after the walk could still queue its works.
Testing: built on arm64 against bluetooth-next (da3cc02). With KASAN and lockdep enabled, the syzbot C reproducer was run under QEMU/HVF (2 vCPUs), one fresh boot per run:
without this series: 7/12 runs hit "workqueue: cannot queue
hci_conn_timeout on wq hci0"
with this series: 0/12 runs, no other warnings, KASAN or
lockdep reports
Sashiko reviewed the series locally with no critical, high or medium findings.
Claude (Opus 5.5) helped analyze the race, draft the patches and audit the __hci_conn_add() callers for hdev->lock. Sashiko reviewed v1 and this series.
Changes in v2:
- Drop the hci_conn_drop() change.
- Add patches 1 and 2.
- Disable the connection works before the drain (patch 3).
- Fixes tag changed from 525daaea459f to 76727c02c1e1.
v1: https://lore.kernel.org/all/[email protected]/
Jeremy Dean Clifton (3):
Bluetooth: hci_conn: Free the unset handle when adding a connection
fails
Bluetooth: hci_conn: Refuse new connections once HCI_UP is cleared
Bluetooth: hci_sync: Disable connection works before draining
net/bluetooth/hci_conn.c | 16 +++++++++++++++- net/bluetooth/hci_sync.c | 17 +++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-)
Signature · 1 line
2.43.0
PATCH v2 1/3 Bluetooth: hci_conn: Free the unset handle when adding a connection fails
hci_conn_add_unset() reserves a handle from hdev->unset_handle_ida and passes it to __hci_conn_add(). When __hci_conn_add() returns an error, no hci_conn exists, so hci_conn_cleanup() never runs and the handle is never returned to the IDA. It stays reserved until the device is released.
The Sashiko AI reviewer found this while reviewing a change that adds another error return to __hci_conn_add().
Free the handle in hci_conn_add_unset() when __hci_conn_add() fails.
--- net/bluetooth/hci_conn.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
net/bluetooth/hci_conn.c +6 -1
@@ -1176,6 +1176,7 @@ static struct hci_conn *__hci_conn_add(struct hci_dev *hdev, int type,struct hci_conn *hci_conn_add_unset (struct hci_dev *hdev,int type,bdaddr_t *dst,u8 dst_type,u8 role) { +struct hci_conn *conn;int handle;bt_dev_dbg (hdev,"dst %pMR" , dst);
@@ -1184,7 +1185,11 @@ struct hci_conn *hci_conn_add_unset(struct hci_dev *hdev, int type,if (unlikely (handle <0 ))return ERR_PTR (-ECONNREFUSED ); -return __hci_conn_add (hdev, type, dst, dst_type, role, handle); + conn =__hci_conn_add (hdev, type, dst, dst_type, role, handle); +if (IS_ERR (conn)) +ida_free (&hdev->unset_handle_ida, handle); + +return conn; } struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst,
Signature · 1 line
2.43.0
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/1213344/
---Test result---
Test Summary:
CheckPatch FAIL 1.65 seconds VerifyFixes PASS 0.11 seconds VerifySignedoff PASS 0.11 seconds GitLint FAIL 0.81 seconds SubjectPrefix PASS 0.30 seconds BuildKernel PASS 18.92 seconds CheckAllWarning PASS 21.45 seconds CheckSparse PASS 23.41 seconds BuildKernel32 PASS 19.56 seconds CheckKernelLLVM PASS 19.68 seconds TestRunnerSetup PASS 415.63 seconds TestRunner_l2cap-tester PASS 14.99 seconds TestRunner_iso-tester PASS 11.13 seconds TestRunner_bnep-tester PASS 2.91 seconds TestRunner_mgmt-tester PASS 83.86 seconds TestRunner_rfcomm-tester PASS 4.06 seconds TestRunner_sco-tester PASS 5.01 seconds TestRunner_ioctl-tester PASS 4.16 seconds TestRunner_mesh-tester PASS 5.08 seconds TestRunner_smp-tester PASS 4.01 seconds TestRunner_userchan-tester PASS 2.86 seconds TestRunner_6lowpan-tester PASS 4.02 seconds IncrementalBuild PASS 21.95 seconds
Details
##############################
Test: CheckPatch - FAIL
Desc: Run checkpatch.pl script
Output:
[v2,1/3] Bluetooth: hci_conn: Free the unset handle when adding a connection fails
WARNING: Non-standard signature: Assisted-by:
#135:
ERROR: Unrecognized email address: 'Claude:claude-opus-5-5' #135:
total: 1 errors, 1 warnings, 0 checks, 19 lines checked
NOTE: For some of the reported defects, checkpatch may be able to
mechanically convert to the typical style using --fix or --fix-inplace.
/home/runner/work/bluetooth-next/bluetooth-next/src/patch/14991326.patch has style problems, please review.
NOTE: Ignored message types: UNKNOWN_COMMIT_ID
NOTE: If any of the errors are false positives, please report
them to the maintainer, see CHECKPATCH in MAINTAINERS.
[v2,2/3] Bluetooth: hci_conn: Refuse new connections once HCI_UP is cleared
WARNING: Non-standard signature: Assisted-by:
#139:
ERROR: Unrecognized email address: 'Claude:claude-opus-5-5' #139:
total: 1 errors, 1 warnings, 0 checks, 15 lines checked
NOTE: For some of the reported defects, checkpatch may be able to
mechanically convert to the typical style using --fix or --fix-inplace.
/home/runner/work/bluetooth-next/bluetooth-next/src/patch/14991327.patch has style problems, please review.
NOTE: Ignored message types: UNKNOWN_COMMIT_ID
NOTE: If any of the errors are false positives, please report
them to the maintainer, see CHECKPATCH in MAINTAINERS.
[v2,3/3] Bluetooth: hci_sync: Disable connection works before draining
WARNING: Non-standard signature: Assisted-by:
#139:
ERROR: Unrecognized email address: 'Claude:claude-opus-5-5' #139:
total: 1 errors, 1 warnings, 0 checks, 29 lines checked
NOTE: For some of the reported defects, checkpatch may be able to
mechanically convert to the typical style using --fix or --fix-inplace.
/home/runner/work/bluetooth-next/bluetooth-next/src/patch/14991328.patch has style problems, please review.
NOTE: Ignored message types: UNKNOWN_COMMIT_ID
NOTE: If any of the errors are false positives, please report
them to the maintainer, see CHECKPATCH in MAINTAINERS.
##############################
Test: GitLint - FAIL
Desc: Run gitlint
Output:
[v2,1/3] Bluetooth: hci_conn: Free the unset handle when adding a connection fails
1: T1 Title exceeds max length (82>80): "[v2,1/3] Bluetooth: hci_conn: Free the unset handle when adding a connection fails"
https://github.com/bluez/bluetooth-next/pull/1142
---
Regards,
Linux Bluetooth
PATCH v2 2/3 Bluetooth: hci_conn: Refuse new connections once HCI_UP is cleared
In reply to Jeremy Dean Cliftonhci_dev_close_sync() clears HCI_UP and later calls hci_conn_hash_flush() to delete every connection. A connection created by __hci_conn_add() after the flush has run is never deleted. It keeps the hci_dev_hold() reference and the sysfs device taken in __hci_conn_add(), so the hci_dev is never freed, and its works can still be queued after the workqueue is drained.
Check HCI_UP in __hci_conn_add(), which serves both hci_conn_add() and hci_conn_add_unset(). All callers hold hdev->lock: the HCI event handlers, create_pa_complete() in hci_sync.c, and the l2cap, sco, iso and mgmt paths that reach the hci_connect_*() and hci_bind_*() helpers. hci_conn_hash_flush() runs under hdev->lock too, so a connection is either added before the flush and deleted by it, or refused.
Also include lockdep_assert_held() so the locking rule is checked.
--- net/bluetooth/hci_conn.c | 9 +++++++++ 1 file changed, 9 insertions(+)
net/bluetooth/hci_conn.c +9 -0
@@ -1001,6 +1001,15 @@ static struct hci_conn *__hci_conn_add(struct hci_dev *hdev, int type,struct hci_conn *conn;struct smp_irk *irk =NULL ; +lockdep_assert_held (&hdev->lock); + +/* hci_dev_close_sync() clears HCI_UP before hci_conn_hash_flush(). +* A connection added after that would never be flushed, so refuse +* it. The caller holds hdev->lock, which the flush also takes. +*/ +if (!test_bit (HCI_UP , &hdev->flags)) +return ERR_PTR (-ENETDOWN ); +switch (type) {case ACL_LINK :if (!hdev->acl_mtu)
Signature · 1 line
2.43.0
PATCH v2 3/3 Bluetooth: hci_sync: Disable connection works before draining
In reply to Jeremy Dean CliftonA connection delayed work whose timer was armed before hci_dev_close_sync() can expire while drain_workqueue() runs. delayed_work_timer_fn() then queues it on the draining workqueue and triggers the "cannot queue ... on wq" warning. hci_conn_drop() can also queue disc_work from another context during the drain.
Disable the per-connection delayed works under hdev->lock before draining. A disabled work is cancelled and any later attempt to queue it fails, so neither the timers nor hci_conn_drop() can queue it. The connections are deleted by hci_conn_hash_flush() below, so nothing needs to re-enable the works.
--- net/bluetooth/hci_sync.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
net/bluetooth/hci_sync.c +17 -0
@@ -6012,6 +6012,7 @@ static int hci_dev_shutdown(struct hci_dev *hdev)int hci_dev_close_sync (struct hci_dev *hdev) { +struct hci_conn *conn;bool auto_off;int err =0 ;
@@ -6079,6 +6080,22 @@ int hci_dev_close_sync(struct hci_dev *hdev)cancel_delayed_work_sync (&adv_instance->rpa_expired_cb); } +/* A delayed work whose timer was armed before close would queue +* itself while the workqueue is draining. Disable the connection +* works so that hci_conn_drop() and the timers can no longer queue +* them. The connections are deleted below, so nothing re-enables +* them. HCI_UP is clear, so no new connection can be added after +* this walk. +*/ +hci_dev_lock (hdev); +list_for_each_entry (conn, &hdev->conn_hash.list, list) { +disable_delayed_work (&conn->disc_work); +disable_delayed_work (&conn->auto_accept_work); +disable_delayed_work (&conn->idle_work); +disable_delayed_work (&conn->le_conn_timeout); + } +hci_dev_unlock (hdev); +/* Avoid potential lockdep warnings from the *_flush() calls by * ensuring the workqueue is empty up front. */
Signature · 1 line
2.43.0
This series was applied to bluetooth/bluetooth-next.git (master) by Luiz Augusto von Dentz <[email protected]>:
Hello:
This series was applied to bluetooth/bluetooth-next.git (master) by Luiz Augusto von Dentz <[email protected]>:
Quoted 10 lines · On Tue, 17 Nov 2026 02:33:41 +0000 you wrote:
syzbot reports "workqueue: cannot queue
hci_conn_timeouton wq hci0" fromhci_conn_drop()whilehci_dev_close_sync()runsdrain_workqueue().v1 made
hci_conn_drop()skip queueingdisc_workwhileHCI_CMD_DRAIN_WORKQUEUEis set. Sashiko pointed out that this misses two cases: a connection delayed work whose timer was armed before close can still fire during the drain, and a connection created afterhci_conn_hash_flush()is never deleted.[...]
Here is the summary with links:
- [v2,1/3] Bluetooth: hci_conn: Free the unset handle when adding a connection fails
https://git.kernel.org/bluetooth/bluetooth-next/c/1f150a0bb8a1 - [v2,2/3] Bluetooth: hci_conn: Refuse new connections once HCI_UP is cleared https://git.kernel.org/bluetooth/bluetooth-next/c/56abe5c17b4d - [v2,3/3] Bluetooth: hci_sync: Disable connection works before draining https://git.kernel.org/bluetooth/bluetooth-next/c/1ea7a0946b9e
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html