Linux bluetooth development
 help / color / mirror / Atom feed
* [PATCH] Bluetooth: mgmt: disable discoverable work before device lock
@ 2026-12-02  2:06 Baek Kyu, Kim
  2026-12-02  3:02 ` bluez.test.bot
  0 siblings, 1 reply; 2+ messages in thread
From: Baek Kyu, Kim @ 2026-12-02  2:06 UTC (permalink / raw)
  To: Marcel Holtmann, Luiz Augusto von Dentz; +Cc: linux-bluetooth

mgmt_index_removed() synchronously cancels discov_off while its callers
hold hdev->lock. The delayed work also takes hdev->lock, so index removal
can wait for a worker that is waiting for the remover's lock.

Drain the work before taking hdev->lock. Keep it disabled across index
removal so another management command cannot requeue it between the
cancellation and the lock acquisition. Re-enable it under hdev->lock
after temporary configuration and user-channel transitions; leave it
disabled during unregister.

Use a locked precheck to skip draining work when a configuration
request cannot remove the index, including unchanged options.

Signed-off-by: Baek Kyu, Kim <pocj8ur4in@gmail.com>
---
On arm64 QEMU/KVM, an instrumented baseline stalled during index
removal while discov_off waited for hdev->lock. The changed kernel
completed teardown. Test-only virtual-controller setup exercised both
configuration transitions and user-channel bind; attempted requeues
were blocked while the work was disabled. Unchanged configuration
requests left it enabled. Normal discoverable timeout and user-channel
tests passed. No physical controller was tested.

 net/bluetooth/hci_core.c |  3 +++
 net/bluetooth/hci_sock.c |  7 +++++++
 net/bluetooth/mgmt.c     | 34 +++++++++++++++++++++++++++++++---
 3 files changed, 41 insertions(+), 3 deletions(-)

diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c
index 255005916f37..ced6996aa1b4 100644
--- a/net/bluetooth/hci_core.c
+++ b/net/bluetooth/hci_core.c
@@ -2778,6 +2778,9 @@ void hci_unregister_dev(struct hci_dev *hdev)
 	if (!test_bit(HCI_INIT, &hdev->flags) &&
 	    !hci_dev_test_flag(hdev, HCI_SETUP) &&
 	    !hci_dev_test_flag(hdev, HCI_CONFIG)) {
+		if (hci_dev_test_flag(hdev, HCI_MGMT))
+			disable_delayed_work_sync(&hdev->discov_off);
+
 		hci_dev_lock(hdev);
 		mgmt_index_removed(hdev);
 		hci_dev_unlock(hdev);
diff --git a/net/bluetooth/hci_sock.c b/net/bluetooth/hci_sock.c
index 3da994d1fe65..098373d14b44 100644
--- a/net/bluetooth/hci_sock.c
+++ b/net/bluetooth/hci_sock.c
@@ -1208,6 +1208,7 @@ static int hci_sock_bind(struct socket *sock, struct sockaddr_unsized *addr,
 	struct sock *sk = sock->sk;
 	struct hci_dev *hdev = NULL;
 	struct sk_buff *skb;
+	bool discov_disabled = false;
 	int len, err = 0;
 
 	BT_DBG("sock %p sk %p", sock, sk);
@@ -1329,8 +1330,14 @@ static int hci_sock_bind(struct socket *sock, struct sockaddr_unsized *addr,
 			goto done;
 		}
 
+		discov_disabled = hci_dev_test_flag(hdev, HCI_MGMT);
+		if (discov_disabled)
+			disable_delayed_work_sync(&hdev->discov_off);
+
 		hci_dev_lock(hdev);
 		mgmt_index_removed(hdev);
+		if (discov_disabled)
+			enable_delayed_work(&hdev->discov_off);
 		hci_dev_unlock(hdev);
 
 		err = hci_dev_open(hdev->id);
diff --git a/net/bluetooth/mgmt.c b/net/bluetooth/mgmt.c
index e6876c76ca24..9156888c5f29 100644
--- a/net/bluetooth/mgmt.c
+++ b/net/bluetooth/mgmt.c
@@ -8588,7 +8588,8 @@ static int set_external_config(struct sock *sk, struct hci_dev *hdev,
 			       void *data, u16 len)
 {
 	struct mgmt_cp_set_external_config *cp = data;
-	bool changed;
+	bool discov_disabled = false;
+	bool changed, configured;
 	int err;
 
 	bt_dev_dbg(hdev, "sock %p", sk);
@@ -8606,6 +8607,19 @@ static int set_external_config(struct sock *sk, struct hci_dev *hdev,
 				       MGMT_STATUS_NOT_SUPPORTED);
 
 	hci_dev_lock(hdev);
+	changed = cp->config != hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED);
+	configured = cp->config &&
+		(!(hci_test_quirk(hdev, HCI_QUIRK_INVALID_BDADDR) ||
+		   hci_test_quirk(hdev, HCI_QUIRK_USE_BDADDR_PROPERTY)) ||
+		 !!bacmp(&hdev->public_addr, BDADDR_ANY));
+	/* Drain the work only if this request may remove the index. */
+	if (changed && hci_dev_test_flag(hdev, HCI_UNCONFIGURED) == configured &&
+	    hci_dev_test_flag(hdev, HCI_MGMT)) {
+		hci_dev_unlock(hdev);
+		disable_delayed_work_sync(&hdev->discov_off);
+		discov_disabled = true;
+		hci_dev_lock(hdev);
+	}
 
 	if (cp->config)
 		changed = !hci_dev_test_and_set_flag(hdev, HCI_EXT_CONFIGURED);
@@ -8636,6 +8650,8 @@ static int set_external_config(struct sock *sk, struct hci_dev *hdev,
 	}
 
 unlock:
+	if (discov_disabled)
+		enable_delayed_work(&hdev->discov_off);
 	hci_dev_unlock(hdev);
 	return err;
 }
@@ -8644,7 +8660,8 @@ static int set_public_address(struct sock *sk, struct hci_dev *hdev,
 			      void *data, u16 len)
 {
 	struct mgmt_cp_set_public_address *cp = data;
-	bool changed;
+	bool discov_disabled = false;
+	bool changed, configured;
 	int err;
 
 	bt_dev_dbg(hdev, "sock %p", sk);
@@ -8662,6 +8679,16 @@ static int set_public_address(struct sock *sk, struct hci_dev *hdev,
 				       MGMT_STATUS_NOT_SUPPORTED);
 
 	hci_dev_lock(hdev);
+	changed = !!bacmp(&hdev->public_addr, &cp->bdaddr);
+	configured = !hci_test_quirk(hdev, HCI_QUIRK_EXTERNAL_CONFIG) ||
+		hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED);
+	/* Drain the work only if this request may remove the index. */
+	if (changed && configured && hci_dev_test_flag(hdev, HCI_MGMT)) {
+		hci_dev_unlock(hdev);
+		disable_delayed_work_sync(&hdev->discov_off);
+		discov_disabled = true;
+		hci_dev_lock(hdev);
+	}
 
 	changed = !!bacmp(&hdev->public_addr, &cp->bdaddr);
 	bacpy(&hdev->public_addr, &cp->bdaddr);
@@ -8688,6 +8715,8 @@ static int set_public_address(struct sock *sk, struct hci_dev *hdev,
 	}
 
 unlock:
+	if (discov_disabled)
+		enable_delayed_work(&hdev->discov_off);
 	hci_dev_unlock(hdev);
 	return err;
 }
@@ -10105,7 +10134,6 @@ void mgmt_index_removed(struct hci_dev *hdev)
 	/* Cancel any remaining timed work */
 	if (!hci_dev_test_flag(hdev, HCI_MGMT))
 		return;
-	cancel_delayed_work_sync(&hdev->discov_off);
 	cancel_delayed_work_sync(&hdev->service_cache);
 	cancel_delayed_work_sync(&hdev->rpa_expired);
 
-- 
2.55.0


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

end of thread, other threads:[~2026-12-02  3:02 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02  2:06 [PATCH] Bluetooth: mgmt: disable discoverable work before device lock Baek Kyu, Kim
2026-12-02  3:02 ` 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