Skip to contentSkip to conversation

drm/amdgpu: remove PT BOs when unmapping

PATCH NAK Huang, JinHuiEric 3 participants 3 messages Oct 29, 2019 – Oct 30, 2019 2 messages not in the archive

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

PATCH 1/1 drm/amdgpu: remove PT BOs when unmapping

The issue is PT BOs are not freed when unmapping VA, which causes vram usage accumulated is huge in some memory stress test, such as kfd big buffer stress test. Function amdgpu_vm_bo_update_mapping() is called by both amdgpu_vm_bo_update() and amdgpu_vm_clear_freed(). The solution is replacing amdgpu_vm_bo_update_mapping() in amdgpu_vm_clear_freed() with removing PT BOs function to save vram usage.

Change-Id Ic24e35bff8ca85265b418a642373f189d972a924
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 56 +++++++++++++++++++++++++++++-----
 1 file changed, 48 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +48 -8
index 0f4c3b2..8a480c7 100644
@@ -1930,6 +1930,51 @@ static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
 }
 
 /**
+ * amdgpu_vm_remove_ptes - free PT BOs
+ *
+ * @adev: amdgpu device structure
+ * @vm: amdgpu vm structure
+ * @start: start of mapped range
+ * @end: end of mapped entry
+ *
+ * Free the page table level.
+ */
+static int amdgpu_vm_remove_ptes(struct amdgpu_device *adev,
+		struct amdgpu_vm *vm, uint64_t start, uint64_t end)
+{
+	struct amdgpu_vm_pt_cursor cursor;
+	unsigned shift, num_entries;
+
+	amdgpu_vm_pt_start(adev, vm, start, &cursor);
+	while (cursor.level < AMDGPU_VM_PTB) {
+		if (!amdgpu_vm_pt_descendant(adev, &cursor))
+			return -ENOENT;
+	}
+
+	while (cursor.pfn < end) {
+		amdgpu_vm_free_table(cursor.entry);
+		num_entries = amdgpu_vm_num_entries(adev, cursor.level - 1);
+
+		if (cursor.entry != &cursor.parent->entries[num_entries - 1]) {
+			/* Next ptb entry */
+			shift = amdgpu_vm_level_shift(adev, cursor.level - 1);
+			cursor.pfn += 1ULL << shift;
+			cursor.pfn &= ~((1ULL << shift) - 1);
+			cursor.entry++;
+		} else {
+			/* Next ptb entry in next pd0 entry */
+			amdgpu_vm_pt_ancestor(&cursor);
+			shift = amdgpu_vm_level_shift(adev, cursor.level - 1);
+			cursor.pfn += 1ULL << shift;
+			cursor.pfn &= ~((1ULL << shift) - 1);
+			amdgpu_vm_pt_descendant(adev, &cursor);
+		}
+	}
+
+	return 0;
+}
+
+/**
  * amdgpu_vm_clear_freed - clear freed BOs in the PT
  *
  * @adev: amdgpu_device pointer
@@ -1949,7 +1994,6 @@ int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
 			  struct dma_fence **fence)
 {
 	struct amdgpu_bo_va_mapping *mapping;
-	uint64_t init_pte_value = 0;
 	struct dma_fence *f = NULL;
 	int r;
 
@@ -1958,13 +2002,10 @@ int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
 			struct amdgpu_bo_va_mapping, list);
 		list_del(&mapping->list);
 
-		if (vm->pte_support_ats &&
-		    mapping->start < AMDGPU_GMC_HOLE_START)
-			init_pte_value = AMDGPU_PTE_DEFAULT_ATC;
+		r = amdgpu_vm_remove_ptes(adev, vm,
+				(mapping->start + 0x1ff) & (~0x1ffll),
+				(mapping->last + 1) & (~0x1ffll));
 
-		r = amdgpu_vm_bo_update_mapping(adev, vm, false, NULL,
-						mapping->start, mapping->last,
-						init_pte_value, 0, NULL, &f);
 		amdgpu_vm_free_mapping(adev, vm, mapping, f);
 		if (r) {
 			dma_fence_put(f);
@@ -1980,7 +2021,6 @@ int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
 	}
 
 	return 0;
-
 }
 
 /**
Signature · 6 lines
2.7.4

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

A message that is not in the archive (sent off-list or not yet received): 1572379585-1401-1-git-send-email-JinhuiEric.Huang-5C7GfCeVMHo@public.gmane.org

#2 lore raw

Am 29.10.19 um 21:06 schrieb Huang, JinHuiEric:

Quoted 8 lines

The issue is PT BOs are not freed when unmapping VA, which causes vram usage accumulated is huge in some memory stress test, such as kfd big buffer stress test. Function amdgpu_vm_bo_update_mapping() is called by both amdgpu_vm_bo_update() and amdgpu_vm_clear_freed(). The solution is replacing amdgpu_vm_bo_update_mapping() in amdgpu_vm_clear_freed() with removing PT BOs function to save vram usage.

NAK, that is intentional behavior.

Otherwise we can run into out of memory situations when page tables need to be allocated again under stress.

Regards,
Christian.

Quoted 89 lines
Change-Id Ic24e35bff8ca85265b418a642373f189d972a924
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 56 +++++++++++++++++++++++++++++-----
  1 file changed, 48 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +48 -7
index 0f4c3b2..8a480c7 100644
@@ -1930,6 +1930,51 @@ static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
  }
 
  /**
+ * amdgpu_vm_remove_ptes - free PT BOs
+ *
+ * @adev: amdgpu device structure
+ * @vm: amdgpu vm structure
+ * @start: start of mapped range
+ * @end: end of mapped entry
+ *
+ * Free the page table level.
+ */
+static int amdgpu_vm_remove_ptes(struct amdgpu_device *adev,
+		struct amdgpu_vm *vm, uint64_t start, uint64_t end)
+{
+	struct amdgpu_vm_pt_cursor cursor;
+	unsigned shift, num_entries;
+
+	amdgpu_vm_pt_start(adev, vm, start, &cursor);
+	while (cursor.level < AMDGPU_VM_PTB) {
+		if (!amdgpu_vm_pt_descendant(adev, &cursor))
+			return -ENOENT;
+	}
+
+	while (cursor.pfn < end) {
+		amdgpu_vm_free_table(cursor.entry);
+		num_entries = amdgpu_vm_num_entries(adev, cursor.level - 1);
+
+		if (cursor.entry != &cursor.parent->entries[num_entries - 1]) {
+			/* Next ptb entry */
+			shift = amdgpu_vm_level_shift(adev, cursor.level - 1);
+			cursor.pfn += 1ULL << shift;
+			cursor.pfn &= ~((1ULL << shift) - 1);
+			cursor.entry++;
+		} else {
+			/* Next ptb entry in next pd0 entry */
+			amdgpu_vm_pt_ancestor(&cursor);
+			shift = amdgpu_vm_level_shift(adev, cursor.level - 1);
+			cursor.pfn += 1ULL << shift;
+			cursor.pfn &= ~((1ULL << shift) - 1);
+			amdgpu_vm_pt_descendant(adev, &cursor);
+		}
+	}
+
+	return 0;
+}
+
+/**
   * amdgpu_vm_clear_freed - clear freed BOs in the PT
   *
   * @adev: amdgpu_device pointer
@@ -1949,7 +1994,6 @@ int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
  			  struct dma_fence **fence)
  {
  	struct amdgpu_bo_va_mapping *mapping;
-	uint64_t init_pte_value = 0;
  	struct dma_fence *f = NULL;
  	int r;
 
@@ -1958,13 +2002,10 @@ int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
  			struct amdgpu_bo_va_mapping, list);
  		list_del(&mapping->list);
 
-		if (vm->pte_support_ats &&
-		    mapping->start < AMDGPU_GMC_HOLE_START)
-			init_pte_value = AMDGPU_PTE_DEFAULT_ATC;
+		r = amdgpu_vm_remove_ptes(adev, vm,
+				(mapping->start + 0x1ff) & (~0x1ffll),
+				(mapping->last + 1) & (~0x1ffll));
 
-		r = amdgpu_vm_bo_update_mapping(adev, vm, false, NULL,
-						mapping->start, mapping->last,
-						init_pte_value, 0, NULL, &f);
  		amdgpu_vm_free_mapping(adev, vm, mapping, f);
  		if (r) {
  			dma_fence_put(f);

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_clear_freed(struct amdgpu_device *adev, · line 2026

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -1
@@ -1980,7 +2021,6 @@ int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
  	}
 
  	return 0;
-
  }
 
  /**

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

A message that is not in the archive (sent off-list or not yet received): ebd653c8-d9e9-b104-769f-e68dee0e4c65-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#4 lore raw
Quoted 10 lines · Christian König

Am 29.10.19 um 21:06 schrieb Huang, JinHuiEric:

Quoted 8 lines

The issue is PT BOs are not freed when unmapping VA, which causes vram usage accumulated is huge in some memory stress test, such as kfd big buffer stress test. Function amdgpu_vm_bo_update_mapping() is called by both amdgpu_vm_bo_update() and amdgpu_vm_clear_freed(). The solution is replacing amdgpu_vm_bo_update_mapping() in amdgpu_vm_clear_freed() with removing PT BOs function to save vram usage.

Quoted 4 lines

NAK, that is intentional behavior.

Otherwise we can run into out of memory situations when page tables need to be allocated again under stress.

That's a bit arbitrary and inconsistent. We are freeing page tables in other situations, when a mapping uses huge pages in amdgpu_vm_update_ptes. Why not when a mapping is destroyed completely?

I'm actually a bit surprised that the huge-page handling in amdgpu_vm_update_ptes isn't kicking in to free up lower-level page tables when a BO is unmapped.

Regards,
  Felix

Quoted 97 lines

Regards,
Christian.

Quoted 93 lines
Change-Id Ic24e35bff8ca85265b418a642373f189d972a924

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 56
+++++++++++++++++++++++++++++-----

  1 file changed, 48 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +45 -1
index 0f4c3b2..8a480c7 100644
@@ -1930,6 +1930,51 @@ static void amdgpu_vm_prt_fini(struct 
 amdgpu_device *adev, struct amdgpu_vm *vm)
   }
     /**
+ * amdgpu_vm_remove_ptes - free PT BOs
+ *
+ * @adev: amdgpu device structure
+ * @vm: amdgpu vm structure
+ * @start: start of mapped range
+ * @end: end of mapped entry
+ *
+ * Free the page table level.
+ */
+static int amdgpu_vm_remove_ptes(struct amdgpu_device *adev,
+        struct amdgpu_vm *vm, uint64_t start, uint64_t end)
+{
+    struct amdgpu_vm_pt_cursor cursor;
+    unsigned shift, num_entries;
+
+    amdgpu_vm_pt_start(adev, vm, start, &cursor);
+    while (cursor.level < AMDGPU_VM_PTB) {
+        if (!amdgpu_vm_pt_descendant(adev, &cursor))
+            return -ENOENT;
+    }
+
+    while (cursor.pfn < end) {
+        amdgpu_vm_free_table(cursor.entry);
+        num_entries = amdgpu_vm_num_entries(adev, cursor.level - 1);
+
+        if (cursor.entry != &cursor.parent->entries[num_entries - 1]) {
+            /* Next ptb entry */
+            shift = amdgpu_vm_level_shift(adev, cursor.level - 1);
+            cursor.pfn += 1ULL << shift;
+            cursor.pfn &= ~((1ULL << shift) - 1);
+            cursor.entry++;
+        } else {
+            /* Next ptb entry in next pd0 entry */
+            amdgpu_vm_pt_ancestor(&cursor);
+            shift = amdgpu_vm_level_shift(adev, cursor.level - 1);
+            cursor.pfn += 1ULL << shift;
+            cursor.pfn &= ~((1ULL << shift) - 1);
+            amdgpu_vm_pt_descendant(adev, &cursor);
+        }
+    }
+
+    return 0;
+}
+
+/**
    * amdgpu_vm_clear_freed - clear freed BOs in the PT
    *
    * @adev: amdgpu_device pointer
@@ -1949,7 +1994,6 @@ int amdgpu_vm_clear_freed(struct amdgpu_device 
 *adev,
                 struct dma_fence **fence)
   {
       struct amdgpu_bo_va_mapping *mapping;
-    uint64_t init_pte_value = 0;
       struct dma_fence *f = NULL;
       int r;

  @@ -1958,13 +2002,10 @@ int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
              struct amdgpu_bo_va_mapping, list); list_del(&mapping->list);
  -        if (vm->pte_support_ats &&
-            mapping->start < AMDGPU_GMC_HOLE_START)
-            init_pte_value = AMDGPU_PTE_DEFAULT_ATC;
+        r = amdgpu_vm_remove_ptes(adev, vm,
+                (mapping->start + 0x1ff) & (~0x1ffll), +                (mapping->last + 1) & (~0x1ffll));
  -        r = amdgpu_vm_bo_update_mapping(adev, vm, false, NULL, -                        mapping->start, mapping->last, -                        init_pte_value, 0, NULL, &f); amdgpu_vm_free_mapping(adev, vm, mapping, f); if (r) {
              dma_fence_put(f);

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -0
@@ -1980,7 +2021,6 @@ int amdgpu_vm_clear_freed(struct amdgpu_device 
 *adev,
       }
         return 0;
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_clear_freed(struct amdgpu_device *adev, · line 2026

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -1
-
   }
     /**

_______________________________________________
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