Skip to contentSkip to conversation

Add sync object UAPI support to VirtIO-GPU driver

PATCH Superseded Dmitry Osipenko 3 participants 10 messages Mar 23, 2023 – Apr 3, 2023

Subsystems (4)

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Dmitry Osipenko

author maintainer
#0 lore raw

v4 cover Add sync object UAPI support to VirtIO-GPU driver

We have multiple Vulkan context types that are awaiting for the addition of the sync object DRM UAPI support to the VirtIO-GPU kernel driver:

1. Venus context
2. Native contexts (virtio-freedreno, virtio-intel, virtio-amdgpu)

Mesa core supports DRM sync object UAPI, providing Vulkan drivers with a generic fencing implementation that we want to utilize.

This patch adds initial sync objects support. It creates fundament for a further fencing improvements. Later on we will want to extend the VirtIO-GPU fencing API with passing fence IDs to host for waiting, it will be a new additional VirtIO-GPU IOCTL and more. Today we have several VirtIO-GPU context drivers in works that require VirtIO-GPU to support sync objects UAPI.

The patch is heavily inspired by the sync object UAPI implementation of the MSM driver.

Changelog:

v4: - Added r-b from Rob Clark to the "refactoring" patch.

- Replaced for/while(ptr && itr) with if (ptr), like was suggested by Rob Clark.

- Dropped NOWARN and NORETRY GFP flags and switched syncobj patch to use kvmalloc.

- Removed unused variables from syncobj patch that were borrowed by accident from another (upcoming) patch after one of git rebases.

v3: - Switched to use dma_fence_unwrap_for_each(), like was suggested by
Rob Clark.

- Fixed missing dma_fence_put() in error code path that was spotted by Rob Clark.

- Removed obsoleted comment to virtio_gpu_execbuffer_ioctl(), like was suggested by Rob Clark.

v2: - Fixed chain-fence context matching by making use of
dma_fence_chain_contained().

- Fixed potential uninitialized var usage in error code patch of parse_post_deps(). MSM driver had a similar issue that is fixed already in upstream.

- Added new patch that refactors job submission code path. I found that it was very difficult to add a new/upcoming host-waits feature because of how variables are passed around the code, the virtgpu_ioctl.c also was growing to unmanageable size.

Dmitry Osipenko (2):
drm/virtio: Refactor job submission code path
drm/virtio: Support sync objects

 drivers/gpu/drm/virtio/Makefile         |   2 +-
 drivers/gpu/drm/virtio/virtgpu_drv.c    |   3 +-
 drivers/gpu/drm/virtio/virtgpu_drv.h    |   4 +
 drivers/gpu/drm/virtio/virtgpu_ioctl.c  | 182 ---------
 drivers/gpu/drm/virtio/virtgpu_submit.c | 521 ++++++++++++++++++++++++
 include/uapi/drm/virtgpu_drm.h          |  16 +-
 6 files changed, 543 insertions(+), 185 deletions(-)
 create mode 100644 drivers/gpu/drm/virtio/virtgpu_submit.c
Signature · 1 line
2.39.2
author maintainer
#1 lore raw

PATCH v4 1/2 drm/virtio: Refactor job submission code path

Move virtio_gpu_execbuffer_ioctl() into separate virtgpu_submit.c file and refactor the code along the way to ease addition of new features to the ioctl.

---
 drivers/gpu/drm/virtio/Makefile         |   2 +-
 drivers/gpu/drm/virtio/virtgpu_drv.h    |   4 +
 drivers/gpu/drm/virtio/virtgpu_ioctl.c  | 182 --------------
 drivers/gpu/drm/virtio/virtgpu_submit.c | 302 ++++++++++++++++++++++++
 4 files changed, 307 insertions(+), 183 deletions(-)
 create mode 100644 drivers/gpu/drm/virtio/virtgpu_submit.c
drivers/gpu/drm/virtio/Makefile +1 -1
index b99fa4a73b68..d2e1788a8227 100644
@@ -6,6 +6,6 @@
 virtio-gpu-y := virtgpu_drv.o virtgpu_kms.o virtgpu_gem.o virtgpu_vram.o \
 	virtgpu_display.o virtgpu_vq.o \
 	virtgpu_fence.o virtgpu_object.o virtgpu_debugfs.o virtgpu_plane.o \
-	virtgpu_ioctl.o virtgpu_prime.o virtgpu_trace_points.o
+	virtgpu_ioctl.o virtgpu_prime.o virtgpu_trace_points.o virtgpu_submit.o
 
 obj-$(CONFIG_DRM_VIRTIO_GPU) += virtio-gpu.o
drivers/gpu/drm/virtio/virtgpu_drv.h +4 -0
index af6ffb696086..4126c384286b 100644
@@ -486,4 +486,8 @@ void virtio_gpu_vram_unmap_dma_buf(struct device *dev,
 				   struct sg_table *sgt,
 				   enum dma_data_direction dir);
 
+/* virtgpu_submit.c */
+int virtio_gpu_execbuffer_ioctl(struct drm_device *dev, void *data,
+				struct drm_file *file);
+
 #endif
drivers/gpu/drm/virtio/virtgpu_ioctl.c +0 -182

194 lines not on this page · raw message

drivers/gpu/drm/virtio/virtgpu_submit.c new file +302 -0

302 lines not on this page · raw message

Signature · 1 line
2.39.2
#2 lore raw

Re: [PATCH v4 1/2] drm/virtio: Refactor job submission code path

Hey Dmitry,

Quoted 4 lines · Dmitry Osipenko

Move virtio_gpu_execbuffer_ioctl() into separate virtgpu_submit.c file and refactor the code along the way to ease addition of new features to the ioctl.

At a glance, we have a handful of no-op as well as some functional changes - let's split those up in separate patches.

drivers/gpu/drm/virtio/virtgpu_submit.c line 52

Quoted 13 lines

---

drivers/gpu/drm/virtio/virtgpu_submit.c new file +8 -0
+static int virtio_gpu_dma_fence_wait(struct virtio_gpu_submit *submit,
+				     struct dma_fence *fence)
+{
+	struct dma_fence_unwrap itr;
+	struct dma_fence *f;
+	int err;
+
+	dma_fence_unwrap_for_each(f, &itr, fence) {

The dma_fence_unwrap_for_each() change should be a separate patch, highlighting why we want it.

Quoted 9 lines
drivers/gpu/drm/virtio/virtgpu_submit.c new file +8 -0
+		err = virtio_gpu_do_fence_wait(submit, f);
+		if (err)
+			return err;
+	}
+
+	return 0;
+}
+
 

drivers/gpu/drm/virtio/virtgpu_submit.c line 287

Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_submit.c new file +9 -0
 
+	ret = virtio_gpu_init_submit(&submit, exbuf, dev, file,
+				     fence_ctx, ring_idx);
+	if (ret)
+		goto cleanup;
+
+	ret = virtio_gpu_wait_in_fence(&submit);
+	if (ret)
+		goto cleanup;
+

We have reshuffled the order around in_fence waiting, out_fence install, handles, cmdbuf, drm events, etc. Can we get that split up a bit, with some comments.

If it were me, I would keep the wait_in_fence early and inline virtio_gpu_init_submit (the nesting/abstraction seems a bit much). This means one can omit the virtio_gpu_submit::exbuf all together.

HTH
Emil

author maintainer
#3 lore raw

Re: [PATCH v4 1/2] drm/virtio: Refactor job submission code path

Quoted 10 lines · Emil Velikov

drivers/gpu/drm/virtio/virtgpu_submit.c line 52

Quoted 8 lines
drivers/gpu/drm/virtio/virtgpu_submit.c new file +8 -0
+static int virtio_gpu_dma_fence_wait(struct virtio_gpu_submit *submit,
+				     struct dma_fence *fence)
+{
+	struct dma_fence_unwrap itr;
+	struct dma_fence *f;
+	int err;
+
+	dma_fence_unwrap_for_each(f, &itr, fence) {

The dma_fence_unwrap_for_each() change should be a separate patch, highlighting why we want it.

Good point, it actually should be a potential optimization for the in-fence waiting.

Quoted 13 lines

drivers/gpu/drm/virtio/virtgpu_submit.c line 287

Quoted 9 lines
drivers/gpu/drm/virtio/virtgpu_submit.c new file +9 -0
+	ret = virtio_gpu_init_submit(&submit, exbuf, dev, file,
+				     fence_ctx, ring_idx);
+	if (ret)
+		goto cleanup;
+
+	ret = virtio_gpu_wait_in_fence(&submit);
+	if (ret)
+		goto cleanup;
+

We have reshuffled the order around in_fence waiting, out_fence install, handles, cmdbuf, drm events, etc. Can we get that split up a bit, with some comments.

Quoted 3 lines

If it were me, I would keep the wait_in_fence early and inline virtio_gpu_init_submit (the nesting/abstraction seems a bit much). This means one can omit the virtio_gpu_submit::exbuf all together.

I tried to inline and this variant makes code much less readable to me.

The point of having wait_in_fence after submit_init is that it makes submit code path shorter. If we have to wait for in-fence, then once fence signals, there is no need to init and instead move directly to a further submission step.

Perhaps won't hurt to also factor out the wait_fence from parse_deps in the second patch and do all the waits right before locking the buflist.

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

PATCH v4 2/2 drm/virtio: Support sync objects

In reply to Dmitry Osipenko

Add sync object DRM UAPI support to VirtIO-GPU driver. It's required for enabling a full-featured Vulkan fencing by Venus and native context VirtIO-GPU Mesa drivers.

---
 drivers/gpu/drm/virtio/virtgpu_drv.c    |   3 +-
 drivers/gpu/drm/virtio/virtgpu_submit.c | 219 ++++++++++++++++++++++++
 include/uapi/drm/virtgpu_drm.h          |  16 +-
 3 files changed, 236 insertions(+), 2 deletions(-)
drivers/gpu/drm/virtio/virtgpu_drv.c +2 -1
index add075681e18..a22155577152 100644
@@ -176,7 +176,8 @@ static const struct drm_driver driver = {
 	 * If KMS is disabled DRIVER_MODESET and DRIVER_ATOMIC are masked
 	 * out via drm_device::driver_features:
 	 */
-	.driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_RENDER | DRIVER_ATOMIC,
+	.driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_RENDER | DRIVER_ATOMIC |
+			   DRIVER_SYNCOBJ | DRIVER_SYNCOBJ_TIMELINE,
 	.open = virtio_gpu_driver_open,
 	.postclose = virtio_gpu_driver_postclose,
 
drivers/gpu/drm/virtio/virtgpu_submit.c +219 -0

260 lines not on this page · raw message

include/uapi/drm/virtgpu_drm.h +15 -1
index 7b158fcb02b4..ce4948aacafd 100644
@@ -64,6 +64,16 @@ struct drm_virtgpu_map {
 	__u32 pad;
 };
 
+#define VIRTGPU_EXECBUF_SYNCOBJ_RESET		0x01
+#define VIRTGPU_EXECBUF_SYNCOBJ_FLAGS ( \
+		VIRTGPU_EXECBUF_SYNCOBJ_RESET | \
+		0)
+struct drm_virtgpu_execbuffer_syncobj {
+	__u32 handle;
+	__u32 flags;
+	__u64 point;
+};
+
 /* fence_fd is modified on success if VIRTGPU_EXECBUF_FENCE_FD_OUT flag is set. */
 struct drm_virtgpu_execbuffer {
 	__u32 flags;
@@ -73,7 +83,11 @@ struct drm_virtgpu_execbuffer {
 	__u32 num_bo_handles;
 	__s32 fence_fd; /* in/out fence fd (see VIRTGPU_EXECBUF_FENCE_FD_IN/OUT) */
 	__u32 ring_idx; /* command ring index (see VIRTGPU_EXECBUF_RING_IDX) */
-	__u32 pad;
+	__u32 syncobj_stride;
+	__u32 num_in_syncobjs;
+	__u32 num_out_syncobjs;
+	__u64 in_syncobjs;
+	__u64 out_syncobjs;
 };
 
 #define VIRTGPU_PARAM_3D_FEATURES 1 /* do we have 3D features in the hw */
Signature · 1 line
2.39.2
#5 lore raw

Re: [PATCH v4 2/2] drm/virtio: Support sync objects

Hi Dmitry,

Have you considered creating a few DRM helpers for this functionality?

AFAICT this is the third driver which supports syncobj timelines and looking at one of the implementations ... it is not great. Note that this suggestion is _not_ a blocker.

Quoted 11 lines · Dmitry Osipenko

Add sync object DRM UAPI support to VirtIO-GPU driver. It's required for enabling a full-featured Vulkan fencing by Venus and native context VirtIO-GPU Mesa drivers.

---

drivers/gpu/drm/virtio/virtgpu_submit.c +4 -0
+static int
+virtio_gpu_parse_deps(struct virtio_gpu_submit *submit)
+{
+	struct drm_virtgpu_execbuffer *exbuf = submit->exbuf;

drivers/gpu/drm/virtio/virtgpu_submit.c static int virtio_gpu_dma_fence_wait(struct virtio_gpu_submit *submit, · line 100

Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_submit.c +10 -0
+	struct drm_virtgpu_execbuffer_syncobj syncobj_desc;
+	size_t syncobj_stride = exbuf->syncobj_stride;
+	struct drm_syncobj **syncobjs;
+	int ret = 0, i;
+
+	if (!submit->num_in_syncobjs)
+		return 0;
+
+	syncobjs = kvcalloc(submit->num_in_syncobjs, sizeof(*syncobjs),
+			    GFP_KERNEL);

Please add an inline note about the decision behind the allocators used, both here and in the parse_post_deps below. IIRC there was some nice discussion between you and Rob in earlier revisions.

Quoted 20 lines
drivers/gpu/drm/virtio/virtgpu_submit.c +20 -0
+	if (!syncobjs)
+		return -ENOMEM;
+
+	for (i = 0; i < submit->num_in_syncobjs; i++) {
+		uint64_t address = exbuf->in_syncobjs + i * syncobj_stride;
+		struct dma_fence *fence;
+
+		if (copy_from_user(&syncobj_desc,
+				   u64_to_user_ptr(address),
+				   min(syncobj_stride, sizeof(syncobj_desc)))) {
+			ret = -EFAULT;
+			break;
+		}
+
+		if (syncobj_desc.flags & ~VIRTGPU_EXECBUF_SYNCOBJ_FLAGS) {
+			ret = -EINVAL;
+			break;
+		}
+
+		ret = drm_syncobj_find_fence(submit->file, syncobj_desc.handle,

drivers/gpu/drm/virtio/virtgpu_submit.c static int virtio_gpu_dma_fence_wait(struct virtio_gpu_submit *submit, · line 129

Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_submit.c +9 -0
+					     syncobj_desc.point, 0, &fence);
+		if (ret)
+			break;
+
 
+		ret = virtio_gpu_dma_fence_wait(submit, fence);
+
+		dma_fence_put(fence);
+		if (ret)
+			break;

If going the DRM helpers route:

The above two are effectively the only variance across vendors - a simple function point as arg should suffice. Might want to use internal flags, but that's also trivial.

drivers/gpu/drm/virtio/virtgpu_submit.c static int virtio_gpu_dma_fence_wait(struct virtio_gpu_submit *submit, · line 158

Quoted 13 lines
drivers/gpu/drm/virtio/virtgpu_submit.c +13 -0
+	submit->in_syncobjs = syncobjs;
+
+	return ret;
+}
+
+static void virtio_gpu_reset_syncobjs(struct drm_syncobj **syncobjs,
+				      uint32_t nr_syncobjs)
+{
+	uint32_t i;
+
+	for (i = 0; i < nr_syncobjs; i++) {
+		if (syncobjs[i])
+			drm_syncobj_replace_fence(syncobjs[i], NULL);

Side note: the drm_syncobj_put() called immediately after also calls replace/reset fence internally. Although reading from the docs, I'm not sure if relying on that is a wise move.

Just thought I'd point it out.

drivers/gpu/drm/virtio/virtgpu_submit.c int virtio_gpu_execbuffer_ioctl(struct drm_device *dev, void *data, · line 505

Quoted 9 lines
drivers/gpu/drm/virtio/virtgpu_submit.c +8 -0
+	ret = virtio_gpu_parse_deps(&submit);
+	if (ret)
+		goto cleanup;
+
+	ret = virtio_gpu_parse_post_deps(&submit);
+	if (ret)
+		goto cleanup;
+

I think we should zero num_(in|out)_syncobjs when the respective parse fails. Otherwise we get one "cleanup" within the parse function itself and a second during the cleanup_submit. Haven't looked at it too closely but I suspect that will trigger an UAF or two.

drivers/gpu/drm/virtio/virtgpu_submit.c int virtio_gpu_execbuffer_ioctl(struct drm_device *dev, void *data, · line 515

Quoted 8 lines
drivers/gpu/drm/virtio/virtgpu_submit.c +1 -0
 	ret = virtio_gpu_install_out_fence_fd(&submit);
 	if (ret)
 		goto cleanup;
@@ -294,6 +512,7 @@ int virtio_gpu_execbuffer_ioctl(struct drm_device *dev, void *data,
 		goto cleanup;
 
 	virtio_gpu_submit(&submit);
+	virtio_gpu_process_post_deps(&submit);

Any particular reason why the virtio_gpu_reset_syncobjs is deferred to virtio_gpu_cleanup_submit(). Having it just above the process_post_deps (similar to msm) allows the reader to get closure about the in syncobjs.

This is just personal preference, so don't read too much into it.

HTH
Emil

author maintainer
#6 lore raw

Re: [PATCH v4 2/2] drm/virtio: Support sync objects

Quoted 7 lines · Emil Velikov

Hi Dmitry,

Have you considered creating a few DRM helpers for this functionality?

AFAICT this is the third driver which supports syncobj timelines and looking at one of the implementations ... it is not great. Note that this suggestion is _not_ a blocker.

Would like to see a third driver starting to use the exactly same drm_execbuffer_syncobj struct because UABI part isn't generic, though it's a replica of the MSM driver for now.

The virtio-gpu is only at the beginning of starting to use sync objects, compared to MSM driver. Will be better to defer the generalization until virtio-gpu will become more mature, like maybe after a year since the time virtio userspace will start using sync objects, IMO.

...

Quoted 13 lines

drivers/gpu/drm/virtio/virtgpu_submit.c static int virtio_gpu_dma_fence_wait(struct virtio_gpu_submit *submit, · line 158

Quoted 8 lines
drivers/gpu/drm/virtio/virtgpu_submit.c +8 -0
+static void virtio_gpu_reset_syncobjs(struct drm_syncobj **syncobjs,
+				      uint32_t nr_syncobjs)
+{
+	uint32_t i;
+
+	for (i = 0; i < nr_syncobjs; i++) {
+		if (syncobjs[i])
+			drm_syncobj_replace_fence(syncobjs[i], NULL);

Side note: the drm_syncobj_put() called immediately after also calls replace/reset fence internally. Although reading from the docs, I'm not sure if relying on that is a wise move.

Quoted 1 line

Just thought I'd point it out.

The drm_syncobj_put() doesn't call replace/reset fence until syncobj is freed. We drop the old fence for active/alive in-syncobj here after handling the fence-wait, this makes syncobj reusable, otherwise userpsace would have to re-create syncobjs after each submission.

Quoted 14 lines

drivers/gpu/drm/virtio/virtgpu_submit.c int virtio_gpu_execbuffer_ioctl(struct drm_device *dev, void *data, · line 505

Quoted 9 lines
drivers/gpu/drm/virtio/virtgpu_submit.c +8 -0
+	ret = virtio_gpu_parse_deps(&submit);
+	if (ret)
+		goto cleanup;
+
+	ret = virtio_gpu_parse_post_deps(&submit);
+	if (ret)
+		goto cleanup;
+

I think we should zero num_(in|out)_syncobjs when the respective parse fails. Otherwise we get one "cleanup" within the parse function itself and a second during the cleanup_submit. Haven't looked at it too closely but I suspect that will trigger an UAF or two.

There are checks for NULL pointers in the code that will prevent the UAF. I'll add zeroing of the nums for more consistency.

Quoted 6 lines
Quoted 6 lines
drivers/gpu/drm/virtio/virtgpu_submit.c +0 -0
 	ret = virtio_gpu_install_out_fence_fd(&submit);
 	if (ret)
 		goto cleanup;
@@ -294,6 +512,7 @@ int virtio_gpu_execbuffer_ioctl(struct drm_device *dev, void *data,
 		goto cleanup;
 
Quoted 8 lines

drivers/gpu/drm/virtio/virtgpu_submit.c int virtio_gpu_execbuffer_ioctl(struct drm_device *dev, void *data, · line 515

Quoted 2 lines
drivers/gpu/drm/virtio/virtgpu_submit.c +1 -0
 	virtio_gpu_submit(&submit);
+	virtio_gpu_process_post_deps(&submit);

Any particular reason why the virtio_gpu_reset_syncobjs is deferred to virtio_gpu_cleanup_submit(). Having it just above the process_post_deps (similar to msm) allows the reader to get closure about the in syncobjs.

This is just personal preference, so don't read too much into it.

The job submission path should be short as possible in general. Technically, virtio_gpu_process_post_deps() should be fast, but since I'm not 100% sure about all the corner cases, it's better to hold until job is sent out.

Thank you very much for the review! I'll address the rest of comments in v5.

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

Re: [PATCH v4 2/2] drm/virtio: Support sync objects

Quoted 10 lines · Dmitry Osipenko
Quoted 7 lines · Emil Velikov

Hi Dmitry,

Have you considered creating a few DRM helpers for this functionality?

AFAICT this is the third driver which supports syncobj timelines and looking at one of the implementations ... it is not great. Note that this suggestion is _not_ a blocker.

Quoted 9 lines

Would like to see a third driver starting to use the exactly same drm_execbuffer_syncobj struct because UABI part isn't generic, though it's a replica of the MSM driver for now.

The virtio-gpu is only at the beginning of starting to use sync objects, compared to MSM driver. Will be better to defer the generalization until virtio-gpu will become more mature, like maybe after a year since the time virtio userspace will start using sync objects, IMO.

I wasn't talking about generic UAPI, but having drm helpers instead. The former (as you pointed out) would need time to crystallize. While the latter can be done even today.

Quoted 14 lines

...

Quoted 13 lines

drivers/gpu/drm/virtio/virtgpu_submit.c static int virtio_gpu_dma_fence_wait(struct virtio_gpu_submit *submit, · line 158

Quoted 8 lines
drivers/gpu/drm/virtio/virtgpu_submit.c +8 -0
+static void virtio_gpu_reset_syncobjs(struct drm_syncobj **syncobjs,
+                                  uint32_t nr_syncobjs)
+{
+    uint32_t i;
+
+    for (i = 0; i < nr_syncobjs; i++) {
+            if (syncobjs[i])
+                    drm_syncobj_replace_fence(syncobjs[i], NULL);

Side note: the drm_syncobj_put() called immediately after also calls replace/reset fence internally. Although reading from the docs, I'm not sure if relying on that is a wise move.

Quoted 7 lines
Quoted 1 line

Just thought I'd point it out.

The drm_syncobj_put() doesn't call replace/reset fence until syncobj is freed. We drop the old fence for active/alive in-syncobj here after handling the fence-wait, this makes syncobj reusable, otherwise userpsace would have to re-create syncobjs after each submission.

I see, thanks.

Quoted 15 lines
Quoted 14 lines

drivers/gpu/drm/virtio/virtgpu_submit.c int virtio_gpu_execbuffer_ioctl(struct drm_device *dev, void *data, · line 505

Quoted 9 lines
drivers/gpu/drm/virtio/virtgpu_submit.c +8 -0
+    ret = virtio_gpu_parse_deps(&submit);
+    if (ret)
+            goto cleanup;
+
+    ret = virtio_gpu_parse_post_deps(&submit);
+    if (ret)
+            goto cleanup;
+

I think we should zero num_(in|out)_syncobjs when the respective parse fails. Otherwise we get one "cleanup" within the parse function itself and a second during the cleanup_submit. Haven't looked at it too closely but I suspect that will trigger an UAF or two.

Quoted 3 lines

There are checks for NULL pointers in the code that will prevent the UAF. I'll add zeroing of the nums for more consistency.

Riiiight the drm_syncobj is attached to the encapsulating struct virtio_gpu_submit _only_ on success.
By clearing the num variables, the NULL checks will no longer be needed ... in case you'd want to drop that.

Either way - even as-is the code is safe.

Quoted 13 lines
Quoted 13 lines

drivers/gpu/drm/virtio/virtgpu_submit.c int virtio_gpu_execbuffer_ioctl(struct drm_device *dev, void *data, · line 515

Quoted 8 lines
     ret = virtio_gpu_install_out_fence_fd(&submit);
     if (ret)
             goto cleanup;
drivers/gpu/drm/virtio/virtgpu_submit.c +1 -0
@@ -294,6 +512,7 @@ int virtio_gpu_execbuffer_ioctl(struct drm_device *dev, void *data,
             goto cleanup;
 
     virtio_gpu_submit(&submit);
+    virtio_gpu_process_post_deps(&submit);

Any particular reason why the virtio_gpu_reset_syncobjs is deferred to virtio_gpu_cleanup_submit(). Having it just above the process_post_deps (similar to msm) allows the reader to get closure about the in syncobjs.

Quoted 7 lines
Quoted 1 line

This is just personal preference, so don't read too much into it.

The job submission path should be short as possible in general. Technically, virtio_gpu_process_post_deps() should be fast, but since I'm not 100% sure about all the corner cases, it's better to hold until job is sent out.

Ack, thanks again

-Emil

#8 lore raw

Re: [PATCH v4 2/2] drm/virtio: Support sync objects

Quoted 9 lines · Emil Velikov
Quoted 8 lines
Quoted 4 lines

I think we should zero num_(in|out)_syncobjs when the respective parse fails. Otherwise we get one "cleanup" within the parse function itself and a second during the cleanup_submit. Haven't looked at it too closely but I suspect that will trigger an UAF or two.

There are checks for NULL pointers in the code that will prevent the UAF. I'll add zeroing of the nums for more consistency.

Quoted 7 lines

Riiiight the drm_syncobj is attached to the encapsulating struct virtio_gpu_submit _only_ on success.
By clearing the num variables, the NULL checks will no longer be needed ... in case you'd want to drop that.

Either way - even as-is the code is safe.

Err or not. The NULL check itself will cause NULL pointer deref.

In more detail: in/out syncobjs are memset() to NULL in virtio_gpu_init_submit(). The virtio_gpu_parse_(|post_)deps() will fail and leave them unchanged. Then virtio_gpu_free_syncobjs() and virtio_gpu_reset_syncobjs() will trigger a NULL ptr deref, because they are accessing the elements of a the (NULL) array.

Apart from the num_(in|out)_syncobjcs=0, I would drop the NULL checks
- they give a false sense of security IMHO.

-Emil

author maintainer
#9 lore raw

Re: [PATCH v4 2/2] drm/virtio: Support sync objects

Quoted 21 lines · Emil Velikov
Quoted 9 lines · Emil Velikov
Quoted 8 lines
Quoted 4 lines

I think we should zero num_(in|out)_syncobjs when the respective parse fails. Otherwise we get one "cleanup" within the parse function itself and a second during the cleanup_submit. Haven't looked at it too closely but I suspect that will trigger an UAF or two.

There are checks for NULL pointers in the code that will prevent the UAF. I'll add zeroing of the nums for more consistency.

Quoted 7 lines

Riiiight the drm_syncobj is attached to the encapsulating struct virtio_gpu_submit _only_ on success.
By clearing the num variables, the NULL checks will no longer be needed ... in case you'd want to drop that.

Either way - even as-is the code is safe.

Err or not. The NULL check itself will cause NULL pointer deref.

Quoted 8 lines

In more detail: in/out syncobjs are memset() to NULL in virtio_gpu_init_submit(). The virtio_gpu_parse_(|post_)deps() will fail and leave them unchanged. Then virtio_gpu_free_syncobjs() and virtio_gpu_reset_syncobjs() will trigger a NULL ptr deref, because they are accessing the elements of a the (NULL) array.

Apart from the num_(in|out)_syncobjcs=0, I would drop the NULL checks
- they give a false sense of security IMHO.

The reset/free are both under the NULL check on cleanup. I think it should work okay on error. Will improve it anyways to make more intuitive. Thanks!

static void virtio_gpu_cleanup_submit(struct virtio_gpu_submit *submit)

{
	if (submit->in_syncobjs) {
		virtio_gpu_reset_syncobjs(submit->in_syncobjs,
					  submit->num_in_syncobjs);

		virtio_gpu_free_syncobjs(submit->in_syncobjs,
					 submit->num_in_syncobjs);
	}
Signature · 2 lines
Best regards,
Dmitry