Skip to contentSkip to conversation

drm/virtio: Don't attach GEM to a non-created context in gem_object_open()

PATCH Superseded Dmitry Osipenko 2 participants 6 messages Mar 26, 2025 – Mar 28, 2025

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Dmitry Osipenko

author maintainer
#0 lore raw

PATCH 1/2 drm/virtio: Don't attach GEM to a non-created context in gem_object_open()

The vfpriv->ctx_id is always initialized to a non-zero value. Check whether context was created before attaching GEM to this context ID. This left unnoticed previously because host silently skips attachment if context doesn't exist, still we shouldn't do that for consistency.

Fixes 086b9f27f0ab ("drm/virtio: Don't create a context with default param if context_init is supported")
Cc <[email protected]> # v6.14+
---
 drivers/gpu/drm/virtio/virtgpu_gem.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
drivers/gpu/drm/virtio/virtgpu_gem.c +6 -5
index dde8fc1a3689..90c99d83c4cf 100644
@@ -115,13 +115,14 @@ int virtio_gpu_gem_object_open(struct drm_gem_object *obj,
 	if (!vgdev->has_context_init)
 		virtio_gpu_create_context(obj->dev, file);
 
-	objs = virtio_gpu_array_alloc(1);
-	if (!objs)
-		return -ENOMEM;
-	virtio_gpu_array_add_obj(objs, obj);
+	if (vfpriv->context_created) {
+		objs = virtio_gpu_array_alloc(1);
+		if (!objs)
+			return -ENOMEM;
+		virtio_gpu_array_add_obj(objs, obj);
 
-	if (vfpriv->ctx_id)
 		virtio_gpu_cmd_context_attach_resource(vgdev, vfpriv->ctx_id, objs);
+	}
 
 out_notify:
 	virtio_gpu_notify(vgdev);
Signature · 1 line
2.49.0
author maintainer
#1 lore raw

PATCH 2/2 drm/virtio: Fix missed dmabuf unpinning in error path of prepare_fb()

Unpin imported dmabuf on fence allocation failure in prepare_fb().

Fixes 4a696a2ee646 ("drm/virtio: Add prepare and cleanup routines for imported dmabuf obj")
Cc <[email protected]> # v6.14+
---
 drivers/gpu/drm/virtio/virtgpu_plane.c | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)
drivers/gpu/drm/virtio/virtgpu_plane.c +14 -11
index a6f5a78f436a..dc1d91639931 100644
@@ -342,6 +342,16 @@ static int virtio_gpu_prepare_imported_obj(struct drm_plane *plane,
 	return ret;
 }
 
+static void virtio_gpu_cleanup_imported_obj(struct drm_gem_object *obj)
+{
+	struct dma_buf_attachment *attach = obj->import_attach;
+	struct dma_resv *resv = attach->dmabuf->resv;
+
+	dma_resv_lock(resv, NULL);
+	dma_buf_unpin(attach);
+	dma_resv_unlock(resv);
+}
+
 static int virtio_gpu_plane_prepare_fb(struct drm_plane *plane,
 				       struct drm_plane_state *new_state)
 {
@@ -376,23 +386,16 @@ static int virtio_gpu_plane_prepare_fb(struct drm_plane *plane,
 		vgplane_st->fence = virtio_gpu_fence_alloc(vgdev,
 						     vgdev->fence_drv.context,
 						     0);
-		if (!vgplane_st->fence)
+		if (!vgplane_st->fence) {
+			if (obj->import_attach)
+				virtio_gpu_cleanup_imported_obj(obj);
 			return -ENOMEM;
+		}
 	}
 
 	return 0;
 }
 
-static void virtio_gpu_cleanup_imported_obj(struct drm_gem_object *obj)
-{
-	struct dma_buf_attachment *attach = obj->import_attach;
-	struct dma_resv *resv = attach->dmabuf->resv;
-
-	dma_resv_lock(resv, NULL);
-	dma_buf_unpin(attach);
-	dma_resv_unlock(resv);
-}
-
 static void virtio_gpu_plane_cleanup_fb(struct drm_plane *plane,
 					struct drm_plane_state *state)
 {
Signature · 1 line
2.49.0
Acked-by on v1 2/2
#2 lore raw

RE: [PATCH v1 2/2] drm/virtio: Fix missed dmabuf unpinning in error path of prepare_fb()

Hi Dmitry,

Quoted 37 lines

Subject: [PATCH v1 2/2] drm/virtio: Fix missed dmabuf unpinning in error path of prepare_fb()

Unpin imported dmabuf on fence allocation failure in prepare_fb().

Fixes 4a696a2ee646 ("drm/virtio: Add prepare and cleanup routines for

imported dmabuf obj")

---
 drivers/gpu/drm/virtio/virtgpu_plane.c | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)
drivers/gpu/drm/virtio/virtgpu_plane.c +10 -0
index a6f5a78f436a..dc1d91639931 100644
@@ -342,6 +342,16 @@ static int virtio_gpu_prepare_imported_obj(struct
 drm_plane *plane,
 	return ret;
 }
 
+static void virtio_gpu_cleanup_imported_obj(struct drm_gem_object *obj)
+{
+	struct dma_buf_attachment *attach = obj->import_attach;
+	struct dma_resv *resv = attach->dmabuf->resv;
+
+	dma_resv_lock(resv, NULL);
+	dma_buf_unpin(attach);
+	dma_resv_unlock(resv);
+}
+
 static int virtio_gpu_plane_prepare_fb(struct drm_plane *plane,
 				       struct drm_plane_state *new_state)

{

drivers/gpu/drm/virtio/virtgpu_plane.c static int virtio_gpu_plane_prepare_fb(struct · line 392

Quoted 8 lines
drivers/gpu/drm/virtio/virtgpu_plane.c +3 -1
@@ -376,23 +386,16 @@ static int virtio_gpu_plane_prepare_fb(struct
 drm_plane *plane,
 		vgplane_st->fence = virtio_gpu_fence_alloc(vgdev,
 						     vgdev->fence_drv.context,
 						     0);
-		if (!vgplane_st->fence)
+		if (!vgplane_st->fence) {
+			if (obj->import_attach)
+				virtio_gpu_cleanup_imported_obj(obj);

I think checking for fence allocation failure before import would be much better. In other words, cleaning up the fence in case of any import errors would be much simpler IMO.

Regardless,

Quoted 23 lines
drivers/gpu/drm/virtio/virtgpu_plane.c +1 -10
 			return -ENOMEM;
+		}
 	}
 
 	return 0;
 }
 
-static void virtio_gpu_cleanup_imported_obj(struct drm_gem_object *obj)
-{
-	struct dma_buf_attachment *attach = obj->import_attach;
-	struct dma_resv *resv = attach->dmabuf->resv;
-
-	dma_resv_lock(resv, NULL);
-	dma_buf_unpin(attach);
-	dma_resv_unlock(resv);
-}
-
 static void virtio_gpu_plane_cleanup_fb(struct drm_plane *plane,
 					struct drm_plane_state *state)

{
--
2.49.0

author maintainer
#3 lore raw

Re: [PATCH v1 2/2] drm/virtio: Fix missed dmabuf unpinning in error path of prepare_fb()

On 3/26/25 08:14, Kasireddy, Vivek wrote:
...

Quoted 16 lines

drivers/gpu/drm/virtio/virtgpu_plane.c static int virtio_gpu_plane_prepare_fb(struct · line 392

Quoted 12 lines

static int virtio_gpu_plane_prepare_fb(struct drm_plane *plane,

drivers/gpu/drm/virtio/virtgpu_plane.c +3 -1
 				       struct drm_plane_state *new_state)
 {
@@ -376,23 +386,16 @@ static int virtio_gpu_plane_prepare_fb(struct
 drm_plane *plane,
 		vgplane_st->fence = virtio_gpu_fence_alloc(vgdev,
 						     vgdev->fence_drv.context,
 						     0);
-		if (!vgplane_st->fence)
+		if (!vgplane_st->fence) {
+			if (obj->import_attach)
+				virtio_gpu_cleanup_imported_obj(obj);

I think checking for fence allocation failure before import would be much better. In other words, cleaning up the fence in case of any import errors would be much simpler IMO.

Quoted 2 lines

Regardless,

Another question, why do we need this fencing for imported dmabuf? Fencing isn't done host/guest blobs in this code, while dmabuf is essentially a guest blob. Could you please clarify why this fence is needed? Maybe we shouldn't allocate fence in the first place for the dmabuf.

Signature · 2 lines
Best regards,
Dmitry
#4 lore raw

RE: [PATCH v1 2/2] drm/virtio: Fix missed dmabuf unpinning in error path of prepare_fb()

Hi Dmitry,

Quoted 22 lines

Subject: Re: [PATCH v1 2/2] drm/virtio: Fix missed dmabuf unpinning in error path of prepare_fb()

On 3/26/25 08:14, Kasireddy, Vivek wrote:
...

Quoted 5 lines
Quoted 5 lines

static int virtio_gpu_plane_prepare_fb(struct drm_plane *plane,

drivers/gpu/drm/virtio/virtgpu_plane.c +0 -0
 				       struct drm_plane_state *new_state)
 {
@@ -376,23 +386,16 @@ static int virtio_gpu_plane_prepare_fb(struct
 drm_plane *plane,
Quoted 8 lines

drivers/gpu/drm/virtio/virtgpu_plane.c static int virtio_gpu_plane_prepare_fb(struct drm_plane *plane, · line 391

Quoted 7 lines
drivers/gpu/drm/virtio/virtgpu_plane.c +3 -1
 		vgplane_st->fence = virtio_gpu_fence_alloc(vgdev,
 						     vgdev->fence_drv.context,
 						     0);
-		if (!vgplane_st->fence)
+		if (!vgplane_st->fence) {
+			if (obj->import_attach)
+				virtio_gpu_cleanup_imported_obj(obj);

I think checking for fence allocation failure before import would be much

better.

Quoted 3 lines

In other words, cleaning up the fence in case of any import errors would be much simpler IMO.

Quoted 7 lines
Quoted 2 lines

Regardless,

Another question, why do we need this fencing for imported dmabuf? Fencing isn't done host/guest blobs in this code, while dmabuf is essentially a guest blob. Could you please clarify why this fence is needed? Maybe we shouldn't allocate fence in the first place for the dmabuf.

At-least for the non-virgl use-cases (where the rendering is done in the Guest such as in passthrough), this Guest/Host synchronization fence serves two purposes:
- It prevents the Guest from reusing/destroying the submitted buffer (Guest compositor FB) until the Host is done using it. Otherwise, the Guest compositor might render into this buffer at the same time while the Host is consuming it, leading to issues such as tearing/flickering. This problem is more noticeable in cases where the Guest compositor has only one backbuffer such as Xorg + dirtfyFb.

- It also prevents the Guest compositor from rendering faster than the Host refresh rate. In other words, it just sets the upper bound in terms of the buffer submission rate as there is no point in going over the Host refresh rate, which would likely lead to wastage of GPU cycles and dropped frames.

Therefore, this fence is really needed for Guest blobs including imported dmabufs.

Thanks,
Vivek

Quoted 4 lines

--
Best regards,
Dmitry

author maintainer
#5 lore raw

Re: [PATCH v1 2/2] drm/virtio: Fix missed dmabuf unpinning in error path of prepare_fb()

Quoted 12 lines · Vivek
Quoted 4 lines

Another question, why do we need this fencing for imported dmabuf? Fencing isn't done host/guest blobs in this code, while dmabuf is essentially a guest blob. Could you please clarify why this fence is needed? Maybe we shouldn't allocate fence in the first place for the dmabuf.

At-least for the non-virgl use-cases (where the rendering is done in the Guest such as in passthrough), this Guest/Host synchronization fence serves two purposes:
- It prevents the Guest from reusing/destroying the submitted buffer (Guest compositor FB) until the Host is done using it. Otherwise, the Guest compositor might render into this buffer at the same time while the Host is consuming it, leading to issues such as tearing/flickering. This problem is more noticeable in cases where the Guest compositor has only one backbuffer such as Xorg + dirtfyFb.

Quoted 6 lines

- It also prevents the Guest compositor from rendering faster than the Host refresh rate. In other words, it just sets the upper bound in terms of the buffer submission rate as there is no point in going over the Host refresh rate, which would likely lead to wastage of GPU cycles and dropped frames.

Therefore, this fence is really needed for Guest blobs including imported dmabufs.

Thanks a lot

Signature · 2 lines
Best regards,
Dmitry