Skip to content

Commit 794d175

Browse files
David HerrmannGustavo F. Padovan
authored andcommitted
Bluetooth: hidp: Stop I/O on shutdown
Current hidp driver purges the in/out queue on HID shutdown, but does not prevent further I/O. If a driver uses hidp_output_raw_report or hidp_get_raw_report during shutdown, the driver hangs for 5 or 10 seconds per call until it gets a timeout. That is, if the output queue of an HID driver has 10 messages pending, it will take 50s until hid_destroy_device() will return. The hidp_session_sem semaphore is held during shutdown so no other HID device may be added/removed during this time. This patch makes hidp_output_raw_report and hidp_get_raw_report fail if session->terminate is true. Also hidp_session will wakeup all current calls to these functions to cancel the current operations. We already purge the current I/O queues on hidp_stop(), so this data loss does not change the behaviour of the HID drivers. Signed-off-by: David Herrmann <dh.herrmann@googlemail.com> Signed-off-by: Gustavo F. Padovan <padovan@profusion.mobi>
1 parent a5fd6f3 commit 794d175

1 file changed

Lines changed: 20 additions & 14 deletions

File tree

‎net/bluetooth/hidp/core.c‎

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,9 @@ static int __hidp_send_ctrl_message(struct hidp_session *session,
255255

256256
BT_DBG("session %p data %p size %d", session, data, size);
257257

258+
if (atomic_read(&session->terminate))
259+
return -EIO;
260+
258261
skb = alloc_skb(size + 1, GFP_ATOMIC);
259262
if (!skb) {
260263
BT_ERR("Can't allocate memory for new frame");
@@ -329,6 +332,7 @@ static int hidp_get_raw_report(struct hid_device *hid,
329332
struct sk_buff *skb;
330333
size_t len;
331334
int numbered_reports = hid->report_enum[report_type].numbered;
335+
int ret;
332336

333337
switch (report_type) {
334338
case HID_FEATURE_REPORT:
@@ -352,8 +356,9 @@ static int hidp_get_raw_report(struct hid_device *hid,
352356
session->waiting_report_number = numbered_reports ? report_number : -1;
353357
set_bit(HIDP_WAITING_FOR_RETURN, &session->flags);
354358
data[0] = report_number;
355-
if (hidp_send_ctrl_message(hid->driver_data, report_type, data, 1))
356-
goto err_eio;
359+
ret = hidp_send_ctrl_message(hid->driver_data, report_type, data, 1);
360+
if (ret)
361+
goto err;
357362

358363
/* Wait for the return of the report. The returned report
359364
gets put in session->report_return. */
@@ -365,11 +370,13 @@ static int hidp_get_raw_report(struct hid_device *hid,
365370
5*HZ);
366371
if (res == 0) {
367372
/* timeout */
368-
goto err_eio;
373+
ret = -EIO;
374+
goto err;
369375
}
370376
if (res < 0) {
371377
/* signal */
372-
goto err_restartsys;
378+
ret = -ERESTARTSYS;
379+
goto err;
373380
}
374381
}
375382

@@ -390,14 +397,10 @@ static int hidp_get_raw_report(struct hid_device *hid,
390397

391398
return len;
392399

393-
err_restartsys:
394-
clear_bit(HIDP_WAITING_FOR_RETURN, &session->flags);
395-
mutex_unlock(&session->report_mutex);
396-
return -ERESTARTSYS;
397-
err_eio:
400+
err:
398401
clear_bit(HIDP_WAITING_FOR_RETURN, &session->flags);
399402
mutex_unlock(&session->report_mutex);
400-
return -EIO;
403+
return ret;
401404
}
402405

403406
static int hidp_output_raw_report(struct hid_device *hid, unsigned char *data, size_t count,
@@ -422,11 +425,10 @@ static int hidp_output_raw_report(struct hid_device *hid, unsigned char *data, s
422425

423426
/* Set up our wait, and send the report request to the device. */
424427
set_bit(HIDP_WAITING_FOR_SEND_ACK, &session->flags);
425-
if (hidp_send_ctrl_message(hid->driver_data, report_type,
426-
data, count)) {
427-
ret = -ENOMEM;
428+
ret = hidp_send_ctrl_message(hid->driver_data, report_type, data,
429+
count);
430+
if (ret)
428431
goto err;
429-
}
430432

431433
/* Wait for the ACK from the device. */
432434
while (test_bit(HIDP_WAITING_FOR_SEND_ACK, &session->flags)) {
@@ -739,6 +741,10 @@ static int hidp_session(void *arg)
739741
remove_wait_queue(sk_sleep(intr_sk), &intr_wait);
740742
remove_wait_queue(sk_sleep(ctrl_sk), &ctrl_wait);
741743

744+
clear_bit(HIDP_WAITING_FOR_SEND_ACK, &session->flags);
745+
clear_bit(HIDP_WAITING_FOR_RETURN, &session->flags);
746+
wake_up_interruptible(&session->report_queue);
747+
742748
down_write(&hidp_session_sem);
743749

744750
hidp_del_timer(session);

0 commit comments

Comments
 (0)