Linux bluetooth development
 help / color / mirror / Atom feed
From: Kyle Pepper <cochcoder@proton.me>
To: linux-bluetooth@vger.kernel.org
Cc: luiz.dentz@gmail.com, marcel@holtmann.org
Subject: [RFC PATCH v4 1/2] Bluetooth: Allow custom matching of synchronous LE events
Date: Fri, 04 Dec 2026 01:03:15 +0000	[thread overview]
Message-ID: <20261204010225.3029996-2-cochcoder@proton.me> (raw)
In-Reply-To: <20261204010225.3029996-1-cochcoder@proton.me>

LE Connection Update Complete includes a connection handle, but the
synchronous HCI request can complete on the event code alone. An update
for a different connection can therefore complete the wrong request.

Allow a synchronous LE event request to install an optional matcher.
Use it for LE Connection Update Complete to check the command and event
payload lengths, match the connection handle, and return the controller
status. Events that do not match still go through normal dispatch.
Callers without a matcher keep event-only completion.

Codex assisted with the matcher and request cleanup changes.
AI assistance: v4
Signed-off-by: Kyle Pepper <cochcoder@proton.me>
---
 include/net/bluetooth/bluetooth.h |  14 +++
 include/net/bluetooth/hci_core.h  |   2 +
 include/net/bluetooth/hci_sync.h  |   9 ++
 net/bluetooth/hci_conn.c          |   5 +-
 net/bluetooth/hci_event.c         |  29 ++++--
 net/bluetooth/hci_sync.c          | 147 ++++++++++++++++++++++++++----
 6 files changed, 176 insertions(+), 30 deletions(-)

diff --git a/include/net/bluetooth/bluetooth.h b/include/net/bluetooth/bluetooth.h
index 777bc1a03..58f8a7186 100644
--- a/include/net/bluetooth/bluetooth.h
+++ b/include/net/bluetooth/bluetooth.h
@@ -572,6 +572,20 @@ int hci_ethtool_ts_info(unsigned int index, int sk_proto,
 #define HCI_REQ_START	BIT(0)
 #define HCI_REQ_SKB	BIT(1)
 
+/**
+ * typedef hci_req_match_t - match an LE event against a pending sync command
+ * @cmd: pending command, starting at its HCI command header
+ * @event: LE Meta event, starting at the LE subevent payload
+ * @status: completion status, set only on a match
+ *
+ * Called from hci_le_meta_evt() with hdev->lock held; must not sleep.
+ * The caller arming the request holds hdev->req_lock.
+ *
+ * Return: true if @event completes @cmd and @status was set, else false.
+ */
+typedef bool (*hci_req_match_t)(const struct sk_buff *cmd,
+			       const struct sk_buff *event, u8 *status);
+
 struct hci_ctrl {
 	struct sock *sk;
 	u16 opcode;
diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h
index 4aa0b49ae..106fc41a6 100644
--- a/include/net/bluetooth/hci_core.h
+++ b/include/net/bluetooth/hci_core.h
@@ -546,6 +546,8 @@ struct hci_dev {
 	__u32			req_status;
 	__u32			req_result;
 	struct sk_buff		*req_skb;
+	/* Protected by hdev->lock; the sync request owner holds req_lock. */
+	hci_req_match_t		req_match;
 	struct sk_buff		*req_rsp;
 
 	void			*smp_data;
diff --git a/include/net/bluetooth/hci_sync.h b/include/net/bluetooth/hci_sync.h
index eab827dfa..d09fb2c06 100644
--- a/include/net/bluetooth/hci_sync.h
+++ b/include/net/bluetooth/hci_sync.h
@@ -37,6 +37,15 @@ struct hci_cmd_sync_work_entry {
 };
 
 struct adv_info;
+struct hci_cp_le_conn_update;
+
+/* Wait for LE Connection Update Complete for cp->handle. The caller must
+ * hold hdev->req_lock and pass a positive timeout in jiffies.
+ * Returns 0 or a negative errno.
+ */
+int hci_le_conn_update_wait_sync(struct hci_dev *hdev,
+				 const struct hci_cp_le_conn_update *cp,
+				 u32 timeout);
 
 struct sk_buff *hci_cmd_sync_alloc(struct hci_dev *hdev, u16 opcode, u32 plen,
 				   const void *param, struct sock *sk);
diff --git a/net/bluetooth/hci_conn.c b/net/bluetooth/hci_conn.c
index 5b7526a85..f37459d41 100644
--- a/net/bluetooth/hci_conn.c
+++ b/net/bluetooth/hci_conn.c
@@ -542,10 +542,7 @@ static int le_conn_update_sync(struct hci_dev *hdev, void *data)
 
 	hci_dev_unlock(hdev);
 
-	err = __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CONN_UPDATE,
-				       sizeof(cp), &cp,
-				       HCI_EV_LE_CONN_UPDATE_COMPLETE,
-				       timeout, NULL);
+	err = hci_le_conn_update_wait_sync(hdev, &cp, timeout);
 	if (err)
 		return err;
 
diff --git a/net/bluetooth/hci_event.c b/net/bluetooth/hci_event.c
index 749963d0c..a6c4fb486 100644
--- a/net/bluetooth/hci_event.c
+++ b/net/bluetooth/hci_event.c
@@ -8486,17 +8486,30 @@ static void hci_le_meta_evt(struct hci_dev *hdev, void *data,
 {
 	struct hci_ev_le_meta *ev = data;
 	const struct hci_le_ev *subev;
+	struct sk_buff *cmd;
+	hci_req_match_t match;
+	u8 result = 0;
 
 	bt_dev_dbg(hdev, "subevent 0x%2.2x", ev->subevent);
 
-	/* Only match event if command OGF is for LE */
-	if (hdev->req_skb &&
-	   (hci_opcode_ogf(hci_skb_opcode(hdev->req_skb)) == 0x08 ||
-	    hci_skb_opcode(hdev->req_skb) == HCI_OP_NOP) &&
-	    hci_skb_event(hdev->req_skb) == ev->subevent) {
-		*opcode = hci_skb_opcode(hdev->req_skb);
-		hci_req_cmd_complete(hdev, *opcode, 0x00, req_complete,
-				     req_complete_skb);
+	/* Avoid taking hdev->lock for reports with no pending command. */
+	if (READ_ONCE(hdev->req_skb)) {
+		hci_dev_lock(hdev);
+		cmd = hdev->req_skb;
+		if (cmd && (hci_opcode_ogf(hci_skb_opcode(cmd)) == 0x08 ||
+			    hci_skb_opcode(cmd) == HCI_OP_NOP) &&
+		    hci_skb_event(cmd) == ev->subevent) {
+			match = hdev->req_match;
+			if (!match ||
+			    (READ_ONCE(hdev->req_status) == HCI_REQ_PEND &&
+			     match(cmd, skb, &result))) {
+				*opcode = hci_skb_opcode(cmd);
+				*status = result;
+				hci_req_cmd_complete(hdev, *opcode, result, req_complete,
+						     req_complete_skb);
+			}
+		}
+		hci_dev_unlock(hdev);
 	}
 
 	subev = &hci_le_ev_table[ev->subevent];
diff --git a/net/bluetooth/hci_sync.c b/net/bluetooth/hci_sync.c
index ee426ffea..ef1009632 100644
--- a/net/bluetooth/hci_sync.c
+++ b/net/bluetooth/hci_sync.c
@@ -20,6 +20,18 @@
 #include "aosp.h"
 #include "leds.h"
 
+static void hci_cmd_sync_clear_req_skb(struct hci_dev *hdev)
+{
+	if (hdev->req_skb) {
+		struct sock *sk = hci_skb_sk(hdev->req_skb);
+
+		if (sk)
+			sock_put(sk);
+	}
+	kfree_skb(hdev->req_skb);
+	hdev->req_skb = NULL;
+}
+
 static void hci_cmd_sync_complete(struct hci_dev *hdev, u8 result, u16 opcode,
 				  struct sk_buff *skb)
 {
@@ -36,14 +48,7 @@ static void hci_cmd_sync_complete(struct hci_dev *hdev, u8 result, u16 opcode,
 	 * response skb doesn't carry it.
 	 */
 	hci_dev_lock(hdev);
-	if (hdev->req_skb) {
-		struct sock *sk = hci_skb_sk(hdev->req_skb);
-
-		if (sk)
-			sock_put(sk);
-	}
-	kfree_skb(hdev->req_skb);
-	hdev->req_skb = NULL;
+	hci_cmd_sync_clear_req_skb(hdev);
 	hci_dev_unlock(hdev);
 
 	if (skb)
@@ -156,10 +161,49 @@ static void hci_request_init(struct hci_request *req, struct hci_dev *hdev)
 	req->err = 0;
 }
 
+static void hci_req_match_set(struct hci_dev *hdev, hci_req_match_t match)
+{
+	hci_dev_lock(hdev);
+	hdev->req_match = match;
+	hci_dev_unlock(hdev);
+}
+
+struct hci_req_match_cleanup {
+	struct work_struct work;
+	struct hci_dev *hdev;
+};
+
+static void hci_cmd_sync_match_abort_work(struct work_struct *work)
+{
+	struct hci_req_match_cleanup *cleanup =
+		container_of(work, struct hci_req_match_cleanup, work);
+	struct hci_dev *hdev = cleanup->hdev;
+
+	hci_dev_lock(hdev);
+	hci_dev_clear_flag(hdev, HCI_CMD_PENDING);
+	hci_cmd_sync_clear_req_skb(hdev);
+	if (READ_ONCE(hdev->req_status) == HCI_REQ_PEND)
+		WRITE_ONCE(hdev->req_status, 0);
+	hdev->req_match = NULL;
+	hci_dev_unlock(hdev);
+}
+
+/* Serialize cleanup with command sending and event readers of req_skb. */
+static void hci_cmd_sync_match_abort(struct hci_dev *hdev)
+{
+	struct hci_req_match_cleanup cleanup = { .hdev = hdev };
+
+	INIT_WORK_ONSTACK(&cleanup.work, hci_cmd_sync_match_abort_work);
+	queue_work(hdev->workqueue, &cleanup.work);
+	flush_work(&cleanup.work);
+	destroy_work_on_stack(&cleanup.work);
+}
+
 /* This function requires the caller holds hdev->req_lock. */
-struct sk_buff *__hci_cmd_sync_sk(struct hci_dev *hdev, u16 opcode, u32 plen,
-				  const void *param, u8 event, u32 timeout,
-				  struct sock *sk)
+static struct sk_buff *hci_cmd_sync_match(struct hci_dev *hdev, u16 opcode,
+					  u32 plen, const void *param,
+					  u8 event, u32 timeout, struct sock *sk,
+					  hci_req_match_t match)
 {
 	struct hci_request req;
 	struct sk_buff *skb;
@@ -170,19 +214,31 @@ struct sk_buff *__hci_cmd_sync_sk(struct hci_dev *hdev, u16 opcode, u32 plen,
 	hci_request_init(&req, hdev);
 
 	hci_cmd_sync_add(&req, opcode, plen, param, event, sk);
+	if (match) {
+		if (req.err)
+			return ERR_PTR(req.err);
+		hci_req_match_set(hdev, match);
+	}
 
 	WRITE_ONCE(hdev->req_status, HCI_REQ_PEND);
 
 	err = hci_req_sync_run(&req);
-	if (err < 0)
+	if (err < 0 && !match)
 		return ERR_PTR(err);
 
-	err = wait_event_interruptible_timeout(hdev->req_wait_q,
-					       READ_ONCE(hdev->req_status) != HCI_REQ_PEND,
-					       timeout);
-
-	if (err == -ERESTARTSYS)
-		return ERR_PTR(-EINTR);
+	if (!err)
+		err = wait_event_interruptible_timeout(hdev->req_wait_q,
+						       READ_ONCE(hdev->req_status) != HCI_REQ_PEND,
+						       timeout);
+	if (match)
+		hci_cmd_sync_match_abort(hdev);
+	if (err == -ERESTARTSYS) {
+		if (!match)
+			return ERR_PTR(-EINTR);
+		err = -EINTR;
+	}
+	if (err < 0)
+		goto done;
 
 	switch (READ_ONCE(hdev->req_status)) {
 	case HCI_REQ_DONE:
@@ -198,6 +254,7 @@ struct sk_buff *__hci_cmd_sync_sk(struct hci_dev *hdev, u16 opcode, u32 plen,
 		break;
 	}
 
+done:
 	WRITE_ONCE(hdev->req_status, 0);
 	hdev->req_result = 0;
 	skb = hdev->req_rsp;
@@ -218,8 +275,62 @@ struct sk_buff *__hci_cmd_sync_sk(struct hci_dev *hdev, u16 opcode, u32 plen,
 
 	return skb;
 }
+
+struct sk_buff *__hci_cmd_sync_sk(struct hci_dev *hdev, u16 opcode, u32 plen,
+				  const void *param, u8 event, u32 timeout,
+				  struct sock *sk)
+{
+	return hci_cmd_sync_match(hdev, opcode, plen, param, event, timeout, sk,
+				  NULL);
+}
 EXPORT_SYMBOL(__hci_cmd_sync_sk);
 
+/* Caller must hold hdev->req_lock. */
+static int __hci_cmd_sync_le_match(struct hci_dev *hdev, u16 opcode, u32 plen,
+				   const void *param, u8 event, u32 timeout,
+				   hci_req_match_t match)
+{
+	struct sk_buff *skb;
+
+	if (match && hci_opcode_ogf(opcode) != 0x08)
+		return -EINVAL;
+
+	skb = hci_cmd_sync_match(hdev, opcode, plen, param, event, timeout, NULL,
+				 match);
+	if (IS_ERR(skb))
+		return PTR_ERR(skb) == -ENODATA ? 0 : PTR_ERR(skb);
+
+	kfree_skb(skb);
+	return 0;
+}
+
+static bool le_conn_update_match(const struct sk_buff *cmd,
+				 const struct sk_buff *event, u8 *status)
+{
+	struct hci_ev_le_conn_update_complete ev_buf;
+	struct hci_cp_le_conn_update cp_buf;
+	const struct hci_ev_le_conn_update_complete *ev;
+	const struct hci_cp_le_conn_update *cp;
+
+	cp = skb_header_pointer(cmd, HCI_COMMAND_HDR_SIZE, sizeof(cp_buf),
+				&cp_buf);
+	ev = skb_header_pointer(event, 0, sizeof(ev_buf), &ev_buf);
+	if (!cp || !ev || cp->handle != ev->handle)
+		return false;
+
+	*status = ev->status;
+	return true;
+}
+
+int hci_le_conn_update_wait_sync(struct hci_dev *hdev,
+				 const struct hci_cp_le_conn_update *cp,
+				 u32 timeout)
+{
+	return __hci_cmd_sync_le_match(hdev, HCI_OP_LE_CONN_UPDATE, sizeof(*cp),
+				       cp, HCI_EV_LE_CONN_UPDATE_COMPLETE,
+				       timeout, le_conn_update_match);
+}
+
 /* This function requires the caller holds hdev->req_lock. */
 struct sk_buff *__hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
 			       const void *param, u32 timeout)


  reply	other threads:[~2026-12-04  1:03 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 18:41 [RFC PATCH v3 0/2] Bluetooth: Load LE connection event length hints through MGMT Kyle Pepper
2026-12-03 18:41 ` [RFC PATCH v3 1/2] Bluetooth: Allow custom matching of synchronous LE events Kyle Pepper
2026-12-04  2:10   ` Bluetooth: Load LE connection event length hints through MGMT bluez.test.bot
2026-12-03 18:41 ` [RFC PATCH v3 2/2] " Kyle Pepper
2026-12-04  1:02 ` [RFC PATCH v4 0/2] " Kyle Pepper
2026-12-04  1:03   ` Kyle Pepper [this message]
2026-12-04  2:19     ` bluez.test.bot
2026-12-04  1:03   ` [RFC PATCH v4 2/2] " Kyle Pepper

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=20261204010225.3029996-2-cochcoder@proton.me \
    --to=cochcoder@proton.me \
    --cc=linux-bluetooth@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