Skip to contentSkip to conversation

Add sync object UAPI support to VirtIO-GPU driver

PATCH Superseded Dmitry Osipenko 2 participants 6 messages Apr 9, 2023 – Apr 12, 2023

Subsystems (4)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Dmitry Osipenko

author maintainer
#0 lore raw

v5 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:

v5: - Factored out dma-fence unwrap API usage into separate patch as was
suggested by Emil Velikov.

- Improved and documented the job submission reorderings as was requested by Emil Velikov. Sync file FD is now installed after job is submitted to virtio to further optimize reorderings.

- Added comment for the kvalloc, as was requested by Emil Velikov.

- The num_in/out_syncobjs now is set only after completed parsing of pre/post deps, as was requested by Emil Velikov.

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 (3):
drm/virtio: Refactor and optimize job submission code path drm/virtio: Wait for each dma-fence of in-fence array individually 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 | 525 ++++++++++++++++++++++++
 include/uapi/drm/virtgpu_drm.h          |  16 +-
 6 files changed, 547 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 v5 1/3 drm/virtio: Refactor and optimize job submission code path

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

The optimization is done by using optimal ordering of the job's submission steps, reducing code path from the start of the ioctl to the point of pushing job to virtio queue. Job's initialization is now performed before in-fence is awaited and out-fence setup is made after sending out job to virtio.

---
 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 | 295 ++++++++++++++++++++++++
 4 files changed, 300 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 +295 -0

295 lines not on this page · raw message

Signature · 1 line
2.39.2
author maintainer
#2 lore raw

PATCH v5 2/3 drm/virtio: Wait for each dma-fence of in-fence array individually

In reply to Dmitry Osipenko

Use dma-fence-unwrap API for waiting each dma-fence of the in-fence array individually. Sync file's in-fence array always has a non-matching fence context ID, which doesn't allow to skip waiting of fences with a matching context ID in a case of a merged sync file fence.

---
 drivers/gpu/drm/virtio/virtgpu_submit.c | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)
drivers/gpu/drm/virtio/virtgpu_submit.c +18 -2
index 902734778d1b..b60dea077240 100644
@@ -32,8 +32,8 @@ struct virtio_gpu_submit {
 	void *buf;
 };
 
-static int virtio_gpu_dma_fence_wait(struct virtio_gpu_submit *submit,
-				     struct dma_fence *in_fence)
+static int virtio_gpu_do_fence_wait(struct virtio_gpu_submit *submit,
+				    struct dma_fence *in_fence)
 {
 	uint32_t context = submit->fence_ctx + submit->ring_idx;
 
@@ -43,6 +43,22 @@ static int virtio_gpu_dma_fence_wait(struct virtio_gpu_submit *submit,
 	return dma_fence_wait(in_fence, true);
 }
 
+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) {
+		err = virtio_gpu_do_fence_wait(submit, f);
+		if (err)
+			return err;
+	}
+
+	return 0;
+}
+
 static int virtio_gpu_fence_event_create(struct drm_device *dev,
 					 struct drm_file *file,
 					 struct virtio_gpu_fence *fence,
Signature · 1 line
2.39.2
author maintainer
#3 lore raw

PATCH v5 3/3 drm/virtio: Support sync objects

In reply to Dmitry Osipenko

Add sync object DRM UAPI support to VirtIO-GPU driver. Sync objects support is needed by native context VirtIO-GPU Mesa drivers, it also will be used by Venus and Virgl contexts.

---
 drivers/gpu/drm/virtio/virtgpu_drv.c    |   3 +-
 drivers/gpu/drm/virtio/virtgpu_submit.c | 214 ++++++++++++++++++++++++
 include/uapi/drm/virtgpu_drm.h          |  16 +-
 3 files changed, 231 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 +214 -0

249 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
#4 lore raw

Re: [PATCH v5 3/3] drm/virtio: Support sync objects

Hi Dmitry,

Quoted 15 lines · Dmitry Osipenko
drivers/gpu/drm/virtio/virtgpu_submit.c +14 -0
+static void virtio_gpu_free_syncobjs(struct drm_syncobj **syncobjs,
+                                    uint32_t nr_syncobjs)
+{
+       uint32_t i = nr_syncobjs;
+
+       while (i--) {
+               if (syncobjs[i])
+                       drm_syncobj_put(syncobjs[i]);
+       }
+
+       kvfree(syncobjs);
+}
+
 
+static void virtio_gpu_reset_syncobjs(struct drm_syncobj **syncobjs,

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

Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_submit.c +10 -0
+                                     uint32_t nr_syncobjs)
+{
+       uint32_t i;
+
+       for (i = 0; i < nr_syncobjs; i++) {
+               if (syncobjs[i])
+                       drm_syncobj_replace_fence(syncobjs[i], NULL);
+       }
+}
+

Can I bribe you (with cookies) about dropping the NULL checks above? They're dead code and rather misleading IMHO.

Quoted 23 lines
drivers/gpu/drm/virtio/virtgpu_submit.c +23 -0
+static void
+virtio_gpu_free_post_deps(struct virtio_gpu_submit_post_dep *post_deps,
+                         uint32_t nr_syncobjs)
+{
+       uint32_t i = nr_syncobjs;
+
+       while (i--) {
+               kfree(post_deps[i].chain);
+               drm_syncobj_put(post_deps[i].syncobj);
+       }
+
+       kvfree(post_deps);
+}
+
+static int virtio_gpu_parse_post_deps(struct virtio_gpu_submit *submit)
+{
+       struct drm_virtgpu_execbuffer *exbuf = submit->exbuf;
+       struct drm_virtgpu_execbuffer_syncobj syncobj_desc;
+       struct virtio_gpu_submit_post_dep *post_deps;
+       u32 num_out_syncobjs = exbuf->num_out_syncobjs;
+       size_t syncobj_stride = exbuf->syncobj_stride;
+       int ret = 0, i;
+

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

Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_submit.c +10 -0
+       if (!num_out_syncobjs)
+               return 0;
+
+       post_deps = kvcalloc(num_out_syncobjs, sizeof(*post_deps), GFP_KERNEL);
+       if (!post_deps)
+               return -ENOMEM;
+
+       for (i = 0; i < num_out_syncobjs; i++) {
+               uint64_t address = exbuf->out_syncobjs + i * syncobj_stride;
+

For my own education: what's the specifics/requirements behind the stride? is there a use-case for the stride to vary across in/out syncobj?

Off the top of my head: userspace could have an array of larger structs, each embedding an syncobj. Thus passing the stride, the kernel will fetch/update them in-place w/o caring about the other data.
Or perhaps there is another trick that userspace utilises the stride for?

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

Quoted 7 lines
drivers/gpu/drm/virtio/virtgpu_submit.c +7 -0
+               if (copy_from_user(&syncobj_desc,
+                                  u64_to_user_ptr(address),
+                                  min(syncobj_stride, sizeof(syncobj_desc)))) {
+                       ret = -EFAULT;
+                       break;
+               }
+

We seem to be parsing garbage(?) stack data in the syncobj_stride < sizeof(syncobj_desc) case.

Zeroing/reseting the syncobj_desc on each iteration is one approach - be that fully or in part. Alternatively we could error out on syncobj_stride < sizeof(syncobj_desc).

Quoted 9 lines
drivers/gpu/drm/virtio/virtgpu_submit.c +9 -0
+               post_deps[i].point = syncobj_desc.point;
+
+               if (syncobj_desc.flags) {
+                       ret = -EINVAL;
+                       break;
+               }
+
+               if (syncobj_desc.point) {
+                       post_deps[i].chain = dma_fence_chain_alloc();

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

Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_submit.c +10 -0
+                       if (!post_deps[i].chain) {
+                               ret = -ENOMEM;
+                               break;
+                       }
+               }
+
+               post_deps[i].syncobj = drm_syncobj_find(submit->file,
+                                                       syncobj_desc.handle);
+               if (!post_deps[i].syncobj) {
+                       ret = -EINVAL;

I think we want a kfree(chain) here. Otherwise we'll leak it, right?

Quoted 5 lines
drivers/gpu/drm/virtio/virtgpu_submit.c +5 -0
+                       break;
+               }
+       }
+
+       if (ret) {

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

Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_submit.c +10 -0
+               virtio_gpu_free_post_deps(post_deps, i);
+               return ret;
+       }
+
+       submit->num_out_syncobjs = num_out_syncobjs;
+       submit->post_deps = post_deps;
+
+       return 0;
+}
+

With the two issues in virtio_gpu_parse_post_deps() addressed, the series is:
Reviewed-by; Emil Velikov <[email protected]>

HTH
Emil

author maintainer
#5 lore raw

Re: [PATCH v5 3/3] drm/virtio: Support sync objects

Hello,

Quoted 19 lines · Emil Velikov

Hi Dmitry,

Quoted 13 lines · Dmitry Osipenko
drivers/gpu/drm/virtio/virtgpu_submit.c +13 -0
+static void virtio_gpu_free_syncobjs(struct drm_syncobj **syncobjs,
+                                    uint32_t nr_syncobjs)
+{
+       uint32_t i = nr_syncobjs;
+
+       while (i--) {
+               if (syncobjs[i])
+                       drm_syncobj_put(syncobjs[i]);
+       }
+
+       kvfree(syncobjs);
+}
+
Quoted 14 lines

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

Quoted 11 lines
drivers/gpu/drm/virtio/virtgpu_submit.c +11 -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);
+       }
+}
+

Can I bribe you (with cookies) about dropping the NULL checks above? They're dead code and rather misleading IMHO.

When userspace doesn't set the VIRTGPU_EXECBUF_SYNCOBJ_RESET flag in virtio_gpu_parse_deps(), the syncobjs[i] is NULL. Hence not a dead code at all :)

Quoted 29 lines
Quoted 29 lines
drivers/gpu/drm/virtio/virtgpu_submit.c +29 -0
+static void
+virtio_gpu_free_post_deps(struct virtio_gpu_submit_post_dep *post_deps,
+                         uint32_t nr_syncobjs)
+{
+       uint32_t i = nr_syncobjs;
+
+       while (i--) {
+               kfree(post_deps[i].chain);
+               drm_syncobj_put(post_deps[i].syncobj);
+       }
+
+       kvfree(post_deps);
+}
+
+static int virtio_gpu_parse_post_deps(struct virtio_gpu_submit *submit)
+{
+       struct drm_virtgpu_execbuffer *exbuf = submit->exbuf;
+       struct drm_virtgpu_execbuffer_syncobj syncobj_desc;
+       struct virtio_gpu_submit_post_dep *post_deps;
+       u32 num_out_syncobjs = exbuf->num_out_syncobjs;
+       size_t syncobj_stride = exbuf->syncobj_stride;
+       int ret = 0, i;
+
+       if (!num_out_syncobjs)
+               return 0;
+
+       post_deps = kvcalloc(num_out_syncobjs, sizeof(*post_deps), GFP_KERNEL);
+       if (!post_deps)
+               return -ENOMEM;
Quoted 8 lines

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

Quoted 4 lines
drivers/gpu/drm/virtio/virtgpu_submit.c +4 -0
+
+       for (i = 0; i < num_out_syncobjs; i++) {
+               uint64_t address = exbuf->out_syncobjs + i * syncobj_stride;
+

For my own education: what's the specifics/requirements behind the stride? is there a use-case for the stride to vary across in/out syncobj?

Stride is the same for in/out syncobjs as the same struct drm_virtgpu_execbuffer_syncobj is used by both.

The out-syncobj don't use the "flags" field of drm_virtgpu_execbuffer_syncobj. We could use separate strides and desc for in/out syncobjs, but in practice it's unlikely that we will be extending the desc anytime soon and usually there are not many out-syncobj to care about the wasted field.

On the other hand, if we will ever need to extend desc for in-syncobjs, there will be more wasted fields. Maybe it indeed won't hurt to split in/out syncobjs, for consistency. I'll think about it for v6, thanks.

Quoted 5 lines

Off the top of my head: userspace could have an array of larger structs, each embedding an syncobj. Thus passing the stride, the kernel will fetch/update them in-place w/o caring about the other data.
Or perhaps there is another trick that userspace utilises the stride for?

Stride is only about potential future expansion of the struct drm_virtgpu_execbuffer_syncobj with new fields. There shouldn't be any special tricks for userspace to use.

Quoted 8 lines

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

Quoted 7 lines
drivers/gpu/drm/virtio/virtgpu_submit.c +7 -0
+               if (copy_from_user(&syncobj_desc,
+                                  u64_to_user_ptr(address),
+                                  min(syncobj_stride, sizeof(syncobj_desc)))) {
+                       ret = -EFAULT;
+                       break;
+               }
+
Quoted 6 lines

We seem to be parsing garbage(?) stack data in the syncobj_stride < sizeof(syncobj_desc) case.

Zeroing/reseting the syncobj_desc on each iteration is one approach - be that fully or in part. Alternatively we could error out on syncobj_stride < sizeof(syncobj_desc).

Good catch! It indeed needs to be zeroed. Nothing terrible will happen today for kernel if it will use garbage data, but a malfunctioning userspace may happen to appear working properly.

Quoted 13 lines
Quoted 13 lines
drivers/gpu/drm/virtio/virtgpu_submit.c +13 -0
+               post_deps[i].point = syncobj_desc.point;
+
+               if (syncobj_desc.flags) {
+                       ret = -EINVAL;
+                       break;
+               }
+
+               if (syncobj_desc.point) {
+                       post_deps[i].chain = dma_fence_chain_alloc();
+                       if (!post_deps[i].chain) {
+                               ret = -ENOMEM;
+                               break;
+                       }
Quoted 8 lines

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

Quoted 6 lines
drivers/gpu/drm/virtio/virtgpu_submit.c +6 -0
+               }
+
+               post_deps[i].syncobj = drm_syncobj_find(submit->file,
+                                                       syncobj_desc.handle);
+               if (!post_deps[i].syncobj) {
+                       ret = -EINVAL;

I think we want a kfree(chain) here. Otherwise we'll leak it, right?

I'm sure there was a kfree here in one of previous version of the patch. Another good catch, thanks :)

Quoted 11 lines
Quoted 11 lines
drivers/gpu/drm/virtio/virtgpu_submit.c +11 -0
+                       break;
+               }
+       }
+
+       if (ret) {
+               virtio_gpu_free_post_deps(post_deps, i);
+               return ret;
+       }
+
+       submit->num_out_syncobjs = num_out_syncobjs;
+       submit->post_deps = post_deps;
Quoted 8 lines

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

Quoted 4 lines
drivers/gpu/drm/virtio/virtgpu_submit.c +4 -0
+
+       return 0;
+}
+

With the two issues in virtio_gpu_parse_post_deps() addressed, the series is:
Reviewed-by; Emil Velikov <[email protected]>

Thanks you for the review!

Signature · 2 lines
Best regards,
Dmitry