From: Zhao Li <enderaoelyther@gmail.com>
To: Marcel Holtmann <marcel@holtmann.org>,
Luiz Augusto von Dentz <luiz.dentz@gmail.com>
Cc: Rob Herring <robh@kernel.org>, Sebastian Reichel <sre@kernel.org>,
Pavel Machek <pavel@ucw.cz>,
Michael McAllister <me@michaelmcallister.org>,
linux-bluetooth@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] Bluetooth: hci_serdev: serialize flush with transmit work
Date: Mon, 16 Nov 2026 16:05:38 +0800 [thread overview]
Message-ID: <20261116080538.66575-1-enderaoelyther@gmail.com> (raw)
hci_uart_flush() runs on HCI close and reset and frees hu->tx_skb while
write_work may still be sending the same partial frame. The worker can
then resume with a freed skb, or both paths can free the frame.
The tty flush has stopped write_work before freeing the partial frame
since commit 1b0d946d6f08 ("Bluetooth: hci_uart: clear HCI_UART_SENDING
when write_work is canceled"). Do the same in the serdev flush: disable
and drain write_work before releasing the frame, and hold
HCI_UART_SENDING meanwhile so that hci_uart_tx_wakeup() only records
HCI_UART_TX_WAKEUP. Then re-enable the work, release SENDING and replay
a wakeup that arrived in between.
AI assistance was used.
Fixes: 82f5169bf3d3 ("Bluetooth: hci_uart: add serdev driver support library")
Cc: stable@vger.kernel.org # needs adjustments for <= 7.4
Signed-off-by: Zhao Li <enderaoelyther@gmail.com>
---
Michael McAllister's v2 series changes the same function; its
HCI_UNREGISTER check goes above the new lines:
https://lore.kernel.org/r/179468159400.2456.12381703314197227930.wcn3950-poweroff-v2-2@michaelmcallister.org
Tested in arm64 QEMU under KASAN with a hook that keeps a partial frame
in hu->tx_skb across an HCI close: without this patch write_work reads
the freed frame and the close path frees it twice; with it the run is
clean, the close returns 0 and the device transmits after reopening.
The same holds with the v2 series applied. No new W=1, sparse or smatch
warnings.
drivers/bluetooth/hci_serdev.c | 19 ++++++++++++++-----
1 file changed, 14 insertions(+), 5 deletions(-)
diff --git a/drivers/bluetooth/hci_serdev.c b/drivers/bluetooth/hci_serdev.c
index d2c48df34199..194f11251cc5 100644
--- a/drivers/bluetooth/hci_serdev.c
+++ b/drivers/bluetooth/hci_serdev.c
@@ -97,17 +97,26 @@ static int hci_uart_flush(struct hci_dev *hdev)
BT_DBG("hdev %p serdev %p", hdev, hu->serdev);
+ /* Stop write_work and hold SENDING so wakeups only set TX_WAKEUP. */
+ disable_work_sync(&hu->write_work);
+ set_bit(HCI_UART_SENDING, &hu->tx_state);
+
if (hu->tx_skb) {
kfree_skb(hu->tx_skb); hu->tx_skb = NULL;
}
- if (!test_bit(HCI_UART_PROTO_READY, &hu->flags))
- return 0;
+ if (test_bit(HCI_UART_PROTO_READY, &hu->flags)) {
+ /* Flush any pending characters in the driver and discipline. */
+ serdev_device_write_flush(hu->serdev);
- /* Flush any pending characters in the driver and discipline. */
- serdev_device_write_flush(hu->serdev);
+ hu->proto->flush(hu);
+ }
- hu->proto->flush(hu);
+ /* Resume TX and replay a wakeup recorded while it was stopped. */
+ enable_work(&hu->write_work);
+ clear_bit(HCI_UART_SENDING, &hu->tx_state);
+ if (test_bit(HCI_UART_TX_WAKEUP, &hu->tx_state))
+ hci_uart_tx_wakeup(hu);
return 0;
}
base-commit: da3cc02587c629420603df4395179ec82955c941
--
2.56.1
reply other threads:[~2026-11-16 8:06 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20261116080538.66575-1-enderaoelyther@gmail.com \
--to=enderaoelyther@gmail.com \
--cc=linux-bluetooth@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=luiz.dentz@gmail.com \
--cc=marcel@holtmann.org \
--cc=me@michaelmcallister.org \
--cc=pavel@ucw.cz \
--cc=robh@kernel.org \
--cc=sre@kernel.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
all inboxes | Powered by JetHome®