Skip to contentSkip to conversation

Bluetooth: hci_serdev: stop write_work before closing the port

PATCH Superseded Zhao Li 3 participants 4 messages Nov 20 – Nov 21

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Zhao Li

author
#0 lore raw

PATCH v2 1/1 Bluetooth: hci_serdev: stop write_work before closing the port

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")

---
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/[email protected]/

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(+)
drivers/bluetooth/hci_serdev.c +13 -0
index 194f11251cc5..a5e4b3d32144 100644
@@ -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

Signature · 1 line
2.56.1
#1 lore raw

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/1216955/

---Test result---

Test Summary:

CheckPatch                    PASS      0.58 seconds
VerifyFixes                   PASS      0.09 seconds
VerifySignedoff               PASS      0.09 seconds
GitLint                       PASS      0.24 seconds
SubjectPrefix                 PASS      0.08 seconds
BuildKernel                   PASS      24.55 seconds
CheckAllWarning               PASS      29.29 seconds
CheckSparse                   PASS      30.55 seconds
BuildKernel32                 PASS      24.23 seconds
CheckKernelLLVM               PASS      26.12 seconds
TestRunnerSetup               PASS      554.79 seconds
IncrementalBuild              PASS      23.44 seconds

https://github.com/bluez/bluetooth-next/pull/1164

---
Regards,
Linux Bluetooth

#2 lore raw
In reply to Zhao Li
Quoted 51 lines · Zhao Li

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")

---
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/[email protected]/

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(+)
drivers/bluetooth/hci_serdev.c +0 -0
index 194f11251cc5..a5e4b3d32144 100644

drivers/bluetooth/hci_serdev.c static int hci_uart_close(struct hci_dev *hdev) · line 173

Quoted 10 lines
drivers/bluetooth/hci_serdev.c +6 -0
@@ -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);

Looks like you are mostly re-doing what has already been done in hci_uart_flush()...
Wouldn't it be simpler to have a hci_uart_flush parameter to not reschedule tx on flush?

Quoted 16 lines
drivers/bluetooth/hci_serdev.c +7 -0
+
+               /* 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

author
#3 lore raw
Quoted 8 lines · Loic Poulain

drivers/bluetooth/hci_serdev.c static int hci_uart_close(struct hci_dev *hdev) · line 173

Quoted 3 lines
drivers/bluetooth/hci_serdev.c +3 -0
+               cancel_work_sync(&hu->write_work);
+               clear_bit(HCI_UART_SENDING, &hu->tx_state);
+               clear_bit(HCI_UART_TX_WAKEUP, &hu->tx_state);

Looks like you are mostly re-doing what has already been done in hci_uart_flush()...
Wouldn't it be simpler to have a hci_uart_flush parameter to not reschedule tx on flush?

Good point. The only reason hci_uart_close() has to stop TX a second time is that hci_uart_flush() unconditionally re-enables write_work at the end.

Skipping just the final wakeup replay wouldn't be enough, though. Once hci_uart_flush() re-enables write_work and clears HCI_UART_SENDING, a serdev write_wakeup callback can still queue write_work before serdev_device_close() runs.

Instead, we can split out a __hci_uart_flush(hdev, bool resume_tx) helper (since hci_uart_flush() itself is the hdev->flush callback):

- Normal flushes pass resume_tx = true and behave as today.
- With HCI_QUIRK_NON_PERSISTENT_SETUP, hci_uart_close() passes resume_tx = false, leaving write_work disabled and HCI_UART_SENDING set across the close.
- After clearing HCI_UART_PROTO_READY under hu->proto_lock (to drain in-flight wakeups) and calling serdev_device_close(), hci_uart_close() clears HCI_UART_TX_WAKEUP, re-enables write_work, and clears HCI_UART_SENDING for the next open.

That drops the duplicate cancel_work_sync() and tx_skb cleanup while keeping write_work completely disabled until the port is closed.

I'll send a v3 along these lines.

Thanks,
Zhao