<div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Sep 3, 2020 at 2:11 PM Chia-I Wu <<a href="mailto:olvaffe@gmail.com">olvaffe@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Wed, Sep 2, 2020 at 2:09 PM Gurchetan Singh<br>
<<a href="mailto:gurchetansingh@chromium.org" target="_blank">gurchetansingh@chromium.org</a>> wrote:<br>
><br>
> From: Gerd Hoffmann <<a href="mailto:kraxel@redhat.com" target="_blank">kraxel@redhat.com</a>><br>
><br>
> Implement resource create blob as specified.<br>
><br>
> Signed-off-by: Gerd Hoffmann <<a href="mailto:kraxel@redhat.com" target="_blank">kraxel@redhat.com</a>><br>
> Co-developed-by: Gurchetan Singh <<a href="mailto:gurchetansingh@chromium.org" target="_blank">gurchetansingh@chromium.org</a>><br>
> Signed-off-by: Gurchetan Singh <<a href="mailto:gurchetansingh@chromium.org" target="_blank">gurchetansingh@chromium.org</a>><br>
> Acked-by: Tomeu Vizoso <<a href="mailto:tomeu.vizoso@collabora.com" target="_blank">tomeu.vizoso@collabora.com</a>><br>
> ---<br>
> drivers/gpu/drm/virtio/virtgpu_drv.h | 4 +-<br>
> drivers/gpu/drm/virtio/virtgpu_ioctl.c | 136 ++++++++++++++++++++++++<br>
> drivers/gpu/drm/virtio/virtgpu_object.c | 5 +-<br>
> drivers/gpu/drm/virtio/virtgpu_vram.c | 2 +<br>
> 4 files changed, 144 insertions(+), 3 deletions(-)<br>
><br>
> diff --git a/drivers/gpu/drm/virtio/virtgpu_drv.h b/drivers/gpu/drm/virtio/virtgpu_drv.h<br>
> index 6162865c162df..d2ea199dbdb90 100644<br>
> --- a/drivers/gpu/drm/virtio/virtgpu_drv.h<br>
> +++ b/drivers/gpu/drm/virtio/virtgpu_drv.h<br>
> @@ -257,8 +257,8 @@ struct virtio_gpu_fpriv {<br>
> struct mutex context_lock;<br>
> };<br>
><br>
> -/* virtgpu_ioctl.c */<br>
> -#define DRM_VIRTIO_NUM_IOCTLS 10<br>
> +/* virtio_ioctl.c */<br>
> +#define DRM_VIRTIO_NUM_IOCTLS 11<br>
> extern struct drm_ioctl_desc virtio_gpu_ioctls[DRM_VIRTIO_NUM_IOCTLS];<br>
> void virtio_gpu_create_context(struct drm_device *dev, struct drm_file *file);<br>
><br>
> diff --git a/drivers/gpu/drm/virtio/virtgpu_ioctl.c b/drivers/gpu/drm/virtio/virtgpu_ioctl.c<br>
> index 7dbe24248a200..442cbca59c8a5 100644<br>
> --- a/drivers/gpu/drm/virtio/virtgpu_ioctl.c<br>
> +++ b/drivers/gpu/drm/virtio/virtgpu_ioctl.c<br>
> @@ -34,6 +34,10 @@<br>
><br>
> #include "virtgpu_drv.h"<br>
><br>
> +#define VIRTGPU_BLOB_FLAG_USE_MASK (VIRTGPU_BLOB_FLAG_USE_MAPPABLE | \<br>
> + VIRTGPU_BLOB_FLAG_USE_SHAREABLE | \<br>
> + VIRTGPU_BLOB_FLAG_USE_CROSS_DEVICE)<br>
> +<br>
> void virtio_gpu_create_context(struct drm_device *dev, struct drm_file *file)<br>
> {<br>
> struct virtio_gpu_device *vgdev = dev->dev_private;<br>
> @@ -520,6 +524,134 @@ static int virtio_gpu_get_caps_ioctl(struct drm_device *dev,<br>
> return 0;<br>
> }<br>
><br>
> +static int verify_blob(struct virtio_gpu_device *vgdev,<br>
> + struct virtio_gpu_fpriv *vfpriv,<br>
> + struct virtio_gpu_object_params *params,<br>
> + struct drm_virtgpu_resource_create_blob *rc_blob,<br>
> + bool *guest_blob, bool *host3d_blob)<br>
> +{<br>
> + if (!vgdev->has_resource_blob)<br>
> + return -EINVAL;<br>
> +<br>
> + if ((rc_blob->blob_flags & ~VIRTGPU_BLOB_FLAG_USE_MASK) ||<br>
> + !rc_blob->blob_flags)<br>
> + return -EINVAL;<br>
> +<br>
> + if (rc_blob->blob_flags & VIRTGPU_BLOB_FLAG_USE_CROSS_DEVICE) {<br>
> + if (!vgdev->has_resource_assign_uuid)<br>
> + return -EINVAL;<br>
> + }<br>
> +<br>
> + switch (rc_blob->blob_mem) {<br>
> + case VIRTGPU_BLOB_MEM_GUEST:<br>
> + *guest_blob = true;<br>
> + break;<br>
> + case VIRTGPU_BLOB_MEM_HOST3D_GUEST:<br>
> + *guest_blob = true;<br>
> + fallthrough;<br>
> + case VIRTGPU_BLOB_MEM_HOST3D:<br>
> + *host3d_blob = true;<br>
> + break;<br>
> + default:<br>
> + return -EINVAL;<br>
> + }<br>
> +<br>
> + if (*host3d_blob) {<br>
> + if (!vgdev->has_virgl_3d)<br>
> + return -EINVAL;<br>
> +<br>
> + /* Must be dword aligned. */<br>
> + if (rc_blob->cmd_size % 4 != 0)<br>
> + return -EINVAL;<br>
> +<br>
> + params->ctx_id = vfpriv->ctx_id;<br>
> + params->blob_id = rc_blob->blob_id;<br>
> + } else {<br>
> + if (rc_blob->blob_id != 0)<br>
> + return -EINVAL;<br>
> +<br>
> + if (rc_blob->cmd_size != 0)<br>
> + return -EINVAL;<br>
> + }<br>
> +<br>
> + params->blob_mem = rc_blob->blob_mem;<br>
> + params->size = rc_blob->size;<br>
> + params->blob = true;<br>
> + params->blob_flags = rc_blob->blob_flags;<br>
> + return 0;<br>
> +}<br>
> +<br>
> +static int virtio_gpu_resource_create_blob(struct drm_device *dev,<br>
> + void *data, struct drm_file *file)<br>
> +{<br>
> + int ret = 0;<br>
> + uint32_t handle = 0;<br>
> + bool guest_blob = false;<br>
> + bool host3d_blob = false;<br>
> + struct drm_gem_object *obj;<br>
> + struct virtio_gpu_object *bo;<br>
> + struct virtio_gpu_object_params params = { 0 };<br>
> + struct virtio_gpu_device *vgdev = dev->dev_private;<br>
> + struct virtio_gpu_fpriv *vfpriv = file->driver_priv;<br>
> + struct drm_virtgpu_resource_create_blob *rc_blob = data;<br>
> +<br>
> + if (verify_blob(vgdev, vfpriv, ¶ms, rc_blob,<br>
> + &guest_blob, &host3d_blob))<br>
> + return -EINVAL;<br>
> +<br>
> + if (vgdev->has_virgl_3d)<br>
> + virtio_gpu_create_context(dev, file);<br>
> +<br>
> + if (rc_blob->cmd_size) {<br>
> + void *buf;<br>
> +<br>
> + buf = memdup_user(u64_to_user_ptr(rc_blob->cmd),<br>
> + rc_blob->cmd_size);<br>
> +<br>
> + if (IS_ERR(buf))<br>
> + return PTR_ERR(buf);<br>
> +<br>
> + virtio_gpu_cmd_submit(vgdev, buf, rc_blob->cmd_size,<br>
> + vfpriv->ctx_id, NULL, NULL);<br>
> + }<br>
> +<br>
> + if (guest_blob)<br>
> + ret = virtio_gpu_object_create(vgdev, ¶ms, &bo, NULL);<br>
> + else if (!guest_blob && host3d_blob)<br>
> + ret = virtio_gpu_vram_create(vgdev, ¶ms, &bo);<br>
When cmd_size != 0, a host blob has been allocated. Will it be leaked<br>
if virtio_gpu_{object,vram}_create fails?<br></blockquote><div><br></div><div>The actual kick happens as a result of drm_gem_handle_create (similar to drm_virtgpu_resource_create), so the host-side allocation doesn't happen if the virtio_gpu_{object,vram}_create sanity checks fail. We could unqueue items from the virtqueue on the failure cases, but blob is big enough already and userspace doesn't really recover gracefully if an ioctl fails either.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
<br>
> + else<br>
> + return -EINVAL;<br>
> +<br>
> + if (ret < 0)<br>
> + return ret;<br>
> +<br>
> + bo->guest_blob = guest_blob;<br>
> + bo->host3d_blob = host3d_blob;<br>
> + bo->blob_mem = rc_blob->blob_mem;<br>
> + bo->blob_flags = rc_blob->blob_flags;<br>
> +<br>
> + obj = &bo->base.base;<br>
> + if (params.blob_flags & VIRTGPU_BLOB_FLAG_USE_CROSS_DEVICE) {<br>
> + ret = virtio_gpu_resource_assign_uuid(vgdev, bo);<br>
> + if (ret) {<br>
> + drm_gem_object_release(obj);<br>
> + return ret;<br>
> + }<br>
> + }<br>
> +<br>
> + ret = drm_gem_handle_create(file, obj, &handle);<br>
> + if (ret) {<br>
> + drm_gem_object_release(obj);<br>
> + return ret;<br>
> + }<br>
> + drm_gem_object_put(obj);<br>
> +<br>
> + rc_blob->res_handle = bo->hw_res_handle;<br>
> + rc_blob->bo_handle = handle;<br>
> +<br>
> + return 0;<br>
> +}<br>
> +<br>
> struct drm_ioctl_desc virtio_gpu_ioctls[DRM_VIRTIO_NUM_IOCTLS] = {<br>
> DRM_IOCTL_DEF_DRV(VIRTGPU_MAP, virtio_gpu_map_ioctl,<br>
> DRM_RENDER_ALLOW),<br>
> @@ -552,4 +684,8 @@ struct drm_ioctl_desc virtio_gpu_ioctls[DRM_VIRTIO_NUM_IOCTLS] = {<br>
><br>
> DRM_IOCTL_DEF_DRV(VIRTGPU_GET_CAPS, virtio_gpu_get_caps_ioctl,<br>
> DRM_RENDER_ALLOW),<br>
> +<br>
> + DRM_IOCTL_DEF_DRV(VIRTGPU_RESOURCE_CREATE_BLOB,<br>
> + virtio_gpu_resource_create_blob,<br>
> + DRM_RENDER_ALLOW),<br>
> };<br>
> diff --git a/drivers/gpu/drm/virtio/virtgpu_object.c b/drivers/gpu/drm/virtio/virtgpu_object.c<br>
> index cef79455257df..258b4eeae7c2c 100644<br>
> --- a/drivers/gpu/drm/virtio/virtgpu_object.c<br>
> +++ b/drivers/gpu/drm/virtio/virtgpu_object.c<br>
> @@ -244,7 +244,10 @@ int virtio_gpu_object_create(struct virtio_gpu_device *vgdev,<br>
> return ret;<br>
> }<br>
><br>
> - if (params->virgl) {<br>
> + if (params->blob) {<br>
> + virtio_gpu_cmd_resource_create_blob(vgdev, bo, params,<br>
> + ents, nents);<br>
> + } else if (params->virgl) {<br>
> virtio_gpu_cmd_resource_create_3d(vgdev, bo, params,<br>
> objs, fence);<br>
> virtio_gpu_object_attach(vgdev, bo, ents, nents);<br>
> diff --git a/drivers/gpu/drm/virtio/virtgpu_vram.c b/drivers/gpu/drm/virtio/virtgpu_vram.c<br>
> index 087945fcd230f..23c21bc4d01e2 100644<br>
> --- a/drivers/gpu/drm/virtio/virtgpu_vram.c<br>
> +++ b/drivers/gpu/drm/virtio/virtgpu_vram.c<br>
> @@ -149,6 +149,8 @@ int virtio_gpu_vram_create(struct virtio_gpu_device *vgdev,<br>
> return ret;<br>
> }<br>
><br>
> + virtio_gpu_cmd_resource_create_blob(vgdev, &vram->base, params, NULL,<br>
> + 0);<br>
> if (params->blob_flags & VIRTGPU_BLOB_FLAG_USE_MAPPABLE) {<br>
> ret = virtio_gpu_vram_map(&vram->base);<br>
> if (ret) {<br>
> --<br>
> 2.26.2<br>
><br>
> _______________________________________________<br>
> dri-devel mailing list<br>
> <a href="mailto:dri-devel@lists.freedesktop.org" target="_blank">dri-devel@lists.freedesktop.org</a><br>
> <a href="https://lists.freedesktop.org/mailman/listinfo/dri-devel" rel="noreferrer" target="_blank">https://lists.freedesktop.org/mailman/listinfo/dri-devel</a><br>
</blockquote></div></div>