Linux bluetooth development
 help / color / mirror / Atom feed
From: Hans de Goede <johannes.goede@oss.qualcomm.com>
To: Luiz Augusto von Dentz <luiz.dentz@gmail.com>
Cc: Marcel Holtmann <marcel@holtmann.org>,
	Ibrahim Abdelkader <iabdelka@qti.qualcomm.com>,
	linux-arm-msm@vger.kernel.org, linux-bluetooth@vger.kernel.org
Subject: Re: [PATCH 2/2] Bluetooth: hci_core: Handle skb_clone() failure in hci_send_cmd_sync()
Date: Wed, 19 Aug 2026 14:38:47 +0200	[thread overview]
Message-ID: <73afb7ba-53c8-423e-81a2-7e923896c995@oss.qualcomm.com> (raw)
In-Reply-To: <CABBYNZ+Bhhx3rtq8i_FGu2a_oFyWquo_gTRcmPyM6HcFC3pkVg@mail.gmail.com>

Hi Luiz,

On 18-Aug-26 20:01, Luiz Augusto von Dentz wrote:
> Hi Hans, Ibrahim,
> 
> On Mon, Aug 17, 2026 at 4:50 PM Hans de Goede
> <johannes.goede@oss.qualcomm.com> wrote:
>>
>> From: Ibrahim Abdelkader <iabdelka@qti.qualcomm.com>
>>
>> hci_send_cmd_sync() does not handle the second skb_clone() failure as it
>> does the first one. At that point the command has already been sent and
>> HCI_CMD_PENDING is set, so an allocation failure leaves the flag set with
>> req_skb NULL. The response then matches nothing and the caller blocks for
>> HCI_INIT_TIMEOUT before getting -ETIMEDOUT, reporting a memory shortage as
>> a controller timeout.
>>
>> Clear the flag, cancel the pending request with -ENOMEM so the caller fails
>> immediately instead of waiting out the timeout, and return -ENOMEM so
>> hci_cmd_work() does not arm cmd_timer for a request that no longer exists.
>>
>> Clearing HCI_CMD_PENDING here is safe: all code touching that flag runs
>> from hdev->workqueue, which is an ordered workqueue, so those paths are
>> serialised and cannot race. The one exception is the cleanup added by
>> commit ed516e3f1aa6 ("Bluetooth: hci_sync: Clear HCI_CMD_PENDING when
>> dropping the last request"), which runs on hci_dev_open_sync() failure and
>> from hci_dev_close_sync(), and the workqueue is stopped before that runs.
>>
>> Fixes: 2615fd9a7c25 ("Bluetooth: hci_sync: Fix overwriting request callback")
>> Signed-off-by: Ibrahim Abdelkader <iabdelka@qti.qualcomm.com>
>> Signed-off-by: Hans de Goede <johannes.goede@oss.qualcomm.com>
>> ---
>>  net/bluetooth/hci_core.c | 5 +++++
>>  1 file changed, 5 insertions(+)
>>
>> diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c
>> index 8aeb3024428b..9585b42e7970 100644
>> --- a/net/bluetooth/hci_core.c
>> +++ b/net/bluetooth/hci_core.c
>> @@ -4092,6 +4092,11 @@ static int hci_send_cmd_sync(struct hci_dev *hdev, struct sk_buff *skb)
>>             !hci_dev_test_and_set_flag(hdev, HCI_CMD_PENDING)) {
>>                 kfree_skb(hdev->req_skb);
>>                 hdev->req_skb = skb_clone(hdev->sent_cmd, GFP_KERNEL);
>> +               if (!hdev->req_skb) {
>> +                       hci_dev_clear_flag(hdev, HCI_CMD_PENDING);
>> +                       hci_cmd_sync_cancel_sync(hdev, ENOMEM);
>> +                       err = -ENOMEM;
>> +               }
> 
> Sashiko flagged a problem with this change:
> 
> https://sashiko.dev/#/patchset/20260817205017.56524-1-johannes.goede%40oss.qualcomm.com
> 
> Im with the opinion that this shouldn't cancel the request, because at
> this point it may already been sent to the hardware so maybe we should
> flag it somehow that req_skb wasn't able to be allocated, or something
> like that, but the command is still outstanding.

Ok, so maybe we should move the hdev->req_skb = skb_clone(hdev->sent_cmd, ...);
to above hci_send_frame() ?  I don't really see a reason why that cannot
be done first.

And then the hdev->req_skb = skb_clone(hdev->sent_cmd, ...) and
hci_send_frame() error handling could share a common err path both
calling hci_cmd_sync_cancel_sync() ?


Also while looking at this I noticed that hci_send_cmd_sync() exits with
ENODATA when hci_skb_opcode(skb) == HCI_OP_NOP, but at this point it
has already set hdev->sent_cmd and will also still set hdev->req_skb
if the conditions for that are met.

I wonder if this is the right thing to do, or if the hci_skb_opcode(skb) ==
HCI_OP_NOP check should be moved higher up in the function followed by
an immediate kfree_skb(skb) + return -ENODATA ?

Regards,

Hans


  reply	other threads:[~2026-08-19 12:38 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-17 20:50 [PATCH 1/2] Bluetooth: hci_core: Return -ENOMEM when the sent_cmd clone fails Hans de Goede
2026-08-17 20:50 ` [PATCH 2/2] Bluetooth: hci_core: Handle skb_clone() failure in hci_send_cmd_sync() Hans de Goede
2026-08-18 18:01   ` Luiz Augusto von Dentz
2026-08-19 12:38     ` Hans de Goede [this message]
2026-08-19 16:42       ` Luiz Augusto von Dentz
2026-08-19 20:01         ` Hans de Goede
2026-08-17 21:46 ` [1/2] Bluetooth: hci_core: Return -ENOMEM when the sent_cmd clone fails bluez.test.bot
2026-08-18 18:37 ` [PATCH 1/2] " patchwork-bot+bluetooth

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=73afb7ba-53c8-423e-81a2-7e923896c995@oss.qualcomm.com \
    --to=johannes.goede@oss.qualcomm.com \
    --cc=iabdelka@qti.qualcomm.com \
    --cc=linux-arm-msm@vger.kernel.org \
    --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