linux-bluetooth.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andre Guedes <andre.guedes@openbossa.org>
To: Johan Hedberg <johan.hedberg@gmail.com>
Cc: linux-bluetooth@vger.kernel.org
Subject: Re: [RFC v4 05/12] Bluetooth: Stop scanning on LE connection
Date: Thu, 19 Dec 2013 18:07:07 -0300	[thread overview]
Message-ID: <1387487227.986.30.camel@bali> (raw)
In-Reply-To: <20131218111116.GA25848@x220.p-661hnu-f1>

Hi Johan,

On Wed, 2013-12-18 at 13:11 +0200, Johan Hedberg wrote:
> Hi Andre,
> 
> On Tue, Dec 17, 2013, Andre Guedes wrote:
> > > > @@ -518,8 +518,17 @@ static void create_le_conn_complete(struct hci_dev *hdev, u8 status)
> > > >  {
> > > >  	struct hci_conn *conn;
> > > >  
> > > > -	if (status == 0)
> > > > +	if (status == 0) {
> > > > +		/* If the discovery procedure was running, we prematurely
> > > > +		 * stopped it. So we have to change the discovery state.
> > > > +		 */
> > > > +		if (hdev->discovery.state == DISCOVERY_FINDING) {
> > > > +			cancel_delayed_work(&hdev->le_scan_disable);
> > > > +			hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
> > > > +		}
> > > > +
> > > >  		return;
> > > > +	}
> > > >  
> > > >  	BT_ERR("HCI request failed to create LE connection: status 0x%2.2x",
> > > >  	       status);
> > > > @@ -552,6 +561,18 @@ static int hci_create_le_conn(struct hci_conn *conn)
> > > >  
> > > >  	hci_req_init(&req, hdev);
> > > >  
> > > > +	/* If controller is scanning, we stop it since some controllers are
> > > > +	 * not able to scan and connect at the same time.
> > > > +	 */
> > > > +	if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) {
> > > > +		struct hci_cp_le_set_scan_enable enable_cp;
> > > > +
> > > > +		memset(&enable_cp, 0, sizeof(enable_cp));
> > > > +		enable_cp.enable = LE_SCAN_DISABLE;
> > > > +		hci_req_add(&req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(enable_cp),
> > > > +			    &enable_cp);
> > > > +	}
> > > > +
> > > >  	memset(&cp, 0, sizeof(cp));
> > > >  	cp.scan_interval = cpu_to_le16(hdev->le_scan_interval);
> > > >  	cp.scan_window = cpu_to_le16(hdev->le_scan_window);
> > > 
> > > The way this all hangs together with a discovery process started through
> > > mgmt_start_discovery feels a bit flimsy to me. Particularly, have you
> > > ensured that everything is fine if you've got inquiry ongoing when you
> > > do hci_discovery_set_state(hdev, DISCOVERY_STOPPED). Also, are there any
> > > risks of race conditions here, e.g. is it fine to let the
> > > cancel_delayed_work() call be in create_le_conn_complete() instead of
> > > doing it in hci_create_le_conn()?
> > 
> > Yes, I did lots of testing, including the inquiry test you mentioned,
> > and I didn't find any issues.
> > 
> > I failed to see any race conditons since cancel_delayed_work() does not
> > block. So it is fine to call cancel_delayed_work() in
> > create_le_conn_complete() as well as in hci_create_le_conn().
> 
> I presume you'd want to prevent the delayed work from getting called as
> soon as you've entered the if (test_bit(HCI_LE_SCAN)) section in
> hci_create_le_conn? In theory it might get called before
> create_le_conn_complete. In such a scenario
> le_scan_disable_work_complete could e.g. trigger inquiry before
> create_le_conn_complete is called, couldn't it?

Yes, this scenario can indeed happen in a very short window. However, it
doesn't seem to be a big issue. If that happen then the discovery
procedure keeps running normally and the connection attempt fails. If it
is a auto connection then the kernel will retry in a near future. If it
a user connection, it will get a busy error.   

> > > What also makes this hard to track is that the condition you're testing
> > > for first is the HCI_LE_SCAN bit, but then later you look at
> > > discovery.state == DISCOVERY_FINDING. For the casual reader there's no
> > > direct indication of how these two are releated for the various types of
> > > discovery that are possible (LE-only, BR/EDR-only and interleaved).
> > 
> > Yes, I see your point. However, we can't check HCI_LE_SCAN in
> > create_le_conn_complete() because the flag was already cleared in
> > hci_cc_le_set_scan_enable().
> 
> Understood.
> 
> > > I don't mean to say that this is a nack for the patch, but I'd like to
> > > know that you've considered and tested this kind of cases. I had to
> > > spend quite some time looking through the existing code and this patch
> > > and still couldn't arrive at absolute confidence of its correctness,
> > > meaning there should hopefully be some room for simplification.
> > 
> > So I think we can do some simplification by moving this discovery
> > handling from create_le_conn_complete() to hci_create_le_conn(), at
> > least I think this code will become a bit easier to follow. What do you
> > think?
> 
> I think it would at least close the potential race I described above,
> i.e. the delayed work getting triggered before create_le_conn_complete.

I realized this approach is not good enough since we'll be changing
discovery state before being sure the HCI command executed successfully.
This could let the discovery state inconsistent.

So after spending some time thinking about this issue, I believe the
better way to handle this is taking it in two steps. First, we simply
stop scanning and handle the discovery state. Then we create the LE
connection.

I'm already working on this "two steps" approach and I should send the
v5 patch set very soon.

Regards,

Andre


  reply	other threads:[~2013-12-19 21:07 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-12-06 22:05 [RFC v4 00/12] LE auto connection and connection parameters Andre Guedes
2013-12-06 22:05 ` [RFC v4 01/12] Bluetooth: Save connection interval parameters in hci_conn Andre Guedes
2013-12-06 22:05 ` [RFC v4 02/12] Bluetooth: Group list_head fields from strcut hci_dev together Andre Guedes
2013-12-06 22:05 ` [RFC v4 03/12] Bluetooth: Introduce connection parameters list Andre Guedes
2013-12-06 22:05 ` [RFC v4 04/12] Bluetooth: Use connection parameters if any Andre Guedes
2013-12-06 22:05 ` [RFC v4 05/12] Bluetooth: Stop scanning on LE connection Andre Guedes
2013-12-10 13:21   ` Johan Hedberg
2013-12-17 14:05     ` Andre Guedes
2013-12-18 11:11       ` Johan Hedberg
2013-12-19 21:07         ` Andre Guedes [this message]
2013-12-06 22:05 ` [RFC v4 06/12] Bluetooth: Introduce hdev->pend_le_conn list Andre Guedes
2013-12-06 22:05 ` [RFC v4 07/12] Bluetooth: Introduce LE auto connection infrastructure Andre Guedes
2013-12-06 22:05 ` [RFC v4 08/12] Bluetooth: Re-enable background scan in case of error Andre Guedes
2013-12-06 22:05 ` [RFC v4 09/12] Bluetooth: Temporarily stop background scanning on discovery Andre Guedes
2013-12-06 22:05 ` [RFC v4 10/12] Bluetooth: Auto connection and power on Andre Guedes
2013-12-06 22:05 ` [RFC v4 11/12] Bleutooth: Add support for auto connect options Andre Guedes
2013-12-06 22:05 ` [RFC v4 12/12] Bluetooth: Add le_auto_conn file on debugfs Andre Guedes

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1387487227.986.30.camel@bali \
    --to=andre.guedes@openbossa.org \
    --cc=johan.hedberg@gmail.com \
    --cc=linux-bluetooth@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).