Skip to contentSkip to conversation

RFC: various virtio-gpu/dmabuf related fixes

PATCH Superseded Marc-André Lureau 2 participants 31 messages Aug 25 – Aug 28

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover RFC: 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.

This series makes each virtio_gpu_scanout own its current dmabuf and uses resource_id to track which resource it displays. It also makes QemuDmaBuf own its file descriptors and moves GTK fence state out of the dmabuf.

Those changes touch a lot of area and I have done basic testing with the various backends. I would not recommend backporting them to stable:
there is a high risk of introducing regressions. Yet, I don't know how to address all the problems uncovered by CVE-2026-66020 in a simple patch series. Help welcome!

thanks

---
Marc-André Lureau (18):
ui/gtk: move GL fence tracking 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
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: disable blob scanouts on mapping cleanup
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: initialize dmabuf_fd to -1
virtio-gpu: fix use-after-free on blob cursor after detach

 contrib/vhost-user-gpu/vhost-user-gpu.c | 28 +++-------
 hw/display/vhost-user-gpu.c             |  1 -
 hw/display/virtio-gpu-rutabaga.c        | 26 +++++++---
 hw/display/virtio-gpu-udmabuf-stubs.c   |  2 +-
 hw/display/virtio-gpu-udmabuf.c         | 90 +++++++++++----------------------
 hw/display/virtio-gpu-virgl.c           | 75 +++++++++++++++------------
 hw/display/virtio-gpu.c                 | 82 +++++++++++++++++++-----------
 hw/vfio/display.c                       |  1 -
 include/hw/virtio/virtio-gpu.h          | 18 ++-----
 include/ui/dmabuf.h                     |  5 --
 include/ui/egl-helpers.h                |  4 +-
 include/ui/gtk.h                        |  8 +--
 ui/console.c                            |  3 ++
 ui/dbus-listener.c                      | 69 +++++++++++++++++--------
 ui/dmabuf.c                             | 51 +++----------------
 ui/egl-helpers.c                        | 15 +++---
 ui/gtk-egl.c                            | 30 ++++++-----
 ui/gtk-gl-area.c                        | 12 ++---
 ui/gtk.c                                | 31 +++++++++---
 ui/sdl2-gl.c                            |  5 ++
 20 files changed, 277 insertions(+), 279 deletions(-)

---
base-commit: 82706188517bd102ed25f1931c4cb041ecf3ef50
change-id: 20260825-virtio-gpu-cleanup-dfb2ba185936

Best regards,

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

PATCH 1/18 ui/gtk: move GL fence tracking from QemuDmaBuf to VirtualGfxConsole

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 405ddd912591..023427b505ed 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 d689f187c4e5..54b4aa6e9fcb 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
#2 lore raw

PATCH 2/18 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.

---
 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
#3 lore raw

Re: [PATCH 02/18] ui/dbus: disable scanout iff dmabuf is current

Quoted 3 lines · Marc-André Lureau

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

My AI agent is so nitpicking about the lack of Fixes: tags and its suggestions look reasonable, so I'm leaving them as comments. For this patch:

Fixes 142ca628a733 ("ui: add a D-Bus display backend")

Regards,
Akihiko Odaki

Quoted 161 lines
---
  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 */
 
#4 lore raw

PATCH 3/18 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 5f08c855dde1..fd47028a1189 100644
@@ -152,30 +152,19 @@ void virtio_gpu_init_udmabuf(struct virtio_gpu_simple_resource *res)
     res->blob = pdata;
 }
 
-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;
         }
     }
@@ -183,31 +172,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,
@@ -217,26 +198,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 220231ec9d43..79eb4b1b8cec 100644
@@ -74,6 +74,7 @@ struct virtio_gpu_framebuffer {
 struct virtio_gpu_scanout {
     QemuConsole *con;
     DisplaySurface *ds;
+    QemuDmaBuf *dmabuf;
     uint32_t width, height;
     int x, y;
     int invalidate;
@@ -178,12 +179,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;
 
@@ -215,11 +210,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
#5 lore raw

PATCH 4/18 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.

---
 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
#6 lore raw

Re: [PATCH 04/18] ui/console: disable GL scanout when dmabuf is the active one

Quoted 3 lines · Marc-André Lureau

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

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

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

Regards,
Akihiko Odaki

Quoted 20 lines
---
  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,
#7 lore raw

PATCH 5/18 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
#8 lore raw

PATCH 6/18 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       | 32 +++++++++-----------------------
 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(+), 45 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 85d03935a332..f692e1351034 100644
@@ -12,7 +12,7 @@ void virtio_gpu_init_udmabuf(struct virtio_gpu_simple_resource *res)
     /* nothing (stub) */
 }
 
-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 -23
index fd47028a1189..094646af8636 100644
@@ -79,7 +79,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);
@@ -143,7 +143,7 @@ void 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;
         }
         pdata = res->remapped;
@@ -152,42 +152,28 @@ void virtio_gpu_init_udmabuf(struct virtio_gpu_simple_resource *res)
     res->blob = pdata;
 }
 
-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;
-        }
-    }
-
-    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 fbb6fec7a0ad..0335b636068d 100644
@@ -978,7 +978,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 79eb4b1b8cec..d3ca8d82586b 100644
@@ -379,8 +379,7 @@ bool virtio_gpu_scanout_blob_to_fb(struct virtio_gpu_framebuffer *fb,
 /* virtio-gpu-udmabuf.c */
 bool virtio_gpu_have_udmabuf(void);
 void 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 381583bd536c..e47b96b454dd 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 b61dc7ad2783..3651f784bd14 100644
@@ -62,10 +62,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);
 }
 
@@ -92,20 +100,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
#9 lore raw

PATCH 7/18 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 0335b636068d..46ddfeeaba87 100644
@@ -387,20 +387,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
#10 lore raw

PATCH 8/18 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 786488150932..cd4e6b60cc13 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);
         }
     }
 
@@ -659,7 +651,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;
@@ -704,12 +696,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;
@@ -794,10 +780,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
#11 lore raw

PATCH 9/18 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.

---
 hw/display/virtio-gpu-rutabaga.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)
hw/display/virtio-gpu-rutabaga.c +17 -3
index a054f8117f14..fcd9c94d580c 100644
@@ -154,8 +154,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,
@@ -259,7 +266,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;
         }
@@ -283,7 +290,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
@@ -307,6 +320,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;
@@ -340,7 +354,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
#12 lore raw

Re: [PATCH 09/18] virtio-gpu-rutabaga: fix scanout handling

Quoted 7 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")
Quoted 67 lines
---
  hw/display/virtio-gpu-rutabaga.c | 20 +++++++++++++++++---
  1 file changed, 17 insertions(+), 3 deletions(-)
hw/display/virtio-gpu-rutabaga.c +17 -3
index a054f8117f14..fcd9c94d580c 100644
@@ -154,8 +154,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,
@@ -259,7 +266,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;
          }
@@ -283,7 +290,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
@@ -307,6 +320,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;
@@ -340,7 +354,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
#13 lore raw

PATCH 10/18 virtio-gpu: disable blob scanouts on mapping cleanup

In reply to 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.

---
 hw/display/virtio-gpu.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
hw/display/virtio-gpu.c +10 -0
index 46ddfeeaba87..e547cac60398 100644
@@ -982,6 +982,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
#14 lore raw

Re: [PATCH 10/18] virtio-gpu: disable blob scanouts on mapping cleanup

Quoted 4 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 32db3c63ae11 ("virtio-gpu: Add virtio_gpu_set_scanout_blob")
Quoted 29 lines
---
  hw/display/virtio-gpu.c | 10 ++++++++++
  1 file changed, 10 insertions(+)
hw/display/virtio-gpu.c +10 -0
index 46ddfeeaba87..e547cac60398 100644
@@ -982,6 +982,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;
#15 lore raw

PATCH 11/18 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 e547cac60398..5362f7a4c269 100644
@@ -403,12 +403,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);
 
@@ -425,11 +419,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);
         }
     }
 
@@ -593,10 +585,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);
@@ -621,16 +613,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;
@@ -1609,7 +1594,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 d3ca8d82586b..1816cb21de76 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
#16 lore raw

PATCH 12/18 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.

---
 hw/display/virtio-gpu.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
hw/display/virtio-gpu.c +9 -4
index 5362f7a4c269..a74475dc6741 100644
@@ -658,9 +658,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)) {
@@ -687,12 +689,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;
         }
 
@@ -702,7 +703,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 ||
@@ -727,6 +729,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
#17 lore raw

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

Quoted 4 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")
Quoted 57 lines
---
  hw/display/virtio-gpu.c | 13 +++++++++----
  1 file changed, 9 insertions(+), 4 deletions(-)
hw/display/virtio-gpu.c +9 -4
index 5362f7a4c269..a74475dc6741 100644
@@ -658,9 +658,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)) {
@@ -687,12 +689,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;
          }
 
@@ -702,7 +703,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 ||
@@ -727,6 +729,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);
      }
#18 lore raw

PATCH 13/18 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.

---
 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 6e298f997d66..532132e46031 100644
@@ -586,6 +586,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 a74475dc6741..c50eb58917e8 100644
@@ -387,7 +387,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 1816cb21de76..22a38589d63d 100644
@@ -360,6 +360,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
#19 lore raw

Re: [PATCH 13/18] virtio-gpu/virgl: release dmabuf when a texture replaces a scanout

Quoted 4 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")
Quoted 48 lines
---
  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 6e298f997d66..532132e46031 100644
@@ -586,6 +586,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 a74475dc6741..c50eb58917e8 100644
@@ -387,7 +387,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 1816cb21de76..22a38589d63d 100644
@@ -360,6 +360,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
#20 lore raw

PATCH 14/18 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.

---
 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 fcd9c94d580c..5344a5f9e26a 100644
@@ -320,9 +320,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 532132e46031..6cb9365a714f 100644
@@ -599,8 +599,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;
 }
@@ -1406,8 +1405,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 c50eb58917e8..a7f549a6a8b9 100644
@@ -404,8 +404,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
#21 lore raw

Re: [PATCH 14/18] virtio-gpu: consolidate disabling scanout

Quoted 2 lines · Marc-André Lureau

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

This actually:

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

because it properly releases scanout->dmabuf.

Regards,
Akihiko Odaki

Quoted 66 lines
---
  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 fcd9c94d580c..5344a5f9e26a 100644
@@ -320,9 +320,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 532132e46031..6cb9365a714f 100644
@@ -599,8 +599,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;
  }
@@ -1406,8 +1405,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 c50eb58917e8..a7f549a6a8b9 100644
@@ -404,8 +404,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;
#22 lore raw

PATCH 15/18 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 | 61 +++++++++++++++++++++++--------------------
 1 file changed, 32 insertions(+), 29 deletions(-)
hw/display/virtio-gpu-virgl.c +32 -29
index 6cb9365a714f..e2ad27343da5 100644
@@ -546,6 +546,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);
@@ -560,10 +562,12 @@ 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));
@@ -574,33 +578,32 @@ static void virgl_cmd_set_scanout(VirtIOGPU *g,
         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
#23 lore raw

PATCH 16/18 virtio-gpu/virgl: disable scanouts on resource unref

In reply to Marc-André Lureau

Disable scanouts referencing a resource being unreferenced.

---
 hw/display/virtio-gpu-virgl.c | 7 +++++++
 1 file changed, 7 insertions(+)
hw/display/virtio-gpu-virgl.c +7 -0
index e2ad27343da5..8b27fe3980d6 100644
@@ -412,6 +412,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;
 
@@ -424,6 +425,12 @@ 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_resource_detach_iov(res->base.resource_id,
                                        &res_iovs,
                                        &num_iovs);
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
#24 lore raw

Re: [PATCH 16/18] virtio-gpu/virgl: disable scanouts on resource unref

Quoted 2 lines · Marc-André Lureau

Disable scanouts referencing a resource being unreferenced.

Fixes 9d9e152136bd ("virtio-gpu: add 3d mode and virgl rendering support.")
Quoted 18 lines
---
  hw/display/virtio-gpu-virgl.c | 7 +++++++
  1 file changed, 7 insertions(+)
hw/display/virtio-gpu-virgl.c +1 -0
index e2ad27343da5..8b27fe3980d6 100644
@@ -412,6 +412,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;
 

hw/display/virtio-gpu-virgl.c virtio_gpu_virgl_resource_unref(VirtIOGPU *g, · line 430

Quoted 6 lines
hw/display/virtio-gpu-virgl.c +3 -0
@@ -424,6 +425,12 @@ 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);

This invokes synchronous frontend callbacks. GTK/EGL can finish with EGL_NO_CONTEXT, while GLArea leaves its widget context current. The function then calls virgl_renderer_resource_unref(), which directly performs GL object deletion.

Please add virgl_renderer_force_ctx_0() after the complete disable loop, before detach/unref. This guarantees a valid renderer context and resynchronizes virglrenderer’s cached context state. Without it, guest-triggered RESOURCE_UNREF can cause undefined GL calls or host GPU-resource leakage.

Regards,
Akihiko Odaki

Quoted 7 lines
hw/display/virtio-gpu-virgl.c +3 -0
+        }
+    }
+
      virgl_renderer_resource_detach_iov(res->base.resource_id,
                                         &res_iovs,
                                         &num_iovs);
#25 lore raw

PATCH 17/18 virtio-gpu: initialize dmabuf_fd to -1

In reply to Marc-André Lureau

FD fields should be initialized to a non-fd value to avoid confusion and potential invalid closes. The following patch will change the guard to finish/close dmabuf and rely on fd being initialized instead.

---
 hw/display/virtio-gpu-rutabaga.c | 4 +++-
 hw/display/virtio-gpu-udmabuf.c  | 3 ++-
 hw/display/virtio-gpu-virgl.c    | 6 +++---
 hw/display/virtio-gpu.c          | 5 ++++-
 4 files changed, 12 insertions(+), 6 deletions(-)
hw/display/virtio-gpu-rutabaga.c +3 -1
index 5344a5f9e26a..ae158de480f5 100644
@@ -101,6 +101,7 @@ rutabaga_cmd_create_resource_2d(VirtIOGPU *g,
     CHECK(!result, cmd);
 
     res = g_new0(struct virtio_gpu_simple_resource, 1);
+    res->dmabuf_fd = -1;
     res->width = c2d.width;
     res->height = c2d.height;
     res->format = c2d.format;
@@ -140,6 +141,7 @@ rutabaga_cmd_create_resource_3d(VirtIOGPU *g,
     CHECK(!result, cmd);
 
     res = g_new0(struct virtio_gpu_simple_resource, 1);
+    res->dmabuf_fd = -1;
     res->width = c3d.width;
     res->height = c3d.height;
     res->format = c3d.format;
@@ -637,7 +639,7 @@ rutabaga_cmd_resource_create_blob(VirtIOGPU *g,
     CHECK(cblob.resource_id != 0, cmd);
 
     res = g_new0(struct virtio_gpu_simple_resource, 1);
-
+    res->dmabuf_fd = -1;
     res->resource_id = cblob.resource_id;
     res->blob_size = cblob.size;
 
hw/display/virtio-gpu-udmabuf.c +2 -1
index 094646af8636..4e9806f82144 100644
@@ -132,7 +132,8 @@ void virtio_gpu_init_udmabuf(struct virtio_gpu_simple_resource *res)
 {
     void *pdata = NULL;
 
-    res->dmabuf_fd = -1;
+    assert(res->dmabuf_fd == -1);
+
     if (res->iov_cnt == 1 &&
         res->iov[0].iov_len < 4096) {
         pdata = res->iov[0].iov_base;
hw/display/virtio-gpu-virgl.c +3 -3
index 8b27fe3980d6..291373274dee 100644
@@ -336,11 +336,11 @@ static void virgl_cmd_create_resource_2d(VirtIOGPU *g,
     }
 
     res = g_new0(struct virtio_gpu_virgl_resource, 1);
+    res->base.dmabuf_fd = -1;
     res->base.width = c2d.width;
     res->base.height = c2d.height;
     res->base.format = c2d.format;
     res->base.resource_id = c2d.resource_id;
-    res->base.dmabuf_fd = -1;
     QTAILQ_INSERT_HEAD(&g->reslist, &res->base, next);
 
     args.handle = c2d.resource_id;
@@ -384,11 +384,11 @@ static void virgl_cmd_create_resource_3d(VirtIOGPU *g,
     }
 
     res = g_new0(struct virtio_gpu_virgl_resource, 1);
+    res->base.dmabuf_fd = -1;
     res->base.width = c3d.width;
     res->base.height = c3d.height;
     res->base.format = c3d.format;
     res->base.resource_id = c3d.resource_id;
-    res->base.dmabuf_fd = -1;
     QTAILQ_INSERT_HEAD(&g->reslist, &res->base, next);
 
     args.handle = c3d.resource_id;
@@ -862,9 +862,9 @@ static void virgl_cmd_resource_create_blob(VirtIOGPU *g,
     }
 
     res = g_new0(struct virtio_gpu_virgl_resource, 1);
+    res->base.dmabuf_fd = -1;
     res->base.resource_id = cblob.resource_id;
     res->base.blob_size = cblob.size;
-    res->base.dmabuf_fd = -1;
 
     if (cblob.blob_mem != VIRTIO_GPU_BLOB_MEM_HOST3D) {
         ret = virtio_gpu_create_mapping_iov(g, cblob.nr_entries, sizeof(cblob),
hw/display/virtio-gpu.c +4 -1
index a7f549a6a8b9..521d4800d872 100644
@@ -274,7 +274,7 @@ static void virtio_gpu_resource_create_2d(VirtIOGPU *g,
     }
 
     res = g_new0(struct virtio_gpu_simple_resource, 1);
-
+    res->dmabuf_fd = -1;
     res->width = c2d.width;
     res->height = c2d.height;
     res->format = c2d.format;
@@ -360,6 +360,7 @@ static void virtio_gpu_resource_create_blob(VirtIOGPU *g,
     }
 
     res = g_new0(struct virtio_gpu_simple_resource, 1);
+    res->dmabuf_fd = -1;
     res->resource_id = cblob.resource_id;
     res->blob_size = cblob.size;
 
@@ -1407,6 +1408,7 @@ static int virtio_gpu_load(QEMUFile *f, void *opaque, size_t size,
         }
 
         res = g_new0(struct virtio_gpu_simple_resource, 1);
+        res->dmabuf_fd = -1;
         res->resource_id = resource_id;
         res->width = qemu_get_be32(f);
         res->height = qemu_get_be32(f);
@@ -1518,6 +1520,7 @@ static int virtio_gpu_blob_load(QEMUFile *f, void *opaque, size_t size,
         }
 
         res = g_new0(struct virtio_gpu_simple_resource, 1);
+        res->dmabuf_fd = -1;
         res->resource_id = resource_id;
         res->blob_size = qemu_get_be32(f);
         res->iov_cnt = qemu_get_be32(f);
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
#26 lore raw

PATCH 18/18 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(). Simplify code by dropping a needless condition for calling fini_udmabuf().

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         | 12 +++++++++---
 2 files changed, 10 insertions(+), 3 deletions(-)
hw/display/virtio-gpu-udmabuf.c +1 -0
index 4e9806f82144..3bd6a77730cd 100644
@@ -89,6 +89,7 @@ void virtio_gpu_fini_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 +9 -3
index 521d4800d872..87823b2cd5af 100644
@@ -63,8 +63,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->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;
@@ -992,9 +1000,7 @@ void virtio_gpu_cleanup_mapping(VirtIOGPU *g,
     g_free(res->addrs);
     res->addrs = NULL;
 
-    if (res->blob) {
-        virtio_gpu_fini_udmabuf(res);
-    }
+    virtio_gpu_fini_udmabuf(res);
 }
 
 static void
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
#27 lore raw
In reply to Marc-André Lureau
Quoted 16 lines · Marc-André Lureau

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.

This series makes each virtio_gpu_scanout own its current dmabuf and uses resource_id to track which resource it displays. It also makes QemuDmaBuf own its file descriptors and moves GTK fence state out of the dmabuf.

Quoted 5 lines

Those changes touch a lot of area and I have done basic testing with the various backends. I would not recommend backporting them to stable:
there is a high risk of introducing regressions. Yet, I don't know how to address all the problems uncovered by CVE-2026-66020 in a simple patch series. Help welcome!

If you mean "all the problems", my coding agent tells there are some problems remaining:

1. Vhost-user still has duplicate, incomplete DMA-BUF ownership.

VhostUserGPU retains its separate g->dmabuf[]. The contrib backend disables a scanout using ordinary VHOST_USER_GPU_SCANOUT with zero dimensions, but the QEMU frontend handles that by only calling qemu_console_set_surface().

Although that changes con->scanout.kind, it neither releases g->dmabuf[id] nor invokes the frontend GL-disable callback. GTK/SDL can therefore retain their private scanout mode and redraw the old buffer; the wrapper and owned fd remain until another DMA-BUF message. Reset and finalize also never free this array.

Separately, contrib’s virgl RESOURCE_UNREF destroys the renderer resource without disabling scanouts using it. The in-process virgl backend fixes precisely this case, but contrib does not.

2. GTK DMA-BUF texture release lacks the required GL context.

GTK’s release callback directly calls egl_dmabuf_release_texture(), which calls glDeleteTextures() and then unconditionally forgets the texture ID. Neither it nor the core release dispatcher establishes the owning context.

This is reachable after another GTK console’s surface update leaves
EGL_NO_CONTEXT current. Deletion is then not guaranteed, yet the ID is lost, leaking the GL import and backing. With another unshared context current, a coincident texture name could be deleted from the wrong context. SDL has the same missing-context pattern.

3. Rutabaga resource destruction remains incomplete.

virtio_gpu_rutabaga_resource_unref() calls Rutabaga unref and frees the QEMU resource, but does not clean res->iov/res->addrs or remove a mapped blob’s host-visible MemoryRegion. Only explicit RESOURCE_UNMAP_BLOB removes that region.

Consequently, MAP_BLOB → RESOURCE_UNREF or reset can leave a
MemoryRegion pointing at storage destroyed by Rutabaga, as well as pinned DMA mappings. This is adjacent to, rather than the original scanout-detach CVE, but it is another unresolved blob-ownership problem.

...so there is still some work to be done.

But when it comes to backporting, I think we can instead do focused backporting to avoid extra trouble. Concretely, the patches you tagged with "Fixes: CVE-2026-66020" can be backported, namely:

- "[PATCH 10/18] virtio-gpu: disable blob scanouts on mapping cleanup" and
- "[PATCH 18/18] virtio-gpu: fix use-after-free on blob cursor after detach"

The latter is interchangeable with "[PATCH v2 4/5] hw/display/virtio-gpu: Check cursor data presence", which I posted (sorry but I forgot the presence of your cursor fix.)

These patches fix use-after-free that can have unpredictable consequences and is potentially vulnerable.

Other patches fix stale DMA-BUF references and potential resource leaks. While they are real bugs and can lead to problems like display glitches and high memory usage, these risks are minor and their security implication is probably negligible.

Regards,
Akihiko Odaki

Quoted 54 lines

thanks

---
Marc-André Lureau (18):
ui/gtk: move GL fence tracking 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
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: disable blob scanouts on mapping cleanup
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: initialize dmabuf_fd to -1
virtio-gpu: fix use-after-free on blob cursor after detach

  contrib/vhost-user-gpu/vhost-user-gpu.c | 28 +++-------
  hw/display/vhost-user-gpu.c             |  1 -
  hw/display/virtio-gpu-rutabaga.c        | 26 +++++++---
  hw/display/virtio-gpu-udmabuf-stubs.c   |  2 +-
  hw/display/virtio-gpu-udmabuf.c         | 90 +++++++++++----------------------
  hw/display/virtio-gpu-virgl.c           | 75 +++++++++++++++------------
  hw/display/virtio-gpu.c                 | 82 +++++++++++++++++++-----------
  hw/vfio/display.c                       |  1 -
  include/hw/virtio/virtio-gpu.h          | 18 ++-----
  include/ui/dmabuf.h                     |  5 --
  include/ui/egl-helpers.h                |  4 +-
  include/ui/gtk.h                        |  8 +--
  ui/console.c                            |  3 ++
  ui/dbus-listener.c                      | 69 +++++++++++++++++--------
  ui/dmabuf.c                             | 51 +++----------------
  ui/egl-helpers.c                        | 15 +++---
  ui/gtk-egl.c                            | 30 ++++++-----
  ui/gtk-gl-area.c                        | 12 ++---
  ui/gtk.c                                | 31 +++++++++---
  ui/sdl2-gl.c                            |  5 ++
  20 files changed, 277 insertions(+), 279 deletions(-)

---
base-commit: 82706188517bd102ed25f1931c4cb041ecf3ef50
change-id: 20260825-virtio-gpu-cleanup-dfb2ba185936

Best regards,
--
Marc-André Lureau <[email protected]>

#28 lore raw
Quoted 17 lines · Akihiko Odaki
Quoted 16 lines · Marc-André Lureau

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.

This series makes each virtio_gpu_scanout own its current dmabuf and uses resource_id to track which resource it displays. It also makes QemuDmaBuf own its file descriptors and moves GTK fence state out of the dmabuf.

Quoted 7 lines
Quoted 5 lines

Those changes touch a lot of area and I have done basic testing with the various backends. I would not recommend backporting them to stable:
there is a high risk of introducing regressions. Yet, I don't know how to address all the problems uncovered by CVE-2026-66020 in a simple patch series. Help welcome!

If you mean "all the problems", my coding agent tells there are some problems remaining:

I forgot to note that I manually reviewed the claims.

I also think you can drop RFC. Though the series is large, each patch is small and I think reasonable.

Quoted 99 lines

1. Vhost-user still has duplicate, incomplete DMA-BUF ownership.

VhostUserGPU retains its separate g->dmabuf[]. The contrib backend disables a scanout using ordinary VHOST_USER_GPU_SCANOUT with zero dimensions, but the QEMU frontend handles that by only calling qemu_console_set_surface().

Although that changes con->scanout.kind, it neither releases g-

Quoted 1 line

dmabuf[id] nor invokes the frontend GL-disable callback. GTK/SDL can

therefore retain their private scanout mode and redraw the old buffer; the wrapper and owned fd remain until another DMA-BUF message. Reset and finalize also never free this array.

Separately, contrib’s virgl RESOURCE_UNREF destroys the renderer resource without disabling scanouts using it. The in-process virgl backend fixes precisely this case, but contrib does not.

2. GTK DMA-BUF texture release lacks the required GL context.

GTK’s release callback directly calls egl_dmabuf_release_texture(), which calls glDeleteTextures() and then unconditionally forgets the texture ID. Neither it nor the core release dispatcher establishes the owning context.

This is reachable after another GTK console’s surface update leaves
EGL_NO_CONTEXT current. Deletion is then not guaranteed, yet the ID is lost, leaking the GL import and backing. With another unshared context current, a coincident texture name could be deleted from the wrong context. SDL has the same missing-context pattern.

3. Rutabaga resource destruction remains incomplete.

virtio_gpu_rutabaga_resource_unref() calls Rutabaga unref and frees the QEMU resource, but does not clean res->iov/res->addrs or remove a mapped blob’s host-visible MemoryRegion. Only explicit RESOURCE_UNMAP_BLOB removes that region.

Consequently, MAP_BLOB → RESOURCE_UNREF or reset can leave a
MemoryRegion pointing at storage destroyed by Rutabaga, as well as pinned DMA mappings. This is adjacent to, rather than the original scanout-detach CVE, but it is another unresolved blob-ownership problem.

...so there is still some work to be done.

But when it comes to backporting, I think we can instead do focused backporting to avoid extra trouble. Concretely, the patches you tagged with "Fixes: CVE-2026-66020" can be backported, namely:

- "[PATCH 10/18] virtio-gpu: disable blob scanouts on mapping cleanup" and
- "[PATCH 18/18] virtio-gpu: fix use-after-free on blob cursor after detach"

The latter is interchangeable with "[PATCH v2 4/5] hw/display/virtio- gpu: Check cursor data presence", which I posted (sorry but I forgot the presence of your cursor fix.)

These patches fix use-after-free that can have unpredictable consequences and is potentially vulnerable.

Other patches fix stale DMA-BUF references and potential resource leaks. While they are real bugs and can lead to problems like display glitches and high memory usage, these risks are minor and their security implication is probably negligible.

Regards,
Akihiko Odaki

Quoted 57 lines

thanks

---
Marc-André Lureau (18):
       ui/gtk: move GL fence tracking 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
       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: disable blob scanouts on mapping cleanup
       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: initialize dmabuf_fd to -1
       virtio-gpu: fix use-after-free on blob cursor after detach

  contrib/vhost-user-gpu/vhost-user-gpu.c | 28 +++------- hw/display/vhost-user-gpu.c             |  1 - hw/display/virtio-gpu-rutabaga.c        | 26 +++++++--- hw/display/virtio-gpu-udmabuf-stubs.c   |  2 +- hw/display/virtio-gpu-udmabuf.c         | 90 ++++++++++

+----------------------
  hw/display/virtio-gpu-virgl.c           | 75 ++++++++++++++ 
+------------
  hw/display/virtio-gpu.c                 | 82 ++++++++++++++++++ 
+-----------
  hw/vfio/display.c                       |  1 -
  include/hw/virtio/virtio-gpu.h          | 18 ++-----
  include/ui/dmabuf.h                     |  5 --
  include/ui/egl-helpers.h                |  4 +-
  include/ui/gtk.h                        |  8 +--
  ui/console.c                            |  3 ++
  ui/dbus-listener.c                      | 69 +++++++++++++++++--------
  ui/dmabuf.c                             | 51 +++----------------
  ui/egl-helpers.c                        | 15 +++---
  ui/gtk-egl.c                            | 30 ++++++-----
  ui/gtk-gl-area.c                        | 12 ++---
  ui/gtk.c                                | 31 +++++++++---
  ui/sdl2-gl.c                            |  5 ++
  20 files changed, 277 insertions(+), 279 deletions(-)

---
base-commit: 82706188517bd102ed25f1931c4cb041ecf3ef50
change-id: 20260825-virtio-gpu-cleanup-dfb2ba185936

Best regards,

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

Hi

Quoted 25 lines · Akihiko Odaki
Quoted 23 lines · Akihiko Odaki
Quoted 21 lines · Marc-André Lureau

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.

This series makes each virtio_gpu_scanout own its current dmabuf and uses resource_id to track which resource it displays. It also makes QemuDmaBuf own its file descriptors and moves GTK fence state out of the dmabuf.

Those changes touch a lot of area and I have done basic testing with the various backends. I would not recommend backporting them to stable:
there is a high risk of introducing regressions. Yet, I don't know how to address all the problems uncovered by CVE-2026-66020 in a simple patch series. Help welcome!

Quoted 52 lines
Quoted 2 lines

If you mean "all the problems", my coding agent tells there are some problems remaining:

I forgot to note that I manually reviewed the claims.

I also think you can drop RFC. Though the series is large, each patch is small and I think reasonable.

Quoted 44 lines

1. Vhost-user still has duplicate, incomplete DMA-BUF ownership.

VhostUserGPU retains its separate g->dmabuf[]. The contrib backend disables a scanout using ordinary VHOST_USER_GPU_SCANOUT with zero dimensions, but the QEMU frontend handles that by only calling qemu_console_set_surface().

Although that changes con->scanout.kind, it neither releases g-

Quoted 1 line

dmabuf[id] nor invokes the frontend GL-disable callback. GTK/SDL can

therefore retain their private scanout mode and redraw the old buffer; the wrapper and owned fd remain until another DMA-BUF message. Reset and finalize also never free this array.

Separately, contrib’s virgl RESOURCE_UNREF destroys the renderer resource without disabling scanouts using it. The in-process virgl backend fixes precisely this case, but contrib does not.

2. GTK DMA-BUF texture release lacks the required GL context.

GTK’s release callback directly calls egl_dmabuf_release_texture(), which calls glDeleteTextures() and then unconditionally forgets the texture ID. Neither it nor the core release dispatcher establishes the owning context.

This is reachable after another GTK console’s surface update leaves EGL_NO_CONTEXT current. Deletion is then not guaranteed, yet the ID is lost, leaking the GL import and backing. With another unshared context current, a coincident texture name could be deleted from the wrong context. SDL has the same missing-context pattern.

3. Rutabaga resource destruction remains incomplete.

virtio_gpu_rutabaga_resource_unref() calls Rutabaga unref and frees the QEMU resource, but does not clean res->iov/res->addrs or remove a mapped blob’s host-visible MemoryRegion. Only explicit RESOURCE_UNMAP_BLOB removes that region.

Consequently, MAP_BLOB → RESOURCE_UNREF or reset can leave a MemoryRegion pointing at storage destroyed by Rutabaga, as well as pinned DMA mappings. This is adjacent to, rather than the original scanout-detach CVE, but it is another unresolved blob-ownership problem.

...so there is still some work to be done.

Thanks for the analysis. Do you think we can review & apply this series first before addressing the remaining issues? (and helping me :)

Quoted 8 lines
Quoted 8 lines

But when it comes to backporting, I think we can instead do focused backporting to avoid extra trouble. Concretely, the patches you tagged with "Fixes: CVE-2026-66020" can be backported, namely:

- "[PATCH 10/18] virtio-gpu: disable blob scanouts on mapping cleanup" and
- "[PATCH 18/18] virtio-gpu: fix use-after-free on blob cursor after detach"

Cc: qemu-stable for those two only? even if there are still corner cases addressed by the other patches)?

thanks again

#30 lore raw
Quoted 80 lines · Marc-André Lureau

Hi

Quoted 76 lines · Akihiko Odaki
Quoted 17 lines · Akihiko Odaki
Quoted 16 lines · Marc-André Lureau

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.

This series makes each virtio_gpu_scanout own its current dmabuf and uses resource_id to track which resource it displays. It also makes QemuDmaBuf own its file descriptors and moves GTK fence state out of the dmabuf.

Quoted 8 lines
Quoted 5 lines

Those changes touch a lot of area and I have done basic testing with the various backends. I would not recommend backporting them to stable:
there is a high risk of introducing regressions. Yet, I don't know how to address all the problems uncovered by CVE-2026-66020 in a simple patch series. Help welcome!

If you mean "all the problems", my coding agent tells there are some problems remaining:

I forgot to note that I manually reviewed the claims.

I also think you can drop RFC. Though the series is large, each patch is small and I think reasonable.

Quoted 43 lines

1. Vhost-user still has duplicate, incomplete DMA-BUF ownership.

VhostUserGPU retains its separate g->dmabuf[]. The contrib backend disables a scanout using ordinary VHOST_USER_GPU_SCANOUT with zero dimensions, but the QEMU frontend handles that by only calling qemu_console_set_surface().

Although that changes con->scanout.kind, it neither releases g-

Quoted 1 line

dmabuf[id] nor invokes the frontend GL-disable callback. GTK/SDL can

therefore retain their private scanout mode and redraw the old buffer; the wrapper and owned fd remain until another DMA-BUF message. Reset and finalize also never free this array.

Separately, contrib’s virgl RESOURCE_UNREF destroys the renderer resource without disabling scanouts using it. The in-process virgl backend fixes precisely this case, but contrib does not.

2. GTK DMA-BUF texture release lacks the required GL context.

GTK’s release callback directly calls egl_dmabuf_release_texture(), which calls glDeleteTextures() and then unconditionally forgets the texture ID. Neither it nor the core release dispatcher establishes the owning context.

This is reachable after another GTK console’s surface update leaves EGL_NO_CONTEXT current. Deletion is then not guaranteed, yet the ID is lost, leaking the GL import and backing. With another unshared context current, a coincident texture name could be deleted from the wrong context. SDL has the same missing-context pattern.

3. Rutabaga resource destruction remains incomplete.

virtio_gpu_rutabaga_resource_unref() calls Rutabaga unref and frees the QEMU resource, but does not clean res->iov/res->addrs or remove a mapped blob’s host-visible MemoryRegion. Only explicit RESOURCE_UNMAP_BLOB removes that region.

Consequently, MAP_BLOB → RESOURCE_UNREF or reset can leave a MemoryRegion pointing at storage destroyed by Rutabaga, as well as pinned DMA mappings. This is adjacent to, rather than the original scanout-detach CVE, but it is another unresolved blob-ownership problem.

Quoted 5 lines
Quoted 1 line
Quoted 1 line

...so there is still some work to be done.

Thanks for the analysis. Do you think we can review & apply this series first before addressing the remaining issues? (and helping me :)

For vhost-user and Rutabaga, yes: I think we can review this series without making it solve every ownership problem first. Those paths are separate and can be follow-ups.

It may be better to handle the GTK/SDL context issue before applying the series, though. The callback already existed, but patches 7 and 10 add release-on-disable and release-on-detach callers. They can invoke glDeleteTextures() with no context, or with another console's context current. A small additional patch should suffice; this does not require keeping the series as RFC.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

But when it comes to backporting, I think we can instead do focused backporting to avoid extra trouble. Concretely, the patches you tagged with "Fixes: CVE-2026-66020" can be backported, namely:

Quoted 6 lines
Quoted 3 lines
Quoted 3 lines

- "[PATCH 10/18] virtio-gpu: disable blob scanouts on mapping cleanup" and
- "[PATCH 18/18] virtio-gpu: fix use-after-free on blob cursor after detach"

Cc: qemu-stable for those two only? even if there are still corner cases addressed by the other patches)?

Yes. I don't think the other corner cases are worthwhile to fix risking a regression.

But rechecking these patches, I found "[PATCH 18/18] virtio-gpu: fix use-after-free on blob cursor after detach" has two dependencies on the earlier patches:

- Patch 6 changes the signature of virtio_gpu_fini_udmabuf().
- Patch 17 allows removing the res->blob guard in virtio_gpu_cleanup_mapping().

So I suggest:

- reordering the series to put patch 10 and patch 18 at the first,
- keeping the res->blob guard in patch 18, and
- dropping the res->blob guard in patch 17 instead.

Regards,
Akihiko Odaki