[lkml]   [2016]   [Nov]   [13]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
Messages in this thread
Patch in this message
Subject[PATCH 3.16 002/346] HID: uhid: fix timeout when probe races with IO
3.16.39-rc1 review patch.  If anyone has any objections, please let me know.


From: Roderick Colenbrander <>

commit 67f8ecc550b5bda03335f845dc869b8501d25fd0 upstream.

Many devices use userspace bluetooth stacks like BlueZ or Bluedroid in combination
with uhid. If any of these stacks is used with a HID device for which the driver
performs a HID request as part .probe (or technically another HID operation),
this results in a deadlock situation. The deadlock results in a 5 second timeout
for I/O operations in HID drivers, so isn't fatal, but none of the I/O operations
have a chance of succeeding.

The root cause for the problem is that uhid only allows for one request to be
processed at a time per uhid instance and locks out other operations. This means
that if a user space is creating a new HID device through 'UHID_CREATE', which
ultimately triggers '.probe' through the HID layer. Then any HID request e.g. a
read for calibration data would trigger a HID operation on uhid again, but it
won't go out to userspace, because it is still stuck in UHID_CREATE.
In addition bluetooth stacks are typically single threaded, so they wouldn't be
able to handle any requests while waiting on uhid.

Lucikly the UHID spec is somewhat flexible and allows for fixing the issue,
without breaking user space. The idea which the patch implements as discussed
with David Herrmann is to decouple adding of a hid device (which triggers .probe)
from UHID_CREATE. The work will kick off roughly once UHID_CREATE completed (or
else will wait a tiny bit of time in .probe for a lock). A HID driver has to call
HID to call 'hid_hw_start()' as part of .probe once it is ready for I/O, which
triggers UHID_START to user space. Any HID operations should function now within
.probe and won't deadlock because userspace is stuck on UHID_CREATE.

We verified this patch on Bluedroid with Android 6.0 and on desktop Linux with
BlueZ stacks. Prior to the patch they had the deadlock issue.

[ reword subject]
Signed-off-by: Roderick Colenbrander <>
Signed-off-by: Jiri Kosina <>
[bwh: Backported to 3.16: adjust context]
Signed-off-by: Ben Hutchings <>
drivers/hid/uhid.c | 33 ++++++++++++++++++++++++---------
1 file changed, 24 insertions(+), 9 deletions(-)

--- a/drivers/hid/uhid.c
+++ b/drivers/hid/uhid.c
@@ -49,10 +49,26 @@ struct uhid_device {
atomic_t report_done;
atomic_t report_id;
struct uhid_event report_buf;
+ struct work_struct worker;

static struct miscdevice uhid_misc;

+static void uhid_device_add_worker(struct work_struct *work)
+ struct uhid_device *uhid = container_of(work, struct uhid_device, worker);
+ int ret;
+ ret = hid_add_device(uhid->hid);
+ if (ret) {
+ hid_err(uhid->hid, "Cannot register HID device: error %d\n", ret);
+ hid_destroy_device(uhid->hid);
+ uhid->hid = NULL;
+ uhid->running = false;
+ }
static void uhid_queue(struct uhid_device *uhid, struct uhid_event *ev)
__u8 newhead;
@@ -471,18 +487,14 @@ static int uhid_dev_create2(struct uhid_
uhid->hid = hid;
uhid->running = true;

- ret = hid_add_device(hid);
- if (ret) {
- hid_err(hid, "Cannot register HID device\n");
- goto err_hid;
- }
+ /* Adding of a HID device is done through a worker, to allow HID drivers
+ * which use feature requests during .probe to work, without they would
+ * be blocked on devlock, which is held by uhid_char_write.
+ */
+ schedule_work(&uhid->worker);

return 0;

- hid_destroy_device(hid);
- uhid->hid = NULL;
- uhid->running = false;
return ret;
@@ -499,6 +511,8 @@ static int uhid_dev_destroy(struct uhid_
atomic_set(&uhid->report_done, 1);

+ cancel_work_sync(&uhid->worker);

@@ -567,6 +581,7 @@ static int uhid_char_open(struct inode *
uhid->running = false;
atomic_set(&uhid->report_done, 1);
+ INIT_WORK(&uhid->worker, uhid_device_add_worker);

file->private_data = uhid;
nonseekable_open(inode, file);
 \ /
  Last update: 2016-11-14 05:02    [W:0.846 / U:0.048 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site