Skip to contentSkip to conversation

amd/amdkfd: add a function to wait no process running in kfd

PATCH In review Zhigang Luo 4 participants 8 messages Mar 22, 2024 – Mar 26, 2024

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

PATCH 1/3 amd/amdkfd: add a function to wait no process running in kfd

Change-Id I2a98d513c26107ac76ecf20e951c188afbc7ede6
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.c | 20 ++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h |  5 ++++-
 drivers/gpu/drm/amd/amdkfd/kfd_device.c    | 11 +++++++++++
 3 files changed, 35 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.c +20 -0
index d5fde8adf19b..e02bfcec608b 100644
@@ -261,6 +261,26 @@ int amdgpu_amdkfd_resume(struct amdgpu_device *adev, bool run_pm)
 	return r;
 }
 
+int amdgpu_amdkfd_wait_no_process_running(struct amdgpu_device *adev)
+{
+	unsigned long end_jiffies;
+
+	if (!adev->kfd.dev)
+		return 0;
+
+	end_jiffies = msecs_to_jiffies(AMDKFD_WAIT_NO_PROCESS_RUNNING_TIMEOUT_MS) + jiffies;
+	while (!kgd2kfd_is_processes_table_empty(adev->kfd.dev)) {
+		if (time_after(jiffies, end_jiffies)) {
+			dev_err(adev->dev, "wait no process running timeout\n");
+
+			return -ETIME;
+		}
+		schedule();
+	}
+
+	return 0;
+}
+
 int amdgpu_amdkfd_pre_reset(struct amdgpu_device *adev)
 {
 	int r = 0;
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +4 -1
index caee36e52a09..796e09abda69 100644
@@ -38,6 +38,8 @@
 #include "amdgpu_vm.h"
 #include "amdgpu_xcp.h"
 
+#define AMDKFD_WAIT_NO_PROCESS_RUNNING_TIMEOUT_MS 10000
+
 extern uint64_t amdgpu_amdkfd_total_mem_size;
 
 enum TLB_FLUSH_TYPE {
@@ -169,7 +171,7 @@ void amdgpu_amdkfd_set_compute_idle(struct amdgpu_device *adev, bool idle);
 bool amdgpu_amdkfd_have_atomics_support(struct amdgpu_device *adev);
 
 bool amdgpu_amdkfd_is_kfd_vmid(struct amdgpu_device *adev, u32 vmid);
-
+int amdgpu_amdkfd_wait_no_process_running(struct amdgpu_device *adev);
 int amdgpu_amdkfd_pre_reset(struct amdgpu_device *adev);
 
 int amdgpu_amdkfd_post_reset(struct amdgpu_device *adev);
@@ -411,6 +413,7 @@ bool kgd2kfd_device_init(struct kfd_dev *kfd,
 void kgd2kfd_device_exit(struct kfd_dev *kfd);
 void kgd2kfd_suspend(struct kfd_dev *kfd, bool run_pm);
 int kgd2kfd_resume(struct kfd_dev *kfd, bool run_pm);
+bool kgd2kfd_is_processes_table_empty(struct kfd_dev *kfd);
 int kgd2kfd_pre_reset(struct kfd_dev *kfd);
 int kgd2kfd_post_reset(struct kfd_dev *kfd);
 void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry);
drivers/gpu/drm/amd/amdkfd/kfd_device.c +11 -0
index 041ec3de55e7..2bec79e0c721 100644
@@ -894,6 +894,17 @@ void kgd2kfd_device_exit(struct kfd_dev *kfd)
 	kfree(kfd);
 }
 
+bool kgd2kfd_is_processes_table_empty(struct kfd_dev *kfd)
+{
+	bool is_empty;
+
+	mutex_lock(&kfd_processes_mutex);
+	is_empty = hash_empty(kfd_processes_table);
+	mutex_unlock(&kfd_processes_mutex);
+
+	return is_empty;
+}
+
 int kgd2kfd_pre_reset(struct kfd_dev *kfd)
 {
 	struct kfd_node *node;
Signature · 1 line
2.25.1
#1 lore raw

PATCH 2/3 amd/amdgpu: wait no process running in kfd before resuming device

it will cause page fault after device recovered if there is a process running.

Change-Id Ib1eddb56b69ecd41fe703abd169944154f48b0cd
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +2 -0
index 70261eb9b0bb..2867e9186e44 100644
@@ -4974,6 +4974,8 @@ static int amdgpu_device_reset_sriov(struct amdgpu_device *adev,
 retry:
 	amdgpu_amdkfd_pre_reset(adev);
 
+	amdgpu_amdkfd_wait_no_process_running(adev);
+
 	amdgpu_device_stop_pending_resets(adev);
 
 	if (from_hypervisor)
Signature · 1 line
2.25.1
maintainer
#2 lore raw

Re: [PATCH 2/3] amd/amdgpu: wait no process running in kfd before resuming device

Quoted 13 lines · Zhigang Luo

it will cause page fault after device recovered if there is a process running.

Change-Id Ib1eddb56b69ecd41fe703abd169944154f48b0cd
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 2 ++
  1 file changed, 2 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +0 -0
index 70261eb9b0bb..2867e9186e44 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c static int amdgpu_device_reset_sriov(struct amdgpu_device *adev, · line 4978

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +2 -0
@@ -4974,6 +4974,8 @@ static int amdgpu_device_reset_sriov(struct amdgpu_device *adev,
  retry:
  	amdgpu_amdkfd_pre_reset(adev);
 
+	amdgpu_amdkfd_wait_no_process_running(adev);
+

This waits for the processes to be terminated. What would cause the processes to be terminated? Why do the processes need to be terminated? Isn't it enough if the processes are removed from the runlist in pre-reset, so they can no longer execute on the GPU?

Regards,
  Felix

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +0 -0
  	amdgpu_device_stop_pending_resets(adev);
 
  	if (from_hypervisor)
#3 lore raw

Re: [PATCH 2/3] amd/amdgpu: wait no process running in kfd before resuming device

On 2024-03-25 14:45, Felix Kuehling wrote:
On 2024-03-22 15:57, Zhigang Luo wrote:
it will cause page fault after device recovered if there is a process running.

Change-Id Ib1eddb56b69ecd41fe703abd169944154f48b0cd

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

  1 file changed, 2 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +0 -0
index 70261eb9b0bb..2867e9186e44 100644
@@ -4974,6 +4974,8 @@ static int amdgpu_device_reset_sriov(struct amdgpu_device *adev,

  retry:
      amdgpu_amdkfd_pre_reset(adev);
  +    amdgpu_amdkfd_wait_no_process_running(adev); +

This waits for the processes to be terminated. What would cause the processes to be terminated? Why do the processes need to be terminated? Isn't it enough if the processes are removed from the runlist in pre-reset, so they can no longer execute on the GPU? mode 1 reset on SRIOV is much faster then BM, kgd2kfd_pre_reset sends GPU reset event to user space, don't remove queues from the runlist, after mode1 reset is done, there is queue still running and generate vm fault because the GPU page table is gone.
Regards,
Philip

Regards,
  Felix

      amdgpu_device_stop_pending_resets(adev);
        if (from_hypervisor)

maintainer
#4 lore raw

Re: [PATCH 2/3] amd/amdgpu: wait no process running in kfd before resuming device

Quoted 25 lines · Philip Yang
Quoted 9 lines · Felix Kuehling
Quoted 8 lines · Zhigang Luo

it will cause page fault after device recovered if there is a process running.

Change-Id Ib1eddb56b69ecd41fe703abd169944154f48b0cd

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

  1 file changed, 2 insertions(+)
Quoted 12 lines
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +0 -0
index 70261eb9b0bb..2867e9186e44 100644
@@ -4974,6 +4974,8 @@ static int amdgpu_device_reset_sriov(struct 
 amdgpu_device *adev,

  retry:
      amdgpu_amdkfd_pre_reset(adev);
  +    amdgpu_amdkfd_wait_no_process_running(adev); +

This waits for the processes to be terminated. What would cause the processes to be terminated? Why do the processes need to be terminated? Isn't it enough if the processes are removed from the runlist in pre-reset, so they can no longer execute on the GPU?

Quoted 2 lines

mode 1 reset on SRIOV is much faster then BM, kgd2kfd_pre_reset sends GPU reset event to user space, don't remove queues from the runlist, after mode1 reset is done, there is queue still running and generate vm fault because the GPU page table is gone.

I think seeing a page fault during the reset is not a problem. Seeing a page fault after the reset would be a bug. The process should not be on the runlist after the reset is done.

Waiting for the process to terminate first looks like a workaround, when the real bug is maybe that we're not updating the process state correctly in pre-reset. All currently running processes should be put into evicted state, so they are not put back on the runlist after the reset.

Regards,
  Felix

Quoted 11 lines

Regards,

Philip

Quoted 7 lines

Regards,
  Felix

Quoted 2 lines

amdgpu_device_stop_pending_resets(adev);
        if (from_hypervisor)

#5 lore raw

Re: [PATCH 2/3] amd/amdgpu: wait no process running in kfd before resuming device

On 2024-03-26 11:01, Felix Kuehling wrote:
On 2024-03-26 10:53, Philip Yang wrote:

On 2024-03-25 14:45, Felix Kuehling wrote:
On 2024-03-22 15:57, Zhigang Luo wrote:
it will cause page fault after device recovered if there is a process running.

Change-Id Ib1eddb56b69ecd41fe703abd169944154f48b0cd

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

  1 file changed, 2 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +0 -0
index 70261eb9b0bb..2867e9186e44 100644
@@ -4974,6 +4974,8 @@ static int amdgpu_device_reset_sriov(struct amdgpu_device *adev,

  retry:
      amdgpu_amdkfd_pre_reset(adev);
  +    amdgpu_amdkfd_wait_no_process_running(adev); +

This waits for the processes to be terminated. What would cause the processes to be terminated? Why do the processes need to be terminated? Isn't it enough if the processes are removed from the runlist in pre-reset, so they can no longer execute on the GPU?

mode 1 reset on SRIOV is much faster then BM, kgd2kfd_pre_reset sends GPU reset event to user space, don't remove queues from the runlist, after mode1 reset is done, there is queue still running and generate vm fault because the GPU page table is gone.

I think seeing a page fault during the reset is not a problem. Seeing a page fault after the reset would be a bug. The process should not be on the runlist after the reset is done.

Waiting for the process to terminate first looks like a workaround, when the real bug is maybe that we're not updating the process state correctly in pre-reset. All currently running processes should be put into evicted state, so they are not put back on the runlist after the reset. Forgot to mention it is F/W hang issue to trigger GPU reset, there is also error message when kgd2kfd_pre_reset -> kgd2kfd_suspend to evict queues from the runlist,  yes, this seems W/A for the real issue related to mode1 reset.

Regards,
Philip

Regards,
  Felix

Regards,

Philip

Regards,
  Felix

amdgpu_device_stop_pending_resets(adev);
        if (from_hypervisor)

#6 lore raw

PATCH 3/3 amd/amdgpu: improve VF recover time

In reply to Zhigang Luo

1. change AMDGPU_VF2PF_UPDATE_MAX_RETRY_LIMIT from 30 to 5.
2. set fatel error detected flag.

Change-Id If1e0357deffa4549d4e83e925c8d764f7f8c9f42
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c   | 1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_virt.h   | 2 +-
 3 files changed, 3 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +1 -0
index 2867e9186e44..50f87fff778e 100644
@@ -4984,6 +4984,7 @@ static int amdgpu_device_reset_sriov(struct amdgpu_device *adev,
 		r = amdgpu_virt_reset_gpu(adev);
 	if (r)
 		return r;
+	amdgpu_ras_set_fed(adev, false);
 	amdgpu_irq_gpu_reset_resume_helper(adev);
 
 	/* some sw clean up VF needs to do before recover */
drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c +1 -0
index aed60aaf1a55..4b6e50cfba08 100644
@@ -593,6 +593,7 @@ static void amdgpu_virt_update_vf2pf_work_item(struct work_struct *work)
 		adev->virt.vf2pf_update_retry_cnt++;
 		if ((adev->virt.vf2pf_update_retry_cnt >= AMDGPU_VF2PF_UPDATE_MAX_RETRY_LIMIT) &&
 		    amdgpu_sriov_runtime(adev) && !amdgpu_in_reset(adev)) {
+			amdgpu_ras_set_fed(adev, true);
 			if (amdgpu_reset_domain_schedule(adev->reset_domain,
 							  &adev->virt.flr_work))
 				return;
drivers/gpu/drm/amd/amdgpu/amdgpu_virt.h +1 -1
index a858bc98cad4..0a57b637c4bd 100644
@@ -52,7 +52,7 @@
 /* tonga/fiji use this offset */
 #define mmBIF_IOV_FUNC_IDENTIFIER 0x1503
 
-#define AMDGPU_VF2PF_UPDATE_MAX_RETRY_LIMIT 30
+#define AMDGPU_VF2PF_UPDATE_MAX_RETRY_LIMIT 5
 
 enum amdgpu_sriov_vf_mode {
 	SRIOV_VF_MODE_BARE_METAL = 0,
Signature · 1 line
2.25.1
#7 lore raw
In reply to Zhigang Luo
Quoted 74 lines · Zhigang Luo
Change-Id I2a98d513c26107ac76ecf20e951c188afbc7ede6
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.c | 20 ++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h |  5 ++++-
 drivers/gpu/drm/amd/amdkfd/kfd_device.c    | 11 +++++++++++
 3 files changed, 35 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.c +20 -0
index d5fde8adf19b..e02bfcec608b 100644
@@ -261,6 +261,26 @@ int amdgpu_amdkfd_resume(struct amdgpu_device *adev, bool run_pm)
 	return r;
 }
 
+int amdgpu_amdkfd_wait_no_process_running(struct amdgpu_device *adev)
+{
+	unsigned long end_jiffies;
+
+	if (!adev->kfd.dev)
+		return 0;
+
+	end_jiffies = msecs_to_jiffies(AMDKFD_WAIT_NO_PROCESS_RUNNING_TIMEOUT_MS) + jiffies;
+	while (!kgd2kfd_is_processes_table_empty(adev->kfd.dev)) {
+		if (time_after(jiffies, end_jiffies)) {
+			dev_err(adev->dev, "wait no process running timeout\n");
+
+			return -ETIME;
+		}
+		schedule();
+	}
+
+	return 0;
+}
+
 int amdgpu_amdkfd_pre_reset(struct amdgpu_device *adev)
 {
 	int r = 0;
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +4 -1
index caee36e52a09..796e09abda69 100644
@@ -38,6 +38,8 @@
 #include "amdgpu_vm.h"
 #include "amdgpu_xcp.h"
 
+#define AMDKFD_WAIT_NO_PROCESS_RUNNING_TIMEOUT_MS 10000
+
 extern uint64_t amdgpu_amdkfd_total_mem_size;
 
 enum TLB_FLUSH_TYPE {
@@ -169,7 +171,7 @@ void amdgpu_amdkfd_set_compute_idle(struct amdgpu_device *adev, bool idle);
 bool amdgpu_amdkfd_have_atomics_support(struct amdgpu_device *adev);
 
 bool amdgpu_amdkfd_is_kfd_vmid(struct amdgpu_device *adev, u32 vmid);
-
+int amdgpu_amdkfd_wait_no_process_running(struct amdgpu_device *adev);
 int amdgpu_amdkfd_pre_reset(struct amdgpu_device *adev);
 
 int amdgpu_amdkfd_post_reset(struct amdgpu_device *adev);
@@ -411,6 +413,7 @@ bool kgd2kfd_device_init(struct kfd_dev *kfd,
 void kgd2kfd_device_exit(struct kfd_dev *kfd);
 void kgd2kfd_suspend(struct kfd_dev *kfd, bool run_pm);
 int kgd2kfd_resume(struct kfd_dev *kfd, bool run_pm);
+bool kgd2kfd_is_processes_table_empty(struct kfd_dev *kfd);
 int kgd2kfd_pre_reset(struct kfd_dev *kfd);
 int kgd2kfd_post_reset(struct kfd_dev *kfd);
 void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry);
drivers/gpu/drm/amd/amdkfd/kfd_device.c +0 -0
index 041ec3de55e7..2bec79e0c721 100644

drivers/gpu/drm/amd/amdkfd/kfd_device.c void kgd2kfd_device_exit(struct kfd_dev *kfd) · line 902

Quoted 9 lines
drivers/gpu/drm/amd/amdkfd/kfd_device.c +6 -0
@@ -894,6 +894,17 @@ void kgd2kfd_device_exit(struct kfd_dev *kfd)
 	kfree(kfd);
 }
 
+bool kgd2kfd_is_processes_table_empty(struct kfd_dev *kfd)
+{
+	bool is_empty;
+
+	mutex_lock(&kfd_processes_mutex);
+	is_empty = hash_empty(kfd_processes_table);

This table is declared globally, but VF FLR is device specific. So you may want to check if this is the right thing to do when there are multiple VFs in a VM.

Thanks,
Lijo

Quoted 8 lines
drivers/gpu/drm/amd/amdkfd/kfd_device.c +5 -0
+	mutex_unlock(&kfd_processes_mutex);
+
+	return is_empty;
+}
+
 int kgd2kfd_pre_reset(struct kfd_dev *kfd)
 {
 	struct kfd_node *node;