Skip to contentSkip to conversation

drm/amdgpu: fix TLB invalidation fence handling

PATCH In review Christian König 5 participants 6 messages Oct 8 – Oct 19

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

PATCH 1/1 drm/amdgpu: fix TLB invalidation fence handling

After each unmap it might be necessary to invalidate the TLB to flush out VM changes to the HW and make it visible to executing work.

We tracked that as part of the vm->last_update fence, but as pointed out by David it can be that this is overwritten by PDE updates when we have a combined unmap/map operation.

In that case userspace doesn't correctly wait for the TLB invalidation to finish leading to all kind of problems (sq_int, page faults etc...).

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c       | 20 ++++++----
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c        | 39 +++++++++----------
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h        | 13 ++++---
 .../gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c  | 21 +++++++---
 4 files changed, 55 insertions(+), 38 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +13 -7
index 374f1322c3ee4..606725d0eb155 100644
@@ -843,18 +843,24 @@ amdgpu_gem_va_update_vm(struct amdgpu_device *adev,
 		/*
 		 * For MAP/REPLACE of non per-VM BOs we need to sync to both the
 		 * bo_va->last_pt_update and vm->last_update or otherwise we
-		 * potentially miss the PDE updates.
+		 * potentially miss the PDE updates or TLB invalidation.
 		 */
 		fence = dma_fence_unwrap_merge(vm->last_update,
+					       vm->last_tlb_inv,
 					       bo_va->last_pt_update);
-		if (!fence) {
-			/* As fallback in OOM situations */
-			dma_fence_wait(vm->last_update, false);
+		if (!fence)
 			dma_fence_wait(bo_va->last_pt_update, false);
-			fence = dma_fence_get_stub();
-		}
+
 	} else {
-		fence = dma_fence_get(vm->last_update);
+		fence = dma_fence_unwrap_merge(vm->last_update,
+					       vm->last_tlb_inv);
+	}
+
+	if (!fence) {
+		/* As fallback in OOM situations */
+		dma_fence_wait(vm->last_update, false);
+		dma_fence_wait(vm->last_tlb_inv, false);
+		fence = dma_fence_get_stub();
 	}
 
 	return fence;
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +19 -20
index 7d3659ef11e53..f124320345ec4 100644
@@ -1071,21 +1071,16 @@ static void amdgpu_vm_tlb_seq_cb(struct dma_fence *fence,
  */
 static void
 amdgpu_vm_tlb_flush(struct amdgpu_vm_update_params *params,
-		    struct dma_fence **fence,
+		    struct dma_fence *fence,
 		    struct amdgpu_vm_tlb_seq_struct *tlb_cb)
 {
 	struct amdgpu_vm *vm = params->vm;
 
 	tlb_cb->vm = vm;
-	if (!fence || !*fence) {
-		amdgpu_vm_tlb_seq_cb(NULL, &tlb_cb->cb);
-		return;
-	}
-
-	if (!dma_fence_add_callback(*fence, &tlb_cb->cb,
-				    amdgpu_vm_tlb_seq_cb)) {
-		dma_fence_put(vm->last_tlb_flush);
-		vm->last_tlb_flush = dma_fence_get(*fence);
+	if (!fence || !dma_fence_add_callback(fence, &tlb_cb->cb,
+					      amdgpu_vm_tlb_seq_cb)) {
+		dma_fence_put(vm->last_seq_flush);
+		vm->last_seq_flush = dma_fence_get(fence);
 	} else {
 		amdgpu_vm_tlb_seq_cb(NULL, &tlb_cb->cb);
 	}
@@ -1095,11 +1090,12 @@ amdgpu_vm_tlb_flush(struct amdgpu_vm_update_params *params,
 	 * sort out the issues with KIQ/MES TLB invalidation timeouts.
 	 */
 	if (!params->unlocked && vm->need_tlb_fence) {
-		amdgpu_vm_tlb_fence_create(params->adev, vm, fence);
+		amdgpu_vm_tlb_fence_create(params->adev, vm, fence,
+					   &vm->last_tlb_inv);
 
 		/* Makes sure no PD/PT is freed before the flush */
-		dma_resv_add_fence(vm->root.bo->tbo.base.resv, *fence,
-				   DMA_RESV_USAGE_BOOKKEEP);
+		dma_resv_add_fence(vm->root.bo->tbo.base.resv,
+				   vm->last_tlb_inv, DMA_RESV_USAGE_BOOKKEEP);
 	}
 }
 
@@ -1248,7 +1244,7 @@ int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 		goto error_free;
 
 	if (params.needs_flush) {
-		amdgpu_vm_tlb_flush(&params, fence, tlb_cb);
+		amdgpu_vm_tlb_flush(&params, *fence, tlb_cb);
 		tlb_cb = NULL;
 	}
 
@@ -2647,7 +2643,8 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 
 	vm->last_update = dma_fence_get_stub();
 	vm->last_unlocked = dma_fence_get_stub();
-	vm->last_tlb_flush = dma_fence_get_stub();
+	vm->last_seq_flush = dma_fence_get_stub();
+	vm->last_tlb_inv = dma_fence_get_stub();
 	vm->generation = amdgpu_vm_generation(adev, NULL);
 
 	mutex_init(&vm->eviction_lock);
@@ -2691,7 +2688,8 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 
 error_free_delayed:
 	dma_fence_put(vm->last_update);
-	dma_fence_put(vm->last_tlb_flush);
+	dma_fence_put(vm->last_seq_flush);
+	dma_fence_put(vm->last_tlb_inv);
 	dma_fence_put(vm->last_unlocked);
 	ttm_lru_bulk_move_fini(&adev->mman.bdev, &vm->lru_bulk_move);
 	amdgpu_vm_fini_entities(vm);
@@ -2834,11 +2832,12 @@ void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
 	amdgpu_bo_reserve(root, true);
 	dma_fence_wait(vm->last_unlocked, false);
 	dma_fence_put(vm->last_unlocked);
-	dma_fence_wait(vm->last_tlb_flush, false);
+	dma_fence_wait(vm->last_seq_flush, false);
 	/* Make sure that all fence callbacks have completed */
-	dma_fence_lock_irqsave(vm->last_tlb_flush, flags);
-	dma_fence_unlock_irqrestore(vm->last_tlb_flush, flags);
-	dma_fence_put(vm->last_tlb_flush);
+	dma_fence_lock_irqsave(vm->last_seq_flush, flags);
+	dma_fence_unlock_irqrestore(vm->last_seq_flush, flags);
+	dma_fence_put(vm->last_seq_flush);
+	dma_fence_put(vm->last_tlb_inv);
 
 	list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
 		if (mapping->flags & AMDGPU_VM_PAGE_PRT && prt_fini_needed) {
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +7 -6
index 8e478d3bda1ac..713a597027bbe 100644
@@ -328,11 +328,12 @@ struct amdgpu_vm {
 	struct drm_sched_entity	immediate;
 	struct drm_sched_entity	delayed;
 
-	/* Last finished delayed update */
+	/* Last TLB seq/fence */
 	atomic64_t		tlb_seq;
-	struct dma_fence	*last_tlb_flush;
+	struct dma_fence	*last_seq_flush;
 	atomic64_t		kfd_last_flushed_seq;
 	uint64_t		tlb_fence_context;
+	struct dma_fence	*last_tlb_inv;
 
 	/* How many times we had to re-generate the page tables */
 	uint64_t		generation;
@@ -561,7 +562,7 @@ static inline uint64_t amdgpu_vm_tlb_seq(struct amdgpu_vm *vm)
 	 * sure that the dma_fence structure isn't freed up.
 	 */
 	rcu_read_lock();
-	lock = dma_fence_spinlock(vm->last_tlb_flush);
+	lock = dma_fence_spinlock(vm->last_seq_flush);
 	rcu_read_unlock();
 
 	spin_lock_irqsave(lock, flags);
@@ -576,9 +577,9 @@ void amdgpu_vm_update_fault_cache(struct amdgpu_device *adev,
 				  uint32_t status,
 				  unsigned int vmhub);
 void amdgpu_vm_tlb_fence_create(struct amdgpu_device *adev,
-				 struct amdgpu_vm *vm,
-				 struct dma_fence **fence);
-
+				struct amdgpu_vm *vm,
+				struct dma_fence *dep,
+				struct dma_fence **fence);
 void amdgpu_vm_print_task_info(struct amdgpu_device *adev,
 			       struct amdgpu_task_info *task_info);
 
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c +16 -5
index b0002c8983861..c91417e4df4ed 100644
@@ -75,7 +75,19 @@ static const struct dma_fence_ops amdgpu_tlb_fence_ops = {
 	.get_timeline_name = amdgpu_tlb_fence_get_timeline_name
 };
 
-void amdgpu_vm_tlb_fence_create(struct amdgpu_device *adev, struct amdgpu_vm *vm,
+/**
+ * amdgpu_vm_tlb_fence_create - create a TLB invalidation fence
+ * @adev: the amdgpu device
+ * @vm: the vm to use
+ * @dep: the dependency to wait for
+ * @fence: the resulting fence
+ *
+ * Creates a TLB invalidation fence which signals when all the VMIDs assigned to
+ * the PASID of the VM are invalidated.
+ */
+void amdgpu_vm_tlb_fence_create(struct amdgpu_device *adev,
+				struct amdgpu_vm *vm,
+				struct dma_fence *dep,
 				struct dma_fence **fence)
 {
 	struct amdgpu_tlb_fence *f;
@@ -86,16 +98,15 @@ void amdgpu_vm_tlb_fence_create(struct amdgpu_device *adev, struct amdgpu_vm *vm
 		 * We can't fail since the PDEs and PTEs are already updated, so
 		 * just block for the dependency and execute the TLB flush
 		 */
-		if (*fence)
-			dma_fence_wait(*fence, false);
+		if (dep)
+			dma_fence_wait(dep, false);
 
 		amdgpu_gmc_flush_gpu_tlb_pasid(adev, vm->pasid, 2, true, 0);
-		*fence = dma_fence_get_stub();
 		return;
 	}
 
 	f->adev = adev;
-	f->dependency = *fence;
+	f->dependency = dma_fence_get(dep);
 	f->pasid = vm->pasid;
 	INIT_WORK(&f->work, amdgpu_tlb_fence_work);
 	spin_lock_init(&f->lock);
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Christian König

After each unmap it might be necessary to invalidate the TLB to flush out VM changes to the HW and make it visible to executing work.

We tracked that as part of the vm->last_update fence, but as pointed out by David it can be that this is overwritten by PDE updates when we have a combined unmap/map operation.

In that case userspace doesn't correctly wait for the TLB invalidation to finish leading to all kind of problems (sq_int, page faults etc...).

If I understand it correctly, you fixed this by tracking two different fences in amdgpu_vm: one that updates the sequence number, and one that signals when the TLB flush completes.

Quoted 2 lines

Why was [email protected] copied on this email? Anyway, the patch looks good to me.

Quoted 222 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c       | 20 ++++++----
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c        | 39 +++++++++----------
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h        | 13 ++++---
  .../gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c  | 21 +++++++---
  4 files changed, 55 insertions(+), 38 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +13 -7
index 374f1322c3ee4..606725d0eb155 100644
@@ -843,18 +843,24 @@ amdgpu_gem_va_update_vm(struct amdgpu_device *adev,
  		/*
  		 * For MAP/REPLACE of non per-VM BOs we need to sync to both the
  		 * bo_va->last_pt_update and vm->last_update or otherwise we
-		 * potentially miss the PDE updates.
+		 * potentially miss the PDE updates or TLB invalidation.
  		 */
  		fence = dma_fence_unwrap_merge(vm->last_update,
+					       vm->last_tlb_inv,
  					       bo_va->last_pt_update);
-		if (!fence) {
-			/* As fallback in OOM situations */
-			dma_fence_wait(vm->last_update, false);
+		if (!fence)
  			dma_fence_wait(bo_va->last_pt_update, false);
-			fence = dma_fence_get_stub();
-		}
+
  	} else {
-		fence = dma_fence_get(vm->last_update);
+		fence = dma_fence_unwrap_merge(vm->last_update,
+					       vm->last_tlb_inv);
+	}
+
+	if (!fence) {
+		/* As fallback in OOM situations */
+		dma_fence_wait(vm->last_update, false);
+		dma_fence_wait(vm->last_tlb_inv, false);
+		fence = dma_fence_get_stub();
  	}
 
  	return fence;
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +19 -20
index 7d3659ef11e53..f124320345ec4 100644
@@ -1071,21 +1071,16 @@ static void amdgpu_vm_tlb_seq_cb(struct dma_fence *fence,
   */
  static void
  amdgpu_vm_tlb_flush(struct amdgpu_vm_update_params *params,
-		    struct dma_fence **fence,
+		    struct dma_fence *fence,
  		    struct amdgpu_vm_tlb_seq_struct *tlb_cb)
  {
  	struct amdgpu_vm *vm = params->vm;
 
  	tlb_cb->vm = vm;
-	if (!fence || !*fence) {
-		amdgpu_vm_tlb_seq_cb(NULL, &tlb_cb->cb);
-		return;
-	}
-
-	if (!dma_fence_add_callback(*fence, &tlb_cb->cb,
-				    amdgpu_vm_tlb_seq_cb)) {
-		dma_fence_put(vm->last_tlb_flush);
-		vm->last_tlb_flush = dma_fence_get(*fence);
+	if (!fence || !dma_fence_add_callback(fence, &tlb_cb->cb,
+					      amdgpu_vm_tlb_seq_cb)) {
+		dma_fence_put(vm->last_seq_flush);
+		vm->last_seq_flush = dma_fence_get(fence);
  	} else {
  		amdgpu_vm_tlb_seq_cb(NULL, &tlb_cb->cb);
  	}
@@ -1095,11 +1090,12 @@ amdgpu_vm_tlb_flush(struct amdgpu_vm_update_params *params,
  	 * sort out the issues with KIQ/MES TLB invalidation timeouts.
  	 */
  	if (!params->unlocked && vm->need_tlb_fence) {
-		amdgpu_vm_tlb_fence_create(params->adev, vm, fence);
+		amdgpu_vm_tlb_fence_create(params->adev, vm, fence,
+					   &vm->last_tlb_inv);
 
  		/* Makes sure no PD/PT is freed before the flush */
-		dma_resv_add_fence(vm->root.bo->tbo.base.resv, *fence,
-				   DMA_RESV_USAGE_BOOKKEEP);
+		dma_resv_add_fence(vm->root.bo->tbo.base.resv,
+				   vm->last_tlb_inv, DMA_RESV_USAGE_BOOKKEEP);
  	}
  }
 
@@ -1248,7 +1244,7 @@ int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  		goto error_free;
 
  	if (params.needs_flush) {
-		amdgpu_vm_tlb_flush(&params, fence, tlb_cb);
+		amdgpu_vm_tlb_flush(&params, *fence, tlb_cb);
  		tlb_cb = NULL;
  	}
 
@@ -2647,7 +2643,8 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 
  	vm->last_update = dma_fence_get_stub();
  	vm->last_unlocked = dma_fence_get_stub();
-	vm->last_tlb_flush = dma_fence_get_stub();
+	vm->last_seq_flush = dma_fence_get_stub();
+	vm->last_tlb_inv = dma_fence_get_stub();
  	vm->generation = amdgpu_vm_generation(adev, NULL);
 
  	mutex_init(&vm->eviction_lock);
@@ -2691,7 +2688,8 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 
  error_free_delayed:
  	dma_fence_put(vm->last_update);
-	dma_fence_put(vm->last_tlb_flush);
+	dma_fence_put(vm->last_seq_flush);
+	dma_fence_put(vm->last_tlb_inv);
  	dma_fence_put(vm->last_unlocked);
  	ttm_lru_bulk_move_fini(&adev->mman.bdev, &vm->lru_bulk_move);
  	amdgpu_vm_fini_entities(vm);
@@ -2834,11 +2832,12 @@ void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
  	amdgpu_bo_reserve(root, true);
  	dma_fence_wait(vm->last_unlocked, false);
  	dma_fence_put(vm->last_unlocked);
-	dma_fence_wait(vm->last_tlb_flush, false);
+	dma_fence_wait(vm->last_seq_flush, false);
  	/* Make sure that all fence callbacks have completed */
-	dma_fence_lock_irqsave(vm->last_tlb_flush, flags);
-	dma_fence_unlock_irqrestore(vm->last_tlb_flush, flags);
-	dma_fence_put(vm->last_tlb_flush);
+	dma_fence_lock_irqsave(vm->last_seq_flush, flags);
+	dma_fence_unlock_irqrestore(vm->last_seq_flush, flags);
+	dma_fence_put(vm->last_seq_flush);
+	dma_fence_put(vm->last_tlb_inv);
 
  	list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
  		if (mapping->flags & AMDGPU_VM_PAGE_PRT && prt_fini_needed) {
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +7 -6
index 8e478d3bda1ac..713a597027bbe 100644
@@ -328,11 +328,12 @@ struct amdgpu_vm {
  	struct drm_sched_entity	immediate;
  	struct drm_sched_entity	delayed;
 
-	/* Last finished delayed update */
+	/* Last TLB seq/fence */
  	atomic64_t		tlb_seq;
-	struct dma_fence	*last_tlb_flush;
+	struct dma_fence	*last_seq_flush;
  	atomic64_t		kfd_last_flushed_seq;
  	uint64_t		tlb_fence_context;
+	struct dma_fence	*last_tlb_inv;
 
  	/* How many times we had to re-generate the page tables */
  	uint64_t		generation;
@@ -561,7 +562,7 @@ static inline uint64_t amdgpu_vm_tlb_seq(struct amdgpu_vm *vm)
  	 * sure that the dma_fence structure isn't freed up.
  	 */
  	rcu_read_lock();
-	lock = dma_fence_spinlock(vm->last_tlb_flush);
+	lock = dma_fence_spinlock(vm->last_seq_flush);
  	rcu_read_unlock();
 
  	spin_lock_irqsave(lock, flags);
@@ -576,9 +577,9 @@ void amdgpu_vm_update_fault_cache(struct amdgpu_device *adev,
  				  uint32_t status,
  				  unsigned int vmhub);
  void amdgpu_vm_tlb_fence_create(struct amdgpu_device *adev,
-				 struct amdgpu_vm *vm,
-				 struct dma_fence **fence);
-
+				struct amdgpu_vm *vm,
+				struct dma_fence *dep,
+				struct dma_fence **fence);
  void amdgpu_vm_print_task_info(struct amdgpu_device *adev,
  			       struct amdgpu_task_info *task_info);
 
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c +16 -5
index b0002c8983861..c91417e4df4ed 100644
@@ -75,7 +75,19 @@ static const struct dma_fence_ops amdgpu_tlb_fence_ops = {
  	.get_timeline_name = amdgpu_tlb_fence_get_timeline_name
  };
 
-void amdgpu_vm_tlb_fence_create(struct amdgpu_device *adev, struct amdgpu_vm *vm,
+/**
+ * amdgpu_vm_tlb_fence_create - create a TLB invalidation fence
+ * @adev: the amdgpu device
+ * @vm: the vm to use
+ * @dep: the dependency to wait for
+ * @fence: the resulting fence
+ *
+ * Creates a TLB invalidation fence which signals when all the VMIDs assigned to
+ * the PASID of the VM are invalidated.
+ */
+void amdgpu_vm_tlb_fence_create(struct amdgpu_device *adev,
+				struct amdgpu_vm *vm,
+				struct dma_fence *dep,
  				struct dma_fence **fence)
  {
  	struct amdgpu_tlb_fence *f;
@@ -86,16 +98,15 @@ void amdgpu_vm_tlb_fence_create(struct amdgpu_device *adev, struct amdgpu_vm *vm
  		 * We can't fail since the PDEs and PTEs are already updated, so
  		 * just block for the dependency and execute the TLB flush
  		 */
-		if (*fence)
-			dma_fence_wait(*fence, false);
+		if (dep)
+			dma_fence_wait(dep, false);
 
  		amdgpu_gmc_flush_gpu_tlb_pasid(adev, vm->pasid, 2, true, 0);
-		*fence = dma_fence_get_stub();
  		return;
  	}
 
  	f->adev = adev;
-	f->dependency = *fence;
+	f->dependency = dma_fence_get(dep);
  	f->pasid = vm->pasid;
  	INIT_WORK(&f->work, amdgpu_tlb_fence_work);
  	spin_lock_init(&f->lock);
maintainer
#2 lore raw
Quoted 11 lines · Felix Kuehling
Quoted 9 lines · Christian König

After each unmap it might be necessary to invalidate the TLB to flush out VM changes to the HW and make it visible to executing work.

We tracked that as part of the vm->last_update fence, but as pointed out by David it can be that this is overwritten by PDE updates when we have a combined unmap/map operation.

In that case userspace doesn't correctly wait for the TLB invalidation to finish leading to all kind of problems (sq_int, page faults etc...).

Quoted 7 lines

If I understand it correctly, you fixed this by tracking two different fences in amdgpu_vm: one that updates the sequence number, and one that signals when the TLB flush completes.

Quoted 2 lines

Why was [email protected] copied on this email?

It was CCed on Davids original mail as well.

Regards,
Christian.

Quoted 210 lines

Anyway, the patch looks good to me.

Quoted 205 lines

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c       | 20 ++++++---- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c        | 39 +++++++++---------- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h        | 13 ++++--- .../gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c  | 21 +++++++---

  4 files changed, 55 insertions(+), 38 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +13 -7
index 374f1322c3ee4..606725d0eb155 100644
@@ -843,18 +843,24 @@ amdgpu_gem_va_update_vm(struct amdgpu_device *adev,
           /*
            * For MAP/REPLACE of non per-VM BOs we need to sync to both the
            * bo_va->last_pt_update and vm->last_update or otherwise we
-         * potentially miss the PDE updates.
+         * potentially miss the PDE updates or TLB invalidation.
            */
           fence = dma_fence_unwrap_merge(vm->last_update,
+                           vm->last_tlb_inv,
                              bo_va->last_pt_update);
-        if (!fence) {
-            /* As fallback in OOM situations */
-            dma_fence_wait(vm->last_update, false);
+        if (!fence)
               dma_fence_wait(bo_va->last_pt_update, false);
-            fence = dma_fence_get_stub();
-        }
+
       } else {
-        fence = dma_fence_get(vm->last_update);
+        fence = dma_fence_unwrap_merge(vm->last_update,
+                           vm->last_tlb_inv);
+    }
+
+    if (!fence) {
+        /* As fallback in OOM situations */
+        dma_fence_wait(vm->last_update, false);
+        dma_fence_wait(vm->last_tlb_inv, false);
+        fence = dma_fence_get_stub();
       }
         return fence;
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -0
index 7d3659ef11e53..f124320345ec4 100644
@@ -1071,21 +1071,16 @@ static void amdgpu_vm_tlb_seq_cb(struct dma_fence *fence,
    */

  static void
  amdgpu_vm_tlb_flush(struct amdgpu_vm_update_params *params, -            struct dma_fence **fence,
+            struct dma_fence *fence,
              struct amdgpu_vm_tlb_seq_struct *tlb_cb) {
      struct amdgpu_vm *vm = params->vm;
        tlb_cb->vm = vm;
-    if (!fence || !*fence) {
-        amdgpu_vm_tlb_seq_cb(NULL, &tlb_cb->cb);
-        return;
-    }
-
-    if (!dma_fence_add_callback(*fence, &tlb_cb->cb, -                    amdgpu_vm_tlb_seq_cb)) {
-        dma_fence_put(vm->last_tlb_flush);
-        vm->last_tlb_flush = dma_fence_get(*fence); +    if (!fence || !dma_fence_add_callback(fence, &tlb_cb->cb, +                          amdgpu_vm_tlb_seq_cb)) { +        dma_fence_put(vm->last_seq_flush);
+        vm->last_seq_flush = dma_fence_get(fence); } else {
          amdgpu_vm_tlb_seq_cb(NULL, &tlb_cb->cb);
      }

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +4 -3
@@ -1095,11 +1090,12 @@ amdgpu_vm_tlb_flush(struct amdgpu_vm_update_params *params,
        * sort out the issues with KIQ/MES TLB invalidation timeouts.
        */
       if (!params->unlocked && vm->need_tlb_fence) {
-        amdgpu_vm_tlb_fence_create(params->adev, vm, fence);
+        amdgpu_vm_tlb_fence_create(params->adev, vm, fence,
+                       &vm->last_tlb_inv);
             /* Makes sure no PD/PT is freed before the flush */
-        dma_resv_add_fence(vm->root.bo->tbo.base.resv, *fence,
-                   DMA_RESV_USAGE_BOOKKEEP);
+        dma_resv_add_fence(vm->root.bo->tbo.base.resv,
+                   vm->last_tlb_inv, DMA_RESV_USAGE_BOOKKEEP);
       }
   }
   @@ -1248,7 +1244,7 @@ int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm,
           goto error_free;

        if (params.needs_flush) {
-        amdgpu_vm_tlb_flush(&params, fence, tlb_cb);
+        amdgpu_vm_tlb_flush(&params, *fence, tlb_cb);
          tlb_cb = NULL;
      }
  @@ -2647,7 +2643,8 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm, vm->last_update = dma_fence_get_stub();
      vm->last_unlocked = dma_fence_get_stub();
-    vm->last_tlb_flush = dma_fence_get_stub();
+    vm->last_seq_flush = dma_fence_get_stub();
+    vm->last_tlb_inv = dma_fence_get_stub();
      vm->generation = amdgpu_vm_generation(adev, NULL); mutex_init(&vm->eviction_lock);

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -0
@@ -2691,7 +2688,8 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,

    error_free_delayed:
      dma_fence_put(vm->last_update);
-    dma_fence_put(vm->last_tlb_flush);
+    dma_fence_put(vm->last_seq_flush);
+    dma_fence_put(vm->last_tlb_inv); dma_fence_put(vm->last_unlocked); ttm_lru_bulk_move_fini(&adev->mman.bdev, &vm->lru_bulk_move); amdgpu_vm_fini_entities(vm);

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +5 -4
@@ -2834,11 +2832,12 @@ void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
       amdgpu_bo_reserve(root, true);
       dma_fence_wait(vm->last_unlocked, false);
       dma_fence_put(vm->last_unlocked);
-    dma_fence_wait(vm->last_tlb_flush, false);
+    dma_fence_wait(vm->last_seq_flush, false);
       /* Make sure that all fence callbacks have completed */
-    dma_fence_lock_irqsave(vm->last_tlb_flush, flags);
-    dma_fence_unlock_irqrestore(vm->last_tlb_flush, flags);
-    dma_fence_put(vm->last_tlb_flush);
+    dma_fence_lock_irqsave(vm->last_seq_flush, flags);
+    dma_fence_unlock_irqrestore(vm->last_seq_flush, flags);
+    dma_fence_put(vm->last_seq_flush);
+    dma_fence_put(vm->last_tlb_inv);
         list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
           if (mapping->flags & AMDGPU_VM_PAGE_PRT && prt_fini_needed) {
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +0 -0
index 8e478d3bda1ac..713a597027bbe 100644
@@ -328,11 +328,12 @@ struct amdgpu_vm {
       struct drm_sched_entity    immediate;
       struct drm_sched_entity    delayed;

  -    /* Last finished delayed update */
+    /* Last TLB seq/fence */
      atomic64_t        tlb_seq;
-    struct dma_fence    *last_tlb_flush;
+    struct dma_fence    *last_seq_flush;
      atomic64_t        kfd_last_flushed_seq;
      uint64_t        tlb_fence_context;
+    struct dma_fence    *last_tlb_inv;
        /* How many times we had to re-generate the page tables */ uint64_t        generation;

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +4 -4
@@ -561,7 +562,7 @@ static inline uint64_t amdgpu_vm_tlb_seq(struct amdgpu_vm *vm)
        * sure that the dma_fence structure isn't freed up.
        */
       rcu_read_lock();
-    lock = dma_fence_spinlock(vm->last_tlb_flush);
+    lock = dma_fence_spinlock(vm->last_seq_flush);
       rcu_read_unlock();
         spin_lock_irqsave(lock, flags);
@@ -576,9 +577,9 @@ void amdgpu_vm_update_fault_cache(struct amdgpu_device *adev,
                     uint32_t status,
                     unsigned int vmhub);
   void amdgpu_vm_tlb_fence_create(struct amdgpu_device *adev,
-                 struct amdgpu_vm *vm,
-                 struct dma_fence **fence);
-
+                struct amdgpu_vm *vm,
+                struct dma_fence *dep,
+                struct dma_fence **fence);
   void amdgpu_vm_print_task_info(struct amdgpu_device *adev,
                      struct amdgpu_task_info *task_info);

  diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c index b0002c8983861..c91417e4df4ed 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c +0 -0
@@ -75,7 +75,19 @@ static const struct dma_fence_ops amdgpu_tlb_fence_ops = {

      .get_timeline_name = amdgpu_tlb_fence_get_timeline_name
  };
  -void amdgpu_vm_tlb_fence_create(struct amdgpu_device *adev, struct amdgpu_vm *vm,

drivers/gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c +13 -0
+/**
+ * amdgpu_vm_tlb_fence_create - create a TLB invalidation fence
+ * @adev: the amdgpu device
+ * @vm: the vm to use
+ * @dep: the dependency to wait for
+ * @fence: the resulting fence
+ *
+ * Creates a TLB invalidation fence which signals when all the VMIDs assigned to
+ * the PASID of the VM are invalidated.
+ */
+void amdgpu_vm_tlb_fence_create(struct amdgpu_device *adev,
+                struct amdgpu_vm *vm,
+                struct dma_fence *dep,

                  struct dma_fence **fence)
  {
      struct amdgpu_tlb_fence *f;

drivers/gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c +3 -4
@@ -86,16 +98,15 @@ void amdgpu_vm_tlb_fence_create(struct amdgpu_device *adev, struct amdgpu_vm *vm
            * We can't fail since the PDEs and PTEs are already updated, so
            * just block for the dependency and execute the TLB flush
            */
-        if (*fence)
-            dma_fence_wait(*fence, false);
+        if (dep)
+            dma_fence_wait(dep, false);
             amdgpu_gmc_flush_gpu_tlb_pasid(adev, vm->pasid, 2, true, 0);
-        *fence = dma_fence_get_stub();
           return;
       }
         f->adev = adev;
-    f->dependency = *fence;
+    f->dependency = dma_fence_get(dep);
       f->pasid = vm->pasid;
       INIT_WORK(&f->work, amdgpu_tlb_fence_work);
       spin_lock_init(&f->lock);
#3 lore raw
In reply to Christian König
Quoted 69 lines · Christian König

After each unmap it might be necessary to invalidate the TLB to flush out VM changes to the HW and make it visible to executing work.

We tracked that as part of the vm->last_update fence, but as pointed out by David it can be that this is overwritten by PDE updates when we have a combined unmap/map operation.

In that case userspace doesn't correctly wait for the TLB invalidation to finish leading to all kind of problems (sq_int, page faults etc...).

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c       | 20 ++++++----
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c        | 39 +++++++++----------
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h        | 13 ++++---
 .../gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c  | 21 +++++++---
 4 files changed, 55 insertions(+), 38 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +13 -7
index 374f1322c3ee4..606725d0eb155 100644
@@ -843,18 +843,24 @@ amdgpu_gem_va_update_vm(struct amdgpu_device *adev,
 		/*
 		 * For MAP/REPLACE of non per-VM BOs we need to sync to both the
 		 * bo_va->last_pt_update and vm->last_update or otherwise we
-		 * potentially miss the PDE updates.
+		 * potentially miss the PDE updates or TLB invalidation.
 		 */
 		fence = dma_fence_unwrap_merge(vm->last_update,
+					       vm->last_tlb_inv,
 					       bo_va->last_pt_update);
-		if (!fence) {
-			/* As fallback in OOM situations */
-			dma_fence_wait(vm->last_update, false);
+		if (!fence)
 			dma_fence_wait(bo_va->last_pt_update, false);
-			fence = dma_fence_get_stub();
-		}
+
 	} else {
-		fence = dma_fence_get(vm->last_update);
+		fence = dma_fence_unwrap_merge(vm->last_update,
+					       vm->last_tlb_inv);
+	}
+
+	if (!fence) {
+		/* As fallback in OOM situations */
+		dma_fence_wait(vm->last_update, false);
+		dma_fence_wait(vm->last_tlb_inv, false);
+		fence = dma_fence_get_stub();
 	}
 
 	return fence;
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -1
index 7d3659ef11e53..f124320345ec4 100644
@@ -1071,21 +1071,16 @@ static void amdgpu_vm_tlb_seq_cb(struct dma_fence *fence,
  */
 static void
 amdgpu_vm_tlb_flush(struct amdgpu_vm_update_params *params,
-		    struct dma_fence **fence,
+		    struct dma_fence *fence,
 		    struct amdgpu_vm_tlb_seq_struct *tlb_cb)
 {
 	struct amdgpu_vm *vm = params->vm;
 
 	tlb_cb->vm = vm;

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c static void amdgpu_vm_tlb_seq_cb(struct dma_fence *fence, · line 1080

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -9
-	if (!fence || !*fence) {
-		amdgpu_vm_tlb_seq_cb(NULL, &tlb_cb->cb);
-		return;
-	}
-
-	if (!dma_fence_add_callback(*fence, &tlb_cb->cb,
-				    amdgpu_vm_tlb_seq_cb)) {
-		dma_fence_put(vm->last_tlb_flush);
-		vm->last_tlb_flush = dma_fence_get(*fence);
+	if (!fence || !dma_fence_add_callback(fence, &tlb_cb->cb,

Shouldn't this be if (fence || !dma_fence_add_callback(...))? Otherwise the tlb_seq never gets incremented.

Quoted 154 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +17 -10
+					      amdgpu_vm_tlb_seq_cb)) {
+		dma_fence_put(vm->last_seq_flush);
+		vm->last_seq_flush = dma_fence_get(fence);
 	} else {
 		amdgpu_vm_tlb_seq_cb(NULL, &tlb_cb->cb);
 	}
@@ -1095,11 +1090,12 @@ amdgpu_vm_tlb_flush(struct amdgpu_vm_update_params *params,
 	 * sort out the issues with KIQ/MES TLB invalidation timeouts.
 	 */
 	if (!params->unlocked && vm->need_tlb_fence) {
-		amdgpu_vm_tlb_fence_create(params->adev, vm, fence);
+		amdgpu_vm_tlb_fence_create(params->adev, vm, fence,
+					   &vm->last_tlb_inv);
 
 		/* Makes sure no PD/PT is freed before the flush */
-		dma_resv_add_fence(vm->root.bo->tbo.base.resv, *fence,
-				   DMA_RESV_USAGE_BOOKKEEP);
+		dma_resv_add_fence(vm->root.bo->tbo.base.resv,
+				   vm->last_tlb_inv, DMA_RESV_USAGE_BOOKKEEP);
 	}
 }
 
@@ -1248,7 +1244,7 @@ int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 		goto error_free;
 
 	if (params.needs_flush) {
-		amdgpu_vm_tlb_flush(&params, fence, tlb_cb);
+		amdgpu_vm_tlb_flush(&params, *fence, tlb_cb);
 		tlb_cb = NULL;
 	}
 
@@ -2647,7 +2643,8 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 
 	vm->last_update = dma_fence_get_stub();
 	vm->last_unlocked = dma_fence_get_stub();
-	vm->last_tlb_flush = dma_fence_get_stub();
+	vm->last_seq_flush = dma_fence_get_stub();
+	vm->last_tlb_inv = dma_fence_get_stub();
 	vm->generation = amdgpu_vm_generation(adev, NULL);
 
 	mutex_init(&vm->eviction_lock);
@@ -2691,7 +2688,8 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 
 error_free_delayed:
 	dma_fence_put(vm->last_update);
-	dma_fence_put(vm->last_tlb_flush);
+	dma_fence_put(vm->last_seq_flush);
+	dma_fence_put(vm->last_tlb_inv);
 	dma_fence_put(vm->last_unlocked);
 	ttm_lru_bulk_move_fini(&adev->mman.bdev, &vm->lru_bulk_move);
 	amdgpu_vm_fini_entities(vm);
@@ -2834,11 +2832,12 @@ void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
 	amdgpu_bo_reserve(root, true);
 	dma_fence_wait(vm->last_unlocked, false);
 	dma_fence_put(vm->last_unlocked);
-	dma_fence_wait(vm->last_tlb_flush, false);
+	dma_fence_wait(vm->last_seq_flush, false);
 	/* Make sure that all fence callbacks have completed */
-	dma_fence_lock_irqsave(vm->last_tlb_flush, flags);
-	dma_fence_unlock_irqrestore(vm->last_tlb_flush, flags);
-	dma_fence_put(vm->last_tlb_flush);
+	dma_fence_lock_irqsave(vm->last_seq_flush, flags);
+	dma_fence_unlock_irqrestore(vm->last_seq_flush, flags);
+	dma_fence_put(vm->last_seq_flush);
+	dma_fence_put(vm->last_tlb_inv);
 
 	list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
 		if (mapping->flags & AMDGPU_VM_PAGE_PRT && prt_fini_needed) {
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +7 -6
index 8e478d3bda1ac..713a597027bbe 100644
@@ -328,11 +328,12 @@ struct amdgpu_vm {
 	struct drm_sched_entity	immediate;
 	struct drm_sched_entity	delayed;
 
-	/* Last finished delayed update */
+	/* Last TLB seq/fence */
 	atomic64_t		tlb_seq;
-	struct dma_fence	*last_tlb_flush;
+	struct dma_fence	*last_seq_flush;
 	atomic64_t		kfd_last_flushed_seq;
 	uint64_t		tlb_fence_context;
+	struct dma_fence	*last_tlb_inv;
 
 	/* How many times we had to re-generate the page tables */
 	uint64_t		generation;
@@ -561,7 +562,7 @@ static inline uint64_t amdgpu_vm_tlb_seq(struct amdgpu_vm *vm)
 	 * sure that the dma_fence structure isn't freed up.
 	 */
 	rcu_read_lock();
-	lock = dma_fence_spinlock(vm->last_tlb_flush);
+	lock = dma_fence_spinlock(vm->last_seq_flush);
 	rcu_read_unlock();
 
 	spin_lock_irqsave(lock, flags);
@@ -576,9 +577,9 @@ void amdgpu_vm_update_fault_cache(struct amdgpu_device *adev,
 				  uint32_t status,
 				  unsigned int vmhub);
 void amdgpu_vm_tlb_fence_create(struct amdgpu_device *adev,
-				 struct amdgpu_vm *vm,
-				 struct dma_fence **fence);
-
+				struct amdgpu_vm *vm,
+				struct dma_fence *dep,
+				struct dma_fence **fence);
 void amdgpu_vm_print_task_info(struct amdgpu_device *adev,
 			       struct amdgpu_task_info *task_info);
 
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c +16 -5
index b0002c8983861..c91417e4df4ed 100644
@@ -75,7 +75,19 @@ static const struct dma_fence_ops amdgpu_tlb_fence_ops = {
 	.get_timeline_name = amdgpu_tlb_fence_get_timeline_name
 };
 
-void amdgpu_vm_tlb_fence_create(struct amdgpu_device *adev, struct amdgpu_vm *vm,
+/**
+ * amdgpu_vm_tlb_fence_create - create a TLB invalidation fence
+ * @adev: the amdgpu device
+ * @vm: the vm to use
+ * @dep: the dependency to wait for
+ * @fence: the resulting fence
+ *
+ * Creates a TLB invalidation fence which signals when all the VMIDs assigned to
+ * the PASID of the VM are invalidated.
+ */
+void amdgpu_vm_tlb_fence_create(struct amdgpu_device *adev,
+				struct amdgpu_vm *vm,
+				struct dma_fence *dep,
 				struct dma_fence **fence)
 {
 	struct amdgpu_tlb_fence *f;
@@ -86,16 +98,15 @@ void amdgpu_vm_tlb_fence_create(struct amdgpu_device *adev, struct amdgpu_vm *vm
 		 * We can't fail since the PDEs and PTEs are already updated, so
 		 * just block for the dependency and execute the TLB flush
 		 */
-		if (*fence)
-			dma_fence_wait(*fence, false);
+		if (dep)
+			dma_fence_wait(dep, false);
 
 		amdgpu_gmc_flush_gpu_tlb_pasid(adev, vm->pasid, 2, true, 0);
-		*fence = dma_fence_get_stub();
 		return;
 	}
 
 	f->adev = adev;
-	f->dependency = *fence;
+	f->dependency = dma_fence_get(dep);
 	f->pasid = vm->pasid;
 	INIT_WORK(&f->work, amdgpu_tlb_fence_work);
 	spin_lock_init(&f->lock);
#4 lore raw

Hi Benjamin,

please CC my AMD mail address as well otherwise I don't see the reply.

Quoted 70 lines · Benjamin Cheng
Quoted 68 lines · Christian König

After each unmap it might be necessary to invalidate the TLB to flush out VM changes to the HW and make it visible to executing work.

We tracked that as part of the vm->last_update fence, but as pointed out by David it can be that this is overwritten by PDE updates when we have a combined unmap/map operation.

In that case userspace doesn't correctly wait for the TLB invalidation to finish leading to all kind of problems (sq_int, page faults etc...).

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c       | 20 ++++++----
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c        | 39 +++++++++----------
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h        | 13 ++++---
 .../gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c  | 21 +++++++---
 4 files changed, 55 insertions(+), 38 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +13 -7
index 374f1322c3ee4..606725d0eb155 100644
@@ -843,18 +843,24 @@ amdgpu_gem_va_update_vm(struct amdgpu_device *adev,
 		/*
 		 * For MAP/REPLACE of non per-VM BOs we need to sync to both the
 		 * bo_va->last_pt_update and vm->last_update or otherwise we
-		 * potentially miss the PDE updates.
+		 * potentially miss the PDE updates or TLB invalidation.
 		 */
 		fence = dma_fence_unwrap_merge(vm->last_update,
+					       vm->last_tlb_inv,
 					       bo_va->last_pt_update);
-		if (!fence) {
-			/* As fallback in OOM situations */
-			dma_fence_wait(vm->last_update, false);
+		if (!fence)
 			dma_fence_wait(bo_va->last_pt_update, false);
-			fence = dma_fence_get_stub();
-		}
+
 	} else {
-		fence = dma_fence_get(vm->last_update);
+		fence = dma_fence_unwrap_merge(vm->last_update,
+					       vm->last_tlb_inv);
+	}
+
+	if (!fence) {
+		/* As fallback in OOM situations */
+		dma_fence_wait(vm->last_update, false);
+		dma_fence_wait(vm->last_tlb_inv, false);
+		fence = dma_fence_get_stub();
 	}
 
 	return fence;
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -1
index 7d3659ef11e53..f124320345ec4 100644
@@ -1071,21 +1071,16 @@ static void amdgpu_vm_tlb_seq_cb(struct dma_fence *fence,
  */
 static void
 amdgpu_vm_tlb_flush(struct amdgpu_vm_update_params *params,
-		    struct dma_fence **fence,
+		    struct dma_fence *fence,
 		    struct amdgpu_vm_tlb_seq_struct *tlb_cb)
 {
 	struct amdgpu_vm *vm = params->vm;
 
Quoted 14 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c static void amdgpu_vm_tlb_seq_cb(struct dma_fence *fence, · line 1080

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -9
 	tlb_cb->vm = vm;
-	if (!fence || !*fence) {
-		amdgpu_vm_tlb_seq_cb(NULL, &tlb_cb->cb);
-		return;
-	}
-
-	if (!dma_fence_add_callback(*fence, &tlb_cb->cb,
-				    amdgpu_vm_tlb_seq_cb)) {
-		dma_fence_put(vm->last_tlb_flush);
-		vm->last_tlb_flush = dma_fence_get(*fence);
+	if (!fence || !dma_fence_add_callback(fence, &tlb_cb->cb,

Shouldn't this be if (fence || !dma_fence_add_callback(...))? Otherwise the tlb_seq never gets incremented.

Right, thanks that is a really good catch.

Regards,
Christian.

Quoted 156 lines
Quoted 156 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +17 -10
+					      amdgpu_vm_tlb_seq_cb)) {
+		dma_fence_put(vm->last_seq_flush);
+		vm->last_seq_flush = dma_fence_get(fence);
 	} else {
 		amdgpu_vm_tlb_seq_cb(NULL, &tlb_cb->cb);
 	}
@@ -1095,11 +1090,12 @@ amdgpu_vm_tlb_flush(struct amdgpu_vm_update_params *params,
 	 * sort out the issues with KIQ/MES TLB invalidation timeouts.
 	 */
 	if (!params->unlocked && vm->need_tlb_fence) {
-		amdgpu_vm_tlb_fence_create(params->adev, vm, fence);
+		amdgpu_vm_tlb_fence_create(params->adev, vm, fence,
+					   &vm->last_tlb_inv);
 
 		/* Makes sure no PD/PT is freed before the flush */
-		dma_resv_add_fence(vm->root.bo->tbo.base.resv, *fence,
-				   DMA_RESV_USAGE_BOOKKEEP);
+		dma_resv_add_fence(vm->root.bo->tbo.base.resv,
+				   vm->last_tlb_inv, DMA_RESV_USAGE_BOOKKEEP);
 	}
 }
 
@@ -1248,7 +1244,7 @@ int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 		goto error_free;
 
 	if (params.needs_flush) {
-		amdgpu_vm_tlb_flush(&params, fence, tlb_cb);
+		amdgpu_vm_tlb_flush(&params, *fence, tlb_cb);
 		tlb_cb = NULL;
 	}
 
@@ -2647,7 +2643,8 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 
 	vm->last_update = dma_fence_get_stub();
 	vm->last_unlocked = dma_fence_get_stub();
-	vm->last_tlb_flush = dma_fence_get_stub();
+	vm->last_seq_flush = dma_fence_get_stub();
+	vm->last_tlb_inv = dma_fence_get_stub();
 	vm->generation = amdgpu_vm_generation(adev, NULL);
 
 	mutex_init(&vm->eviction_lock);
@@ -2691,7 +2688,8 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 
 error_free_delayed:
 	dma_fence_put(vm->last_update);
-	dma_fence_put(vm->last_tlb_flush);
+	dma_fence_put(vm->last_seq_flush);
+	dma_fence_put(vm->last_tlb_inv);
 	dma_fence_put(vm->last_unlocked);
 	ttm_lru_bulk_move_fini(&adev->mman.bdev, &vm->lru_bulk_move);
 	amdgpu_vm_fini_entities(vm);
@@ -2834,11 +2832,12 @@ void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
 	amdgpu_bo_reserve(root, true);
 	dma_fence_wait(vm->last_unlocked, false);
 	dma_fence_put(vm->last_unlocked);
-	dma_fence_wait(vm->last_tlb_flush, false);
+	dma_fence_wait(vm->last_seq_flush, false);
 	/* Make sure that all fence callbacks have completed */
-	dma_fence_lock_irqsave(vm->last_tlb_flush, flags);
-	dma_fence_unlock_irqrestore(vm->last_tlb_flush, flags);
-	dma_fence_put(vm->last_tlb_flush);
+	dma_fence_lock_irqsave(vm->last_seq_flush, flags);
+	dma_fence_unlock_irqrestore(vm->last_seq_flush, flags);
+	dma_fence_put(vm->last_seq_flush);
+	dma_fence_put(vm->last_tlb_inv);
 
 	list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
 		if (mapping->flags & AMDGPU_VM_PAGE_PRT && prt_fini_needed) {
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +7 -6
index 8e478d3bda1ac..713a597027bbe 100644
@@ -328,11 +328,12 @@ struct amdgpu_vm {
 	struct drm_sched_entity	immediate;
 	struct drm_sched_entity	delayed;
 
-	/* Last finished delayed update */
+	/* Last TLB seq/fence */
 	atomic64_t		tlb_seq;
-	struct dma_fence	*last_tlb_flush;
+	struct dma_fence	*last_seq_flush;
 	atomic64_t		kfd_last_flushed_seq;
 	uint64_t		tlb_fence_context;
+	struct dma_fence	*last_tlb_inv;
 
 	/* How many times we had to re-generate the page tables */
 	uint64_t		generation;
@@ -561,7 +562,7 @@ static inline uint64_t amdgpu_vm_tlb_seq(struct amdgpu_vm *vm)
 	 * sure that the dma_fence structure isn't freed up.
 	 */
 	rcu_read_lock();
-	lock = dma_fence_spinlock(vm->last_tlb_flush);
+	lock = dma_fence_spinlock(vm->last_seq_flush);
 	rcu_read_unlock();
 
 	spin_lock_irqsave(lock, flags);
@@ -576,9 +577,9 @@ void amdgpu_vm_update_fault_cache(struct amdgpu_device *adev,
 				  uint32_t status,
 				  unsigned int vmhub);
 void amdgpu_vm_tlb_fence_create(struct amdgpu_device *adev,
-				 struct amdgpu_vm *vm,
-				 struct dma_fence **fence);
-
+				struct amdgpu_vm *vm,
+				struct dma_fence *dep,
+				struct dma_fence **fence);
 void amdgpu_vm_print_task_info(struct amdgpu_device *adev,
 			       struct amdgpu_task_info *task_info);
 
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c +16 -5
index b0002c8983861..c91417e4df4ed 100644
@@ -75,7 +75,19 @@ static const struct dma_fence_ops amdgpu_tlb_fence_ops = {
 	.get_timeline_name = amdgpu_tlb_fence_get_timeline_name
 };
 
-void amdgpu_vm_tlb_fence_create(struct amdgpu_device *adev, struct amdgpu_vm *vm,
+/**
+ * amdgpu_vm_tlb_fence_create - create a TLB invalidation fence
+ * @adev: the amdgpu device
+ * @vm: the vm to use
+ * @dep: the dependency to wait for
+ * @fence: the resulting fence
+ *
+ * Creates a TLB invalidation fence which signals when all the VMIDs assigned to
+ * the PASID of the VM are invalidated.
+ */
+void amdgpu_vm_tlb_fence_create(struct amdgpu_device *adev,
+				struct amdgpu_vm *vm,
+				struct dma_fence *dep,
 				struct dma_fence **fence)
 {
 	struct amdgpu_tlb_fence *f;
@@ -86,16 +98,15 @@ void amdgpu_vm_tlb_fence_create(struct amdgpu_device *adev, struct amdgpu_vm *vm
 		 * We can't fail since the PDEs and PTEs are already updated, so
 		 * just block for the dependency and execute the TLB flush
 		 */
-		if (*fence)
-			dma_fence_wait(*fence, false);
+		if (dep)
+			dma_fence_wait(dep, false);
 
 		amdgpu_gmc_flush_gpu_tlb_pasid(adev, vm->pasid, 2, true, 0);
-		*fence = dma_fence_get_stub();
 		return;
 	}
 
 	f->adev = adev;
-	f->dependency = *fence;
+	f->dependency = dma_fence_get(dep);
 	f->pasid = vm->pasid;
 	INIT_WORK(&f->work, amdgpu_tlb_fence_work);
 	spin_lock_init(&f->lock);
#5 lore raw

Hi Christian,

On top of Benjamin's note, we found a reference leak while testing this on gfx1151 (Strix Halo, CPU page table updates for compute VMs), backported to 6.17 together with Benjamin's "wait for tlb flush before freeing BO".

amdgpu_vm_tlb_fence_create() now writes into &vm->last_tlb_inv. The final "*fence = &f->base;" overwrites the previous fence without dropping its reference, so every TLB flush leaks one fence. With the series applied, kmalloc-128 grew by about 40 objects per process and never leveled off. It stopped growing with:

	dma_fence_put(*fence);
	*fence = &f->base;

With this series, the page faults we saw in the rocm-examples hipThreads examples (Libraries/hipThreads) on gfx1151 are gone. This is important for APUs running oem kernels without dkms, hope it gets upstreamed smoothly!

Thanks,
Zichuan