Skip to contentSkip to conversation

drm/amdgpu: fix deadlock caused by overflow

PATCH In review Victor Zhao 5 participants 21 messages Sep 14, 2022 – Sep 20, 2022

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

PATCH 1/2 drm/amdgpu: fix deadlock caused by overflow

[background]
For a gpu recovery caused by a hang on one ring (e.g. compute), jobs from another ring (e.g. gfx) may continue signaling during drm_sched_stop stage. The signal bit will not be cleared.

At the resubmit stage after recovery, the job with hw fence signaled bit set will call job done directly instead go through fence process. This makes the hw_rq_count decrease but rcu fence pointer not cleared yet.

Then overflow happens in the fence driver slots and some jobs may be skipped and leave the rcu pointer not cleared which makes an infinite wait for the slot on the next fence emitted.

This infinite wait cause a job timeout on the emitting job. And driver will stuck at the its sched stop step because kthread_park cannot be done.

[how]
1. move amdgpu_fence_driver_isr_toggle earlier to close interrupt before drm sched stop
2. handle all fences in fence process to aviod skip when overflow happens

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 16 +++++++++++++---
 drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c  |  6 +-----
 2 files changed, 14 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +13 -3
index 943c9e750575..c0cfae52f12b 100644
@@ -4610,8 +4610,6 @@ int amdgpu_device_pre_asic_reset(struct amdgpu_device *adev,
 		amdgpu_virt_fini_data_exchange(adev);
 	}
 
-	amdgpu_fence_driver_isr_toggle(adev, true);
-
 	/* block all schedulers and reset given job's ring */
 	for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
 		struct amdgpu_ring *ring = adev->rings[i];
@@ -5214,6 +5212,8 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
 		      amdgpu_device_ip_need_full_reset(tmp_adev))
 			amdgpu_ras_suspend(tmp_adev);
 
+		amdgpu_fence_driver_isr_toggle(tmp_adev, true);
+
 		for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
 			struct amdgpu_ring *ring = tmp_adev->rings[i];
 
@@ -5228,8 +5228,12 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
 		atomic_inc(&tmp_adev->gpu_reset_counter);
 	}
 
-	if (need_emergency_restart)
+	if (need_emergency_restart) {
+		list_for_each_entry (tmp_adev, device_list_handle, reset_list) {
+			amdgpu_fence_driver_isr_toggle(tmp_adev, false);
+		}
 		goto skip_sched_resume;
+	}
 
 	/*
 	 * Must check guilty signal here since after this point all old
@@ -5240,6 +5244,9 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
 	if (job && dma_fence_is_signaled(&job->hw_fence)) {
 		job_signaled = true;
 		dev_info(adev->dev, "Guilty job already signaled, skipping HW reset");
+		list_for_each_entry (tmp_adev, device_list_handle, reset_list) {
+			amdgpu_fence_driver_isr_toggle(tmp_adev, false);
+		}
 		goto skip_hw_reset;
 	}
 
@@ -5276,6 +5283,7 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
 		if (r && r == -EAGAIN) {
 			set_bit(AMDGPU_SKIP_MODE2_RESET, &reset_context->flags);
 			adev->asic_reset_res = 0;
+			amdgpu_fence_driver_isr_toggle(adev, true);
 			goto retry;
 		}
 	}
@@ -5711,6 +5719,8 @@ pci_ers_result_t amdgpu_pci_slot_reset(struct pci_dev *pdev)
 	set_bit(AMDGPU_SKIP_HW_RESET, &reset_context.flags);
 	set_bit(AMDGPU_SKIP_MODE2_RESET, &reset_context.flags);
 
+	amdgpu_fence_driver_isr_toggle(adev, true);
+
 	adev->no_hw_access = true;
 	r = amdgpu_device_pre_asic_reset(adev, &reset_context);
 	adev->no_hw_access = false;
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +1 -5
index 8adeb7469f1e..65a877e1a7fc 100644
@@ -287,15 +287,11 @@ bool amdgpu_fence_process(struct amdgpu_ring *ring)
 	if (unlikely(seq == last_seq))
 		return false;
 
-	last_seq &= drv->num_fences_mask;
-	seq &= drv->num_fences_mask;
-
 	do {
 		struct dma_fence *fence, **ptr;
 
 		++last_seq;
-		last_seq &= drv->num_fences_mask;
-		ptr = &drv->fences[last_seq];
+		ptr = &drv->fences[last_seq & drv->num_fences_mask];
 
 		/* There is always exactly one thread signaling this fence slot */
 		fence = rcu_dereference_protected(*ptr, 1);
Signature · 1 line
2.25.1
#1 lore raw

PATCH 2/2 drm/amdgpu: move enable irq later to avoid race with ih resume

[background]
On current sienna cichlid mode2 reset, on the slow job hang cases, since page table context was reverted to completely stop gpu, it will generate page fault interrupt.

Since the irq are open during recovery stage, during ih resume step, if this interrupt was in processing, which increased ih ring rptr, and ih resume meanwhile will set rptr and wptr to 0. This may cause rptr greater than wptr. Such case was not handled in ih process, and it will cause rptr continue increasing util reaches the max. Such case will make fence fallback situation happen.

[how]
Move the enable of irq after ih resumed and before ib test. Adjusting the position of enable irq on other reset paths accordingly.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c  | 8 ++++----
 drivers/gpu/drm/amd/amdgpu/sienna_cichlid.c | 1 +
 2 files changed, 5 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +4 -4
index c0cfae52f12b..0b658225e9ef 100644
@@ -4625,8 +4625,6 @@ int amdgpu_device_pre_asic_reset(struct amdgpu_device *adev,
 		amdgpu_fence_driver_force_completion(ring);
 	}
 
-	amdgpu_fence_driver_isr_toggle(adev, false);
-
 	if (job && job->vm)
 		drm_sched_increase_karma(&job->base);
 
@@ -4758,6 +4756,10 @@ int amdgpu_do_asic_reset(struct list_head *device_list_handle,
 		test_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
 	skip_hw_reset = test_bit(AMDGPU_SKIP_HW_RESET, &reset_context->flags);
 
+	list_for_each_entry (tmp_adev, device_list_handle, reset_list) {
+		amdgpu_fence_driver_isr_toggle(tmp_adev, false);
+	}
+
 	/*
 	 * ASIC reset has to be done on all XGMI hive nodes ASAP
 	 * to allow proper links negotiation in FW (within 1 sec)
@@ -5031,8 +5033,6 @@ static void amdgpu_device_recheck_guilty_jobs(
 			/* Clear this failed job from fence array */
 			amdgpu_fence_driver_clear_job_fences(ring);
 
-			amdgpu_fence_driver_isr_toggle(adev, false);
-
 			/* Since the job won't signal and we go for
 			 * another resubmit drop this parent pointer
 			 */
drivers/gpu/drm/amd/amdgpu/sienna_cichlid.c +1 -0
index 7aa570c1ce4a..953036482d1f 100644
@@ -240,6 +240,7 @@ sienna_cichlid_mode2_restore_hwcontext(struct amdgpu_reset_control *reset_ctl,
 	* Add this ASIC as tracked as reset was already
 	* complete successfully.
 	*/
+	amdgpu_fence_driver_isr_toggle(tmp_adev, false);
 	amdgpu_register_gpu_instance(tmp_adev);
 
 	/* Resume RAS */
Signature · 1 line
2.25.1
#6 lore raw
In reply to Victor Zhao

[AMD Official Use Only - General]

Ping.

Hi @Koenig, Christian and @Grodzovsky, Andrey,

We found some reset related issues during stress test on the sequence. Please help give some comments.

Thanks,
Victor

-----Original Message-----
From: Victor Zhao <[email protected]>
Sent: Wednesday, September 14, 2022 6:10 PM
To: [email protected]
Cc: Deng, Emily <[email protected]>; Grodzovsky, Andrey <[email protected]>; Zhao, Victor <[email protected]>
Subject: [PATCH 1/2] drm/amdgpu: fix deadlock caused by overflow

[background]
For a gpu recovery caused by a hang on one ring (e.g. compute), jobs from another ring (e.g. gfx) may continue signaling during drm_sched_stop stage. The signal bit will not be cleared.

At the resubmit stage after recovery, the job with hw fence signaled bit set will call job done directly instead go through fence process. This makes the hw_rq_count decrease but rcu fence pointer not cleared yet.

Then overflow happens in the fence driver slots and some jobs may be skipped and leave the rcu pointer not cleared which makes an infinite wait for the slot on the next fence emitted.

This infinite wait cause a job timeout on the emitting job. And driver will stuck at the its sched stop step because kthread_park cannot be done.

[how]
1. move amdgpu_fence_driver_isr_toggle earlier to close interrupt before drm sched stop 2. handle all fences in fence process to aviod skip when overflow happens

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 16 +++++++++++++---  drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c  |  6 +-----
 2 files changed, 14 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +0 -2
index 943c9e750575..c0cfae52f12b 100644
@@ -4610,8 +4610,6 @@ int amdgpu_device_pre_asic_reset(struct amdgpu_device *adev,
 		amdgpu_virt_fini_data_exchange(adev);
 	}
 
-	amdgpu_fence_driver_isr_toggle(adev, true);
-
 	/* block all schedulers and reset given job's ring */
 	for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
 		struct amdgpu_ring *ring = adev->rings[i]; @@ -5214,6 +5212,8 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,

amdgpu_device_ip_need_full_reset(tmp_adev))
amdgpu_ras_suspend(tmp_adev);

+ amdgpu_fence_driver_isr_toggle(tmp_adev, true);
+
for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
struct amdgpu_ring *ring = tmp_adev->rings[i];

@@ -5228,8 +5228,12 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
atomic_inc(&tmp_adev->gpu_reset_counter);
}

- if (need_emergency_restart)
+ if (need_emergency_restart) {
+ list_for_each_entry (tmp_adev, device_list_handle, reset_list) {
+ amdgpu_fence_driver_isr_toggle(tmp_adev, false);
+ }
goto skip_sched_resume;
+ }

/*
* Must check guilty signal here since after this point all old @@ -5240,6 +5244,9 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
if (job && dma_fence_is_signaled(&job->hw_fence)) {
job_signaled = true;
dev_info(adev->dev, "Guilty job already signaled, skipping HW reset");
+ list_for_each_entry (tmp_adev, device_list_handle, reset_list) {
+ amdgpu_fence_driver_isr_toggle(tmp_adev, false);
+ }
goto skip_hw_reset;
}

@@ -5276,6 +5283,7 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
if (r && r == -EAGAIN) {
set_bit(AMDGPU_SKIP_MODE2_RESET, &reset_context->flags); adev->asic_reset_res = 0;
+ amdgpu_fence_driver_isr_toggle(adev, true);
goto retry;
}
}
@@ -5711,6 +5719,8 @@ pci_ers_result_t amdgpu_pci_slot_reset(struct pci_dev *pdev)
set_bit(AMDGPU_SKIP_HW_RESET, &reset_context.flags); set_bit(AMDGPU_SKIP_MODE2_RESET, &reset_context.flags);

+ amdgpu_fence_driver_isr_toggle(adev, true);
+
adev->no_hw_access = true;
r = amdgpu_device_pre_asic_reset(adev, &reset_context); adev->no_hw_access = false;

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +1 -5
index 8adeb7469f1e..65a877e1a7fc 100644
@@ -287,15 +287,11 @@ bool amdgpu_fence_process(struct amdgpu_ring *ring)
 	if (unlikely(seq == last_seq))
 		return false;
 
-	last_seq &= drv->num_fences_mask;
-	seq &= drv->num_fences_mask;
-
 	do {
 		struct dma_fence *fence, **ptr;
 
 		++last_seq;
-		last_seq &= drv->num_fences_mask;
-		ptr = &drv->fences[last_seq];
+		ptr = &drv->fences[last_seq & drv->num_fences_mask];
 
 		/* There is always exactly one thread signaling this fence slot */
 		fence = rcu_dereference_protected(*ptr, 1);

--
2.25.1

#2 lore raw

Re: [PATCH 2/2] drm/amdgpu: move enable irq later to avoid race with ih resume

In reply to Victor Zhao
Quoted 8 lines · Victor Zhao

[background]
On current sienna cichlid mode2 reset, on the slow job hang cases, since page table context was reverted to completely stop gpu, it will generate page fault interrupt.

Since the irq are open during recovery stage, during ih resume step, if this interrupt was in processing, which increased ih ring rptr, and ih resume meanwhile will set rptr and wptr to 0. This may cause

AFAIK, only GFX/SDMA are affected by mode-2. IH is not suspended before mode-2. Why do you resume IH after mode-2 when it is not suspended? Is it a special case for virtualization?

Thanks,
Lijo

Quoted 60 lines

rptr greater than wptr. Such case was not handled in ih process, and it will cause rptr continue increasing util reaches the max. Such case will make fence fallback situation happen.

[how]
Move the enable of irq after ih resumed and before ib test. Adjusting the position of enable irq on other reset paths accordingly.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_device.c  | 8 ++++----
  drivers/gpu/drm/amd/amdgpu/sienna_cichlid.c | 1 +
  2 files changed, 5 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +4 -4
index c0cfae52f12b..0b658225e9ef 100644
@@ -4625,8 +4625,6 @@ int amdgpu_device_pre_asic_reset(struct amdgpu_device *adev,
  		amdgpu_fence_driver_force_completion(ring);
  	}
 
-	amdgpu_fence_driver_isr_toggle(adev, false);
-
  	if (job && job->vm)
  		drm_sched_increase_karma(&job->base);
 
@@ -4758,6 +4756,10 @@ int amdgpu_do_asic_reset(struct list_head *device_list_handle,
  		test_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
  	skip_hw_reset = test_bit(AMDGPU_SKIP_HW_RESET, &reset_context->flags);
 
+	list_for_each_entry (tmp_adev, device_list_handle, reset_list) {
+		amdgpu_fence_driver_isr_toggle(tmp_adev, false);
+	}
+
  	/*
  	 * ASIC reset has to be done on all XGMI hive nodes ASAP
  	 * to allow proper links negotiation in FW (within 1 sec)
@@ -5031,8 +5033,6 @@ static void amdgpu_device_recheck_guilty_jobs(
  			/* Clear this failed job from fence array */
  			amdgpu_fence_driver_clear_job_fences(ring);
 
-			amdgpu_fence_driver_isr_toggle(adev, false);
-
  			/* Since the job won't signal and we go for
  			 * another resubmit drop this parent pointer
  			 */
drivers/gpu/drm/amd/amdgpu/sienna_cichlid.c +1 -0
index 7aa570c1ce4a..953036482d1f 100644
@@ -240,6 +240,7 @@ sienna_cichlid_mode2_restore_hwcontext(struct amdgpu_reset_control *reset_ctl,
  	* Add this ASIC as tracked as reset was already
  	* complete successfully.
  	*/
+	amdgpu_fence_driver_isr_toggle(tmp_adev, false);
  	amdgpu_register_gpu_instance(tmp_adev);
 
  	/* Resume RAS */
maintainer
#7 lore raw
In reply to Zhao, Victor

Am 15.09.22 um 06:02 schrieb Zhao, Victor:

Quoted 111 lines

[AMD Official Use Only - General]

Ping.

Hi @Koenig, Christian and @Grodzovsky, Andrey,

We found some reset related issues during stress test on the sequence. Please help give some comments.

Thanks,
Victor

-----Original Message-----
From: Victor Zhao <[email protected]>
Sent: Wednesday, September 14, 2022 6:10 PM
To: [email protected]
Cc: Deng, Emily <[email protected]>; Grodzovsky, Andrey <[email protected]>; Zhao, Victor <[email protected]>
Subject: [PATCH 1/2] drm/amdgpu: fix deadlock caused by overflow

[background]
For a gpu recovery caused by a hang on one ring (e.g. compute), jobs from another ring (e.g. gfx) may continue signaling during drm_sched_stop stage. The signal bit will not be cleared.

At the resubmit stage after recovery, the job with hw fence signaled bit set will call job done directly instead go through fence process. This makes the hw_rq_count decrease but rcu fence pointer not cleared yet.

Then overflow happens in the fence driver slots and some jobs may be skipped and leave the rcu pointer not cleared which makes an infinite wait for the slot on the next fence emitted.

This infinite wait cause a job timeout on the emitting job. And driver will stuck at the its sched stop step because kthread_park cannot be done.

[how]
1. move amdgpu_fence_driver_isr_toggle earlier to close interrupt before drm sched stop 2. handle all fences in fence process to aviod skip when overflow happens

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 16 +++++++++++++---  drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c  |  6 +-----
  2 files changed, 14 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +0 -2
index 943c9e750575..c0cfae52f12b 100644
@@ -4610,8 +4610,6 @@ int amdgpu_device_pre_asic_reset(struct amdgpu_device *adev,
  		amdgpu_virt_fini_data_exchange(adev);
  	}
 
-	amdgpu_fence_driver_isr_toggle(adev, true);
-
  	/* block all schedulers and reset given job's ring */
  	for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  		struct amdgpu_ring *ring = adev->rings[i]; @@ -5214,6 +5212,8 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
  		      amdgpu_device_ip_need_full_reset(tmp_adev))
  			amdgpu_ras_suspend(tmp_adev);
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +7 -1
+		amdgpu_fence_driver_isr_toggle(tmp_adev, true);
+
  		for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  			struct amdgpu_ring *ring = tmp_adev->rings[i];
@@ -5228,8 +5228,12 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
  		atomic_inc(&tmp_adev->gpu_reset_counter);
  	}
 
-	if (need_emergency_restart)
+	if (need_emergency_restart) {
+		list_for_each_entry (tmp_adev, device_list_handle, reset_list) {
+			amdgpu_fence_driver_isr_toggle(tmp_adev, false);
+		}
  		goto skip_sched_resume;
+	}
 
  	/*
  	 * Must check guilty signal here since after this point all old @@ -5240,6 +5244,9 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
  	if (job && dma_fence_is_signaled(&job->hw_fence)) {
  		job_signaled = true;
  		dev_info(adev->dev, "Guilty job already signaled, skipping HW reset");
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +6 -0
+		list_for_each_entry (tmp_adev, device_list_handle, reset_list) {
+			amdgpu_fence_driver_isr_toggle(tmp_adev, false);
+		}
  		goto skip_hw_reset;
  	}
@@ -5276,6 +5283,7 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
  		if (r && r == -EAGAIN) {
  			set_bit(AMDGPU_SKIP_MODE2_RESET, &reset_context->flags);
  			adev->asic_reset_res = 0;
+			amdgpu_fence_driver_isr_toggle(adev, true);
  			goto retry;
  		}
  	}
@@ -5711,6 +5719,8 @@ pci_ers_result_t amdgpu_pci_slot_reset(struct pci_dev *pdev)
  	set_bit(AMDGPU_SKIP_HW_RESET, &reset_context.flags);
  	set_bit(AMDGPU_SKIP_MODE2_RESET, &reset_context.flags);
 
+	amdgpu_fence_driver_isr_toggle(adev, true);
+
  	adev->no_hw_access = true;
  	r = amdgpu_device_pre_asic_reset(adev, &reset_context);
  	adev->no_hw_access = false;
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +0 -3
index 8adeb7469f1e..65a877e1a7fc 100644
@@ -287,15 +287,11 @@ bool amdgpu_fence_process(struct amdgpu_ring *ring)
  	if (unlikely(seq == last_seq))
  		return false;
 
-	last_seq &= drv->num_fences_mask;
-	seq &= drv->num_fences_mask;
-

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c bool amdgpu_fence_process(struct amdgpu_ring *ring) · line 297

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +1 -2
  	do {
  		struct dma_fence *fence, **ptr;
 
  		++last_seq;
-		last_seq &= drv->num_fences_mask;
-		ptr = &drv->fences[last_seq];
+		ptr = &drv->fences[last_seq & drv->num_fences_mask];
 
  		/* There is always exactly one thread signaling this fence slot */
  		fence = rcu_dereference_protected(*ptr, 1);

Those changes here doesn't seem to make sense. Please explain further why that is necessary.

Christian.

Quoted 2 lines

--
2.25.1

#3 lore raw

RE: [PATCH 2/2] drm/amdgpu: move enable irq later to avoid race with ih resume

In reply to Lazar, Lijo

[AMD Official Use Only - General]

Hi Lijo,

IH resume was added to resolve an issue found during mode2 bring up on sienna cichlid:
- close down mode2 reset and do a mode1 reset first
- open mode2 reset and do a mode2 reset. Mode2 reset was found fail in this case.

Resume IH helps in this case

Thanks,
Victor

-----Original Message-----
From: Lazar, Lijo <[email protected]>
Sent: Thursday, September 15, 2022 1:58 PM
To: Zhao, Victor <[email protected]>; [email protected]
Cc: Deng, Emily <[email protected]>; Grodzovsky, Andrey <[email protected]>
Subject: Re: [PATCH 2/2] drm/amdgpu: move enable irq later to avoid race with ih resume

Quoted 8 lines · Victor Zhao

[background]
On current sienna cichlid mode2 reset, on the slow job hang cases, since page table context was reverted to completely stop gpu, it will generate page fault interrupt.

Since the irq are open during recovery stage, during ih resume step, if this interrupt was in processing, which increased ih ring rptr, and ih resume meanwhile will set rptr and wptr to 0. This may cause

AFAIK, only GFX/SDMA are affected by mode-2. IH is not suspended before mode-2. Why do you resume IH after mode-2 when it is not suspended? Is it a special case for virtualization?

Thanks,
Lijo

Quoted 63 lines

rptr greater than wptr. Such case was not handled in ih process, and it will cause rptr continue increasing util reaches the max. Such case will make fence fallback situation happen.

[how]
Move the enable of irq after ih resumed and before ib test. Adjusting the position of enable irq on other reset paths accordingly.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_device.c  | 8 ++++----
  drivers/gpu/drm/amd/amdgpu/sienna_cichlid.c | 1 +
  2 files changed, 5 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +4 -2
index c0cfae52f12b..0b658225e9ef 100644
@@ -4625,8 +4625,6 @@ int amdgpu_device_pre_asic_reset(struct amdgpu_device *adev,
  		amdgpu_fence_driver_force_completion(ring);
  	}
 
-	amdgpu_fence_driver_isr_toggle(adev, false);
-
  	if (job && job->vm)
  		drm_sched_increase_karma(&job->base);
 
@@ -4758,6 +4756,10 @@ int amdgpu_do_asic_reset(struct list_head *device_list_handle,
  		test_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
  	skip_hw_reset = test_bit(AMDGPU_SKIP_HW_RESET, 
 &reset_context->flags);
 
+	list_for_each_entry (tmp_adev, device_list_handle, reset_list) {
+		amdgpu_fence_driver_isr_toggle(tmp_adev, false);
+	}
+
  	/*
  	 * ASIC reset has to be done on all XGMI hive nodes ASAP

* to allow proper links negotiation in FW (within 1 sec) @@
-5031,8 +5033,6 @@ static void amdgpu_device_recheck_guilty_jobs(
/* Clear this failed job from fence array */ amdgpu_fence_driver_clear_job_fences(ring);

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +0 -2
-			amdgpu_fence_driver_isr_toggle(adev, false);
-
  			/* Since the job won't signal and we go for
  			 * another resubmit drop this parent pointer
  			 */
drivers/gpu/drm/amd/amdgpu/sienna_cichlid.c +1 -0
index 7aa570c1ce4a..953036482d1f 100644
@@ -240,6 +240,7 @@ sienna_cichlid_mode2_restore_hwcontext(struct amdgpu_reset_control *reset_ctl,
  	* Add this ASIC as tracked as reset was already
  	* complete successfully.
  	*/
+	amdgpu_fence_driver_isr_toggle(tmp_adev, false);
  	amdgpu_register_gpu_instance(tmp_adev);
 
  	/* Resume RAS */
#4 lore raw

Re: [PATCH 2/2] drm/amdgpu: move enable irq later to avoid race with ih resume

Quoted 10 lines · Victor

[AMD Official Use Only - General]

Hi Lijo,

IH resume was added to resolve an issue found during mode2 bring up on sienna cichlid:
- close down mode2 reset and do a mode1 reset first
- open mode2 reset and do a mode2 reset. Mode2 reset was found fail in this case.

Resume IH helps in this case

Sorry, what do you mean by 'close down' mode2 /'open mode2 reset'? Do you mean if mode-1 reset is done first, a subsequent mode-2 reset doesn't work without IH resume?

Thanks,
Lijo

Quoted 93 lines

Thanks,
Victor

-----Original Message-----
From: Lazar, Lijo <[email protected]>
Sent: Thursday, September 15, 2022 1:58 PM
To: Zhao, Victor <[email protected]>; [email protected]
Cc: Deng, Emily <[email protected]>; Grodzovsky, Andrey <[email protected]>
Subject: Re: [PATCH 2/2] drm/amdgpu: move enable irq later to avoid race with ih resume

Quoted 8 lines · Victor Zhao

[background]
On current sienna cichlid mode2 reset, on the slow job hang cases, since page table context was reverted to completely stop gpu, it will generate page fault interrupt.

Since the irq are open during recovery stage, during ih resume step, if this interrupt was in processing, which increased ih ring rptr, and ih resume meanwhile will set rptr and wptr to 0. This may cause

AFAIK, only GFX/SDMA are affected by mode-2. IH is not suspended before mode-2. Why do you resume IH after mode-2 when it is not suspended? Is it a special case for virtualization?

Thanks,
Lijo

Quoted 63 lines

rptr greater than wptr. Such case was not handled in ih process, and it will cause rptr continue increasing util reaches the max. Such case will make fence fallback situation happen.

[how]
Move the enable of irq after ih resumed and before ib test. Adjusting the position of enable irq on other reset paths accordingly.

---
   drivers/gpu/drm/amd/amdgpu/amdgpu_device.c  | 8 ++++----
   drivers/gpu/drm/amd/amdgpu/sienna_cichlid.c | 1 +
   2 files changed, 5 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +4 -2
index c0cfae52f12b..0b658225e9ef 100644
@@ -4625,8 +4625,6 @@ int amdgpu_device_pre_asic_reset(struct amdgpu_device *adev,
   		amdgpu_fence_driver_force_completion(ring);
   	}
 
-	amdgpu_fence_driver_isr_toggle(adev, false);
-
   	if (job && job->vm)
   		drm_sched_increase_karma(&job->base);
 
@@ -4758,6 +4756,10 @@ int amdgpu_do_asic_reset(struct list_head *device_list_handle,
   		test_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
   	skip_hw_reset = test_bit(AMDGPU_SKIP_HW_RESET,
 &reset_context->flags);
 
+	list_for_each_entry (tmp_adev, device_list_handle, reset_list) {
+		amdgpu_fence_driver_isr_toggle(tmp_adev, false);
+	}
+
   	/*
   	 * ASIC reset has to be done on all XGMI hive nodes ASAP

* to allow proper links negotiation in FW (within 1 sec) @@
-5031,8 +5033,6 @@ static void amdgpu_device_recheck_guilty_jobs(
/* Clear this failed job from fence array */ amdgpu_fence_driver_clear_job_fences(ring);

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +0 -2
-			amdgpu_fence_driver_isr_toggle(adev, false);
-
   			/* Since the job won't signal and we go for
   			 * another resubmit drop this parent pointer
   			 */
drivers/gpu/drm/amd/amdgpu/sienna_cichlid.c +1 -0
index 7aa570c1ce4a..953036482d1f 100644
@@ -240,6 +240,7 @@ sienna_cichlid_mode2_restore_hwcontext(struct amdgpu_reset_control *reset_ctl,
   	* Add this ASIC as tracked as reset was already
   	* complete successfully.
   	*/
+	amdgpu_fence_driver_isr_toggle(tmp_adev, false);
   	amdgpu_register_gpu_instance(tmp_adev);
 
   	/* Resume RAS */
#8 lore raw
In reply to Christian König

[AMD Official Use Only - General]

Hi Christian,

The test sequence is executing a compute engine hang while running a lot of containers submitting gfx jobs. We have advanced tdr mode and mode2 reset enabled on driver.
When a compute hang job timeout happens, the 2 jobs on the gfx pending list maybe signaled after drm_sched_stop. So they will not be removed from pending list but have the DMA_FENCE_FLAG_SIGNALED_BIT set.
At the amdgpu_device_recheck_guilty_jobs step, the first job will be rerun and removed from pending list.
At the resubmit setp, the second job (with signaled bit) will be resubmitted. Since it still has signaled bit, drm_sched_job_done will be called directly. This decrease the hw_rq_count which allows more jobs emitted but did not clean fence_drv rcu ptr. This results in an overflow in the fence_drv. Since we will use num_fences_mask in amdgpu_fence_process, when overflow happens, the signal of some job will be skipped which result in an infinite wait for the fence_drv rcu ptr.

So close irq before sched_stop could avoid signal jobs after drm_sched_stop. And signal job one by one in fence_process instead of using a mask will handle the overflow situation.

Another fix could be skip submitting jobs which already signaled during resubmit stage, which may look cleaner.

Please help give some advice.

Thanks,
Victor

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Thursday, September 15, 2022 2:32 PM
To: Zhao, Victor <[email protected]>; [email protected]; Grodzovsky, Andrey <[email protected]>
Cc: Deng, Emily <[email protected]>
Subject: Re: [PATCH 1/2] drm/amdgpu: fix deadlock caused by overflow

Am 15.09.22 um 06:02 schrieb Zhao, Victor:

Quoted 117 lines

[AMD Official Use Only - General]

Ping.

Hi @Koenig, Christian and @Grodzovsky, Andrey,

We found some reset related issues during stress test on the sequence. Please help give some comments.

Thanks,
Victor

-----Original Message-----
From: Victor Zhao <[email protected]>
Sent: Wednesday, September 14, 2022 6:10 PM
To: [email protected]
Cc: Deng, Emily <[email protected]>; Grodzovsky, Andrey <[email protected]>; Zhao, Victor <[email protected]>
Subject: [PATCH 1/2] drm/amdgpu: fix deadlock caused by overflow

[background]
For a gpu recovery caused by a hang on one ring (e.g. compute), jobs from another ring (e.g. gfx) may continue signaling during drm_sched_stop stage. The signal bit will not be cleared.

At the resubmit stage after recovery, the job with hw fence signaled bit set will call job done directly instead go through fence process. This makes the hw_rq_count decrease but rcu fence pointer not cleared yet.

Then overflow happens in the fence driver slots and some jobs may be skipped and leave the rcu pointer not cleared which makes an infinite wait for the slot on the next fence emitted.

This infinite wait cause a job timeout on the emitting job. And driver will stuck at the its sched stop step because kthread_park cannot be done.

[how]
1. move amdgpu_fence_driver_isr_toggle earlier to close interrupt before drm sched stop 2. handle all fences in fence process to aviod skip when overflow happens

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 16 +++++++++++++---  drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c  |  6 +-----
  2 files changed, 14 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +0 -2
index 943c9e750575..c0cfae52f12b 100644
@@ -4610,8 +4610,6 @@ int amdgpu_device_pre_asic_reset(struct amdgpu_device *adev,
  		amdgpu_virt_fini_data_exchange(adev);
  	}
 
-	amdgpu_fence_driver_isr_toggle(adev, true);
-
  	/* block all schedulers and reset given job's ring */
  	for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  		struct amdgpu_ring *ring = adev->rings[i]; @@ -5214,6 +5212,8 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
  		      amdgpu_device_ip_need_full_reset(tmp_adev))
  			amdgpu_ras_suspend(tmp_adev);
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +7 -1
+		amdgpu_fence_driver_isr_toggle(tmp_adev, true);
+
  		for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  			struct amdgpu_ring *ring = tmp_adev->rings[i];
@@ -5228,8 +5228,12 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
  		atomic_inc(&tmp_adev->gpu_reset_counter);
  	}
 
-	if (need_emergency_restart)
+	if (need_emergency_restart) {
+		list_for_each_entry (tmp_adev, device_list_handle, reset_list) {
+			amdgpu_fence_driver_isr_toggle(tmp_adev, false);
+		}
  		goto skip_sched_resume;
+	}
 
  	/*
  	 * Must check guilty signal here since after this point all old @@ -5240,6 +5244,9 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,

if (job && dma_fence_is_signaled(&job->hw_fence)) {
job_signaled = true;
dev_info(adev->dev, "Guilty job already signaled, skipping HW
reset");

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +6 -0
+		list_for_each_entry (tmp_adev, device_list_handle, reset_list) {
+			amdgpu_fence_driver_isr_toggle(tmp_adev, false);
+		}
  		goto skip_hw_reset;
  	}
@@ -5276,6 +5283,7 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
  		if (r && r == -EAGAIN) {
  			set_bit(AMDGPU_SKIP_MODE2_RESET, &reset_context->flags);
  			adev->asic_reset_res = 0;
+			amdgpu_fence_driver_isr_toggle(adev, true);
  			goto retry;
  		}
  	}
@@ -5711,6 +5719,8 @@ pci_ers_result_t amdgpu_pci_slot_reset(struct pci_dev *pdev)
  	set_bit(AMDGPU_SKIP_HW_RESET, &reset_context.flags);
  	set_bit(AMDGPU_SKIP_MODE2_RESET, &reset_context.flags);
 
+	amdgpu_fence_driver_isr_toggle(adev, true);
+
  	adev->no_hw_access = true;
  	r = amdgpu_device_pre_asic_reset(adev, &reset_context);
  	adev->no_hw_access = false;
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +0 -3
index 8adeb7469f1e..65a877e1a7fc 100644
@@ -287,15 +287,11 @@ bool amdgpu_fence_process(struct amdgpu_ring *ring)
  	if (unlikely(seq == last_seq))
  		return false;
 
-	last_seq &= drv->num_fences_mask;
-	seq &= drv->num_fences_mask;
-

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c bool amdgpu_fence_process(struct amdgpu_ring *ring) · line 297

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +1 -2
  	do {
  		struct dma_fence *fence, **ptr;
 
  		++last_seq;
-		last_seq &= drv->num_fences_mask;
-		ptr = &drv->fences[last_seq];
+		ptr = &drv->fences[last_seq & drv->num_fences_mask];
 
  		/* There is always exactly one thread signaling this fence slot */
  		fence = rcu_dereference_protected(*ptr, 1);

Those changes here doesn't seem to make sense. Please explain further why that is necessary.

Christian.

Quoted 2 lines

--
2.25.1

#5 lore raw

RE: [PATCH 2/2] drm/amdgpu: move enable irq later to avoid race with ih resume

In reply to Lazar, Lijo

[AMD Official Use Only - General]

Hi Lijo,

Yes, this is what I observed in sienna cichlid.

Thanks,
Victor

-----Original Message-----
From: Lazar, Lijo <[email protected]>
Sent: Thursday, September 15, 2022 4:00 PM
To: Zhao, Victor <[email protected]>; [email protected]
Cc: Deng, Emily <[email protected]>; Grodzovsky, Andrey <[email protected]>
Subject: Re: [PATCH 2/2] drm/amdgpu: move enable irq later to avoid race with ih resume

Quoted 10 lines · Victor

[AMD Official Use Only - General]

Hi Lijo,

IH resume was added to resolve an issue found during mode2 bring up on sienna cichlid:
- close down mode2 reset and do a mode1 reset first
- open mode2 reset and do a mode2 reset. Mode2 reset was found fail in this case.

Resume IH helps in this case

Sorry, what do you mean by 'close down' mode2 /'open mode2 reset'? Do you mean if mode-1 reset is done first, a subsequent mode-2 reset doesn't work without IH resume?

Thanks,
Lijo

Quoted 95 lines

Thanks,
Victor

-----Original Message-----
From: Lazar, Lijo <[email protected]>
Sent: Thursday, September 15, 2022 1:58 PM
To: Zhao, Victor <[email protected]>; [email protected]
Cc: Deng, Emily <[email protected]>; Grodzovsky, Andrey <[email protected]>
Subject: Re: [PATCH 2/2] drm/amdgpu: move enable irq later to avoid race with ih resume

Quoted 8 lines · Victor Zhao

[background]
On current sienna cichlid mode2 reset, on the slow job hang cases, since page table context was reverted to completely stop gpu, it will generate page fault interrupt.

Since the irq are open during recovery stage, during ih resume step, if this interrupt was in processing, which increased ih ring rptr, and ih resume meanwhile will set rptr and wptr to 0. This may cause

AFAIK, only GFX/SDMA are affected by mode-2. IH is not suspended before mode-2. Why do you resume IH after mode-2 when it is not suspended? Is it a special case for virtualization?

Thanks,
Lijo

Quoted 63 lines

rptr greater than wptr. Such case was not handled in ih process, and it will cause rptr continue increasing util reaches the max. Such case will make fence fallback situation happen.

[how]
Move the enable of irq after ih resumed and before ib test. Adjusting the position of enable irq on other reset paths accordingly.

---
   drivers/gpu/drm/amd/amdgpu/amdgpu_device.c  | 8 ++++----
   drivers/gpu/drm/amd/amdgpu/sienna_cichlid.c | 1 +
   2 files changed, 5 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +4 -2
index c0cfae52f12b..0b658225e9ef 100644
@@ -4625,8 +4625,6 @@ int amdgpu_device_pre_asic_reset(struct amdgpu_device *adev,
   		amdgpu_fence_driver_force_completion(ring);
   	}
 
-	amdgpu_fence_driver_isr_toggle(adev, false);
-
   	if (job && job->vm)
   		drm_sched_increase_karma(&job->base);
 
@@ -4758,6 +4756,10 @@ int amdgpu_do_asic_reset(struct list_head *device_list_handle,
   		test_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
   	skip_hw_reset = test_bit(AMDGPU_SKIP_HW_RESET, 
 &reset_context->flags);
 
+	list_for_each_entry (tmp_adev, device_list_handle, reset_list) {
+		amdgpu_fence_driver_isr_toggle(tmp_adev, false);
+	}
+
   	/*
   	 * ASIC reset has to be done on all XGMI hive nodes ASAP

* to allow proper links negotiation in FW (within 1 sec) @@
-5031,8 +5033,6 @@ static void amdgpu_device_recheck_guilty_jobs(
/* Clear this failed job from fence array */ amdgpu_fence_driver_clear_job_fences(ring);

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +0 -2
-			amdgpu_fence_driver_isr_toggle(adev, false);
-
   			/* Since the job won't signal and we go for
   			 * another resubmit drop this parent pointer
   			 */
drivers/gpu/drm/amd/amdgpu/sienna_cichlid.c +1 -0
index 7aa570c1ce4a..953036482d1f 100644
@@ -240,6 +240,7 @@ sienna_cichlid_mode2_restore_hwcontext(struct amdgpu_reset_control *reset_ctl,
   	* Add this ASIC as tracked as reset was already
   	* complete successfully.
   	*/
+	amdgpu_fence_driver_isr_toggle(tmp_adev, false);
   	amdgpu_register_gpu_instance(tmp_adev);
 
   	/* Resume RAS */
maintainer
#9 lore raw
In reply to Zhao, Victor

Hi Victor,

the advanced tdr mode is not a feature we want to enable in production and keep for much longer.

So would that issue happen without this as well? If not it is rather questionable if we should look into fixing this in the first place.

Regards,
Christian.

Am 15.09.22 um 12:09 schrieb Zhao, Victor:

Quoted 165 lines

[AMD Official Use Only - General]

Hi Christian,

The test sequence is executing a compute engine hang while running a lot of containers submitting gfx jobs. We have advanced tdr mode and mode2 reset enabled on driver.
When a compute hang job timeout happens, the 2 jobs on the gfx pending list maybe signaled after drm_sched_stop. So they will not be removed from pending list but have the DMA_FENCE_FLAG_SIGNALED_BIT set.
At the amdgpu_device_recheck_guilty_jobs step, the first job will be rerun and removed from pending list.
At the resubmit setp, the second job (with signaled bit) will be resubmitted. Since it still has signaled bit, drm_sched_job_done will be called directly. This decrease the hw_rq_count which allows more jobs emitted but did not clean fence_drv rcu ptr. This results in an overflow in the fence_drv. Since we will use num_fences_mask in amdgpu_fence_process, when overflow happens, the signal of some job will be skipped which result in an infinite wait for the fence_drv rcu ptr.

So close irq before sched_stop could avoid signal jobs after drm_sched_stop. And signal job one by one in fence_process instead of using a mask will handle the overflow situation.

Another fix could be skip submitting jobs which already signaled during resubmit stage, which may look cleaner.

Please help give some advice.

Thanks,
Victor

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Thursday, September 15, 2022 2:32 PM
To: Zhao, Victor <[email protected]>; [email protected]; Grodzovsky, Andrey <[email protected]>
Cc: Deng, Emily <[email protected]>
Subject: Re: [PATCH 1/2] drm/amdgpu: fix deadlock caused by overflow

Am 15.09.22 um 06:02 schrieb Zhao, Victor:

Quoted 117 lines

[AMD Official Use Only - General]

Ping.

Hi @Koenig, Christian and @Grodzovsky, Andrey,

We found some reset related issues during stress test on the sequence. Please help give some comments.

Thanks,
Victor

-----Original Message-----
From: Victor Zhao <[email protected]>
Sent: Wednesday, September 14, 2022 6:10 PM
To: [email protected]
Cc: Deng, Emily <[email protected]>; Grodzovsky, Andrey <[email protected]>; Zhao, Victor <[email protected]>
Subject: [PATCH 1/2] drm/amdgpu: fix deadlock caused by overflow

[background]
For a gpu recovery caused by a hang on one ring (e.g. compute), jobs from another ring (e.g. gfx) may continue signaling during drm_sched_stop stage. The signal bit will not be cleared.

At the resubmit stage after recovery, the job with hw fence signaled bit set will call job done directly instead go through fence process. This makes the hw_rq_count decrease but rcu fence pointer not cleared yet.

Then overflow happens in the fence driver slots and some jobs may be skipped and leave the rcu pointer not cleared which makes an infinite wait for the slot on the next fence emitted.

This infinite wait cause a job timeout on the emitting job. And driver will stuck at the its sched stop step because kthread_park cannot be done.

[how]
1. move amdgpu_fence_driver_isr_toggle earlier to close interrupt before drm sched stop 2. handle all fences in fence process to aviod skip when overflow happens

---
   drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 16 +++++++++++++---  drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c  |  6 +-----
   2 files changed, 14 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +0 -2
index 943c9e750575..c0cfae52f12b 100644
@@ -4610,8 +4610,6 @@ int amdgpu_device_pre_asic_reset(struct amdgpu_device *adev,
   		amdgpu_virt_fini_data_exchange(adev);
   	}
 
-	amdgpu_fence_driver_isr_toggle(adev, true);
-
   	/* block all schedulers and reset given job's ring */
   	for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
   		struct amdgpu_ring *ring = adev->rings[i]; @@ -5214,6 +5212,8 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
   		      amdgpu_device_ip_need_full_reset(tmp_adev))
   			amdgpu_ras_suspend(tmp_adev);
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +7 -1
+		amdgpu_fence_driver_isr_toggle(tmp_adev, true);
+
   		for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
   			struct amdgpu_ring *ring = tmp_adev->rings[i];
@@ -5228,8 +5228,12 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
   		atomic_inc(&tmp_adev->gpu_reset_counter);
   	}
 
-	if (need_emergency_restart)
+	if (need_emergency_restart) {
+		list_for_each_entry (tmp_adev, device_list_handle, reset_list) {
+			amdgpu_fence_driver_isr_toggle(tmp_adev, false);
+		}
   		goto skip_sched_resume;
+	}
 
   	/*
   	 * Must check guilty signal here since after this point all old @@ -5240,6 +5244,9 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
   	if (job && dma_fence_is_signaled(&job->hw_fence)) {
   		job_signaled = true;
   		dev_info(adev->dev, "Guilty job already signaled, skipping HW

reset");

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +6 -0
+		list_for_each_entry (tmp_adev, device_list_handle, reset_list) {
+			amdgpu_fence_driver_isr_toggle(tmp_adev, false);
+		}
   		goto skip_hw_reset;
   	}
@@ -5276,6 +5283,7 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
   		if (r && r == -EAGAIN) {
   			set_bit(AMDGPU_SKIP_MODE2_RESET, &reset_context->flags);
   			adev->asic_reset_res = 0;
+			amdgpu_fence_driver_isr_toggle(adev, true);
   			goto retry;
   		}
   	}
@@ -5711,6 +5719,8 @@ pci_ers_result_t amdgpu_pci_slot_reset(struct pci_dev *pdev)
   	set_bit(AMDGPU_SKIP_HW_RESET, &reset_context.flags);
   	set_bit(AMDGPU_SKIP_MODE2_RESET, &reset_context.flags);
 
+	amdgpu_fence_driver_isr_toggle(adev, true);
+
   	adev->no_hw_access = true;
   	r = amdgpu_device_pre_asic_reset(adev, &reset_context);
   	adev->no_hw_access = false;
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +0 -3
index 8adeb7469f1e..65a877e1a7fc 100644
@@ -287,15 +287,11 @@ bool amdgpu_fence_process(struct amdgpu_ring *ring)
   	if (unlikely(seq == last_seq))
   		return false;
 
-	last_seq &= drv->num_fences_mask;
-	seq &= drv->num_fences_mask;
-

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c bool amdgpu_fence_process(struct amdgpu_ring *ring) · line 297

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +1 -2
   	do {
   		struct dma_fence *fence, **ptr;
 
   		++last_seq;
-		last_seq &= drv->num_fences_mask;
-		ptr = &drv->fences[last_seq];
+		ptr = &drv->fences[last_seq & drv->num_fences_mask];
 
   		/* There is always exactly one thread signaling this fence slot */
   		fence = rcu_dereference_protected(*ptr, 1);

Those changes here doesn't seem to make sense. Please explain further why that is necessary.

Christian.

Quoted 2 lines

--
2.25.1

#10 lore raw
In reply to Zhao, Victor
Quoted 10 lines · Victor

[AMD Official Use Only - General]

Hi Christian,

The test sequence is executing a compute engine hang while running a lot of containers submitting gfx jobs. We have advanced tdr mode and mode2 reset enabled on driver.
When a compute hang job timeout happens, the 2 jobs on the gfx pending list maybe signaled after drm_sched_stop. So they will not be removed from pending list but have the DMA_FENCE_FLAG_SIGNALED_BIT set.
At the amdgpu_device_recheck_guilty_jobs step, the first job will be rerun and removed from pending list.
At the resubmit setp, the second job (with signaled bit) will be resubmitted. Since it still has signaled bit, drm_sched_job_done will be called directly. This decrease the hw_rq_count which allows more jobs emitted but did not clean fence_drv rcu ptr. This results in an overflow in the fence_drv. Since we will use num_fences_mask in amdgpu_fence_process, when overflow happens, the signal of some job will be skipped which result in an infinite wait for the fence_drv rcu ptr.

Quoted 5 lines

So close irq before sched_stop could avoid signal jobs after drm_sched_stop. And signal job one by one in fence_process instead of using a mask will handle the overflow situation.

Another fix could be skip submitting jobs which already signaled during resubmit stage, which may look cleaner.

Please help give some advice.

How about the code bellow  instead ? The real problem is that we reuse a dma fence twice which is not according to fma fence design, so maybe this can help ?

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +0 -0
index 8adeb7469f1e..033f0ae16784 100644
@@ -164,6 +164,10 @@ int amdgpu_fence_emit(struct amdgpu_ring *ring, struct dma_fence **f, struct amd

        if (job && job->job_run_counter) {
                /* reinit seq for resubmitted jobs */
                fence->seqno = seq;
+
+               /* For resubmitted job clear the singled bit */
+               celar_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags); +
                /* TO be inline with external fence creation and other drivers */ dma_fence_get(fence);
        } else {

Andrey

Quoted 150 lines

Thanks,
Victor

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Thursday, September 15, 2022 2:32 PM
To: Zhao, Victor <[email protected]>; [email protected]; Grodzovsky, Andrey <[email protected]>
Cc: Deng, Emily <[email protected]>
Subject: Re: [PATCH 1/2] drm/amdgpu: fix deadlock caused by overflow

Am 15.09.22 um 06:02 schrieb Zhao, Victor:

Quoted 117 lines

[AMD Official Use Only - General]

Ping.

Hi @Koenig, Christian and @Grodzovsky, Andrey,

We found some reset related issues during stress test on the sequence. Please help give some comments.

Thanks,
Victor

-----Original Message-----
From: Victor Zhao <[email protected]>
Sent: Wednesday, September 14, 2022 6:10 PM
To: [email protected]
Cc: Deng, Emily <[email protected]>; Grodzovsky, Andrey <[email protected]>; Zhao, Victor <[email protected]>
Subject: [PATCH 1/2] drm/amdgpu: fix deadlock caused by overflow

[background]
For a gpu recovery caused by a hang on one ring (e.g. compute), jobs from another ring (e.g. gfx) may continue signaling during drm_sched_stop stage. The signal bit will not be cleared.

At the resubmit stage after recovery, the job with hw fence signaled bit set will call job done directly instead go through fence process. This makes the hw_rq_count decrease but rcu fence pointer not cleared yet.

Then overflow happens in the fence driver slots and some jobs may be skipped and leave the rcu pointer not cleared which makes an infinite wait for the slot on the next fence emitted.

This infinite wait cause a job timeout on the emitting job. And driver will stuck at the its sched stop step because kthread_park cannot be done.

[how]
1. move amdgpu_fence_driver_isr_toggle earlier to close interrupt before drm sched stop 2. handle all fences in fence process to aviod skip when overflow happens

---
   drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 16 +++++++++++++---  drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c  |  6 +-----
   2 files changed, 14 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +0 -2
index 943c9e750575..c0cfae52f12b 100644
@@ -4610,8 +4610,6 @@ int amdgpu_device_pre_asic_reset(struct amdgpu_device *adev,
   		amdgpu_virt_fini_data_exchange(adev);
   	}
 
-	amdgpu_fence_driver_isr_toggle(adev, true);
-
   	/* block all schedulers and reset given job's ring */
   	for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
   		struct amdgpu_ring *ring = adev->rings[i]; @@ -5214,6 +5212,8 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
   		      amdgpu_device_ip_need_full_reset(tmp_adev))
   			amdgpu_ras_suspend(tmp_adev);
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +7 -1
+		amdgpu_fence_driver_isr_toggle(tmp_adev, true);
+
   		for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
   			struct amdgpu_ring *ring = tmp_adev->rings[i];
@@ -5228,8 +5228,12 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
   		atomic_inc(&tmp_adev->gpu_reset_counter);
   	}
 
-	if (need_emergency_restart)
+	if (need_emergency_restart) {
+		list_for_each_entry (tmp_adev, device_list_handle, reset_list) {
+			amdgpu_fence_driver_isr_toggle(tmp_adev, false);
+		}
   		goto skip_sched_resume;
+	}
 
   	/*
   	 * Must check guilty signal here since after this point all old @@ -5240,6 +5244,9 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
   	if (job && dma_fence_is_signaled(&job->hw_fence)) {
   		job_signaled = true;
   		dev_info(adev->dev, "Guilty job already signaled, skipping HW

reset");

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +6 -0
+		list_for_each_entry (tmp_adev, device_list_handle, reset_list) {
+			amdgpu_fence_driver_isr_toggle(tmp_adev, false);
+		}
   		goto skip_hw_reset;
   	}
@@ -5276,6 +5283,7 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
   		if (r && r == -EAGAIN) {
   			set_bit(AMDGPU_SKIP_MODE2_RESET, &reset_context->flags);
   			adev->asic_reset_res = 0;
+			amdgpu_fence_driver_isr_toggle(adev, true);
   			goto retry;
   		}
   	}
@@ -5711,6 +5719,8 @@ pci_ers_result_t amdgpu_pci_slot_reset(struct pci_dev *pdev)
   	set_bit(AMDGPU_SKIP_HW_RESET, &reset_context.flags);
   	set_bit(AMDGPU_SKIP_MODE2_RESET, &reset_context.flags);
 
+	amdgpu_fence_driver_isr_toggle(adev, true);
+
   	adev->no_hw_access = true;
   	r = amdgpu_device_pre_asic_reset(adev, &reset_context);
   	adev->no_hw_access = false;
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +0 -3
index 8adeb7469f1e..65a877e1a7fc 100644
@@ -287,15 +287,11 @@ bool amdgpu_fence_process(struct amdgpu_ring *ring)
   	if (unlikely(seq == last_seq))
   		return false;
 
-	last_seq &= drv->num_fences_mask;
-	seq &= drv->num_fences_mask;
-

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c bool amdgpu_fence_process(struct amdgpu_ring *ring) · line 297

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +1 -2
   	do {
   		struct dma_fence *fence, **ptr;
 
   		++last_seq;
-		last_seq &= drv->num_fences_mask;
-		ptr = &drv->fences[last_seq];
+		ptr = &drv->fences[last_seq & drv->num_fences_mask];
 
   		/* There is always exactly one thread signaling this fence slot */
   		fence = rcu_dereference_protected(*ptr, 1);

Those changes here doesn't seem to make sense. Please explain further why that is necessary.

Christian.

Quoted 2 lines

--
2.25.1

#11 lore raw

Had a typo - see bellow

Quoted 189 lines · Andrey Grodzovsky
Quoted 10 lines · Victor

[AMD Official Use Only - General]

Hi Christian,

The test sequence is executing a compute engine hang while running a lot of containers submitting gfx jobs. We have advanced tdr mode and mode2 reset enabled on driver.
When a compute hang job timeout happens, the 2 jobs on the gfx pending list maybe signaled after drm_sched_stop. So they will not be removed from pending list but have the DMA_FENCE_FLAG_SIGNALED_BIT set.
At the amdgpu_device_recheck_guilty_jobs step, the first job will be rerun and removed from pending list.
At the resubmit setp, the second job (with signaled bit) will be resubmitted. Since it still has signaled bit, drm_sched_job_done will be called directly. This decrease the hw_rq_count which allows more jobs emitted but did not clean fence_drv rcu ptr. This results in an overflow in the fence_drv. Since we will use num_fences_mask in amdgpu_fence_process, when overflow happens, the signal of some job will be skipped which result in an infinite wait for the fence_drv rcu ptr.

Quoted 5 lines

So close irq before sched_stop could avoid signal jobs after drm_sched_stop. And signal job one by one in fence_process instead of using a mask will handle the overflow situation.

Another fix could be skip submitting jobs which already signaled during resubmit stage, which may look cleaner.

Please help give some advice.

How about the code bellow  instead ? The real problem is that we reuse a dma fence twice which is not according to fma fence design, so maybe this can help ?

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +0 -0
index 8adeb7469f1e..033f0ae16784 100644
@@ -164,6 +164,10 @@ int amdgpu_fence_emit(struct amdgpu_ring *ring, 

struct dma_fence **f, struct amd
        if (job && job->job_run_counter) {
                /* reinit seq for resubmitted jobs */ fence->seqno = seq;

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +4 -0
+
+               /* For resubmitted job clear the singled bit */
+               celar_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags);
+

                /* TO be inline with external fence creation and other drivers */ dma_fence_get(fence);
        } else {

Andrey

Quoted 146 lines

Thanks,
Victor

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Thursday, September 15, 2022 2:32 PM
To: Zhao, Victor <[email protected]>; [email protected]; Grodzovsky, Andrey <[email protected]>
Cc: Deng, Emily <[email protected]>
Subject: Re: [PATCH 1/2] drm/amdgpu: fix deadlock caused by overflow

Am 15.09.22 um 06:02 schrieb Zhao, Victor:

Quoted 113 lines

[AMD Official Use Only - General]

Ping.

Hi @Koenig, Christian and @Grodzovsky, Andrey,

We found some reset related issues during stress test on the sequence. Please help give some comments.

Thanks,
Victor

-----Original Message-----
From: Victor Zhao <[email protected]>
Sent: Wednesday, September 14, 2022 6:10 PM
To: [email protected]
Cc: Deng, Emily <[email protected]>; Grodzovsky, Andrey <[email protected]>; Zhao, Victor <[email protected]>
Subject: [PATCH 1/2] drm/amdgpu: fix deadlock caused by overflow

[background]
For a gpu recovery caused by a hang on one ring (e.g. compute), jobs from another ring (e.g. gfx) may continue signaling during drm_sched_stop stage. The signal bit will not be cleared.

At the resubmit stage after recovery, the job with hw fence signaled bit set will call job done directly instead go through fence process. This makes the hw_rq_count decrease but rcu fence pointer not cleared yet.

Then overflow happens in the fence driver slots and some jobs may be skipped and leave the rcu pointer not cleared which makes an infinite wait for the slot on the next fence emitted.

This infinite wait cause a job timeout on the emitting job. And driver will stuck at the its sched stop step because kthread_park cannot be done.

[how]
1. move amdgpu_fence_driver_isr_toggle earlier to close interrupt before drm sched stop 2. handle all fences in fence process to aviod skip when overflow happens

---
   drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 16 +++++++++++++---  drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c |  6 +-----

   2 files changed, 14 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +0 -1
index 943c9e750575..c0cfae52f12b 100644
@@ -4610,8 +4610,6 @@ int amdgpu_device_pre_asic_reset(struct 
 amdgpu_device *adev,
            amdgpu_virt_fini_data_exchange(adev);
        }
    -    amdgpu_fence_driver_isr_toggle(adev, true);
-
        /* block all schedulers and reset given job's ring */
        for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {

           struct amdgpu_ring *ring = adev->rings[i]; @@ -5214,6
+5212,8 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
                 amdgpu_device_ip_need_full_reset(tmp_adev))
               amdgpu_ras_suspend(tmp_adev);
   +        amdgpu_fence_driver_isr_toggle(tmp_adev, true);
+
           for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
               struct amdgpu_ring *ring = tmp_adev->rings[i];
   @@ -5228,8 +5228,12 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
           atomic_inc(&tmp_adev->gpu_reset_counter);
       }
   -    if (need_emergency_restart)
+    if (need_emergency_restart) {
+        list_for_each_entry (tmp_adev, device_list_handle,
reset_list) {
+            amdgpu_fence_driver_isr_toggle(tmp_adev, false);
+        }
           goto skip_sched_resume;
+    }
          /*
        * Must check guilty signal here since after this point all old @@ -5240,6 +5244,9 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev, if (job && dma_fence_is_signaled(&job->hw_fence)) {
           job_signaled = true;
           dev_info(adev->dev, "Guilty job already signaled, skipping HW
reset");
+        list_for_each_entry (tmp_adev, device_list_handle,
reset_list) {
+            amdgpu_fence_driver_isr_toggle(tmp_adev, false);
+        }
           goto skip_hw_reset;
       }
   @@ -5276,6 +5283,7 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
           if (r && r == -EAGAIN) {
               set_bit(AMDGPU_SKIP_MODE2_RESET, &reset_context->flags);
               adev->asic_reset_res = 0;
+            amdgpu_fence_driver_isr_toggle(adev, true);
               goto retry;
           }
       }

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +1 -0
@@ -5711,6 +5719,8 @@ pci_ers_result_t amdgpu_pci_slot_reset(struct 
 pci_dev *pdev)
        set_bit(AMDGPU_SKIP_HW_RESET, &reset_context.flags);
        set_bit(AMDGPU_SKIP_MODE2_RESET, &reset_context.flags);
    +    amdgpu_fence_driver_isr_toggle(adev, true);
+
        adev->no_hw_access = true;
        r = amdgpu_device_pre_asic_reset(adev, &reset_context);

       adev->no_hw_access = false;

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +0 -0
index 8adeb7469f1e..65a877e1a7fc 100644
@@ -287,15 +287,11 @@ bool amdgpu_fence_process(struct amdgpu_ring 
 *ring)
        if (unlikely(seq == last_seq))
            return false;
    -    last_seq &= drv->num_fences_mask;

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c bool amdgpu_fence_process(struct amdgpu_ring · line 296

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +1 -4
-    seq &= drv->num_fences_mask;
-
        do {
            struct dma_fence *fence, **ptr;
               ++last_seq;
-        last_seq &= drv->num_fences_mask;
-        ptr = &drv->fences[last_seq];
+        ptr = &drv->fences[last_seq & drv->num_fences_mask];
               /* There is always exactly one thread signaling this fence slot */
            fence = rcu_dereference_protected(*ptr, 1);

Those changes here doesn't seem to make sense. Please explain further why that is necessary.

Christian.

Quoted 2 lines
Signature · 1 line
2.25.1
maintainer
#12 lore raw
In reply to Andrey Grodzovsky

Am 15.09.22 um 20:29 schrieb Andrey Grodzovsky:

Quoted 31 lines · Victor
Quoted 10 lines

[AMD Official Use Only - General]

Hi Christian,

The test sequence is executing a compute engine hang while running a lot of containers submitting gfx jobs. We have advanced tdr mode and mode2 reset enabled on driver.
When a compute hang job timeout happens, the 2 jobs on the gfx pending list maybe signaled after drm_sched_stop. So they will not be removed from pending list but have the DMA_FENCE_FLAG_SIGNALED_BIT set.
At the amdgpu_device_recheck_guilty_jobs step, the first job will be rerun and removed from pending list.
At the resubmit setp, the second job (with signaled bit) will be resubmitted. Since it still has signaled bit, drm_sched_job_done will be called directly. This decrease the hw_rq_count which allows more jobs emitted but did not clean fence_drv rcu ptr. This results in an overflow in the fence_drv. Since we will use num_fences_mask in amdgpu_fence_process, when overflow happens, the signal of some job will be skipped which result in an infinite wait for the fence_drv rcu ptr.

Quoted 5 lines

So close irq before sched_stop could avoid signal jobs after drm_sched_stop. And signal job one by one in fence_process instead of using a mask will handle the overflow situation.

Another fix could be skip submitting jobs which already signaled during resubmit stage, which may look cleaner.

Please help give some advice.

How about the code bellow  instead ? The real problem is that we reuse a dma fence twice which is not according to fma fence design, so maybe this can help ?

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +0 -0
index 8adeb7469f1e..033f0ae16784 100644
@@ -164,6 +164,10 @@ int amdgpu_fence_emit(struct amdgpu_ring *ring, 

struct dma_fence **f, struct amd
        if (job && job->job_run_counter) {
                /* reinit seq for resubmitted jobs */ fence->seqno = seq;

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +4 -0
+
+               /* For resubmitted job clear the singled bit */
+               celar_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags);
+

Upstream will pretty much kill you for that.

Re-setting a fence from a signaled to an unsignaled state is a massive no-go.

Christian.

Quoted 155 lines

                /* TO be inline with external fence creation and other drivers */ dma_fence_get(fence);
        } else {

Andrey

Quoted 146 lines

Thanks,
Victor

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Thursday, September 15, 2022 2:32 PM
To: Zhao, Victor <[email protected]>; [email protected]; Grodzovsky, Andrey <[email protected]>
Cc: Deng, Emily <[email protected]>
Subject: Re: [PATCH 1/2] drm/amdgpu: fix deadlock caused by overflow

Am 15.09.22 um 06:02 schrieb Zhao, Victor:

Quoted 113 lines

[AMD Official Use Only - General]

Ping.

Hi @Koenig, Christian and @Grodzovsky, Andrey,

We found some reset related issues during stress test on the sequence. Please help give some comments.

Thanks,
Victor

-----Original Message-----
From: Victor Zhao <[email protected]>
Sent: Wednesday, September 14, 2022 6:10 PM
To: [email protected]
Cc: Deng, Emily <[email protected]>; Grodzovsky, Andrey <[email protected]>; Zhao, Victor <[email protected]>
Subject: [PATCH 1/2] drm/amdgpu: fix deadlock caused by overflow

[background]
For a gpu recovery caused by a hang on one ring (e.g. compute), jobs from another ring (e.g. gfx) may continue signaling during drm_sched_stop stage. The signal bit will not be cleared.

At the resubmit stage after recovery, the job with hw fence signaled bit set will call job done directly instead go through fence process. This makes the hw_rq_count decrease but rcu fence pointer not cleared yet.

Then overflow happens in the fence driver slots and some jobs may be skipped and leave the rcu pointer not cleared which makes an infinite wait for the slot on the next fence emitted.

This infinite wait cause a job timeout on the emitting job. And driver will stuck at the its sched stop step because kthread_park cannot be done.

[how]
1. move amdgpu_fence_driver_isr_toggle earlier to close interrupt before drm sched stop 2. handle all fences in fence process to aviod skip when overflow happens

---
   drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 16 +++++++++++++---  drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c |  6 +-----

   2 files changed, 14 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +0 -1
index 943c9e750575..c0cfae52f12b 100644
@@ -4610,8 +4610,6 @@ int amdgpu_device_pre_asic_reset(struct 
 amdgpu_device *adev,
            amdgpu_virt_fini_data_exchange(adev);
        }
    -    amdgpu_fence_driver_isr_toggle(adev, true);
-
        /* block all schedulers and reset given job's ring */
        for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {

           struct amdgpu_ring *ring = adev->rings[i]; @@ -5214,6
+5212,8 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
                 amdgpu_device_ip_need_full_reset(tmp_adev))
               amdgpu_ras_suspend(tmp_adev);
   +        amdgpu_fence_driver_isr_toggle(tmp_adev, true);
+
           for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
               struct amdgpu_ring *ring = tmp_adev->rings[i];
   @@ -5228,8 +5228,12 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
           atomic_inc(&tmp_adev->gpu_reset_counter);
       }
   -    if (need_emergency_restart)
+    if (need_emergency_restart) {
+        list_for_each_entry (tmp_adev, device_list_handle,
reset_list) {
+            amdgpu_fence_driver_isr_toggle(tmp_adev, false);
+        }
           goto skip_sched_resume;
+    }
          /*
        * Must check guilty signal here since after this point all old @@ -5240,6 +5244,9 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev, if (job && dma_fence_is_signaled(&job->hw_fence)) {
           job_signaled = true;
           dev_info(adev->dev, "Guilty job already signaled, skipping HW
reset");
+        list_for_each_entry (tmp_adev, device_list_handle,
reset_list) {
+            amdgpu_fence_driver_isr_toggle(tmp_adev, false);
+        }
           goto skip_hw_reset;
       }
   @@ -5276,6 +5283,7 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
           if (r && r == -EAGAIN) {
               set_bit(AMDGPU_SKIP_MODE2_RESET, &reset_context->flags);
               adev->asic_reset_res = 0;
+            amdgpu_fence_driver_isr_toggle(adev, true);
               goto retry;
           }
       }

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +1 -0
@@ -5711,6 +5719,8 @@ pci_ers_result_t amdgpu_pci_slot_reset(struct 
 pci_dev *pdev)
        set_bit(AMDGPU_SKIP_HW_RESET, &reset_context.flags);
        set_bit(AMDGPU_SKIP_MODE2_RESET, &reset_context.flags);
    +    amdgpu_fence_driver_isr_toggle(adev, true);
+
        adev->no_hw_access = true;
        r = amdgpu_device_pre_asic_reset(adev, &reset_context);

       adev->no_hw_access = false;

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +0 -0
index 8adeb7469f1e..65a877e1a7fc 100644
@@ -287,15 +287,11 @@ bool amdgpu_fence_process(struct amdgpu_ring 
 *ring)
        if (unlikely(seq == last_seq))
            return false;
    -    last_seq &= drv->num_fences_mask;

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c bool amdgpu_fence_process(struct amdgpu_ring · line 296

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +1 -4
-    seq &= drv->num_fences_mask;
-
        do {
            struct dma_fence *fence, **ptr;
               ++last_seq;
-        last_seq &= drv->num_fences_mask;
-        ptr = &drv->fences[last_seq];
+        ptr = &drv->fences[last_seq & drv->num_fences_mask];
               /* There is always exactly one thread signaling this fence slot */
            fence = rcu_dereference_protected(*ptr, 1);

Those changes here doesn't seem to make sense. Please explain further why that is necessary.

Christian.

Quoted 2 lines
Signature · 1 line
2.25.1
#13 lore raw
Quoted 39 lines · Christian König

Am 15.09.22 um 20:29 schrieb Andrey Grodzovsky:

Quoted 31 lines · Victor
Quoted 10 lines

[AMD Official Use Only - General]

Hi Christian,

The test sequence is executing a compute engine hang while running a lot of containers submitting gfx jobs. We have advanced tdr mode and mode2 reset enabled on driver.
When a compute hang job timeout happens, the 2 jobs on the gfx pending list maybe signaled after drm_sched_stop. So they will not be removed from pending list but have the DMA_FENCE_FLAG_SIGNALED_BIT set.
At the amdgpu_device_recheck_guilty_jobs step, the first job will be rerun and removed from pending list.
At the resubmit setp, the second job (with signaled bit) will be resubmitted. Since it still has signaled bit, drm_sched_job_done will be called directly. This decrease the hw_rq_count which allows more jobs emitted but did not clean fence_drv rcu ptr. This results in an overflow in the fence_drv. Since we will use num_fences_mask in amdgpu_fence_process, when overflow happens, the signal of some job will be skipped which result in an infinite wait for the fence_drv rcu ptr.

Quoted 5 lines

So close irq before sched_stop could avoid signal jobs after drm_sched_stop. And signal job one by one in fence_process instead of using a mask will handle the overflow situation.

Another fix could be skip submitting jobs which already signaled during resubmit stage, which may look cleaner.

Please help give some advice.

How about the code bellow  instead ? The real problem is that we reuse a dma fence twice which is not according to fma fence design, so maybe this can help ?

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +0 -0
index 8adeb7469f1e..033f0ae16784 100644
@@ -164,6 +164,10 @@ int amdgpu_fence_emit(struct amdgpu_ring *ring, 

struct dma_fence **f, struct amd
        if (job && job->job_run_counter) {
                /* reinit seq for resubmitted jobs */ fence->seqno = seq;

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +4 -0
+
+               /* For resubmitted job clear the singled bit */
+               celar_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags);
+

Upstream will pretty much kill you for that.

Quoted 3 lines

Re-setting a fence from a signaled to an unsignaled state is a massive no-go.

Christian.

Is it worse then doing fence->seqno = seq; ? This is already a huge hack , no ?

Andrey

Quoted 159 lines
Quoted 158 lines

                /* TO be inline with external fence creation and other drivers */ dma_fence_get(fence);
        } else {

Andrey

Quoted 148 lines

Thanks,
Victor

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Thursday, September 15, 2022 2:32 PM
To: Zhao, Victor <[email protected]>; [email protected]; Grodzovsky, Andrey <[email protected]>
Cc: Deng, Emily <[email protected]>
Subject: Re: [PATCH 1/2] drm/amdgpu: fix deadlock caused by overflow

Am 15.09.22 um 06:02 schrieb Zhao, Victor:

Quoted 115 lines

[AMD Official Use Only - General]

Ping.

Hi @Koenig, Christian and @Grodzovsky, Andrey,

We found some reset related issues during stress test on the sequence. Please help give some comments.

Thanks,
Victor

-----Original Message-----
From: Victor Zhao <[email protected]>
Sent: Wednesday, September 14, 2022 6:10 PM
To: [email protected]
Cc: Deng, Emily <[email protected]>; Grodzovsky, Andrey <[email protected]>; Zhao, Victor <[email protected]>
Subject: [PATCH 1/2] drm/amdgpu: fix deadlock caused by overflow

[background]
For a gpu recovery caused by a hang on one ring (e.g. compute), jobs from another ring (e.g. gfx) may continue signaling during drm_sched_stop stage. The signal bit will not be cleared.

At the resubmit stage after recovery, the job with hw fence signaled bit set will call job done directly instead go through fence process. This makes the hw_rq_count decrease but rcu fence pointer not cleared yet.

Then overflow happens in the fence driver slots and some jobs may be skipped and leave the rcu pointer not cleared which makes an infinite wait for the slot on the next fence emitted.

This infinite wait cause a job timeout on the emitting job. And driver will stuck at the its sched stop step because kthread_park cannot be done.

[how]
1. move amdgpu_fence_driver_isr_toggle earlier to close interrupt before drm sched stop 2. handle all fences in fence process to aviod skip when overflow happens

---
   drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 16 +++++++++++++---  drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c |  6
+-----

   2 files changed, 14 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +0 -1
index 943c9e750575..c0cfae52f12b 100644
@@ -4610,8 +4610,6 @@ int amdgpu_device_pre_asic_reset(struct 
 amdgpu_device *adev,
            amdgpu_virt_fini_data_exchange(adev);
        }
    -    amdgpu_fence_driver_isr_toggle(adev, true);
-
        /* block all schedulers and reset given job's ring */
        for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {

           struct amdgpu_ring *ring = adev->rings[i]; @@ -5214,6
+5212,8 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
                 amdgpu_device_ip_need_full_reset(tmp_adev))
               amdgpu_ras_suspend(tmp_adev);
   +        amdgpu_fence_driver_isr_toggle(tmp_adev, true);
+
           for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
               struct amdgpu_ring *ring = tmp_adev->rings[i];
   @@ -5228,8 +5228,12 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
           atomic_inc(&tmp_adev->gpu_reset_counter);
       }
   -    if (need_emergency_restart)
+    if (need_emergency_restart) {
+        list_for_each_entry (tmp_adev, device_list_handle,
reset_list) {
+            amdgpu_fence_driver_isr_toggle(tmp_adev, false);
+        }
           goto skip_sched_resume;
+    }
          /*
        * Must check guilty signal here since after this point all old @@ -5240,6 +5244,9 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev, if (job && dma_fence_is_signaled(&job->hw_fence)) {
           job_signaled = true;
           dev_info(adev->dev, "Guilty job already signaled, skipping HW
reset");
+        list_for_each_entry (tmp_adev, device_list_handle,
reset_list) {
+            amdgpu_fence_driver_isr_toggle(tmp_adev, false);
+        }
           goto skip_hw_reset;
       }
   @@ -5276,6 +5283,7 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
           if (r && r == -EAGAIN) {
               set_bit(AMDGPU_SKIP_MODE2_RESET, &reset_context->flags);
               adev->asic_reset_res = 0;
+            amdgpu_fence_driver_isr_toggle(adev, true);
               goto retry;
           }
       }

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +1 -0
@@ -5711,6 +5719,8 @@ pci_ers_result_t amdgpu_pci_slot_reset(struct 
 pci_dev *pdev)
        set_bit(AMDGPU_SKIP_HW_RESET, &reset_context.flags);
        set_bit(AMDGPU_SKIP_MODE2_RESET, &reset_context.flags);
    +    amdgpu_fence_driver_isr_toggle(adev, true);
+
        adev->no_hw_access = true;
        r = amdgpu_device_pre_asic_reset(adev, &reset_context);

       adev->no_hw_access = false;

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +0 -0
index 8adeb7469f1e..65a877e1a7fc 100644
@@ -287,15 +287,11 @@ bool amdgpu_fence_process(struct amdgpu_ring 
 *ring)
        if (unlikely(seq == last_seq))
            return false;
    -    last_seq &= drv->num_fences_mask;

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c bool amdgpu_fence_process(struct amdgpu_ring · line 296

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +1 -4
-    seq &= drv->num_fences_mask;
-
        do {
            struct dma_fence *fence, **ptr;
               ++last_seq;
-        last_seq &= drv->num_fences_mask;
-        ptr = &drv->fences[last_seq];
+        ptr = &drv->fences[last_seq & drv->num_fences_mask];
               /* There is always exactly one thread signaling this fence slot */
            fence = rcu_dereference_protected(*ptr, 1);

Those changes here doesn't seem to make sense. Please explain further why that is necessary.

Christian.

Quoted 2 lines
Signature · 1 line
2.25.1
maintainer
#14 lore raw

Am 15.09.22 um 22:37 schrieb Andrey Grodzovsky:

Quoted 41 lines · Christian König
Quoted 39 lines

Am 15.09.22 um 20:29 schrieb Andrey Grodzovsky:

Quoted 31 lines · Victor
Quoted 10 lines

[AMD Official Use Only - General]

Hi Christian,

The test sequence is executing a compute engine hang while running a lot of containers submitting gfx jobs. We have advanced tdr mode and mode2 reset enabled on driver.
When a compute hang job timeout happens, the 2 jobs on the gfx pending list maybe signaled after drm_sched_stop. So they will not be removed from pending list but have the DMA_FENCE_FLAG_SIGNALED_BIT set.
At the amdgpu_device_recheck_guilty_jobs step, the first job will be rerun and removed from pending list.
At the resubmit setp, the second job (with signaled bit) will be resubmitted. Since it still has signaled bit, drm_sched_job_done will be called directly. This decrease the hw_rq_count which allows more jobs emitted but did not clean fence_drv rcu ptr. This results in an overflow in the fence_drv. Since we will use num_fences_mask in amdgpu_fence_process, when overflow happens, the signal of some job will be skipped which result in an infinite wait for the fence_drv rcu ptr.

Quoted 5 lines

So close irq before sched_stop could avoid signal jobs after drm_sched_stop. And signal job one by one in fence_process instead of using a mask will handle the overflow situation.

Another fix could be skip submitting jobs which already signaled during resubmit stage, which may look cleaner.

Please help give some advice.

How about the code bellow  instead ? The real problem is that we reuse a dma fence twice which is not according to fma fence design, so maybe this can help ?

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +0 -0
index 8adeb7469f1e..033f0ae16784 100644
@@ -164,6 +164,10 @@ int amdgpu_fence_emit(struct amdgpu_ring *ring, 

struct dma_fence **f, struct amd
        if (job && job->job_run_counter) {
                /* reinit seq for resubmitted jobs */ fence->seqno = seq;

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +4 -0
+
+               /* For resubmitted job clear the singled bit */
+               celar_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags);
+

Upstream will pretty much kill you for that.

Quoted 6 lines
Quoted 3 lines

Re-setting a fence from a signaled to an unsignaled state is a massive no-go.

Christian.

Is it worse then doing fence->seqno = seq; ? This is already a huge hack , no ?

No, it's as equally bad. I don't think we can do either.

Christian.

Quoted 163 lines

Andrey

Quoted 159 lines
Quoted 158 lines

                /* TO be inline with external fence creation and other drivers */ dma_fence_get(fence);
        } else {

Andrey

Quoted 148 lines

Thanks,
Victor

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Thursday, September 15, 2022 2:32 PM
To: Zhao, Victor <[email protected]>; [email protected]; Grodzovsky, Andrey <[email protected]>
Cc: Deng, Emily <[email protected]>
Subject: Re: [PATCH 1/2] drm/amdgpu: fix deadlock caused by overflow

Am 15.09.22 um 06:02 schrieb Zhao, Victor:

Quoted 115 lines

[AMD Official Use Only - General]

Ping.

Hi @Koenig, Christian and @Grodzovsky, Andrey,

We found some reset related issues during stress test on the sequence. Please help give some comments.

Thanks,
Victor

-----Original Message-----
From: Victor Zhao <[email protected]>
Sent: Wednesday, September 14, 2022 6:10 PM
To: [email protected]
Cc: Deng, Emily <[email protected]>; Grodzovsky, Andrey <[email protected]>; Zhao, Victor <[email protected]>
Subject: [PATCH 1/2] drm/amdgpu: fix deadlock caused by overflow

[background]
For a gpu recovery caused by a hang on one ring (e.g. compute), jobs from another ring (e.g. gfx) may continue signaling during drm_sched_stop stage. The signal bit will not be cleared.

At the resubmit stage after recovery, the job with hw fence signaled bit set will call job done directly instead go through fence process. This makes the hw_rq_count decrease but rcu fence pointer not cleared yet.

Then overflow happens in the fence driver slots and some jobs may be skipped and leave the rcu pointer not cleared which makes an infinite wait for the slot on the next fence emitted.

This infinite wait cause a job timeout on the emitting job. And driver will stuck at the its sched stop step because kthread_park cannot be done.

[how]
1. move amdgpu_fence_driver_isr_toggle earlier to close interrupt before drm sched stop 2. handle all fences in fence process to aviod skip when overflow happens

---
   drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 16 +++++++++++++--- drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c |  6
+-----

   2 files changed, 14 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +0 -1
index 943c9e750575..c0cfae52f12b 100644
@@ -4610,8 +4610,6 @@ int amdgpu_device_pre_asic_reset(struct 
 amdgpu_device *adev,
            amdgpu_virt_fini_data_exchange(adev);
        }
    -    amdgpu_fence_driver_isr_toggle(adev, true);
-
        /* block all schedulers and reset given job's ring */
        for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {

           struct amdgpu_ring *ring = adev->rings[i]; @@ -5214,6
+5212,8 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
amdgpu_device_ip_need_full_reset(tmp_adev))
               amdgpu_ras_suspend(tmp_adev);
   +        amdgpu_fence_driver_isr_toggle(tmp_adev, true);
+
           for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
               struct amdgpu_ring *ring = tmp_adev->rings[i];
   @@ -5228,8 +5228,12 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
atomic_inc(&tmp_adev->gpu_reset_counter);
       }
   -    if (need_emergency_restart)
+    if (need_emergency_restart) {
+        list_for_each_entry (tmp_adev, device_list_handle,
reset_list) {
+            amdgpu_fence_driver_isr_toggle(tmp_adev, false);
+        }
           goto skip_sched_resume;
+    }
          /*
        * Must check guilty signal here since after this point all old @@ -5240,6 +5244,9 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev, if (job && dma_fence_is_signaled(&job->hw_fence)) {
           job_signaled = true;
           dev_info(adev->dev, "Guilty job already signaled, skipping HW
reset");
+        list_for_each_entry (tmp_adev, device_list_handle,
reset_list) {
+            amdgpu_fence_driver_isr_toggle(tmp_adev, false);
+        }
           goto skip_hw_reset;
       }
   @@ -5276,6 +5283,7 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
           if (r && r == -EAGAIN) {
               set_bit(AMDGPU_SKIP_MODE2_RESET, &reset_context->flags);
               adev->asic_reset_res = 0;
+            amdgpu_fence_driver_isr_toggle(adev, true);
               goto retry;
           }
       }

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +1 -0
@@ -5711,6 +5719,8 @@ pci_ers_result_t 
 amdgpu_pci_slot_reset(struct pci_dev *pdev)
        set_bit(AMDGPU_SKIP_HW_RESET, &reset_context.flags);
        set_bit(AMDGPU_SKIP_MODE2_RESET, &reset_context.flags);
    +    amdgpu_fence_driver_isr_toggle(adev, true);
+
        adev->no_hw_access = true;
        r = amdgpu_device_pre_asic_reset(adev, &reset_context);

       adev->no_hw_access = false;

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +0 -0
index 8adeb7469f1e..65a877e1a7fc 100644
@@ -287,15 +287,11 @@ bool amdgpu_fence_process(struct amdgpu_ring 
 *ring)
        if (unlikely(seq == last_seq))
            return false;
    -    last_seq &= drv->num_fences_mask;

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c bool amdgpu_fence_process(struct amdgpu_ring · line 296

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +1 -4
-    seq &= drv->num_fences_mask;
-
        do {
            struct dma_fence *fence, **ptr;
               ++last_seq;
-        last_seq &= drv->num_fences_mask;
-        ptr = &drv->fences[last_seq];
+        ptr = &drv->fences[last_seq & drv->num_fences_mask];
               /* There is always exactly one thread signaling this fence slot */
            fence = rcu_dereference_protected(*ptr, 1);

Those changes here doesn't seem to make sense. Please explain further why that is necessary.

Christian.

Quoted 2 lines
Signature · 1 line
2.25.1
#15 lore raw
Quoted 45 lines · Christian König

Am 15.09.22 um 22:37 schrieb Andrey Grodzovsky:

Quoted 44 lines · Christian König
Quoted 42 lines

Am 15.09.22 um 20:29 schrieb Andrey Grodzovsky:

Quoted 27 lines · Victor
Quoted 10 lines

[AMD Official Use Only - General]

Hi Christian,

The test sequence is executing a compute engine hang while running a lot of containers submitting gfx jobs. We have advanced tdr mode and mode2 reset enabled on driver.
When a compute hang job timeout happens, the 2 jobs on the gfx pending list maybe signaled after drm_sched_stop. So they will not be removed from pending list but have the DMA_FENCE_FLAG_SIGNALED_BIT set.
At the amdgpu_device_recheck_guilty_jobs step, the first job will be rerun and removed from pending list.
At the resubmit setp, the second job (with signaled bit) will be resubmitted. Since it still has signaled bit, drm_sched_job_done will be called directly. This decrease the hw_rq_count which allows more jobs emitted but did not clean fence_drv rcu ptr. This results in an overflow in the fence_drv. Since we will use num_fences_mask in amdgpu_fence_process, when overflow happens, the signal of some job will be skipped which result in an infinite wait for the fence_drv rcu ptr.

Quoted 5 lines

So close irq before sched_stop could avoid signal jobs after drm_sched_stop. And signal job one by one in fence_process instead of using a mask will handle the overflow situation.

Another fix could be skip submitting jobs which already signaled during resubmit stage, which may look cleaner.

Please help give some advice.

How about the code bellow  instead ? The real problem is that we reuse a dma fence twice which is not according to fma fence design, so maybe this can help ?

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +0 -0
index 8adeb7469f1e..033f0ae16784 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c int amdgpu_fence_emit(struct amdgpu_ring · line 172

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +4 -0
@@ -164,6 +164,10 @@ int amdgpu_fence_emit(struct amdgpu_ring 
 *ring, struct dma_fence **f, struct amd
         if (job && job->job_run_counter) {
                 /* reinit seq for resubmitted jobs */
                 fence->seqno = seq;
+
+               /* For resubmitted job clear the singled bit */
+               celar_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, 
 &fence->flags);
+

Upstream will pretty much kill you for that.

Re-setting a fence from a signaled to an unsignaled state is a massive no-go.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Christian.

Is it worse then doing fence->seqno = seq; ? This is already a huge hack , no ?

No, it's as equally bad. I don't think we can do either.

Christian.

And all those ugly hack are there because we reuse a dma_fence (hw_fence embedded into the job) and correct me if I am wrong but I don't think dma_fence is ever supposed to be reused.

So maybe like Victor suggested we should move close and flush irq before sched_stop - this in my opinion should solve the issue, but Victor - why then you still need the change in amdgpu_fence_process ? You will not have the overflow situation because by moving irq_disable before stop any job that signaled will be removed from the scheduler pending list anyway. Also not that this change reverts 'drm/amdgpu: sanitize fence numbers' and could reintroduce that bug.

Andrey

Quoted 165 lines
Quoted 164 lines

Andrey

Quoted 159 lines
Quoted 158 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +0 -0
                 /* TO be inline with external fence creation and other drivers */

                dma_fence_get(fence);
        } else {

Andrey

Quoted 148 lines

Thanks,
Victor

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Thursday, September 15, 2022 2:32 PM
To: Zhao, Victor <[email protected]>; [email protected]; Grodzovsky, Andrey <[email protected]>
Cc: Deng, Emily <[email protected]>
Subject: Re: [PATCH 1/2] drm/amdgpu: fix deadlock caused by overflow

Am 15.09.22 um 06:02 schrieb Zhao, Victor:

Quoted 115 lines

[AMD Official Use Only - General]

Ping.

Hi @Koenig, Christian and @Grodzovsky, Andrey,

We found some reset related issues during stress test on the sequence. Please help give some comments.

Thanks,
Victor

-----Original Message-----
From: Victor Zhao <[email protected]>
Sent: Wednesday, September 14, 2022 6:10 PM
To: [email protected]
Cc: Deng, Emily <[email protected]>; Grodzovsky, Andrey <[email protected]>; Zhao, Victor <[email protected]>
Subject: [PATCH 1/2] drm/amdgpu: fix deadlock caused by overflow

[background]
For a gpu recovery caused by a hang on one ring (e.g. compute), jobs from another ring (e.g. gfx) may continue signaling during drm_sched_stop stage. The signal bit will not be cleared.

At the resubmit stage after recovery, the job with hw fence signaled bit set will call job done directly instead go through fence process. This makes the hw_rq_count decrease but rcu fence pointer not cleared yet.

Then overflow happens in the fence driver slots and some jobs may be skipped and leave the rcu pointer not cleared which makes an infinite wait for the slot on the next fence emitted.

This infinite wait cause a job timeout on the emitting job. And driver will stuck at the its sched stop step because kthread_park cannot be done.

[how]
1. move amdgpu_fence_driver_isr_toggle earlier to close interrupt before drm sched stop 2. handle all fences in fence process to aviod skip when overflow happens

---
   drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 16 +++++++++++++--- drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c |  6
+-----

   2 files changed, 14 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +0 -1
index 943c9e750575..c0cfae52f12b 100644
@@ -4610,8 +4610,6 @@ int amdgpu_device_pre_asic_reset(struct 
 amdgpu_device *adev,
            amdgpu_virt_fini_data_exchange(adev);
        }
    -    amdgpu_fence_driver_isr_toggle(adev, true);
-
        /* block all schedulers and reset given job's ring */
        for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {

           struct amdgpu_ring *ring = adev->rings[i]; @@ -5214,6
+5212,8 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
amdgpu_device_ip_need_full_reset(tmp_adev))
               amdgpu_ras_suspend(tmp_adev);
   +        amdgpu_fence_driver_isr_toggle(tmp_adev, true);
+
           for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
               struct amdgpu_ring *ring = tmp_adev->rings[i];
   @@ -5228,8 +5228,12 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
atomic_inc(&tmp_adev->gpu_reset_counter);
       }
   -    if (need_emergency_restart)
+    if (need_emergency_restart) {
+        list_for_each_entry (tmp_adev, device_list_handle,
reset_list) {
+            amdgpu_fence_driver_isr_toggle(tmp_adev, false);
+        }
           goto skip_sched_resume;
+    }
          /*
        * Must check guilty signal here since after this point all old @@ -5240,6 +5244,9 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev, if (job && dma_fence_is_signaled(&job->hw_fence)) {
           job_signaled = true;
           dev_info(adev->dev, "Guilty job already signaled, skipping HW
reset");
+        list_for_each_entry (tmp_adev, device_list_handle,
reset_list) {
+            amdgpu_fence_driver_isr_toggle(tmp_adev, false);
+        }
           goto skip_hw_reset;
       }
   @@ -5276,6 +5283,7 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
           if (r && r == -EAGAIN) {
               set_bit(AMDGPU_SKIP_MODE2_RESET, &reset_context->flags);
               adev->asic_reset_res = 0;
+            amdgpu_fence_driver_isr_toggle(adev, true);
               goto retry;
           }
       }

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +1 -0
@@ -5711,6 +5719,8 @@ pci_ers_result_t 
 amdgpu_pci_slot_reset(struct pci_dev *pdev)
        set_bit(AMDGPU_SKIP_HW_RESET, &reset_context.flags);
        set_bit(AMDGPU_SKIP_MODE2_RESET, &reset_context.flags);
    +    amdgpu_fence_driver_isr_toggle(adev, true);
+
        adev->no_hw_access = true;
        r = amdgpu_device_pre_asic_reset(adev, &reset_context);

       adev->no_hw_access = false;

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +0 -0
index 8adeb7469f1e..65a877e1a7fc 100644
@@ -287,15 +287,11 @@ bool amdgpu_fence_process(struct 
 amdgpu_ring *ring)
        if (unlikely(seq == last_seq))
            return false;
    -    last_seq &= drv->num_fences_mask;

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c bool amdgpu_fence_process(struct · line 296

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +1 -4
-    seq &= drv->num_fences_mask;
-
        do {
            struct dma_fence *fence, **ptr;
               ++last_seq;
-        last_seq &= drv->num_fences_mask;
-        ptr = &drv->fences[last_seq];
+        ptr = &drv->fences[last_seq & drv->num_fences_mask];
               /* There is always exactly one thread signaling this fence slot */
            fence = rcu_dereference_protected(*ptr, 1);

Those changes here doesn't seem to make sense. Please explain further why that is necessary.

Christian.

Quoted 2 lines
Signature · 1 line
2.25.1
maintainer
#16 lore raw

Am 16.09.22 um 15:50 schrieb Andrey Grodzovsky:

Quoted 52 lines · Christian König
Quoted 50 lines

Am 15.09.22 um 22:37 schrieb Andrey Grodzovsky:

Quoted 48 lines · Christian König
Quoted 40 lines

Am 15.09.22 um 20:29 schrieb Andrey Grodzovsky:

Quoted 27 lines · Victor
Quoted 10 lines

[AMD Official Use Only - General]

Hi Christian,

The test sequence is executing a compute engine hang while running a lot of containers submitting gfx jobs. We have advanced tdr mode and mode2 reset enabled on driver.
When a compute hang job timeout happens, the 2 jobs on the gfx pending list maybe signaled after drm_sched_stop. So they will not be removed from pending list but have the DMA_FENCE_FLAG_SIGNALED_BIT set.
At the amdgpu_device_recheck_guilty_jobs step, the first job will be rerun and removed from pending list.
At the resubmit setp, the second job (with signaled bit) will be resubmitted. Since it still has signaled bit, drm_sched_job_done will be called directly. This decrease the hw_rq_count which allows more jobs emitted but did not clean fence_drv rcu ptr. This results in an overflow in the fence_drv. Since we will use num_fences_mask in amdgpu_fence_process, when overflow happens, the signal of some job will be skipped which result in an infinite wait for the fence_drv rcu ptr.

Quoted 5 lines

So close irq before sched_stop could avoid signal jobs after drm_sched_stop. And signal job one by one in fence_process instead of using a mask will handle the overflow situation.

Another fix could be skip submitting jobs which already signaled during resubmit stage, which may look cleaner.

Please help give some advice.

How about the code bellow  instead ? The real problem is that we reuse a dma fence twice which is not according to fma fence design, so maybe this can help ?

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +0 -0
index 8adeb7469f1e..033f0ae16784 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c int amdgpu_fence_emit(struct amdgpu_ring · line 172

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +4 -0
@@ -164,6 +164,10 @@ int amdgpu_fence_emit(struct amdgpu_ring 
 *ring, struct dma_fence **f, struct amd
         if (job && job->job_run_counter) {
                 /* reinit seq for resubmitted jobs */
                 fence->seqno = seq;
+
+               /* For resubmitted job clear the singled bit */
+               celar_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, 
 &fence->flags);
+

Upstream will pretty much kill you for that.

Quoted 3 lines

Re-setting a fence from a signaled to an unsignaled state is a massive no-go.

Christian.

Is it worse then doing fence->seqno = seq; ? This is already a huge hack , no ?

Quoted 7 lines
Quoted 3 lines

No, it's as equally bad. I don't think we can do either.

Christian.

And all those ugly hack are there because we reuse a dma_fence (hw_fence embedded into the job) and correct me if I am wrong but I don't think dma_fence is ever supposed to be reused.

Exactly that, yes.

Christian.

Quoted 172 lines

So maybe like Victor suggested we should move close and flush irq before sched_stop - this in my opinion should solve the issue, but Victor - why then you still need the change in amdgpu_fence_process ? You will not have the overflow situation because by moving irq_disable before stop any job that signaled will be removed from the scheduler pending list anyway. Also not that this change reverts 'drm/amdgpu: sanitize fence numbers' and could reintroduce that bug.

Andrey

Quoted 166 lines
Quoted 165 lines

Andrey

Quoted 160 lines
Quoted 159 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +0 -0
                 /* TO be inline with external fence creation and other drivers */

                dma_fence_get(fence);
        } else {

Andrey

Quoted 149 lines

Thanks,
Victor

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Thursday, September 15, 2022 2:32 PM
To: Zhao, Victor <[email protected]>; [email protected]; Grodzovsky, Andrey <[email protected]>
Cc: Deng, Emily <[email protected]>
Subject: Re: [PATCH 1/2] drm/amdgpu: fix deadlock caused by overflow

Am 15.09.22 um 06:02 schrieb Zhao, Victor:

Quoted 116 lines

[AMD Official Use Only - General]

Ping.

Hi @Koenig, Christian and @Grodzovsky, Andrey,

We found some reset related issues during stress test on the sequence. Please help give some comments.

Thanks,
Victor

-----Original Message-----
From: Victor Zhao <[email protected]>
Sent: Wednesday, September 14, 2022 6:10 PM
To: [email protected]
Cc: Deng, Emily <[email protected]>; Grodzovsky, Andrey <[email protected]>; Zhao, Victor <[email protected]>
Subject: [PATCH 1/2] drm/amdgpu: fix deadlock caused by overflow

[background]
For a gpu recovery caused by a hang on one ring (e.g. compute), jobs from another ring (e.g. gfx) may continue signaling during drm_sched_stop stage. The signal bit will not be cleared.

At the resubmit stage after recovery, the job with hw fence signaled bit set will call job done directly instead go through fence process. This makes the hw_rq_count decrease but rcu fence pointer not cleared yet.

Then overflow happens in the fence driver slots and some jobs may be skipped and leave the rcu pointer not cleared which makes an infinite wait for the slot on the next fence emitted.

This infinite wait cause a job timeout on the emitting job. And driver will stuck at the its sched stop step because kthread_park cannot be done.

[how]
1. move amdgpu_fence_driver_isr_toggle earlier to close interrupt before drm sched stop 2. handle all fences in fence process to aviod skip when overflow happens

---
   drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 16 +++++++++++++--- drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c |  6
+-----

   2 files changed, 14 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +0 -1
index 943c9e750575..c0cfae52f12b 100644
@@ -4610,8 +4610,6 @@ int amdgpu_device_pre_asic_reset(struct 
 amdgpu_device *adev,
            amdgpu_virt_fini_data_exchange(adev);
        }
    -    amdgpu_fence_driver_isr_toggle(adev, true);
-
        /* block all schedulers and reset given job's ring */
        for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {

           struct amdgpu_ring *ring = adev->rings[i]; @@ -5214,6
+5212,8 @@ int amdgpu_device_gpu_recover(struct amdgpu_device
*adev,
amdgpu_device_ip_need_full_reset(tmp_adev))
               amdgpu_ras_suspend(tmp_adev);
   +        amdgpu_fence_driver_isr_toggle(tmp_adev, true);
+
           for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
               struct amdgpu_ring *ring = tmp_adev->rings[i];
   @@ -5228,8 +5228,12 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
atomic_inc(&tmp_adev->gpu_reset_counter);
       }
   -    if (need_emergency_restart)
+    if (need_emergency_restart) {
+        list_for_each_entry (tmp_adev, device_list_handle,
reset_list) {
+            amdgpu_fence_driver_isr_toggle(tmp_adev, false);
+        }
           goto skip_sched_resume;
+    }
          /*
        * Must check guilty signal here since after this point all old @@ -5240,6 +5244,9 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev, if (job && dma_fence_is_signaled(&job->hw_fence)) {
           job_signaled = true;
           dev_info(adev->dev, "Guilty job already signaled, skipping HW
reset");
+        list_for_each_entry (tmp_adev, device_list_handle,
reset_list) {
+            amdgpu_fence_driver_isr_toggle(tmp_adev, false);
+        }
           goto skip_hw_reset;
       }
   @@ -5276,6 +5283,7 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
           if (r && r == -EAGAIN) {
               set_bit(AMDGPU_SKIP_MODE2_RESET, &reset_context->flags);
               adev->asic_reset_res = 0;
+            amdgpu_fence_driver_isr_toggle(adev, true);
               goto retry;
           }
       }

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +1 -0
@@ -5711,6 +5719,8 @@ pci_ers_result_t 
 amdgpu_pci_slot_reset(struct pci_dev *pdev)
        set_bit(AMDGPU_SKIP_HW_RESET, &reset_context.flags);
        set_bit(AMDGPU_SKIP_MODE2_RESET, &reset_context.flags);
    +    amdgpu_fence_driver_isr_toggle(adev, true);
+
        adev->no_hw_access = true;
        r = amdgpu_device_pre_asic_reset(adev, &reset_context);

       adev->no_hw_access = false;

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +0 -0
index 8adeb7469f1e..65a877e1a7fc 100644
@@ -287,15 +287,11 @@ bool amdgpu_fence_process(struct 
 amdgpu_ring *ring)
        if (unlikely(seq == last_seq))
            return false;
    -    last_seq &= drv->num_fences_mask;

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c bool amdgpu_fence_process(struct · line 296

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +1 -4
-    seq &= drv->num_fences_mask;
-
        do {
            struct dma_fence *fence, **ptr;
               ++last_seq;
-        last_seq &= drv->num_fences_mask;
-        ptr = &drv->fences[last_seq];
+        ptr = &drv->fences[last_seq & drv->num_fences_mask];
               /* There is always exactly one thread signaling this fence slot */
            fence = rcu_dereference_protected(*ptr, 1);

Those changes here doesn't seem to make sense. Please explain further why that is necessary.

Christian.

Quoted 2 lines
Signature · 1 line
2.25.1
#17 lore raw
In reply to Andrey Grodzovsky

[AMD Official Use Only - General]

Hi Andrey,

Yes, moving irq disable can fix the issue. Change in amdgpu_fence_process is just want to make sure driver can correct itself from an overflow situation. Didn’t know about the previous issue there.

Do you know if the issue still exists? Or is it on VCE only?

Thanks,
Victor

-----Original Message-----
From: Grodzovsky, Andrey <[email protected]>
Sent: Friday, September 16, 2022 9:50 PM
To: Koenig, Christian <[email protected]>; Zhao, Victor <[email protected]>; [email protected]
Cc: Deng, Emily <[email protected]>
Subject: Re: [PATCH 1/2] drm/amdgpu: fix deadlock caused by overflow

Quoted 67 lines · Christian König

Am 15.09.22 um 22:37 schrieb Andrey Grodzovsky:

Quoted 66 lines · Christian König
Quoted 62 lines

Am 15.09.22 um 20:29 schrieb Andrey Grodzovsky:

Quoted 45 lines · Victor
Quoted 22 lines

[AMD Official Use Only - General]

Hi Christian,

The test sequence is executing a compute engine hang while running a lot of containers submitting gfx jobs. We have advanced tdr mode and mode2 reset enabled on driver.
When a compute hang job timeout happens, the 2 jobs on the gfx pending list maybe signaled after drm_sched_stop. So they will not be removed from pending list but have the DMA_FENCE_FLAG_SIGNALED_BIT set.
At the amdgpu_device_recheck_guilty_jobs step, the first job will be rerun and removed from pending list.
At the resubmit setp, the second job (with signaled bit) will be resubmitted. Since it still has signaled bit, drm_sched_job_done will be called directly. This decrease the hw_rq_count which allows more jobs emitted but did not clean fence_drv rcu ptr. This results in an overflow in the fence_drv. Since we will use num_fences_mask in amdgpu_fence_process, when overflow happens, the signal of some job will be skipped which result in an infinite wait for the fence_drv rcu ptr.

Quoted 8 lines

So close irq before sched_stop could avoid signal jobs after drm_sched_stop. And signal job one by one in fence_process instead of using a mask will handle the overflow situation.

Another fix could be skip submitting jobs which already signaled during resubmit stage, which may look cleaner.

Please help give some advice.

How about the code bellow  instead ? The real problem is that we reuse a dma fence twice which is not according to fma fence design, so maybe this can help ?

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +0 -0
index 8adeb7469f1e..033f0ae16784 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c int amdgpu_fence_emit(struct amdgpu_ring · line 172

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +4 -0
@@ -164,6 +164,10 @@ int amdgpu_fence_emit(struct amdgpu_ring 
 *ring, struct dma_fence **f, struct amd
         if (job && job->job_run_counter) {
                 /* reinit seq for resubmitted jobs */
                 fence->seqno = seq;
+
+               /* For resubmitted job clear the singled bit */
+               celar_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT,
 &fence->flags);
+

Upstream will pretty much kill you for that.

Re-setting a fence from a signaled to an unsignaled state is a massive no-go.

Christian.

Quoted 6 lines
Quoted 2 lines

Is it worse then doing fence->seqno = seq; ? This is already a huge hack , no ?

No, it's as equally bad. I don't think we can do either.

Christian.

And all those ugly hack are there because we reuse a dma_fence (hw_fence embedded into the job) and correct me if I am wrong but I don't think dma_fence is ever supposed to be reused.

So maybe like Victor suggested we should move close and flush irq before sched_stop - this in my opinion should solve the issue, but Victor - why then you still need the change in amdgpu_fence_process ? You will not have the overflow situation because by moving irq_disable before stop any job that signaled will be removed from the scheduler pending list anyway. Also not that this change reverts 'drm/amdgpu: sanitize fence numbers' and could reintroduce that bug.

Andrey

Quoted 186 lines
Quoted 185 lines

Andrey

Quoted 180 lines
Quoted 179 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +0 -0
                 /* TO be inline with external fence creation and 

other drivers */
                dma_fence_get(fence);
        } else {

Andrey

Quoted 168 lines

Thanks,
Victor

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Thursday, September 15, 2022 2:32 PM
To: Zhao, Victor <[email protected]>; [email protected]; Grodzovsky, Andrey <[email protected]>
Cc: Deng, Emily <[email protected]>
Subject: Re: [PATCH 1/2] drm/amdgpu: fix deadlock caused by overflow

Am 15.09.22 um 06:02 schrieb Zhao, Victor:

Quoted 139 lines

[AMD Official Use Only - General]

Ping.

Hi @Koenig, Christian and @Grodzovsky, Andrey,

We found some reset related issues during stress test on the sequence. Please help give some comments.

Thanks,
Victor

-----Original Message-----
From: Victor Zhao <[email protected]>
Sent: Wednesday, September 14, 2022 6:10 PM
To: [email protected]
Cc: Deng, Emily <[email protected]>; Grodzovsky, Andrey <[email protected]>; Zhao, Victor <[email protected]>
Subject: [PATCH 1/2] drm/amdgpu: fix deadlock caused by overflow

[background]
For a gpu recovery caused by a hang on one ring (e.g. compute), jobs from another ring (e.g. gfx) may continue signaling during drm_sched_stop stage. The signal bit will not be cleared.

At the resubmit stage after recovery, the job with hw fence signaled bit set will call job done directly instead go through fence process.
This makes the hw_rq_count decrease but rcu fence pointer not cleared yet.

Then overflow happens in the fence driver slots and some jobs may be skipped and leave the rcu pointer not cleared which makes an infinite wait for the slot on the next fence emitted.

This infinite wait cause a job timeout on the emitting job. And driver will stuck at the its sched stop step because kthread_park cannot be done.

[how]
1. move amdgpu_fence_driver_isr_toggle earlier to close interrupt before drm sched stop 2. handle all fences in fence process to aviod skip when overflow happens

---
   drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 16 +++++++++++++--- drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c |  6
+-----

   2 files changed, 14 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +0 -1
index 943c9e750575..c0cfae52f12b 100644
@@ -4610,8 +4610,6 @@ int amdgpu_device_pre_asic_reset(struct
 amdgpu_device *adev,
            amdgpu_virt_fini_data_exchange(adev);
        }
    -    amdgpu_fence_driver_isr_toggle(adev, true);
-
        /* block all schedulers and reset given job's ring */
        for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {

           struct amdgpu_ring *ring = adev->rings[i]; @@ -5214,6 +5212,8 @@ int amdgpu_device_gpu_recover(struct amdgpu_device +*adev,
amdgpu_device_ip_need_full_reset(tmp_adev)) amdgpu_ras_suspend(tmp_adev);
   +        amdgpu_fence_driver_isr_toggle(tmp_adev, true); +
           for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { struct amdgpu_ring *ring = tmp_adev->rings[i]; @@ -5228,8 +5228,12 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev, atomic_inc(&tmp_adev->gpu_reset_counter); }
   -    if (need_emergency_restart)
+    if (need_emergency_restart) {
+        list_for_each_entry (tmp_adev, device_list_handle, reset_list) {
+            amdgpu_fence_driver_isr_toggle(tmp_adev, false); +        }
           goto skip_sched_resume;
+    }
          /*
        * Must check guilty signal here since after this point all old @@ -5240,6 +5244,9 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev, if (job && dma_fence_is_signaled(&job->hw_fence)) { job_signaled = true;
           dev_info(adev->dev, "Guilty job already signaled, skipping HW reset");
+        list_for_each_entry (tmp_adev, device_list_handle, reset_list) {
+            amdgpu_fence_driver_isr_toggle(tmp_adev, false); +        }
           goto skip_hw_reset;
       }
   @@ -5276,6 +5283,7 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
           if (r && r == -EAGAIN) { set_bit(AMDGPU_SKIP_MODE2_RESET, &reset_context->flags);
               adev->asic_reset_res = 0;
+            amdgpu_fence_driver_isr_toggle(adev, true); goto retry;
           }
       }

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +1 -0
@@ -5711,6 +5719,8 @@ pci_ers_result_t 
 amdgpu_pci_slot_reset(struct pci_dev *pdev)
        set_bit(AMDGPU_SKIP_HW_RESET, &reset_context.flags);
        set_bit(AMDGPU_SKIP_MODE2_RESET, &reset_context.flags);
    +    amdgpu_fence_driver_isr_toggle(adev, true);
+
        adev->no_hw_access = true;
        r = amdgpu_device_pre_asic_reset(adev, &reset_context);

       adev->no_hw_access = false; diff --git
a/drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c
b/drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c
index 8adeb7469f1e..65a877e1a7fc 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +1 -4
@@ -287,15 +287,11 @@ bool amdgpu_fence_process(struct 
 amdgpu_ring *ring)
        if (unlikely(seq == last_seq))
            return false;
    -    last_seq &= drv->num_fences_mask;
-    seq &= drv->num_fences_mask;
-
        do {
            struct dma_fence *fence, **ptr;
               ++last_seq;
-        last_seq &= drv->num_fences_mask;
-        ptr = &drv->fences[last_seq];
+        ptr = &drv->fences[last_seq & drv->num_fences_mask];
               /* There is always exactly one thread signaling 
Quoted 2 lines

this fence slot */
           fence = rcu_dereference_protected(*ptr, 1);

Those changes here doesn't seem to make sense. Please explain further why that is necessary.

Christian.

Quoted 2 lines

--
2.25.1

#18 lore raw

I don't know if issue still exist but it worth checking with Christian who wrote this patch.

Andrey

Quoted 277 lines · Victor

[AMD Official Use Only - General]

Hi Andrey,

Yes, moving irq disable can fix the issue. Change in amdgpu_fence_process is just want to make sure driver can correct itself from an overflow situation. Didn’t know about the previous issue there.

Do you know if the issue still exists? Or is it on VCE only?

Thanks,
Victor

-----Original Message-----
From: Grodzovsky, Andrey <[email protected]>
Sent: Friday, September 16, 2022 9:50 PM
To: Koenig, Christian <[email protected]>; Zhao, Victor <[email protected]>; [email protected]
Cc: Deng, Emily <[email protected]>
Subject: Re: [PATCH 1/2] drm/amdgpu: fix deadlock caused by overflow

Quoted 60 lines · Christian König

Am 15.09.22 um 22:37 schrieb Andrey Grodzovsky:

Quoted 59 lines · Christian König
Quoted 58 lines

Am 15.09.22 um 20:29 schrieb Andrey Grodzovsky:

Quoted 43 lines · Victor
Quoted 22 lines

[AMD Official Use Only - General]

Hi Christian,

The test sequence is executing a compute engine hang while running a lot of containers submitting gfx jobs. We have advanced tdr mode and mode2 reset enabled on driver.
When a compute hang job timeout happens, the 2 jobs on the gfx pending list maybe signaled after drm_sched_stop. So they will not be removed from pending list but have the DMA_FENCE_FLAG_SIGNALED_BIT set.
At the amdgpu_device_recheck_guilty_jobs step, the first job will be rerun and removed from pending list.
At the resubmit setp, the second job (with signaled bit) will be resubmitted. Since it still has signaled bit, drm_sched_job_done will be called directly. This decrease the hw_rq_count which allows more jobs emitted but did not clean fence_drv rcu ptr. This results in an overflow in the fence_drv. Since we will use num_fences_mask in amdgpu_fence_process, when overflow happens, the signal of some job will be skipped which result in an infinite wait for the fence_drv rcu ptr.

Quoted 8 lines

So close irq before sched_stop could avoid signal jobs after drm_sched_stop. And signal job one by one in fence_process instead of using a mask will handle the overflow situation.

Another fix could be skip submitting jobs which already signaled during resubmit stage, which may look cleaner.

Please help give some advice.

How about the code bellow  instead ? The real problem is that we reuse a dma fence twice which is not according to fma fence design, so maybe this can help ?

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +0 -0
index 8adeb7469f1e..033f0ae16784 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c int amdgpu_fence_emit(struct amdgpu_ring · line 172

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +4 -0
@@ -164,6 +164,10 @@ int amdgpu_fence_emit(struct amdgpu_ring
 *ring, struct dma_fence **f, struct amd
         if (job && job->job_run_counter) {
                 /* reinit seq for resubmitted jobs */
                 fence->seqno = seq;
+
+               /* For resubmitted job clear the singled bit */
+               celar_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT,
 &fence->flags);
+

Upstream will pretty much kill you for that.

Re-setting a fence from a signaled to an unsignaled state is a massive no-go.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Christian.

Is it worse then doing fence->seqno = seq; ? This is already a huge hack , no ?

No, it's as equally bad. I don't think we can do either.

Christian.

And all those ugly hack are there because we reuse a dma_fence (hw_fence embedded into the job) and correct me if I am wrong but I don't think dma_fence is ever supposed to be reused.

So maybe like Victor suggested we should move close and flush irq before sched_stop - this in my opinion should solve the issue, but Victor - why then you still need the change in amdgpu_fence_process ? You will not have the overflow situation because by moving irq_disable before stop any job that signaled will be removed from the scheduler pending list anyway. Also not that this change reverts 'drm/amdgpu: sanitize fence numbers' and could reintroduce that bug.

Andrey

Quoted 179 lines
Quoted 179 lines

Andrey

Quoted 176 lines
Quoted 176 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +0 -0
                 /* TO be inline with external fence creation and

other drivers */
                dma_fence_get(fence);
        } else {

Andrey

Quoted 167 lines

Thanks,
Victor

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Thursday, September 15, 2022 2:32 PM
To: Zhao, Victor <[email protected]>; [email protected]; Grodzovsky, Andrey <[email protected]>
Cc: Deng, Emily <[email protected]>
Subject: Re: [PATCH 1/2] drm/amdgpu: fix deadlock caused by overflow

Am 15.09.22 um 06:02 schrieb Zhao, Victor:

Quoted 139 lines

[AMD Official Use Only - General]

Ping.

Hi @Koenig, Christian and @Grodzovsky, Andrey,

We found some reset related issues during stress test on the sequence. Please help give some comments.

Thanks,
Victor

-----Original Message-----
From: Victor Zhao <[email protected]>
Sent: Wednesday, September 14, 2022 6:10 PM
To: [email protected]
Cc: Deng, Emily <[email protected]>; Grodzovsky, Andrey <[email protected]>; Zhao, Victor <[email protected]>
Subject: [PATCH 1/2] drm/amdgpu: fix deadlock caused by overflow

[background]
For a gpu recovery caused by a hang on one ring (e.g. compute), jobs from another ring (e.g. gfx) may continue signaling during drm_sched_stop stage. The signal bit will not be cleared.

At the resubmit stage after recovery, the job with hw fence signaled bit set will call job done directly instead go through fence process.
This makes the hw_rq_count decrease but rcu fence pointer not cleared yet.

Then overflow happens in the fence driver slots and some jobs may be skipped and leave the rcu pointer not cleared which makes an infinite wait for the slot on the next fence emitted.

This infinite wait cause a job timeout on the emitting job. And driver will stuck at the its sched stop step because kthread_park cannot be done.

[how]
1. move amdgpu_fence_driver_isr_toggle earlier to close interrupt before drm sched stop 2. handle all fences in fence process to aviod skip when overflow happens

---
    drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 16
+++++++++++++--- drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c |  6
+-----
    2 files changed, 14 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +2 -1
index 943c9e750575..c0cfae52f12b 100644
@@ -4610,8 +4610,6 @@ int amdgpu_device_pre_asic_reset(struct
 amdgpu_device *adev,
            amdgpu_virt_fini_data_exchange(adev);
        }
    -    amdgpu_fence_driver_isr_toggle(adev, true);
-
        /* block all schedulers and reset given job's ring */
        for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
            struct amdgpu_ring *ring = adev->rings[i]; @@ -5214,6
+5212,8 @@ int amdgpu_device_gpu_recover(struct amdgpu_device
+*adev,

amdgpu_device_ip_need_full_reset(tmp_adev))
               amdgpu_ras_suspend(tmp_adev);
   +        amdgpu_fence_driver_isr_toggle(tmp_adev, true);
+
           for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { struct amdgpu_ring *ring = tmp_adev->rings[i]; @@ -5228,8 +5228,12 @@ int amdgpu_device_gpu_recover(struct
amdgpu_device *adev, atomic_inc(&tmp_adev->gpu_reset_counter);
       }
   -    if (need_emergency_restart)
+    if (need_emergency_restart) {
+        list_for_each_entry (tmp_adev, device_list_handle, reset_list) {
+            amdgpu_fence_driver_isr_toggle(tmp_adev, false); +        }
           goto skip_sched_resume;
+    }
          /*
        * Must check guilty signal here since after this point
all old @@ -5240,6 +5244,9 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
       if (job && dma_fence_is_signaled(&job->hw_fence)) { job_signaled = true;
           dev_info(adev->dev, "Guilty job already signaled,
skipping HW reset");
+        list_for_each_entry (tmp_adev, device_list_handle, reset_list) {
+            amdgpu_fence_driver_isr_toggle(tmp_adev, false); +        }
           goto skip_hw_reset;
       }
   @@ -5276,6 +5283,7 @@ int amdgpu_device_gpu_recover(struct
amdgpu_device *adev,
           if (r && r == -EAGAIN) { set_bit(AMDGPU_SKIP_MODE2_RESET,
&reset_context->flags);
               adev->asic_reset_res = 0;
+            amdgpu_fence_driver_isr_toggle(adev, true);
               goto retry;
           }
       }

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +1 -0
@@ -5711,6 +5719,8 @@ pci_ers_result_t
 amdgpu_pci_slot_reset(struct pci_dev *pdev)
        set_bit(AMDGPU_SKIP_HW_RESET, &reset_context.flags);
        set_bit(AMDGPU_SKIP_MODE2_RESET, &reset_context.flags);
    +    amdgpu_fence_driver_isr_toggle(adev, true);
+
        adev->no_hw_access = true;
        r = amdgpu_device_pre_asic_reset(adev, &reset_context);
        adev->no_hw_access = false; diff --git

a/drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c
b/drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c
index 8adeb7469f1e..65a877e1a7fc 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +1 -4
@@ -287,15 +287,11 @@ bool amdgpu_fence_process(struct
 amdgpu_ring *ring)
        if (unlikely(seq == last_seq))
            return false;
    -    last_seq &= drv->num_fences_mask;
-    seq &= drv->num_fences_mask;
-
        do {
            struct dma_fence *fence, **ptr;
               ++last_seq;
-        last_seq &= drv->num_fences_mask;
-        ptr = &drv->fences[last_seq];
+        ptr = &drv->fences[last_seq & drv->num_fences_mask];
               /* There is always exactly one thread signaling
Quoted 2 lines

this fence slot */
           fence = rcu_dereference_protected(*ptr, 1);

Those changes here doesn't seem to make sense. Please explain further why that is necessary.

Christian.

Quoted 2 lines

--
2.25.1

#19 lore raw

[AMD Official Use Only - General]

Hi @Koenig, Christian,

About the change 'drm/amdgpu: sanitize fence numbers', do you know if this vce issue still exists? Can we change fence process back?

Hi @Grodzovsky, Andrey,

So looks like close irq is possibly the most appropriate fix for this issue for now? Please help review this part.

Thanks,
Victor

-----Original Message-----
From: Grodzovsky, Andrey <[email protected]>
Sent: Monday, September 19, 2022 11:04 PM
To: Zhao, Victor <[email protected]>; Koenig, Christian <[email protected]>; [email protected]
Cc: Deng, Emily <[email protected]>
Subject: Re: [PATCH 1/2] drm/amdgpu: fix deadlock caused by overflow

I don't know if issue still exist but it worth checking with Christian who wrote this patch.

Andrey

Quoted 279 lines · Victor

[AMD Official Use Only - General]

Hi Andrey,

Yes, moving irq disable can fix the issue. Change in amdgpu_fence_process is just want to make sure driver can correct itself from an overflow situation. Didn’t know about the previous issue there.

Do you know if the issue still exists? Or is it on VCE only?

Thanks,
Victor

-----Original Message-----
From: Grodzovsky, Andrey <[email protected]>
Sent: Friday, September 16, 2022 9:50 PM
To: Koenig, Christian <[email protected]>; Zhao, Victor <[email protected]>; [email protected]
Cc: Deng, Emily <[email protected]>
Subject: Re: [PATCH 1/2] drm/amdgpu: fix deadlock caused by overflow

Quoted 60 lines · Christian König

Am 15.09.22 um 22:37 schrieb Andrey Grodzovsky:

Quoted 59 lines · Christian König
Quoted 58 lines

Am 15.09.22 um 20:29 schrieb Andrey Grodzovsky:

Quoted 43 lines · Victor
Quoted 22 lines

[AMD Official Use Only - General]

Hi Christian,

The test sequence is executing a compute engine hang while running a lot of containers submitting gfx jobs. We have advanced tdr mode and mode2 reset enabled on driver.
When a compute hang job timeout happens, the 2 jobs on the gfx pending list maybe signaled after drm_sched_stop. So they will not be removed from pending list but have the DMA_FENCE_FLAG_SIGNALED_BIT set.
At the amdgpu_device_recheck_guilty_jobs step, the first job will be rerun and removed from pending list.
At the resubmit setp, the second job (with signaled bit) will be resubmitted. Since it still has signaled bit, drm_sched_job_done will be called directly. This decrease the hw_rq_count which allows more jobs emitted but did not clean fence_drv rcu ptr. This results in an overflow in the fence_drv. Since we will use num_fences_mask in amdgpu_fence_process, when overflow happens, the signal of some job will be skipped which result in an infinite wait for the fence_drv rcu ptr.

Quoted 8 lines

So close irq before sched_stop could avoid signal jobs after drm_sched_stop. And signal job one by one in fence_process instead of using a mask will handle the overflow situation.

Another fix could be skip submitting jobs which already signaled during resubmit stage, which may look cleaner.

Please help give some advice.

How about the code bellow  instead ? The real problem is that we reuse a dma fence twice which is not according to fma fence design, so maybe this can help ?

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +0 -0
index 8adeb7469f1e..033f0ae16784 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c int amdgpu_fence_emit(struct amdgpu_ring · line 172

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +4 -0
@@ -164,6 +164,10 @@ int amdgpu_fence_emit(struct amdgpu_ring 
 *ring, struct dma_fence **f, struct amd
         if (job && job->job_run_counter) {
                 /* reinit seq for resubmitted jobs */
                 fence->seqno = seq;
+
+               /* For resubmitted job clear the singled bit */
+               celar_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT,
 &fence->flags);
+

Upstream will pretty much kill you for that.

Re-setting a fence from a signaled to an unsignaled state is a massive no-go.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Christian.

Is it worse then doing fence->seqno = seq; ? This is already a huge hack , no ?

No, it's as equally bad. I don't think we can do either.

Christian.

And all those ugly hack are there because we reuse a dma_fence (hw_fence embedded into the job) and correct me if I am wrong but I don't think dma_fence is ever supposed to be reused.

So maybe like Victor suggested we should move close and flush irq before sched_stop - this in my opinion should solve the issue, but Victor - why then you still need the change in amdgpu_fence_process ? You will not have the overflow situation because by moving irq_disable before stop any job that signaled will be removed from the scheduler pending list anyway. Also not that this change reverts 'drm/amdgpu: sanitize fence numbers' and could reintroduce that bug.

Andrey

Quoted 180 lines
Quoted 180 lines

Andrey

Quoted 177 lines
Quoted 177 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +0 -0
                 /* TO be inline with external fence creation and 

other drivers */
                dma_fence_get(fence);
        } else {

Andrey

Quoted 168 lines

Thanks,
Victor

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Thursday, September 15, 2022 2:32 PM
To: Zhao, Victor <[email protected]>; [email protected]; Grodzovsky, Andrey <[email protected]>
Cc: Deng, Emily <[email protected]>
Subject: Re: [PATCH 1/2] drm/amdgpu: fix deadlock caused by overflow

Am 15.09.22 um 06:02 schrieb Zhao, Victor:

Quoted 140 lines

[AMD Official Use Only - General]

Ping.

Hi @Koenig, Christian and @Grodzovsky, Andrey,

We found some reset related issues during stress test on the sequence. Please help give some comments.

Thanks,
Victor

-----Original Message-----
From: Victor Zhao <[email protected]>
Sent: Wednesday, September 14, 2022 6:10 PM
To: [email protected]
Cc: Deng, Emily <[email protected]>; Grodzovsky, Andrey <[email protected]>; Zhao, Victor <[email protected]>
Subject: [PATCH 1/2] drm/amdgpu: fix deadlock caused by overflow

[background]
For a gpu recovery caused by a hang on one ring (e.g. compute), jobs from another ring (e.g. gfx) may continue signaling during drm_sched_stop stage. The signal bit will not be cleared.

At the resubmit stage after recovery, the job with hw fence signaled bit set will call job done directly instead go through fence process.
This makes the hw_rq_count decrease but rcu fence pointer not cleared yet.

Then overflow happens in the fence driver slots and some jobs may be skipped and leave the rcu pointer not cleared which makes an infinite wait for the slot on the next fence emitted.

This infinite wait cause a job timeout on the emitting job. And driver will stuck at the its sched stop step because kthread_park cannot be done.

[how]
1. move amdgpu_fence_driver_isr_toggle earlier to close interrupt before drm sched stop 2. handle all fences in fence process to aviod skip when overflow happens

---
    drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 16
+++++++++++++--- drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c |  6
+-----
    2 files changed, 14 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +2 -2
index 943c9e750575..c0cfae52f12b 100644
@@ -4610,8 +4610,6 @@ int amdgpu_device_pre_asic_reset(struct
 amdgpu_device *adev,
            amdgpu_virt_fini_data_exchange(adev);
        }
    -    amdgpu_fence_driver_isr_toggle(adev, true);
-
        /* block all schedulers and reset given job's ring */
        for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
            struct amdgpu_ring *ring = adev->rings[i]; @@ 
-5214,6
+5212,8 @@ int amdgpu_device_gpu_recover(struct amdgpu_device 
+*adev,

amdgpu_device_ip_need_full_reset(tmp_adev))
               amdgpu_ras_suspend(tmp_adev);
   +        amdgpu_fence_driver_isr_toggle(tmp_adev, true);
+
           for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { struct amdgpu_ring *ring = tmp_adev->rings[i]; @@ -5228,8 +5228,12 @@ int amdgpu_device_gpu_recover(struct
amdgpu_device *adev, atomic_inc(&tmp_adev->gpu_reset_counter);
       }
   -    if (need_emergency_restart)
+    if (need_emergency_restart) {
+        list_for_each_entry (tmp_adev, device_list_handle, reset_list) {
+            amdgpu_fence_driver_isr_toggle(tmp_adev, false); +        }
           goto skip_sched_resume;
+    }
          /*
        * Must check guilty signal here since after this point
all old @@ -5240,6 +5244,9 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
       if (job && dma_fence_is_signaled(&job->hw_fence)) { job_signaled = true;
           dev_info(adev->dev, "Guilty job already signaled,
skipping HW reset");
+        list_for_each_entry (tmp_adev, device_list_handle, reset_list) {
+            amdgpu_fence_driver_isr_toggle(tmp_adev, false); +        }
           goto skip_hw_reset;
       }
   @@ -5276,6 +5283,7 @@ int amdgpu_device_gpu_recover(struct
amdgpu_device *adev,
           if (r && r == -EAGAIN) { set_bit(AMDGPU_SKIP_MODE2_RESET,
&reset_context->flags);
               adev->asic_reset_res = 0;
+            amdgpu_fence_driver_isr_toggle(adev, true);
               goto retry;
           }
       }

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +1 -0
@@ -5711,6 +5719,8 @@ pci_ers_result_t 
 amdgpu_pci_slot_reset(struct pci_dev *pdev)
        set_bit(AMDGPU_SKIP_HW_RESET, &reset_context.flags);
        set_bit(AMDGPU_SKIP_MODE2_RESET, &reset_context.flags);
    +    amdgpu_fence_driver_isr_toggle(adev, true);
+
        adev->no_hw_access = true;
        r = amdgpu_device_pre_asic_reset(adev, &reset_context);
        adev->no_hw_access = false; diff --git 

a/drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c
b/drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c
index 8adeb7469f1e..65a877e1a7fc 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +1 -4
@@ -287,15 +287,11 @@ bool amdgpu_fence_process(struct 
 amdgpu_ring *ring)
        if (unlikely(seq == last_seq))
            return false;
    -    last_seq &= drv->num_fences_mask;
-    seq &= drv->num_fences_mask;
-
        do {
            struct dma_fence *fence, **ptr;
               ++last_seq;
-        last_seq &= drv->num_fences_mask;
-        ptr = &drv->fences[last_seq];
+        ptr = &drv->fences[last_seq & drv->num_fences_mask];
               /* There is always exactly one thread signaling 
Quoted 2 lines

this fence slot */
           fence = rcu_dereference_protected(*ptr, 1);

Those changes here doesn't seem to make sense. Please explain further why that is necessary.

Christian.

Quoted 2 lines

--
2.25.1

#20 lore raw

Hi Victor,

yes that behavior is mandatory. We can't rely on all engines always sending valid fence numbers.

Regards,
Christian.

Am 20.09.22 um 12:22 schrieb Zhao, Victor:

Quoted 304 lines

[AMD Official Use Only - General]

Hi @Koenig, Christian,

About the change 'drm/amdgpu: sanitize fence numbers', do you know if this vce issue still exists? Can we change fence process back?

Hi @Grodzovsky, Andrey,

So looks like close irq is possibly the most appropriate fix for this issue for now? Please help review this part.

Thanks,
Victor

-----Original Message-----
From: Grodzovsky, Andrey <[email protected]>
Sent: Monday, September 19, 2022 11:04 PM
To: Zhao, Victor <[email protected]>; Koenig, Christian <[email protected]>; [email protected]
Cc: Deng, Emily <[email protected]>
Subject: Re: [PATCH 1/2] drm/amdgpu: fix deadlock caused by overflow

I don't know if issue still exist but it worth checking with Christian who wrote this patch.

Andrey

Quoted 275 lines · Victor

[AMD Official Use Only - General]

Hi Andrey,

Yes, moving irq disable can fix the issue. Change in amdgpu_fence_process is just want to make sure driver can correct itself from an overflow situation. Didn’t know about the previous issue there.

Do you know if the issue still exists? Or is it on VCE only?

Thanks,
Victor

-----Original Message-----
From: Grodzovsky, Andrey <[email protected]>
Sent: Friday, September 16, 2022 9:50 PM
To: Koenig, Christian <[email protected]>; Zhao, Victor <[email protected]>; [email protected]
Cc: Deng, Emily <[email protected]>
Subject: Re: [PATCH 1/2] drm/amdgpu: fix deadlock caused by overflow

Quoted 59 lines · Christian König

Am 15.09.22 um 22:37 schrieb Andrey Grodzovsky:

Quoted 58 lines · Christian König
Quoted 57 lines

Am 15.09.22 um 20:29 schrieb Andrey Grodzovsky:

Quoted 42 lines · Victor
Quoted 22 lines

[AMD Official Use Only - General]

Hi Christian,

The test sequence is executing a compute engine hang while running a lot of containers submitting gfx jobs. We have advanced tdr mode and mode2 reset enabled on driver.
When a compute hang job timeout happens, the 2 jobs on the gfx pending list maybe signaled after drm_sched_stop. So they will not be removed from pending list but have the DMA_FENCE_FLAG_SIGNALED_BIT set.
At the amdgpu_device_recheck_guilty_jobs step, the first job will be rerun and removed from pending list.
At the resubmit setp, the second job (with signaled bit) will be resubmitted. Since it still has signaled bit, drm_sched_job_done will be called directly. This decrease the hw_rq_count which allows more jobs emitted but did not clean fence_drv rcu ptr. This results in an overflow in the fence_drv. Since we will use num_fences_mask in amdgpu_fence_process, when overflow happens, the signal of some job will be skipped which result in an infinite wait for the fence_drv rcu ptr.

Quoted 8 lines

So close irq before sched_stop could avoid signal jobs after drm_sched_stop. And signal job one by one in fence_process instead of using a mask will handle the overflow situation.

Another fix could be skip submitting jobs which already signaled during resubmit stage, which may look cleaner.

Please help give some advice.

How about the code bellow  instead ? The real problem is that we reuse a dma fence twice which is not according to fma fence design, so maybe this can help ?

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +0 -0
index 8adeb7469f1e..033f0ae16784 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c int amdgpu_fence_emit(struct amdgpu_ring · line 172

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +4 -0
@@ -164,6 +164,10 @@ int amdgpu_fence_emit(struct amdgpu_ring
 *ring, struct dma_fence **f, struct amd
          if (job && job->job_run_counter) {
                  /* reinit seq for resubmitted jobs */
                  fence->seqno = seq;
+
+               /* For resubmitted job clear the singled bit */
+               celar_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT,
 &fence->flags);
+

Upstream will pretty much kill you for that.

Re-setting a fence from a signaled to an unsignaled state is a massive no-go.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Christian.

Is it worse then doing fence->seqno = seq; ? This is already a huge hack , no ?

No, it's as equally bad. I don't think we can do either.

Christian.

And all those ugly hack are there because we reuse a dma_fence (hw_fence embedded into the job) and correct me if I am wrong but I don't think dma_fence is ever supposed to be reused.

So maybe like Victor suggested we should move close and flush irq before sched_stop - this in my opinion should solve the issue, but Victor - why then you still need the change in amdgpu_fence_process ? You will not have the overflow situation because by moving irq_disable before stop any job that signaled will be removed from the scheduler pending list anyway. Also not that this change reverts 'drm/amdgpu: sanitize fence numbers' and could reintroduce that bug.

Andrey

Quoted 179 lines
Quoted 179 lines

Andrey

Quoted 176 lines
Quoted 176 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +0 -0
                  /* TO be inline with external fence creation and

other drivers */
                dma_fence_get(fence);
        } else {

Andrey

Quoted 167 lines

Thanks,
Victor

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Thursday, September 15, 2022 2:32 PM
To: Zhao, Victor <[email protected]>; [email protected]; Grodzovsky, Andrey <[email protected]>
Cc: Deng, Emily <[email protected]>
Subject: Re: [PATCH 1/2] drm/amdgpu: fix deadlock caused by overflow

Am 15.09.22 um 06:02 schrieb Zhao, Victor:

Quoted 140 lines

[AMD Official Use Only - General]

Ping.

Hi @Koenig, Christian and @Grodzovsky, Andrey,

We found some reset related issues during stress test on the sequence. Please help give some comments.

Thanks,
Victor

-----Original Message-----
From: Victor Zhao <[email protected]>
Sent: Wednesday, September 14, 2022 6:10 PM
To: [email protected]
Cc: Deng, Emily <[email protected]>; Grodzovsky, Andrey <[email protected]>; Zhao, Victor <[email protected]>
Subject: [PATCH 1/2] drm/amdgpu: fix deadlock caused by overflow

[background]
For a gpu recovery caused by a hang on one ring (e.g. compute), jobs from another ring (e.g. gfx) may continue signaling during drm_sched_stop stage. The signal bit will not be cleared.

At the resubmit stage after recovery, the job with hw fence signaled bit set will call job done directly instead go through fence process.
This makes the hw_rq_count decrease but rcu fence pointer not cleared yet.

Then overflow happens in the fence driver slots and some jobs may be skipped and leave the rcu pointer not cleared which makes an infinite wait for the slot on the next fence emitted.

This infinite wait cause a job timeout on the emitting job. And driver will stuck at the its sched stop step because kthread_park cannot be done.

[how]
1. move amdgpu_fence_driver_isr_toggle earlier to close interrupt before drm sched stop 2. handle all fences in fence process to aviod skip when overflow happens

---
     drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 16
+++++++++++++--- drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c |  6
+-----
     2 files changed, 14 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +2 -2
index 943c9e750575..c0cfae52f12b 100644
@@ -4610,8 +4610,6 @@ int amdgpu_device_pre_asic_reset(struct
 amdgpu_device *adev,
             amdgpu_virt_fini_data_exchange(adev);
         }
     -    amdgpu_fence_driver_isr_toggle(adev, true);
-
         /* block all schedulers and reset given job's ring */
         for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
             struct amdgpu_ring *ring = adev->rings[i]; @@
-5214,6
+5212,8 @@ int amdgpu_device_gpu_recover(struct amdgpu_device
+*adev,

amdgpu_device_ip_need_full_reset(tmp_adev))
               amdgpu_ras_suspend(tmp_adev);
   +        amdgpu_fence_driver_isr_toggle(tmp_adev, true);
+
           for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { struct amdgpu_ring *ring = tmp_adev->rings[i]; @@ -5228,8 +5228,12 @@ int amdgpu_device_gpu_recover(struct
amdgpu_device *adev, atomic_inc(&tmp_adev->gpu_reset_counter);
       }
   -    if (need_emergency_restart)
+    if (need_emergency_restart) {
+        list_for_each_entry (tmp_adev, device_list_handle, reset_list) {
+            amdgpu_fence_driver_isr_toggle(tmp_adev, false); +        }
           goto skip_sched_resume;
+    }
          /*
        * Must check guilty signal here since after this point
all old @@ -5240,6 +5244,9 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
       if (job && dma_fence_is_signaled(&job->hw_fence)) { job_signaled = true;
           dev_info(adev->dev, "Guilty job already signaled,
skipping HW reset");
+        list_for_each_entry (tmp_adev, device_list_handle, reset_list) {
+            amdgpu_fence_driver_isr_toggle(tmp_adev, false); +        }
           goto skip_hw_reset;
       }
   @@ -5276,6 +5283,7 @@ int amdgpu_device_gpu_recover(struct
amdgpu_device *adev,
           if (r && r == -EAGAIN) { set_bit(AMDGPU_SKIP_MODE2_RESET,
&reset_context->flags);
               adev->asic_reset_res = 0;
+            amdgpu_fence_driver_isr_toggle(adev, true);
               goto retry;
           }
       }

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +1 -0
@@ -5711,6 +5719,8 @@ pci_ers_result_t
 amdgpu_pci_slot_reset(struct pci_dev *pdev)
         set_bit(AMDGPU_SKIP_HW_RESET, &reset_context.flags);
         set_bit(AMDGPU_SKIP_MODE2_RESET, &reset_context.flags);
     +    amdgpu_fence_driver_isr_toggle(adev, true);
+
         adev->no_hw_access = true;
         r = amdgpu_device_pre_asic_reset(adev, &reset_context);
         adev->no_hw_access = false; diff --git

a/drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c
b/drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c
index 8adeb7469f1e..65a877e1a7fc 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +1 -4
@@ -287,15 +287,11 @@ bool amdgpu_fence_process(struct
 amdgpu_ring *ring)
         if (unlikely(seq == last_seq))
             return false;
     -    last_seq &= drv->num_fences_mask;
-    seq &= drv->num_fences_mask;
-
         do {
             struct dma_fence *fence, **ptr;
                ++last_seq;
-        last_seq &= drv->num_fences_mask;
-        ptr = &drv->fences[last_seq];
+        ptr = &drv->fences[last_seq & drv->num_fences_mask];
                /* There is always exactly one thread signaling
Quoted 2 lines

this fence slot */
           fence = rcu_dereference_protected(*ptr, 1);

Those changes here doesn't seem to make sense. Please explain further why that is necessary.

Christian.

Quoted 2 lines

--
2.25.1