Skip to contentSkip to conversation

drm/amdgpu: Fix desktop freezed after gpu-reset

PATCH In review Alan Liu 5 participants 12 messages Mar 27, 2023 – Mar 31, 2023

Subsystems (3)
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().

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c            |  3 +++
 .../gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c | 14 ++++++++++----
 2 files changed, 13 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c +3 -0
index a6aef488a822..1b66003657e2 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 (!amdgpu_irq_enabled(adev, src, type))
+		return 0;
+
 	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 +10 -4
index dc4f37240beb..e04f846b0b19 100644
@@ -146,7 +146,7 @@ static void vblank_control_worker(struct work_struct *work)
 
 static inline int dm_set_vblank(struct drm_crtc *crtc, bool enable)
 {
-	enum dc_irq_source irq_source;
+	int irq_type;
 	struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
 	struct amdgpu_device *adev = drm_to_adev(crtc->dev);
 	struct dm_crtc_state *acrtc_state = to_dm_crtc_state(crtc->state);
@@ -169,10 +169,16 @@ static inline int dm_set_vblank(struct drm_crtc *crtc, bool enable)
 	if (rc)
 		return rc;
 
-	irq_source = IRQ_TYPE_VBLANK + acrtc->otg_inst;
+	irq_type = amdgpu_display_crtc_idx_to_irq_type(adev, acrtc->crtc_id);
+
+	if (enable)
+		rc = amdgpu_irq_get(adev, &adev->crtc_irq, irq_type);
+
+	else
+		rc = amdgpu_irq_put(adev, &adev->crtc_irq, irq_type);
 
-	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,

That's a good fix. Some questions and comments below:

Quoted 4 lines · Alan Liu

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

Quoted 8 lines

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.

Is it possible to reconcile controlling VBlank IRQ to a single refcount?

Quoted 54 lines

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().

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c            |  3 +++
 .../gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c | 14 ++++++++++----
 2 files changed, 13 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c +3 -0
index a6aef488a822..1b66003657e2 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 (!amdgpu_irq_enabled(adev, src, type))
+		return 0;
+
 	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 +1 -1
index dc4f37240beb..e04f846b0b19 100644
@@ -146,7 +146,7 @@ static void vblank_control_worker(struct work_struct *work)
 
 static inline int dm_set_vblank(struct drm_crtc *crtc, bool enable)
 {
-	enum dc_irq_source irq_source;
+	int irq_type;
 	struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
 	struct amdgpu_device *adev = drm_to_adev(crtc->dev);
 	struct dm_crtc_state *acrtc_state = to_dm_crtc_state(crtc->state);

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

Quoted 10 lines
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c +6 -1
@@ -169,10 +169,16 @@ static inline int dm_set_vblank(struct drm_crtc *crtc, bool enable)
 	if (rc)
 		return rc;
 
-	irq_source = IRQ_TYPE_VBLANK + acrtc->otg_inst;
+	irq_type = amdgpu_display_crtc_idx_to_irq_type(adev, acrtc->crtc_id);
+
+	if (enable)
+		rc = amdgpu_irq_get(adev, &adev->crtc_irq, irq_type);
+
+	else

There's an unnecessary empty line before the "else". It's a good idea to run patches through scripts/checkpatch.pl.

Quoted 9 lines
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c +3 -2
+		rc = amdgpu_irq_put(adev, &adev->crtc_irq, irq_type);
 
-	if (!dc_interrupt_set(adev->dm.dc, irq_source, enable))
-		return -EBUSY;
+	if (rc)
+		return rc;
 
 skip:
 	if (amdgpu_in_reset(adev))
Signature · 2 lines
Regards,
Luben
#2 lore raw

[AMD Official Use Only - General]

Hi, Luben

Thanks for the review. Please see inline.

Best Regards,

Alan

-----Original Message-----
From: Tuikov, Luben <[email protected]>
Sent: Tuesday, March 28, 2023 3:00 AM
To: Liu, HaoPing (Alan) <[email protected]>; [email protected]
Cc: Lakha, Bhawanpreet <[email protected]>
Subject: Re: [PATCH] drm/amdgpu: Fix desktop freezed after gpu-reset

Hi,

That's a good fix. Some questions and comments below:

Quoted 16 lines · Alan Liu

[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.

Quoted 7 lines

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.

Is it possible to reconcile controlling VBlank IRQ to a single refcount?

In struct drm_vblank_crtc, we have "enabled" and "refcount" to store vblank irq state, and in struct amdgpu_irq_src we have "enabled_types" as the refcount for each irq in dm layer.

To reconcile vblank irq to a single refcount, my idea is to remove enabled and refcount from struct drm_vblank_crtc, and add a callback function like vblank_irq_enabled() to drm_crtc_funcs.

Drm layer can use this function to check the state or refcount of vblank irq from dm layer. But it may be dangerous because it is a change to drm layer. Do you have any comments?

Quoted 131 lines

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().

---

 drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c            |  3 +++
 .../gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c | 14

++++++++++----

 2 files changed, 13 insertions(+), 4 deletions(-)
(quoted diff) +0 -0

b/drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c

index a6aef488a822..1b66003657e2 100644

--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c

+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c

(quoted diff) +3 -0
@@ -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 (!amdgpu_irq_enabled(adev, src, type))
 
+                       return 0;
 
+
 
           if (atomic_dec_and_test(&src->enabled_types[type]))
 
                          return amdgpu_irq_update(adev, src, type);
(quoted diff) +0 -0

b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c

index dc4f37240beb..e04f846b0b19 100644

--- a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c

+++ b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c

(quoted diff) +0 -0
@@ -146,7 +146,7 @@ static void vblank_control_worker(struct
 
 work_struct *work)
 
 
 
 static inline int dm_set_vblank(struct drm_crtc *crtc, bool enable)
 

{

drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c +3 -2
-          enum dc_irq_source irq_source;
 
+         int irq_type;
 
           struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
 
           struct amdgpu_device *adev = drm_to_adev(crtc->dev);
 
           struct dm_crtc_state *acrtc_state = to_dm_crtc_state(crtc->state);
@@ -169,10 +169,16 @@ static inline int dm_set_vblank(struct drm_crtc *crtc, bool enable)
 
           if (rc)
 
                          return rc;
 
 
 
-          irq_source = IRQ_TYPE_VBLANK + acrtc->otg_inst;
 
+         irq_type = amdgpu_display_crtc_idx_to_irq_type(adev,
 
+acrtc->crtc_id);

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

Quoted 10 lines
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c +5 -0
 
+
 
+         if (enable)
 
+                       rc = amdgpu_irq_get(adev, &adev->crtc_irq, irq_type);
 
+
 
+         else

There's an unnecessary empty line before the "else". It's a good idea to run patches through scripts/checkpatch.pl.

Thanks, will use the tool next time.

Quoted 7 lines
(quoted diff) +1 -2
+                       rc = amdgpu_irq_put(adev, &adev->crtc_irq, irq_type);
 
 
 
-          if (!dc_interrupt_set(adev->dm.dc, irq_source, enable))
 
-                        return -EBUSY;

Review

Quoted 10 lines
(quoted diff) +2 -0
 
+         if (rc)
 
+                       return rc;
 
 
 
 skip:
 
           if (amdgpu_in_reset(adev))

--

Regards,

Luben

#3 lore raw

Hi Alan,

I'll comment in the other thread, as it seems Christian commented directly to your patch the day after my comment, rather than following up with my email sent the previous day and we now have two divergent threads where you post two identical comments, and it shouldn't be like that--we should have one thread only.

Regards,
Luben

Quoted 254 lines · HaoPing (Alan)

[AMD Official Use Only - General]

Hi, Luben

Thanks for the review. Please see inline.

Best Regards,

Alan

-----Original Message-----
From: Tuikov, Luben <[email protected]>
Sent: Tuesday, March 28, 2023 3:00 AM
To: Liu, HaoPing (Alan) <[email protected]>; [email protected]
Cc: Lakha, Bhawanpreet <[email protected]>
Subject: Re: [PATCH] drm/amdgpu: Fix desktop freezed after gpu-reset

Hi,

That's a good fix. Some questions and comments below:

Quoted 16 lines · Alan Liu

[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.

Quoted 7 lines

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.

Is it possible to reconcile controlling VBlank IRQ to a single refcount?

In struct drm_vblank_crtc, we have “enabled” and “refcount” to store vblank irq state, and in struct amdgpu_irq_src we have “enabled_types” as the refcount for each irq in dm layer.

To reconcile vblank irq to a single refcount, my idea is to remove enabled and refcount from struct drm_vblank_crtc, and add a callback function like vblank_irq_enabled() to drm_crtc_funcs.

Drm layer can use this function to check the state or refcount of vblank irq from dm layer. But it may be dangerous because it is a change to drm layer. Do you have any comments?

Quoted 131 lines

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().

Signed-off-by Alan Liu <[email protected] <mailto:[email protected]>>

---

  drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c            |  3 +++

  .../gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c | 14

++++++++++----

  2 files changed, 13 insertions(+), 4 deletions(-)
(quoted diff) +0 -0

b/drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c

index a6aef488a822..1b66003657e2 100644

--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c

+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c

drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c +0 -0
@@ -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 (!amdgpu_irq_enabled(adev, src, type))

+                       return 0;

+

            if (atomic_dec_and_test(&src->enabled_types[type]))

                           return amdgpu_irq_update(adev, src, type);

(quoted diff) +0 -0

b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c

index dc4f37240beb..e04f846b0b19 100644

--- a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c

+++ b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c

(quoted diff) +0 -0
@@ -146,7 +146,7 @@ static void vblank_control_worker(struct
 
 work_struct *work)
 

 static inline int dm_set_vblank(struct drm_crtc *crtc, bool enable)

{

-          enum dc_irq_source irq_source;

+         int irq_type;

            struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);

            struct amdgpu_device *adev = drm_to_adev(crtc->dev);

            struct dm_crtc_state *acrtc_state = to_dm_crtc_state(crtc->state);

drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c +0 -0
@@ -169,10 +169,16 @@ static inline int dm_set_vblank(struct drm_crtc *crtc, bool enable)
 
             if (rc)
 
                            return rc;
 

-          irq_source = IRQ_TYPE_VBLANK + acrtc->otg_inst;

+         irq_type = amdgpu_display_crtc_idx_to_irq_type(adev,

(quoted diff) +1 -0
+acrtc->crtc_id);

Review

Quoted 10 lines
(quoted diff) +5 -0
 
+
 
+         if (enable)
 
+                       rc = amdgpu_irq_get(adev, &adev->crtc_irq, irq_type);
 
+
 
+         else

There's an unnecessary empty line before the "else". It's a good idea to run patches through scripts/checkpatch.pl.

Thanks, will use the tool next time.

Quoted 10 lines

+                       rc = amdgpu_irq_put(adev, &adev->crtc_irq, irq_type);

-          if (!dc_interrupt_set(adev->dm.dc, irq_source, enable))

-                        return -EBUSY;

+         if (rc)

Quoted 7 lines

+                       return rc;

  skip:

            if (amdgpu_in_reset(adev))

--

Regards,

Luben

#4 lore raw

Hi Luben,

sorry, looks like I haven't seen your reply in my inbox at the time I've wrote my reply.

That seems to happen quite often with freedesktop.org list recently and I'm wondering if there's something wrong with my setup or the list server.

Christian.

Am 30.03.23 um 14:27 schrieb Luben Tuikov:

Quoted 166 lines

Hi Alan,

I'll comment in the other thread, as it seems Christian commented directly to your patch the day after my comment, rather than following up with my email sent the previous day and we now have two divergent threads where you post two identical comments, and it shouldn't be like that--we should have one thread only.

Regards,
Luben

Quoted 155 lines · HaoPing (Alan)

[AMD Official Use Only - General]

Hi, Luben

Thanks for the review. Please see inline.

Best Regards,

Alan

-----Original Message-----
From: Tuikov, Luben <[email protected]>
Sent: Tuesday, March 28, 2023 3:00 AM
To: Liu, HaoPing (Alan) <[email protected]>; [email protected]
Cc: Lakha, Bhawanpreet <[email protected]>
Subject: Re: [PATCH] drm/amdgpu: Fix desktop freezed after gpu-reset

Hi,

That's a good fix. Some questions and comments below:

Quoted 10 lines · Alan Liu

[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.

Is it possible to reconcile controlling VBlank IRQ to a single refcount?

In struct drm_vblank_crtc, we have “enabled” and “refcount” to store vblank irq state, and in struct amdgpu_irq_src we have “enabled_types” as the refcount for each irq in dm layer.

To reconcile vblank irq to a single refcount, my idea is to remove enabled and refcount from struct drm_vblank_crtc, and add a callback function like vblank_irq_enabled() to drm_crtc_funcs.

Drm layer can use this function to check the state or refcount of vblank irq from dm layer. But it may be dangerous because it is a change to drm layer. Do you have any comments?

Quoted 56 lines

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().

Signed-off-by Alan Liu <[email protected] <mailto:[email protected]>>
---
   drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c            |  3 +++
   .../gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c | 14
++++++++++----
   2 files changed, 13 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c +3 -0
index a6aef488a822..1b66003657e2 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 (!amdgpu_irq_enabled(adev, src, type))
+                       return 0;
+
             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 +1 -1
index dc4f37240beb..e04f846b0b19 100644
@@ -146,7 +146,7 @@ static void vblank_control_worker(struct
 work_struct *work)
 
  static inline int dm_set_vblank(struct drm_crtc *crtc, bool enable)
 {
-          enum dc_irq_source irq_source;
+         int irq_type;
             struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
             struct amdgpu_device *adev = drm_to_adev(crtc->dev);

            struct dm_crtc_state *acrtc_state = to_dm_crtc_state(crtc->state);

drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c +0 -0
@@ -169,10 +169,16 @@ static inline int dm_set_vblank(struct drm_crtc *crtc, bool enable)
             if (rc)

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

Quoted 10 lines
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c +7 -1
                            return rc;
 
-          irq_source = IRQ_TYPE_VBLANK + acrtc->otg_inst;
+         irq_type = amdgpu_display_crtc_idx_to_irq_type(adev,
+acrtc->crtc_id);
+
+         if (enable)
+                       rc = amdgpu_irq_get(adev, &adev->crtc_irq, irq_type);
+
+         else

There's an unnecessary empty line before the "else". It's a good idea to run patches through scripts/checkpatch.pl.

Thanks, will use the tool next time.

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

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

--

Regards,

Luben

#5 lore raw
In reply to Alan Liu

Am 27.03.23 um 17:20 schrieb Alan Liu:

Quoted 4 lines

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

Quoted 8 lines

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.

This is the source of the problem and you should address this instead. The change you suggested below would break in some use cases.

Quoted 31 lines

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().

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c            |  3 +++
  .../gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c | 14 ++++++++++----
  2 files changed, 13 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c +0 -0
index a6aef488a822..1b66003657e2 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c int amdgpu_irq_put(struct amdgpu_device *adev, struct amdgpu_irq_src *src, · line 602

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c +3 -0
@@ -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 (!amdgpu_irq_enabled(adev, src, type))
+		return 0;
+

That is racy and won't work. The intention of amdgpu_irq_update() is to always update the irq state, no matter what the status is.

Regards,
Christian.

Quoted 36 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c +0 -0
  	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 +10 -4
index dc4f37240beb..e04f846b0b19 100644
@@ -146,7 +146,7 @@ static void vblank_control_worker(struct work_struct *work)
 
  static inline int dm_set_vblank(struct drm_crtc *crtc, bool enable)
  {
-	enum dc_irq_source irq_source;
+	int irq_type;
  	struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
  	struct amdgpu_device *adev = drm_to_adev(crtc->dev);
  	struct dm_crtc_state *acrtc_state = to_dm_crtc_state(crtc->state);
@@ -169,10 +169,16 @@ static inline int dm_set_vblank(struct drm_crtc *crtc, bool enable)
  	if (rc)
  		return rc;
 
-	irq_source = IRQ_TYPE_VBLANK + acrtc->otg_inst;
+	irq_type = amdgpu_display_crtc_idx_to_irq_type(adev, acrtc->crtc_id);
+
+	if (enable)
+		rc = amdgpu_irq_get(adev, &adev->crtc_irq, irq_type);
+
+	else
+		rc = amdgpu_irq_put(adev, &adev->crtc_irq, irq_type);
 
-	if (!dc_interrupt_set(adev->dm.dc, irq_source, enable))
-		return -EBUSY;
+	if (rc)
+		return rc;
 
  skip:
  	if (amdgpu_in_reset(adev))
#6 lore raw

[AMD Official Use Only - General]

Hi Christian,

Thanks for the review. Please see inline.

Best Regards,

Alan

-----Original Message-----
From: Christian König <[email protected]>
Sent: Tuesday, March 28, 2023 7:16 PM
To: Liu, HaoPing (Alan) <[email protected]>; [email protected]
Cc: Lakha, Bhawanpreet <[email protected]>
Subject: Re: [PATCH] drm/amdgpu: Fix desktop freezed after gpu-reset

Am 27.03.23 um 17:20 schrieb Alan Liu:

Quoted 16 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.

Quoted 7 lines

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.

This is the source of the problem and you should address this instead.

The change you suggested below would break in some use cases.

In struct drm_vblank_crtc we have a vblank irq refcount controlled by drm layer, and in struct amdgpu_irq_src we have enabled_types as refcount for each irq controlled by the dm.

I think the best solution will be to get rid of the refcount in drm and only maintain the dm one, and add a crtc function for the drm layer to get the refcount/state of vblank.

But this may be dangerous since it's a change in drm layer. Do you have any comments?

Quoted 69 lines

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().

---

  drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c            |  3 +++
  .../gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c | 14 ++++++++++----
  2 files changed, 13 insertions(+), 4 deletions(-)
(quoted diff) +0 -0

b/drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c

index a6aef488a822..1b66003657e2 100644

--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c

+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c

(quoted diff) +0 -0
@@ -597,6 +597,9 @@ int amdgpu_irq_put(struct amdgpu_device *adev, struct amdgpu_irq_src *src,
 
           if (!src->enabled_types || !src->funcs->set)

Review int amdgpu_irq_put(struct amdgpu_device *adev, struct amdgpu_irq_src *src,

Quoted 10 lines
(quoted diff) +3 -0
 
                          return -EINVAL;
 
 
 
+         if (!amdgpu_irq_enabled(adev, src, type))
 
+                       return 0;
 
+

That is racy and won't work. The intention of amdgpu_irq_update() is to always update the irq state, no matter what the status is.

This is a change to amdgpu_irq_put() to prevent the refcount from being cut to less than 0. Does it break the intention of amdgpu_irq_update()?

Regards,

Christian.

Quoted 77 lines
           if (atomic_dec_and_test(&src->enabled_types[type]))

                          return amdgpu_irq_update(adev, src, type);
(quoted diff) +0 -0

b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c

index dc4f37240beb..e04f846b0b19 100644

--- a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c

+++ b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c

(quoted diff) +0 -0
@@ -146,7 +146,7 @@ static void vblank_control_worker(struct
 
 work_struct *work)
 
 
 
  static inline int dm_set_vblank(struct drm_crtc *crtc, bool enable)
 
  {
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c +11 -4
-          enum dc_irq_source irq_source;
 
+         int irq_type;
 
           struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
 
           struct amdgpu_device *adev = drm_to_adev(crtc->dev);
 
           struct dm_crtc_state *acrtc_state = to_dm_crtc_state(crtc->state);
@@ -169,10 +169,16 @@ static inline int dm_set_vblank(struct drm_crtc *crtc, bool enable)
 
           if (rc)
 
                          return rc;
 
 
 
-          irq_source = IRQ_TYPE_VBLANK + acrtc->otg_inst;
 
+         irq_type = amdgpu_display_crtc_idx_to_irq_type(adev,
 
+acrtc->crtc_id);
 
+
 
+         if (enable)
 
+                       rc = amdgpu_irq_get(adev, &adev->crtc_irq, irq_type);
 
+
 
+         else
 
+                       rc = amdgpu_irq_put(adev, &adev->crtc_irq, irq_type);
 
 
 
-          if (!dc_interrupt_set(adev->dm.dc, irq_source, enable))
 
-                        return -EBUSY;
 
+         if (rc)
 
+                       return rc;
 
 
 
  skip:
 
           if (amdgpu_in_reset(adev))
#7 lore raw

Am 30.03.23 um 11:15 schrieb Liu, HaoPing (Alan):

Quoted 47 lines

[AMD Official Use Only - General]

Hi Christian,

Thanks for the review. Please see inline.

Best Regards,

Alan

-----Original Message-----
From: Christian König <[email protected]>
Sent: Tuesday, March 28, 2023 7:16 PM
To: Liu, HaoPing (Alan) <[email protected]>; [email protected]
Cc: Lakha, Bhawanpreet <[email protected]>
Subject: Re: [PATCH] drm/amdgpu: Fix desktop freezed after gpu-reset

Am 27.03.23 um 17:20 schrieb Alan Liu:

Quoted 8 lines

[Why]

After gpu-reset, sometimes the driver would fail to enable vblank irq,

causing flip_done timed out and the desktop freezed.

Quoted 5 lines

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.

Quoted 9 lines

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.

This is the source of the problem and you should address this instead.

Quoted 8 lines

The change you suggested below would break in some use cases.

In struct drm_vblank_crtc we have a vblank irq refcount controlled by drm layer, and in struct amdgpu_irq_src we have enabled_types as refcount for each irq controlled by the dm.

I think the best solution will be to get rid of the refcount in drm and only maintain the dm one, and add a crtc function for the drm layer to get the refcount/state of vblank.

But this may be dangerous since it’s a change in drm layer. Do you have any comments?

You don't necessarily need to remove it completely, what you can do as well is properly chaining of them.

E.g. when the DRM counter goes from 0->1 or 1->0 it calls some function to enable/disable the hw irq. In this situation you call amdgpu_irq_get()/amdgpu_irq_put() as appropriate.

The the code in this patch already looks like it goes into the right direction regarding that. It just seems to be that you have some race issues when you need to add checks that the IRQ counter doesn't goes below 0.

Quoted 78 lines
Quoted 38 lines

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

Quoted 7 lines

then

0 in amdgpu_irq_put().

<mailto:[email protected]>>

Review int amdgpu_irq_put(struct amdgpu_device *adev,

Quoted 21 lines

---

drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c            |  3 +++

.../gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c | 14 ++++++++++----

   2 files changed, 13 insertions(+), 4 deletions(-)
(quoted diff) +0 -0

b/drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c

index a6aef488a822..1b66003657e2 100644

--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c

+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c

(quoted diff) +0 -0
@@ -597,6 +597,9 @@ int amdgpu_irq_put(struct amdgpu_device *adev, 

struct amdgpu_irq_src *src,

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c +1 -0
             if (!src->enabled_types || !src->funcs->set)
 
                            return -EINVAL;
 
 
 
+         if (!amdgpu_irq_enabled(adev, src, type))
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c int amdgpu_irq_put(struct amdgpu_device *adev, struct amdgpu_irq_src *src,

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c +2 -0
+                       return 0;
 
+

That is racy and won't work. The intention of amdgpu_irq_update() is to always update the irq state, no matter what the status is.

This is a change to amdgpu_irq_put() to prevent the refcount from being cut to less than 0. Does it break the intention of amdgpu_irq_update()?

Yes, exactly that. The reference count can *never* be below 0 or you have a bug in the caller.

What you could do is to add a WARN_ON(!amdgpu_irq_enabled(adev, src, type)), but just ignoring the call is an absolute no-go.

Regards,
Christian.

PS: Please don't use HTML formating when discussing on public mailing lists.

Quoted 86 lines

Regards,

Christian.

Quoted 28 lines

            if (atomic_dec_and_test(&src->enabled_types[type]))

                           return amdgpu_irq_update(adev, src, type);

(quoted diff) +0 -0

b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c

index dc4f37240beb..e04f846b0b19 100644

--- a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c

+++ b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c

(quoted diff) +0 -0
@@ -146,7 +146,7 @@ static void vblank_control_worker(struct
 
 work_struct *work)
 
 
 
    static inline int dm_set_vblank(struct drm_crtc *crtc, bool enable)
 

   {

-          enum dc_irq_source irq_source;

Quoted 7 lines

+         int irq_type;

            struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);

            struct amdgpu_device *adev = drm_to_adev(crtc->dev);

            struct dm_crtc_state *acrtc_state =

to_dm_crtc_state(crtc->state);

Review static inline int dm_set_vblank(struct

Quoted 1 line
(quoted diff) +0 -0
@@ -169,10 +169,16 @@ static inline int dm_set_vblank(struct 

drm_crtc *crtc, bool enable)

Quoted 7 lines
(quoted diff) +0 -1
             if (rc)
 
                            return rc;
 
 
 
-          irq_source = IRQ_TYPE_VBLANK + acrtc->otg_inst;

Review static inline int dm_set_vblank(struct

Quoted 10 lines
(quoted diff) +5 -0
 
+         irq_type = amdgpu_display_crtc_idx_to_irq_type(adev,
 
+acrtc->crtc_id);
 
+
 
+         if (enable)
 
+                       rc = amdgpu_irq_get(adev, &adev->crtc_irq, 

irq_type);

Quoted 5 lines

+

+         else

+                       rc = amdgpu_irq_put(adev, &adev->crtc_irq,

irq_type);

Quoted 15 lines

-          if (!dc_interrupt_set(adev->dm.dc, irq_source, enable))

-                        return -EBUSY;

+         if (rc)

+                       return rc;

   skip:

            if (amdgpu_in_reset(adev))

#8 lore raw

Hi Alan,

Inline:

Quoted 67 lines · Christian König

Am 30.03.23 um 11:15 schrieb Liu, HaoPing (Alan):

Quoted 66 lines

[AMD Official Use Only - General]

Hi Christian,

Thanks for the review. Please see inline.

Best Regards,

Alan

-----Original Message-----
From: Christian König <[email protected]>
Sent: Tuesday, March 28, 2023 7:16 PM
To: Liu, HaoPing (Alan) <[email protected]>; [email protected]
Cc: Lakha, Bhawanpreet <[email protected]>
Subject: Re: [PATCH] drm/amdgpu: Fix desktop freezed after gpu-reset

Am 27.03.23 um 17:20 schrieb Alan Liu:

Quoted 16 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.

Quoted 7 lines

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.

This is the source of the problem and you should address this instead.

The change you suggested below would break in some use cases.

In struct drm_vblank_crtc we have a vblank irq refcount controlled by drm layer, and in struct amdgpu_irq_src we have enabled_types as refcount for each irq controlled by the dm.

I think the best solution will be to get rid of the refcount in drm and only maintain the dm one, and add a crtc function for the drm layer to get the refcount/state of vblank.

Quoted 8 lines
Quoted 2 lines

But this may be dangerous since it’s a change in drm layer. Do you have any comments?

You don't necessarily need to remove it completely, what you can do as well is properly chaining of them.

E.g. when the DRM counter goes from 0->1 or 1->0 it calls some function to enable/disable the hw irq. In this situation you call amdgpu_irq_get()/amdgpu_irq_put() as appropriate.

The the code in this patch already looks like it goes into the right direction regarding that. It just seems to be that you have some race issues when you need to add checks that the IRQ counter doesn't goes below 0.

Changing the DRM layer is generally not a good idea, unless there is a compelling reason to do so, like fixing a bug, or adding a new feature benefiting all drivers. As there are many drivers using DRM, any changes in DRM are vetted thoroughly and need a good reason to take place.

I suggest you follow Christian's advice.

Note that there's already a callback from drm_vblank_get() down to amdgpu_enable_vblank_kms() which calls amdgpu_irq_get(). Perhaps, you can leverage that. Similarly for the drm_vblank_put() to the amdgpu_vblank_put() path.

Quoted 87 lines
Quoted 87 lines

Quoted 74 lines

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().

Signed-off-by Alan Liu <[email protected] <mailto:[email protected]>>

---

   drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c            |  3 +++

   .../gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c | 14 ++++++++++----

   2 files changed, 13 insertions(+), 4 deletions(-)

(quoted diff) +0 -0

b/drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c

index a6aef488a822..1b66003657e2 100644

--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c

+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c

drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c +0 -0
@@ -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;
 

Quoted 5 lines

+         if (!amdgpu_irq_enabled(adev, src, type))

+                       return 0;

+

That is racy and won't work. The intention of amdgpu_irq_update() is to always update the irq state, no matter what the status is.

Quoted 9 lines
Quoted 4 lines

This is a change to amdgpu_irq_put() to prevent the refcount from being cut to less than 0. Does it break the intention of amdgpu_irq_update()?

Yes, exactly that. The reference count can *never* be below 0 or you have a bug in the caller.

What you could do is to add a WARN_ON(!amdgpu_irq_enabled(adev, src, type)), but just ignoring the call is an absolute no-go.

In addition to adding the WARN_ON() as Christian suggested, and noting that you cannot ignore the amdgpu_irq_put() call, perhaps investigate if you can allow the decrement to take place and then check for negative--atomic_t is an "int". If it is negative, then complain, dump the stack, set to 0 and return.

Signature · 2 lines
Regards,
Luben
Quoted 94 lines

Regards,
Christian.

PS: Please don't use HTML formating when discussing on public mailing lists.

Quoted 88 lines

Regards,

Christian.

Quoted 70 lines

            if (atomic_dec_and_test(&src->enabled_types[type]))

                           return amdgpu_irq_update(adev, src, type);

(quoted diff) +0 -0

b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c

index dc4f37240beb..e04f846b0b19 100644

--- a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c

+++ b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c

(quoted diff) +0 -0
@@ -146,7 +146,7 @@ static void vblank_control_worker(struct
 
 work_struct *work)
 

   static inline int dm_set_vblank(struct drm_crtc *crtc, bool enable)

   {

-          enum dc_irq_source irq_source;

+         int irq_type;

            struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);

            struct amdgpu_device *adev = drm_to_adev(crtc->dev);

            struct dm_crtc_state *acrtc_state = to_dm_crtc_state(crtc->state);

drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c +0 -0
@@ -169,10 +169,16 @@ static inline int dm_set_vblank(struct drm_crtc *crtc, bool enable)
 
             if (rc)
 
                            return rc;
 

-          irq_source = IRQ_TYPE_VBLANK + acrtc->otg_inst;

+         irq_type = amdgpu_display_crtc_idx_to_irq_type(adev,

(quoted diff) +7 -0
+acrtc->crtc_id);
 
+
 
+         if (enable)
 
+                       rc = amdgpu_irq_get(adev, &adev->crtc_irq, irq_type);
 
+
 
+         else
 
+                       rc = amdgpu_irq_put(adev, &adev->crtc_irq, irq_type);

-          if (!dc_interrupt_set(adev->dm.dc, irq_source, enable))

-                        return -EBUSY;

+         if (rc)

Quoted 7 lines

+                       return rc;

   skip:

            if (amdgpu_in_reset(adev))

maintainer
#9 lore raw
Quoted 79 lines · Luben Tuikov

Hi Alan,

Inline:

Quoted 74 lines · Christian König

Am 30.03.23 um 11:15 schrieb Liu, HaoPing (Alan):

Quoted 60 lines

[AMD Official Use Only - General]

Hi Christian,

Thanks for the review. Please see inline.

Best Regards,

Alan

-----Original Message-----
From: Christian König <[email protected]>
Sent: Tuesday, March 28, 2023 7:16 PM
To: Liu, HaoPing (Alan) <[email protected]>; [email protected]
Cc: Lakha, Bhawanpreet <[email protected]>
Subject: Re: [PATCH] drm/amdgpu: Fix desktop freezed after gpu-reset

Am 27.03.23 um 17:20 schrieb Alan Liu:

Quoted 16 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.

Quoted 7 lines

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.

This is the source of the problem and you should address this instead.

The change you suggested below would break in some use cases.

Quoted 8 lines

In struct drm_vblank_crtc we have a vblank irq refcount controlled by drm layer, and in struct amdgpu_irq_src we have enabled_types as refcount for each irq controlled by the dm.

I think the best solution will be to get rid of the refcount in drm and only maintain the dm one, and add a crtc function for the drm layer to get the refcount/state of vblank.

But this may be dangerous since it’s a change in drm layer. Do you have any comments?

You don't necessarily need to remove it completely, what you can do as well is properly chaining of them.

E.g. when the DRM counter goes from 0->1 or 1->0 it calls some function to enable/disable the hw irq. In this situation you call amdgpu_irq_get()/amdgpu_irq_put() as appropriate.

Quoted 8 lines
Quoted 1 line

The the code in this patch already looks like it goes into the right direction regarding that. It just seems to be that you have some race issues when you need to add checks that the IRQ counter doesn't goes below 0.

Changing the DRM layer is generally not a good idea, unless there is a compelling reason to do so, like fixing a bug, or adding a new feature benefiting all drivers. As there are many drivers using DRM, any changes in DRM are vetted thoroughly and need a good reason to take place.

While I agree in this case I also observe that we sometimes shy away from DRM changes when they make sense and instead do weird workarounds in our driver. I would encourage DRM changes when they make sense but when one does so one needs to make sure none of the other DRM drivers break.

Harry

Quoted 108 lines

I suggest you follow Christian's advice.

Note that there's already a callback from drm_vblank_get() down to amdgpu_enable_vblank_kms() which calls amdgpu_irq_get(). Perhaps, you can leverage that. Similarly for the drm_vblank_put() to the amdgpu_vblank_put() path.

Quoted 87 lines
Quoted 87 lines

Quoted 74 lines

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().

Signed-off-by Alan Liu <[email protected] <mailto:[email protected]>>

---

   drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c            |  3 +++

   .../gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c | 14 ++++++++++----

   2 files changed, 13 insertions(+), 4 deletions(-)

(quoted diff) +0 -0

b/drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c

index a6aef488a822..1b66003657e2 100644

--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c

+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c

drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c +0 -0
@@ -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;
 

Quoted 5 lines

+         if (!amdgpu_irq_enabled(adev, src, type))

+                       return 0;

+

That is racy and won't work. The intention of amdgpu_irq_update() is to always update the irq state, no matter what the status is.

Quoted 9 lines
Quoted 4 lines

This is a change to amdgpu_irq_put() to prevent the refcount from being cut to less than 0. Does it break the intention of amdgpu_irq_update()?

Yes, exactly that. The reference count can *never* be below 0 or you have a bug in the caller.

What you could do is to add a WARN_ON(!amdgpu_irq_enabled(adev, src, type)), but just ignoring the call is an absolute no-go.

In addition to adding the WARN_ON() as Christian suggested, and noting that you cannot ignore the amdgpu_irq_put() call, perhaps investigate if you can allow the decrement to take place and then check for negative--atomic_t is an "int". If it is negative, then complain, dump the stack, set to 0 and return.

#10 lore raw
In reply to Christian König
Quoted 68 lines · Christian König

Am 30.03.23 um 11:15 schrieb Liu, HaoPing (Alan):

[AMD Official Use Only - General]

Hi Christian,

Thanks for the review. Please see inline.

Best Regards,

Alan

-----Original Message-----
From: Christian König <[email protected]>
Sent: Tuesday, March 28, 2023 7:16 PM
To: Liu, HaoPing (Alan) <[email protected]>; [email protected]
Cc: Lakha, Bhawanpreet <[email protected]>
Subject: Re: [PATCH] drm/amdgpu: Fix desktop freezed after gpu-reset

Am 27.03.23 um 17:20 schrieb Alan Liu:

Quoted 16 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.

Quoted 7 lines

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.

This is the source of the problem and you should address this instead.

The change you suggested below would break in some use cases.

In struct drm_vblank_crtc we have a vblank irq refcount controlled by drm layer, and in struct amdgpu_irq_src we have enabled_types as refcount for each irq controlled by the dm.

I think the best solution will be to get rid of the refcount in drm and only maintain the dm one, and add a crtc function for the drm layer to get the refcount/state of vblank.

Quoted 9 lines

But this may be dangerous since it’s a change in drm layer. Do you have any comments?

You don't necessarily need to remove it completely, what you can do as well is properly chaining of them.

E.g. when the DRM counter goes from 0->1 or 1->0 it calls some function to enable/disable the hw irq. In this situation you call amdgpu_irq_get()/amdgpu_irq_put() as appropriate.

The the code in this patch already looks like it goes into the right direction regarding that. It just seems to be that you have some race issues when you need to add checks that the IRQ counter doesn't goes below 0.

I think the problem is that atomic in general is not GPU reset compatible. GPU resets sort of happen outside of the standard atomic flow. So the DRM level interrupts will keep their original state, but internally, the driver has to deal with the fact that we need to re-enable interrupts as part of the reset sequence.

Alex

Quoted 189 lines
Quoted 71 lines

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().

---

  drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c            |  3 +++
  .../gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c | 14 ++++++++++----
  2 files changed, 13 insertions(+), 4 deletions(-)
(quoted diff) +0 -0

b/drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c

index a6aef488a822..1b66003657e2 100644

--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c

+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c

(quoted diff) +0 -0
@@ -597,6 +597,9 @@ int amdgpu_irq_put(struct amdgpu_device *adev, struct amdgpu_irq_src *src,
 
           if (!src->enabled_types || !src->funcs->set)

Review int amdgpu_irq_put(struct amdgpu_device *adev, struct amdgpu_irq_src *src,

Quoted 10 lines
(quoted diff) +3 -0
 
                          return -EINVAL;
 
 
 
+         if (!amdgpu_irq_enabled(adev, src, type))
 
+                       return 0;
 
+

That is racy and won't work. The intention of amdgpu_irq_update() is to always update the irq state, no matter what the status is.

This is a change to amdgpu_irq_put() to prevent the refcount from being cut to less than 0. Does it break the intention of amdgpu_irq_update()?

Yes, exactly that. The reference count can *never* be below 0 or you have a bug in the caller.

What you could do is to add a WARN_ON(!amdgpu_irq_enabled(adev, src, type)), but just ignoring the call is an absolute no-go.

Regards,
Christian.

PS: Please don't use HTML formating when discussing on public mailing lists.

Regards,

Christian.

Quoted 77 lines
           if (atomic_dec_and_test(&src->enabled_types[type]))

                          return amdgpu_irq_update(adev, src, type);
(quoted diff) +0 -0

b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c

index dc4f37240beb..e04f846b0b19 100644

--- a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c

+++ b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c

(quoted diff) +0 -0
@@ -146,7 +146,7 @@ static void vblank_control_worker(struct
 
 work_struct *work)
 
 
 
  static inline int dm_set_vblank(struct drm_crtc *crtc, bool enable)
 
  {
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c +11 -4
-          enum dc_irq_source irq_source;
 
+         int irq_type;
 
           struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
 
           struct amdgpu_device *adev = drm_to_adev(crtc->dev);
 
           struct dm_crtc_state *acrtc_state = to_dm_crtc_state(crtc->state);
@@ -169,10 +169,16 @@ static inline int dm_set_vblank(struct drm_crtc *crtc, bool enable)
 
           if (rc)
 
                          return rc;
 
 
 
-          irq_source = IRQ_TYPE_VBLANK + acrtc->otg_inst;
 
+         irq_type = amdgpu_display_crtc_idx_to_irq_type(adev,
 
+acrtc->crtc_id);
 
+
 
+         if (enable)
 
+                       rc = amdgpu_irq_get(adev, &adev->crtc_irq, irq_type);
 
+
 
+         else
 
+                       rc = amdgpu_irq_put(adev, &adev->crtc_irq, irq_type);
 
 
 
-          if (!dc_interrupt_set(adev->dm.dc, irq_source, enable))
 
-                        return -EBUSY;
 
+         if (rc)
 
+                       return rc;
 
 
 
  skip:
 
           if (amdgpu_in_reset(adev))
#11 lore raw

Am 30.03.23 um 15:10 schrieb Alex Deucher:

Quoted 62 lines
Quoted 60 lines · Christian König

Am 30.03.23 um 11:15 schrieb Liu, HaoPing (Alan):

[AMD Official Use Only - General]

Hi Christian,

Thanks for the review. Please see inline.

Best Regards,

Alan

-----Original Message-----
From: Christian König <[email protected]>
Sent: Tuesday, March 28, 2023 7:16 PM
To: Liu, HaoPing (Alan) <[email protected]>; [email protected]
Cc: Lakha, Bhawanpreet <[email protected]>
Subject: Re: [PATCH] drm/amdgpu: Fix desktop freezed after gpu-reset

Am 27.03.23 um 17:20 schrieb Alan Liu:

Quoted 10 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.

This is the source of the problem and you should address this instead.

The change you suggested below would break in some use cases.

In struct drm_vblank_crtc we have a vblank irq refcount controlled by drm layer, and in struct amdgpu_irq_src we have enabled_types as refcount for each irq controlled by the dm.

I think the best solution will be to get rid of the refcount in drm and only maintain the dm one, and add a crtc function for the drm layer to get the refcount/state of vblank.

But this may be dangerous since it’s a change in drm layer. Do you have any comments?

You don't necessarily need to remove it completely, what you can do as well is properly chaining of them.

E.g. when the DRM counter goes from 0->1 or 1->0 it calls some function to enable/disable the hw irq. In this situation you call amdgpu_irq_get()/amdgpu_irq_put() as appropriate.

Quoted 7 lines
Quoted 2 lines

The the code in this patch already looks like it goes into the right direction regarding that. It just seems to be that you have some race issues when you need to add checks that the IRQ counter doesn't goes below 0.

I think the problem is that atomic in general is not GPU reset compatible. GPU resets sort of happen outside of the standard atomic flow. So the DRM level interrupts will keep their original state, but internally, the driver has to deal with the fact that we need to re-enable interrupts as part of the reset sequence.

If I'm not completely mistaken we already do update all the interrupt state after a GPU reset.

I think what could potentially happen here is that we try to update the state while *inside* the GPU reset.

This basically means we just need to grab the appropriate locks.

Christian.

Quoted 102 lines

Alex

Quoted 98 lines
Quoted 30 lines

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().

---
   drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c            |  3 +++
   .../gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c | 14 ++++++++++----
   2 files changed, 13 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c +0 -0
index a6aef488a822..1b66003657e2 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c int amdgpu_irq_put(struct amdgpu_device *adev, struct amdgpu_irq_src *src, · line 601

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c +3 -0
@@ -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 (!amdgpu_irq_enabled(adev, src, type))
+                       return 0;
+

That is racy and won't work. The intention of amdgpu_irq_update() is to always update the irq state, no matter what the status is.

This is a change to amdgpu_irq_put() to prevent the refcount from being cut to less than 0. Does it break the intention of amdgpu_irq_update()?

Yes, exactly that. The reference count can *never* be below 0 or you have a bug in the caller.

What you could do is to add a WARN_ON(!amdgpu_irq_enabled(adev, src, type)), but just ignoring the call is an absolute no-go.

Regards,
Christian.

PS: Please don't use HTML formating when discussing on public mailing lists.

Regards,

Christian.

Quoted 34 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c +0 -0
            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 +11 -4
index dc4f37240beb..e04f846b0b19 100644
@@ -146,7 +146,7 @@ static void vblank_control_worker(struct
 work_struct *work)
   static inline int dm_set_vblank(struct drm_crtc *crtc, bool enable)
   {
-          enum dc_irq_source irq_source;
+         int irq_type;
            struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
            struct amdgpu_device *adev = drm_to_adev(crtc->dev);
            struct dm_crtc_state *acrtc_state = to_dm_crtc_state(crtc->state);
@@ -169,10 +169,16 @@ static inline int dm_set_vblank(struct drm_crtc *crtc, bool enable)
            if (rc)
                           return rc;
-          irq_source = IRQ_TYPE_VBLANK + acrtc->otg_inst;
+         irq_type = amdgpu_display_crtc_idx_to_irq_type(adev,
+acrtc->crtc_id);
+
+         if (enable)
+                       rc = amdgpu_irq_get(adev, &adev->crtc_irq, irq_type);
+
+         else
+                       rc = amdgpu_irq_put(adev, &adev->crtc_irq, irq_type);
-          if (!dc_interrupt_set(adev->dm.dc, irq_source, enable))
-                        return -EBUSY;
+         if (rc)
+                       return rc;
   skip:
            if (amdgpu_in_reset(adev))