Bluetooth: MGMT: Fix advertising instances with Global Advertising
Subsystems (2)
Luiz Augusto von Dentz
maintainerPATCH v4 1/4 Bluetooth: MGMT: Fix advertising instances with Global Advertising
From: Luiz Augusto von Dentz <[email protected]>
Global Advertising, enabled with MGMT_OP_SET_ADVERTISING, uses instance 0 and with extended advertising it has its own advertising set, but instances were still being treated as if they shared the same set:
- Add Advertising and Add Extended Advertising Data would return success without ever enabling the instance.
- Removing the last instance would disable all advertising sets, stopping Global Advertising while leaving its setting enabled.
- Disabling Global Advertising would disable all sets, including active instances.
Fix this by only overriding instances that share the advertising set of Global Advertising, and by tracking HCI_LE_ADV_0 rather than HCI_LE_ADV to determine the state of Global Advertising, which is now also cleared when all advertising sets are disabled.
--- include/net/bluetooth/hci_sync.h | 1 + net/bluetooth/hci_event.c | 8 ++- net/bluetooth/hci_sync.c | 2 +- net/bluetooth/mgmt.c | 83 +++++++++++++++++++++++++++----- 4 files changed, 81 insertions(+), 13 deletions(-)
include/net/bluetooth/hci_sync.h +1 -0
@@ -122,6 +122,7 @@ int hci_disable_per_advertising_sync(struct hci_dev *hdev, u8 instance);int hci_remove_advertising_sync (struct hci_dev *hdev,struct sock *sk,u8 instance,bool force);int hci_disable_advertising_sync (struct hci_dev *hdev); +int hci_disable_ext_adv_legacy_instance_sync (struct hci_dev *hdev);int hci_clear_adv_instance_sync (struct hci_dev *hdev,struct sock *sk,u8 instance,bool force);int hci_update_passive_scan_sync (struct hci_dev *hdev);
net/bluetooth/hci_event.c +7 -1
@@ -1686,8 +1686,12 @@ static u8 hci_cc_le_set_ext_adv_enable(struct hci_dev *hdev, void *data,hci_dev_clear_flag (hdev,HCI_LE_ADV_0 );/* If just one instance was disabled check if there are -* any other instance enabled before clearing HCI_LE_ADV +* any other instance enabled, including instance 0, +* before clearing HCI_LE_ADV */ +if (hci_dev_test_flag (hdev,HCI_LE_ADV_0 )) + goto unlock; +list_for_each_entry_safe (adv, n, &hdev->adv_instances, list) {if (adv->enabled)
@@ -1698,6 +1702,8 @@ static u8 hci_cc_le_set_ext_adv_enable(struct hci_dev *hdev, void *data,list_for_each_entry_safe (adv, n, &hdev->adv_instances, list) adv->enabled = false; + +hci_dev_clear_flag (hdev,HCI_LE_ADV_0 ); }hci_dev_clear_flag (hdev,HCI_LE_ADV );
net/bluetooth/hci_sync.c +1 -1
@@ -1156,7 +1156,7 @@ int hci_update_random_address_sync(struct hci_dev *hdev, bool require_privacy,return 0 ; } -static int hci_disable_ext_adv_legacy_instance_sync (struct hci_dev *hdev) +int hci_disable_ext_adv_legacy_instance_sync (struct hci_dev *hdev) {struct hci_cp_le_set_ext_adv_enable *cp;struct hci_cp_ext_adv_set *set;
net/bluetooth/mgmt.c +72 -11
135 lines not on this page · raw message
Signature · 1 line
2.55.0
Luiz Augusto von Dentz
maintainerPATCH v4 2/4 Bluetooth: HCI: Fix tracking of instances sharing handle 0x00
From: Luiz Augusto von Dentz <[email protected]>
When the controller supports a single advertising set, instance 1 uses handle 0x00 which is the same handle used by instance 0 (Global Advertising), but hci_cc_le_set_ext_adv_enable would lookup the instance using the handle as instance identifier, marking instance 0 as enabled/disabled instead of instance 1, leaving its state out of sync.
Fix this by matching instances by handle and, for handle 0x00, using hdev->cur_adv_instance, now set when enabling an instance, to determine which instance owns the set. Also disable the instance sharing the advertising set of instance 0 before overriding it.
--- net/bluetooth/hci_event.c | 47 ++++++++++++++++++++++++++++++--------- net/bluetooth/hci_sync.c | 32 ++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 11 deletions(-)
net/bluetooth/hci_event.c +36 -11
@@ -1639,6 +1639,39 @@ static u8 hci_cc_le_set_adv_enable(struct hci_dev *hdev, void *data,return rp->status; } +/* Update the enabled state of the instance(s) using the advertising set +* identified by handle. +* +* Instances may share handle 0x00 with instance 0 (Global Advertising), e.g. +* when the controller supports a single advertising set, in which case the +* set is owned by the instance last enabled (hdev->cur_adv_instance) and any +* other instance sharing it shall be considered disabled. +*/ +static void hci_le_ext_adv_set_enabled (struct hci_dev *hdev,u8 handle, +bool enable) +{ +struct adv_info *adv; +bool owned = false; + +list_for_each_entry (adv, &hdev->adv_instances, list) { +if (adv->handle != handle) +continue ; + + adv->enabled = enable && + (handle || + adv->instance == hdev->cur_adv_instance); + owned |= adv->enabled; + } + +if (handle) +return ; + +if (enable && !owned) +hci_dev_set_flag (hdev,HCI_LE_ADV_0 ); +else +hci_dev_clear_flag (hdev,HCI_LE_ADV_0 ); +} +static u8 hci_cc_le_set_ext_adv_enable (struct hci_dev *hdev,void *data,struct sk_buff *skb) {
@@ -1660,18 +1693,13 @@ static u8 hci_cc_le_set_ext_adv_enable(struct hci_dev *hdev, void *data,hci_dev_lock (hdev); -if (cp->num_of_sets) - adv =hci_find_adv_instance (hdev, set->handle); -if (cp->enable) {struct hci_conn *conn;hci_dev_set_flag (hdev,HCI_LE_ADV ); -if (adv) - adv->enabled = true; -else if (!set->handle) -hci_dev_set_flag (hdev,HCI_LE_ADV_0 ); +if (cp->num_of_sets) +hci_le_ext_adv_set_enabled (hdev, set->handle, true); conn =hci_lookup_le_connect (hdev);if (conn)
@@ -1680,10 +1708,7 @@ static u8 hci_cc_le_set_ext_adv_enable(struct hci_dev *hdev, void *data, conn->conn_timeout); }else {if (cp->num_of_sets) { -if (adv) - adv->enabled = false; -else if (!set->handle) -hci_dev_clear_flag (hdev,HCI_LE_ADV_0 ); +hci_le_ext_adv_set_enabled (hdev, set->handle, false);/* If just one instance was disabled check if there are * any other instance enabled, including instance 0,
net/bluetooth/hci_sync.c +32 -0
@@ -1413,6 +1413,32 @@ int hci_setup_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance)if (err)return err; +/* Disable any instance sharing the advertising set of +* instance 0 since it is going to be overridden. +* +* Lookup the instance while holding hdev->lock but don't +* hold it while sending the HCI command. +*/ + instance =0x00 ; + +hci_dev_lock (hdev); +list_for_each_entry (adv, &hdev->adv_instances, list) { +if (!adv->handle && adv->enabled) { + instance = adv->instance; +break ; + } + } +hci_dev_unlock (hdev); + +if (instance) { + err =hci_disable_ext_adv_instance_sync (hdev, + instance); +if (err) +return err; + + instance =0x00 ; + } + adv =NULL ; }
@@ -1676,6 +1702,12 @@ int hci_enable_ext_advertising_sync(struct hci_dev *hdev, u8 instance) set->duration =cpu_to_le16 (duration /10 ); } +/* Track which instance owns the advertising set since the handle +* alone is ambiguous when instances share handle 0x00, see +* hci_cc_le_set_ext_adv_enable. +*/ + hdev->cur_adv_instance = instance; + return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE, sizeof(*cp) + sizeof(*set) * cp->num_of_sets,
Signature · 1 line
2.55.0
Luiz Augusto von Dentz
maintainerPATCH v4 3/4 Bluetooth: MGMT: Fix pending state of instances added without HCI traffic
In reply to Luiz Augusto von DentzFrom: Luiz Augusto von Dentz <[email protected]>
When Add Advertising completes without HCI communication, e.g. when the device is not powered or the instance is overridden by Global Advertising, the instance is left marked as pending, so a subsequent failure in add_adv_complete would remove it despite it having been successfully added.
Fix this by clearing the pending state before completing the command.
--- net/bluetooth/mgmt.c | 6 ++++++ 1 file changed, 6 insertions(+)
net/bluetooth/mgmt.c +6 -0
@@ -9173,6 +9173,12 @@ static int add_advertising(struct sock *sk, struct hci_dev *hdev,if (!hdev_is_powered (hdev) ||adv_overridden_by_global (hdev, adv) || !schedule_instance) { +/* Instance has been accepted so it shall no longer be +* considered pending, otherwise a later failure in +* add_adv_complete would remove it. +*/ + adv->pending = false; + rp.instance = cp->instance; err =mgmt_cmd_complete (sk, hdev->id,MGMT_OP_ADD_ADVERTISING ,MGMT_STATUS_SUCCESS , &rp,sizeof (rp));
Signature · 1 line
2.55.0
Luiz Augusto von Dentz
maintainerPATCH v4 4/4 Bluetooth: MGMT: Fix leaking instance on Add Extended Advertising Parameters
In reply to Luiz Augusto von DentzFrom: Luiz Augusto von Dentz <[email protected]>
If queueing the command fails the instance is never removed, despite it having already been added or modified by hci_add_adv_instance, so remove it as it is done by add_ext_adv_params_complete on failure, signaling it has been removed if it was previously added.
--- net/bluetooth/mgmt.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-)
net/bluetooth/mgmt.c +16 -3
@@ -9361,14 +9361,15 @@ static int add_ext_adv_params(struct sock *sk, struct hci_dev *hdev, data, data_len);if (!cmd) { err = -ENOMEM ; -hci_remove_adv_instance (hdev, cp->instance); - goto unlock; + goto remove; } err =hci_cmd_sync_queue (hdev, add_ext_adv_params_sync, cmd, add_ext_adv_params_complete); -if (err <0 ) +if (err <0 ) {mgmt_pending_free (cmd); + goto remove; + } }else { rp.instance = cp->instance; rp.tx_power =HCI_ADV_TX_POWER_NO_PREFERENCE ;
@@ -9379,6 +9380,18 @@ static int add_ext_adv_params(struct sock *sk, struct hci_dev *hdev, MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); } + goto unlock; + +remove: +/* The instance may have been modified already so remove it, like it +* is done in add_ext_adv_params_complete, signaling it has been +* removed if it was previously added. +*/ +if (!adv->pending) +mgmt_advertising_removed (sk, hdev, cp->instance); + +hci_remove_adv_instance (hdev, cp->instance); + unlock:hci_dev_unlock (hdev);
Signature · 1 line
2.55.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/1179910/
---Test result---
Test Summary:
CheckPatch PASS 3.21 seconds VerifyFixes PASS 0.09 seconds VerifySignedoff PASS 0.10 seconds GitLint FAIL 1.05 seconds SubjectPrefix PASS 0.64 seconds BuildKernel PASS 28.56 seconds CheckAllWarning PASS 32.39 seconds CheckSparse WARNING 35.46 seconds BuildKernel32 PASS 28.14 seconds CheckKernelLLVM PASS 32.04 seconds TestRunnerSetup PASS 710.46 seconds TestRunner_l2cap-tester PASS 12.96 seconds TestRunner_iso-tester FAIL 24.88 seconds TestRunner_bnep-tester PASS 3.17 seconds TestRunner_mgmt-tester FAIL 62.14 seconds TestRunner_rfcomm-tester PASS 4.21 seconds TestRunner_sco-tester PASS 5.28 seconds TestRunner_ioctl-tester PASS 4.37 seconds TestRunner_mesh-tester FAIL 7.67 seconds TestRunner_smp-tester PASS 4.05 seconds TestRunner_userchan-tester PASS 2.91 seconds TestRunner_6lowpan-tester PASS 4.11 seconds IncrementalBuild PASS 44.09 seconds
Details
##############################
Test: GitLint - FAIL
Desc: Run gitlint
Output:
[v4,3/4] Bluetooth: MGMT: Fix pending state of instances added without HCI traffic
1: T1 Title exceeds max length (82>80): "[v4,3/4] Bluetooth: MGMT: Fix pending state of instances added without HCI traffic" [v4,4/4] Bluetooth: MGMT: Fix leaking instance on Add Extended Advertising Parameters
1: T1 Title exceeds max length (85>80): "[v4,4/4] Bluetooth: MGMT: Fix leaking instance on Add Extended Advertising Parameters"
##############################
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):net/bluetooth/hci_event.c: note: in included file (through include/net/bluetooth/hci_core.h):
##############################
Test: TestRunner_iso-tester - FAIL
Desc: Run iso-tester with test-runner
Output:
Total: 141, Passed: 136 (96.5%), Failed: 5, Not Run: 0
Failed Test Cases
ISO Broadcaster PAST Info - Success Timed out 2.582 seconds ISO Broadcaster PAST Info RPA - Success Timed out 2.000 seconds ISO Broadcaster PAST Sender - Success Timed out 1.998 seconds ISO Broadcaster PAST Receiver - Success Timed out 1.824 seconds ISO Broadcaster PAST Receiver Sync Lost - Success Timed out 2.701 seconds
##############################
Test: TestRunner_mgmt-tester - FAIL
Desc: Run mgmt-tester with test-runner Output:
Total: 503, Passed: 502 (99.8%), Failed: 1, Not Run: 0
Failed Test Cases
Add Ext Advertising - Success 5 (Set Adv off override) Timed out 1.950 seconds
##############################
Test: TestRunner_mesh-tester - FAIL
Desc: Run mesh-tester with test-runner Output:
Total: 10, Passed: 8 (80.0%), Failed: 2, Not Run: 0
Failed Test Cases
Mesh - Send cancel - 1 Timed out 2.519 seconds Mesh - Send cancel - 2 Timed out 2.000 seconds
https://github.com/bluez/bluetooth-next/pull/864
---
Regards,
Linux Bluetooth