<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body>
    <p><br>
    </p>
    <div class="moz-cite-prefix">On 2024-08-21 18:01, Xiaogang.Chen
      wrote:<br>
    </div>
    <blockquote type="cite" cite="mid:20240821220133.25975-1-xiaogang.chen@amd.com">
      <pre class="moz-quote-pre" wrap="">From: Xiaogang Chen <a class="moz-txt-link-rfc2396E" href="mailto:xiaogang.chen@amd.com"><xiaogang.chen@amd.com></a>

When app unmap vm ranges(munmap) kfd/svm starts drain pending page fault and
not handle any incoming pages fault of this process until a deferred work item
got executed by default system wq. The time period of "not handle page fault"
can be long and is unpredicable. That is advese to kfd performance on page
faults recovery.

This patch uses time stamp of incoming page fault to decide to drop or recover
page fault. When app unmap vm ranges kfd records each gpu device's ih ring
current time stamp. These time stamps are used at kfd page fault recovery
routine.

Any page fault happened on unmapped ranges after unmap events is application
bug that accesses vm range after unmap. It is not driver work to cover that.

By using time stamp of page fault do not need drain page faults at deferred
work. So, the time period that kfd does not handle page faults is reduced
and can be controlled.

Signed-off-by: Xiaogang.Chen <a class="moz-txt-link-rfc2396E" href="mailto:Xiaogang.Chen@amd.com"><Xiaogang.Chen@amd.com></a></pre>
    </blockquote>
    Some nitpicks below.<br>
    This patch is Reviewed-by: Philip Yang <a class="moz-txt-link-rfc2396E" href="mailto:Philip.Yang@amd.com"><Philip.Yang@amd.com></a><br>
    <blockquote type="cite" cite="mid:20240821220133.25975-1-xiaogang.chen@amd.com">
      <pre class="moz-quote-pre" wrap="">
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c |  4 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h |  2 +-
 drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c |  3 +-
 drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c  |  4 +-
 drivers/gpu/drm/amd/amdkfd/kfd_priv.h  |  2 +
 drivers/gpu/drm/amd/amdkfd/kfd_svm.c   | 98 +++++++++++++++++---------
 drivers/gpu/drm/amd/amdkfd/kfd_svm.h   |  2 +-
 7 files changed, 75 insertions(+), 40 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
index a060c28f0877..d89a4c14bbb8 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
@@ -2764,7 +2764,7 @@ int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
  * shouldn't be reported any more.
  */
 bool amdgpu_vm_handle_fault(struct amdgpu_device *adev, u32 pasid,
-                           u32 vmid, u32 node_id, uint64_t addr,
+                           u32 vmid, u32 node_id, uint64_t addr, uint64_t ts,
                            bool write_fault)
 {
        bool is_compute_context = false;
@@ -2790,7 +2790,7 @@ bool amdgpu_vm_handle_fault(struct amdgpu_device *adev, u32 pasid,
        addr /= AMDGPU_GPU_PAGE_SIZE;
 
        if (is_compute_context && !svm_range_restore_pages(adev, pasid, vmid,
-           node_id, addr, write_fault)) {
+           node_id, addr, ts, write_fault)) {
                amdgpu_bo_unref(&root);
                return true;
        }
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
index 046949c4b695..d12d66dca8e9 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
@@ -558,7 +558,7 @@ amdgpu_vm_get_task_info_vm(struct amdgpu_vm *vm);
 void amdgpu_vm_put_task_info(struct amdgpu_task_info *task_info);
 
 bool amdgpu_vm_handle_fault(struct amdgpu_device *adev, u32 pasid,
-                           u32 vmid, u32 node_id, uint64_t addr,
+                           u32 vmid, u32 node_id, uint64_t addr, uint64_t ts,
                            bool write_fault);
 
 void amdgpu_vm_set_task_info(struct amdgpu_vm *vm);
diff --git a/drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c b/drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c
index f0ceab3ce5bf..9784a2892185 100644
--- a/drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c
+++ b/drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c
@@ -132,7 +132,8 @@ static int gmc_v10_0_process_interrupt(struct amdgpu_device *adev,
                /* Try to handle the recoverable page faults by filling page
                 * tables
                 */
-               if (amdgpu_vm_handle_fault(adev, entry->pasid, 0, 0, addr, write_fault))
+               if (amdgpu_vm_handle_fault(adev, entry->pasid, 0, 0, addr,
+                                          entry->timestamp, write_fault))
                        return 1;
        }
 
diff --git a/drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c b/drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c
index b73136d390cc..c76ac0dfe572 100644
--- a/drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c
+++ b/drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c
@@ -595,7 +595,7 @@ static int gmc_v9_0_process_interrupt(struct amdgpu_device *adev,
                        cam_index = entry->src_data[2] & 0x3ff;
 
                        ret = amdgpu_vm_handle_fault(adev, entry->pasid, entry->vmid, node_id,
-                                                    addr, write_fault);
+                                                    addr, entry->timestamp, write_fault);
                        WDOORBELL32(adev->irq.retry_cam_doorbell_index, cam_index);
                        if (ret)
                                return 1;
@@ -618,7 +618,7 @@ static int gmc_v9_0_process_interrupt(struct amdgpu_device *adev,
                         * tables
                         */
                        if (amdgpu_vm_handle_fault(adev, entry->pasid, entry->vmid, node_id,
-                                                  addr, write_fault))
+                                                  addr, entry->timestamp, write_fault))
                                return 1;
                }
        }
diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_priv.h b/drivers/gpu/drm/amd/amdkfd/kfd_priv.h
index 4190fa339913..288ebf02fa1c 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_priv.h
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_priv.h
@@ -863,6 +863,8 @@ struct svm_range_list {
        struct delayed_work             restore_work;
        DECLARE_BITMAP(bitmap_supported, MAX_GPU_INSTANCE);
        struct task_struct              *faulting_task;
+       /* check point ts decides if page fault recovery need be dropped */
+       uint64_t                        checkpoint_ts[MAX_GPU_INSTANCE];
 };
 
 /* Process data */
diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
index 10b1a1f64198..e30c644e44a6 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
@@ -2256,16 +2256,10 @@ static void svm_range_drain_retry_fault(struct svm_range_list *svms)
 {
        struct kfd_process_device *pdd;
        struct kfd_process *p;
-       int drain;
        uint32_t i;
 
        p = container_of(svms, struct kfd_process, svms);
 
-restart:
-       drain = atomic_read(&svms->drain_pagefaults);
-       if (!drain)
-               return;
-
        for_each_set_bit(i, svms->bitmap_supported, p->n_pdds) {
                pdd = p->pdds[i];
                if (!pdd)
@@ -2285,8 +2279,6 @@ static void svm_range_drain_retry_fault(struct svm_range_list *svms)
 
                pr_debug("drain retry fault gpu %d svms 0x%p done\n", i, svms);
        }
-       if (atomic_cmpxchg(&svms->drain_pagefaults, drain, 0) != drain)
-               goto restart;
 }
 
 static void svm_range_deferred_list_work(struct work_struct *work)
@@ -2308,17 +2300,8 @@ static void svm_range_deferred_list_work(struct work_struct *work)
                         prange->start, prange->last, prange->work_item.op);
 
                mm = prange->work_item.mm;
-retry:
-               mmap_write_lock(mm);
 
-               /* Checking for the need to drain retry faults must be inside
-                * mmap write lock to serialize with munmap notifiers.
-                */
-               if (unlikely(atomic_read(&svms->drain_pagefaults))) {
-                       mmap_write_unlock(mm);
-                       svm_range_drain_retry_fault(svms);
-                       goto retry;
-               }
+               mmap_write_lock(mm);
 
                /* Remove from deferred_list must be inside mmap write lock, for
                 * two race cases:
@@ -2439,6 +2422,7 @@ svm_range_unmap_from_cpu(struct mm_struct *mm, struct svm_range *prange,
        struct kfd_process *p;
        unsigned long s, l;
        bool unmap_parent;
+       uint32_t i;
 
        if (atomic_read(&prange->queue_refcount)) {
                int r;
@@ -2458,11 +2442,37 @@ svm_range_unmap_from_cpu(struct mm_struct *mm, struct svm_range *prange,
        pr_debug("svms 0x%p prange 0x%p [0x%lx 0x%lx] [0x%lx 0x%lx]\n", svms,
                 prange, prange->start, prange->last, start, last);
 
-       /* Make sure pending page faults are drained in the deferred worker
-        * before the range is freed to avoid straggler interrupts on
-        * unmapped memory causing "phantom faults".
+       /* calculate time stamps that are used to decide which page faults need be
+        * dropped or handled before unmap pages from gpu vm
         */
-       atomic_inc(&svms->drain_pagefaults);
+       for_each_set_bit(i, svms->bitmap_supported, p->n_pdds) {
+               struct kfd_process_device *pdd;
+               struct amdgpu_device *adev;
+               struct amdgpu_ih_ring *ih;
+               uint32_t checkpoint_wptr;
+
+               pdd = p->pdds[i];
+               if (!pdd)
+                       continue;
+
+               adev = pdd->dev->adev;
+
+               /* Check and drain ih1 ring if cam not available */
+               ih = &adev->irq.ih1;
+               checkpoint_wptr = amdgpu_ih_get_wptr(adev, ih);
+               if (ih->rptr != checkpoint_wptr) {
+                       svms->checkpoint_ts[i] = amdgpu_ih_decode_iv_ts(adev, ih, checkpoint_wptr, -1);
+                       continue;
+               }
+
+               /* check if dev->irq.ih_soft is not empty */
+               ih = &adev->irq.ih_soft;
+               checkpoint_wptr = amdgpu_ih_get_wptr(adev, ih);
+               if (ih->rptr != checkpoint_wptr) {
+                       svms->checkpoint_ts[i] = amdgpu_ih_decode_iv_ts(adev, ih, checkpoint_wptr, -1);
+                       continue;
+               }</pre>
    </blockquote>
    continue is not needed.<br>
    <pre class="moz-quote-pre" wrap="">if (ih->rptr != checkpoint_wptr)
        svms->checkpoint_ts[i] = amdgpu_ih_decode_iv_ts(adev, ih, checkpoint_wptr, -1);
</pre>
    <blockquote type="cite" cite="mid:20240821220133.25975-1-xiaogang.chen@amd.com">
      <pre class="moz-quote-pre" wrap="">
+       }
 
        unmap_parent = start <= prange->start && last >= prange->last;
 
@@ -2903,7 +2913,7 @@ svm_fault_allowed(struct vm_area_struct *vma, bool write_fault)
 int
 svm_range_restore_pages(struct amdgpu_device *adev, unsigned int pasid,
                        uint32_t vmid, uint32_t node_id,
-                       uint64_t addr, bool write_fault)
+                       uint64_t addr, uint64_t ts, bool write_fault)
 {
        unsigned long start, last, size;
        struct mm_struct *mm = NULL;
@@ -2913,7 +2923,7 @@ svm_range_restore_pages(struct amdgpu_device *adev, unsigned int pasid,
        ktime_t timestamp = ktime_get_boottime();
        struct kfd_node *node;
        int32_t best_loc;
-       int32_t gpuidx = MAX_GPU_INSTANCE;
+       int32_t gpuid, gpuidx = MAX_GPU_INSTANCE;
        bool write_locked = false;
        struct vm_area_struct *vma;
        bool migration = false;
@@ -2934,11 +2944,38 @@ svm_range_restore_pages(struct amdgpu_device *adev, unsigned int pasid,
        pr_debug("restoring svms 0x%p fault address 0x%llx\n", svms, addr);
 
        if (atomic_read(&svms->drain_pagefaults)) {
-               pr_debug("draining retry fault, drop fault 0x%llx\n", addr);
+               pr_debug("page fault handling disabled, drop fault 0x%llx\n", addr);
                r = 0;
                goto out;
        }
 
+       node = kfd_node_by_irq_ids(adev, node_id, vmid);
+       if (!node) {
+               pr_debug("kfd node does not exist node_id: %d, vmid: %d\n", node_id,
+                        vmid);
+               r = -EFAULT;
+               goto out;
+       }
+
+       if (kfd_process_gpuid_from_node(p, node, &gpuid, &gpuidx)) {
+               pr_debug("failed to get gpuid/gpuidex for node_id: %d\n", node_id);
+               r = -EFAULT;
+               goto out;
+       }
+
+       /* check if this page fault time stamp is before svms->checkpoint_ts */
+       if (svms->checkpoint_ts[gpuidx] != 0) {
+               if (amdgpu_ih_ts_after(ts,  svms->checkpoint_ts[gpuidx])) {
+                       pr_debug("draining retry fault, drop fault 0x%llx\n", addr);
+                       r = 0;
+                       goto out;
+               } else
</pre>
    </blockquote>
                            } else {<br>
    <blockquote type="cite" cite="mid:20240821220133.25975-1-xiaogang.chen@amd.com">
      <pre class="moz-quote-pre" wrap="">
+                       /* ts is after svms->checkpoint_ts now, reset svms->checkpoint_ts
+                        * to zero to avoid following ts wrap around give wrong comparing
+                        */
+                       svms->checkpoint_ts[gpuidx] = 0;</pre>
    </blockquote>
                           }<br>
    <blockquote type="cite" cite="mid:20240821220133.25975-1-xiaogang.chen@amd.com">
      <pre class="moz-quote-pre" wrap="">
+       }
+
        if (!p->xnack_enabled) {
                pr_debug("XNACK not enabled for pasid 0x%x\n", pasid);
                r = -EFAULT;
@@ -2955,13 +2992,6 @@ svm_range_restore_pages(struct amdgpu_device *adev, unsigned int pasid,
                goto out;
        }
 
-       node = kfd_node_by_irq_ids(adev, node_id, vmid);
-       if (!node) {
-               pr_debug("kfd node does not exist node_id: %d, vmid: %d\n", node_id,
-                        vmid);
-               r = -EFAULT;
-               goto out;
-       }
        mmap_read_lock(mm);
 retry_write_locked:
        mutex_lock(&svms->lock);
@@ -3176,9 +3206,11 @@ void svm_range_list_fini(struct kfd_process *p)
        /*
         * Ensure no retry fault comes in afterwards, as page fault handler will
         * not find kfd process and take mm lock to recover fault.
+        * stop kfd page fault handing, then wait pending page faults got drained
         */
-       atomic_inc(&p->svms.drain_pagefaults);
+       atomic_set(&p->svms.drain_pagefaults, 1);
        svm_range_drain_retry_fault(&p->svms);
+       atomic_set(&p->svms.drain_pagefaults, 0);</pre>
    </blockquote>
    <pre class="moz-quote-pre" wrap="">atomic_set(&p->svms.drain_pagefaults, 0) is not needed, as p is freed afterwards.

Regards,
Philip
</pre>
    <blockquote type="cite" cite="mid:20240821220133.25975-1-xiaogang.chen@amd.com">
      <pre class="moz-quote-pre" wrap="">
 
        list_for_each_entry_safe(prange, next, &p->svms.list, list) {
                svm_range_unlink(prange);
diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_svm.h b/drivers/gpu/drm/amd/amdkfd/kfd_svm.h
index 747325a2ea89..974bbc6b14c8 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_svm.h
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_svm.h
@@ -174,7 +174,7 @@ int svm_range_vram_node_new(struct kfd_node *node, struct svm_range *prange,
                            bool clear);
 void svm_range_vram_node_free(struct svm_range *prange);
 int svm_range_restore_pages(struct amdgpu_device *adev, unsigned int pasid,
-                           uint32_t vmid, uint32_t node_id, uint64_t addr,
+                           uint32_t vmid, uint32_t node_id, uint64_t addr, uint64_t ts,
                            bool write_fault);
 int svm_range_schedule_evict_svm_bo(struct amdgpu_amdkfd_fence *fence);
 void svm_range_add_list_work(struct svm_range_list *svms,
</pre>
    </blockquote>
  </body>
</html>