Skip to contentSkip to conversation

Various virtio-gpu/dmabuf related fixes

PATCH Superseded Marc-André Lureau 2 participants 52 messages Sep 4 – Sep 9

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover Various virtio-gpu/dmabuf related fixes

Hi,

While working on CVE-2026-66020 (which had a few revision on list), I kept finding several places where virtio-gpu scanout and dmabuf ownership was unclear.

The CVE comes from using a blob after RESOURCE_DETACH_BACKING has unmapped its memory. Following that path also found stale dmabuf references, duplicated scanout bookkeeping, and inconsistent cleanup between display backends.

Those changes touch a lot of area and I have done basic testing with the various backends. I would not recommend backporting them all to stable. The first 2 patches address CVE-2026-66020 in the general case and should be safe to backport.

thanks

---
Changes in v2: addresses Akihiko review
- reorder to have CVE fix first, easily back-portable (but also more
limited fix)
- include virtio_gpu_{simple,virgl}_resource_new() patch, and other sent
separately
- move draw_submitted from QemuDmaBuf to VirtualGfxConsole
- contrib/vhost-user-gpu: disable scanouts on resource unref
- drop rfc, collect rb, adjust commit messages
- Link to v1: https://lore.kernel.org/qemu-devel/[email protected]

---
Marc-André Lureau (25):
virtio-gpu: disable blob scanouts on mapping cleanup
virtio-gpu: fix use-after-free on blob cursor after detach hw/display/virtio-gpu: introduce virtio_gpu_{simple,virgl}_resource_new() virtio-gpu/virgl: free BHs and timers on renderer reset hw/display/virtio-gpu: set share_handle for udmabuf blob resources ui/gtk: move GL fence tracking from QemuDmaBuf to VirtualGfxConsole ui/gtk: move draw_submitted from QemuDmaBuf to VirtualGfxConsole ui/dbus: disable scanout iff dmabuf is current
virtio-gpu: store the scanout DMABUF in virtio_gpu_scanout ui/console: disable GL scanout when dmabuf is the active one ui/sdl2: clear guest_fb.dmabuf on release
ui/dmabuf: own and close fds on free
ui: make GL context current before releasing DMA-BUF textures
virtio-gpu: release the dmabuf when a scanout is disabled contrib/vhost-user-gpu: drop scanout_bitmask bookkeeping
virtio-gpu-rutabaga: fix scanout handling
virtio-gpu: drop scanout_bitmask bookkeeping
virtio-gpu: release dmabuf when switching to a surface virtio-gpu/virgl: release dmabuf when a texture replaces a scanout
virtio-gpu: consolidate disabling scanout
virtio-gpu: minor refactoring
virtio-gpu/virgl: disable scanouts on resource unref
virtio-gpu: extract virtio_gpu_disable_scanout_for_resource helper
vhost-user-gpu: release DMA-BUFs on scanout disable, reset and finalize contrib/vhost-user-gpu: disable scanouts on resource unref

 contrib/vhost-user-gpu/vhost-user-gpu.c |  28 ++----
 contrib/vhost-user-gpu/virgl.c          |  12 +++
 hw/display/vhost-user-gpu.c             |  19 ++++-
 hw/display/virtio-gpu-rutabaga.c        |  38 ++++-----
 hw/display/virtio-gpu-udmabuf-stubs.c   |   2 +-
 hw/display/virtio-gpu-udmabuf.c         |  89 +++++++------------
 hw/display/virtio-gpu-virgl.c           | 131 +++++++++++++++++-----------
 hw/display/virtio-gpu.c                 | 147 +++++++++++++++++++++-----------
 hw/vfio/display.c                       |   1 -
 include/hw/virtio/virtio-gpu.h          |  31 ++++---
 include/ui/dmabuf.h                     |   7 --
 include/ui/egl-helpers.h                |   4 +-
 include/ui/gtk.h                        |  14 ++-
 ui/console.c                            |   3 +
 ui/dbus-listener.c                      |  69 ++++++++++-----
 ui/dmabuf.c                             |  64 ++------------
 ui/egl-headless.c                       |   2 +
 ui/egl-helpers.c                        |  15 ++--
 ui/gtk-egl.c                            |  55 +++++++-----
 ui/gtk-gl-area.c                        |  33 ++++---
 ui/gtk.c                                |  43 ++++++----
 ui/sdl2-gl.c                            |   6 ++
 ui/spice-display.c                      |   2 +
 23 files changed, 449 insertions(+), 366 deletions(-)

---
base-commit: 99e54ab5e7a6efc945af6d5661842155d1f3fc7a
change-id: 20260825-virtio-gpu-cleanup-dfb2ba185936

Best regards,

Signature · 1 line
Marc-André Lureau <[email protected]>
#1 lore raw

PATCH v2 1/25 virtio-gpu: disable blob scanouts on mapping cleanup

When a blob resource backing is cleaned up (for ex via detach_backing), any scanouts referencing it must be disabled first to prevent the dmabuf from outliving its backing memory.

Fixes CVE-2026-66020
Fixes 32db3c63ae11 ("virtio-gpu: Add virtio_gpu_set_scanout_blob")
---
 hw/display/virtio-gpu.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
hw/display/virtio-gpu.c +10 -0
index 55a1c7f80fb8..5b63a292933a 100644
@@ -1022,6 +1022,16 @@ void virtio_gpu_cleanup_mapping_iov(VirtIOGPU *g,
 void virtio_gpu_cleanup_mapping(VirtIOGPU *g,
                                 struct virtio_gpu_simple_resource *res)
 {
+    if (res->blob) {
+        int i, max_outputs = g->parent_obj.conf.max_outputs;
+
+        for (i = 0; i < max_outputs; i++) {
+            if (g->parent_obj.scanout[i].resource_id == res->resource_id) {
+                virtio_gpu_disable_scanout(g, i);
+            }
+        }
+    }
+
     virtio_gpu_cleanup_mapping_iov(g, res->iov, res->iov_cnt);
     res->iov = NULL;
     res->iov_cnt = 0;
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
Reviewed-by on v2 1/25
#2 lore raw

Re: [PATCH v2 01/25] virtio-gpu: disable blob scanouts on mapping cleanup

Quoted 8 lines · Marc-André Lureau

When a blob resource backing is cleaned up (for ex via detach_backing), any scanouts referencing it must be disabled first to prevent the dmabuf from outliving its backing memory.

Fixes CVE-2026-66020
Fixes 32db3c63ae11 ("virtio-gpu: Add virtio_gpu_set_scanout_blob")

Regards,
Akihiko Odaki

#3 lore raw

PATCH v2 2/25 virtio-gpu: fix use-after-free on blob cursor after detach

In reply to Marc-André Lureau

virtio_gpu_cleanup_mapping() unmaps the guest IOVs and tears down any udmabuf mapping, but leaves res->blob pointing at the freed memory. Once a blob resource goes through RESOURCE_DETACH_BACKING, res->blob is left dangling while res->blob_size is still non-zero.

virtio_gpu_update_cursor_data() only checks res->blob_size before copying from res->blob, so an UPDATE_CURSOR on a detached blob resource makes QEMU memcpy from freed memory.

Clear res->blob after tearing down its backing, keeping init/fini symmetric, and check it before dereferencing in virtio_gpu_update_cursor_data().

Fixes CVE-2026-66020
Fixes bdd53f739273 ("virtio-gpu: Update cursor data using blob")

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3881

---
 hw/display/virtio-gpu-udmabuf.c | 1 +
 hw/display/virtio-gpu.c         | 8 ++++++++
 2 files changed, 9 insertions(+)
hw/display/virtio-gpu-udmabuf.c +1 -0
index c230509852ff..03f0b29754e1 100644
@@ -92,6 +92,7 @@ static void virtio_gpu_destroy_udmabuf(struct virtio_gpu_simple_resource *res)
         close(res->dmabuf_fd);
         res->dmabuf_fd = -1;
     }
+    res->blob = NULL;
 }
 
 static int find_memory_backend_type(Object *obj, void *opaque)
hw/display/virtio-gpu.c +8 -0
index 5b63a292933a..aeb6155130d0 100644
@@ -61,8 +61,16 @@ void virtio_gpu_update_cursor_data(VirtIOGPU *g,
         }
         data = pixman_image_get_data(res->image);
     } else {
+        if (!res->blob) {
+            qemu_log_mask(LOG_GUEST_ERROR, "%s: resource %d has no blob\n",
+                          __func__, resource_id);
+            return;
+        }
         if (!res->iov || res->blob_size < (s->current_cursor->width *
                                            s->current_cursor->height * 4)) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: blob size too small for resource %d\n",
+                          __func__, resource_id);
             return;
         }
         data = res->blob;
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
#4 lore raw

Re: [PATCH v2 02/25] virtio-gpu: fix use-after-free on blob cursor after detach

Quoted 8 lines · Marc-André Lureau

virtio_gpu_cleanup_mapping() unmaps the guest IOVs and tears down any udmabuf mapping, but leaves res->blob pointing at the freed memory. Once a blob resource goes through RESOURCE_DETACH_BACKING, res->blob is left dangling while res->blob_size is still non-zero.

virtio_gpu_update_cursor_data() only checks res->blob_size before copying from res->blob, so an UPDATE_CURSOR on a detached blob resource makes QEMU memcpy from freed memory.

Well, I forgot the presence of this patch and fixed the described issue with d3c2da174ca6 ("hw/display/virtio-gpu: Check cursor data presence"), so the issue is no longer present. Clearing res->blob and adding proper error messages are still nice additions.

Quoted 33 lines

Clear res->blob after tearing down its backing, keeping init/fini symmetric, and check it before dereferencing in virtio_gpu_update_cursor_data().

Fixes CVE-2026-66020
Fixes bdd53f739273 ("virtio-gpu: Update cursor data using blob")

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3881

---
  hw/display/virtio-gpu-udmabuf.c | 1 +
  hw/display/virtio-gpu.c         | 8 ++++++++
  2 files changed, 9 insertions(+)
hw/display/virtio-gpu-udmabuf.c +1 -0
index c230509852ff..03f0b29754e1 100644
@@ -92,6 +92,7 @@ static void virtio_gpu_destroy_udmabuf(struct virtio_gpu_simple_resource *res)
          close(res->dmabuf_fd);
          res->dmabuf_fd = -1;
      }
+    res->blob = NULL;
  }
 
  static int find_memory_backend_type(Object *obj, void *opaque)
hw/display/virtio-gpu.c +0 -0
index 5b63a292933a..aeb6155130d0 100644

hw/display/virtio-gpu.c void virtio_gpu_update_cursor_data(VirtIOGPU *g, · line 70

Quoted 10 lines
hw/display/virtio-gpu.c +5 -0
@@ -61,8 +61,16 @@ void virtio_gpu_update_cursor_data(VirtIOGPU *g,
          }
          data = pixman_image_get_data(res->image);
      } else {
+        if (!res->blob) {
+            qemu_log_mask(LOG_GUEST_ERROR, "%s: resource %d has no blob\n",
+                          __func__, resource_id);
+            return;
+        }
          if (!res->iov || res->blob_size < (s->current_cursor->width *
                                             s->current_cursor->height * 4)) {

This res->iov check I added will be unnecessary and misleading with the error message.

Regards,
Akihiko Odaki

Quoted 7 lines
hw/display/virtio-gpu.c +3 -0
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: blob size too small for resource %d\n",
+                          __func__, resource_id);
              return;
          }
          data = res->blob;
#5 lore raw

PATCH v2 3/25 hw/display/virtio-gpu: introduce virtio_gpu_{simple,virgl}_resource_new()

In reply to Marc-André Lureau

All allocation sites used g_new0() which zero-initializes fields. However share_handle must be SHAREABLE_NONE (-1 on Unix, NULL on Windows) and dmabuf_fd must be -1, not 0. Centralize allocation and field initialization in new constructors fix this and reduce code duplication.

This fixes -display dbus with virtio-gpu blob resources. The other end is currently receiving qemu fd 0.

Fixes 5f899c34af1d (“virtio-gpu: allocate shareable 2d resources on !win32”)
---
 hw/display/virtio-gpu-rutabaga.c | 21 +++----------
 hw/display/virtio-gpu-virgl.c    | 48 +++++++++++++++++++----------
 hw/display/virtio-gpu.c          | 66 +++++++++++++++++++++++++++++-----------
 include/hw/virtio/virtio-gpu.h   | 11 +++++++
 4 files changed, 97 insertions(+), 49 deletions(-)
hw/display/virtio-gpu-rutabaga.c +5 -16
index 041216a10d04..b8f8a2068d0b 100644
@@ -100,12 +100,8 @@ rutabaga_cmd_create_resource_2d(VirtIOGPU *g,
     result = rutabaga_resource_create_3d(vr->rutabaga, c2d.resource_id, &rc_3d);
     CHECK(!result, cmd);
 
-    res = g_new0(struct virtio_gpu_simple_resource, 1);
-    res->width = c2d.width;
-    res->height = c2d.height;
-    res->format = c2d.format;
-    res->resource_id = c2d.resource_id;
-
+    res = virtio_gpu_simple_resource_new(c2d.resource_id, c2d.width,
+                                         c2d.height, c2d.format);
     QTAILQ_INSERT_HEAD(&g->reslist, res, next);
 }
 
@@ -139,12 +135,8 @@ rutabaga_cmd_create_resource_3d(VirtIOGPU *g,
     result = rutabaga_resource_create_3d(vr->rutabaga, c3d.resource_id, &rc_3d);
     CHECK(!result, cmd);
 
-    res = g_new0(struct virtio_gpu_simple_resource, 1);
-    res->width = c3d.width;
-    res->height = c3d.height;
-    res->format = c3d.format;
-    res->resource_id = c3d.resource_id;
-
+    res = virtio_gpu_simple_resource_new(c3d.resource_id, c3d.width,
+                                         c3d.height, c3d.format);
     QTAILQ_INSERT_HEAD(&g->reslist, res, next);
 }
 
@@ -634,10 +626,7 @@ rutabaga_cmd_resource_create_blob(VirtIOGPU *g,
 
     CHECK(cblob.resource_id != 0, cmd);
 
-    res = g_new0(struct virtio_gpu_simple_resource, 1);
-
-    res->resource_id = cblob.resource_id;
-    res->blob_size = cblob.size;
+    res = virtio_gpu_simple_resource_new_blob(cblob.resource_id, cblob.size);
 
     if (cblob.blob_mem != VIRTIO_GPU_BLOB_MEM_HOST3D) {
         result = virtio_gpu_create_mapping_iov(g, cblob.nr_entries,
hw/display/virtio-gpu-virgl.c +32 -16

72 lines not on this page · raw message

hw/display/virtio-gpu.c +49 -17

106 lines not on this page · raw message

include/hw/virtio/virtio-gpu.h +11 -0
index 69b5ee2e382f..8c0df60b3b7e 100644
@@ -64,6 +64,17 @@ struct virtio_gpu_simple_resource {
     QTAILQ_ENTRY(virtio_gpu_simple_resource) next;
 };
 
+void
+virtio_gpu_simple_resource_init(struct virtio_gpu_simple_resource *res,
+                               uint32_t resource_id);
+
+struct virtio_gpu_simple_resource *
+virtio_gpu_simple_resource_new(uint32_t resource_id, uint32_t width,
+                               uint32_t height, uint32_t format);
+
+struct virtio_gpu_simple_resource *
+virtio_gpu_simple_resource_new_blob(uint32_t resource_id, uint64_t blob_size);
+
 struct virtio_gpu_framebuffer {
     pixman_format_code_t format;
     uint32_t width, height;
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
Reviewed-by on v2 3/25
#6 lore raw

Re: [PATCH v2 03/25] hw/display/virtio-gpu: introduce virtio_gpu_{simple,virgl}_resource_new()

Quoted 11 lines · Marc-André Lureau

All allocation sites used g_new0() which zero-initializes fields. However share_handle must be SHAREABLE_NONE (-1 on Unix, NULL on Windows) and dmabuf_fd must be -1, not 0. Centralize allocation and field initialization in new constructors fix this and reduce code duplication.

This fixes -display dbus with virtio-gpu blob resources. The other end is currently receiving qemu fd 0.

Fixes 5f899c34af1d (“virtio-gpu: allocate shareable 2d resources on !win32”)

Regards,
Akihiko Odaki

#7 lore raw

PATCH v2 4/25 virtio-gpu/virgl: free BHs and timers on renderer reset

In reply to Marc-André Lureau

virtio_gpu_virgl_reset() does not free the bottom halves or timers allocated by the previous virtio_gpu_virgl_init() call. A subsequent virtio_gpu_virgl_init() overwrites the old pointers, leaking the previous allocations.

Fixes 7e688d1bf515 ("virtio-gpu: Handle virtio_gpu_virgl_init() failure")
---
 hw/display/virtio-gpu-virgl.c | 6 ++++++
 1 file changed, 6 insertions(+)
hw/display/virtio-gpu-virgl.c +6 -0
index 742d9dde4a32..7f1d1e0c3276 100644
@@ -1430,6 +1430,7 @@ void virtio_gpu_virgl_reset_scanout(VirtIOGPU *g)
 
 static bool virtio_gpu_virgl_reset(VirtIOGPU *g)
 {
+    VirtIOGPUGL *gl = VIRTIO_GPU_GL(g);
     struct virtio_gpu_simple_resource *res, *tmp;
 
     /*
@@ -1448,6 +1449,11 @@ static bool virtio_gpu_virgl_reset(VirtIOGPU *g)
 
     virtio_gpu_virgl_reset_async_fences(g);
 
+    g_clear_pointer(&gl->cmdq_resume_bh, qemu_bh_delete);
+    g_clear_pointer(&gl->async_fence_bh, qemu_bh_delete);
+    g_clear_pointer(&gl->print_stats, timer_free);
+    g_clear_pointer(&gl->fence_poll, timer_free);
+
     return true;
 }
 
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
#8 lore raw

PATCH v2 5/25 hw/display/virtio-gpu: set share_handle for udmabuf blob resources

In reply to Marc-André Lureau

Set share_handle to the dmabuf fd after successful udmabuf creation, and reset it to SHAREABLE_NONE on teardown.

Fixes 5f899c34af1d ("virtio-gpu: allocate shareable 2d resources on !win32")
---
 hw/display/virtio-gpu-udmabuf.c | 3 +++
 1 file changed, 3 insertions(+)
hw/display/virtio-gpu-udmabuf.c +3 -0
index 03f0b29754e1..fc63f2c4ec21 100644
@@ -91,6 +91,7 @@ static void virtio_gpu_destroy_udmabuf(struct virtio_gpu_simple_resource *res)
     if (res->dmabuf_fd >= 0) {
         close(res->dmabuf_fd);
         res->dmabuf_fd = -1;
+        res->share_handle = SHAREABLE_NONE;
     }
     res->blob = NULL;
 }
@@ -150,6 +151,7 @@ bool virtio_gpu_init_udmabuf(struct virtio_gpu_simple_resource *res)
             virtio_gpu_destroy_udmabuf(res);
             return false;
         }
+        res->share_handle = res->dmabuf_fd;
         pdata = res->remapped;
     }
 
@@ -183,6 +185,7 @@ void virtio_gpu_fini_udmabuf(VirtIOGPU *g, struct virtio_gpu_simple_resource *re
             res->dmabuf_fd != -1) {
             qemu_dmabuf_close(dmabuf->buf);
             res->dmabuf_fd = -1;
+            res->share_handle = SHAREABLE_NONE;
         }
     }
 
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
#9 lore raw

PATCH v2 6/25 ui/gtk: move GL fence tracking from QemuDmaBuf to VirtualGfxConsole

In reply to Marc-André Lureau

The sync and fence_fd fields on QemuDmaBuf are only used by the GTK display backend for GPU fence synchronization during draw. They are not intrinsic properties of the DMA-BUF itself but transient GL state tied to the display listener.

Move fence_fd to VirtualGfxConsole and keep the EGL sync local to the draw operation. Change egl_dmabuf_create_sync() and egl_dmabuf_create_fence() to return values instead of storing them on the dmabuf. This makes the fence lifecycle independent of the dmabuf lifetime.

---
 include/ui/dmabuf.h      |  4 ----
 include/ui/egl-helpers.h |  4 ++--
 include/ui/gtk.h         |  8 +++++---
 ui/dmabuf.c              | 29 -----------------------------
 ui/egl-helpers.c         | 15 ++++++++-------
 ui/gtk-egl.c             | 30 ++++++++++++++++--------------
 ui/gtk-gl-area.c         | 12 +++---------
 ui/gtk.c                 | 31 +++++++++++++++++++++++--------
 8 files changed, 57 insertions(+), 76 deletions(-)
include/ui/dmabuf.h +0 -4
index 3decdca4979c..381583bd536c 100644
@@ -41,13 +41,9 @@ uint32_t qemu_dmabuf_get_y(QemuDmaBuf *dmabuf);
 uint32_t qemu_dmabuf_get_backing_width(QemuDmaBuf *dmabuf);
 uint32_t qemu_dmabuf_get_backing_height(QemuDmaBuf *dmabuf);
 bool qemu_dmabuf_get_y0_top(QemuDmaBuf *dmabuf);
-void *qemu_dmabuf_get_sync(QemuDmaBuf *dmabuf);
-int32_t qemu_dmabuf_get_fence_fd(QemuDmaBuf *dmabuf);
 bool qemu_dmabuf_get_allow_fences(QemuDmaBuf *dmabuf);
 bool qemu_dmabuf_get_draw_submitted(QemuDmaBuf *dmabuf);
 void qemu_dmabuf_set_texture(QemuDmaBuf *dmabuf, uint32_t texture);
-void qemu_dmabuf_set_fence_fd(QemuDmaBuf *dmabuf, int32_t fence_fd);
-void qemu_dmabuf_set_sync(QemuDmaBuf *dmabuf, void *sync);
 void qemu_dmabuf_set_draw_submitted(QemuDmaBuf *dmabuf, bool draw_submitted);
 
 #endif
include/ui/egl-helpers.h +2 -2
index 679c79eacd54..533d9afc37ba 100644
@@ -54,8 +54,8 @@ bool egl_dmabuf_export_texture(uint32_t tex_id, int *fd, EGLint *offset,
 
 void egl_dmabuf_import_texture(QemuDmaBuf *dmabuf);
 void egl_dmabuf_release_texture(QemuDmaBuf *dmabuf);
-void egl_dmabuf_create_sync(QemuDmaBuf *dmabuf);
-void egl_dmabuf_create_fence(QemuDmaBuf *dmabuf);
+EGLSyncKHR egl_create_sync(void);
+int egl_create_fence(EGLSyncKHR sync);
 
 #endif
 
include/ui/gtk.h +5 -3
index 5c9c34a069ea..4b4ef50e3701 100644
@@ -58,6 +58,7 @@ typedef struct VirtualGfxConsole {
     bool y0_top;
     bool scanout_mode;
     bool has_dmabuf;
+    int gl_fence_fd;
 #endif
 } VirtualGfxConsole;
 
@@ -160,7 +161,7 @@ extern bool gtk_use_gl_area;
 /* ui/gtk.c */
 void gd_update_windowsize(VirtualConsole *vc);
 void gd_update_monitor_refresh_rate(VirtualConsole *vc, GtkWidget *widget);
-void gd_hw_gl_flushed(void *vc);
+void gd_gl_wait_sync(VirtualConsole *vc, void *sync);
 
 /* ui/gtk-egl.c */
 void gd_egl_init(VirtualConsole *vc);
@@ -190,8 +191,9 @@ void gd_egl_cursor_position(DisplayChangeListener *dcl,
                             uint32_t pos_x, uint32_t pos_y);
 void gd_egl_flush(DisplayChangeListener *dcl,
                   uint32_t x, uint32_t y, uint32_t w, uint32_t h);
-void gd_egl_scanout_flush(DisplayChangeListener *dcl,
-                          uint32_t x, uint32_t y, uint32_t w, uint32_t h);
+void *gd_egl_scanout_flush(DisplayChangeListener *dcl,
+                           uint32_t x, uint32_t y,
+                           uint32_t w, uint32_t h);
 void gtk_egl_init(DisplayGLMode mode);
 int gd_egl_make_current(DisplayGLCtx *dgc,
                         QEMUGLContext ctx);
ui/dmabuf.c +0 -29
index 7433a268f0df..b61dc7ad2783 100644
@@ -25,8 +25,6 @@ struct QemuDmaBuf {
     uint32_t  backing_width;
     uint32_t  backing_height;
     bool      y0_top;
-    void      *sync;
-    int       fence_fd;
     bool      allow_fences;
     bool      draw_submitted;
 };
@@ -57,7 +55,6 @@ QemuDmaBuf *qemu_dmabuf_new(uint32_t width, uint32_t height,
     memcpy(dmabuf->fd, dmabuf_fd, num_planes * sizeof(*dmabuf_fd));
     dmabuf->allow_fences = allow_fences;
     dmabuf->y0_top = y0_top;
-    dmabuf->fence_fd = -1;
     dmabuf->num_planes = num_planes;
 
     return dmabuf;
@@ -208,20 +205,6 @@ bool qemu_dmabuf_get_y0_top(QemuDmaBuf *dmabuf)
     return dmabuf->y0_top;
 }
 
-void *qemu_dmabuf_get_sync(QemuDmaBuf *dmabuf)
-{
-    assert(dmabuf != NULL);
-
-    return dmabuf->sync;
-}
-
-int32_t qemu_dmabuf_get_fence_fd(QemuDmaBuf *dmabuf)
-{
-    assert(dmabuf != NULL);
-
-    return dmabuf->fence_fd;
-}
-
 bool qemu_dmabuf_get_allow_fences(QemuDmaBuf *dmabuf)
 {
     assert(dmabuf != NULL);
@@ -242,18 +225,6 @@ void qemu_dmabuf_set_texture(QemuDmaBuf *dmabuf, uint32_t texture)
     dmabuf->texture = texture;
 }
 
-void qemu_dmabuf_set_fence_fd(QemuDmaBuf *dmabuf, int32_t fence_fd)
-{
-    assert(dmabuf != NULL);
-    dmabuf->fence_fd = fence_fd;
-}
-
-void qemu_dmabuf_set_sync(QemuDmaBuf *dmabuf, void *sync)
-{
-    assert(dmabuf != NULL);
-    dmabuf->sync = sync;
-}
-
 void qemu_dmabuf_set_draw_submitted(QemuDmaBuf *dmabuf, bool draw_submitted)
 {
     assert(dmabuf != NULL);
ui/egl-helpers.c +8 -7
index 8e3729a54d15..cb9c1e839cf9 100644
@@ -432,7 +432,7 @@ void egl_dmabuf_release_texture(QemuDmaBuf *dmabuf)
     qemu_dmabuf_set_texture(dmabuf, 0);
 }
 
-void egl_dmabuf_create_sync(QemuDmaBuf *dmabuf)
+EGLSyncKHR egl_create_sync(void)
 {
     EGLSyncKHR sync;
 
@@ -443,23 +443,24 @@ void egl_dmabuf_create_sync(QemuDmaBuf *dmabuf)
         sync = eglCreateSyncKHR(qemu_egl_display,
                                 EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
         if (sync != EGL_NO_SYNC_KHR) {
-            qemu_dmabuf_set_sync(dmabuf, sync);
+            return sync;
         }
     }
+
+    return NULL;
 }
 
-void egl_dmabuf_create_fence(QemuDmaBuf *dmabuf)
+int egl_create_fence(EGLSyncKHR sync)
 {
-    void *sync = qemu_dmabuf_get_sync(dmabuf);
-    int fence_fd;
+    int fence_fd = -1;
 
     if (sync) {
         fence_fd = eglDupNativeFenceFDANDROID(qemu_egl_display,
                                               sync);
-        qemu_dmabuf_set_fence_fd(dmabuf, fence_fd);
         eglDestroySyncKHR(qemu_egl_display, sync);
-        qemu_dmabuf_set_sync(dmabuf, NULL);
     }
+
+    return fence_fd;
 }
 
 #endif /* CONFIG_GBM */
ui/gtk-egl.c +16 -14
index adc81f34b1c8..c671aabd11b3 100644
@@ -68,7 +68,7 @@ void gd_egl_draw(VirtualConsole *vc)
     GdkWindow *window;
 #ifdef CONFIG_GBM
     QemuDmaBuf *dmabuf = vc->gfx.guest_fb.dmabuf;
-    int fence_fd;
+    EGLSyncKHR sync;
 #endif
     int ww, wh, pw, ph, gs;
 
@@ -94,7 +94,11 @@ void gd_egl_draw(VirtualConsole *vc)
             qemu_console_hw_gl_block(vc->gfx.dcl.con, true);
         }
 #endif
-        gd_egl_scanout_flush(&vc->gfx.dcl, 0, 0, vc->gfx.w, vc->gfx.h);
+#ifdef CONFIG_GBM
+        sync =
+#endif
+            gd_egl_scanout_flush(&vc->gfx.dcl, 0, 0,
+                                 vc->gfx.w, vc->gfx.h);
 
         gd_update_scale(vc, ww, wh,
                         surface_width(vc->gfx.ds),
@@ -103,13 +107,7 @@ void gd_egl_draw(VirtualConsole *vc)
         glFlush();
 #ifdef CONFIG_GBM
         if (dmabuf) {
-            egl_dmabuf_create_fence(dmabuf);
-            fence_fd = qemu_dmabuf_get_fence_fd(dmabuf);
-            if (fence_fd >= 0) {
-                qemu_set_fd_handler(fence_fd, gd_hw_gl_flushed, NULL, vc);
-                return;
-            }
-            qemu_console_hw_gl_block(vc->gfx.dcl.con, false);
+            gd_gl_wait_sync(vc, sync);
         }
 #endif
     } else {
@@ -330,10 +328,12 @@ void gd_egl_cursor_position(DisplayChangeListener *dcl,
     vc->gfx.cursor_y = pos_y * vc->gfx.scale_y;
 }
 
-void gd_egl_scanout_flush(DisplayChangeListener *dcl,
-                          uint32_t x, uint32_t y, uint32_t w, uint32_t h)
+EGLSyncKHR gd_egl_scanout_flush(DisplayChangeListener *dcl,
+                                uint32_t x, uint32_t y,
+                                uint32_t w, uint32_t h)
 {
     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
+    EGLSyncKHR sync = EGL_NO_SYNC_KHR;
     GdkWindow *window;
     int px_offset, py_offset;
     int gs;
@@ -342,10 +342,10 @@ void gd_egl_scanout_flush(DisplayChangeListener *dcl,
     int fbw, fbh;
 
     if (!vc->gfx.scanout_mode) {
-        return;
+        return sync;
     }
     if (!vc->gfx.guest_fb.framebuffer) {
-        return;
+        return sync;
     }
 
     eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
@@ -391,11 +391,13 @@ void gd_egl_scanout_flush(DisplayChangeListener *dcl,
 
 #ifdef CONFIG_GBM
     if (vc->gfx.guest_fb.dmabuf) {
-        egl_dmabuf_create_sync(vc->gfx.guest_fb.dmabuf);
+        sync = egl_create_sync();
     }
 #endif
 
     eglSwapBuffers(qemu_egl_display, vc->gfx.esurface);
+
+    return sync;
 }
 
 void gd_egl_flush(DisplayChangeListener *dcl,
ui/gtk-gl-area.c +3 -9
index 29497019ee46..29d1b1e4116f 100644
@@ -41,6 +41,7 @@ void gd_gl_area_draw(VirtualConsole *vc)
 {
 #ifdef CONFIG_GBM
     QemuDmaBuf *dmabuf = vc->gfx.guest_fb.dmabuf;
+    EGLSyncKHR sync = EGL_NO_SYNC_KHR;
 #endif
     int pw, ph, gs, y1, y2;
     int ww, wh;
@@ -119,20 +120,13 @@ void gd_gl_area_draw(VirtualConsole *vc)
                           GL_COLOR_BUFFER_BIT, GL_NEAREST);
 #ifdef CONFIG_GBM
         if (dmabuf) {
-            egl_dmabuf_create_sync(dmabuf);
+            sync = egl_create_sync();
         }
 #endif
         glFlush();
 #ifdef CONFIG_GBM
         if (dmabuf) {
-            int fence_fd;
-            egl_dmabuf_create_fence(dmabuf);
-            fence_fd = qemu_dmabuf_get_fence_fd(dmabuf);
-            if (fence_fd >= 0) {
-                qemu_set_fd_handler(fence_fd, gd_hw_gl_flushed, NULL, vc);
-                return;
-            }
-            qemu_console_hw_gl_block(vc->gfx.dcl.con, false);
+            gd_gl_wait_sync(vc, sync);
         }
 #endif
     } else {
ui/gtk.c +23 -8
index 7078d89d6797..1c75b9ea3c4d 100644
@@ -597,21 +597,32 @@ static void gd_gl_release_dmabuf(DisplayChangeListener *dcl,
 #endif
 }
 
-void gd_hw_gl_flushed(void *vcon)
+#ifdef CONFIG_GBM
+static void gd_gl_fence_cb(void *vcon)
 {
     VirtualConsole *vc = vcon;
-    QemuDmaBuf *dmabuf = vc->gfx.guest_fb.dmabuf;
-    int fence_fd;
 
-    fence_fd = qemu_dmabuf_get_fence_fd(dmabuf);
-    if (fence_fd >= 0) {
-        qemu_set_fd_handler(fence_fd, NULL, NULL, NULL);
-        close(fence_fd);
-        qemu_dmabuf_set_fence_fd(dmabuf, -1);
+    if (vc->gfx.gl_fence_fd >= 0) {
+        qemu_set_fd_handler(vc->gfx.gl_fence_fd, NULL, NULL, NULL);
+        g_clear_fd(&vc->gfx.gl_fence_fd, NULL);
         qemu_console_hw_gl_block(vc->gfx.dcl.con, false);
     }
 }
 
+void gd_gl_wait_sync(VirtualConsole *vc, EGLSyncKHR sync)
+{
+    assert(vc->gfx.gl_fence_fd < 0);
+
+    vc->gfx.gl_fence_fd = egl_create_fence(sync);
+    if (vc->gfx.gl_fence_fd >= 0) {
+        qemu_set_fd_handler(vc->gfx.gl_fence_fd,
+                            gd_gl_fence_cb, NULL, vc);
+    } else {
+        qemu_console_hw_gl_block(vc->gfx.dcl.con, false);
+    }
+}
+#endif
+
 /** DisplayState Callbacks (opengl version) **/
 
 static const DisplayChangeListenerOps dcl_gl_area_ops = {
@@ -2355,6 +2366,7 @@ add_gfx_console(GtkDisplayState *s, QemuConsole *con)
     vc->gfx.scale_y = vc->gfx.preferred_scale;
 
 #if defined(CONFIG_OPENGL)
+    vc->gfx.gl_fence_fd = -1;
     if (display_opengl) {
         if (gtk_use_gl_area) {
             vc->gfx.drawing_area = gtk_gl_area_new();
@@ -2647,6 +2659,9 @@ static void gd_vc_free(void *p)
 
     switch (vc->type) {
     case GD_VC_GFX:
+#if defined(CONFIG_OPENGL) && defined(CONFIG_GBM)
+        gd_gl_fence_cb(vc);
+#endif
         qemu_console_unregister_listener(&vc->gfx.dcl);
 #if defined(CONFIG_OPENGL)
         if (display_opengl) {
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
Reviewed-by on v2 6/25
#10 lore raw

Re: [PATCH v2 06/25] ui/gtk: move GL fence tracking from QemuDmaBuf to VirtualGfxConsole

Quoted 5 lines · Marc-André Lureau

The sync and fence_fd fields on QemuDmaBuf are only used by the GTK display backend for GPU fence synchronization during draw. They are not intrinsic properties of the DMA-BUF itself but transient GL state tied to the display listener.

Quoted 7 lines

Move fence_fd to VirtualGfxConsole and keep the EGL sync local to the draw operation. Change egl_dmabuf_create_sync() and egl_dmabuf_create_fence() to return values instead of storing them on the dmabuf. This makes the fence lifecycle independent of the dmabuf lifetime.

Regards,
Akihiko Odaki

#11 lore raw

PATCH v2 7/25 ui/gtk: move draw_submitted from QemuDmaBuf to VirtualGfxConsole

In reply to Marc-André Lureau

The draw_submitted flag on QemuDmaBuf is only used by the GTK display backend to gate the draw pipeline and prevent gl_block refcount imbalance when the guest submits frames faster than GTK draws them. It is not an intrinsic property of the DMA-BUF but transient display listener state.

Move it to VirtualGfxConsole, replacing the getter/setter calls with direct struct member access. Reset the flag when the active dmabuf is released in gd_gl_release_dmabuf().

---
 include/ui/dmabuf.h |  2 --
 include/ui/gtk.h    |  1 +
 ui/dmabuf.c         | 13 -------------
 ui/gtk-egl.c        | 13 ++++++-------
 ui/gtk-gl-area.c    | 10 +++++-----
 ui/gtk.c            |  1 +
 6 files changed, 13 insertions(+), 27 deletions(-)
include/ui/dmabuf.h +0 -2
index 381583bd536c..b972099859f2 100644
@@ -42,8 +42,6 @@ uint32_t qemu_dmabuf_get_backing_width(QemuDmaBuf *dmabuf);
 uint32_t qemu_dmabuf_get_backing_height(QemuDmaBuf *dmabuf);
 bool qemu_dmabuf_get_y0_top(QemuDmaBuf *dmabuf);
 bool qemu_dmabuf_get_allow_fences(QemuDmaBuf *dmabuf);
-bool qemu_dmabuf_get_draw_submitted(QemuDmaBuf *dmabuf);
 void qemu_dmabuf_set_texture(QemuDmaBuf *dmabuf, uint32_t texture);
-void qemu_dmabuf_set_draw_submitted(QemuDmaBuf *dmabuf, bool draw_submitted);
 
 #endif
include/ui/gtk.h +1 -0
index 4b4ef50e3701..b66b9ca5c519 100644
@@ -59,6 +59,7 @@ typedef struct VirtualGfxConsole {
     bool scanout_mode;
     bool has_dmabuf;
     int gl_fence_fd;
+    bool draw_submitted;
 #endif
 } VirtualGfxConsole;
 
ui/dmabuf.c +0 -13
index b61dc7ad2783..16063dc62ae8 100644
@@ -26,7 +26,6 @@ struct QemuDmaBuf {
     uint32_t  backing_height;
     bool      y0_top;
     bool      allow_fences;
-    bool      draw_submitted;
 };
 
 QemuDmaBuf *qemu_dmabuf_new(uint32_t width, uint32_t height,
@@ -212,21 +211,9 @@ bool qemu_dmabuf_get_allow_fences(QemuDmaBuf *dmabuf)
     return dmabuf->allow_fences;
 }
 
-bool qemu_dmabuf_get_draw_submitted(QemuDmaBuf *dmabuf)
-{
-    assert(dmabuf != NULL);
-
-    return dmabuf->draw_submitted;
-}
-
 void qemu_dmabuf_set_texture(QemuDmaBuf *dmabuf, uint32_t texture)
 {
     assert(dmabuf != NULL);
     dmabuf->texture = texture;
 }
 
-void qemu_dmabuf_set_draw_submitted(QemuDmaBuf *dmabuf, bool draw_submitted)
-{
-    assert(dmabuf != NULL);
-    dmabuf->draw_submitted = draw_submitted;
-}
ui/gtk-egl.c +6 -7
index c671aabd11b3..8623181336e9 100644
@@ -86,10 +86,10 @@ void gd_egl_draw(VirtualConsole *vc)
     if (vc->gfx.scanout_mode) {
 #ifdef CONFIG_GBM
         if (dmabuf) {
-            if (!qemu_dmabuf_get_draw_submitted(dmabuf)) {
+            if (!vc->gfx.draw_submitted) {
                 return;
             } else {
-                qemu_dmabuf_set_draw_submitted(dmabuf, false);
+                vc->gfx.draw_submitted = false;
             }
             qemu_console_hw_gl_block(vc->gfx.dcl.con, true);
         }
@@ -169,8 +169,7 @@ void gd_egl_refresh(DisplayChangeListener *dcl)
 #endif
     }
 
-    if (vc->gfx.guest_fb.dmabuf &&
-        qemu_dmabuf_get_draw_submitted(vc->gfx.guest_fb.dmabuf)) {
+    if (vc->gfx.guest_fb.dmabuf && vc->gfx.draw_submitted) {
         gd_egl_draw(vc);
         return;
     }
@@ -290,6 +289,7 @@ void gd_egl_scanout_dmabuf(DisplayChangeListener *dcl,
 
     if (qemu_dmabuf_get_allow_fences(dmabuf)) {
         vc->gfx.guest_fb.dmabuf = dmabuf;
+        vc->gfx.draw_submitted = false;
     }
 #endif
 }
@@ -406,9 +406,8 @@ void gd_egl_flush(DisplayChangeListener *dcl,
     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
     GtkWidget *area = vc->gfx.drawing_area;
 
-    if (vc->gfx.guest_fb.dmabuf &&
-        !qemu_dmabuf_get_draw_submitted(vc->gfx.guest_fb.dmabuf)) {
-        qemu_dmabuf_set_draw_submitted(vc->gfx.guest_fb.dmabuf, true);
+    if (vc->gfx.guest_fb.dmabuf && !vc->gfx.draw_submitted) {
+        vc->gfx.draw_submitted = true;
         gtk_egl_set_scanout_mode(vc, true);
     }
 
ui/gtk-gl-area.c +5 -5
index 29d1b1e4116f..45c0c8c81a11 100644
@@ -82,10 +82,10 @@ void gd_gl_area_draw(VirtualConsole *vc)
 
 #ifdef CONFIG_GBM
         if (dmabuf) {
-            if (!qemu_dmabuf_get_draw_submitted(dmabuf)) {
+            if (!vc->gfx.draw_submitted) {
                 return;
             } else {
-                qemu_dmabuf_set_draw_submitted(dmabuf, false);
+                vc->gfx.draw_submitted = false;
             }
             qemu_console_hw_gl_block(vc->gfx.dcl.con, true);
         }
@@ -319,9 +319,8 @@ void gd_gl_area_scanout_flush(DisplayChangeListener *dcl,
 {
     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
 
-    if (vc->gfx.guest_fb.dmabuf &&
-        !qemu_dmabuf_get_draw_submitted(vc->gfx.guest_fb.dmabuf)) {
-        qemu_dmabuf_set_draw_submitted(vc->gfx.guest_fb.dmabuf, true);
+    if (vc->gfx.guest_fb.dmabuf && !vc->gfx.draw_submitted) {
+        vc->gfx.draw_submitted = true;
         gtk_gl_area_set_scanout_mode(vc, true);
     }
     gtk_gl_area_queue_render(GTK_GL_AREA(vc->gfx.drawing_area));
@@ -356,6 +355,7 @@ void gd_gl_area_scanout_dmabuf(DisplayChangeListener *dcl,
 
     if (qemu_dmabuf_get_allow_fences(dmabuf)) {
         vc->gfx.guest_fb.dmabuf = dmabuf;
+        vc->gfx.draw_submitted = false;
     }
 #endif
 }
ui/gtk.c +1 -0
index 1c75b9ea3c4d..55e45e8445bc 100644
@@ -593,6 +593,7 @@ static void gd_gl_release_dmabuf(DisplayChangeListener *dcl,
     egl_dmabuf_release_texture(dmabuf);
     if (vc->gfx.guest_fb.dmabuf == dmabuf) {
         vc->gfx.guest_fb.dmabuf = NULL;
+        vc->gfx.draw_submitted = false;
     }
 #endif
 }
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
Reviewed-by on v2 7/25
#12 lore raw

Re: [PATCH v2 07/25] ui/gtk: move draw_submitted from QemuDmaBuf to VirtualGfxConsole

Quoted 11 lines · Marc-André Lureau

The draw_submitted flag on QemuDmaBuf is only used by the GTK display backend to gate the draw pipeline and prevent gl_block refcount imbalance when the guest submits frames faster than GTK draws them. It is not an intrinsic property of the DMA-BUF but transient display listener state.

Move it to VirtualGfxConsole, replacing the getter/setter calls with direct struct member access. Reset the flag when the active dmabuf is released in gd_gl_release_dmabuf().

Regards,
Akihiko Odaki

#13 lore raw

PATCH v2 8/25 ui/dbus: disable scanout iff dmabuf is current

In reply to Marc-André Lureau

Narrows release_dmabuf to the active scanout, as the device & console may interleave new scanouts before releasing old ones.

Fixes 142ca628a733 ("ui: add a D-Bus display backend")
---
 ui/dbus-listener.c | 68 ++++++++++++++++++++++++++++++++++++++----------------
 1 file changed, 48 insertions(+), 20 deletions(-)
ui/dbus-listener.c +48 -20
index 2e2f6ba4183a..5a72c7eeae59 100644
@@ -91,6 +91,8 @@ struct _DBusDisplayListener {
     guint dbus_filter;
     guint32 display_serial_to_discard;
     guint32 cursor_serial_to_discard;
+
+    QemuDmaBuf *scanout_dmabuf;
 };
 
 G_DEFINE_TYPE(DBusDisplayListener, dbus_display_listener, G_TYPE_OBJECT)
@@ -119,6 +121,7 @@ static void dbus_scanout_disable(DisplayChangeListener *dcl)
 {
     DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
 
+    ddl->scanout_dmabuf = NULL;
     ddl_discard_display_messages(ddl);
 
     qemu_dbus_display1_listener_call_disable(
@@ -298,9 +301,9 @@ static void dbus_call_update_gl(DisplayChangeListener *dcl,
 
 #ifdef CONFIG_GBM
 static void dbus_scanout_dmabuf_v1(DBusDisplayListener *ddl,
-                                   QemuDmaBuf *dmabuf)
+                                   QemuDmaBuf *dmabuf,
+                                   GError **err)
 {
-    g_autoptr(GError) err = NULL;
     g_autoptr(GUnixFDList) fd_list = NULL;
     int fd;
     uint32_t width, height, stride, fourcc;
@@ -309,8 +312,7 @@ static void dbus_scanout_dmabuf_v1(DBusDisplayListener *ddl,
 
     fd = qemu_dmabuf_get_fds(dmabuf, NULL)[0];
     fd_list = g_unix_fd_list_new();
-    if (g_unix_fd_list_append(fd_list, fd, &err) != 0) {
-        error_report("Failed to setup dmabuf fdlist: %s", err->message);
+    if (g_unix_fd_list_append(fd_list, fd, err) != 0) {
         return;
     }
 
@@ -332,9 +334,9 @@ static void dbus_scanout_dmabuf_v1(DBusDisplayListener *ddl,
 }
 
 static void dbus_scanout_dmabuf_v2(DBusDisplayListener *ddl,
-                                   QemuDmaBuf *dmabuf)
+                                   QemuDmaBuf *dmabuf,
+                                   GError **err)
 {
-    g_autoptr(GError) err = NULL;
     g_autoptr(GUnixFDList) fd_list = NULL;
     int i, fd_index[DMABUF_MAX_PLANES], num_fds;
     uint32_t x, y, width, height, fourcc, backing_width, backing_height;
@@ -360,9 +362,8 @@ static void dbus_scanout_dmabuf_v2(DBusDisplayListener *ddl,
             break;
         }
 
-        fd_index[num_fds] = g_unix_fd_list_append(fd_list, plane_fd, &err);
+        fd_index[num_fds] = g_unix_fd_list_append(fd_list, plane_fd, err);
         if (fd_index[num_fds] < 0) {
-            error_report("Failed to setup dmabuf fdlist: %s", err->message);
             return;
         }
     }
@@ -395,20 +396,36 @@ static void dbus_scanout_dmabuf_v2(DBusDisplayListener *ddl,
         G_DBUS_CALL_FLAGS_NONE, -1, fd_list, NULL, NULL, NULL);
 }
 
+static bool dbus_call_scanout_dmabuf(DBusDisplayListener *ddl,
+                                     QemuDmaBuf *dmabuf)
+{
+    g_autoptr(GError) err = NULL;
+
+    if (ddl->scanout_dmabuf_v2_proxy) {
+        dbus_scanout_dmabuf_v2(ddl, dmabuf, &err);
+    } else {
+        if (qemu_dmabuf_get_num_planes(dmabuf) > 1) {
+            error_report("Peer does not support multi plane dmabuf");
+            return false;
+        }
+        dbus_scanout_dmabuf_v1(ddl, dmabuf, &err);
+    }
+
+    if (err) {
+        error_report("Failed to scanout dmabuf: %s", err->message);
+        return false;
+    }
+
+    return true;
+}
+
 static void dbus_scanout_dmabuf(DisplayChangeListener *dcl,
                                 QemuDmaBuf *dmabuf)
 {
     DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
 
-    if (ddl->scanout_dmabuf_v2_proxy) {
-        dbus_scanout_dmabuf_v2(ddl, dmabuf);
-    } else {
-        if (qemu_dmabuf_get_num_planes(dmabuf) > 1) {
-            g_debug("org.qemu.Display1.Listener.ScanoutDMABUF "
-                    "does not support mutli plane");
-            return;
-        }
-        dbus_scanout_dmabuf_v1(ddl, dmabuf);
+    if (dbus_call_scanout_dmabuf(ddl, dmabuf)) {
+        ddl->scanout_dmabuf = dmabuf;
     }
 }
 #endif /* GBM */
@@ -589,6 +606,10 @@ static void dbus_scanout_texture(DisplayChangeListener *dcl,
                                  uint32_t w, uint32_t h,
                                  void *d3d_tex2d)
 {
+#if defined(CONFIG_GBM) || defined(WIN32)
+    DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
+#endif
+
     trace_dbus_scanout_texture(tex_id, backing_y_0_top,
                                backing_width, backing_height, x, y, w, h);
 #ifdef CONFIG_GBM
@@ -607,13 +628,13 @@ static void dbus_scanout_texture(DisplayChangeListener *dcl,
                              backing_height, fourcc, modifier, fd, num_planes,
                              false, backing_y_0_top);
 
-    dbus_scanout_dmabuf(dcl, dmabuf);
+    if (dbus_call_scanout_dmabuf(ddl, dmabuf)) {
+        ddl->scanout_dmabuf = NULL;
+    }
     qemu_dmabuf_close(dmabuf);
 #endif
 
 #ifdef WIN32
-    DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
-
     /* there must be a matching gfx_switch before */
     assert(surface_width(ddl->ds) == w);
     assert(surface_height(ddl->ds) == h);
@@ -686,7 +707,14 @@ static void dbus_cursor_dmabuf(DisplayChangeListener *dcl,
 static void dbus_release_dmabuf(DisplayChangeListener *dcl,
                                 QemuDmaBuf *dmabuf)
 {
+    DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
+
+    if (ddl->scanout_dmabuf != dmabuf) {
+        return;
+    }
+
     dbus_scanout_disable(dcl);
+    ddl->scanout_dmabuf = NULL;
 }
 #endif /* GBM */
 
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
#15 lore raw

PATCH v2 9/25 virtio-gpu: store the scanout DMABUF in virtio_gpu_scanout

In reply to Marc-André Lureau

VirtIOGPU tracks presented dmabufs both through a per-scanout primary pointer and a device-wide list of VGPUDMABuf wrappers. Only the current DMA-BUF for a scanout can ever be presented, so the list and the wrapper struct are redundant bookkeeping.

Store the QemuDmaBuf directly in virtio_gpu_scanout and drop VGPUDMABuf and the device-wide list. The wrapper only carried the buffer and its scanout id; the id is now implicit in the owning scanout.

No functional change intended.

---
 hw/display/virtio-gpu-udmabuf.c | 68 +++++++++++++++--------------------------
 include/hw/virtio/virtio-gpu.h  | 12 +-------
 2 files changed, 25 insertions(+), 55 deletions(-)
hw/display/virtio-gpu-udmabuf.c +24 -44
index fc63f2c4ec21..a38afb24c71f 100644
@@ -160,30 +160,19 @@ bool virtio_gpu_init_udmabuf(struct virtio_gpu_simple_resource *res)
     return true;
 }
 
-static void virtio_gpu_free_dmabuf(VirtIOGPU *g, VGPUDMABuf *dmabuf)
-{
-    struct virtio_gpu_scanout *scanout;
-
-    scanout = &g->parent_obj.scanout[dmabuf->scanout_id];
-    qemu_console_gl_release_dmabuf(scanout->con, dmabuf->buf);
-    g_clear_pointer(&dmabuf->buf, qemu_dmabuf_free);
-    QTAILQ_REMOVE(&g->dmabuf.bufs, dmabuf, next);
-    g_free(dmabuf);
-}
-
 void virtio_gpu_fini_udmabuf(VirtIOGPU *g, struct virtio_gpu_simple_resource *res)
 {
     int max_outputs = g->parent_obj.conf.max_outputs;
     int i;
 
     for (i = 0; i < max_outputs; i++) {
-        VGPUDMABuf *dmabuf = g->dmabuf.primary[i];
+        struct virtio_gpu_scanout *scanout = &g->parent_obj.scanout[i];
 
-        if (dmabuf &&
-            qemu_dmabuf_get_num_planes(dmabuf->buf) > 0 &&
-            qemu_dmabuf_get_fds(dmabuf->buf, NULL)[0] == res->dmabuf_fd &&
+        if (scanout->dmabuf &&
+            qemu_dmabuf_get_num_planes(scanout->dmabuf) > 0 &&
+            qemu_dmabuf_get_fds(scanout->dmabuf, NULL)[0] == res->dmabuf_fd &&
             res->dmabuf_fd != -1) {
-            qemu_dmabuf_close(dmabuf->buf);
+            qemu_dmabuf_close(scanout->dmabuf);
             res->dmabuf_fd = -1;
             res->share_handle = SHAREABLE_NONE;
         }
@@ -192,31 +181,23 @@ void virtio_gpu_fini_udmabuf(VirtIOGPU *g, struct virtio_gpu_simple_resource *re
     virtio_gpu_destroy_udmabuf(res);
 }
 
-static VGPUDMABuf
-*virtio_gpu_create_dmabuf(VirtIOGPU *g,
-                          uint32_t scanout_id,
-                          struct virtio_gpu_simple_resource *res,
-                          struct virtio_gpu_framebuffer *fb,
-                          struct virtio_gpu_rect *r)
+static QemuDmaBuf *
+virtio_gpu_create_dmabuf(struct virtio_gpu_simple_resource *res,
+                         struct virtio_gpu_framebuffer *fb,
+                         struct virtio_gpu_rect *r)
 {
-    VGPUDMABuf *dmabuf;
     uint32_t offset = 0;
 
     if (res->dmabuf_fd < 0) {
         return NULL;
     }
 
-    dmabuf = g_new0(VGPUDMABuf, 1);
-    dmabuf->buf = qemu_dmabuf_new(r->width, r->height,
-                                  &offset, &fb->stride,
-                                  r->x, r->y, fb->width, fb->height,
-                                  qemu_pixman_to_drm_format(fb->format),
-                                  DRM_FORMAT_MOD_INVALID, &res->dmabuf_fd,
-                                  1, true, false);
-    dmabuf->scanout_id = scanout_id;
-    QTAILQ_INSERT_HEAD(&g->dmabuf.bufs, dmabuf, next);
-
-    return dmabuf;
+    return qemu_dmabuf_new(r->width, r->height,
+                           &offset, &fb->stride,
+                           r->x, r->y, fb->width, fb->height,
+                           qemu_pixman_to_drm_format(fb->format),
+                           DRM_FORMAT_MOD_INVALID, &res->dmabuf_fd,
+                           1, true, false);
 }
 
 int virtio_gpu_update_dmabuf(VirtIOGPU *g,
@@ -226,26 +207,25 @@ int virtio_gpu_update_dmabuf(VirtIOGPU *g,
                              struct virtio_gpu_rect *r)
 {
     struct virtio_gpu_scanout *scanout = &g->parent_obj.scanout[scanout_id];
-    VGPUDMABuf *new_primary, *old_primary = NULL;
+    QemuDmaBuf *new_primary, *old_primary;
     uint32_t width, height;
 
-    new_primary = virtio_gpu_create_dmabuf(g, scanout_id, res, fb, r);
+    new_primary = virtio_gpu_create_dmabuf(res, fb, r);
     if (!new_primary) {
         return -EINVAL;
     }
 
-    if (g->dmabuf.primary[scanout_id]) {
-        old_primary = g->dmabuf.primary[scanout_id];
-    }
+    old_primary = scanout->dmabuf;
 
-    width = qemu_dmabuf_get_width(new_primary->buf);
-    height = qemu_dmabuf_get_height(new_primary->buf);
-    g->dmabuf.primary[scanout_id] = new_primary;
+    width = qemu_dmabuf_get_width(new_primary);
+    height = qemu_dmabuf_get_height(new_primary);
+    scanout->dmabuf = new_primary;
     qemu_console_resize(scanout->con, width, height);
-    qemu_console_gl_scanout_dmabuf(scanout->con, new_primary->buf);
+    qemu_console_gl_scanout_dmabuf(scanout->con, new_primary);
 
     if (old_primary) {
-        virtio_gpu_free_dmabuf(g, old_primary);
+        qemu_console_gl_release_dmabuf(scanout->con, old_primary);
+        qemu_dmabuf_free(old_primary);
     }
 
     return 0;
include/hw/virtio/virtio-gpu.h +1 -11
index 8c0df60b3b7e..ee5f6f2f2375 100644
@@ -85,6 +85,7 @@ struct virtio_gpu_framebuffer {
 struct virtio_gpu_scanout {
     QemuConsole *con;
     DisplaySurface *ds;
+    QemuDmaBuf *dmabuf;
     uint32_t width, height;
     int x, y;
     int invalidate;
@@ -189,12 +190,6 @@ struct VirtIOGPUBaseClass {
     DEFINE_PROP_UINT32("xres", _state, _conf.xres, 1280), \
     DEFINE_PROP_UINT32("yres", _state, _conf.yres, 800)
 
-typedef struct VGPUDMABuf {
-    QemuDmaBuf *buf;
-    uint32_t scanout_id;
-    QTAILQ_ENTRY(VGPUDMABuf) next;
-} VGPUDMABuf;
-
 struct VirtIOGPU {
     VirtIOGPUBase parent_obj;
 
@@ -226,11 +221,6 @@ struct VirtIOGPU {
         uint32_t bytes_3d;
     } stats;
 
-    struct {
-        QTAILQ_HEAD(, VGPUDMABuf) bufs;
-        VGPUDMABuf *primary[VIRTIO_GPU_MAX_SCANOUTS];
-    } dmabuf;
-
     GArray *capset_ids;
 };
 
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
Reviewed-by on v2 9/25
#16 lore raw

Re: [PATCH v2 09/25] virtio-gpu: store the scanout DMABUF in virtio_gpu_scanout

Quoted 5 lines · Marc-André Lureau

VirtIOGPU tracks presented dmabufs both through a per-scanout primary pointer and a device-wide list of VGPUDMABuf wrappers. Only the current DMA-BUF for a scanout can ever be presented, so the list and the wrapper struct are redundant bookkeeping.

Quoted 7 lines

Store the QemuDmaBuf directly in virtio_gpu_scanout and drop VGPUDMABuf and the device-wide list. The wrapper only carried the buffer and its scanout id; the id is now implicit in the owning scanout.

No functional change intended.

Regards,
Akihiko Odaki

#17 lore raw

PATCH v2 10/25 ui/console: disable GL scanout when dmabuf is the active one

In reply to Marc-André Lureau

Disable the GL scanout when the actively-displayed dmabuf is released, so the console does not retain a stale reference.

The VFIO commit changes to call qemu_console_gl_release_dmabuf() before qemu_graphic_console_close(), which dereferences the active scanout.

Fixes 239b7c5705a4 ("vfio/pci: close display console during unrealize, not finalize")
---
 ui/console.c | 3 +++
 1 file changed, 3 insertions(+)
ui/console.c +3 -0
index a8a2a247d8f4..14f148118e37 100644
@@ -1049,6 +1049,9 @@ void qemu_console_gl_release_dmabuf(QemuConsole *con,
             dcl->ops->dpy_gl_release_dmabuf(dcl, dmabuf);
         }
     }
+    if (con->scanout.kind == SCANOUT_DMABUF && dmabuf == con->scanout.dmabuf) {
+        qemu_console_gl_scanout_disable(con);
+    }
 }
 
 void qemu_console_gl_update(QemuConsole *con,
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
Reviewed-by on v2 10/25
#18 lore raw

Re: [PATCH v2 10/25] ui/console: disable GL scanout when dmabuf is the active one

Quoted 8 lines · Marc-André Lureau

Disable the GL scanout when the actively-displayed dmabuf is released, so the console does not retain a stale reference.

The VFIO commit changes to call qemu_console_gl_release_dmabuf() before qemu_graphic_console_close(), which dereferences the active scanout.

Fixes 239b7c5705a4 ("vfio/pci: close display console during unrealize, not finalize")

Regards,
Akihiko Odaki

#19 lore raw

PATCH v2 11/25 ui/sdl2: clear guest_fb.dmabuf on release

In reply to Marc-André Lureau

sdl2_gl_release_dmabuf() releases the texture but leaves the guest_fb.dmabuf pointer dangling. Clear it to prevent stale references after the dmabuf is freed.

Fixes 52053b7e0a0e ("ui/sdl2: Implement dpy dmabuf functions")
---
 ui/sdl2-gl.c | 5 +++++
 1 file changed, 5 insertions(+)
ui/sdl2-gl.c +5 -0
index 1547ad2f6f80..46eff768c968 100644
@@ -286,7 +286,12 @@ void sdl2_gl_scanout_dmabuf(DisplayChangeListener *dcl,
 void sdl2_gl_release_dmabuf(DisplayChangeListener *dcl,
                             QemuDmaBuf *dmabuf)
 {
+    struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
+
     egl_dmabuf_release_texture(dmabuf);
+    if (scon->guest_fb.dmabuf == dmabuf) {
+        scon->guest_fb.dmabuf = NULL;
+    }
 }
 
 bool sdl2_gl_has_dmabuf(DisplayChangeListener *dcl)
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
Reviewed-by on v2 11/25
#20 lore raw

Re: [PATCH v2 11/25] ui/sdl2: clear guest_fb.dmabuf on release

Quoted 6 lines · Marc-André Lureau

sdl2_gl_release_dmabuf() releases the texture but leaves the guest_fb.dmabuf pointer dangling. Clear it to prevent stale references after the dmabuf is freed.

Fixes 52053b7e0a0e ("ui/sdl2: Implement dpy dmabuf functions")

Regards,
Akihiko Odaki

#21 lore raw

PATCH v2 12/25 ui/dmabuf: own and close fds on free

In reply to Marc-André Lureau

Make qemu_dmabuf_free() call qemu_dmabuf_close(), so the dmabuf always owns and closes its fds. This removes the need for callers to explicitly close fds before freeing and simplify the code.

To support this, virtio_gpu_create_dmabuf() now dup()s the resource fd so each QemuDmaBuf has an independent copy. The scanout-matching loop in virtio_gpu_fini_udmabuf() is no longer needed and is removed.

---
 hw/display/vhost-user-gpu.c           |  1 -
 hw/display/virtio-gpu-udmabuf-stubs.c |  2 +-
 hw/display/virtio-gpu-udmabuf.c       | 33 +++++++++------------------------
 hw/display/virtio-gpu.c               |  2 +-
 hw/vfio/display.c                     |  1 -
 include/hw/virtio/virtio-gpu.h        |  3 +--
 include/ui/dmabuf.h                   |  1 -
 ui/dbus-listener.c                    |  1 -
 ui/dmabuf.c                           | 22 ++++++++--------------
 9 files changed, 20 insertions(+), 46 deletions(-)
hw/display/vhost-user-gpu.c +0 -1
index cd684d63639e..c09aca041135 100644
@@ -293,7 +293,6 @@ vhost_user_gpu_handle_display(VhostUserGPU *g, VhostUserGpuMsg *msg)
         dmabuf = g->dmabuf[m->scanout_id];
 
         if (dmabuf) {
-            qemu_dmabuf_close(dmabuf);
             qemu_console_gl_release_dmabuf(con, dmabuf);
             g_clear_pointer(&dmabuf, qemu_dmabuf_free);
         }
hw/display/virtio-gpu-udmabuf-stubs.c +1 -1
index 0883bf05fac1..1a9dfbdec109 100644
@@ -13,7 +13,7 @@ bool virtio_gpu_init_udmabuf(struct virtio_gpu_simple_resource *res)
     return false;
 }
 
-void virtio_gpu_fini_udmabuf(VirtIOGPU *g, struct virtio_gpu_simple_resource *res)
+void virtio_gpu_fini_udmabuf(struct virtio_gpu_simple_resource *res)
 {
     /* nothing (stub) */
 }
hw/display/virtio-gpu-udmabuf.c +9 -24
index a38afb24c71f..6d2bc8d9771f 100644
@@ -82,7 +82,7 @@ static void virtio_gpu_remap_udmabuf(struct virtio_gpu_simple_resource *res)
     }
 }
 
-static void virtio_gpu_destroy_udmabuf(struct virtio_gpu_simple_resource *res)
+void virtio_gpu_fini_udmabuf(struct virtio_gpu_simple_resource *res)
 {
     if (res->remapped) {
         munmap(res->remapped, res->blob_size);
@@ -148,7 +148,7 @@ bool virtio_gpu_init_udmabuf(struct virtio_gpu_simple_resource *res)
         }
         virtio_gpu_remap_udmabuf(res);
         if (!res->remapped) {
-            virtio_gpu_destroy_udmabuf(res);
+            virtio_gpu_fini_udmabuf(res);
             return false;
         }
         res->share_handle = res->dmabuf_fd;
@@ -160,43 +160,28 @@ bool virtio_gpu_init_udmabuf(struct virtio_gpu_simple_resource *res)
     return true;
 }
 
-void virtio_gpu_fini_udmabuf(VirtIOGPU *g, struct virtio_gpu_simple_resource *res)
-{
-    int max_outputs = g->parent_obj.conf.max_outputs;
-    int i;
-
-    for (i = 0; i < max_outputs; i++) {
-        struct virtio_gpu_scanout *scanout = &g->parent_obj.scanout[i];
-
-        if (scanout->dmabuf &&
-            qemu_dmabuf_get_num_planes(scanout->dmabuf) > 0 &&
-            qemu_dmabuf_get_fds(scanout->dmabuf, NULL)[0] == res->dmabuf_fd &&
-            res->dmabuf_fd != -1) {
-            qemu_dmabuf_close(scanout->dmabuf);
-            res->dmabuf_fd = -1;
-            res->share_handle = SHAREABLE_NONE;
-        }
-    }
-
-    virtio_gpu_destroy_udmabuf(res);
-}
-
 static QemuDmaBuf *
 virtio_gpu_create_dmabuf(struct virtio_gpu_simple_resource *res,
                          struct virtio_gpu_framebuffer *fb,
                          struct virtio_gpu_rect *r)
 {
     uint32_t offset = 0;
+    int fd;
 
     if (res->dmabuf_fd < 0) {
         return NULL;
     }
 
+    fd = qemu_dup(res->dmabuf_fd);
+    if (fd < 0) {
+        return NULL;
+    }
+
     return qemu_dmabuf_new(r->width, r->height,
                            &offset, &fb->stride,
                            r->x, r->y, fb->width, fb->height,
                            qemu_pixman_to_drm_format(fb->format),
-                           DRM_FORMAT_MOD_INVALID, &res->dmabuf_fd,
+                           DRM_FORMAT_MOD_INVALID, &fd,
                            1, true, false);
 }
 
hw/display/virtio-gpu.c +1 -1
index 1b89d939cfd3..c1981efa5cab 100644
@@ -1077,7 +1077,7 @@ void virtio_gpu_cleanup_mapping(VirtIOGPU *g,
     res->addrs = NULL;
 
     if (res->blob) {
-        virtio_gpu_fini_udmabuf(g, res);
+        virtio_gpu_fini_udmabuf(res);
     }
 }
 
hw/vfio/display.c +0 -1
index cb83d98e9af6..89854eceb43c 100644
@@ -263,7 +263,6 @@ static void vfio_display_free_one_dmabuf(VFIODisplay *dpy, VFIODMABuf *dmabuf)
 {
     QTAILQ_REMOVE(&dpy->dmabuf.bufs, dmabuf, next);
 
-    qemu_dmabuf_close(dmabuf->buf);
     qemu_console_gl_release_dmabuf(dpy->con, dmabuf->buf);
     g_clear_pointer(&dmabuf->buf, qemu_dmabuf_free);
     g_free(dmabuf);
include/hw/virtio/virtio-gpu.h +1 -2
index ee5f6f2f2375..c2018b41056f 100644
@@ -390,8 +390,7 @@ bool virtio_gpu_scanout_blob_to_fb(struct virtio_gpu_framebuffer *fb,
 /* virtio-gpu-udmabuf.c */
 bool virtio_gpu_have_udmabuf(void);
 bool virtio_gpu_init_udmabuf(struct virtio_gpu_simple_resource *res);
-void virtio_gpu_fini_udmabuf(VirtIOGPU *g,
-                             struct virtio_gpu_simple_resource *res);
+void virtio_gpu_fini_udmabuf(struct virtio_gpu_simple_resource *res);
 int virtio_gpu_update_dmabuf(VirtIOGPU *g,
                              uint32_t scanout_id,
                              struct virtio_gpu_simple_resource *res,
include/ui/dmabuf.h +0 -1
index b972099859f2..968f3f9c8d48 100644
@@ -27,7 +27,6 @@ G_DEFINE_AUTOPTR_CLEANUP_FUNC(QemuDmaBuf, qemu_dmabuf_free);
 
 const int *qemu_dmabuf_get_fds(QemuDmaBuf *dmabuf, int *nfds);
 void qemu_dmabuf_dup_fds(QemuDmaBuf *dmabuf, int *fds, int nfds);
-void qemu_dmabuf_close(QemuDmaBuf *dmabuf);
 uint32_t qemu_dmabuf_get_width(QemuDmaBuf *dmabuf);
 uint32_t qemu_dmabuf_get_height(QemuDmaBuf *dmabuf);
 const uint32_t *qemu_dmabuf_get_offsets(QemuDmaBuf *dmabuf, int *noffsets);
ui/dbus-listener.c +0 -1
index 5a72c7eeae59..c1e86648384c 100644
@@ -631,7 +631,6 @@ static void dbus_scanout_texture(DisplayChangeListener *dcl,
     if (dbus_call_scanout_dmabuf(ddl, dmabuf)) {
         ddl->scanout_dmabuf = NULL;
     }
-    qemu_dmabuf_close(dmabuf);
 #endif
 
 #ifdef WIN32
ui/dmabuf.c +8 -14
index 16063dc62ae8..697d853ad847 100644
@@ -61,10 +61,18 @@ QemuDmaBuf *qemu_dmabuf_new(uint32_t width, uint32_t height,
 
 void qemu_dmabuf_free(QemuDmaBuf *dmabuf)
 {
+    int i;
+
     if (dmabuf == NULL) {
         return;
     }
 
+    for (i = 0; i < dmabuf->num_planes; i++) {
+        if (dmabuf->fd[i] >= 0) {
+            close(dmabuf->fd[i]);
+            dmabuf->fd[i] = -1;
+        }
+    }
     g_free(dmabuf);
 }
 
@@ -91,20 +99,6 @@ void qemu_dmabuf_dup_fds(QemuDmaBuf *dmabuf, int *fds, int nfds)
     }
 }
 
-void qemu_dmabuf_close(QemuDmaBuf *dmabuf)
-{
-    int i;
-
-    assert(dmabuf != NULL);
-
-    for (i = 0; i < dmabuf->num_planes; i++) {
-        if (dmabuf->fd[i] >= 0) {
-            close(dmabuf->fd[i]);
-            dmabuf->fd[i] = -1;
-        }
-    }
-}
-
 uint32_t qemu_dmabuf_get_width(QemuDmaBuf *dmabuf)
 {
     assert(dmabuf != NULL);
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
Reviewed-by on v2 12/25
#22 lore raw

Re: [PATCH v2 12/25] ui/dmabuf: own and close fds on free

Quoted 9 lines · Marc-André Lureau

Make qemu_dmabuf_free() call qemu_dmabuf_close(), so the dmabuf always owns and closes its fds. This removes the need for callers to explicitly close fds before freeing and simplify the code.

To support this, virtio_gpu_create_dmabuf() now dup()s the resource fd so each QemuDmaBuf has an independent copy. The scanout-matching loop in virtio_gpu_fini_udmabuf() is no longer needed and is removed.

Regards,
Akihiko Odaki

#23 lore raw

PATCH v2 13/25 ui: make GL context current before releasing DMA-BUF textures

In reply to Marc-André Lureau

The dpy_gl_release_dmabuf callbacks in the GTK, SDL, egl-headless, and spice backends call egl_dmabuf_release_texture(), which calls glDeleteTextures(), without first making the owning GL context current. This is reachable after another console's surface update leaves EGL_NO_CONTEXT current (e.g. gd_egl_update clears the context on return). The deletion then silently fails, leaking the GL texture and its backing storage. With an unshared context current, a coincident texture name could even be deleted from the wrong context.

Fix by making the appropriate GL context current in each backend's release callback before calling egl_dmabuf_release_texture():

- GTK EGL: eglMakeCurrent with the VC's EGL context
- GTK GL Area: gtk_gl_area_make_current
- SDL: SDL_GL_MakeCurrent with the console's window context
- egl-headless: eglMakeCurrent with the render-node context
- spice: eglMakeCurrent with the render-node context

For GTK, the previously shared gd_gl_release_dmabuf is split into backend-specific gd_egl_release_dmabuf and gd_gl_area_release_dmabuf, matching the existing pattern where each backend manages its own context.

---
 include/ui/gtk.h   |  5 +++++
 ui/egl-headless.c  |  2 ++
 ui/gtk-egl.c       | 12 ++++++++++++
 ui/gtk-gl-area.c   | 11 +++++++++++
 ui/gtk.c           | 13 ++++---------
 ui/sdl2-gl.c       |  1 +
 ui/spice-display.c |  2 ++
 7 files changed, 37 insertions(+), 9 deletions(-)
include/ui/gtk.h +5 -0
index b66b9ca5c519..6b62e9758813 100644
@@ -163,6 +163,7 @@ extern bool gtk_use_gl_area;
 void gd_update_windowsize(VirtualConsole *vc);
 void gd_update_monitor_refresh_rate(VirtualConsole *vc, GtkWidget *widget);
 void gd_gl_wait_sync(VirtualConsole *vc, void *sync);
+void gd_release_dmabuf(VirtualConsole *vc, QemuDmaBuf *dmabuf);
 
 /* ui/gtk-egl.c */
 void gd_egl_init(VirtualConsole *vc);
@@ -190,6 +191,8 @@ void gd_egl_cursor_dmabuf(DisplayChangeListener *dcl,
                           uint32_t hot_x, uint32_t hot_y);
 void gd_egl_cursor_position(DisplayChangeListener *dcl,
                             uint32_t pos_x, uint32_t pos_y);
+void gd_egl_release_dmabuf(DisplayChangeListener *dcl,
+                           QemuDmaBuf *dmabuf);
 void gd_egl_flush(DisplayChangeListener *dcl,
                   uint32_t x, uint32_t y, uint32_t w, uint32_t h);
 void *gd_egl_scanout_flush(DisplayChangeListener *dcl,
@@ -221,6 +224,8 @@ void gd_gl_area_scanout_texture(DisplayChangeListener *dcl,
                                 uint32_t x, uint32_t y,
                                 uint32_t w, uint32_t h,
                                 void *d3d_tex2d);
+void gd_gl_area_release_dmabuf(DisplayChangeListener *dcl,
+                               QemuDmaBuf *dmabuf);
 void gd_gl_area_scanout_disable(DisplayChangeListener *dcl);
 void gd_gl_area_scanout_flush(DisplayChangeListener *dcl,
                               uint32_t x, uint32_t y, uint32_t w, uint32_t h);
ui/egl-headless.c +2 -0
index ba27efcf4c3e..ffe7b30f919b 100644
@@ -126,6 +126,8 @@ static void egl_cursor_dmabuf(DisplayChangeListener *dcl,
 static void egl_release_dmabuf(DisplayChangeListener *dcl,
                                QemuDmaBuf *dmabuf)
 {
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
     egl_dmabuf_release_texture(dmabuf);
 }
 
ui/gtk-egl.c +12 -0
index 8623181336e9..d595916476d9 100644
@@ -426,6 +426,18 @@ void gtk_egl_init(DisplayGLMode mode)
     display_opengl = 1;
 }
 
+void gd_egl_release_dmabuf(DisplayChangeListener *dcl,
+                           QemuDmaBuf *dmabuf)
+{
+#ifdef CONFIG_GBM
+    VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
+
+    eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
+                   vc->gfx.esurface, vc->gfx.ectx);
+    gd_release_dmabuf(vc, dmabuf);
+#endif
+}
+
 int gd_egl_make_current(DisplayGLCtx *dgc,
                         QEMUGLContext ctx)
 {
ui/gtk-gl-area.c +11 -0
index 45c0c8c81a11..effd88f28721 100644
@@ -360,6 +360,17 @@ void gd_gl_area_scanout_dmabuf(DisplayChangeListener *dcl,
 #endif
 }
 
+void gd_gl_area_release_dmabuf(DisplayChangeListener *dcl,
+                               QemuDmaBuf *dmabuf)
+{
+#ifdef CONFIG_GBM
+    VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
+
+    gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
+    gd_release_dmabuf(vc, dmabuf);
+#endif
+}
+
 void gtk_gl_area_init(void)
 {
     display_opengl = 1;
ui/gtk.c +4 -9
index 55e45e8445bc..c615d35451b6 100644
@@ -584,21 +584,16 @@ static bool gd_has_dmabuf(DisplayChangeListener *dcl)
     return vc->gfx.has_dmabuf;
 }
 
-static void gd_gl_release_dmabuf(DisplayChangeListener *dcl,
-                                 QemuDmaBuf *dmabuf)
-{
 #ifdef CONFIG_GBM
-    VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
-
+void gd_release_dmabuf(VirtualConsole *vc, QemuDmaBuf *dmabuf)
+{
     egl_dmabuf_release_texture(dmabuf);
     if (vc->gfx.guest_fb.dmabuf == dmabuf) {
         vc->gfx.guest_fb.dmabuf = NULL;
         vc->gfx.draw_submitted = false;
     }
-#endif
 }
 
-#ifdef CONFIG_GBM
 static void gd_gl_fence_cb(void *vcon)
 {
     VirtualConsole *vc = vcon;
@@ -639,7 +634,7 @@ static const DisplayChangeListenerOps dcl_gl_area_ops = {
     .dpy_gl_scanout_disable  = gd_gl_area_scanout_disable,
     .dpy_gl_update           = gd_gl_area_scanout_flush,
     .dpy_gl_scanout_dmabuf   = gd_gl_area_scanout_dmabuf,
-    .dpy_gl_release_dmabuf   = gd_gl_release_dmabuf,
+    .dpy_gl_release_dmabuf   = gd_gl_area_release_dmabuf,
     .dpy_has_dmabuf          = gd_has_dmabuf,
 };
 
@@ -673,7 +668,7 @@ static const DisplayChangeListenerOps dcl_egl_ops = {
     .dpy_gl_cursor_dmabuf    = gd_egl_cursor_dmabuf,
     .dpy_gl_cursor_position  = gd_egl_cursor_position,
     .dpy_gl_update           = gd_egl_flush,
-    .dpy_gl_release_dmabuf   = gd_gl_release_dmabuf,
+    .dpy_gl_release_dmabuf   = gd_egl_release_dmabuf,
     .dpy_has_dmabuf          = gd_has_dmabuf,
 };
 
ui/sdl2-gl.c +1 -0
index 46eff768c968..3a5aa09618ca 100644
@@ -288,6 +288,7 @@ void sdl2_gl_release_dmabuf(DisplayChangeListener *dcl,
 {
     struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
 
+    SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
     egl_dmabuf_release_texture(dmabuf);
     if (scon->guest_fb.dmabuf == dmabuf) {
         scon->guest_fb.dmabuf = NULL;
ui/spice-display.c +2 -0
index 75c7df7bb5e0..22cd1e266caa 100644
@@ -1176,6 +1176,8 @@ static void qemu_spice_gl_release_dmabuf(DisplayChangeListener *dcl,
         ssd->guest_dmabuf = NULL;
         ssd->guest_dmabuf_refresh = false;
     }
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
     egl_dmabuf_release_texture(dmabuf);
 }
 
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
Reviewed-by on v2 13/25
#24 lore raw

Re: [PATCH v2 13/25] ui: make GL context current before releasing DMA-BUF textures

Quoted 18 lines · Marc-André Lureau

The dpy_gl_release_dmabuf callbacks in the GTK, SDL, egl-headless, and spice backends call egl_dmabuf_release_texture(), which calls glDeleteTextures(), without first making the owning GL context current. This is reachable after another console's surface update leaves EGL_NO_CONTEXT current (e.g. gd_egl_update clears the context on return). The deletion then silently fails, leaking the GL texture and its backing storage. With an unshared context current, a coincident texture name could even be deleted from the wrong context.

Fix by making the appropriate GL context current in each backend's release callback before calling egl_dmabuf_release_texture():

- GTK EGL: eglMakeCurrent with the VC's EGL context
- GTK GL Area: gtk_gl_area_make_current
- SDL: SDL_GL_MakeCurrent with the console's window context
- egl-headless: eglMakeCurrent with the render-node context
- spice: eglMakeCurrent with the render-node context

Quoted 6 lines

For GTK, the previously shared gd_gl_release_dmabuf is split into backend-specific gd_egl_release_dmabuf and gd_gl_area_release_dmabuf, matching the existing pattern where each backend manages its own context.

Regards,
Akihiko Odaki

#25 lore raw

PATCH v2 14/25 virtio-gpu: release the dmabuf when a scanout is disabled

In reply to Marc-André Lureau

Rather than waiting for the next set_scanout()/update_dmabuf() to replace it.

---
 hw/display/virtio-gpu.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)
hw/display/virtio-gpu.c +15 -4
index c1981efa5cab..95c905b4fec5 100644
@@ -418,20 +418,31 @@ static void virtio_gpu_resource_create_blob(VirtIOGPU *g,
     QTAILQ_INSERT_HEAD(&g->reslist, res, next);
 }
 
+static void virtio_gpu_release_scanout_dmabuf(VirtIOGPU *g, int scanout_id)
+{
+    struct virtio_gpu_scanout *scanout = &g->parent_obj.scanout[scanout_id];
+    g_autoptr(QemuDmaBuf) dmabuf = scanout->dmabuf;
+
+    if (!dmabuf) {
+        return;
+    }
+
+    scanout->dmabuf = NULL;
+    qemu_console_gl_release_dmabuf(scanout->con, dmabuf);
+}
+
 void virtio_gpu_disable_scanout(VirtIOGPU *g, int scanout_id)
 {
     struct virtio_gpu_scanout *scanout = &g->parent_obj.scanout[scanout_id];
     struct virtio_gpu_simple_resource *res;
 
-    if (scanout->resource_id == 0) {
-        return;
-    }
-
     res = virtio_gpu_find_resource(g, scanout->resource_id);
     if (res) {
         res->scanout_bitmask &= ~(1 << scanout_id);
     }
 
+    virtio_gpu_release_scanout_dmabuf(g, scanout_id);
+
     qemu_console_set_surface(scanout->con, NULL);
     scanout->resource_id = 0;
     scanout->ds = NULL;
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
#27 lore raw

PATCH v2 15/25 contrib/vhost-user-gpu: drop scanout_bitmask bookkeeping

In reply to Marc-André Lureau

The per-resource scanout_bitmask is a reverse index of which scanouts reference a given resource. The information is already available from scanout->resource_id. Performance impact should be negligible.

---
 contrib/vhost-user-gpu/vhost-user-gpu.c | 28 +++++++---------------------
 1 file changed, 7 insertions(+), 21 deletions(-)
contrib/vhost-user-gpu/vhost-user-gpu.c +7 -21
index 933bdbb671c0..f70b34c179f3 100644
@@ -36,7 +36,6 @@ struct virtio_gpu_simple_resource {
     uint32_t format;
     struct iovec *iov;
     unsigned int iov_cnt;
-    uint32_t scanout_bitmask;
     pixman_image_t *image;
     struct vugbm_buffer buffer;
     QTAILQ_ENTRY(virtio_gpu_simple_resource) next;
@@ -416,17 +415,12 @@ static void
 vg_disable_scanout(VuGpu *g, int scanout_id)
 {
     struct virtio_gpu_scanout *scanout = &g->scanout[scanout_id];
-    struct virtio_gpu_simple_resource *res;
 
     if (scanout->resource_id == 0) {
         return;
     }
 
-    res = virtio_gpu_find_resource(g, scanout->resource_id);
-    if (res) {
-        res->scanout_bitmask &= ~(1 << scanout_id);
-    }
-
+    scanout->resource_id = 0;
     scanout->width = 0;
     scanout->height = 0;
 
@@ -446,11 +440,9 @@ vg_resource_destroy(VuGpu *g,
 {
     int i;
 
-    if (res->scanout_bitmask) {
-        for (i = 0; i < VIRTIO_GPU_MAX_SCANOUTS; i++) {
-            if (res->scanout_bitmask & (1 << i)) {
-                vg_disable_scanout(g, i);
-            }
+    for (i = 0; i < VIRTIO_GPU_MAX_SCANOUTS; i++) {
+        if (g->scanout[i].resource_id == res->resource_id) {
+            vg_disable_scanout(g, i);
         }
     }
 
@@ -662,7 +654,7 @@ static void
 vg_set_scanout(VuGpu *g,
                struct virtio_gpu_ctrl_command *cmd)
 {
-    struct virtio_gpu_simple_resource *res, *ores;
+    struct virtio_gpu_simple_resource *res;
     struct virtio_gpu_scanout *scanout;
     struct virtio_gpu_set_scanout ss;
     int fd;
@@ -707,12 +699,6 @@ vg_set_scanout(VuGpu *g,
 
     scanout = &g->scanout[ss.scanout_id];
 
-    ores = virtio_gpu_find_resource(g, scanout->resource_id);
-    if (ores) {
-        ores->scanout_bitmask &= ~(1 << ss.scanout_id);
-    }
-
-    res->scanout_bitmask |= (1 << ss.scanout_id);
     scanout->resource_id = ss.resource_id;
     scanout->x = ss.r.x;
     scanout->y = ss.r.y;
@@ -797,10 +783,10 @@ vg_resource_flush(VuGpu *g,
         pixman_region16_t region, finalregion;
         pixman_box16_t *extents;
 
-        if (!(res->scanout_bitmask & (1 << i))) {
+        scanout = &g->scanout[i];
+        if (scanout->resource_id != res->resource_id) {
             continue;
         }
-        scanout = &g->scanout[i];
 
         pixman_region_init(&finalregion);
         pixman_region_init_rect(&region, scanout->x, scanout->y,
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
Reviewed-by on v2 15/25
#28 lore raw

Re: [PATCH v2 15/25] contrib/vhost-user-gpu: drop scanout_bitmask bookkeeping

Quoted 6 lines · Marc-André Lureau

The per-resource scanout_bitmask is a reverse index of which scanouts reference a given resource. The information is already available from scanout->resource_id. Performance impact should be negligible.

Regards,
Akihiko Odaki

#29 lore raw

PATCH v2 16/25 virtio-gpu-rutabaga: fix scanout handling

In reply to Marc-André Lureau

The rutabaga backend was misusing the scanout_bitmask field as a plain scanout ID instead of a bitmask.

Fix the scanout lookup to use scanout->resource_id directly. Also fix the missing resource_id clear when disabling a scanout, and flush all matching scanouts instead of just the first one.

Fixes 1dcc6adbc168 ("gfxstream + rutabaga: add initial support for gfxstream")
---
 hw/display/virtio-gpu-rutabaga.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)
hw/display/virtio-gpu-rutabaga.c +17 -3
index b8f8a2068d0b..8a82e96b7461 100644
@@ -146,8 +146,15 @@ virtio_gpu_rutabaga_resource_unref(VirtIOGPU *g,
                                    Error **errp)
 {
     int32_t result;
+    int i;
     VirtIOGPURutabaga *vr = VIRTIO_GPU_RUTABAGA(g);
 
+    for (i = 0; i < g->parent_obj.conf.max_outputs; i++) {
+        if (g->parent_obj.scanout[i].resource_id == res->resource_id) {
+            virtio_gpu_disable_scanout(g, i);
+        }
+    }
+
     result = rutabaga_resource_unref(vr->rutabaga, res->resource_id);
     if (result) {
         error_setg_errno(errp,
@@ -251,7 +258,7 @@ rutabaga_cmd_resource_flush(VirtIOGPU *g, struct virtio_gpu_ctrl_command *cmd)
 
     for (i = 0; i < vb->conf.max_outputs; i++) {
         scanout = &vb->scanout[i];
-        if (i == res->scanout_bitmask) {
+        if (scanout->resource_id == res->resource_id) {
             found = true;
             break;
         }
@@ -275,7 +282,13 @@ rutabaga_cmd_resource_flush(VirtIOGPU *g, struct virtio_gpu_ctrl_command *cmd)
                                              rf.resource_id, &transfer,
                                              &transfer_iovec);
     CHECK(!result, cmd);
-    qemu_console_update_full(scanout->con);
+
+    for (i = 0; i < vb->conf.max_outputs; i++) {
+        scanout = &vb->scanout[i];
+        if (scanout->resource_id == res->resource_id) {
+            qemu_console_update_full(scanout->con);
+        }
+    }
 }
 
 static void
@@ -299,6 +312,7 @@ rutabaga_cmd_set_scanout(VirtIOGPU *g, struct virtio_gpu_ctrl_command *cmd)
     scanout = &vb->scanout[ss.scanout_id];
 
     if (ss.resource_id == 0) {
+        scanout->resource_id = 0;
         qemu_console_set_surface(scanout->con, NULL);
         qemu_console_gl_scanout_disable(scanout->con);
         return;
@@ -332,7 +346,7 @@ rutabaga_cmd_set_scanout(VirtIOGPU *g, struct virtio_gpu_ctrl_command *cmd)
     scanout->ds = qemu_create_displaysurface_pixman(res->image);
     qemu_console_set_surface(scanout->con, NULL);
     qemu_console_set_surface(scanout->con, scanout->ds);
-    res->scanout_bitmask = ss.scanout_id;
+    scanout->resource_id = ss.resource_id;
 }
 
 static void
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
Reviewed-by on v2 16/25
#30 lore raw

Re: [PATCH v2 16/25] virtio-gpu-rutabaga: fix scanout handling

Quoted 9 lines · Marc-André Lureau

The rutabaga backend was misusing the scanout_bitmask field as a plain scanout ID instead of a bitmask.

Fix the scanout lookup to use scanout->resource_id directly. Also fix the missing resource_id clear when disabling a scanout, and flush all matching scanouts instead of just the first one.

Fixes 1dcc6adbc168 ("gfxstream + rutabaga: add initial support for gfxstream")

Regards,
Akihiko Odaki

#31 lore raw

PATCH v2 17/25 virtio-gpu: drop scanout_bitmask bookkeeping

In reply to Marc-André Lureau

The per-resource scanout_bitmask is a reverse index of which scanouts reference a given resource. The information is already available from scanout->resource_id. Performance impact should be negligible.

---
 hw/display/virtio-gpu.c        | 26 +++++---------------------
 include/hw/virtio/virtio-gpu.h |  1 -
 2 files changed, 5 insertions(+), 22 deletions(-)
hw/display/virtio-gpu.c +5 -21
index 95c905b4fec5..b244afb69e4e 100644
@@ -434,12 +434,6 @@ static void virtio_gpu_release_scanout_dmabuf(VirtIOGPU *g, int scanout_id)
 void virtio_gpu_disable_scanout(VirtIOGPU *g, int scanout_id)
 {
     struct virtio_gpu_scanout *scanout = &g->parent_obj.scanout[scanout_id];
-    struct virtio_gpu_simple_resource *res;
-
-    res = virtio_gpu_find_resource(g, scanout->resource_id);
-    if (res) {
-        res->scanout_bitmask &= ~(1 << scanout_id);
-    }
 
     virtio_gpu_release_scanout_dmabuf(g, scanout_id);
 
@@ -456,11 +450,9 @@ static void virtio_gpu_resource_destroy(VirtIOGPU *g,
 {
     int i;
 
-    if (res->scanout_bitmask) {
-        for (i = 0; i < g->parent_obj.conf.max_outputs; i++) {
-            if (res->scanout_bitmask & (1 << i)) {
-                virtio_gpu_disable_scanout(g, i);
-            }
+    for (i = 0; i < g->parent_obj.conf.max_outputs; i++) {
+        if (g->parent_obj.scanout[i].resource_id == res->resource_id) {
+            virtio_gpu_disable_scanout(g, i);
         }
     }
 
@@ -639,10 +631,10 @@ static void virtio_gpu_resource_flush(VirtIOGPU *g,
     for (i = 0; i < g->parent_obj.conf.max_outputs; i++) {
         QemuRect rect;
 
-        if (!(res->scanout_bitmask & (1 << i))) {
+        scanout = &g->parent_obj.scanout[i];
+        if (scanout->resource_id != res->resource_id) {
             continue;
         }
-        scanout = &g->parent_obj.scanout[i];
 
         qemu_rect_init(&rect, scanout->x, scanout->y,
                        scanout->width, scanout->height);
@@ -667,16 +659,9 @@ void virtio_gpu_update_scanout(VirtIOGPU *g,
                                struct virtio_gpu_framebuffer *fb,
                                struct virtio_gpu_rect *r)
 {
-    struct virtio_gpu_simple_resource *ores;
     struct virtio_gpu_scanout *scanout;
 
     scanout = &g->parent_obj.scanout[scanout_id];
-    ores = virtio_gpu_find_resource(g, scanout->resource_id);
-    if (ores) {
-        ores->scanout_bitmask &= ~(1 << scanout_id);
-    }
-
-    res->scanout_bitmask |= (1 << scanout_id);
     scanout->resource_id = res->resource_id;
     scanout->x = r->x;
     scanout->y = r->y;
@@ -1707,7 +1692,6 @@ static int virtio_gpu_post_load(void *opaque, int version_id)
         if (scanout->cursor.resource_id) {
             update_cursor(g, &scanout->cursor);
         }
-        res->scanout_bitmask |= (1 << i);
     }
 
     return 0;
include/hw/virtio/virtio-gpu.h +0 -1
index c2018b41056f..071cbbb6113e 100644
@@ -51,7 +51,6 @@ struct virtio_gpu_simple_resource {
     uint64_t *addrs;
     struct iovec *iov;
     unsigned int iov_cnt;
-    uint32_t scanout_bitmask;
     pixman_image_t *image;
     qemu_pixman_shareable share_handle;
     uint64_t hostmem;
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
Reviewed-by on v2 17/25
#32 lore raw

Re: [PATCH v2 17/25] virtio-gpu: drop scanout_bitmask bookkeeping

Quoted 6 lines · Marc-André Lureau

The per-resource scanout_bitmask is a reverse index of which scanouts reference a given resource. The information is already available from scanout->resource_id. Performance impact should be negligible.

Regards,
Akihiko Odaki

#33 lore raw

PATCH v2 18/25 virtio-gpu: release dmabuf when switching to a surface

In reply to Marc-André Lureau

A scanout may hold an active QemuDmaBuf when a SET_SCANOUT switches to CPU-backed display data. Force surface recreation and release the old buffer in this case.

Fixes 32db3c63ae11 ("virtio-gpu: Add virtio_gpu_set_scanout_blob")
---
 hw/display/virtio-gpu.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
hw/display/virtio-gpu.c +9 -4
index b244afb69e4e..5c3cd2fb4616 100644
@@ -704,9 +704,11 @@ static bool virtio_gpu_do_set_scanout(VirtIOGPU *g,
 {
     struct virtio_gpu_scanout *scanout;
     uint32_t bytes_pp = virtio_gpu_format_bytes_pp(fb->format);
+    bool was_dmabuf;
     uint8_t *data;
 
     scanout = &g->parent_obj.scanout[scanout_id];
+    was_dmabuf = scanout->dmabuf != NULL;
 
     if (!virtio_gpu_check_scanout_bounds(scanout_id, res->resource_id,
                                          fb->width, fb->height, r, error)) {
@@ -733,12 +735,11 @@ static bool virtio_gpu_do_set_scanout(VirtIOGPU *g,
 
     if (res->blob) {
         if (qemu_console_has_gl(scanout->con)) {
-            if (!virtio_gpu_update_dmabuf(g, scanout_id, res, fb, r)) {
-                virtio_gpu_update_scanout(g, scanout_id, res, fb, r);
-            } else {
+            if (virtio_gpu_update_dmabuf(g, scanout_id, res, fb, r)) {
                 *error = VIRTIO_GPU_RESP_ERR_OUT_OF_MEMORY;
                 return false;
             }
+            virtio_gpu_update_scanout(g, scanout_id, res, fb, r);
             return true;
         }
 
@@ -748,7 +749,8 @@ static bool virtio_gpu_do_set_scanout(VirtIOGPU *g,
     }
 
     /* create a surface for this scanout */
-    if ((res->blob && !qemu_console_has_gl(scanout->con)) ||
+    if (was_dmabuf ||
+        (res->blob && !qemu_console_has_gl(scanout->con)) ||
         !scanout->ds ||
         surface_data(scanout->ds) != data + fb->offset ||
         scanout->width != r->width ||
@@ -773,6 +775,9 @@ static bool virtio_gpu_do_set_scanout(VirtIOGPU *g,
         qemu_displaysurface_set_share_handle(scanout->ds, res->share_handle, fb->offset);
 
         pixman_image_unref(rect);
+        if (was_dmabuf) {
+            virtio_gpu_release_scanout_dmabuf(g, scanout_id);
+        }
         qemu_console_set_surface(g->parent_obj.scanout[scanout_id].con,
                                 scanout->ds);
     }
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
Reviewed-by on v2 18/25
#34 lore raw

Re: [PATCH v2 18/25] virtio-gpu: release dmabuf when switching to a surface

Quoted 6 lines · Marc-André Lureau

A scanout may hold an active QemuDmaBuf when a SET_SCANOUT switches to CPU-backed display data. Force surface recreation and release the old buffer in this case.

Fixes 32db3c63ae11 ("virtio-gpu: Add virtio_gpu_set_scanout_blob")

Regards,
Akihiko Odaki

#35 lore raw

PATCH v2 19/25 virtio-gpu/virgl: release dmabuf when a texture replaces a scanout

In reply to Marc-André Lureau

Release the scanout dmabuf before installing the texture. A full virtio_gpu_disable_scanout() is not used because the scanout is reassigned immediately afterwards.

Fixes 7c092f17ccee ("virtio-gpu: Handle resource blob commands")
---
 hw/display/virtio-gpu-virgl.c  | 3 +++
 hw/display/virtio-gpu.c        | 2 +-
 include/hw/virtio/virtio-gpu.h | 2 ++
 3 files changed, 6 insertions(+), 1 deletion(-)
hw/display/virtio-gpu-virgl.c +3 -0
index 7f1d1e0c3276..79d4c0b569fb 100644
@@ -605,6 +605,9 @@ static void virgl_cmd_set_scanout(VirtIOGPU *g,
                                              &cmd->error)) {
             return;
         }
+
+        virtio_gpu_release_scanout_dmabuf(g, ss.scanout_id);
+
         qemu_console_resize(g->parent_obj.scanout[ss.scanout_id].con,
                             ss.r.width, ss.r.height);
         virgl_renderer_force_ctx_0();
hw/display/virtio-gpu.c +1 -1
index 5c3cd2fb4616..ae1f87ad68b2 100644
@@ -418,7 +418,7 @@ static void virtio_gpu_resource_create_blob(VirtIOGPU *g,
     QTAILQ_INSERT_HEAD(&g->reslist, res, next);
 }
 
-static void virtio_gpu_release_scanout_dmabuf(VirtIOGPU *g, int scanout_id)
+void virtio_gpu_release_scanout_dmabuf(VirtIOGPU *g, int scanout_id)
 {
     struct virtio_gpu_scanout *scanout = &g->parent_obj.scanout[scanout_id];
     g_autoptr(QemuDmaBuf) dmabuf = scanout->dmabuf;
include/hw/virtio/virtio-gpu.h +2 -0
index 071cbbb6113e..2f18d839ccd3 100644
@@ -371,6 +371,8 @@ bool virtio_gpu_check_scanout_bounds(uint32_t scanout_id, uint32_t resource_id,
                                      const struct virtio_gpu_rect *r,
                                      uint32_t *error);
 
+void virtio_gpu_release_scanout_dmabuf(VirtIOGPU *g, int scanout_id);
+
 /**
  * virtio_gpu_scanout_blob_to_fb() - fill out fb based on scanout data
  * fb: the frame-buffer descriptor to fill out
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
Reviewed-by on v2 19/25
#36 lore raw

Re: [PATCH v2 19/25] virtio-gpu/virgl: release dmabuf when a texture replaces a scanout

Quoted 6 lines · Marc-André Lureau

Release the scanout dmabuf before installing the texture. A full virtio_gpu_disable_scanout() is not used because the scanout is reassigned immediately afterwards.

Fixes 7c092f17ccee ("virtio-gpu: Handle resource blob commands")

Regards,
Akihiko Odaki

#37 lore raw

PATCH v2 20/25 virtio-gpu: consolidate disabling scanout

In reply to Marc-André Lureau

Use the common virtio_gpu_disable_scanout() to reset the scanout state, including GL state if any.

Fixes 7c092f17ccee ("virtio-gpu: Handle resource blob commands")
---
 hw/display/virtio-gpu-rutabaga.c | 4 +---
 hw/display/virtio-gpu-virgl.c    | 6 ++----
 hw/display/virtio-gpu.c          | 7 +++++--
 3 files changed, 8 insertions(+), 9 deletions(-)
hw/display/virtio-gpu-rutabaga.c +1 -3
index 8a82e96b7461..8697dd6936ba 100644
@@ -312,9 +312,7 @@ rutabaga_cmd_set_scanout(VirtIOGPU *g, struct virtio_gpu_ctrl_command *cmd)
     scanout = &vb->scanout[ss.scanout_id];
 
     if (ss.resource_id == 0) {
-        scanout->resource_id = 0;
-        qemu_console_set_surface(scanout->con, NULL);
-        qemu_console_gl_scanout_disable(scanout->con);
+        virtio_gpu_disable_scanout(g, ss.scanout_id);
         return;
     }
 
hw/display/virtio-gpu-virgl.c +2 -4
index 79d4c0b569fb..87d1fc3d5f65 100644
@@ -618,8 +618,7 @@ static void virgl_cmd_set_scanout(VirtIOGPU *g,
             ss.r.x, ss.r.y, ss.r.width, ss.r.height,
             d3d_tex2d);
     } else {
-        qemu_console_set_surface(g->parent_obj.scanout[ss.scanout_id].con, NULL);
-        qemu_console_gl_scanout_disable(g->parent_obj.scanout[ss.scanout_id].con);
+        virtio_gpu_disable_scanout(g, ss.scanout_id);
     }
     g->parent_obj.scanout[ss.scanout_id].resource_id = ss.resource_id;
 }
@@ -1426,8 +1425,7 @@ void virtio_gpu_virgl_reset_scanout(VirtIOGPU *g)
     int i;
 
     for (i = 0; i < g->parent_obj.conf.max_outputs; i++) {
-        qemu_console_set_surface(g->parent_obj.scanout[i].con, NULL);
-        qemu_console_gl_scanout_disable(g->parent_obj.scanout[i].con);
+        virtio_gpu_disable_scanout(g, i);
     }
 }
 
hw/display/virtio-gpu.c +5 -2
index ae1f87ad68b2..cb096cb349cd 100644
@@ -435,8 +435,11 @@ void virtio_gpu_disable_scanout(VirtIOGPU *g, int scanout_id)
 {
     struct virtio_gpu_scanout *scanout = &g->parent_obj.scanout[scanout_id];
 
-    virtio_gpu_release_scanout_dmabuf(g, scanout_id);
-
+    if (scanout->dmabuf) {
+        virtio_gpu_release_scanout_dmabuf(g, scanout_id);
+    } else {
+        qemu_console_gl_scanout_disable(scanout->con);
+    }
     qemu_console_set_surface(scanout->con, NULL);
     scanout->resource_id = 0;
     scanout->ds = NULL;
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
#38 lore raw

Re: [PATCH v2 20/25] virtio-gpu: consolidate disabling scanout

Quoted 55 lines · Marc-André Lureau

Use the common virtio_gpu_disable_scanout() to reset the scanout state, including GL state if any.

Fixes 7c092f17ccee ("virtio-gpu: Handle resource blob commands")
---
  hw/display/virtio-gpu-rutabaga.c | 4 +---
  hw/display/virtio-gpu-virgl.c    | 6 ++----
  hw/display/virtio-gpu.c          | 7 +++++--
  3 files changed, 8 insertions(+), 9 deletions(-)
hw/display/virtio-gpu-rutabaga.c +1 -3
index 8a82e96b7461..8697dd6936ba 100644
@@ -312,9 +312,7 @@ rutabaga_cmd_set_scanout(VirtIOGPU *g, struct virtio_gpu_ctrl_command *cmd)
      scanout = &vb->scanout[ss.scanout_id];
 
      if (ss.resource_id == 0) {
-        scanout->resource_id = 0;
-        qemu_console_set_surface(scanout->con, NULL);
-        qemu_console_gl_scanout_disable(scanout->con);
+        virtio_gpu_disable_scanout(g, ss.scanout_id);
          return;
      }
 
hw/display/virtio-gpu-virgl.c +2 -4
index 79d4c0b569fb..87d1fc3d5f65 100644
@@ -618,8 +618,7 @@ static void virgl_cmd_set_scanout(VirtIOGPU *g,
              ss.r.x, ss.r.y, ss.r.width, ss.r.height,
              d3d_tex2d);
      } else {
-        qemu_console_set_surface(g->parent_obj.scanout[ss.scanout_id].con, NULL);
-        qemu_console_gl_scanout_disable(g->parent_obj.scanout[ss.scanout_id].con);
+        virtio_gpu_disable_scanout(g, ss.scanout_id);
      }
      g->parent_obj.scanout[ss.scanout_id].resource_id = ss.resource_id;
  }
@@ -1426,8 +1425,7 @@ void virtio_gpu_virgl_reset_scanout(VirtIOGPU *g)
      int i;
 
      for (i = 0; i < g->parent_obj.conf.max_outputs; i++) {
-        qemu_console_set_surface(g->parent_obj.scanout[i].con, NULL);
-        qemu_console_gl_scanout_disable(g->parent_obj.scanout[i].con);
+        virtio_gpu_disable_scanout(g, i);
      }
  }
 
hw/display/virtio-gpu.c +0 -0
index ae1f87ad68b2..cb096cb349cd 100644

hw/display/virtio-gpu.c void virtio_gpu_disable_scanout(VirtIOGPU *g, int scanout_id) · line 442

Quoted 10 lines
hw/display/virtio-gpu.c +5 -2
@@ -435,8 +435,11 @@ void virtio_gpu_disable_scanout(VirtIOGPU *g, int scanout_id)
  {
      struct virtio_gpu_scanout *scanout = &g->parent_obj.scanout[scanout_id];
 
-    virtio_gpu_release_scanout_dmabuf(g, scanout_id);
-
+    if (scanout->dmabuf) {
+        virtio_gpu_release_scanout_dmabuf(g, scanout_id);
+    } else {
+        qemu_console_gl_scanout_disable(scanout->con);
+    }

GL needs to be always disabled when clearing a scanout.

Having scanout->dmabuf suppresses the explicit GL-disable callback. This misses a real reset path:
1. Reset a Virgl device displaying a hostmem-mapped blob.
2. virtio_gpu_virgl_resource_unref() starts asynchronous unmapping and
returns before disabling its scanouts.
3. virtio_gpu_reset_bh() installs placeholder surfaces, changing the
console to SCANOUT_SURFACE; virtio_gpu_base_reset() clears the
resource IDs.
4. virtio_gpu_gl_reset() calls virtio_gpu_virgl_reset_scanout(). The new
helper releases the retained DMA-BUF, but
qemu_console_gl_release_dmabuf() invokes GL-disable only for
SCANOUT_DMABUF.
5. GTK’s release callback clears the DMA-BUF pointer but leaves
scanout_mode and its framebuffer. The queued redraw continues through
the old framebuffer path instead of displaying the placeholder, blit
in gd_gl_area_draw().

Before this patch, virtio_gpu_virgl_reset_scanout() always called GL-disable. Please ensure the disable helper actually disables the frontend even when releasing a wrapper whose console has already switched to a surface.

Regards,
Akihiko Odaki

Quoted 4 lines
hw/display/virtio-gpu.c +0 -0
      qemu_console_set_surface(scanout->con, NULL);
      scanout->resource_id = 0;
      scanout->ds = NULL;
#39 lore raw

Re: [PATCH v2 20/25] virtio-gpu: consolidate disabling scanout

Hi

Quoted 60 lines · Akihiko Odaki
Quoted 58 lines · Marc-André Lureau

Use the common virtio_gpu_disable_scanout() to reset the scanout state, including GL state if any.

Fixes 7c092f17ccee ("virtio-gpu: Handle resource blob commands")
---
  hw/display/virtio-gpu-rutabaga.c | 4 +---
  hw/display/virtio-gpu-virgl.c    | 6 ++----
  hw/display/virtio-gpu.c          | 7 +++++--
  3 files changed, 8 insertions(+), 9 deletions(-)
hw/display/virtio-gpu-rutabaga.c +1 -3
index 8a82e96b7461..8697dd6936ba 100644
@@ -312,9 +312,7 @@ rutabaga_cmd_set_scanout(VirtIOGPU *g, struct virtio_gpu_ctrl_command *cmd)
      scanout = &vb->scanout[ss.scanout_id];
 
      if (ss.resource_id == 0) {
-        scanout->resource_id = 0;
-        qemu_console_set_surface(scanout->con, NULL);
-        qemu_console_gl_scanout_disable(scanout->con);
+        virtio_gpu_disable_scanout(g, ss.scanout_id);
          return;
      }
 
hw/display/virtio-gpu-virgl.c +2 -4
index 79d4c0b569fb..87d1fc3d5f65 100644
@@ -618,8 +618,7 @@ static void virgl_cmd_set_scanout(VirtIOGPU *g,
              ss.r.x, ss.r.y, ss.r.width, ss.r.height,
              d3d_tex2d);
      } else {
-        qemu_console_set_surface(g->parent_obj.scanout[ss.scanout_id].con, NULL);
-        qemu_console_gl_scanout_disable(g->parent_obj.scanout[ss.scanout_id].con);
+        virtio_gpu_disable_scanout(g, ss.scanout_id);
      }
      g->parent_obj.scanout[ss.scanout_id].resource_id = ss.resource_id;
  }
@@ -1426,8 +1425,7 @@ void virtio_gpu_virgl_reset_scanout(VirtIOGPU *g)
      int i;
 
      for (i = 0; i < g->parent_obj.conf.max_outputs; i++) {
-        qemu_console_set_surface(g->parent_obj.scanout[i].con, NULL);
-        qemu_console_gl_scanout_disable(g->parent_obj.scanout[i].con);
+        virtio_gpu_disable_scanout(g, i);
      }
  }
 
hw/display/virtio-gpu.c +0 -0
index ae1f87ad68b2..cb096cb349cd 100644
@@ -435,8 +435,11 @@ void virtio_gpu_disable_scanout(VirtIOGPU *g, int scanout_id)
  {
      struct virtio_gpu_scanout *scanout = &g->parent_obj.scanout[scanout_id];
 
Quoted 10 lines

hw/display/virtio-gpu.c void virtio_gpu_disable_scanout(VirtIOGPU *g, int scanout_id) · line 442

Quoted 7 lines
hw/display/virtio-gpu.c +5 -2
-    virtio_gpu_release_scanout_dmabuf(g, scanout_id);
-
+    if (scanout->dmabuf) {
+        virtio_gpu_release_scanout_dmabuf(g, scanout_id);
+    } else {
+        qemu_console_gl_scanout_disable(scanout->con);
+    }

GL needs to be always disabled when clearing a scanout.

Ok, I removed the else condition.

Are you going to review the remaining 1-23 patches? thanks

Quoted 31 lines

Having scanout->dmabuf suppresses the explicit GL-disable callback. This misses a real reset path:
1. Reset a Virgl device displaying a hostmem-mapped blob.
2. virtio_gpu_virgl_resource_unref() starts asynchronous unmapping and returns before disabling its scanouts.
3. virtio_gpu_reset_bh() installs placeholder surfaces, changing the console to SCANOUT_SURFACE; virtio_gpu_base_reset() clears the resource IDs.
4. virtio_gpu_gl_reset() calls virtio_gpu_virgl_reset_scanout(). The new helper releases the retained DMA-BUF, but qemu_console_gl_release_dmabuf() invokes GL-disable only for SCANOUT_DMABUF.
5. GTK’s release callback clears the DMA-BUF pointer but leaves scanout_mode and its framebuffer. The queued redraw continues through the old framebuffer path instead of displaying the placeholder, blit in gd_gl_area_draw().

Before this patch, virtio_gpu_virgl_reset_scanout() always called GL-disable. Please ensure the disable helper actually disables the frontend even when releasing a wrapper whose console has already switched to a surface.

Regards,
Akihiko Odaki

Quoted 4 lines
hw/display/virtio-gpu.c +0 -0
      qemu_console_set_surface(scanout->con, NULL);
      scanout->resource_id = 0;
      scanout->ds = NULL;
#40 lore raw

Re: [PATCH v2 20/25] virtio-gpu: consolidate disabling scanout

Quoted 75 lines · Marc-André Lureau

Hi

Quoted 60 lines · Akihiko Odaki
Quoted 58 lines · Marc-André Lureau

Use the common virtio_gpu_disable_scanout() to reset the scanout state, including GL state if any.

Fixes 7c092f17ccee ("virtio-gpu: Handle resource blob commands")
---
   hw/display/virtio-gpu-rutabaga.c | 4 +---
   hw/display/virtio-gpu-virgl.c    | 6 ++----
   hw/display/virtio-gpu.c          | 7 +++++--
   3 files changed, 8 insertions(+), 9 deletions(-)
hw/display/virtio-gpu-rutabaga.c +1 -3
index 8a82e96b7461..8697dd6936ba 100644
@@ -312,9 +312,7 @@ rutabaga_cmd_set_scanout(VirtIOGPU *g, struct virtio_gpu_ctrl_command *cmd)
       scanout = &vb->scanout[ss.scanout_id];
 
       if (ss.resource_id == 0) {
-        scanout->resource_id = 0;
-        qemu_console_set_surface(scanout->con, NULL);
-        qemu_console_gl_scanout_disable(scanout->con);
+        virtio_gpu_disable_scanout(g, ss.scanout_id);
           return;
       }
 
hw/display/virtio-gpu-virgl.c +2 -4
index 79d4c0b569fb..87d1fc3d5f65 100644
@@ -618,8 +618,7 @@ static void virgl_cmd_set_scanout(VirtIOGPU *g,
               ss.r.x, ss.r.y, ss.r.width, ss.r.height,
               d3d_tex2d);
       } else {
-        qemu_console_set_surface(g->parent_obj.scanout[ss.scanout_id].con, NULL);
-        qemu_console_gl_scanout_disable(g->parent_obj.scanout[ss.scanout_id].con);
+        virtio_gpu_disable_scanout(g, ss.scanout_id);
       }
       g->parent_obj.scanout[ss.scanout_id].resource_id = ss.resource_id;
   }
@@ -1426,8 +1425,7 @@ void virtio_gpu_virgl_reset_scanout(VirtIOGPU *g)
       int i;
 
       for (i = 0; i < g->parent_obj.conf.max_outputs; i++) {
-        qemu_console_set_surface(g->parent_obj.scanout[i].con, NULL);
-        qemu_console_gl_scanout_disable(g->parent_obj.scanout[i].con);
+        virtio_gpu_disable_scanout(g, i);
       }
   }
 
hw/display/virtio-gpu.c +0 -0
index ae1f87ad68b2..cb096cb349cd 100644
@@ -435,8 +435,11 @@ void virtio_gpu_disable_scanout(VirtIOGPU *g, int scanout_id)
   {
       struct virtio_gpu_scanout *scanout = &g->parent_obj.scanout[scanout_id];
 
Quoted 10 lines

hw/display/virtio-gpu.c void virtio_gpu_disable_scanout(VirtIOGPU *g, int scanout_id) · line 442

Quoted 7 lines
hw/display/virtio-gpu.c +5 -2
-    virtio_gpu_release_scanout_dmabuf(g, scanout_id);
-
+    if (scanout->dmabuf) {
+        virtio_gpu_release_scanout_dmabuf(g, scanout_id);
+    } else {
+        qemu_console_gl_scanout_disable(scanout->con);
+    }

GL needs to be always disabled when clearing a scanout.

Quoted 4 lines

Ok, I removed the else condition.

Are you going to review the remaining 1-23 patches? thanks

I reviewed them all. I have comments for "[PATCH v2 02/25] virtio-gpu: fix use-after-free on blob cursor after detach". The other patches look good to me.

Regards,
Akihiko Odaki

#41 lore raw

Re: [PATCH v2 20/25] virtio-gpu: consolidate disabling scanout

Hi

Quoted 77 lines · Akihiko Odaki
Quoted 75 lines · Marc-André Lureau

Hi

Quoted 70 lines · Akihiko Odaki
Quoted 55 lines · Marc-André Lureau

Use the common virtio_gpu_disable_scanout() to reset the scanout state, including GL state if any.

Fixes 7c092f17ccee ("virtio-gpu: Handle resource blob commands")
---
   hw/display/virtio-gpu-rutabaga.c | 4 +---
   hw/display/virtio-gpu-virgl.c    | 6 ++----
   hw/display/virtio-gpu.c          | 7 +++++--
   3 files changed, 8 insertions(+), 9 deletions(-)
hw/display/virtio-gpu-rutabaga.c +1 -3
index 8a82e96b7461..8697dd6936ba 100644
@@ -312,9 +312,7 @@ rutabaga_cmd_set_scanout(VirtIOGPU *g, struct virtio_gpu_ctrl_command *cmd)
       scanout = &vb->scanout[ss.scanout_id];
 
       if (ss.resource_id == 0) {
-        scanout->resource_id = 0;
-        qemu_console_set_surface(scanout->con, NULL);
-        qemu_console_gl_scanout_disable(scanout->con);
+        virtio_gpu_disable_scanout(g, ss.scanout_id);
           return;
       }
 
hw/display/virtio-gpu-virgl.c +2 -4
index 79d4c0b569fb..87d1fc3d5f65 100644
@@ -618,8 +618,7 @@ static void virgl_cmd_set_scanout(VirtIOGPU *g,
               ss.r.x, ss.r.y, ss.r.width, ss.r.height,
               d3d_tex2d);
       } else {
-        qemu_console_set_surface(g->parent_obj.scanout[ss.scanout_id].con, NULL);
-        qemu_console_gl_scanout_disable(g->parent_obj.scanout[ss.scanout_id].con);
+        virtio_gpu_disable_scanout(g, ss.scanout_id);
       }
       g->parent_obj.scanout[ss.scanout_id].resource_id = ss.resource_id;
   }
@@ -1426,8 +1425,7 @@ void virtio_gpu_virgl_reset_scanout(VirtIOGPU *g)
       int i;
 
       for (i = 0; i < g->parent_obj.conf.max_outputs; i++) {
-        qemu_console_set_surface(g->parent_obj.scanout[i].con, NULL);
-        qemu_console_gl_scanout_disable(g->parent_obj.scanout[i].con);
+        virtio_gpu_disable_scanout(g, i);
       }
   }
 
hw/display/virtio-gpu.c +0 -0
index ae1f87ad68b2..cb096cb349cd 100644

hw/display/virtio-gpu.c void virtio_gpu_disable_scanout(VirtIOGPU *g, int scanout_id) · line 442

Quoted 10 lines
hw/display/virtio-gpu.c +5 -2
@@ -435,8 +435,11 @@ void virtio_gpu_disable_scanout(VirtIOGPU *g, int scanout_id)
   {
       struct virtio_gpu_scanout *scanout = &g->parent_obj.scanout[scanout_id];
 
-    virtio_gpu_release_scanout_dmabuf(g, scanout_id);
-
+    if (scanout->dmabuf) {
+        virtio_gpu_release_scanout_dmabuf(g, scanout_id);
+    } else {
+        qemu_console_gl_scanout_disable(scanout->con);
+    }

GL needs to be always disabled when clearing a scanout.

Quoted 8 lines
Quoted 4 lines

Ok, I removed the else condition.

Are you going to review the remaining 1-23 patches? thanks

I reviewed them all. I have comments for "[PATCH v2 02/25] virtio-gpu:
fix use-after-free on blob cursor after detach". The other patches look good to me.

Thanks; I'll send a v3 removing the vhost-user-gpu patch 24, leaving only 2 patches missing an r-b.

#42 lore raw

PATCH v2 21/25 virtio-gpu: minor refactoring

In reply to Marc-André Lureau

Move disable_scanout() branch earlier, return early. (scanout->resource_id is cleared there)

---
 hw/display/virtio-gpu-virgl.c | 71 ++++++++++++++++++++++---------------------
 1 file changed, 36 insertions(+), 35 deletions(-)
hw/display/virtio-gpu-virgl.c +36 -35
index 87d1fc3d5f65..118419171ff0 100644
@@ -565,6 +565,8 @@ static void virgl_cmd_set_scanout(VirtIOGPU *g,
                                   struct virtio_gpu_ctrl_command *cmd)
 {
     struct virtio_gpu_set_scanout ss;
+    struct virgl_renderer_resource_info info;
+    void *d3d_tex2d = NULL;
     int ret;
 
     VIRTIO_GPU_FILL_CMD(ss);
@@ -579,47 +581,46 @@ static void virgl_cmd_set_scanout(VirtIOGPU *g,
     }
     g->parent_obj.enable = 1;
 
-    if (ss.resource_id) {
-        struct virgl_renderer_resource_info info;
-        void *d3d_tex2d = NULL;
+    if (!ss.resource_id) {
+        virtio_gpu_disable_scanout(g, ss.scanout_id);
+        return;
+    }
 
 #if VIRGL_VERSION_MAJOR >= 1
-        struct virgl_renderer_resource_info_ext ext;
-        memset(&ext, 0, sizeof(ext));
-        ret = virgl_renderer_resource_get_info_ext(ss.resource_id, &ext);
-        info = ext.base;
-        d3d_tex2d = ext.d3d_tex2d;
+    struct virgl_renderer_resource_info_ext ext;
+    memset(&ext, 0, sizeof(ext));
+    ret = virgl_renderer_resource_get_info_ext(ss.resource_id, &ext);
+    info = ext.base;
+    d3d_tex2d = ext.d3d_tex2d;
 #else
-        memset(&info, 0, sizeof(info));
-        ret = virgl_renderer_resource_get_info(ss.resource_id, &info);
+    memset(&info, 0, sizeof(info));
+    ret = virgl_renderer_resource_get_info(ss.resource_id, &info);
 #endif
-        if (ret) {
-            qemu_log_mask(LOG_GUEST_ERROR,
-                          "%s: illegal resource specified %d\n",
-                          __func__, ss.resource_id);
-            cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
-            return;
-        }
-        if (!virtio_gpu_check_scanout_bounds(ss.scanout_id, ss.resource_id,
-                                             info.width, info.height, &ss.r,
-                                             &cmd->error)) {
-            return;
-        }
 
-        virtio_gpu_release_scanout_dmabuf(g, ss.scanout_id);
-
-        qemu_console_resize(g->parent_obj.scanout[ss.scanout_id].con,
-                            ss.r.width, ss.r.height);
-        virgl_renderer_force_ctx_0();
-        qemu_console_gl_scanout_texture(
-            g->parent_obj.scanout[ss.scanout_id].con, info.tex_id,
-            info.flags & VIRTIO_GPU_RESOURCE_FLAG_Y_0_TOP,
-            info.width, info.height,
-            ss.r.x, ss.r.y, ss.r.width, ss.r.height,
-            d3d_tex2d);
-    } else {
-        virtio_gpu_disable_scanout(g, ss.scanout_id);
+    if (ret) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: illegal resource specified %d\n",
+                      __func__, ss.resource_id);
+        cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
+        return;
     }
+    if (!virtio_gpu_check_scanout_bounds(ss.scanout_id, ss.resource_id,
+                                         info.width, info.height, &ss.r,
+                                         &cmd->error)) {
+        return;
+    }
+
+    virtio_gpu_release_scanout_dmabuf(g, ss.scanout_id);
+
+    qemu_console_resize(g->parent_obj.scanout[ss.scanout_id].con,
+                        ss.r.width, ss.r.height);
+    virgl_renderer_force_ctx_0();
+    qemu_console_gl_scanout_texture(
+        g->parent_obj.scanout[ss.scanout_id].con, info.tex_id,
+        info.flags & VIRTIO_GPU_RESOURCE_FLAG_Y_0_TOP,
+        info.width, info.height,
+        ss.r.x, ss.r.y, ss.r.width, ss.r.height,
+        d3d_tex2d);
     g->parent_obj.scanout[ss.scanout_id].resource_id = ss.resource_id;
 }
 
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
#44 lore raw

PATCH v2 22/25 virtio-gpu/virgl: disable scanouts on resource unref

In reply to Marc-André Lureau

Disable scanouts referencing a resource being unreferenced.

The scanout disable callbacks may change the current GL context, so restore the renderer context before detaching and unreferencing the resource.

Fixes 9d9e152136bd ("virtio-gpu: add 3d mode and virgl rendering support.")
---
 hw/display/virtio-gpu-virgl.c | 8 ++++++++
 1 file changed, 8 insertions(+)
hw/display/virtio-gpu-virgl.c +8 -0
index 118419171ff0..cf624761610a 100644
@@ -431,6 +431,7 @@ virtio_gpu_virgl_resource_unref(VirtIOGPU *g,
 {
     struct iovec *res_iovs = NULL;
     int num_iovs = 0;
+    int i;
 #if VIRGL_VERSION_MAJOR >= 1
     int ret;
 
@@ -443,6 +444,13 @@ virtio_gpu_virgl_resource_unref(VirtIOGPU *g,
     }
 #endif
 
+    for (i = 0; i < g->parent_obj.conf.max_outputs; i++) {
+        if (g->parent_obj.scanout[i].resource_id == res->base.resource_id) {
+            virtio_gpu_disable_scanout(g, i);
+        }
+    }
+
+    virgl_renderer_force_ctx_0();
     virgl_renderer_resource_detach_iov(res->base.resource_id,
                                        &res_iovs,
                                        &num_iovs);
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
Reviewed-by on v2 22/25
#45 lore raw

Re: [PATCH v2 22/25] virtio-gpu/virgl: disable scanouts on resource unref

Quoted 8 lines · Marc-André Lureau

Disable scanouts referencing a resource being unreferenced.

The scanout disable callbacks may change the current GL context, so restore the renderer context before detaching and unreferencing the resource.

Fixes 9d9e152136bd ("virtio-gpu: add 3d mode and virgl rendering support.")

Regards,
Akihiko Odaki

#46 lore raw

PATCH v2 23/25 virtio-gpu: extract virtio_gpu_disable_scanout_for_resource helper

In reply to Marc-André Lureau

The same loop iterating scanouts and disabling those matching a given resource_id was duplicated across four call sites in three backends. Factor it into a shared helper.

---
 hw/display/virtio-gpu-rutabaga.c |  7 +------
 hw/display/virtio-gpu-virgl.c    |  7 +------
 hw/display/virtio-gpu.c          | 26 ++++++++++++--------------
 include/hw/virtio/virtio-gpu.h   |  2 ++
 4 files changed, 16 insertions(+), 26 deletions(-)
hw/display/virtio-gpu-rutabaga.c +1 -6
index 8697dd6936ba..3aafa4dc66cd 100644
@@ -146,14 +146,9 @@ virtio_gpu_rutabaga_resource_unref(VirtIOGPU *g,
                                    Error **errp)
 {
     int32_t result;
-    int i;
     VirtIOGPURutabaga *vr = VIRTIO_GPU_RUTABAGA(g);
 
-    for (i = 0; i < g->parent_obj.conf.max_outputs; i++) {
-        if (g->parent_obj.scanout[i].resource_id == res->resource_id) {
-            virtio_gpu_disable_scanout(g, i);
-        }
-    }
+    virtio_gpu_disable_scanout_for_resource(g, res->resource_id);
 
     result = rutabaga_resource_unref(vr->rutabaga, res->resource_id);
     if (result) {
hw/display/virtio-gpu-virgl.c +1 -6
index cf624761610a..248aaf1a7149 100644
@@ -431,7 +431,6 @@ virtio_gpu_virgl_resource_unref(VirtIOGPU *g,
 {
     struct iovec *res_iovs = NULL;
     int num_iovs = 0;
-    int i;
 #if VIRGL_VERSION_MAJOR >= 1
     int ret;
 
@@ -444,11 +443,7 @@ virtio_gpu_virgl_resource_unref(VirtIOGPU *g,
     }
 #endif
 
-    for (i = 0; i < g->parent_obj.conf.max_outputs; i++) {
-        if (g->parent_obj.scanout[i].resource_id == res->base.resource_id) {
-            virtio_gpu_disable_scanout(g, i);
-        }
-    }
+    virtio_gpu_disable_scanout_for_resource(g, res->base.resource_id);
 
     virgl_renderer_force_ctx_0();
     virgl_renderer_resource_detach_iov(res->base.resource_id,
hw/display/virtio-gpu.c +12 -14
index cb096cb349cd..9995f7184610 100644
@@ -447,17 +447,21 @@ void virtio_gpu_disable_scanout(VirtIOGPU *g, int scanout_id)
     scanout->height = 0;
 }
 
+void virtio_gpu_disable_scanout_for_resource(VirtIOGPU *g,
+                                             uint32_t resource_id)
+{
+    for (int i = 0; i < g->parent_obj.conf.max_outputs; i++) {
+        if (g->parent_obj.scanout[i].resource_id == resource_id) {
+            virtio_gpu_disable_scanout(g, i);
+        }
+    }
+}
+
 static void virtio_gpu_resource_destroy(VirtIOGPU *g,
                                         struct virtio_gpu_simple_resource *res,
                                         Error **errp)
 {
-    int i;
-
-    for (i = 0; i < g->parent_obj.conf.max_outputs; i++) {
-        if (g->parent_obj.scanout[i].resource_id == res->resource_id) {
-            virtio_gpu_disable_scanout(g, i);
-        }
-    }
+    virtio_gpu_disable_scanout_for_resource(g, res->resource_id);
 
     qemu_pixman_image_unref(res->image);
     virtio_gpu_cleanup_mapping(g, res);
@@ -1065,13 +1069,7 @@ void virtio_gpu_cleanup_mapping(VirtIOGPU *g,
                                 struct virtio_gpu_simple_resource *res)
 {
     if (res->blob) {
-        int i, max_outputs = g->parent_obj.conf.max_outputs;
-
-        for (i = 0; i < max_outputs; i++) {
-            if (g->parent_obj.scanout[i].resource_id == res->resource_id) {
-                virtio_gpu_disable_scanout(g, i);
-            }
-        }
+        virtio_gpu_disable_scanout_for_resource(g, res->resource_id);
     }
 
     virtio_gpu_cleanup_mapping_iov(g, res->iov, res->iov_cnt);
include/hw/virtio/virtio-gpu.h +2 -0
index 2f18d839ccd3..631dfc63a4d4 100644
@@ -404,6 +404,8 @@ void virtio_gpu_update_scanout(VirtIOGPU *g,
                                struct virtio_gpu_framebuffer *fb,
                                struct virtio_gpu_rect *r);
 void virtio_gpu_disable_scanout(VirtIOGPU *g, int scanout_id);
+void virtio_gpu_disable_scanout_for_resource(VirtIOGPU *g,
+                                             uint32_t resource_id);
 
 /* virtio-gpu-3d.c */
 void virtio_gpu_virgl_process_cmd(VirtIOGPU *g,
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
Reviewed-by on v2 23/25
#47 lore raw

Re: [PATCH v2 23/25] virtio-gpu: extract virtio_gpu_disable_scanout_for_resource helper

Quoted 5 lines · Marc-André Lureau

The same loop iterating scanouts and disabling those matching a given resource_id was duplicated across four call sites in three backends. Factor it into a shared helper.

Regards,
Akihiko Odaki

#48 lore raw

PATCH v2 24/25 vhost-user-gpu: release DMA-BUFs on scanout disable, reset and finalize

In reply to Marc-André Lureau

When the backend sends VHOST_USER_GPU_SCANOUT with zero dimensions to disable a scanout, the frontend only calls qemu_console_set_surface(NULL) without releasing the g->dmabuf[] entry or invoking qemu_console_gl_scanout_disable(). Display listeners (GTK, SDL) can therefore remain in GL scanout mode and keep redrawing the stale buffer.

Release the DMA-BUF and call qemu_console_gl_scanout_disable() in the SCANOUT handler when a previously active DMA-BUF exists.

Additionally, vhost_user_gpu_reset() and vhost_user_gpu_instance_finalize() never iterate the dmabuf[] array, so active DMA-BUFs leak on device reset or object destruction. Add cleanup loops to both paths.

Fixes 267f664658fe ("hw/display: add vhost-user-vga & gpu-pci")
---
 hw/display/vhost-user-gpu.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
hw/display/vhost-user-gpu.c +18 -0
index c09aca041135..3f1fd333eeab 100644
@@ -263,6 +263,11 @@ vhost_user_gpu_handle_display(VhostUserGPU *g, VhostUserGpuMsg *msg)
         con = s->con;
 
         if (m->width == 0) {
+            if (g->dmabuf[m->scanout_id]) {
+                qemu_console_gl_release_dmabuf(con, g->dmabuf[m->scanout_id]);
+                g_clear_pointer(&g->dmabuf[m->scanout_id], qemu_dmabuf_free);
+                qemu_console_gl_scanout_disable(con);
+            }
             qemu_console_set_surface(con, NULL);
         } else {
             s->ds = qemu_create_displaysurface(m->width, m->height);
@@ -631,6 +636,10 @@ vhost_user_gpu_instance_finalize(Object *obj)
 {
     VhostUserGPU *g = VHOST_USER_GPU(obj);
 
+    for (int i = 0; i < g->parent_obj.conf.max_outputs; i++) {
+        g_clear_pointer(&g->dmabuf[i], qemu_dmabuf_free);
+    }
+
     object_unref(OBJECT(g->vhost));
 }
 
@@ -639,6 +648,15 @@ vhost_user_gpu_reset(VirtIODevice *vdev)
 {
     VhostUserGPU *g = VHOST_USER_GPU(vdev);
 
+    for (int i = 0; i < g->parent_obj.conf.max_outputs; i++) {
+        if (g->dmabuf[i]) {
+            QemuConsole *con = g->parent_obj.scanout[i].con;
+            qemu_console_gl_release_dmabuf(con, g->dmabuf[i]);
+            g_clear_pointer(&g->dmabuf[i], qemu_dmabuf_free);
+            qemu_console_gl_scanout_disable(con);
+        }
+    }
+
     virtio_gpu_base_reset(VIRTIO_GPU_BASE(vdev));
 
     vhost_user_backend_stop(g->vhost);
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
#49 lore raw

Re: [PATCH v2 24/25] vhost-user-gpu: release DMA-BUFs on scanout disable, reset and finalize

Quoted 6 lines · Marc-André Lureau

When the backend sends VHOST_USER_GPU_SCANOUT with zero dimensions to disable a scanout, the frontend only calls qemu_console_set_surface(NULL) without releasing the g->dmabuf[] entry or invoking qemu_console_gl_scanout_disable(). Display listeners (GTK, SDL) can therefore remain in GL scanout mode and keep redrawing the stale buffer.

Quoted 7 lines

Release the DMA-BUF and call qemu_console_gl_scanout_disable() in the SCANOUT handler when a previously active DMA-BUF exists.

Additionally, vhost_user_gpu_reset() and vhost_user_gpu_instance_finalize() never iterate the dmabuf[] array, so active DMA-BUFs leak on device reset or object destruction. Add cleanup loops to both paths.

We need to ensure that DMA-BUF reset cleanup happens on the main-thread.

A guest writes zero to the modern PCI status register → virtio_pci_common_write() → virtio_pci_reset() → virtio_bus_reset() → virtio_reset() → vhost_user_gpu_reset(). This executes on the vCPU thread. The newly added release calls invoke GTK/SDL/EGL display listeners there. For example, egl_release_dmabuf() attempts to bind qemu_egl_rn_ctx, which remains current on the main thread, then calls glDeleteTextures() through egl_dmabuf_release_texture(). The context bind fails, leaving the GL deletion without the required current context. EGL 1.5 §3.7.3 explicitly requires EGL_BAD_ACCESS when the context is current on another thread.

Existing virtio_gpu_reset() handles this constraint by scheduling a main-loop BH and waiting, but it also has its own race and deadlock issues. [1] is my latest attempt to fix it. You may review it, and adopt its approach if it looks reasonable to you.

[1] https://lore.kernel.org/qemu-devel/[email protected]/
("[PATCH v3 0/9] virtio-gpu: Do not wait for the main thread during reset")

Quoted 24 lines
Fixes 267f664658fe ("hw/display: add vhost-user-vga & gpu-pci")
---
  hw/display/vhost-user-gpu.c | 18 ++++++++++++++++++
  1 file changed, 18 insertions(+)
hw/display/vhost-user-gpu.c +5 -0
index c09aca041135..3f1fd333eeab 100644
@@ -263,6 +263,11 @@ vhost_user_gpu_handle_display(VhostUserGPU *g, VhostUserGpuMsg *msg)
          con = s->con;
 
          if (m->width == 0) {
+            if (g->dmabuf[m->scanout_id]) {
+                qemu_console_gl_release_dmabuf(con, g->dmabuf[m->scanout_id]);
+                g_clear_pointer(&g->dmabuf[m->scanout_id], qemu_dmabuf_free);
+                qemu_console_gl_scanout_disable(con);
+            }
              qemu_console_set_surface(con, NULL);
          } else {
              s->ds = qemu_create_displaysurface(m->width, m->height);

hw/display/vhost-user-gpu.c vhost_user_gpu_instance_finalize(Object *obj) · line 639

Quoted 4 lines
hw/display/vhost-user-gpu.c +1 -0
@@ -631,6 +636,10 @@ vhost_user_gpu_instance_finalize(Object *obj)
  {
      VhostUserGPU *g = VHOST_USER_GPU(obj);
 
+    for (int i = 0; i < g->parent_obj.conf.max_outputs; i++) {

Please bound finalizer cleanup by the actual DMA-BUF array size.

conf.max_outputs is a writable uint32 property, and the rejection of values above 16 happens only in virtio_gpu_base_device_realize(). The finalizer also runs after realization failure, including missing chardev, before that validation. Thus setting max_outputs=4096 makes this new loop traverse beyond dmabuf[16], reading and potentially freeing arbitrary values. Iterating ARRAY_SIZE(g->dmabuf) instead is the simplest fix I came up.

Regards,
Akihiko Odaki

Quoted 23 lines
hw/display/vhost-user-gpu.c +12 -0
+        g_clear_pointer(&g->dmabuf[i], qemu_dmabuf_free);
+    }
+
      object_unref(OBJECT(g->vhost));
  }
 
@@ -639,6 +648,15 @@ vhost_user_gpu_reset(VirtIODevice *vdev)
  {
      VhostUserGPU *g = VHOST_USER_GPU(vdev);
 
+    for (int i = 0; i < g->parent_obj.conf.max_outputs; i++) {
+        if (g->dmabuf[i]) {
+            QemuConsole *con = g->parent_obj.scanout[i].con;
+            qemu_console_gl_release_dmabuf(con, g->dmabuf[i]);
+            g_clear_pointer(&g->dmabuf[i], qemu_dmabuf_free);
+            qemu_console_gl_scanout_disable(con);
+        }
+    }
+
      virtio_gpu_base_reset(VIRTIO_GPU_BASE(vdev));
 
      vhost_user_backend_stop(g->vhost);
#50 lore raw

PATCH v2 25/25 contrib/vhost-user-gpu: disable scanouts on resource unref

In reply to Marc-André Lureau

virgl_cmd_resource_unref() destroys the virgl renderer resource without first disabling any scanouts that reference it. The QEMU frontend then holds a stale DMA-BUF until another DMABUF_SCANOUT message arrives.

Send a DMABUF_SCANOUT disable message (fd=-1) for each affected scanout before destroying the resource.

Fixes d52c454aadcd ("contrib: add vhost-user-gpu")
---
 contrib/vhost-user-gpu/virgl.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
contrib/vhost-user-gpu/virgl.c +12 -0
index 20bae57d0fe4..5a5f9f14c80c 100644
@@ -113,6 +113,18 @@ virgl_cmd_resource_unref(VuGpu *g,
 
     VUGPU_FILL_CMD(unref);
 
+    for (int i = 0; i < VIRTIO_GPU_MAX_SCANOUTS; i++) {
+        if (g->scanout[i].resource_id == unref.resource_id) {
+            VhostUserGpuMsg msg = {
+                .request = VHOST_USER_GPU_DMABUF_SCANOUT,
+                .size = sizeof(VhostUserGpuDMABUFScanout),
+                .payload.dmabuf_scanout.scanout_id = i,
+            };
+            vg_send_msg(g, &msg, -1);
+            g->scanout[i].resource_id = 0;
+        }
+    }
+
     virgl_renderer_resource_detach_iov(unref.resource_id,
                                        &res_iovs,
                                        &num_iovs);
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
Reviewed-by on v2 25/25
#51 lore raw

Re: [PATCH v2 25/25] contrib/vhost-user-gpu: disable scanouts on resource unref

Quoted 9 lines · Marc-André Lureau

virgl_cmd_resource_unref() destroys the virgl renderer resource without first disabling any scanouts that reference it. The QEMU frontend then holds a stale DMA-BUF until another DMABUF_SCANOUT message arrives.

Send a DMABUF_SCANOUT disable message (fd=-1) for each affected scanout before destroying the resource.

Fixes d52c454aadcd ("contrib: add vhost-user-gpu")

Regards,
Akihiko Odaki