Bluetooth: MGMT: Fix discovery stuck after a failed Stop Discovery
Subsystems (2)
PATCH v5 1/1 Bluetooth: MGMT: Fix discovery stuck after a failed Stop Discovery
Stop Discovery moves the discovery to DISCOVERY_STOPPING, and stop_discovery_complete() only moves it on to DISCOVERY_STOPPED when stopping succeeded. When it fails, for example because LE Set Scan Enable times out, nothing moves the discovery out ofDISCOVERY_STOPPING: Stop Discovery is rejected, as the discovery is not active anymore, and the LE scan timeout was cancelled before the scan was disabled. Until the controller is powered off, every Start Discovery then fails with Busy, and hci_update_passive_scan_sync() skips background scanning, so devices set to auto-connect are not connected anymore.
Move the discovery back to DISCOVERY_FINDING when stopping it failed while its inquiry or active LE scan is still running, so that it can be stopped again. Only change the state while the discovery is still stopping, as Inquiry Cancel may already have stopped it, for example when only the passive scan could not be disabled, and a new Start Discovery may have followed. Do this under hci_dev_lock(), like the command completions that change these flags, and before replying, so that a client acting on the reply sees the new state. Only cancel the LE scan timeout once the scan is disabled, so that the discovery still ends on its own, except when suspending, where it is still cancelled first.
A timed out LE Set Scan Enable can still complete afterwards, which cancels the timeout as well, so also stop the discovery when its scan is disabled while the timeout is pending, or while the timeout work waits for the lock.
---
Changes in v5:
- Only change the state while the discovery is still stopping, so that
a failed stop does not overwrite a new Start Discovery (Sashiko).
- Change the state before replying, so that a client acting on the
reply sees it (Sashiko).
- Keep cancelling the LE scan timeout first when suspending, so that it does not run while suspended (Sashiko).
- Rebase on bluetooth-next 42b8c2d729.
- v4: https://lore.kernel.org/linux-bluetooth/[email protected]/
Changes in v4:
- Only go back to DISCOVERY_FINDING while the discovery is still
stopping, and not for the passive scan (Sashiko).
- Check the flags and change the state under hci_dev_lock() (Sashiko).
- Also stop the discovery in le_scan_disable() when the scan was
disabled while it waited for the lock (Sashiko).
- Rebase on bluetooth-next 1081a6ef8f.
- v3: https://lore.kernel.org/linux-bluetooth/[email protected]/
Changes in v3:
- Go back to DISCOVERY_FINDING instead of DISCOVERY_STOPPED when
stopping failed while the inquiry or the LE scan is still running.
- Keep the LE scan timeout until the scan is disabled, so that the discovery still ends on its own.
- Stop the discovery when a timed out LE Set Scan Enable completes afterwards, as that cancels the timeout too.
- v2: https://lore.kernel.org/linux-bluetooth/[email protected]/
I did not find an error status that shows by itself that the scan already stopped: the kernel only disables the scan while HCI_LE_SCAN is set, and hci_cc_inquiry_cancel() already turns Command Disallowed after the inquiry completed into success. So this checks HCI_INQUIRY and HCI_LE_SCAN instead, which are only cleared once the controller confirms. Going back to DISCOVERY_FINDING sends Discovering (enabled) again.
Changing the state before replying also sends Discovering (disabled) before the reply when stopping succeeds. bluetoothd already gets that order when Inquiry Cancel stops a BR/EDR discovery, and its tests pass as before.
Reproduced in test-runner with a new mgmt-tester case, "Stop Discovery - Scan Disable Failed 1", in which the emulator answers the first LE Set Scan Enable (disable) with Unspecified Error: without this patch a second Stop Discovery is rejected and Start Discovery fails with Busy; with it the second Stop Discovery succeeds, and so does Start Discovery (3 of 3 runs). Without the second Stop Discovery, the discovery ends at the LE scan timeout.
The v3 and v4 regressions reproduce with a BR/EDR discovery while passive scanning for an auto-connect device, where only disabling the passive scan fails. With v3 the discovery is enabled again and Start Discovery fails with Busy (2 of 2 runs). With v4, when another socket starts a discovery after Inquiry Cancel and before the failure, a second Start Discovery right after the failed stop is accepted (3 of 3). With this version the discovery stays stopped in the first case, and the second Start Discovery fails with Busy in the other (3 of 3 each).
Freezing the emulated controller while bluetoothd stops discovering makes LE Set Scan Enable time out and then complete just after: the discovery is stopped when it completes, and Start Discovery succeeds (3 of 3). The other kernel testers pass as before. The lock orderings, the reply ordering and the suspend path are only checked in the code, not reproduced. Only tested with the emulator, not with real controllers. I can send the mgmt-tester case to BlueZ if you want it.
Not changed: hci_pause_discovery_sync() can still leave the discovery in DISCOVERY_STOPPING on suspend when stopping fails, which I have not reproduced. A simultaneous discovery that is stopped while it resolves names also goes back to DISCOVERY_FINDING rather than DISCOVERY_RESOLVING, so its remaining names are not resolved and the LE scan timeout ends it.
net/bluetooth/hci_event.c | 12 +++++++++++- net/bluetooth/hci_sync.c | 26 +++++++++++++++++++++++--- net/bluetooth/mgmt.c | 23 ++++++++++++++++++++--- 3 files changed, 54 insertions(+), 7 deletions(-)
net/bluetooth/hci_event.c +11 -1
@@ -1819,6 +1819,8 @@ static void store_pending_adv_report(struct hci_dev *hdev, bdaddr_t *bdaddr,static void le_set_scan_enable_complete (struct hci_dev *hdev,u8 enable) { +bool timeout_pending; +hci_dev_lock (hdev);switch (enable) {
@@ -1848,10 +1850,18 @@ static void le_set_scan_enable_complete(struct hci_dev *hdev, u8 enable)/* Cancel this timer so that we don't try to disable scanning * when it's already disabled. */ -cancel_delayed_work (&hdev->le_scan_disable); + timeout_pending =cancel_delayed_work (&hdev->le_scan_disable);hci_dev_clear_flag (hdev,HCI_LE_SCAN ); +/* The scan of a discovery was disabled before its timeout, +* for example by a Stop Discovery that timed out and still +* completed, so nothing else would end the discovery. +*/ +if (timeout_pending && + hdev->discovery.state ==DISCOVERY_FINDING ) +hci_discovery_set_state (hdev,DISCOVERY_STOPPED ); + if (hdev->discovery.type == DISCOV_TYPE_INTERLEAVED && hci_test_quirk(hdev, HCI_QUIRK_SIMULTANEOUS_DISCOVERY) && !test_bit(HCI_INQUIRY, &hdev->flags) &&
net/bluetooth/hci_sync.c +23 -3
@@ -376,8 +376,18 @@ static void le_scan_disable(struct work_struct *work)bt_dev_dbg (hdev,"" );hci_dev_lock (hdev); -if (!hci_dev_test_flag (hdev,HCI_LE_SCAN )) +if (!hci_dev_test_flag (hdev,HCI_LE_SCAN )) { +/* The scan of a discovery can be disabled while this work +* waits for the lock, for example by a Stop Discovery that +* timed out and still completed, so end the discovery as +* le_set_scan_enable_complete() does when it cancels this +* work. +*/ +if (hdev->discovery.state ==DISCOVERY_FINDING ) + goto discov_stopped; + goto _return; + } status =hci_cmd_sync_queue (hdev, scan_disable_sync,NULL ,NULL );if (status) {
@@ -5920,11 +5930,15 @@ int hci_stop_discovery_sync(struct hci_dev *hdev) }if (hci_dev_test_flag (hdev,HCI_LE_SCAN )) { -cancel_delayed_work (&hdev->le_scan_disable); - err =hci_scan_disable_sync (hdev);if (err)return err; + +/* Only cancel the LE scan timeout once the scan is +* disabled, so that the discovery still ends if +* stopping it failed. +*/ +cancel_delayed_work (&hdev->le_scan_disable); } }else {
@@ -6559,6 +6573,12 @@ static int hci_pause_discovery_sync(struct hci_dev *hdev)return 0 ;hci_discovery_set_state (hdev,DISCOVERY_STOPPING ); + +/* Cancel the LE scan timeout first, so that it does not run while +* suspended if the scan cannot be disabled. +*/ +cancel_delayed_work (&hdev->le_scan_disable); + err =hci_stop_discovery_sync (hdev);if (err)return err;
net/bluetooth/mgmt.c +20 -3
@@ -6318,12 +6318,29 @@ static void stop_discovery_complete(struct hci_dev *hdev, void *data, int err)bt_dev_dbg (hdev,"err %d" , err); +hci_dev_lock (hdev); + +/* Change the state before replying, so that a client acting on the +* reply sees it, and only while the discovery is still stopping, as +* Inquiry Cancel may already have stopped it and a new Start Discovery +* may have followed. Go back to DISCOVERY_FINDING when stopping failed +* while the inquiry or the active LE scan of the discovery is still +* running, so that it can be stopped again. +*/ +if (hdev->discovery.state ==DISCOVERY_STOPPING ) { +if (err && (test_bit (HCI_INQUIRY , &hdev->flags) || + (hci_dev_test_flag (hdev,HCI_LE_SCAN ) && + hdev->le_scan_type ==LE_SCAN_ACTIVE ))) +hci_discovery_set_state (hdev,DISCOVERY_FINDING ); +else +hci_discovery_set_state (hdev,DISCOVERY_STOPPED ); + } + +hci_dev_unlock (hdev); +mgmt_cmd_complete (cmd->sk, cmd->hdev->id, cmd->opcode,mgmt_status (err), cmd->param,1 );mgmt_pending_free (cmd); - -if (!err) -hci_discovery_set_state (hdev,DISCOVERY_STOPPED ); }static int stop_discovery_sync (struct hci_dev *hdev,void *data)
base-commit: 42b8c2d729268a65a2d98064e8dc59ff53646b14
Signature · 1 line
2.53.0
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/1199295/
---Test result---
Test Summary:
CheckPatch PASS 1.15 seconds VerifyFixes PASS 0.43 seconds VerifySignedoff PASS 0.09 seconds GitLint PASS 0.27 seconds SubjectPrefix PASS 0.08 seconds BuildKernel PASS 30.64 seconds CheckAllWarning PASS 34.89 seconds CheckSparse WARNING 39.31 seconds BuildKernel32 PASS 30.78 seconds CheckKernelLLVM PASS 31.92 seconds TestRunnerSetup PASS 700.26 seconds TestRunner_l2cap-tester FAIL 10.54 seconds TestRunner_iso-tester PASS 10.50 seconds TestRunner_bnep-tester PASS 3.15 seconds TestRunner_mgmt-tester PASS 87.46 seconds TestRunner_rfcomm-tester PASS 4.50 seconds TestRunner_sco-tester PASS 5.57 seconds TestRunner_ioctl-tester PASS 4.65 seconds TestRunner_mesh-tester PASS 5.52 seconds TestRunner_smp-tester PASS 4.35 seconds TestRunner_userchan-tester PASS 3.22 seconds TestRunner_6lowpan-tester PASS 4.40 seconds IncrementalBuild PASS 28.90 seconds
Details
##############################
Test: CheckSparse - WARNING
Desc: Run sparse tool with linux kernel
Output:net/bluetooth/hci_event.c: note: in included file (through include/net/bluetooth/hci_core.h):
##############################
Test: TestRunner_l2cap-tester - FAIL
Desc: Run l2cap-tester with test-runner
Output:
Sanitizer errors:
==35==ERROR: AddressSanitizer: heap-use-after-free on address 0x7c0351fe33d0 at pc 0x55973e00ee8e bp 0x7ffe21df4d40 sp 0x7ffe21df4d30 READ of size 4 at 0x7c0351fe33d0 thread T0
#0 0x55973e00ee8d in timeout_callback src/shared/timeout-mainloop.c:33
#1 0x55973e00fb75 in timeout_callback src/shared/mainloop.c:270
#2 0x55973e010aa6 in mainloop_run src/shared/mainloop.c:124
#3 0x55973e0133fa in mainloop_run_with_signal src/shared/mainloop-notify.c:196
#4 0x55973e01864b in tester_run src/shared/tester.c:1223
#5 0x55973df4217d in main tools/l2cap-tester.c:3918
#6 0x7fc352f45600 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:59
#7 0x7fc352f45717 in __libc_start_main_impl ../csu/libc-start.c:360
#8 0x55973df48c84 in _start (/home/runner/work/bluetooth-next/bluetooth-next/src/bluez/tools/l2cap-tester+0x16cc84) (BuildId: 91c9d49e9766c394468e27ffa29b05e7d348169f)0x7c0351fe33d0 is located 0 bytes inside of 40-byte region [0x7c0351fe33d0,0x7c0351fe33f8) freed by thread T0 here:
#0 0x7fc3538833ff in free ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:51
#1 0x55973e00f36a in timeout_destroy src/shared/mainloop.c:251previously allocated by thread T0 here:
#0 0x7fc35388460f in malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:67
#1 0x55973dff92d5 in util_malloc src/shared/util.c:46SUMMARY: AddressSanitizer: heap-use-after-free src/shared/timeout-mainloop.c:33 in timeout_callback No test result found
https://github.com/bluez/bluetooth-next/pull/1046
---
Regards,
Linux Bluetooth
This patch was applied to bluetooth/bluetooth-next.git (master) by Luiz Augusto von Dentz <[email protected]>:
Hello:
This patch was applied to bluetooth/bluetooth-next.git (master) by Luiz Augusto von Dentz <[email protected]>:
Quoted 11 lines · On Mon, 2 Nov 2026 09:00:00 -0800 you wrote:
Stop Discovery moves the discovery to
DISCOVERY_STOPPING, andstop_discovery_complete()only moves it on toDISCOVERY_STOPPEDwhen stopping succeeded. When it fails, for example because LE Set Scan Enable times out, nothing moves the discovery out ofDISCOVERY_STOPPING: Stop Discovery is rejected, as the discovery is not active anymore, and the LE scan timeout was cancelled before the scan was disabled. Until the controller is powered off, every Start Discovery then fails with Busy, andhci_update_passive_scan_sync()skips background scanning, so devices set to auto-connect are not connected anymore.
Quoted 1 line
[...]
Here is the summary with links:
- [v5] Bluetooth: MGMT: Fix discovery stuck after a failed Stop Discovery
https://git.kernel.org/bluetooth/bluetooth-next/c/60b10121db60
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html