Skip to contentSkip to conversation

drm/amdgpu: set an error on all fences from a bad context

PATCH In review Alex Deucher 3 participants 7 messages Sep 15, 2025

Subsystems (2)
lore
Share & tools
lore

Alex Deucher

author maintainer
#0 lore raw

PATCH 1/2 drm/amdgpu: set an error on all fences from a bad context

When we backup ring contents to reemit after a queue reset, we don't backup ring contents from the bad context. When we signal the fences, we should set an error on those fences as well.

v2: misc cleanups
v3: add locking for fence error, fix comment (Christian)

Fixes 77cc0da39c7c ("drm/amdgpu: track ring state associated with a fence")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c | 33 ++++++++++++++++++++---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c  |  2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h  |  2 +-
 3 files changed, 31 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +29 -4
index fd8cca241da62..72f0f16924476 100644
@@ -758,11 +758,36 @@ void amdgpu_fence_driver_force_completion(struct amdgpu_ring *ring)
  * @fence: fence of the ring to signal
  *
  */
-void amdgpu_fence_driver_guilty_force_completion(struct amdgpu_fence *fence)
+void amdgpu_fence_driver_guilty_force_completion(struct amdgpu_fence *af)
 {
-	dma_fence_set_error(&fence->base, -ETIME);
-	amdgpu_fence_write(fence->ring, fence->seq);
-	amdgpu_fence_process(fence->ring);
+	struct dma_fence *unprocessed;
+	struct dma_fence __rcu **ptr;
+	struct amdgpu_fence *fence;
+	struct amdgpu_ring *ring = af->ring;
+	unsigned long flags;
+	u64 i, seqno;
+
+	seqno = amdgpu_fence_read(ring);
+
+	spin_lock_irqsave(&ring->fence_drv.lock, flags);
+	for (i = seqno + 1; i <= ring->fence_drv.sync_seq; ++i) {
+		ptr = &ring->fence_drv.fences[i & ring->fence_drv.num_fences_mask];
+		rcu_read_lock();
+		unprocessed = rcu_dereference(*ptr);
+
+		if (unprocessed && !dma_fence_is_signaled(unprocessed)) {
+			fence = container_of(unprocessed, struct amdgpu_fence, base);
+
+			if (fence == af)
+				dma_fence_set_error(&fence->base, -ETIME);
+			else if (fence->context == af->context)
+				dma_fence_set_error(&fence->base, -ECANCELED);
+		}
+		rcu_read_unlock();
+	}
+	spin_unlock_irqrestore(&ring->fence_drv.lock, flags);
+	amdgpu_fence_write(ring, af->seq);
+	amdgpu_fence_process(ring);
 }
 
 void amdgpu_fence_save_wptr(struct dma_fence *fence)
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +1 -1
index 6379bb25bf5ce..f6c9decedbd51 100644
@@ -812,7 +812,7 @@ int amdgpu_ring_reset_helper_end(struct amdgpu_ring *ring,
 	if (r)
 		return r;
 
-	/* signal the fence of the bad job */
+	/* signal the guilty fence and set an error on all fences from the context */
 	if (guilty_fence)
 		amdgpu_fence_driver_guilty_force_completion(guilty_fence);
 	/* Re-emit the non-guilty commands */
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h +1 -1
index 7670f5d82b9e4..0704fd9b85fdb 100644
@@ -155,7 +155,7 @@ extern const struct drm_sched_backend_ops amdgpu_sched_ops;
 void amdgpu_fence_driver_clear_job_fences(struct amdgpu_ring *ring);
 void amdgpu_fence_driver_set_error(struct amdgpu_ring *ring, int error);
 void amdgpu_fence_driver_force_completion(struct amdgpu_ring *ring);
-void amdgpu_fence_driver_guilty_force_completion(struct amdgpu_fence *fence);
+void amdgpu_fence_driver_guilty_force_completion(struct amdgpu_fence *af);
 void amdgpu_fence_save_wptr(struct dma_fence *fence);
 
 int amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring);
Signature · 1 line
2.51.0
author maintainer
#1 lore raw

PATCH 2/2 drm/amdgpu: clean up and unify hw fence handling

Decouple the amdgpu fence from the amdgpu_job structure. This lets us clean up the separate fence ops for the embedded fence and other fences. This also allows us to allocate the vm fence up front when we allocate the job.

v2: Additional cleanup suggested by Christian
v3: Additional cleanups suggested by Christian
v4: Additional cleanups suggested by David and
vm fence fix
v5: cast seqno (David)

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c |   2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c  |   7 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c   | 143 ++------------------
 drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c      |  17 ++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_job.c     |  43 ++++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_job.h     |   3 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h    |   8 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c      |   7 +-
 8 files changed, 63 insertions(+), 167 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c +1 -1
index a70651050acff..0a62727e74f0f 100644
@@ -1902,7 +1902,7 @@ static void amdgpu_ib_preempt_mark_partial_job(struct amdgpu_ring *ring)
 			continue;
 		}
 		job = to_amdgpu_job(s_job);
-		if (preempted && (&job->hw_fence.base) == fence)
+		if (preempted && (&job->hw_fence->base) == fence)
 			/* mark the job as preempted */
 			job->preemption_status |= AMDGPU_IB_PREEMPTED;
 	}
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +1 -6
index bdfb80377e6a8..dded88bc374c7 100644
@@ -5802,11 +5802,6 @@ int amdgpu_device_pre_asic_reset(struct amdgpu_device *adev,
 		if (!amdgpu_ring_sched_ready(ring))
 			continue;
 
-		/* Clear job fence from fence drv to avoid force_completion
-		 * leave NULL and vm flush fence in fence drv
-		 */
-		amdgpu_fence_driver_clear_job_fences(ring);
-
 		/* after all hw jobs are reset, hw fence is meaningless, so force_completion */
 		amdgpu_fence_driver_force_completion(ring);
 	}
@@ -6530,7 +6525,7 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
 	 *
 	 * job->base holds a reference to parent fence
 	 */
-	if (job && dma_fence_is_signaled(&job->hw_fence.base)) {
+	if (job && dma_fence_is_signaled(&job->hw_fence->base)) {
 		job_signaled = true;
 		dev_info(adev->dev, "Guilty job already signaled, skipping HW reset");
 		goto skip_hw_reset;
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +13 -130

235 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c +11 -6
index 7d9bcb72e8dd3..39229ece83f83 100644
@@ -149,17 +149,19 @@ int amdgpu_ib_schedule(struct amdgpu_ring *ring, unsigned int num_ibs,
 	if (job) {
 		vm = job->vm;
 		fence_ctx = job->base.s_fence ?
-			job->base.s_fence->scheduled.context : 0;
+			job->base.s_fence->finished.context : 0;
 		shadow_va = job->shadow_va;
 		csa_va = job->csa_va;
 		gds_va = job->gds_va;
 		init_shadow = job->init_shadow;
-		af = &job->hw_fence;
+		af = job->hw_fence;
 		/* Save the context of the job for reset handling.
 		 * The driver needs this so it can skip the ring
 		 * contents for guilty contexts.
 		 */
-		af->context = job->base.s_fence ? job->base.s_fence->finished.context : 0;
+		af->context = fence_ctx;
+		/* the vm fence is also part of the job's context */
+		job->hw_vm_fence->context = fence_ctx;
 	} else {
 		vm = NULL;
 		fence_ctx = 0;
@@ -167,7 +169,9 @@ int amdgpu_ib_schedule(struct amdgpu_ring *ring, unsigned int num_ibs,
 		csa_va = 0;
 		gds_va = 0;
 		init_shadow = false;
-		af = NULL;
+		af = kzalloc(sizeof(*af), GFP_ATOMIC);
+		if (!af)
+			return -ENOMEM;
 	}
 
 	if (!ring->sched.ready) {
@@ -289,7 +293,7 @@ int amdgpu_ib_schedule(struct amdgpu_ring *ring, unsigned int num_ibs,
 		amdgpu_ring_init_cond_exec(ring, ring->cond_exe_gpu_addr);
 	}
 
-	r = amdgpu_fence_emit(ring, f, af, fence_flags);
+	r = amdgpu_fence_emit(ring, af, fence_flags);
 	if (r) {
 		dev_err(adev->dev, "failed to emit fence (%d)\n", r);
 		if (job && job->vmid)
@@ -297,6 +301,7 @@ int amdgpu_ib_schedule(struct amdgpu_ring *ring, unsigned int num_ibs,
 		amdgpu_ring_undo(ring);
 		return r;
 	}
+	*f = &af->base;
 
 	if (ring->funcs->insert_end)
 		ring->funcs->insert_end(ring);
@@ -317,7 +322,7 @@ int amdgpu_ib_schedule(struct amdgpu_ring *ring, unsigned int num_ibs,
 	 * fence so we know what rings contents to backup
 	 * after we reset the queue.
 	 */
-	amdgpu_fence_save_wptr(*f);
+	amdgpu_fence_save_wptr(af);
 
 	amdgpu_ring_ib_end(ring);
 	amdgpu_ring_commit(ring);
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +30 -13

87 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_job.h +2 -1
index 2f302266662bc..d25f1fcf0242e 100644
@@ -48,7 +48,8 @@ struct amdgpu_job {
 	struct drm_sched_job    base;
 	struct amdgpu_vm	*vm;
 	struct amdgpu_sync	explicit_sync;
-	struct amdgpu_fence	hw_fence;
+	struct amdgpu_fence	*hw_fence;
+	struct amdgpu_fence	*hw_vm_fence;
 	struct dma_fence	*gang_submit;
 	uint32_t		preamble_status;
 	uint32_t                preemption_status;
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h +3 -5
index 0704fd9b85fdb..5d44f1354c8d8 100644
@@ -147,16 +147,14 @@ struct amdgpu_fence {
 	u64				wptr;
 	/* fence context for resets */
 	u64				context;
-	uint32_t			seq;
 };
 
 extern const struct drm_sched_backend_ops amdgpu_sched_ops;
 
-void amdgpu_fence_driver_clear_job_fences(struct amdgpu_ring *ring);
 void amdgpu_fence_driver_set_error(struct amdgpu_ring *ring, int error);
 void amdgpu_fence_driver_force_completion(struct amdgpu_ring *ring);
 void amdgpu_fence_driver_guilty_force_completion(struct amdgpu_fence *af);
-void amdgpu_fence_save_wptr(struct dma_fence *fence);
+void amdgpu_fence_save_wptr(struct amdgpu_fence *af);
 
 int amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring);
 int amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring,
@@ -166,8 +164,8 @@ void amdgpu_fence_driver_hw_init(struct amdgpu_device *adev);
 void amdgpu_fence_driver_hw_fini(struct amdgpu_device *adev);
 int amdgpu_fence_driver_sw_init(struct amdgpu_device *adev);
 void amdgpu_fence_driver_sw_fini(struct amdgpu_device *adev);
-int amdgpu_fence_emit(struct amdgpu_ring *ring, struct dma_fence **f,
-		      struct amdgpu_fence *af, unsigned int flags);
+int amdgpu_fence_emit(struct amdgpu_ring *ring, struct amdgpu_fence *af,
+		      unsigned int flags);
 int amdgpu_fence_emit_polling(struct amdgpu_ring *ring, uint32_t *s,
 			      uint32_t timeout);
 bool amdgpu_fence_process(struct amdgpu_ring *ring);
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2 -5
index bd12d8ff15a42..76bae546df11c 100644
@@ -772,7 +772,6 @@ int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job,
 	bool cleaner_shader_needed = false;
 	bool pasid_mapping_needed = false;
 	struct dma_fence *fence = NULL;
-	struct amdgpu_fence *af;
 	unsigned int patch;
 	int r;
 
@@ -835,12 +834,10 @@ int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job,
 	}
 
 	if (vm_flush_needed || pasid_mapping_needed || cleaner_shader_needed) {
-		r = amdgpu_fence_emit(ring, &fence, NULL, 0);
+		r = amdgpu_fence_emit(ring, job->hw_vm_fence, 0);
 		if (r)
 			return r;
-		/* this is part of the job's context */
-		af = container_of(fence, struct amdgpu_fence, base);
-		af->context = job->base.s_fence ? job->base.s_fence->finished.context : 0;
+		fence = &job->hw_vm_fence->base;
 	}
 
 	if (vm_flush_needed) {
Signature · 1 line
2.51.0
maintainer
#2 lore raw
In reply to Alex Deucher
Quoted 31 lines · Alex Deucher

When we backup ring contents to reemit after a queue reset, we don't backup ring contents from the bad context. When we signal the fences, we should set an error on those fences as well.

v2: misc cleanups
v3: add locking for fence error, fix comment (Christian)

Fixes 77cc0da39c7c ("drm/amdgpu: track ring state associated with a fence")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c | 33 ++++++++++++++++++++---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c  |  2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h  |  2 +-
 3 files changed, 31 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +2 -4
index fd8cca241da62..72f0f16924476 100644
@@ -758,11 +758,36 @@ void amdgpu_fence_driver_force_completion(struct amdgpu_ring *ring)
  * @fence: fence of the ring to signal
  *
  */
-void amdgpu_fence_driver_guilty_force_completion(struct amdgpu_fence *fence)
+void amdgpu_fence_driver_guilty_force_completion(struct amdgpu_fence *af)
 {
-	dma_fence_set_error(&fence->base, -ETIME);
-	amdgpu_fence_write(fence->ring, fence->seq);
-	amdgpu_fence_process(fence->ring);
+	struct dma_fence *unprocessed;

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c void amdgpu_fence_driver_force_completion(struct amdgpu_ring *ring) · line 773

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +10 -0
+	struct dma_fence __rcu **ptr;
+	struct amdgpu_fence *fence;
+	struct amdgpu_ring *ring = af->ring;
+	unsigned long flags;
+	u64 i, seqno;
+
+	seqno = amdgpu_fence_read(ring);
+
+	spin_lock_irqsave(&ring->fence_drv.lock, flags);
+	for (i = seqno + 1; i <= ring->fence_drv.sync_seq; ++i) {

That won't work, the seqno can wrap around.

I would just go over all fences, e.g. from 0 to end of array.

The checks below should make sure that we don't try to set an error on something already processed.

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c void amdgpu_fence_driver_force_completion(struct amdgpu_ring *ring) · line 778

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +5 -0
+		ptr = &ring->fence_drv.fences[i & ring->fence_drv.num_fences_mask];
+		rcu_read_lock();
+		unprocessed = rcu_dereference(*ptr);
+
+		if (unprocessed && !dma_fence_is_signaled(unprocessed)) {

dma_fence_is_signaled_locked(), otherwise the function would try to lock the same spinlock again.

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c void amdgpu_fence_driver_force_completion(struct amdgpu_ring *ring)

Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +12 -0
+			fence = container_of(unprocessed, struct amdgpu_fence, base);
+
+			if (fence == af)
+				dma_fence_set_error(&fence->base, -ETIME);
+			else if (fence->context == af->context)
+				dma_fence_set_error(&fence->base, -ECANCELED);
+		}
+		rcu_read_unlock();
+	}
+	spin_unlock_irqrestore(&ring->fence_drv.lock, flags);
 
+	amdgpu_fence_write(ring, af->seq);
+	amdgpu_fence_process(ring);

That's actually wrong. We want the other fences to signal later on when we process them.

Regards,
Christian.

Quoted 29 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +0 -0
 }
 

void amdgpu_fence_save_wptr(struct dma_fence *fence)

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +1 -1
index 6379bb25bf5ce..f6c9decedbd51 100644
@@ -812,7 +812,7 @@ int amdgpu_ring_reset_helper_end(struct amdgpu_ring *ring,
 	if (r)
 		return r;
 
-	/* signal the fence of the bad job */
+	/* signal the guilty fence and set an error on all fences from the context */
 	if (guilty_fence)
 		amdgpu_fence_driver_guilty_force_completion(guilty_fence);
 	/* Re-emit the non-guilty commands */
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h +1 -1
index 7670f5d82b9e4..0704fd9b85fdb 100644
@@ -155,7 +155,7 @@ extern const struct drm_sched_backend_ops amdgpu_sched_ops;
 void amdgpu_fence_driver_clear_job_fences(struct amdgpu_ring *ring);
 void amdgpu_fence_driver_set_error(struct amdgpu_ring *ring, int error);
 void amdgpu_fence_driver_force_completion(struct amdgpu_ring *ring);
-void amdgpu_fence_driver_guilty_force_completion(struct amdgpu_fence *fence);
+void amdgpu_fence_driver_guilty_force_completion(struct amdgpu_fence *af);
 void amdgpu_fence_save_wptr(struct dma_fence *fence);
 
 int amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring);
#3 lore raw
Quoted 71 lines · Christian König
Quoted 31 lines · Alex Deucher

When we backup ring contents to reemit after a queue reset, we don't backup ring contents from the bad context. When we signal the fences, we should set an error on those fences as well.

v2: misc cleanups
v3: add locking for fence error, fix comment (Christian)

Fixes 77cc0da39c7c ("drm/amdgpu: track ring state associated with a fence")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c | 33 ++++++++++++++++++++---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c  |  2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h  |  2 +-
 3 files changed, 31 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +2 -4
index fd8cca241da62..72f0f16924476 100644
@@ -758,11 +758,36 @@ void amdgpu_fence_driver_force_completion(struct amdgpu_ring *ring)
  * @fence: fence of the ring to signal
  *
  */
-void amdgpu_fence_driver_guilty_force_completion(struct amdgpu_fence *fence)
+void amdgpu_fence_driver_guilty_force_completion(struct amdgpu_fence *af)
 {
-     dma_fence_set_error(&fence->base, -ETIME);
-     amdgpu_fence_write(fence->ring, fence->seq);
-     amdgpu_fence_process(fence->ring);
+     struct dma_fence *unprocessed;

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c void amdgpu_fence_driver_force_completion(struct amdgpu_ring *ring) · line 773

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +10 -0
+     struct dma_fence __rcu **ptr;
+     struct amdgpu_fence *fence;
+     struct amdgpu_ring *ring = af->ring;
+     unsigned long flags;
+     u64 i, seqno;
+
+     seqno = amdgpu_fence_read(ring);
+
+     spin_lock_irqsave(&ring->fence_drv.lock, flags);
+     for (i = seqno + 1; i <= ring->fence_drv.sync_seq; ++i) {

That won't work, the seqno can wrap around.

I would just go over all fences, e.g. from 0 to end of array.

The checks below should make sure that we don't try to set an error on something already processed.

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c void amdgpu_fence_driver_force_completion(struct amdgpu_ring *ring) · line 778

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +5 -0
+             ptr = &ring->fence_drv.fences[i & ring->fence_drv.num_fences_mask];
+             rcu_read_lock();
+             unprocessed = rcu_dereference(*ptr);
+
+             if (unprocessed && !dma_fence_is_signaled(unprocessed)) {

dma_fence_is_signaled_locked(), otherwise the function would try to lock the same spinlock again.

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +10 -0
+                     fence = container_of(unprocessed, struct amdgpu_fence, base);
+
+                     if (fence == af)
+                             dma_fence_set_error(&fence->base, -ETIME);
+                     else if (fence->context == af->context)
+                             dma_fence_set_error(&fence->base, -ECANCELED);
+             }
+             rcu_read_unlock();
+     }
+     spin_unlock_irqrestore(&ring->fence_drv.lock, flags);
Quoted 4 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c void amdgpu_fence_driver_force_completion(struct amdgpu_ring *ring) · line 790

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +2 -0
+     amdgpu_fence_write(ring, af->seq);
+     amdgpu_fence_process(ring);

That's actually wrong. We want the other fences to signal later on when we process them.

This fence is the original guilty fence. The other fences will naturally signal when the later fences signal.

Alex

Quoted 34 lines

Regards,
Christian.

Quoted 29 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +0 -0
 }
 
 void amdgpu_fence_save_wptr(struct dma_fence *fence)
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +1 -1
index 6379bb25bf5ce..f6c9decedbd51 100644
@@ -812,7 +812,7 @@ int amdgpu_ring_reset_helper_end(struct amdgpu_ring *ring,
      if (r)
              return r;
 
-     /* signal the fence of the bad job */
+     /* signal the guilty fence and set an error on all fences from the context */
      if (guilty_fence)
              amdgpu_fence_driver_guilty_force_completion(guilty_fence);
      /* Re-emit the non-guilty commands */
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h +1 -1
index 7670f5d82b9e4..0704fd9b85fdb 100644
@@ -155,7 +155,7 @@ extern const struct drm_sched_backend_ops amdgpu_sched_ops;
 void amdgpu_fence_driver_clear_job_fences(struct amdgpu_ring *ring);
 void amdgpu_fence_driver_set_error(struct amdgpu_ring *ring, int error);
 void amdgpu_fence_driver_force_completion(struct amdgpu_ring *ring);
-void amdgpu_fence_driver_guilty_force_completion(struct amdgpu_fence *fence);
+void amdgpu_fence_driver_guilty_force_completion(struct amdgpu_fence *af);
 void amdgpu_fence_save_wptr(struct dma_fence *fence);
 
 int amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring);
maintainer
#4 lore raw
Quoted 73 lines · Alex Deucher
Quoted 71 lines · Christian König
Quoted 31 lines · Alex Deucher

When we backup ring contents to reemit after a queue reset, we don't backup ring contents from the bad context. When we signal the fences, we should set an error on those fences as well.

v2: misc cleanups
v3: add locking for fence error, fix comment (Christian)

Fixes 77cc0da39c7c ("drm/amdgpu: track ring state associated with a fence")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c | 33 ++++++++++++++++++++---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c  |  2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h  |  2 +-
 3 files changed, 31 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +2 -4
index fd8cca241da62..72f0f16924476 100644
@@ -758,11 +758,36 @@ void amdgpu_fence_driver_force_completion(struct amdgpu_ring *ring)
  * @fence: fence of the ring to signal
  *
  */
-void amdgpu_fence_driver_guilty_force_completion(struct amdgpu_fence *fence)
+void amdgpu_fence_driver_guilty_force_completion(struct amdgpu_fence *af)
 {
-     dma_fence_set_error(&fence->base, -ETIME);
-     amdgpu_fence_write(fence->ring, fence->seq);
-     amdgpu_fence_process(fence->ring);
+     struct dma_fence *unprocessed;

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c void amdgpu_fence_driver_force_completion(struct amdgpu_ring *ring) · line 773

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +10 -0
+     struct dma_fence __rcu **ptr;
+     struct amdgpu_fence *fence;
+     struct amdgpu_ring *ring = af->ring;
+     unsigned long flags;
+     u64 i, seqno;
+
+     seqno = amdgpu_fence_read(ring);
+
+     spin_lock_irqsave(&ring->fence_drv.lock, flags);
+     for (i = seqno + 1; i <= ring->fence_drv.sync_seq; ++i) {

That won't work, the seqno can wrap around.

I would just go over all fences, e.g. from 0 to end of array.

The checks below should make sure that we don't try to set an error on something already processed.

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c void amdgpu_fence_driver_force_completion(struct amdgpu_ring *ring) · line 778

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +5 -0
+             ptr = &ring->fence_drv.fences[i & ring->fence_drv.num_fences_mask];
+             rcu_read_lock();
+             unprocessed = rcu_dereference(*ptr);
+
+             if (unprocessed && !dma_fence_is_signaled(unprocessed)) {

dma_fence_is_signaled_locked(), otherwise the function would try to lock the same spinlock again.

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +10 -0
+                     fence = container_of(unprocessed, struct amdgpu_fence, base);
+
+                     if (fence == af)
+                             dma_fence_set_error(&fence->base, -ETIME);
+                     else if (fence->context == af->context)
+                             dma_fence_set_error(&fence->base, -ECANCELED);
+             }
+             rcu_read_unlock();
+     }
+     spin_unlock_irqrestore(&ring->fence_drv.lock, flags);
Quoted 7 lines
Quoted 4 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c void amdgpu_fence_driver_force_completion(struct amdgpu_ring *ring) · line 790

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +2 -0
+     amdgpu_fence_write(ring, af->seq);
+     amdgpu_fence_process(ring);

That's actually wrong. We want the other fences to signal later on when we process them.

This fence is the original guilty fence. The other fences will naturally signal when the later fences signal.

Ah, got it. But that is still racy as hell.

We should probably rather signal the guilty fence separately by calling dma_fence_signal().

Regards,
Christian.

Quoted 37 lines

Alex

Quoted 34 lines

Regards,
Christian.

Quoted 29 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +0 -0
 }
 
 void amdgpu_fence_save_wptr(struct dma_fence *fence)
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +1 -1
index 6379bb25bf5ce..f6c9decedbd51 100644
@@ -812,7 +812,7 @@ int amdgpu_ring_reset_helper_end(struct amdgpu_ring *ring,
      if (r)
              return r;
 
-     /* signal the fence of the bad job */
+     /* signal the guilty fence and set an error on all fences from the context */
      if (guilty_fence)
              amdgpu_fence_driver_guilty_force_completion(guilty_fence);
      /* Re-emit the non-guilty commands */
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h +1 -1
index 7670f5d82b9e4..0704fd9b85fdb 100644
@@ -155,7 +155,7 @@ extern const struct drm_sched_backend_ops amdgpu_sched_ops;
 void amdgpu_fence_driver_clear_job_fences(struct amdgpu_ring *ring);
 void amdgpu_fence_driver_set_error(struct amdgpu_ring *ring, int error);
 void amdgpu_fence_driver_force_completion(struct amdgpu_ring *ring);
-void amdgpu_fence_driver_guilty_force_completion(struct amdgpu_fence *fence);
+void amdgpu_fence_driver_guilty_force_completion(struct amdgpu_fence *af);
 void amdgpu_fence_save_wptr(struct dma_fence *fence);
 
 int amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring);
#5 lore raw
Quoted 78 lines · Christian König
Quoted 76 lines · Alex Deucher
Quoted 74 lines · Christian König
Quoted 31 lines · Alex Deucher

When we backup ring contents to reemit after a queue reset, we don't backup ring contents from the bad context. When we signal the fences, we should set an error on those fences as well.

v2: misc cleanups
v3: add locking for fence error, fix comment (Christian)

Fixes 77cc0da39c7c ("drm/amdgpu: track ring state associated with a fence")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c | 33 ++++++++++++++++++++---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c  |  2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h  |  2 +-
 3 files changed, 31 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +2 -4
index fd8cca241da62..72f0f16924476 100644
@@ -758,11 +758,36 @@ void amdgpu_fence_driver_force_completion(struct amdgpu_ring *ring)
  * @fence: fence of the ring to signal
  *
  */
-void amdgpu_fence_driver_guilty_force_completion(struct amdgpu_fence *fence)
+void amdgpu_fence_driver_guilty_force_completion(struct amdgpu_fence *af)
 {
-     dma_fence_set_error(&fence->base, -ETIME);
-     amdgpu_fence_write(fence->ring, fence->seq);
-     amdgpu_fence_process(fence->ring);
+     struct dma_fence *unprocessed;

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c void amdgpu_fence_driver_force_completion(struct amdgpu_ring *ring) · line 773

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +10 -0
+     struct dma_fence __rcu **ptr;
+     struct amdgpu_fence *fence;
+     struct amdgpu_ring *ring = af->ring;
+     unsigned long flags;
+     u64 i, seqno;
+
+     seqno = amdgpu_fence_read(ring);
+
+     spin_lock_irqsave(&ring->fence_drv.lock, flags);
+     for (i = seqno + 1; i <= ring->fence_drv.sync_seq; ++i) {

That won't work, the seqno can wrap around.

I would just go over all fences, e.g. from 0 to end of array.

The checks below should make sure that we don't try to set an error on something already processed.

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c void amdgpu_fence_driver_force_completion(struct amdgpu_ring *ring) · line 778

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +5 -0
+             ptr = &ring->fence_drv.fences[i & ring->fence_drv.num_fences_mask];
+             rcu_read_lock();
+             unprocessed = rcu_dereference(*ptr);
+
+             if (unprocessed && !dma_fence_is_signaled(unprocessed)) {

dma_fence_is_signaled_locked(), otherwise the function would try to lock the same spinlock again.

Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +12 -0
+                     fence = container_of(unprocessed, struct amdgpu_fence, base);
+
+                     if (fence == af)
+                             dma_fence_set_error(&fence->base, -ETIME);
+                     else if (fence->context == af->context)
+                             dma_fence_set_error(&fence->base, -ECANCELED);
+             }
+             rcu_read_unlock();
+     }
+     spin_unlock_irqrestore(&ring->fence_drv.lock, flags);
 
+     amdgpu_fence_write(ring, af->seq);
+     amdgpu_fence_process(ring);
Quoted 8 lines
Quoted 4 lines
Quoted 1 line

That's actually wrong. We want the other fences to signal later on when we process them.

This fence is the original guilty fence. The other fences will naturally signal when the later fences signal.

Ah, got it. But that is still racy as hell.

We should probably rather signal the guilty fence separately by calling dma_fence_signal().

What is it racing with? amdgpu_fence_process() ultimately calls dma_fence_signal(), plus it handles various AMD specific bookkeeping.

Alex

Quoted 42 lines

Regards,
Christian.

Quoted 37 lines

Alex

Quoted 34 lines

Regards,
Christian.

Quoted 29 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +0 -0
 }
 

void amdgpu_fence_save_wptr(struct dma_fence *fence)

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +1 -1
index 6379bb25bf5ce..f6c9decedbd51 100644
@@ -812,7 +812,7 @@ int amdgpu_ring_reset_helper_end(struct amdgpu_ring *ring,
      if (r)
              return r;
 
-     /* signal the fence of the bad job */
+     /* signal the guilty fence and set an error on all fences from the context */
      if (guilty_fence)
              amdgpu_fence_driver_guilty_force_completion(guilty_fence);
      /* Re-emit the non-guilty commands */
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h +1 -1
index 7670f5d82b9e4..0704fd9b85fdb 100644
@@ -155,7 +155,7 @@ extern const struct drm_sched_backend_ops amdgpu_sched_ops;
 void amdgpu_fence_driver_clear_job_fences(struct amdgpu_ring *ring);
 void amdgpu_fence_driver_set_error(struct amdgpu_ring *ring, int error);
 void amdgpu_fence_driver_force_completion(struct amdgpu_ring *ring);
-void amdgpu_fence_driver_guilty_force_completion(struct amdgpu_fence *fence);
+void amdgpu_fence_driver_guilty_force_completion(struct amdgpu_fence *af);
 void amdgpu_fence_save_wptr(struct dma_fence *fence);
 
 int amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring);
maintainer
#6 lore raw
Quoted 85 lines · Alex Deucher
Quoted 83 lines · Christian König
Quoted 80 lines · Alex Deucher
Quoted 71 lines · Christian König
Quoted 31 lines · Alex Deucher

When we backup ring contents to reemit after a queue reset, we don't backup ring contents from the bad context. When we signal the fences, we should set an error on those fences as well.

v2: misc cleanups
v3: add locking for fence error, fix comment (Christian)

Fixes 77cc0da39c7c ("drm/amdgpu: track ring state associated with a fence")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c | 33 ++++++++++++++++++++---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c  |  2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h  |  2 +-
 3 files changed, 31 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +2 -4
index fd8cca241da62..72f0f16924476 100644
@@ -758,11 +758,36 @@ void amdgpu_fence_driver_force_completion(struct amdgpu_ring *ring)
  * @fence: fence of the ring to signal
  *
  */
-void amdgpu_fence_driver_guilty_force_completion(struct amdgpu_fence *fence)
+void amdgpu_fence_driver_guilty_force_completion(struct amdgpu_fence *af)
 {
-     dma_fence_set_error(&fence->base, -ETIME);
-     amdgpu_fence_write(fence->ring, fence->seq);
-     amdgpu_fence_process(fence->ring);
+     struct dma_fence *unprocessed;

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c void amdgpu_fence_driver_force_completion(struct amdgpu_ring *ring) · line 773

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +10 -0
+     struct dma_fence __rcu **ptr;
+     struct amdgpu_fence *fence;
+     struct amdgpu_ring *ring = af->ring;
+     unsigned long flags;
+     u64 i, seqno;
+
+     seqno = amdgpu_fence_read(ring);
+
+     spin_lock_irqsave(&ring->fence_drv.lock, flags);
+     for (i = seqno + 1; i <= ring->fence_drv.sync_seq; ++i) {

That won't work, the seqno can wrap around.

I would just go over all fences, e.g. from 0 to end of array.

The checks below should make sure that we don't try to set an error on something already processed.

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c void amdgpu_fence_driver_force_completion(struct amdgpu_ring *ring) · line 778

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +5 -0
+             ptr = &ring->fence_drv.fences[i & ring->fence_drv.num_fences_mask];
+             rcu_read_lock();
+             unprocessed = rcu_dereference(*ptr);
+
+             if (unprocessed && !dma_fence_is_signaled(unprocessed)) {

dma_fence_is_signaled_locked(), otherwise the function would try to lock the same spinlock again.

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +10 -0
+                     fence = container_of(unprocessed, struct amdgpu_fence, base);
+
+                     if (fence == af)
+                             dma_fence_set_error(&fence->base, -ETIME);
+                     else if (fence->context == af->context)
+                             dma_fence_set_error(&fence->base, -ECANCELED);
+             }
+             rcu_read_unlock();
+     }
+     spin_unlock_irqrestore(&ring->fence_drv.lock, flags);
Quoted 4 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c void amdgpu_fence_driver_force_completion(struct amdgpu_ring *ring) · line 790

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +2 -0
+     amdgpu_fence_write(ring, af->seq);
+     amdgpu_fence_process(ring);

That's actually wrong. We want the other fences to signal later on when we process them.

This fence is the original guilty fence. The other fences will naturally signal when the later fences signal.

Quoted 6 lines
Quoted 3 lines

Ah, got it. But that is still racy as hell.

We should probably rather signal the guilty fence separately by calling dma_fence_signal().

What is it racing with? amdgpu_fence_process() ultimately calls dma_fence_signal(), plus it handles various AMD specific bookkeeping.

Potentially concurrent signaling. Keep in mind that it is perfectly possible that the guilty fence just completes while we do all this here.

Regards,
Christian.

Quoted 45 lines

Alex

Quoted 42 lines

Regards,
Christian.

Quoted 37 lines

Alex

Quoted 34 lines

Regards,
Christian.

Quoted 29 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +0 -0
 }
 
 void amdgpu_fence_save_wptr(struct dma_fence *fence)
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +1 -1
index 6379bb25bf5ce..f6c9decedbd51 100644
@@ -812,7 +812,7 @@ int amdgpu_ring_reset_helper_end(struct amdgpu_ring *ring,
      if (r)
              return r;
 
-     /* signal the fence of the bad job */
+     /* signal the guilty fence and set an error on all fences from the context */
      if (guilty_fence)
              amdgpu_fence_driver_guilty_force_completion(guilty_fence);
      /* Re-emit the non-guilty commands */
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h +1 -1
index 7670f5d82b9e4..0704fd9b85fdb 100644
@@ -155,7 +155,7 @@ extern const struct drm_sched_backend_ops amdgpu_sched_ops;
 void amdgpu_fence_driver_clear_job_fences(struct amdgpu_ring *ring);
 void amdgpu_fence_driver_set_error(struct amdgpu_ring *ring, int error);
 void amdgpu_fence_driver_force_completion(struct amdgpu_ring *ring);
-void amdgpu_fence_driver_guilty_force_completion(struct amdgpu_fence *fence);
+void amdgpu_fence_driver_guilty_force_completion(struct amdgpu_fence *af);
 void amdgpu_fence_save_wptr(struct dma_fence *fence);
 
 int amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring);