Commit a087a98e authored by Johan Hedberg's avatar Johan Hedberg Committed by Marcel Holtmann

Bluetooth: btusb: Fix race when waiting for BTUSB_DOWNLOADING

The test for BTUSB_DOWNLOADING must be after adding to the wait queue
and setting the TASK_INTERRUPTIBLE state. Otherwise the flag may get
cleared after we test for it and we end up getting a timeout since
schedule_timeout() waits for the full duration. This patch uses a
wait_on_bit_timeout() + wake_up_bit(). To perform the task both
race-free as well as in a much simpler way.

Since there's no global wait_on_bit_timeout() helper yet (even though
all the building blocks for it are in place) this patch creates a
temporary local btusb copy of it until the global one has made it to
upstream trees.
Signed-off-by: default avatarJohan Hedberg <johan.hedberg@intel.com>
Signed-off-by: default avatarMarcel Holtmann <marcel@holtmann.org>
parent 74d9daaf
...@@ -334,6 +334,16 @@ struct btusb_data { ...@@ -334,6 +334,16 @@ struct btusb_data {
int (*recv_bulk)(struct btusb_data *data, void *buffer, int count); int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
}; };
static int btusb_wait_on_bit_timeout(void *word, int bit, unsigned long timeout,
unsigned mode)
{
might_sleep();
if (!test_bit(bit, word))
return 0;
return out_of_line_wait_on_bit_timeout(word, bit, bit_wait_timeout,
mode, timeout);
}
static inline void btusb_free_frags(struct btusb_data *data) static inline void btusb_free_frags(struct btusb_data *data)
{ {
unsigned long flags; unsigned long flags;
...@@ -1800,8 +1810,10 @@ static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb) ...@@ -1800,8 +1810,10 @@ static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
if (test_and_clear_bit(BTUSB_DOWNLOADING, if (test_and_clear_bit(BTUSB_DOWNLOADING,
&data->flags) && &data->flags) &&
test_bit(BTUSB_FIRMWARE_LOADED, &data->flags)) test_bit(BTUSB_FIRMWARE_LOADED, &data->flags)) {
wake_up_interruptible(&hdev->req_wait_q); smp_mb__after_atomic();
wake_up_bit(&data->flags, BTUSB_DOWNLOADING);
}
} }
/* When switching to the operational firmware the device /* When switching to the operational firmware the device
...@@ -2165,43 +2177,32 @@ static int btusb_setup_intel_new(struct hci_dev *hdev) ...@@ -2165,43 +2177,32 @@ static int btusb_setup_intel_new(struct hci_dev *hdev)
set_bit(BTUSB_FIRMWARE_LOADED, &data->flags); set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
BT_INFO("%s: Waiting for firmware download to complete", hdev->name);
/* Before switching the device into operational mode and with that /* Before switching the device into operational mode and with that
* booting the loaded firmware, wait for the bootloader notification * booting the loaded firmware, wait for the bootloader notification
* that all fragments have been successfully received. * that all fragments have been successfully received.
* *
* When the event processing receives the notification, then this * When the event processing receives the notification, then the
* flag will be cleared. So just in case that happens really quickly, * BTUSB_DOWNLOADING flag will be cleared.
* check it first before adding the wait queue. *
* The firmware loading should not take longer than 5 seconds
* and thus just timeout if that happens and fail the setup
* of this device.
*/ */
if (test_bit(BTUSB_DOWNLOADING, &data->flags)) { err = btusb_wait_on_bit_timeout(&data->flags, BTUSB_DOWNLOADING,
DECLARE_WAITQUEUE(wait, current); msecs_to_jiffies(5000),
signed long timeout; TASK_INTERRUPTIBLE);
if (err == 1) {
BT_INFO("%s: Waiting for firmware download to complete", BT_ERR("%s: Firmware loading interrupted", hdev->name);
hdev->name); err = -EINTR;
goto done;
add_wait_queue(&hdev->req_wait_q, &wait); }
set_current_state(TASK_INTERRUPTIBLE);
/* The firmware loading should not take longer than 5 seconds
* and thus just timeout if that happens and fail the setup
* of this device.
*/
timeout = schedule_timeout(msecs_to_jiffies(5000));
remove_wait_queue(&hdev->req_wait_q, &wait);
if (signal_pending(current)) {
BT_ERR("%s: Firmware loading interrupted", hdev->name);
err = -EINTR;
goto done;
}
if (!timeout) { if (err) {
BT_ERR("%s: Firmware loading timeout", hdev->name); BT_ERR("%s: Firmware loading timeout", hdev->name);
err = -ETIMEDOUT; err = -ETIMEDOUT;
goto done; goto done;
}
} }
if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) { if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment