Linux bluetooth development
 help / color / mirror / Atom feed
* [PATCH 6.6.y] Bluetooth: hci_uart: clear HCI_UART_SENDING when write_work is canceled
@ 2026-11-26  2:45 Dickson Wu
  2026-11-26  3:16 ` [6.6.y] " bluez.test.bot
  2026-11-26 15:12 ` [PATCH 6.6.y] " Dickson Wu
  0 siblings, 2 replies; 3+ messages in thread
From: Dickson Wu @ 2026-11-26  2:45 UTC (permalink / raw)
  To: stable
  Cc: Pauli Virtanen, Luiz Augusto von Dentz, Marcel Holtmann,
	linux-bluetooth

From: Pauli Virtanen <pav@iki.fi>

commit 1b0d946d6f08bd39211385bc703a440911b41e46 upstream.

HCI_UART_SENDING bit in tx_state means write_work is pending and blocks
queueing it again. Currently this bit is not cleared when canceling the
work in hci_uart_close(), which blocks future writes when device is
reopened later if write_work was pending.

Fix by clearing HCI_UART_SENDING when canceling the work.

Also make clearing of tx_skb safe by using disable_work_sync +
enable_work instead of just cancel_work_sync. hci_uart_flush() purges
the proto tx queue so we can cancel the pending write_work there,
instead of doing it just in hci_uart_close(). Re-enable and possibly
requeue the work after queue flush.

Fixes: c1bb9336ae6b ("Bluetooth: hci_uart: fix UAFs and race
conditions in close and init paths")
Link: https://lore.kernel.org/linux-bluetooth/07e0a28650773abec711ee492fdb1bf5d21a6c98.camel@iki.fi/
Cc: stable@vger.kernel.org
Signed-off-by: Pauli Virtanen <pav@iki.fi>
Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
[ adapted to 6.6: no disable_work_sync()/enable_work(), use tx_lock
and HCI_UART_FLUSHING around cancel_work_sync() ]
Signed-off-by: Dickson Wu <dickson.wu@certik.com>
---
6.12.y and 6.18.y already have this. 6.6 has no disable_work_sync() or
enable_work(), so instead of disabling write_work during the flush this
adds a tx_lock and an HCI_UART_FLUSHING bit. hci_uart_tx_wakeup() won't
queue write_work while a flush is running, the flush cancels and waits
for any running work, holds HCI_UART_SENDING while it frees tx_skb, then
clears both bits and replays a wakeup that came in meanwhile. That's the
sequence upstream gets from disable_work_sync() and enable_work().

The serdev follow-up, ee43bade37c4 ("Bluetooth: hci_serdev: serialize
flush with transmit work"), isn't in 6.12.y or 6.18.y yet, so it's not
included here.

Tested on v6.6.163:
- x86_64 allmodconfig W=1 build of drivers/bluetooth/, base vs patched,
  no new warnings
- x86_64 defconfig with BT_HCIUART, BT_HCIUART_SERDEV and BT_HCIUART_H4
  built in, boots under QEMU/KVM
- I don't have UART Bluetooth hardware, so the close/flush race itself
  is untested here. No existing selftest reaches it.

Since the locking here differs from upstream, a look from the Bluetooth
folks would be very welcome.

The backport and the test runs were prepared with AI coding tools
(OpenAI Codex agents, with review by Claude). I went over the result
before signing off.

 drivers/bluetooth/hci_ldisc.c  | 37 ++++++++++++++++++++++++----------
 drivers/bluetooth/hci_serdev.c |  1 +
 drivers/bluetooth/hci_uart.h   |  4 ++++
 3 files changed, 31 insertions(+), 11 deletions(-)

diff --git a/drivers/bluetooth/hci_ldisc.c b/drivers/bluetooth/hci_ldisc.c
index 548e58e69eb7..8c43ffb30725 100644
--- a/drivers/bluetooth/hci_ldisc.c
+++ b/drivers/bluetooth/hci_ldisc.c
@@ -116,6 +116,8 @@ static inline struct sk_buff
*hci_uart_dequeue(struct hci_uart *hu)

 int hci_uart_tx_wakeup(struct hci_uart *hu)
 {
+	unsigned long flags;
+
 	/* This may be called in an IRQ context, so we can't sleep. Therefore
 	 * we try to acquire the lock only, and if that fails we assume the
 	 * tty is being closed because that is the only time the write lock is
@@ -129,13 +131,14 @@ int hci_uart_tx_wakeup(struct hci_uart *hu)
 	    !test_bit(HCI_UART_PROTO_INIT, &hu->flags))
 		goto no_schedule;

+	spin_lock_irqsave(&hu->tx_lock, flags);
 	set_bit(HCI_UART_TX_WAKEUP, &hu->tx_state);
-	if (test_and_set_bit(HCI_UART_SENDING, &hu->tx_state))
-		goto no_schedule;
-
-	BT_DBG("");
-
-	schedule_work(&hu->write_work);
+	if (!test_bit(HCI_UART_FLUSHING, &hu->tx_state) &&
+	    !test_and_set_bit(HCI_UART_SENDING, &hu->tx_state)) {
+		BT_DBG("");
+		schedule_work(&hu->write_work);
+	}
+	spin_unlock_irqrestore(&hu->tx_lock, flags);

 no_schedule:
 	percpu_up_read(&hu->proto_lock);
@@ -176,7 +179,8 @@ static void hci_uart_write_work(struct work_struct *work)
 	}

 	clear_bit(HCI_UART_SENDING, &hu->tx_state);
-	if (test_bit(HCI_UART_TX_WAKEUP, &hu->tx_state))
+	if (test_bit(HCI_UART_TX_WAKEUP, &hu->tx_state) &&
+	    !test_bit(HCI_UART_FLUSHING, &hu->tx_state))
 		goto restart;

 	wake_up_bit(&hu->tx_state, HCI_UART_SENDING);
@@ -236,9 +240,16 @@ static int hci_uart_flush(struct hci_dev *hdev)
 {
 	struct hci_uart *hu  = hci_get_drvdata(hdev);
 	struct tty_struct *tty = hu->tty;
+	unsigned long flags;

 	BT_DBG("hdev %p tty %p", hdev, tty);

+	spin_lock_irqsave(&hu->tx_lock, flags);
+	set_bit(HCI_UART_FLUSHING, &hu->tx_state);
+	spin_unlock_irqrestore(&hu->tx_lock, flags);
+	cancel_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;
 	}
@@ -254,6 +265,13 @@ static int hci_uart_flush(struct hci_dev *hdev)

 	percpu_up_read(&hu->proto_lock);

+	spin_lock_irqsave(&hu->tx_lock, flags);
+	clear_bit(HCI_UART_FLUSHING, &hu->tx_state);
+	clear_bit(HCI_UART_SENDING, &hu->tx_state);
+	spin_unlock_irqrestore(&hu->tx_lock, flags);
+	if (test_bit(HCI_UART_TX_WAKEUP, &hu->tx_state))
+		hci_uart_tx_wakeup(hu);
+
 	return 0;
 }

@@ -271,12 +289,8 @@ static int hci_uart_open(struct hci_dev *hdev)
 /* Close device */
 static int hci_uart_close(struct hci_dev *hdev)
 {
-	struct hci_uart *hu = hci_get_drvdata(hdev);
-
 	BT_DBG("hdev %p", hdev);

-	cancel_work_sync(&hu->write_work);
-
 	hci_uart_flush(hdev);
 	hdev->flush = NULL;
 	return 0;
@@ -516,6 +530,7 @@ static int hci_uart_tty_open(struct tty_struct *tty)

 	tty->disc_data = hu;
 	hu->tty = tty;
+	spin_lock_init(&hu->tx_lock);
 	tty->receive_room = 65536;

 	/* disable alignment support by default */
diff --git a/drivers/bluetooth/hci_serdev.c b/drivers/bluetooth/hci_serdev.c
index 753343ca8489..835ec8f2f09e 100644
--- a/drivers/bluetooth/hci_serdev.c
+++ b/drivers/bluetooth/hci_serdev.c
@@ -313,6 +313,7 @@ int hci_uart_register_device_priv(struct hci_uart *hu,

 	if (percpu_init_rwsem(&hu->proto_lock))
 		return -ENOMEM;
+	spin_lock_init(&hu->tx_lock);

 	err = serdev_device_open(hu->serdev);
 	if (err)
diff --git a/drivers/bluetooth/hci_uart.h b/drivers/bluetooth/hci_uart.h
index 39f39704be41..2687f76cc01b 100644
--- a/drivers/bluetooth/hci_uart.h
+++ b/drivers/bluetooth/hci_uart.h
@@ -12,6 +12,8 @@
 #define N_HCI	15
 #endif

+#include <linux/spinlock.h>
+
 /* Ioctls */
 #define HCIUARTSETPROTO		_IOW('U', 200, int)
 #define HCIUARTGETPROTO		_IOR('U', 201, int)
@@ -76,6 +78,7 @@ struct hci_uart {

 	struct sk_buff		*tx_skb;
 	unsigned long		tx_state;
+	spinlock_t		tx_lock;

 	unsigned int init_speed;
 	unsigned int oper_speed;
@@ -94,6 +97,7 @@ struct hci_uart {
 /* TX states  */
 #define HCI_UART_SENDING	1
 #define HCI_UART_TX_WAKEUP	2
+#define HCI_UART_FLUSHING	3

 int hci_uart_register_proto(const struct hci_uart_proto *p);
 int hci_uart_unregister_proto(const struct hci_uart_proto *p);

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

end of thread, other threads:[~2026-11-26 15:12 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26  2:45 [PATCH 6.6.y] Bluetooth: hci_uart: clear HCI_UART_SENDING when write_work is canceled Dickson Wu
2026-11-26  3:16 ` [6.6.y] " bluez.test.bot
2026-11-26 15:12 ` [PATCH 6.6.y] " Dickson Wu

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