Skip to contentSkip to conversation

drm/virtio: Don't detach GEM from a non-created context

PATCH Applied Jason Macnak 4 participants 6 messages Jun 25 – Jul 13

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/virtio: Don't detach GEM from a non-created context

Applies the same treatment as commit 7cf6dd467e87 ("drm/virtio:
Don't attach GEM to a non-created context in gem_object_open()") to virtio_gpu_gem_object_close() to avoid trying to detach a resource that was never attached due to a context never being created when context_init is supported.

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 | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
drivers/gpu/drm/virtio/virtgpu_gem.c +8 -6
index 435d37d36034..66c3f6f74e9c 100644
@@ -139,13 +139,15 @@ void virtio_gpu_gem_object_close(struct drm_gem_object *obj,
 	if (!vgdev->has_virgl_3d)
 		return;
 
-	objs = virtio_gpu_array_alloc(1);
-	if (!objs)
-		return;
-	virtio_gpu_array_add_obj(objs, obj);
+	if (vfpriv->context_created) {
+		objs = virtio_gpu_array_alloc(1);
+		if (!objs)
+			return;
+		virtio_gpu_array_add_obj(objs, obj);
 
-	virtio_gpu_cmd_context_detach_resource(vgdev, vfpriv->ctx_id,
-					       objs);
+		virtio_gpu_cmd_context_detach_resource(vgdev, vfpriv->ctx_id,
+						       objs);
+	}
 	virtio_gpu_notify(vgdev);
 }
 
Signature · 1 line
2.55.0.rc0.799.gd6f94ed593-goog
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [Medium] Memory and reference leak of vbuf, vbuf->objs, and vbuf->data_buf on error paths during command queuing. --

commit 170d71262d2472089fb4a6a1649487aa23d08e9a
Author: Jason Macnak <[email protected]>

drm/virtio: Don't detach GEM from a non-created context

Applies the same treatment as commit 7cf6dd467e87 ("drm/virtio:
Don't attach GEM to a non-created context in gem_object_open()") to virtio_gpu_gem_object_close() to avoid trying to detach a resource that was never attached due to a context never being created when context_init is supported.

Quoted 7 lines
drivers/gpu/drm/virtio/virtgpu_gem.c +0 -0
index 435d37d360340..66c3f6f74e9c6 100644
@@ -139,13 +139,15 @@ void virtio_gpu_gem_object_close(struct drm_gem_object *obj,
 	if (!vgdev->has_virgl_3d)
 		return;

[ ... ]

drivers/gpu/drm/virtio/virtgpu_gem.c void virtio_gpu_gem_object_close(struct drm_gem_object *obj, · line 149

Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_gem.c +7 -2
+	if (vfpriv->context_created) {
+		objs = virtio_gpu_array_alloc(1);
+		if (!objs)
+			return;
+		virtio_gpu_array_add_obj(objs, obj);
 
-	virtio_gpu_cmd_context_detach_resource(vgdev, vfpriv->ctx_id,
-					       objs);
+		virtio_gpu_cmd_context_detach_resource(vgdev, vfpriv->ctx_id,
+						       objs);

[Severity: Medium]
This is a pre-existing issue, but does this code leak the objs array and GEM object references on error paths deeper in the command queuing stack?

When virtio_gpu_cmd_context_detach_resource() queues the command, it calls into virtio_gpu_queue_ctrl_buffer(). If an error occurs further down, the vbuf and vbuf->objs may not be cleaned up.

For example, in drivers/gpu/drm/virtio/virtgpu_vq.c:virtio_gpu_queue_fenced_ctrl_buffer():

    if (!sgt) {
        if (fence && vbuf->objs)
            virtio_gpu_array_unlock_resv(vbuf->objs);
        return -ENOMEM;
    }

It returns -ENOMEM without calling free_vbuf() or releasing vbuf->objs.

Similarly, in drivers/gpu/drm/virtio/virtgpu_vq.c:virtio_gpu_queue_ctrl_sgs():

    if (!drm_dev_enter(vgdev->ddev, &idx)) {
        if (fence && vbuf->objs)
            virtio_gpu_array_unlock_resv(vbuf->objs);
        free_vbuf(vgdev, vbuf);
        return -ENODEV;
    }

It frees the vbuf but fails to release vbuf->objs and its GEM references (unlike the success path in virtio_gpu_dequeue_ctrl_func).

Since callers like virtio_gpu_cmd_context_detach_resource() ignore the return value, is there a mechanism to prevent these resources from leaking when command queuing fails?

Quoted 4 lines
drivers/gpu/drm/virtio/virtgpu_gem.c +1 -0
+	}
 	virtio_gpu_notify(vgdev);
 }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#2 lore raw
In reply to Jason Macnak

Hi,

Quoted 28 lines · Jason Macnak

Applies the same treatment as commit 7cf6dd467e87 ("drm/virtio:
Don't attach GEM to a non-created context in gem_object_open()") to virtio_gpu_gem_object_close() to avoid trying to detach a resource that was never attached due to a context never being created when context_init is supported.

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 | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
drivers/gpu/drm/virtio/virtgpu_gem.c +3 -4
index 435d37d36034..66c3f6f74e9c 100644
@@ -139,13 +139,15 @@ void virtio_gpu_gem_object_close(struct drm_gem_object *obj,
 	if (!vgdev->has_virgl_3d)
 		return;
 
-	objs = virtio_gpu_array_alloc(1);
-	if (!objs)
-		return;
-	virtio_gpu_array_add_obj(objs, obj);
+	if (vfpriv->context_created) {
+		objs = virtio_gpu_array_alloc(1);
+		if (!objs)

drivers/gpu/drm/virtio/virtgpu_gem.c void virtio_gpu_gem_object_close(struct drm_gem_object *obj, · line 152

Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_gem.c +5 -2
+			return;
+		virtio_gpu_array_add_obj(objs, obj);
 
-	virtio_gpu_cmd_context_detach_resource(vgdev, vfpriv->ctx_id,
-					       objs);
+		virtio_gpu_cmd_context_detach_resource(vgdev, vfpriv->ctx_id,
+						       objs);
+	}
 	virtio_gpu_notify(vgdev);
 }

The following scenario still will be troubling:

1. vgdev->has_context_init = true
2. virtio_gpu_gem_object_open() invoked, GEM created and not attached to ctx
3. virtio_gpu_context_init_ioctl() invoked, now vfpriv->context_created = true
4. virtio_gpu_gem_object_close() will detach resource that wasn't attached

Add obj->ctx_attached member to struct virtio_gpu_object. See virtio_gpu_object_attach() that uses obj->attached, do the same for virtio_gpu_cmd_context_attach_resource().

Signature · 2 lines
Best regards,
Dmitry
#3 lore raw
Quoted 51 lines · Dmitry Osipenko

Hi,

Quoted 28 lines · Jason Macnak

Applies the same treatment as commit 7cf6dd467e87 ("drm/virtio:
Don't attach GEM to a non-created context in gem_object_open()") to virtio_gpu_gem_object_close() to avoid trying to detach a resource that was never attached due to a context never being created when context_init is supported.

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 | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
drivers/gpu/drm/virtio/virtgpu_gem.c +3 -4
index 435d37d36034..66c3f6f74e9c 100644
@@ -139,13 +139,15 @@ void virtio_gpu_gem_object_close(struct drm_gem_object *obj,
      if (!vgdev->has_virgl_3d)
              return;
 
-     objs = virtio_gpu_array_alloc(1);
-     if (!objs)
-             return;
-     virtio_gpu_array_add_obj(objs, obj);
+     if (vfpriv->context_created) {
+             objs = virtio_gpu_array_alloc(1);
+             if (!objs)

drivers/gpu/drm/virtio/virtgpu_gem.c void virtio_gpu_gem_object_close(struct drm_gem_object *obj, · line 152

Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_gem.c +5 -2
+                     return;
+             virtio_gpu_array_add_obj(objs, obj);
 
-     virtio_gpu_cmd_context_detach_resource(vgdev, vfpriv->ctx_id,
-                                            objs);
+             virtio_gpu_cmd_context_detach_resource(vgdev, vfpriv->ctx_id,
+                                                    objs);
+     }
      virtio_gpu_notify(vgdev);
 }

The following scenario still will be troubling:

1. vgdev->has_context_init = true
2. virtio_gpu_gem_object_open() invoked, GEM created and not attached to ctx
3. virtio_gpu_context_init_ioctl() invoked, now vfpriv->context_created = true
4. virtio_gpu_gem_object_close() will detach resource that wasn't attached

Quoted 7 lines

Add obj->ctx_attached member to struct virtio_gpu_object. See virtio_gpu_object_attach() that uses obj->attached, do the same for virtio_gpu_cmd_context_attach_resource().

--
Best regards,
Dmitry

Hi Dmitry,

WIth context_init, resource attach/detach is per-context based. So a simple obj->ctx_attached won't work. One would have to track in the guest context_init ctx for whether a bo has been attached or not.

Another option is to accept this patch and live with the case you mentioned. We can consider that "invalid" user behavior.

Best,
Yiwei

maintainer
#4 lore raw
Quoted 61 lines · Yiwei Zhang
Quoted 51 lines · Dmitry Osipenko

Hi,

Quoted 28 lines · Jason Macnak

Applies the same treatment as commit 7cf6dd467e87 ("drm/virtio:
Don't attach GEM to a non-created context in gem_object_open()") to virtio_gpu_gem_object_close() to avoid trying to detach a resource that was never attached due to a context never being created when context_init is supported.

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 | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
drivers/gpu/drm/virtio/virtgpu_gem.c +3 -4
index 435d37d36034..66c3f6f74e9c 100644
@@ -139,13 +139,15 @@ void virtio_gpu_gem_object_close(struct drm_gem_object *obj,
      if (!vgdev->has_virgl_3d)
              return;
 
-     objs = virtio_gpu_array_alloc(1);
-     if (!objs)
-             return;
-     virtio_gpu_array_add_obj(objs, obj);
+     if (vfpriv->context_created) {
+             objs = virtio_gpu_array_alloc(1);
+             if (!objs)

drivers/gpu/drm/virtio/virtgpu_gem.c void virtio_gpu_gem_object_close(struct drm_gem_object *obj, · line 152

Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_gem.c +5 -2
+                     return;
+             virtio_gpu_array_add_obj(objs, obj);
 
-     virtio_gpu_cmd_context_detach_resource(vgdev, vfpriv->ctx_id,
-                                            objs);
+             virtio_gpu_cmd_context_detach_resource(vgdev, vfpriv->ctx_id,
+                                                    objs);
+     }
      virtio_gpu_notify(vgdev);
 }

The following scenario still will be troubling:

1. vgdev->has_context_init = true
2. virtio_gpu_gem_object_open() invoked, GEM created and not attached to ctx
3. virtio_gpu_context_init_ioctl() invoked, now vfpriv->context_created = true
4. virtio_gpu_gem_object_close() will detach resource that wasn't attached

Quoted 7 lines

Add obj->ctx_attached member to struct virtio_gpu_object. See virtio_gpu_object_attach() that uses obj->attached, do the same for virtio_gpu_cmd_context_attach_resource().

--
Best regards,
Dmitry

Quoted 8 lines

Hi Dmitry,

WIth context_init, resource attach/detach is per-context based. So a simple obj->ctx_attached won't work. One would have to track in the guest context_init ctx for whether a bo has been attached or not.

Another option is to accept this patch and live with the case you mentioned. We can consider that "invalid" user behavior.

Indeed, obj->ctx_attached shouldn't work for a shared/exported BO. Will think on it for a couple days more and then merge this version if no better ideas will appear.

Signature · 2 lines
Best regards,
Dmitry
maintainer Applied
#5 lore raw
In reply to Jason Macnak
Quoted 29 lines · Jason Macnak

Applies the same treatment as commit 7cf6dd467e87 ("drm/virtio:
Don't attach GEM to a non-created context in gem_object_open()") to virtio_gpu_gem_object_close() to avoid trying to detach a resource that was never attached due to a context never being created when context_init is supported.

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 | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
drivers/gpu/drm/virtio/virtgpu_gem.c +4 -4
index 435d37d36034..66c3f6f74e9c 100644
@@ -139,13 +139,15 @@ void virtio_gpu_gem_object_close(struct drm_gem_object *obj,
 	if (!vgdev->has_virgl_3d)
 		return;
 
-	objs = virtio_gpu_array_alloc(1);
-	if (!objs)
-		return;
-	virtio_gpu_array_add_obj(objs, obj);
+	if (vfpriv->context_created) {
+		objs = virtio_gpu_array_alloc(1);
+		if (!objs)
+			return;

drivers/gpu/drm/virtio/virtgpu_gem.c void virtio_gpu_gem_object_close(struct drm_gem_object *obj, · line 153

Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_gem.c +4 -2
+		virtio_gpu_array_add_obj(objs, obj);
 
-	virtio_gpu_cmd_context_detach_resource(vgdev, vfpriv->ctx_id,
-					       objs);
+		virtio_gpu_cmd_context_detach_resource(vgdev, vfpriv->ctx_id,
+						       objs);
+	}
 	virtio_gpu_notify(vgdev);
 }
 

Applied to misc-fixes, thanks!

Signature · 2 lines
Best regards,
Dmitry