From: Zhao Li <enderaoelyther@gmail.com>
To: Marcel Holtmann <marcel@holtmann.org>,
Luiz Augusto von Dentz <luiz.dentz@gmail.com>
Cc: linux-bluetooth@vger.kernel.org, linux-kernel@vger.kernel.org,
Michael McAllister <me@michaelmcallister.org>,
stable@vger.kernel.org, Zhao Li <enderaoelyther@gmail.com>
Subject: [PATCH v2] Bluetooth: hci_serdev: stop write_work before closing the port
Date: Fri, 20 Nov 2026 14:20:48 +0800 [thread overview]
Message-ID: <20261120062048.40310-1-enderaoelyther@gmail.com> (raw)
With HCI_QUIRK_NON_PERSISTENT_SETUP, hci_uart_close() clears
HCI_UART_PROTO_READY without hu->proto_lock and closes the serdev port
without waiting for write_work. The hci_uart_flush() just before it
re-enables write_work and may replay a recorded wakeup, and
hci_uart_tx_wakeup() queues it from any caller that passed the
HCI_UART_PROTO_READY test, such as the serdev write_wakeup callback.
write_work can then call ttyport_write_buf() while ttyport_close()
releases the tty, so it writes through a released tty.
A partial frame that write_work leaves in hu->tx_skb also survives into
the next open, which flushes nothing, and would be sent first to the
controller after it is powered on again.
Quiesce the transmit path as hci_uart_unregister_device() does: clear
HCI_UART_PROTO_READY under percpu_down_write() so that no wakeup is in
flight, cancel write_work, free the leftover frame, then close the port.
A wakeup sets HCI_UART_SENDING and HCI_UART_TX_WAKEUP before it queues
write_work, so clear both after the cancel: a work cancelled while
still pending would leave SENDING set, and the next open, which reuses
hu, would never queue write_work again.
Found by code review. Compile-tested with W=1.
Fixes: de8892df72be ("Bluetooth: hci_serdev: Close UART port if NON_PERSISTENT_SETUP is set")
Cc: stable@vger.kernel.org
Signed-off-by: Zhao Li <enderaoelyther@gmail.com>
---
v2:
- Clear HCI_UART_SENDING and HCI_UART_TX_WAKEUP after cancelling
write_work. A pending write_work cancelled there left SENDING set, so
the next power-on never queued write_work (Sashiko's review of v1).
v1: https://lore.kernel.org/linux-bluetooth/20261118124821.3990-1-enderaoelyther@gmail.com/
Before 6.16 the quirk test in hci_uart_close() reads
test_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks); with that
context line the patch applies to 6.12.y, 6.6.y, 6.1.y, 5.15.y and
5.10.y. It applies as is to 6.18.y and 7.3.y.
No new W=1, sparse or smatch warnings.
AI assistance was used.
drivers/bluetooth/hci_serdev.c | 13 +++++++++++++
1 file changed, 13 insertions(+)
diff --git a/drivers/bluetooth/hci_serdev.c b/drivers/bluetooth/hci_serdev.c
index 194f11251cc5..a5e4b3d32144 100644
--- a/drivers/bluetooth/hci_serdev.c
+++ b/drivers/bluetooth/hci_serdev.c
@@ -164,7 +164,20 @@ static int hci_uart_close(struct hci_dev *hdev)
* open may drain the battery.
*/
if (hci_test_quirk(hdev, HCI_QUIRK_NON_PERSISTENT_SETUP)) {
+ percpu_down_write(&hu->proto_lock);
clear_bit(HCI_UART_PROTO_READY, &hu->flags);
+ percpu_up_write(&hu->proto_lock);
+
+ cancel_work_sync(&hu->write_work);
+ clear_bit(HCI_UART_SENDING, &hu->tx_state);
+ clear_bit(HCI_UART_TX_WAKEUP, &hu->tx_state);
+
+ /* Do not send a stale partial frame after the next open. */
+ if (hu->tx_skb) {
+ kfree_skb(hu->tx_skb);
+ hu->tx_skb = NULL;
+ }
+
serdev_device_close(hu->serdev);
}
base-commit: 1d79f61152c315e30e758116e525871732181a98
--
2.56.1
next reply other threads:[~2026-11-20 6:21 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-20 6:20 Zhao Li [this message]
2026-11-20 13:26 ` [v2] Bluetooth: hci_serdev: stop write_work before closing the port bluez.test.bot
2026-11-20 13:39 ` [PATCH v2] " Loic Poulain
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=20261120062048.40310-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=stable@vger.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.