linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Vladis Dronov <vdronov@redhat.com>
To: Jiri Kosina <jikos@kernel.org>,
	Benjamin Tissoires <benjamin.tissoires@redhat.com>,
	linux-input@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: Vladis Dronov <vdronov@redhat.com>
Subject: [PATCH 3/3] HID: debug: fix the ring buffer writer implementation
Date: Wed,  3 Oct 2018 19:19:36 +0200	[thread overview]
Message-ID: <20181003171936.11271-4-vdronov@redhat.com> (raw)
In-Reply-To: <20181003171936.11271-1-vdronov@redhat.com>

ring buffer implementation hid_debug_event() is strange allowing a lost
of data. it does not move head pointer on append and uses per-byte for()
loop. fix this by introducing a new ring buffer implementation, which
overwrites the oldest data in case of the ring buffer overflow. it uses
some calculations for the buffer pointers but only 2 or less memcpy()
calls.

Signed-off-by: Vladis Dronov <vdronov@redhat.com>
---
 drivers/hid/hid-debug.c | 66 ++++++++++++++++++++++++++++++++++++-----
 1 file changed, 58 insertions(+), 8 deletions(-)

diff --git a/drivers/hid/hid-debug.c b/drivers/hid/hid-debug.c
index de640e4132c7..63247ac4a9ce 100644
--- a/drivers/hid/hid-debug.c
+++ b/drivers/hid/hid-debug.c
@@ -661,20 +661,70 @@ EXPORT_SYMBOL_GPL(hid_dump_device);
 /* enqueue string to 'events' ring buffer */
 void hid_debug_event(struct hid_device *hdev, char *buf)
 {
-	unsigned i;
 	struct hid_debug_list *list;
-	unsigned long flags, flags2;
+	unsigned long flags1, flags2;
+	unsigned int part1, part2;
+	size_t len;
 
-	spin_lock_irqsave(&hdev->debug_list_lock, flags);
+	if (!buf || !buf[0])
+		return;
+
+	len = strlen(buf);
+
+	spin_lock_irqsave(&hdev->debug_list_lock, flags1);
 	list_for_each_entry(list, &hdev->debug_list, node) {
 		spin_lock_irqsave(&list->list_lock, flags2);
-		for (i = 0; buf[i]; i++)
-			list->hid_debug_buf[(list->tail + i) % HID_DEBUG_BUFSIZE] =
-				buf[i];
-		list->tail = (list->tail + i) % HID_DEBUG_BUFSIZE;
+
+		/* len >= BUFSIZE, copy the last HID_DEBUG_BUFSIZE-1 bytes */
+		if (len > HID_DEBUG_BUFSIZE - 1) {
+			memcpy(list->hid_debug_buf,
+				buf + len - (HID_DEBUG_BUFSIZE - 1),
+				HID_DEBUG_BUFSIZE - 1);
+
+			list->head = 0;
+			list->tail = HID_DEBUG_BUFSIZE - 1;
+		} else {
+			/* append data to the ring buffer */
+			part1 = min_t(size_t, HID_DEBUG_BUFSIZE - list->tail,
+					len);
+			part2 = len - part1;
+
+			memcpy(list->hid_debug_buf + list->tail, buf, part1);
+
+			/* check if head must be moved as the ring is full */
+			if ((list->tail < list->head) &&
+			    (list->tail + part1 >= list->head)) {
+				list->head = list->tail + part1 + 1;
+
+				if (list->head >= HID_DEBUG_BUFSIZE)
+					list->head -= HID_DEBUG_BUFSIZE;
+			}
+
+			/* set tail after the end of appended data */
+			list->tail = list->tail + part1;
+			if (list->tail == HID_DEBUG_BUFSIZE) {
+				list->tail = 0;
+				if (list->head == 0)
+					list->head = 1;
+			}
+
+			/* append the 2nd part of data to the ring */
+			if (part2 > 0) {
+				memcpy(list->hid_debug_buf, buf + part1, part2);
+
+				/* check if head must be moved again as
+				 * the ring is full
+				 */
+				if (list->tail + part2 >= list->head)
+					list->head = list->tail + part2 + 1;
+
+				list->tail = list->tail + part2;
+			}
+		}
+
 		spin_unlock_irqrestore(&list->list_lock, flags2);
 	}
-	spin_unlock_irqrestore(&hdev->debug_list_lock, flags);
+	spin_unlock_irqrestore(&hdev->debug_list_lock, flags1);
 
 	wake_up_interruptible(&hdev->debug_wait);
 }
-- 
2.19.0


  parent reply	other threads:[~2018-10-03 17:20 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-10-03 17:19 [PATCH 0/3] HID: debug: fix the ring buffer implementation Vladis Dronov
2018-10-03 17:19 ` [PATCH 1/3] HID: debug: avoid infinite loop and corrupting data Vladis Dronov
2018-10-03 17:19 ` [PATCH 2/3] HID: debug: provide reader-writer locking for the ring buffer Vladis Dronov
2018-10-03 17:19 ` Vladis Dronov [this message]
2018-10-26 15:25 ` [PATCH 0/3] HID: debug: fix the ring buffer implementation Jiri Kosina
2018-10-29 20:51   ` Vladis Dronov

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=20181003171936.11271-4-vdronov@redhat.com \
    --to=vdronov@redhat.com \
    --cc=benjamin.tissoires@redhat.com \
    --cc=jikos@kernel.org \
    --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).