All of lore.kernel.org
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH 0/2] virtio-input: eventq batching fixes
@ 2017-03-24 14:24 Ladi Prosek
  2017-03-24 14:24 ` [Qemu-devel] [PATCH 1/2] virtio-input: free event queue when finalizing Ladi Prosek
  2017-03-24 14:24 ` [Qemu-devel] [PATCH 2/2] virtio-input: fix eventq batching Ladi Prosek
  0 siblings, 2 replies; 3+ messages in thread
From: Ladi Prosek @ 2017-03-24 14:24 UTC (permalink / raw)
  To: qemu-devel; +Cc: kraxel

Two bugs found when reviewing the code against the candidate spec:

* memory leak: the event queue is never freed
* incorrect virtqueue check: virtqueue_get_avail_bytes does not
  guarantee number of available buffers

Ladi Prosek (2):
  virtio-input: free event queue when finalizing
  virtio-input: fix eventq batching

 hw/input/virtio-input.c          | 33 +++++++++++++++++----------------
 include/hw/virtio/virtio-input.h |  5 ++++-
 2 files changed, 21 insertions(+), 17 deletions(-)

Thanks!
Ladi

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

* [Qemu-devel] [PATCH 1/2] virtio-input: free event queue when finalizing
  2017-03-24 14:24 [Qemu-devel] [PATCH 0/2] virtio-input: eventq batching fixes Ladi Prosek
@ 2017-03-24 14:24 ` Ladi Prosek
  2017-03-24 14:24 ` [Qemu-devel] [PATCH 2/2] virtio-input: fix eventq batching Ladi Prosek
  1 sibling, 0 replies; 3+ messages in thread
From: Ladi Prosek @ 2017-03-24 14:24 UTC (permalink / raw)
  To: qemu-devel; +Cc: kraxel

VirtIOInput.queue was never freed. This commit adds an explicit
g_free to virtio_input_finalize and switches the allocation
function from realloc to g_realloc in virtio_input_send.

Signed-off-by: Ladi Prosek <lprosek@redhat.com>
---
 hw/input/virtio-input.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/hw/input/virtio-input.c b/hw/input/virtio-input.c
index b678ee9..728832a 100644
--- a/hw/input/virtio-input.c
+++ b/hw/input/virtio-input.c
@@ -32,8 +32,8 @@ void virtio_input_send(VirtIOInput *vinput, virtio_input_event *event)
     /* queue up events ... */
     if (vinput->qindex == vinput->qsize) {
         vinput->qsize++;
-        vinput->queue = realloc(vinput->queue, vinput->qsize *
-                                sizeof(virtio_input_event));
+        vinput->queue = g_realloc(vinput->queue, vinput->qsize *
+                                  sizeof(virtio_input_event));
     }
     vinput->queue[vinput->qindex++] = *event;
 
@@ -272,6 +272,8 @@ static void virtio_input_finalize(Object *obj)
         QTAILQ_REMOVE(&vinput->cfg_list, cfg, node);
         g_free(cfg);
     }
+
+    g_free(vinput->queue);
 }
 static void virtio_input_device_unrealize(DeviceState *dev, Error **errp)
 {
-- 
2.7.4

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

* [Qemu-devel] [PATCH 2/2] virtio-input: fix eventq batching
  2017-03-24 14:24 [Qemu-devel] [PATCH 0/2] virtio-input: eventq batching fixes Ladi Prosek
  2017-03-24 14:24 ` [Qemu-devel] [PATCH 1/2] virtio-input: free event queue when finalizing Ladi Prosek
@ 2017-03-24 14:24 ` Ladi Prosek
  1 sibling, 0 replies; 3+ messages in thread
From: Ladi Prosek @ 2017-03-24 14:24 UTC (permalink / raw)
  To: qemu-devel; +Cc: kraxel

virtio_input_send buffers input events until it sees a SYNC. Then it
either sends or drops the entire batch, depending on whether eventq
has enough space available. The case to avoid here is partial sends
where only part of the batch would get to the guest.

Using virtqueue_get_avail_bytes to check the state of eventq was not
correct. The queue may have a smaller number of larger buffers
available so bytes may be enough but the batch would still not be
possible to send, leading to the "Huh?  No vq elem available" error.

Instead of checking available bytes, this patch optimistically pops
buffers from the queue and puts them back in case it runs out of
space and the batch needs to be dropped.

Signed-off-by: Ladi Prosek <lprosek@redhat.com>
---
 hw/input/virtio-input.c          | 29 ++++++++++++++---------------
 include/hw/virtio/virtio-input.h |  5 ++++-
 2 files changed, 18 insertions(+), 16 deletions(-)

diff --git a/hw/input/virtio-input.c b/hw/input/virtio-input.c
index 728832a..0e42f0d 100644
--- a/hw/input/virtio-input.c
+++ b/hw/input/virtio-input.c
@@ -22,7 +22,6 @@
 void virtio_input_send(VirtIOInput *vinput, virtio_input_event *event)
 {
     VirtQueueElement *elem;
-    unsigned have, need;
     int i, len;
 
     if (!vinput->active) {
@@ -33,9 +32,9 @@ void virtio_input_send(VirtIOInput *vinput, virtio_input_event *event)
     if (vinput->qindex == vinput->qsize) {
         vinput->qsize++;
         vinput->queue = g_realloc(vinput->queue, vinput->qsize *
-                                  sizeof(virtio_input_event));
+                                  sizeof(vinput->queue[0]));
     }
-    vinput->queue[vinput->qindex++] = *event;
+    vinput->queue[vinput->qindex++].event = *event;
 
     /* ... until we see a report sync ... */
     if (event->type != cpu_to_le16(EV_SYN) ||
@@ -44,24 +43,24 @@ void virtio_input_send(VirtIOInput *vinput, virtio_input_event *event)
     }
 
     /* ... then check available space ... */
-    need = sizeof(virtio_input_event) * vinput->qindex;
-    virtqueue_get_avail_bytes(vinput->evt, &have, NULL, need, 0);
-    if (have < need) {
-        vinput->qindex = 0;
-        trace_virtio_input_queue_full();
-        return;
-    }
-
-    /* ... and finally pass them to the guest */
     for (i = 0; i < vinput->qindex; i++) {
         elem = virtqueue_pop(vinput->evt, sizeof(VirtQueueElement));
         if (!elem) {
-            /* should not happen, we've checked for space beforehand */
-            fprintf(stderr, "%s: Huh?  No vq elem available ...\n", __func__);
+            while (--i >= 0) {
+                virtqueue_unpop(vinput->evt, vinput->queue[i].elem, 0);
+            }
+            vinput->qindex = 0;
+            trace_virtio_input_queue_full();
             return;
         }
+        vinput->queue[i].elem = elem;
+    }
+
+    /* ... and finally pass them to the guest */
+    for (i = 0; i < vinput->qindex; i++) {
+        elem = vinput->queue[i].elem;
         len = iov_from_buf(elem->in_sg, elem->in_num,
-                           0, vinput->queue+i, sizeof(virtio_input_event));
+                           0, &vinput->queue[i].event, sizeof(virtio_input_event));
         virtqueue_push(vinput->evt, elem, len);
         g_free(elem);
     }
diff --git a/include/hw/virtio/virtio-input.h b/include/hw/virtio/virtio-input.h
index 55db310..91df57e 100644
--- a/include/hw/virtio/virtio-input.h
+++ b/include/hw/virtio/virtio-input.h
@@ -62,7 +62,10 @@ struct VirtIOInput {
     VirtQueue                         *evt, *sts;
     char                              *serial;
 
-    virtio_input_event                *queue;
+    struct {
+        virtio_input_event event;
+        VirtQueueElement *elem;
+    }                                 *queue;
     uint32_t                          qindex, qsize;
 
     bool                              active;
-- 
2.7.4

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

end of thread, other threads:[~2017-03-24 14:25 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-03-24 14:24 [Qemu-devel] [PATCH 0/2] virtio-input: eventq batching fixes Ladi Prosek
2017-03-24 14:24 ` [Qemu-devel] [PATCH 1/2] virtio-input: free event queue when finalizing Ladi Prosek
2017-03-24 14:24 ` [Qemu-devel] [PATCH 2/2] virtio-input: fix eventq batching Ladi Prosek

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.