From 1dfd62b2b573a7417bbad5a80ecaca4039d3e78a Mon Sep 17 00:00:00 2001 From: Laurent Gauthier Date: Wed, 9 Sep 2020 00:11:49 +0200 Subject: [PATCH] HID: hid-debug: fix nonblocking read semantics wrt EIO/ERESTARTSYS When the file has been open in non-blocking mode, EIO or ERESTARTSYS would never be returned even if they should (for example when device has been unplugged, you want EIO and not EAGAIN to be returned). Move the O_NONBLOCK check after other checks have been performed. Based on similar to patches hidraw and hiddev by Founder Fang and Jiri Kosina . Signed-off-by: Laurent Gauthier Signed-off-by: Jiri Kosina --- drivers/hid/hid-debug.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/drivers/hid/hid-debug.c b/drivers/hid/hid-debug.c index 9453147d020db..d7eaf91003706 100644 --- a/drivers/hid/hid-debug.c +++ b/drivers/hid/hid-debug.c @@ -1101,11 +1101,6 @@ static ssize_t hid_debug_events_read(struct file *file, char __user *buffer, set_current_state(TASK_INTERRUPTIBLE); while (kfifo_is_empty(&list->hid_debug_fifo)) { - if (file->f_flags & O_NONBLOCK) { - ret = -EAGAIN; - break; - } - if (signal_pending(current)) { ret = -ERESTARTSYS; break; @@ -1122,6 +1117,11 @@ static ssize_t hid_debug_events_read(struct file *file, char __user *buffer, goto out; } + if (file->f_flags & O_NONBLOCK) { + ret = -EAGAIN; + break; + } + /* allow O_NONBLOCK from other threads */ mutex_unlock(&list->read_mutex); schedule(); -- 2.39.5