@samitouri / QOSamiQemu / commits / 235f9b3638

virtio-gpu: Ensure BHs are invoked only from main-loop thread

QEMU's display GL core is tied to main-loop thread and virtio-gpu interacts with display while processing GPU commands. Virtio-gpu BHs work in generic AIO context that can be invoked on vCPU thread, while GL and UI toolkits are bound to the main-loop thread. Make virtio-gpu BHs use iohandler AIO context that is handled in a main-loop thread only. 0 SDL_GL_MakeCurrent() (libSDL3) 1 SDL_GL_MakeCurrent_REAL() (libSDL2) 2 sdl2_gl_make_context_current() (ui/sdl2-gl.c:201) 3 make_current() (virglrenderer.c:639) 4 vrend_finish_context_switch() (vrend_renderer.c:11630) 5 vrend_hw_switch_context() (vrend_renderer.c:11613) 6 vrend_renderer_force_ctx_0() (vrend_renderer.c:12986) 7 virgl_renderer_force_ctx_0() (virglrenderer.c:460) 8 virtio_gpu_virgl_process_cmd() (virtio-gpu-virgl.c:1013) 9 virtio_gpu_process_cmdq() (virtio-gpu.c:1050) 10 virtio_gpu_gl_handle_ctrl() (virtio-gpu-gl.c:86) 11 aio_bh_poll() (util/async.c) 12 aio_poll() (util/aio-posix.c) 13 blk_pwrite() (block/block-gen.c:1985) 14 pflash_update() (pflash_cfi01.c:396) 15 pflash_write() (pflash_cfi01.c:541) 16 memory_region_dispatch_write() (system/memory.c:1554) 17 flatview_write() (system/physmem.c:3333) 18 address_space_write() (system/physmem.c:3453) 19 kvm_cpu_exec() (accel/kvm/kall-all.c:3248) 20 kvm_vcpu_thread_fn() (accel/kvm/kaccel-ops.c:53) Cc: qemu-stable@nongnu.org Signed-off-by: Dmitry Osipenko <dmitry.osipenko@collabora.com> Message-ID: <20260303151422.977399-8-dmitry.osipenko@collabora.com> Message-ID: <20260304165043.1437519-10-alex.bennee@linaro.org> Signed-off-by: Alex Bennée <alex.bennee@linaro.org>

Dmitry Osipenko committed Mar 4, 2026 at 16:50 UTC 235f9b36383e4cc7a790bca51eddbe38edd5438c
4 files changed +26 -6
hw/display/virtio-gpu-virgl.c
+3 -3
@@ -1203,9 +1203,9 @@ int virtio_gpu_virgl_init(VirtIOGPU *g)
1203 }
1204
1205 #if VIRGL_VERSION_MAJOR >= 1
1206 - gl->cmdq_resume_bh = aio_bh_new(qemu_get_aio_context(),
1207 - virtio_gpu_virgl_resume_cmdq_bh,
1208 - g);
1206 + gl->cmdq_resume_bh = virtio_bh_io_new_guarded(DEVICE(g),
1207 + virtio_gpu_virgl_resume_cmdq_bh,
1208 + g);
1209 #endif
1210
1211 return 0;
hw/display/virtio-gpu.c
+3 -3
@@ -1526,9 +1526,9 @@ void virtio_gpu_device_realize(DeviceState *qdev, Error **errp)
1526
1527 g->ctrl_vq = virtio_get_queue(vdev, 0);
1528 g->cursor_vq = virtio_get_queue(vdev, 1);
1529 - g->ctrl_bh = virtio_bh_new_guarded(qdev, virtio_gpu_ctrl_bh, g);
1530 - g->cursor_bh = virtio_bh_new_guarded(qdev, virtio_gpu_cursor_bh, g);
1531 - g->reset_bh = qemu_bh_new(virtio_gpu_reset_bh, g);
1529 + g->ctrl_bh = virtio_bh_io_new_guarded(qdev, virtio_gpu_ctrl_bh, g);
1530 + g->cursor_bh = virtio_bh_io_new_guarded(qdev, virtio_gpu_cursor_bh, g);
1531 + g->reset_bh = virtio_bh_io_new_guarded(qdev, virtio_gpu_reset_bh, g);
1532 qemu_cond_init(&g->reset_cond);
1533 QTAILQ_INIT(&g->reslist);
1534 QTAILQ_INIT(&g->cmdq);
hw/virtio/virtio.c
+10
@@ -4497,3 +4497,13 @@ QEMUBH *virtio_bh_new_guarded_full(DeviceState *dev,
4497 return qemu_bh_new_full(cb, opaque, name,
4498 &transport->mem_reentrancy_guard);
4499 }
4500 +
4501 +QEMUBH *virtio_bh_io_new_guarded_full(DeviceState *dev,
4502 + QEMUBHFunc *cb, void *opaque,
4503 + const char *name)
4504 +{
4505 + DeviceState *transport = qdev_get_parent_bus(dev)->parent;
4506 +
4507 + return aio_bh_new_full(iohandler_get_aio_context(), cb, opaque, name,
4508 + &transport->mem_reentrancy_guard);
4509 +}
include/hw/virtio/virtio.h
+10
@@ -552,4 +552,14 @@ QEMUBH *virtio_bh_new_guarded_full(DeviceState *dev,
552 #define virtio_bh_new_guarded(dev, cb, opaque) \
553 virtio_bh_new_guarded_full((dev), (cb), (opaque), (stringify(cb)))
554
555 +/*
556 + * The "_io" variant runs BH only on a main-loop thread, while generic BH
557 + * may run on a vCPU thread.
558 + */
559 +QEMUBH *virtio_bh_io_new_guarded_full(DeviceState *dev,
560 + QEMUBHFunc *cb, void *opaque,
561 + const char *name);
562 +#define virtio_bh_io_new_guarded(dev, cb, opaque) \
563 + virtio_bh_io_new_guarded_full((dev), (cb), (opaque), (stringify(cb)))
564 +
565 #endif