Bluetooth: Fixes for 4.3-rc series
Subsystems (2)
cover Bluetooth: Fixes for 4.3-rc series
Hi,
Here's the combined set of patches that should be considered for bluetooth.git.
Johan
----------------------------------------------------------------
Jakub Pawlowski (2):
Bluetooth: Fix double scan updates
Bluetooth: Fix initializing conn_params in scan phase
Johan Hedberg (4):
Bluetooth: Fix reference counting for LE-scan based connections
Bluetooth: Fix LE reconnection logic
Bluetooth: Fix remove_device behavior for explicit connects
Bluetooth: Fix conn_params list update in hci_connect_le_scan_cleanup
net/bluetooth/hci_conn.c | 99 +++++++++++++++++++++++++++++++++------------- net/bluetooth/hci_core.c | 7 ---- net/bluetooth/hci_event.c | 11 ++++-- net/bluetooth/mgmt.c | 24 +++++++++-- 4 files changed, 99 insertions(+), 42 deletions(-)
PATCH 1/6 Bluetooth: Fix double scan updates
From: Jakub Pawlowski <[email protected]>
When disable/enable scan command is issued twice, some controllers will return an error for the second request, i.e. requests with this command will fail on some controllers, and succeed on others.
This patch makes sure that unnecessary scan disable/enable commands are not issued.
When adding device to the auto connect whitelist when there is pending connect attempt, there is no need to update scan.
hci_connect_le_scan_cleanup is conditionally executing hci_conn_params_del, that is calling hci_update_background_scan. Make the other case also update scan, and remove reduntand call from hci_connect_le_scan_remove.
When stopping interleaved discovery the state should be set to stopped only when both LE scanning and discovery has stopped.
--- net/bluetooth/hci_conn.c | 7 ++++--- net/bluetooth/hci_event.c | 7 ++++++- net/bluetooth/mgmt.c | 6 +++++- 3 files changed, 15 insertions(+), 5 deletions(-)
net/bluetooth/hci_conn.c +4 -3
@@ -91,10 +91,12 @@ static void hci_connect_le_scan_cleanup(struct hci_conn *conn)* autoconnect action, remove them completely. If they are, just unmark * them as waiting for connection, by clearing explicit_connect field. */ -if (params->auto_connect ==HCI_AUTO_CONN_EXPLICIT ) +if (params->auto_connect ==HCI_AUTO_CONN_EXPLICIT ) {hci_conn_params_del (conn->hdev, bdaddr, bdaddr_type); -else + }else { params->explicit_connect = false; +hci_update_background_scan (conn->hdev); + } }/* This function requires the caller holds hdev->lock */
@@ -103,7 +105,6 @@ static void hci_connect_le_scan_remove(struct hci_conn *conn)hci_connect_le_scan_cleanup (conn);hci_conn_hash_del (conn->hdev, conn); -hci_update_background_scan (conn->hdev); }static void hci_acl_create_connection (struct hci_conn *conn)
net/bluetooth/hci_event.c +6 -1
@@ -55,7 +55,12 @@ static void hci_cc_inquiry_cancel(struct hci_dev *hdev, struct sk_buff *skb)wake_up_bit (&hdev->flags,HCI_INQUIRY );hci_dev_lock (hdev); -hci_discovery_set_state (hdev,DISCOVERY_STOPPED ); +/* Set discovery state to stopped if we're not doing LE active +* scanning. +*/ +if (!hci_dev_test_flag (hdev,HCI_LE_SCAN ) || + hdev->le_scan_type !=LE_SCAN_ACTIVE ) +hci_discovery_set_state (hdev,DISCOVERY_STOPPED );hci_dev_unlock (hdev);hci_conn_check_pending (hdev);
net/bluetooth/mgmt.c +5 -1
@@ -6124,7 +6124,11 @@ static int hci_conn_params_set(struct hci_request *req, bdaddr_t *addr,case HCI_AUTO_CONN_ALWAYS :if (!is_connected (hdev, addr, addr_type)) {list_add (¶ms->action, &hdev->pend_le_conns); -__hci_update_background_scan (req); +/* If we are in scan phase of connecting, we were +* already added to pend_le_conns and scanning. +*/ +if (params->auto_connect !=HCI_AUTO_CONN_EXPLICIT ) +__hci_update_background_scan (req); }break ; }
Signature · 1 line
2.4.3
PATCH 2/6 Bluetooth: Fix reference counting for LE-scan based connections
In reply to Johan HedbergFrom: Johan Hedberg <[email protected]>
The code should never directly call hci_conn_hash_del since many cleanup & reference counting updates would be lost. Normally hci_conn_del is the right thing to do, but in the case of a connection doing LE scanning this could cause a deadlock due to doing a cancel_delayed_work_sync() on the same work callback that we were called from.
Connections in the LE scanning state actually need very little cleanup
- just a small subset of hci_conn_del. To solve the issue, refactor out these essential pieces into a new hci_conn_cleanup() function and call that from the two necessary places.
--- net/bluetooth/hci_conn.c | 53 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 17 deletions(-)
net/bluetooth/hci_conn.c +36 -17
@@ -99,12 +99,41 @@ static void hci_connect_le_scan_cleanup(struct hci_conn *conn) } } +static void hci_conn_cleanup (struct hci_conn *conn) +{ +struct hci_dev *hdev = conn->hdev; + +if (test_bit (HCI_CONN_PARAM_REMOVAL_PEND , &conn->flags)) +hci_conn_params_del (conn->hdev, &conn->dst, conn->dst_type); + +hci_chan_list_flush (conn); + +hci_conn_hash_del (hdev, conn); + +if (hdev->notify) + hdev->notify (hdev,HCI_NOTIFY_CONN_DEL ); + +hci_conn_del_sysfs (conn); + +debugfs_remove_recursive (conn->debugfs); + +hci_dev_put (hdev); + +hci_conn_put (conn); +} +/* This function requires the caller holds hdev->lock */ static void hci_connect_le_scan_remove (struct hci_conn *conn) {hci_connect_le_scan_cleanup (conn); -hci_conn_hash_del (conn->hdev, conn); +/* We can't call hci_conn_del here since that would deadlock +* with trying to call cancel_delayed_work_sync(&conn->disc_work). +* Instead, call just hci_conn_cleanup() which contains the bare +* minimum cleanup operations needed for a connection in this +* state. +*/ +hci_conn_cleanup (conn); }static void hci_acl_create_connection (struct hci_conn *conn)
@@ -582,27 +611,17 @@ int hci_conn_del(struct hci_conn *conn) } } -hci_chan_list_flush (conn); -if (conn->amp_mgr)amp_mgr_put (conn->amp_mgr); -hci_conn_hash_del (hdev, conn); -if (hdev->notify) - hdev->notify (hdev,HCI_NOTIFY_CONN_DEL ); -skb_queue_purge (&conn->data_q); -hci_conn_del_sysfs (conn); - -debugfs_remove_recursive (conn->debugfs); - -if (test_bit (HCI_CONN_PARAM_REMOVAL_PEND , &conn->flags)) -hci_conn_params_del (conn->hdev, &conn->dst, conn->dst_type); - -hci_dev_put (hdev); - -hci_conn_put (conn); +/* Remove the connection from the list and cleanup its remaining +* state. This is a separate function since for some cases like +* BT_CONNECT_SCAN we *only* want the cleanup part without the +* rest of hci_conn_del. +*/ +hci_conn_cleanup (conn);return 0 ; }
Signature · 1 line
2.4.3
PATCH 3/6 Bluetooth: Fix LE reconnection logic
In reply to Johan HedbergFrom: Johan Hedberg <[email protected]>
We can't use hci_explicit_connect_lookup() since that would only cover explicit connections, leaving normal reconnections completely untouched. Not using it in turn means leaving out entries in pend_le_reports.
To fix this and simplify the logic move conn params from the reports list to the pend_le_conns list for the duration of an explicit connect. Once the connect is complete move the params back to the pend_le_reports list. This also means that the explicit connect lookup function only needs to look into the pend_le_conns list.
--- net/bluetooth/hci_conn.c | 4 ++-- net/bluetooth/hci_core.c | 7 ------- net/bluetooth/hci_event.c | 4 ++-- net/bluetooth/mgmt.c | 5 ++++- 4 files changed, 8 insertions(+), 12 deletions(-)
net/bluetooth/hci_conn.c +2 -2
@@ -1000,8 +1000,8 @@ static int hci_explicit_conn_params_set(struct hci_request *req,/* If we created new params, or existing params were marked as disabled, * mark them to be used just once to connect. */ -if (params->auto_connect ==HCI_AUTO_CONN_DISABLED ) { - params->auto_connect =HCI_AUTO_CONN_EXPLICIT ; +if (params->auto_connect ==HCI_AUTO_CONN_DISABLED || + params->auto_connect ==HCI_AUTO_CONN_REPORT ) {list_del_init (¶ms->action);list_add (¶ms->action, &hdev->pend_le_conns); }
net/bluetooth/hci_core.c +0 -7
@@ -2861,13 +2861,6 @@ struct hci_conn_params *hci_explicit_connect_lookup(struct hci_dev *hdev,return param; } -list_for_each_entry (param, &hdev->pend_le_reports, action) { -if (bacmp (¶m->addr, addr) ==0 && - param->addr_type == addr_type && - param->explicit_connect) -return param; - } -return NULL ; }
net/bluetooth/hci_event.c +2 -2
@@ -4653,8 +4653,8 @@ static struct hci_conn *check_pending_le_conn(struct hci_dev *hdev,/* If we're not connectable only connect devices that we have in * our pend_le_conns list. */ - params =hci_explicit_connect_lookup (hdev, addr, addr_type); - + params =hci_pend_le_action_lookup (&hdev->pend_le_conns, addr, + addr_type);if (!params)return NULL ;
net/bluetooth/mgmt.c +4 -1
@@ -6117,7 +6117,10 @@ static int hci_conn_params_set(struct hci_request *req, bdaddr_t *addr,__hci_update_background_scan (req);break ;case HCI_AUTO_CONN_REPORT : -list_add (¶ms->action, &hdev->pend_le_reports); +if (params->explicit_connect) +list_add (¶ms->action, &hdev->pend_le_conns); +else +list_add (¶ms->action, &hdev->pend_le_reports);__hci_update_background_scan (req);break ;case HCI_AUTO_CONN_DIRECT :
Signature · 1 line
2.4.3
PATCH 4/6 Bluetooth: Fix remove_device behavior for explicit connects
In reply to Johan HedbergFrom: Johan Hedberg <[email protected]>
Devices undergoing an explicit connect should not have their conn_params struct removed by the mgmt Remove Device command. This patch fixes the necessary checks in the command handler to correct the behavior.
--- net/bluetooth/mgmt.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
net/bluetooth/mgmt.c +6 -1
@@ -6386,7 +6386,8 @@ static int remove_device(struct sock *sk, struct hci_dev *hdev, goto unlock; } -if (params->auto_connect ==HCI_AUTO_CONN_DISABLED ) { +if (params->auto_connect ==HCI_AUTO_CONN_DISABLED || + params->auto_connect ==HCI_AUTO_CONN_EXPLICIT ) { err = cmd->cmd_complete (cmd,MGMT_STATUS_INVALID_PARAMS );mgmt_pending_remove (cmd);
@@ -6422,6 +6423,10 @@ static int remove_device(struct sock *sk, struct hci_dev *hdev,if (p->auto_connect ==HCI_AUTO_CONN_DISABLED )continue ;device_removed (sk, hdev, &p->addr, p->addr_type); +if (p->explicit_connect) { + p->auto_connect =HCI_AUTO_CONN_EXPLICIT ; +continue ; + }list_del (&p->action);list_del (&p->list);kfree (p);
Signature · 1 line
2.4.3
PATCH 5/6 Bluetooth: Fix conn_params list update in hci_connect_le_scan_cleanup
In reply to Johan HedbergFrom: Johan Hedberg <[email protected]>
After clearing the params->explicit_connect variable the parameters may need to be either added back to the right list or potentially left absent from both the le_reports and the le_conns lists.
--- net/bluetooth/hci_conn.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-)
net/bluetooth/hci_conn.c +19 -4
@@ -91,12 +91,27 @@ static void hci_connect_le_scan_cleanup(struct hci_conn *conn)* autoconnect action, remove them completely. If they are, just unmark * them as waiting for connection, by clearing explicit_connect field. */ -if (params->auto_connect ==HCI_AUTO_CONN_EXPLICIT ) { + params->explicit_connect = false; + +list_del_init (¶ms->action); + +switch (params->auto_connect) { +case HCI_AUTO_CONN_EXPLICIT :hci_conn_params_del (conn->hdev, bdaddr, bdaddr_type); - }else { - params->explicit_connect = false; -hci_update_background_scan (conn->hdev); +/* return instead of break to avoid duplicate scan update */ +return ; +case HCI_AUTO_CONN_DIRECT : +case HCI_AUTO_CONN_ALWAYS : +list_add (¶ms->action, &conn->hdev->pend_le_conns); +break ; +case HCI_AUTO_CONN_REPORT : +list_add (¶ms->action, &conn->hdev->pend_le_reports); +break ; +default : +break ; } + +hci_update_background_scan (conn->hdev); }static void hci_conn_cleanup (struct hci_conn *conn)
Signature · 1 line
2.4.3
PATCH 6/6 Bluetooth: Fix initializing conn_params in scan phase
In reply to Johan HedbergFrom: Jakub Pawlowski <[email protected]>
This patch makes sure that conn_params that were created just for explicit_connect, will get properly deleted during cleanup.
--- net/bluetooth/hci_conn.c | 22 +++++++++++++++------- net/bluetooth/mgmt.c | 6 +++++- 2 files changed, 20 insertions(+), 8 deletions(-)
net/bluetooth/hci_conn.c +15 -7
@@ -1008,15 +1008,23 @@ static int hci_explicit_conn_params_set(struct hci_request *req,if (is_connected (hdev, addr, addr_type))return -EISCONN ; - params =hci_conn_params_add (hdev, addr, addr_type); -if (!params) -return -EIO ; + params =hci_conn_params_lookup (hdev, addr, addr_type); +if (!params) { + params =hci_conn_params_add (hdev, addr, addr_type); +if (!params) +return -ENOMEM ; + +/* If we created new params, mark them to be deleted in +* hci_connect_le_scan_cleanup. It's different case than +* existing disabled params, those will stay after cleanup. +*/ + params->auto_connect =HCI_AUTO_CONN_EXPLICIT ; + } -/* If we created new params, or existing params were marked as disabled, -* mark them to be used just once to connect. -*/ +/* We're trying to connect, so make sure params are at pend_le_conns */ if (params->auto_connect ==HCI_AUTO_CONN_DISABLED || - params->auto_connect ==HCI_AUTO_CONN_REPORT ) { + params->auto_connect ==HCI_AUTO_CONN_REPORT || + params->auto_connect ==HCI_AUTO_CONN_EXPLICIT ) {list_del_init (¶ms->action);list_add (¶ms->action, &hdev->pend_le_conns); }
net/bluetooth/mgmt.c +5 -1
@@ -3545,6 +3545,7 @@ static int pair_device(struct sock *sk, struct hci_dev *hdev, void *data, auth_type); }else {u8 addr_type; +struct hci_conn_params *p;/* Convert from L2CAP channel address type to HCI address type */
@@ -3562,7 +3563,10 @@ static int pair_device(struct sock *sk, struct hci_dev *hdev, void *data,* If connection parameters already exist, then they * will be kept and this function does nothing. */ -hci_conn_params_add (hdev, &cp->addr.bdaddr, addr_type); + p =hci_conn_params_add (hdev, &cp->addr.bdaddr, addr_type); + +if (p->auto_connect ==HCI_AUTO_CONN_EXPLICIT ) + p->auto_connect =HCI_AUTO_CONN_DISABLED ; conn = hci_connect_le_scan(hdev, &cp->addr.bdaddr, addr_type, sec_level,
Signature · 1 line
2.4.3