virtio-gpu: don't stall the control queue behind a blob unmap

Unmapping a hostmem blob detaches its MemoryRegion, and the virgl mapping
has to survive until that MR is fully unreferenced (a flat view may still
be under an RCU reader), which takes ~100 ms.  Blocking all command
processing for that grace period, as before, serialized a burst of unmaps
into multi-second round trips for every other command: a D3D12 guest
tearing down hundreds of staging heaps pushed unrelated packets past
dxgkrnl's timeout, and the app died with DXGI_ERROR_DEVICE_RESET.

Keep the queue flowing instead.  UNMAP_BLOB is answered as soon as the
subregion is gone -- the guest cannot reach the pages any more -- and the
virgl unmap runs from the resume BH once the region finalizes.  Only work
that needs that unmap to have happened waits for it, and per resource
rather than per queue: an UNREF of a still-mapped resource is completed
(virgl unref + response) from the same BH, leaving the queue meanwhile
(cmd->deferred), and a re-MAP of a resource whose previous mapping is
still going away is the one case that still suspends the queue.

Assisted-by: Claude:claude-fable-5
This commit is contained in:
osy committed 2026-08-30 02:18:07 -07:00
1 parent 3824e0d996
commit 3bdba2ae2e
3 files changed
+210 -55

No files matched your search

+188 -55
View File
@@ -47,6 +47,12 @@
struct virtio_gpu_virgl_resource {
struct virtio_gpu_simple_resource base;
MemoryRegion *mr;
/* RESOURCE_UNREF that arrived while the blob mapping was still being
* torn down; completed (virgl unref + response) once the MR finalizes. */
struct virtio_gpu_ctrl_command *unref_cmd;
/* A MAP_BLOB is suspended at the cmdq head (renderer_blocked held)
* until the in-flight unmap of this resource finalizes. */
bool map_blocked;
};
static struct virtio_gpu_virgl_resource *
@@ -75,7 +81,11 @@ struct virtio_gpu_virgl_hostmem_region {
Object parent_obj;
MemoryRegion mr;
struct VirtIOGPU *g;
struct virtio_gpu_virgl_resource *res;
/* Subregion detached; the MR is being finalized asynchronously. */
bool unmapping;
bool finish_unmapping;
QTAILQ_ENTRY(virtio_gpu_virgl_hostmem_region) done_next;
};
#define TYPE_VIRTIO_GPU_VIRGL_HOSTMEM_REGION "virtio-gpu-virgl-hostmem-region"
@@ -89,23 +99,86 @@ to_hostmem_region(MemoryRegion *mr)
return container_of(mr, struct virtio_gpu_virgl_hostmem_region, mr);
}
/*
* Finish the unmap of a hostmem region whose MemoryRegion has finalized:
* drop the virgl mapping, and complete a RESOURCE_UNREF that was deferred
* behind it. Main-loop only (virglrenderer runs on the GL thread).
*/
static void
virtio_gpu_virgl_finish_unmap(VirtIOGPU *g,
struct virtio_gpu_virgl_hostmem_region *vmr)
{
struct virtio_gpu_virgl_resource *res = vmr->res;
VirtIOGPUBase *b = VIRTIO_GPU_BASE(g);
VirtIOGPUGL *gl = VIRTIO_GPU_GL(g);
struct virtio_gpu_ctrl_command *cmd;
struct iovec *res_iovs = NULL;
int num_iovs = 0;
int ret;
QTAILQ_REMOVE(&gl->unmap_done_list, vmr, done_next);
res->mr = NULL;
g_free(vmr);
ret = virgl_renderer_resource_unmap(res->base.resource_id);
if (ret) {
qemu_log_mask(LOG_GUEST_ERROR,
"%s: failed to unmap virgl resource: %s\n",
__func__, strerror(-ret));
}
if (res->map_blocked) {
res->map_blocked = false;
b->renderer_blocked--;
}
cmd = res->unref_cmd;
if (!cmd) {
return;
}
res->unref_cmd = NULL;
virgl_renderer_resource_detach_iov(res->base.resource_id,
&res_iovs, &num_iovs);
if (res_iovs != NULL && num_iovs != 0) {
virtio_gpu_cleanup_mapping_iov(g, res_iovs, num_iovs);
}
virgl_renderer_resource_unref(res->base.resource_id);
QTAILQ_REMOVE(&g->reslist, &res->base, next);
g_free(res);
virtio_gpu_ctrl_response_nodata(g, cmd, VIRTIO_GPU_RESP_OK_NODATA);
g_free(cmd);
}
static void virtio_gpu_virgl_resume_cmdq_bh(void *opaque)
{
VirtIOGPU *g = opaque;
VirtIOGPUGL *gl = VIRTIO_GPU_GL(g);
struct virtio_gpu_virgl_hostmem_region *vmr;
while ((vmr = QTAILQ_FIRST(&gl->unmap_done_list)) != NULL) {
virtio_gpu_virgl_finish_unmap(g, vmr);
}
virtio_gpu_process_cmdq(g);
}
/*
* MR could outlive the resource if MR's reference is held outside of
* virtio-gpu. In order to prevent unmapping resource while MR is alive,
* and thus, making the data pointer invalid, we will block virtio-gpu
* command processing until MR is fully unreferenced and freed.
* virtio-gpu (a flat view still under an RCU reader), so the virgl mapping
* stays alive until the MR is fully unreferenced and freed; only then is
* the resource unmapped. That grace period is on the order of 100 ms, and
* blocking the whole control queue for it (as upstream does) turns a burst
* of unmaps into multi-second round trips for every other command -- past
* a WDDM guest's GPU-scheduler timeout. The queue therefore keeps flowing:
* UNMAP_BLOB is answered once the subregion is gone (the guest cannot reach
* the pages any more), and only work that needs the virgl unmap to have
* happened -- an UNREF of the resource, a re-MAP -- waits for it.
*/
static void virtio_gpu_virgl_hostmem_region_finalize(Object *obj)
{
struct virtio_gpu_virgl_hostmem_region *vmr = VIRTIO_GPU_VIRGL_HOSTMEM_REGION(obj);
VirtIOGPUBase *b;
VirtIOGPUGL *gl;
if (!vmr->g) {
@@ -114,15 +187,13 @@ static void virtio_gpu_virgl_hostmem_region_finalize(Object *obj)
vmr->finish_unmapping = true;
b = VIRTIO_GPU_BASE(vmr->g);
b->renderer_blocked--;
/*
* memory_region_unref() is executed from RCU thread context, while
* virglrenderer works only on the main-loop thread that's holding GL
* context.
*/
gl = VIRTIO_GPU_GL(vmr->g);
QTAILQ_INSERT_TAIL(&gl->unmap_done_list, vmr, done_next);
qemu_bh_schedule(gl->cmdq_resume_bh);
}
@@ -170,6 +241,7 @@ virtio_gpu_virgl_map_resource_blob(VirtIOGPU *g,
object_initialize_child(OBJECT(g), name, vmr,
TYPE_VIRTIO_GPU_VIRGL_HOSTMEM_REGION);
vmr->g = g;
vmr->res = res;
mr = &vmr->mr;
memory_region_init_ram_ptr(mr, OBJECT(vmr), "mr", size, data);
@@ -181,55 +253,47 @@ virtio_gpu_virgl_map_resource_blob(VirtIOGPU *g,
return 0;
}
static int
/*
* Begin (or, once the MR has finalized, finish) unmapping a blob:
*
* 1. Detach the subregion: the guest loses access immediately, and the
* MR goes away asynchronously once its last (RCU) reference drops.
* 2. virtio_gpu_virgl_hostmem_region_finalize() queues the region for the
* main loop, which drops the virgl mapping and completes any UNREF
* deferred behind it (virtio_gpu_virgl_finish_unmap).
*
* Returns true while the unmap is still in flight (res->mr stays set until
* it completes), false when the resource is unmapped on return.
*/
static bool
virtio_gpu_virgl_unmap_resource_blob(VirtIOGPU *g,
struct virtio_gpu_virgl_resource *res,
bool *cmd_suspended)
struct virtio_gpu_virgl_resource *res)
{
struct virtio_gpu_virgl_hostmem_region *vmr;
VirtIOGPUBase *b = VIRTIO_GPU_BASE(g);
MemoryRegion *mr = res->mr;
int ret;
if (!mr) {
return 0;
return false;
}
vmr = to_hostmem_region(res->mr);
/*
* Perform async unmapping in 3 steps:
*
* 1. Begin async unmapping with memory_region_del_subregion()
* and suspend/block cmd processing.
* 2. Wait for res->mr to be freed and cmd processing resumed
* asynchronously by virtio_gpu_virgl_hostmem_region_finalize().
* 3. Finish the unmapping with final virgl_renderer_resource_unmap().
*/
if (vmr->finish_unmapping) {
res->mr = NULL;
g_free(vmr);
ret = virgl_renderer_resource_unmap(res->base.resource_id);
if (ret) {
qemu_log_mask(LOG_GUEST_ERROR,
"%s: failed to unmap virgl resource: %s\n",
__func__, strerror(-ret));
return ret;
}
} else {
*cmd_suspended = true;
/* render will be unblocked once MR is freed */
b->renderer_blocked++;
/* Finalized, but the completion BH has not run yet: finish here. */
virtio_gpu_virgl_finish_unmap(g, vmr);
return false;
}
if (!vmr->unmapping) {
vmr->unmapping = true;
/* memory region owns self res->mr object and frees it by itself */
memory_region_set_enabled(mr, false);
memory_region_del_subregion(&b->hostmem, mr);
object_unparent(OBJECT(vmr));
}
return 0;
return true;
}
static void
@@ -238,24 +302,42 @@ virtio_gpu_virgl_destroy_hostmem_region(VirtIOGPU *g,
{
struct virtio_gpu_virgl_hostmem_region *vmr = to_hostmem_region(res->mr);
VirtIOGPUBase *b = VIRTIO_GPU_BASE(g);
VirtIOGPUGL *gl = VIRTIO_GPU_GL(g);
/*
* Reset drops queued commands without a response (see the cmdq flush in
* virtio_gpu_reset); a deferred UNREF is no different. The resource is
* being destroyed by the caller either way.
*/
if (res->unref_cmd) {
g_free(res->unref_cmd);
res->unref_cmd = NULL;
}
if (res->map_blocked) {
res->map_blocked = false;
b->renderer_blocked--;
}
/*
* vmr is not QOM-owned, so object_finalize() does not free it; the unmap
* step 3 normally does. Reset can run here at any stage of an in-flight
* unmap, where step 3 may not run. This and the finalizer both hold the
* BQL, so free vmr or hand it to object_finalize() per the stage.
* completion normally does. Reset can run here at any stage of an
* in-flight unmap, where the completion may not run. This and the
* finalizer both hold the BQL, so free vmr or hand it to
* object_finalize() per the stage.
*/
if (vmr->finish_unmapping) {
/* Finalizer ran and balanced renderer_blocked; just free vmr. */
/* Finalizer ran; the completion BH has not: just free vmr. */
QTAILQ_REMOVE(&gl->unmap_done_list, vmr, done_next);
res->mr = NULL;
g_free(vmr);
virgl_renderer_resource_unmap(res->base.resource_id);
return;
}
if (res->mr->container != &b->hostmem) {
if (vmr->unmapping) {
/* Async unmap detached the subregion; let the finalizer free vmr. */
OBJECT(vmr)->free = g_free;
vmr->g = NULL;
res->mr = NULL;
return;
}
@@ -418,11 +500,21 @@ static void virgl_cmd_resource_unref(VirtIOGPU *g,
}
#if VIRGL_VERSION_MAJOR >= 1
if (virtio_gpu_virgl_unmap_resource_blob(g, res, cmd_suspended)) {
cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
if (res->unref_cmd) {
qemu_log_mask(LOG_GUEST_ERROR, "%s: resource %d already being unref'd\n",
__func__, unref.resource_id);
cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
return;
}
if (*cmd_suspended) {
if (virtio_gpu_virgl_unmap_resource_blob(g, res)) {
/*
* The mapping is still being torn down: the virgl unref must wait
* for it, and so must this command's completion -- but not the
* commands behind it. virtio_gpu_virgl_finish_unmap() completes
* the unref and answers the command.
*/
res->unref_cmd = cmd;
cmd->deferred = true;
return;
}
#endif
@@ -890,7 +982,8 @@ static void virgl_cmd_resource_create_blob(VirtIOGPU *g,
}
static void virgl_cmd_resource_map_blob(VirtIOGPU *g,
struct virtio_gpu_ctrl_command *cmd)
struct virtio_gpu_ctrl_command *cmd,
bool *cmd_suspended)
{
struct virtio_gpu_resource_map_blob mblob;
struct virtio_gpu_virgl_resource *res;
@@ -908,6 +1001,38 @@ static void virgl_cmd_resource_map_blob(VirtIOGPU *g,
return;
}
if (res->unref_cmd) {
qemu_log_mask(LOG_GUEST_ERROR, "%s: resource %d is being unref'd\n",
__func__, mblob.resource_id);
cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
return;
}
if (res->mr) {
struct virtio_gpu_virgl_hostmem_region *vmr = to_hostmem_region(res->mr);
if (!vmr->unmapping) {
qemu_log_mask(LOG_GUEST_ERROR, "%s: resource %d already mapped\n",
__func__, mblob.resource_id);
cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER;
return;
}
if (virtio_gpu_virgl_unmap_resource_blob(g, res)) {
/*
* Re-mapping a blob whose previous mapping is still being torn
* down: the virgl map must follow the virgl unmap, so wait it
* out at the cmdq head (rare; the guest normally has the UNREF
* or a fresh resource by now).
*/
if (!res->map_blocked) {
res->map_blocked = true;
VIRTIO_GPU_BASE(g)->renderer_blocked++;
}
*cmd_suspended = true;
return;
}
}
ret = virtio_gpu_virgl_map_resource_blob(g, res, mblob.offset);
if (ret) {
cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
@@ -921,12 +1046,10 @@ static void virgl_cmd_resource_map_blob(VirtIOGPU *g,
}
static void virgl_cmd_resource_unmap_blob(VirtIOGPU *g,
struct virtio_gpu_ctrl_command *cmd,
bool *cmd_suspended)
struct virtio_gpu_ctrl_command *cmd)
{
struct virtio_gpu_resource_unmap_blob ublob;
struct virtio_gpu_virgl_resource *res;
int ret;
VIRTIO_GPU_FILL_CMD(ublob);
virtio_gpu_unmap_blob_bswap(&ublob);
@@ -939,11 +1062,20 @@ static void virgl_cmd_resource_unmap_blob(VirtIOGPU *g,
return;
}
ret = virtio_gpu_virgl_unmap_resource_blob(g, res, cmd_suspended);
if (ret) {
cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
if (res->unref_cmd) {
qemu_log_mask(LOG_GUEST_ERROR, "%s: resource %d is being unref'd\n",
__func__, ublob.resource_id);
cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
return;
}
/*
* The subregion is gone before this returns, so the guest cannot reach
* the pages any more: answer now. Dropping the virgl mapping waits for
* the MR to finalize but nothing the guest can do depends on it, except
* an UNREF or a re-MAP of this resource, which each wait on their own.
*/
virtio_gpu_virgl_unmap_resource_blob(g, res);
}
#if defined(HAVE_VIRGL_RENDERER_NATIVE_SCANOUT)
@@ -1153,10 +1285,10 @@ void virtio_gpu_virgl_process_cmd(VirtIOGPU *g,
virgl_cmd_resource_create_blob(g, cmd);
break;
case VIRTIO_GPU_CMD_RESOURCE_MAP_BLOB:
virgl_cmd_resource_map_blob(g, cmd);
virgl_cmd_resource_map_blob(g, cmd, &cmd_suspended);
break;
case VIRTIO_GPU_CMD_RESOURCE_UNMAP_BLOB:
virgl_cmd_resource_unmap_blob(g, cmd, &cmd_suspended);
virgl_cmd_resource_unmap_blob(g, cmd);
break;
case VIRTIO_GPU_CMD_SET_SCANOUT_BLOB:
virgl_cmd_set_scanout_blob(g, cmd);
@@ -1169,7 +1301,7 @@ void virtio_gpu_virgl_process_cmd(VirtIOGPU *g,
cmd->suspended = cmd_suspended;
if (cmd_suspended || cmd->finished) {
if (cmd_suspended || cmd->finished || cmd->deferred) {
return;
}
if (cmd->error) {
@@ -1562,6 +1694,7 @@ int virtio_gpu_virgl_init(VirtIOGPU *g)
}
#if VIRGL_VERSION_MAJOR >= 1
QTAILQ_INIT(&gl->unmap_done_list);
gl->cmdq_resume_bh = virtio_bh_io_new_guarded(DEVICE(g),
virtio_gpu_virgl_resume_cmdq_bh,
g);
+11
View File
@@ -1061,6 +1061,16 @@ void virtio_gpu_process_cmdq(VirtIOGPU *g)
/* process command */
vgc->process_cmd(g, cmd);
/*
* A deferred command is owned by whoever deferred it, which responds
* and frees it once its host-side work lands. Unlike a suspended
* command it must not hold up the queue behind it.
*/
if (cmd->deferred) {
QTAILQ_REMOVE(&g->cmdq, cmd, next);
continue;
}
/*
* A suspended command has not registered its fence (if any) yet
* and must be re-processed at the cmdq head after resume, so it
@@ -1132,6 +1142,7 @@ static void virtio_gpu_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
cmd->error = 0;
cmd->finished = false;
cmd->suspended = false;
cmd->deferred = false;
QTAILQ_INSERT_TAIL(&g->cmdq, cmd, next);
cmd = virtqueue_pop(vq, sizeof(struct virtio_gpu_ctrl_command));
}
+11
View File
@@ -142,6 +142,12 @@ struct virtio_gpu_ctrl_command {
bool finished;
/* Set if process_cmd deferred completion; keep at cmdq head for resume. */
bool suspended;
/*
* Set if process_cmd took ownership of the command to complete it later
* (a mapped blob's teardown waits for the memory region to finalize):
* it leaves cmdq without a response and without blocking the queue.
*/
bool deferred;
QTAILQ_ENTRY(virtio_gpu_ctrl_command) next;
};
@@ -260,6 +266,8 @@ typedef enum {
RS_RESET, /* inited and reset pending, moves to start after reset */
} RenderState;
struct virtio_gpu_virgl_hostmem_region;
struct VirtIOGPUGL {
struct VirtIOGPU parent_obj;
@@ -269,6 +277,9 @@ struct VirtIOGPUGL {
QEMUTimer *print_stats;
QEMUBH *cmdq_resume_bh;
/* Hostmem regions whose MemoryRegion finalized; cmdq_resume_bh finishes
* their unmap (and any deferred unref) on the main loop. */
QTAILQ_HEAD(, virtio_gpu_virgl_hostmem_region) unmap_done_list;
QEMUBH *async_fence_bh;
QSLIST_HEAD(, virtio_gpu_virgl_context_fence) async_fenceq;