2024-01-08 18:40:04

by Jonas Dreßler

[permalink] [raw]
Subject: [PATCH v2 0/4] Bluetooth: Improve retrying of connection attempts

Since commit 4c67bc74f016 ("[Bluetooth] Support concurrent connect
requests"), the kernel supports trying to connect again in case the
bluetooth card is busy and fails to connect.

The logic that should handle this became a bit spotty over time, and also
cards these days appear to fail with more errors than just "Command
Disallowed".

This series refactores the handling of concurrent connection requests
by serializing all "Create Connection" commands for ACL connections
similar to how we do it for LE connections.

---

v1: https://lore.kernel.org/linux-bluetooth/[email protected]/
v2:
- Move to using hci_sync queue for "Create Connection" and therefore
always serialize those requests.
- Follow commit message style better and properly cite patches

Jonas Dreßler (4):
Bluetooth: Remove superfluous call to hci_conn_check_pending()
Bluetooth: hci_event: Use HCI error defines instead of magic values
Bluetooth: hci_conn: Only do ACL connections sequentially
Bluetooth: Remove pending ACL connection attempts

include/net/bluetooth/hci.h | 3 ++
include/net/bluetooth/hci_core.h | 1 -
net/bluetooth/hci_conn.c | 51 ++++++++++++++++----------------
net/bluetooth/hci_event.c | 29 +++++-------------
4 files changed, 35 insertions(+), 49 deletions(-)

--
2.43.0



2024-01-08 18:40:25

by Jonas Dreßler

[permalink] [raw]
Subject: [PATCH v2 1/4] Bluetooth: Remove superfluous call to hci_conn_check_pending()

The "pending connections" feature was originally introduced with commit
4c67bc74f016 ("[Bluetooth] Support concurrent connect requests") and
6bd57416127e ("[Bluetooth] Handling pending connect attempts after
inquiry") to handle controllers supporting only a single connection request
at a time. Later things were extended to also cancel ongoing inquiries on
connect() with commit 89e65975fea5 ("Bluetooth: Cancel Inquiry before
Create Connection").

With commit a9de9248064b ("[Bluetooth] Switch from OGF+OCF to using only
opcodes"), hci_conn_check_pending() was introduced as a helper to
consolidate a few places where we check for pending connections (indicated
by the BT_CONNECT2 flag) and then try to connect.

This refactoring commit also snuck in two more calls to
hci_conn_check_pending():

- One is in the failure callback of hci_cs_inquiry(), this one probably
makes sense: If we send an "HCI Inquiry" command and then immediately
after a "Create Connection" command, the "Create Connection" command might
fail before the "HCI Inquiry" command, and then we want to retry the
"Create Connection" on failure of the "HCI Inquiry".

- The other added call to hci_conn_check_pending() is in the event handler
for the "Remote Name" event, this seems unrelated and is possibly a
copy-paste error, so remove that one.

Fixes: a9de9248064b ("[Bluetooth] Switch from OGF+OCF to using only opcodes")
Signed-off-by: Jonas Dreßler <[email protected]>
---
net/bluetooth/hci_event.c | 2 --
1 file changed, 2 deletions(-)

diff --git a/net/bluetooth/hci_event.c b/net/bluetooth/hci_event.c
index 31ca320ce..13396329f 100644
--- a/net/bluetooth/hci_event.c
+++ b/net/bluetooth/hci_event.c
@@ -3538,8 +3538,6 @@ static void hci_remote_name_evt(struct hci_dev *hdev, void *data,

bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);

- hci_conn_check_pending(hdev);
-
hci_dev_lock(hdev);

conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
--
2.43.0


2024-01-08 18:41:39

by Jonas Dreßler

[permalink] [raw]
Subject: [PATCH v2 4/4] Bluetooth: Remove pending ACL connection attempts

With the last commit we moved to using the hci_sync queue for "Create
Connection" requests, removing the need for retrying the paging after
finished/failed "Create Connection" requests and after the end of
inquiries.

hci_conn_check_pending() was used to trigger this retry, we can remove it
now.

Note that we can also remove the special handling for COMMAND_DISALLOWED
errors in the completion handler of "Create Connection", because "Create
Connection" requests are now always serialized.

This is somewhat reverting commit 4c67bc74f016 ("[Bluetooth] Support
concurrent connect requests").

With this, the BT_CONNECT2 state of ACL hci_conn objects should now be
back to meaning only one thing: That we received a connection request
from another device (see hci_conn_request_evt), but the actual connect
should be deferred.
---
include/net/bluetooth/hci_core.h | 1 -
net/bluetooth/hci_conn.c | 16 ----------------
net/bluetooth/hci_event.c | 21 ++++-----------------
3 files changed, 4 insertions(+), 34 deletions(-)

diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h
index 2c30834c1..d7483958d 100644
--- a/include/net/bluetooth/hci_core.h
+++ b/include/net/bluetooth/hci_core.h
@@ -1330,7 +1330,6 @@ struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst,
u8 role);
void hci_conn_del(struct hci_conn *conn);
void hci_conn_hash_flush(struct hci_dev *hdev);
-void hci_conn_check_pending(struct hci_dev *hdev);

struct hci_chan *hci_chan_create(struct hci_conn *conn);
void hci_chan_del(struct hci_chan *chan);
diff --git a/net/bluetooth/hci_conn.c b/net/bluetooth/hci_conn.c
index 541d55301..22033057b 100644
--- a/net/bluetooth/hci_conn.c
+++ b/net/bluetooth/hci_conn.c
@@ -2534,22 +2534,6 @@ void hci_conn_hash_flush(struct hci_dev *hdev)
}
}

-/* Check pending connect attempts */
-void hci_conn_check_pending(struct hci_dev *hdev)
-{
- struct hci_conn *conn;
-
- BT_DBG("hdev %s", hdev->name);
-
- hci_dev_lock(hdev);
-
- conn = hci_conn_hash_lookup_state(hdev, ACL_LINK, BT_CONNECT2);
- if (conn)
- hci_cmd_sync_queue(hdev, hci_acl_create_connection_sync, conn, NULL);
-
- hci_dev_unlock(hdev);
-}
-
static u32 get_link_mode(struct hci_conn *conn)
{
u32 link_mode = 0;
diff --git a/net/bluetooth/hci_event.c b/net/bluetooth/hci_event.c
index e8b4a0126..91973d6d1 100644
--- a/net/bluetooth/hci_event.c
+++ b/net/bluetooth/hci_event.c
@@ -117,8 +117,6 @@ static u8 hci_cc_inquiry_cancel(struct hci_dev *hdev, void *data,
hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
hci_dev_unlock(hdev);

- hci_conn_check_pending(hdev);
-
return rp->status;
}

@@ -149,8 +147,6 @@ static u8 hci_cc_exit_periodic_inq(struct hci_dev *hdev, void *data,

hci_dev_clear_flag(hdev, HCI_PERIODIC_INQ);

- hci_conn_check_pending(hdev);
-
return rp->status;
}

@@ -2296,10 +2292,8 @@ static void hci_cs_inquiry(struct hci_dev *hdev, __u8 status)
{
bt_dev_dbg(hdev, "status 0x%2.2x", status);

- if (status) {
- hci_conn_check_pending(hdev);
+ if (status)
return;
- }

set_bit(HCI_INQUIRY, &hdev->flags);
}
@@ -2323,12 +2317,9 @@ static void hci_cs_create_conn(struct hci_dev *hdev, __u8 status)

if (status) {
if (conn && conn->state == BT_CONNECT) {
- if (status != HCI_ERROR_COMMAND_DISALLOWED || conn->attempt > 2) {
- conn->state = BT_CLOSED;
- hci_connect_cfm(conn, status);
- hci_conn_del(conn);
- } else
- conn->state = BT_CONNECT2;
+ conn->state = BT_CLOSED;
+ hci_connect_cfm(conn, status);
+ hci_conn_del(conn);
}
} else {
if (!conn) {
@@ -3020,8 +3011,6 @@ static void hci_inquiry_complete_evt(struct hci_dev *hdev, void *data,

bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);

- hci_conn_check_pending(hdev);
-
if (!test_and_clear_bit(HCI_INQUIRY, &hdev->flags))
return;

@@ -3247,8 +3236,6 @@ static void hci_conn_complete_evt(struct hci_dev *hdev, void *data,

unlock:
hci_dev_unlock(hdev);
-
- hci_conn_check_pending(hdev);
}

static void hci_reject_conn(struct hci_dev *hdev, bdaddr_t *bdaddr)
--
2.43.0


2024-01-08 19:15:02

by Luiz Augusto von Dentz

[permalink] [raw]
Subject: Re: [PATCH v2 4/4] Bluetooth: Remove pending ACL connection attempts

Hi Jonas,

On Mon, Jan 8, 2024 at 1:55 PM Jonas Dreßler <[email protected]> wrote:
>
> On 1/8/24 19:44, Jonas Dreßler wrote:
> > On 1/8/24 19:39, Jonas Dreßler wrote:
> >> With the last commit we moved to using the hci_sync queue for "Create
> >> Connection" requests, removing the need for retrying the paging after
> >> finished/failed "Create Connection" requests and after the end of
> >> inquiries.
> >>
> >> hci_conn_check_pending() was used to trigger this retry, we can remove it
> >> now.
> >>
> >> Note that we can also remove the special handling for COMMAND_DISALLOWED
> >> errors in the completion handler of "Create Connection", because "Create
> >> Connection" requests are now always serialized.
> >>
> >> This is somewhat reverting commit 4c67bc74f016 ("[Bluetooth] Support
> >> concurrent connect requests").
> >>
> >> With this, the BT_CONNECT2 state of ACL hci_conn objects should now be
> >> back to meaning only one thing: That we received a connection request
> >> from another device (see hci_conn_request_evt), but the actual connect
> >> should be deferred.
> >> ---
> >> include/net/bluetooth/hci_core.h | 1 -
> >> net/bluetooth/hci_conn.c | 16 ----------------
> >> net/bluetooth/hci_event.c | 21 ++++-----------------
> >> 3 files changed, 4 insertions(+), 34 deletions(-)
> >>
> >> diff --git a/include/net/bluetooth/hci_core.h
> >> b/include/net/bluetooth/hci_core.h
> >> index 2c30834c1..d7483958d 100644
> >> --- a/include/net/bluetooth/hci_core.h
> >> +++ b/include/net/bluetooth/hci_core.h
> >> @@ -1330,7 +1330,6 @@ struct hci_conn *hci_conn_add(struct hci_dev
> >> *hdev, int type, bdaddr_t *dst,
> >> u8 role);
> >> void hci_conn_del(struct hci_conn *conn);
> >> void hci_conn_hash_flush(struct hci_dev *hdev);
> >> -void hci_conn_check_pending(struct hci_dev *hdev);
> >> struct hci_chan *hci_chan_create(struct hci_conn *conn);
> >> void hci_chan_del(struct hci_chan *chan);
> >> diff --git a/net/bluetooth/hci_conn.c b/net/bluetooth/hci_conn.c
> >> index 541d55301..22033057b 100644
> >> --- a/net/bluetooth/hci_conn.c
> >> +++ b/net/bluetooth/hci_conn.c
> >> @@ -2534,22 +2534,6 @@ void hci_conn_hash_flush(struct hci_dev *hdev)
> >> }
> >> }
> >> -/* Check pending connect attempts */
> >> -void hci_conn_check_pending(struct hci_dev *hdev)
> >> -{
> >> - struct hci_conn *conn;
> >> -
> >> - BT_DBG("hdev %s", hdev->name);
> >> -
> >> - hci_dev_lock(hdev);
> >> -
> >> - conn = hci_conn_hash_lookup_state(hdev, ACL_LINK, BT_CONNECT2);
> >> - if (conn)
> >> - hci_cmd_sync_queue(hdev, hci_acl_create_connection_sync,
> >> conn, NULL);
> >> -
> >> - hci_dev_unlock(hdev);
> >> -}
> >> -
> >> static u32 get_link_mode(struct hci_conn *conn)
> >> {
> >> u32 link_mode = 0;
> >> diff --git a/net/bluetooth/hci_event.c b/net/bluetooth/hci_event.c
> >> index e8b4a0126..91973d6d1 100644
> >> --- a/net/bluetooth/hci_event.c
> >> +++ b/net/bluetooth/hci_event.c
> >> @@ -117,8 +117,6 @@ static u8 hci_cc_inquiry_cancel(struct hci_dev
> >> *hdev, void *data,
> >> hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
> >> hci_dev_unlock(hdev);
> >> - hci_conn_check_pending(hdev);
> >> -
> >> return rp->status;
> >> }
> >> @@ -149,8 +147,6 @@ static u8 hci_cc_exit_periodic_inq(struct hci_dev
> >> *hdev, void *data,
> >> hci_dev_clear_flag(hdev, HCI_PERIODIC_INQ);
> >> - hci_conn_check_pending(hdev);
> >> -
> >> return rp->status;
> >> }
> >> @@ -2296,10 +2292,8 @@ static void hci_cs_inquiry(struct hci_dev
> >> *hdev, __u8 status)
> >> {
> >> bt_dev_dbg(hdev, "status 0x%2.2x", status);
> >> - if (status) {
> >> - hci_conn_check_pending(hdev);
> >> + if (status)
> >> return;
> >> - }
> >> set_bit(HCI_INQUIRY, &hdev->flags);
> >> }
> >> @@ -2323,12 +2317,9 @@ static void hci_cs_create_conn(struct hci_dev
> >> *hdev, __u8 status)
> >> if (status) {
> >> if (conn && conn->state == BT_CONNECT) {
> >> - if (status != HCI_ERROR_COMMAND_DISALLOWED ||
> >> conn->attempt > 2) {
> >> - conn->state = BT_CLOSED;
> >> - hci_connect_cfm(conn, status);
> >> - hci_conn_del(conn);
> >> - } else
> >> - conn->state = BT_CONNECT2;
> >> + conn->state = BT_CLOSED;
> >> + hci_connect_cfm(conn, status);
> >> + hci_conn_del(conn);
> >> }
> >> } else {
> >> if (!conn) {
> >> @@ -3020,8 +3011,6 @@ static void hci_inquiry_complete_evt(struct
> >> hci_dev *hdev, void *data,
> >> bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
> >> - hci_conn_check_pending(hdev);
> >> -
> >> if (!test_and_clear_bit(HCI_INQUIRY, &hdev->flags))
> >> return;
> >> @@ -3247,8 +3236,6 @@ static void hci_conn_complete_evt(struct hci_dev
> >> *hdev, void *data,
> >> unlock:
> >> hci_dev_unlock(hdev);
> >> -
> >> - hci_conn_check_pending(hdev);
> >> }
> >> static void hci_reject_conn(struct hci_dev *hdev, bdaddr_t *bdaddr)
> >
> > Please take a special look at this one: I'm not sure if I'm breaking the
> > functionality of deferred connecting using BT_CONNECT2 in
> > hci_conn_request_evt() here, as I don't see anywhere where we check for
> > this state and establish a connection later.
> >
> > It seems that this is how hci_conn_request_evt() was initially written
> > though, hci_conn_check_pending() only got introduced later and seems
> > unrelated.
>
> Ahh nevermind... The check for BT_CONNECT2 on "Conn Complete event" got
> introduced with 4c67bc74f01 ([Bluetooth] Support concurrent connect
> requests). And later the deferred connection setup on "Conn Request
> event" got introduced with 20714bfef8 ("Bluetooth: Implement deferred
> sco socket setup").
>
> I assume the latter commit was relying on the "Create Connection"
> request "Conn Complete event" that got introduced with the former commit
> then? That would imply that we use BT_CONNECT2 if there's already a
> "Create Connection" going on when the "Conn Request event" happens, and
> we must wait for that existing request to finish.. Is that how those
> deferred connections are supposed to work?

Well if you are not sure that works we better make sure we have tests
that cover this, for LE I know for sure it works because we have the
likes of iso-tester that do connect 2 peers simultaneously, but for
classic I don't recall having any test that does multiple connections.

> >
> > Thanks,
> > Jonas



--
Luiz Augusto von Dentz