From: "Andrei A. Bykov" <andreiab9019@gmail.com>
To: linux-bluetooth@vger.kernel.org
Cc: marcel@holtmann.org, luiz.dentz@gmail.com, linux-kernel@vger.kernel.org
Subject: [PATCH v2 1/2] Bluetooth: hci_sync: keep unrelated replies away from event waiters
Date: Tue, 24 Nov 2026 11:09:55 -0300 [thread overview]
Message-ID: <20261124140956.15296-2-andreiab9019@gmail.com> (raw)
In-Reply-To: <20261124140956.15296-1-andreiab9019@gmail.com>
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
next prev parent reply other threads:[~2026-11-24 14:10 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
2026-11-24 14:09 ` [PATCH v2 2/2] Bluetooth: hci_core: preserve LE feature holds across encryption events Andrei A. Bykov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261124140956.15296-2-andreiab9019@gmail.com \
--to=andreiab9019@gmail.com \
--cc=linux-bluetooth@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=luiz.dentz@gmail.com \
--cc=marcel@holtmann.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox