Skip to contentSkip to conversation

drm/amdgpu: fix amdgpu_vm_pt_free warning

PATCH Superseded Philip Yang 4 participants 5 messages Sep 9, 2022 – Sep 12, 2022

Subsystems (2)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/amdgpu: fix amdgpu_vm_pt_free warning

Free page table BO from vm resv unlocked context generate below warnings.

Add a free_work in vm_bo base of each page table BO, pass vm resv unlock status from page table update caller, and schedule the work if calling with vm resv unlocked.

 WARNING: CPU: 12 PID: 3238 at
 drivers/gpu/drm/ttm/ttm_bo.c:106 ttm_bo_set_bulk_move+0xa1/0xc0
 Call Trace:
  amdgpu_vm_pt_free+0x42/0xd0 [amdgpu]
  amdgpu_vm_pt_free_dfs+0xb3/0xf0 [amdgpu]
  amdgpu_vm_ptes_update+0x52d/0x850 [amdgpu]
  amdgpu_vm_update_range+0x2a6/0x640 [amdgpu]
  svm_range_unmap_from_gpus+0x110/0x300 [amdgpu]
  svm_range_cpu_invalidate_pagetables+0x535/0x600 [amdgpu]
 __mmu_notifier_invalidate_range_start+0x1cd/0x230
  unmap_vmas+0x9d/0x140
  unmap_region+0xa8/0x110

 WARNING: CPU: 0 PID: 1475 at
 drivers/dma-buf/dma-resv.c:483 dma_resv_iter_next
 Call Trace:
  dma_resv_iter_first+0x43/0xa0
  amdgpu_vm_sdma_update+0x69/0x2d0 [amdgpu]
  amdgpu_vm_ptes_update+0x29c/0x870 [amdgpu]
  amdgpu_vm_update_range+0x2f6/0x6c0 [amdgpu]
  svm_range_unmap_from_gpus+0x115/0x300 [amdgpu]
  svm_range_cpu_invalidate_pagetables+0x510/0x5e0 [amdgpu]
  __mmu_notifier_invalidate_range_start+0x1d3/0x230
  unmap_vmas+0x140/0x150
  unmap_region+0xa8/0x110
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h    |  3 ++
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c | 40 +++++++++++++++++++----
 2 files changed, 37 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +3 -0
index 9ecb7f663e19..ac7cd2c738e5 100644
@@ -150,6 +150,9 @@ struct amdgpu_vm_bo_base {
 
 	/* protected by the BO being reserved */
 	bool				moved;
+
+	/* work to free pt bo if vm resv is not hold */
+	struct work_struct              free_work;
 };
 
 /* provided by hw blocks that can write ptes, e.g., sdma */
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +34 -6
index 88de9f0d4728..e6f6d7e6368f 100644
@@ -37,6 +37,8 @@ struct amdgpu_vm_pt_cursor {
 	unsigned int level;
 };
 
+static void amdgpu_vm_pt_free_work(struct work_struct *work);
+
 /**
  * amdgpu_vm_pt_level_shift - return the addr shift for each level
  *
@@ -607,6 +609,7 @@ static int amdgpu_vm_pt_alloc(struct amdgpu_device *adev,
 	pt_bo = &pt->bo;
 	pt_bo->parent = amdgpu_bo_ref(cursor->parent->bo);
 	amdgpu_vm_bo_base_init(entry, vm, pt_bo);
+	INIT_WORK(&entry->free_work, amdgpu_vm_pt_free_work);
 	r = amdgpu_vm_pt_clear(adev, vm, pt, immediate);
 	if (r)
 		goto error_free_pt;
@@ -624,23 +627,46 @@ static int amdgpu_vm_pt_alloc(struct amdgpu_device *adev,
  *
  * @entry: PDE to free
  */
-static void amdgpu_vm_pt_free(struct amdgpu_vm_bo_base *entry)
+static void amdgpu_vm_pt_free(struct amdgpu_vm_bo_base *entry, bool unlocked)
 {
 	struct amdgpu_bo *shadow;
 
 	if (!entry->bo)
 		return;
+
+	if (unlocked) {
+		schedule_work(&entry->free_work);
+		return;
+	}
+
 	shadow = amdgpu_bo_shadowed(entry->bo);
 	if (shadow) {
 		ttm_bo_set_bulk_move(&shadow->tbo, NULL);
 		amdgpu_bo_unref(&shadow);
 	}
 	ttm_bo_set_bulk_move(&entry->bo->tbo, NULL);
+
 	entry->bo->vm_bo = NULL;
 	list_del(&entry->vm_status);
 	amdgpu_bo_unref(&entry->bo);
 }
 
+static void amdgpu_vm_pt_free_work(struct work_struct *work)
+{
+	struct amdgpu_vm_bo_base *entry;
+	struct amdgpu_bo *root;
+
+	entry = container_of(work, struct amdgpu_vm_bo_base, delayed_work);
+
+	root = amdgpu_bo_ref(entry->vm->root.bo);
+	amdgpu_bo_reserve(root, true);
+
+	amdgpu_vm_pt_free(entry, true);
+
+	amdgpu_bo_unreserve(root);
+	amdgpu_bo_unref(&root);
+}
+
 /**
  * amdgpu_vm_pt_free_dfs - free PD/PT levels
  *
@@ -652,16 +678,17 @@ static void amdgpu_vm_pt_free(struct amdgpu_vm_bo_base *entry)
  */
 static void amdgpu_vm_pt_free_dfs(struct amdgpu_device *adev,
 				  struct amdgpu_vm *vm,
-				  struct amdgpu_vm_pt_cursor *start)
+				  struct amdgpu_vm_pt_cursor *start,
+				  bool unlocked)
 {
 	struct amdgpu_vm_pt_cursor cursor;
 	struct amdgpu_vm_bo_base *entry;
 
 	for_each_amdgpu_vm_pt_dfs_safe(adev, vm, start, cursor, entry)
-		amdgpu_vm_pt_free(entry);
+		amdgpu_vm_pt_free(entry, unlocked);
 
 	if (start)
-		amdgpu_vm_pt_free(start->entry);
+		amdgpu_vm_pt_free(start->entry, unlocked);
 }
 
 /**
@@ -673,7 +700,7 @@ static void amdgpu_vm_pt_free_dfs(struct amdgpu_device *adev,
  */
 void amdgpu_vm_pt_free_root(struct amdgpu_device *adev, struct amdgpu_vm *vm)
 {
-	amdgpu_vm_pt_free_dfs(adev, vm, NULL);
+	amdgpu_vm_pt_free_dfs(adev, vm, NULL, false);
 }
 
 /**
@@ -966,7 +993,8 @@ int amdgpu_vm_ptes_update(struct amdgpu_vm_update_params *params,
 				if (cursor.entry->bo) {
 					params->table_freed = true;
 					amdgpu_vm_pt_free_dfs(adev, params->vm,
-							      &cursor);
+							      &cursor,
+							      params->unlocked);
 				}
 				amdgpu_vm_pt_next(adev, &cursor);
 			}
Signature · 1 line
2.35.1
#1 lore raw

Am 2022-09-09 um 14:25 schrieb Philip Yang:

Quoted 104 lines

Free page table BO from vm resv unlocked context generate below warnings.

Add a free_work in vm_bo base of each page table BO, pass vm resv unlock status from page table update caller, and schedule the work if calling with vm resv unlocked.

  WARNING: CPU: 12 PID: 3238 at
  drivers/gpu/drm/ttm/ttm_bo.c:106 ttm_bo_set_bulk_move+0xa1/0xc0
  Call Trace:
   amdgpu_vm_pt_free+0x42/0xd0 [amdgpu]
   amdgpu_vm_pt_free_dfs+0xb3/0xf0 [amdgpu]
   amdgpu_vm_ptes_update+0x52d/0x850 [amdgpu]
   amdgpu_vm_update_range+0x2a6/0x640 [amdgpu]
   svm_range_unmap_from_gpus+0x110/0x300 [amdgpu]
   svm_range_cpu_invalidate_pagetables+0x535/0x600 [amdgpu]
  __mmu_notifier_invalidate_range_start+0x1cd/0x230
   unmap_vmas+0x9d/0x140
   unmap_region+0xa8/0x110

  WARNING: CPU: 0 PID: 1475 at
  drivers/dma-buf/dma-resv.c:483 dma_resv_iter_next
  Call Trace:
   dma_resv_iter_first+0x43/0xa0
   amdgpu_vm_sdma_update+0x69/0x2d0 [amdgpu]
   amdgpu_vm_ptes_update+0x29c/0x870 [amdgpu]
   amdgpu_vm_update_range+0x2f6/0x6c0 [amdgpu]
   svm_range_unmap_from_gpus+0x115/0x300 [amdgpu]
   svm_range_cpu_invalidate_pagetables+0x510/0x5e0 [amdgpu]
   __mmu_notifier_invalidate_range_start+0x1d3/0x230
   unmap_vmas+0x140/0x150
   unmap_region+0xa8/0x110
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h    |  3 ++
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c | 40 +++++++++++++++++++----
  2 files changed, 37 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +3 -0
index 9ecb7f663e19..ac7cd2c738e5 100644
@@ -150,6 +150,9 @@ struct amdgpu_vm_bo_base {
 
  	/* protected by the BO being reserved */
  	bool				moved;
+
+	/* work to free pt bo if vm resv is not hold */
+	struct work_struct              free_work;
  };
 
  /* provided by hw blocks that can write ptes, e.g., sdma */
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +12 -1
index 88de9f0d4728..e6f6d7e6368f 100644
@@ -37,6 +37,8 @@ struct amdgpu_vm_pt_cursor {
  	unsigned int level;
  };
 
+static void amdgpu_vm_pt_free_work(struct work_struct *work);
+
  /**
   * amdgpu_vm_pt_level_shift - return the addr shift for each level
   *
@@ -607,6 +609,7 @@ static int amdgpu_vm_pt_alloc(struct amdgpu_device *adev,
  	pt_bo = &pt->bo;
  	pt_bo->parent = amdgpu_bo_ref(cursor->parent->bo);
  	amdgpu_vm_bo_base_init(entry, vm, pt_bo);
+	INIT_WORK(&entry->free_work, amdgpu_vm_pt_free_work);
  	r = amdgpu_vm_pt_clear(adev, vm, pt, immediate);
  	if (r)
  		goto error_free_pt;
@@ -624,23 +627,46 @@ static int amdgpu_vm_pt_alloc(struct amdgpu_device *adev,
   *
   * @entry: PDE to free
   */
-static void amdgpu_vm_pt_free(struct amdgpu_vm_bo_base *entry)
+static void amdgpu_vm_pt_free(struct amdgpu_vm_bo_base *entry, bool unlocked)
  {
  	struct amdgpu_bo *shadow;
 
  	if (!entry->bo)
  		return;
+
+	if (unlocked) {
+		schedule_work(&entry->free_work);
+		return;
+	}
+
  	shadow = amdgpu_bo_shadowed(entry->bo);
  	if (shadow) {
  		ttm_bo_set_bulk_move(&shadow->tbo, NULL);
  		amdgpu_bo_unref(&shadow);
  	}
  	ttm_bo_set_bulk_move(&entry->bo->tbo, NULL);
+
  	entry->bo->vm_bo = NULL;
  	list_del(&entry->vm_status);
  	amdgpu_bo_unref(&entry->bo);
  }
 
+static void amdgpu_vm_pt_free_work(struct work_struct *work)

drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c static int amdgpu_vm_pt_alloc(struct amdgpu_device *adev, · line 664

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +10 -0
+{
+	struct amdgpu_vm_bo_base *entry;
+	struct amdgpu_bo *root;
+
+	entry = container_of(work, struct amdgpu_vm_bo_base, delayed_work);
+
+	root = amdgpu_bo_ref(entry->vm->root.bo);
+	amdgpu_bo_reserve(root, true);
+
+	amdgpu_vm_pt_free(entry, true);

Doesn't this just cause the work to be scheduled again, rather than actually freeing the BO?

Regards,
  Felix

Quoted 48 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +12 -5
+
+	amdgpu_bo_unreserve(root);
+	amdgpu_bo_unref(&root);
+}
+
  /**
   * amdgpu_vm_pt_free_dfs - free PD/PT levels
   *
@@ -652,16 +678,17 @@ static void amdgpu_vm_pt_free(struct amdgpu_vm_bo_base *entry)
   */
  static void amdgpu_vm_pt_free_dfs(struct amdgpu_device *adev,
  				  struct amdgpu_vm *vm,
-				  struct amdgpu_vm_pt_cursor *start)
+				  struct amdgpu_vm_pt_cursor *start,
+				  bool unlocked)
  {
  	struct amdgpu_vm_pt_cursor cursor;
  	struct amdgpu_vm_bo_base *entry;
 
  	for_each_amdgpu_vm_pt_dfs_safe(adev, vm, start, cursor, entry)
-		amdgpu_vm_pt_free(entry);
+		amdgpu_vm_pt_free(entry, unlocked);
 
  	if (start)
-		amdgpu_vm_pt_free(start->entry);
+		amdgpu_vm_pt_free(start->entry, unlocked);
  }
 
  /**
@@ -673,7 +700,7 @@ static void amdgpu_vm_pt_free_dfs(struct amdgpu_device *adev,
   */
  void amdgpu_vm_pt_free_root(struct amdgpu_device *adev, struct amdgpu_vm *vm)
  {
-	amdgpu_vm_pt_free_dfs(adev, vm, NULL);
+	amdgpu_vm_pt_free_dfs(adev, vm, NULL, false);
  }
 
  /**
@@ -966,7 +993,8 @@ int amdgpu_vm_ptes_update(struct amdgpu_vm_update_params *params,
  				if (cursor.entry->bo) {
  					params->table_freed = true;
  					amdgpu_vm_pt_free_dfs(adev, params->vm,
-							      &cursor);
+							      &cursor,
+							      params->unlocked);
  				}
  				amdgpu_vm_pt_next(adev, &cursor);
  			}
#2 lore raw
Quoted 107 lines · Felix Kuehling

Am 2022-09-09 um 14:25 schrieb Philip Yang:

Quoted 106 lines

Free page table BO from vm resv unlocked context generate below warnings.

Add a free_work in vm_bo base of each page table BO, pass vm resv unlock status from page table update caller, and schedule the work if calling with vm resv unlocked.

  WARNING: CPU: 12 PID: 3238 at
  drivers/gpu/drm/ttm/ttm_bo.c:106 ttm_bo_set_bulk_move+0xa1/0xc0
  Call Trace:
   amdgpu_vm_pt_free+0x42/0xd0 [amdgpu]
   amdgpu_vm_pt_free_dfs+0xb3/0xf0 [amdgpu]
   amdgpu_vm_ptes_update+0x52d/0x850 [amdgpu]
   amdgpu_vm_update_range+0x2a6/0x640 [amdgpu]
   svm_range_unmap_from_gpus+0x110/0x300 [amdgpu]
   svm_range_cpu_invalidate_pagetables+0x535/0x600 [amdgpu]
  __mmu_notifier_invalidate_range_start+0x1cd/0x230
   unmap_vmas+0x9d/0x140
   unmap_region+0xa8/0x110

  WARNING: CPU: 0 PID: 1475 at
  drivers/dma-buf/dma-resv.c:483 dma_resv_iter_next
  Call Trace:
   dma_resv_iter_first+0x43/0xa0
   amdgpu_vm_sdma_update+0x69/0x2d0 [amdgpu]
   amdgpu_vm_ptes_update+0x29c/0x870 [amdgpu]
   amdgpu_vm_update_range+0x2f6/0x6c0 [amdgpu]
   svm_range_unmap_from_gpus+0x115/0x300 [amdgpu]
   svm_range_cpu_invalidate_pagetables+0x510/0x5e0 [amdgpu]
   __mmu_notifier_invalidate_range_start+0x1d3/0x230
   unmap_vmas+0x140/0x150
   unmap_region+0xa8/0x110

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h    |  3 ++ drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c | 40 +++++++++++++++++++----

  2 files changed, 37 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +3 -0
index 9ecb7f663e19..ac7cd2c738e5 100644
@@ -150,6 +150,9 @@ struct amdgpu_vm_bo_base {
         /* protected by the BO being reserved */
       bool                moved;
+
+    /* work to free pt bo if vm resv is not hold */
+    struct work_struct              free_work;
   };
     /* provided by hw blocks that can write ptes, e.g., sdma */
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +2 -0
index 88de9f0d4728..e6f6d7e6368f 100644
@@ -37,6 +37,8 @@ struct amdgpu_vm_pt_cursor {
       unsigned int level;
   };
   +static void amdgpu_vm_pt_free_work(struct work_struct *work);
+
   /**
    * amdgpu_vm_pt_level_shift - return the addr shift for each level
    *
@@ -607,6 +609,7 @@ static int amdgpu_vm_pt_alloc(struct 
 amdgpu_device *adev,
       pt_bo = &pt->bo;
       pt_bo->parent = amdgpu_bo_ref(cursor->parent->bo);
       amdgpu_vm_bo_base_init(entry, vm, pt_bo);
+    INIT_WORK(&entry->free_work, amdgpu_vm_pt_free_work);
       r = amdgpu_vm_pt_clear(adev, vm, pt, immediate);
       if (r)

          goto error_free_pt;

drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +12 -1
@@ -624,23 +627,46 @@ static int amdgpu_vm_pt_alloc(struct 
 amdgpu_device *adev,
    *
    * @entry: PDE to free
    */
-static void amdgpu_vm_pt_free(struct amdgpu_vm_bo_base *entry)
+static void amdgpu_vm_pt_free(struct amdgpu_vm_bo_base *entry, bool 
 unlocked)
   {
       struct amdgpu_bo *shadow;
         if (!entry->bo)
           return;
+
+    if (unlocked) {
+        schedule_work(&entry->free_work);
+        return;
+    }
+
       shadow = amdgpu_bo_shadowed(entry->bo);
       if (shadow) {
           ttm_bo_set_bulk_move(&shadow->tbo, NULL);
           amdgpu_bo_unref(&shadow);
       }
       ttm_bo_set_bulk_move(&entry->bo->tbo, NULL);
+
       entry->bo->vm_bo = NULL;
       list_del(&entry->vm_status);
       amdgpu_bo_unref(&entry->bo);
   }
   +static void amdgpu_vm_pt_free_work(struct work_struct *work)
+{
+    struct amdgpu_vm_bo_base *entry;
+    struct amdgpu_bo *root;
+
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c static int amdgpu_vm_pt_alloc(struct amdgpu_device *adev, · line 664

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +6 -0
+    entry = container_of(work, struct amdgpu_vm_bo_base, delayed_work);
+
+    root = amdgpu_bo_ref(entry->vm->root.bo);
+    amdgpu_bo_reserve(root, true);
+
+    amdgpu_vm_pt_free(entry, true);

Doesn't this just cause the work to be scheduled again, rather than actually freeing the BO?

You are right, I will send v2 using Christian's suggestion to use a single worker in the vm.

Thanks,

Philip

Quoted 52 lines

Regards,
  Felix

Quoted 47 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +5 -0
+
+    amdgpu_bo_unreserve(root);
+    amdgpu_bo_unref(&root);
+}
+
   /**
    * amdgpu_vm_pt_free_dfs - free PD/PT levels

   *

drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +7 -5
@@ -652,16 +678,17 @@ static void amdgpu_vm_pt_free(struct 
 amdgpu_vm_bo_base *entry)
    */
   static void amdgpu_vm_pt_free_dfs(struct amdgpu_device *adev,
                     struct amdgpu_vm *vm,
-                  struct amdgpu_vm_pt_cursor *start)
+                  struct amdgpu_vm_pt_cursor *start,
+                  bool unlocked)
   {
       struct amdgpu_vm_pt_cursor cursor;
       struct amdgpu_vm_bo_base *entry;
         for_each_amdgpu_vm_pt_dfs_safe(adev, vm, start, cursor, entry)
-        amdgpu_vm_pt_free(entry);
+        amdgpu_vm_pt_free(entry, unlocked);
         if (start)
-        amdgpu_vm_pt_free(start->entry);
+        amdgpu_vm_pt_free(start->entry, unlocked);
   }
     /**
@@ -673,7 +700,7 @@ static void amdgpu_vm_pt_free_dfs(struct 
 amdgpu_device *adev,
    */
   void amdgpu_vm_pt_free_root(struct amdgpu_device *adev, struct amdgpu_vm *vm)
   {
-    amdgpu_vm_pt_free_dfs(adev, vm, NULL);
+    amdgpu_vm_pt_free_dfs(adev, vm, NULL, false);
   }
     /**
@@ -966,7 +993,8 @@ int amdgpu_vm_ptes_update(struct 
 amdgpu_vm_update_params *params,
                   if (cursor.entry->bo) {
                       params->table_freed = true;
                       amdgpu_vm_pt_free_dfs(adev, params->vm,
-                                  &cursor);
+                                  &cursor,
+                                  params->unlocked);
                   }
                   amdgpu_vm_pt_next(adev, &cursor);

              }

#3 lore raw
In reply to Philip Yang

Am 09.09.22 um 20:25 schrieb Philip Yang:

Quoted 44 lines

Free page table BO from vm resv unlocked context generate below warnings.

Add a free_work in vm_bo base of each page table BO, pass vm resv unlock status from page table update caller, and schedule the work if calling with vm resv unlocked.

  WARNING: CPU: 12 PID: 3238 at
  drivers/gpu/drm/ttm/ttm_bo.c:106 ttm_bo_set_bulk_move+0xa1/0xc0
  Call Trace:
   amdgpu_vm_pt_free+0x42/0xd0 [amdgpu]
   amdgpu_vm_pt_free_dfs+0xb3/0xf0 [amdgpu]
   amdgpu_vm_ptes_update+0x52d/0x850 [amdgpu]
   amdgpu_vm_update_range+0x2a6/0x640 [amdgpu]
   svm_range_unmap_from_gpus+0x110/0x300 [amdgpu]
   svm_range_cpu_invalidate_pagetables+0x535/0x600 [amdgpu]
  __mmu_notifier_invalidate_range_start+0x1cd/0x230
   unmap_vmas+0x9d/0x140
   unmap_region+0xa8/0x110

  WARNING: CPU: 0 PID: 1475 at
  drivers/dma-buf/dma-resv.c:483 dma_resv_iter_next
  Call Trace:
   dma_resv_iter_first+0x43/0xa0
   amdgpu_vm_sdma_update+0x69/0x2d0 [amdgpu]
   amdgpu_vm_ptes_update+0x29c/0x870 [amdgpu]
   amdgpu_vm_update_range+0x2f6/0x6c0 [amdgpu]
   svm_range_unmap_from_gpus+0x115/0x300 [amdgpu]
   svm_range_cpu_invalidate_pagetables+0x510/0x5e0 [amdgpu]
   __mmu_notifier_invalidate_range_start+0x1d3/0x230
   unmap_vmas+0x140/0x150
   unmap_region+0xa8/0x110
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h    |  3 ++
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c | 40 +++++++++++++++++++----
  2 files changed, 37 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +0 -0
index 9ecb7f663e19..ac7cd2c738e5 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h struct amdgpu_vm_bo_base { · line 155

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +3 -0
@@ -150,6 +150,9 @@ struct amdgpu_vm_bo_base {
 
  	/* protected by the BO being reserved */
  	bool				moved;
+
+	/* work to free pt bo if vm resv is not hold */
+	struct work_struct              free_work;

Oh, this is a pretty clear NAK. We can't increase the size of this struct.

Instead just come up with a single worker in the VM and a new state for the page tables.

Regards,
Christian.

Quoted 112 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +0 -0
  };
 
  /* provided by hw blocks that can write ptes, e.g., sdma */
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +34 -6
index 88de9f0d4728..e6f6d7e6368f 100644
@@ -37,6 +37,8 @@ struct amdgpu_vm_pt_cursor {
  	unsigned int level;
  };
 
+static void amdgpu_vm_pt_free_work(struct work_struct *work);
+
  /**
   * amdgpu_vm_pt_level_shift - return the addr shift for each level
   *
@@ -607,6 +609,7 @@ static int amdgpu_vm_pt_alloc(struct amdgpu_device *adev,
  	pt_bo = &pt->bo;
  	pt_bo->parent = amdgpu_bo_ref(cursor->parent->bo);
  	amdgpu_vm_bo_base_init(entry, vm, pt_bo);
+	INIT_WORK(&entry->free_work, amdgpu_vm_pt_free_work);
  	r = amdgpu_vm_pt_clear(adev, vm, pt, immediate);
  	if (r)
  		goto error_free_pt;
@@ -624,23 +627,46 @@ static int amdgpu_vm_pt_alloc(struct amdgpu_device *adev,
   *
   * @entry: PDE to free
   */
-static void amdgpu_vm_pt_free(struct amdgpu_vm_bo_base *entry)
+static void amdgpu_vm_pt_free(struct amdgpu_vm_bo_base *entry, bool unlocked)
  {
  	struct amdgpu_bo *shadow;
 
  	if (!entry->bo)
  		return;
+
+	if (unlocked) {
+		schedule_work(&entry->free_work);
+		return;
+	}
+
  	shadow = amdgpu_bo_shadowed(entry->bo);
  	if (shadow) {
  		ttm_bo_set_bulk_move(&shadow->tbo, NULL);
  		amdgpu_bo_unref(&shadow);
  	}
  	ttm_bo_set_bulk_move(&entry->bo->tbo, NULL);
+
  	entry->bo->vm_bo = NULL;
  	list_del(&entry->vm_status);
  	amdgpu_bo_unref(&entry->bo);
  }
 
+static void amdgpu_vm_pt_free_work(struct work_struct *work)
+{
+	struct amdgpu_vm_bo_base *entry;
+	struct amdgpu_bo *root;
+
+	entry = container_of(work, struct amdgpu_vm_bo_base, delayed_work);
+
+	root = amdgpu_bo_ref(entry->vm->root.bo);
+	amdgpu_bo_reserve(root, true);
+
+	amdgpu_vm_pt_free(entry, true);
+
+	amdgpu_bo_unreserve(root);
+	amdgpu_bo_unref(&root);
+}
+
  /**
   * amdgpu_vm_pt_free_dfs - free PD/PT levels
   *
@@ -652,16 +678,17 @@ static void amdgpu_vm_pt_free(struct amdgpu_vm_bo_base *entry)
   */
  static void amdgpu_vm_pt_free_dfs(struct amdgpu_device *adev,
  				  struct amdgpu_vm *vm,
-				  struct amdgpu_vm_pt_cursor *start)
+				  struct amdgpu_vm_pt_cursor *start,
+				  bool unlocked)
  {
  	struct amdgpu_vm_pt_cursor cursor;
  	struct amdgpu_vm_bo_base *entry;
 
  	for_each_amdgpu_vm_pt_dfs_safe(adev, vm, start, cursor, entry)
-		amdgpu_vm_pt_free(entry);
+		amdgpu_vm_pt_free(entry, unlocked);
 
  	if (start)
-		amdgpu_vm_pt_free(start->entry);
+		amdgpu_vm_pt_free(start->entry, unlocked);
  }
 
  /**
@@ -673,7 +700,7 @@ static void amdgpu_vm_pt_free_dfs(struct amdgpu_device *adev,
   */
  void amdgpu_vm_pt_free_root(struct amdgpu_device *adev, struct amdgpu_vm *vm)
  {
-	amdgpu_vm_pt_free_dfs(adev, vm, NULL);
+	amdgpu_vm_pt_free_dfs(adev, vm, NULL, false);
  }
 
  /**
@@ -966,7 +993,8 @@ int amdgpu_vm_ptes_update(struct amdgpu_vm_update_params *params,
  				if (cursor.entry->bo) {
  					params->table_freed = true;
  					amdgpu_vm_pt_free_dfs(adev, params->vm,
-							      &cursor);
+							      &cursor,
+							      params->unlocked);
  				}
  				amdgpu_vm_pt_next(adev, &cursor);
  			}
#4 lore raw
Quoted 53 lines · Christian König

Am 09.09.22 um 20:25 schrieb Philip Yang:

Quoted 44 lines

Free page table BO from vm resv unlocked context generate below warnings.

Add a free_work in vm_bo base of each page table BO, pass vm resv unlock status from page table update caller, and schedule the work if calling with vm resv unlocked.

  WARNING: CPU: 12 PID: 3238 at
  drivers/gpu/drm/ttm/ttm_bo.c:106 ttm_bo_set_bulk_move+0xa1/0xc0
  Call Trace:
   amdgpu_vm_pt_free+0x42/0xd0 [amdgpu]
   amdgpu_vm_pt_free_dfs+0xb3/0xf0 [amdgpu]
   amdgpu_vm_ptes_update+0x52d/0x850 [amdgpu]
   amdgpu_vm_update_range+0x2a6/0x640 [amdgpu]
   svm_range_unmap_from_gpus+0x110/0x300 [amdgpu]
   svm_range_cpu_invalidate_pagetables+0x535/0x600 [amdgpu]
  __mmu_notifier_invalidate_range_start+0x1cd/0x230
   unmap_vmas+0x9d/0x140
   unmap_region+0xa8/0x110

  WARNING: CPU: 0 PID: 1475 at
  drivers/dma-buf/dma-resv.c:483 dma_resv_iter_next
  Call Trace:
   dma_resv_iter_first+0x43/0xa0
   amdgpu_vm_sdma_update+0x69/0x2d0 [amdgpu]
   amdgpu_vm_ptes_update+0x29c/0x870 [amdgpu]
   amdgpu_vm_update_range+0x2f6/0x6c0 [amdgpu]
   svm_range_unmap_from_gpus+0x115/0x300 [amdgpu]
   svm_range_cpu_invalidate_pagetables+0x510/0x5e0 [amdgpu]
   __mmu_notifier_invalidate_range_start+0x1d3/0x230
   unmap_vmas+0x140/0x150
   unmap_region+0xa8/0x110

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h    |  3 ++ drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c | 40 +++++++++++++++++++----

  2 files changed, 37 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +0 -0
index 9ecb7f663e19..ac7cd2c738e5 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h struct amdgpu_vm_bo_base { · line 154

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +3 -0
@@ -150,6 +150,9 @@ struct amdgpu_vm_bo_base {
         /* protected by the BO being reserved */
       bool                moved;
+
+    /* work to free pt bo if vm resv is not hold */
+    struct work_struct              free_work;

Oh, this is a pretty clear NAK. We can't increase the size of this struct.

Quoted 1 line

Instead just come up with a single worker in the VM and a new state for the page tables.

Good idea, I agree it is too much overhead to add a worker in each pt bo. I will send v2 patch.

Regards,

Philip

Quoted 115 lines

Regards,
Christian.

Quoted 110 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +0 -0
   };
     /* provided by hw blocks that can write ptes, e.g., sdma */
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +2 -0
index 88de9f0d4728..e6f6d7e6368f 100644
@@ -37,6 +37,8 @@ struct amdgpu_vm_pt_cursor {
       unsigned int level;
   };
   +static void amdgpu_vm_pt_free_work(struct work_struct *work);
+
   /**
    * amdgpu_vm_pt_level_shift - return the addr shift for each level
    *
@@ -607,6 +609,7 @@ static int amdgpu_vm_pt_alloc(struct 
 amdgpu_device *adev,
       pt_bo = &pt->bo;
       pt_bo->parent = amdgpu_bo_ref(cursor->parent->bo);
       amdgpu_vm_bo_base_init(entry, vm, pt_bo);
+    INIT_WORK(&entry->free_work, amdgpu_vm_pt_free_work);
       r = amdgpu_vm_pt_clear(adev, vm, pt, immediate);
       if (r)

          goto error_free_pt;

drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +23 -1
@@ -624,23 +627,46 @@ static int amdgpu_vm_pt_alloc(struct 
 amdgpu_device *adev,
    *
    * @entry: PDE to free
    */
-static void amdgpu_vm_pt_free(struct amdgpu_vm_bo_base *entry)
+static void amdgpu_vm_pt_free(struct amdgpu_vm_bo_base *entry, bool 
 unlocked)
   {
       struct amdgpu_bo *shadow;
         if (!entry->bo)
           return;
+
+    if (unlocked) {
+        schedule_work(&entry->free_work);
+        return;
+    }
+
       shadow = amdgpu_bo_shadowed(entry->bo);
       if (shadow) {
           ttm_bo_set_bulk_move(&shadow->tbo, NULL);
           amdgpu_bo_unref(&shadow);
       }
       ttm_bo_set_bulk_move(&entry->bo->tbo, NULL);
+
       entry->bo->vm_bo = NULL;
       list_del(&entry->vm_status);
       amdgpu_bo_unref(&entry->bo);
   }
   +static void amdgpu_vm_pt_free_work(struct work_struct *work)
+{
+    struct amdgpu_vm_bo_base *entry;
+    struct amdgpu_bo *root;
+
+    entry = container_of(work, struct amdgpu_vm_bo_base, delayed_work);
+
+    root = amdgpu_bo_ref(entry->vm->root.bo);
+    amdgpu_bo_reserve(root, true);
+
+    amdgpu_vm_pt_free(entry, true);
+
+    amdgpu_bo_unreserve(root);
+    amdgpu_bo_unref(&root);
+}
+
   /**
    * amdgpu_vm_pt_free_dfs - free PD/PT levels

   *

drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +7 -5
@@ -652,16 +678,17 @@ static void amdgpu_vm_pt_free(struct 
 amdgpu_vm_bo_base *entry)
    */
   static void amdgpu_vm_pt_free_dfs(struct amdgpu_device *adev,
                     struct amdgpu_vm *vm,
-                  struct amdgpu_vm_pt_cursor *start)
+                  struct amdgpu_vm_pt_cursor *start,
+                  bool unlocked)
   {
       struct amdgpu_vm_pt_cursor cursor;
       struct amdgpu_vm_bo_base *entry;
         for_each_amdgpu_vm_pt_dfs_safe(adev, vm, start, cursor, entry)
-        amdgpu_vm_pt_free(entry);
+        amdgpu_vm_pt_free(entry, unlocked);
         if (start)
-        amdgpu_vm_pt_free(start->entry);
+        amdgpu_vm_pt_free(start->entry, unlocked);
   }
     /**
@@ -673,7 +700,7 @@ static void amdgpu_vm_pt_free_dfs(struct 
 amdgpu_device *adev,
    */
   void amdgpu_vm_pt_free_root(struct amdgpu_device *adev, struct amdgpu_vm *vm)
   {
-    amdgpu_vm_pt_free_dfs(adev, vm, NULL);
+    amdgpu_vm_pt_free_dfs(adev, vm, NULL, false);
   }
     /**
@@ -966,7 +993,8 @@ int amdgpu_vm_ptes_update(struct 
 amdgpu_vm_update_params *params,
                   if (cursor.entry->bo) {
                       params->table_freed = true;
                       amdgpu_vm_pt_free_dfs(adev, params->vm,
-                                  &cursor);
+                                  &cursor,
+                                  params->unlocked);
                   }
                   amdgpu_vm_pt_next(adev, &cursor);

              }