Linux bluetooth development
 help / color / mirror / Atom feed
* [PATCH] Bluetooth: hci_serdev: serialize flush with transmit work
@ 2026-11-16  8:05 Zhao Li
  2026-11-16  9:16 ` bluez.test.bot
  0 siblings, 1 reply; 2+ messages in thread
From: Zhao Li @ 2026-11-16  8:05 UTC (permalink / raw)
  To: Marcel Holtmann, Luiz Augusto von Dentz
  Cc: Rob Herring, Sebastian Reichel, Pavel Machek, Michael McAllister,
	linux-bluetooth, linux-kernel

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


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-16  9:16 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-16  8:05 [PATCH] Bluetooth: hci_serdev: serialize flush with transmit work Zhao Li
2026-11-16  9:16 ` bluez.test.bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox