Bluetooth: fix bonded LE peripheral reconnect completion and holds
Subsystems (2)
cover Bluetooth: fix bonded LE peripheral reconnect completion and holds
These patches fix two consecutive failures observed when a Windows central reconnects to a Linux LE HID peripheral using an existing bond.
Patch 1 prevents an asynchronous command reply from completing a different synchronous request that is still waiting for its terminal event. Once that is fixed, patch 2 prevents encryption confirmation from consuming the hold owned by asynchronous LE feature discovery.
The series is based on bluetooth-next:
029d11a1a6e4c414eb87ed965ed6bc09f36322ee
Both patches also apply to mainline:
5f22fb20497f4983094131f00a734362300d22d2
I tested the series on Linux 7.3.5 built from kernel.org sources and on the Arch Linux ZEN variant, using an Intel AX210 peripheral and BlueZ 5.88. On ZEN, I observed successful operation during a session whose process duration was 3h13m11s. I also tested application restart, Windows Bluetooth off/on, Windows suspend/resume, and concurrent physical mouse and Bluetooth audio use on ZEN; these scenarios worked successfully.
On the kernel.org stable build with this series applied, repeated application restarts also reconnected successfully. An HCI/ftrace capture confirmed that feature completion leaves the ATT hold at one and that explicit test teardown releases the last hold. The same capture contains 48 outgoing ATT test notifications.
On selected files from the stated bluetooth-next base, extracted-function checks pass 121 dispatcher and 30 lifetime assertions with GCC and Clang, including an ASan/UBSan run. Unpatched negative controls fail 13 dispatcher assertions and 20 lifetime assertions. Dependencies are modeled; these checks do not replace a full-kernel concurrency test. Full kernels were built and booted for 7.3.5. A full GCC build of the stated bluetooth-next base with both patches also completed successfully (kernel and modules, including Rust and Bluetooth module BTF), using a configuration derived from the running kernel.org stable 7.3.5 build. That upstream build has not been installed or booted.
Separate unresolved observation: after suspending the Linux peripheral, the application and bond survive, but advertising is not re-enabled in the captured interval. Restarting the application re-enables advertising and permits reconnection. Its relationship to these patches has not been established. This series does not claim to fix suspend/resume.
OpenAI Codex was used to assist with the analysis, patch preparation and tests. No raw radio captures or pairing keys are attached. A sanitized trace summary is available separately.
Andrei A. Bykov
---
Andrei A. Bykov (2):
Bluetooth: hci_event: keep event waiters pending on unrelated replies
Bluetooth: hci_core: keep the LE feature hold until completion
include/net/bluetooth/hci_core.h | 4 +++- net/bluetooth/hci_event.c | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-)
base-commit: 029d11a1a6e4c414eb87ed965ed6bc09f36322ee
PATCH 1/2 Bluetooth: hci_event: keep event waiters pending on unrelated replies
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.
--- net/bluetooth/hci_event.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+)
net/bluetooth/hci_event.c +26 -0
@@ -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); }
PATCH 2/2 Bluetooth: hci_core: keep the LE feature hold until completion
In reply to Andrei A. BykovLE 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.
--- include/net/bluetooth/hci_core.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
include/net/bluetooth/hci_core.h +3 -1
@@ -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 ; }
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/1217910/
---Test result---
Test Summary:
CheckPatch PASS 1.07 seconds VerifyFixes PASS 0.10 seconds VerifySignedoff PASS 0.10 seconds GitLint PASS 0.52 seconds SubjectPrefix PASS 0.20 seconds BuildKernel PASS 20.05 seconds CheckAllWarning PASS 23.01 seconds CheckSparse WARNING 24.33 seconds BuildKernel32 PASS 19.95 seconds CheckKernelLLVM PASS 20.35 seconds TestRunnerSetup PASS 433.53 seconds TestRunner_l2cap-tester PASS 14.55 seconds TestRunner_iso-tester PASS 10.59 seconds TestRunner_bnep-tester PASS 2.84 seconds TestRunner_mgmt-tester PASS 82.63 seconds TestRunner_rfcomm-tester PASS 3.99 seconds TestRunner_sco-tester PASS 4.98 seconds TestRunner_ioctl-tester PASS 4.11 seconds TestRunner_mesh-tester PASS 4.10 seconds TestRunner_smp-tester PASS 3.96 seconds TestRunner_userchan-tester PASS 2.86 seconds TestRunner_6lowpan-tester PASS 4.01 seconds IncrementalBuild PASS 31.77 seconds
Details
##############################
Test: CheckSparse - WARNING
Desc: Run sparse tool with linux kernel
Output:net/bluetooth/hci_event.c: note: in included file (through include/net/bluetooth/hci_core.h):
https://github.com/bluez/bluetooth-next/pull/1171
---
Regards,
Linux Bluetooth