Linux bluetooth development
 help / color / mirror / Atom feed
* [PATCH v2 0/3] Bluetooth: Fix work queued while hci_dev_close_sync() drains
       [not found] <20261117023344.278269-1-deaner92.ref@yahoo.com>
@ 2026-11-17  2:33 ` Jeremy Dean Clifton
  2026-11-17  2:33   ` [PATCH v2 1/3] Bluetooth: hci_conn: Free the unset handle when adding a connection fails Jeremy Dean Clifton
                     ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Jeremy Dean Clifton @ 2026-11-17  2:33 UTC (permalink / raw)
  To: marcel, luiz.dentz
  Cc: linux-bluetooth, linux-kernel, william.xuanziyang, johan.hedberg,
	hdanton, Jeremy Dean Clifton

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/20261020145320.115333-1-deaner92@yahoo.com/

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(-)

-- 
2.43.0


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

* [PATCH v2 1/3] Bluetooth: hci_conn: Free the unset handle when adding a connection fails
  2026-11-17  2:33 ` [PATCH v2 0/3] Bluetooth: Fix work queued while hci_dev_close_sync() drains Jeremy Dean Clifton
@ 2026-11-17  2:33   ` Jeremy Dean Clifton
  2026-11-17 11:23     ` Bluetooth: Fix work queued while hci_dev_close_sync() drains bluez.test.bot
  2026-11-17  2:33   ` [PATCH v2 2/3] Bluetooth: hci_conn: Refuse new connections once HCI_UP is cleared Jeremy Dean Clifton
  2026-11-17  2:33   ` [PATCH v2 3/3] Bluetooth: hci_sync: Disable connection works before draining Jeremy Dean Clifton
  2 siblings, 1 reply; 5+ messages in thread
From: Jeremy Dean Clifton @ 2026-11-17  2:33 UTC (permalink / raw)
  To: marcel, luiz.dentz
  Cc: linux-bluetooth, linux-kernel, william.xuanziyang, johan.hedberg,
	hdanton, Jeremy Dean Clifton

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.

Fixes: 181a42edddf5 ("Bluetooth: Make handle of hci_conn be unique")
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Jeremy Dean Clifton <deaner92@yahoo.com>
---
 net/bluetooth/hci_conn.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/net/bluetooth/hci_conn.c b/net/bluetooth/hci_conn.c
index c474263fbab9..d729a72f2f77 100644
--- a/net/bluetooth/hci_conn.c
+++ b/net/bluetooth/hci_conn.c
@@ -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,
-- 
2.43.0


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

* [PATCH v2 2/3] Bluetooth: hci_conn: Refuse new connections once HCI_UP is cleared
  2026-11-17  2:33 ` [PATCH v2 0/3] Bluetooth: Fix work queued while hci_dev_close_sync() drains Jeremy Dean Clifton
  2026-11-17  2:33   ` [PATCH v2 1/3] Bluetooth: hci_conn: Free the unset handle when adding a connection fails Jeremy Dean Clifton
@ 2026-11-17  2:33   ` Jeremy Dean Clifton
  2026-11-17  2:33   ` [PATCH v2 3/3] Bluetooth: hci_sync: Disable connection works before draining Jeremy Dean Clifton
  2 siblings, 0 replies; 5+ messages in thread
From: Jeremy Dean Clifton @ 2026-11-17  2:33 UTC (permalink / raw)
  To: marcel, luiz.dentz
  Cc: linux-bluetooth, linux-kernel, william.xuanziyang, johan.hedberg,
	hdanton, Jeremy Dean Clifton

hci_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.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Jeremy Dean Clifton <deaner92@yahoo.com>
---
 net/bluetooth/hci_conn.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/net/bluetooth/hci_conn.c b/net/bluetooth/hci_conn.c
index d729a72f2f77..7f0250653189 100644
--- a/net/bluetooth/hci_conn.c
+++ b/net/bluetooth/hci_conn.c
@@ -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)
-- 
2.43.0


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

* [PATCH v2 3/3] Bluetooth: hci_sync: Disable connection works before draining
  2026-11-17  2:33 ` [PATCH v2 0/3] Bluetooth: Fix work queued while hci_dev_close_sync() drains Jeremy Dean Clifton
  2026-11-17  2:33   ` [PATCH v2 1/3] Bluetooth: hci_conn: Free the unset handle when adding a connection fails Jeremy Dean Clifton
  2026-11-17  2:33   ` [PATCH v2 2/3] Bluetooth: hci_conn: Refuse new connections once HCI_UP is cleared Jeremy Dean Clifton
@ 2026-11-17  2:33   ` Jeremy Dean Clifton
  2 siblings, 0 replies; 5+ messages in thread
From: Jeremy Dean Clifton @ 2026-11-17  2:33 UTC (permalink / raw)
  To: marcel, luiz.dentz
  Cc: linux-bluetooth, linux-kernel, william.xuanziyang, johan.hedberg,
	hdanton, Jeremy Dean Clifton, syzbot+5897b3c764f5989ea942

A 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.

Fixes: 76727c02c1e1 ("Bluetooth: Call drain_workqueue() before resetting state")
Reported-by: syzbot+5897b3c764f5989ea942@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=5897b3c764f5989ea942
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Jeremy Dean Clifton <deaner92@yahoo.com>
---
 net/bluetooth/hci_sync.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/net/bluetooth/hci_sync.c b/net/bluetooth/hci_sync.c
index 931494348325..3fe179d649d0 100644
--- a/net/bluetooth/hci_sync.c
+++ b/net/bluetooth/hci_sync.c
@@ -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.
 	 */
-- 
2.43.0


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

* RE: Bluetooth: Fix work queued while hci_dev_close_sync() drains
  2026-11-17  2:33   ` [PATCH v2 1/3] Bluetooth: hci_conn: Free the unset handle when adding a connection fails Jeremy Dean Clifton
@ 2026-11-17 11:23     ` bluez.test.bot
  0 siblings, 0 replies; 5+ messages in thread
From: bluez.test.bot @ 2026-11-17 11:23 UTC (permalink / raw)
  To: linux-bluetooth, deaner92

[-- Attachment #1: Type: text/plain, Size: 4259 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/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: 
Assisted-by: Claude:claude-opus-5-5

ERROR: Unrecognized email address: 'Claude:claude-opus-5-5'
#135: 
Assisted-by: Claude:claude-opus-5-5

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: 
Assisted-by: Claude:claude-opus-5-5

ERROR: Unrecognized email address: 'Claude:claude-opus-5-5'
#139: 
Assisted-by: Claude:claude-opus-5-5

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: 
Assisted-by: Claude:claude-opus-5-5

ERROR: Unrecognized email address: 'Claude:claude-opus-5-5'
#139: 
Assisted-by: Claude:claude-opus-5-5

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


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

end of thread, other threads:[~2026-11-17 11:23 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <20261117023344.278269-1-deaner92.ref@yahoo.com>
2026-11-17  2:33 ` [PATCH v2 0/3] Bluetooth: Fix work queued while hci_dev_close_sync() drains Jeremy Dean Clifton
2026-11-17  2:33   ` [PATCH v2 1/3] Bluetooth: hci_conn: Free the unset handle when adding a connection fails Jeremy Dean Clifton
2026-11-17 11:23     ` Bluetooth: Fix work queued while hci_dev_close_sync() drains bluez.test.bot
2026-11-17  2:33   ` [PATCH v2 2/3] Bluetooth: hci_conn: Refuse new connections once HCI_UP is cleared Jeremy Dean Clifton
2026-11-17  2:33   ` [PATCH v2 3/3] Bluetooth: hci_sync: Disable connection works before draining Jeremy Dean Clifton

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