Skip to contentSkip to conversation

Fixing zero timeout handling for fence functions

PATCH In review Christian König 3 participants 8 messages Oct 25, 2016 – Oct 26, 2016

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

Hi Sumit,

sending this once more with all the patches in once set, cause the last one turned out to be a bit chaotic because I send from the wrong branch.

The following patch set fixes the handling in the fence and reservation object wait function when the timeout is zero.

An AMD developer introduced this a while ago to work around some issues in TTM and our amdgpu driver, but essentially this effort was a bit flawed because even with a zero timeout enable_signalling() should be called.

Otherwise someone busy waiting for the fence might never be signaled when you have hardware with faulty interrupts for example.

Please review and/or comment,
Christian.

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

#1 lore raw

PATCH 1/4 dma-buf/fence: make timeout handling in fence_default_wait consistent

From: Christian König <[email protected]>

Kernel functions taking a timeout usually return 1 on success even when they get a zero timeout.

---
 drivers/dma-buf/fence.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/dma-buf/fence.c +5 -3
index 4d51f9e..fb915ab 100644
@@ -335,18 +335,20 @@ fence_default_wait_cb(struct fence *fence, struct fence_cb *cb)
  * @timeout:	[in]	timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
  *
  * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or the
- * remaining timeout in jiffies on success.
+ * remaining timeout in jiffies on success. If timeout is zero the value one is
+ * returned if the fence is already signaled for consistency with other
+ * functions taking a jiffies timeout.
  */
 signed long
 fence_default_wait(struct fence *fence, bool intr, signed long timeout)
 {
 	struct default_wait_cb cb;
 	unsigned long flags;
-	signed long ret = timeout;
+	signed long ret = timeout ? timeout : 1;
 	bool was_set;
 
 	if (test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags))
-		return timeout;
+		return ret;
 
 	spin_lock_irqsave(fence->lock, flags);
 
Signature · 6 lines
2.5.0

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

Re: [PATCH 1/4] dma-buf/fence: make timeout handling in fence_default_wait consistent

Quoted 4 lines · Christian König

From: Christian König <[email protected]>

Kernel functions taking a timeout usually return 1 on success even when they get a zero timeout.

Which? The canonical example of schedule_timeout() doesn't behave like this.

Quoted 18 lines
---
 drivers/dma-buf/fence.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/dma-buf/fence.c +2 -1
index 4d51f9e..fb915ab 100644
@@ -335,18 +335,20 @@ fence_default_wait_cb(struct fence *fence, struct fence_cb *cb)
  * @timeout:	[in]	timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
  *
  * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or the
- * remaining timeout in jiffies on success.
+ * remaining timeout in jiffies on success. If timeout is zero the value one is
+ * returned if the fence is already signaled for consistency with other

drivers/dma-buf/fence.c fence_default_wait_cb(struct fence *fence, struct fence_cb *cb) · line 348

Quoted 10 lines
drivers/dma-buf/fence.c +2 -1
+ * functions taking a jiffies timeout.
  */
 signed long
 fence_default_wait(struct fence *fence, bool intr, signed long timeout)
 {
 	struct default_wait_cb cb;
 	unsigned long flags;
-	signed long ret = timeout;
+	signed long ret = timeout ? timeout : 1;
 	bool was_set;

This is horrible.

reservation_object_wait_timeout_rcu(), we pass it a timeout of zero to do a safe probe on all attached fences.

The first fence happens to be signaled, so now we set the timeout to 1 jiffie. Any subsequent incomplete fence will then wait for that jiffie before reporting 0.

When we report to userspace, we convert the timeout into an error, usually EBUSY or ETIME. Applying that same convention here makes this all much cleaner...
-Chris

Signature · 5 lines
Chris Wilson, Intel Open Source Technology Centre
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#3 lore raw

Re: [PATCH 1/4] dma-buf/fence: make timeout handling in fence_default_wait consistent

Am 26.10.2016 um 10:49 schrieb Chris Wilson:

Quoted 7 lines · Christian König
Quoted 4 lines

From: Christian König <[email protected]>

Kernel functions taking a timeout usually return 1 on success even when they get a zero timeout.

Which? The canonical example of schedule_timeout() doesn't behave like this.

wait_event_timeout() for example, as well as to the fence_wait_timeout() and reservation_object_wait_timeout_rcu() functions build on top of this one.

Regards,
Christian.

Quoted 34 lines
Quoted 19 lines
---
  drivers/dma-buf/fence.c | 8 +++++---
  1 file changed, 5 insertions(+), 3 deletions(-)
drivers/dma-buf/fence.c +2 -1
index 4d51f9e..fb915ab 100644
@@ -335,18 +335,20 @@ fence_default_wait_cb(struct fence *fence, struct fence_cb *cb)
   * @timeout:	[in]	timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
   *
   * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or the
- * remaining timeout in jiffies on success.
+ * remaining timeout in jiffies on success. If timeout is zero the value one is
+ * returned if the fence is already signaled for consistency with other

drivers/dma-buf/fence.c fence_default_wait_cb(struct fence *fence, struct fence_cb *cb) · line 348

Quoted 10 lines
drivers/dma-buf/fence.c +2 -1
+ * functions taking a jiffies timeout.
   */
  signed long
  fence_default_wait(struct fence *fence, bool intr, signed long timeout)
  {
  	struct default_wait_cb cb;
  	unsigned long flags;
-	signed long ret = timeout;
+	signed long ret = timeout ? timeout : 1;
  	bool was_set;

This is horrible.

reservation_object_wait_timeout_rcu(), we pass it a timeout of zero to do a safe probe on all attached fences.

Quoted 9 lines

The first fence happens to be signaled, so now we set the timeout to 1 jiffie. Any subsequent incomplete fence will then wait for that jiffie before reporting 0.

When we report to userspace, we convert the timeout into an error, usually EBUSY or ETIME. Applying that same convention here makes this all much cleaner...
-Chris

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

#4 lore raw

PATCH 2/4 dma-buf/fence: revert "don't wait when specified timeout is zero"

In reply to Christian König

From: Christian König <[email protected]>

This reverts commit 847b19a39e4c9b5e74c40f0842c48b41664cb43c.

When we don't call the wait function software signaling might never be activated. This can cause infinite polling loops with unreliable interrupt driven hardware.

---
 drivers/dma-buf/fence.c | 3 ---
 1 file changed, 3 deletions(-)
drivers/dma-buf/fence.c +0 -3
index fb915ab..64478f9 100644
@@ -159,9 +159,6 @@ fence_wait_timeout(struct fence *fence, bool intr, signed long timeout)
 	if (WARN_ON(timeout < 0))
 		return -EINVAL;
 
-	if (timeout == 0)
-		return fence_is_signaled(fence);
-
 	trace_fence_wait_start(fence);
 	ret = fence->ops->wait(fence, intr, timeout);
 	trace_fence_wait_end(fence);
Signature · 6 lines
2.5.0

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

PATCH 3/4 drm/ttm: fix ttm_bo_wait

In reply to Christian König

From: Christian König <[email protected]>

reservation_object_wait_timeout_rcu() should enable signaling even with a zero timeout, but ttm_bo_wait() can also be called from atomic context and then it is not a good idea to do this.

---
 drivers/gpu/drm/ttm/ttm_bo.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/gpu/drm/ttm/ttm_bo.c +8 -1
index 608c585..478d563 100644
@@ -1605,7 +1605,14 @@ EXPORT_SYMBOL(ttm_bo_unmap_virtual);
 int ttm_bo_wait(struct ttm_buffer_object *bo,
 		bool interruptible, bool no_wait)
 {
-	long timeout = no_wait ? 0 : 15 * HZ;
+	long timeout = 15 * HZ;
+
+	if (no_wait) {
+		if (reservation_object_test_signaled_rcu(bo->resv, true))
+			return 0;
+		else
+			return -EBUSY;
+	}
 
 	timeout = reservation_object_wait_timeout_rcu(bo->resv, true,
 						      interruptible, timeout);
Signature · 6 lines
2.5.0

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

PATCH 4/4 reservation: revert "wait only with non-zero timeout specified (v3)" v2

In reply to Christian König

From: Christian König <[email protected]>

This reverts commit fb8b7d2b9d80e1e71f379e57355936bd2b024be9.

Otherwise signaling might never be activated on the fences. This can result in infinite waiting with hardware which has unreliable interrupts.

v2: still return one when the timeout is zero and we don't have any fences.

---
 drivers/dma-buf/reservation.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/dma-buf/reservation.c +1 -4
index 9566a62..debb91d 100644
@@ -379,10 +379,7 @@ long reservation_object_wait_timeout_rcu(struct reservation_object *obj,
 {
 	struct fence *fence;
 	unsigned seq, shared_count, i = 0;
-	long ret = timeout;
-
-	if (!timeout)
-		return reservation_object_test_signaled_rcu(obj, wait_all);
+	long ret = timeout ? timeout : 1;
 
 retry:
 	fence = NULL;
Signature · 6 lines
2.5.0

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
maintainer
#7 lore raw
In reply to Christian König

Hi Christian,

Quoted 8 lines · Christian König

Hi Sumit,

sending this once more with all the patches in once set, cause the last one turned out to be a bit chaotic because I send from the wrong branch.

The following patch set fixes the handling in the fence and reservation object wait function when the timeout is zero.

Thanks for your patches!

Adding a few more reviewers.

Also, usually, I find it easier if the intro patch comes as (patch 0/X), since that lists all the patches of the patch series as well. If you could do that from next time onwards, that'd be great!

Quoted 10 lines

An AMD developer introduced this a while ago to work around some issues in TTM and our amdgpu driver, but essentially this effort was a bit flawed because even with a zero timeout enable_signalling() should be called.

Otherwise someone busy waiting for the fence might never be signaled when you have hardware with faulty interrupts for example.

Please review and/or comment,
Christian.

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