Skip to contentSkip to conversation

drm/msm: Fix fence rollover issue

PATCH In review Rob Clark 2 participants 3 messages Jun 15, 2022 – Jun 16, 2022

Subsystems (3)
lore
Share & tools
lore

Rob Clark

author
#0 lore raw

PATCH 1/1 drm/msm: Fix fence rollover issue

From: Rob Clark <[email protected]>

And while we are at it, let's start the fence counter close to the rollover point so that if issues slip in, they are more obvious.

---
 drivers/gpu/drm/msm/msm_fence.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
drivers/gpu/drm/msm/msm_fence.c +11 -2
index 3df255402a33..a35a6746c7cd 100644
@@ -28,6 +28,14 @@ msm_fence_context_alloc(struct drm_device *dev, volatile uint32_t *fenceptr,
 	fctx->fenceptr = fenceptr;
 	spin_lock_init(&fctx->spinlock);
 
+	/*
+	 * Start out close to the 32b fence rollover point, so we can
+	 * catch bugs with fence comparisons.
+	 */
+	fctx->last_fence = 0xffffff00;
+	fctx->completed_fence = fctx->last_fence;
+	*fctx->fenceptr = fctx->last_fence;
+
 	return fctx;
 }
 
@@ -46,11 +54,12 @@ bool msm_fence_completed(struct msm_fence_context *fctx, uint32_t fence)
 		(int32_t)(*fctx->fenceptr - fence) >= 0;
 }
 
-/* called from workqueue */
+/* called from irq handler */
 void msm_update_fence(struct msm_fence_context *fctx, uint32_t fence)
 {
 	spin_lock(&fctx->spinlock);
-	fctx->completed_fence = max(fence, fctx->completed_fence);
+	if (fence_after(fence, fctx->completed_fence))
+		fctx->completed_fence = fence;
 	spin_unlock(&fctx->spinlock);
 }
 
Signature · 1 line
2.36.1
#1 lore raw
Quoted 6 lines · Rob Clark

From: Rob Clark <[email protected]>

And while we are at it, let's start the fence counter close to the rollover point so that if issues slip in, they are more obvious.

Should it also have

Fixes fde5de6cb461 ("drm/msm: move fence code to it's own file")

Or maybe

Fixes 5f3aee4ceb5b ("drm/msm: Handle fence rollover")

Otherwise:

Reviewed: Dmitry Baryshkov <[email protected]>

Quoted 9 lines
---
  drivers/gpu/drm/msm/msm_fence.c | 13 +++++++++++--
  1 file changed, 11 insertions(+), 2 deletions(-)
drivers/gpu/drm/msm/msm_fence.c +0 -0
index 3df255402a33..a35a6746c7cd 100644

drivers/gpu/drm/msm/msm_fence.c msm_fence_context_alloc(struct drm_device *dev, volatile uint32_t *fenceptr, · line 37

Quoted 10 lines
drivers/gpu/drm/msm/msm_fence.c +7 -0
@@ -28,6 +28,14 @@ msm_fence_context_alloc(struct drm_device *dev, volatile uint32_t *fenceptr,
  	fctx->fenceptr = fenceptr;
  	spin_lock_init(&fctx->spinlock);
 
+	/*
+	 * Start out close to the 32b fence rollover point, so we can
+	 * catch bugs with fence comparisons.
+	 */
+	fctx->last_fence = 0xffffff00;
+	fctx->completed_fence = fctx->last_fence;
+	*fctx->fenceptr = fctx->last_fence;

This looks like a debugging hack. But probably it's fine to have it, as it wouldn't cause any side effects.

Quoted 19 lines
drivers/gpu/drm/msm/msm_fence.c +4 -2
+
  	return fctx;
  }
 
@@ -46,11 +54,12 @@ bool msm_fence_completed(struct msm_fence_context *fctx, uint32_t fence)
  		(int32_t)(*fctx->fenceptr - fence) >= 0;
  }
 
-/* called from workqueue */
+/* called from irq handler */
  void msm_update_fence(struct msm_fence_context *fctx, uint32_t fence)
  {
  	spin_lock(&fctx->spinlock);
-	fctx->completed_fence = max(fence, fctx->completed_fence);
+	if (fence_after(fence, fctx->completed_fence))
+		fctx->completed_fence = fence;
  	spin_unlock(&fctx->spinlock);
  }
 
Signature · 2 lines
With best wishes
Dmitry
author
#2 lore raw
Quoted 9 lines · Dmitry Baryshkov
Quoted 6 lines · Rob Clark

From: Rob Clark <[email protected]>

And while we are at it, let's start the fence counter close to the rollover point so that if issues slip in, they are more obvious.

Quoted 7 lines

Should it also have

Fixes fde5de6cb461 ("drm/msm: move fence code to it's own file")

Or maybe

Fixes 5f3aee4ceb5b ("drm/msm: Handle fence rollover")

arguably it fixes the first commit that added GPU support (and finishes up a couple spots that the above commit missed)

I guess I could use the fixes tag just to indicate how far back it would be reasonable to backport to stable branches.

Quoted 17 lines

Otherwise:

Reviewed: Dmitry Baryshkov <[email protected]>

Quoted 12 lines
---
  drivers/gpu/drm/msm/msm_fence.c | 13 +++++++++++--
  1 file changed, 11 insertions(+), 2 deletions(-)
drivers/gpu/drm/msm/msm_fence.c +0 -0
index 3df255402a33..a35a6746c7cd 100644
@@ -28,6 +28,14 @@ msm_fence_context_alloc(struct drm_device *dev, volatile uint32_t *fenceptr,
      fctx->fenceptr = fenceptr;
      spin_lock_init(&fctx->spinlock);
 
Quoted 10 lines

drivers/gpu/drm/msm/msm_fence.c msm_fence_context_alloc(struct drm_device *dev, volatile uint32_t *fenceptr, · line 37

Quoted 7 lines
drivers/gpu/drm/msm/msm_fence.c +7 -0
+     /*
+      * Start out close to the 32b fence rollover point, so we can
+      * catch bugs with fence comparisons.
+      */
+     fctx->last_fence = 0xffffff00;
+     fctx->completed_fence = fctx->last_fence;
+     *fctx->fenceptr = fctx->last_fence;

This looks like a debugging hack. But probably it's fine to have it, as it wouldn't cause any side effects.

I was originally going to add a modparam or kconfig to enable this.. but then thought, if there is a bug and thing are to go wrong, it's best for that to happen ASAP rather than after 200-400 days of uptime.. the latter case can be rather hard to reproduce bugs ;-)

IIRC the kernel does something similar with jiffies to ensure the rollover point is hit quickly

BR,
-R

Quoted 24 lines
Quoted 9 lines
drivers/gpu/drm/msm/msm_fence.c +1 -1
+
      return fctx;
  }
 
@@ -46,11 +54,12 @@ bool msm_fence_completed(struct msm_fence_context *fctx, uint32_t fence)
              (int32_t)(*fctx->fenceptr - fence) >= 0;
  }
 
-/* called from workqueue */

drivers/gpu/drm/msm/msm_fence.c bool msm_fence_completed(struct msm_fence_context *fctx, uint32_t fence) · line 65

Quoted 10 lines
drivers/gpu/drm/msm/msm_fence.c +3 -1
+/* called from irq handler */
  void msm_update_fence(struct msm_fence_context *fctx, uint32_t fence)
  {
      spin_lock(&fctx->spinlock);
-     fctx->completed_fence = max(fence, fctx->completed_fence);
+     if (fence_after(fence, fctx->completed_fence))
+             fctx->completed_fence = fence;
      spin_unlock(&fctx->spinlock);
  }
 

--
With best wishes
Dmitry