Skip to contentSkip to conversation

drm/amdgpu: Fix desktop freezed after gpu-reset

PATCH Superseded Alan Liu 3 participants 4 messages Apr 14, 2023 – Apr 17, 2023

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Alan Liu

author
#0 lore raw

PATCH 1/1 drm/amdgpu: Fix desktop freezed after gpu-reset

[Why]
After gpu-reset, sometimes the driver would fail to enable vblank irq, causing flip_done timed out and the desktop freezed.

During gpu-reset, we will disable and enable vblank irq in dm_suspend() and dm_resume(). Later on in amdgpu_irq_gpu_reset_resume_helper(), we will check irqs' refcount and decide to enable or disable the irqs again.

However, we have 2 sets of API for controling vblank irq, one is dm_vblank_get/put() and another is amdgpu_irq_get/put(). Each API has its own refcount and flag to store the state of vblank irq, and they are not synchronized.

In drm we use the first API to control vblank irq but in amdgpu_irq_gpu_reset_resume_helper() we use the second set of API.

The failure happens when vblank irq was enabled by dm_vblank_get() before gpu-reset, we have vblank->enabled true. However, during gpu-reset, in amdgpu_irq_gpu_reset_resume_helper(), vblank irq's state checked from amdgpu_irq_update() is DISABLED. So finally it will disable vblank irq again. After gpu-reset, if there is a cursor plane commit, the driver will try to enable vblank irq by calling drm_vblank_enable(), but the vblank->enabled is still true, so it fails to turn on vblank irq and causes flip_done can't be completed in vblank irq handler and desktop become freezed.

[How]
Combining the 2 vblank control APIs by letting drm's API finally calls amdgpu_irq's API, so the irq's refcount and state of both APIs can be synchronized. Also add a check to prevent refcount from being less then 0 in amdgpu_irq_put().

v2:
- Add warning in amdgpu_irq_enable() if the irq is already disabled.
- Call dc_interrupt_set() in dm_set_vblank() to avoid refcount change if it is in gpu-reset.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c         |  3 +++
 .../drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c  | 17 ++++++++++++++---
 2 files changed, 17 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c +3 -0
index a6aef488a822..c8413470e057 100644
@@ -597,6 +597,9 @@ int amdgpu_irq_put(struct amdgpu_device *adev, struct amdgpu_irq_src *src,
 	if (!src->enabled_types || !src->funcs->set)
 		return -EINVAL;
 
+	if (WARN_ON(!amdgpu_irq_enabled(adev, src, type)))
+		return -EINVAL;
+
 	if (atomic_dec_and_test(&src->enabled_types[type]))
 		return amdgpu_irq_update(adev, src, type);
 
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c +14 -3
index 1d924dc51a3e..514fefef109d 100644
@@ -169,10 +169,21 @@ static inline int dm_set_vblank(struct drm_crtc *crtc, bool enable)
 	if (rc)
 		return rc;
 
-	irq_source = IRQ_TYPE_VBLANK + acrtc->otg_inst;
+	if (amdgpu_in_reset(adev)) {
+		irq_source = IRQ_TYPE_VBLANK + acrtc->otg_inst;
+		/* During gpu-reset we'll disable and then enable vblank irq,
+		 * so don't use amdgpu_irq_get/put() to avoid refcount change.
+		 */
+		if (!dc_interrupt_set(adev->dm.dc, irq_source, enable))
+			rc = -EBUSY;
+	} else {
+		rc = (enable)
+			? amdgpu_irq_get(adev, &adev->crtc_irq, acrtc->crtc_id)
+			: amdgpu_irq_put(adev, &adev->crtc_irq, acrtc->crtc_id);
+	}
 
-	if (!dc_interrupt_set(adev->dm.dc, irq_source, enable))
-		return -EBUSY;
+	if (rc)
+		return rc;
 
 skip:
 	if (amdgpu_in_reset(adev))
Signature · 1 line
2.34.1
#1 lore raw

Hi Alan,

Em 14/04/2023 13:22, Alan Liu escreveu:

Quoted 27 lines

[Why]
After gpu-reset, sometimes the driver would fail to enable vblank irq, causing flip_done timed out and the desktop freezed.

During gpu-reset, we will disable and enable vblank irq in dm_suspend() and dm_resume(). Later on in amdgpu_irq_gpu_reset_resume_helper(), we will check irqs' refcount and decide to enable or disable the irqs again.

However, we have 2 sets of API for controling vblank irq, one is dm_vblank_get/put() and another is amdgpu_irq_get/put(). Each API has its own refcount and flag to store the state of vblank irq, and they are not synchronized.

In drm we use the first API to control vblank irq but in amdgpu_irq_gpu_reset_resume_helper() we use the second set of API.

The failure happens when vblank irq was enabled by dm_vblank_get() before gpu-reset, we have vblank->enabled true. However, during gpu-reset, in amdgpu_irq_gpu_reset_resume_helper(), vblank irq's state checked from amdgpu_irq_update() is DISABLED. So finally it will disable vblank irq again. After gpu-reset, if there is a cursor plane commit, the driver will try to enable vblank irq by calling drm_vblank_enable(), but the vblank->enabled is still true, so it fails to turn on vblank irq and causes flip_done can't be completed in vblank irq handler and desktop become freezed.

Quoted 6 lines

[How]
Combining the 2 vblank control APIs by letting drm's API finally calls amdgpu_irq's API, so the irq's refcount and state of both APIs can be synchronized. Also add a check to prevent refcount from being less then 0 in amdgpu_irq_put().

How have you tested this patch?

Quoted 5 lines

v2:
- Add warning in amdgpu_irq_enable() if the irq is already disabled.
- Call dc_interrupt_set() in dm_set_vblank() to avoid refcount change if it is in gpu-reset.

If this is a v2, please use [PATCH v2] in the subject.

Thanks,
André

#2 lore raw

Hi André

Thanks for your comment, please see inline.

Quoted 30 lines · André Almeida

Hi Alan,

Em 14/04/2023 13:22, Alan Liu escreveu:

Quoted 27 lines

[Why]
After gpu-reset, sometimes the driver would fail to enable vblank irq, causing flip_done timed out and the desktop freezed.

During gpu-reset, we will disable and enable vblank irq in dm_suspend() and dm_resume(). Later on in amdgpu_irq_gpu_reset_resume_helper(), we will check irqs' refcount and decide to enable or disable the irqs again.

However, we have 2 sets of API for controling vblank irq, one is dm_vblank_get/put() and another is amdgpu_irq_get/put(). Each API has its own refcount and flag to store the state of vblank irq, and they are not synchronized.

In drm we use the first API to control vblank irq but in amdgpu_irq_gpu_reset_resume_helper() we use the second set of API.

The failure happens when vblank irq was enabled by dm_vblank_get() before gpu-reset, we have vblank->enabled true. However, during gpu-reset, in amdgpu_irq_gpu_reset_resume_helper(), vblank irq's state checked from amdgpu_irq_update() is DISABLED. So finally it will disable vblank irq again. After gpu-reset, if there is a cursor plane commit, the driver will try to enable vblank irq by calling drm_vblank_enable(), but the vblank->enabled is still true, so it fails to turn on vblank irq and causes flip_done can't be completed in vblank irq handler and desktop become freezed.

Quoted 9 lines
Quoted 6 lines

[How]
Combining the 2 vblank control APIs by letting drm's API finally calls amdgpu_irq's API, so the irq's refcount and state of both APIs can be synchronized. Also add a check to prevent refcount from being less then 0 in amdgpu_irq_put().

How have you tested this patch?

I triggered gpu-reset by this command: sudo cat /sys/kernel/debug/dri/0/amdgpu_gpu_recover

When display lights up after gpu-reset, the desktop becomes freezed once I move the cursor (sometimes you need to retrigger gpu-reset for 1 or 2 more times to reproduce it).

I made this patch after finding the root cause, and didn't find the display freezed after testing for about 20 times.

Quoted 7 lines
Quoted 5 lines

v2:
- Add warning in amdgpu_irq_enable() if the irq is already disabled.
- Call dc_interrupt_set() in dm_set_vblank() to avoid refcount change if it is in gpu-reset.

If this is a v2, please use [PATCH v2] in the subject.

Thanks for the reminding. I will keep it in mind.

Best Regards,

Alan

Quoted 3 lines

Thanks,
    André

#3 lore raw
In reply to Alan Liu

Am 14.04.23 um 18:22 schrieb Alan Liu:

Quoted 69 lines

[Why]
After gpu-reset, sometimes the driver would fail to enable vblank irq, causing flip_done timed out and the desktop freezed.

During gpu-reset, we will disable and enable vblank irq in dm_suspend() and dm_resume(). Later on in amdgpu_irq_gpu_reset_resume_helper(), we will check irqs' refcount and decide to enable or disable the irqs again.

However, we have 2 sets of API for controling vblank irq, one is dm_vblank_get/put() and another is amdgpu_irq_get/put(). Each API has its own refcount and flag to store the state of vblank irq, and they are not synchronized.

In drm we use the first API to control vblank irq but in amdgpu_irq_gpu_reset_resume_helper() we use the second set of API.

The failure happens when vblank irq was enabled by dm_vblank_get() before gpu-reset, we have vblank->enabled true. However, during gpu-reset, in amdgpu_irq_gpu_reset_resume_helper(), vblank irq's state checked from amdgpu_irq_update() is DISABLED. So finally it will disable vblank irq again. After gpu-reset, if there is a cursor plane commit, the driver will try to enable vblank irq by calling drm_vblank_enable(), but the vblank->enabled is still true, so it fails to turn on vblank irq and causes flip_done can't be completed in vblank irq handler and desktop become freezed.

[How]
Combining the 2 vblank control APIs by letting drm's API finally calls amdgpu_irq's API, so the irq's refcount and state of both APIs can be synchronized. Also add a check to prevent refcount from being less then 0 in amdgpu_irq_put().

v2:
- Add warning in amdgpu_irq_enable() if the irq is already disabled.
- Call dc_interrupt_set() in dm_set_vblank() to avoid refcount change if it is in gpu-reset.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c         |  3 +++
  .../drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c  | 17 ++++++++++++++---
  2 files changed, 17 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c +3 -0
index a6aef488a822..c8413470e057 100644
@@ -597,6 +597,9 @@ int amdgpu_irq_put(struct amdgpu_device *adev, struct amdgpu_irq_src *src,
  	if (!src->enabled_types || !src->funcs->set)
  		return -EINVAL;
 
+	if (WARN_ON(!amdgpu_irq_enabled(adev, src, type)))
+		return -EINVAL;
+
  	if (atomic_dec_and_test(&src->enabled_types[type]))
  		return amdgpu_irq_update(adev, src, type);
 
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c +2 -1
index 1d924dc51a3e..514fefef109d 100644
@@ -169,10 +169,21 @@ static inline int dm_set_vblank(struct drm_crtc *crtc, bool enable)
  	if (rc)
  		return rc;
 
-	irq_source = IRQ_TYPE_VBLANK + acrtc->otg_inst;
+	if (amdgpu_in_reset(adev)) {
+		irq_source = IRQ_TYPE_VBLANK + acrtc->otg_inst;

drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c static inline int dm_set_vblank(struct drm_crtc *crtc, bool enable) · line 183

Quoted 10 lines
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c +10 -0
+		/* During gpu-reset we'll disable and then enable vblank irq,
+		 * so don't use amdgpu_irq_get/put() to avoid refcount change.
+		 */
+		if (!dc_interrupt_set(adev->dm.dc, irq_source, enable))
+			rc = -EBUSY;
+	} else {
+		rc = (enable)
+			? amdgpu_irq_get(adev, &adev->crtc_irq, acrtc->crtc_id)
+			: amdgpu_irq_put(adev, &adev->crtc_irq, acrtc->crtc_id);
+	}

It would be cleaner if we could do this on a higher level, e.g. in the GPU reset code.

But for now that should do it and I think this could even be back-ported to stable.

Regards,
Christian.

Quoted 8 lines
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c +2 -2
-	if (!dc_interrupt_set(adev->dm.dc, irq_source, enable))
-		return -EBUSY;
+	if (rc)
+		return rc;
 
  skip:
  	if (amdgpu_in_reset(adev))