Skip to contentSkip to conversation

drm/msm: Add fence->wait() op

PATCH In review Rob Clark 4 participants 15 messages Jul 20, 2021 – Jul 22, 2021

Subsystems (4)
lore
Share & tools
lore

Rob Clark

author
#0 lore raw

PATCH 1/1 drm/msm: Add fence->wait() op

From: Rob Clark <[email protected]>

Somehow we had neither ->wait() nor dma_fence_signal() calls, and no one noticed. Oops.

Note that this removes the !timeout case, which has not been used in a long time.

---
 drivers/gpu/drm/msm/msm_fence.c | 59 +++++++++++++++++++--------------
 1 file changed, 34 insertions(+), 25 deletions(-)
drivers/gpu/drm/msm/msm_fence.c +34 -25
index cd59a5918038..8ee96b90ded6 100644
@@ -38,11 +38,10 @@ static inline bool fence_completed(struct msm_fence_context *fctx, uint32_t fenc
 	return (int32_t)(fctx->completed_fence - fence) >= 0;
 }
 
-/* legacy path for WAIT_FENCE ioctl: */
-int msm_wait_fence(struct msm_fence_context *fctx, uint32_t fence,
-		ktime_t *timeout, bool interruptible)
+static signed long wait_fence(struct msm_fence_context *fctx, uint32_t fence,
+		signed long remaining_jiffies, bool interruptible)
 {
-	int ret;
+	signed long ret;
 
 	if (fence > fctx->last_fence) {
 		DRM_ERROR_RATELIMITED("%s: waiting on invalid fence: %u (of %u)\n",
@@ -50,33 +49,34 @@ int msm_wait_fence(struct msm_fence_context *fctx, uint32_t fence,
 		return -EINVAL;
 	}
 
-	if (!timeout) {
-		/* no-wait: */
-		ret = fence_completed(fctx, fence) ? 0 : -EBUSY;
+	if (interruptible) {
+		ret = wait_event_interruptible_timeout(fctx->event,
+			fence_completed(fctx, fence),
+			remaining_jiffies);
 	} else {
-		unsigned long remaining_jiffies = timeout_to_jiffies(timeout);
-
-		if (interruptible)
-			ret = wait_event_interruptible_timeout(fctx->event,
-				fence_completed(fctx, fence),
-				remaining_jiffies);
-		else
-			ret = wait_event_timeout(fctx->event,
-				fence_completed(fctx, fence),
-				remaining_jiffies);
-
-		if (ret == 0) {
-			DBG("timeout waiting for fence: %u (completed: %u)",
-					fence, fctx->completed_fence);
-			ret = -ETIMEDOUT;
-		} else if (ret != -ERESTARTSYS) {
-			ret = 0;
-		}
+		ret = wait_event_timeout(fctx->event,
+			fence_completed(fctx, fence),
+			remaining_jiffies);
+	}
+
+	if (ret == 0) {
+		DBG("timeout waiting for fence: %u (completed: %u)",
+				fence, fctx->completed_fence);
+		ret = -ETIMEDOUT;
+	} else if (ret != -ERESTARTSYS) {
+		ret = 0;
 	}
 
 	return ret;
 }
 
+/* legacy path for WAIT_FENCE ioctl: */
+int msm_wait_fence(struct msm_fence_context *fctx, uint32_t fence,
+		ktime_t *timeout, bool interruptible)
+{
+	return wait_fence(fctx, fence, timeout_to_jiffies(timeout), interruptible);
+}
+
 /* called from workqueue */
 void msm_update_fence(struct msm_fence_context *fctx, uint32_t fence)
 {
@@ -114,10 +114,19 @@ static bool msm_fence_signaled(struct dma_fence *fence)
 	return fence_completed(f->fctx, f->base.seqno);
 }
 
+static signed long msm_fence_wait(struct dma_fence *fence, bool intr,
+		signed long timeout)
+{
+	struct msm_fence *f = to_msm_fence(fence);
+
+	return wait_fence(f->fctx, fence->seqno, timeout, intr);
+}
+
 static const struct dma_fence_ops msm_fence_ops = {
 	.get_driver_name = msm_fence_get_driver_name,
 	.get_timeline_name = msm_fence_get_timeline_name,
 	.signaled = msm_fence_signaled,
+	.wait = msm_fence_wait,
 };
 
 struct dma_fence *
Signature · 1 line
2.31.1
#1 lore raw

Hi Rob,

Am 20.07.21 um 17:07 schrieb Rob Clark:

Quoted 4 lines

From: Rob Clark <[email protected]>

Somehow we had neither ->wait() nor dma_fence_signal() calls, and no one noticed. Oops.

I'm not sure if that is a good idea.

The dma_fence->wait() callback is pretty much deprecated and should not be used any more.

What exactly do you need that for?

Regards,
Christian.

Quoted 106 lines

Note that this removes the !timeout case, which has not been used in a long time.

---
  drivers/gpu/drm/msm/msm_fence.c | 59 +++++++++++++++++++--------------
  1 file changed, 34 insertions(+), 25 deletions(-)
drivers/gpu/drm/msm/msm_fence.c +34 -25
index cd59a5918038..8ee96b90ded6 100644
@@ -38,11 +38,10 @@ static inline bool fence_completed(struct msm_fence_context *fctx, uint32_t fenc
  	return (int32_t)(fctx->completed_fence - fence) >= 0;
  }
 
-/* legacy path for WAIT_FENCE ioctl: */
-int msm_wait_fence(struct msm_fence_context *fctx, uint32_t fence,
-		ktime_t *timeout, bool interruptible)
+static signed long wait_fence(struct msm_fence_context *fctx, uint32_t fence,
+		signed long remaining_jiffies, bool interruptible)
  {
-	int ret;
+	signed long ret;
 
  	if (fence > fctx->last_fence) {
  		DRM_ERROR_RATELIMITED("%s: waiting on invalid fence: %u (of %u)\n",
@@ -50,33 +49,34 @@ int msm_wait_fence(struct msm_fence_context *fctx, uint32_t fence,
  		return -EINVAL;
  	}
 
-	if (!timeout) {
-		/* no-wait: */
-		ret = fence_completed(fctx, fence) ? 0 : -EBUSY;
+	if (interruptible) {
+		ret = wait_event_interruptible_timeout(fctx->event,
+			fence_completed(fctx, fence),
+			remaining_jiffies);
  	} else {
-		unsigned long remaining_jiffies = timeout_to_jiffies(timeout);
-
-		if (interruptible)
-			ret = wait_event_interruptible_timeout(fctx->event,
-				fence_completed(fctx, fence),
-				remaining_jiffies);
-		else
-			ret = wait_event_timeout(fctx->event,
-				fence_completed(fctx, fence),
-				remaining_jiffies);
-
-		if (ret == 0) {
-			DBG("timeout waiting for fence: %u (completed: %u)",
-					fence, fctx->completed_fence);
-			ret = -ETIMEDOUT;
-		} else if (ret != -ERESTARTSYS) {
-			ret = 0;
-		}
+		ret = wait_event_timeout(fctx->event,
+			fence_completed(fctx, fence),
+			remaining_jiffies);
+	}
+
+	if (ret == 0) {
+		DBG("timeout waiting for fence: %u (completed: %u)",
+				fence, fctx->completed_fence);
+		ret = -ETIMEDOUT;
+	} else if (ret != -ERESTARTSYS) {
+		ret = 0;
  	}
 
  	return ret;
  }
 
+/* legacy path for WAIT_FENCE ioctl: */
+int msm_wait_fence(struct msm_fence_context *fctx, uint32_t fence,
+		ktime_t *timeout, bool interruptible)
+{
+	return wait_fence(fctx, fence, timeout_to_jiffies(timeout), interruptible);
+}
+
  /* called from workqueue */
  void msm_update_fence(struct msm_fence_context *fctx, uint32_t fence)
  {
@@ -114,10 +114,19 @@ static bool msm_fence_signaled(struct dma_fence *fence)
  	return fence_completed(f->fctx, f->base.seqno);
  }
 
+static signed long msm_fence_wait(struct dma_fence *fence, bool intr,
+		signed long timeout)
+{
+	struct msm_fence *f = to_msm_fence(fence);
+
+	return wait_fence(f->fctx, fence->seqno, timeout, intr);
+}
+
  static const struct dma_fence_ops msm_fence_ops = {
  	.get_driver_name = msm_fence_get_driver_name,
  	.get_timeline_name = msm_fence_get_timeline_name,
  	.signaled = msm_fence_signaled,
+	.wait = msm_fence_wait,
  };
 
  struct dma_fence *
author
#2 lore raw
Quoted 10 lines · Christian König

Hi Rob,

Am 20.07.21 um 17:07 schrieb Rob Clark:

Quoted 4 lines

From: Rob Clark <[email protected]>

Somehow we had neither ->wait() nor dma_fence_signal() calls, and no one noticed. Oops.

Quoted 6 lines

I'm not sure if that is a good idea.

The dma_fence->wait() callback is pretty much deprecated and should not be used any more.

What exactly do you need that for?

Well, the alternative is to track the set of fences which have signalling enabled, and then figure out which ones to signal, which seems like a lot more work, vs just re-purposing the wait implementation we already have for non-dma_fence cases ;-)

Why is the ->wait() callback (pretty much) deprecated?

BR,
-R

Quoted 110 lines

Regards,
Christian.

Quoted 106 lines

Note that this removes the !timeout case, which has not been used in a long time.

---
  drivers/gpu/drm/msm/msm_fence.c | 59 +++++++++++++++++++--------------
  1 file changed, 34 insertions(+), 25 deletions(-)
drivers/gpu/drm/msm/msm_fence.c +34 -25
index cd59a5918038..8ee96b90ded6 100644
@@ -38,11 +38,10 @@ static inline bool fence_completed(struct msm_fence_context *fctx, uint32_t fenc
      return (int32_t)(fctx->completed_fence - fence) >= 0;
  }
 
-/* legacy path for WAIT_FENCE ioctl: */
-int msm_wait_fence(struct msm_fence_context *fctx, uint32_t fence,
-             ktime_t *timeout, bool interruptible)
+static signed long wait_fence(struct msm_fence_context *fctx, uint32_t fence,
+             signed long remaining_jiffies, bool interruptible)
  {
-     int ret;
+     signed long ret;
 
      if (fence > fctx->last_fence) {
              DRM_ERROR_RATELIMITED("%s: waiting on invalid fence: %u (of %u)\n",
@@ -50,33 +49,34 @@ int msm_wait_fence(struct msm_fence_context *fctx, uint32_t fence,
              return -EINVAL;
      }
 
-     if (!timeout) {
-             /* no-wait: */
-             ret = fence_completed(fctx, fence) ? 0 : -EBUSY;
+     if (interruptible) {
+             ret = wait_event_interruptible_timeout(fctx->event,
+                     fence_completed(fctx, fence),
+                     remaining_jiffies);
      } else {
-             unsigned long remaining_jiffies = timeout_to_jiffies(timeout);
-
-             if (interruptible)
-                     ret = wait_event_interruptible_timeout(fctx->event,
-                             fence_completed(fctx, fence),
-                             remaining_jiffies);
-             else
-                     ret = wait_event_timeout(fctx->event,
-                             fence_completed(fctx, fence),
-                             remaining_jiffies);
-
-             if (ret == 0) {
-                     DBG("timeout waiting for fence: %u (completed: %u)",
-                                     fence, fctx->completed_fence);
-                     ret = -ETIMEDOUT;
-             } else if (ret != -ERESTARTSYS) {
-                     ret = 0;
-             }
+             ret = wait_event_timeout(fctx->event,
+                     fence_completed(fctx, fence),
+                     remaining_jiffies);
+     }
+
+     if (ret == 0) {
+             DBG("timeout waiting for fence: %u (completed: %u)",
+                             fence, fctx->completed_fence);
+             ret = -ETIMEDOUT;
+     } else if (ret != -ERESTARTSYS) {
+             ret = 0;
      }
 
      return ret;
  }
 
+/* legacy path for WAIT_FENCE ioctl: */
+int msm_wait_fence(struct msm_fence_context *fctx, uint32_t fence,
+             ktime_t *timeout, bool interruptible)
+{
+     return wait_fence(fctx, fence, timeout_to_jiffies(timeout), interruptible);
+}
+
  /* called from workqueue */
  void msm_update_fence(struct msm_fence_context *fctx, uint32_t fence)
  {
@@ -114,10 +114,19 @@ static bool msm_fence_signaled(struct dma_fence *fence)
      return fence_completed(f->fctx, f->base.seqno);
  }
 
+static signed long msm_fence_wait(struct dma_fence *fence, bool intr,
+             signed long timeout)
+{
+     struct msm_fence *f = to_msm_fence(fence);
+
+     return wait_fence(f->fctx, fence->seqno, timeout, intr);
+}
+
  static const struct dma_fence_ops msm_fence_ops = {
      .get_driver_name = msm_fence_get_driver_name,
      .get_timeline_name = msm_fence_get_timeline_name,
      .signaled = msm_fence_signaled,
+     .wait = msm_fence_wait,
  };
 
  struct dma_fence *
#3 lore raw
Quoted 18 lines · Rob Clark
Quoted 15 lines · Christian König

Hi Rob,

Am 20.07.21 um 17:07 schrieb Rob Clark:

Quoted 4 lines

From: Rob Clark <[email protected]>

Somehow we had neither ->wait() nor dma_fence_signal() calls, and no one noticed. Oops.

I'm not sure if that is a good idea.

The dma_fence->wait() callback is pretty much deprecated and should not be used any more.

Quoted 8 lines
Quoted 1 line

What exactly do you need that for?

Well, the alternative is to track the set of fences which have signalling enabled, and then figure out which ones to signal, which seems like a lot more work, vs just re-purposing the wait implementation we already have for non-dma_fence cases ;-)

Why is the ->wait() callback (pretty much) deprecated?

Because if you need it that means for your driver dma_fence_add_cb is broken, which means a _lot_ of things don't work. Like dma_buf poll (compositors have patches to start using that), and I think drm/scheduler also becomes rather unhappy.

It essentially exists only for old drivers where ->enable_signalling is unreliable and we paper over that with a retry loop in ->wait and pray no one notices that it's too butchered. The proper fix is to have a driver thread to guarantee that ->enable_signalling works reliable, so you don't need a ->wait.

Can you type up a kerneldoc patch for dma_fence_ops->wait to hammer this in please?
-Daniel

Quoted 114 lines

BR,
-R

Quoted 110 lines

Regards,
Christian.

Quoted 106 lines

Note that this removes the !timeout case, which has not been used in a long time.

---
  drivers/gpu/drm/msm/msm_fence.c | 59 +++++++++++++++++++--------------
  1 file changed, 34 insertions(+), 25 deletions(-)
drivers/gpu/drm/msm/msm_fence.c +34 -25
index cd59a5918038..8ee96b90ded6 100644
@@ -38,11 +38,10 @@ static inline bool fence_completed(struct msm_fence_context *fctx, uint32_t fenc
      return (int32_t)(fctx->completed_fence - fence) >= 0;
  }
 
-/* legacy path for WAIT_FENCE ioctl: */
-int msm_wait_fence(struct msm_fence_context *fctx, uint32_t fence,
-             ktime_t *timeout, bool interruptible)
+static signed long wait_fence(struct msm_fence_context *fctx, uint32_t fence,
+             signed long remaining_jiffies, bool interruptible)
  {
-     int ret;
+     signed long ret;
 
      if (fence > fctx->last_fence) {
              DRM_ERROR_RATELIMITED("%s: waiting on invalid fence: %u (of %u)\n",
@@ -50,33 +49,34 @@ int msm_wait_fence(struct msm_fence_context *fctx, uint32_t fence,
              return -EINVAL;
      }
 
-     if (!timeout) {
-             /* no-wait: */
-             ret = fence_completed(fctx, fence) ? 0 : -EBUSY;
+     if (interruptible) {
+             ret = wait_event_interruptible_timeout(fctx->event,
+                     fence_completed(fctx, fence),
+                     remaining_jiffies);
      } else {
-             unsigned long remaining_jiffies = timeout_to_jiffies(timeout);
-
-             if (interruptible)
-                     ret = wait_event_interruptible_timeout(fctx->event,
-                             fence_completed(fctx, fence),
-                             remaining_jiffies);
-             else
-                     ret = wait_event_timeout(fctx->event,
-                             fence_completed(fctx, fence),
-                             remaining_jiffies);
-
-             if (ret == 0) {
-                     DBG("timeout waiting for fence: %u (completed: %u)",
-                                     fence, fctx->completed_fence);
-                     ret = -ETIMEDOUT;
-             } else if (ret != -ERESTARTSYS) {
-                     ret = 0;
-             }
+             ret = wait_event_timeout(fctx->event,
+                     fence_completed(fctx, fence),
+                     remaining_jiffies);
+     }
+
+     if (ret == 0) {
+             DBG("timeout waiting for fence: %u (completed: %u)",
+                             fence, fctx->completed_fence);
+             ret = -ETIMEDOUT;
+     } else if (ret != -ERESTARTSYS) {
+             ret = 0;
      }
 
      return ret;
  }
 
+/* legacy path for WAIT_FENCE ioctl: */
+int msm_wait_fence(struct msm_fence_context *fctx, uint32_t fence,
+             ktime_t *timeout, bool interruptible)
+{
+     return wait_fence(fctx, fence, timeout_to_jiffies(timeout), interruptible);
+}
+
  /* called from workqueue */
  void msm_update_fence(struct msm_fence_context *fctx, uint32_t fence)
  {
@@ -114,10 +114,19 @@ static bool msm_fence_signaled(struct dma_fence *fence)
      return fence_completed(f->fctx, f->base.seqno);
  }
 
+static signed long msm_fence_wait(struct dma_fence *fence, bool intr,
+             signed long timeout)
+{
+     struct msm_fence *f = to_msm_fence(fence);
+
+     return wait_fence(f->fctx, fence->seqno, timeout, intr);
+}
+
  static const struct dma_fence_ops msm_fence_ops = {
      .get_driver_name = msm_fence_get_driver_name,
      .get_timeline_name = msm_fence_get_timeline_name,
      .signaled = msm_fence_signaled,
+     .wait = msm_fence_wait,
  };
 
  struct dma_fence *
Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
author
#4 lore raw
Quoted 27 lines · Daniel Vetter
Quoted 25 lines · Rob Clark
Quoted 10 lines · Christian König

Hi Rob,

Am 20.07.21 um 17:07 schrieb Rob Clark:

Quoted 4 lines

From: Rob Clark <[email protected]>

Somehow we had neither ->wait() nor dma_fence_signal() calls, and no one noticed. Oops.

Quoted 6 lines

I'm not sure if that is a good idea.

The dma_fence->wait() callback is pretty much deprecated and should not be used any more.

What exactly do you need that for?

Well, the alternative is to track the set of fences which have signalling enabled, and then figure out which ones to signal, which seems like a lot more work, vs just re-purposing the wait implementation we already have for non-dma_fence cases ;-)

Quoted 6 lines
Quoted 1 line

Why is the ->wait() callback (pretty much) deprecated?

Because if you need it that means for your driver dma_fence_add_cb is broken, which means a _lot_ of things don't work. Like dma_buf poll (compositors have patches to start using that), and I think drm/scheduler also becomes rather unhappy.

I'm starting to page back in how this works.. fence cb's aren't broken (which is also why dma_fence_wait() was not completely broken), because in retire_submits() we call dma_fence_is_signaled(submit->hw_fence).

But the reason that the custom wait function cleans up a tiny bit of jank is that the wait_queue_head_t gets signaled earlier, before we start iterating the submits and doing all that retire_submit() stuff (unpin/unref bo's, etc). I suppose I could just split things up to call dma_fence_signal() earlier, and *then* do the retire_submits() stuff.

BR,
-R

Quoted 131 lines

It essentially exists only for old drivers where ->enable_signalling is unreliable and we paper over that with a retry loop in ->wait and pray no one notices that it's too butchered. The proper fix is to have a driver thread to guarantee that ->enable_signalling works reliable, so you don't need a ->wait.

Can you type up a kerneldoc patch for dma_fence_ops->wait to hammer this in please?
-Daniel

Quoted 114 lines

BR,
-R

Quoted 110 lines

Regards,
Christian.

Quoted 106 lines

Note that this removes the !timeout case, which has not been used in a long time.

---
  drivers/gpu/drm/msm/msm_fence.c | 59 +++++++++++++++++++--------------
  1 file changed, 34 insertions(+), 25 deletions(-)
drivers/gpu/drm/msm/msm_fence.c +34 -25
index cd59a5918038..8ee96b90ded6 100644
@@ -38,11 +38,10 @@ static inline bool fence_completed(struct msm_fence_context *fctx, uint32_t fenc
      return (int32_t)(fctx->completed_fence - fence) >= 0;
  }
 
-/* legacy path for WAIT_FENCE ioctl: */
-int msm_wait_fence(struct msm_fence_context *fctx, uint32_t fence,
-             ktime_t *timeout, bool interruptible)
+static signed long wait_fence(struct msm_fence_context *fctx, uint32_t fence,
+             signed long remaining_jiffies, bool interruptible)
  {
-     int ret;
+     signed long ret;
 
      if (fence > fctx->last_fence) {
              DRM_ERROR_RATELIMITED("%s: waiting on invalid fence: %u (of %u)\n",
@@ -50,33 +49,34 @@ int msm_wait_fence(struct msm_fence_context *fctx, uint32_t fence,
              return -EINVAL;
      }
 
-     if (!timeout) {
-             /* no-wait: */
-             ret = fence_completed(fctx, fence) ? 0 : -EBUSY;
+     if (interruptible) {
+             ret = wait_event_interruptible_timeout(fctx->event,
+                     fence_completed(fctx, fence),
+                     remaining_jiffies);
      } else {
-             unsigned long remaining_jiffies = timeout_to_jiffies(timeout);
-
-             if (interruptible)
-                     ret = wait_event_interruptible_timeout(fctx->event,
-                             fence_completed(fctx, fence),
-                             remaining_jiffies);
-             else
-                     ret = wait_event_timeout(fctx->event,
-                             fence_completed(fctx, fence),
-                             remaining_jiffies);
-
-             if (ret == 0) {
-                     DBG("timeout waiting for fence: %u (completed: %u)",
-                                     fence, fctx->completed_fence);
-                     ret = -ETIMEDOUT;
-             } else if (ret != -ERESTARTSYS) {
-                     ret = 0;
-             }
+             ret = wait_event_timeout(fctx->event,
+                     fence_completed(fctx, fence),
+                     remaining_jiffies);
+     }
+
+     if (ret == 0) {
+             DBG("timeout waiting for fence: %u (completed: %u)",
+                             fence, fctx->completed_fence);
+             ret = -ETIMEDOUT;
+     } else if (ret != -ERESTARTSYS) {
+             ret = 0;
      }
 
      return ret;
  }
 
+/* legacy path for WAIT_FENCE ioctl: */
+int msm_wait_fence(struct msm_fence_context *fctx, uint32_t fence,
+             ktime_t *timeout, bool interruptible)
+{
+     return wait_fence(fctx, fence, timeout_to_jiffies(timeout), interruptible);
+}
+
  /* called from workqueue */
  void msm_update_fence(struct msm_fence_context *fctx, uint32_t fence)
  {
@@ -114,10 +114,19 @@ static bool msm_fence_signaled(struct dma_fence *fence)
      return fence_completed(f->fctx, f->base.seqno);
  }
 
+static signed long msm_fence_wait(struct dma_fence *fence, bool intr,
+             signed long timeout)
+{
+     struct msm_fence *f = to_msm_fence(fence);
+
+     return wait_fence(f->fctx, fence->seqno, timeout, intr);
+}
+
  static const struct dma_fence_ops msm_fence_ops = {
      .get_driver_name = msm_fence_get_driver_name,
      .get_timeline_name = msm_fence_get_timeline_name,
      .signaled = msm_fence_signaled,
+     .wait = msm_fence_wait,
  };
 
  struct dma_fence *

--
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch

#5 lore raw
Quoted 41 lines · Rob Clark
Quoted 27 lines · Daniel Vetter
Quoted 25 lines · Rob Clark
Quoted 10 lines · Christian König

Hi Rob,

Am 20.07.21 um 17:07 schrieb Rob Clark:

Quoted 4 lines

From: Rob Clark <[email protected]>

Somehow we had neither ->wait() nor dma_fence_signal() calls, and no one noticed. Oops.

Quoted 6 lines

I'm not sure if that is a good idea.

The dma_fence->wait() callback is pretty much deprecated and should not be used any more.

What exactly do you need that for?

Well, the alternative is to track the set of fences which have signalling enabled, and then figure out which ones to signal, which seems like a lot more work, vs just re-purposing the wait implementation we already have for non-dma_fence cases ;-)

Quoted 6 lines
Quoted 1 line

Why is the ->wait() callback (pretty much) deprecated?

Because if you need it that means for your driver dma_fence_add_cb is broken, which means a _lot_ of things don't work. Like dma_buf poll (compositors have patches to start using that), and I think drm/scheduler also becomes rather unhappy.

I'm starting to page back in how this works.. fence cb's aren't broken (which is also why dma_fence_wait() was not completely broken), because in retire_submits() we call dma_fence_is_signaled(submit->hw_fence).

Quoted 6 lines

But the reason that the custom wait function cleans up a tiny bit of jank is that the wait_queue_head_t gets signaled earlier, before we start iterating the submits and doing all that retire_submit() stuff (unpin/unref bo's, etc). I suppose I could just split things up to call dma_fence_signal() earlier, and *then* do the retire_submits() stuff.

Yeah reducing the latency there sounds like a good idea. -Daniel

Quoted 135 lines

BR,
-R

Quoted 131 lines

It essentially exists only for old drivers where ->enable_signalling is unreliable and we paper over that with a retry loop in ->wait and pray no one notices that it's too butchered. The proper fix is to have a driver thread to guarantee that ->enable_signalling works reliable, so you don't need a ->wait.

Can you type up a kerneldoc patch for dma_fence_ops->wait to hammer this in please?
-Daniel

Quoted 114 lines

BR,
-R

Quoted 110 lines

Regards,
Christian.

Quoted 106 lines

Note that this removes the !timeout case, which has not been used in a long time.

---
  drivers/gpu/drm/msm/msm_fence.c | 59 +++++++++++++++++++--------------
  1 file changed, 34 insertions(+), 25 deletions(-)
drivers/gpu/drm/msm/msm_fence.c +34 -25
index cd59a5918038..8ee96b90ded6 100644
@@ -38,11 +38,10 @@ static inline bool fence_completed(struct msm_fence_context *fctx, uint32_t fenc
      return (int32_t)(fctx->completed_fence - fence) >= 0;
  }
 
-/* legacy path for WAIT_FENCE ioctl: */
-int msm_wait_fence(struct msm_fence_context *fctx, uint32_t fence,
-             ktime_t *timeout, bool interruptible)
+static signed long wait_fence(struct msm_fence_context *fctx, uint32_t fence,
+             signed long remaining_jiffies, bool interruptible)
  {
-     int ret;
+     signed long ret;
 
      if (fence > fctx->last_fence) {
              DRM_ERROR_RATELIMITED("%s: waiting on invalid fence: %u (of %u)\n",
@@ -50,33 +49,34 @@ int msm_wait_fence(struct msm_fence_context *fctx, uint32_t fence,
              return -EINVAL;
      }
 
-     if (!timeout) {
-             /* no-wait: */
-             ret = fence_completed(fctx, fence) ? 0 : -EBUSY;
+     if (interruptible) {
+             ret = wait_event_interruptible_timeout(fctx->event,
+                     fence_completed(fctx, fence),
+                     remaining_jiffies);
      } else {
-             unsigned long remaining_jiffies = timeout_to_jiffies(timeout);
-
-             if (interruptible)
-                     ret = wait_event_interruptible_timeout(fctx->event,
-                             fence_completed(fctx, fence),
-                             remaining_jiffies);
-             else
-                     ret = wait_event_timeout(fctx->event,
-                             fence_completed(fctx, fence),
-                             remaining_jiffies);
-
-             if (ret == 0) {
-                     DBG("timeout waiting for fence: %u (completed: %u)",
-                                     fence, fctx->completed_fence);
-                     ret = -ETIMEDOUT;
-             } else if (ret != -ERESTARTSYS) {
-                     ret = 0;
-             }
+             ret = wait_event_timeout(fctx->event,
+                     fence_completed(fctx, fence),
+                     remaining_jiffies);
+     }
+
+     if (ret == 0) {
+             DBG("timeout waiting for fence: %u (completed: %u)",
+                             fence, fctx->completed_fence);
+             ret = -ETIMEDOUT;
+     } else if (ret != -ERESTARTSYS) {
+             ret = 0;
      }
 
      return ret;
  }
 
+/* legacy path for WAIT_FENCE ioctl: */
+int msm_wait_fence(struct msm_fence_context *fctx, uint32_t fence,
+             ktime_t *timeout, bool interruptible)
+{
+     return wait_fence(fctx, fence, timeout_to_jiffies(timeout), interruptible);
+}
+
  /* called from workqueue */
  void msm_update_fence(struct msm_fence_context *fctx, uint32_t fence)
  {
@@ -114,10 +114,19 @@ static bool msm_fence_signaled(struct dma_fence *fence)
      return fence_completed(f->fctx, f->base.seqno);
  }
 
+static signed long msm_fence_wait(struct dma_fence *fence, bool intr,
+             signed long timeout)
+{
+     struct msm_fence *f = to_msm_fence(fence);
+
+     return wait_fence(f->fctx, fence->seqno, timeout, intr);
+}
+
  static const struct dma_fence_ops msm_fence_ops = {
      .get_driver_name = msm_fence_get_driver_name,
      .get_timeline_name = msm_fence_get_timeline_name,
      .signaled = msm_fence_signaled,
+     .wait = msm_fence_wait,
  };
 
  struct dma_fence *

--
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch

Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
author
#6 lore raw
Quoted 50 lines · Daniel Vetter
Quoted 41 lines · Rob Clark
Quoted 27 lines · Daniel Vetter
Quoted 25 lines · Rob Clark
Quoted 10 lines · Christian König

Hi Rob,

Am 20.07.21 um 17:07 schrieb Rob Clark:

Quoted 4 lines

From: Rob Clark <[email protected]>

Somehow we had neither ->wait() nor dma_fence_signal() calls, and no one noticed. Oops.

Quoted 6 lines

I'm not sure if that is a good idea.

The dma_fence->wait() callback is pretty much deprecated and should not be used any more.

What exactly do you need that for?

Well, the alternative is to track the set of fences which have signalling enabled, and then figure out which ones to signal, which seems like a lot more work, vs just re-purposing the wait implementation we already have for non-dma_fence cases ;-)

Quoted 6 lines
Quoted 1 line

Why is the ->wait() callback (pretty much) deprecated?

Because if you need it that means for your driver dma_fence_add_cb is broken, which means a _lot_ of things don't work. Like dma_buf poll (compositors have patches to start using that), and I think drm/scheduler also becomes rather unhappy.

I'm starting to page back in how this works.. fence cb's aren't broken (which is also why dma_fence_wait() was not completely broken), because in retire_submits() we call dma_fence_is_signaled(submit->hw_fence).

Quoted 6 lines

But the reason that the custom wait function cleans up a tiny bit of jank is that the wait_queue_head_t gets signaled earlier, before we start iterating the submits and doing all that retire_submit() stuff (unpin/unref bo's, etc). I suppose I could just split things up to call dma_fence_signal() earlier, and *then* do the retire_submits() stuff.

Quoted 3 lines

Yeah reducing the latency there sounds like a good idea. -Daniel

Hmm, no, turns out that isn't the problem.. or, well, it is probably a good idea to call drm_fence_signal() earlier. But it seems like waking up from wait_event_* is faster than wake_up_state(wait->task, TASK_NORMAL). I suppose the wake_up_state() approach still needs for the scheduler to get around to schedule the runnable task.

So for now, I'm going back to my own wait function (plus earlier drm_fence_signal())

Before removing dma_fence_opps::wait(), I guess we want to re-think dma_fence_default_wait().. but I think that would require a dma_fence_context base class (rather than just a raw integer).

BR,
-R

Quoted 142 lines
Quoted 135 lines

BR,
-R

Quoted 131 lines

It essentially exists only for old drivers where ->enable_signalling is unreliable and we paper over that with a retry loop in ->wait and pray no one notices that it's too butchered. The proper fix is to have a driver thread to guarantee that ->enable_signalling works reliable, so you don't need a ->wait.

Can you type up a kerneldoc patch for dma_fence_ops->wait to hammer this in please?
-Daniel

Quoted 114 lines

BR,
-R

Quoted 110 lines

Regards,
Christian.

Quoted 106 lines

Note that this removes the !timeout case, which has not been used in a long time.

---
  drivers/gpu/drm/msm/msm_fence.c | 59 +++++++++++++++++++--------------
  1 file changed, 34 insertions(+), 25 deletions(-)
drivers/gpu/drm/msm/msm_fence.c +34 -25
index cd59a5918038..8ee96b90ded6 100644
@@ -38,11 +38,10 @@ static inline bool fence_completed(struct msm_fence_context *fctx, uint32_t fenc
      return (int32_t)(fctx->completed_fence - fence) >= 0;
  }
 
-/* legacy path for WAIT_FENCE ioctl: */
-int msm_wait_fence(struct msm_fence_context *fctx, uint32_t fence,
-             ktime_t *timeout, bool interruptible)
+static signed long wait_fence(struct msm_fence_context *fctx, uint32_t fence,
+             signed long remaining_jiffies, bool interruptible)
  {
-     int ret;
+     signed long ret;
 
      if (fence > fctx->last_fence) {
              DRM_ERROR_RATELIMITED("%s: waiting on invalid fence: %u (of %u)\n",
@@ -50,33 +49,34 @@ int msm_wait_fence(struct msm_fence_context *fctx, uint32_t fence,
              return -EINVAL;
      }
 
-     if (!timeout) {
-             /* no-wait: */
-             ret = fence_completed(fctx, fence) ? 0 : -EBUSY;
+     if (interruptible) {
+             ret = wait_event_interruptible_timeout(fctx->event,
+                     fence_completed(fctx, fence),
+                     remaining_jiffies);
      } else {
-             unsigned long remaining_jiffies = timeout_to_jiffies(timeout);
-
-             if (interruptible)
-                     ret = wait_event_interruptible_timeout(fctx->event,
-                             fence_completed(fctx, fence),
-                             remaining_jiffies);
-             else
-                     ret = wait_event_timeout(fctx->event,
-                             fence_completed(fctx, fence),
-                             remaining_jiffies);
-
-             if (ret == 0) {
-                     DBG("timeout waiting for fence: %u (completed: %u)",
-                                     fence, fctx->completed_fence);
-                     ret = -ETIMEDOUT;
-             } else if (ret != -ERESTARTSYS) {
-                     ret = 0;
-             }
+             ret = wait_event_timeout(fctx->event,
+                     fence_completed(fctx, fence),
+                     remaining_jiffies);
+     }
+
+     if (ret == 0) {
+             DBG("timeout waiting for fence: %u (completed: %u)",
+                             fence, fctx->completed_fence);
+             ret = -ETIMEDOUT;
+     } else if (ret != -ERESTARTSYS) {
+             ret = 0;
      }
 
      return ret;
  }
 
+/* legacy path for WAIT_FENCE ioctl: */
+int msm_wait_fence(struct msm_fence_context *fctx, uint32_t fence,
+             ktime_t *timeout, bool interruptible)
+{
+     return wait_fence(fctx, fence, timeout_to_jiffies(timeout), interruptible);
+}
+
  /* called from workqueue */
  void msm_update_fence(struct msm_fence_context *fctx, uint32_t fence)
  {
@@ -114,10 +114,19 @@ static bool msm_fence_signaled(struct dma_fence *fence)
      return fence_completed(f->fctx, f->base.seqno);
  }
 
+static signed long msm_fence_wait(struct dma_fence *fence, bool intr,
+             signed long timeout)
+{
+     struct msm_fence *f = to_msm_fence(fence);
+
+     return wait_fence(f->fctx, fence->seqno, timeout, intr);
+}
+
  static const struct dma_fence_ops msm_fence_ops = {
      .get_driver_name = msm_fence_get_driver_name,
      .get_timeline_name = msm_fence_get_timeline_name,
      .signaled = msm_fence_signaled,
+     .wait = msm_fence_wait,
  };
 
  struct dma_fence *

--
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch

--
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch

#7 lore raw
Quoted 61 lines · Rob Clark
Quoted 50 lines · Daniel Vetter
Quoted 41 lines · Rob Clark
Quoted 27 lines · Daniel Vetter
Quoted 25 lines · Rob Clark
Quoted 10 lines · Christian König

Hi Rob,

Am 20.07.21 um 17:07 schrieb Rob Clark:

Quoted 4 lines

From: Rob Clark <[email protected]>

Somehow we had neither ->wait() nor dma_fence_signal() calls, and no one noticed. Oops.

Quoted 6 lines

I'm not sure if that is a good idea.

The dma_fence->wait() callback is pretty much deprecated and should not be used any more.

What exactly do you need that for?

Well, the alternative is to track the set of fences which have signalling enabled, and then figure out which ones to signal, which seems like a lot more work, vs just re-purposing the wait implementation we already have for non-dma_fence cases ;-)

Quoted 6 lines
Quoted 1 line

Why is the ->wait() callback (pretty much) deprecated?

Because if you need it that means for your driver dma_fence_add_cb is broken, which means a _lot_ of things don't work. Like dma_buf poll (compositors have patches to start using that), and I think drm/scheduler also becomes rather unhappy.

I'm starting to page back in how this works.. fence cb's aren't broken (which is also why dma_fence_wait() was not completely broken), because in retire_submits() we call dma_fence_is_signaled(submit->hw_fence).

Quoted 6 lines

But the reason that the custom wait function cleans up a tiny bit of jank is that the wait_queue_head_t gets signaled earlier, before we start iterating the submits and doing all that retire_submit() stuff (unpin/unref bo's, etc). I suppose I could just split things up to call dma_fence_signal() earlier, and *then* do the retire_submits() stuff.

Quoted 3 lines

Yeah reducing the latency there sounds like a good idea. -Daniel

Hmm, no, turns out that isn't the problem.. or, well, it is probably a good idea to call drm_fence_signal() earlier. But it seems like waking up from wait_event_* is faster than wake_up_state(wait->task, TASK_NORMAL). I suppose the wake_up_state() approach still needs for the scheduler to get around to schedule the runnable task.

Quoted 6 lines

So for now, I'm going back to my own wait function (plus earlier drm_fence_signal())

Before removing dma_fence_opps::wait(), I guess we want to re-think dma_fence_default_wait().. but I think that would require a dma_fence_context base class (rather than just a raw integer).

Uh that's not great ... can't we fix this instead of papering over it in drivers? Aside from maybe different wakeup flags it all is supposed to work exactly the same underneath, and whether using a wait queue or not really shouldn't matter.
-Daniel

Quoted 146 lines

BR,
-R

Quoted 142 lines
Quoted 135 lines

BR,
-R

Quoted 131 lines

It essentially exists only for old drivers where ->enable_signalling is unreliable and we paper over that with a retry loop in ->wait and pray no one notices that it's too butchered. The proper fix is to have a driver thread to guarantee that ->enable_signalling works reliable, so you don't need a ->wait.

Can you type up a kerneldoc patch for dma_fence_ops->wait to hammer this in please?
-Daniel

Quoted 114 lines

BR,
-R

Quoted 110 lines

Regards,
Christian.

Quoted 106 lines

Note that this removes the !timeout case, which has not been used in a long time.

---
  drivers/gpu/drm/msm/msm_fence.c | 59 +++++++++++++++++++--------------
  1 file changed, 34 insertions(+), 25 deletions(-)
drivers/gpu/drm/msm/msm_fence.c +34 -25
index cd59a5918038..8ee96b90ded6 100644
@@ -38,11 +38,10 @@ static inline bool fence_completed(struct msm_fence_context *fctx, uint32_t fenc
      return (int32_t)(fctx->completed_fence - fence) >= 0;
  }
 
-/* legacy path for WAIT_FENCE ioctl: */
-int msm_wait_fence(struct msm_fence_context *fctx, uint32_t fence,
-             ktime_t *timeout, bool interruptible)
+static signed long wait_fence(struct msm_fence_context *fctx, uint32_t fence,
+             signed long remaining_jiffies, bool interruptible)
  {
-     int ret;
+     signed long ret;
 
      if (fence > fctx->last_fence) {
              DRM_ERROR_RATELIMITED("%s: waiting on invalid fence: %u (of %u)\n",
@@ -50,33 +49,34 @@ int msm_wait_fence(struct msm_fence_context *fctx, uint32_t fence,
              return -EINVAL;
      }
 
-     if (!timeout) {
-             /* no-wait: */
-             ret = fence_completed(fctx, fence) ? 0 : -EBUSY;
+     if (interruptible) {
+             ret = wait_event_interruptible_timeout(fctx->event,
+                     fence_completed(fctx, fence),
+                     remaining_jiffies);
      } else {
-             unsigned long remaining_jiffies = timeout_to_jiffies(timeout);
-
-             if (interruptible)
-                     ret = wait_event_interruptible_timeout(fctx->event,
-                             fence_completed(fctx, fence),
-                             remaining_jiffies);
-             else
-                     ret = wait_event_timeout(fctx->event,
-                             fence_completed(fctx, fence),
-                             remaining_jiffies);
-
-             if (ret == 0) {
-                     DBG("timeout waiting for fence: %u (completed: %u)",
-                                     fence, fctx->completed_fence);
-                     ret = -ETIMEDOUT;
-             } else if (ret != -ERESTARTSYS) {
-                     ret = 0;
-             }
+             ret = wait_event_timeout(fctx->event,
+                     fence_completed(fctx, fence),
+                     remaining_jiffies);
+     }
+
+     if (ret == 0) {
+             DBG("timeout waiting for fence: %u (completed: %u)",
+                             fence, fctx->completed_fence);
+             ret = -ETIMEDOUT;
+     } else if (ret != -ERESTARTSYS) {
+             ret = 0;
      }
 
      return ret;
  }
 
+/* legacy path for WAIT_FENCE ioctl: */
+int msm_wait_fence(struct msm_fence_context *fctx, uint32_t fence,
+             ktime_t *timeout, bool interruptible)
+{
+     return wait_fence(fctx, fence, timeout_to_jiffies(timeout), interruptible);
+}
+
  /* called from workqueue */
  void msm_update_fence(struct msm_fence_context *fctx, uint32_t fence)
  {
@@ -114,10 +114,19 @@ static bool msm_fence_signaled(struct dma_fence *fence)
      return fence_completed(f->fctx, f->base.seqno);
  }
 
+static signed long msm_fence_wait(struct dma_fence *fence, bool intr,
+             signed long timeout)
+{
+     struct msm_fence *f = to_msm_fence(fence);
+
+     return wait_fence(f->fctx, fence->seqno, timeout, intr);
+}
+
  static const struct dma_fence_ops msm_fence_ops = {
      .get_driver_name = msm_fence_get_driver_name,
      .get_timeline_name = msm_fence_get_timeline_name,
      .signaled = msm_fence_signaled,
+     .wait = msm_fence_wait,
  };
 
  struct dma_fence *

--
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch

--
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch

Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
#8 lore raw

Am 21.07.21 um 21:03 schrieb Daniel Vetter:

Quoted 42 lines · Rob Clark
Quoted 41 lines · Daniel Vetter
Quoted 40 lines · Rob Clark
Quoted 39 lines · Daniel Vetter
Quoted 22 lines · Rob Clark
Quoted 21 lines
Quoted 8 lines · Christian König

Hi Rob,

Am 20.07.21 um 17:07 schrieb Rob Clark:

Quoted 4 lines

From: Rob Clark <[email protected]>

Somehow we had neither ->wait() nor dma_fence_signal() calls, and no one noticed. Oops.

Quoted 6 lines

I'm not sure if that is a good idea.

The dma_fence->wait() callback is pretty much deprecated and should not be used any more.

What exactly do you need that for?

Well, the alternative is to track the set of fences which have signalling enabled, and then figure out which ones to signal, which seems like a lot more work, vs just re-purposing the wait implementation we already have for non-dma_fence cases ;-)

Quoted 5 lines
Quoted 1 line

Why is the ->wait() callback (pretty much) deprecated?

Because if you need it that means for your driver dma_fence_add_cb is broken, which means a _lot_ of things don't work. Like dma_buf poll (compositors have patches to start using that), and I think drm/scheduler also becomes rather unhappy.

I'm starting to page back in how this works.. fence cb's aren't broken (which is also why dma_fence_wait() was not completely broken), because in retire_submits() we call dma_fence_is_signaled(submit->hw_fence).

But the reason that the custom wait function cleans up a tiny bit of jank is that the wait_queue_head_t gets signaled earlier, before we start iterating the submits and doing all that retire_submit() stuff (unpin/unref bo's, etc). I suppose I could just split things up to call dma_fence_signal() earlier, and *then* do the retire_submits() stuff.

Quoted 8 lines
Quoted 8 lines
Quoted 3 lines

Yeah reducing the latency there sounds like a good idea. -Daniel

Hmm, no, turns out that isn't the problem.. or, well, it is probably a good idea to call drm_fence_signal() earlier. But it seems like waking up from wait_event_* is faster than wake_up_state(wait->task, TASK_NORMAL). I suppose the wake_up_state() approach still needs for the scheduler to get around to schedule the runnable task.

As far as I know wake_up_state() tries to run the thread on the CPU it was scheduled last, while wait_event_* makes the thread run on the CPU who issues the wake by default.

And yes I've also noticed this already and it was one of the reason why I suggested to use a wait_queue instead of the hand wired dma_fence_wait implementation.

Quoted 11 lines
Quoted 7 lines

So for now, I'm going back to my own wait function (plus earlier drm_fence_signal())

Before removing dma_fence_opps::wait(), I guess we want to re-think dma_fence_default_wait().. but I think that would require a dma_fence_context base class (rather than just a raw integer).

Uh that's not great ... can't we fix this instead of papering over it in drivers? Aside from maybe different wakeup flags it all is supposed to work exactly the same underneath, and whether using a wait queue or not really shouldn't matter.

Well it would have been nicer if we used the existing infrastructure instead of re-inventing stuff for dma_fence, but that chance is long gone.

And you don't need a dma_fence_context base class, but rather just a flag in the dma_fence_ops if you want to change the behavior.

Regards,
Christian.

Quoted 139 lines

-Daniel

Quoted 137 lines

BR,
-R

Quoted 134 lines
Quoted 128 lines

BR,
-R

Quoted 125 lines

It essentially exists only for old drivers where ->enable_signalling is unreliable and we paper over that with a retry loop in ->wait and pray no one notices that it's too butchered. The proper fix is to have a driver thread to guarantee that ->enable_signalling works reliable, so you don't need a ->wait.

Can you type up a kerneldoc patch for dma_fence_ops->wait to hammer this in please?
-Daniel

Quoted 108 lines

BR,
-R

Quoted 105 lines

Regards,
Christian.

Quoted 102 lines

Note that this removes the !timeout case, which has not been used in a long time.

---
   drivers/gpu/drm/msm/msm_fence.c | 59 +++++++++++++++++++--------------
   1 file changed, 34 insertions(+), 25 deletions(-)
drivers/gpu/drm/msm/msm_fence.c +34 -25
index cd59a5918038..8ee96b90ded6 100644
@@ -38,11 +38,10 @@ static inline bool fence_completed(struct msm_fence_context *fctx, uint32_t fenc
       return (int32_t)(fctx->completed_fence - fence) >= 0;
   }
 
-/* legacy path for WAIT_FENCE ioctl: */
-int msm_wait_fence(struct msm_fence_context *fctx, uint32_t fence,
-             ktime_t *timeout, bool interruptible)
+static signed long wait_fence(struct msm_fence_context *fctx, uint32_t fence,
+             signed long remaining_jiffies, bool interruptible)
   {
-     int ret;
+     signed long ret;
 
       if (fence > fctx->last_fence) {
               DRM_ERROR_RATELIMITED("%s: waiting on invalid fence: %u (of %u)\n",
@@ -50,33 +49,34 @@ int msm_wait_fence(struct msm_fence_context *fctx, uint32_t fence,
               return -EINVAL;
       }
 
-     if (!timeout) {
-             /* no-wait: */
-             ret = fence_completed(fctx, fence) ? 0 : -EBUSY;
+     if (interruptible) {
+             ret = wait_event_interruptible_timeout(fctx->event,
+                     fence_completed(fctx, fence),
+                     remaining_jiffies);
       } else {
-             unsigned long remaining_jiffies = timeout_to_jiffies(timeout);
-
-             if (interruptible)
-                     ret = wait_event_interruptible_timeout(fctx->event,
-                             fence_completed(fctx, fence),
-                             remaining_jiffies);
-             else
-                     ret = wait_event_timeout(fctx->event,
-                             fence_completed(fctx, fence),
-                             remaining_jiffies);
-
-             if (ret == 0) {
-                     DBG("timeout waiting for fence: %u (completed: %u)",
-                                     fence, fctx->completed_fence);
-                     ret = -ETIMEDOUT;
-             } else if (ret != -ERESTARTSYS) {
-                     ret = 0;
-             }
+             ret = wait_event_timeout(fctx->event,
+                     fence_completed(fctx, fence),
+                     remaining_jiffies);
+     }
+
+     if (ret == 0) {
+             DBG("timeout waiting for fence: %u (completed: %u)",
+                             fence, fctx->completed_fence);
+             ret = -ETIMEDOUT;
+     } else if (ret != -ERESTARTSYS) {
+             ret = 0;
       }
 
       return ret;
   }
 
+/* legacy path for WAIT_FENCE ioctl: */
+int msm_wait_fence(struct msm_fence_context *fctx, uint32_t fence,
+             ktime_t *timeout, bool interruptible)
+{
+     return wait_fence(fctx, fence, timeout_to_jiffies(timeout), interruptible);
+}
+
   /* called from workqueue */
   void msm_update_fence(struct msm_fence_context *fctx, uint32_t fence)
   {
@@ -114,10 +114,19 @@ static bool msm_fence_signaled(struct dma_fence *fence)
       return fence_completed(f->fctx, f->base.seqno);
   }
 
+static signed long msm_fence_wait(struct dma_fence *fence, bool intr,
+             signed long timeout)
+{
+     struct msm_fence *f = to_msm_fence(fence);
+
+     return wait_fence(f->fctx, fence->seqno, timeout, intr);
+}
+
   static const struct dma_fence_ops msm_fence_ops = {
       .get_driver_name = msm_fence_get_driver_name,
       .get_timeline_name = msm_fence_get_timeline_name,
       .signaled = msm_fence_signaled,
+     .wait = msm_fence_wait,
   };
 
Quoted 1 line
Quoted 1 line

drivers/gpu/drm/msm/msm_fence.c static bool msm_fence_signaled(struct dma_fence *fence) · line 132

Quoted 1 line
drivers/gpu/drm/msm/msm_fence.c +0 -0
   struct dma_fence *

--
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch

--
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch

#9 lore raw
Quoted 52 lines · Christian König

Am 21.07.21 um 21:03 schrieb Daniel Vetter:

Quoted 42 lines · Rob Clark
Quoted 41 lines · Daniel Vetter
Quoted 40 lines · Rob Clark
Quoted 39 lines · Daniel Vetter
Quoted 22 lines · Rob Clark
Quoted 21 lines
Quoted 8 lines · Christian König

Hi Rob,

Am 20.07.21 um 17:07 schrieb Rob Clark:

Quoted 4 lines

From: Rob Clark <[email protected]>

Somehow we had neither ->wait() nor dma_fence_signal() calls, and no one noticed. Oops.

Quoted 6 lines

I'm not sure if that is a good idea.

The dma_fence->wait() callback is pretty much deprecated and should not be used any more.

What exactly do you need that for?

Well, the alternative is to track the set of fences which have signalling enabled, and then figure out which ones to signal, which seems like a lot more work, vs just re-purposing the wait implementation we already have for non-dma_fence cases ;-)

Quoted 5 lines
Quoted 1 line

Why is the ->wait() callback (pretty much) deprecated?

Because if you need it that means for your driver dma_fence_add_cb is broken, which means a _lot_ of things don't work. Like dma_buf poll (compositors have patches to start using that), and I think drm/scheduler also becomes rather unhappy.

I'm starting to page back in how this works.. fence cb's aren't broken (which is also why dma_fence_wait() was not completely broken), because in retire_submits() we call dma_fence_is_signaled(submit->hw_fence).

But the reason that the custom wait function cleans up a tiny bit of jank is that the wait_queue_head_t gets signaled earlier, before we start iterating the submits and doing all that retire_submit() stuff (unpin/unref bo's, etc). I suppose I could just split things up to call dma_fence_signal() earlier, and *then* do the retire_submits() stuff.

Quoted 8 lines
Quoted 8 lines
Quoted 3 lines

Yeah reducing the latency there sounds like a good idea. -Daniel

Hmm, no, turns out that isn't the problem.. or, well, it is probably a good idea to call drm_fence_signal() earlier. But it seems like waking up from wait_event_* is faster than wake_up_state(wait->task, TASK_NORMAL). I suppose the wake_up_state() approach still needs for the scheduler to get around to schedule the runnable task.

Quoted 7 lines

As far as I know wake_up_state() tries to run the thread on the CPU it was scheduled last, while wait_event_* makes the thread run on the CPU who issues the wake by default.

And yes I've also noticed this already and it was one of the reason why I suggested to use a wait_queue instead of the hand wired dma_fence_wait implementation.

The first versions had used wait_queue, but iirc we had some issues with the callbacks and stuff and that was the reasons for hand-rolling. Or maybe it was the integration of the lockless fastpath for dma_fence_is_signalled().

Quoted 11 lines
Quoted 10 lines
Quoted 6 lines

So for now, I'm going back to my own wait function (plus earlier drm_fence_signal())

Before removing dma_fence_opps::wait(), I guess we want to re-think dma_fence_default_wait().. but I think that would require a dma_fence_context base class (rather than just a raw integer).

Uh that's not great ... can't we fix this instead of papering over it in drivers? Aside from maybe different wakeup flags it all is supposed to work exactly the same underneath, and whether using a wait queue or not really shouldn't matter.

Quoted 5 lines

Well it would have been nicer if we used the existing infrastructure instead of re-inventing stuff for dma_fence, but that chance is long gone.

And you don't need a dma_fence_context base class, but rather just a flag in the dma_fence_ops if you want to change the behavior.

If there's something broken we should just fix it, not force everyone to set a random flag. dma_fence work like special wait_queues, so if we differ then we should go back to that.
-Daniel

Quoted 144 lines

Regards,
Christian.

Quoted 139 lines

-Daniel

Quoted 137 lines

BR,
-R

Quoted 134 lines
Quoted 128 lines

BR,
-R

Quoted 125 lines

It essentially exists only for old drivers where ->enable_signalling is unreliable and we paper over that with a retry loop in ->wait and pray no one notices that it's too butchered. The proper fix is to have a driver thread to guarantee that ->enable_signalling works reliable, so you don't need a ->wait.

Can you type up a kerneldoc patch for dma_fence_ops->wait to hammer this in please?
-Daniel

Quoted 108 lines

BR,
-R

Quoted 105 lines

Regards,
Christian.

Quoted 102 lines

Note that this removes the !timeout case, which has not been used in a long time.

---
   drivers/gpu/drm/msm/msm_fence.c | 59 +++++++++++++++++++--------------
   1 file changed, 34 insertions(+), 25 deletions(-)
drivers/gpu/drm/msm/msm_fence.c +34 -25
index cd59a5918038..8ee96b90ded6 100644
@@ -38,11 +38,10 @@ static inline bool fence_completed(struct msm_fence_context *fctx, uint32_t fenc
       return (int32_t)(fctx->completed_fence - fence) >= 0;
   }
 
-/* legacy path for WAIT_FENCE ioctl: */
-int msm_wait_fence(struct msm_fence_context *fctx, uint32_t fence,
-             ktime_t *timeout, bool interruptible)
+static signed long wait_fence(struct msm_fence_context *fctx, uint32_t fence,
+             signed long remaining_jiffies, bool interruptible)
   {
-     int ret;
+     signed long ret;
 
       if (fence > fctx->last_fence) {
               DRM_ERROR_RATELIMITED("%s: waiting on invalid fence: %u (of %u)\n",
@@ -50,33 +49,34 @@ int msm_wait_fence(struct msm_fence_context *fctx, uint32_t fence,
               return -EINVAL;
       }
 
-     if (!timeout) {
-             /* no-wait: */
-             ret = fence_completed(fctx, fence) ? 0 : -EBUSY;
+     if (interruptible) {
+             ret = wait_event_interruptible_timeout(fctx->event,
+                     fence_completed(fctx, fence),
+                     remaining_jiffies);
       } else {
-             unsigned long remaining_jiffies = timeout_to_jiffies(timeout);
-
-             if (interruptible)
-                     ret = wait_event_interruptible_timeout(fctx->event,
-                             fence_completed(fctx, fence),
-                             remaining_jiffies);
-             else
-                     ret = wait_event_timeout(fctx->event,
-                             fence_completed(fctx, fence),
-                             remaining_jiffies);
-
-             if (ret == 0) {
-                     DBG("timeout waiting for fence: %u (completed: %u)",
-                                     fence, fctx->completed_fence);
-                     ret = -ETIMEDOUT;
-             } else if (ret != -ERESTARTSYS) {
-                     ret = 0;
-             }
+             ret = wait_event_timeout(fctx->event,
+                     fence_completed(fctx, fence),
+                     remaining_jiffies);
+     }
+
+     if (ret == 0) {
+             DBG("timeout waiting for fence: %u (completed: %u)",
+                             fence, fctx->completed_fence);
+             ret = -ETIMEDOUT;
+     } else if (ret != -ERESTARTSYS) {
+             ret = 0;
       }
 
       return ret;
   }
 
+/* legacy path for WAIT_FENCE ioctl: */
+int msm_wait_fence(struct msm_fence_context *fctx, uint32_t fence,
+             ktime_t *timeout, bool interruptible)
+{
+     return wait_fence(fctx, fence, timeout_to_jiffies(timeout), interruptible);
+}
+
   /* called from workqueue */
   void msm_update_fence(struct msm_fence_context *fctx, uint32_t fence)
   {
@@ -114,10 +114,19 @@ static bool msm_fence_signaled(struct dma_fence *fence)
       return fence_completed(f->fctx, f->base.seqno);
   }
 
+static signed long msm_fence_wait(struct dma_fence *fence, bool intr,
+             signed long timeout)
+{
+     struct msm_fence *f = to_msm_fence(fence);
+
+     return wait_fence(f->fctx, fence->seqno, timeout, intr);
+}
+
   static const struct dma_fence_ops msm_fence_ops = {
       .get_driver_name = msm_fence_get_driver_name,
       .get_timeline_name = msm_fence_get_timeline_name,
       .signaled = msm_fence_signaled,
+     .wait = msm_fence_wait,
   };
 
Quoted 1 line
Quoted 1 line

drivers/gpu/drm/msm/msm_fence.c static bool msm_fence_signaled(struct dma_fence *fence) · line 132

Quoted 1 line
drivers/gpu/drm/msm/msm_fence.c +0 -0
   struct dma_fence *

--
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch

--
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch

Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
#10 lore raw

Am 22.07.21 um 11:08 schrieb Daniel Vetter:

Quoted 17 lines

[SNIP]

Quoted 7 lines

As far as I know wake_up_state() tries to run the thread on the CPU it was scheduled last, while wait_event_* makes the thread run on the CPU who issues the wake by default.

And yes I've also noticed this already and it was one of the reason why I suggested to use a wait_queue instead of the hand wired dma_fence_wait implementation.

The first versions had used wait_queue, but iirc we had some issues with the callbacks and stuff and that was the reasons for hand-rolling. Or maybe it was the integration of the lockless fastpath for dma_fence_is_signalled().

Quoted 4 lines

[SNIP]
Well it would have been nicer if we used the existing infrastructure instead of re-inventing stuff for dma_fence, but that chance is long gone.

Quoted 5 lines
Quoted 2 lines

And you don't need a dma_fence_context base class, but rather just a flag in the dma_fence_ops if you want to change the behavior.

If there's something broken we should just fix it, not force everyone to set a random flag. dma_fence work like special wait_queues, so if we differ then we should go back to that.

Wait a second with that, this is not broken. It's just different behavior and there are good arguments for both sides.

If a wait is short you can have situations where you want to start the thread on the original CPU.
    This is because you can assume that the caches on that CPU are still hot and heating up the caches on the local CPU would take longer than an inter CPU interrupt.

But if the wait is long it makes more sense to run the thread on the CPU where you noticed the wake up event.
    This is because you can assume that the caches are cold anyway and starting the thread on the current CPU (most likely from an interrupt handler) gives you the absolutely best latency. In other words you usually return from the interrupt handler and just directly switch to the now running thread.

I'm not sure if all drivers want the same behavior. Rob here seems to prefer number 2, but we have used 1 for dma_fence for a rather long time now and it could be that some people start to complain when we switch unconditionally.

Regards,
Christian.

Quoted 2 lines

-Daniel

#11 lore raw
Quoted 18 lines · Christian König

Am 22.07.21 um 11:08 schrieb Daniel Vetter:

Quoted 17 lines

[SNIP]

Quoted 7 lines

As far as I know wake_up_state() tries to run the thread on the CPU it was scheduled last, while wait_event_* makes the thread run on the CPU who issues the wake by default.

And yes I've also noticed this already and it was one of the reason why I suggested to use a wait_queue instead of the hand wired dma_fence_wait implementation.

The first versions had used wait_queue, but iirc we had some issues with the callbacks and stuff and that was the reasons for hand-rolling. Or maybe it was the integration of the lockless fastpath for dma_fence_is_signalled().

Quoted 4 lines

[SNIP]
Well it would have been nicer if we used the existing infrastructure instead of re-inventing stuff for dma_fence, but that chance is long gone.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

And you don't need a dma_fence_context base class, but rather just a flag in the dma_fence_ops if you want to change the behavior.

If there's something broken we should just fix it, not force everyone to set a random flag. dma_fence work like special wait_queues, so if we differ then we should go back to that.

Wait a second with that, this is not broken. It's just different behavior and there are good arguments for both sides.

Oh I know, but since dma_fence is meant to be a wait_queue with hw support, they really should work the same and have the same tuning.

Quoted 14 lines

If a wait is short you can have situations where you want to start the thread on the original CPU.
    This is because you can assume that the caches on that CPU are still hot and heating up the caches on the local CPU would take longer than an inter CPU interrupt.

But if the wait is long it makes more sense to run the thread on the CPU where you noticed the wake up event.
    This is because you can assume that the caches are cold anyway and starting the thread on the current CPU (most likely from an interrupt handler) gives you the absolutely best latency.
    In other words you usually return from the interrupt handler and just directly switch to the now running thread.

Quoted 3 lines

I'm not sure if all drivers want the same behavior. Rob here seems to prefer number 2, but we have used 1 for dma_fence for a rather long time now and it could be that some people start to complain when we switch unconditionally.

I think the defaults are different because usually if you wake up a wait queue, there's a 1:1 relationship between waker and waiter.

Otoh if you just wake a thread it's probably some kinda of service thread, so N:1 relationship between waker and waiter. And in that case moving the waiter is a really bad idea.

I think dma_fence is generally much closer to 1:1 (with the most common one irq handler -> scheduler thread for that engine), so having the "same cpu" wake behaviour really sounds like the right thing to do. And not anything that is specifically an issue with how qualcom gpus work, and hence should be msm specific.

If it turns out to be the wrong thing, well I guess we'll learn something. And then maybe we have a different version of dma_fence_wait. -Daniel

Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
#12 lore raw

Am 22.07.21 um 12:47 schrieb Daniel Vetter:

Quoted 46 lines · Christian König
Quoted 18 lines

Am 22.07.21 um 11:08 schrieb Daniel Vetter:

Quoted 17 lines

[SNIP]

Quoted 7 lines

As far as I know wake_up_state() tries to run the thread on the CPU it was scheduled last, while wait_event_* makes the thread run on the CPU who issues the wake by default.

And yes I've also noticed this already and it was one of the reason why I suggested to use a wait_queue instead of the hand wired dma_fence_wait implementation.

The first versions had used wait_queue, but iirc we had some issues with the callbacks and stuff and that was the reasons for hand-rolling. Or maybe it was the integration of the lockless fastpath for dma_fence_is_signalled().

Quoted 4 lines

[SNIP]
Well it would have been nicer if we used the existing infrastructure instead of re-inventing stuff for dma_fence, but that chance is long gone.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

And you don't need a dma_fence_context base class, but rather just a flag in the dma_fence_ops if you want to change the behavior.

If there's something broken we should just fix it, not force everyone to set a random flag. dma_fence work like special wait_queues, so if we differ then we should go back to that.

Wait a second with that, this is not broken. It's just different behavior and there are good arguments for both sides.

Oh I know, but since dma_fence is meant to be a wait_queue with hw support, they really should work the same and have the same tuning.

Quoted 14 lines

If a wait is short you can have situations where you want to start the thread on the original CPU.
    This is because you can assume that the caches on that CPU are still hot
and heating up the caches on the local CPU would take longer than an inter CPU interrupt.

But if the wait is long it makes more sense to run the thread on the CPU where you noticed the wake up event.
    This is because you can assume that the caches are cold anyway and
starting the thread on the current CPU (most likely from an interrupt handler) gives you the absolutely best latency.
    In other words you usually return from the interrupt handler and just
directly switch to the now running thread.

Quoted 3 lines

I'm not sure if all drivers want the same behavior. Rob here seems to prefer number 2, but we have used 1 for dma_fence for a rather long time now and it could be that some people start to complain when we switch unconditionally.

Quoted 6 lines

I think the defaults are different because usually if you wake up a wait queue, there's a 1:1 relationship between waker and waiter.

Otoh if you just wake a thread it's probably some kinda of service thread, so N:1 relationship between waker and waiter. And in that case moving the waiter is a really bad idea.

Exactly that, yes.

Quoted 5 lines

I think dma_fence is generally much closer to 1:1 (with the most common one irq handler -> scheduler thread for that engine), so having the "same cpu" wake behaviour really sounds like the right thing to do. And not anything that is specifically an issue with how qualcom gpus work, and hence should be msm specific.

That's the point I really can't judge. At least for AMD stuff we try very hard to avoid waiting for the GPU in the first place.

But yes it might indeed be better to do it like this, but to be honest no idea what functions should actually be used for this.

So feel free to investigate further how to improve this.

Quoted 2 lines

If it turns out to be the wrong thing, well I guess we'll learn something. And then maybe we have a different version of dma_fence_wait.

Yeah, I would rather try to avoid that.

Christian.

Quoted 1 line

-Daniel

author
#14 lore raw
In reply to Christian König
Quoted 73 lines · Christian König

Am 21.07.21 um 21:03 schrieb Daniel Vetter:

Quoted 42 lines · Rob Clark
Quoted 41 lines · Daniel Vetter
Quoted 40 lines · Rob Clark
Quoted 39 lines · Daniel Vetter
Quoted 22 lines · Rob Clark
Quoted 21 lines
Quoted 8 lines · Christian König

Hi Rob,

Am 20.07.21 um 17:07 schrieb Rob Clark:

Quoted 4 lines

From: Rob Clark <[email protected]>

Somehow we had neither ->wait() nor dma_fence_signal() calls, and no one noticed. Oops.

Quoted 6 lines

I'm not sure if that is a good idea.

The dma_fence->wait() callback is pretty much deprecated and should not be used any more.

What exactly do you need that for?

Well, the alternative is to track the set of fences which have signalling enabled, and then figure out which ones to signal, which seems like a lot more work, vs just re-purposing the wait implementation we already have for non-dma_fence cases ;-)

Quoted 5 lines
Quoted 1 line

Why is the ->wait() callback (pretty much) deprecated?

Because if you need it that means for your driver dma_fence_add_cb is broken, which means a _lot_ of things don't work. Like dma_buf poll (compositors have patches to start using that), and I think drm/scheduler also becomes rather unhappy.

I'm starting to page back in how this works.. fence cb's aren't broken (which is also why dma_fence_wait() was not completely broken), because in retire_submits() we call dma_fence_is_signaled(submit->hw_fence).

But the reason that the custom wait function cleans up a tiny bit of jank is that the wait_queue_head_t gets signaled earlier, before we start iterating the submits and doing all that retire_submit() stuff (unpin/unref bo's, etc). I suppose I could just split things up to call dma_fence_signal() earlier, and *then* do the retire_submits() stuff.

Quoted 8 lines
Quoted 8 lines
Quoted 3 lines

Yeah reducing the latency there sounds like a good idea. -Daniel

Hmm, no, turns out that isn't the problem.. or, well, it is probably a good idea to call drm_fence_signal() earlier. But it seems like waking up from wait_event_* is faster than wake_up_state(wait->task, TASK_NORMAL). I suppose the wake_up_state() approach still needs for the scheduler to get around to schedule the runnable task.

As far as I know wake_up_state() tries to run the thread on the CPU it was scheduled last, while wait_event_* makes the thread run on the CPU who issues the wake by default.

And yes I've also noticed this already and it was one of the reason why I suggested to use a wait_queue instead of the hand wired dma_fence_wait implementation.

Quoted 11 lines
Quoted 7 lines

So for now, I'm going back to my own wait function (plus earlier drm_fence_signal())

Before removing dma_fence_opps::wait(), I guess we want to re-think dma_fence_default_wait().. but I think that would require a dma_fence_context base class (rather than just a raw integer).

Uh that's not great ... can't we fix this instead of papering over it in drivers? Aside from maybe different wakeup flags it all is supposed to work exactly the same underneath, and whether using a wait queue or not really shouldn't matter.

Quoted 5 lines

Well it would have been nicer if we used the existing infrastructure instead of re-inventing stuff for dma_fence, but that chance is long gone.

And you don't need a dma_fence_context base class, but rather just a flag in the dma_fence_ops if you want to change the behavior.

Hmm, I was thinking dma_fence_context to have a place for the wait_queue_head, but I guess that could also be per-dma_fence

author
#13 lore raw
In reply to Christian König
Quoted 42 lines · Christian König

Am 22.07.21 um 11:08 schrieb Daniel Vetter:

Quoted 17 lines

[SNIP]

Quoted 7 lines

As far as I know wake_up_state() tries to run the thread on the CPU it was scheduled last, while wait_event_* makes the thread run on the CPU who issues the wake by default.

And yes I've also noticed this already and it was one of the reason why I suggested to use a wait_queue instead of the hand wired dma_fence_wait implementation.

The first versions had used wait_queue, but iirc we had some issues with the callbacks and stuff and that was the reasons for hand-rolling. Or maybe it was the integration of the lockless fastpath for dma_fence_is_signalled().

Quoted 4 lines

[SNIP]
Well it would have been nicer if we used the existing infrastructure instead of re-inventing stuff for dma_fence, but that chance is long gone.

Quoted 5 lines
Quoted 2 lines

And you don't need a dma_fence_context base class, but rather just a flag in the dma_fence_ops if you want to change the behavior.

If there's something broken we should just fix it, not force everyone to set a random flag. dma_fence work like special wait_queues, so if we differ then we should go back to that.

Wait a second with that, this is not broken. It's just different behavior and there are good arguments for both sides.

If a wait is short you can have situations where you want to start the thread on the original CPU.
This is because you can assume that the caches on that CPU are
still hot and heating up the caches on the local CPU would take longer than an inter CPU interrupt.

But if the wait is long it makes more sense to run the thread on the CPU where you noticed the wake up event.
This is because you can assume that the caches are cold anyway and
starting the thread on the current CPU (most likely from an interrupt handler) gives you the absolutely best latency.
In other words you usually return from the interrupt handler and
just directly switch to the now running thread.

Quoted 5 lines

I'm not sure if all drivers want the same behavior. Rob here seems to prefer number 2, but we have used 1 for dma_fence for a rather long time now and it could be that some people start to complain when we switch unconditionally.

Hmm, I wonder if it would make sense to have a dma_wait_fence() flag to control the behavior, since it is maybe more about the waiter (and perhaps how long the waiter expects to wait) than the signaler..

BR,
-R