* [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
2026-11-17 2:33 ` [PATCH v2 3/3] Bluetooth: hci_sync: Disable connection works before draining Jeremy Dean Clifton
2 siblings, 0 replies; 4+ 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] 4+ 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; 4+ 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] 4+ 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; 4+ 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] 4+ messages in thread