Bluetooth: Fix race condition with rfkill handling
Subsystems (2)
cover Bluetooth: Fix race condition with rfkill handling
Hi,
These two patches fix a pretty severe (hence Cc: stable) race condition with rfkill handling. If a device gets rfkilled before the HCI setup stage completes it stays unaccessible through the mgmt interface even it eventually gets unblocked through rfkill.
The situation is easy to reproduce by running "rfkill block all" before inserting a dongle, "rfkill unblock all" a bit later and then trying to do e.g. "btmgmt info". The device will not show up there.
Johan
----------------------------------------------------------------
Johan Hedberg (2):
Bluetooth: Introduce a new HCI_RFKILLED flag
Bluetooth: Fix rfkill functionality during the HCI setup stage
include/net/bluetooth/hci.h | 1 + net/bluetooth/hci_core.c | 22 +++++++++++++++++----- 2 files changed, 18 insertions(+), 5 deletions(-)
PATCH 1/2 Bluetooth: Introduce a new HCI_RFKILLED flag
From: Johan Hedberg <[email protected]>
This makes it more convenient to check for rfkill (no need to check for dev->rfkill before calling rfkill_blocked()) and also avoids potential races if the RFKILL state needs to be checked from within the rfkill callback.
--- include/net/bluetooth/hci.h | 1 + net/bluetooth/hci_core.c | 12 +++++++----- 2 files changed, 8 insertions(+), 5 deletions(-)
include/net/bluetooth/hci.h +1 -0
@@ -104,6 +104,7 @@ enum {enum {HCI_SETUP ,HCI_AUTO_OFF , +HCI_RFKILLED ,HCI_MGMT ,HCI_PAIRABLE ,HCI_SERVICE_CACHE ,
net/bluetooth/hci_core.c +7 -5
@@ -1148,7 +1148,7 @@ int hci_dev_open(__u16 dev) goto done; } -if (hdev->rfkill &&rfkill_blocked (hdev->rfkill)) { +if (test_bit (HCI_RFKILLED , &hdev->dev_flags)) { ret = -ERFKILL ; goto done; }
@@ -1597,10 +1597,12 @@ static int hci_rfkill_set_block(void *data, bool blocked)if (test_bit (HCI_USER_CHANNEL , &hdev->dev_flags))return -EBUSY ; -if (!blocked) -return 0 ; - -hci_dev_do_close (hdev); +if (blocked) { +set_bit (HCI_RFKILLED , &hdev->dev_flags); +hci_dev_do_close (hdev); + }else { +clear_bit (HCI_RFKILLED , &hdev->dev_flags); + }return 0 ; }
Signature · 1 line
1.8.4.rc3
PATCH 2/2 Bluetooth: Fix rfkill functionality during the HCI setup stage
In reply to [email protected]From: Johan Hedberg <[email protected]>
We need to let the setup stage complete cleanly even when the HCI device is rfkilled. Otherwise the HCI device will stay in an undefined state and never get notified to user space through mgmt (even when it gets unblocked through rfkill).
This patch makes sure that hci_dev_open() can be called in the HCI_SETUP stage, that blocking the device doesn't abort the setup stage, and that the device gets proper powered down as soon as the setup stage completes in case it was blocked meanwhile.
The bug that this patch fixed can be very easily reproduced using e.g. the rfkill command line too. By running "rfkill block all" before inserting a Bluetooth dongle the resulting HCI device goes into a state where it is never announced over mgmt, not even when "rfkill unblock all" is run.
--- net/bluetooth/hci_core.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)
net/bluetooth/hci_core.c +12 -2
@@ -1148,7 +1148,11 @@ int hci_dev_open(__u16 dev) goto done; } -if (test_bit (HCI_RFKILLED , &hdev->dev_flags)) { +/* Check for rfkill but allow the HCI setup stage to proceed +* (which in itself doesn't cause any RF activity). +*/ +if (test_bit (HCI_RFKILLED , &hdev->dev_flags) && + !test_bit (HCI_SETUP , &hdev->dev_flags)) { ret = -ERFKILL ; goto done; }
@@ -1599,7 +1603,8 @@ static int hci_rfkill_set_block(void *data, bool blocked)if (blocked) {set_bit (HCI_RFKILLED , &hdev->dev_flags); -hci_dev_do_close (hdev); +if (!test_bit (HCI_SETUP , &hdev->dev_flags)) +hci_dev_do_close (hdev); }else {clear_bit (HCI_RFKILLED , &hdev->dev_flags); }
@@ -1624,6 +1629,11 @@ static void hci_power_on(struct work_struct *work)return ; } +if (test_bit (HCI_RFKILLED , &hdev->dev_flags)) { +clear_bit (HCI_AUTO_OFF , &hdev->dev_flags); +hci_dev_do_close (hdev); + } +if (test_bit (HCI_AUTO_OFF , &hdev->dev_flags))queue_delayed_work (hdev->req_workqueue, &hdev->power_off,HCI_AUTO_OFF_TIMEOUT );
Signature · 1 line
1.8.4.rc3
Marcel Holtmann
maintainerRe: [PATCH 1/2] Bluetooth: Introduce a new HCI_RFKILLED flag
In reply to [email protected]Hi Johan,
Quoted 29 lines
This makes it more convenient to check for rfkill (no need to check for
dev->rfkillbefore callingrfkill_blocked()) and also avoids potential races if the RFKILL state needs to be checked from within the rfkill callback.Signed-off-by Johan Hedberg <[email protected]>---
include/net/bluetooth/hci.h| 1 +net/bluetooth/hci_core.c| 12 +++++++-----2 files changed, 8 insertions(+), 5 deletions(-)include/net/bluetooth/hci.h +1
-0@@ -104,6 +104,7 @@ enum {enum {HCI_SETUP ,HCI_AUTO_OFF , +HCI_RFKILLED ,HCI_MGMT ,HCI_PAIRABLE ,HCI_SERVICE_CACHE ,net/bluetooth/hci_core.c +0
-0
net/bluetooth/hci_core.c int hci_dev_open(__u16 dev) · line 1154
Quoted 8 lines
net/bluetooth/hci_core.c +1
-1@@ -1148,7 +1148,7 @@ int hci_dev_open(__u16 dev) goto done; } -if (hdev->rfkill &&rfkill_blocked (hdev->rfkill)) { +if (test_bit (HCI_RFKILLED , &hdev->dev_flags)) { ret = -ERFKILL ; goto done; }
the problem here is that we never set the initial value of HCI_RFKILLED properly. To make the patch fully equivalent, we should do that.
Quoted 6 lines
net/bluetooth/hci_core.c +0
-2@@ -1597,10 +1597,12 @@ static int hci_rfkill_set_block(void *data, bool blocked)if (test_bit (HCI_USER_CHANNEL , &hdev->dev_flags))return -EBUSY ; -if (!blocked) -return 0 ;
net/bluetooth/hci_core.c static int hci_rfkill_set_block(void *data, bool blocked) · line 1607
Quoted 10 lines
net/bluetooth/hci_core.c +6
-2- -hci_dev_do_close (hdev); +if (blocked) { +set_bit (HCI_RFKILLED , &hdev->dev_flags); +hci_dev_do_close (hdev); + }else { +clear_bit (HCI_RFKILLED , &hdev->dev_flags); + }return 0 ;
Regards
Marcel
Marcel Holtmann
maintainerRe: [PATCH 2/2] Bluetooth: Fix rfkill functionality during the HCI setup stage
In reply to [email protected]Hi Johan,
Quoted 29 lines
We need to let the setup stage complete cleanly even when the HCI device is rfkilled. Otherwise the HCI device will stay in an undefined state and never get notified to user space through mgmt (even when it gets unblocked through rfkill).
This patch makes sure that
hci_dev_open()can be called in theHCI_SETUPstage, that blocking the device doesn't abort the setup stage, and that the device gets proper powered down as soon as the setup stage completes in case it was blocked meanwhile.The bug that this patch fixed can be very easily reproduced using e.g. the rfkill command line too. By running "rfkill block all" before inserting a Bluetooth dongle the resulting HCI device goes into a state where it is never announced over mgmt, not even when "rfkill unblock all" is run.
Signed-off-by Johan Hedberg <[email protected]>---
net/bluetooth/hci_core.c| 14 ++++++++++++--1 file changed, 12 insertions(+), 2 deletions(-)net/bluetooth/hci_core.c +0
-0@@ -1148,7 +1148,11 @@ int hci_dev_open(__u16 dev) goto done; }
net/bluetooth/hci_core.c int hci_dev_open(__u16 dev) · line 1158
Quoted 10 lines
net/bluetooth/hci_core.c +5
-1-if (test_bit (HCI_RFKILLED , &hdev->dev_flags)) { +/* Check for rfkill but allow the HCI setup stage to proceed +* (which in itself doesn't cause any RF activity). +*/ +if (test_bit (HCI_RFKILLED , &hdev->dev_flags) && + !test_bit (HCI_SETUP , &hdev->dev_flags)) { ret = -ERFKILL ; goto done; }
so explain to me how this actually works. If we add a HCI controller when RFKILL is currently blocking all radios. Which part is setting HCI_RFKILLED initially? Or does the RFKILL subsystem is calling into set_blocked by itself during registration?
Regards
Marcel
Re: [PATCH 2/2] Bluetooth: Fix rfkill functionality during the HCI setup stage
Hi Marcel,
Quoted 30 lines · Marcel Holtmann
Quoted 30 lines
We need to let the setup stage complete cleanly even when the HCI device is rfkilled. Otherwise the HCI device will stay in an undefined state and never get notified to user space through mgmt (even when it gets unblocked through rfkill).
This patch makes sure that
hci_dev_open()can be called in theHCI_SETUPstage, that blocking the device doesn't abort the setup stage, and that the device gets proper powered down as soon as the setup stage completes in case it was blocked meanwhile.The bug that this patch fixed can be very easily reproduced using e.g. the rfkill command line too. By running "rfkill block all" before inserting a Bluetooth dongle the resulting HCI device goes into a state where it is never announced over mgmt, not even when "rfkill unblock all" is run.
Signed-off-by Johan Hedberg <[email protected]>---
net/bluetooth/hci_core.c| 14 ++++++++++++--1 file changed, 12 insertions(+), 2 deletions(-)net/bluetooth/hci_core.c +0
-0@@ -1148,7 +1148,11 @@ int hci_dev_open(__u16 dev) goto done; }
Quoted 14 lines
net/bluetooth/hci_core.c int hci_dev_open(__u16 dev) · line 1158
Quoted 9 lines
net/bluetooth/hci_core.c +5
-1-if (test_bit (HCI_RFKILLED , &hdev->dev_flags)) { +/* Check for rfkill but allow the HCI setup stage to proceed +* (which in itself doesn't cause any RF activity). +*/ +if (test_bit (HCI_RFKILLED , &hdev->dev_flags) && + !test_bit (HCI_SETUP , &hdev->dev_flags)) { ret = -ERFKILL ; goto done; }so explain to me how this actually works. If we add a HCI controller when RFKILL is currently blocking all radios. Which part is setting
HCI_RFKILLEDinitially? Or does the RFKILL subsystem is calling intoset_blockedby itself during registration?
Yes, the rfkill subsystem almost immediately calls our hci_rfkill_set_block() function after we've registered the hdev but before the hci_power_on() work callback that eventually calls hci_dev_open() gets called. That's why I didn't think to initialize HCI_RFKILLED after calling rfkill_register() (instead it defaults to being unset). I will add this extra initialization in v2.
Considering that the situation is really simple to reproduce I'd recommend just trying yourself and you'll see how the function calls go.
Johan