Skip to contentSkip to conversation

i915: Add support for drm syncobjs

PATCH In review Jason Ekstrand 2 participants 11 messages Jul 5, 2017 – Aug 3, 2017

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

PATCH 1/1 i915: Add support for drm syncobjs

This commit adds support for waiting on or signaling DRM syncobjs as part of execbuf. It does so by hijacking the currently unused cliprects pointer to instead point to an array of i915_gem_exec_fence structs which containe a DRM syncobj and a flags parameter which specifies whether to wait on it or to signal it. This implementation theoretically allows for both flags to be set in which case it waits on the dma_fence that was in the syncobj and then immediately replaces it with the dma_fence from the current execbuf.

Cc Chris Wilson <[email protected]>

---

Ideally, I'd like to get whatever kernel reviewing and/or merging is required to land the userspace bits ASAP. That said, I understand that this is my first kernel patch and it's liable to have a few rounds of review before going in. :-)

The mesa branch for using this in Vulkan can be found here:

https://cgit.freedesktop.org/~jekstrand/mesa/log/?h=review/anv-syncobj

 drivers/gpu/drm/i915/i915_drv.c            |  3 +-
 drivers/gpu/drm/i915/i915_gem_execbuffer.c | 97 ++++++++++++++++++++++++++++--
 include/uapi/drm/i915_drm.h                | 30 ++++++++-
 3 files changed, 121 insertions(+), 9 deletions(-)
drivers/gpu/drm/i915/i915_drv.c +2 -1
index 9167a73..e6f7f49 100644
@@ -384,6 +384,7 @@ static int i915_getparam(struct drm_device *dev, void *data,
 	case I915_PARAM_HAS_EXEC_FENCE:
 	case I915_PARAM_HAS_EXEC_CAPTURE:
 	case I915_PARAM_HAS_EXEC_BATCH_FIRST:
+	case I915_PARAM_HAS_EXEC_FENCE_ARRAY:
 		/* For the time being all of these are always true;
 		 * if some supported hardware does not have one of these
 		 * features this value needs to be provided from
@@ -2734,7 +2735,7 @@ static struct drm_driver driver = {
 	 */
 	.driver_features =
 	    DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED | DRIVER_GEM | DRIVER_PRIME |
-	    DRIVER_RENDER | DRIVER_MODESET | DRIVER_ATOMIC,
+	    DRIVER_RENDER | DRIVER_MODESET | DRIVER_ATOMIC | DRIVER_SYNCOBJ,
 	.release = i915_driver_release,
 	.open = i915_driver_open,
 	.lastclose = i915_driver_lastclose,
drivers/gpu/drm/i915/i915_gem_execbuffer.c +91 -6

169 lines not on this page · raw message

include/uapi/drm/i915_drm.h +28 -2
index 7ccbd6a..5b5f033 100644
@@ -431,6 +431,11 @@ typedef struct drm_i915_irq_wait {
  */
 #define I915_PARAM_HAS_EXEC_BATCH_FIRST	 48
 
+/* Query whether DRM_I915_GEM_EXECBUFFER2 supports supplying an array of
+ * drm_i915_gem_exec_fence structures.  See I915_EXEC_FENCE_ARRAY.
+ */
+#define I915_PARAM_HAS_EXEC_FENCE_ARRAY  49
+
 typedef struct drm_i915_getparam {
 	__s32 param;
 	/*
@@ -812,6 +817,17 @@ struct drm_i915_gem_exec_object2 {
 	__u64 rsvd2;
 };
 
+struct drm_i915_gem_exec_fence {
+	/**
+	 * User's handle for a dma-fence to wait on or signal.
+	 */
+	__u32 handle;
+
+#define I915_EXEC_FENCE_WAIT            (1<<0)
+#define I915_EXEC_FENCE_SIGNAL          (1<<1)
+	__u32 flags;
+};
+
 struct drm_i915_gem_execbuffer2 {
 	/**
 	 * List of gem_exec_object2 structs
@@ -826,7 +842,10 @@ struct drm_i915_gem_execbuffer2 {
 	__u32 DR1;
 	__u32 DR4;
 	__u32 num_cliprects;
-	/** This is a struct drm_clip_rect *cliprects */
+	/** This is a struct drm_clip_rect *cliprects if I915_EXEC_FENCE_ARRAY
+     * is not set.  If I915_EXEC_FENCE_ARRAY is set, then this is a
+     * struct drm_i915_gem_exec_fence *fences.
+     */
 	__u64 cliprects_ptr;
 #define I915_EXEC_RING_MASK              (7<<0)
 #define I915_EXEC_DEFAULT                (0<<0)
@@ -927,7 +946,14 @@ struct drm_i915_gem_execbuffer2 {
  * element).
  */
 #define I915_EXEC_BATCH_FIRST		(1<<18)
-#define __I915_EXEC_UNKNOWN_FLAGS (-(I915_EXEC_BATCH_FIRST<<1))
+
+/* Setting I915_FENCE_ARRAY implies that num_cliprects and cliprects_ptr
+ * define an array of i915_gem_exec_fence structures which specify a set of
+ * dma fences to wait upon or signal.
+ */
+#define I915_EXEC_FENCE_ARRAY   (1<<19)
+
+#define __I915_EXEC_UNKNOWN_FLAGS (-(I915_EXEC_FENCE_ARRAY<<1))
 
 #define I915_EXEC_CONTEXT_ID_MASK	(0xffffffff)
 #define i915_execbuffer2_set_context_id(eb2, context) \
Signature · 6 lines
2.5.0.400.gff86faf

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#1 lore raw

Quoting Jason Ekstrand (2017-07-05 18:21:22)

Quoted 102 lines

This commit adds support for waiting on or signaling DRM syncobjs as part of execbuf. It does so by hijacking the currently unused cliprects pointer to instead point to an array of i915_gem_exec_fence structs which containe a DRM syncobj and a flags parameter which specifies whether to wait on it or to signal it. This implementation theoretically allows for both flags to be set in which case it waits on the dma_fence that was in the syncobj and then immediately replaces it with the dma_fence from the current execbuf.

Cc Chris Wilson <[email protected]>

---

Ideally, I'd like to get whatever kernel reviewing and/or merging is required to land the userspace bits ASAP. That said, I understand that this is my first kernel patch and it's liable to have a few rounds of review before going in. :-)

The mesa branch for using this in Vulkan can be found here:

https://cgit.freedesktop.org/~jekstrand/mesa/log/?h=review/anv-syncobj

 drivers/gpu/drm/i915/i915_drv.c            |  3 +-
 drivers/gpu/drm/i915/i915_gem_execbuffer.c | 97 ++++++++++++++++++++++++++++--
 include/uapi/drm/i915_drm.h                | 30 ++++++++-
 3 files changed, 121 insertions(+), 9 deletions(-)
drivers/gpu/drm/i915/i915_drv.c +2 -1
index 9167a73..e6f7f49 100644
@@ -384,6 +384,7 @@ static int i915_getparam(struct drm_device *dev, void *data,
        case I915_PARAM_HAS_EXEC_FENCE:
        case I915_PARAM_HAS_EXEC_CAPTURE:
        case I915_PARAM_HAS_EXEC_BATCH_FIRST:
+       case I915_PARAM_HAS_EXEC_FENCE_ARRAY:
                /* For the time being all of these are always true;
                 * if some supported hardware does not have one of these
                 * features this value needs to be provided from
@@ -2734,7 +2735,7 @@ static struct drm_driver driver = {
         */
        .driver_features =
            DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED | DRIVER_GEM | DRIVER_PRIME |
-           DRIVER_RENDER | DRIVER_MODESET | DRIVER_ATOMIC,
+           DRIVER_RENDER | DRIVER_MODESET | DRIVER_ATOMIC | DRIVER_SYNCOBJ,
        .release = i915_driver_release,
        .open = i915_driver_open,
        .lastclose = i915_driver_lastclose,
drivers/gpu/drm/i915/i915_gem_execbuffer.c +18 -3
index 929f275..81b7b7b 100644
@@ -33,6 +33,7 @@
 
 #include <drm/drmP.h>
 #include <drm/i915_drm.h>
+#include <drm/drm_syncobj.h>
 
 #include "i915_drv.h"
 #include "i915_gem_clflush.h"
@@ -1882,8 +1883,10 @@ static bool i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
                return false;
 
        /* Kernel clipping was a DRI1 misfeature */
-       if (exec->num_cliprects || exec->cliprects_ptr)
-               return false;
+       if (!(exec->flags & I915_EXEC_FENCE_ARRAY)) {
+               if (exec->num_cliprects || exec->cliprects_ptr)
+                       return false;
+       }
 
        if (exec->DR4 == 0xffffffff) {
                DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
@@ -2118,12 +2121,15 @@ static int
 i915_gem_do_execbuffer(struct drm_device *dev,
                       struct drm_file *file,
                       struct drm_i915_gem_execbuffer2 *args,
-                      struct drm_i915_gem_exec_object2 *exec)
+                      struct drm_i915_gem_exec_object2 *exec,
+                      struct drm_i915_gem_exec_fence *fences)
 {
        struct i915_execbuffer eb;
        struct dma_fence *in_fence = NULL;
        struct sync_file *out_fence = NULL;
+       struct drm_syncobj **syncobjs = NULL;
        int out_fence_fd = -1;
+       unsigned int i;
        int err;
 
        BUILD_BUG_ON(__EXEC_OBJECT_INTERNAL_FLAGS &
@@ -2186,9 +2192,30 @@ i915_gem_do_execbuffer(struct drm_device *dev,
                }
        }
 
+       if (args->flags & I915_EXEC_FENCE_ARRAY) {
+               syncobjs = kvmalloc_array(args->num_cliprects,
+                                         sizeof(*syncobjs),
+                                         __GFP_NOWARN | GFP_TEMPORARY |
+                                         __GFP_ZERO);
+               if (syncobjs == NULL) {
+                       DRM_DEBUG("Failed to allocate array of %d syncobjs\n",
+                                 args->num_cliprects);
+                       err = -ENOMEM;

drivers/gpu/drm/i915/i915_gem_execbuffer.c i915_gem_do_execbuffer(struct drm_device *dev, · line 2213

Quoted 10 lines
drivers/gpu/drm/i915/i915_gem_execbuffer.c +10 -0
+                       goto err_out_fence;
+               }
+               for (i = 0; i < args->num_cliprects; i++) {
+                       syncobjs[i] = drm_syncobj_find(file, fences[i].handle);
+                       if (!syncobjs[i]) {
+                               DRM_DEBUG("Invalid syncobj handle provided\n");
+                               err = -EINVAL;
+                               goto err_syncobjs;
+                       }
+               }

I did this in the caller so that we only allocated and passed in a single array of drm_syncobj (with the flags packed into the low bits).

Quoted 34 lines
drivers/gpu/drm/i915/i915_gem_execbuffer.c +17 -1
+       }
+
        err = eb_create(&eb);
        if (err)
-               goto err_out_fence;
+               goto err_syncobjs;
 
        GEM_BUG_ON(!eb.lut_size);
 
@@ -2304,6 +2331,16 @@ i915_gem_do_execbuffer(struct drm_device *dev,
                        goto err_request;
        }
 
+       if (args->flags & I915_EXEC_FENCE_ARRAY) {
+               for (i = 0; i < args->num_cliprects; i++) {
+                       if (fences[i].flags & I915_EXEC_FENCE_WAIT) {
+                               if (i915_gem_request_await_dma_fence(eb.request,
+                                                                    syncobjs[i]->fence))
+                                       goto err_request;
+                       }
+               }
+       }
+
        if (out_fence_fd != -1) {
                out_fence = sync_file_create(&eb.request->fence);
                if (!out_fence) {
@@ -2312,6 +2349,20 @@ i915_gem_do_execbuffer(struct drm_device *dev,
                }
        }
 
+    /* We need to add fences to syncobjs last because doing so mutates the
+     * syncobj and we have no good way to recover if the execbuf fails
+     * after this point.  Fortunately, drm_syncobj_replace_fence can never
+     * fail.

drivers/gpu/drm/i915/i915_gem_execbuffer.c i915_gem_do_execbuffer(struct drm_device *dev, · line 2365

Quoted 10 lines
drivers/gpu/drm/i915/i915_gem_execbuffer.c +10 -0
+     */
+       if (args->flags & I915_EXEC_FENCE_ARRAY) {
+               for (i = 0; i < args->num_cliprects; i++) {
+                       if (fences[i].flags & I915_EXEC_FENCE_SIGNAL) {
+                               drm_syncobj_replace_fence(file, syncobjs[i],
+                                                         &eb.request->fence);
+                       }
+               }
+       }
+

This has to be after the execbuf is submitted, next to where out_fence is attached is a good spot, and should only be updated on success. (The kernel API allows combining of FENCE_WAIT | FENCE_SIGNAL so we can only replace a FENCE_WAIT once we know we have committed the execbuf.)

I moved all of these to little functions: get_fence_array, await_fence_array, signal_fence_array and put_fence_array. -Chris
_______________________________________________
dri-devel mailing list [email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#2 lore raw
Quoted 118 lines · Chris Wilson

Quoting Jason Ekstrand (2017-07-05 18:21:22)

Quoted 17 lines

This commit adds support for waiting on or signaling DRM syncobjs as part of execbuf. It does so by hijacking the currently unused cliprects pointer to instead point to an array of i915_gem_exec_fence structs which containe a DRM syncobj and a flags parameter which specifies whether to wait on it or to signal it. This implementation theoretically allows for both flags to be set in which case it waits on the dma_fence that was in the syncobj and then immediately replaces it with the dma_fence from the current execbuf.

Cc Chris Wilson <[email protected]>

---

Ideally, I'd like to get whatever kernel reviewing and/or merging is required to land the userspace bits ASAP. That said, I understand that this is my first kernel patch and it's liable to have a few rounds of review before going in. :-)

Quoted 6 lines

The mesa branch for using this in Vulkan can be found here:

https://cgit.freedesktop.org/~jekstrand/mesa/log/?h=review/anv-syncobj

 drivers/gpu/drm/i915/i915_drv.c            |  3 +-
 drivers/gpu/drm/i915/i915_gem_execbuffer.c | 97

++++++++++++++++++++++++++++--

Review

Quoted 4 lines
 include/uapi/drm/i915_drm.h                | 30 ++++++++-
 3 files changed, 121 insertions(+), 9 deletions(-)
(quoted diff) +0 -0

b/drivers/gpu/drm/i915/i915_drv.c

drivers/gpu/drm/i915/i915_drv.c static int i915_getparam(struct drm_device *dev,

Quoted 4 lines

index 9167a73..e6f7f49 100644

drivers/gpu/drm/i915/i915_drv.c +0 -0
@@ -384,6 +384,7 @@ static int i915_getparam(struct drm_device *dev,

void *data,

drivers/gpu/drm/i915/i915_drv.c static struct drm_driver driver = { · line 2737

Quoted 11 lines
drivers/gpu/drm/i915/i915_drv.c +1 -0
        case I915_PARAM_HAS_EXEC_FENCE:
        case I915_PARAM_HAS_EXEC_CAPTURE:
        case I915_PARAM_HAS_EXEC_BATCH_FIRST:
+       case I915_PARAM_HAS_EXEC_FENCE_ARRAY:
                /* For the time being all of these are always true;
                 * if some supported hardware does not have one of these
                 * features this value needs to be provided from
@@ -2734,7 +2735,7 @@ static struct drm_driver driver = {
         */
        .driver_features =
            DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED | DRIVER_GEM |

DRIVER_PRIME |

drivers/gpu/drm/i915/i915_drv.c static struct drm_driver driver = { · line 2738

Quoted 2 lines
drivers/gpu/drm/i915/i915_drv.c +1 -1
-           DRIVER_RENDER | DRIVER_MODESET | DRIVER_ATOMIC,
+           DRIVER_RENDER | DRIVER_MODESET | DRIVER_ATOMIC |

DRIVER_SYNCOBJ,

Review

Quoted 4 lines
drivers/gpu/drm/i915/i915_drv.c +0 -0
        .release = i915_driver_release,
        .open = i915_driver_open,
        .lastclose = i915_driver_lastclose,
(quoted diff) +0 -0

b/drivers/gpu/drm/i915/i915_gem_execbuffer.c

drivers/gpu/drm/i915/i915_gem_execbuffer.c static bool i915_gem_check_execbuffer(struct

Quoted 12 lines

index 929f275..81b7b7b 100644

drivers/gpu/drm/i915/i915_gem_execbuffer.c +1 -0
@@ -33,6 +33,7 @@
 
 #include <drm/drmP.h>
 #include <drm/i915_drm.h>
+#include <drm/drm_syncobj.h>
 
 #include "i915_drv.h"
 #include "i915_gem_clflush.h"
@@ -1882,8 +1883,10 @@ static bool i915_gem_check_execbuffer(struct

drm_i915_gem_execbuffer2 *exec)

Quoted 30 lines
drivers/gpu/drm/i915/i915_gem_execbuffer.c +8 -3
                return false;
 
        /* Kernel clipping was a DRI1 misfeature */
-       if (exec->num_cliprects || exec->cliprects_ptr)
-               return false;
+       if (!(exec->flags & I915_EXEC_FENCE_ARRAY)) {
+               if (exec->num_cliprects || exec->cliprects_ptr)
+                       return false;
+       }
 
        if (exec->DR4 == 0xffffffff) {
                DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
@@ -2118,12 +2121,15 @@ static int
 i915_gem_do_execbuffer(struct drm_device *dev,
                       struct drm_file *file,
                       struct drm_i915_gem_execbuffer2 *args,
-                      struct drm_i915_gem_exec_object2 *exec)
+                      struct drm_i915_gem_exec_object2 *exec,
+                      struct drm_i915_gem_exec_fence *fences)
 {
        struct i915_execbuffer eb;
        struct dma_fence *in_fence = NULL;
        struct sync_file *out_fence = NULL;
+       struct drm_syncobj **syncobjs = NULL;
        int out_fence_fd = -1;
+       unsigned int i;
        int err;
 
        BUILD_BUG_ON(__EXEC_OBJECT_INTERNAL_FLAGS &

drivers/gpu/drm/i915/i915_gem_execbuffer.c i915_gem_do_execbuffer(struct drm_device *dev, · line 2201

Quoted 10 lines
drivers/gpu/drm/i915/i915_gem_execbuffer.c +7 -0
@@ -2186,9 +2192,30 @@ i915_gem_do_execbuffer(struct drm_device *dev,
                }
        }
 
+       if (args->flags & I915_EXEC_FENCE_ARRAY) {
+               syncobjs = kvmalloc_array(args->num_cliprects,
+                                         sizeof(*syncobjs),
+                                         __GFP_NOWARN | GFP_TEMPORARY |
+                                         __GFP_ZERO);
+               if (syncobjs == NULL) {
+                       DRM_DEBUG("Failed to allocate array of %d

syncobjs\n",

drivers/gpu/drm/i915/i915_gem_execbuffer.c i915_gem_do_execbuffer(struct drm_device *dev,

Quoted 6 lines
drivers/gpu/drm/i915/i915_gem_execbuffer.c +6 -0
+                                 args->num_cliprects);
+                       err = -ENOMEM;
+                       goto err_out_fence;
+               }
+               for (i = 0; i < args->num_cliprects; i++) {
+                       syncobjs[i] = drm_syncobj_find(file,

fences[i].handle);

drivers/gpu/drm/i915/i915_gem_execbuffer.c i915_gem_do_execbuffer(struct drm_device *dev, · line 2209

Quoted 2 lines
drivers/gpu/drm/i915/i915_gem_execbuffer.c +2 -0
+                       if (!syncobjs[i]) {
+                               DRM_DEBUG("Invalid syncobj handle
Quoted 9 lines

provided\n");

drivers/gpu/drm/i915/i915_gem_execbuffer.c i915_gem_do_execbuffer(struct drm_device *dev, · line 2213

Quoted 4 lines
drivers/gpu/drm/i915/i915_gem_execbuffer.c +4 -0
+                               err = -EINVAL;
+                               goto err_syncobjs;
+                       }
+               }

I did this in the caller so that we only allocated and passed in a single array of drm_syncobj (with the flags packed into the low bits).

Packing things into the low bits seems a bit sketchy but it works so long as we only have the two flag bits.

Quoted 51 lines
Quoted 10 lines
drivers/gpu/drm/i915/i915_gem_execbuffer.c +3 -1
+       }
+
        err = eb_create(&eb);
        if (err)
-               goto err_out_fence;
+               goto err_syncobjs;
 
        GEM_BUG_ON(!eb.lut_size);
 

drivers/gpu/drm/i915/i915_gem_execbuffer.c i915_gem_do_execbuffer(struct drm_device *dev, · line 2337

Quoted 7 lines
drivers/gpu/drm/i915/i915_gem_execbuffer.c +4 -0
@@ -2304,6 +2331,16 @@ i915_gem_do_execbuffer(struct drm_device *dev,
                        goto err_request;
        }
 
+       if (args->flags & I915_EXEC_FENCE_ARRAY) {
+               for (i = 0; i < args->num_cliprects; i++) {
+                       if (fences[i].flags & I915_EXEC_FENCE_WAIT) {
+                               if (i915_gem_request_await_dma_

fence(eb.request,

drivers/gpu/drm/i915/i915_gem_execbuffer.c i915_gem_do_execbuffer(struct drm_device *dev, · line 2338

Quoted 1 line
drivers/gpu/drm/i915/i915_gem_execbuffer.c +1 -0
+

syncobjs[i]->fence))

drivers/gpu/drm/i915/i915_gem_execbuffer.c i915_gem_do_execbuffer(struct drm_device *dev, · line 2352

Quoted 13 lines
drivers/gpu/drm/i915/i915_gem_execbuffer.c +6 -0
+                                       goto err_request;
+                       }
+               }
+       }
+
        if (out_fence_fd != -1) {
                out_fence = sync_file_create(&eb.request->fence);
                if (!out_fence) {
@@ -2312,6 +2349,20 @@ i915_gem_do_execbuffer(struct drm_device *dev,
                }
        }
 
+    /* We need to add fences to syncobjs last because doing so mutates

the

drivers/gpu/drm/i915/i915_gem_execbuffer.c i915_gem_do_execbuffer(struct drm_device *dev, · line 2354

Quoted 2 lines
drivers/gpu/drm/i915/i915_gem_execbuffer.c +2 -0
+     * syncobj and we have no good way to recover if the execbuf fails
+     * after this point.  Fortunately, drm_syncobj_replace_fence can

never

drivers/gpu/drm/i915/i915_gem_execbuffer.c i915_gem_do_execbuffer(struct drm_device *dev,

Quoted 6 lines
drivers/gpu/drm/i915/i915_gem_execbuffer.c +6 -0
+     * fail.
+     */
+       if (args->flags & I915_EXEC_FENCE_ARRAY) {
+               for (i = 0; i < args->num_cliprects; i++) {
+                       if (fences[i].flags & I915_EXEC_FENCE_SIGNAL) {
+                               drm_syncobj_replace_fence(file,

syncobjs[i],

drivers/gpu/drm/i915/i915_gem_execbuffer.c i915_gem_do_execbuffer(struct drm_device *dev, · line 2361

Quoted 1 line
drivers/gpu/drm/i915/i915_gem_execbuffer.c +1 -0
+

&eb.request->fence);

drivers/gpu/drm/i915/i915_gem_execbuffer.c i915_gem_do_execbuffer(struct drm_device *dev, · line 2215

Quoted 4 lines
drivers/gpu/drm/i915/i915_gem_execbuffer.c +4 -0
+                       }
+               }
+       }
+
Quoted 5 lines

This has to be after the execbuf is submitted, next to where out_fence is attached is a good spot, and should only be updated on success. (The kernel API allows combining of FENCE_WAIT | FENCE_SIGNAL so we can only replace a FENCE_WAIT once we know we have committed the execbuf.)

It's currently right after we call

        out_fence = sync_file_create(&eb.request->fence);

which is before eb_submit(). Are sync files wrong? Note that I was careful to ensure that there are no error paths after syncobj_replace_fence so we know for 100% sure that it will be committed, hence the comment.

Quoted 3 lines

I moved all of these to little functions: get_fence_array, await_fence_array, signal_fence_array and put_fence_array.

You clearly have a version of this patch somewhere. Where can I find it?

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#3 lore raw

Quoting Jason Ekstrand (2017-07-05 19:32:21)

Quoted 191 lines · Chris Wilson
    Quoting Jason Ekstrand (2017-07-05 18:21:22)
    > This commit adds support for waiting on or signaling DRM syncobjs as
    > part of execbuf.  It does so by hijacking the currently unused cliprects
    > pointer to instead point to an array of i915_gem_exec_fence structs
    > which containe a DRM syncobj and a flags parameter which specifies
    > whether to wait on it or to signal it.  This implementation
    > theoretically allows for both flags to be set in which case it waits on
    > the dma_fence that was in the syncobj and then immediately replaces it
    > with the dma_fence from the current execbuf.
    >
    > Cc: Chris Wilson <[email protected]>
    > ---
    >
    > Ideally, I'd like to get whatever kernel reviewing and/or merging is
    > required to land the userspace bits ASAP.  That said, I understand that
    > this is my first kernel patch and it's liable to have a few rounds of
    > review before going in.  :-)
    >
    > The mesa branch for using this in Vulkan can be found here:
    >
    > https://cgit.freedesktop.org/~jekstrand/mesa/log/?h=review/anv-syncobj
    >
    >  drivers/gpu/drm/i915/i915_drv.c            |  3 +-
    >  drivers/gpu/drm/i915/i915_gem_execbuffer.c | 97
    ++++++++++++++++++++++++++++--
    >  include/uapi/drm/i915_drm.h                | 30 ++++++++-
    >  3 files changed, 121 insertions(+), 9 deletions(-)
    >
    > diff --git a/drivers/gpu/drm/i915/i915_drv.c b/drivers/gpu/drm/i915/i915_
    drv.c
    > index 9167a73..e6f7f49 100644
    > --- a/drivers/gpu/drm/i915/i915_drv.c
    > +++ b/drivers/gpu/drm/i915/i915_drv.c
    > @@ -384,6 +384,7 @@ static int i915_getparam(struct drm_device *dev, void
    *data,
    >         case I915_PARAM_HAS_EXEC_FENCE:
    >         case I915_PARAM_HAS_EXEC_CAPTURE:
    >         case I915_PARAM_HAS_EXEC_BATCH_FIRST:
    > +       case I915_PARAM_HAS_EXEC_FENCE_ARRAY:
    >                 /* For the time being all of these are always true;
    >                  * if some supported hardware does not have one of these
    >                  * features this value needs to be provided from
    > @@ -2734,7 +2735,7 @@ static struct drm_driver driver = {
    >          */
    >         .driver_features =
    >             DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED | DRIVER_GEM |
    DRIVER_PRIME |
    > -           DRIVER_RENDER | DRIVER_MODESET | DRIVER_ATOMIC,
    > +           DRIVER_RENDER | DRIVER_MODESET | DRIVER_ATOMIC |
    DRIVER_SYNCOBJ,
    >         .release = i915_driver_release,
    >         .open = i915_driver_open,
    >         .lastclose = i915_driver_lastclose,
    > diff --git a/drivers/gpu/drm/i915/i915_gem_execbuffer.c b/drivers/gpu/drm
    /i915/i915_gem_execbuffer.c
    > index 929f275..81b7b7b 100644
    > --- a/drivers/gpu/drm/i915/i915_gem_execbuffer.c
    > +++ b/drivers/gpu/drm/i915/i915_gem_execbuffer.c
    > @@ -33,6 +33,7 @@
    >
    >  #include <drm/drmP.h>
    >  #include <drm/i915_drm.h>
    > +#include <drm/drm_syncobj.h>
    >
    >  #include "i915_drv.h"
    >  #include "i915_gem_clflush.h"
    > @@ -1882,8 +1883,10 @@ static bool i915_gem_check_execbuffer(struct
    drm_i915_gem_execbuffer2 *exec)
    >                 return false;
    >
    >         /* Kernel clipping was a DRI1 misfeature */
    > -       if (exec->num_cliprects || exec->cliprects_ptr)
    > -               return false;
    > +       if (!(exec->flags & I915_EXEC_FENCE_ARRAY)) {
    > +               if (exec->num_cliprects || exec->cliprects_ptr)
    > +                       return false;
    > +       }
    >
    >         if (exec->DR4 == 0xffffffff) {
    >                 DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
    > @@ -2118,12 +2121,15 @@ static int
    >  i915_gem_do_execbuffer(struct drm_device *dev,
    >                        struct drm_file *file,
    >                        struct drm_i915_gem_execbuffer2 *args,
    > -                      struct drm_i915_gem_exec_object2 *exec)
    > +                      struct drm_i915_gem_exec_object2 *exec,
    > +                      struct drm_i915_gem_exec_fence *fences)
    >  {
    >         struct i915_execbuffer eb;
    >         struct dma_fence *in_fence = NULL;
    >         struct sync_file *out_fence = NULL;
    > +       struct drm_syncobj **syncobjs = NULL;
    >         int out_fence_fd = -1;
    > +       unsigned int i;
    >         int err;
    >
    >         BUILD_BUG_ON(__EXEC_OBJECT_INTERNAL_FLAGS &
    > @@ -2186,9 +2192,30 @@ i915_gem_do_execbuffer(struct drm_device *dev,
    >                 }
    >         }
    >
    > +       if (args->flags & I915_EXEC_FENCE_ARRAY) {
    > +               syncobjs = kvmalloc_array(args->num_cliprects,
    > +                                         sizeof(*syncobjs),
    > +                                         __GFP_NOWARN | GFP_TEMPORARY |
    > +                                         __GFP_ZERO);
    > +               if (syncobjs == NULL) {
    > +                       DRM_DEBUG("Failed to allocate array of %d
    syncobjs\n",
    > +                                 args->num_cliprects);
    > +                       err = -ENOMEM;
    > +                       goto err_out_fence;
    > +               }
    > +               for (i = 0; i < args->num_cliprects; i++) {
    > +                       syncobjs[i] = drm_syncobj_find(file, fences
    [i].handle);
    > +                       if (!syncobjs[i]) {
    > +                               DRM_DEBUG("Invalid syncobj handle
    provided\n");
    > +                               err = -EINVAL;
    > +                               goto err_syncobjs;
    > +                       }
    > +               }

    I did this in the caller so that we only allocated and passed in a single
    array of drm_syncobj (with the flags packed into the low bits).

Packing things into the low bits seems a bit sketchy but it works so long as we only have the two flag bits.

Quoted 10 lines

+       }
+
         err = eb_create(&eb);
         if (err)
-               goto err_out_fence;
+               goto err_syncobjs;

         GEM_BUG_ON(!eb.lut_size);

drivers/gpu/drm/i915/i915_gem_execbuffer.c i915_gem_do_execbuffer(struct drm_device *dev, · line 2337

Quoted 7 lines
drivers/gpu/drm/i915/i915_gem_execbuffer.c +4 -0
@@ -2304,6 +2331,16 @@ i915_gem_do_execbuffer(struct drm_device *dev,
                          goto err_request;
          }
 
+       if (args->flags & I915_EXEC_FENCE_ARRAY) {
+               for (i = 0; i < args->num_cliprects; i++) {
+                       if (fences[i].flags & I915_EXEC_FENCE_WAIT) {
+                               if (i915_gem_request_await_dma_fence
    (eb.request,
    > +                                                                   
    syncobjs[i]->fence))
    > +                                       goto err_request;
    > +                       }
    > +               }
    > +       }
    > +
    >         if (out_fence_fd != -1) {
    >                 out_fence = sync_file_create(&eb.request->fence);
    >                 if (!out_fence) {
    > @@ -2312,6 +2349,20 @@ i915_gem_do_execbuffer(struct drm_device *dev,
    >                 }
    >         }
    >
    > +    /* We need to add fences to syncobjs last because doing so mutates
    the
    > +     * syncobj and we have no good way to recover if the execbuf fails
    > +     * after this point.  Fortunately, drm_syncobj_replace_fence can
    never
    > +     * fail.
    > +     */
    > +       if (args->flags & I915_EXEC_FENCE_ARRAY) {
    > +               for (i = 0; i < args->num_cliprects; i++) {
    > +                       if (fences[i].flags & I915_EXEC_FENCE_SIGNAL) {
    > +                               drm_syncobj_replace_fence(file, syncobjs
    [i],
    > +                                                         &eb.request->
    fence);
    > +                       }
    > +               }
    > +       }
    > +

    This has to be after the execbuf is submitted, next to where out_fence
    is attached is a good spot, and should only be updated on success. (The
    kernel API allows combining of FENCE_WAIT | FENCE_SIGNAL so we can only
    replace a FENCE_WAIT once we know we have committed the execbuf.)
Quoted 3 lines

It's currently right after we call

        out_fence = sync_file_create(&eb.request->fence);

That's where we create the out fence, not where we attach it to the request, see the fd_install, which is after we have successfully committed the request.

Quoted 3 lines

which is before eb_submit().  Are sync files wrong?  Note that I was careful to ensure that there are no error paths after syncobj_replace_fence so we know for 100% sure that it will be committed, hence the comment.

There's the rather big one in eb_submit() that will generate EINTR for your pain.

Quoted 5 lines

I moved all of these to little functions: get_fence_array, await_fence_array, signal_fence_array and put_fence_array.

You clearly have a version of this patch somewhere.  Where can I find it?

It's still on the older syncobj api, I am easily distracted. :| -Chris
_______________________________________________
dri-devel mailing list [email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#4 lore raw
Quoted 233 lines · Chris Wilson

Quoting Jason Ekstrand (2017-07-05 19:32:21)

Quoted 1 line

On Wed, Jul 5, 2017 at 10:37 AM, Chris Wilson <[email protected]>

Quoted 3 lines · wrote:

Quoting Jason Ekstrand (2017-07-05 18:21:22)
> This commit adds support for waiting on or signaling DRM syncobjs

as

Quoted 1 line
Quoted 1 line

part of execbuf. It does so by hijacking the currently unused

cliprects

Quoted 4 lines
Quoted 4 lines

pointer to instead point to an array of i915_gem_exec_fence structs which containe a DRM syncobj and a flags parameter which specifies whether to wait on it or to signal it. This implementation theoretically allows for both flags to be set in which case it

waits on

Quoted 1 line
Quoted 1 line

the dma_fence that was in the syncobj and then immediately

replaces it

Quoted 6 lines
Quoted 6 lines

with the dma_fence from the current execbuf.

Cc Chris Wilson <[email protected]>

---

Ideally, I'd like to get whatever kernel reviewing and/or merging

is

Quoted 1 line
Quoted 1 line

required to land the userspace bits ASAP. That said, I understand

that

Quoted 1 line
Quoted 1 line

this is my first kernel patch and it's liable to have a few rounds

of

Quoted 5 lines
Quoted 5 lines

review before going in. :-)

The mesa branch for using this in Vulkan can be found here:

https://cgit.freedesktop.org/~jekstrand/mesa/log/?h=review/

anv-syncobj

Quoted 8 lines
Quoted 3 lines
 drivers/gpu/drm/i915/i915_drv.c            |  3 +-
 drivers/gpu/drm/i915/i915_gem_execbuffer.c | 97
    ++++++++++++++++++++++++++++--
    >  include/uapi/drm/i915_drm.h                | 30 ++++++++-
    >  3 files changed, 121 insertions(+), 9 deletions(-)
    >
    > diff --git a/drivers/gpu/drm/i915/i915_drv.c

b/drivers/gpu/drm/i915/i915_

Quoted 5 lines
    drv.c
    > index 9167a73..e6f7f49 100644
    > --- a/drivers/gpu/drm/i915/i915_drv.c
    > +++ b/drivers/gpu/drm/i915/i915_drv.c
    > @@ -384,6 +384,7 @@ static int i915_getparam(struct drm_device

*dev, void

Quoted 7 lines

*data,
> case I915_PARAM_HAS_EXEC_FENCE:
> case I915_PARAM_HAS_EXEC_CAPTURE:
> case I915_PARAM_HAS_EXEC_BATCH_FIRST:
> + case I915_PARAM_HAS_EXEC_FENCE_ARRAY:
> /* For the time being all of these are always true;
> * if some supported hardware does not have one of

these

Quoted 5 lines

drivers/gpu/drm/i915/i915_drv.c static struct drm_driver driver = { · line 2737

Quoted 5 lines

* features this value needs to be provided from

drivers/gpu/drm/i915/i915_drv.c +0 -0
@@ -2734,7 +2735,7 @@ static struct drm_driver driver = {
         */
        .driver_features =
            DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED | DRIVER_GEM |
Quoted 8 lines

DRIVER_PRIME |
> - DRIVER_RENDER | DRIVER_MODESET | DRIVER_ATOMIC,
> + DRIVER_RENDER | DRIVER_MODESET | DRIVER_ATOMIC |
DRIVER_SYNCOBJ,
> .release = i915_driver_release,
> .open = i915_driver_open,
> .lastclose = i915_driver_lastclose,
> diff --git a/drivers/gpu/drm/i915/i915_gem_execbuffer.c

b/drivers/gpu/drm

Quoted 5 lines
    /i915/i915_gem_execbuffer.c
    > index 929f275..81b7b7b 100644
    > --- a/drivers/gpu/drm/i915/i915_gem_execbuffer.c
    > +++ b/drivers/gpu/drm/i915/i915_gem_execbuffer.c
    > @@ -33,6 +33,7 @@
Quoted 8 lines
    >
    >  #include <drm/drmP.h>
    >  #include <drm/i915_drm.h>
    > +#include <drm/drm_syncobj.h>
    >
    >  #include "i915_drv.h"
    >  #include "i915_gem_clflush.h"
    > @@ -1882,8 +1883,10 @@ static bool i915_gem_check_execbuffer(

struct

Quoted 5 lines
    drm_i915_gem_execbuffer2 *exec)
    >                 return false;
    >
    >         /* Kernel clipping was a DRI1 misfeature */
    > -       if (exec->num_cliprects || exec->cliprects_ptr)
Quoted 8 lines
    > -               return false;
    > +       if (!(exec->flags & I915_EXEC_FENCE_ARRAY)) {
    > +               if (exec->num_cliprects || exec->cliprects_ptr)
    > +                       return false;
    > +       }
    >
    >         if (exec->DR4 == 0xffffffff) {
    >                 DRM_DEBUG("UXA submitting garbage DR4, fixing

up\n");

Quoted 16 lines
Quoted 16 lines
drivers/gpu/drm/i915/i915_gem_execbuffer.c +4 -1
@@ -2118,12 +2121,15 @@ static int
 i915_gem_do_execbuffer(struct drm_device *dev,
                       struct drm_file *file,
                       struct drm_i915_gem_execbuffer2 *args,
-                      struct drm_i915_gem_exec_object2 *exec)
+                      struct drm_i915_gem_exec_object2 *exec,
+                      struct drm_i915_gem_exec_fence *fences)
 {
        struct i915_execbuffer eb;
        struct dma_fence *in_fence = NULL;
        struct sync_file *out_fence = NULL;
+       struct drm_syncobj **syncobjs = NULL;
        int out_fence_fd = -1;
+       unsigned int i;
        int err;
 
Quoted 2 lines

drivers/gpu/drm/i915/i915_gem_execbuffer.c i915_gem_do_execbuffer(struct drm_device

Quoted 2 lines
drivers/gpu/drm/i915/i915_gem_execbuffer.c +0 -0
        BUILD_BUG_ON(__EXEC_OBJECT_INTERNAL_FLAGS &
@@ -2186,9 +2192,30 @@ i915_gem_do_execbuffer(struct drm_device

*dev,

Quoted 7 lines

drivers/gpu/drm/i915/i915_gem_execbuffer.c i915_gem_do_execbuffer(struct drm_device *dev,

Quoted 7 lines
drivers/gpu/drm/i915/i915_gem_execbuffer.c +4 -0
                }
        }
 
+       if (args->flags & I915_EXEC_FENCE_ARRAY) {
+               syncobjs = kvmalloc_array(args->num_cliprects,
+                                         sizeof(*syncobjs),
+                                         __GFP_NOWARN |

GFP_TEMPORARY |

Quoted 12 lines

drivers/gpu/drm/i915/i915_gem_execbuffer.c i915_gem_do_execbuffer(struct drm_device *dev,

Quoted 3 lines
drivers/gpu/drm/i915/i915_gem_execbuffer.c +3 -0
+                                         __GFP_ZERO);
+               if (syncobjs == NULL) {
+                       DRM_DEBUG("Failed to allocate array of %d
    syncobjs\n",
    > +                                 args->num_cliprects);
    > +                       err = -ENOMEM;
    > +                       goto err_out_fence;
    > +               }
    > +               for (i = 0; i < args->num_cliprects; i++) {
    > +                       syncobjs[i] = drm_syncobj_find(file, fences
    [i].handle);
    > +                       if (!syncobjs[i]) {
Quoted 8 lines
    > +                               DRM_DEBUG("Invalid syncobj handle
    provided\n");
    > +                               err = -EINVAL;
    > +                               goto err_syncobjs;
    > +                       }
    > +               }

    I did this in the caller so that we only allocated and passed in a

single

Quoted 4 lines

array of drm_syncobj (with the flags packed into the low bits).

Packing things into the low bits seems a bit sketchy but it works so

long as we

Quoted 10 lines

only have the two flag bits.

Quoted 7 lines
drivers/gpu/drm/i915/i915_gem_execbuffer.c +3 -1
+       }
+
        err = eb_create(&eb);
        if (err)
-               goto err_out_fence;
+               goto err_syncobjs;
 
Quoted 3 lines

drivers/gpu/drm/i915/i915_gem_execbuffer.c i915_gem_do_execbuffer(struct drm_device

Quoted 3 lines
drivers/gpu/drm/i915/i915_gem_execbuffer.c +0 -0
        GEM_BUG_ON(!eb.lut_size);
 
@@ -2304,6 +2331,16 @@ i915_gem_do_execbuffer(struct drm_device

*dev,

Quoted 6 lines

drivers/gpu/drm/i915/i915_gem_execbuffer.c i915_gem_do_execbuffer(struct drm_device *dev,

Quoted 6 lines
drivers/gpu/drm/i915/i915_gem_execbuffer.c +3 -0
                        goto err_request;
        }
 
+       if (args->flags & I915_EXEC_FENCE_ARRAY) {
+               for (i = 0; i < args->num_cliprects; i++) {
+                       if (fences[i].flags &

I915_EXEC_FENCE_WAIT) {

Quoted 1 line

Review i915_gem_do_execbuffer(struct drm_device · line 2337

Quoted 1 line
(quoted diff) +1 -0
+                               if (i915_gem_request_await_dma_

fence

Quoted 5 lines
    (eb.request,
    > +

    syncobjs[i]->fence))
    > +                                       goto err_request;
Quoted 8 lines
    > +                       }
    > +               }
    > +       }
    > +
    >         if (out_fence_fd != -1) {
    >                 out_fence = sync_file_create(&eb.request->fence);
    >                 if (!out_fence) {
    > @@ -2312,6 +2349,20 @@ i915_gem_do_execbuffer(struct drm_device

*dev,

Quoted 4 lines

Review i915_gem_do_execbuffer(struct drm_device · line 2341

Quoted 4 lines
(quoted diff) +1 -0
                }
        }
 
+    /* We need to add fences to syncobjs last because doing so

mutates

Quoted 2 lines

the
> + * syncobj and we have no good way to recover if the execbuf

fails

Quoted 1 line

Review i915_gem_do_execbuffer(struct drm_device · line 2342

Quoted 1 line
(quoted diff) +1 -0
+     * after this point.  Fortunately, drm_syncobj_replace_fence

can

Quoted 6 lines
    never
    > +     * fail.
    > +     */
    > +       if (args->flags & I915_EXEC_FENCE_ARRAY) {
    > +               for (i = 0; i < args->num_cliprects; i++) {
    > +                       if (fences[i].flags &

I915_EXEC_FENCE_SIGNAL) {

Quoted 1 line

Review i915_gem_do_execbuffer(struct drm_device · line 2343

Quoted 1 line
(quoted diff) +1 -0
+                               drm_syncobj_replace_fence(file,

syncobjs

Quoted 2 lines
    [i],
    > +

&eb.request->

Quoted 7 lines
    fence);
    > +                       }
    > +               }
    > +       }
    > +

    This has to be after the execbuf is submitted, next to where

out_fence

Quoted 1 line

is attached is a good spot, and should only be updated on success.

(The

Quoted 1 line

kernel API allows combining of FENCE_WAIT | FENCE_SIGNAL so we can

only

Quoted 6 lines

replace a FENCE_WAIT once we know we have committed the execbuf.)

It's currently right after we call

        out_fence = sync_file_create(&eb.request->fence);

That's where we create the out fence, not where we attach it to the request, see the fd_install, which is after we have successfully committed the request.

Quoted 9 lines
Quoted 1 line

which is before eb_submit(). Are sync files wrong? Note that I was

careful to

Quoted 1 line

ensure that there are no error paths after syncobj_replace_fence so we

know for

Quoted 1 line

100% sure that it will be committed, hence the comment.

There's the rather big one in eb_submit() that will generate EINTR for your pain.

Oh. :( I see it now. I'll move to right around fd_install()

Quoted 8 lines
Quoted 5 lines

I moved all of these to little functions: get_fence_array, await_fence_array, signal_fence_array and put_fence_array.

You clearly have a version of this patch somewhere. Where can I find it?

It's still on the older syncobj api, I am easily distracted. :|

That's ok. If you could just point me at it, I'm happy to either pull the good ideas into mine or rebase it, whichever is easier.

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#5 lore raw

PATCH 1/1 i915: Add support for drm syncobjs

In reply to Jason Ekstrand

This commit adds support for waiting on or signaling DRM syncobjs as part of execbuf. It does so by hijacking the currently unused cliprects pointer to instead point to an array of i915_gem_exec_fence structs which containe a DRM syncobj and a flags parameter which specifies whether to wait on it or to signal it. This implementation theoretically allows for both flags to be set in which case it waits on the dma_fence that was in the syncobj and then immediately replaces it with the dma_fence from the current execbuf.

v2:
- Rebase on new syncobj API
v3:
- Pull everything out into helpers
- Do all allocation in gem_execbuffer2
- Pack the flags in the bottom 2 bits of the drm_syncobj*

---
 drivers/gpu/drm/i915/i915_drv.c            |   3 +-
 drivers/gpu/drm/i915/i915_gem_execbuffer.c | 141 ++++++++++++++++++++++++++++-
 include/uapi/drm/i915_drm.h                |  30 +++++-
 3 files changed, 166 insertions(+), 8 deletions(-)
drivers/gpu/drm/i915/i915_drv.c +2 -1
index 9167a73..e6f7f49 100644
@@ -384,6 +384,7 @@ static int i915_getparam(struct drm_device *dev, void *data,
 	case I915_PARAM_HAS_EXEC_FENCE:
 	case I915_PARAM_HAS_EXEC_CAPTURE:
 	case I915_PARAM_HAS_EXEC_BATCH_FIRST:
+	case I915_PARAM_HAS_EXEC_FENCE_ARRAY:
 		/* For the time being all of these are always true;
 		 * if some supported hardware does not have one of these
 		 * features this value needs to be provided from
@@ -2734,7 +2735,7 @@ static struct drm_driver driver = {
 	 */
 	.driver_features =
 	    DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED | DRIVER_GEM | DRIVER_PRIME |
-	    DRIVER_RENDER | DRIVER_MODESET | DRIVER_ATOMIC,
+	    DRIVER_RENDER | DRIVER_MODESET | DRIVER_ATOMIC | DRIVER_SYNCOBJ,
 	.release = i915_driver_release,
 	.open = i915_driver_open,
 	.lastclose = i915_driver_lastclose,
drivers/gpu/drm/i915/i915_gem_execbuffer.c +136 -5

198 lines not on this page · raw message

include/uapi/drm/i915_drm.h +28 -2
index 7ccbd6a..5b5f033 100644
@@ -431,6 +431,11 @@ typedef struct drm_i915_irq_wait {
  */
 #define I915_PARAM_HAS_EXEC_BATCH_FIRST	 48
 
+/* Query whether DRM_I915_GEM_EXECBUFFER2 supports supplying an array of
+ * drm_i915_gem_exec_fence structures.  See I915_EXEC_FENCE_ARRAY.
+ */
+#define I915_PARAM_HAS_EXEC_FENCE_ARRAY  49
+
 typedef struct drm_i915_getparam {
 	__s32 param;
 	/*
@@ -812,6 +817,17 @@ struct drm_i915_gem_exec_object2 {
 	__u64 rsvd2;
 };
 
+struct drm_i915_gem_exec_fence {
+	/**
+	 * User's handle for a dma-fence to wait on or signal.
+	 */
+	__u32 handle;
+
+#define I915_EXEC_FENCE_WAIT            (1<<0)
+#define I915_EXEC_FENCE_SIGNAL          (1<<1)
+	__u32 flags;
+};
+
 struct drm_i915_gem_execbuffer2 {
 	/**
 	 * List of gem_exec_object2 structs
@@ -826,7 +842,10 @@ struct drm_i915_gem_execbuffer2 {
 	__u32 DR1;
 	__u32 DR4;
 	__u32 num_cliprects;
-	/** This is a struct drm_clip_rect *cliprects */
+	/** This is a struct drm_clip_rect *cliprects if I915_EXEC_FENCE_ARRAY
+     * is not set.  If I915_EXEC_FENCE_ARRAY is set, then this is a
+     * struct drm_i915_gem_exec_fence *fences.
+     */
 	__u64 cliprects_ptr;
 #define I915_EXEC_RING_MASK              (7<<0)
 #define I915_EXEC_DEFAULT                (0<<0)
@@ -927,7 +946,14 @@ struct drm_i915_gem_execbuffer2 {
  * element).
  */
 #define I915_EXEC_BATCH_FIRST		(1<<18)
-#define __I915_EXEC_UNKNOWN_FLAGS (-(I915_EXEC_BATCH_FIRST<<1))
+
+/* Setting I915_FENCE_ARRAY implies that num_cliprects and cliprects_ptr
+ * define an array of i915_gem_exec_fence structures which specify a set of
+ * dma fences to wait upon or signal.
+ */
+#define I915_EXEC_FENCE_ARRAY   (1<<19)
+
+#define __I915_EXEC_UNKNOWN_FLAGS (-(I915_EXEC_FENCE_ARRAY<<1))
 
 #define I915_EXEC_CONTEXT_ID_MASK	(0xffffffff)
 #define i915_execbuffer2_set_context_id(eb2, context) \
Signature · 6 lines
2.5.0.400.gff86faf

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#6 lore raw
Quoted 154 lines · Jason Ekstrand

This commit adds support for waiting on or signaling DRM syncobjs as part of execbuf. It does so by hijacking the currently unused cliprects pointer to instead point to an array of i915_gem_exec_fence structs which containe a DRM syncobj and a flags parameter which specifies whether to wait on it or to signal it. This implementation theoretically allows for both flags to be set in which case it waits on the dma_fence that was in the syncobj and then immediately replaces it with the dma_fence from the current execbuf.

v2:
- Rebase on new syncobj API
v3:
- Pull everything out into helpers
- Do all allocation in gem_execbuffer2
- Pack the flags in the bottom 2 bits of the drm_syncobj*

---
 drivers/gpu/drm/i915/i915_drv.c            |   3 +-
 drivers/gpu/drm/i915/i915_gem_execbuffer.c | 141
++++++++++++++++++++++++++++-
 include/uapi/drm/i915_drm.h                |  30 +++++-
 3 files changed, 166 insertions(+), 8 deletions(-)
drivers/gpu/drm/i915/i915_ renamed from drivers/gpu/drm/i915/i915_drv.c +0 -0

drv.c
index 9167a73..e6f7f49 100644

drivers/gpu/drm/i915/i915_drv.c +1 -0
@@ -384,6 +384,7 @@ static int i915_getparam(struct drm_device *dev, void
 *data,
        case I915_PARAM_HAS_EXEC_FENCE:
        case I915_PARAM_HAS_EXEC_CAPTURE:
        case I915_PARAM_HAS_EXEC_BATCH_FIRST:
+       case I915_PARAM_HAS_EXEC_FENCE_ARRAY:
                /* For the time being all of these are always true;
                 * if some supported hardware does not have one of these

* features this value needs to be provided from

drivers/gpu/drm/i915/i915_drv.c +0 -0
@@ -2734,7 +2735,7 @@ static struct drm_driver driver = {
         */
        .driver_features =
            DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED | DRIVER_GEM |

DRIVER_PRIME |

drivers/gpu/drm/i915/i915_drv.c +1 -1
-           DRIVER_RENDER | DRIVER_MODESET | DRIVER_ATOMIC,
+           DRIVER_RENDER | DRIVER_MODESET | DRIVER_ATOMIC |

DRIVER_SYNCOBJ,

        .release = i915_driver_release,
        .open = i915_driver_open,
        .lastclose = i915_driver_lastclose,
drivers/gpu/drm/i915/i915_gem_execbuffer.c +5 -2
index 929f275..4f97649 100644
@@ -33,6 +33,7 @@
 
 #include <drm/drmP.h>
 #include <drm/i915_drm.h>
+#include <drm/drm_syncobj.h>
 
 #include "i915_drv.h"
 #include "i915_gem_clflush.h"
@@ -1882,8 +1883,10 @@ static bool i915_gem_check_execbuffer(struct
 drm_i915_gem_execbuffer2 *exec)
                return false;
 
        /* Kernel clipping was a DRI1 misfeature */
-       if (exec->num_cliprects || exec->cliprects_ptr)
-               return false;
+       if (!(exec->flags & I915_EXEC_FENCE_ARRAY)) {
+               if (exec->num_cliprects || exec->cliprects_ptr)
+                       return false;
+       }
 
        if (exec->DR4 == 0xffffffff) {
                DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
drivers/gpu/drm/i915/i915_gem_execbuffer.c +73 -0
@@ -2114,11 +2117,120 @@ eb_select_engine(struct drm_i915_private
 *dev_priv,
        return engine;
 }
 
+static void __free_fence_array(struct drm_syncobj **fences, unsigned int
 n)
+{
+       while (n--)
+               drm_syncobj_put(ptr_mask_bits(fences[n], 2));
+       kvfree(fences);
+}
+
+static struct drm_syncobj **get_fence_array(struct
 drm_i915_gem_execbuffer2 *args,
+                                           struct drm_file *file)
+{
+       const unsigned int nfences = args->num_cliprects;
+       struct drm_i915_gem_exec_fence __user *user;
+       struct drm_syncobj **fences;
+       unsigned int n;
+       int err;
+
+       if (!(args->flags & I915_EXEC_FENCE_ARRAY))
+               return NULL;
+
+       if (nfences > SIZE_MAX / sizeof(*fences))
+               return ERR_PTR(-EINVAL);
+
+       user = u64_to_user_ptr(args->cliprects_ptr);
+       if (!access_ok(VERIFY_READ, user, nfences * 2 * sizeof(u32)))
+               return ERR_PTR(-EFAULT);
+
+       fences = kvmalloc_array(args->num_cliprects, sizeof(*fences),
+                               __GFP_NOWARN | GFP_TEMPORARY);
+       if (!fences)
+               return ERR_PTR(-ENOMEM);
+
+       for (n = 0; n < nfences; n++) {
+               struct drm_i915_gem_exec_fence fence;
+               struct drm_syncobj *syncobj;
+
+               if (__copy_from_user(&fence, user++, sizeof(fence))) {
+                       err = -EFAULT;
+                       goto err;
+               }
+
+               syncobj = drm_syncobj_find(file, fence.handle);
+               if (!syncobj) {
+                       DRM_DEBUG("Invalid syncobj handle provided\n");
+                       err = -EINVAL;
+                       goto err;
+               }
+
+               fences[n] = ptr_pack_bits(syncobj, fence.flags, 2);
+       }
+
+       return fences;
+
+err:
+       __free_fence_array(fences, n);
+       return ERR_PTR(err);
+}
+
+static void put_fence_array(struct drm_i915_gem_execbuffer2 *args,
+                           struct drm_syncobj **fences)
+{
+       if (!fences)
+               return;
+       __free_fence_array(fences, args->num_cliprects);
+}
+
+static int await_fence_array(struct i915_execbuffer *eb,
+                            struct drm_syncobj **fences)
+{
+       const unsigned int nfences = eb->args->num_cliprects;
+       unsigned int n;
+       int err;
+
+       for (n = 0; n < nfences; n++) {

drivers/gpu/drm/i915/i915_gem_execbuffer.c eb_select_engine(struct drm_i915_private · line 2205

Quoted 10 lines
drivers/gpu/drm/i915/i915_gem_execbuffer.c +9 -0
+               struct drm_syncobj *syncobj;
+               unsigned int flags;
+
+               syncobj = ptr_unpack_bits(fences[n], &flags, 2);
+               if (!(flags & I915_EXEC_FENCE_WAIT))
+                       continue;
+
+               err = i915_gem_request_await_dma_fence(eb->request,
+                                                      syncobj->fence);
 

Is there a race here? What happens if some other process replaces the fence between the syncobj->fence lookup and gem_request_await_dma_fence taking its reference?

Quoted 170 lines
drivers/gpu/drm/i915/i915_gem_execbuffer.c +38 -2
+               if (err < 0)
+                       return err;
+       }
+
+       return 0;
+}
+
+static void signal_fence_array(struct i915_execbuffer *eb,
+                              struct drm_syncobj **fences)
+{
+       const unsigned int nfences = eb->args->num_cliprects;
+       struct dma_fence * const fence = &eb->request->fence;
+       unsigned int n;
+
+       for (n = 0; n < nfences; n++) {
+               struct drm_syncobj *syncobj;
+               unsigned int flags;
+
+               syncobj = ptr_unpack_bits(fences[n], &flags, 2);
+               if (!(flags & I915_EXEC_FENCE_SIGNAL))
+                       continue;
+
+               drm_syncobj_replace_fence(NULL, syncobj, fence);
+       }
+}
+
 static int
 i915_gem_do_execbuffer(struct drm_device *dev,
                       struct drm_file *file,
                       struct drm_i915_gem_execbuffer2 *args,
-                      struct drm_i915_gem_exec_object2 *exec)
+                      struct drm_i915_gem_exec_object2 *exec,
+                      struct drm_syncobj **fences)
 {
        struct i915_execbuffer eb;
        struct dma_fence *in_fence = NULL;
@@ -2304,6 +2416,12 @@ i915_gem_do_execbuffer(struct drm_device *dev,
                        goto err_request;
        }
 
+       if (fences) {
+               err = await_fence_array(&eb, fences);
+               if (err < 0)
+                       goto err_request;
+       }
+
        if (out_fence_fd != -1) {
                out_fence = sync_file_create(&eb.request->fence);
                if (!out_fence) {
@@ -2327,6 +2445,9 @@ i915_gem_do_execbuffer(struct drm_device *dev,
        __i915_add_request(eb.request, err == 0);
        add_to_client(eb.request, file);
 
+       if (fences)
+               signal_fence_array(&eb, fences);
+
        if (out_fence) {
                if (err == 0) {
                        fd_install(out_fence_fd, out_fence->file);
@@ -2428,7 +2549,7 @@ i915_gem_execbuffer(struct drm_device *dev, void
 *data,
                        exec2_list[i].flags = 0;
        }
 
-       err = i915_gem_do_execbuffer(dev, file, &exec2, exec2_list);
+       err = i915_gem_do_execbuffer(dev, file, &exec2, exec2_list, NULL);
        if (exec2.flags & __EXEC_HAS_RELOC) {
                struct drm_i915_gem_exec_object __user *user_exec_list =
                        u64_to_user_ptr(args->buffers_ptr);
drivers/gpu/drm/i915/i915_gem_execbuffer.c +1 -0
@@ -2460,6 +2581,7 @@ i915_gem_execbuffer2(struct drm_device *dev, void
 *data,
        const size_t sz = sizeof(struct drm_i915_gem_exec_object2);
        struct drm_i915_gem_execbuffer2 *args = data;
        struct drm_i915_gem_exec_object2 *exec2_list;
+       struct drm_syncobj **fences = NULL;
        int err;
 
        if (args->buffer_count < 1 || args->buffer_count > SIZE_MAX / sz -

1) {

drivers/gpu/drm/i915/i915_gem_execbuffer.c +9 -1
@@ -2486,7 +2608,15 @@ i915_gem_execbuffer2(struct drm_device *dev, void
 *data,
                return -EFAULT;
        }
 
-       err = i915_gem_do_execbuffer(dev, file, args, exec2_list);
+       if (args->flags & I915_EXEC_FENCE_ARRAY) {
+               fences = get_fence_array(args, file);
+               if (IS_ERR(fences)) {
+                       kvfree(exec2_list);
+                       return PTR_ERR(fences);
+               }
+       }
+
+       err = i915_gem_do_execbuffer(dev, file, args, exec2_list, fences);
 
        /*

* Now that we have begun execution of the batchbuffer, we ignore

drivers/gpu/drm/i915/i915_gem_execbuffer.c +1 -0
@@ -2517,5 +2647,6 @@ i915_gem_execbuffer2(struct drm_device *dev, void
 *data,
 
        args->flags &= ~__I915_EXEC_UNKNOWN_FLAGS;
        kvfree(exec2_list);
+       put_fence_array(args, fences);
        return err;

}

include/uapi/drm/i915_drm.h +17 -1
index 7ccbd6a..5b5f033 100644
@@ -431,6 +431,11 @@ typedef struct drm_i915_irq_wait {
  */
 #define I915_PARAM_HAS_EXEC_BATCH_FIRST         48
 
+/* Query whether DRM_I915_GEM_EXECBUFFER2 supports supplying an array of
+ * drm_i915_gem_exec_fence structures.  See I915_EXEC_FENCE_ARRAY.
+ */
+#define I915_PARAM_HAS_EXEC_FENCE_ARRAY  49
+
 typedef struct drm_i915_getparam {
        __s32 param;
        /*
@@ -812,6 +817,17 @@ struct drm_i915_gem_exec_object2 {
        __u64 rsvd2;
 };
 
+struct drm_i915_gem_exec_fence {
+       /**
+        * User's handle for a dma-fence to wait on or signal.
+        */
+       __u32 handle;
+
+#define I915_EXEC_FENCE_WAIT            (1<<0)
+#define I915_EXEC_FENCE_SIGNAL          (1<<1)
+       __u32 flags;
+};
+
 struct drm_i915_gem_execbuffer2 {
        /**
         * List of gem_exec_object2 structs
@@ -826,7 +842,10 @@ struct drm_i915_gem_execbuffer2 {
        __u32 DR1;
        __u32 DR4;
        __u32 num_cliprects;
-       /** This is a struct drm_clip_rect *cliprects */
+       /** This is a struct drm_clip_rect *cliprects if

I915_EXEC_FENCE_ARRAY

include/uapi/drm/i915_drm.h +6 -1
+     * is not set.  If I915_EXEC_FENCE_ARRAY is set, then this is a
+     * struct drm_i915_gem_exec_fence *fences.
+     */
        __u64 cliprects_ptr;
 #define I915_EXEC_RING_MASK              (7<<0)
 #define I915_EXEC_DEFAULT                (0<<0)
@@ -927,7 +946,14 @@ struct drm_i915_gem_execbuffer2 {
  * element).
  */
 #define I915_EXEC_BATCH_FIRST          (1<<18)
-#define __I915_EXEC_UNKNOWN_FLAGS (-(I915_EXEC_BATCH_FIRST<<1))
+
+/* Setting I915_FENCE_ARRAY implies that num_cliprects and cliprects_ptr
+ * define an array of i915_gem_exec_fence structures which specify a set

of
+ * dma fences to wait upon or signal.
+ */

include/uapi/drm/i915_drm.h +3 -1
+#define I915_EXEC_FENCE_ARRAY   (1<<19)
+
+#define __I915_EXEC_UNKNOWN_FLAGS (-(I915_EXEC_FENCE_ARRAY<<1))
 
 #define I915_EXEC_CONTEXT_ID_MASK      (0xffffffff)
 #define i915_execbuffer2_set_context_id(eb2, context) \
--
Quoted 3 lines

2.5.0.400.gff86faf

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#7 lore raw

Quoting Jason Ekstrand (2017-07-05 22:15:09)

Quoted 11 lines · Jason Ekstrand

This commit adds support for waiting on or signaling DRM syncobjs as part of execbuf.  It does so by hijacking the currently unused cliprects pointer to instead point to an array of i915_gem_exec_fence structs which containe a DRM syncobj and a flags parameter which specifies whether to wait on it or to signal it.  This implementation theoretically allows for both flags to be set in which case it waits on the dma_fence that was in the syncobj and then immediately replaces it with the dma_fence from the current execbuf.

Quoted 6 lines

v2:
 - Rebase on new syncobj API
v3:
 - Pull everything out into helpers
 - Do all allocation in gem_execbuffer2
 - Pack the flags in the bottom 2 bits of the drm_syncobj*

Just noticed, no sign off. Could you please check
https://developercertificate.org/ and apply your Signed-off-by, just a reply will do.

Quoted 19 lines
    +static int await_fence_array(struct i915_execbuffer *eb,
    +                            struct drm_syncobj **fences)
    +{
    +       const unsigned int nfences = eb->args->num_cliprects;
    +       unsigned int n;
    +       int err;
    +
    +       for (n = 0; n < nfences; n++) {
    +               struct drm_syncobj *syncobj;
    +               unsigned int flags;
    +
    +               syncobj = ptr_unpack_bits(fences[n], &flags, 2);
    +               if (!(flags & I915_EXEC_FENCE_WAIT))
    +                       continue;
    +
    +               err = i915_gem_request_await_dma_fence(eb->request,
    +                                                      syncobj->fence);
Quoted 3 lines

Is there a race here?  What happens if some other process replaces the fence between the syncobj->fence lookup and gem_request_await_dma_fence taking its reference?

Yes. It's inherently racy be via from objects, dmabuf or explicit fences. We obtain a snapshot of fences, and the only condition we must impose is that they are not cyclic - i.e. we must complete the snapshot of all fences before exposing our new &request->fence to the world.

As to the race where the fence pointed to may change whilst we inspect it, there is nothing we can do to prevent that nor define what the right behaviour is. It is up to userspace to apply its own execution barriers if it requires strict control over the ordering of fences, i.e. what does if mean if client B changed the fence whilst client A was executing? Should client A use the original fence or the new fence? If it matters, the two clients need to coordinate usage of their shared fence. -Chris
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#8 lore raw
Quoted 23 lines · Chris Wilson

Quoting Jason Ekstrand (2017-07-05 22:15:09)

Quoted 1 line

On Wed, Jul 5, 2017 at 2:13 PM, Jason Ekstrand <[email protected]>

Quoted 3 lines · wrote:

This commit adds support for waiting on or signaling DRM syncobjs as part of execbuf. It does so by hijacking the currently unused

cliprects

Quoted 4 lines

pointer to instead point to an array of i915_gem_exec_fence structs which containe a DRM syncobj and a flags parameter which specifies whether to wait on it or to signal it. This implementation theoretically allows for both flags to be set in which case it waits

on

Quoted 1 line

the dma_fence that was in the syncobj and then immediately replaces

it

Quoted 8 lines

with the dma_fence from the current execbuf.

v2:
- Rebase on new syncobj API
v3:
- Pull everything out into helpers
- Do all allocation in gem_execbuffer2
- Pack the flags in the bottom 2 bits of the drm_syncobj*

Quoted 4 lines

Just noticed, no sign off. Could you please check
https://developercertificate.org/ and apply your Signed-off-by, just a reply will do.

Sorry, not familiar with the kernel...

Quoted 31 lines
Quoted 9 lines

+static int await_fence_array(struct i915_execbuffer *eb,
+ struct drm_syncobj **fences)
+{
+ const unsigned int nfences = eb->args->num_cliprects;
+ unsigned int n;
+ int err;
+
+ for (n = 0; n < nfences; n++) {
+ struct drm_syncobj *syncobj;

Quoted 8 lines

+ unsigned int flags;
+
+ syncobj = ptr_unpack_bits(fences[n], &flags, 2);
+ if (!(flags & I915_EXEC_FENCE_WAIT))
+ continue;
+
+ err = i915_gem_request_await_dma_fence(eb->request,
+

syncobj->fence);

Quoted 3 lines

Is there a race here? What happens if some other process replaces the

fence

Quoted 1 line

between the syncobj->fence lookup and gem_request_await_dma_fence taking

its

Quoted 1 line

reference?

Yes. It's inherently racy be via from objects, dmabuf or explicit fences. We obtain a snapshot of fences, and the only condition we must impose is that they are not cyclic - i.e. we must complete the snapshot of all fences before exposing our new &request->fence to the world.

Quoted 8 lines

As to the race where the fence pointed to may change whilst we inspect it, there is nothing we can do to prevent that nor define what the right behaviour is. It is up to userspace to apply its own execution barriers if it requires strict control over the ordering of fences, i.e. what does if mean if client B changed the fence whilst client A was executing? Should client A use the original fence or the new fence? If it matters, the two clients need to coordinate usage of their shared fence.

I'm not concerned about what happens to racy clients. They get what they get. What concerns me is what happens if somehow the fence is replaced and deleted before i915_gem_request_await_dma_fence takes it's reference. Can this cause the kernel to segfault?

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#9 lore raw

Quoting Jason Ekstrand (2017-08-03 19:06:02)

Quoted 4 lines

I'm not concerned about what happens to racy clients.  They get what they get. What concerns me is what happens if somehow the fence is replaced and deleted before i915_gem_request_await_dma_fence takes it's reference.  Can this cause the kernel to segfault?

Gotcha, yup nothing prevents that.

	fence = dma_fence_get_rcu_safe(&syncobj->fence);
	if (!fence)
		return -EINVAL;

	err = await_fence();
	dma_fence_put(fence);
	if (err < 0)
		return;

Happy?
-Chris
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#10 lore raw
Quoted 20 lines · Chris Wilson

Quoting Jason Ekstrand (2017-08-03 19:06:02)

Quoted 1 line

I'm not concerned about what happens to racy clients. They get what

they get.

Quoted 1 line

What concerns me is what happens if somehow the fence is replaced and

deleted

Quoted 1 line

before i915_gem_request_await_dma_fence takes it's reference. Can this

cause

Quoted 1 line

the kernel to segfault?

Gotcha, yup nothing prevents that.

        fence = dma_fence_get_rcu_safe(&syncobj->fence);
        if (!fence)
                return -EINVAL;

        err = await_fence();
        dma_fence_put(fence);
        if (err < 0)
                return;
Quoted 2 lines

Happy?

Assuming dma_fence_get_rcu_safe does what I think it does, then yes.

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel