Skip to contentSkip to conversation

drm/amdgpu: flush TLB if valid PDE turns into PTE

PATCH Superseded Philip Yang 4 participants 12 messages May 12, 2021 – May 14, 2021

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

PATCH 1/1 drm/amdgpu: flush TLB if valid PDE turns into PTE

Mapping huge page, 2MB aligned address with 2MB size, uses PDE0 as PTE. If previously valid PDE0, PDE0.V=1 and PDE0.P=0 turns into PTE, this requires TLB flush, otherwise page table walker will not read updated PDE0.

Change page table update mapping to return free_table flag to indicate the previously valid PDE may have turned into a PTE if page table is freed.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 17 +++++++++++------
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h |  2 +-
 drivers/gpu/drm/amd/amdkfd/kfd_svm.c   | 12 ++++++++++--
 3 files changed, 22 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +11 -6
index 3dcdcc9ff522..27418f9407f1 100644
@@ -1457,7 +1457,7 @@ static void amdgpu_vm_fragment(struct amdgpu_vm_update_params *params,
  */
 static int amdgpu_vm_update_ptes(struct amdgpu_vm_update_params *params,
 				 uint64_t start, uint64_t end,
-				 uint64_t dst, uint64_t flags)
+				 uint64_t dst, uint64_t flags, bool *free_table)
 {
 	struct amdgpu_device *adev = params->adev;
 	struct amdgpu_vm_pt_cursor cursor;
@@ -1583,6 +1583,8 @@ static int amdgpu_vm_update_ptes(struct amdgpu_vm_update_params *params,
 			while (cursor.pfn < frag_start) {
 				amdgpu_vm_free_pts(adev, params->vm, &cursor);
 				amdgpu_vm_pt_next(adev, &cursor);
+				if (free_table)
+					*free_table = true;
 			}
 
 		} else if (frag >= shift) {
@@ -1610,6 +1612,7 @@ static int amdgpu_vm_update_ptes(struct amdgpu_vm_update_params *params,
  * @nodes: array of drm_mm_nodes with the MC addresses
  * @pages_addr: DMA addresses to use for mapping
  * @fence: optional resulting fence
+ * @free_table: return true if page table is freed
  *
  * Fill in the page table entries between @start and @last.
  *
@@ -1624,7 +1627,8 @@ int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
 				uint64_t flags, uint64_t offset,
 				struct drm_mm_node *nodes,
 				dma_addr_t *pages_addr,
-				struct dma_fence **fence)
+				struct dma_fence **fence,
+				bool *free_table)
 {
 	struct amdgpu_vm_update_params params;
 	enum amdgpu_sync_mode sync_mode;
@@ -1721,7 +1725,8 @@ int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
 		}
 
 		tmp = start + num_entries;
-		r = amdgpu_vm_update_ptes(&params, start, tmp, addr, flags);
+		r = amdgpu_vm_update_ptes(&params, start, tmp, addr, flags,
+					  free_table);
 		if (r)
 			goto error_unlock;
 
@@ -1879,7 +1884,7 @@ int amdgpu_vm_bo_update(struct amdgpu_device *adev, struct amdgpu_bo_va *bo_va,
 						resv, mapping->start,
 						mapping->last, update_flags,
 						mapping->offset, nodes,
-						pages_addr, last_update);
+						pages_addr, last_update, NULL);
 		if (r)
 			return r;
 	}
@@ -2090,7 +2095,7 @@ int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
 		r = amdgpu_vm_bo_update_mapping(adev, adev, vm, false, false,
 						resv, mapping->start,
 						mapping->last, init_pte_value,
-						0, NULL, NULL, &f);
+						0, NULL, NULL, &f, NULL);
 		amdgpu_vm_free_mapping(adev, vm, mapping, f);
 		if (r) {
 			dma_fence_put(f);
@@ -3428,7 +3433,7 @@ bool amdgpu_vm_handle_fault(struct amdgpu_device *adev, u32 pasid,
 	}
 
 	r = amdgpu_vm_bo_update_mapping(adev, adev, vm, true, false, NULL, addr,
-					addr, flags, value, NULL, NULL,
+					addr, flags, value, NULL, NULL, NULL,
 					NULL);
 	if (r)
 		goto error_unlock;
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +1 -1
index ea60ec122b51..f61c20b70b79 100644
@@ -404,7 +404,7 @@ int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
 				uint64_t flags, uint64_t offset,
 				struct drm_mm_node *nodes,
 				dma_addr_t *pages_addr,
-				struct dma_fence **fence);
+				struct dma_fence **fence, bool *free_table);
 int amdgpu_vm_bo_update(struct amdgpu_device *adev,
 			struct amdgpu_bo_va *bo_va,
 			bool clear);
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +10 -2
index b665e9ff77e3..4f28052d44bf 100644
@@ -1084,7 +1084,7 @@ svm_range_unmap_from_gpu(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 
 	return amdgpu_vm_bo_update_mapping(adev, adev, vm, false, true, NULL,
 					   start, last, init_pte_value, 0,
-					   NULL, NULL, fence);
+					   NULL, NULL, fence, NULL);
 }
 
 static int
@@ -1137,12 +1137,15 @@ svm_range_map_to_gpu(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 		     struct amdgpu_device *bo_adev, struct dma_fence **fence)
 {
 	struct amdgpu_bo_va bo_va;
+	bool free_table = false;
+	struct kfd_process *p;
 	uint64_t pte_flags;
 	int r = 0;
 
 	pr_debug("svms 0x%p [0x%lx 0x%lx]\n", prange->svms, prange->start,
 		 prange->last);
 
+	p = container_of(prange->svms, struct kfd_process, svms);
 	if (prange->svm_bo && prange->ttm_res) {
 		bo_va.is_xgmi = amdgpu_xgmi_same_hive(adev, bo_adev);
 		prange->mapping.bo_va = &bo_va;
@@ -1159,7 +1162,8 @@ svm_range_map_to_gpu(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 					prange->mapping.offset,
 					prange->ttm_res ?
 						prange->ttm_res->mm_node : NULL,
-					dma_addr, &vm->last_update);
+					dma_addr, &vm->last_update,
+					&free_table);
 	if (r) {
 		pr_debug("failed %d to map to gpu 0x%lx\n", r, prange->start);
 		goto out;
@@ -1175,6 +1179,10 @@ svm_range_map_to_gpu(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 	if (fence)
 		*fence = dma_fence_get(vm->last_update);
 
+	if (free_table)
+		amdgpu_amdkfd_flush_gpu_tlb_pasid((struct kgd_dev *)adev,
+						  p->pasid);
+
 out:
 	prange->mapping.bo_va = NULL;
 	return r;
Signature · 6 lines
2.17.1

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

Am 12.05.21 um 14:34 schrieb Philip Yang:

Quoted 21 lines

Mapping huge page, 2MB aligned address with 2MB size, uses PDE0 as PTE. If previously valid PDE0, PDE0.V=1 and PDE0.P=0 turns into PTE, this requires TLB flush, otherwise page table walker will not read updated PDE0.

Change page table update mapping to return free_table flag to indicate the previously valid PDE may have turned into a PTE if page table is freed.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 17 +++++++++++------
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h |  2 +-
  drivers/gpu/drm/amd/amdkfd/kfd_svm.c   | 12 ++++++++++--
  3 files changed, 22 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -0
index 3dcdcc9ff522..27418f9407f1 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c static void amdgpu_vm_fragment(struct amdgpu_vm_update_params *params, · line 1460

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -1
@@ -1457,7 +1457,7 @@ static void amdgpu_vm_fragment(struct amdgpu_vm_update_params *params,
   */
  static int amdgpu_vm_update_ptes(struct amdgpu_vm_update_params *params,
  				 uint64_t start, uint64_t end,
-				 uint64_t dst, uint64_t flags)
+				 uint64_t dst, uint64_t flags, bool *free_table)

Please put that flag into the params structure instead.

Christian.

Quoted 120 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +10 -5
  {
  	struct amdgpu_device *adev = params->adev;
  	struct amdgpu_vm_pt_cursor cursor;
@@ -1583,6 +1583,8 @@ static int amdgpu_vm_update_ptes(struct amdgpu_vm_update_params *params,
  			while (cursor.pfn < frag_start) {
  				amdgpu_vm_free_pts(adev, params->vm, &cursor);
  				amdgpu_vm_pt_next(adev, &cursor);
+				if (free_table)
+					*free_table = true;
  			}
 
  		} else if (frag >= shift) {
@@ -1610,6 +1612,7 @@ static int amdgpu_vm_update_ptes(struct amdgpu_vm_update_params *params,
   * @nodes: array of drm_mm_nodes with the MC addresses
   * @pages_addr: DMA addresses to use for mapping
   * @fence: optional resulting fence
+ * @free_table: return true if page table is freed
   *
   * Fill in the page table entries between @start and @last.
   *
@@ -1624,7 +1627,8 @@ int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
  				uint64_t flags, uint64_t offset,
  				struct drm_mm_node *nodes,
  				dma_addr_t *pages_addr,
-				struct dma_fence **fence)
+				struct dma_fence **fence,
+				bool *free_table)
  {
  	struct amdgpu_vm_update_params params;
  	enum amdgpu_sync_mode sync_mode;
@@ -1721,7 +1725,8 @@ int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
  		}
 
  		tmp = start + num_entries;
-		r = amdgpu_vm_update_ptes(&params, start, tmp, addr, flags);
+		r = amdgpu_vm_update_ptes(&params, start, tmp, addr, flags,
+					  free_table);
  		if (r)
  			goto error_unlock;
 
@@ -1879,7 +1884,7 @@ int amdgpu_vm_bo_update(struct amdgpu_device *adev, struct amdgpu_bo_va *bo_va,
  						resv, mapping->start,
  						mapping->last, update_flags,
  						mapping->offset, nodes,
-						pages_addr, last_update);
+						pages_addr, last_update, NULL);
  		if (r)
  			return r;
  	}
@@ -2090,7 +2095,7 @@ int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
  		r = amdgpu_vm_bo_update_mapping(adev, adev, vm, false, false,
  						resv, mapping->start,
  						mapping->last, init_pte_value,
-						0, NULL, NULL, &f);
+						0, NULL, NULL, &f, NULL);
  		amdgpu_vm_free_mapping(adev, vm, mapping, f);
  		if (r) {
  			dma_fence_put(f);
@@ -3428,7 +3433,7 @@ bool amdgpu_vm_handle_fault(struct amdgpu_device *adev, u32 pasid,
  	}
 
  	r = amdgpu_vm_bo_update_mapping(adev, adev, vm, true, false, NULL, addr,
-					addr, flags, value, NULL, NULL,
+					addr, flags, value, NULL, NULL, NULL,
  					NULL);
  	if (r)
  		goto error_unlock;
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +1 -1
index ea60ec122b51..f61c20b70b79 100644
@@ -404,7 +404,7 @@ int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
  				uint64_t flags, uint64_t offset,
  				struct drm_mm_node *nodes,
  				dma_addr_t *pages_addr,
-				struct dma_fence **fence);
+				struct dma_fence **fence, bool *free_table);
  int amdgpu_vm_bo_update(struct amdgpu_device *adev,
  			struct amdgpu_bo_va *bo_va,
  			bool clear);
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +6 -2
index b665e9ff77e3..4f28052d44bf 100644
@@ -1084,7 +1084,7 @@ svm_range_unmap_from_gpu(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 
  	return amdgpu_vm_bo_update_mapping(adev, adev, vm, false, true, NULL,
  					   start, last, init_pte_value, 0,
-					   NULL, NULL, fence);
+					   NULL, NULL, fence, NULL);
  }
 
  static int
@@ -1137,12 +1137,15 @@ svm_range_map_to_gpu(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  		     struct amdgpu_device *bo_adev, struct dma_fence **fence)
  {
  	struct amdgpu_bo_va bo_va;
+	bool free_table = false;
+	struct kfd_process *p;
  	uint64_t pte_flags;
  	int r = 0;
 
  	pr_debug("svms 0x%p [0x%lx 0x%lx]\n", prange->svms, prange->start,
  		 prange->last);
 
+	p = container_of(prange->svms, struct kfd_process, svms);
  	if (prange->svm_bo && prange->ttm_res) {
  		bo_va.is_xgmi = amdgpu_xgmi_same_hive(adev, bo_adev);
  		prange->mapping.bo_va = &bo_va;
@@ -1159,7 +1162,8 @@ svm_range_map_to_gpu(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  					prange->mapping.offset,
  					prange->ttm_res ?
  						prange->ttm_res->mm_node : NULL,
-					dma_addr, &vm->last_update);
+					dma_addr, &vm->last_update,
+					&free_table);
  	if (r) {
  		pr_debug("failed %d to map to gpu 0x%lx\n", r, prange->start);
  		goto out;

drivers/gpu/drm/amd/amdkfd/kfd_svm.c svm_range_map_to_gpu(struct amdgpu_device *adev, struct amdgpu_vm *vm, · line 1188

Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +4 -0
@@ -1175,6 +1179,10 @@ svm_range_map_to_gpu(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  	if (fence)
  		*fence = dma_fence_get(vm->last_update);
 
+	if (free_table)
+		amdgpu_amdkfd_flush_gpu_tlb_pasid((struct kgd_dev *)adev,
+						  p->pasid);
+
  out:
  	prange->mapping.bo_va = NULL;
  	return r;

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

maintainer
#2 lore raw

Am 2021-05-12 um 8:38 a.m. schrieb Christian König:

Quoted 35 lines

Am 12.05.21 um 14:34 schrieb Philip Yang:

Quoted 24 lines

Mapping huge page, 2MB aligned address with 2MB size, uses PDE0 as PTE. If previously valid PDE0, PDE0.V=1 and PDE0.P=0 turns into PTE, this requires TLB flush, otherwise page table walker will not read updated PDE0.

Change page table update mapping to return free_table flag to indicate the previously valid PDE may have turned into a PTE if page table is freed.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 17 +++++++++++------ drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h |  2 +- drivers/gpu/drm/amd/amdkfd/kfd_svm.c   | 12 ++++++++++--

  3 files changed, 22 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -0
index 3dcdcc9ff522..27418f9407f1 100644
@@ -1457,7 +1457,7 @@ static void amdgpu_vm_fragment(struct
 amdgpu_vm_update_params *params,
    */
Quoted 5 lines

  static int amdgpu_vm_update_ptes(struct amdgpu_vm_update_params *params,
                   uint64_t start, uint64_t end,
-                 uint64_t dst, uint64_t flags)
+                 uint64_t dst, uint64_t flags, bool *free_table)

Please put that flag into the params structure instead.

Quoted 1 line

Christian.

I agree. Christian, I think we also need to keep track of this for graphics command submissions. amdgpu_cs_vm_handling needs get this flag from amdgpu_vm_bo_update, and amdgpu_cs_submit needs to update job->vm_needs_flushed based on this.

Regards,
  Felix

Quoted 136 lines
Quoted 136 lines

  {
      struct amdgpu_device *adev = params->adev;
      struct amdgpu_vm_pt_cursor cursor;

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +3 -0
@@ -1583,6 +1583,8 @@ static int amdgpu_vm_update_ptes(struct
 amdgpu_vm_update_params *params,
               while (cursor.pfn < frag_start) {
                   amdgpu_vm_free_pts(adev, params->vm, &cursor);
                   amdgpu_vm_pt_next(adev, &cursor);
+                if (free_table)
+                    *free_table = true;
               }
             } else if (frag >= shift) {
@@ -1610,6 +1612,7 @@ static int amdgpu_vm_update_ptes(struct
 amdgpu_vm_update_params *params,
    * @nodes: array of drm_mm_nodes with the MC addresses
    * @pages_addr: DMA addresses to use for mapping
    * @fence: optional resulting fence
+ * @free_table: return true if page table is freed
    *
    * Fill in the page table entries between @start and @last.

   *

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2 -1
@@ -1624,7 +1627,8 @@ int amdgpu_vm_bo_update_mapping(struct
 amdgpu_device *adev,
                   uint64_t flags, uint64_t offset,
                   struct drm_mm_node *nodes,
                   dma_addr_t *pages_addr,
-                struct dma_fence **fence)
+                struct dma_fence **fence,
+                bool *free_table)
   {
       struct amdgpu_vm_update_params params;

      enum amdgpu_sync_mode sync_mode;

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2 -1
@@ -1721,7 +1725,8 @@ int amdgpu_vm_bo_update_mapping(struct
 amdgpu_device *adev,
           }
             tmp = start + num_entries;
-        r = amdgpu_vm_update_ptes(&params, start, tmp, addr, flags);
+        r = amdgpu_vm_update_ptes(&params, start, tmp, addr, flags,
+                      free_table);
           if (r)
               goto error_unlock;

  @@ -1879,7 +1884,7 @@ int amdgpu_vm_bo_update(struct amdgpu_device *adev, struct amdgpu_bo_va *bo_va,
                          resv, mapping->start, mapping->last, update_flags, mapping->offset, nodes,
-                        pages_addr, last_update);
+                        pages_addr, last_update, NULL); if (r)
              return r;
      }

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -1
@@ -2090,7 +2095,7 @@ int amdgpu_vm_clear_freed(struct amdgpu_device
 *adev,
           r = amdgpu_vm_bo_update_mapping(adev, adev, vm, false, false,
                           resv, mapping->start,
                           mapping->last, init_pte_value,
-                        0, NULL, NULL, &f);
+                        0, NULL, NULL, &f, NULL);
           amdgpu_vm_free_mapping(adev, vm, mapping, f);
           if (r) {

              dma_fence_put(f);

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -1
@@ -3428,7 +3433,7 @@ bool amdgpu_vm_handle_fault(struct
 amdgpu_device *adev, u32 pasid,
       }
         r = amdgpu_vm_bo_update_mapping(adev, adev, vm, true, false,
 NULL, addr,
-                    addr, flags, value, NULL, NULL,
+                    addr, flags, value, NULL, NULL, NULL,
                       NULL);
       if (r)

          goto error_unlock;

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +1 -1
index ea60ec122b51..f61c20b70b79 100644
@@ -404,7 +404,7 @@ int amdgpu_vm_bo_update_mapping(struct
 amdgpu_device *adev,
                   uint64_t flags, uint64_t offset,
                   struct drm_mm_node *nodes,
                   dma_addr_t *pages_addr,
-                struct dma_fence **fence);
+                struct dma_fence **fence, bool *free_table);
   int amdgpu_vm_bo_update(struct amdgpu_device *adev,
               struct amdgpu_bo_va *bo_va,

              bool clear);

drivers/gpu/drm/amd/amdkfd/kfd_svm.c +1 -1
index b665e9ff77e3..4f28052d44bf 100644
@@ -1084,7 +1084,7 @@ svm_range_unmap_from_gpu(struct amdgpu_device
 *adev, struct amdgpu_vm *vm,
         return amdgpu_vm_bo_update_mapping(adev, adev, vm, false,
 true, NULL,
                          start, last, init_pte_value, 0,
-                       NULL, NULL, fence);
+                       NULL, NULL, fence, NULL);
   }

    static int

drivers/gpu/drm/amd/amdkfd/kfd_svm.c +2 -0
@@ -1137,12 +1137,15 @@ svm_range_map_to_gpu(struct amdgpu_device
 *adev, struct amdgpu_vm *vm,
                struct amdgpu_device *bo_adev, struct dma_fence **fence)
   {
       struct amdgpu_bo_va bo_va;
+    bool free_table = false;
+    struct kfd_process *p;
       uint64_t pte_flags;
       int r = 0;
         pr_debug("svms 0x%p [0x%lx 0x%lx]\n", prange->svms,
 prange->start,
            prange->last);
   +    p = container_of(prange->svms, struct kfd_process, svms);
       if (prange->svm_bo && prange->ttm_res) {
           bo_va.is_xgmi = amdgpu_xgmi_same_hive(adev, bo_adev);

          prange->mapping.bo_va = &bo_va;

drivers/gpu/drm/amd/amdkfd/kfd_svm.c +0 -0
@@ -1159,7 +1162,8 @@ svm_range_map_to_gpu(struct amdgpu_device
 *adev, struct amdgpu_vm *vm,
                       prange->mapping.offset,

                      prange->ttm_res ? prange->ttm_res->mm_node : NULL,
-                    dma_addr, &vm->last_update);
+                    dma_addr, &vm->last_update,
+                    &free_table);
      if (r) {
          pr_debug("failed %d to map to gpu 0x%lx\n", r, prange->start); goto out;

drivers/gpu/drm/amd/amdkfd/kfd_svm.c +3 -0
@@ -1175,6 +1179,10 @@ svm_range_map_to_gpu(struct amdgpu_device
 *adev, struct amdgpu_vm *vm,
       if (fence)
           *fence = dma_fence_get(vm->last_update);
   +    if (free_table)
+        amdgpu_amdkfd_flush_gpu_tlb_pasid((struct kgd_dev *)adev,
+                          p->pasid);
+
Quoted 8 lines
Quoted 3 lines

  out:
      prange->mapping.bo_va = NULL;
      return r;

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

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

maintainer
#4 lore raw

Am 2021-05-12 um 2:40 p.m. schrieb philip yang:

Quoted 42 lines · Felix Kuehling
Quoted 33 lines

Am 2021-05-12 um 8:38 a.m. schrieb Christian König:

Quoted 32 lines

Am 12.05.21 um 14:34 schrieb Philip Yang:

Quoted 24 lines

Mapping huge page, 2MB aligned address with 2MB size, uses PDE0 as PTE. If previously valid PDE0, PDE0.V=1 and PDE0.P=0 turns into PTE, this requires TLB flush, otherwise page table walker will not read updated PDE0.

Change page table update mapping to return free_table flag to indicate the previously valid PDE may have turned into a PTE if page table is freed.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 17 +++++++++++------ drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h |  2 +- drivers/gpu/drm/amd/amdkfd/kfd_svm.c   | 12 ++++++++++--

  3 files changed, 22 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -0
index 3dcdcc9ff522..27418f9407f1 100644
@@ -1457,7 +1457,7 @@ static void amdgpu_vm_fragment(struct
 amdgpu_vm_update_params *params,
    */
Quoted 5 lines

  static int amdgpu_vm_update_ptes(struct amdgpu_vm_update_params *params,
                   uint64_t start, uint64_t end,
-                 uint64_t dst, uint64_t flags)
+                 uint64_t dst, uint64_t flags, bool *free_table)

Please put that flag into the params structure instead.

Quoted 5 lines
Quoted 1 line

Christian.

I agree. Christian, I think we also need to keep track of this for graphics command submissions. amdgpu_cs_vm_handling needs get this flag from amdgpu_vm_bo_update, and amdgpu_cs_submit needs to update job->vm_needs_flushed based on this.

Quoted 5 lines

amdgpu_vm_bo_update to pass NULL to ignore the free_table return flag.

This new flag only used by SVM APIs. Old KFD and graphics command submission path is not changed.

Understood. I'm pointing out that the same problem we're fixing for SVM may also affect graphics. So a follow-up patch may want to implement this conditional TLB flush for graphics command submissions as well. That would mean adding a "free_table" output parameter to amdgpu_vm_bo_update as well, and handling it in the amdgpu_cs code.

Regards,
  Felix

Quoted 150 lines

Thanks,

Philip

Quoted 146 lines

Regards,
  Felix

Quoted 142 lines
Quoted 135 lines

  {
      struct amdgpu_device *adev = params->adev;
      struct amdgpu_vm_pt_cursor cursor;

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +3 -0
@@ -1583,6 +1583,8 @@ static int amdgpu_vm_update_ptes(struct
 amdgpu_vm_update_params *params,
               while (cursor.pfn < frag_start) {
                   amdgpu_vm_free_pts(adev, params->vm, &cursor);
                   amdgpu_vm_pt_next(adev, &cursor);
+                if (free_table)
+                    *free_table = true;
               }
             } else if (frag >= shift) {
@@ -1610,6 +1612,7 @@ static int amdgpu_vm_update_ptes(struct
 amdgpu_vm_update_params *params,
    * @nodes: array of drm_mm_nodes with the MC addresses
    * @pages_addr: DMA addresses to use for mapping
    * @fence: optional resulting fence
+ * @free_table: return true if page table is freed
    *
    * Fill in the page table entries between @start and @last.

   *

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2 -1
@@ -1624,7 +1627,8 @@ int amdgpu_vm_bo_update_mapping(struct
 amdgpu_device *adev,
                   uint64_t flags, uint64_t offset,
                   struct drm_mm_node *nodes,
                   dma_addr_t *pages_addr,
-                struct dma_fence **fence)
+                struct dma_fence **fence,
+                bool *free_table)
   {
       struct amdgpu_vm_update_params params;

      enum amdgpu_sync_mode sync_mode;

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2 -1
@@ -1721,7 +1725,8 @@ int amdgpu_vm_bo_update_mapping(struct
 amdgpu_device *adev,
           }
             tmp = start + num_entries;
-        r = amdgpu_vm_update_ptes(&params, start, tmp, addr, flags);
+        r = amdgpu_vm_update_ptes(&params, start, tmp, addr, flags,
+                      free_table);
           if (r)
               goto error_unlock;

  @@ -1879,7 +1884,7 @@ int amdgpu_vm_bo_update(struct amdgpu_device *adev, struct amdgpu_bo_va *bo_va,
                          resv, mapping->start, mapping->last, update_flags, mapping->offset, nodes,
-                        pages_addr, last_update);
+                        pages_addr, last_update, NULL); if (r)
              return r;
      }

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -1
@@ -2090,7 +2095,7 @@ int amdgpu_vm_clear_freed(struct amdgpu_device
 *adev,
           r = amdgpu_vm_bo_update_mapping(adev, adev, vm, false, false,
                           resv, mapping->start,
                           mapping->last, init_pte_value,
-                        0, NULL, NULL, &f);
+                        0, NULL, NULL, &f, NULL);
           amdgpu_vm_free_mapping(adev, vm, mapping, f);
           if (r) {

              dma_fence_put(f);

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -1
@@ -3428,7 +3433,7 @@ bool amdgpu_vm_handle_fault(struct
 amdgpu_device *adev, u32 pasid,
       }
         r = amdgpu_vm_bo_update_mapping(adev, adev, vm, true, false,
 NULL, addr,
-                    addr, flags, value, NULL, NULL,
+                    addr, flags, value, NULL, NULL, NULL,
                       NULL);
       if (r)

          goto error_unlock;

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +1 -1
index ea60ec122b51..f61c20b70b79 100644
@@ -404,7 +404,7 @@ int amdgpu_vm_bo_update_mapping(struct
 amdgpu_device *adev,
                   uint64_t flags, uint64_t offset,
                   struct drm_mm_node *nodes,
                   dma_addr_t *pages_addr,
-                struct dma_fence **fence);
+                struct dma_fence **fence, bool *free_table);
   int amdgpu_vm_bo_update(struct amdgpu_device *adev,
               struct amdgpu_bo_va *bo_va,

              bool clear);

drivers/gpu/drm/amd/amdkfd/kfd_svm.c +1 -1
index b665e9ff77e3..4f28052d44bf 100644
@@ -1084,7 +1084,7 @@ svm_range_unmap_from_gpu(struct amdgpu_device
 *adev, struct amdgpu_vm *vm,
         return amdgpu_vm_bo_update_mapping(adev, adev, vm, false,
 true, NULL,
                          start, last, init_pte_value, 0,
-                       NULL, NULL, fence);
+                       NULL, NULL, fence, NULL);
   }

    static int

drivers/gpu/drm/amd/amdkfd/kfd_svm.c +2 -0
@@ -1137,12 +1137,15 @@ svm_range_map_to_gpu(struct amdgpu_device
 *adev, struct amdgpu_vm *vm,
                struct amdgpu_device *bo_adev, struct dma_fence **fence)
   {
       struct amdgpu_bo_va bo_va;
+    bool free_table = false;
+    struct kfd_process *p;
       uint64_t pte_flags;
       int r = 0;
         pr_debug("svms 0x%p [0x%lx 0x%lx]\n", prange->svms,
 prange->start,
            prange->last);
   +    p = container_of(prange->svms, struct kfd_process, svms);
       if (prange->svm_bo && prange->ttm_res) {
           bo_va.is_xgmi = amdgpu_xgmi_same_hive(adev, bo_adev);

          prange->mapping.bo_va = &bo_va;

drivers/gpu/drm/amd/amdkfd/kfd_svm.c +0 -0
@@ -1159,7 +1162,8 @@ svm_range_map_to_gpu(struct amdgpu_device
 *adev, struct amdgpu_vm *vm,
                       prange->mapping.offset,

                      prange->ttm_res ? prange->ttm_res->mm_node : NULL,
-                    dma_addr, &vm->last_update);
+                    dma_addr, &vm->last_update,
+                    &free_table);
      if (r) {
          pr_debug("failed %d to map to gpu 0x%lx\n", r, prange->start); goto out;

drivers/gpu/drm/amd/amdkfd/kfd_svm.c +3 -0
@@ -1175,6 +1179,10 @@ svm_range_map_to_gpu(struct amdgpu_device
 *adev, struct amdgpu_vm *vm,
       if (fence)
           *fence = dma_fence_get(vm->last_update);
   +    if (free_table)
+        amdgpu_amdkfd_flush_gpu_tlb_pasid((struct kgd_dev *)adev,
+                          p->pasid);
+
Quoted 3 lines

  out:
      prange->mapping.bo_va = NULL;
      return r;

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

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

#5 lore raw
In reply to philip yang

Am 12.05.21 um 20:40 schrieb philip yang:

Quoted 37 lines · Felix Kuehling
Quoted 35 lines

Am 2021-05-12 um 8:38 a.m. schrieb Christian König:

Quoted 33 lines

Am 12.05.21 um 14:34 schrieb Philip Yang:

Quoted 22 lines

Mapping huge page, 2MB aligned address with 2MB size, uses PDE0 as PTE. If previously valid PDE0, PDE0.V=1 and PDE0.P=0 turns into PTE, this requires TLB flush, otherwise page table walker will not read updated PDE0.

Change page table update mapping to return free_table flag to indicate the previously valid PDE may have turned into a PTE if page table is freed.

---
   drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 17 +++++++++++------
   drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h |  2 +-
   drivers/gpu/drm/amd/amdkfd/kfd_svm.c   | 12 ++++++++++--
   3 files changed, 22 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -0
index 3dcdcc9ff522..27418f9407f1 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c static void amdgpu_vm_fragment(struct · line 1462

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -1
@@ -1457,7 +1457,7 @@ static void amdgpu_vm_fragment(struct
 amdgpu_vm_update_params *params,
    */
   static int amdgpu_vm_update_ptes(struct amdgpu_vm_update_params
 *params,
                    uint64_t start, uint64_t end,
-                 uint64_t dst, uint64_t flags)
+                 uint64_t dst, uint64_t flags, bool *free_table)

Please put that flag into the params structure instead.

Christian.

Quoted 9 lines
Quoted 4 lines

I agree. Christian, I think we also need to keep track of this for graphics command submissions. amdgpu_cs_vm_handling needs get this flag from amdgpu_vm_bo_update, and amdgpu_cs_submit needs to update job->vm_needs_flushed based on this.

amdgpu_vm_bo_update to pass NULL to ignore the free_table return flag.

This new flag only used by SVM APIs. Old KFD and graphics command submission path is not changed.

In the CS path we always flush, no matter if it is a mapping or unmapping operation.

But we should still move this into the parmeters structure instead. It is less overhead than the if(ptr) *p = true;

Christian.

Quoted 150 lines

Thanks,

Philip

Quoted 146 lines

Regards,
  Felix

Quoted 142 lines
Quoted 128 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -0
   {

      struct amdgpu_device *adev = params->adev;
      struct amdgpu_vm_pt_cursor cursor;

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +3 -0
@@ -1583,6 +1583,8 @@ static int amdgpu_vm_update_ptes(struct
 amdgpu_vm_update_params *params,
               while (cursor.pfn < frag_start) {
                   amdgpu_vm_free_pts(adev, params->vm, &cursor);
                   amdgpu_vm_pt_next(adev, &cursor);
+                if (free_table)
+                    *free_table = true;
               }
             } else if (frag >= shift) {
@@ -1610,6 +1612,7 @@ static int amdgpu_vm_update_ptes(struct
 amdgpu_vm_update_params *params,
    * @nodes: array of drm_mm_nodes with the MC addresses
    * @pages_addr: DMA addresses to use for mapping
    * @fence: optional resulting fence
+ * @free_table: return true if page table is freed
    *
    * Fill in the page table entries between @start and @last.

   *

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2 -1
@@ -1624,7 +1627,8 @@ int amdgpu_vm_bo_update_mapping(struct
 amdgpu_device *adev,
                   uint64_t flags, uint64_t offset,
                   struct drm_mm_node *nodes,
                   dma_addr_t *pages_addr,
-                struct dma_fence **fence)
+                struct dma_fence **fence,
+                bool *free_table)
   {
       struct amdgpu_vm_update_params params;

      enum amdgpu_sync_mode sync_mode;

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2 -1
@@ -1721,7 +1725,8 @@ int amdgpu_vm_bo_update_mapping(struct
 amdgpu_device *adev,
           }
             tmp = start + num_entries;
-        r = amdgpu_vm_update_ptes(&params, start, tmp, addr, flags);
+        r = amdgpu_vm_update_ptes(&params, start, tmp, addr, flags,
+                      free_table);
           if (r)
               goto error_unlock;
   @@ -1879,7 +1884,7 @@ int amdgpu_vm_bo_update(struct amdgpu_device

*adev, struct amdgpu_bo_va *bo_va,
                          resv, mapping->start, mapping->last, update_flags, mapping->offset, nodes,
-                        pages_addr, last_update); +                        pages_addr, last_update, NULL);
          if (r)
              return r;
      }

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -1
@@ -2090,7 +2095,7 @@ int amdgpu_vm_clear_freed(struct amdgpu_device
 *adev,
           r = amdgpu_vm_bo_update_mapping(adev, adev, vm, false, false,
                           resv, mapping->start,
                           mapping->last, init_pte_value,
-                        0, NULL, NULL, &f);
+                        0, NULL, NULL, &f, NULL);
           amdgpu_vm_free_mapping(adev, vm, mapping, f);
           if (r) {

              dma_fence_put(f);

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -1
@@ -3428,7 +3433,7 @@ bool amdgpu_vm_handle_fault(struct
 amdgpu_device *adev, u32 pasid,
       }
         r = amdgpu_vm_bo_update_mapping(adev, adev, vm, true, false,
 NULL, addr,
-                    addr, flags, value, NULL, NULL,
+                    addr, flags, value, NULL, NULL, NULL,
                       NULL);
       if (r)

          goto error_unlock;

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +1 -1
index ea60ec122b51..f61c20b70b79 100644
@@ -404,7 +404,7 @@ int amdgpu_vm_bo_update_mapping(struct
 amdgpu_device *adev,
                   uint64_t flags, uint64_t offset,
                   struct drm_mm_node *nodes,
                   dma_addr_t *pages_addr,
-                struct dma_fence **fence);
+                struct dma_fence **fence, bool *free_table);
   int amdgpu_vm_bo_update(struct amdgpu_device *adev,
               struct amdgpu_bo_va *bo_va,

              bool clear);

drivers/gpu/drm/amd/amdkfd/kfd_svm.c +5 -2
index b665e9ff77e3..4f28052d44bf 100644
@@ -1084,7 +1084,7 @@ svm_range_unmap_from_gpu(struct amdgpu_device
 *adev, struct amdgpu_vm *vm,
         return amdgpu_vm_bo_update_mapping(adev, adev, vm, false,
 true, NULL,
                          start, last, init_pte_value, 0,
-                       NULL, NULL, fence);
+                       NULL, NULL, fence, NULL);
   }
     static int
@@ -1137,12 +1137,15 @@ svm_range_map_to_gpu(struct amdgpu_device
 *adev, struct amdgpu_vm *vm,
                struct amdgpu_device *bo_adev, struct dma_fence **fence)
   {
       struct amdgpu_bo_va bo_va;
+    bool free_table = false;
+    struct kfd_process *p;
       uint64_t pte_flags;
       int r = 0;
         pr_debug("svms 0x%p [0x%lx 0x%lx]\n", prange->svms,
 prange->start,
            prange->last);
   +    p = container_of(prange->svms, struct kfd_process, svms);
       if (prange->svm_bo && prange->ttm_res) {
           bo_va.is_xgmi = amdgpu_xgmi_same_hive(adev, bo_adev);
           prange->mapping.bo_va = &bo_va;
@@ -1159,7 +1162,8 @@ svm_range_map_to_gpu(struct amdgpu_device
 *adev, struct amdgpu_vm *vm,
                       prange->mapping.offset,
                       prange->ttm_res ?
                           prange->ttm_res->mm_node : NULL,
-                    dma_addr, &vm->last_update);
+                    dma_addr, &vm->last_update,
+                    &free_table);
       if (r) {
           pr_debug("failed %d to map to gpu 0x%lx\n", r, prange->start);

          goto out;

drivers/gpu/drm/amd/amdkfd/kfd_svm.c svm_range_map_to_gpu(struct amdgpu_device · line 1188

Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +3 -0
@@ -1175,6 +1179,10 @@ svm_range_map_to_gpu(struct amdgpu_device
 *adev, struct amdgpu_vm *vm,
       if (fence)
           *fence = dma_fence_get(vm->last_update);
   +    if (free_table)
+        amdgpu_amdkfd_flush_gpu_tlb_pasid((struct kgd_dev *)adev,
+                          p->pasid);
+
   out:
       prange->mapping.bo_va = NULL;
       return r;

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

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

#6 lore raw

PATCH v2 1/1 drm/amdgpu: flush TLB if valid PDE turns into PTE

In reply to Philip Yang

Mapping huge page, 2MB aligned address with 2MB size, uses PDE0 as PTE. If previously valid PDE0, PDE0.V=1 and PDE0.P=0 turns into PTE, this requires TLB flush, otherwise page table walker will not read updated PDE0.

Change page table update mapping to return free_table flag to indicate the previously valid PDE may have turned into a PTE if page table is freed.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 13 +++++++++----
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h |  7 ++++++-
 drivers/gpu/drm/amd/amdkfd/kfd_svm.c   | 12 ++++++++++--
 3 files changed, 25 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +9 -4
index 3dcdcc9ff522..f9c00b451a3d 100644
@@ -1583,6 +1583,8 @@ static int amdgpu_vm_update_ptes(struct amdgpu_vm_update_params *params,
 			while (cursor.pfn < frag_start) {
 				amdgpu_vm_free_pts(adev, params->vm, &cursor);
 				amdgpu_vm_pt_next(adev, &cursor);
+				if (params->free_table)
+					*params->free_table = true;
 			}
 
 		} else if (frag >= shift) {
@@ -1610,6 +1612,7 @@ static int amdgpu_vm_update_ptes(struct amdgpu_vm_update_params *params,
  * @nodes: array of drm_mm_nodes with the MC addresses
  * @pages_addr: DMA addresses to use for mapping
  * @fence: optional resulting fence
+ * @free_table: return true if page table is freed
  *
  * Fill in the page table entries between @start and @last.
  *
@@ -1624,7 +1627,8 @@ int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
 				uint64_t flags, uint64_t offset,
 				struct drm_mm_node *nodes,
 				dma_addr_t *pages_addr,
-				struct dma_fence **fence)
+				struct dma_fence **fence,
+				bool *free_table)
 {
 	struct amdgpu_vm_update_params params;
 	enum amdgpu_sync_mode sync_mode;
@@ -1637,6 +1641,7 @@ int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
 	params.immediate = immediate;
 	params.pages_addr = pages_addr;
 	params.unlocked = unlocked;
+	params.free_table = free_table;
 
 	/* Implicitly sync to command submissions in the same VM before
 	 * unmapping. Sync to moving fences before mapping.
@@ -1879,7 +1884,7 @@ int amdgpu_vm_bo_update(struct amdgpu_device *adev, struct amdgpu_bo_va *bo_va,
 						resv, mapping->start,
 						mapping->last, update_flags,
 						mapping->offset, nodes,
-						pages_addr, last_update);
+						pages_addr, last_update, NULL);
 		if (r)
 			return r;
 	}
@@ -2090,7 +2095,7 @@ int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
 		r = amdgpu_vm_bo_update_mapping(adev, adev, vm, false, false,
 						resv, mapping->start,
 						mapping->last, init_pte_value,
-						0, NULL, NULL, &f);
+						0, NULL, NULL, &f, NULL);
 		amdgpu_vm_free_mapping(adev, vm, mapping, f);
 		if (r) {
 			dma_fence_put(f);
@@ -3428,7 +3433,7 @@ bool amdgpu_vm_handle_fault(struct amdgpu_device *adev, u32 pasid,
 	}
 
 	r = amdgpu_vm_bo_update_mapping(adev, adev, vm, true, false, NULL, addr,
-					addr, flags, value, NULL, NULL,
+					addr, flags, value, NULL, NULL, NULL,
 					NULL);
 	if (r)
 		goto error_unlock;
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +6 -1
index ea60ec122b51..41a55b035cda 100644
@@ -231,6 +231,11 @@ struct amdgpu_vm_update_params {
 	 * @num_dw_left: number of dw left for the IB
 	 */
 	unsigned int num_dw_left;
+
+	/**
+	 * @free_table: return true if page table is freed when updating
+	 */
+	bool *free_table;
 };
 
 struct amdgpu_vm_update_funcs {
@@ -404,7 +409,7 @@ int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
 				uint64_t flags, uint64_t offset,
 				struct drm_mm_node *nodes,
 				dma_addr_t *pages_addr,
-				struct dma_fence **fence);
+				struct dma_fence **fence, bool *free_table);
 int amdgpu_vm_bo_update(struct amdgpu_device *adev,
 			struct amdgpu_bo_va *bo_va,
 			bool clear);
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +10 -2
index b665e9ff77e3..4f28052d44bf 100644
@@ -1084,7 +1084,7 @@ svm_range_unmap_from_gpu(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 
 	return amdgpu_vm_bo_update_mapping(adev, adev, vm, false, true, NULL,
 					   start, last, init_pte_value, 0,
-					   NULL, NULL, fence);
+					   NULL, NULL, fence, NULL);
 }
 
 static int
@@ -1137,12 +1137,15 @@ svm_range_map_to_gpu(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 		     struct amdgpu_device *bo_adev, struct dma_fence **fence)
 {
 	struct amdgpu_bo_va bo_va;
+	bool free_table = false;
+	struct kfd_process *p;
 	uint64_t pte_flags;
 	int r = 0;
 
 	pr_debug("svms 0x%p [0x%lx 0x%lx]\n", prange->svms, prange->start,
 		 prange->last);
 
+	p = container_of(prange->svms, struct kfd_process, svms);
 	if (prange->svm_bo && prange->ttm_res) {
 		bo_va.is_xgmi = amdgpu_xgmi_same_hive(adev, bo_adev);
 		prange->mapping.bo_va = &bo_va;
@@ -1159,7 +1162,8 @@ svm_range_map_to_gpu(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 					prange->mapping.offset,
 					prange->ttm_res ?
 						prange->ttm_res->mm_node : NULL,
-					dma_addr, &vm->last_update);
+					dma_addr, &vm->last_update,
+					&free_table);
 	if (r) {
 		pr_debug("failed %d to map to gpu 0x%lx\n", r, prange->start);
 		goto out;
@@ -1175,6 +1179,10 @@ svm_range_map_to_gpu(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 	if (fence)
 		*fence = dma_fence_get(vm->last_update);
 
+	if (free_table)
+		amdgpu_amdkfd_flush_gpu_tlb_pasid((struct kgd_dev *)adev,
+						  p->pasid);
+
 out:
 	prange->mapping.bo_va = NULL;
 	return r;
Signature · 6 lines
2.17.1

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
maintainer Reviewed-by on v2 1/1
#7 lore raw

Am 2021-05-12 um 2:43 p.m. schrieb Philip Yang:

Quoted 123 lines

Mapping huge page, 2MB aligned address with 2MB size, uses PDE0 as PTE. If previously valid PDE0, PDE0.V=1 and PDE0.P=0 turns into PTE, this requires TLB flush, otherwise page table walker will not read updated PDE0.

Change page table update mapping to return free_table flag to indicate the previously valid PDE may have turned into a PTE if page table is freed.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 13 +++++++++----
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h |  7 ++++++-
 drivers/gpu/drm/amd/amdkfd/kfd_svm.c   | 12 ++++++++++--
 3 files changed, 25 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +9 -4
index 3dcdcc9ff522..f9c00b451a3d 100644
@@ -1583,6 +1583,8 @@ static int amdgpu_vm_update_ptes(struct amdgpu_vm_update_params *params,
 			while (cursor.pfn < frag_start) {
 				amdgpu_vm_free_pts(adev, params->vm, &cursor);
 				amdgpu_vm_pt_next(adev, &cursor);
+				if (params->free_table)
+					*params->free_table = true;
 			}
 
 		} else if (frag >= shift) {
@@ -1610,6 +1612,7 @@ static int amdgpu_vm_update_ptes(struct amdgpu_vm_update_params *params,
  * @nodes: array of drm_mm_nodes with the MC addresses
  * @pages_addr: DMA addresses to use for mapping
  * @fence: optional resulting fence
+ * @free_table: return true if page table is freed
  *
  * Fill in the page table entries between @start and @last.
  *
@@ -1624,7 +1627,8 @@ int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
 				uint64_t flags, uint64_t offset,
 				struct drm_mm_node *nodes,
 				dma_addr_t *pages_addr,
-				struct dma_fence **fence)
+				struct dma_fence **fence,
+				bool *free_table)
 {
 	struct amdgpu_vm_update_params params;
 	enum amdgpu_sync_mode sync_mode;
@@ -1637,6 +1641,7 @@ int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
 	params.immediate = immediate;
 	params.pages_addr = pages_addr;
 	params.unlocked = unlocked;
+	params.free_table = free_table;
 
 	/* Implicitly sync to command submissions in the same VM before
 	 * unmapping. Sync to moving fences before mapping.
@@ -1879,7 +1884,7 @@ int amdgpu_vm_bo_update(struct amdgpu_device *adev, struct amdgpu_bo_va *bo_va,
 						resv, mapping->start,
 						mapping->last, update_flags,
 						mapping->offset, nodes,
-						pages_addr, last_update);
+						pages_addr, last_update, NULL);
 		if (r)
 			return r;
 	}
@@ -2090,7 +2095,7 @@ int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
 		r = amdgpu_vm_bo_update_mapping(adev, adev, vm, false, false,
 						resv, mapping->start,
 						mapping->last, init_pte_value,
-						0, NULL, NULL, &f);
+						0, NULL, NULL, &f, NULL);
 		amdgpu_vm_free_mapping(adev, vm, mapping, f);
 		if (r) {
 			dma_fence_put(f);
@@ -3428,7 +3433,7 @@ bool amdgpu_vm_handle_fault(struct amdgpu_device *adev, u32 pasid,
 	}
 
 	r = amdgpu_vm_bo_update_mapping(adev, adev, vm, true, false, NULL, addr,
-					addr, flags, value, NULL, NULL,
+					addr, flags, value, NULL, NULL, NULL,
 					NULL);
 	if (r)
 		goto error_unlock;
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +6 -1
index ea60ec122b51..41a55b035cda 100644
@@ -231,6 +231,11 @@ struct amdgpu_vm_update_params {
 	 * @num_dw_left: number of dw left for the IB
 	 */
 	unsigned int num_dw_left;
+
+	/**
+	 * @free_table: return true if page table is freed when updating
+	 */
+	bool *free_table;
 };
 
 struct amdgpu_vm_update_funcs {
@@ -404,7 +409,7 @@ int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
 				uint64_t flags, uint64_t offset,
 				struct drm_mm_node *nodes,
 				dma_addr_t *pages_addr,
-				struct dma_fence **fence);
+				struct dma_fence **fence, bool *free_table);
 int amdgpu_vm_bo_update(struct amdgpu_device *adev,
 			struct amdgpu_bo_va *bo_va,
 			bool clear);
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +1 -1
index b665e9ff77e3..4f28052d44bf 100644
@@ -1084,7 +1084,7 @@ svm_range_unmap_from_gpu(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 
 	return amdgpu_vm_bo_update_mapping(adev, adev, vm, false, true, NULL,
 					   start, last, init_pte_value, 0,
-					   NULL, NULL, fence);
+					   NULL, NULL, fence, NULL);
 }
 
 static int
@@ -1137,12 +1137,15 @@ svm_range_map_to_gpu(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 		     struct amdgpu_device *bo_adev, struct dma_fence **fence)
 {

drivers/gpu/drm/amd/amdkfd/kfd_svm.c svm_range_map_to_gpu(struct amdgpu_device *adev, struct amdgpu_vm *vm, · line 1148

Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +3 -0
 	struct amdgpu_bo_va bo_va;
+	bool free_table = false;
+	struct kfd_process *p;
 	uint64_t pte_flags;
 	int r = 0;
 
 	pr_debug("svms 0x%p [0x%lx 0x%lx]\n", prange->svms, prange->start,
 		 prange->last);
 
+	p = container_of(prange->svms, struct kfd_process, svms);

You can move this and the declaration of "p" into the if (free_table) below. With that fixed the patch is

Please also give Christian time to review this before you submit.

Thanks,
  Felix

Quoted 14 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +2 -1
 	if (prange->svm_bo && prange->ttm_res) {
 		bo_va.is_xgmi = amdgpu_xgmi_same_hive(adev, bo_adev);
 		prange->mapping.bo_va = &bo_va;
@@ -1159,7 +1162,8 @@ svm_range_map_to_gpu(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 					prange->mapping.offset,
 					prange->ttm_res ?
 						prange->ttm_res->mm_node : NULL,
-					dma_addr, &vm->last_update);
+					dma_addr, &vm->last_update,
+					&free_table);
 	if (r) {
 		pr_debug("failed %d to map to gpu 0x%lx\n", r, prange->start);
 		goto out;

drivers/gpu/drm/amd/amdkfd/kfd_svm.c svm_range_map_to_gpu(struct amdgpu_device *adev, struct amdgpu_vm *vm, · line 1188

Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +4 -0
@@ -1175,6 +1179,10 @@ svm_range_map_to_gpu(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 	if (fence)
 		*fence = dma_fence_get(vm->last_update);
 
+	if (free_table)
+		amdgpu_amdkfd_flush_gpu_tlb_pasid((struct kgd_dev *)adev,
+						  p->pasid);
+
 out:
 	prange->mapping.bo_va = NULL;
 	return r;

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

#8 lore raw
In reply to Philip Yang

Am 12.05.21 um 20:43 schrieb Philip Yang:

Quoted 21 lines

Mapping huge page, 2MB aligned address with 2MB size, uses PDE0 as PTE. If previously valid PDE0, PDE0.V=1 and PDE0.P=0 turns into PTE, this requires TLB flush, otherwise page table walker will not read updated PDE0.

Change page table update mapping to return free_table flag to indicate the previously valid PDE may have turned into a PTE if page table is freed.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 13 +++++++++----
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h |  7 ++++++-
  drivers/gpu/drm/amd/amdkfd/kfd_svm.c   | 12 ++++++++++--
  3 files changed, 25 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -0
index 3dcdcc9ff522..f9c00b451a3d 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c static int amdgpu_vm_update_ptes(struct amdgpu_vm_update_params *params, · line 1587

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2 -0
@@ -1583,6 +1583,8 @@ static int amdgpu_vm_update_ptes(struct amdgpu_vm_update_params *params,
  			while (cursor.pfn < frag_start) {
  				amdgpu_vm_free_pts(adev, params->vm, &cursor);
  				amdgpu_vm_pt_next(adev, &cursor);
+				if (params->free_table)
+					*params->free_table = true;

Make the value a bool in the params structure and copy it to the input variable in the upper level function and rename it to something like "table_freed" instead.

Apart from that looks good to me.

Christian.

Quoted 121 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +7 -4
  			}
 
  		} else if (frag >= shift) {
@@ -1610,6 +1612,7 @@ static int amdgpu_vm_update_ptes(struct amdgpu_vm_update_params *params,
   * @nodes: array of drm_mm_nodes with the MC addresses
   * @pages_addr: DMA addresses to use for mapping
   * @fence: optional resulting fence
+ * @free_table: return true if page table is freed
   *
   * Fill in the page table entries between @start and @last.
   *
@@ -1624,7 +1627,8 @@ int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
  				uint64_t flags, uint64_t offset,
  				struct drm_mm_node *nodes,
  				dma_addr_t *pages_addr,
-				struct dma_fence **fence)
+				struct dma_fence **fence,
+				bool *free_table)
  {
  	struct amdgpu_vm_update_params params;
  	enum amdgpu_sync_mode sync_mode;
@@ -1637,6 +1641,7 @@ int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
  	params.immediate = immediate;
  	params.pages_addr = pages_addr;
  	params.unlocked = unlocked;
+	params.free_table = free_table;
 
  	/* Implicitly sync to command submissions in the same VM before
  	 * unmapping. Sync to moving fences before mapping.
@@ -1879,7 +1884,7 @@ int amdgpu_vm_bo_update(struct amdgpu_device *adev, struct amdgpu_bo_va *bo_va,
  						resv, mapping->start,
  						mapping->last, update_flags,
  						mapping->offset, nodes,
-						pages_addr, last_update);
+						pages_addr, last_update, NULL);
  		if (r)
  			return r;
  	}
@@ -2090,7 +2095,7 @@ int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
  		r = amdgpu_vm_bo_update_mapping(adev, adev, vm, false, false,
  						resv, mapping->start,
  						mapping->last, init_pte_value,
-						0, NULL, NULL, &f);
+						0, NULL, NULL, &f, NULL);
  		amdgpu_vm_free_mapping(adev, vm, mapping, f);
  		if (r) {
  			dma_fence_put(f);
@@ -3428,7 +3433,7 @@ bool amdgpu_vm_handle_fault(struct amdgpu_device *adev, u32 pasid,
  	}
 
  	r = amdgpu_vm_bo_update_mapping(adev, adev, vm, true, false, NULL, addr,
-					addr, flags, value, NULL, NULL,
+					addr, flags, value, NULL, NULL, NULL,
  					NULL);
  	if (r)
  		goto error_unlock;
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +6 -1
index ea60ec122b51..41a55b035cda 100644
@@ -231,6 +231,11 @@ struct amdgpu_vm_update_params {
  	 * @num_dw_left: number of dw left for the IB
  	 */
  	unsigned int num_dw_left;
+
+	/**
+	 * @free_table: return true if page table is freed when updating
+	 */
+	bool *free_table;
  };
 
  struct amdgpu_vm_update_funcs {
@@ -404,7 +409,7 @@ int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
  				uint64_t flags, uint64_t offset,
  				struct drm_mm_node *nodes,
  				dma_addr_t *pages_addr,
-				struct dma_fence **fence);
+				struct dma_fence **fence, bool *free_table);
  int amdgpu_vm_bo_update(struct amdgpu_device *adev,
  			struct amdgpu_bo_va *bo_va,
  			bool clear);
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +6 -2
index b665e9ff77e3..4f28052d44bf 100644
@@ -1084,7 +1084,7 @@ svm_range_unmap_from_gpu(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 
  	return amdgpu_vm_bo_update_mapping(adev, adev, vm, false, true, NULL,
  					   start, last, init_pte_value, 0,
-					   NULL, NULL, fence);
+					   NULL, NULL, fence, NULL);
  }
 
  static int
@@ -1137,12 +1137,15 @@ svm_range_map_to_gpu(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  		     struct amdgpu_device *bo_adev, struct dma_fence **fence)
  {
  	struct amdgpu_bo_va bo_va;
+	bool free_table = false;
+	struct kfd_process *p;
  	uint64_t pte_flags;
  	int r = 0;
 
  	pr_debug("svms 0x%p [0x%lx 0x%lx]\n", prange->svms, prange->start,
  		 prange->last);
 
+	p = container_of(prange->svms, struct kfd_process, svms);
  	if (prange->svm_bo && prange->ttm_res) {
  		bo_va.is_xgmi = amdgpu_xgmi_same_hive(adev, bo_adev);
  		prange->mapping.bo_va = &bo_va;
@@ -1159,7 +1162,8 @@ svm_range_map_to_gpu(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  					prange->mapping.offset,
  					prange->ttm_res ?
  						prange->ttm_res->mm_node : NULL,
-					dma_addr, &vm->last_update);
+					dma_addr, &vm->last_update,
+					&free_table);
  	if (r) {
  		pr_debug("failed %d to map to gpu 0x%lx\n", r, prange->start);
  		goto out;

drivers/gpu/drm/amd/amdkfd/kfd_svm.c svm_range_map_to_gpu(struct amdgpu_device *adev, struct amdgpu_vm *vm, · line 1188

Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +4 -0
@@ -1175,6 +1179,10 @@ svm_range_map_to_gpu(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  	if (fence)
  		*fence = dma_fence_get(vm->last_update);
 
+	if (free_table)
+		amdgpu_amdkfd_flush_gpu_tlb_pasid((struct kgd_dev *)adev,
+						  p->pasid);
+
  out:
  	prange->mapping.bo_va = NULL;
  	return r;

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

#9 lore raw

PATCH v3 1/1 drm/amdgpu: flush TLB if valid PDE turns into PTE

In reply to Philip Yang

Mapping huge page, 2MB aligned address with 2MB size, uses PDE0 as PTE. If previously valid PDE0, PDE0.V=1 and PDE0.P=0 turns into PTE, this requires TLB flush, otherwise page table walker will not read updated PDE0.

Change page table update mapping to return table_freed flag to indicate the previously valid PDE may have turned into a PTE if page table is freed.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 14 ++++++++++----
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h |  7 ++++++-
 drivers/gpu/drm/amd/amdkfd/kfd_svm.c   | 13 +++++++++++--
 3 files changed, 27 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +10 -4
index 3dcdcc9ff522..7e4c60a90eee 100644
@@ -1583,6 +1583,7 @@ static int amdgpu_vm_update_ptes(struct amdgpu_vm_update_params *params,
 			while (cursor.pfn < frag_start) {
 				amdgpu_vm_free_pts(adev, params->vm, &cursor);
 				amdgpu_vm_pt_next(adev, &cursor);
+				params->table_freed = true;
 			}
 
 		} else if (frag >= shift) {
@@ -1610,6 +1611,7 @@ static int amdgpu_vm_update_ptes(struct amdgpu_vm_update_params *params,
  * @nodes: array of drm_mm_nodes with the MC addresses
  * @pages_addr: DMA addresses to use for mapping
  * @fence: optional resulting fence
+ * @table_freed: return true if page table is freed
  *
  * Fill in the page table entries between @start and @last.
  *
@@ -1624,7 +1626,8 @@ int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
 				uint64_t flags, uint64_t offset,
 				struct drm_mm_node *nodes,
 				dma_addr_t *pages_addr,
-				struct dma_fence **fence)
+				struct dma_fence **fence,
+				bool *table_freed)
 {
 	struct amdgpu_vm_update_params params;
 	enum amdgpu_sync_mode sync_mode;
@@ -1736,6 +1739,9 @@ int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
 
 	r = vm->update_funcs->commit(&params, fence);
 
+	if (table_freed)
+		*table_freed = params.table_freed;
+
 error_unlock:
 	amdgpu_vm_eviction_unlock(vm);
 	return r;
@@ -1879,7 +1885,7 @@ int amdgpu_vm_bo_update(struct amdgpu_device *adev, struct amdgpu_bo_va *bo_va,
 						resv, mapping->start,
 						mapping->last, update_flags,
 						mapping->offset, nodes,
-						pages_addr, last_update);
+						pages_addr, last_update, NULL);
 		if (r)
 			return r;
 	}
@@ -2090,7 +2096,7 @@ int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
 		r = amdgpu_vm_bo_update_mapping(adev, adev, vm, false, false,
 						resv, mapping->start,
 						mapping->last, init_pte_value,
-						0, NULL, NULL, &f);
+						0, NULL, NULL, &f, NULL);
 		amdgpu_vm_free_mapping(adev, vm, mapping, f);
 		if (r) {
 			dma_fence_put(f);
@@ -3428,7 +3434,7 @@ bool amdgpu_vm_handle_fault(struct amdgpu_device *adev, u32 pasid,
 	}
 
 	r = amdgpu_vm_bo_update_mapping(adev, adev, vm, true, false, NULL, addr,
-					addr, flags, value, NULL, NULL,
+					addr, flags, value, NULL, NULL, NULL,
 					NULL);
 	if (r)
 		goto error_unlock;
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +6 -1
index ea60ec122b51..e91288d637ce 100644
@@ -231,6 +231,11 @@ struct amdgpu_vm_update_params {
 	 * @num_dw_left: number of dw left for the IB
 	 */
 	unsigned int num_dw_left;
+
+	/**
+	 * @table_freed: return true if page table is freed when updating
+	 */
+	bool table_freed;
 };
 
 struct amdgpu_vm_update_funcs {
@@ -404,7 +409,7 @@ int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
 				uint64_t flags, uint64_t offset,
 				struct drm_mm_node *nodes,
 				dma_addr_t *pages_addr,
-				struct dma_fence **fence);
+				struct dma_fence **fence, bool *free_table);
 int amdgpu_vm_bo_update(struct amdgpu_device *adev,
 			struct amdgpu_bo_va *bo_va,
 			bool clear);
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +11 -2
index b665e9ff77e3..a518ad26ceec 100644
@@ -1084,7 +1084,7 @@ svm_range_unmap_from_gpu(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 
 	return amdgpu_vm_bo_update_mapping(adev, adev, vm, false, true, NULL,
 					   start, last, init_pte_value, 0,
-					   NULL, NULL, fence);
+					   NULL, NULL, fence, NULL);
 }
 
 static int
@@ -1137,6 +1137,7 @@ svm_range_map_to_gpu(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 		     struct amdgpu_device *bo_adev, struct dma_fence **fence)
 {
 	struct amdgpu_bo_va bo_va;
+	bool table_freed = false;
 	uint64_t pte_flags;
 	int r = 0;
 
@@ -1159,7 +1160,8 @@ svm_range_map_to_gpu(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 					prange->mapping.offset,
 					prange->ttm_res ?
 						prange->ttm_res->mm_node : NULL,
-					dma_addr, &vm->last_update);
+					dma_addr, &vm->last_update,
+					&table_freed);
 	if (r) {
 		pr_debug("failed %d to map to gpu 0x%lx\n", r, prange->start);
 		goto out;
@@ -1175,6 +1177,13 @@ svm_range_map_to_gpu(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 	if (fence)
 		*fence = dma_fence_get(vm->last_update);
 
+	if (table_freed) {
+		struct kfd_process *p;
+
+		p = container_of(prange->svms, struct kfd_process, svms);
+		amdgpu_amdkfd_flush_gpu_tlb_pasid((struct kgd_dev *)adev,
+						  p->pasid);
+	}
 out:
 	prange->mapping.bo_va = NULL;
 	return r;
Signature · 6 lines
2.17.1

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
maintainer Reviewed-by on v3 1/1
#10 lore raw

Am 2021-05-13 um 12:58 p.m. schrieb Philip Yang:

Quoted 10 lines

Mapping huge page, 2MB aligned address with 2MB size, uses PDE0 as PTE. If previously valid PDE0, PDE0.V=1 and PDE0.P=0 turns into PTE, this requires TLB flush, otherwise page table walker will not read updated PDE0.

Change page table update mapping to return table_freed flag to indicate the previously valid PDE may have turned into a PTE if page table is freed.

Quoted 133 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 14 ++++++++++----
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h |  7 ++++++-
 drivers/gpu/drm/amd/amdkfd/kfd_svm.c   | 13 +++++++++++--
 3 files changed, 27 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +10 -4
index 3dcdcc9ff522..7e4c60a90eee 100644
@@ -1583,6 +1583,7 @@ static int amdgpu_vm_update_ptes(struct amdgpu_vm_update_params *params,
 			while (cursor.pfn < frag_start) {
 				amdgpu_vm_free_pts(adev, params->vm, &cursor);
 				amdgpu_vm_pt_next(adev, &cursor);
+				params->table_freed = true;
 			}
 
 		} else if (frag >= shift) {
@@ -1610,6 +1611,7 @@ static int amdgpu_vm_update_ptes(struct amdgpu_vm_update_params *params,
  * @nodes: array of drm_mm_nodes with the MC addresses
  * @pages_addr: DMA addresses to use for mapping
  * @fence: optional resulting fence
+ * @table_freed: return true if page table is freed
  *
  * Fill in the page table entries between @start and @last.
  *
@@ -1624,7 +1626,8 @@ int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
 				uint64_t flags, uint64_t offset,
 				struct drm_mm_node *nodes,
 				dma_addr_t *pages_addr,
-				struct dma_fence **fence)
+				struct dma_fence **fence,
+				bool *table_freed)
 {
 	struct amdgpu_vm_update_params params;
 	enum amdgpu_sync_mode sync_mode;
@@ -1736,6 +1739,9 @@ int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
 
 	r = vm->update_funcs->commit(&params, fence);
 
+	if (table_freed)
+		*table_freed = params.table_freed;
+
 error_unlock:
 	amdgpu_vm_eviction_unlock(vm);
 	return r;
@@ -1879,7 +1885,7 @@ int amdgpu_vm_bo_update(struct amdgpu_device *adev, struct amdgpu_bo_va *bo_va,
 						resv, mapping->start,
 						mapping->last, update_flags,
 						mapping->offset, nodes,
-						pages_addr, last_update);
+						pages_addr, last_update, NULL);
 		if (r)
 			return r;
 	}
@@ -2090,7 +2096,7 @@ int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
 		r = amdgpu_vm_bo_update_mapping(adev, adev, vm, false, false,
 						resv, mapping->start,
 						mapping->last, init_pte_value,
-						0, NULL, NULL, &f);
+						0, NULL, NULL, &f, NULL);
 		amdgpu_vm_free_mapping(adev, vm, mapping, f);
 		if (r) {
 			dma_fence_put(f);
@@ -3428,7 +3434,7 @@ bool amdgpu_vm_handle_fault(struct amdgpu_device *adev, u32 pasid,
 	}
 
 	r = amdgpu_vm_bo_update_mapping(adev, adev, vm, true, false, NULL, addr,
-					addr, flags, value, NULL, NULL,
+					addr, flags, value, NULL, NULL, NULL,
 					NULL);
 	if (r)
 		goto error_unlock;
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +6 -1
index ea60ec122b51..e91288d637ce 100644
@@ -231,6 +231,11 @@ struct amdgpu_vm_update_params {
 	 * @num_dw_left: number of dw left for the IB
 	 */
 	unsigned int num_dw_left;
+
+	/**
+	 * @table_freed: return true if page table is freed when updating
+	 */
+	bool table_freed;
 };
 
 struct amdgpu_vm_update_funcs {
@@ -404,7 +409,7 @@ int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
 				uint64_t flags, uint64_t offset,
 				struct drm_mm_node *nodes,
 				dma_addr_t *pages_addr,
-				struct dma_fence **fence);
+				struct dma_fence **fence, bool *free_table);
 int amdgpu_vm_bo_update(struct amdgpu_device *adev,
 			struct amdgpu_bo_va *bo_va,
 			bool clear);
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +4 -2
index b665e9ff77e3..a518ad26ceec 100644
@@ -1084,7 +1084,7 @@ svm_range_unmap_from_gpu(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 
 	return amdgpu_vm_bo_update_mapping(adev, adev, vm, false, true, NULL,
 					   start, last, init_pte_value, 0,
-					   NULL, NULL, fence);
+					   NULL, NULL, fence, NULL);
 }
 
 static int
@@ -1137,6 +1137,7 @@ svm_range_map_to_gpu(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 		     struct amdgpu_device *bo_adev, struct dma_fence **fence)
 {
 	struct amdgpu_bo_va bo_va;
+	bool table_freed = false;
 	uint64_t pte_flags;
 	int r = 0;
 
@@ -1159,7 +1160,8 @@ svm_range_map_to_gpu(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 					prange->mapping.offset,
 					prange->ttm_res ?
 						prange->ttm_res->mm_node : NULL,
-					dma_addr, &vm->last_update);
+					dma_addr, &vm->last_update,
+					&table_freed);
 	if (r) {
 		pr_debug("failed %d to map to gpu 0x%lx\n", r, prange->start);
 		goto out;
@@ -1175,6 +1177,13 @@ svm_range_map_to_gpu(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 	if (fence)
 		*fence = dma_fence_get(vm->last_update);
 

drivers/gpu/drm/amd/amdkfd/kfd_svm.c svm_range_map_to_gpu(struct amdgpu_device *adev, struct amdgpu_vm *vm, · line 1189

Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +7 -0
+	if (table_freed) {
+		struct kfd_process *p;
+
+		p = container_of(prange->svms, struct kfd_process, svms);
+		amdgpu_amdkfd_flush_gpu_tlb_pasid((struct kgd_dev *)adev,
+						  p->pasid);
+	}
 out:
 	prange->mapping.bo_va = NULL;
 	return r;

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

Reviewed-by
#11 lore raw

Am 13.05.21 um 19:19 schrieb Felix Kuehling:

Quoted 6 lines

Am 2021-05-13 um 12:58 p.m. schrieb Philip Yang:

Quoted 5 lines

Mapping huge page, 2MB aligned address with 2MB size, uses PDE0 as PTE. If previously valid PDE0, PDE0.V=1 and PDE0.P=0 turns into PTE, this requires TLB flush, otherwise page table walker will not read updated PDE0.

Quoted 6 lines
Quoted 5 lines

Change page table update mapping to return table_freed flag to indicate the previously valid PDE may have turned into a PTE if page table is freed.

Quoted 135 lines
Quoted 135 lines

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 14 ++++++++++----
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h |  7 ++++++-
  drivers/gpu/drm/amd/amdkfd/kfd_svm.c   | 13 +++++++++++--
  3 files changed, 27 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +10 -4
index 3dcdcc9ff522..7e4c60a90eee 100644
@@ -1583,6 +1583,7 @@ static int amdgpu_vm_update_ptes(struct amdgpu_vm_update_params *params,
  			while (cursor.pfn < frag_start) {
  				amdgpu_vm_free_pts(adev, params->vm, &cursor);
  				amdgpu_vm_pt_next(adev, &cursor);
+				params->table_freed = true;
  			}
 
  		} else if (frag >= shift) {
@@ -1610,6 +1611,7 @@ static int amdgpu_vm_update_ptes(struct amdgpu_vm_update_params *params,
   * @nodes: array of drm_mm_nodes with the MC addresses
   * @pages_addr: DMA addresses to use for mapping
   * @fence: optional resulting fence
+ * @table_freed: return true if page table is freed
   *
   * Fill in the page table entries between @start and @last.
   *
@@ -1624,7 +1626,8 @@ int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
  				uint64_t flags, uint64_t offset,
  				struct drm_mm_node *nodes,
  				dma_addr_t *pages_addr,
-				struct dma_fence **fence)
+				struct dma_fence **fence,
+				bool *table_freed)
  {
  	struct amdgpu_vm_update_params params;
  	enum amdgpu_sync_mode sync_mode;
@@ -1736,6 +1739,9 @@ int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
 
  	r = vm->update_funcs->commit(&params, fence);
 
+	if (table_freed)
+		*table_freed = params.table_freed;
+
  error_unlock:
  	amdgpu_vm_eviction_unlock(vm);
  	return r;
@@ -1879,7 +1885,7 @@ int amdgpu_vm_bo_update(struct amdgpu_device *adev, struct amdgpu_bo_va *bo_va,
  						resv, mapping->start,
  						mapping->last, update_flags,
  						mapping->offset, nodes,
-						pages_addr, last_update);
+						pages_addr, last_update, NULL);
  		if (r)
  			return r;
  	}
@@ -2090,7 +2096,7 @@ int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
  		r = amdgpu_vm_bo_update_mapping(adev, adev, vm, false, false,
  						resv, mapping->start,
  						mapping->last, init_pte_value,
-						0, NULL, NULL, &f);
+						0, NULL, NULL, &f, NULL);
  		amdgpu_vm_free_mapping(adev, vm, mapping, f);
  		if (r) {
  			dma_fence_put(f);
@@ -3428,7 +3434,7 @@ bool amdgpu_vm_handle_fault(struct amdgpu_device *adev, u32 pasid,
  	}
 
  	r = amdgpu_vm_bo_update_mapping(adev, adev, vm, true, false, NULL, addr,
-					addr, flags, value, NULL, NULL,
+					addr, flags, value, NULL, NULL, NULL,
  					NULL);
  	if (r)
  		goto error_unlock;
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +6 -1
index ea60ec122b51..e91288d637ce 100644
@@ -231,6 +231,11 @@ struct amdgpu_vm_update_params {
  	 * @num_dw_left: number of dw left for the IB
  	 */
  	unsigned int num_dw_left;
+
+	/**
+	 * @table_freed: return true if page table is freed when updating
+	 */
+	bool table_freed;
  };
 
  struct amdgpu_vm_update_funcs {
@@ -404,7 +409,7 @@ int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
  				uint64_t flags, uint64_t offset,
  				struct drm_mm_node *nodes,
  				dma_addr_t *pages_addr,
-				struct dma_fence **fence);
+				struct dma_fence **fence, bool *free_table);
  int amdgpu_vm_bo_update(struct amdgpu_device *adev,
  			struct amdgpu_bo_va *bo_va,
  			bool clear);
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +4 -2
index b665e9ff77e3..a518ad26ceec 100644
@@ -1084,7 +1084,7 @@ svm_range_unmap_from_gpu(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 
  	return amdgpu_vm_bo_update_mapping(adev, adev, vm, false, true, NULL,
  					   start, last, init_pte_value, 0,
-					   NULL, NULL, fence);
+					   NULL, NULL, fence, NULL);
  }
 
  static int
@@ -1137,6 +1137,7 @@ svm_range_map_to_gpu(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  		     struct amdgpu_device *bo_adev, struct dma_fence **fence)
  {
  	struct amdgpu_bo_va bo_va;
+	bool table_freed = false;
  	uint64_t pte_flags;
  	int r = 0;
 
@@ -1159,7 +1160,8 @@ svm_range_map_to_gpu(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  					prange->mapping.offset,
  					prange->ttm_res ?
  						prange->ttm_res->mm_node : NULL,
-					dma_addr, &vm->last_update);
+					dma_addr, &vm->last_update,
+					&table_freed);
  	if (r) {
  		pr_debug("failed %d to map to gpu 0x%lx\n", r, prange->start);
  		goto out;
@@ -1175,6 +1177,13 @@ svm_range_map_to_gpu(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  	if (fence)
  		*fence = dma_fence_get(vm->last_update);
 
Quoted 14 lines

drivers/gpu/drm/amd/amdkfd/kfd_svm.c svm_range_map_to_gpu(struct amdgpu_device *adev, struct amdgpu_vm *vm, · line 1189

Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +7 -0
+	if (table_freed) {
+		struct kfd_process *p;
+
+		p = container_of(prange->svms, struct kfd_process, svms);
+		amdgpu_amdkfd_flush_gpu_tlb_pasid((struct kgd_dev *)adev,
+						  p->pasid);
+	}
  out:
  	prange->mapping.bo_va = NULL;
  	return r;

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

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