[PATCH v2 1/3] Bluetooth: hci_conn: Free the unset handle when adding a connection fails
From: Jeremy Dean Clifton
Date: Mon Nov 16 2026 - 21:36:00 EST
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@xxxxxxxxx>
---
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