Re: [PATCH] Bluetooth: btintel_pcie: Fix context analysis errors on req_lock

From: Luiz Augusto von Dentz

Date: Thu Dec 03 2026 - 11:16:58 EST


Hi Sasha,

On Thu, Dec 3, 2026 at 5:59 AM Sasha Levin <sashal@xxxxxxxxxx> wrote:
>
> With clang 23 and CONFIG_WARN_CONTEXT_ANALYSIS, which drivers/bluetooth
> is opted into, btintel_pcie_yoyo_send_hcmds() triggers two warnings, and
> with CONFIG_WERROR the build fails:
>
> btintel_pcie.c:981:2: error: mutex 'data->hdev->req_lock' is not
> held on every path through here [-Werror,-Wthread-safety-analysis]
> btintel_pcie.c:1049:3: error: releasing mutex 'data->hdev->req_lock'
> that was not held [-Werror,-Wthread-safety-analysis]

Can you give some more context so I can try enabling it in our CI to
avoid committing new code with this problem. (No need to respin just
want a lead to enable this sort of errors in our CI).

> The function takes hdev->req_lock only when its caller does not hold it,
> and releases it at the end under the same condition. The analysis cannot
> match a conditional lock with a later conditional unlock in the same
> function.
>
> Move the part that runs under req_lock into
> btintel_pcie_yoyo_send_hcmds_locked(). Callers that hold the lock go
> straight to it, and the path that takes the lock releases it
> unconditionally. No functional change.
>
> Fixes: 738af1958d2e ("Bluetooth: btintel_pcie: Dispatch yoyo triggers at timepoints")
> Signed-off-by: Sasha Levin <sashal@xxxxxxxxxx>
> ---
> Based on bluetooth-next (738af1958d2e).
>
> The two mac_access_lock errors of the same kind, in
> btintel_pcie_apply_yoyo_config_sets(), are still there after this
> patch. Rosen Penev's patch fixes those:
>
> https://lore.kernel.org/all/20261126000439.318058-1-rosenp@xxxxxxxxx/
>
> It needs "git am -3" on today's bluetooth-next, and can go in before or
> after this one. With both applied drivers/bluetooth builds on x86_64
> allmodconfig with clang 23.1.6.
>
> drivers/bluetooth/btintel_pcie.c | 111 +++++++++++++++++--------------
> 1 file changed, 60 insertions(+), 51 deletions(-)
>
> diff --git a/drivers/bluetooth/btintel_pcie.c b/drivers/bluetooth/btintel_pcie.c
> index ab24c3c75f1b0..a5bef0b24773f 100644
> --- a/drivers/bluetooth/btintel_pcie.c
> +++ b/drivers/bluetooth/btintel_pcie.c
> @@ -923,65 +923,20 @@ static void btintel_pcie_release_mac_access(struct btintel_pcie_data *data)
> }
> }
>
> -/* btintel_pcie_yoyo_send_hcmds - Send all HCI commands configured for a timepoint */
> -static void btintel_pcie_yoyo_send_hcmds(struct btintel_pcie_data *data,
> - enum btintel_pcie_dbg_timepoint tp,
> - bool req_locked)
> +/* Send the HCI commands configured for @tp. The caller holds hdev->req_lock. */
> +static void btintel_pcie_yoyo_send_hcmds_locked(struct btintel_pcie_data *data,
> + enum btintel_pcie_dbg_timepoint tp)
> {
> struct btintel_pcie_yoyo_hcmd *hcmd;
> - struct hci_dev *hdev;
> struct sk_buff *skb;
> u8 plen;
> u16 opcode;
>
> - if (!data)
> - return;
> -
> - hdev = data->hdev;
> - if (!hdev)
> - return;
> -
> - /* __hci_cmd_sync() requires req_lock, taken before yoyo.lock to match
> - * the order setup established. Suspend and resume do not hold it, so
> - * take it only when the timepoint has commands to send.
> - */
> - if (!req_locked) {
> - bool found = false;
> -
> - mutex_lock(&data->yoyo.lock);
> - if (data->yoyo.enabled) {
> - list_for_each_entry(hcmd, &data->yoyo.hcmd_list, list) {
> - if (hcmd->time_point == tp) {
> - found = true;
> - break;
> - }
> - }
> - }
> - mutex_unlock(&data->yoyo.lock);
> -
> - if (!found)
> - return;
> -
> - hci_req_sync_lock(hdev);
> -
> - /* A closed device rejects the commands and a user channel
> - * consumes their completion, so __hci_cmd_sync() would only
> - * fail or time out into a controller reset.
> - */
> - if (!test_bit(HCI_UP, &hdev->flags) ||
> - hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
> - bt_dev_dbg(hdev, "YoYo: hdev down or user channel, tp %u commands skipped",
> - tp);
> - hci_req_sync_unlock(hdev);
> - return;
> - }
> - }
> -
> /* Held across __hci_cmd_sync() so cleanup cannot free entries in use */
> mutex_lock(&data->yoyo.lock);
> if (!data->yoyo.enabled) {
> mutex_unlock(&data->yoyo.lock);
> - goto unlock;
> + return;
> }
>
> list_for_each_entry(hcmd, &data->yoyo.hcmd_list, list) {
> @@ -1044,9 +999,63 @@ static void btintel_pcie_yoyo_send_hcmds(struct btintel_pcie_data *data,
> }
>
> mutex_unlock(&data->yoyo.lock);
> -unlock:
> - if (!req_locked)
> +}
> +
> +/* btintel_pcie_yoyo_send_hcmds - Send all HCI commands configured for a timepoint */
> +static void btintel_pcie_yoyo_send_hcmds(struct btintel_pcie_data *data,
> + enum btintel_pcie_dbg_timepoint tp,
> + bool req_locked)
> +{
> + struct btintel_pcie_yoyo_hcmd *hcmd;
> + struct hci_dev *hdev;
> + bool found = false;
> +
> + if (!data)
> + return;
> +
> + hdev = data->hdev;
> + if (!hdev)
> + return;
> +
> + if (req_locked) {
> + btintel_pcie_yoyo_send_hcmds_locked(data, tp);
> + return;
> + }
> +
> + /* __hci_cmd_sync() requires req_lock, taken before yoyo.lock to match
> + * the order setup established. Suspend and resume do not hold it, so
> + * take it only when the timepoint has commands to send.
> + */
> + mutex_lock(&data->yoyo.lock);
> + if (data->yoyo.enabled) {
> + list_for_each_entry(hcmd, &data->yoyo.hcmd_list, list) {
> + if (hcmd->time_point == tp) {
> + found = true;
> + break;
> + }
> + }
> + }
> + mutex_unlock(&data->yoyo.lock);
> +
> + if (!found)
> + return;
> +
> + hci_req_sync_lock(hdev);
> +
> + /* A closed device rejects the commands and a user channel
> + * consumes their completion, so __hci_cmd_sync() would only
> + * fail or time out into a controller reset.
> + */
> + if (!test_bit(HCI_UP, &hdev->flags) ||
> + hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
> + bt_dev_dbg(hdev, "YoYo: hdev down or user channel, tp %u commands skipped",
> + tp);
> hci_req_sync_unlock(hdev);
> + return;
> + }
> +
> + btintel_pcie_yoyo_send_hcmds_locked(data, tp);
> + hci_req_sync_unlock(hdev);
> }
>
> static void
>
> base-commit: 738af1958d2e7205008ca818352199ae8e454509
> --
> 2.53.0
>


--
Luiz Augusto von Dentz