Skip to contentSkip to conversation

Fix mode1 reset test failures

PATCH In review Philip Yang 2 participants 7 messages Feb 26, 2025

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

cover Fix mode1 reset test failures

mode1 reset test running with compute applications trigger many different failures, such as machine reboot, kernel crash with general protection fault, NULL pointer access or cpu page fault etc from random calling backtrace.

With KASAN and slub_debug enabled kernel, we capture slub left-redzone overwrtten warning, but no KASAN warning before crash. This can confirm there is system memory overwritten from GPU, not from CPU.

Change hanghws test to evict user queues first, then do mode1 reset test, no crash anymore, this can confirm the system memory overwritten by user queues. Because the user queues keep using GPU while KFD cleanup worker free the outstanding BOs, the freed system memory is allocated and reused to create job, resource. Then the data structure is corrupted by user queue and cause crash.

The fix is in KFD cleanup worker, after evicting all user queues, flush reset_domain->wq to ensure ongoing mode1 reset is done or user queues are evicted, then free outstanding BOs.

Philip Yang (5):
drm/amdkfd: Remove kfd_process_hw_exception worker drm/amdkfd: KFD release_work possible circular locking drm/amdkfd: Fix mode1 reset crash issue drm/amdkfd: Fix pqm_destroy_queue race with GPU reset drm/amdkfd: debugfs hang_hws skip GPU with MES

 drivers/gpu/drm/amd/amdkfd/kfd_device.c       |  5 +++
 .../drm/amd/amdkfd/kfd_device_queue_manager.c | 11 +------
 .../drm/amd/amdkfd/kfd_device_queue_manager.h |  1 -
 drivers/gpu/drm/amd/amdkfd/kfd_process.c      | 33 ++++++++++++++-----
 .../amd/amdkfd/kfd_process_queue_manager.c    |  2 +-
 5 files changed, 32 insertions(+), 20 deletions(-)
Signature · 1 line
2.47.1
#1 lore raw

PATCH 1/5 drm/amdkfd: Remove kfd_process_hw_exception worker

With GPU reset-domain worker implemented, KFD hw_exception worker is not needed any more, just call amdgpu_amdkfd_gpu_reset directly from kfd_hws_hang.

---
 drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c | 11 +----------
 drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.h |  1 -
 2 files changed, 1 insertion(+), 11 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +1 -10
index 94b1ac8a4735..91e4988dc1e3 100644
@@ -66,7 +66,6 @@ static inline void deallocate_hqd(struct device_queue_manager *dqm,
 static int allocate_hqd(struct device_queue_manager *dqm, struct queue *q);
 static int allocate_sdma_queue(struct device_queue_manager *dqm,
 				struct queue *q, const uint32_t *restore_sdma_id);
-static void kfd_process_hw_exception(struct work_struct *work);
 
 static inline
 enum KFD_MQD_TYPE get_mqd_type_from_queue_type(enum kfd_queue_type type)
@@ -170,7 +169,7 @@ static void kfd_hws_hang(struct device_queue_manager *dqm)
 	/*
 	 * Issue a GPU reset if HWS is unresponsive
 	 */
-	schedule_work(&dqm->hw_exception_work);
+	amdgpu_amdkfd_gpu_reset(dqm->dev->adev);
 }
 
 static int convert_to_mes_queue_type(int queue_type)
@@ -1740,7 +1739,6 @@ static int initialize_cpsch(struct device_queue_manager *dqm)
 	dqm->active_cp_queue_count = 0;
 	dqm->gws_queue_count = 0;
 	dqm->active_runlist = false;
-	INIT_WORK(&dqm->hw_exception_work, kfd_process_hw_exception);
 	dqm->trap_debug_vmid = 0;
 
 	init_sdma_bitmaps(dqm);
@@ -3080,13 +3078,6 @@ int kfd_evict_process_device(struct kfd_process_device *pdd)
 	return ret;
 }
 
-static void kfd_process_hw_exception(struct work_struct *work)
-{
-	struct device_queue_manager *dqm = container_of(work,
-			struct device_queue_manager, hw_exception_work);
-	amdgpu_amdkfd_gpu_reset(dqm->dev->adev);
-}
-
 int reserve_debug_trap_vmid(struct device_queue_manager *dqm,
 				struct qcm_process_device *qpd)
 {
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.h +0 -1
index 09ab36f8e8c6..7146e227e2c1 100644
@@ -269,7 +269,6 @@ struct device_queue_manager {
 	/* hw exception  */
 	bool			is_hws_hang;
 	bool			is_resetting;
-	struct work_struct	hw_exception_work;
 	struct kfd_mem_obj	hiq_sdma_mqd;
 	bool			sched_running;
 	bool			sched_halt;
Signature · 1 line
2.47.1
#2 lore raw

PATCH 2/5 drm/amdkfd: KFD release_work possible circular locking

In reply to Philip Yang

If waiting for gpu reset done in KFD release_work, thers is WARNING:
possible circular locking dependency detected

#2 kfd_create_process
kfd_process_mutex
flush kfd release work

#1 kfd release work
wait for amdgpu reset work

#0 amdgpu_device_gpu_reset
kgd2kfd_pre_reset
kfd_process_mutex

  Possible unsafe locking scenario:
        CPU0                    CPU1
        ----                    ----
   lock((work_completion)(&p->release_work));
                  lock((wq_completion)kfd_process_wq);
                  lock((work_completion)(&p->release_work));

lock((wq_completion)amdgpu-reset-dev);

To fix this, KFD create process move flush release work outside kfd_process_mutex.

---
 drivers/gpu/drm/amd/amdkfd/kfd_process.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_process.c +8 -8
index 1067afdb456e..2715ca53e9da 100644
@@ -838,6 +838,14 @@ struct kfd_process *kfd_create_process(struct task_struct *thread)
 		return ERR_PTR(-EINVAL);
 	}
 
+	/* If the process just called exec(3), it is possible that the
+	 * cleanup of the kfd_process (following the release of the mm
+	 * of the old process image) is still in the cleanup work queue.
+	 * Make sure to drain any job before trying to recreate any
+	 * resource for this process.
+	 */
+	flush_workqueue(kfd_process_wq);
+
 	/*
 	 * take kfd processes mutex before starting of process creation
 	 * so there won't be a case where two threads of the same process
@@ -858,14 +866,6 @@ struct kfd_process *kfd_create_process(struct task_struct *thread)
 	if (process) {
 		pr_debug("Process already found\n");
 	} else {
-		/* If the process just called exec(3), it is possible that the
-		 * cleanup of the kfd_process (following the release of the mm
-		 * of the old process image) is still in the cleanup work queue.
-		 * Make sure to drain any job before trying to recreate any
-		 * resource for this process.
-		 */
-		flush_workqueue(kfd_process_wq);
-
 		process = create_process(thread);
 		if (IS_ERR(process))
 			goto out;
Signature · 1 line
2.47.1
#3 lore raw

PATCH 3/5 drm/amdkfd: Fix mode1 reset crash issue

In reply to Philip Yang

If HW scheduler hangs and mode1 reset is used to recover GPU, KFD signal user space to abort the processes. After process abort exit, user queues still use the GPU to access system memory before h/w is reset while KFD cleanup worker free system memory and free VRAM.

There is use-after-free race bug that KFD allocate and reuse the freed system memory, and user queue write to the same system memory to corrupt the data structure and cause driver crash.

To fix this race, KFD cleanup worker terminate user queues, then flush reset_domain wq to wait for any GPU ongoing reset complete, and then free outstanding BOs.

---
 drivers/gpu/drm/amd/amdkfd/kfd_process.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
drivers/gpu/drm/amd/amdkfd/kfd_process.c +17 -0
index 2715ca53e9da..7c0c24732481 100644
@@ -35,6 +35,7 @@
 #include <linux/pm_runtime.h>
 #include "amdgpu_amdkfd.h"
 #include "amdgpu.h"
+#include "amdgpu_reset.h"
 
 struct mm_struct;
 
@@ -1133,6 +1134,17 @@ static void kfd_process_remove_sysfs(struct kfd_process *p)
 	p->kobj = NULL;
 }
 
+/*
+ * If any GPU is ongoing reset, wait for reset complete.
+ */
+static void kfd_process_wait_gpu_reset_complete(struct kfd_process *p)
+{
+	int i;
+
+	for (i = 0; i < p->n_pdds; i++)
+		flush_workqueue(p->pdds[i]->dev->adev->reset_domain->wq);
+}
+
 /* No process locking is needed in this function, because the process
  * is not findable any more. We must assume that no other thread is
  * using it any more, otherwise we couldn't safely free the process
@@ -1147,6 +1159,11 @@ static void kfd_process_wq_release(struct work_struct *work)
 	kfd_process_dequeue_from_all_devices(p);
 	pqm_uninit(&p->pqm);
 
+	/*
+	 * If GPU in reset, user queues may still running, wait for reset complete.
+	 */
+	kfd_process_wait_gpu_reset_complete(p);
+
 	/* Signal the eviction fence after user mode queues are
 	 * destroyed. This allows any BOs to be freed without
 	 * triggering pointless evictions or waiting for fences.
Signature · 1 line
2.47.1
#4 lore raw

PATCH 4/5 drm/amdkfd: Fix pqm_destroy_queue race with GPU reset

In reply to Philip Yang

If GPU in reset, destroy_queue return -EIO, pqm_destroy_queue should delete the queue from process_queue_list and free the resource.

---
 drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c +1 -1
index 93647ee27325..662c595ce783 100644
@@ -548,7 +548,7 @@ int pqm_destroy_queue(struct process_queue_manager *pqm, unsigned int qid)
 			pr_err("Pasid 0x%x destroy queue %d failed, ret %d\n",
 				pdd->pasid,
 				pqn->q->properties.queue_id, retval);
-			if (retval != -ETIME)
+			if (retval != -ETIME && retval != -EIO)
 				goto err_destroy_queue;
 		}
 		kfd_procfs_del_queue(pqn->q);
Signature · 1 line
2.47.1
#5 lore raw

PATCH 5/5 drm/amdkfd: debugfs hang_hws skip GPU with MES

In reply to Philip Yang

debugfs hang_hws is used by GPU reset test with HWS, for MES this crash the kernel with NULL pointer access because dqm->packet_mgr is not setup for MES path.

Skip GPU with MES for now, MES hang_hws debugfs interface will be supported later.

---
 drivers/gpu/drm/amd/amdkfd/kfd_device.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/gpu/drm/amd/amdkfd/kfd_device.c +5 -0
index 095e73790dc4..b9c82be6ce13 100644
@@ -1604,6 +1604,11 @@ int kfd_debugfs_hang_hws(struct kfd_node *dev)
 		return -EINVAL;
 	}
 
+	if (dev->kfd->shared_resources.enable_mes) {
+		dev_err(dev->adev->dev, "Inducing MES hang is not supported\n");
+		return -EINVAL;
+	}
+
 	return dqm_debugfs_hang_hws(dev->dqm);
 }
 
Signature · 1 line
2.47.1
maintainer Reviewed-by on the v1 series
#6 lore raw
In reply to Philip Yang

The series is

Quoted 32 lines · Philip Yang

mode1 reset test running with compute applications trigger many different failures, such as machine reboot, kernel crash with general protection fault, NULL pointer access or cpu page fault etc from random calling backtrace.

With KASAN and slub_debug enabled kernel, we capture slub left-redzone overwrtten warning, but no KASAN warning before crash. This can confirm there is system memory overwritten from GPU, not from CPU.

Change hanghws test to evict user queues first, then do mode1 reset test, no crash anymore, this can confirm the system memory overwritten by user queues. Because the user queues keep using GPU while KFD cleanup worker free the outstanding BOs, the freed system memory is allocated and reused to create job, resource. Then the data structure is corrupted by user queue and cause crash.

The fix is in KFD cleanup worker, after evicting all user queues, flush reset_domain->wq to ensure ongoing mode1 reset is done or user queues are evicted, then free outstanding BOs.

Philip Yang (5):
drm/amdkfd: Remove kfd_process_hw_exception worker drm/amdkfd: KFD release_work possible circular locking drm/amdkfd: Fix mode1 reset crash issue drm/amdkfd: Fix pqm_destroy_queue race with GPU reset drm/amdkfd: debugfs hang_hws skip GPU with MES

  drivers/gpu/drm/amd/amdkfd/kfd_device.c       |  5 +++
  .../drm/amd/amdkfd/kfd_device_queue_manager.c | 11 +------
  .../drm/amd/amdkfd/kfd_device_queue_manager.h |  1 -
  drivers/gpu/drm/amd/amdkfd/kfd_process.c      | 33 ++++++++++++++-----
  .../amd/amdkfd/kfd_process_queue_manager.c    |  2 +-
  5 files changed, 32 insertions(+), 20 deletions(-)