mirror of https://lore.kernel.org/lkml/
 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
                     ` (3 more replies)
  0 siblings, 4 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  2:33   ` [PATCH v2 2/3] Bluetooth: hci_conn: Refuse new connections once HCI_UP is cleared Jeremy Dean Clifton
                     ` (2 subsequent siblings)
  3 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_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	[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
  2026-11-17 18:50   ` [PATCH v2 0/3] Bluetooth: Fix work queued while hci_dev_close_sync() drains patchwork-bot+bluetooth
  3 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	[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
  2026-11-17 18:50   ` [PATCH v2 0/3] Bluetooth: Fix work queued while hci_dev_close_sync() drains patchwork-bot+bluetooth
  3 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	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 0/3] Bluetooth: Fix work queued while hci_dev_close_sync() drains
  2026-11-17  2:33 ` [PATCH v2 0/3] Bluetooth: Fix work queued while hci_dev_close_sync() drains Jeremy Dean Clifton
                     ` (2 preceding siblings ...)
  2026-11-17  2:33   ` [PATCH v2 3/3] Bluetooth: hci_sync: Disable connection works before draining Jeremy Dean Clifton
@ 2026-11-17 18:50   ` patchwork-bot+bluetooth
  3 siblings, 0 replies; 5+ messages in thread
From: patchwork-bot+bluetooth @ 2026-11-17 18:50 UTC (permalink / raw)
  To: Jeremy Dean Clifton
  Cc: marcel, luiz.dentz, linux-bluetooth, linux-kernel,
	william.xuanziyang, johan.hedberg, hdanton

Hello:

This series was applied to bluetooth/bluetooth-next.git (master)
by Luiz Augusto von Dentz <luiz.von.dentz@intel.com>:

On Tue, 17 Nov 2026 02:33:41 +0000 you wrote:
> 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.
> 
> [...]

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!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



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

end of thread, other threads:[~2026-11-17 18:50 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  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
2026-11-17 18:50   ` [PATCH v2 0/3] Bluetooth: Fix work queued while hci_dev_close_sync() drains patchwork-bot+bluetooth

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

all inboxes | Powered by JetHome®