Skip to contentSkip to conversation

Add sync object UAPI support to VirtIO-GPU driver

PATCH Superseded Dmitry Osipenko 2 participants 5 messages Mar 19, 2023 – Mar 22, 2023

Subsystems (4)

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Dmitry Osipenko

author maintainer
#0 lore raw

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

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 | 510 ++++++++++++++++++++++++
 include/uapi/drm/virtgpu_drm.h          |  16 +-
 6 files changed, 532 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 v2 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 | 298 ++++++++++++++++++++++++
 4 files changed, 303 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 +298 -0

298 lines not on this page · raw message

Signature · 1 line
2.39.2
#2 lore raw

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

Quoted 5 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.

Quoted 9 lines
---
 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 | 298 ++++++++++++++++++++++++
 4 files changed, 303 insertions(+), 183 deletions(-)
 create mode 100644 drivers/gpu/drm/virtio/virtgpu_submit.c

<snip all the deletes>

Quoted 46 lines
drivers/gpu/drm/virtio/virtgpu_submit.c new file +40 -0
new file mode 100644
index 000000000000..a96f9d3285c7
@@ -0,0 +1,298 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright (C) 2015 Red Hat, Inc.
+ * All Rights Reserved.
+ *
+ * Authors:
+ *    Dave Airlie
+ *    Alon Levy
+ */
+
+#include <linux/file.h>
+#include <linux/sync_file.h>
+#include <linux/uaccess.h>
+
+#include <drm/drm_file.h>
+#include <drm/virtgpu_drm.h>
+
+#include "virtgpu_drv.h"
+
+struct virtio_gpu_submit {
+       struct virtio_gpu_object_array *buflist;
+       struct drm_virtgpu_execbuffer *exbuf;
+       struct virtio_gpu_fence *out_fence;
+       struct virtio_gpu_fpriv *vfpriv;
+       struct virtio_gpu_device *vgdev;
+       struct drm_file *file;
+       uint64_t fence_ctx;
+       uint32_t ring_idx;
+       int out_fence_fd;
+       void *buf;
+};
+
+static int virtio_gpu_do_fence_wait(struct virtio_gpu_submit *submit,
+                                   struct dma_fence *dma_fence)
+{
+       uint32_t context = submit->fence_ctx + submit->ring_idx;
+
+       if (dma_fence_match_context(dma_fence, context))
+               return 0;
+

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

Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_submit.c new file +10 -0
+       return dma_fence_wait(dma_fence, true);
+}
+
+static int virtio_gpu_dma_fence_wait(struct virtio_gpu_submit *submit,
+                                    struct dma_fence *fence)
+{
+       struct dma_fence *itr;
+       int idx, err;
+
+       dma_fence_array_for_each(itr, idx, fence) {

I guess unwrapping is for the later step of host waits?

At any rate, I think you should use dma_fence_unwrap_for_each() to handle the fence-chain case as well?

Quoted 105 lines
drivers/gpu/drm/virtio/virtgpu_submit.c new file +105 -0
+               err = virtio_gpu_do_fence_wait(submit, itr);
+               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,
+                                        uint32_t ring_idx)
+{
+       struct virtio_gpu_fpriv *vfpriv = file->driver_priv;
+       struct virtio_gpu_fence_event *e = NULL;
+       int ret;
+
+       if (!(vfpriv->ring_idx_mask & BIT_ULL(ring_idx)))
+               return 0;
+
+       e = kzalloc(sizeof(*e), GFP_KERNEL);
+       if (!e)
+               return -ENOMEM;
+
+       e->event.type = VIRTGPU_EVENT_FENCE_SIGNALED;
+       e->event.length = sizeof(e->event);
+
+       ret = drm_event_reserve_init(dev, file, &e->base, &e->event);
+       if (ret) {
+               kfree(e);
+               return ret;
+       }
+
+       fence->e = e;
+
+       return 0;
+}
+
+static int virtio_gpu_init_submit_buflist(struct virtio_gpu_submit *submit)
+{
+       struct drm_virtgpu_execbuffer *exbuf = submit->exbuf;
+       uint32_t *bo_handles;
+
+       if (!exbuf->num_bo_handles)
+               return 0;
+
+       bo_handles = kvmalloc_array(exbuf->num_bo_handles, sizeof(uint32_t),
+                                   GFP_KERNEL);
+       if (!bo_handles)
+               return -ENOMEM;
+
+       if (copy_from_user(bo_handles, u64_to_user_ptr(exbuf->bo_handles),
+                          exbuf->num_bo_handles * sizeof(uint32_t))) {
+               kvfree(bo_handles);
+               return -EFAULT;
+       }
+
+       submit->buflist = virtio_gpu_array_from_handles(submit->file, bo_handles,
+                                                       exbuf->num_bo_handles);
+       if (!submit->buflist) {
+               kvfree(bo_handles);
+               return -ENOENT;
+       }
+
+       kvfree(bo_handles);
+
+       return 0;
+}
+
+static void virtio_gpu_cleanup_submit(struct virtio_gpu_submit *submit)
+{
+       if (!IS_ERR(submit->buf))
+               kvfree(submit->buf);
+
+       if (submit->buflist)
+               virtio_gpu_array_put_free(submit->buflist);
+
+       if (submit->out_fence_fd >= 0)
+               put_unused_fd(submit->out_fence_fd);
+}
+
+static void virtio_gpu_submit(struct virtio_gpu_submit *submit)
+{
+       virtio_gpu_cmd_submit(submit->vgdev, submit->buf, submit->exbuf->size,
+                             submit->vfpriv->ctx_id, submit->buflist,
+                             submit->out_fence);
+       virtio_gpu_notify(submit->vgdev);
+
+       submit->buf = NULL;
+       submit->buflist = NULL;
+       submit->out_fence_fd = -1;
+}
+
+static int virtio_gpu_init_submit(struct virtio_gpu_submit *submit,
+                                 struct drm_virtgpu_execbuffer *exbuf,
+                                 struct drm_device *dev,
+                                 struct drm_file *file,
+                                 uint64_t fence_ctx, uint32_t ring_idx)
+{
+       struct virtio_gpu_fpriv *vfpriv = file->driver_priv;
+       struct virtio_gpu_device *vgdev = dev->dev_private;
+       struct virtio_gpu_fence *out_fence;
+       int err;
+
+       memset(submit, 0, sizeof(*submit));

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

Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_submit.c new file +10 -0
+
+       out_fence = virtio_gpu_fence_alloc(vgdev, fence_ctx, ring_idx);
+       if (!out_fence)
+               return -ENOMEM;
+
+       err = virtio_gpu_fence_event_create(dev, file, out_fence, ring_idx);
+       if (err) {
+               dma_fence_put(&out_fence->f);
+               return err;
+       }

If we fail at any point after here, where is the out_fence referenced dropped?

Quoted 70 lines
drivers/gpu/drm/virtio/virtgpu_submit.c new file +70 -0
+
+       submit->out_fence = out_fence;
+       submit->fence_ctx = fence_ctx;
+       submit->ring_idx = ring_idx;
+       submit->out_fence_fd = -1;
+       submit->vfpriv = vfpriv;
+       submit->vgdev = vgdev;
+       submit->exbuf = exbuf;
+       submit->file = file;
+
+       err = virtio_gpu_init_submit_buflist(submit);
+       if (err)
+               return err;
+
+       submit->buf = vmemdup_user(u64_to_user_ptr(exbuf->command), exbuf->size);
+       if (IS_ERR(submit->buf))
+               return PTR_ERR(submit->buf);
+
+       if (exbuf->flags & VIRTGPU_EXECBUF_FENCE_FD_OUT) {
+               err = get_unused_fd_flags(O_CLOEXEC);
+               if (err < 0)
+                       return err;
+
+               submit->out_fence_fd = err;
+       }
+
+       return 0;
+}
+
+static int virtio_gpu_wait_in_fence(struct virtio_gpu_submit *submit)
+{
+       int ret = 0;
+
+       if (submit->exbuf->flags & VIRTGPU_EXECBUF_FENCE_FD_IN) {
+               struct dma_fence *in_fence =
+                               sync_file_get_fence(submit->exbuf->fence_fd);
+               if (!in_fence)
+                       return -EINVAL;
+
+               /*
+                * Wait if the fence is from a foreign context, or if the fence
+                * array contains any fence from a foreign context.
+                */
+               ret = virtio_gpu_dma_fence_wait(submit, in_fence);
+
+               dma_fence_put(in_fence);
+       }
+
+       return ret;
+}
+
+static int virtio_gpu_install_out_fence_fd(struct virtio_gpu_submit *submit)
+{
+       if (submit->out_fence_fd >= 0) {
+               struct sync_file *sync_file =
+                                       sync_file_create(&submit->out_fence->f);
+               if (!sync_file)
+                       return -ENOMEM;
+
+               submit->exbuf->fence_fd = submit->out_fence_fd;
+               fd_install(submit->out_fence_fd, sync_file->file);
+       }
+
+       return 0;
+}
+
+static int virtio_gpu_lock_buflist(struct virtio_gpu_submit *submit)
+{
+       if (submit->buflist)
+               return virtio_gpu_array_lock_resv(submit->buflist);

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

Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_submit.c new file +10 -0
+
+       return 0;
+}
+
+/*
+ * Usage of execbuffer:
+ * Relocations need to take into account the full VIRTIO_GPUDrawable size.
+ * However, the command as passed from user space must *not* contain the initial
+ * VIRTIO_GPUReleaseInfo struct (first XXX bytes)
+ */

I know this is just getting moved from the old location, but I'm not even sure what this comment means ;-)

At least it doesn't make any sense for non-virgl contexts.. I haven't looked too closely at virgl protocol itself

BR,
-R

Quoted 56 lines
drivers/gpu/drm/virtio/virtgpu_submit.c new file +53 -1
+int virtio_gpu_execbuffer_ioctl(struct drm_device *dev, void *data,
+                               struct drm_file *file)
+{
+       struct virtio_gpu_device *vgdev = dev->dev_private;
+       struct virtio_gpu_fpriv *vfpriv = file->driver_priv;
+       uint64_t fence_ctx = vgdev->fence_drv.context;
+       struct drm_virtgpu_execbuffer *exbuf = data;
+       struct virtio_gpu_submit submit;
+       uint32_t ring_idx = 0;
+       int ret = -EINVAL;
+
+       if (vgdev->has_virgl_3d == false)
+               return -ENOSYS;
+
+       if ((exbuf->flags & ~VIRTGPU_EXECBUF_FLAGS))
+               return ret;
+
+       if ((exbuf->flags & VIRTGPU_EXECBUF_RING_IDX)) {
+               if (exbuf->ring_idx >= vfpriv->num_rings)
+                       return ret;
+
+               if (!vfpriv->base_fence_ctx)
+                       return ret;
+
+               fence_ctx = vfpriv->base_fence_ctx;
+               ring_idx = exbuf->ring_idx;
+       }
+
+       virtio_gpu_create_context(dev, file);
+
+       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;
+
+       ret = virtio_gpu_install_out_fence_fd(&submit);
+       if (ret)
+               goto cleanup;
+
+       ret = virtio_gpu_lock_buflist(&submit);
+       if (ret)
+               goto cleanup;
+
+       virtio_gpu_submit(&submit);
+cleanup:
+       virtio_gpu_cleanup_submit(&submit);
+
+       return ret;
+}
--

2.39.2

author maintainer
#3 lore raw

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

On 3/23/23 00:25, Rob Clark wrote:
...

Quoted 10 lines

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

Quoted 7 lines
drivers/gpu/drm/virtio/virtgpu_submit.c new file +7 -0
+static int virtio_gpu_dma_fence_wait(struct virtio_gpu_submit *submit,
+                                    struct dma_fence *fence)
+{
+       struct dma_fence *itr;
+       int idx, err;
+
+       dma_fence_array_for_each(itr, idx, fence) {

I guess unwrapping is for the later step of host waits?

Quoted 2 lines

At any rate, I think you should use dma_fence_unwrap_for_each() to handle the fence-chain case as well?

Yes, seems so. I actually missed the dma_fence_unwrap, thanks!

...

Quoted 16 lines
Quoted 16 lines
drivers/gpu/drm/virtio/virtgpu_submit.c new file +16 -0
+static int virtio_gpu_init_submit(struct virtio_gpu_submit *submit,
+                                 struct drm_virtgpu_execbuffer *exbuf,
+                                 struct drm_device *dev,
+                                 struct drm_file *file,
+                                 uint64_t fence_ctx, uint32_t ring_idx)
+{
+       struct virtio_gpu_fpriv *vfpriv = file->driver_priv;
+       struct virtio_gpu_device *vgdev = dev->dev_private;
+       struct virtio_gpu_fence *out_fence;
+       int err;
+
+       memset(submit, 0, sizeof(*submit));
+
+       out_fence = virtio_gpu_fence_alloc(vgdev, fence_ctx, ring_idx);
+       if (!out_fence)
+               return -ENOMEM;
Quoted 8 lines

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

Quoted 6 lines
drivers/gpu/drm/virtio/virtgpu_submit.c new file +6 -0
+
+       err = virtio_gpu_fence_event_create(dev, file, out_fence, ring_idx);
+       if (err) {
+               dma_fence_put(&out_fence->f);
+               return err;
+       }

If we fail at any point after here, where is the out_fence referenced dropped?

Good catch, don't see either where it's dropped. Perhaps the drop got lost after moving the code around, will fix.

...

Quoted 7 lines
Quoted 6 lines

+/*
+ * Usage of execbuffer:
+ * Relocations need to take into account the full VIRTIO_GPUDrawable size.
+ * However, the command as passed from user space must *not* contain the initial
+ * VIRTIO_GPUReleaseInfo struct (first XXX bytes)
+ */

Quoted 5 lines

I know this is just getting moved from the old location, but I'm not even sure what this comment means ;-)

At least it doesn't make any sense for non-virgl contexts.. I haven't looked too closely at virgl protocol itself

Had exactly the same thought :) Well, if nobody will clarify, then I'm happy with removing it in v3.

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

PATCH v2 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 | 214 +++++++++++++++++++++++-
 include/uapi/drm/virtgpu_drm.h          |  16 +-
 3 files changed, 230 insertions(+), 3 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 +213 -1

263 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