From: Benjamin Tissoires <benjamin.tissoires@redhat.com>
To: Jiri Kosina <jkosina@suse.cz>,
Dmitry Torokhov <dmitry.torokhov@gmail.com>,
Benjamin Tissoires <benjamin.tissoires@gmail.com>,
linux-input@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: Benjamin Tissoires <benjamin.tissoires@redhat.com>
Subject: [PATCH 3/4] HID: debug: empty output queue on read
Date: Wed, 17 Apr 2013 19:38:15 +0200 [thread overview]
Message-ID: <1366220296-14346-4-git-send-email-benjamin.tissoires@redhat.com> (raw)
In-Reply-To: <1366220296-14346-1-git-send-email-benjamin.tissoires@redhat.com>
If an event occurs while the hid debugfs is forwarding events, list->tail
is updated during copy_to_user().
Remove the gotos and use a regular while-loop to empty the queue.
Second benefit, it checks that we are not writing more than count bytes
to the user-space output buffer.
Signed-off-by: Benjamin Tissoires <benjamin.tissoires@redhat.com>
---
drivers/hid/hid-debug.c | 27 +++++++++++----------------
1 file changed, 11 insertions(+), 16 deletions(-)
diff --git a/drivers/hid/hid-debug.c b/drivers/hid/hid-debug.c
index 094cbcf..1dc8104 100644
--- a/drivers/hid/hid-debug.c
+++ b/drivers/hid/hid-debug.c
@@ -1000,6 +1000,7 @@ static ssize_t hid_debug_events_read(struct file *file, char __user *buffer,
size_t count, loff_t *ppos)
{
struct hid_debug_list *list = file->private_data;
+ char *buf_head;
int ret = 0, len;
DECLARE_WAITQUEUE(wait, current);
@@ -1039,28 +1040,22 @@ static ssize_t hid_debug_events_read(struct file *file, char __user *buffer,
goto out;
/* pass the ringbuffer contents to userspace */
-copy_rest:
- if (list->tail == list->head)
- goto out;
- if (list->tail > list->head) {
- len = list->tail - list->head;
+ while (list->tail != list->head && ret < count) {
+ buf_head = &list->hid_debug_buf[list->head];
- if (copy_to_user(buffer + ret, &list->hid_debug_buf[list->head], len)) {
- ret = -EFAULT;
- goto out;
- }
- ret += len;
- list->head += len;
- } else {
- len = HID_DEBUG_BUFSIZE - list->head;
+ if (list->tail > list->head)
+ len = list->tail - list->head;
+ else
+ len = HID_DEBUG_BUFSIZE - list->head;
+
+ len = min(count - ret, len);
- if (copy_to_user(buffer, &list->hid_debug_buf[list->head], len)) {
+ if (copy_to_user(buffer + ret, buf_head, len)) {
ret = -EFAULT;
goto out;
}
- list->head = 0;
ret += len;
- goto copy_rest;
+ list->head = (list->head + len) % HID_DEBUG_BUFSIZE;
}
}
--
1.8.1.4
next prev parent reply other threads:[~2013-04-17 17:38 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-04-17 17:38 [PATCH 0/4] HID: debugfs rework Benjamin Tissoires
2013-04-17 17:38 ` [PATCH 1/4] HID: debug: break out hid_dump_report() into hid-debug Benjamin Tissoires
2013-04-19 2:01 ` Jiri Kosina
2013-04-17 17:38 ` [PATCH 2/4] HID: core: break out hid_find_max_report() in hid-core Benjamin Tissoires
2013-04-17 17:38 ` Benjamin Tissoires [this message]
2013-04-19 1:55 ` [PATCH 3/4] HID: debug: empty output queue on read Jiri Kosina
2013-04-17 17:38 ` [PATCH 4/4] HID: debug: allocate the output buffer with an estimate Benjamin Tissoires
2013-04-17 17:44 ` [PATCH 0/4] HID: debugfs rework Jiri Kosina
2013-04-18 7:52 ` Benjamin Tissoires
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1366220296-14346-4-git-send-email-benjamin.tissoires@redhat.com \
--to=benjamin.tissoires@redhat.com \
--cc=benjamin.tissoires@gmail.com \
--cc=dmitry.torokhov@gmail.com \
--cc=jkosina@suse.cz \
--cc=linux-input@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).