virgl: Set res->maybe_busy to true when creating resources
authorJiang Feng <jiangfeng@kylinos.cn>
Mon, 18 Jul 2022 06:53:26 +0000 (14:53 +0800)
committerMarge Bot <emma+marge@anholt.net>
Tue, 2 Aug 2022 07:27:28 +0000 (07:27 +0000)
commitfe9333f7b5e274b683033443bb008d24822b9d55
tree4257ee65a76c796724dee1d01feacf6f468982fd
parenta19dd43082869f213b25fdcc5c2ec617532e7b64
virgl: Set res->maybe_busy to true when creating resources

Currently, res->maybe_busy is false by default. If wait immediately
after the resource is created, virgl_drm_resource_wait() will return
directly without checking the actual state of the kernel, which will
cause synchronization problems, such as:

On Guest:
  pipe_buffer_create [mesa]
    virgl_drm_winsys_resource_create
      virtio_gpu_resource_create_ioctl [kernel]
        virtio_gpu_fence_alloc
        virtio_gpu_object_create
          virtio_gpu_cmd_resource_create_3d
            VIRTIO_GPU_CMD_RESOURCE_CREATE_3D
          virtio_gpu_object_attach
            virtio_gpu_cmd_resource_attach_backing
              VIRTIO_GPU_CMD_RESOURCE_ATTACH_BACKING
  resource_wait [mesa]
    virgl_drm_resource_wait /* return directly without fence waiting */
  pipe_buffer_map [mesa]
    virgl_drm_resource_map
      virtio_gpu_map_ioctl [kernel]
      os_mmap
  memcpy /* <== here */

On Host (with QEMU):
  VIRTIO_GPU_CMD_RESOURCE_CREATE_3D
    virgl_cmd_create_resource_3d [qemu]
      virgl_renderer_resource_create [virglrenderer]
  VIRTIO_GPU_CMD_RESOURCE_ATTACH_BACKING
    virgl_resource_attach_backing [qemu]
      virtio_gpu_create_mapping_iov
      virgl_renderer_resource_attach_iov [virglrenderer]
        virgl_resource_attach_iov
          vrend_pipe_resource_attach_iov
            vrend_write_to_iovec /* <== here */
      virtio_gpu_cleanup_mapping_iov [qemu]

In the example above, there is a race condition between memcpy and
vrend_write_to_iovec.

Signed-off-by: Jiang Feng <jiangfeng@kylinos.cn>
Reviewed-by: Gert Wollny <gert.wollny@collabora.com>
Reviewed-by: Chia-I Wu <olvaffe@gmail.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/17592>
src/gallium/winsys/virgl/drm/virgl_drm_winsys.c