Skip to contentSkip to conversation

Bluetooth: hci_conn: Fix BIG cleanup while BISes are still bound

PATCH Superseded Simon Mikuda 2 participants 4 messages Nov 22

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 Bluetooth: hci_conn: Fix BIG cleanup while BISes are still bound

If a BIS bound to a BIG closes before LE BIG Complete, bis_cleanup() sees no other BIS in use and terminates the BIG. That removes the advertising set the BIG is being created on, and a Broadcom/Infineon controller
crashes: it sends vendor events with a memory dump and answers no further command.

< LE Create Broadcast Isochronous Group
> LE Broadcast Isochronous Group Complete Status: Success < LE Set Periodic Advertising Enable Disabled
< LE Setup Isochronous Data Path
< LE Set Extended Advertising Enable Disabled
< LE Remove Advertising Set
< LE Terminate Broadcast Isochronous Group

hci_le_create_big_complete_evt() connects every BIS in BT_BOUND, so count them as users of the BIG too. They were counted until the lookup of any BIS of the BIG was replaced with lookups by state.

Fixes d36349ea73d8 ("Bluetooth: hci_conn: Fix running bis_cleanup for hci_conn->type PA_LINK")
---
 net/bluetooth/hci_conn.c | 7 +++++++
 1 file changed, 7 insertions(+)
net/bluetooth/hci_conn.c +7 -0
index 7f0250653189..8024fc4223e9 100644
@@ -927,6 +927,13 @@ static void bis_cleanup(struct hci_conn *conn)
 		if (bis)
 			return;
 
+		bis = hci_conn_hash_lookup_big_state(hdev,
+						     conn->iso_qos.bcast.big,
+						     BT_BOUND,
+						     HCI_ROLE_MASTER);
+		if (bis)
+			return;
+
 		hci_le_terminate_big(hdev, conn);
 	} else {
 		hci_le_big_terminate(hdev, conn);

base-commit: cf357cfc025e7f72851479ed3fb60d5f16413c1b

Signature · 1 line
2.43.0
#1 lore raw

PATCH 2/3 Bluetooth: hci_conn: Terminate a BIG before its periodic advertising

From: Michal Dzik <[email protected]>

terminate_big_sync() disables the periodic advertising of a broadcast source and removes its advertising set first, and only then terminates the BIG: the same order in which they are created rather than the reverse. Every BIG has an associated periodic advertising train, which carries its BIGInfo ([Vol 6] Part B, Section 4.4.6.9), and the BIG exists until the controller reports HCI_LE_Terminate_BIG_Complete ([Vol 4] Part E, Section 7.8.105), so removing the set first leaves a BIG without its train. An AMPAK controller then fails to terminate the BIG; its vendor requires terminating the BIG, waiting for HCI_LE_Terminate_BIG_Complete and only then terminating the advertising.

Terminate the BIG first, and make hci_le_terminate_big_sync() wait for HCI_LE_Terminate_BIG_Complete, as hci_disconnect_sync() waits for HCI_Disconnection_Complete, before the advertising is disabled and removed. A controller that does not send the event delays the teardown by HCI_CMD_TIMEOUT, after which the advertising is disabled and removed as before. hci_iso_term_big_sync(), which terminates a BIG created after its BISes were closed, waits for the event too.

The wait is done in the cmd_sync work, not by removing the advertising set from the event handler: a removal queued from there would run after a new broadcast started on the same advertising set right away. It would turn off the periodic advertising and remove the set of the new BIG, and receivers would no longer find the new broadcast.

Fixes eca0ae4aea66 ("Bluetooth: Add initial implementation of BIS connections")
---
 include/net/bluetooth/hci.h |  6 ++++++
 net/bluetooth/hci_conn.c    | 17 +++++++++++------
 net/bluetooth/hci_sync.c    |  9 +++++++--
 3 files changed, 24 insertions(+), 8 deletions(-)
include/net/bluetooth/hci.h +6 -0
index efb85c781e4e..36a5668779a5 100644
@@ -3335,6 +3335,12 @@ struct hci_evt_le_create_big_complete {
 	__le16  bis_handle[];
 } __packed;
 
+#define HCI_EVT_LE_TERM_BIG_COMPLETE	0x1c
+struct hci_evt_le_term_big_complete {
+	__u8    handle;
+	__u8    reason;
+} __packed;
+
 #define HCI_EVT_LE_BIG_SYNC_ESTABLISHED 0x1d
 struct hci_evt_le_big_sync_established {
 	__u8    status;
net/bluetooth/hci_conn.c +11 -6
index 8024fc4223e9..5710234bffc3 100644
@@ -769,18 +769,23 @@ static void bis_list(struct hci_conn *conn, void *data)
 static int terminate_big_sync(struct hci_dev *hdev, void *data)
 {
 	struct iso_list_data *d = data;
+	int err = 0;
 
 	bt_dev_dbg(hdev, "big 0x%2.2x bis 0x%2.2x", d->big, d->bis);
 
+	/* Only terminate BIG if it has been created. Terminate it before the
+	 * periodic advertising that carries its BIGInfo, in the reverse order
+	 * of creating them: some controllers fail to terminate a BIG whose
+	 * periodic advertising has already been disabled.
+	 */
+	if (d->big_term)
+		err = hci_le_terminate_big_sync(hdev, d->big,
+						HCI_ERROR_LOCAL_HOST_TERM);
+
 	hci_disable_per_advertising_sync(hdev, d->bis);
 	hci_remove_ext_adv_instance_sync(hdev, d->bis, NULL);
 
-	/* Only terminate BIG if it has been created */
-	if (!d->big_term)
-		return 0;
-
-	return hci_le_terminate_big_sync(hdev, d->big,
-					 HCI_ERROR_LOCAL_HOST_TERM);
+	return err;
 }
 
 static void terminate_big_destroy(struct hci_dev *hdev, void *data, int err)
net/bluetooth/hci_sync.c +7 -2
index ea2014e74d32..bdb173440176 100644
@@ -2314,8 +2314,13 @@ int hci_le_terminate_big_sync(struct hci_dev *hdev, u8 handle, u8 reason)
 	cp.handle = handle;
 	cp.reason = reason;
 
-	return __hci_cmd_sync_status(hdev, HCI_OP_LE_TERM_BIG,
-				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
+	/* Wait for HCI_LE_Terminate_BIG_Complete, so the periodic advertising
+	 * of the BIG is not disabled while the BIG is still being terminated.
+	 */
+	return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_TERM_BIG,
+					sizeof(cp), &cp,
+					HCI_EVT_LE_TERM_BIG_COMPLETE,
+					HCI_CMD_TIMEOUT, NULL);
 }
 
 int hci_schedule_adv_instance_sync(struct hci_dev *hdev, u8 instance,
Signature · 1 line
2.43.0
#2 lore raw

PATCH 3/3 Bluetooth: hci_sync: Fix closing all BISes while their BIG is created

In reply to Simon Mikuda

LE Create BIG does not wait for LE BIG Complete. If all BIS sockets close in between, the cleanup removes the advertising set the BIG is being created on and terminates the BIG only after that:

< LE Create Broadcast Isochronous Group
> Command Status: Success
< LE Set Periodic Advertising Enable: Disabled > LE Broadcast Isochronous Group Complete: Success < LE Set Extended Advertising Enable: Disabled < LE Remove Advertising Set
< LE Terminate Broadcast Isochronous Group

A Realtek controller then stops answering commands:

    Bluetooth: hci0: command 0x2039 tx timeout

Wait for LE BIG Complete, or LE Create BIG Complete v2 after LE Create BIG v2, and delete a BIS closed meanwhile as one of a created BIG, so that the last one terminates the BIG first.

Fixes eca0ae4aea66 ("Bluetooth: Add initial implementation of BIS connections")
---
 net/bluetooth/hci_event.c | 11 +++++++++++
 net/bluetooth/hci_sync.c  | 20 ++++++++++++++++----
 2 files changed, 27 insertions(+), 4 deletions(-)
net/bluetooth/hci_event.c +11 -0
index ab875b90a143..eeb27bf249ee 100644
@@ -7881,6 +7881,17 @@ static void hci_le_create_big_complete(struct hci_dev *hdev, u8 status,
 			continue;
 		}
 
+		/* A BIS closed during LE Create BIG is still bound. Delete it
+		 * now, so that the last one terminates the BIG first.
+		 */
+		if (conn->abort_reason) {
+			i++;
+			set_bit(HCI_CONN_BIG_CREATED, conn->flags);
+			hci_connect_cfm(conn, conn->abort_reason);
+			hci_conn_del(conn);
+			continue;
+		}
+
 		if (hci_conn_set_handle(conn,
 					__le16_to_cpu(bis_handle[i++]))) {
 			bt_dev_err(hdev,
net/bluetooth/hci_sync.c +16 -4
index bdb173440176..d6e0b0fe10b7 100644
@@ -8962,8 +8962,15 @@ static int hci_le_create_big_v2_sync(struct hci_conn *conn,
 		cp.bis.hdt_rates = cpu_to_le16(qos->bcast.out.rates ? :
 					       hci_hdt_tx_rates(hdev));
 
-	return __hci_cmd_sync_status(hdev, HCI_OP_LE_CREATE_BIG_V2,
-				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
+	/* Wait for LE BIG Complete, so that the cleanup of BISes closed
+	 * meanwhile terminates the BIG before removing its advertising set.
+	 * An HDT capable controller reports it with LE Create BIG Complete v2,
+	 * which hci_le_set_event_mask_sync() unmasks for it.
+	 */
+	return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CREATE_BIG_V2,
+					sizeof(cp), &cp,
+					HCI_EVT_LE_CREATE_BIG_COMPLETE_V2,
+					conn->conn_timeout, NULL);
 }
 
 int hci_le_create_big_sync(struct hci_conn *conn, struct bt_iso_qos_ext *qos,
@@ -8989,8 +8996,13 @@ int hci_le_create_big_sync(struct hci_conn *conn, struct bt_iso_qos_ext *qos,
 	cp.bis.encryption = qos->bcast.encryption;
 	memcpy(cp.bis.bcode, qos->bcast.bcode, sizeof(cp.bis.bcode));
 
-	return __hci_cmd_sync_status(hdev, HCI_OP_LE_CREATE_BIG,
-				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
+	/* Wait for LE BIG Complete, so that the cleanup of BISes closed
+	 * meanwhile terminates the BIG before removing its advertising set.
+	 */
+	return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CREATE_BIG,
+					sizeof(cp), &cp,
+					HCI_EVT_LE_CREATE_BIG_COMPLETE,
+					conn->conn_timeout, NULL);
 }
 
 static void le_enable_encrypt_complete(struct hci_dev *hdev, void *data, int err)
Signature · 1 line
2.43.0
#3 lore raw
In reply to Simon Mikuda

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

---Test result---

Test Summary:

CheckPatch                    PASS      3.01 seconds
VerifyFixes                   PASS      0.13 seconds
VerifySignedoff               PASS      0.13 seconds
GitLint                       PASS      1.03 seconds
SubjectPrefix                 PASS      0.37 seconds
BuildKernel                   PASS      31.92 seconds
CheckAllWarning               PASS      35.66 seconds
CheckSparse                   WARNING   37.92 seconds
BuildKernel32                 PASS      31.20 seconds
CheckKernelLLVM               PASS      33.07 seconds
TestRunnerSetup               PASS      749.01 seconds
TestRunner_l2cap-tester       PASS      14.77 seconds
TestRunner_iso-tester         PASS      13.12 seconds
TestRunner_bnep-tester        PASS      3.19 seconds
TestRunner_mgmt-tester        PASS      88.93 seconds
TestRunner_rfcomm-tester      PASS      4.56 seconds
TestRunner_sco-tester         PASS      5.70 seconds
TestRunner_ioctl-tester       PASS      4.69 seconds
TestRunner_mesh-tester        PASS      4.58 seconds
TestRunner_smp-tester         PASS      4.41 seconds
TestRunner_userchan-tester    PASS      3.27 seconds
TestRunner_6lowpan-tester     PASS      4.48 seconds
IncrementalBuild              PASS      52.80 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):

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

---
Regards,
Linux Bluetooth