Skip to contentSkip to conversation

drm/vc4: Clean-up the BO seqnos

PATCH Superseded Maíra Canal 4 participants 20 messages Dec 12, 2024 – Dec 18, 2024

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Maíra Canal

author reviewer
#0 lore raw

v2 cover drm/vc4: Clean-up the BO seqnos

This series introduces a series of clean-ups for BO reservations in the VC4 driver. Currently, VC4 uses shared fences to track BO reservations, similar to many other DRM devices. However, in addition to DMA reservation objects, VC4 has been maintaining a separate seqno tracking system for BOs to track job completion.

The seqno tracking system was implemented before DMA reservation objects and was left in the code after DMA reservation's introduction. This approach is now redundant, as job completion can be effectively tracked using fences. Consequently, this series focuses on eliminating seqnos from the BO implementation.

Patch Breakdown
===============

* Patch #1: Uses the DRM Exec to replace the open-coded implementation of the functions drm_gem_lock_reservations() and drm_gem_unlock_reservations() in the VC4 driver code. A straightforward change with no functional changes.

* Patch #2: Implements the VC4 wait BO IOCTL using DMA reservations. The new implementation closely mirrors the V3D approach and removes the IOCTL's dependency on BO sequence numbers.

* Patch #3: The central piece of this patchset. It removes bo->seqno, bo->write_seqno, and exec->bin_dep_seqno from the driver. As the seqno tracking system is redundant, its deletion is relatively straightforward. The only notable change is vc4_async_set_fence_cb(), which now uses dma_fence_add_callback() to add the VC4 callback.

* Patch #4: Deletes the now-unused function vc4_queue_seqno_cb().

Changes
=======

v1 -> v2: https://lore.kernel.org/dri-devel/[email protected]/T/

* [1/4]: Use DRM Exec to replace the (un)lock reservation functions (Christian König)

Best Regards,
- Maíra

Maíra Canal (4):
drm/vc4: Use DRM Execution Contexts drm/vc4: Use DMA Resv to implement VC4 wait BO IOCTL drm/vc4: Remove BOs seqnos
drm/vc4: Remove vc4_queue_seqno_cb()

 drivers/gpu/drm/vc4/Kconfig        |   1 +
 drivers/gpu/drm/vc4/vc4_crtc.c     |  22 ++--
 drivers/gpu/drm/vc4/vc4_drv.h      |  26 ++---
 drivers/gpu/drm/vc4/vc4_gem.c      | 180 +++++++----------------------
 drivers/gpu/drm/vc4/vc4_validate.c |  11 --
 5 files changed, 64 insertions(+), 176 deletions(-)
Signature · 1 line
2.47.1
author reviewer
#1 lore raw

PATCH v2 1/4 drm/vc4: Use DRM Execution Contexts

VC4 has internal copies of drm_gem_lock_reservations() and drm_gem_unlock_reservations() inside the driver and ideally, we should move those hard-coded functions to the generic functions provided by DRM. But, instead of using the DRM GEM functions to (un)lock reservations, move the new DRM Execution Contexts API.

---
 drivers/gpu/drm/vc4/Kconfig   |  1 +
 drivers/gpu/drm/vc4/vc4_gem.c | 99 ++++++++---------------------------
 2 files changed, 22 insertions(+), 78 deletions(-)
drivers/gpu/drm/vc4/Kconfig +1 -0
index c5f30b317698..0627e826fda4 100644
@@ -13,6 +13,7 @@ config DRM_VC4
 	select DRM_DISPLAY_HDMI_HELPER
 	select DRM_DISPLAY_HDMI_STATE_HELPER
 	select DRM_DISPLAY_HELPER
+	select DRM_EXEC
 	select DRM_KMS_HELPER
 	select DRM_GEM_DMA_HELPER
 	select DRM_PANEL_BRIDGE
drivers/gpu/drm/vc4/vc4_gem.c +21 -78

171 lines not on this page · raw message

Signature · 1 line
2.47.1
Acked-by on v2 1/4
#2 lore raw

Re: [PATCH v2 1/4] drm/vc4: Use DRM Execution Contexts

Am 12.12.24 um 21:20 schrieb Maíra Canal:

Quoted 7 lines

VC4 has internal copies of drm_gem_lock_reservations() and drm_gem_unlock_reservations() inside the driver and ideally, we should move those hard-coded functions to the generic functions provided by DRM. But, instead of using the DRM GEM functions to (un)lock reservations, move the new DRM Execution Contexts API.

Oh, that looks really straight forward and clean. But I can only give an

since I don't know the VC4 code at all.

Regards,
Christian.

Quoted 202 lines
---
  drivers/gpu/drm/vc4/Kconfig   |  1 +
  drivers/gpu/drm/vc4/vc4_gem.c | 99 ++++++++---------------------------
  2 files changed, 22 insertions(+), 78 deletions(-)
drivers/gpu/drm/vc4/Kconfig +1 -0
index c5f30b317698..0627e826fda4 100644
@@ -13,6 +13,7 @@ config DRM_VC4
  	select DRM_DISPLAY_HDMI_HELPER
  	select DRM_DISPLAY_HDMI_STATE_HELPER
  	select DRM_DISPLAY_HELPER
+	select DRM_EXEC
  	select DRM_KMS_HELPER
  	select DRM_GEM_DMA_HELPER
  	select DRM_PANEL_BRIDGE
drivers/gpu/drm/vc4/vc4_gem.c +21 -78
index 22bccd69eb62..1021f45cb53c 100644
@@ -29,6 +29,7 @@
  #include <linux/sched/signal.h>
  #include <linux/dma-fence-array.h>
 
+#include <drm/drm_exec.h>
  #include <drm/drm_syncobj.h>
 
  #include "uapi/drm/vc4_drm.h"
@@ -578,19 +579,6 @@ vc4_update_bo_seqnos(struct vc4_exec_info *exec, uint64_t seqno)
  	}
  }
 
-static void
-vc4_unlock_bo_reservations(struct drm_device *dev,
-			   struct vc4_exec_info *exec,
-			   struct ww_acquire_ctx *acquire_ctx)
-{
-	int i;
-
-	for (i = 0; i < exec->bo_count; i++)
-		dma_resv_unlock(exec->bo[i]->resv);
-
-	ww_acquire_fini(acquire_ctx);
-}
-
  /* Takes the reservation lock on all the BOs being referenced, so that
   * at queue submit time we can update the reservations.
   *
@@ -599,70 +587,23 @@ vc4_unlock_bo_reservations(struct drm_device *dev,
   * to vc4, so we don't attach dma-buf fences to them.
   */
  static int
-vc4_lock_bo_reservations(struct drm_device *dev,
-			 struct vc4_exec_info *exec,
-			 struct ww_acquire_ctx *acquire_ctx)
+vc4_lock_bo_reservations(struct vc4_exec_info *exec,
+			 struct drm_exec *exec_ctx)
  {
-	int contended_lock = -1;
-	int i, ret;
-	struct drm_gem_object *bo;
-
-	ww_acquire_init(acquire_ctx, &reservation_ww_class);
-
-retry:
-	if (contended_lock != -1) {
-		bo = exec->bo[contended_lock];
-		ret = dma_resv_lock_slow_interruptible(bo->resv, acquire_ctx);
-		if (ret) {
-			ww_acquire_done(acquire_ctx);
-			return ret;
-		}
-	}
-
-	for (i = 0; i < exec->bo_count; i++) {
-		if (i == contended_lock)
-			continue;
-
-		bo = exec->bo[i];
-
-		ret = dma_resv_lock_interruptible(bo->resv, acquire_ctx);
-		if (ret) {
-			int j;
-
-			for (j = 0; j < i; j++) {
-				bo = exec->bo[j];
-				dma_resv_unlock(bo->resv);
-			}
-
-			if (contended_lock != -1 && contended_lock >= i) {
-				bo = exec->bo[contended_lock];
-
-				dma_resv_unlock(bo->resv);
-			}
-
-			if (ret == -EDEADLK) {
-				contended_lock = i;
-				goto retry;
-			}
-
-			ww_acquire_done(acquire_ctx);
-			return ret;
-		}
-	}
-
-	ww_acquire_done(acquire_ctx);
+	int ret;
 
  	/* Reserve space for our shared (read-only) fence references,
  	 * before we commit the CL to the hardware.
  	 */
-	for (i = 0; i < exec->bo_count; i++) {
-		bo = exec->bo[i];
+	drm_exec_init(exec_ctx, DRM_EXEC_INTERRUPTIBLE_WAIT, exec->bo_count);
+	drm_exec_until_all_locked(exec_ctx) {
+		ret = drm_exec_prepare_array(exec_ctx, exec->bo,
+					     exec->bo_count, 1);
+	}
 
-		ret = dma_resv_reserve_fences(bo->resv, 1);
-		if (ret) {
-			vc4_unlock_bo_reservations(dev, exec, acquire_ctx);
-			return ret;
-		}
+	if (ret) {
+		drm_exec_fini(exec_ctx);
+		return ret;
  	}
 
  	return 0;
@@ -679,7 +620,7 @@ vc4_lock_bo_reservations(struct drm_device *dev,
   */
  static int
  vc4_queue_submit(struct drm_device *dev, struct vc4_exec_info *exec,
-		 struct ww_acquire_ctx *acquire_ctx,
+		 struct drm_exec *exec_ctx,
  		 struct drm_syncobj *out_sync)
  {
  	struct vc4_dev *vc4 = to_vc4_dev(dev);
@@ -708,7 +649,7 @@ vc4_queue_submit(struct drm_device *dev, struct vc4_exec_info *exec,
 
  	vc4_update_bo_seqnos(exec, seqno);
 
-	vc4_unlock_bo_reservations(dev, exec, acquire_ctx);
+	drm_exec_fini(exec_ctx);
 
  	list_add_tail(&exec->head, &vc4->bin_job_list);
 
@@ -1123,7 +1064,7 @@ vc4_submit_cl_ioctl(struct drm_device *dev, void *data,
  	struct drm_vc4_submit_cl *args = data;
  	struct drm_syncobj *out_sync = NULL;
  	struct vc4_exec_info *exec;
-	struct ww_acquire_ctx acquire_ctx;
+	struct drm_exec exec_ctx;
  	struct dma_fence *in_fence;
  	int ret = 0;
 
@@ -1216,7 +1157,7 @@ vc4_submit_cl_ioctl(struct drm_device *dev, void *data,
  	if (ret)
  		goto fail;
 
-	ret = vc4_lock_bo_reservations(dev, exec, &acquire_ctx);
+	ret = vc4_lock_bo_reservations(exec, &exec_ctx);
  	if (ret)
  		goto fail;
 
@@ -1224,7 +1165,7 @@ vc4_submit_cl_ioctl(struct drm_device *dev, void *data,
  		out_sync = drm_syncobj_find(file_priv, args->out_sync);
  		if (!out_sync) {
  			ret = -EINVAL;
-			goto fail;
+			goto fail_unreserve;
  		}
 
  		/* We replace the fence in out_sync in vc4_queue_submit since
@@ -1239,7 +1180,7 @@ vc4_submit_cl_ioctl(struct drm_device *dev, void *data,
  	 */
  	exec->args = NULL;
 
-	ret = vc4_queue_submit(dev, exec, &acquire_ctx, out_sync);
+	ret = vc4_queue_submit(dev, exec, &exec_ctx, out_sync);
 
  	/* The syncobj isn't part of the exec data and we need to free our
  	 * reference even if job submission failed.
@@ -1248,13 +1189,15 @@ vc4_submit_cl_ioctl(struct drm_device *dev, void *data,
  		drm_syncobj_put(out_sync);
 
  	if (ret)
-		goto fail;
+		goto fail_unreserve;
 
  	/* Return the seqno for our job. */
  	args->seqno = vc4->emit_seqno;
 
  	return 0;
 
+fail_unreserve:
+	drm_exec_fini(&exec_ctx);
  fail:
  	vc4_complete_exec(&vc4->base, exec);
 
#3 lore raw

Re: [PATCH v2 1/4] drm/vc4: Use DRM Execution Contexts

In reply to Maíra Canal
Quoted 115 lines · Maíra Canal

VC4 has internal copies of drm_gem_lock_reservations() and drm_gem_unlock_reservations() inside the driver and ideally, we should move those hard-coded functions to the generic functions provided by DRM. But, instead of using the DRM GEM functions to (un)lock reservations, move the new DRM Execution Contexts API.

---
 drivers/gpu/drm/vc4/Kconfig   |  1 +
 drivers/gpu/drm/vc4/vc4_gem.c | 99 ++++++++---------------------------
 2 files changed, 22 insertions(+), 78 deletions(-)
drivers/gpu/drm/vc4/Kconfig +1 -0
index c5f30b317698..0627e826fda4 100644
@@ -13,6 +13,7 @@ config DRM_VC4
 	select DRM_DISPLAY_HDMI_HELPER
 	select DRM_DISPLAY_HDMI_STATE_HELPER
 	select DRM_DISPLAY_HELPER
+	select DRM_EXEC
 	select DRM_KMS_HELPER
 	select DRM_GEM_DMA_HELPER
 	select DRM_PANEL_BRIDGE
drivers/gpu/drm/vc4/vc4_gem.c +4 -64
index 22bccd69eb62..1021f45cb53c 100644
@@ -29,6 +29,7 @@
 #include <linux/sched/signal.h>
 #include <linux/dma-fence-array.h>
 
+#include <drm/drm_exec.h>
 #include <drm/drm_syncobj.h>
 
 #include "uapi/drm/vc4_drm.h"
@@ -578,19 +579,6 @@ vc4_update_bo_seqnos(struct vc4_exec_info *exec, uint64_t seqno)
 	}
 }
 
-static void
-vc4_unlock_bo_reservations(struct drm_device *dev,
-			   struct vc4_exec_info *exec,
-			   struct ww_acquire_ctx *acquire_ctx)
-{
-	int i;
-
-	for (i = 0; i < exec->bo_count; i++)
-		dma_resv_unlock(exec->bo[i]->resv);
-
-	ww_acquire_fini(acquire_ctx);
-}
-
 /* Takes the reservation lock on all the BOs being referenced, so that
  * at queue submit time we can update the reservations.
  *
@@ -599,70 +587,23 @@ vc4_unlock_bo_reservations(struct drm_device *dev,
  * to vc4, so we don't attach dma-buf fences to them.
  */
 static int
-vc4_lock_bo_reservations(struct drm_device *dev,
-			 struct vc4_exec_info *exec,
-			 struct ww_acquire_ctx *acquire_ctx)
+vc4_lock_bo_reservations(struct vc4_exec_info *exec,
+			 struct drm_exec *exec_ctx)
 {
-	int contended_lock = -1;
-	int i, ret;
-	struct drm_gem_object *bo;
-
-	ww_acquire_init(acquire_ctx, &reservation_ww_class);
-
-retry:
-	if (contended_lock != -1) {
-		bo = exec->bo[contended_lock];
-		ret = dma_resv_lock_slow_interruptible(bo->resv, acquire_ctx);
-		if (ret) {
-			ww_acquire_done(acquire_ctx);
-			return ret;
-		}
-	}
-
-	for (i = 0; i < exec->bo_count; i++) {
-		if (i == contended_lock)
-			continue;
-
-		bo = exec->bo[i];
-
-		ret = dma_resv_lock_interruptible(bo->resv, acquire_ctx);
-		if (ret) {
-			int j;
-
-			for (j = 0; j < i; j++) {
-				bo = exec->bo[j];
-				dma_resv_unlock(bo->resv);
-			}
-
-			if (contended_lock != -1 && contended_lock >= i) {
-				bo = exec->bo[contended_lock];
-
-				dma_resv_unlock(bo->resv);
-			}
-
-			if (ret == -EDEADLK) {
-				contended_lock = i;
-				goto retry;
-			}
-
-			ww_acquire_done(acquire_ctx);
-			return ret;
-		}
-	}
-
-	ww_acquire_done(acquire_ctx);
+	int ret;

drivers/gpu/drm/vc4/vc4_gem.c vc4_unlock_bo_reservations(struct drm_device *dev, · line 601

Quoted 10 lines
drivers/gpu/drm/vc4/vc4_gem.c +4 -2
 
 	/* Reserve space for our shared (read-only) fence references,
 	 * before we commit the CL to the hardware.
 	 */
-	for (i = 0; i < exec->bo_count; i++) {
-		bo = exec->bo[i];
+	drm_exec_init(exec_ctx, DRM_EXEC_INTERRUPTIBLE_WAIT, exec->bo_count);
+	drm_exec_until_all_locked(exec_ctx) {
+		ret = drm_exec_prepare_array(exec_ctx, exec->bo,
+					     exec->bo_count, 1);

Hi Maíra,

So, I'm not familiar too drm_exec, but the original implementation of vc4_lock_bo_reservations() has a retry of locks from the contention and I don't see it inside the drm_exec_prepare_array(), why don't use the loop drm_exec_prepare_obj() plus drm_exec_retry_on_contention() (similar to the typical usage documented for drm_exec)?

Also, probably you already considered that: we can extend this update to v3d, right?

Melissa

Quoted 87 lines
drivers/gpu/drm/vc4/vc4_gem.c +13 -12
+	}
 
-		ret = dma_resv_reserve_fences(bo->resv, 1);
-		if (ret) {
-			vc4_unlock_bo_reservations(dev, exec, acquire_ctx);
-			return ret;
-		}
+	if (ret) {
+		drm_exec_fini(exec_ctx);
+		return ret;
 	}
 
 	return 0;
@@ -679,7 +620,7 @@ vc4_lock_bo_reservations(struct drm_device *dev,
  */
 static int
 vc4_queue_submit(struct drm_device *dev, struct vc4_exec_info *exec,
-		 struct ww_acquire_ctx *acquire_ctx,
+		 struct drm_exec *exec_ctx,
 		 struct drm_syncobj *out_sync)
 {
 	struct vc4_dev *vc4 = to_vc4_dev(dev);
@@ -708,7 +649,7 @@ vc4_queue_submit(struct drm_device *dev, struct vc4_exec_info *exec,
 
 	vc4_update_bo_seqnos(exec, seqno);
 
-	vc4_unlock_bo_reservations(dev, exec, acquire_ctx);
+	drm_exec_fini(exec_ctx);
 
 	list_add_tail(&exec->head, &vc4->bin_job_list);
 
@@ -1123,7 +1064,7 @@ vc4_submit_cl_ioctl(struct drm_device *dev, void *data,
 	struct drm_vc4_submit_cl *args = data;
 	struct drm_syncobj *out_sync = NULL;
 	struct vc4_exec_info *exec;
-	struct ww_acquire_ctx acquire_ctx;
+	struct drm_exec exec_ctx;
 	struct dma_fence *in_fence;
 	int ret = 0;
 
@@ -1216,7 +1157,7 @@ vc4_submit_cl_ioctl(struct drm_device *dev, void *data,
 	if (ret)
 		goto fail;
 
-	ret = vc4_lock_bo_reservations(dev, exec, &acquire_ctx);
+	ret = vc4_lock_bo_reservations(exec, &exec_ctx);
 	if (ret)
 		goto fail;
 
@@ -1224,7 +1165,7 @@ vc4_submit_cl_ioctl(struct drm_device *dev, void *data,
 		out_sync = drm_syncobj_find(file_priv, args->out_sync);
 		if (!out_sync) {
 			ret = -EINVAL;
-			goto fail;
+			goto fail_unreserve;
 		}
 
 		/* We replace the fence in out_sync in vc4_queue_submit since
@@ -1239,7 +1180,7 @@ vc4_submit_cl_ioctl(struct drm_device *dev, void *data,
 	 */
 	exec->args = NULL;
 
-	ret = vc4_queue_submit(dev, exec, &acquire_ctx, out_sync);
+	ret = vc4_queue_submit(dev, exec, &exec_ctx, out_sync);
 
 	/* The syncobj isn't part of the exec data and we need to free our
 	 * reference even if job submission failed.
@@ -1248,13 +1189,15 @@ vc4_submit_cl_ioctl(struct drm_device *dev, void *data,
 		drm_syncobj_put(out_sync);
 
 	if (ret)
-		goto fail;
+		goto fail_unreserve;
 
 	/* Return the seqno for our job. */
 	args->seqno = vc4->emit_seqno;
 
 	return 0;
 
+fail_unreserve:
+	drm_exec_fini(&exec_ctx);
 fail:
 	vc4_complete_exec(&vc4->base, exec);
 
Signature · 1 line
2.47.1
#4 lore raw

Re: [PATCH v2 1/4] drm/vc4: Use DRM Execution Contexts

Am 16.12.24 um 20:08 schrieb Melissa Wen:

Quoted 128 lines · Maíra Canal
Quoted 115 lines

VC4 has internal copies of drm_gem_lock_reservations() and drm_gem_unlock_reservations() inside the driver and ideally, we should move those hard-coded functions to the generic functions provided by DRM. But, instead of using the DRM GEM functions to (un)lock reservations, move the new DRM Execution Contexts API.

---
  drivers/gpu/drm/vc4/Kconfig   |  1 +
  drivers/gpu/drm/vc4/vc4_gem.c | 99 ++++++++---------------------------
  2 files changed, 22 insertions(+), 78 deletions(-)
drivers/gpu/drm/vc4/Kconfig +1 -0
index c5f30b317698..0627e826fda4 100644
@@ -13,6 +13,7 @@ config DRM_VC4
  	select DRM_DISPLAY_HDMI_HELPER
  	select DRM_DISPLAY_HDMI_STATE_HELPER
  	select DRM_DISPLAY_HELPER
+	select DRM_EXEC
  	select DRM_KMS_HELPER
  	select DRM_GEM_DMA_HELPER
  	select DRM_PANEL_BRIDGE
drivers/gpu/drm/vc4/vc4_gem.c +4 -64
index 22bccd69eb62..1021f45cb53c 100644
@@ -29,6 +29,7 @@
  #include <linux/sched/signal.h>
  #include <linux/dma-fence-array.h>
 
+#include <drm/drm_exec.h>
  #include <drm/drm_syncobj.h>
 
  #include "uapi/drm/vc4_drm.h"
@@ -578,19 +579,6 @@ vc4_update_bo_seqnos(struct vc4_exec_info *exec, uint64_t seqno)
  	}
  }
 
-static void
-vc4_unlock_bo_reservations(struct drm_device *dev,
-			   struct vc4_exec_info *exec,
-			   struct ww_acquire_ctx *acquire_ctx)
-{
-	int i;
-
-	for (i = 0; i < exec->bo_count; i++)
-		dma_resv_unlock(exec->bo[i]->resv);
-
-	ww_acquire_fini(acquire_ctx);
-}
-
  /* Takes the reservation lock on all the BOs being referenced, so that
   * at queue submit time we can update the reservations.
   *
@@ -599,70 +587,23 @@ vc4_unlock_bo_reservations(struct drm_device *dev,
   * to vc4, so we don't attach dma-buf fences to them.
   */
  static int
-vc4_lock_bo_reservations(struct drm_device *dev,
-			 struct vc4_exec_info *exec,
-			 struct ww_acquire_ctx *acquire_ctx)
+vc4_lock_bo_reservations(struct vc4_exec_info *exec,
+			 struct drm_exec *exec_ctx)
  {
-	int contended_lock = -1;
-	int i, ret;
-	struct drm_gem_object *bo;
-
-	ww_acquire_init(acquire_ctx, &reservation_ww_class);
-
-retry:
-	if (contended_lock != -1) {
-		bo = exec->bo[contended_lock];
-		ret = dma_resv_lock_slow_interruptible(bo->resv, acquire_ctx);
-		if (ret) {
-			ww_acquire_done(acquire_ctx);
-			return ret;
-		}
-	}
-
-	for (i = 0; i < exec->bo_count; i++) {
-		if (i == contended_lock)
-			continue;
-
-		bo = exec->bo[i];
-
-		ret = dma_resv_lock_interruptible(bo->resv, acquire_ctx);
-		if (ret) {
-			int j;
-
-			for (j = 0; j < i; j++) {
-				bo = exec->bo[j];
-				dma_resv_unlock(bo->resv);
-			}
-
-			if (contended_lock != -1 && contended_lock >= i) {
-				bo = exec->bo[contended_lock];
-
-				dma_resv_unlock(bo->resv);
-			}
-
-			if (ret == -EDEADLK) {
-				contended_lock = i;
-				goto retry;
-			}
-
-			ww_acquire_done(acquire_ctx);
-			return ret;
-		}
-	}
-
-	ww_acquire_done(acquire_ctx);
+	int ret;

drivers/gpu/drm/vc4/vc4_gem.c vc4_unlock_bo_reservations(struct drm_device *dev, · line 601

Quoted 10 lines
drivers/gpu/drm/vc4/vc4_gem.c +4 -2
 
  	/* Reserve space for our shared (read-only) fence references,
  	 * before we commit the CL to the hardware.
  	 */
-	for (i = 0; i < exec->bo_count; i++) {
-		bo = exec->bo[i];
+	drm_exec_init(exec_ctx, DRM_EXEC_INTERRUPTIBLE_WAIT, exec->bo_count);
+	drm_exec_until_all_locked(exec_ctx) {
+		ret = drm_exec_prepare_array(exec_ctx, exec->bo,
+					     exec->bo_count, 1);

Hi Maíra,

Quoted 5 lines

So, I'm not familiar too drm_exec, but the original implementation of vc4_lock_bo_reservations() has a retry of locks from the contention and I don't see it inside the drm_exec_prepare_array(), why don't use the loop drm_exec_prepare_obj() plus drm_exec_retry_on_contention() (similar to the typical usage documented for drm_exec)?

The way how drm_exec and drm_exec_prepare_array is used seems to be correct here.

drm_exec_prepare_array() basically just loops over all the GEM BOs in the array and calls drm_exec_prepare_obj() on them, so no need to open code that.

drm_exec_retry_on_contention() is only needed if you have multiple calls to drm_exec_prepare_array() or drm_exec_prepare_obj(), so that the loop is restarted in between the calls.

Regards,
Christian.

Quoted 93 lines

Also, probably you already considered that: we can extend this update to v3d, right?

Melissa

Quoted 87 lines
drivers/gpu/drm/vc4/vc4_gem.c +13 -12
+	}
 
-		ret = dma_resv_reserve_fences(bo->resv, 1);
-		if (ret) {
-			vc4_unlock_bo_reservations(dev, exec, acquire_ctx);
-			return ret;
-		}
+	if (ret) {
+		drm_exec_fini(exec_ctx);
+		return ret;
  	}
 
  	return 0;
@@ -679,7 +620,7 @@ vc4_lock_bo_reservations(struct drm_device *dev,
   */
  static int
  vc4_queue_submit(struct drm_device *dev, struct vc4_exec_info *exec,
-		 struct ww_acquire_ctx *acquire_ctx,
+		 struct drm_exec *exec_ctx,
  		 struct drm_syncobj *out_sync)
  {
  	struct vc4_dev *vc4 = to_vc4_dev(dev);
@@ -708,7 +649,7 @@ vc4_queue_submit(struct drm_device *dev, struct vc4_exec_info *exec,
 
  	vc4_update_bo_seqnos(exec, seqno);
 
-	vc4_unlock_bo_reservations(dev, exec, acquire_ctx);
+	drm_exec_fini(exec_ctx);
 
  	list_add_tail(&exec->head, &vc4->bin_job_list);
 
@@ -1123,7 +1064,7 @@ vc4_submit_cl_ioctl(struct drm_device *dev, void *data,
  	struct drm_vc4_submit_cl *args = data;
  	struct drm_syncobj *out_sync = NULL;
  	struct vc4_exec_info *exec;
-	struct ww_acquire_ctx acquire_ctx;
+	struct drm_exec exec_ctx;
  	struct dma_fence *in_fence;
  	int ret = 0;
 
@@ -1216,7 +1157,7 @@ vc4_submit_cl_ioctl(struct drm_device *dev, void *data,
  	if (ret)
  		goto fail;
 
-	ret = vc4_lock_bo_reservations(dev, exec, &acquire_ctx);
+	ret = vc4_lock_bo_reservations(exec, &exec_ctx);
  	if (ret)
  		goto fail;
 
@@ -1224,7 +1165,7 @@ vc4_submit_cl_ioctl(struct drm_device *dev, void *data,
  		out_sync = drm_syncobj_find(file_priv, args->out_sync);
  		if (!out_sync) {
  			ret = -EINVAL;
-			goto fail;
+			goto fail_unreserve;
  		}
 
  		/* We replace the fence in out_sync in vc4_queue_submit since
@@ -1239,7 +1180,7 @@ vc4_submit_cl_ioctl(struct drm_device *dev, void *data,
  	 */
  	exec->args = NULL;
 
-	ret = vc4_queue_submit(dev, exec, &acquire_ctx, out_sync);
+	ret = vc4_queue_submit(dev, exec, &exec_ctx, out_sync);
 
  	/* The syncobj isn't part of the exec data and we need to free our
  	 * reference even if job submission failed.
@@ -1248,13 +1189,15 @@ vc4_submit_cl_ioctl(struct drm_device *dev, void *data,
  		drm_syncobj_put(out_sync);
 
  	if (ret)
-		goto fail;
+		goto fail_unreserve;
 
  	/* Return the seqno for our job. */
  	args->seqno = vc4->emit_seqno;
 
  	return 0;
 
+fail_unreserve:
+	drm_exec_fini(&exec_ctx);
  fail:
  	vc4_complete_exec(&vc4->base, exec);
 
Signature · 1 line
2.47.1
author reviewer
#5 lore raw

Re: [PATCH v2 1/4] drm/vc4: Use DRM Execution Contexts

Hi Christian,

Quoted 14 lines · Christian König

Am 16.12.24 um 20:08 schrieb Melissa Wen:

Quoted 13 lines · Maíra Canal
Quoted 12 lines

VC4 has internal copies of drm_gem_lock_reservations() and drm_gem_unlock_reservations() inside the driver and ideally, we should move those hard-coded functions to the generic functions provided by DRM. But, instead of using the DRM GEM functions to (un)lock reservations, move the new DRM Execution Contexts API.

---
  drivers/gpu/drm/vc4/Kconfig   |  1 + drivers/gpu/drm/vc4/vc4_gem.c | 99 ++++++++---------------------------

  2 files changed, 22 insertions(+), 78 deletions(-)

[...]

Quoted 21 lines
Quoted 13 lines
Quoted 9 lines

-
-    ww_acquire_done(acquire_ctx);
+    int ret;
      /* Reserve space for our shared (read-only) fence references,
       * before we commit the CL to the hardware.
       */
-    for (i = 0; i < exec->bo_count; i++) {
-        bo = exec->bo[i];
+    drm_exec_init(exec_ctx, DRM_EXEC_INTERRUPTIBLE_WAIT, exec-

Quoted 4 lines
Quoted 1 line

bo_count);

+    drm_exec_until_all_locked(exec_ctx) {
+        ret = drm_exec_prepare_array(exec_ctx, exec->bo, +                         exec->bo_count, 1);

Quoted 7 lines

Hi Maíra,

So, I'm not familiar too drm_exec, but the original implementation of vc4_lock_bo_reservations() has a retry of locks from the contention and I don't see it inside the drm_exec_prepare_array(), why don't use the loop drm_exec_prepare_obj() plus drm_exec_retry_on_contention() (similar to the typical usage documented for drm_exec)?

Quoted 5 lines

The way how drm_exec and drm_exec_prepare_array is used seems to be correct here.

drm_exec_prepare_array() basically just loops over all the GEM BOs in the array and calls drm_exec_prepare_obj() on them, so no need to open code that.

drm_exec_retry_on_contention() is only needed if you have multiple calls to drm_exec_prepare_array() or drm_exec_prepare_obj(), so that the loop is restarted in between the calls.

But doesn't drm_exec_prepare_array() have multiple calls to drm_exec_prepare_obj()? The fact that the multiple calls are wrapped in the function makes a difference?

Best Regards,
- Maíra

Quoted 87 lines

Regards,
Christian.

Quoted 82 lines

Also, probably you already considered that: we can extend this update to v3d, right?

Melissa

Quoted 76 lines

+    }
-        ret = dma_resv_reserve_fences(bo->resv, 1);
-        if (ret) {
-            vc4_unlock_bo_reservations(dev, exec, acquire_ctx); -            return ret;
-        }
+    if (ret) {
+        drm_exec_fini(exec_ctx);
+        return ret;
      }
      return 0;

(quoted diff) +0 -0
@@ -679,7 +620,7 @@ vc4_lock_bo_reservations(struct drm_device *dev,
    */

  static int
  vc4_queue_submit(struct drm_device *dev, struct vc4_exec_info *exec, -         struct ww_acquire_ctx *acquire_ctx,
+         struct drm_exec *exec_ctx,
           struct drm_syncobj *out_sync)
  {
      struct vc4_dev *vc4 = to_vc4_dev(dev);

drivers/gpu/drm/vc4/vc4_gem.c +8 -6
@@ -708,7 +649,7 @@ vc4_queue_submit(struct drm_device *dev, struct 
 vc4_exec_info *exec,
       vc4_update_bo_seqnos(exec, seqno);
-    vc4_unlock_bo_reservations(dev, exec, acquire_ctx);
+    drm_exec_fini(exec_ctx);
       list_add_tail(&exec->head, &vc4->bin_job_list);
@@ -1123,7 +1064,7 @@ vc4_submit_cl_ioctl(struct drm_device *dev, 
 void *data,
       struct drm_vc4_submit_cl *args = data;
       struct drm_syncobj *out_sync = NULL;
       struct vc4_exec_info *exec;
-    struct ww_acquire_ctx acquire_ctx;
+    struct drm_exec exec_ctx;
       struct dma_fence *in_fence;
       int ret = 0;
@@ -1216,7 +1157,7 @@ vc4_submit_cl_ioctl(struct drm_device *dev, 
 void *data,
       if (ret)
           goto fail;
-    ret = vc4_lock_bo_reservations(dev, exec, &acquire_ctx);
+    ret = vc4_lock_bo_reservations(exec, &exec_ctx);
       if (ret)
           goto fail;
@@ -1224,7 +1165,7 @@ vc4_submit_cl_ioctl(struct drm_device *dev, 
 void *data,
           out_sync = drm_syncobj_find(file_priv, args->out_sync);
           if (!out_sync) {
               ret = -EINVAL;
-            goto fail;
+            goto fail_unreserve;
           }
           /* We replace the fence in out_sync in vc4_queue_submit since
@@ -1239,7 +1180,7 @@ vc4_submit_cl_ioctl(struct drm_device *dev, 
 void *data,
        */
       exec->args = NULL;
-    ret = vc4_queue_submit(dev, exec, &acquire_ctx, out_sync);
+    ret = vc4_queue_submit(dev, exec, &exec_ctx, out_sync);
       /* The syncobj isn't part of the exec data and we need to free our
        * reference even if job submission failed.
@@ -1248,13 +1189,15 @@ vc4_submit_cl_ioctl(struct drm_device *dev, 
 void *data,
           drm_syncobj_put(out_sync);
       if (ret)
-        goto fail;
+        goto fail_unreserve;
       /* Return the seqno for our job. */
       args->seqno = vc4->emit_seqno;
       return 0;
+fail_unreserve:
+    drm_exec_fini(&exec_ctx);

  fail:
      vc4_complete_exec(&vc4->base, exec);

Signature · 1 line
2.47.1
#6 lore raw

Re: [PATCH v2 1/4] drm/vc4: Use DRM Execution Contexts

Am 17.12.24 um 12:09 schrieb Maíra Canal:

Quoted 44 lines

Hi Christian,

Quoted 15 lines · Christian König

Am 16.12.24 um 20:08 schrieb Melissa Wen:

Quoted 14 lines · Maíra Canal
Quoted 13 lines

VC4 has internal copies of drm_gem_lock_reservations() and drm_gem_unlock_reservations() inside the driver and ideally, we should move those hard-coded functions to the generic functions provided by DRM. But, instead of using the DRM GEM functions to (un)lock reservations, move the new DRM Execution Contexts API.

---
  drivers/gpu/drm/vc4/Kconfig   |  1 +
  drivers/gpu/drm/vc4/vc4_gem.c | 99
++++++++---------------------------

  2 files changed, 22 insertions(+), 78 deletions(-)

[...]

Quoted 23 lines
Quoted 13 lines
Quoted 9 lines

-
-    ww_acquire_done(acquire_ctx);
+    int ret;
      /* Reserve space for our shared (read-only) fence references,
       * before we commit the CL to the hardware.
       */
-    for (i = 0; i < exec->bo_count; i++) {
-        bo = exec->bo[i];
+    drm_exec_init(exec_ctx, DRM_EXEC_INTERRUPTIBLE_WAIT, exec-

Quoted 4 lines
Quoted 1 line

bo_count);

+    drm_exec_until_all_locked(exec_ctx) {
+        ret = drm_exec_prepare_array(exec_ctx, exec->bo, +                         exec->bo_count, 1);

Quoted 7 lines

Hi Maíra,

So, I'm not familiar too drm_exec, but the original implementation of vc4_lock_bo_reservations() has a retry of locks from the contention and I don't see it inside the drm_exec_prepare_array(), why don't use the loop drm_exec_prepare_obj() plus drm_exec_retry_on_contention() (similar to the typical usage documented for drm_exec)?

The way how drm_exec and drm_exec_prepare_array is used seems to be correct here.

Quoted 7 lines
Quoted 3 lines

drm_exec_prepare_array() basically just loops over all the GEM BOs in the array and calls drm_exec_prepare_obj() on them, so no need to open code that.

drm_exec_retry_on_contention() is only needed if you have multiple calls to drm_exec_prepare_array() or drm_exec_prepare_obj(), so that the loop is restarted in between the calls.

But doesn't drm_exec_prepare_array() have multiple calls to drm_exec_prepare_obj()? The fact that the multiple calls are wrapped in the function makes a difference?

Yeah. I know, it's not so easy to understand :)

What drm_exec_until_all_locked() and drm_exec_retry_on_contention() are basically doing is nicely wrapped error handling.

In other words drm_exec_retry_on_contention() does a "goto *__drm_exec_retry_ptr" if it detects that we have a contention. But you can't goto from a label in a function back into the caller.

So what drm_exec_prepare_array() does is to abort as soon as it sees the first error:

                ret = drm_exec_prepare_obj(exec, objects[i], num_fences); if (unlikely(ret))
                        return ret;

And in the caller we have:

drm_exec_until_all_locked(exec_ctx) {
    ret = drm_exec_prepare_array(exec_ctx, exec->bo, exec->bo_count, 1); }

So the loop restarts after drm_exec_prepare_array() anyway and because of this using drm_exec_retry_on_contention() is not explicitly necessary.

Regards,
Christian.

Quoted 92 lines

Best Regards,
- Maíra

Quoted 87 lines

Regards,
Christian.

Quoted 82 lines

Also, probably you already considered that: we can extend this update to v3d, right?

Melissa

Quoted 76 lines

+    }
-        ret = dma_resv_reserve_fences(bo->resv, 1);
-        if (ret) {
-            vc4_unlock_bo_reservations(dev, exec, acquire_ctx); -            return ret;
-        }
+    if (ret) {
+        drm_exec_fini(exec_ctx);
+        return ret;
      }
      return 0;

(quoted diff) +0 -0
@@ -679,7 +620,7 @@ vc4_lock_bo_reservations(struct drm_device *dev,
    */

  static int
  vc4_queue_submit(struct drm_device *dev, struct vc4_exec_info *exec, -         struct ww_acquire_ctx *acquire_ctx,
+         struct drm_exec *exec_ctx,
           struct drm_syncobj *out_sync)
  {
      struct vc4_dev *vc4 = to_vc4_dev(dev);

drivers/gpu/drm/vc4/vc4_gem.c +8 -6
@@ -708,7 +649,7 @@ vc4_queue_submit(struct drm_device *dev, struct 
 vc4_exec_info *exec,
       vc4_update_bo_seqnos(exec, seqno);
-    vc4_unlock_bo_reservations(dev, exec, acquire_ctx);
+    drm_exec_fini(exec_ctx);
       list_add_tail(&exec->head, &vc4->bin_job_list);
@@ -1123,7 +1064,7 @@ vc4_submit_cl_ioctl(struct drm_device *dev, 
 void *data,
       struct drm_vc4_submit_cl *args = data;
       struct drm_syncobj *out_sync = NULL;
       struct vc4_exec_info *exec;
-    struct ww_acquire_ctx acquire_ctx;
+    struct drm_exec exec_ctx;
       struct dma_fence *in_fence;
       int ret = 0;
@@ -1216,7 +1157,7 @@ vc4_submit_cl_ioctl(struct drm_device *dev, 
 void *data,
       if (ret)
           goto fail;
-    ret = vc4_lock_bo_reservations(dev, exec, &acquire_ctx);
+    ret = vc4_lock_bo_reservations(exec, &exec_ctx);
       if (ret)
           goto fail;
@@ -1224,7 +1165,7 @@ vc4_submit_cl_ioctl(struct drm_device *dev, 
 void *data,
           out_sync = drm_syncobj_find(file_priv, args->out_sync);
           if (!out_sync) {
               ret = -EINVAL;
-            goto fail;
+            goto fail_unreserve;
           }
           /* We replace the fence in out_sync in vc4_queue_submit since
@@ -1239,7 +1180,7 @@ vc4_submit_cl_ioctl(struct drm_device *dev, 
 void *data,
        */
       exec->args = NULL;
-    ret = vc4_queue_submit(dev, exec, &acquire_ctx, out_sync);
+    ret = vc4_queue_submit(dev, exec, &exec_ctx, out_sync);
       /* The syncobj isn't part of the exec data and we need to free our
        * reference even if job submission failed.
@@ -1248,13 +1189,15 @@ vc4_submit_cl_ioctl(struct drm_device *dev, 
 void *data,
           drm_syncobj_put(out_sync);
       if (ret)
-        goto fail;
+        goto fail_unreserve;
       /* Return the seqno for our job. */
       args->seqno = vc4->emit_seqno;
       return 0;
+fail_unreserve:
+    drm_exec_fini(&exec_ctx);

  fail:
      vc4_complete_exec(&vc4->base, exec);

Signature · 1 line
2.47.1
#7 lore raw

Re: [PATCH v2 1/4] drm/vc4: Use DRM Execution Contexts

Quoted 79 lines · Christian König

Am 17.12.24 um 12:09 schrieb Maíra Canal:

Quoted 53 lines

Hi Christian,

Quoted 18 lines · Christian König

Am 16.12.24 um 20:08 schrieb Melissa Wen:

Quoted 17 lines · Maíra Canal
Quoted 16 lines

VC4 has internal copies of drm_gem_lock_reservations() and drm_gem_unlock_reservations() inside the driver and ideally, we should
move those hard-coded functions to the generic functions provided by DRM.
But, instead of using the DRM GEM functions to (un)lock reservations, move
the new DRM Execution Contexts API.

---
  drivers/gpu/drm/vc4/Kconfig   |  1 +
  drivers/gpu/drm/vc4/vc4_gem.c | 99
++++++++---------------------------

  2 files changed, 22 insertions(+), 78 deletions(-)

[...]

Quoted 29 lines
Quoted 13 lines
Quoted 5 lines

-
-    ww_acquire_done(acquire_ctx);
+    int ret;
      /* Reserve space for our shared (read-only) fence references,
       * before we commit the CL to the hardware.

Quoted 8 lines

       */
-    for (i = 0; i < exec->bo_count; i++) {
-        bo = exec->bo[i];
+    drm_exec_init(exec_ctx, DRM_EXEC_INTERRUPTIBLE_WAIT, exec- >bo_count);
+    drm_exec_until_all_locked(exec_ctx) {
+        ret = drm_exec_prepare_array(exec_ctx, exec->bo, +                         exec->bo_count, 1);

Quoted 8 lines

Hi Maíra,

So, I'm not familiar too drm_exec, but the original implementation of vc4_lock_bo_reservations() has a retry of locks from the contention and I don't see it inside the drm_exec_prepare_array(), why don't use the loop drm_exec_prepare_obj() plus drm_exec_retry_on_contention() (similar
to the typical usage documented for drm_exec)?

The way how drm_exec and drm_exec_prepare_array is used seems to be correct here.

drm_exec_prepare_array() basically just loops over all the GEM BOs in the array and calls drm_exec_prepare_obj() on them, so no need to open code that.

Quoted 7 lines
Quoted 3 lines

drm_exec_retry_on_contention() is only needed if you have multiple calls to drm_exec_prepare_array() or drm_exec_prepare_obj(), so that the loop is restarted in between the calls.

But doesn't drm_exec_prepare_array() have multiple calls to drm_exec_prepare_obj()? The fact that the multiple calls are wrapped in the function makes a difference?

Yeah. I know, it's not so easy to understand :)

What drm_exec_until_all_locked() and drm_exec_retry_on_contention() are basically doing is nicely wrapped error handling.

In other words drm_exec_retry_on_contention() does a "goto *__drm_exec_retry_ptr" if it detects that we have a contention. But you can't goto from a label in a function back into the caller.

So what drm_exec_prepare_array() does is to abort as soon as it sees the first error:

                ret = drm_exec_prepare_obj(exec, objects[i], num_fences); if (unlikely(ret))
                        return ret;

Quoted 8 lines

And in the caller we have:

drm_exec_until_all_locked(exec_ctx) {
    ret = drm_exec_prepare_array(exec_ctx, exec->bo, exec->bo_count, 1); }

So the loop restarts after drm_exec_prepare_array() anyway and because of this using drm_exec_retry_on_contention() is not explicitly necessary.

Ah, got it now. Thanks for explaining.

Melissa

Quoted 100 lines

Regards,
Christian.

Quoted 95 lines

Best Regards,
- Maíra

Quoted 90 lines

Regards,
Christian.

Quoted 85 lines

Also, probably you already considered that: we can extend this update to
v3d, right?

Melissa

Quoted 78 lines

+    }
-        ret = dma_resv_reserve_fences(bo->resv, 1);
-        if (ret) {
-            vc4_unlock_bo_reservations(dev, exec, acquire_ctx); -            return ret;
-        }
+    if (ret) {
+        drm_exec_fini(exec_ctx);
+        return ret;
      }
      return 0;

(quoted diff) +0 -0
@@ -679,7 +620,7 @@ vc4_lock_bo_reservations(struct drm_device *dev,
    */

  static int
  vc4_queue_submit(struct drm_device *dev, struct vc4_exec_info *exec, -         struct ww_acquire_ctx *acquire_ctx,
+         struct drm_exec *exec_ctx,
           struct drm_syncobj *out_sync)
  {
      struct vc4_dev *vc4 = to_vc4_dev(dev);

drivers/gpu/drm/vc4/vc4_gem.c +4 -4
@@ -708,7 +649,7 @@ vc4_queue_submit(struct drm_device *dev,
 struct vc4_exec_info *exec,
       vc4_update_bo_seqnos(exec, seqno);
-    vc4_unlock_bo_reservations(dev, exec, acquire_ctx);
+    drm_exec_fini(exec_ctx);
       list_add_tail(&exec->head, &vc4->bin_job_list);
@@ -1123,7 +1064,7 @@ vc4_submit_cl_ioctl(struct drm_device
 *dev, void *data,
       struct drm_vc4_submit_cl *args = data;
       struct drm_syncobj *out_sync = NULL;
       struct vc4_exec_info *exec;
-    struct ww_acquire_ctx acquire_ctx;
+    struct drm_exec exec_ctx;
       struct dma_fence *in_fence;
       int ret = 0;
@@ -1216,7 +1157,7 @@ vc4_submit_cl_ioctl(struct drm_device
 *dev, void *data,
       if (ret)
           goto fail;
-    ret = vc4_lock_bo_reservations(dev, exec, &acquire_ctx);
+    ret = vc4_lock_bo_reservations(exec, &exec_ctx);
       if (ret)
           goto fail;
@@ -1224,7 +1165,7 @@ vc4_submit_cl_ioctl(struct drm_device
 *dev, void *data,
           out_sync = drm_syncobj_find(file_priv, args->out_sync);
           if (!out_sync) {
               ret = -EINVAL;
-            goto fail;
+            goto fail_unreserve;
           }
           /* We replace the fence in out_sync in

vc4_queue_submit since

drivers/gpu/drm/vc4/vc4_gem.c +1 -1
@@ -1239,7 +1180,7 @@ vc4_submit_cl_ioctl(struct drm_device
 *dev, void *data,
        */
       exec->args = NULL;
-    ret = vc4_queue_submit(dev, exec, &acquire_ctx, out_sync);
+    ret = vc4_queue_submit(dev, exec, &exec_ctx, out_sync);
       /* The syncobj isn't part of the exec data and we need

to free our
       * reference even if job submission failed.

drivers/gpu/drm/vc4/vc4_gem.c +3 -1
@@ -1248,13 +1189,15 @@ vc4_submit_cl_ioctl(struct
 drm_device *dev, void *data,
           drm_syncobj_put(out_sync);
       if (ret)
-        goto fail;
+        goto fail_unreserve;
       /* Return the seqno for our job. */
       args->seqno = vc4->emit_seqno;
       return 0;
+fail_unreserve:
+    drm_exec_fini(&exec_ctx);

  fail:
      vc4_complete_exec(&vc4->base, exec);

Signature · 1 line
2.47.1
#8 lore raw

Re: [PATCH v2 1/4] drm/vc4: Use DRM Execution Contexts

In reply to Christian König
Quoted 134 lines · Christian König

Am 16.12.24 um 20:08 schrieb Melissa Wen:

Quoted 124 lines · Maíra Canal
Quoted 111 lines

VC4 has internal copies of drm_gem_lock_reservations() and drm_gem_unlock_reservations() inside the driver and ideally, we should move those hard-coded functions to the generic functions provided by DRM. But, instead of using the DRM GEM functions to (un)lock reservations, move the new DRM Execution Contexts API.

---
  drivers/gpu/drm/vc4/Kconfig   |  1 +
  drivers/gpu/drm/vc4/vc4_gem.c | 99 ++++++++---------------------------
  2 files changed, 22 insertions(+), 78 deletions(-)
drivers/gpu/drm/vc4/Kconfig +1 -0
index c5f30b317698..0627e826fda4 100644
@@ -13,6 +13,7 @@ config DRM_VC4
  	select DRM_DISPLAY_HDMI_HELPER
  	select DRM_DISPLAY_HDMI_STATE_HELPER
  	select DRM_DISPLAY_HELPER
+	select DRM_EXEC
  	select DRM_KMS_HELPER
  	select DRM_GEM_DMA_HELPER
  	select DRM_PANEL_BRIDGE
drivers/gpu/drm/vc4/vc4_gem.c +3 -64
index 22bccd69eb62..1021f45cb53c 100644
@@ -29,6 +29,7 @@
  #include <linux/sched/signal.h>
  #include <linux/dma-fence-array.h>
+#include <drm/drm_exec.h>
  #include <drm/drm_syncobj.h>
  #include "uapi/drm/vc4_drm.h"
@@ -578,19 +579,6 @@ vc4_update_bo_seqnos(struct vc4_exec_info *exec, uint64_t seqno)
  	}
  }
-static void
-vc4_unlock_bo_reservations(struct drm_device *dev,
-			   struct vc4_exec_info *exec,
-			   struct ww_acquire_ctx *acquire_ctx)
-{
-	int i;
-
-	for (i = 0; i < exec->bo_count; i++)
-		dma_resv_unlock(exec->bo[i]->resv);
-
-	ww_acquire_fini(acquire_ctx);
-}
-
  /* Takes the reservation lock on all the BOs being referenced, so that
   * at queue submit time we can update the reservations.
   *
@@ -599,70 +587,23 @@ vc4_unlock_bo_reservations(struct drm_device *dev,
   * to vc4, so we don't attach dma-buf fences to them.
   */
  static int
-vc4_lock_bo_reservations(struct drm_device *dev,
-			 struct vc4_exec_info *exec,
-			 struct ww_acquire_ctx *acquire_ctx)
+vc4_lock_bo_reservations(struct vc4_exec_info *exec,
+			 struct drm_exec *exec_ctx)
  {
-	int contended_lock = -1;
-	int i, ret;
-	struct drm_gem_object *bo;
-
-	ww_acquire_init(acquire_ctx, &reservation_ww_class);
-
-retry:
-	if (contended_lock != -1) {
-		bo = exec->bo[contended_lock];
-		ret = dma_resv_lock_slow_interruptible(bo->resv, acquire_ctx);
-		if (ret) {
-			ww_acquire_done(acquire_ctx);
-			return ret;
-		}
-	}
-
-	for (i = 0; i < exec->bo_count; i++) {
-		if (i == contended_lock)
-			continue;
-
-		bo = exec->bo[i];
-
-		ret = dma_resv_lock_interruptible(bo->resv, acquire_ctx);
-		if (ret) {
-			int j;
-
-			for (j = 0; j < i; j++) {
-				bo = exec->bo[j];
-				dma_resv_unlock(bo->resv);
-			}
-
-			if (contended_lock != -1 && contended_lock >= i) {
-				bo = exec->bo[contended_lock];
-
-				dma_resv_unlock(bo->resv);
-			}
-
-			if (ret == -EDEADLK) {
-				contended_lock = i;
-				goto retry;
-			}
-
-			ww_acquire_done(acquire_ctx);
-			return ret;
-		}
-	}
-
-	ww_acquire_done(acquire_ctx);

drivers/gpu/drm/vc4/vc4_gem.c vc4_unlock_bo_reservations(struct drm_device *dev, · line 600

Quoted 10 lines
drivers/gpu/drm/vc4/vc4_gem.c +5 -2
+	int ret;
  	/* Reserve space for our shared (read-only) fence references,
  	 * before we commit the CL to the hardware.
  	 */
-	for (i = 0; i < exec->bo_count; i++) {
-		bo = exec->bo[i];
+	drm_exec_init(exec_ctx, DRM_EXEC_INTERRUPTIBLE_WAIT, exec->bo_count);
+	drm_exec_until_all_locked(exec_ctx) {
+		ret = drm_exec_prepare_array(exec_ctx, exec->bo,
+					     exec->bo_count, 1);

Hi Maíra,

Quoted 5 lines

So, I'm not familiar too drm_exec, but the original implementation of vc4_lock_bo_reservations() has a retry of locks from the contention and I don't see it inside the drm_exec_prepare_array(), why don't use the loop drm_exec_prepare_obj() plus drm_exec_retry_on_contention() (similar to the typical usage documented for drm_exec)?

The way how drm_exec and drm_exec_prepare_array is used seems to be correct here.

Quoted 7 lines

drm_exec_prepare_array() basically just loops over all the GEM BOs in the array and calls drm_exec_prepare_obj() on them, so no need to open code that.

drm_exec_retry_on_contention() is only needed if you have multiple calls to drm_exec_prepare_array() or drm_exec_prepare_obj(), so that the loop is restarted in between the calls.

Current behavior of vc4 is to retry locking the contended lock and then all others, instead of just return on error, and I don't see this being covered by drm_exec_prepare_array(). vc4 behavior seems closer to what is done by MSM, a loop of drm_exec_prepare_obj() plus drm_exec_retry_on_contention().

Moreover, I don't see any DRM driver using drm_exec_prepare_array(), so maybe updating this function would make it more useful and meet vc4 current behavior too. Something as below:

```
int drm_exec_prepare_array(struct drm_exec *exec,
			   struct drm_gem_object **objects,
			   unsigned int num_objects,
			   unsigned int num_fences)
{
	int ret;

	for (unsigned int i = 0; i < num_objects; ++i) {
		ret = drm_exec_prepare_obj(exec, objects[i], num_fences);
		drm_exec_retry_on_contention(exec); <----- here
		if (unlikely(ret))
			return ret;
	}

	return 0;
}
```

WDYT?

Melissa

Quoted 81 lines

Regards,
Christian.

Quoted 76 lines

Also, probably you already considered that: we can extend this update to v3d, right?

Melissa

Quoted 70 lines
drivers/gpu/drm/vc4/vc4_gem.c +13 -12
+	}
-		ret = dma_resv_reserve_fences(bo->resv, 1);
-		if (ret) {
-			vc4_unlock_bo_reservations(dev, exec, acquire_ctx);
-			return ret;
-		}
+	if (ret) {
+		drm_exec_fini(exec_ctx);
+		return ret;
  	}
  	return 0;
@@ -679,7 +620,7 @@ vc4_lock_bo_reservations(struct drm_device *dev,
   */
  static int
  vc4_queue_submit(struct drm_device *dev, struct vc4_exec_info *exec,
-		 struct ww_acquire_ctx *acquire_ctx,
+		 struct drm_exec *exec_ctx,
  		 struct drm_syncobj *out_sync)
  {
  	struct vc4_dev *vc4 = to_vc4_dev(dev);
@@ -708,7 +649,7 @@ vc4_queue_submit(struct drm_device *dev, struct vc4_exec_info *exec,
  	vc4_update_bo_seqnos(exec, seqno);
-	vc4_unlock_bo_reservations(dev, exec, acquire_ctx);
+	drm_exec_fini(exec_ctx);
  	list_add_tail(&exec->head, &vc4->bin_job_list);
@@ -1123,7 +1064,7 @@ vc4_submit_cl_ioctl(struct drm_device *dev, void *data,
  	struct drm_vc4_submit_cl *args = data;
  	struct drm_syncobj *out_sync = NULL;
  	struct vc4_exec_info *exec;
-	struct ww_acquire_ctx acquire_ctx;
+	struct drm_exec exec_ctx;
  	struct dma_fence *in_fence;
  	int ret = 0;
@@ -1216,7 +1157,7 @@ vc4_submit_cl_ioctl(struct drm_device *dev, void *data,
  	if (ret)
  		goto fail;
-	ret = vc4_lock_bo_reservations(dev, exec, &acquire_ctx);
+	ret = vc4_lock_bo_reservations(exec, &exec_ctx);
  	if (ret)
  		goto fail;
@@ -1224,7 +1165,7 @@ vc4_submit_cl_ioctl(struct drm_device *dev, void *data,
  		out_sync = drm_syncobj_find(file_priv, args->out_sync);
  		if (!out_sync) {
  			ret = -EINVAL;
-			goto fail;
+			goto fail_unreserve;
  		}
  		/* We replace the fence in out_sync in vc4_queue_submit since
@@ -1239,7 +1180,7 @@ vc4_submit_cl_ioctl(struct drm_device *dev, void *data,
  	 */
  	exec->args = NULL;
-	ret = vc4_queue_submit(dev, exec, &acquire_ctx, out_sync);
+	ret = vc4_queue_submit(dev, exec, &exec_ctx, out_sync);
  	/* The syncobj isn't part of the exec data and we need to free our
  	 * reference even if job submission failed.
@@ -1248,13 +1189,15 @@ vc4_submit_cl_ioctl(struct drm_device *dev, void *data,
  		drm_syncobj_put(out_sync);
  	if (ret)
-		goto fail;
+		goto fail_unreserve;
  	/* Return the seqno for our job. */
  	args->seqno = vc4->emit_seqno;
  	return 0;
+fail_unreserve:
+	drm_exec_fini(&exec_ctx);
  fail:
  	vc4_complete_exec(&vc4->base, exec);
Signature · 1 line
2.47.1
author reviewer
#9 lore raw

PATCH v2 2/4 drm/vc4: Use DMA Resv to implement VC4 wait BO IOCTL

In reply to Maíra Canal

As the BOs used by VC4 have DMA Reservation Objects attached to it, there is no need to use seqnos wait for the BOs availability. Instead, we can use dma_gem_dma_resv_wait().

---
 drivers/gpu/drm/vc4/vc4_drv.h | 10 ++++++++++
 drivers/gpu/drm/vc4/vc4_gem.c | 31 +++++++++++++++++++------------
 2 files changed, 29 insertions(+), 12 deletions(-)
drivers/gpu/drm/vc4/vc4_drv.h +10 -0
index 4a078ffd9f82..03ed40ab9a93 100644
@@ -924,6 +924,16 @@ struct vc4_validated_shader_info {
 						   (Wmax))
 #define wait_for(COND, MS)		_wait_for((COND), (MS) * 1000, 10, 1000)
 
+static inline unsigned long nsecs_to_jiffies_timeout(const u64 n)
+{
+	/* nsecs_to_jiffies64() does not guard against overflow */
+	if ((NSEC_PER_SEC % HZ) != 0 &&
+	    div_u64(n, NSEC_PER_SEC) >= MAX_JIFFY_OFFSET / HZ)
+		return MAX_JIFFY_OFFSET;
+
+	return min_t(u64, MAX_JIFFY_OFFSET, nsecs_to_jiffies64(n) + 1);
+}
+
 /* vc4_bo.c */
 struct drm_gem_object *vc4_create_object(struct drm_device *dev, size_t size);
 struct vc4_bo *vc4_bo_create(struct drm_device *dev, size_t size,
drivers/gpu/drm/vc4/vc4_gem.c +19 -12
index 1021f45cb53c..4037c65eb269 100644
@@ -1017,11 +1017,13 @@ int
 vc4_wait_bo_ioctl(struct drm_device *dev, void *data,
 		  struct drm_file *file_priv)
 {
-	struct vc4_dev *vc4 = to_vc4_dev(dev);
 	int ret;
+	struct vc4_dev *vc4 = to_vc4_dev(dev);
 	struct drm_vc4_wait_bo *args = data;
-	struct drm_gem_object *gem_obj;
-	struct vc4_bo *bo;
+	unsigned long timeout_jiffies =
+		nsecs_to_jiffies_timeout(args->timeout_ns);
+	ktime_t start = ktime_get();
+	u64 delta_ns;
 
 	if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
 		return -ENODEV;
@@ -1029,17 +1031,22 @@ vc4_wait_bo_ioctl(struct drm_device *dev, void *data,
 	if (args->pad != 0)
 		return -EINVAL;
 
-	gem_obj = drm_gem_object_lookup(file_priv, args->handle);
-	if (!gem_obj) {
-		DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
-		return -EINVAL;
-	}
-	bo = to_vc4_bo(gem_obj);
+	ret = drm_gem_dma_resv_wait(file_priv, args->handle,
+				    true, timeout_jiffies);
 
-	ret = vc4_wait_for_seqno_ioctl_helper(dev, bo->seqno,
-					      &args->timeout_ns);
+	/* Decrement the user's timeout, in case we got interrupted
+	 * such that the ioctl will be restarted.
+	 */
+	delta_ns = ktime_to_ns(ktime_sub(ktime_get(), start));
+	if (delta_ns < args->timeout_ns)
+		args->timeout_ns -= delta_ns;
+	else
+		args->timeout_ns = 0;
+
+	/* Asked to wait beyond the jiffy/scheduler precision? */
+	if (ret == -ETIME && args->timeout_ns)
+		ret = -EAGAIN;
 
-	drm_gem_object_put(gem_obj);
 	return ret;
 }
 
Signature · 1 line
2.47.1
Reviewed-by on v2 2/4
#10 lore raw

Re: [PATCH v2 2/4] drm/vc4: Use DMA Resv to implement VC4 wait BO IOCTL

Quoted 3 lines · Maíra Canal

As the BOs used by VC4 have DMA Reservation Objects attached to it, there is no need to use seqnos wait for the BOs availability. Instead, we can use dma_gem_dma_resv_wait().

typo: drm_gem_dma_resc_wait()

Quoted 15 lines
---
 drivers/gpu/drm/vc4/vc4_drv.h | 10 ++++++++++
 drivers/gpu/drm/vc4/vc4_gem.c | 31 +++++++++++++++++++------------
 2 files changed, 29 insertions(+), 12 deletions(-)
drivers/gpu/drm/vc4/vc4_drv.h +0 -0
index 4a078ffd9f82..03ed40ab9a93 100644
@@ -924,6 +924,16 @@ struct vc4_validated_shader_info {
 						   (Wmax))
 #define wait_for(COND, MS)		_wait_for((COND), (MS) * 1000, 10, 1000)
 

drivers/gpu/drm/vc4/vc4_drv.h struct vc4_validated_shader_info { · line 936

Quoted 10 lines
drivers/gpu/drm/vc4/vc4_drv.h +10 -0
+static inline unsigned long nsecs_to_jiffies_timeout(const u64 n)
+{
+	/* nsecs_to_jiffies64() does not guard against overflow */
+	if ((NSEC_PER_SEC % HZ) != 0 &&
+	    div_u64(n, NSEC_PER_SEC) >= MAX_JIFFY_OFFSET / HZ)
+		return MAX_JIFFY_OFFSET;
+
+	return min_t(u64, MAX_JIFFY_OFFSET, nsecs_to_jiffies64(n) + 1);
+}
+

Should nsecs_to_jiffies_timeout become common code?

Quoted 43 lines
drivers/gpu/drm/vc4/vc4_drv.h +0 -0
 /* vc4_bo.c */
 struct drm_gem_object *vc4_create_object(struct drm_device *dev, size_t size);
 struct vc4_bo *vc4_bo_create(struct drm_device *dev, size_t size,
drivers/gpu/drm/vc4/vc4_gem.c +11 -11
index 1021f45cb53c..4037c65eb269 100644
@@ -1017,11 +1017,13 @@ int
 vc4_wait_bo_ioctl(struct drm_device *dev, void *data,
 		  struct drm_file *file_priv)
 {
-	struct vc4_dev *vc4 = to_vc4_dev(dev);
 	int ret;
+	struct vc4_dev *vc4 = to_vc4_dev(dev);
 	struct drm_vc4_wait_bo *args = data;
-	struct drm_gem_object *gem_obj;
-	struct vc4_bo *bo;
+	unsigned long timeout_jiffies =
+		nsecs_to_jiffies_timeout(args->timeout_ns);
+	ktime_t start = ktime_get();
+	u64 delta_ns;
 
 	if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
 		return -ENODEV;
@@ -1029,17 +1031,22 @@ vc4_wait_bo_ioctl(struct drm_device *dev, void *data,
 	if (args->pad != 0)
 		return -EINVAL;
 
-	gem_obj = drm_gem_object_lookup(file_priv, args->handle);
-	if (!gem_obj) {
-		DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
-		return -EINVAL;
-	}
-	bo = to_vc4_bo(gem_obj);
+	ret = drm_gem_dma_resv_wait(file_priv, args->handle,
+				    true, timeout_jiffies);
 
-	ret = vc4_wait_for_seqno_ioctl_helper(dev, bo->seqno,
-					      &args->timeout_ns);
+	/* Decrement the user's timeout, in case we got interrupted
+	 * such that the ioctl will be restarted.
+	 */
+	delta_ns = ktime_to_ns(ktime_sub(ktime_get(), start));

drivers/gpu/drm/vc4/vc4_gem.c vc4_wait_bo_ioctl(struct drm_device *dev, void *data, · line 1042

Quoted 10 lines
drivers/gpu/drm/vc4/vc4_gem.c +8 -1
+	if (delta_ns < args->timeout_ns)
+		args->timeout_ns -= delta_ns;
+	else
+		args->timeout_ns = 0;
+
+	/* Asked to wait beyond the jiffy/scheduler precision? */
+	if (ret == -ETIME && args->timeout_ns)
+		ret = -EAGAIN;
 
-	drm_gem_object_put(gem_obj);

Just left some comments above.

This change sounds sensible and the patch LGTM.
Thanks!

Quoted 6 lines
drivers/gpu/drm/vc4/vc4_gem.c +0 -0
 	return ret;
 }
 
Signature · 1 line
2.47.1
#11 lore raw

Re: [PATCH v2 2/4] drm/vc4: Use DMA Resv to implement VC4 wait BO IOCTL

In reply to Maíra Canal
Quoted 37 lines · Maíra Canal

As the BOs used by VC4 have DMA Reservation Objects attached to it, there is no need to use seqnos wait for the BOs availability. Instead, we can use dma_gem_dma_resv_wait().

---
  drivers/gpu/drm/vc4/vc4_drv.h | 10 ++++++++++
  drivers/gpu/drm/vc4/vc4_gem.c | 31 +++++++++++++++++++------------
  2 files changed, 29 insertions(+), 12 deletions(-)
drivers/gpu/drm/vc4/vc4_drv.h +10 -0
index 4a078ffd9f82..03ed40ab9a93 100644
@@ -924,6 +924,16 @@ struct vc4_validated_shader_info {
  						   (Wmax))
  #define wait_for(COND, MS)		_wait_for((COND), (MS) * 1000, 10, 1000)
 
+static inline unsigned long nsecs_to_jiffies_timeout(const u64 n)
+{
+	/* nsecs_to_jiffies64() does not guard against overflow */
+	if ((NSEC_PER_SEC % HZ) != 0 &&
+	    div_u64(n, NSEC_PER_SEC) >= MAX_JIFFY_OFFSET / HZ)
+		return MAX_JIFFY_OFFSET;
+
+	return min_t(u64, MAX_JIFFY_OFFSET, nsecs_to_jiffies64(n) + 1);
+}
+
  /* vc4_bo.c */
  struct drm_gem_object *vc4_create_object(struct drm_device *dev, size_t size);
  struct vc4_bo *vc4_bo_create(struct drm_device *dev, size_t size,
drivers/gpu/drm/vc4/vc4_gem.c +0 -0
index 1021f45cb53c..4037c65eb269 100644
@@ -1017,11 +1017,13 @@ int
  vc4_wait_bo_ioctl(struct drm_device *dev, void *data,

drivers/gpu/drm/vc4/vc4_gem.c int · line 1024

Quoted 10 lines
drivers/gpu/drm/vc4/vc4_gem.c +3 -3
  		  struct drm_file *file_priv)
  {
-	struct vc4_dev *vc4 = to_vc4_dev(dev);
  	int ret;
+	struct vc4_dev *vc4 = to_vc4_dev(dev);
  	struct drm_vc4_wait_bo *args = data;
-	struct drm_gem_object *gem_obj;
-	struct vc4_bo *bo;
+	unsigned long timeout_jiffies =
+		nsecs_to_jiffies_timeout(args->timeout_ns);

What about a potentially simpler alternative:

timeout_jiffies = usecs_to_jiffies(div_u64(args->timeout_ns, 1000));

No need for a new helper and code cannot sleep for less than one jiffy anyway.

Quoted 22 lines
drivers/gpu/drm/vc4/vc4_gem.c +6 -8
+	ktime_t start = ktime_get();
+	u64 delta_ns;
 
  	if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
  		return -ENODEV;
@@ -1029,17 +1031,22 @@ vc4_wait_bo_ioctl(struct drm_device *dev, void *data,
  	if (args->pad != 0)
  		return -EINVAL;
 
-	gem_obj = drm_gem_object_lookup(file_priv, args->handle);
-	if (!gem_obj) {
-		DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
-		return -EINVAL;
-	}
-	bo = to_vc4_bo(gem_obj);
+	ret = drm_gem_dma_resv_wait(file_priv, args->handle,
+				    true, timeout_jiffies);
 
-	ret = vc4_wait_for_seqno_ioctl_helper(dev, bo->seqno,
-					      &args->timeout_ns);
+	/* Decrement the user's timeout, in case we got interrupted
+	 * such that the ioctl will be restarted.

drivers/gpu/drm/vc4/vc4_gem.c vc4_wait_bo_ioctl(struct drm_device *dev, void *data, · line 1048

Quoted 10 lines
drivers/gpu/drm/vc4/vc4_gem.c +10 -0
+	 */
+	delta_ns = ktime_to_ns(ktime_sub(ktime_get(), start));
+	if (delta_ns < args->timeout_ns)
+		args->timeout_ns -= delta_ns;
+	else
+		args->timeout_ns = 0;
+
+	/* Asked to wait beyond the jiffy/scheduler precision? */
+	if (ret == -ETIME && args->timeout_ns)
+		ret = -EAGAIN;

EAGAIN is new from here, right? Will userspace cope in the right way?

Regards,

Tvrtko

Quoted 5 lines
drivers/gpu/drm/vc4/vc4_gem.c +0 -1
-	drm_gem_object_put(gem_obj);
  	return ret;
  }
 
author reviewer
#12 lore raw

Re: [PATCH v2 2/4] drm/vc4: Use DMA Resv to implement VC4 wait BO IOCTL

Hi Tvrtko,

Quoted 51 lines · Tvrtko Ursulin
Quoted 36 lines · Maíra Canal

As the BOs used by VC4 have DMA Reservation Objects attached to it, there is no need to use seqnos wait for the BOs availability. Instead, we can use dma_gem_dma_resv_wait().

---
  drivers/gpu/drm/vc4/vc4_drv.h | 10 ++++++++++ drivers/gpu/drm/vc4/vc4_gem.c | 31 +++++++++++++++++++------------

  2 files changed, 29 insertions(+), 12 deletions(-)
drivers/gpu/drm/vc4/vc4_drv.h +10 -0
index 4a078ffd9f82..03ed40ab9a93 100644
@@ -924,6 +924,16 @@ struct vc4_validated_shader_info {
                              (Wmax))
   #define wait_for(COND, MS)        _wait_for((COND), (MS) * 1000, 10, 1000)
+static inline unsigned long nsecs_to_jiffies_timeout(const u64 n)
+{
+    /* nsecs_to_jiffies64() does not guard against overflow */
+    if ((NSEC_PER_SEC % HZ) != 0 &&
+        div_u64(n, NSEC_PER_SEC) >= MAX_JIFFY_OFFSET / HZ)
+        return MAX_JIFFY_OFFSET;
+
+    return min_t(u64, MAX_JIFFY_OFFSET, nsecs_to_jiffies64(n) + 1);
+}
+
   /* vc4_bo.c */
   struct drm_gem_object *vc4_create_object(struct drm_device *dev, size_t size);
   struct vc4_bo *vc4_bo_create(struct drm_device *dev, size_t size,
drivers/gpu/drm/vc4/vc4_gem.c +0 -0
index 1021f45cb53c..4037c65eb269 100644
@@ -1017,11 +1017,13 @@ int
   vc4_wait_bo_ioctl(struct drm_device *dev, void *data,

drivers/gpu/drm/vc4/vc4_gem.c int · line 1024

Quoted 10 lines
drivers/gpu/drm/vc4/vc4_gem.c +3 -3
             struct drm_file *file_priv)
   {
-    struct vc4_dev *vc4 = to_vc4_dev(dev);
       int ret;
+    struct vc4_dev *vc4 = to_vc4_dev(dev);
       struct drm_vc4_wait_bo *args = data;
-    struct drm_gem_object *gem_obj;
-    struct vc4_bo *bo;
+    unsigned long timeout_jiffies =
+        nsecs_to_jiffies_timeout(args->timeout_ns);

What about a potentially simpler alternative:

Quoted 3 lines

timeout_jiffies = usecs_to_jiffies(div_u64(args->timeout_ns, 1000));

No need for a new helper and code cannot sleep for less than one jiffy anyway.

I ended up deciding to use a function available on "drm_utils.h", drm_timeout_abs_to_jiffies(), which seems to deal appropriately with overflows.

Quoted 25 lines
Quoted 25 lines
drivers/gpu/drm/vc4/vc4_gem.c +10 -8
+    ktime_t start = ktime_get();
+    u64 delta_ns;
       if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
           return -ENODEV;
@@ -1029,17 +1031,22 @@ vc4_wait_bo_ioctl(struct drm_device *dev, void 
 *data,
       if (args->pad != 0)
           return -EINVAL;
-    gem_obj = drm_gem_object_lookup(file_priv, args->handle);
-    if (!gem_obj) {
-        DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
-        return -EINVAL;
-    }
-    bo = to_vc4_bo(gem_obj);
+    ret = drm_gem_dma_resv_wait(file_priv, args->handle,
+                    true, timeout_jiffies);
-    ret = vc4_wait_for_seqno_ioctl_helper(dev, bo->seqno,
-                          &args->timeout_ns);
+    /* Decrement the user's timeout, in case we got interrupted
+     * such that the ioctl will be restarted.
+     */
+    delta_ns = ktime_to_ns(ktime_sub(ktime_get(), start));
+    if (delta_ns < args->timeout_ns)
+        args->timeout_ns -= delta_ns;
Quoted 8 lines

drivers/gpu/drm/vc4/vc4_gem.c vc4_wait_bo_ioctl(struct drm_device *dev, void *data, · line 1048

Quoted 6 lines
drivers/gpu/drm/vc4/vc4_gem.c +6 -0
+    else
+        args->timeout_ns = 0;
+
+    /* Asked to wait beyond the jiffy/scheduler precision? */
+    if (ret == -ETIME && args->timeout_ns)
+        ret = -EAGAIN;

EAGAIN is new from here, right? Will userspace cope in the right way?

Thanks for noticing! I'll address it in the next version.

Best Regards,
- Maíra

Quoted 8 lines

Regards,

Tvrtko

Quoted 3 lines
drivers/gpu/drm/vc4/vc4_gem.c +0 -1
-    drm_gem_object_put(gem_obj);
       return ret;
   }
#13 lore raw

Re: [PATCH v2 2/4] drm/vc4: Use DMA Resv to implement VC4 wait BO IOCTL

Quoted 54 lines · Maíra Canal

Hi Tvrtko,

Quoted 51 lines · Tvrtko Ursulin
Quoted 36 lines · Maíra Canal

As the BOs used by VC4 have DMA Reservation Objects attached to it, there is no need to use seqnos wait for the BOs availability. Instead, we can use dma_gem_dma_resv_wait().

---
  drivers/gpu/drm/vc4/vc4_drv.h | 10 ++++++++++ drivers/gpu/drm/vc4/vc4_gem.c | 31 +++++++++++++++++++------------

  2 files changed, 29 insertions(+), 12 deletions(-)
drivers/gpu/drm/vc4/vc4_drv.h +10 -0
index 4a078ffd9f82..03ed40ab9a93 100644
@@ -924,6 +924,16 @@ struct vc4_validated_shader_info {
                              (Wmax))
   #define wait_for(COND, MS)        _wait_for((COND), (MS) * 1000, 10, 1000)
+static inline unsigned long nsecs_to_jiffies_timeout(const u64 n)
+{
+    /* nsecs_to_jiffies64() does not guard against overflow */
+    if ((NSEC_PER_SEC % HZ) != 0 &&
+        div_u64(n, NSEC_PER_SEC) >= MAX_JIFFY_OFFSET / HZ)
+        return MAX_JIFFY_OFFSET;
+
+    return min_t(u64, MAX_JIFFY_OFFSET, nsecs_to_jiffies64(n) + 1);
+}
+
   /* vc4_bo.c */
   struct drm_gem_object *vc4_create_object(struct drm_device *dev, size_t size);
   struct vc4_bo *vc4_bo_create(struct drm_device *dev, size_t size,
drivers/gpu/drm/vc4/vc4_gem.c +0 -0
index 1021f45cb53c..4037c65eb269 100644
@@ -1017,11 +1017,13 @@ int
   vc4_wait_bo_ioctl(struct drm_device *dev, void *data,

drivers/gpu/drm/vc4/vc4_gem.c int · line 1024

Quoted 10 lines
drivers/gpu/drm/vc4/vc4_gem.c +3 -3
             struct drm_file *file_priv)
   {
-    struct vc4_dev *vc4 = to_vc4_dev(dev);
       int ret;
+    struct vc4_dev *vc4 = to_vc4_dev(dev);
       struct drm_vc4_wait_bo *args = data;
-    struct drm_gem_object *gem_obj;
-    struct vc4_bo *bo;
+    unsigned long timeout_jiffies =
+        nsecs_to_jiffies_timeout(args->timeout_ns);

What about a potentially simpler alternative:

Quoted 7 lines
Quoted 3 lines

timeout_jiffies = usecs_to_jiffies(div_u64(args->timeout_ns, 1000));

No need for a new helper and code cannot sleep for less than one jiffy anyway.

I ended up deciding to use a function available on "drm_utils.h", drm_timeout_abs_to_jiffies(), which seems to deal appropriately with overflows.

You will convert relative to absolute to be able to use it? At that point usecs_to_jiffies() might still be more elegant.

Regards,

Tvrtko

Quoted 49 lines
Quoted 26 lines
Quoted 26 lines
drivers/gpu/drm/vc4/vc4_gem.c +10 -8
+    ktime_t start = ktime_get();
+    u64 delta_ns;
       if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
           return -ENODEV;
@@ -1029,17 +1031,22 @@ vc4_wait_bo_ioctl(struct drm_device *dev, 
 void *data,
       if (args->pad != 0)
           return -EINVAL;
-    gem_obj = drm_gem_object_lookup(file_priv, args->handle);
-    if (!gem_obj) {
-        DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
-        return -EINVAL;
-    }
-    bo = to_vc4_bo(gem_obj);
+    ret = drm_gem_dma_resv_wait(file_priv, args->handle,
+                    true, timeout_jiffies);
-    ret = vc4_wait_for_seqno_ioctl_helper(dev, bo->seqno,
-                          &args->timeout_ns);
+    /* Decrement the user's timeout, in case we got interrupted
+     * such that the ioctl will be restarted.
+     */
+    delta_ns = ktime_to_ns(ktime_sub(ktime_get(), start));
+    if (delta_ns < args->timeout_ns)
+        args->timeout_ns -= delta_ns;
Quoted 8 lines

drivers/gpu/drm/vc4/vc4_gem.c vc4_wait_bo_ioctl(struct drm_device *dev, void *data, · line 1048

Quoted 6 lines
drivers/gpu/drm/vc4/vc4_gem.c +6 -0
+    else
+        args->timeout_ns = 0;
+
+    /* Asked to wait beyond the jiffy/scheduler precision? */
+    if (ret == -ETIME && args->timeout_ns)
+        ret = -EAGAIN;

EAGAIN is new from here, right? Will userspace cope in the right way?

Thanks for noticing! I'll address it in the next version.

Best Regards,
- Maíra

Quoted 8 lines

Regards,

Tvrtko

Quoted 3 lines
drivers/gpu/drm/vc4/vc4_gem.c +0 -1
-    drm_gem_object_put(gem_obj);
       return ret;
   }
author reviewer
#14 lore raw

PATCH v2 3/4 drm/vc4: Remove BOs seqnos

In reply to Maíra Canal

bo->seqno, bo->write_seqno, and exec->bin_dep_seqno are leftovers from a time when VC4 didn't support DMA Reservation Objects. When they were introduced, it made sense to think about tracking the correspondence between the BOs and the jobs through the job's seqno.

This is no longer needed, as VC4 already has support for DMA Reservation Objects and attaches the "job done" fence to the BOs.

---
 drivers/gpu/drm/vc4/vc4_crtc.c     | 22 +++++++++++-----------
 drivers/gpu/drm/vc4/vc4_drv.h      | 13 -------------
 drivers/gpu/drm/vc4/vc4_gem.c      | 18 ++----------------
 drivers/gpu/drm/vc4/vc4_validate.c | 11 -----------
 4 files changed, 13 insertions(+), 51 deletions(-)
drivers/gpu/drm/vc4/vc4_crtc.c +11 -11
index cf40a53ad42e..3a5b5743cb2f 100644
@@ -919,10 +919,11 @@ vc4_async_page_flip_complete(struct vc4_async_flip_state *flip_state)
 	kfree(flip_state);
 }
 
-static void vc4_async_page_flip_seqno_complete(struct vc4_seqno_cb *cb)
+static void vc4_async_page_flip_seqno_complete(struct dma_fence *fence,
+					       struct dma_fence_cb *cb)
 {
 	struct vc4_async_flip_state *flip_state =
-		container_of(cb, struct vc4_async_flip_state, cb.seqno);
+		container_of(cb, struct vc4_async_flip_state, cb.fence);
 	struct vc4_bo *bo = NULL;
 
 	if (flip_state->old_fb) {
@@ -961,16 +962,15 @@ static int vc4_async_set_fence_cb(struct drm_device *dev,
 {
 	struct drm_framebuffer *fb = flip_state->fb;
 	struct drm_gem_dma_object *dma_bo = drm_fb_dma_get_gem_obj(fb, 0);
+	dma_fence_func_t async_page_flip_complete_function;
 	struct vc4_dev *vc4 = to_vc4_dev(dev);
 	struct dma_fence *fence;
 	int ret;
 
-	if (vc4->gen == VC4_GEN_4) {
-		struct vc4_bo *bo = to_vc4_bo(&dma_bo->base);
-
-		return vc4_queue_seqno_cb(dev, &flip_state->cb.seqno, bo->seqno,
-					  vc4_async_page_flip_seqno_complete);
-	}
+	if (vc4->gen == VC4_GEN_4)
+		async_page_flip_complete_function = vc4_async_page_flip_seqno_complete;
+	else
+		async_page_flip_complete_function = vc4_async_page_flip_fence_complete;
 
 	ret = dma_resv_get_singleton(dma_bo->base.resv, DMA_RESV_USAGE_READ, &fence);
 	if (ret)
@@ -978,14 +978,14 @@ static int vc4_async_set_fence_cb(struct drm_device *dev,
 
 	/* If there's no fence, complete the page flip immediately */
 	if (!fence) {
-		vc4_async_page_flip_fence_complete(fence, &flip_state->cb.fence);
+		async_page_flip_complete_function(fence, &flip_state->cb.fence);
 		return 0;
 	}
 
 	/* If the fence has already been completed, complete the page flip */
 	if (dma_fence_add_callback(fence, &flip_state->cb.fence,
-				   vc4_async_page_flip_fence_complete))
-		vc4_async_page_flip_fence_complete(fence, &flip_state->cb.fence);
+				   async_page_flip_complete_function))
+		async_page_flip_complete_function(fence, &flip_state->cb.fence);
 
 	return 0;
 }
drivers/gpu/drm/vc4/vc4_drv.h +0 -13
index 03ed40ab9a93..ff8048991030 100644
@@ -247,16 +247,6 @@ struct vc4_dev {
 struct vc4_bo {
 	struct drm_gem_dma_object base;
 
-	/* seqno of the last job to render using this BO. */
-	uint64_t seqno;
-
-	/* seqno of the last job to use the RCL to write to this BO.
-	 *
-	 * Note that this doesn't include binner overflow memory
-	 * writes.
-	 */
-	uint64_t write_seqno;
-
 	bool t_format;
 
 	/* List entry for the BO's position in either
@@ -695,9 +685,6 @@ struct vc4_exec_info {
 	/* Sequence number for this bin/render job. */
 	uint64_t seqno;
 
-	/* Latest write_seqno of any BO that binning depends on. */
-	uint64_t bin_dep_seqno;
-
 	struct dma_fence *fence;
 
 	/* Last current addresses the hardware was processing when the
drivers/gpu/drm/vc4/vc4_gem.c +2 -16
index 4037c65eb269..1cbd95c4f9ef 100644
@@ -553,27 +553,19 @@ vc4_move_job_to_render(struct drm_device *dev, struct vc4_exec_info *exec)
 }
 
 static void
-vc4_update_bo_seqnos(struct vc4_exec_info *exec, uint64_t seqno)
+vc4_attach_fences(struct vc4_exec_info *exec)
 {
 	struct vc4_bo *bo;
 	unsigned i;
 
 	for (i = 0; i < exec->bo_count; i++) {
 		bo = to_vc4_bo(exec->bo[i]);
-		bo->seqno = seqno;
-
 		dma_resv_add_fence(bo->base.base.resv, exec->fence,
 				   DMA_RESV_USAGE_READ);
 	}
 
-	list_for_each_entry(bo, &exec->unref_list, unref_head) {
-		bo->seqno = seqno;
-	}
-
 	for (i = 0; i < exec->rcl_write_bo_count; i++) {
 		bo = to_vc4_bo(&exec->rcl_write_bo[i]->base);
-		bo->write_seqno = seqno;
-
 		dma_resv_add_fence(bo->base.base.resv, exec->fence,
 				   DMA_RESV_USAGE_WRITE);
 	}
@@ -647,7 +639,7 @@ vc4_queue_submit(struct drm_device *dev, struct vc4_exec_info *exec,
 	if (out_sync)
 		drm_syncobj_replace_fence(out_sync, exec->fence);
 
-	vc4_update_bo_seqnos(exec, seqno);
+	vc4_attach_fences(exec);
 
 	drm_exec_fini(exec_ctx);
 
@@ -845,12 +837,6 @@ vc4_get_bcl(struct drm_device *dev, struct vc4_exec_info *exec)
 			goto fail;
 	}
 
-	/* Block waiting on any previous rendering into the CS's VBO,
-	 * IB, or textures, so that pixels are actually written by the
-	 * time we try to read them.
-	 */
-	ret = vc4_wait_for_seqno(dev, exec->bin_dep_seqno, ~0ull, true);
-
 fail:
 	kvfree(temp);
 	return ret;
drivers/gpu/drm/vc4/vc4_validate.c +0 -11
index 5bf134968ade..1e7bdda55698 100644
@@ -284,9 +284,6 @@ validate_indexed_prim_list(VALIDATE_ARGS)
 	if (!ib)
 		return -EINVAL;
 
-	exec->bin_dep_seqno = max(exec->bin_dep_seqno,
-				  to_vc4_bo(&ib->base)->write_seqno);
-
 	if (offset > ib->base.size ||
 	    (ib->base.size - offset) / index_size < length) {
 		DRM_DEBUG("IB access overflow (%d + %d*%d > %zd)\n",
@@ -738,11 +735,6 @@ reloc_tex(struct vc4_exec_info *exec,
 
 	*validated_p0 = tex->dma_addr + p0;
 
-	if (is_cs) {
-		exec->bin_dep_seqno = max(exec->bin_dep_seqno,
-					  to_vc4_bo(&tex->base)->write_seqno);
-	}
-
 	return true;
  fail:
 	DRM_INFO("Texture p0 at %d: 0x%08x\n", sample->p_offset[0], p0);
@@ -904,9 +896,6 @@ validate_gl_shader_rec(struct drm_device *dev,
 		uint32_t stride = *(uint8_t *)(pkt_u + o + 5);
 		uint32_t max_index;
 
-		exec->bin_dep_seqno = max(exec->bin_dep_seqno,
-					  to_vc4_bo(&vbo->base)->write_seqno);
-
 		if (state->addr & 0x8)
 			stride |= (*(uint32_t *)(pkt_u + 100 + i * 4)) & ~0xff;
 
Signature · 1 line
2.47.1
#15 lore raw

Re: [PATCH v2 3/4] drm/vc4: Remove BOs seqnos

Quoted 21 lines · Maíra Canal

bo->seqno, bo->write_seqno, and exec->bin_dep_seqno are leftovers from a time when VC4 didn't support DMA Reservation Objects. When they were introduced, it made sense to think about tracking the correspondence between the BOs and the jobs through the job's seqno.

This is no longer needed, as VC4 already has support for DMA Reservation Objects and attaches the "job done" fence to the BOs.

---
 drivers/gpu/drm/vc4/vc4_crtc.c     | 22 +++++++++++-----------
 drivers/gpu/drm/vc4/vc4_drv.h      | 13 -------------
 drivers/gpu/drm/vc4/vc4_gem.c      | 18 ++----------------
 drivers/gpu/drm/vc4/vc4_validate.c | 11 -----------
 4 files changed, 13 insertions(+), 51 deletions(-)
drivers/gpu/drm/vc4/vc4_crtc.c +0 -0
index cf40a53ad42e..3a5b5743cb2f 100644

drivers/gpu/drm/vc4/vc4_crtc.c vc4_async_page_flip_complete(struct vc4_async_flip_state *flip_state) · line 926

Quoted 10 lines
drivers/gpu/drm/vc4/vc4_crtc.c +3 -2
@@ -919,10 +919,11 @@ vc4_async_page_flip_complete(struct vc4_async_flip_state *flip_state)
 	kfree(flip_state);
 }
 
-static void vc4_async_page_flip_seqno_complete(struct vc4_seqno_cb *cb)
+static void vc4_async_page_flip_seqno_complete(struct dma_fence *fence,
+					       struct dma_fence_cb *cb)
 {
 	struct vc4_async_flip_state *flip_state =
-		container_of(cb, struct vc4_async_flip_state, cb.seqno);
+		container_of(cb, struct vc4_async_flip_state, cb.fence);

hmm... I didn't get why you still call this function vc4_async_page_flip_seqno_complete if you are not using seqno anymore.

On top of that, can we just use vc4_async_page_flip_fence_complete instead? I mean, looks like we don't need two different async page flip paths according to the hardware anymore.

Quoted 117 lines
drivers/gpu/drm/vc4/vc4_crtc.c +8 -9
 	struct vc4_bo *bo = NULL;
 
 	if (flip_state->old_fb) {
@@ -961,16 +962,15 @@ static int vc4_async_set_fence_cb(struct drm_device *dev,
 {
 	struct drm_framebuffer *fb = flip_state->fb;
 	struct drm_gem_dma_object *dma_bo = drm_fb_dma_get_gem_obj(fb, 0);
+	dma_fence_func_t async_page_flip_complete_function;
 	struct vc4_dev *vc4 = to_vc4_dev(dev);
 	struct dma_fence *fence;
 	int ret;
 
-	if (vc4->gen == VC4_GEN_4) {
-		struct vc4_bo *bo = to_vc4_bo(&dma_bo->base);
-
-		return vc4_queue_seqno_cb(dev, &flip_state->cb.seqno, bo->seqno,
-					  vc4_async_page_flip_seqno_complete);
-	}
+	if (vc4->gen == VC4_GEN_4)
+		async_page_flip_complete_function = vc4_async_page_flip_seqno_complete;
+	else
+		async_page_flip_complete_function = vc4_async_page_flip_fence_complete;
 
 	ret = dma_resv_get_singleton(dma_bo->base.resv, DMA_RESV_USAGE_READ, &fence);
 	if (ret)
@@ -978,14 +978,14 @@ static int vc4_async_set_fence_cb(struct drm_device *dev,
 
 	/* If there's no fence, complete the page flip immediately */
 	if (!fence) {
-		vc4_async_page_flip_fence_complete(fence, &flip_state->cb.fence);
+		async_page_flip_complete_function(fence, &flip_state->cb.fence);
 		return 0;
 	}
 
 	/* If the fence has already been completed, complete the page flip */
 	if (dma_fence_add_callback(fence, &flip_state->cb.fence,
-				   vc4_async_page_flip_fence_complete))
-		vc4_async_page_flip_fence_complete(fence, &flip_state->cb.fence);
+				   async_page_flip_complete_function))
+		async_page_flip_complete_function(fence, &flip_state->cb.fence);
 
 	return 0;
 }
drivers/gpu/drm/vc4/vc4_drv.h +0 -13
index 03ed40ab9a93..ff8048991030 100644
@@ -247,16 +247,6 @@ struct vc4_dev {
 struct vc4_bo {
 	struct drm_gem_dma_object base;
 
-	/* seqno of the last job to render using this BO. */
-	uint64_t seqno;
-
-	/* seqno of the last job to use the RCL to write to this BO.
-	 *
-	 * Note that this doesn't include binner overflow memory
-	 * writes.
-	 */
-	uint64_t write_seqno;
-
 	bool t_format;
 
 	/* List entry for the BO's position in either
@@ -695,9 +685,6 @@ struct vc4_exec_info {
 	/* Sequence number for this bin/render job. */
 	uint64_t seqno;
 
-	/* Latest write_seqno of any BO that binning depends on. */
-	uint64_t bin_dep_seqno;
-
 	struct dma_fence *fence;
 
 	/* Last current addresses the hardware was processing when the
drivers/gpu/drm/vc4/vc4_gem.c +2 -10
index 4037c65eb269..1cbd95c4f9ef 100644
@@ -553,27 +553,19 @@ vc4_move_job_to_render(struct drm_device *dev, struct vc4_exec_info *exec)
 }
 
 static void
-vc4_update_bo_seqnos(struct vc4_exec_info *exec, uint64_t seqno)
+vc4_attach_fences(struct vc4_exec_info *exec)
 {
 	struct vc4_bo *bo;
 	unsigned i;
 
 	for (i = 0; i < exec->bo_count; i++) {
 		bo = to_vc4_bo(exec->bo[i]);
-		bo->seqno = seqno;
-
 		dma_resv_add_fence(bo->base.base.resv, exec->fence,
 				   DMA_RESV_USAGE_READ);
 	}
 
-	list_for_each_entry(bo, &exec->unref_list, unref_head) {
-		bo->seqno = seqno;
-	}
-
 	for (i = 0; i < exec->rcl_write_bo_count; i++) {
 		bo = to_vc4_bo(&exec->rcl_write_bo[i]->base);
-		bo->write_seqno = seqno;
-
 		dma_resv_add_fence(bo->base.base.resv, exec->fence,
 				   DMA_RESV_USAGE_WRITE);
 	}
@@ -647,7 +639,7 @@ vc4_queue_submit(struct drm_device *dev, struct vc4_exec_info *exec,
 	if (out_sync)
 		drm_syncobj_replace_fence(out_sync, exec->fence);
 
-	vc4_update_bo_seqnos(exec, seqno);
+	vc4_attach_fences(exec);
 
 	drm_exec_fini(exec_ctx);
 

drivers/gpu/drm/vc4/vc4_gem.c vc4_get_bcl(struct drm_device *dev, struct vc4_exec_info *exec) · line 852

Quoted 8 lines
drivers/gpu/drm/vc4/vc4_gem.c +0 -5
@@ -845,12 +837,6 @@ vc4_get_bcl(struct drm_device *dev, struct vc4_exec_info *exec)
 			goto fail;
 	}
 
-	/* Block waiting on any previous rendering into the CS's VBO,
-	 * IB, or textures, so that pixels are actually written by the
-	 * time we try to read them.
-	 */
-	ret = vc4_wait_for_seqno(dev, exec->bin_dep_seqno, ~0ull, true);

Don't we still need waiting for fences here?

Quoted 43 lines
drivers/gpu/drm/vc4/vc4_gem.c +0 -1
-
 fail:
 	kvfree(temp);
 	return ret;
drivers/gpu/drm/vc4/vc4_validate.c +0 -11
index 5bf134968ade..1e7bdda55698 100644
@@ -284,9 +284,6 @@ validate_indexed_prim_list(VALIDATE_ARGS)
 	if (!ib)
 		return -EINVAL;
 
-	exec->bin_dep_seqno = max(exec->bin_dep_seqno,
-				  to_vc4_bo(&ib->base)->write_seqno);
-
 	if (offset > ib->base.size ||
 	    (ib->base.size - offset) / index_size < length) {
 		DRM_DEBUG("IB access overflow (%d + %d*%d > %zd)\n",
@@ -738,11 +735,6 @@ reloc_tex(struct vc4_exec_info *exec,
 
 	*validated_p0 = tex->dma_addr + p0;
 
-	if (is_cs) {
-		exec->bin_dep_seqno = max(exec->bin_dep_seqno,
-					  to_vc4_bo(&tex->base)->write_seqno);
-	}
-
 	return true;
  fail:
 	DRM_INFO("Texture p0 at %d: 0x%08x\n", sample->p_offset[0], p0);
@@ -904,9 +896,6 @@ validate_gl_shader_rec(struct drm_device *dev,
 		uint32_t stride = *(uint8_t *)(pkt_u + o + 5);
 		uint32_t max_index;
 
-		exec->bin_dep_seqno = max(exec->bin_dep_seqno,
-					  to_vc4_bo(&vbo->base)->write_seqno);
-
 		if (state->addr & 0x8)
 			stride |= (*(uint32_t *)(pkt_u + 100 + i * 4)) & ~0xff;
 
Signature · 1 line
2.47.1
author reviewer
#16 lore raw

Re: [PATCH v2 3/4] drm/vc4: Remove BOs seqnos

Hi Melissa,

Thanks for your review!

Quoted 25 lines · Melissa Wen
Quoted 24 lines · Maíra Canal

bo->seqno, bo->write_seqno, and exec->bin_dep_seqno are leftovers from a time when VC4 didn't support DMA Reservation Objects. When they were introduced, it made sense to think about tracking the correspondence between the BOs and the jobs through the job's seqno.

This is no longer needed, as VC4 already has support for DMA Reservation Objects and attaches the "job done" fence to the BOs.

---
  drivers/gpu/drm/vc4/vc4_crtc.c     | 22 +++++++++++-----------
  drivers/gpu/drm/vc4/vc4_drv.h      | 13 -------------
  drivers/gpu/drm/vc4/vc4_gem.c      | 18 ++----------------
  drivers/gpu/drm/vc4/vc4_validate.c | 11 -----------
  4 files changed, 13 insertions(+), 51 deletions(-)
drivers/gpu/drm/vc4/vc4_crtc.c +0 -0
index cf40a53ad42e..3a5b5743cb2f 100644
@@ -919,10 +919,11 @@ vc4_async_page_flip_complete(struct vc4_async_flip_state *flip_state)
  	kfree(flip_state);
  }
 
Quoted 10 lines

drivers/gpu/drm/vc4/vc4_crtc.c vc4_async_page_flip_complete(struct vc4_async_flip_state *flip_state) · line 926

Quoted 7 lines
drivers/gpu/drm/vc4/vc4_crtc.c +3 -2
-static void vc4_async_page_flip_seqno_complete(struct vc4_seqno_cb *cb)
+static void vc4_async_page_flip_seqno_complete(struct dma_fence *fence,
+					       struct dma_fence_cb *cb)
  {
  	struct vc4_async_flip_state *flip_state =
-		container_of(cb, struct vc4_async_flip_state, cb.seqno);
+		container_of(cb, struct vc4_async_flip_state, cb.fence);

hmm... I didn't get why you still call this function vc4_async_page_flip_seqno_complete if you are not using seqno anymore.

I'll rename it. Thanks!

Quoted 4 lines

On top of that, can we just use vc4_async_page_flip_fence_complete instead? I mean, looks like we don't need two different async page flip paths according to the hardware anymore.

vc4_async_page_flip_seqno_complete() updates the BO usecnt, which is needed for VC4 (GEN == 4). Maxime, Dave, could you confirm that this usecnt logic is still needed?

Quoted 44 lines
Quoted 44 lines
drivers/gpu/drm/vc4/vc4_crtc.c +8 -9
  	struct vc4_bo *bo = NULL;
 
  	if (flip_state->old_fb) {
@@ -961,16 +962,15 @@ static int vc4_async_set_fence_cb(struct drm_device *dev,
  {
  	struct drm_framebuffer *fb = flip_state->fb;
  	struct drm_gem_dma_object *dma_bo = drm_fb_dma_get_gem_obj(fb, 0);
+	dma_fence_func_t async_page_flip_complete_function;
  	struct vc4_dev *vc4 = to_vc4_dev(dev);
  	struct dma_fence *fence;
  	int ret;
 
-	if (vc4->gen == VC4_GEN_4) {
-		struct vc4_bo *bo = to_vc4_bo(&dma_bo->base);
-
-		return vc4_queue_seqno_cb(dev, &flip_state->cb.seqno, bo->seqno,
-					  vc4_async_page_flip_seqno_complete);
-	}
+	if (vc4->gen == VC4_GEN_4)
+		async_page_flip_complete_function = vc4_async_page_flip_seqno_complete;
+	else
+		async_page_flip_complete_function = vc4_async_page_flip_fence_complete;
 
  	ret = dma_resv_get_singleton(dma_bo->base.resv, DMA_RESV_USAGE_READ, &fence);
  	if (ret)
@@ -978,14 +978,14 @@ static int vc4_async_set_fence_cb(struct drm_device *dev,
 
  	/* If there's no fence, complete the page flip immediately */
  	if (!fence) {
-		vc4_async_page_flip_fence_complete(fence, &flip_state->cb.fence);
+		async_page_flip_complete_function(fence, &flip_state->cb.fence);
  		return 0;
  	}
 
  	/* If the fence has already been completed, complete the page flip */
  	if (dma_fence_add_callback(fence, &flip_state->cb.fence,
-				   vc4_async_page_flip_fence_complete))
-		vc4_async_page_flip_fence_complete(fence, &flip_state->cb.fence);
+				   async_page_flip_complete_function))
+		async_page_flip_complete_function(fence, &flip_state->cb.fence);
 
  	return 0;
  }

[...]

Quoted 4 lines
Quoted 4 lines
drivers/gpu/drm/vc4/vc4_gem.c +0 -0
index 4037c65eb269..1cbd95c4f9ef 100644

[...]

Quoted 4 lines
Quoted 4 lines
drivers/gpu/drm/vc4/vc4_gem.c +0 -0
@@ -845,12 +837,6 @@ vc4_get_bcl(struct drm_device *dev, struct vc4_exec_info *exec)
  			goto fail;
  	}
 
Quoted 8 lines

drivers/gpu/drm/vc4/vc4_gem.c vc4_get_bcl(struct drm_device *dev, struct vc4_exec_info *exec) · line 852

Quoted 5 lines
drivers/gpu/drm/vc4/vc4_gem.c +0 -5
-	/* Block waiting on any previous rendering into the CS's VBO,
-	 * IB, or textures, so that pixels are actually written by the
-	 * time we try to read them.
-	 */
-	ret = vc4_wait_for_seqno(dev, exec->bin_dep_seqno, ~0ull, true);

Don't we still need waiting for fences here?

As we indicate the implicit DMA reservation usage through the enum dma_resv_usage flag, I believe we can be safe to assume that the BOs won't be read before we finish writing.

Best Regards,
- Maíra

Quoted 3 lines
Quoted 3 lines
Signature · 1 line
2.47.1
author reviewer
#17 lore raw

PATCH v2 4/4 drm/vc4: Remove `vc4_queue_seqno_cb()`

In reply to Maíra Canal

The function vc4_queue_seqno_cb() is no longer needed, as we are using DMA Reservation Objects to track BOs. Using DMA Resv, we can use dma_fence_add_callback() to perform the async page flip.

---
 drivers/gpu/drm/vc4/vc4_drv.h |  3 ---
 drivers/gpu/drm/vc4/vc4_gem.c | 32 --------------------------------
 2 files changed, 35 deletions(-)
drivers/gpu/drm/vc4/vc4_drv.h +0 -3
index ff8048991030..7e23363d008f 100644
@@ -1022,9 +1022,6 @@ void vc4_move_job_to_render(struct drm_device *dev, struct vc4_exec_info *exec);
 int vc4_wait_for_seqno(struct drm_device *dev, uint64_t seqno,
 		       uint64_t timeout_ns, bool interruptible);
 void vc4_job_handle_completed(struct vc4_dev *vc4);
-int vc4_queue_seqno_cb(struct drm_device *dev,
-		       struct vc4_seqno_cb *cb, uint64_t seqno,
-		       void (*func)(struct vc4_seqno_cb *cb));
 int vc4_gem_madvise_ioctl(struct drm_device *dev, void *data,
 			  struct drm_file *file_priv);
 
drivers/gpu/drm/vc4/vc4_gem.c +0 -32
index 1cbd95c4f9ef..c1cc942a26ae 100644
@@ -922,38 +922,6 @@ vc4_job_handle_completed(struct vc4_dev *vc4)
 	spin_unlock_irqrestore(&vc4->job_lock, irqflags);
 }
 
-static void vc4_seqno_cb_work(struct work_struct *work)
-{
-	struct vc4_seqno_cb *cb = container_of(work, struct vc4_seqno_cb, work);
-
-	cb->func(cb);
-}
-
-int vc4_queue_seqno_cb(struct drm_device *dev,
-		       struct vc4_seqno_cb *cb, uint64_t seqno,
-		       void (*func)(struct vc4_seqno_cb *cb))
-{
-	struct vc4_dev *vc4 = to_vc4_dev(dev);
-	unsigned long irqflags;
-
-	if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
-		return -ENODEV;
-
-	cb->func = func;
-	INIT_WORK(&cb->work, vc4_seqno_cb_work);
-
-	spin_lock_irqsave(&vc4->job_lock, irqflags);
-	if (seqno > vc4->finished_seqno) {
-		cb->seqno = seqno;
-		list_add_tail(&cb->work.entry, &vc4->seqno_cb_list);
-	} else {
-		schedule_work(&cb->work);
-	}
-	spin_unlock_irqrestore(&vc4->job_lock, irqflags);
-
-	return 0;
-}
-
 /* Scheduled when any job has been completed, this walks the list of
  * jobs that had completed and unrefs their BOs and frees their exec
  * structs.
Signature · 1 line
2.47.1
#18 lore raw

Re: [PATCH v2 4/4] drm/vc4: Remove `vc4_queue_seqno_cb()`

Quoted 54 lines · Maíra Canal

The function vc4_queue_seqno_cb() is no longer needed, as we are using DMA Reservation Objects to track BOs. Using DMA Resv, we can use dma_fence_add_callback() to perform the async page flip.

---
 drivers/gpu/drm/vc4/vc4_drv.h |  3 ---
 drivers/gpu/drm/vc4/vc4_gem.c | 32 --------------------------------
 2 files changed, 35 deletions(-)
drivers/gpu/drm/vc4/vc4_drv.h +0 -3
index ff8048991030..7e23363d008f 100644
@@ -1022,9 +1022,6 @@ void vc4_move_job_to_render(struct drm_device *dev, struct vc4_exec_info *exec);
 int vc4_wait_for_seqno(struct drm_device *dev, uint64_t seqno,
 		       uint64_t timeout_ns, bool interruptible);
 void vc4_job_handle_completed(struct vc4_dev *vc4);
-int vc4_queue_seqno_cb(struct drm_device *dev,
-		       struct vc4_seqno_cb *cb, uint64_t seqno,
-		       void (*func)(struct vc4_seqno_cb *cb));
 int vc4_gem_madvise_ioctl(struct drm_device *dev, void *data,
 			  struct drm_file *file_priv);
 
drivers/gpu/drm/vc4/vc4_gem.c +0 -22
index 1cbd95c4f9ef..c1cc942a26ae 100644
@@ -922,38 +922,6 @@ vc4_job_handle_completed(struct vc4_dev *vc4)
 	spin_unlock_irqrestore(&vc4->job_lock, irqflags);
 }
 
-static void vc4_seqno_cb_work(struct work_struct *work)
-{
-	struct vc4_seqno_cb *cb = container_of(work, struct vc4_seqno_cb, work);
-
-	cb->func(cb);
-}
-
-int vc4_queue_seqno_cb(struct drm_device *dev,
-		       struct vc4_seqno_cb *cb, uint64_t seqno,
-		       void (*func)(struct vc4_seqno_cb *cb))
-{
-	struct vc4_dev *vc4 = to_vc4_dev(dev);
-	unsigned long irqflags;
-
-	if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
-		return -ENODEV;
-
-	cb->func = func;
-	INIT_WORK(&cb->work, vc4_seqno_cb_work);
-
-	spin_lock_irqsave(&vc4->job_lock, irqflags);
-	if (seqno > vc4->finished_seqno) {

drivers/gpu/drm/vc4/vc4_gem.c vc4_job_handle_completed(struct vc4_dev *vc4) · line 956

Quoted 10 lines
drivers/gpu/drm/vc4/vc4_gem.c +0 -10
-		cb->seqno = seqno;
-		list_add_tail(&cb->work.entry, &vc4->seqno_cb_list);
-	} else {
-		schedule_work(&cb->work);
-	}
-	spin_unlock_irqrestore(&vc4->job_lock, irqflags);
-
-	return 0;
-}
-

I think squashing this patch with the previous one is more consistent, since the last and only usage of vc4_queue_seqno_cb() is removed there.

Melissa

Quoted 6 lines
drivers/gpu/drm/vc4/vc4_gem.c +0 -0
 /* Scheduled when any job has been completed, this walks the list of
  * jobs that had completed and unrefs their BOs and frees their exec
  * structs.
Signature · 1 line
2.47.1
#19 lore raw

Re: [PATCH v2 4/4] drm/vc4: Remove `vc4_queue_seqno_cb()`

In reply to Maíra Canal
Quoted 48 lines · Maíra Canal

The function vc4_queue_seqno_cb() is no longer needed, as we are using DMA Reservation Objects to track BOs. Using DMA Resv, we can use dma_fence_add_callback() to perform the async page flip.

---
  drivers/gpu/drm/vc4/vc4_drv.h |  3 ---
  drivers/gpu/drm/vc4/vc4_gem.c | 32 --------------------------------
  2 files changed, 35 deletions(-)
drivers/gpu/drm/vc4/vc4_drv.h +0 -3
index ff8048991030..7e23363d008f 100644
@@ -1022,9 +1022,6 @@ void vc4_move_job_to_render(struct drm_device *dev, struct vc4_exec_info *exec);
  int vc4_wait_for_seqno(struct drm_device *dev, uint64_t seqno,
  		       uint64_t timeout_ns, bool interruptible);
  void vc4_job_handle_completed(struct vc4_dev *vc4);
-int vc4_queue_seqno_cb(struct drm_device *dev,
-		       struct vc4_seqno_cb *cb, uint64_t seqno,
-		       void (*func)(struct vc4_seqno_cb *cb));
  int vc4_gem_madvise_ioctl(struct drm_device *dev, void *data,
  			  struct drm_file *file_priv);
 
drivers/gpu/drm/vc4/vc4_gem.c +0 -16
index 1cbd95c4f9ef..c1cc942a26ae 100644
@@ -922,38 +922,6 @@ vc4_job_handle_completed(struct vc4_dev *vc4)
  	spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  }
 
-static void vc4_seqno_cb_work(struct work_struct *work)
-{
-	struct vc4_seqno_cb *cb = container_of(work, struct vc4_seqno_cb, work);
-
-	cb->func(cb);
-}
-
-int vc4_queue_seqno_cb(struct drm_device *dev,
-		       struct vc4_seqno_cb *cb, uint64_t seqno,
-		       void (*func)(struct vc4_seqno_cb *cb))
-{
-	struct vc4_dev *vc4 = to_vc4_dev(dev);
-	unsigned long irqflags;
-
-	if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
-		return -ENODEV;

drivers/gpu/drm/vc4/vc4_gem.c vc4_job_handle_completed(struct vc4_dev *vc4) · line 950

Quoted 10 lines
drivers/gpu/drm/vc4/vc4_gem.c +0 -10
-
-	cb->func = func;
-	INIT_WORK(&cb->work, vc4_seqno_cb_work);
-
-	spin_lock_irqsave(&vc4->job_lock, irqflags);
-	if (seqno > vc4->finished_seqno) {
-		cb->seqno = seqno;
-		list_add_tail(&cb->work.entry, &vc4->seqno_cb_list);
-	} else {
-		schedule_work(&cb->work);

I haven't done a full read through yet but just strikes me on a casual browse, that if the rest of the series is correct, then it looks seqno_cb_list and the whole struct vc4_seqno_cb be removed as well.

Regards,

Tvrtko

Quoted 9 lines
drivers/gpu/drm/vc4/vc4_gem.c +0 -6
-	}
-	spin_unlock_irqrestore(&vc4->job_lock, irqflags);
-
-	return 0;
-}
-
  /* Scheduled when any job has been completed, this walks the list of
   * jobs that had completed and unrefs their BOs and frees their exec
   * structs.