Skip to contentSkip to conversation

drm/amdgpu: Release reset locks during failures

PATCH In review Ce Sun 3 participants 3 messages Jun 6, 2025 – Jun 16, 2025

Subsystems (2)
lore
Share & tools
lore

Ce Sun

author
#0 lore raw

PATCH 1/1 drm/amdgpu: Release reset locks during failures

From: Lijo Lazar <[email protected]>

Make sure to release reset domain lock in case of failures.

Fixes 0f936e23cf9d ("drm/amdgpu: refactor amdgpu_device_gpu_recover")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 80 +++++++++++++++-------
 1 file changed, 55 insertions(+), 25 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +55 -25
index 2b84df8da61a..85509cd4cab8 100644
@@ -6068,16 +6068,12 @@ static int amdgpu_device_health_check(struct list_head *device_list_handle)
 	return ret;
 }
 
-static int amdgpu_device_halt_activities(struct amdgpu_device *adev,
-			      struct amdgpu_job *job,
-			      struct amdgpu_reset_context *reset_context,
-			      struct list_head *device_list,
-			      struct amdgpu_hive_info *hive,
-			      bool need_emergency_restart)
+static int amdgpu_device_recovery_prepare(struct amdgpu_device *adev,
+					  struct list_head *device_list,
+					  struct amdgpu_hive_info *hive)
 {
-	struct list_head *device_list_handle =  NULL;
 	struct amdgpu_device *tmp_adev = NULL;
-	int i, r = 0;
+	int r;
 
 	/*
 	 * Build list of devices to reset.
@@ -6094,26 +6090,54 @@ static int amdgpu_device_halt_activities(struct amdgpu_device *adev,
 		}
 		if (!list_is_first(&adev->reset_list, device_list))
 			list_rotate_to_front(&adev->reset_list, device_list);
-		device_list_handle = device_list;
 	} else {
 		list_add_tail(&adev->reset_list, device_list);
-		device_list_handle = device_list;
 	}
 
 	if (!amdgpu_sriov_vf(adev) && (!adev->pcie_reset_ctx.occurs_dpc)) {
-		r = amdgpu_device_health_check(device_list_handle);
+		r = amdgpu_device_health_check(device_list);
 		if (r)
 			return r;
 	}
 
-	/* We need to lock reset domain only once both for XGMI and single device */
-	tmp_adev = list_first_entry(device_list_handle, struct amdgpu_device,
-				    reset_list);
+	return 0;
+}
+
+static void amdgpu_device_recovery_get_reset_lock(struct amdgpu_device *adev,
+						  struct list_head *device_list)
+{
+	struct amdgpu_device *tmp_adev = NULL;
+
+	if (list_empty(device_list))
+		return;
+	tmp_adev =
+		list_first_entry(device_list, struct amdgpu_device, reset_list);
 	amdgpu_device_lock_reset_domain(tmp_adev->reset_domain);
+}
 
-	/* block all schedulers and reset given job's ring */
-	list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
+static void amdgpu_device_recovery_put_reset_lock(struct amdgpu_device *adev,
+						  struct list_head *device_list)
+{
+	struct amdgpu_device *tmp_adev = NULL;
 
+	if (list_empty(device_list))
+		return;
+	tmp_adev =
+		list_first_entry(device_list, struct amdgpu_device, reset_list);
+	amdgpu_device_unlock_reset_domain(tmp_adev->reset_domain);
+}
+
+static int amdgpu_device_halt_activities(
+	struct amdgpu_device *adev, struct amdgpu_job *job,
+	struct amdgpu_reset_context *reset_context,
+	struct list_head *device_list, struct amdgpu_hive_info *hive,
+	bool need_emergency_restart)
+{
+	struct amdgpu_device *tmp_adev = NULL;
+	int i, r = 0;
+
+	/* block all schedulers and reset given job's ring */
+	list_for_each_entry(tmp_adev, device_list, reset_list) {
 		amdgpu_device_set_mp1_state(tmp_adev);
 
 		/*
@@ -6301,11 +6325,6 @@ static void amdgpu_device_gpu_resume(struct amdgpu_device *adev,
 		amdgpu_ras_set_error_query_ready(tmp_adev, true);
 
 	}
-
-	tmp_adev = list_first_entry(device_list, struct amdgpu_device,
-					    reset_list);
-	amdgpu_device_unlock_reset_domain(tmp_adev->reset_domain);
-
 }
 
 
@@ -6376,10 +6395,16 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
 	reset_context->hive = hive;
 	INIT_LIST_HEAD(&device_list);
 
+	if (amdgpu_device_recovery_prepare(adev, &device_list, hive))
+		goto end_reset;
+
+	/* We need to lock reset domain only once both for XGMI and single device */
+	amdgpu_device_recovery_get_reset_lock(adev, &device_list);
+
 	r = amdgpu_device_halt_activities(adev, job, reset_context, &device_list,
 					 hive, need_emergency_restart);
 	if (r)
-		goto end_reset;
+		goto reset_unlock;
 
 	if (need_emergency_restart)
 		goto skip_sched_resume;
@@ -6397,13 +6422,15 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
 
 	r = amdgpu_device_asic_reset(adev, &device_list, reset_context);
 	if (r)
-		goto end_reset;
+		goto reset_unlock;
 skip_hw_reset:
 	r = amdgpu_device_sched_resume(&device_list, reset_context, job_signaled);
 	if (r)
-		goto end_reset;
+		goto reset_unlock;
 skip_sched_resume:
 	amdgpu_device_gpu_resume(adev, &device_list, need_emergency_restart);
+reset_unlock:
+	amdgpu_device_recovery_put_reset_lock(adev, &device_list);
 end_reset:
 	if (hive) {
 		mutex_unlock(&hive->hive_lock);
@@ -6821,6 +6848,8 @@ pci_ers_result_t amdgpu_pci_error_detected(struct pci_dev *pdev, pci_channel_sta
 		memset(&reset_context, 0, sizeof(reset_context));
 		INIT_LIST_HEAD(&device_list);
 
+		amdgpu_device_recovery_prepare(adev, &device_list, hive);
+		amdgpu_device_recovery_get_reset_lock(adev, &device_list);
 		r = amdgpu_device_halt_activities(adev, NULL, &reset_context, &device_list,
 					 hive, false);
 		if (hive) {
@@ -6938,8 +6967,8 @@ pci_ers_result_t amdgpu_pci_slot_reset(struct pci_dev *pdev)
 		if (hive) {
 			list_for_each_entry(tmp_adev, &device_list, reset_list)
 				amdgpu_device_unset_mp1_state(tmp_adev);
-			amdgpu_device_unlock_reset_domain(adev->reset_domain);
 		}
+		amdgpu_device_recovery_put_reset_lock(adev, &device_list);
 	}
 
 	if (hive) {
@@ -6985,6 +7014,7 @@ void amdgpu_pci_resume(struct pci_dev *pdev)
 
 	amdgpu_device_sched_resume(&device_list, NULL, NULL);
 	amdgpu_device_gpu_resume(adev, &device_list, false);
+	amdgpu_device_recovery_put_reset_lock(adev, &device_list);
 	adev->pcie_reset_ctx.occurs_dpc = false;
 
 	if (hive) {
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw

[AMD Official Use Only - AMD Internal Distribution Only]

Regards,
Hawking
-----Original Message-----
From: Sun, Ce(Overlord) <[email protected]>
Sent: Friday, June 6, 2025 15:13
To: [email protected]
Cc: Zhou1, Tao <[email protected]>; Zhang, Hawking <[email protected]>; Chai, Thomas <[email protected]>; Lazar, Lijo <[email protected]>; Sun, Ce(Overlord) <[email protected]>
Subject: [PATCH] drm/amdgpu: Release reset locks during failures

From: Lijo Lazar <[email protected]>

Make sure to release reset domain lock in case of failures.

Fixes 0f936e23cf9d ("drm/amdgpu: refactor amdgpu_device_gpu_recover")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 80 +++++++++++++++-------
 1 file changed, 55 insertions(+), 25 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +55 -25
index 2b84df8da61a..85509cd4cab8 100644
@@ -6068,16 +6068,12 @@ static int amdgpu_device_health_check(struct list_head *device_list_handle)
        return ret;
 }
 
-static int amdgpu_device_halt_activities(struct amdgpu_device *adev,
-                             struct amdgpu_job *job,
-                             struct amdgpu_reset_context *reset_context,
-                             struct list_head *device_list,
-                             struct amdgpu_hive_info *hive,
-                             bool need_emergency_restart)
+static int amdgpu_device_recovery_prepare(struct amdgpu_device *adev,
+                                         struct list_head *device_list,
+                                         struct amdgpu_hive_info *hive)
 {
-       struct list_head *device_list_handle =  NULL;
        struct amdgpu_device *tmp_adev = NULL;
-       int i, r = 0;
+       int r;
 
        /*
         * Build list of devices to reset.
@@ -6094,26 +6090,54 @@ static int amdgpu_device_halt_activities(struct amdgpu_device *adev,
                }
                if (!list_is_first(&adev->reset_list, device_list))
                        list_rotate_to_front(&adev->reset_list, device_list);
-               device_list_handle = device_list;
        } else {
                list_add_tail(&adev->reset_list, device_list);
-               device_list_handle = device_list;
        }
 
        if (!amdgpu_sriov_vf(adev) && (!adev->pcie_reset_ctx.occurs_dpc)) {
-               r = amdgpu_device_health_check(device_list_handle);
+               r = amdgpu_device_health_check(device_list);
                if (r)
                        return r;
        }
 
-       /* We need to lock reset domain only once both for XGMI and single device */
-       tmp_adev = list_first_entry(device_list_handle, struct amdgpu_device,
-                                   reset_list);
+       return 0;
+}
+
+static void amdgpu_device_recovery_get_reset_lock(struct amdgpu_device *adev,
+                                                 struct list_head *device_list)
+{
+       struct amdgpu_device *tmp_adev = NULL;
+
+       if (list_empty(device_list))
+               return;
+       tmp_adev =
+               list_first_entry(device_list, struct amdgpu_device, reset_list);
        amdgpu_device_lock_reset_domain(tmp_adev->reset_domain);
+}
 
-       /* block all schedulers and reset given job's ring */
-       list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
+static void amdgpu_device_recovery_put_reset_lock(struct amdgpu_device *adev,
+                                                 struct list_head *device_list)
+{
+       struct amdgpu_device *tmp_adev = NULL;
 
+       if (list_empty(device_list))
+               return;
+       tmp_adev =
+               list_first_entry(device_list, struct amdgpu_device, reset_list);
+       amdgpu_device_unlock_reset_domain(tmp_adev->reset_domain);
+}
+
+static int amdgpu_device_halt_activities(
+       struct amdgpu_device *adev, struct amdgpu_job *job,
+       struct amdgpu_reset_context *reset_context,
+       struct list_head *device_list, struct amdgpu_hive_info *hive,
+       bool need_emergency_restart)
+{
+       struct amdgpu_device *tmp_adev = NULL;
+       int i, r = 0;
+
+       /* block all schedulers and reset given job's ring */
+       list_for_each_entry(tmp_adev, device_list, reset_list) {
                amdgpu_device_set_mp1_state(tmp_adev);
 
                /*
@@ -6301,11 +6325,6 @@ static void amdgpu_device_gpu_resume(struct amdgpu_device *adev,
                amdgpu_ras_set_error_query_ready(tmp_adev, true);
 
        }
-
-       tmp_adev = list_first_entry(device_list, struct amdgpu_device,
-                                           reset_list);
-       amdgpu_device_unlock_reset_domain(tmp_adev->reset_domain);
-
 }
 
 
@@ -6376,10 +6395,16 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
        reset_context->hive = hive;
        INIT_LIST_HEAD(&device_list);
 
+       if (amdgpu_device_recovery_prepare(adev, &device_list, hive))
+               goto end_reset;
+
+       /* We need to lock reset domain only once both for XGMI and single device */
+       amdgpu_device_recovery_get_reset_lock(adev, &device_list);
+
        r = amdgpu_device_halt_activities(adev, job, reset_context, &device_list,
                                         hive, need_emergency_restart);
        if (r)
-               goto end_reset;
+               goto reset_unlock;
 
        if (need_emergency_restart)
                goto skip_sched_resume;
@@ -6397,13 +6422,15 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
 
        r = amdgpu_device_asic_reset(adev, &device_list, reset_context);
        if (r)
-               goto end_reset;
+               goto reset_unlock;
 skip_hw_reset:
        r = amdgpu_device_sched_resume(&device_list, reset_context, job_signaled);
        if (r)
-               goto end_reset;
+               goto reset_unlock;
 skip_sched_resume:
        amdgpu_device_gpu_resume(adev, &device_list, need_emergency_restart);
+reset_unlock:
+       amdgpu_device_recovery_put_reset_lock(adev, &device_list);
 end_reset:
        if (hive) {
                mutex_unlock(&hive->hive_lock);
@@ -6821,6 +6848,8 @@ pci_ers_result_t amdgpu_pci_error_detected(struct pci_dev *pdev, pci_channel_sta
                memset(&reset_context, 0, sizeof(reset_context));
                INIT_LIST_HEAD(&device_list);
 
+               amdgpu_device_recovery_prepare(adev, &device_list, hive);
+               amdgpu_device_recovery_get_reset_lock(adev, &device_list);
                r = amdgpu_device_halt_activities(adev, NULL, &reset_context, &device_list,
                                         hive, false);
                if (hive) {
@@ -6938,8 +6967,8 @@ pci_ers_result_t amdgpu_pci_slot_reset(struct pci_dev *pdev)
                if (hive) {
                        list_for_each_entry(tmp_adev, &device_list, reset_list)
                                amdgpu_device_unset_mp1_state(tmp_adev);
-                       amdgpu_device_unlock_reset_domain(adev->reset_domain);
                }
+               amdgpu_device_recovery_put_reset_lock(adev, &device_list);
        }
 
        if (hive) {
@@ -6985,6 +7014,7 @@ void amdgpu_pci_resume(struct pci_dev *pdev)
 
        amdgpu_device_sched_resume(&device_list, NULL, NULL);
        amdgpu_device_gpu_resume(adev, &device_list, false);
+       amdgpu_device_recovery_put_reset_lock(adev, &device_list);
        adev->pcie_reset_ctx.occurs_dpc = false;
 
        if (hive) {

--
2.34.1

#2 lore raw
In reply to Ce Sun

[AMD Official Use Only - AMD Internal Distribution Only]

Ping...

Regards,
Ce, Sun

________________________________
From: Sun, Ce(Overlord) <[email protected]>
Sent: Friday, June 6, 2025 3:12 PM
To: [email protected] <[email protected]>
Cc: Zhou1, Tao <[email protected]>; Zhang, Hawking <[email protected]>; Chai, Thomas <[email protected]>; Lazar, Lijo <[email protected]>; Sun, Ce(Overlord) <[email protected]>
Subject: [PATCH] drm/amdgpu: Release reset locks during failures

From: Lijo Lazar <[email protected]>

Make sure to release reset domain lock in case of failures.

Fixes 0f936e23cf9d ("drm/amdgpu: refactor amdgpu_device_gpu_recover")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 80 +++++++++++++++-------
 1 file changed, 55 insertions(+), 25 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +55 -25
index 2b84df8da61a..85509cd4cab8 100644
@@ -6068,16 +6068,12 @@ static int amdgpu_device_health_check(struct list_head *device_list_handle)
         return ret;
 }
 
-static int amdgpu_device_halt_activities(struct amdgpu_device *adev,
-                             struct amdgpu_job *job,
-                             struct amdgpu_reset_context *reset_context,
-                             struct list_head *device_list,
-                             struct amdgpu_hive_info *hive,
-                             bool need_emergency_restart)
+static int amdgpu_device_recovery_prepare(struct amdgpu_device *adev,
+                                         struct list_head *device_list,
+                                         struct amdgpu_hive_info *hive)
 {
-       struct list_head *device_list_handle =  NULL;
         struct amdgpu_device *tmp_adev = NULL;
-       int i, r = 0;
+       int r;
 
         /*
          * Build list of devices to reset.
@@ -6094,26 +6090,54 @@ static int amdgpu_device_halt_activities(struct amdgpu_device *adev,
                 }
                 if (!list_is_first(&adev->reset_list, device_list))
                         list_rotate_to_front(&adev->reset_list, device_list);
-               device_list_handle = device_list;
         } else {
                 list_add_tail(&adev->reset_list, device_list);
-               device_list_handle = device_list;
         }
 
         if (!amdgpu_sriov_vf(adev) && (!adev->pcie_reset_ctx.occurs_dpc)) {
-               r = amdgpu_device_health_check(device_list_handle);
+               r = amdgpu_device_health_check(device_list);
                 if (r)
                         return r;
         }
 
-       /* We need to lock reset domain only once both for XGMI and single device */
-       tmp_adev = list_first_entry(device_list_handle, struct amdgpu_device,
-                                   reset_list);
+       return 0;
+}
+
+static void amdgpu_device_recovery_get_reset_lock(struct amdgpu_device *adev,
+                                                 struct list_head *device_list)
+{
+       struct amdgpu_device *tmp_adev = NULL;
+
+       if (list_empty(device_list))
+               return;
+       tmp_adev =
+               list_first_entry(device_list, struct amdgpu_device, reset_list);
         amdgpu_device_lock_reset_domain(tmp_adev->reset_domain);
+}
 
-       /* block all schedulers and reset given job's ring */
-       list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
+static void amdgpu_device_recovery_put_reset_lock(struct amdgpu_device *adev,
+                                                 struct list_head *device_list)
+{
+       struct amdgpu_device *tmp_adev = NULL;
 
+       if (list_empty(device_list))
+               return;
+       tmp_adev =
+               list_first_entry(device_list, struct amdgpu_device, reset_list);
+       amdgpu_device_unlock_reset_domain(tmp_adev->reset_domain);
+}
+
+static int amdgpu_device_halt_activities(
+       struct amdgpu_device *adev, struct amdgpu_job *job,
+       struct amdgpu_reset_context *reset_context,
+       struct list_head *device_list, struct amdgpu_hive_info *hive,
+       bool need_emergency_restart)
+{
+       struct amdgpu_device *tmp_adev = NULL;
+       int i, r = 0;
+
+       /* block all schedulers and reset given job's ring */
+       list_for_each_entry(tmp_adev, device_list, reset_list) {
                 amdgpu_device_set_mp1_state(tmp_adev);
 
                 /*
@@ -6301,11 +6325,6 @@ static void amdgpu_device_gpu_resume(struct amdgpu_device *adev,
                 amdgpu_ras_set_error_query_ready(tmp_adev, true);
 
         }
-
-       tmp_adev = list_first_entry(device_list, struct amdgpu_device,
-                                           reset_list);
-       amdgpu_device_unlock_reset_domain(tmp_adev->reset_domain);
-
 }
 
 
@@ -6376,10 +6395,16 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
         reset_context->hive = hive;
         INIT_LIST_HEAD(&device_list);
 
+       if (amdgpu_device_recovery_prepare(adev, &device_list, hive))
+               goto end_reset;
+
+       /* We need to lock reset domain only once both for XGMI and single device */
+       amdgpu_device_recovery_get_reset_lock(adev, &device_list);
+
         r = amdgpu_device_halt_activities(adev, job, reset_context, &device_list,
                                          hive, need_emergency_restart);
         if (r)
-               goto end_reset;
+               goto reset_unlock;
 
         if (need_emergency_restart)
                 goto skip_sched_resume;
@@ -6397,13 +6422,15 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
 
         r = amdgpu_device_asic_reset(adev, &device_list, reset_context);
         if (r)
-               goto end_reset;
+               goto reset_unlock;
 skip_hw_reset:
         r = amdgpu_device_sched_resume(&device_list, reset_context, job_signaled);
         if (r)
-               goto end_reset;
+               goto reset_unlock;
 skip_sched_resume:
         amdgpu_device_gpu_resume(adev, &device_list, need_emergency_restart);
+reset_unlock:
+       amdgpu_device_recovery_put_reset_lock(adev, &device_list);
 end_reset:
         if (hive) {
                 mutex_unlock(&hive->hive_lock);
@@ -6821,6 +6848,8 @@ pci_ers_result_t amdgpu_pci_error_detected(struct pci_dev *pdev, pci_channel_sta
                 memset(&reset_context, 0, sizeof(reset_context));
                 INIT_LIST_HEAD(&device_list);
 
+               amdgpu_device_recovery_prepare(adev, &device_list, hive);
+               amdgpu_device_recovery_get_reset_lock(adev, &device_list);
                 r = amdgpu_device_halt_activities(adev, NULL, &reset_context, &device_list,
                                          hive, false);
                 if (hive) {
@@ -6938,8 +6967,8 @@ pci_ers_result_t amdgpu_pci_slot_reset(struct pci_dev *pdev)
                 if (hive) {
                         list_for_each_entry(tmp_adev, &device_list, reset_list)
                                 amdgpu_device_unset_mp1_state(tmp_adev);
-                       amdgpu_device_unlock_reset_domain(adev->reset_domain);
                 }
+               amdgpu_device_recovery_put_reset_lock(adev, &device_list);
         }
 
         if (hive) {
@@ -6985,6 +7014,7 @@ void amdgpu_pci_resume(struct pci_dev *pdev)
 
         amdgpu_device_sched_resume(&device_list, NULL, NULL);
         amdgpu_device_gpu_resume(adev, &device_list, false);
+       amdgpu_device_recovery_put_reset_lock(adev, &device_list);
         adev->pcie_reset_ctx.occurs_dpc = false;
 
         if (hive) {

--
2.34.1