Bluetooth: hci_conn: Consolidate code for aborting connections
Subsystems (2)
Luiz Augusto von Dentz
maintainerPATCH v4 1/3 Bluetooth: hci_conn: Consolidate code for aborting connections
From: Luiz Augusto von Dentz <[email protected]>
This consolidates code for aborting connections using hci_cmd_sync_queue so it is synchronized with other threads, but because of the fact that some commands may block the cmd_sync_queue while waiting specific events this attempt to cancel those requests by using hci_cmd_sync_cancel.
--- include/net/bluetooth/hci_core.h | 2 +- net/bluetooth/hci_conn.c | 160 +++++++------------------------ net/bluetooth/hci_sync.c | 23 +++-- net/bluetooth/mgmt.c | 15 +-- 4 files changed, 50 insertions(+), 150 deletions(-)
include/net/bluetooth/hci_core.h +1 -1
@@ -741,6 +741,7 @@ struct hci_conn {unsigned long flags;enum conn_reasons conn_reason; +__u8 abort_reason;__u32 clock;__u16 clock_accuracy;
@@ -760,7 +761,6 @@ struct hci_conn {struct delayed_work auto_accept_work;struct delayed_work idle_work;struct delayed_work le_conn_timeout; -struct work_struct le_scan_cleanup;struct device dev;struct dentry *debugfs;
net/bluetooth/hci_conn.c +33 -127
191 lines not on this page · raw message
net/bluetooth/hci_sync.c +15 -8
@@ -5271,22 +5271,27 @@ static int hci_disconnect_sync(struct hci_dev *hdev, struct hci_conn *conn, }static int hci_le_connect_cancel_sync (struct hci_dev *hdev, -struct hci_conn *conn) +struct hci_conn *conn,u8 reason) { +/* Return reason if scanning since the connection shall probably be +* cleanup directly. +*/ if (test_bit (HCI_CONN_SCANNING , &conn->flags)) -return 0 ; +return reason; -if (test_and_set_bit (HCI_CONN_CANCEL , &conn->flags)) +if (conn->role ==HCI_ROLE_SLAVE || +test_and_set_bit (HCI_CONN_CANCEL , &conn->flags))return 0 ;return __hci_cmd_sync_status (hdev,HCI_OP_LE_CREATE_CONN_CANCEL ,0 ,NULL ,HCI_CMD_TIMEOUT ); } -static int hci_connect_cancel_sync (struct hci_dev *hdev,struct hci_conn *conn) +static int hci_connect_cancel_sync (struct hci_dev *hdev,struct hci_conn *conn, +u8 reason) {if (conn->type ==LE_LINK ) -return hci_le_connect_cancel_sync (hdev, conn); +return hci_le_connect_cancel_sync (hdev, conn, reason);if (hdev->hci_ver <BLUETOOTH_VER_1_2 )return 0 ;
@@ -5339,9 +5344,11 @@ int hci_abort_conn_sync(struct hci_dev *hdev, struct hci_conn *conn, u8 reason)case BT_CONFIG :return hci_disconnect_sync (hdev, conn, reason);case BT_CONNECT : - err =hci_connect_cancel_sync (hdev, conn); + err =hci_connect_cancel_sync (hdev, conn, reason);/* Cleanup hci_conn object if it cannot be cancelled as it -* likelly means the controller and host stack are out of sync. +* likelly means the controller and host stack are out of sync +* or in case of LE it was still scanning so it can be cleanup +* safely. */ if (err) {hci_dev_lock (hdev);
@@ -6255,7 +6262,7 @@ int hci_le_create_conn_sync(struct hci_dev *hdev, struct hci_conn *conn) done:if (err == -ETIMEDOUT ) -hci_le_connect_cancel_sync (hdev, conn); +hci_le_connect_cancel_sync (hdev, conn,0x00 );/* Re-enable advertising after the connection attempt is finished. */ hci_resume_advertising_sync (hdev);
net/bluetooth/mgmt.c +1 -14
@@ -3586,18 +3586,6 @@ static int pair_device(struct sock *sk, struct hci_dev *hdev, void *data,return err; } -static int abort_conn_sync (struct hci_dev *hdev,void *data) -{ -struct hci_conn *conn; -u16 handle =PTR_ERR (data); - - conn =hci_conn_hash_lookup_handle (hdev, handle); -if (!conn) -return 0 ; - -return hci_abort_conn_sync (hdev, conn,HCI_ERROR_REMOTE_USER_TERM ); -} -static int cancel_pair_device (struct sock *sk,struct hci_dev *hdev,void *data,u16 len) {
@@ -3648,8 +3636,7 @@ static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data, le_addr_type(addr->type));if (conn->conn_reason ==CONN_REASON_PAIR_DEVICE ) -hci_cmd_sync_queue (hdev, abort_conn_sync,ERR_PTR (conn->handle), -NULL ); +hci_abort_conn (conn,HCI_ERROR_REMOTE_USER_TERM ); unlock:hci_dev_unlock (hdev);
Signature · 1 line
2.40.1
Luiz Augusto von Dentz
maintainerPATCH v4 2/3 Bluetooth: hci_sync: Fix not handling ISO_LINK in hci_abort_conn_sync
From: Luiz Augusto von Dentz <[email protected]>
ISO_LINK connections where not being handled properly on hci_abort_conn_sync which sometimes resulted in sending the wrong commands, or in case of having the reject command being sent by the socket code (iso.c) which is sort of a layer violation.
--- net/bluetooth/hci_conn.c | 23 +++++++++++++++++++---- net/bluetooth/hci_sync.c | 34 ++++++++++++++++++++++++++++++++++ net/bluetooth/iso.c | 14 -------------- 3 files changed, 53 insertions(+), 18 deletions(-)
net/bluetooth/hci_conn.c +19 -4
@@ -1223,7 +1223,12 @@ void hci_conn_failed(struct hci_conn *conn, u8 status)static void create_le_conn_complete (struct hci_dev *hdev,void *data,int err) { -struct hci_conn *conn = data; +struct hci_conn *conn; +u16 handle =PTR_ERR (data); + + conn =hci_conn_hash_lookup_handle (hdev, handle); +if (!conn) +return ;bt_dev_dbg (hdev,"err %d" , err);
@@ -1248,10 +1253,17 @@ static void create_le_conn_complete(struct hci_dev *hdev, void *data, int err)static int hci_connect_le_sync (struct hci_dev *hdev,void *data) { -struct hci_conn *conn = data; +struct hci_conn *conn; +u16 handle =PTR_ERR (data); + + conn =hci_conn_hash_lookup_handle (hdev, handle); +if (!conn) +return 0 ;bt_dev_dbg (hdev,"conn %p" , conn); + conn->state =BT_CONNECT ; +return hci_le_create_conn_sync (hdev, conn); }
@@ -1321,10 +1333,10 @@ struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst, conn->sec_level =BT_SECURITY_LOW ; conn->conn_timeout = conn_timeout; - conn->state =BT_CONNECT ;clear_bit (HCI_CONN_SCANNING , &conn->flags); - err =hci_cmd_sync_queue (hdev, hci_connect_le_sync, conn, + err =hci_cmd_sync_queue (hdev, hci_connect_le_sync, +ERR_PTR (conn->handle), create_le_conn_complete);if (err) {hci_conn_del (conn);
@@ -2858,6 +2870,9 @@ int hci_abort_conn(struct hci_conn *conn, u8 reason)/* If the connection is pending check the command opcode since that * might be blocking on hci_cmd_sync_work while waiting its respective * event so we need to hci_cmd_sync_cancel to cancel it. +* +* hci_connect_le serializes the connection attempts so only one +* connection can be in BT_CONNECT at time. */ if (conn->state ==BT_CONNECT && hdev->req_status ==HCI_REQ_PEND ) {switch (hci_skb_event (hdev->sent_cmd)) {
net/bluetooth/hci_sync.c +34 -0
@@ -5293,6 +5293,24 @@ static int hci_connect_cancel_sync(struct hci_dev *hdev, struct hci_conn *conn,if (conn->type ==LE_LINK )return hci_le_connect_cancel_sync (hdev, conn, reason); +if (conn->type ==ISO_LINK ) { +/* BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E +* page 1857: +* +* If this command is issued for a CIS on the Central and the +* CIS is successfully terminated before being established, +* then an HCI_LE_CIS_Established event shall also be sent for +* this CIS with the Status Operation Cancelled by Host (0x44). +*/ +if (test_bit (HCI_CONN_CREATE_CIS , &conn->flags)) +return hci_disconnect_sync (hdev, conn, reason); + +/* There is no way to cancel a BIS without terminating the BIG +* which is done later on connection cleanup. +*/ +return 0 ; + } +if (hdev->hci_ver <BLUETOOTH_VER_1_2 )return 0 ;
@@ -5319,11 +5337,27 @@ static int hci_reject_sco_sync(struct hci_dev *hdev, struct hci_conn *conn, sizeof(cp), &cp, HCI_CMD_TIMEOUT); } +static int hci_le_reject_cis_sync (struct hci_dev *hdev,struct hci_conn *conn, +u8 reason) +{ +struct hci_cp_le_reject_cis cp; + +memset (&cp,0 ,sizeof (cp)); + cp.handle =cpu_to_le16 (conn->handle); + cp.reason = reason; + +return __hci_cmd_sync_status (hdev,HCI_OP_LE_REJECT_CIS , +sizeof (cp), &cp,HCI_CMD_TIMEOUT ); +} +static int hci_reject_conn_sync (struct hci_dev *hdev,struct hci_conn *conn,u8 reason) {struct hci_cp_reject_conn_req cp; +if (conn->type ==ISO_LINK ) +return hci_le_reject_cis_sync (hdev, conn, reason); +if (conn->type ==SCO_LINK || conn->type ==ESCO_LINK )return hci_reject_sco_sync (hdev, conn, reason);
net/bluetooth/iso.c +0 -14
@@ -614,18 +614,6 @@ static void iso_sock_kill(struct sock *sk)sock_put (sk); } -static void iso_conn_defer_reject (struct hci_conn *conn) -{ -struct hci_cp_le_reject_cis cp; - -BT_DBG ("conn %p" , conn); - -memset (&cp,0 ,sizeof (cp)); - cp.handle =cpu_to_le16 (conn->handle); - cp.reason =HCI_ERROR_REJ_BAD_ADDR ; -hci_send_cmd (conn->hdev,HCI_OP_LE_REJECT_CIS ,sizeof (cp), &cp); -} -static void __iso_sock_close (struct sock *sk) {BT_DBG ("sk %p state %d socket %p" , sk, sk->sk_state, sk->sk_socket);
@@ -650,8 +638,6 @@ static void __iso_sock_close(struct sock *sk)break ;case BT_CONNECT2 : -if (iso_pi (sk)->conn->hcon) -iso_conn_defer_reject (iso_pi (sk)->conn->hcon);iso_chan_del (sk,ECONNRESET );break ;case BT_CONNECT :
Signature · 1 line
2.40.1
Luiz Augusto von Dentz
maintainerPATCH v4 3/3 Bluetooth: hci_conn: Always allocate unique handles
In reply to Luiz Augusto von DentzFrom: Luiz Augusto von Dentz <[email protected]>
This attempts to always allocate a unique handle for connections so they can be properly aborted by the likes of hci_abort_conn, so this uses the invalid range as a pool of unset handles that way if userspace is trying to create multiple connections at once each will be given a unique handle which will be considered unset.
--- include/net/bluetooth/hci_core.h | 2 +- net/bluetooth/hci_conn.c | 25 ++++++++++++++++++++++--- net/bluetooth/hci_event.c | 6 +++--- 3 files changed, 26 insertions(+), 7 deletions(-)
include/net/bluetooth/hci_core.h +1 -1
@@ -321,8 +321,8 @@ struct adv_monitor {#define HCI_MAX_SHORT_NAME_LENGTH 10 -#define HCI_CONN_HANDLE_UNSET 0xffff#define HCI_CONN_HANDLE_MAX 0x0eff +#define HCI_CONN_HANDLE_UNSET (_handle) (_handle > HCI_CONN_HANDLE_MAX)/* Min encryption key size to match with SMP */ #define HCI_MIN_ENC_KEY_SIZE 7
net/bluetooth/hci_conn.c +22 -3
@@ -932,6 +932,25 @@ static void cis_cleanup(struct hci_conn *conn)hci_le_remove_cig (hdev, conn->iso_qos.ucast.cig); } +static u16 hci_conn_hash_alloc_unset (struct hci_dev *hdev) +{ +struct hci_conn_hash *h = &hdev->conn_hash; +struct hci_conn *c; +u16 handle =HCI_CONN_HANDLE_MAX +1 ; + +rcu_read_lock (); + +list_for_each_entry_rcu (c, &h->list, list) { +/* Find the first unused handle */ +if (handle ==0xffff || c->handle != handle) +break ; + handle++; + } +rcu_read_unlock (); + +return handle; +} +struct hci_conn *hci_conn_add (struct hci_dev *hdev,int type,bdaddr_t *dst,u8 role) {
@@ -945,7 +964,7 @@ struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst,bacpy (&conn->dst, dst);bacpy (&conn->src, &hdev->bdaddr); - conn->handle =HCI_CONN_HANDLE_UNSET ; + conn->handle =hci_conn_hash_alloc_unset (hdev); conn->hdev = hdev; conn->type = type; conn->role = role;
@@ -1057,7 +1076,7 @@ static void hci_conn_unlink(struct hci_conn *conn)*/ if ((child->type ==SCO_LINK || child->type ==ESCO_LINK ) && - child->handle ==HCI_CONN_HANDLE_UNSET ) +HCI_CONN_HANDLE_UNSET (child->handle))hci_conn_del (child); }
@@ -1943,7 +1962,7 @@ int hci_conn_check_create_cis(struct hci_conn *conn)return -EINVAL ;if (!conn->parent || conn->parent->state !=BT_CONNECTED || - conn->state !=BT_CONNECT || conn->handle ==HCI_CONN_HANDLE_UNSET ) + conn->state !=BT_CONNECT ||HCI_CONN_HANDLE_UNSET (conn->handle))return 1 ;return 0 ;
net/bluetooth/hci_event.c +3 -3
@@ -3173,7 +3173,7 @@ static void hci_conn_complete_evt(struct hci_dev *hdev, void *data,* As the connection handle is set here for the first time, it indicates * whether the connection is already set up. */ -if (conn->handle !=HCI_CONN_HANDLE_UNSET ) { +if (!HCI_CONN_HANDLE_UNSET (conn->handle)) {bt_dev_err (hdev,"Ignoring HCI_Connection_Complete for existing connection" ); goto unlock; }
@@ -5032,7 +5032,7 @@ static void hci_sync_conn_complete_evt(struct hci_dev *hdev, void *data,* As the connection handle is set here for the first time, it indicates * whether the connection is already set up. */ -if (conn->handle !=HCI_CONN_HANDLE_UNSET ) { +if (!HCI_CONN_HANDLE_UNSET (conn->handle)) {bt_dev_err (hdev,"Ignoring HCI_Sync_Conn_Complete event for existing connection" ); goto unlock; }
@@ -5896,7 +5896,7 @@ static void le_conn_complete_evt(struct hci_dev *hdev, u8 status,* As the connection handle is set here for the first time, it indicates * whether the connection is already set up. */ -if (conn->handle !=HCI_CONN_HANDLE_UNSET ) { +if (!HCI_CONN_HANDLE_UNSET (conn->handle)) {bt_dev_err (hdev,"Ignoring HCI_Connection_Complete for existing connection" ); goto unlock; }
Signature · 1 line
2.40.1
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/project/bluetooth/list/?series=761073
---Test result---
Test Summary:
CheckPatch PASS 3.47 seconds GitLint PASS 0.89 seconds SubjectPrefix PASS 0.30 seconds BuildKernel PASS 37.83 seconds CheckAllWarning PASS 42.13 seconds CheckSparse WARNING 47.23 seconds CheckSmatch WARNING 127.59 seconds BuildKernel32 PASS 37.05 seconds TestRunnerSetup PASS 525.20 seconds TestRunner_l2cap-tester PASS 15.19 seconds TestRunner_iso-tester PASS 27.81 seconds TestRunner_bnep-tester PASS 6.49 seconds TestRunner_mgmt-tester PASS 147.80 seconds TestRunner_rfcomm-tester PASS 10.38 seconds TestRunner_sco-tester PASS 11.59 seconds TestRunner_ioctl-tester PASS 11.01 seconds TestRunner_mesh-tester PASS 8.37 seconds TestRunner_smp-tester PASS 9.48 seconds TestRunner_userchan-tester PASS 6.79 seconds IncrementalBuild PASS 72.87 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: CheckSmatch - WARNING
Desc: Run smatch tool with source
Output:net/bluetooth/hci_event.c: note: in included file (through include/net/bluetooth/hci_core.h):
---
Regards,
Linux Bluetooth
This series was applied to bluetooth/bluetooth-next.git (master) by Luiz Augusto von Dentz <[email protected]>:
Hello:
This series was applied to bluetooth/bluetooth-next.git (master) by Luiz Augusto von Dentz <[email protected]>:
Quoted 9 lines · On Wed, 28 Jun 2023 17:13:08 -0700 you wrote:
From: Luiz Augusto von Dentz <[email protected]>
This consolidates code for aborting connections using
hci_cmd_sync_queueso it is synchronized with other threads, but because of the fact that some commands may block thecmd_sync_queuewhile waiting specific events this attempt to cancel those requests by usinghci_cmd_sync_cancel.[...]
Here is the summary with links:
- [v4,1/3] Bluetooth: hci_conn: Consolidate code for aborting connections
https://git.kernel.org/bluetooth/bluetooth-next/c/22f0e1a7429e - [v4,2/3] Bluetooth: hci_sync: Fix not handling ISO_LINK in hci_abort_conn_sync https://git.kernel.org/bluetooth/bluetooth-next/c/b2947ca2f663 - [v4,3/3] Bluetooth: hci_conn: Always allocate unique handles https://git.kernel.org/bluetooth/bluetooth-next/c/fd859b4a3815
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html