Skip to contentSkip to conversation

virtio-gpu: fixes and spice preparation

PATCH New Gerd Hoffmann 1 participant 7 messages Jan 22, 2016

lore
Share & tools
lore
#0 lore raw

v2 cover virtio-gpu: fixes and spice preparation

Hi,

This patch series brings some minor opengl bug fixes and also some virtio-gpu preparations for spice support.

v2: address review comments from Marc-André Lureau (mostly patch #4)

Gerd Hoffmann (6):
zap qemu_egl_has_ext in include/ui/egl-helpers.h
console: block rendering until client is done
virtio-gpu: fix memory leak in error path
virtio-gpu: maintain command queue
virtio-gpu: add support to enable/disable command processing
virtio-gpu: block any rendering until client (ui) is done

 hw/display/virtio-gpu-3d.c     | 11 ++++--
 hw/display/virtio-gpu.c        | 77 ++++++++++++++++++++++++++++++------------
 hw/display/virtio-vga.c        | 10 ++++++
 include/hw/virtio/virtio-gpu.h |  4 +++
 include/ui/console.h           |  2 ++
 include/ui/egl-helpers.h       |  1 -
 ui/console.c                   | 10 ++++++
 7 files changed, 90 insertions(+), 25 deletions(-)
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH v2 1/6 zap qemu_egl_has_ext in include/ui/egl-helpers.h

Drop leftover prototype which sneaked in by mistake

---
 include/ui/egl-helpers.h | 1 -
 1 file changed, 1 deletion(-)
include/ui/egl-helpers.h +0 -1
index 5ad5dc3..8c84398 100644
@@ -11,6 +11,5 @@ EGLSurface qemu_egl_init_surface_x11(EGLContext ectx, Window win);
 
 int qemu_egl_init_dpy(EGLNativeDisplayType dpy, bool gles, bool debug);
 EGLContext qemu_egl_init_ctx(void);
-bool qemu_egl_has_ext(const char *haystack, const char *needle);
 
 #endif /* EGL_HELPERS_H */
Signature · 1 line
1.8.3.1
#2 lore raw

PATCH v2 2/6 console: block rendering until client is done

In reply to Gerd Hoffmann

Allow gl user interfaces to block display device gl rendering. The ui code might want to do that in case it takes a little longer to bring things to screen, for example because we'll hand over a dma-buf to another process (spice will do that).

---
 include/ui/console.h |  2 ++
 ui/console.c         | 10 ++++++++++
 2 files changed, 12 insertions(+)
include/ui/console.h +2 -0
index adac36d..12ad627 100644
@@ -362,6 +362,7 @@ typedef struct GraphicHwOps {
     void (*text_update)(void *opaque, console_ch_t *text);
     void (*update_interval)(void *opaque, uint64_t interval);
     int (*ui_info)(void *opaque, uint32_t head, QemuUIInfo *info);
+    void (*gl_block)(void *opaque, bool block);
 } GraphicHwOps;
 
 QemuConsole *graphic_console_init(DeviceState *dev, uint32_t head,
@@ -374,6 +375,7 @@ void graphic_console_set_hwops(QemuConsole *con,
 void graphic_hw_update(QemuConsole *con);
 void graphic_hw_invalidate(QemuConsole *con);
 void graphic_hw_text_update(QemuConsole *con, console_ch_t *chardata);
+void graphic_hw_gl_block(QemuConsole *con, bool block);
 
 QemuConsole *qemu_console_lookup_by_index(unsigned int index);
 QemuConsole *qemu_console_lookup_by_device(DeviceState *dev, uint32_t head);
ui/console.c +10 -0
index fe950c6..791b4fc 100644
@@ -261,6 +261,16 @@ void graphic_hw_update(QemuConsole *con)
     }
 }
 
+void graphic_hw_gl_block(QemuConsole *con, bool block)
+{
+    if (!con) {
+        con = active_console;
+    }
+    if (con && con->hw_ops->gl_block) {
+        con->hw_ops->gl_block(con->hw, block);
+    }
+}
+
 void graphic_hw_invalidate(QemuConsole *con)
 {
     if (!con) {
Signature · 1 line
1.8.3.1
#3 lore raw

PATCH v2 3/6 virtio-gpu: fix memory leak in error path

In reply to Gerd Hoffmann

Found by Coverity Scan, buf not freed on error.

---
 hw/display/virtio-gpu-3d.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
hw/display/virtio-gpu-3d.c +2 -1
index 28dccfd..d2af327 100644
@@ -197,7 +197,7 @@ static void virgl_cmd_submit_3d(VirtIOGPU *g,
         qemu_log_mask(LOG_GUEST_ERROR, "%s: size mismatch (%zd/%d)",
                       __func__, s, cs.size);
         cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER;
-        return;
+        goto out;
     }
 
     if (virtio_gpu_stats_enabled(g->conf)) {
@@ -207,6 +207,7 @@ static void virgl_cmd_submit_3d(VirtIOGPU *g,
 
     virgl_renderer_submit_cmd(buf, cs.hdr.ctx_id, cs.size / 4);
 
+out:
     g_free(buf);
 }
 
Signature · 1 line
1.8.3.1
#4 lore raw

PATCH v2 4/6 virtio-gpu: maintain command queue

In reply to Gerd Hoffmann

We'll go take out the commands we receive out of the virt queue and put them into a linked list, to decouple virtio queue handling from actual command processing.

Also move cmd processing to new virtio_gpu_handle_ctrl func, so we can easily kick it from different places.

---
 hw/display/virtio-gpu.c        | 63 +++++++++++++++++++++++++++---------------
 include/hw/virtio/virtio-gpu.h |  1 +
 2 files changed, 42 insertions(+), 22 deletions(-)
hw/display/virtio-gpu.c +41 -22
index 7e79a9c..d4ee34d 100644
@@ -754,33 +754,21 @@ static void virtio_gpu_handle_cursor_cb(VirtIODevice *vdev, VirtQueue *vq)
     qemu_bh_schedule(g->cursor_bh);
 }
 
-static void virtio_gpu_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
+static void virtio_gpu_process_cmdq(VirtIOGPU *g)
 {
-    VirtIOGPU *g = VIRTIO_GPU(vdev);
     struct virtio_gpu_ctrl_command *cmd;
 
-    if (!virtio_queue_ready(vq)) {
-        return;
-    }
-
-#ifdef CONFIG_VIRGL
-    if (!g->renderer_inited && g->use_virgl_renderer) {
-        virtio_gpu_virgl_init(g);
-        g->renderer_inited = true;
-    }
-#endif
-
-    cmd = g_new(struct virtio_gpu_ctrl_command, 1);
-    while (virtqueue_pop(vq, &cmd->elem)) {
-        cmd->vq = vq;
-        cmd->error = 0;
-        cmd->finished = false;
-        if (virtio_gpu_stats_enabled(g->conf)) {
-            g->stats.requests++;
-        }
+    while (!QTAILQ_EMPTY(&g->cmdq)) {
+        cmd = QTAILQ_FIRST(&g->cmdq);
 
+        /* process command */
         VIRGL(g, virtio_gpu_virgl_process_cmd, virtio_gpu_simple_process_cmd,
               g, cmd);
+        QTAILQ_REMOVE(&g->cmdq, cmd, next);
+        if (virtio_gpu_stats_enabled(g->conf)) {
+            g->stats.requests++;
+        }
+
         if (!cmd->finished) {
             QTAILQ_INSERT_TAIL(&g->fenceq, cmd, next);
             g->inflight++;
@@ -790,11 +778,41 @@ static void virtio_gpu_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
                 }
                 fprintf(stderr, "inflight: %3d (+)\r", g->inflight);
             }
-            cmd = g_new(struct virtio_gpu_ctrl_command, 1);
+        } else {
+            g_free(cmd);
         }
     }
+}
+
+static void virtio_gpu_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
+{
+    VirtIOGPU *g = VIRTIO_GPU(vdev);
+    struct virtio_gpu_ctrl_command *cmd;
+
+    if (!virtio_queue_ready(vq)) {
+        return;
+    }
+
+#ifdef CONFIG_VIRGL
+    if (!g->renderer_inited && g->use_virgl_renderer) {
+        virtio_gpu_virgl_init(g);
+        g->renderer_inited = true;
+    }
+#endif
+
+    cmd = g_new(struct virtio_gpu_ctrl_command, 1);
+    while (virtqueue_pop(vq, &cmd->elem)) {
+        cmd->vq = vq;
+        cmd->error = 0;
+        cmd->finished = false;
+        cmd->waiting = false;
+        QTAILQ_INSERT_TAIL(&g->cmdq, cmd, next);
+        cmd = g_new(struct virtio_gpu_ctrl_command, 1);
+    }
     g_free(cmd);
 
+    virtio_gpu_process_cmdq(g);
+
 #ifdef CONFIG_VIRGL
     if (g->use_virgl_renderer) {
         virtio_gpu_virgl_fence_poll(g);
@@ -920,6 +938,7 @@ static void virtio_gpu_device_realize(DeviceState *qdev, Error **errp)
     g->ctrl_bh = qemu_bh_new(virtio_gpu_ctrl_bh, g);
     g->cursor_bh = qemu_bh_new(virtio_gpu_cursor_bh, g);
     QTAILQ_INIT(&g->reslist);
+    QTAILQ_INIT(&g->cmdq);
     QTAILQ_INIT(&g->fenceq);
 
     g->enabled_output_bitmask = 1;
include/hw/virtio/virtio-gpu.h +1 -0
index 9b279d7..f7e7a52 100644
@@ -94,6 +94,7 @@ typedef struct VirtIOGPU {
     DeviceState *qdev;
 
     QTAILQ_HEAD(, virtio_gpu_simple_resource) reslist;
+    QTAILQ_HEAD(, virtio_gpu_ctrl_command) cmdq;
     QTAILQ_HEAD(, virtio_gpu_ctrl_command) fenceq;
 
     struct virtio_gpu_scanout scanout[VIRTIO_GPU_MAX_SCANOUT];
Signature · 1 line
1.8.3.1
#5 lore raw

PATCH v2 5/6 virtio-gpu: add support to enable/disable command processing

In reply to Gerd Hoffmann

So we can stop rendering for a while in case we have to.

---
 hw/display/virtio-gpu-3d.c     | 3 ++-
 hw/display/virtio-gpu.c        | 5 ++++-
 include/hw/virtio/virtio-gpu.h | 2 ++
 3 files changed, 8 insertions(+), 2 deletions(-)
hw/display/virtio-gpu-3d.c +2 -1
index d2af327..d6487f7 100644
@@ -553,7 +553,8 @@ static void virtio_gpu_fence_poll(void *opaque)
     VirtIOGPU *g = opaque;
 
     virgl_renderer_poll();
-    if (g->inflight) {
+    virtio_gpu_process_cmdq(g);
+    if (!QTAILQ_EMPTY(&g->cmdq) || !QTAILQ_EMPTY(&g->fenceq)) {
         timer_mod(g->fence_poll, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 10);
     }
 }
hw/display/virtio-gpu.c +4 -1
index d4ee34d..e341d6e 100644
@@ -754,7 +754,7 @@ static void virtio_gpu_handle_cursor_cb(VirtIODevice *vdev, VirtQueue *vq)
     qemu_bh_schedule(g->cursor_bh);
 }
 
-static void virtio_gpu_process_cmdq(VirtIOGPU *g)
+void virtio_gpu_process_cmdq(VirtIOGPU *g)
 {
     struct virtio_gpu_ctrl_command *cmd;
 
@@ -764,6 +764,9 @@ static void virtio_gpu_process_cmdq(VirtIOGPU *g)
         /* process command */
         VIRGL(g, virtio_gpu_virgl_process_cmd, virtio_gpu_simple_process_cmd,
               g, cmd);
+        if (cmd->waiting) {
+            break;
+        }
         QTAILQ_REMOVE(&g->cmdq, cmd, next);
         if (virtio_gpu_stats_enabled(g->conf)) {
             g->stats.requests++;
include/hw/virtio/virtio-gpu.h +2 -0
index f7e7a52..f6cae0b 100644
@@ -76,6 +76,7 @@ struct virtio_gpu_ctrl_command {
     VirtQueue *vq;
     struct virtio_gpu_ctrl_hdr cmd_hdr;
     uint32_t error;
+    bool waiting;
     bool finished;
     QTAILQ_ENTRY(virtio_gpu_ctrl_command) next;
 };
@@ -152,6 +153,7 @@ int virtio_gpu_create_mapping_iov(struct virtio_gpu_resource_attach_backing *ab,
                                   struct virtio_gpu_ctrl_command *cmd,
                                   struct iovec **iov);
 void virtio_gpu_cleanup_mapping_iov(struct iovec *iov, uint32_t count);
+void virtio_gpu_process_cmdq(VirtIOGPU *g);
 
 /* virtio-gpu-3d.c */
 void virtio_gpu_virgl_process_cmd(VirtIOGPU *g,
Signature · 1 line
1.8.3.1
#6 lore raw

PATCH v2 6/6 virtio-gpu: block any rendering until client (ui) is done

In reply to Gerd Hoffmann

Wire up gl_block callback, so ui code can request to stop virtio-gpu rendering.

---
 hw/display/virtio-gpu-3d.c     |  5 +++++
 hw/display/virtio-gpu.c        | 11 +++++++++++
 hw/display/virtio-vga.c        | 10 ++++++++++
 include/hw/virtio/virtio-gpu.h |  1 +
 4 files changed, 27 insertions(+)
hw/display/virtio-gpu-3d.c +5 -0
index d6487f7..8906316 100644
@@ -382,6 +382,11 @@ void virtio_gpu_virgl_process_cmd(VirtIOGPU *g,
 {
     VIRTIO_GPU_FILL_CMD(cmd->cmd_hdr);
 
+    cmd->waiting = g->renderer_blocked;
+    if (cmd->waiting) {
+        return;
+    }
+
     virgl_renderer_force_ctx_0();
     switch (cmd->cmd_hdr.type) {
     case VIRTIO_GPU_CMD_CTX_CREATE:
hw/display/virtio-gpu.c +11 -0
index e341d6e..ce02194 100644
@@ -896,11 +896,22 @@ static int virtio_gpu_ui_info(void *opaque, uint32_t idx, QemuUIInfo *info)
     return 0;
 }
 
+static void virtio_gpu_gl_block(void *opaque, bool block)
+{
+    VirtIOGPU *g = opaque;
+
+    g->renderer_blocked = block;
+    if (!block) {
+        virtio_gpu_process_cmdq(g);
+    }
+}
+
 const GraphicHwOps virtio_gpu_ops = {
     .invalidate = virtio_gpu_invalidate_display,
     .gfx_update = virtio_gpu_update_display,
     .text_update = virtio_gpu_text_update,
     .ui_info = virtio_gpu_ui_info,
+    .gl_block = virtio_gpu_gl_block,
 };
 
 static void virtio_gpu_device_realize(DeviceState *qdev, Error **errp)
hw/display/virtio-vga.c +10 -0
index f7e539f..255d419 100644
@@ -65,11 +65,21 @@ static int virtio_vga_ui_info(void *opaque, uint32_t idx, QemuUIInfo *info)
     return -1;
 }
 
+static void virtio_vga_gl_block(void *opaque, bool block)
+{
+    VirtIOVGA *vvga = opaque;
+
+    if (virtio_gpu_ops.gl_block) {
+        virtio_gpu_ops.gl_block(&vvga->vdev, block);
+    }
+}
+
 static const GraphicHwOps virtio_vga_ops = {
     .invalidate = virtio_vga_invalidate_display,
     .gfx_update = virtio_vga_update_display,
     .text_update = virtio_vga_text_update,
     .ui_info = virtio_vga_ui_info,
+    .gl_block = virtio_vga_gl_block,
 };
 
 /* VGA device wrapper around PCI device around virtio GPU */
include/hw/virtio/virtio-gpu.h +1 -0
index f6cae0b..13b0ab0 100644
@@ -107,6 +107,7 @@ typedef struct VirtIOGPU {
 
     bool use_virgl_renderer;
     bool renderer_inited;
+    bool renderer_blocked;
     QEMUTimer *fence_poll;
     QEMUTimer *print_stats;
 
Signature · 1 line
1.8.3.1