From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=185.70.43.16 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796346202; b=v65MmPq9XXN+FtuYdwAfP4v/1iLPvWxdavGlXd9uOOqy1sRkRwO1PRBVGPTrjmPvbp6t V7WuadVMTZU+DI8W9YMkqS6bFOnO2lo2XU8vguWM0tki9avKUGPKty/nWT9/ttADJsz6/ r6QioM1CehxXwH3OZFQk5CXt9Hpc9l3jHOsz/w0DVG0BlPDXa4hiLX5mFsm5zF5hFHmr5 NalwWTXB0jlIkFtenm+b3rFBC9YHpBlW97wGMPQI+J6O7Yf9eQPKTMYgBaL+CjSbE4Q5m +oeils6DdsXg5S3plMGCrJLBgpZAK0PuZf+NjXn9Bx8qleBsvXqik1iFvf9hFEV2ztQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796346202; h=DMARC-Filter:DKIM-Signature:Date:To:From:Cc:Subject:Message-ID: In-Reply-To:References:Feedback-ID:X-Pm-Message-ID:MIME-Version: Content-Type:Content-Transfer-Encoding; bh=uzAwsY5P355HgpYrUJV+dRD6DORfBFx/wXufszGnGbQ=; b=a+z9WLZHsKEOpOvcyhb0rPsOIRiW3FI2Z7NdGXW6HpO2jVDm2j53Ooaofa/CEeW6ciEf d9qXwFsFJv7yCtebzP3xNd6b9naIsWO458PbooJE/G4PD9kgNLV2P6S/w8JT4WudRbS22 fggo4cnceUpNxMd0o2CAENaHerHntB3DlyADA4ExpxdKzsyA5e6kB3fgdHVyhWI1PY12i jNbjLp+Mxa31nw2KxBX0rZbS8/LArxIzdjtu4VUPTf6OEptStRM9deZfmAZ7FA/4Y1boF +ZqynWZ/74q6EgdKJSB4cr4qOkGkMdKLGcrpdtJayCussrrtrE6EEJyl++/U+oyY7Ng== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=proton.me; spf=pass smtp.mailfrom=proton.me; dkim=pass header.d=proton.me header.i=@proton.me header.a=rsa-sha256 header.s=protonmail2 header.b=mUZLMa1n; arc=none smtp.remote-ip=185.70.43.16 Received: from mail-4316.protonmail.ch (mail-4316.protonmail.ch [185.70.43.16]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 422E1606A322 for ; Fri, 4 Dec 2026 01:03:22 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 422E1606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=proton.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=proton.me Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=proton.me header.i=@proton.me header.a=rsa-sha256 header.s=protonmail2 header.b=mUZLMa1n DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail2; t=1796346199; x=1796605399; bh=uzAwsY5P355HgpYrUJV+dRD6DORfBFx/wXufszGnGbQ=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=mUZLMa1nk2u2sl03i9ffkxbWL0oHmK7xY0nDHoYszmlA9K00qPevFHZyM0TgPwLzK HA59Ek8zXjaepdBMgoWO2Oip6Nxpw49LwbI7hS1351Tyx7LaTXAGKF8vVPtIVnnPQm lxjNN+u5ZIogMBn5pXovxsaT68MTfisVEBMij3gX71MRry+BHM8xOXvmAIf9vvHrQu LED0O3kj6+zIKsa7S48Ktli8Vm0ee8XMq8j7BXwXFLdsO0zZTXWAKFQ5EOrFHZ0+Jp p184457xZVeknlBoh9us5h9opawwYVEqeKkxKPYBPRTdjAcpIhXlpHx23D9Ui1APQl TvXwukYFOXEZg== Date: Fri, 04 Dec 2026 01:03:15 +0000 To: linux-bluetooth@vger.kernel.org From: Kyle Pepper Cc: luiz.dentz@gmail.com, marcel@holtmann.org Subject: [RFC PATCH v4 1/2] Bluetooth: Allow custom matching of synchronous LE events Message-ID: <20261204010225.3029996-2-cochcoder@proton.me> In-Reply-To: <20261204010225.3029996-1-cochcoder@proton.me> References: <20261203184044.134357-1-cochcoder@proton.me> <20261204010225.3029996-1-cochcoder@proton.me> Feedback-ID: 97716137:user:proton X-Pm-Message-ID: 4742aed41354649f460015614b1d932ad69710fb X-Mailing-List: linux-bluetooth@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable 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 --- 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/blue= tooth.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_pro= to, #define HCI_REQ_START=09BIT(0) #define HCI_REQ_SKB=09BIT(1) =20 +/** + * typedef hci_req_match_t - match an LE event against a pending sync comm= and + * @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, +=09=09=09 const struct sk_buff *event, u8 *status); + struct hci_ctrl { =09struct sock *sk; =09u16 opcode; diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_c= ore.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 { =09__u32=09=09=09req_status; =09__u32=09=09=09req_result; =09struct sk_buff=09=09*req_skb; +=09/* Protected by hdev->lock; the sync request owner holds req_lock. */ +=09hci_req_match_t=09=09req_match; =09struct sk_buff=09=09*req_rsp; =20 =09void=09=09=09*smp_data; diff --git a/include/net/bluetooth/hci_sync.h b/include/net/bluetooth/hci_s= ync.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 { }; =20 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, +=09=09=09=09 const struct hci_cp_le_conn_update *cp, +=09=09=09=09 u32 timeout); =20 struct sk_buff *hci_cmd_sync_alloc(struct hci_dev *hdev, u16 opcode, u32 p= len, =09=09=09=09 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, v= oid *data) =20 =09hci_dev_unlock(hdev); =20 -=09err =3D __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CONN_UPDATE, -=09=09=09=09 sizeof(cp), &cp, -=09=09=09=09 HCI_EV_LE_CONN_UPDATE_COMPLETE, -=09=09=09=09 timeout, NULL); +=09err =3D hci_le_conn_update_wait_sync(hdev, &cp, timeout); =09if (err) =09=09return err; =20 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, v= oid *data, { =09struct hci_ev_le_meta *ev =3D data; =09const struct hci_le_ev *subev; +=09struct sk_buff *cmd; +=09hci_req_match_t match; +=09u8 result =3D 0; =20 =09bt_dev_dbg(hdev, "subevent 0x%2.2x", ev->subevent); =20 -=09/* Only match event if command OGF is for LE */ -=09if (hdev->req_skb && -=09 (hci_opcode_ogf(hci_skb_opcode(hdev->req_skb)) =3D=3D 0x08 || -=09 hci_skb_opcode(hdev->req_skb) =3D=3D HCI_OP_NOP) && -=09 hci_skb_event(hdev->req_skb) =3D=3D ev->subevent) { -=09=09*opcode =3D hci_skb_opcode(hdev->req_skb); -=09=09hci_req_cmd_complete(hdev, *opcode, 0x00, req_complete, -=09=09=09=09 req_complete_skb); +=09/* Avoid taking hdev->lock for reports with no pending command. */ +=09if (READ_ONCE(hdev->req_skb)) { +=09=09hci_dev_lock(hdev); +=09=09cmd =3D hdev->req_skb; +=09=09if (cmd && (hci_opcode_ogf(hci_skb_opcode(cmd)) =3D=3D 0x08 || +=09=09=09 hci_skb_opcode(cmd) =3D=3D HCI_OP_NOP) && +=09=09 hci_skb_event(cmd) =3D=3D ev->subevent) { +=09=09=09match =3D hdev->req_match; +=09=09=09if (!match || +=09=09=09 (READ_ONCE(hdev->req_status) =3D=3D HCI_REQ_PEND && +=09=09=09 match(cmd, skb, &result))) { +=09=09=09=09*opcode =3D hci_skb_opcode(cmd); +=09=09=09=09*status =3D result; +=09=09=09=09hci_req_cmd_complete(hdev, *opcode, result, req_complete, +=09=09=09=09=09=09 req_complete_skb); +=09=09=09} +=09=09} +=09=09hci_dev_unlock(hdev); =09} =20 =09subev =3D &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" =20 +static void hci_cmd_sync_clear_req_skb(struct hci_dev *hdev) +{ +=09if (hdev->req_skb) { +=09=09struct sock *sk =3D hci_skb_sk(hdev->req_skb); + +=09=09if (sk) +=09=09=09sock_put(sk); +=09} +=09kfree_skb(hdev->req_skb); +=09hdev->req_skb =3D NULL; +} + static void hci_cmd_sync_complete(struct hci_dev *hdev, u8 result, u16 opc= ode, =09=09=09=09 struct sk_buff *skb) { @@ -36,14 +48,7 @@ static void hci_cmd_sync_complete(struct hci_dev *hdev, = u8 result, u16 opcode, =09 * response skb doesn't carry it. =09 */ =09hci_dev_lock(hdev); -=09if (hdev->req_skb) { -=09=09struct sock *sk =3D hci_skb_sk(hdev->req_skb); - -=09=09if (sk) -=09=09=09sock_put(sk); -=09} -=09kfree_skb(hdev->req_skb); -=09hdev->req_skb =3D NULL; +=09hci_cmd_sync_clear_req_skb(hdev); =09hci_dev_unlock(hdev); =20 =09if (skb) @@ -156,10 +161,49 @@ static void hci_request_init(struct hci_request *req,= struct hci_dev *hdev) =09req->err =3D 0; } =20 +static void hci_req_match_set(struct hci_dev *hdev, hci_req_match_t match) +{ +=09hci_dev_lock(hdev); +=09hdev->req_match =3D match; +=09hci_dev_unlock(hdev); +} + +struct hci_req_match_cleanup { +=09struct work_struct work; +=09struct hci_dev *hdev; +}; + +static void hci_cmd_sync_match_abort_work(struct work_struct *work) +{ +=09struct hci_req_match_cleanup *cleanup =3D +=09=09container_of(work, struct hci_req_match_cleanup, work); +=09struct hci_dev *hdev =3D cleanup->hdev; + +=09hci_dev_lock(hdev); +=09hci_dev_clear_flag(hdev, HCI_CMD_PENDING); +=09hci_cmd_sync_clear_req_skb(hdev); +=09if (READ_ONCE(hdev->req_status) =3D=3D HCI_REQ_PEND) +=09=09WRITE_ONCE(hdev->req_status, 0); +=09hdev->req_match =3D NULL; +=09hci_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) +{ +=09struct hci_req_match_cleanup cleanup =3D { .hdev =3D hdev }; + +=09INIT_WORK_ONSTACK(&cleanup.work, hci_cmd_sync_match_abort_work); +=09queue_work(hdev->workqueue, &cleanup.work); +=09flush_work(&cleanup.work); +=09destroy_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 pl= en, -=09=09=09=09 const void *param, u8 event, u32 timeout, -=09=09=09=09 struct sock *sk) +static struct sk_buff *hci_cmd_sync_match(struct hci_dev *hdev, u16 opcode= , +=09=09=09=09=09 u32 plen, const void *param, +=09=09=09=09=09 u8 event, u32 timeout, struct sock *sk, +=09=09=09=09=09 hci_req_match_t match) { =09struct hci_request req; =09struct sk_buff *skb; @@ -170,19 +214,31 @@ struct sk_buff *__hci_cmd_sync_sk(struct hci_dev *hde= v, u16 opcode, u32 plen, =09hci_request_init(&req, hdev); =20 =09hci_cmd_sync_add(&req, opcode, plen, param, event, sk); +=09if (match) { +=09=09if (req.err) +=09=09=09return ERR_PTR(req.err); +=09=09hci_req_match_set(hdev, match); +=09} =20 =09WRITE_ONCE(hdev->req_status, HCI_REQ_PEND); =20 =09err =3D hci_req_sync_run(&req); -=09if (err < 0) +=09if (err < 0 && !match) =09=09return ERR_PTR(err); =20 -=09err =3D wait_event_interruptible_timeout(hdev->req_wait_q, -=09=09=09=09=09 READ_ONCE(hdev->req_status) !=3D HCI_REQ_PEND, -=09=09=09=09=09 timeout); - -=09if (err =3D=3D -ERESTARTSYS) -=09=09return ERR_PTR(-EINTR); +=09if (!err) +=09=09err =3D wait_event_interruptible_timeout(hdev->req_wait_q, +=09=09=09=09=09=09 READ_ONCE(hdev->req_status) !=3D HCI_REQ_PEND, +=09=09=09=09=09=09 timeout); +=09if (match) +=09=09hci_cmd_sync_match_abort(hdev); +=09if (err =3D=3D -ERESTARTSYS) { +=09=09if (!match) +=09=09=09return ERR_PTR(-EINTR); +=09=09err =3D -EINTR; +=09} +=09if (err < 0) +=09=09goto done; =20 =09switch (READ_ONCE(hdev->req_status)) { =09case HCI_REQ_DONE: @@ -198,6 +254,7 @@ struct sk_buff *__hci_cmd_sync_sk(struct hci_dev *hdev,= u16 opcode, u32 plen, =09=09break; =09} =20 +done: =09WRITE_ONCE(hdev->req_status, 0); =09hdev->req_result =3D 0; =09skb =3D hdev->req_rsp; @@ -218,8 +275,62 @@ struct sk_buff *__hci_cmd_sync_sk(struct hci_dev *hdev= , u16 opcode, u32 plen, =20 =09return skb; } + +struct sk_buff *__hci_cmd_sync_sk(struct hci_dev *hdev, u16 opcode, u32 pl= en, +=09=09=09=09 const void *param, u8 event, u32 timeout, +=09=09=09=09 struct sock *sk) +{ +=09return hci_cmd_sync_match(hdev, opcode, plen, param, event, timeout, sk= , +=09=09=09=09 NULL); +} EXPORT_SYMBOL(__hci_cmd_sync_sk); =20 +/* Caller must hold hdev->req_lock. */ +static int __hci_cmd_sync_le_match(struct hci_dev *hdev, u16 opcode, u32 p= len, +=09=09=09=09 const void *param, u8 event, u32 timeout, +=09=09=09=09 hci_req_match_t match) +{ +=09struct sk_buff *skb; + +=09if (match && hci_opcode_ogf(opcode) !=3D 0x08) +=09=09return -EINVAL; + +=09skb =3D hci_cmd_sync_match(hdev, opcode, plen, param, event, timeout, N= ULL, +=09=09=09=09 match); +=09if (IS_ERR(skb)) +=09=09return PTR_ERR(skb) =3D=3D -ENODATA ? 0 : PTR_ERR(skb); + +=09kfree_skb(skb); +=09return 0; +} + +static bool le_conn_update_match(const struct sk_buff *cmd, +=09=09=09=09 const struct sk_buff *event, u8 *status) +{ +=09struct hci_ev_le_conn_update_complete ev_buf; +=09struct hci_cp_le_conn_update cp_buf; +=09const struct hci_ev_le_conn_update_complete *ev; +=09const struct hci_cp_le_conn_update *cp; + +=09cp =3D skb_header_pointer(cmd, HCI_COMMAND_HDR_SIZE, sizeof(cp_buf), +=09=09=09=09&cp_buf); +=09ev =3D skb_header_pointer(event, 0, sizeof(ev_buf), &ev_buf); +=09if (!cp || !ev || cp->handle !=3D ev->handle) +=09=09return false; + +=09*status =3D ev->status; +=09return true; +} + +int hci_le_conn_update_wait_sync(struct hci_dev *hdev, +=09=09=09=09 const struct hci_cp_le_conn_update *cp, +=09=09=09=09 u32 timeout) +{ +=09return __hci_cmd_sync_le_match(hdev, HCI_OP_LE_CONN_UPDATE, sizeof(*cp)= , +=09=09=09=09 cp, HCI_EV_LE_CONN_UPDATE_COMPLETE, +=09=09=09=09 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, =09=09=09 const void *param, u32 timeout)