* [PATCH 1/2] Bluetooth: hci_event: keep event waiters pending on unrelated replies
2026-11-21 3:31 [PATCH 0/2] Bluetooth: fix bonded LE peripheral reconnect completion and holds Andrei A. Bykov
@ 2026-11-21 3:31 ` Andrei A. Bykov
2026-11-21 9:32 ` Bluetooth: fix bonded LE peripheral reconnect completion and holds bluez.test.bot
2026-11-21 3:31 ` [PATCH 2/2] Bluetooth: hci_core: keep the LE feature hold until completion Andrei A. Bykov
1 sibling, 1 reply; 4+ messages in thread
From: Andrei A. Bykov @ 2026-11-21 3:31 UTC (permalink / raw)
To: linux-bluetooth; +Cc: marcel, luiz.dentz, linux-kernel
An incoming bonded LE connection may issue LE Read Remote Features and
wait for its terminal event while responding to an LTK Request with an
asynchronous LTK Request Reply.
hci_req_cmd_complete() accepts the reply as a valid sent command but
selects the completion callback from req_skb. If req_skb still belongs to
Remote Features, the LTK reply completes that request before its terminal
event arrives. This releases the feature hold while the connection is
still in BT_CONFIG and can immediately disconnect it.
Keep an active request with a terminal event pending when a valid reply
belongs to a different opcode. Apply the guard to Command Complete and
Command Status after command-specific handling and command-credit
updates, and continue scheduling queued commands.
Limit the guard to an active waiter. Replies following cancellation or
timeout retain the existing pending-flag cleanup. Unmatched replies still
reach the spontaneous Reset recovery path, and matching replies retain
their existing completion semantics.
Observed with an Intel AX210 peripheral, BlueZ 5.88 and a bonded Windows
central. Three unpatched attempts disconnected immediately after LTK
Reply completion, before Remote Features completion.
Fixes: 2615fd9a7c25 ("Bluetooth: hci_sync: Fix overwriting request callback")
Signed-off-by: Andrei A. Bykov <andreiab9019@gmail.com>
---
net/bluetooth/hci_event.c | 26 ++++++++++++++++++++++++++
1 file changed, 26 insertions(+)
diff --git a/net/bluetooth/hci_event.c b/net/bluetooth/hci_event.c
index ab875b9..5281551 100644
--- a/net/bluetooth/hci_event.c
+++ b/net/bluetooth/hci_event.c
@@ -4862,6 +4862,18 @@ static void hci_cmd_complete_evt(struct hci_dev *hdev, void *data,
handle_cmd_cnt_and_timer(hdev, ev->ncmd);
+ /* An unrelated command may complete while a request waits for its
+ * terminal event. Process the reply and command credits, but do not
+ * complete the pending request with that command's response.
+ * Unmatched replies must still reach hci_req_cmd_complete(), e.g.
+ * for the spontaneous Reset workaround during initialization.
+ */
+ if (READ_ONCE(hdev->req_status) == HCI_REQ_PEND &&
+ hdev->req_skb && hci_skb_event(hdev->req_skb) &&
+ hci_skb_opcode(hdev->req_skb) != *opcode &&
+ hci_sent_cmd_data(hdev, *opcode))
+ goto queue_next;
+
hci_req_cmd_complete(hdev, *opcode, *status, req_complete,
req_complete_skb);
@@ -4871,6 +4883,7 @@ static void hci_cmd_complete_evt(struct hci_dev *hdev, void *data,
return;
}
+queue_next:
if (atomic_read(&hdev->cmd_cnt) && !skb_queue_empty(&hdev->cmd_q))
queue_work(hdev->workqueue, &hdev->cmd_work);
}
@@ -4982,6 +4995,18 @@ static void hci_cmd_status_evt(struct hci_dev *hdev, void *data,
handle_cmd_cnt_and_timer(hdev, ev->ncmd);
+ /* An unrelated command may complete while a request waits for its
+ * terminal event. Process the reply and command credits, but do not
+ * complete the pending request with that command's response.
+ * Unmatched replies must still reach hci_req_cmd_complete(), e.g.
+ * for the spontaneous Reset workaround during initialization.
+ */
+ if (READ_ONCE(hdev->req_status) == HCI_REQ_PEND &&
+ hdev->req_skb && hci_skb_event(hdev->req_skb) &&
+ hci_skb_opcode(hdev->req_skb) != *opcode &&
+ hci_sent_cmd_data(hdev, *opcode))
+ goto queue_next;
+
/* Indicate request completion if the command failed. Also, if
* we're not waiting for a special event and we get a success
* command status we should try to flag the request as completed
@@ -4998,6 +5023,7 @@ static void hci_cmd_status_evt(struct hci_dev *hdev, void *data,
}
}
+queue_next:
if (atomic_read(&hdev->cmd_cnt) && !skb_queue_empty(&hdev->cmd_q))
queue_work(hdev->workqueue, &hdev->cmd_work);
}
^ permalink raw reply related [flat|nested] 4+ messages in thread* [PATCH 2/2] Bluetooth: hci_core: keep the LE feature hold until completion
2026-11-21 3:31 [PATCH 0/2] Bluetooth: fix bonded LE peripheral reconnect completion and holds Andrei A. Bykov
2026-11-21 3:31 ` [PATCH 1/2] Bluetooth: hci_event: keep event waiters pending on unrelated replies Andrei A. Bykov
@ 2026-11-21 3:31 ` Andrei A. Bykov
1 sibling, 0 replies; 4+ messages in thread
From: Andrei A. Bykov @ 2026-11-21 3:31 UTC (permalink / raw)
To: linux-bluetooth; +Cc: marcel, luiz.dentz, linux-kernel
LE feature discovery owns a connection hold until
le_read_features_complete() runs. If encryption finishes while the
connection is in BT_CONFIG, hci_encrypt_cfm() also drops a hold after
notifying the upper layers.
When an ATT socket has taken its own hold, the sequence is:
feature discovery: 0 -> 1
ATT connection: 1 -> 2
encryption confirmation: 2 -> 1
feature completion: 1 -> 0
The final drop arms the incoming connection's disconnect timer despite
the open ATT socket. After applying the event-waiter fix, four hardware
attempts showed the same sequence and a local disconnect approximately
four seconds later.
Restrict the drop in hci_encrypt_cfm() to non-LE links. BR/EDR connection
setup takes its configuration hold in hci_conn_complete_evt() and still
needs this drop. LE feature discovery releases its hold in its own
completion callback, including error paths. Keep the state transition
and upper-layer confirmation when encryption finishes first.
With both fixes on Linux 7.3.5, a hardware trace leaves the ATT hold at
one after feature completion and drops it to zero only when the test
explicitly closes the connection.
Fixes: a106e50be74b ("Bluetooth: HCI: Add support for LL Extended Feature Set")
Signed-off-by: Andrei A. Bykov <andreiab9019@gmail.com>
---
include/net/bluetooth/hci_core.h | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h
index 4aa0b49..227830f 100644
--- a/include/net/bluetooth/hci_core.h
+++ b/include/net/bluetooth/hci_core.h
@@ -2335,7 +2335,9 @@ static inline void hci_encrypt_cfm(struct hci_conn *conn, __u8 status)
conn->state = BT_CONNECTED;
hci_connect_cfm(conn, status);
- hci_conn_drop(conn);
+ /* LE feature discovery releases its own hold asynchronously. */
+ if (conn->type != LE_LINK)
+ hci_conn_drop(conn);
return;
}
^ permalink raw reply related [flat|nested] 4+ messages in thread