Skip to contentSkip to conversation

Bluetooth: MGMT: Fix discovery stuck after a failed Stop Discovery

PATCH Superseded Kevin Dewald 3 participants 3 messages Oct 27

Subsystems (2)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 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 else moves the discovery out of DISCOVERY_STOPPING, as 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 to DISCOVERY_STOPPED also when stopping it failed, as start_discovery_complete() does when starting fails. This sends Discovering (disabled), and the passive scan update disables the scan again if it is still enabled.

Fixes 0e05bba6f6f8 ("Bluetooth: Update stop_discovery to use HCI request")
Assisted-by Claude:claude-opus-5.5

---
Changes in v2:
- Add the missing Signed-off-by. v1 went out three times without it,
sorry for the noise.
- v1: https://lore.kernel.org/linux-bluetooth/[email protected]/

Reproduced in test-runner with a new mgmt-tester case, "Stop Discovery - Scan Disable Failed 1", in which the emulator answers LE Set Scan Enable (disable) with Unspecified Error: without this patch no Discovering (disabled) event is sent, the scan is left enabled and Start Discovery fails with Busy one second later (3 of 3 runs); with it the scan is disabled again and Start Discovery succeeds (3 of 3). Freezing the emulated controller while bluetoothd stops discovering gives the same result through the HCI command timeout. The other kernel testers pass as before. Only tested with the emulator, not with real controllers. I can send the mgmt-tester case to BlueZ if you want it.

hci_pause_discovery_sync() has the same pattern on suspend, which I have not reproduced.

 net/bluetooth/mgmt.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
net/bluetooth/mgmt.c +5 -2
index 3cb4f9ece8..06595175d9 100644
@@ -6322,8 +6322,11 @@ static void stop_discovery_complete(struct hci_dev *hdev, void *data, int err)
 			  cmd->param, 1);
 	mgmt_pending_free(cmd);
 
-	if (!err)
-		hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
+	/* Leave DISCOVERY_STOPPING also when stopping failed, since nothing
+	 * else moves the discovery out of it: Start Discovery would keep
+	 * failing with Busy and passive scanning would not be resumed.
+	 */
+	hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
 }
 
 static int stop_discovery_sync(struct hci_dev *hdev, void *data)

base-commit: 47d4d9f67e92d56ef3965f2727f9c98a5a611368

Signature · 1 line
2.53.0
#1 lore raw

Hi Kevin,

Quoted 25 lines · Kevin Dewald

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 else moves the discovery out of DISCOVERY_STOPPING, as 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 to DISCOVERY_STOPPED also when stopping it failed, as start_discovery_complete() does when starting fails. This sends Discovering (disabled), and the passive scan update disables the scan again if it is still enabled.

Fixes 0e05bba6f6f8 ("Bluetooth: Update stop_discovery to use HCI request")
Assisted-by Claude:claude-opus-5.5

---
Changes in v2:
- Add the missing Signed-off-by. v1 went out three times without it,
sorry for the noise.
- v1: https://lore.kernel.org/linux-bluetooth/[email protected]/

Quoted 10 lines

Reproduced in test-runner with a new mgmt-tester case, "Stop Discovery - Scan Disable Failed 1", in which the emulator answers LE Set Scan Enable (disable) with Unspecified Error: without this patch no Discovering (disabled) event is sent, the scan is left enabled and Start Discovery fails with Busy one second later (3 of 3 runs); with it the scan is disabled again and Start Discovery succeeds (3 of 3). Freezing the emulated controller while bluetoothd stops discovering gives the same result through the HCI command timeout. The other kernel testers pass as before. Only tested with the emulator, not with real controllers. I can send the mgmt-tester case to BlueZ if you want it.

Ok, but this is not the expected behavior, I can agree if the error indicates the scan has already stopped; otherwise, we should probably fail and restore the discovery state won't be, or starting it again may fail because the controller state in sync with the discovery state.

Quoted 29 lines

hci_pause_discovery_sync() has the same pattern on suspend, which I have not reproduced.

 net/bluetooth/mgmt.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
net/bluetooth/mgmt.c +5 -2
index 3cb4f9ece8..06595175d9 100644
@@ -6322,8 +6322,11 @@ static void stop_discovery_complete(struct hci_dev *hdev, void *data, int err)
                          cmd->param, 1);
        mgmt_pending_free(cmd);
 
-       if (!err)
-               hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
+       /* Leave DISCOVERY_STOPPING also when stopping failed, since nothing
+        * else moves the discovery out of it: Start Discovery would keep
+        * failing with Busy and passive scanning would not be resumed.
+        */
+       hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
 }
 
 static int stop_discovery_sync(struct hci_dev *hdev, void *data)

base-commit: 47d4d9f67e92d56ef3965f2727f9c98a5a611368 --
2.53.0

Signature · 1 line
Luiz Augusto von Dentz
#2 lore raw
In reply to Kevin Dewald

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

---Test result---

Test Summary:

CheckPatch                    PASS      0.52 seconds
VerifyFixes                   PASS      0.10 seconds
VerifySignedoff               PASS      0.10 seconds
GitLint                       PASS      0.26 seconds
SubjectPrefix                 PASS      0.10 seconds
BuildKernel                   PASS      19.10 seconds
CheckAllWarning               PASS      29.28 seconds
CheckSparse                   PASS      24.05 seconds
BuildKernel32                 PASS      20.28 seconds
CheckKernelLLVM               PASS      22.09 seconds
TestRunnerSetup               PASS      453.55 seconds
TestRunner_mgmt-tester        PASS      83.31 seconds
TestRunner_mesh-tester        PASS      5.04 seconds
IncrementalBuild              PASS      20.25 seconds

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

---
Regards,
Linux Bluetooth