virgl_write_fence() completes and frees every command on fenceq whose
fence_id is not above the signalled fence, without checking its state.
GET_DISPLAY_INFO and GET_EDID wait for a reply from the frontend on that
same queue, in VG_CMD_STATE_PENDING with a zero fence_id, so the next
signalled fence frees them. The reply callback then takes QTAILQ_LAST()
of the queue, which is either empty or another command, and crashes or
trips the type assertion.
Keep the command that waits for the frontend reply in VuGpu instead,
free it from the reply callback, and don't put it on fenceq at all, so
that the queue and the inflight count only track commands submitted to
virglrenderer.
Fixes: bd690feb1554 ("vhost-user-gpu: handle display-info in a callback")
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4848
Reported-by: Dmitry R <[email protected]>
Signed-off-by: Marc-André Lureau <[email protected]>
---
contrib/vhost-user-gpu/vhost-user-gpu.c | 21 +++++++++++++++------
contrib/vhost-user-gpu/virgl.c | 4 ++++
contrib/vhost-user-gpu/vugpu.h | 3 +++
3 files changed, 22 insertions(+), 6 deletions(-)
diff --git a/contrib/vhost-user-gpu/vhost-user-gpu.c
b/contrib/vhost-user-gpu/vhost-user-gpu.c
index f70b34c179f3..5240b171d538 100644
--- a/contrib/vhost-user-gpu/vhost-user-gpu.c
+++ b/contrib/vhost-user-gpu/vhost-user-gpu.c
@@ -268,17 +268,18 @@ get_display_info_cb(gint fd, GIOCondition condition,
gpointer user_data)
{
struct virtio_gpu_resp_display_info dpy_info = { {} };
VuGpu *vg = user_data;
- struct virtio_gpu_ctrl_command *cmd = QTAILQ_LAST(&vg->fenceq);
+ struct virtio_gpu_ctrl_command *cmd = vg->pending_cmd;
g_debug("disp info cb");
- assert(cmd->cmd_hdr.type == VIRTIO_GPU_CMD_GET_DISPLAY_INFO);
+ assert(cmd && cmd->cmd_hdr.type == VIRTIO_GPU_CMD_GET_DISPLAY_INFO);
if (!vg_recv_msg(vg, VHOST_USER_GPU_GET_DISPLAY_INFO,
sizeof(dpy_info), &dpy_info)) {
return G_SOURCE_CONTINUE;
}
- QTAILQ_REMOVE(&vg->fenceq, cmd, next);
+ vg->pending_cmd = NULL;
vg_ctrl_response(vg, cmd, &dpy_info.hdr, sizeof(dpy_info));
+ free(cmd);
vg->wait_in = 0;
vg_handle_ctrl(&vg->dev.parent, 0);
@@ -297,6 +298,7 @@ vg_get_display_info(VuGpu *vg, struct
virtio_gpu_ctrl_command *cmd)
assert(vg->wait_in == 0);
vg_send_msg(vg, &msg, -1);
+ vg->pending_cmd = cmd;
vg->wait_in = g_unix_fd_add(vg->sock_fd, G_IO_IN | G_IO_HUP,
get_display_info_cb, vg);
cmd->state = VG_CMD_STATE_PENDING;
@@ -307,17 +309,18 @@ get_edid_cb(gint fd, GIOCondition condition, gpointer
user_data)
{
struct virtio_gpu_resp_edid resp_edid;
VuGpu *vg = user_data;
- struct virtio_gpu_ctrl_command *cmd = QTAILQ_LAST(&vg->fenceq);
+ struct virtio_gpu_ctrl_command *cmd = vg->pending_cmd;
g_debug("get edid cb");
- assert(cmd->cmd_hdr.type == VIRTIO_GPU_CMD_GET_EDID);
+ assert(cmd && cmd->cmd_hdr.type == VIRTIO_GPU_CMD_GET_EDID);
if (!vg_recv_msg(vg, VHOST_USER_GPU_GET_EDID,
sizeof(resp_edid), &resp_edid)) {
return G_SOURCE_CONTINUE;
}
- QTAILQ_REMOVE(&vg->fenceq, cmd, next);
+ vg->pending_cmd = NULL;
vg_ctrl_response(vg, cmd, &resp_edid.hdr, sizeof(resp_edid));
+ free(cmd);
vg->wait_in = 0;
vg_handle_ctrl(&vg->dev.parent, 0);
@@ -344,6 +347,7 @@ vg_get_edid(VuGpu *vg, struct virtio_gpu_ctrl_command *cmd)
assert(vg->wait_in == 0);
vg_send_msg(vg, &msg, -1);
+ vg->pending_cmd = cmd;
vg->wait_in = g_unix_fd_add(vg->sock_fd, G_IO_IN | G_IO_HUP,
get_edid_cb, vg);
cmd->state = VG_CMD_STATE_PENDING;
@@ -940,6 +944,11 @@ vg_handle_ctrl(VuDev *dev, int qidx)
}
}
+ if (cmd->state == VG_CMD_STATE_PENDING) {
+ /* freed by the frontend reply callback */
+ continue;
+ }
+
if (cmd->state != VG_CMD_STATE_FINISHED) {
QTAILQ_INSERT_TAIL(&vg->fenceq, cmd, next);
vg->inflight++;
diff --git a/contrib/vhost-user-gpu/virgl.c b/contrib/vhost-user-gpu/virgl.c
index 0ef4b9d8c903..3242bf13586d 100644
--- a/contrib/vhost-user-gpu/virgl.c
+++ b/contrib/vhost-user-gpu/virgl.c
@@ -608,6 +608,10 @@ virgl_write_fence(void *opaque, uint32_t fence)
* the guest can end up emitting fences out of order
* so we should check all fenced cmds not just the first one.
*/
+ if (cmd->state == VG_CMD_STATE_PENDING) {
+ /* not fenced, completed by the frontend reply */
+ continue;
+ }
if (cmd->cmd_hdr.fence_id > fence) {
continue;
}
diff --git a/contrib/vhost-user-gpu/vugpu.h b/contrib/vhost-user-gpu/vugpu.h
index aaf2870cb24d..1c2b0ed4fbe8 100644
--- a/contrib/vhost-user-gpu/vugpu.h
+++ b/contrib/vhost-user-gpu/vugpu.h
@@ -147,6 +147,9 @@ typedef struct VuGpu {
struct virtio_gpu_scanout scanout[VIRTIO_GPU_MAX_SCANOUTS];
QTAILQ_HEAD(, virtio_gpu_simple_resource) reslist;
QTAILQ_HEAD(, virtio_gpu_ctrl_command) fenceq;
+
+ /* command waiting for a reply from the frontend */
+ struct virtio_gpu_ctrl_command *pending_cmd;
} VuGpu;
enum {
--
2.56.0.116.g6de20f6092dc