[PATCH] Bluetooth: hci_serdev: serialize flush with transmit work
From: Zhao Li
Date: Mon Nov 16 2026 - 03:06:06 EST
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@xxxxxxxxxxxxxxx # needs adjustments for <= 7.4
Signed-off-by: Zhao Li <enderaoelyther@xxxxxxxxx>
---
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@xxxxxxxxxxxxxxxxxxxxx
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