Skip to contentSkip to conversation

drm/amdgpu: race issue when jobs on 2 ring timeout

PATCH Andrey Grodzovsky 3 participants 7 messages Jan 19, 2021 2 messages not in the archive

lore
Share & tools
lore
#1 lore raw
In reply to a message not in the archive
Quoted 38 lines · Horace Chen

Fix a racing issue when jobs on 2 rings timeout simultaneously.

If 2 rings timed out at the same time, the amdgpu_device_gpu_recover will be reentered. Then the adev->gmc.xgmi.head will be grabbed by 2 local linked list, which may cause wild pointer issue in iterating.

lock the device earily to prevent the node be added to 2 different lists.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 42 +++++++++++++++-------
  1 file changed, 30 insertions(+), 12 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +5 -0
index 4d434803fb49..9574da3abc32 100644
@@ -4540,6 +4540,7 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
  	int i, r = 0;
  	bool need_emergency_restart = false;
  	bool audio_suspended = false;
+	bool get_dev_lock = false;
 
  	/*
  	 * Special case: RAS triggered and full reset isn't supported
@@ -4582,28 +4583,45 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
  	 * Build list of devices to reset.
  	 * In case we are in XGMI hive mode, resort the device list
  	 * to put adev in the 1st position.
+	 *
+	 * lock the device before we try to operate the linked list
+	 * if didn't get the device lock, don't touch the linked list since
+	 * others may iterating it.
  	 */
  	INIT_LIST_HEAD(&device_list);
  	if (adev->gmc.xgmi.num_physical_nodes > 1) {

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c int amdgpu_device_gpu_recover(struct amdgpu_device *adev, · line 4599

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +5 -3
  		if (!hive)
  			return -ENODEV;
-		if (!list_is_first(&adev->gmc.xgmi.head, &hive->device_list))
-			list_rotate_to_front(&adev->gmc.xgmi.head, &hive->device_list);
-		device_list_handle = &hive->device_list;
+
+		list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) {
+			get_dev_lock = amdgpu_device_lock_adev(tmp_adev, hive);
+			if (!get_dev_lock)
+				break;

What about unlocking back all the devices you already locked if the break
happens in the middle of the iteration ?
Note that at skip_recovery: we don't do it. BTW, i see this issue is already in the current code.

Also, maybe now it's better to separate the actual locking in amdgpu_device_lock_adev from the other stuff going on there since I don't think you would wont to toggle stuff like adev->mp1_state back and forth and also the function name is not descriptive of the other stuff going on there anyway.

Andrey

Quoted 27 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +20 -2
+		}
+		if (get_dev_lock) {
+			if (!list_is_first(&adev->gmc.xgmi.head, &hive->device_list))
+				list_rotate_to_front(&adev->gmc.xgmi.head, &hive->device_list);
+			device_list_handle = &hive->device_list;
+		}
  	} else {
-		list_add_tail(&adev->gmc.xgmi.head, &device_list);
-		device_list_handle = &device_list;
+		get_dev_lock = amdgpu_device_lock_adev(adev, hive);
+		tmp_adev = adev;
+		if (get_dev_lock) {
+			list_add_tail(&adev->gmc.xgmi.head, &device_list);
+			device_list_handle = &device_list;
+		}
+	}
+
+	if (!get_dev_lock) {
+		dev_info(tmp_adev->dev, "Bailing on TDR for s_job:%llx, as another already in progress",
+					job ? job->base.id : -1);
+		r = 0;
+		/* even we skipped this reset, still need to set the job to guilty */
+		goto skip_recovery;
  	}
 
  	/* block all schedulers and reset given job's ring */
  	list_for_each_entry(tmp_adev, device_list_handle, gmc.xgmi.head) {

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c int amdgpu_device_gpu_recover(struct amdgpu_device *adev, · line 4627

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +0 -7
-		if (!amdgpu_device_lock_adev(tmp_adev, hive)) {
-			dev_info(tmp_adev->dev, "Bailing on TDR for s_job:%llx, as another already in progress",
-				  job ? job->base.id : -1);
-			r = 0;
-			goto skip_recovery;
-		}
-
  		/*
  		 * Try to put the audio codec into suspend state
  		 * before gpu reset started.

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

author Reviewed-by
#8 lore raw

Re: [PATCH 2/2] drm/amdgpu: set job guilty if reset skipped

In reply to a message not in the archive

Andrey

Quoted 27 lines · Horace Chen

If 2 jobs on 2 different ring timed out the at a very short period, the reset for second job will be skipped because the reset is already in progress.

But it doesn't mean the second job is not guilty since it also timed out and can be a bad job. So before skipped out from the reset, we need to increase karma for this job too.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 4 ++++
  1 file changed, 4 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +2 -0
index 9574da3abc32..1d6ff9fe37de 100644
@@ -4574,6 +4574,8 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
  			DRM_INFO("Bailing on TDR for s_job:%llx, hive: %llx as another already in progress",
  				job ? job->base.id : -1, hive->hive_id);
  			amdgpu_put_xgmi_hive(hive);
+			if (job)
+				drm_sched_increase_karma(&job->base);
  			return 0;
  		}
  		mutex_lock(&hive->hive_lock);

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c int amdgpu_device_gpu_recover(struct amdgpu_device *adev, · line 4626

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +2 -0
@@ -4617,6 +4619,8 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
  					job ? job->base.id : -1);
  		r = 0;
  		/* even we skipped this reset, still need to set the job to guilty */
+		if (job)
+			drm_sched_increase_karma(&job->base);
  		goto skip_recovery;
  	}
 

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#2 lore raw
In reply to Andrey Grodzovsky

[AMD Official Use Only - Internal Distribution Only]

Hi Andrey,

I think the list in the XGMI hive won't be break in the middle if we lock the device before we change the list. Because if 2 devices in 1 hive went into the function, it will follow the same sequence to lock the devices. So one of them will definately break at the first device. I add iterate devices here is just to lock all device in the hive since we will change the device sequence in the hive soon after.

The reason to break the interation in the middle is that the list is changed during the iteration without taking any lock. It is quite bad since I'm fixing one of this issue. And for XGMI hive, there are 2 locks protecting the list, one is the device lock I changed here, the other one is in front of my change, there is a hive->lock to protect the hive.

Even the bad thing really happened, I think moving back through the list is also very dengerous since we don't know what the list finally be, Unless we stack the devices we have iterated through a mirrored list. That can be a big change.

I'm ok to seperate the locking in amdgpu_device_lock_adev here, I'll do some test and update the code later.

Thanks & Regards,
Horace.
________________________________
发件人: Grodzovsky, Andrey <[email protected]>
发送时间: 2021年1月19日 22:33
收件人: Chen, Horace <[email protected]>; [email protected] <[email protected]>
抄送: Quan, Evan <[email protected]>; Tuikov, Luben <[email protected]>; Koenig, Christian <[email protected]>; Deucher, Alexander <[email protected]>; Xiao, Jack <[email protected]>; Zhang, Hawking <[email protected]>; Liu, Monk <[email protected]>; Xu, Feifei <[email protected]>; Wang, Kevin(Yang) <[email protected]>; Xiaojie Yuan <[email protected]> 主题: Re: [PATCH 1/2] drm/amdgpu: race issue when jobs on 2 ring timeout

Quoted 38 lines · Horace Chen

Fix a racing issue when jobs on 2 rings timeout simultaneously.

If 2 rings timed out at the same time, the amdgpu_device_gpu_recover will be reentered. Then the adev->gmc.xgmi.head will be grabbed by 2 local linked list, which may cause wild pointer issue in iterating.

lock the device earily to prevent the node be added to 2 different lists.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 42 +++++++++++++++-------
  1 file changed, 30 insertions(+), 12 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +5 -0
index 4d434803fb49..9574da3abc32 100644
@@ -4540,6 +4540,7 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
       int i, r = 0;
       bool need_emergency_restart = false;
       bool audio_suspended = false;
+     bool get_dev_lock = false;
 
       /*
        * Special case: RAS triggered and full reset isn't supported
@@ -4582,28 +4583,45 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
        * Build list of devices to reset.
        * In case we are in XGMI hive mode, resort the device list
        * to put adev in the 1st position.
+      *
+      * lock the device before we try to operate the linked list
+      * if didn't get the device lock, don't touch the linked list since
+      * others may iterating it.
        */
       INIT_LIST_HEAD(&device_list);
       if (adev->gmc.xgmi.num_physical_nodes > 1) {

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c int amdgpu_device_gpu_recover(struct amdgpu_device *adev, · line 4599

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +5 -3
               if (!hive)
                       return -ENODEV;
-             if (!list_is_first(&adev->gmc.xgmi.head, &hive->device_list))
-                     list_rotate_to_front(&adev->gmc.xgmi.head, &hive->device_list);
-             device_list_handle = &hive->device_list;
+
+             list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) {
+                     get_dev_lock = amdgpu_device_lock_adev(tmp_adev, hive);
+                     if (!get_dev_lock)
+                             break;

What about unlocking back all the devices you already locked if the break happens in the middle of the iteration ?
Note that at skip_recovery: we don't do it. BTW, i see this issue is already in the current code.

Also, maybe now it's better to separate the actual locking in amdgpu_device_lock_adev
from the other stuff going on there since I don't think you would wont to toggle stuff
like adev->mp1_state back and forth and also the function name is not descriptive of
the other stuff going on there anyway.

Andrey

Quoted 27 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +20 -2
+             }
+             if (get_dev_lock) {
+                     if (!list_is_first(&adev->gmc.xgmi.head, &hive->device_list))
+                             list_rotate_to_front(&adev->gmc.xgmi.head, &hive->device_list);
+                     device_list_handle = &hive->device_list;
+             }
       } else {
-             list_add_tail(&adev->gmc.xgmi.head, &device_list);
-             device_list_handle = &device_list;
+             get_dev_lock = amdgpu_device_lock_adev(adev, hive);
+             tmp_adev = adev;
+             if (get_dev_lock) {
+                     list_add_tail(&adev->gmc.xgmi.head, &device_list);
+                     device_list_handle = &device_list;
+             }
+     }
+
+     if (!get_dev_lock) {
+             dev_info(tmp_adev->dev, "Bailing on TDR for s_job:%llx, as another already in progress",
+                                     job ? job->base.id : -1);
+             r = 0;
+             /* even we skipped this reset, still need to set the job to guilty */
+             goto skip_recovery;
       }
 
       /* block all schedulers and reset given job's ring */
       list_for_each_entry(tmp_adev, device_list_handle, gmc.xgmi.head) {

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c int amdgpu_device_gpu_recover(struct amdgpu_device *adev, · line 4627

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +0 -7
-             if (!amdgpu_device_lock_adev(tmp_adev, hive)) {
-                     dev_info(tmp_adev->dev, "Bailing on TDR for s_job:%llx, as another already in progress",
-                               job ? job->base.id : -1);
-                     r = 0;
-                     goto skip_recovery;
-             }
-
               /*
                * Try to put the audio codec into suspend state
                * before gpu reset started.

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#6 lore raw
In reply to Andrey Grodzovsky

[AMD Public Use]

What about changing the lock hive logic like

                If (this device locked) return;

Lock hive -> lock this device.

In the regular flow, lock every thing in the list except this device. Thanks,
Lijo

From: amd-gfx <[email protected]> On Behalf Of Andrey Grodzovsky
Sent: Tuesday, January 19, 2021 10:45 PM
To: Chen, Horace <[email protected]>; [email protected]
Cc: Xiao, Jack <[email protected]>; Xu, Feifei <[email protected]>; Wang, Kevin(Yang) <[email protected]>; Tuikov, Luben <[email protected]>; Deucher, Alexander <[email protected]>; Quan, Evan <[email protected]>; Koenig, Christian <[email protected]>; Liu, Monk <[email protected]>; Zhang, Hawking <[email protected]>
Subject: Re: 回复: 回复: [PATCH 1/2] drm/amdgpu: race issue when jobs on 2 ring timeout

Well, it shouldn't happen with the hive locked as I am browsing the code but then your code should reflect that and if you do fail to lock particular adev AFTER the hive is locked you should not silently break iteration but throw an error, WARN_ON or BUG_ON then. Or alternatively bail out with unlocking all already locked devices.

Andrey

On 1/19/21 12:09 PM, Chen, Horace wrote:

[AMD Official Use Only - Internal Distribution Only]

OK, I understand. You mean one device in the hive may be locked up independently without locking up the whole hive.

It could happen, I'll change my code.

Thanks & Regards,
Horace.

________________________________
发件人: Grodzovsky, Andrey <[email protected]><mailto:[email protected]>
发送时间: 2021年1月20日 0:58
收件人: Chen, Horace <[email protected]><mailto:[email protected]>; [email protected]<mailto:[email protected]> <[email protected]><mailto:[email protected]>
抄送: Quan, Evan <[email protected]><mailto:[email protected]>; Tuikov, Luben <[email protected]><mailto:[email protected]>; Koenig, Christian <[email protected]><mailto:[email protected]>; Deucher, Alexander <[email protected]><mailto:[email protected]>; Xiao, Jack <[email protected]><mailto:[email protected]>; Zhang, Hawking <[email protected]><mailto:[email protected]>; Liu, Monk <[email protected]><mailto:[email protected]>; Xu, Feifei <[email protected]><mailto:[email protected]>; Wang, Kevin(Yang) <[email protected]><mailto:[email protected]>; Xiaojie Yuan <[email protected]><mailto:[email protected]> 主题: Re: 回复: [PATCH 1/2] drm/amdgpu: race issue when jobs on 2 ring timeout

On 1/19/21 11:39 AM, Chen, Horace wrote:

[AMD Official Use Only - Internal Distribution Only]

Hi Andrey,

I think the list in the XGMI hive won't be break in the middle if we lock the device before we change the list. Because if 2 devices in 1 hive went into the function, it will follow the same sequence to lock the devices. So one of them will definately break at the first device. I add iterate devices here is just to lock all device in the hive since we will change the device sequence in the hive soon after.

I didn't mean break in a sense of breaking the list itself, I just meant the literal 'break' instruction to terminate the iteration once you failed to lock a particular device.

The reason to break the interation in the middle is that the list is changed during the iteration without taking any lock. It is quite bad since I'm fixing one of this issue. And for XGMI hive, there are 2 locks protecting the list, one is the device lock I changed here, the other one is in front of my change, there is a hive->lock to protect the hive.

Even the bad thing really happened, I think moving back through the list is also very dengerous since we don't know what the list finally be, Unless we stack the devices we have iterated through a mirrored list. That can be a big change.

Not sure we are on the same page, my concern is let's sat your XGMI hive consists of 2 devices, you manged to call successfully do amdgpu_device_lock_adev for dev1 but then failed for dev2, in this case you will bail out without releasing dev1, no ?

Andrey

I'm ok to seperate the locking in amdgpu_device_lock_adev here, I'll do some test and update the code later.

Thanks & Regards,
Horace.
________________________________
发件人: Grodzovsky, Andrey <[email protected]><mailto:[email protected]>
发送时间: 2021年1月19日 22:33
收件人: Chen, Horace <[email protected]><mailto:[email protected]>; [email protected]<mailto:[email protected]> <[email protected]><mailto:[email protected]>
抄送: Quan, Evan <[email protected]><mailto:[email protected]>; Tuikov, Luben <[email protected]><mailto:[email protected]>; Koenig, Christian <[email protected]><mailto:[email protected]>; Deucher, Alexander <[email protected]><mailto:[email protected]>; Xiao, Jack <[email protected]><mailto:[email protected]>; Zhang, Hawking <[email protected]><mailto:[email protected]>; Liu, Monk <[email protected]><mailto:[email protected]>; Xu, Feifei <[email protected]><mailto:[email protected]>; Wang, Kevin(Yang) <[email protected]><mailto:[email protected]>; Xiaojie Yuan <[email protected]><mailto:[email protected]> 主题: Re: [PATCH 1/2] drm/amdgpu: race issue when jobs on 2 ring timeout

Quoted 38 lines · Horace Chen

Fix a racing issue when jobs on 2 rings timeout simultaneously.

If 2 rings timed out at the same time, the amdgpu_device_gpu_recover will be reentered. Then the adev->gmc.xgmi.head will be grabbed by 2 local linked list, which may cause wild pointer issue in iterating.

lock the device earily to prevent the node be added to 2 different lists.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 42 +++++++++++++++-------
  1 file changed, 30 insertions(+), 12 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +5 -0
index 4d434803fb49..9574da3abc32 100644
@@ -4540,6 +4540,7 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
       int i, r = 0;
       bool need_emergency_restart = false;
       bool audio_suspended = false;
+     bool get_dev_lock = false;
 
       /*
        * Special case: RAS triggered and full reset isn't supported
@@ -4582,28 +4583,45 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
        * Build list of devices to reset.
        * In case we are in XGMI hive mode, resort the device list
        * to put adev in the 1st position.
+      *
+      * lock the device before we try to operate the linked list
+      * if didn't get the device lock, don't touch the linked list since
+      * others may iterating it.
        */
       INIT_LIST_HEAD(&device_list);
       if (adev->gmc.xgmi.num_physical_nodes > 1) {

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c int amdgpu_device_gpu_recover(struct amdgpu_device *adev, · line 4599

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +5 -3
               if (!hive)
                       return -ENODEV;
-             if (!list_is_first(&adev->gmc.xgmi.head, &hive->device_list))
-                     list_rotate_to_front(&adev->gmc.xgmi.head, &hive->device_list);
-             device_list_handle = &hive->device_list;
+
+             list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) {
+                     get_dev_lock = amdgpu_device_lock_adev(tmp_adev, hive);
+                     if (!get_dev_lock)
+                             break;

What about unlocking back all the devices you already locked if the break happens in the middle of the iteration ?
Note that at skip_recovery: we don't do it. BTW, i see this issue is already in the current code.

Also, maybe now it's better to separate the actual locking in amdgpu_device_lock_adev
from the other stuff going on there since I don't think you would wont to toggle stuff
like adev->mp1_state back and forth and also the function name is not descriptive of
the other stuff going on there anyway.

Andrey

Quoted 27 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +20 -2
+             }
+             if (get_dev_lock) {
+                     if (!list_is_first(&adev->gmc.xgmi.head, &hive->device_list))
+                             list_rotate_to_front(&adev->gmc.xgmi.head, &hive->device_list);
+                     device_list_handle = &hive->device_list;
+             }
       } else {
-             list_add_tail(&adev->gmc.xgmi.head, &device_list);
-             device_list_handle = &device_list;
+             get_dev_lock = amdgpu_device_lock_adev(adev, hive);
+             tmp_adev = adev;
+             if (get_dev_lock) {
+                     list_add_tail(&adev->gmc.xgmi.head, &device_list);
+                     device_list_handle = &device_list;
+             }
+     }
+
+     if (!get_dev_lock) {
+             dev_info(tmp_adev->dev, "Bailing on TDR for s_job:%llx, as another already in progress",
+                                     job ? job->base.id : -1);
+             r = 0;
+             /* even we skipped this reset, still need to set the job to guilty */
+             goto skip_recovery;
       }
 
       /* block all schedulers and reset given job's ring */
       list_for_each_entry(tmp_adev, device_list_handle, gmc.xgmi.head) {

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c int amdgpu_device_gpu_recover(struct amdgpu_device *adev, · line 4627

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +0 -7
-             if (!amdgpu_device_lock_adev(tmp_adev, hive)) {
-                     dev_info(tmp_adev->dev, "Bailing on TDR for s_job:%llx, as another already in progress",
-                               job ? job->base.id : -1);
-                     r = 0;
-                     goto skip_recovery;
-             }
-
               /*
                * Try to put the audio codec into suspend state
                * before gpu reset started.

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#4 lore raw
In reply to Andrey Grodzovsky

[AMD Official Use Only - Internal Distribution Only]

OK, I understand. You mean one device in the hive may be locked up independently without locking up the whole hive.

It could happen, I'll change my code.

Thanks & Regards,
Horace.

________________________________
发件人: Grodzovsky, Andrey <[email protected]>
发送时间: 2021年1月20日 0:58
收件人: Chen, Horace <[email protected]>; [email protected] <[email protected]>
抄送: Quan, Evan <[email protected]>; Tuikov, Luben <[email protected]>; Koenig, Christian <[email protected]>; Deucher, Alexander <[email protected]>; Xiao, Jack <[email protected]>; Zhang, Hawking <[email protected]>; Liu, Monk <[email protected]>; Xu, Feifei <[email protected]>; Wang, Kevin(Yang) <[email protected]>; Xiaojie Yuan <[email protected]> 主题: Re: 回复: [PATCH 1/2] drm/amdgpu: race issue when jobs on 2 ring timeout

On 1/19/21 11:39 AM, Chen, Horace wrote:

[AMD Official Use Only - Internal Distribution Only]

Hi Andrey,

I think the list in the XGMI hive won't be break in the middle if we lock the device before we change the list. Because if 2 devices in 1 hive went into the function, it will follow the same sequence to lock the devices. So one of them will definately break at the first device. I add iterate devices here is just to lock all device in the hive since we will change the device sequence in the hive soon after.

I didn't mean break in a sense of breaking the list itself, I just meant the literal 'break' instruction to terminate the iteration once you failed to lock a particular device.

The reason to break the interation in the middle is that the list is changed during the iteration without taking any lock. It is quite bad since I'm fixing one of this issue. And for XGMI hive, there are 2 locks protecting the list, one is the device lock I changed here, the other one is in front of my change, there is a hive->lock to protect the hive.

Even the bad thing really happened, I think moving back through the list is also very dengerous since we don't know what the list finally be, Unless we stack the devices we have iterated through a mirrored list. That can be a big change.

Not sure we are on the same page, my concern is let's sat your XGMI hive consists of 2 devices, you manged to call successfully do amdgpu_device_lock_adev for dev1 but then failed for dev2, in this case you will bail out without releasing dev1, no ?

Andrey

I'm ok to seperate the locking in amdgpu_device_lock_adev here, I'll do some test and update the code later.

Thanks & Regards,
Horace.
________________________________
发件人: Grodzovsky, Andrey <[email protected]><mailto:[email protected]>
发送时间: 2021年1月19日 22:33
收件人: Chen, Horace <[email protected]><mailto:[email protected]>; [email protected]<mailto:[email protected]> <[email protected]><mailto:[email protected]>
抄送: Quan, Evan <[email protected]><mailto:[email protected]>; Tuikov, Luben <[email protected]><mailto:[email protected]>; Koenig, Christian <[email protected]><mailto:[email protected]>; Deucher, Alexander <[email protected]><mailto:[email protected]>; Xiao, Jack <[email protected]><mailto:[email protected]>; Zhang, Hawking <[email protected]><mailto:[email protected]>; Liu, Monk <[email protected]><mailto:[email protected]>; Xu, Feifei <[email protected]><mailto:[email protected]>; Wang, Kevin(Yang) <[email protected]><mailto:[email protected]>; Xiaojie Yuan <[email protected]><mailto:[email protected]> 主题: Re: [PATCH 1/2] drm/amdgpu: race issue when jobs on 2 ring timeout

Quoted 38 lines · Horace Chen

Fix a racing issue when jobs on 2 rings timeout simultaneously.

If 2 rings timed out at the same time, the amdgpu_device_gpu_recover will be reentered. Then the adev->gmc.xgmi.head will be grabbed by 2 local linked list, which may cause wild pointer issue in iterating.

lock the device earily to prevent the node be added to 2 different lists.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 42 +++++++++++++++-------
  1 file changed, 30 insertions(+), 12 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +5 -0
index 4d434803fb49..9574da3abc32 100644
@@ -4540,6 +4540,7 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
       int i, r = 0;
       bool need_emergency_restart = false;
       bool audio_suspended = false;
+     bool get_dev_lock = false;
 
       /*
        * Special case: RAS triggered and full reset isn't supported
@@ -4582,28 +4583,45 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
        * Build list of devices to reset.
        * In case we are in XGMI hive mode, resort the device list
        * to put adev in the 1st position.
+      *
+      * lock the device before we try to operate the linked list
+      * if didn't get the device lock, don't touch the linked list since
+      * others may iterating it.
        */
       INIT_LIST_HEAD(&device_list);
       if (adev->gmc.xgmi.num_physical_nodes > 1) {

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c int amdgpu_device_gpu_recover(struct amdgpu_device *adev, · line 4599

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +5 -3
               if (!hive)
                       return -ENODEV;
-             if (!list_is_first(&adev->gmc.xgmi.head, &hive->device_list))
-                     list_rotate_to_front(&adev->gmc.xgmi.head, &hive->device_list);
-             device_list_handle = &hive->device_list;
+
+             list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) {
+                     get_dev_lock = amdgpu_device_lock_adev(tmp_adev, hive);
+                     if (!get_dev_lock)
+                             break;

What about unlocking back all the devices you already locked if the break happens in the middle of the iteration ?
Note that at skip_recovery: we don't do it. BTW, i see this issue is already in the current code.

Also, maybe now it's better to separate the actual locking in amdgpu_device_lock_adev
from the other stuff going on there since I don't think you would wont to toggle stuff
like adev->mp1_state back and forth and also the function name is not descriptive of
the other stuff going on there anyway.

Andrey

Quoted 27 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +20 -2
+             }
+             if (get_dev_lock) {
+                     if (!list_is_first(&adev->gmc.xgmi.head, &hive->device_list))
+                             list_rotate_to_front(&adev->gmc.xgmi.head, &hive->device_list);
+                     device_list_handle = &hive->device_list;
+             }
       } else {
-             list_add_tail(&adev->gmc.xgmi.head, &device_list);
-             device_list_handle = &device_list;
+             get_dev_lock = amdgpu_device_lock_adev(adev, hive);
+             tmp_adev = adev;
+             if (get_dev_lock) {
+                     list_add_tail(&adev->gmc.xgmi.head, &device_list);
+                     device_list_handle = &device_list;
+             }
+     }
+
+     if (!get_dev_lock) {
+             dev_info(tmp_adev->dev, "Bailing on TDR for s_job:%llx, as another already in progress",
+                                     job ? job->base.id : -1);
+             r = 0;
+             /* even we skipped this reset, still need to set the job to guilty */
+             goto skip_recovery;
       }
 
       /* block all schedulers and reset given job's ring */
       list_for_each_entry(tmp_adev, device_list_handle, gmc.xgmi.head) {

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c int amdgpu_device_gpu_recover(struct amdgpu_device *adev, · line 4627

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +0 -7
-             if (!amdgpu_device_lock_adev(tmp_adev, hive)) {
-                     dev_info(tmp_adev->dev, "Bailing on TDR for s_job:%llx, as another already in progress",
-                               job ? job->base.id : -1);
-                     r = 0;
-                     goto skip_recovery;
-             }
-
               /*
                * Try to put the audio codec into suspend state
                * before gpu reset started.

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#5 lore raw

Well, it shouldn't happen with the hive locked as I am browsing the code but then your code should reflect that and if you do fail to lock particular adev AFTER the hive is locked you should not silently break iteration but throw an error, WARN_ON or BUG_ON then. Or alternatively bail out with unlocking all already locked devices.

Andrey

Quoted 36 lines · Horace

[AMD Official Use Only - Internal Distribution Only]

OK, I understand. You mean one device in the hive may be locked up independently without locking up the whole hive.

It could happen, I'll change my code.

Thanks & Regards,
Horace.

--------------------------------------------------------------------------------
*发件人:* Grodzovsky, Andrey <[email protected]>
*发送时间:* 2021年1月20日 0:58
*收件人:* Chen, Horace <[email protected]>; [email protected] <[email protected]>
*抄送:* Quan, Evan <[email protected]>; Tuikov, Luben <[email protected]>; Koenig, Christian <[email protected]>; Deucher, Alexander <[email protected]>; Xiao, Jack <[email protected]>; Zhang, Hawking <[email protected]>; Liu, Monk <[email protected]>; Xu, Feifei <[email protected]>; Wang, Kevin(Yang) <[email protected]>; Xiaojie Yuan <[email protected]> *主题:* Re: 回复: [PATCH 1/2] drm/amdgpu: race issue when jobs on 2 ring timeout

Quoted 7 lines · Horace

[AMD Official Use Only - Internal Distribution Only]

Hi Andrey,

I think the list in the XGMI hive won't be break in the middle if we lock the device before we change the list. Because if 2 devices in 1 hive went into the function, it will follow the same sequence to lock the devices. So one of them will definately break at the first device. I add iterate devices here is just to lock all device in the hive since we will change the device sequence in the hive soon after.

I didn't mean break in a sense of breaking the list itself, I just meant the literal 'break' instruction to terminate the iteration once you failed to lock a particular device.

Quoted 3 lines

The reason to break the interation in the middle is that the list is changed during the iteration without taking any lock. It is quite bad since I'm fixing one of this issue. And for XGMI hive, there are 2 locks protecting the list, one is the device lock I changed here, the other one is in front of my change, there is a hive->lock to protect the hive.

Quoted 133 lines
Quoted 1 line

Even the bad thing really happened, I think moving back through the list is also very dengerous since we don't know what the list finally be, Unless we stack the devices we have iterated through a mirrored list. That can be a big change.

Not sure we are on the same page, my concern is let's sat your XGMI hive consists of 2 devices, you manged to call successfully do amdgpu_device_lock_adev for dev1 but then failed for dev2, in this case you will bail out without releasing dev1, no ?

Andrey

Quoted 123 lines

I'm ok to seperate the locking in amdgpu_device_lock_adev here, I'll do some test and update the code later.

Thanks & Regards,
Horace.
--------------------------------------------------------------------------------
*发件人:* Grodzovsky, Andrey <[email protected]> <mailto:[email protected]>
*发送时间:* 2021年1月19日 22:33
*收件人:* Chen, Horace <[email protected]> <mailto:[email protected]>; [email protected] <mailto:[email protected]> <[email protected]> <mailto:[email protected]>
*抄送:* Quan, Evan <[email protected]> <mailto:[email protected]>; Tuikov, Luben <[email protected]> <mailto:[email protected]>; Koenig, Christian <[email protected]> <mailto:[email protected]>; Deucher, Alexander <[email protected]> <mailto:[email protected]>; Xiao, Jack <[email protected]> <mailto:[email protected]>; Zhang, Hawking <[email protected]> <mailto:[email protected]>; Liu, Monk <[email protected]> <mailto:[email protected]>; Xu, Feifei <[email protected]> <mailto:[email protected]>; Wang, Kevin(Yang) <[email protected]> <mailto:[email protected]>; Xiaojie Yuan <[email protected]> <mailto:[email protected]> *主题:* Re: [PATCH 1/2] drm/amdgpu: race issue when jobs on 2 ring timeout

Quoted 10 lines · Horace Chen

Fix a racing issue when jobs on 2 rings timeout simultaneously.

If 2 rings timed out at the same time, the amdgpu_device_gpu_recover will be reentered. Then the adev->gmc.xgmi.head will be grabbed by 2 local linked list, which may cause wild pointer issue in iterating.

lock the device earily to prevent the node be added to 2 different lists.

Review

Quoted 6 lines

---
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 42 +++++++++++++++-------

   1 file changed, 30 insertions(+), 12 deletions(-)
(quoted diff) +0 -0

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

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c int amdgpu_device_gpu_recover(struct amdgpu_device

Quoted 12 lines

index 4d434803fb49..9574da3abc32 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +1 -0
@@ -4540,6 +4540,7 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
         int i, r = 0;
         bool need_emergency_restart = false;
         bool audio_suspended = false;
+     bool get_dev_lock = false;
 
         /*
          * Special case: RAS triggered and full reset isn't supported
@@ -4582,28 +4583,45 @@ int amdgpu_device_gpu_recover(struct amdgpu_device 

*adev,

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +4 -0
          * Build list of devices to reset.
          * In case we are in XGMI hive mode, resort the device list
          * to put adev in the 1st position.
+      *
+      * lock the device before we try to operate the linked list
+      * if didn't get the device lock, don't touch the linked list since
+      * others may iterating it.
          */
         INIT_LIST_HEAD(&device_list);
         if (adev->gmc.xgmi.num_physical_nodes > 1) {

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c int amdgpu_device_gpu_recover(struct amdgpu_device · line 4599

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +5 -3
                 if (!hive)
                         return -ENODEV;
-             if (!list_is_first(&adev->gmc.xgmi.head, &hive->device_list))
- list_rotate_to_front(&adev->gmc.xgmi.head, &hive->device_list);
-             device_list_handle = &hive->device_list;
+
+ list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) {
+                     get_dev_lock = amdgpu_device_lock_adev(tmp_adev, hive);
+                     if (!get_dev_lock)
+                             break;

What about unlocking back all the devices you already locked if the break happens in the middle of the iteration ?
Note that at skip_recovery: we don't do it. BTW, i see this issue is already in the current code.

Also, maybe now it's better to separate the actual locking in amdgpu_device_lock_adev
from the other stuff going on there since I don't think you would wont to toggle stuff
like adev->mp1_state back and forth and also the function name is not descriptive of
the other stuff going on there anyway.

Andrey

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c int amdgpu_device_gpu_recover(struct amdgpu_device · line 4602

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +3 -0
+             }
+             if (get_dev_lock) {
+                     if (!list_is_first(&adev->gmc.xgmi.head, 

&hive->device_list))

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +3 -2
+ list_rotate_to_front(&adev->gmc.xgmi.head, &hive->device_list);
+                     device_list_handle = &hive->device_list;
+             }
         } else {
- list_add_tail(&adev->gmc.xgmi.head, &device_list);
-             device_list_handle = &device_list;

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c int amdgpu_device_gpu_recover(struct amdgpu_device · line 4616

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +10 -0
+             get_dev_lock = amdgpu_device_lock_adev(adev, hive);
+             tmp_adev = adev;
+             if (get_dev_lock) {
+ list_add_tail(&adev->gmc.xgmi.head, &device_list);
+                     device_list_handle = &device_list;
+             }
+     }
+
+     if (!get_dev_lock) {
+             dev_info(tmp_adev->dev, "Bailing on TDR for s_job:%llx, as 

another already in progress",

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c int amdgpu_device_gpu_recover(struct amdgpu_device · line 4619

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +3 -0
+                                     job ? job->base.id : -1);
+             r = 0;
+             /* even we skipped this reset, still need to set the job to 

guilty */

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c int amdgpu_device_gpu_recover(struct amdgpu_device · line 4601

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +1 -2
+             goto skip_recovery;
         }
 
         /* block all schedulers and reset given job's ring */
         list_for_each_entry(tmp_adev, device_list_handle, gmc.xgmi.head) {
-             if (!amdgpu_device_lock_adev(tmp_adev, hive)) {
- dev_info(tmp_adev->dev, "Bailing on TDR for s_job:%llx, as another 

already in progress",

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +0 -5
-                               job ? job->base.id : -1);
-                     r = 0;
-                     goto skip_recovery;
-             }
-
                 /*
                  * Try to put the audio codec into suspend state
                  * before gpu reset started.

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#3 lore raw
In reply to Chen, Horace
Quoted 7 lines · Horace

[AMD Official Use Only - Internal Distribution Only]

Hi Andrey,

I think the list in the XGMI hive won't be break in the middle if we lock the device before we change the list. Because if 2 devices in 1 hive went into the function, it will follow the same sequence to lock the devices. So one of them will definately break at the first device. I add iterate devices here is just to lock all device in the hive since we will change the device sequence in the hive soon after.

I didn't mean break in a sense of breaking the list itself, I just meant the literal 'break' instruction to terminate the iteration once you failed to lock a particular device.

Quoted 4 lines

The reason to break the interation in the middle is that the list is changed during the iteration without taking any lock. It is quite bad since I'm fixing one of this issue. And for XGMI hive, there are 2 locks protecting the list, one is the device lock I changed here, the other one is in front of my change, there is a hive->lock to protect the hive.

Even the bad thing really happened, I think moving back through the list is also very dengerous since we don't know what the list finally be, Unless we stack the devices we have iterated through a mirrored list. That can be a big change.

Not sure we are on the same page, my concern is let's sat your XGMI hive consists of 2 devices, you manged to call successfully do amdgpu_device_lock_adev for dev1 but then failed for dev2, in this case you will bail out without releasing dev1, no ?

Andrey

Quoted 107 lines

I'm ok to seperate the locking in amdgpu_device_lock_adev here, I'll do some test and update the code later.

Thanks & Regards,
Horace.
--------------------------------------------------------------------------------
*发件人:* Grodzovsky, Andrey <[email protected]>
*发送时间:* 2021年1月19日 22:33
*收件人:* Chen, Horace <[email protected]>; [email protected] <[email protected]>
*抄送:* Quan, Evan <[email protected]>; Tuikov, Luben <[email protected]>; Koenig, Christian <[email protected]>; Deucher, Alexander <[email protected]>; Xiao, Jack <[email protected]>; Zhang, Hawking <[email protected]>; Liu, Monk <[email protected]>; Xu, Feifei <[email protected]>; Wang, Kevin(Yang) <[email protected]>; Xiaojie Yuan <[email protected]> *主题:* Re: [PATCH 1/2] drm/amdgpu: race issue when jobs on 2 ring timeout

Quoted 10 lines · Horace Chen

Fix a racing issue when jobs on 2 rings timeout simultaneously.

If 2 rings timed out at the same time, the amdgpu_device_gpu_recover will be reentered. Then the adev->gmc.xgmi.head will be grabbed by 2 local linked list, which may cause wild pointer issue in iterating.

lock the device earily to prevent the node be added to 2 different lists.

Review

Quoted 6 lines

---
   drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 42 +++++++++++++++-------

   1 file changed, 30 insertions(+), 12 deletions(-)
(quoted diff) +0 -0

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

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c int amdgpu_device_gpu_recover(struct amdgpu_device

Quoted 12 lines

index 4d434803fb49..9574da3abc32 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +1 -0
@@ -4540,6 +4540,7 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
         int i, r = 0;
         bool need_emergency_restart = false;
         bool audio_suspended = false;
+     bool get_dev_lock = false;
 
         /*
          * Special case: RAS triggered and full reset isn't supported
@@ -4582,28 +4583,45 @@ int amdgpu_device_gpu_recover(struct amdgpu_device 

*adev,

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +4 -0
          * Build list of devices to reset.
          * In case we are in XGMI hive mode, resort the device list
          * to put adev in the 1st position.
+      *
+      * lock the device before we try to operate the linked list
+      * if didn't get the device lock, don't touch the linked list since
+      * others may iterating it.
          */
         INIT_LIST_HEAD(&device_list);
         if (adev->gmc.xgmi.num_physical_nodes > 1) {

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c int amdgpu_device_gpu_recover(struct amdgpu_device · line 4599

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +5 -3
                 if (!hive)
                         return -ENODEV;
-             if (!list_is_first(&adev->gmc.xgmi.head, &hive->device_list))
- list_rotate_to_front(&adev->gmc.xgmi.head, &hive->device_list);
-             device_list_handle = &hive->device_list;
+
+             list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) {
+                     get_dev_lock = amdgpu_device_lock_adev(tmp_adev, hive);
+                     if (!get_dev_lock)
+                             break;

What about unlocking back all the devices you already locked if the break happens in the middle of the iteration ?
Note that at skip_recovery: we don't do it. BTW, i see this issue is already in the current code.

Also, maybe now it's better to separate the actual locking in amdgpu_device_lock_adev
from the other stuff going on there since I don't think you would wont to toggle stuff
like adev->mp1_state back and forth and also the function name is not descriptive of
the other stuff going on there anyway.

Andrey

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c int amdgpu_device_gpu_recover(struct amdgpu_device · line 4602

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +3 -0
+             }
+             if (get_dev_lock) {
+                     if (!list_is_first(&adev->gmc.xgmi.head, 

&hive->device_list))

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +3 -2
+ list_rotate_to_front(&adev->gmc.xgmi.head, &hive->device_list);
+                     device_list_handle = &hive->device_list;
+             }
         } else {
- list_add_tail(&adev->gmc.xgmi.head, &device_list);
-             device_list_handle = &device_list;

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c int amdgpu_device_gpu_recover(struct amdgpu_device · line 4616

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +10 -0
+             get_dev_lock = amdgpu_device_lock_adev(adev, hive);
+             tmp_adev = adev;
+             if (get_dev_lock) {
+ list_add_tail(&adev->gmc.xgmi.head, &device_list);
+                     device_list_handle = &device_list;
+             }
+     }
+
+     if (!get_dev_lock) {
+             dev_info(tmp_adev->dev, "Bailing on TDR for s_job:%llx, as 

another already in progress",

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c int amdgpu_device_gpu_recover(struct amdgpu_device · line 4619

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +3 -0
+                                     job ? job->base.id : -1);
+             r = 0;
+             /* even we skipped this reset, still need to set the job to 

guilty */

Quoted 16 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c int amdgpu_device_gpu_recover(struct amdgpu_device · line 4601

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +1 -2
+             goto skip_recovery;
         }
 
         /* block all schedulers and reset given job's ring */
         list_for_each_entry(tmp_adev, device_list_handle, gmc.xgmi.head) {
-             if (!amdgpu_device_lock_adev(tmp_adev, hive)) {
-                     dev_info(tmp_adev->dev, "Bailing on TDR for 

s_job:%llx, as another already in progress",

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +0 -5
-                               job ? job->base.id : -1);
-                     r = 0;
-                     goto skip_recovery;
-             }
-
                 /*
                  * Try to put the audio codec into suspend state
                  * before gpu reset started.

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx