Bluetooth: hci_serdev: serialize flush with transmit work
PATCH 1/1 Bluetooth: hci_serdev: serialize flush with transmit work
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.
---
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(-)
drivers/bluetooth/hci_serdev.c +14 -5
@@ -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
Signature · 1 line
2.56.1
This is automated email and please do not reply to this email!
Dear submitter,
Thank you for submitting the patches to the linux bluetooth mailing list. This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/series/1212408/
---Test result---
Test Summary:
CheckPatch PASS 0.59 seconds VerifyFixes PASS 0.09 seconds VerifySignedoff PASS 0.09 seconds GitLint PASS 0.25 seconds SubjectPrefix PASS 0.09 seconds BuildKernel PASS 22.83 seconds CheckAllWarning PASS 26.23 seconds CheckSparse PASS 28.24 seconds BuildKernel32 PASS 23.16 seconds CheckKernelLLVM PASS 23.92 seconds TestRunnerSetup PASS 544.36 seconds IncrementalBuild PASS 21.55 seconds
https://github.com/bluez/bluetooth-next/pull/1139
---
Regards,
Linux Bluetooth