Linux bluetooth development
 help / color / mirror / Atom feed
* RE: Bluetooth: fix bonded LE peripheral reconnect completion and holds
  2026-11-21  3:31 [PATCH 1/2] Bluetooth: hci_event: keep event waiters pending on unrelated replies Andrei A. Bykov
@ 2026-11-21  9:32 ` bluez.test.bot
  0 siblings, 0 replies; 6+ messages in thread
From: bluez.test.bot @ 2026-11-21  9:32 UTC (permalink / raw)
  To: linux-bluetooth, andreiab9019

[-- Attachment #1: Type: text/plain, Size: 1815 bytes --]

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


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

* RE: Bluetooth: fix bonded LE peripheral reconnect completion and holds
  2026-11-24 14:09 [PATCH v2 1/2] Bluetooth: hci_sync: keep unrelated replies away from event waiters Andrei A. Bykov
@ 2026-11-24 18:22 ` bluez.test.bot
  0 siblings, 0 replies; 6+ messages in thread
From: bluez.test.bot @ 2026-11-24 18:22 UTC (permalink / raw)
  To: linux-bluetooth, andreiab9019

[-- Attachment #1: Type: text/plain, Size: 849 bytes --]

This is an automated email and please do not reply to this email.

Dear Submitter,

Thank you for submitting the patches to the linux bluetooth mailing list.
While preparing the CI tests, the patches you submitted couldn't be applied to the current HEAD of the repository.

----- Output -----

error: patch failed: net/bluetooth/hci_event.c:5435
error: net/bluetooth/hci_event.c: patch does not apply
hint: Use 'git am --show-current-patch=diff' to see the failed patch
hint: When you have resolved this problem, run "git am --continue".
hint: If you prefer to skip this patch, run "git am --skip" instead.
hint: To restore the original branch and stop patching, run "git am --abort".
hint: Disable this message with "git config set advice.mergeConflict false"

Please resolve the issue and submit the patches again.


---
Regards,
Linux Bluetooth


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

* [PATCH v3 0/2] Bluetooth: fix bonded LE peripheral reconnect completion and holds
@ 2026-11-24 21:11 Andrei A. Bykov
  2026-11-24 21:11 ` [PATCH v3 1/2] Bluetooth: hci_sync: keep unrelated replies away from event waiters Andrei A. Bykov
  2026-11-24 21:11 ` [PATCH v3 2/2] Bluetooth: hci_core: preserve LE feature holds across encryption events Andrei A. Bykov
  0 siblings, 2 replies; 6+ messages in thread
From: Andrei A. Bykov @ 2026-11-24 21:11 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: marcel, luiz.dentz, linux-kernel

This series fixes two issues in the Bluetooth HCI layer seen when a
bonded Windows central reconnects to a Linux LE HID peripheral.

Changes since v2:
- Rebase onto bluetooth-next at 6a40daae69b0. The Bluetooth CI bot
  could not apply v2 because the key-refresh configuration code moved
  into hci_conn_refresh_key(). Update that shared helper in patch 2 so
  both Key Refresh Complete event formats use the same path.
- Preserve the LE setup abort checks in both feature-complete handlers
  on the new base. Patch 1 has no functional changes.

Changes since v1:
- Refresh the saved synchronous request after timeout or cancellation,
  preserving socket references across controller reset resends.
- Keep the LE feature hold across encryption and key refresh. Explicitly
  abort failed setup and prevent later events from reporting success.

Both v3 patches apply in order with git am on the pinned bluetooth-next
base and the GitHub workflow branch used by Bluetooth CI. Local model
tests pass 152 command-dispatch checks and 62 connection-lifetime checks
with GCC, Clang and ASan/UBSan.

The v2 backport to Linux 7.3.5 passed repeated bonded reconnects,
approximately seven hours without observed connection loss, and Windows
Bluetooth off/on. Those hardware results are from v2. In separate
Windows suspend/resume attempts, the test HID application could still
wait for an HID subscription after BlueZ reported a connection.

OpenAI Codex assisted with analysis, patch preparation and tests.
Sashiko provided the findings addressed in v2.

v1: https://lore.kernel.org/r/20261121033104.441146-1-andreiab9019@gmail.com
v2: https://lore.kernel.org/r/20261124140956.15296-1-andreiab9019@gmail.com
v2 CI: https://patchwork.kernel.org/comment/27404265/

Andrei A. Bykov (2):
  Bluetooth: hci_sync: keep unrelated replies away from event waiters
  Bluetooth: hci_core: preserve LE feature holds across encryption
    events

 include/net/bluetooth/hci_core.h |  9 +++++++-
 net/bluetooth/hci_core.c         | 16 +++++++++++++-
 net/bluetooth/hci_event.c        | 36 +++++++++++++++++++++++++-------
 3 files changed, 52 insertions(+), 9 deletions(-)


base-commit: 6a40daae69b0c45d4f8b6aa247fca165b8534f60
-- 
2.56.1


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

* [PATCH v3 1/2] Bluetooth: hci_sync: keep unrelated replies away from event waiters
  2026-11-24 21:11 [PATCH v3 0/2] Bluetooth: fix bonded LE peripheral reconnect completion and holds Andrei A. Bykov
@ 2026-11-24 21:11 ` Andrei A. Bykov
  2026-11-25  2:55   ` Bluetooth: fix bonded LE peripheral reconnect completion and holds bluez.test.bot
  2026-11-24 21:11 ` [PATCH v3 2/2] Bluetooth: hci_core: preserve LE feature holds across encryption events Andrei A. Bykov
  1 sibling, 1 reply; 6+ messages in thread
From: Andrei A. Bykov @ 2026-11-24 21:11 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: marcel, luiz.dentz, linux-kernel

An asynchronous command can finish while a synchronous request waits for
its terminal event. For example, an LE LTK Request Reply can complete
before LE Read Remote Features Complete arrives. Completing the waiter
with that reply releases the feature-discovery hold too early.

Keep valid unrelated Command Complete and Command Status replies away
from event waiters after processing their command handlers and credits.
Continue scheduling queued commands and preserve unmatched Reset recovery.

Do not identify the current waiter solely by HCI_CMD_PENDING: timeout
and cancellation can leave it set with an old req_skb. Capture the actual
synchronous command carrying HCI_REQ_SKB even when that flag is already
set. Callback-less commands must not replace the waiter. Drop the stale
socket reference when replacing the request, but preserve the same skb
backing storage and socket reference when Reset recovery resends it.
The request skb is still replaced on the ordered command/event workqueue.

This also lets the next synchronous command complete normally after an
event timeout, without waiting for the old terminal event to arrive.

Fixes: 2615fd9a7c25 ("Bluetooth: hci_sync: Fix overwriting request callback")
Signed-off-by: Andrei A. Bykov <andreiab9019@gmail.com>
---
 net/bluetooth/hci_core.c  | 16 +++++++++++++++-
 net/bluetooth/hci_event.c | 26 ++++++++++++++++++++++++++
 2 files changed, 41 insertions(+), 1 deletion(-)

diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c
index 024960e9af..072ab361fe 100644
--- a/net/bluetooth/hci_core.c
+++ b/net/bluetooth/hci_core.c
@@ -4386,11 +4386,25 @@ static int hci_send_cmd_sync(struct hci_dev *hdev, struct sk_buff *skb)
 		kfree_skb(skb);
 	}
 
+	/* A synchronous command starts a new waiter, even if an earlier
+	 * event waiter timed out without clearing HCI_CMD_PENDING. Commands
+	 * without a completion callback must not replace the waiter.
+	 */
 	if (READ_ONCE(hdev->req_status) == HCI_REQ_PEND &&
-	    !hci_dev_test_and_set_flag(hdev, HCI_CMD_PENDING)) {
+	    (bt_cb(hdev->sent_cmd)->hci.req_flags & HCI_REQ_SKB)) {
+		/* hci_resend_last() clones the same command during reset
+		 * recovery. It does not own another socket reference.
+		 */
+		if (hdev->req_skb &&
+		    hdev->req_skb->head == hdev->sent_cmd->head)
+			return err;
+
 		hci_dev_lock(hdev);
+		if (hdev->req_skb && hci_skb_sk(hdev->req_skb))
+			sock_put(hci_skb_sk(hdev->req_skb));
 		kfree_skb(hdev->req_skb);
 		hdev->req_skb = skb_get(hdev->sent_cmd);
+		hci_dev_set_flag(hdev, HCI_CMD_PENDING);
 		hci_dev_unlock(hdev);
 	}
 
diff --git a/net/bluetooth/hci_event.c b/net/bluetooth/hci_event.c
index bdeb86f78a..7991db54c2 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);
 }
-- 
2.56.1


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

* [PATCH v3 2/2] Bluetooth: hci_core: preserve LE feature holds across encryption events
  2026-11-24 21:11 [PATCH v3 0/2] Bluetooth: fix bonded LE peripheral reconnect completion and holds Andrei A. Bykov
  2026-11-24 21:11 ` [PATCH v3 1/2] Bluetooth: hci_sync: keep unrelated replies away from event waiters Andrei A. Bykov
@ 2026-11-24 21:11 ` Andrei A. Bykov
  1 sibling, 0 replies; 6+ messages in thread
From: Andrei A. Bykov @ 2026-11-24 21:11 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: marcel, luiz.dentz, linux-kernel

LE feature discovery now owns its connection hold until its asynchronous
completion callback. Encryption Change and Key Refresh Complete can run
while that discovery is pending in BT_CONFIG. Neither path owns a second
setup hold, so dropping one there consumes the upper-layer hold after
feature discovery finishes and disconnects an active ATT connection.

Keep the BT_CONFIG drop for BR/EDR, whose configuration path still owns
that hold. Route both LE key refresh event formats through the same
confirmation helper via hci_conn_refresh_key().

On LE setup failure, explicitly request disconnection instead of relying
on a drop of the feature worker-owned hold. The abort request records
abort_reason before queuing its work. Do not let a later encryption or
feature-complete event announce such a connection as successful while
that abort is pending. Apply the check to both LE feature event formats.
The feature worker still releases its own hold and device reference.

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 |  9 ++++++++-
 net/bluetooth/hci_event.c        | 10 +++-------
 2 files changed, 11 insertions(+), 8 deletions(-)

diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h
index 4aa0b49ae1..68cce0628e 100644
--- a/include/net/bluetooth/hci_core.h
+++ b/include/net/bluetooth/hci_core.h
@@ -2331,11 +2331,18 @@ static inline void hci_encrypt_cfm(struct hci_conn *conn, __u8 status)
 	__u8 encrypt;
 
 	if (conn->state == BT_CONFIG) {
+		if (conn->type == LE_LINK && conn->abort_reason)
+			return;
+
 		if (!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);
+		else if (status)
+			hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
 		return;
 	}
 
diff --git a/net/bluetooth/hci_event.c b/net/bluetooth/hci_event.c
index 7991db54c2..cb9e4255a3 100644
--- a/net/bluetooth/hci_event.c
+++ b/net/bluetooth/hci_event.c
@@ -5798,11 +5798,7 @@ static void hci_conn_refresh_key(struct hci_conn *conn, u8 status)
 	}
 
 	if (conn->state == BT_CONFIG) {
-		if (!status)
-			conn->state = BT_CONNECTED;
-
-		hci_connect_cfm(conn, status);
-		hci_conn_drop(conn);
+		hci_encrypt_cfm(conn, status);
 	} else {
 		hci_auth_cfm(conn, status);
 
@@ -7470,7 +7466,7 @@ static void hci_le_remote_feat_complete_evt(struct hci_dev *hdev, void *data,
 			conn->le_rx_def_phys &= ~HCI_LE_SET_PHY_HDT;
 		}
 
-		if (conn->state == BT_CONFIG) {
+		if (conn->state == BT_CONFIG && !conn->abort_reason) {
 			__u8 status;
 
 			/* If the local controller supports peripheral-initiated
@@ -8171,7 +8167,7 @@ static void hci_le_read_all_remote_features_evt(struct hci_dev *hdev,
 		hci_le_eks_oppt_encrypt(conn);
 	}
 
-	if (conn->state == BT_CONFIG) {
+	if (conn->state == BT_CONFIG && !conn->abort_reason) {
 		__u8 status;
 
 		/* If the local controller supports peripheral-initiated
-- 
2.56.1


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

* RE: Bluetooth: fix bonded LE peripheral reconnect completion and holds
  2026-11-24 21:11 ` [PATCH v3 1/2] Bluetooth: hci_sync: keep unrelated replies away from event waiters Andrei A. Bykov
@ 2026-11-25  2:55   ` bluez.test.bot
  0 siblings, 0 replies; 6+ messages in thread
From: bluez.test.bot @ 2026-11-25  2:55 UTC (permalink / raw)
  To: linux-bluetooth, andreiab9019

[-- Attachment #1: Type: text/plain, Size: 2500 bytes --]

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/1221297/

---Test result---

Test Summary:
CheckPatch                    PASS      2.01 seconds
VerifyFixes                   PASS      0.12 seconds
VerifySignedoff               PASS      0.12 seconds
GitLint                       PASS      0.67 seconds
SubjectPrefix                 PASS      0.23 seconds
BuildKernel                   PASS      33.17 seconds
CheckAllWarning               PASS      36.39 seconds
CheckSparse                   WARNING   39.43 seconds
BuildKernel32                 PASS      32.70 seconds
CheckKernelLLVM               PASS      33.93 seconds
TestRunnerSetup               PASS      781.29 seconds
TestRunner_l2cap-tester       PASS      14.87 seconds
TestRunner_iso-tester         PASS      13.74 seconds
TestRunner_bnep-tester        PASS      3.24 seconds
TestRunner_mgmt-tester        PASS      90.05 seconds
TestRunner_rfcomm-tester      PASS      4.56 seconds
TestRunner_sco-tester         PASS      5.71 seconds
TestRunner_ioctl-tester       PASS      4.77 seconds
TestRunner_mesh-tester        PASS      4.60 seconds
TestRunner_smp-tester         PASS      4.50 seconds
TestRunner_userchan-tester    PASS      3.31 seconds
TestRunner_6lowpan-tester     PASS      4.53 seconds
IncrementalBuild              FAIL      33.91 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):net/bluetooth/hci_event.c: note: in included file (through include/net/bluetooth/hci_core.h):
##############################
Test: IncrementalBuild - FAIL
Desc: Incremental build with the patches in the series
Output:

error: patch failed: net/bluetooth/hci_event.c:7470
error: net/bluetooth/hci_event.c: patch does not apply
hint: Use 'git am --show-current-patch=diff' to see the failed patch
hint: When you have resolved this problem, run "git am --continue".
hint: If you prefer to skip this patch, run "git am --skip" instead.
hint: To restore the original branch and stop patching, run "git am --abort".
hint: Disable this message with "git config set advice.mergeConflict false"


https://github.com/bluez/bluetooth-next/pull/1221

---
Regards,
Linux Bluetooth


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

end of thread, other threads:[~2026-11-25  2:55 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24 21:11 [PATCH v3 0/2] Bluetooth: fix bonded LE peripheral reconnect completion and holds Andrei A. Bykov
2026-11-24 21:11 ` [PATCH v3 1/2] Bluetooth: hci_sync: keep unrelated replies away from event waiters Andrei A. Bykov
2026-11-25  2:55   ` Bluetooth: fix bonded LE peripheral reconnect completion and holds bluez.test.bot
2026-11-24 21:11 ` [PATCH v3 2/2] Bluetooth: hci_core: preserve LE feature holds across encryption events Andrei A. Bykov
  -- strict thread matches above, loose matches on Subject: below --
2026-11-24 14:09 [PATCH v2 1/2] Bluetooth: hci_sync: keep unrelated replies away from event waiters Andrei A. Bykov
2026-11-24 18:22 ` Bluetooth: fix bonded LE peripheral reconnect completion and holds bluez.test.bot
2026-11-21  3:31 [PATCH 1/2] Bluetooth: hci_event: keep event waiters pending on unrelated replies Andrei A. Bykov
2026-11-21  9:32 ` Bluetooth: fix bonded LE peripheral reconnect completion and holds bluez.test.bot

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