The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH v2 0/2] Bluetooth: fix bonded LE peripheral reconnect completion and holds
@ 2026-11-24 14:09 Andrei A. Bykov
  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 14:09 ` [PATCH v2 2/2] Bluetooth: hci_core: preserve LE feature holds across encryption events Andrei A. Bykov
  0 siblings, 2 replies; 3+ messages in thread
From: Andrei A. Bykov @ 2026-11-24 14:09 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: marcel, luiz.dentz, linux-kernel

This series fixes two interactions seen when reconnecting a bonded Windows
central to an LE HID peripheral on an Intel AX210 with BlueZ 5.88.

The first patch preserves a synchronous terminal-event waiter across an
unrelated command reply and refreshes that waiter for the next synchronous
command after timeout or cancellation. The second keeps LE discovery hold
ownership in its completion callback and handles both encryption and key
refresh setup paths, including failed setup followed by a late event.

Changes since v1, following the public Sashiko report:
- Refresh the saved request from a callback-bearing synchronous command,
  including after timeout or cancellation; retain Reset resend ownership.
- Handle the BT_CONFIG key-refresh path through hci_encrypt_cfm().
- Explicitly abort failed LE setup and prevent later feature events from
  announcing that aborting connection as successfully connected.
- Rebase onto the official Bluetooth master at 4fe38719e673, rather than
  the GitHub workflow branch used for v1.

Local extracted-function tests pass 152 dispatch and 62 lifetime checks
with GCC, Clang and ASan/UBSan. Both the unpatched tree and v1 fail the
new regression cases. These checks model kernel services and do not
exercise concurrent workqueues or a physical controller.

A full kernel and modules build of this revision on the official base
completed successfully; that upstream build was not boot-tested.

I also tested a backport to Linux 7.3.5 with the same configuration used
for v1 and upstream prerequisite f3183d7ad101 (Bluetooth: hci_sync: Drop
the command's sk reference), already present in the series base. Repeated
bonded reconnects, approximately seven hours without observed connection
loss, and disabling/re-enabling Bluetooth on Windows passed.

A hardware trace confirms encryption can complete before remote features
without prematurely completing the feature waiter. The short HID test
sent 48 reports. In a teardown run with 20 seconds of post-exit recording,
Disconnection Complete arrived 137 ms after Disconnect, with no observed
command or connection timeout. One earlier teardown wait of about two
seconds was not reproduced; its cause and relation to this series remain
unconfirmed.

Windows suspend/resume remains a limitation: in two attempts BlueZ
reported a connection while the test HID application continued waiting
for the notification subscription. I have not localized this to the peer,
BlueZ, the application or the kernel; this series does not claim to
resolve that scenario.

OpenAI Codex assisted with the analysis, patch preparation and tests.
Sashiko supplied the review findings addressed by this revision.

v1: https://lore.kernel.org/r/20261121033104.441146-1-andreiab9019@gmail.com
Review: https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261121033104.441146-1-andreiab9019%40gmail.com

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: 4fe38719e6738feaa90486e36b0790dd5d960a96
-- 
2.56.1


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

* [PATCH v2 1/2] Bluetooth: hci_sync: keep unrelated replies away from event waiters
  2026-11-24 14:09 [PATCH v2 0/2] Bluetooth: fix bonded LE peripheral reconnect completion and holds Andrei A. Bykov
@ 2026-11-24 14:09 ` Andrei A. Bykov
  2026-11-24 14:09 ` [PATCH v2 2/2] Bluetooth: hci_core: preserve LE feature holds across encryption events Andrei A. Bykov
  1 sibling, 0 replies; 3+ messages in thread
From: Andrei A. Bykov @ 2026-11-24 14:09 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 255005916..ae0773a81 100644
--- a/net/bluetooth/hci_core.c
+++ b/net/bluetooth/hci_core.c
@@ -4284,11 +4284,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 039bcea98..477b876fd 100644
--- a/net/bluetooth/hci_event.c
+++ b/net/bluetooth/hci_event.c
@@ -4487,6 +4487,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);
 
@@ -4496,6 +4508,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);
 }
@@ -4606,6 +4619,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
@@ -4622,6 +4647,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] 3+ messages in thread

* [PATCH v2 2/2] Bluetooth: hci_core: preserve LE feature holds across encryption events
  2026-11-24 14:09 [PATCH v2 0/2] Bluetooth: fix bonded LE peripheral reconnect completion and holds Andrei A. Bykov
  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 14:09 ` Andrei A. Bykov
  1 sibling, 0 replies; 3+ messages in thread
From: Andrei A. Bykov @ 2026-11-24 14:09 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 LE key refresh through the same confirmation helper.

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 7cf63bc0f..469d4f077 100644
--- a/include/net/bluetooth/hci_core.h
+++ b/include/net/bluetooth/hci_core.h
@@ -2272,11 +2272,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 477b876fd..b224d4d69 100644
--- a/net/bluetooth/hci_event.c
+++ b/net/bluetooth/hci_event.c
@@ -5435,11 +5435,7 @@ static void hci_key_refresh_complete_evt(struct hci_dev *hdev, void *data,
 	}
 
 	if (conn->state == BT_CONFIG) {
-		if (!ev->status)
-			conn->state = BT_CONNECTED;
-
-		hci_connect_cfm(conn, ev->status);
-		hci_conn_drop(conn);
+		hci_encrypt_cfm(conn, ev->status);
 	} else {
 		hci_auth_cfm(conn, ev->status);
 
@@ -6961,7 +6957,7 @@ static void hci_le_remote_feat_complete_evt(struct hci_dev *hdev, void *data,
 			}
 		}
 
-		if (conn->state == BT_CONFIG) {
+		if (conn->state == BT_CONFIG && !conn->abort_reason) {
 			__u8 status;
 
 			/* If the local controller supports peripheral-initiated
@@ -7639,7 +7635,7 @@ static void hci_le_read_all_remote_features_evt(struct hci_dev *hdev,
 		}
 	}
 
-	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] 3+ messages in thread

end of thread, other threads:[~2026-11-24 14:10 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24 14:09 [PATCH v2 0/2] Bluetooth: fix bonded LE peripheral reconnect completion and holds Andrei A. Bykov
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 14:09 ` [PATCH v2 2/2] Bluetooth: hci_core: preserve LE feature holds across encryption events Andrei A. Bykov

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