linux-input.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] hid: avoid '\0' in hid debugfs events file
@ 2010-03-15 18:00 Bruno Prémont
  2010-03-16 12:58 ` Jiri Kosina
  0 siblings, 1 reply; 3+ messages in thread
From: Bruno Prémont @ 2010-03-15 18:00 UTC (permalink / raw)
  To: Jiri Kosina; +Cc: linux-input, linux-kernel

When dumping /sys/kernel/debug/hid/$device/events '\0' characters show up
(invisible if cat to console but shown by less or while looking at a dump
file).
These are due to hid_debug_event() adding strlen()+1 bytes to the ring
buffer (e.g. including the trailing '\0').
Any roll-over causes a '\0' as well as hid_debug_event() handles the ring
buffers with HID_DEBUG_BUFSIZE-1 size while hid_debug_events_read() handles
it with full HID_DEBUG_BUFSIZE size.

Signed-off-by: Bruno Prémont <bonbons@linux-vserver.org>
---
Note:
The ring buffer overflow case (when tail crosses head) seems to be
suboptimal at best.
Would there be a good way to mark those cases so reader can know where
data got lost. (though this might not be easy keeping the lockless
design)

diff --git a/drivers/hid/hid-debug.c b/drivers/hid/hid-debug.c
index 6abd036..2602695 100644
--- a/drivers/hid/hid-debug.c
+++ b/drivers/hid/hid-debug.c
@@ -564,10 +564,10 @@ void hid_debug_event(struct hid_device *hdev, char *buf)
 	struct hid_debug_list *list;
 
 	list_for_each_entry(list, &hdev->debug_list, node) {
-		for (i = 0; i <= strlen(buf); i++)
-			list->hid_debug_buf[(list->tail + i) % (HID_DEBUG_BUFSIZE - 1)] =
+		for (i = 0; i < strlen(buf); i++)
+			list->hid_debug_buf[(list->tail + i) % HID_DEBUG_BUFSIZE] =
 				buf[i];
-		list->tail = (list->tail + i) % (HID_DEBUG_BUFSIZE - 1);
+		list->tail = (list->tail + i) % HID_DEBUG_BUFSIZE;
         }
 }
 EXPORT_SYMBOL_GPL(hid_debug_event);
--
To unsubscribe from this list: send the line "unsubscribe linux-input" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] hid: avoid '\0' in hid debugfs events file
  2010-03-15 18:00 [PATCH] hid: avoid '\0' in hid debugfs events file Bruno Prémont
@ 2010-03-16 12:58 ` Jiri Kosina
  2010-03-16 17:35   ` Bruno Prémont
  0 siblings, 1 reply; 3+ messages in thread
From: Jiri Kosina @ 2010-03-16 12:58 UTC (permalink / raw)
  To: Bruno Prémont; +Cc: linux-input, linux-kernel

On Mon, 15 Mar 2010, Bruno Prémont wrote:

> When dumping /sys/kernel/debug/hid/$device/events '\0' characters show up
> (invisible if cat to console but shown by less or while looking at a dump
> file).
> These are due to hid_debug_event() adding strlen()+1 bytes to the ring
> buffer (e.g. including the trailing '\0').
> Any roll-over causes a '\0' as well as hid_debug_event() handles the ring
> buffers with HID_DEBUG_BUFSIZE-1 size while hid_debug_events_read() handles
> it with full HID_DEBUG_BUFSIZE size.

Applied, thanks.

> Signed-off-by: Bruno Prémont <bonbons@linux-vserver.org>
> ---
> Note:
> The ring buffer overflow case (when tail crosses head) seems to be
> suboptimal at best.
> Would there be a good way to mark those cases so reader can know where
> data got lost. (though this might not be easy keeping the lockless
> design)

I agree that there might be better implementation. The circular buffer is 
merely the same to what we use in other userspace interfaces already (see 
original hiddev, for example). Plus this is only debugging interface, and 
with SIZE being 512, it's very unlikely that events will get lost.

But I don't think the lockless design is that important here, so if you 
are motivated to rewrite it to something better, I wouldn't object.

Thanks,

-- 
Jiri Kosina
SUSE Labs, Novell Inc.
--
To unsubscribe from this list: send the line "unsubscribe linux-input" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] hid: avoid '\0' in hid debugfs events file
  2010-03-16 12:58 ` Jiri Kosina
@ 2010-03-16 17:35   ` Bruno Prémont
  0 siblings, 0 replies; 3+ messages in thread
From: Bruno Prémont @ 2010-03-16 17:35 UTC (permalink / raw)
  To: Jiri Kosina; +Cc: linux-input, linux-kernel

On Tue, 16 March 2010 Jiri Kosina <jkosina@suse.cz> wrote:
> On Mon, 15 Mar 2010, Bruno Prémont wrote:
> > The ring buffer overflow case (when tail crosses head) seems to be
> > suboptimal at best.
> > Would there be a good way to mark those cases so reader can know where
> > data got lost. (though this might not be easy keeping the lockless
> > design)
> 
> I agree that there might be better implementation. The circular buffer is 
> merely the same to what we use in other userspace interfaces already (see 
> original hiddev, for example). Plus this is only debugging interface, and 
> with SIZE being 512, it's very unlikely that events will get lost.

Well, 512 is not that large, considering that a single event may well
fill half of it (e.g. 64byte HID event * 3 for hex dump, adding decoded
event to it). For basic keyboard events it's definitely sufficient.

In my case I was seeing overflow with outbound reports I dumped there
too, where it's pretty common to have more than a single report sent
back to device in a row.
Sure it's just an aid for debugging so loosing data is not an issue,
just having a hard time to understand output when things got lost would
be nice to avoid.

> But I don't think the lockless design is that important here, so if you 
> are motivated to rewrite it to something better, I wouldn't object.

The easy way to keep output consistent would be to stop appending new
data when the ring buffer is full. (and maybe adding a single '\0' or
equivalent just before head if more data has to be written than space is
available so there is an overflow marker).
This way reader would not risk reading old data a second time.

Dropping old data looks more complicated to get race-free or it would
have to be a bigger change (with new locking).


The optimal case would be to have a single ring-buffer implementation
for use by anyone needing it (I don't know if such one exists nor
how many ring-buffers are being used around the kernel)...
This is for a later time when I'm happy with my PicoLCD driver!

Bruno
--
To unsubscribe from this list: send the line "unsubscribe linux-input" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2010-03-16 17:36 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-03-15 18:00 [PATCH] hid: avoid '\0' in hid debugfs events file Bruno Prémont
2010-03-16 12:58 ` Jiri Kosina
2010-03-16 17:35   ` Bruno Prémont

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).