virtio-gpu: run scanout reset cleanup on the main loop thread

virtio_gpu_gl_reset() executes in the context of the thread that wrote the
device reset register, i.e. a TCG vCPU thread.  It calls
virtio_gpu_virgl_reset_scanout() -> egl_scanout_disable() ->
egl_fb_destroy() -> glDeleteFramebuffers(), which requires the display EGL
context to be current on that thread.  It is not:

  #01 egl_fb_destroy                ui/egl-helpers.c
  #02 egl_scanout_disable           ui/egl-headless.c
  #04 virtio_gpu_virgl_reset_scanout
  #05 virtio_gpu_gl_reset           hw/display/virtio-gpu-gl.c
  #11 do_st_mmio_leN                accel/tcg/cputlb.c

Hosts whose GL implementation dereferences the missing context crash.  This
was reproduced on Android/aarch64 with both zink and llvmpipe, which makes
it a threading bug rather than a driver bug.

Defer the cleanup to the main loop: add a VirtIOGPUClass reset_scanout
callback, invoke it from the existing virtio_gpu_reset_bh() before
reset_finished is signalled, and reduce virtio_gpu_gl_reset() to setting
renderer_state to RS_RESET.

Signed-off-by: oumeng <[email protected]>
---
 hw/display/virtio-gpu-gl.c     | 13 ++++++++++++-
 hw/display/virtio-gpu.c        |  5 +++++
 include/hw/virtio/virtio-gpu.h |  1 +
 3 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/hw/display/virtio-gpu-gl.c b/hw/display/virtio-gpu-gl.c
index 2b7a41c..9a93fdf 100644
--- a/hw/display/virtio-gpu-gl.c
+++ b/hw/display/virtio-gpu-gl.c
@@ -99,7 +99,8 @@ static void virtio_gpu_gl_reset(VirtIODevice *vdev)
      * thread, and when the renderer is unblocked.
      */
     if (gl->renderer_state == RS_INITED) {
-        virtio_gpu_virgl_reset_scanout(g);
+        /* scanout cleanup is deferred to virtio_gpu_reset_bh() which runs
+         * on the main loop thread (reset_scanout class callback). */
         gl->renderer_state = RS_RESET;
     }
 }
@@ -209,6 +210,15 @@ static void
virtio_gpu_gl_device_unrealize(DeviceState *qdev)
      */
 }

+static void virtio_gpu_gl_reset_scanout(VirtIOGPU *g)
+{
+    VirtIOGPUGL *gl = VIRTIO_GPU_GL(g);
+
+    if (gl->renderer_state == RS_INITED) {
+        virtio_gpu_virgl_reset_scanout(g);
+    }
+}
+
 static void virtio_gpu_gl_class_init(ObjectClass *klass, const void *data)
 {
     DeviceClass *dc = DEVICE_CLASS(klass);
@@ -222,6 +232,7 @@ static void virtio_gpu_gl_class_init(ObjectClass
*klass, const void *data)
     vgc->update_cursor_data = virtio_gpu_gl_update_cursor_data;

     vgc->resource_destroy = virtio_gpu_virgl_resource_destroy;
+    vgc->reset_scanout = virtio_gpu_gl_reset_scanout;
     vdc->realize = virtio_gpu_gl_device_realize;
     vdc->unrealize = virtio_gpu_gl_device_unrealize;
     vdc->reset = virtio_gpu_gl_reset;
diff --git a/hw/display/virtio-gpu.c b/hw/display/virtio-gpu.c
index 5d43ffb..4a2789f 100644
--- a/hw/display/virtio-gpu.c
+++ b/hw/display/virtio-gpu.c
@@ -1841,6 +1841,11 @@ static void virtio_gpu_reset_bh(void *opaque)
         qemu_console_set_surface(g->parent_obj.scanout[i].con, NULL);
     }

+    /* scanout/GL cleanup must run here, on the main loop thread */
+    if (vgc->reset_scanout) {
+        vgc->reset_scanout(g);
+    }
+
     g->reset_finished = true;
     qemu_cond_signal(&g->reset_cond);
 }
diff --git a/include/hw/virtio/virtio-gpu.h b/include/hw/virtio/virtio-gpu.h
index 631dfc6..91072b0 100644
--- a/include/hw/virtio/virtio-gpu.h
+++ b/include/hw/virtio/virtio-gpu.h
@@ -234,6 +234,7 @@ struct VirtIOGPUClass {
     void (*resource_destroy)(VirtIOGPU *g,
                              struct virtio_gpu_simple_resource *res,
                              Error **errp);
+    void (*reset_scanout)(VirtIOGPU *g);
 };

 struct virtio_gpu_virgl_context_fence {
-- 
2.53.0

Reply via email to