Skip to contentSkip to conversation

drm/amdgpu: replace TLB seq callback with HW seq

PATCH Superseded Shashank Sharma 2 participants 5 messages Jan 31, 2024 – Feb 6, 2024

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/3 drm/amdgpu: replace TLB seq callback with HW seq

From: Christian König <[email protected]>

The callback we installed for the SDMA update were actually pretty horrible. since we now have seq64 use that one and HW seq writes instead.

V2:(Shashank)
- rebased on amd-drm-staging-next
- changed amdgpu_seq64_gpu_addr

Cc Christian König <[email protected]>
Cc Alex Deucher <[email protected]>
Cc Felix Kuehling <[email protected]>
Cc Rajneesh Bhardwaj <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_seq64.c   | 14 ++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_seq64.h   |  1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c      | 79 ++++-----------------
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h      | 27 ++-----
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm_cpu.c  |  3 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c   |  2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm_sdma.c |  5 ++
 7 files changed, 42 insertions(+), 89 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_seq64.c +14 -0
index 3d0d56087d41..300dc79fa2b9 100644
@@ -199,6 +199,20 @@ void amdgpu_seq64_free(struct amdgpu_device *adev, u64 va)
 		__clear_bit(bit_pos, adev->seq64.used);
 }
 
+/**
+ * amdgpu_seq64_gpu_addr - Calculate GPU addr from va
+ *
+ * @adev: amdgpu_device pointer
+ * @va: virtual address in client address space
+ *
+ * Calculate the GART address for a VA.
+ */
+u64 amdgpu_seq64_gpu_addr(struct amdgpu_device *adev, u64 va)
+{
+	return va - amdgpu_seq64_get_va_base(adev) +
+		amdgpu_bo_gpu_offset(adev->seq64.sbo);
+}
+
 /**
  * amdgpu_seq64_fini - Cleanup seq64 driver
  *
drivers/gpu/drm/amd/amdgpu/amdgpu_seq64.h +1 -0
index 4203b2ab318d..63e8ac0a2057 100644
@@ -43,6 +43,7 @@ void amdgpu_seq64_free(struct amdgpu_device *adev, u64 gpu_addr);
 int amdgpu_seq64_map(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 		     struct amdgpu_bo_va **bo_va);
 void amdgpu_seq64_unmap(struct amdgpu_device *adev, struct amdgpu_fpriv *fpriv);
+u64 amdgpu_seq64_gpu_addr(struct amdgpu_device *adev, u64 va);
 
 #endif
 
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +12 -67

169 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +7 -20
index 666698a57192..ac9380afcb69 100644
@@ -247,9 +247,9 @@ struct amdgpu_vm_update_params {
 	unsigned int num_dw_left;
 
 	/**
-	 * @table_freed: return true if page table is freed when updating
+	 * @needs_flush: true whenever we need to invalidate the TLB
 	 */
-	bool table_freed;
+	bool needs_flush;
 
 	/**
 	 * @allow_override: true for memory that is not uncached: allows MTYPE
@@ -328,9 +328,11 @@ struct amdgpu_vm {
 	struct drm_sched_entity	immediate;
 	struct drm_sched_entity	delayed;
 
-	/* Last finished delayed update */
+	/* Sequence number indicating necessary TLB flush */
 	atomic64_t		tlb_seq;
-	struct dma_fence	*last_tlb_flush;
+	uint64_t		tlb_seq_va;
+	uint64_t		*tlb_seq_cpu_addr;
+
 	atomic64_t		kfd_last_flushed_seq;
 
 	/* How many times we had to re-generate the page tables */
@@ -549,22 +551,7 @@ int amdgpu_vm_pt_map_tables(struct amdgpu_device *adev, struct amdgpu_vm *vm);
  */
 static inline uint64_t amdgpu_vm_tlb_seq(struct amdgpu_vm *vm)
 {
-	unsigned long flags;
-	spinlock_t *lock;
-
-	/*
-	 * Workaround to stop racing between the fence signaling and handling
-	 * the cb. The lock is static after initially setting it up, just make
-	 * sure that the dma_fence structure isn't freed up.
-	 */
-	rcu_read_lock();
-	lock = vm->last_tlb_flush->lock;
-	rcu_read_unlock();
-
-	spin_lock_irqsave(lock, flags);
-	spin_unlock_irqrestore(lock, flags);
-
-	return atomic64_read(&vm->tlb_seq);
+	return READ_ONCE(*vm->tlb_seq_cpu_addr);
 }
 
 /*
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_cpu.c +2 -1
index 6e31621452de..0f8fd0acef7b 100644
@@ -108,7 +108,8 @@ static int amdgpu_vm_cpu_update(struct amdgpu_vm_update_params *p,
 static int amdgpu_vm_cpu_commit(struct amdgpu_vm_update_params *p,
 				struct dma_fence **fence)
 {
-	/* Flush HDP */
+	if (p->needs_flush)
+		*p->vm->tlb_seq_cpu_addr = atomic64_inc_return(&p->vm->tlb_seq);
 	mb();
 	amdgpu_device_flush_hdp(p->adev, NULL);
 	return 0;
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +1 -1
index a160265ddc07..95dc0afdaffb 100644
@@ -1056,7 +1056,7 @@ int amdgpu_vm_ptes_update(struct amdgpu_vm_update_params *params,
 			while (cursor.pfn < frag_start) {
 				/* Make sure previous mapping is freed */
 				if (cursor.entry->bo) {
-					params->table_freed = true;
+					params->needs_flush = true;
 					amdgpu_vm_pt_free_dfs(adev, params->vm,
 							      &cursor,
 							      params->unlocked);
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_sdma.c +5 -0
index 349416e176a1..91cc3121fd4b 100644
@@ -126,6 +126,11 @@ static int amdgpu_vm_sdma_commit(struct amdgpu_vm_update_params *p,
 
 	WARN_ON(ib->length_dw == 0);
 	amdgpu_ring_pad_ib(ring, ib);
+	if (p->needs_flush) {
+		p->job->uf_addr = amdgpu_seq64_gpu_addr(p->adev,
+							p->vm->tlb_seq_va);
+		p->job->uf_sequence = atomic64_inc_return(&p->vm->tlb_seq);
+	}
 	WARN_ON(ib->length_dw > p->num_dw_left);
 	f = amdgpu_job_submit(p->job);
 
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 2/3 drm/amdgpu: implement TLB flush fence

From: Christian König <[email protected]>

The problem is that when (for example) 4k pages are replaced with a single 2M page we need to wait for change to be flushed out by invalidating the TLB before the PT can be freed.

Solve this by moving the TLB flush into a DMA-fence object which can be used to delay the freeing of the PT BOs until it is signaled.

V2: (Shashank)
- rebase
- set dma_fence_error only in case of error
- add tlb_flush fence only when PT/PD BO is locked (Felix)
- use vm->pasid when f is NULL (Mukul)

Cc Christian Koenig <[email protected]>
Cc Felix Kuehling <[email protected]>
Cc Rajneesh Bhardwaj <[email protected]>
Cc Alex Deucher <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/Makefile           |   3 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c        |  10 ++
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h        |   4 +
 .../gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c  | 106 ++++++++++++++++++
 4 files changed, 122 insertions(+), 1 deletion(-)
 create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c
drivers/gpu/drm/amd/amdgpu/Makefile +2 -1
index 4c989da4d2f3..fdbb3d770c7b 100644
@@ -70,7 +70,8 @@ amdgpu-y += amdgpu_device.o amdgpu_doorbell_mgr.o amdgpu_kms.o \
 	amdgpu_cs.o amdgpu_bios.o amdgpu_benchmark.o \
 	atombios_dp.o amdgpu_afmt.o amdgpu_trace_points.o \
 	atombios_encoders.o amdgpu_sa.o atombios_i2c.o \
-	amdgpu_dma_buf.o amdgpu_vm.o amdgpu_vm_pt.o amdgpu_ib.o amdgpu_pll.o \
+	amdgpu_dma_buf.o amdgpu_vm.o amdgpu_vm_pt.o amdgpu_vm_tlb_fence.o \
+	amdgpu_ib.o amdgpu_pll.o \
 	amdgpu_ucode.o amdgpu_bo_list.o amdgpu_ctx.o amdgpu_sync.o \
 	amdgpu_gtt_mgr.o amdgpu_preempt_mgr.o amdgpu_vram_mgr.o amdgpu_virt.o \
 	amdgpu_atomfirmware.o amdgpu_vf_error.o amdgpu_sched.o \
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +10 -0
index 0960e0a665d3..67c690044b97 100644
@@ -932,6 +932,15 @@ int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 	if (r)
 		goto error_unlock;
 
+	/* Prepare a TLB flush fence to be attached to PTs */
+	if (!unlocked && params.needs_flush && vm->is_compute_context) {
+		amdgpu_vm_tlb_fence_create(adev, vm, fence);
+
+		/* 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);
+	}
+
 	amdgpu_res_first(pages_addr ? NULL : res, offset,
 			 (last - start + 1) * AMDGPU_GPU_PAGE_SIZE, &cursor);
 	while (cursor.remaining) {
@@ -2237,6 +2246,7 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 
 	mutex_init(&vm->eviction_lock);
 	vm->evicting = false;
+	vm->tlb_fence_context = dma_fence_context_alloc(1);
 
 	r = amdgpu_vm_pt_create(adev, vm, adev->vm_manager.root_level,
 				false, &root, xcp_id);
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +4 -0
index ac9380afcb69..8e6fd25d07b7 100644
@@ -332,6 +332,7 @@ struct amdgpu_vm {
 	atomic64_t		tlb_seq;
 	uint64_t		tlb_seq_va;
 	uint64_t		*tlb_seq_cpu_addr;
+	uint64_t		tlb_fence_context;
 
 	atomic64_t		kfd_last_flushed_seq;
 
@@ -585,5 +586,8 @@ void amdgpu_vm_update_fault_cache(struct amdgpu_device *adev,
 				  uint64_t addr,
 				  uint32_t status,
 				  unsigned int vmhub);
+void amdgpu_vm_tlb_fence_create(struct amdgpu_device *adev,
+				 struct amdgpu_vm *vm,
+				 struct dma_fence **fence);
 
 #endif
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c new file +106 -0

106 lines not on this page · raw message

Signature · 1 line
2.43.0
#2 lore raw

PATCH v2 3/3 drm/amdgpu: sync page table freeing with tlb flush

In reply to Shashank Sharma

This patch:
- Attaches the TLB flush fence to the PT objects being freed
- Adds a new ptr in VM to save this last TLB flush fence
- Adds a new lock in VM to prevent out-of-context update of saved TLB flush fence
- Adds a new ptr in tlb_flush structure to save vm

The idea is to delay freeing of page table objects until we have the respective TLB entries flushed.

V2: rebase

Cc Christian König <[email protected]>
Cc Alex Deucher <[email protected]>
Cc Felix Kuehling <[email protected]>
Cc Rajneesh Bhardwaj <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c        |  3 +++
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h        |  4 +++
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c     | 27 +++++++++++++++++++
 .../gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c  | 13 +++++++--
 4 files changed, 45 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +3 -0
index 67c690044b97..b0e81c249e3a 100644
@@ -2245,6 +2245,7 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 	vm->generation = 0;
 
 	mutex_init(&vm->eviction_lock);
+	mutex_init(&vm->tlb_flush_lock);
 	vm->evicting = false;
 	vm->tlb_fence_context = dma_fence_context_alloc(1);
 
@@ -2360,7 +2361,9 @@ int amdgpu_vm_make_compute(struct amdgpu_device *adev, struct amdgpu_vm *vm)
 	}
 
 	dma_fence_put(vm->last_update);
+	dma_fence_put(vm->tlb_fence_last);
 	vm->last_update = dma_fence_get_stub();
+	vm->tlb_fence_last = dma_fence_get_stub();
 	vm->is_compute_context = true;
 
 	/* Free the shadow bo for compute VM */
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +4 -0
index 8e6fd25d07b7..b05bc586237f 100644
@@ -334,6 +334,10 @@ struct amdgpu_vm {
 	uint64_t		*tlb_seq_cpu_addr;
 	uint64_t		tlb_fence_context;
 
+	/* Ptr and lock to maintain tlb flush sync */
+	struct mutex		tlb_flush_lock;
+	struct dma_fence        *tlb_fence_last;
+
 	atomic64_t		kfd_last_flushed_seq;
 
 	/* How many times we had to re-generate the page tables */
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +27 -0
index 95dc0afdaffb..f1c4418c4d63 100644
@@ -631,6 +631,18 @@ static int amdgpu_vm_pt_alloc(struct amdgpu_device *adev,
 	return r;
 }
 
+static inline
+void amdgpu_vm_attach_tlb_fence(struct amdgpu_bo *bo, struct dma_fence *fence)
+{
+	if (!bo || !fence)
+		return;
+
+	if (!dma_resv_reserve_fences(bo->tbo.base.resv, 1)) {
+		dma_resv_add_fence(bo->tbo.base.resv, fence,
+				   DMA_RESV_USAGE_BOOKKEEP);
+	}
+}
+
 /**
  * amdgpu_vm_pt_free - free one PD/PT
  *
@@ -638,6 +650,7 @@ static int amdgpu_vm_pt_alloc(struct amdgpu_device *adev,
  */
 static void amdgpu_vm_pt_free(struct amdgpu_vm_bo_base *entry)
 {
+	struct amdgpu_vm *vm;
 	struct amdgpu_bo *shadow;
 
 	if (!entry->bo)
@@ -646,9 +659,23 @@ static void amdgpu_vm_pt_free(struct amdgpu_vm_bo_base *entry)
 	entry->bo->vm_bo = NULL;
 	shadow = amdgpu_bo_shadowed(entry->bo);
 	if (shadow) {
+		vm = shadow->vm_bo->vm;
+
+		mutex_lock(&vm->tlb_flush_lock);
+		if (vm->tlb_fence_last)
+			amdgpu_vm_attach_tlb_fence(shadow, vm->tlb_fence_last);
+		mutex_unlock(&vm->tlb_flush_lock);
+
 		ttm_bo_set_bulk_move(&shadow->tbo, NULL);
 		amdgpu_bo_unref(&shadow);
 	}
+
+	vm = entry->vm;
+	mutex_lock(&vm->tlb_flush_lock);
+	if (vm->tlb_fence_last)
+		amdgpu_vm_attach_tlb_fence(entry->bo, vm->tlb_fence_last);
+	mutex_unlock(&vm->tlb_flush_lock);
+
 	ttm_bo_set_bulk_move(&entry->bo->tbo, NULL);
 
 	spin_lock(&entry->vm->status_lock);
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c +11 -2
index 569681badd7c..54ec81d30034 100644
@@ -31,6 +31,7 @@
 struct amdgpu_tlb_fence {
 	struct dma_fence	base;
 	struct amdgpu_device	*adev;
+	struct amdgpu_vm	*vm;
 	struct dma_fence	*dependency;
 	struct work_struct	work;
 	spinlock_t		lock;
@@ -51,6 +52,7 @@ static const char *amdgpu_tlb_fence_get_timeline_name(struct dma_fence *f)
 static void amdgpu_tlb_fence_work(struct work_struct *work)
 {
 	struct amdgpu_tlb_fence *f = container_of(work, typeof(*f), work);
+	struct amdgpu_vm *vm = f->vm;
 	int r;
 
 	r = amdgpu_gmc_flush_gpu_tlb_pasid(f->adev, f->pasid, 2, true, 0);
@@ -62,6 +64,10 @@ static void amdgpu_tlb_fence_work(struct work_struct *work)
 
 	dma_fence_signal(&f->base);
 	dma_fence_put(&f->base);
+
+	mutex_lock(&vm->tlb_flush_lock);
+	vm->tlb_fence_last = NULL;
+	mutex_unlock(&vm->tlb_flush_lock);
 }
 
 static const struct dma_fence_ops amdgpu_tlb_fence_ops = {
@@ -92,6 +98,7 @@ void amdgpu_vm_tlb_fence_create(struct amdgpu_device *adev, struct amdgpu_vm *vm
 	f->adev = adev;
 	f->dependency = *fence;
 	f->pasid = vm->pasid;
+	f->vm = vm;
 	INIT_WORK(&f->work, amdgpu_tlb_fence_work);
 	spin_lock_init(&f->lock);
 
@@ -99,8 +106,10 @@ void amdgpu_vm_tlb_fence_create(struct amdgpu_device *adev, struct amdgpu_vm *vm
 		       vm->tlb_fence_context, atomic64_read(&vm->tlb_seq));
 
 	/* TODO: We probably need a separate wq here */
-	dma_fence_get(&f->base);
-	schedule_work(&f->work);
+	mutex_lock(&vm->tlb_flush_lock);
+	vm->tlb_fence_last = dma_fence_get(&f->base);
+	mutex_unlock(&vm->tlb_flush_lock);
 
+	schedule_work(&f->work);
 	*fence = &f->base;
 }
Signature · 1 line
2.43.0
maintainer
#3 lore raw

Re: [PATCH v2 3/3] drm/amdgpu: sync page table freeing with tlb flush

Am 31.01.24 um 18:14 schrieb Shashank Sharma:

Quoted 103 lines

This patch:
- Attaches the TLB flush fence to the PT objects being freed
- Adds a new ptr in VM to save this last TLB flush fence
- Adds a new lock in VM to prevent out-of-context update of saved TLB flush fence
- Adds a new ptr in tlb_flush structure to save vm

The idea is to delay freeing of page table objects until we have the respective TLB entries flushed.

V2: rebase

Cc Christian König <[email protected]>
Cc Alex Deucher <[email protected]>
Cc Felix Kuehling <[email protected]>
Cc Rajneesh Bhardwaj <[email protected]>
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c        |  3 +++
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h        |  4 +++
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c     | 27 +++++++++++++++++++
  .../gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c  | 13 +++++++--
  4 files changed, 45 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +3 -0
index 67c690044b97..b0e81c249e3a 100644
@@ -2245,6 +2245,7 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  	vm->generation = 0;
 
  	mutex_init(&vm->eviction_lock);
+	mutex_init(&vm->tlb_flush_lock);
  	vm->evicting = false;
  	vm->tlb_fence_context = dma_fence_context_alloc(1);
 
@@ -2360,7 +2361,9 @@ int amdgpu_vm_make_compute(struct amdgpu_device *adev, struct amdgpu_vm *vm)
  	}
 
  	dma_fence_put(vm->last_update);
+	dma_fence_put(vm->tlb_fence_last);
  	vm->last_update = dma_fence_get_stub();
+	vm->tlb_fence_last = dma_fence_get_stub();
  	vm->is_compute_context = true;
 
  	/* Free the shadow bo for compute VM */
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +4 -0
index 8e6fd25d07b7..b05bc586237f 100644
@@ -334,6 +334,10 @@ struct amdgpu_vm {
  	uint64_t		*tlb_seq_cpu_addr;
  	uint64_t		tlb_fence_context;
 
+	/* Ptr and lock to maintain tlb flush sync */
+	struct mutex		tlb_flush_lock;
+	struct dma_fence        *tlb_fence_last;
+
  	atomic64_t		kfd_last_flushed_seq;
 
  	/* How many times we had to re-generate the page tables */
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +20 -0
index 95dc0afdaffb..f1c4418c4d63 100644
@@ -631,6 +631,18 @@ static int amdgpu_vm_pt_alloc(struct amdgpu_device *adev,
  	return r;
  }
 
+static inline
+void amdgpu_vm_attach_tlb_fence(struct amdgpu_bo *bo, struct dma_fence *fence)
+{
+	if (!bo || !fence)
+		return;
+
+	if (!dma_resv_reserve_fences(bo->tbo.base.resv, 1)) {
+		dma_resv_add_fence(bo->tbo.base.resv, fence,
+				   DMA_RESV_USAGE_BOOKKEEP);
+	}
+}
+
  /**
   * amdgpu_vm_pt_free - free one PD/PT
   *
@@ -638,6 +650,7 @@ static int amdgpu_vm_pt_alloc(struct amdgpu_device *adev,
   */
  static void amdgpu_vm_pt_free(struct amdgpu_vm_bo_base *entry)
  {
+	struct amdgpu_vm *vm;
  	struct amdgpu_bo *shadow;
 
  	if (!entry->bo)
@@ -646,9 +659,23 @@ static void amdgpu_vm_pt_free(struct amdgpu_vm_bo_base *entry)
  	entry->bo->vm_bo = NULL;
  	shadow = amdgpu_bo_shadowed(entry->bo);
  	if (shadow) {
+		vm = shadow->vm_bo->vm;
+
+		mutex_lock(&vm->tlb_flush_lock);
+		if (vm->tlb_fence_last)
+			amdgpu_vm_attach_tlb_fence(shadow, vm->tlb_fence_last);
+		mutex_unlock(&vm->tlb_flush_lock);
+

drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c static void amdgpu_vm_pt_free(struct amdgpu_vm_bo_base *entry) · line 678

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +7 -0
  		ttm_bo_set_bulk_move(&shadow->tbo, NULL);
  		amdgpu_bo_unref(&shadow);
  	}
+
+	vm = entry->vm;
+	mutex_lock(&vm->tlb_flush_lock);
+	if (vm->tlb_fence_last)
+		amdgpu_vm_attach_tlb_fence(entry->bo, vm->tlb_fence_last);
+	mutex_unlock(&vm->tlb_flush_lock);
+

That approach doesn't make sense. Instead add the freed PT/PDs to a linked list in the parameters structure and only really free them after adding the fence to the root PD.

drivers/gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c line 34

Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +0 -0
  	ttm_bo_set_bulk_move(&entry->bo->tbo, NULL);
 
  	spin_lock(&entry->vm->status_lock);
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c +1 -0
index 569681badd7c..54ec81d30034 100644
@@ -31,6 +31,7 @@
  struct amdgpu_tlb_fence {
  	struct dma_fence	base;
  	struct amdgpu_device	*adev;
+	struct amdgpu_vm	*vm;

Big NAK to that. The VM might not live long enough to see the end of the TLB flush.

Regards,
Christian.

Quoted 43 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c +10 -2
  	struct dma_fence	*dependency;
  	struct work_struct	work;
  	spinlock_t		lock;
@@ -51,6 +52,7 @@ static const char *amdgpu_tlb_fence_get_timeline_name(struct dma_fence *f)
  static void amdgpu_tlb_fence_work(struct work_struct *work)
  {
  	struct amdgpu_tlb_fence *f = container_of(work, typeof(*f), work);
+	struct amdgpu_vm *vm = f->vm;
  	int r;
 
  	r = amdgpu_gmc_flush_gpu_tlb_pasid(f->adev, f->pasid, 2, true, 0);
@@ -62,6 +64,10 @@ static void amdgpu_tlb_fence_work(struct work_struct *work)
 
  	dma_fence_signal(&f->base);
  	dma_fence_put(&f->base);
+
+	mutex_lock(&vm->tlb_flush_lock);
+	vm->tlb_fence_last = NULL;
+	mutex_unlock(&vm->tlb_flush_lock);
  }
 
  static const struct dma_fence_ops amdgpu_tlb_fence_ops = {
@@ -92,6 +98,7 @@ void amdgpu_vm_tlb_fence_create(struct amdgpu_device *adev, struct amdgpu_vm *vm
  	f->adev = adev;
  	f->dependency = *fence;
  	f->pasid = vm->pasid;
+	f->vm = vm;
  	INIT_WORK(&f->work, amdgpu_tlb_fence_work);
  	spin_lock_init(&f->lock);
 
@@ -99,8 +106,10 @@ void amdgpu_vm_tlb_fence_create(struct amdgpu_device *adev, struct amdgpu_vm *vm
  		       vm->tlb_fence_context, atomic64_read(&vm->tlb_seq));
 
  	/* TODO: We probably need a separate wq here */
-	dma_fence_get(&f->base);
-	schedule_work(&f->work);
+	mutex_lock(&vm->tlb_flush_lock);
+	vm->tlb_fence_last = dma_fence_get(&f->base);
+	mutex_unlock(&vm->tlb_flush_lock);
 
+	schedule_work(&f->work);
  	*fence = &f->base;
  }
#4 lore raw

Re: [PATCH v2 3/3] drm/amdgpu: sync page table freeing with tlb flush

Hey Christian,

Quoted 107 lines · Christian König

Am 31.01.24 um 18:14 schrieb Shashank Sharma:

Quoted 104 lines

This patch:
- Attaches the TLB flush fence to the PT objects being freed
- Adds a new ptr in VM to save this last TLB flush fence
- Adds a new lock in VM to prevent out-of-context update of saved TLB flush fence
- Adds a new ptr in tlb_flush structure to save vm

The idea is to delay freeing of page table objects until we have the respective TLB entries flushed.

V2: rebase

Cc Christian König <[email protected]>
Cc Alex Deucher <[email protected]>
Cc Felix Kuehling <[email protected]>
Cc Rajneesh Bhardwaj <[email protected]>

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c        |  3 +++ drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h        |  4 +++ drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c     | 27 +++++++++++++++++++ .../gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c  | 13 +++++++--

  4 files changed, 45 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -0
index 67c690044b97..b0e81c249e3a 100644
@@ -2245,6 +2245,7 @@ int amdgpu_vm_init(struct amdgpu_device *adev, 
 struct amdgpu_vm *vm,
       vm->generation = 0;
         mutex_init(&vm->eviction_lock);
+    mutex_init(&vm->tlb_flush_lock);
       vm->evicting = false;
       vm->tlb_fence_context = dma_fence_context_alloc(1);
   @@ -2360,7 +2361,9 @@ int amdgpu_vm_make_compute(struct amdgpu_device *adev, struct amdgpu_vm *vm)

      }
        dma_fence_put(vm->last_update);
+    dma_fence_put(vm->tlb_fence_last);
      vm->last_update = dma_fence_get_stub();
+    vm->tlb_fence_last = dma_fence_get_stub();
      vm->is_compute_context = true;
        /* Free the shadow bo for compute VM */

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +0 -0
index 8e6fd25d07b7..b05bc586237f 100644
@@ -334,6 +334,10 @@ struct amdgpu_vm {
       uint64_t        *tlb_seq_cpu_addr;
       uint64_t        tlb_fence_context;

  +    /* Ptr and lock to maintain tlb flush sync */
+    struct mutex        tlb_flush_lock;
+    struct dma_fence        *tlb_fence_last;
+
      atomic64_t        kfd_last_flushed_seq;
        /* How many times we had to re-generate the page tables */

drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +0 -0
index 95dc0afdaffb..f1c4418c4d63 100644
@@ -631,6 +631,18 @@ static int amdgpu_vm_pt_alloc(struct 
 amdgpu_device *adev,
       return r;
   }

  +static inline
+void amdgpu_vm_attach_tlb_fence(struct amdgpu_bo *bo, struct dma_fence *fence)

drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +10 -0
+{
+    if (!bo || !fence)
+        return;
+
+    if (!dma_resv_reserve_fences(bo->tbo.base.resv, 1)) {
+        dma_resv_add_fence(bo->tbo.base.resv, fence,
+                   DMA_RESV_USAGE_BOOKKEEP);
+    }
+}
+

  /**
   * amdgpu_vm_pt_free - free one PD/PT
   *

drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +9 -0
@@ -638,6 +650,7 @@ static int amdgpu_vm_pt_alloc(struct 
 amdgpu_device *adev,
    */
   static void amdgpu_vm_pt_free(struct amdgpu_vm_bo_base *entry)
   {
+    struct amdgpu_vm *vm;
       struct amdgpu_bo *shadow;
         if (!entry->bo)
@@ -646,9 +659,23 @@ static void amdgpu_vm_pt_free(struct 
 amdgpu_vm_bo_base *entry)
       entry->bo->vm_bo = NULL;
       shadow = amdgpu_bo_shadowed(entry->bo);
       if (shadow) {
+        vm = shadow->vm_bo->vm;
+
+        mutex_lock(&vm->tlb_flush_lock);
+        if (vm->tlb_fence_last)
+            amdgpu_vm_attach_tlb_fence(shadow, vm->tlb_fence_last);
+        mutex_unlock(&vm->tlb_flush_lock);
+
           ttm_bo_set_bulk_move(&shadow->tbo, NULL);
           amdgpu_bo_unref(&shadow);
       }
+
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c static void amdgpu_vm_pt_free(struct amdgpu_vm_bo_base *entry) · line 678

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +6 -0
+    vm = entry->vm;
+    mutex_lock(&vm->tlb_flush_lock);
+    if (vm->tlb_fence_last)
+        amdgpu_vm_attach_tlb_fence(entry->bo, vm->tlb_fence_last);
+    mutex_unlock(&vm->tlb_flush_lock);
+

That approach doesn't make sense. Instead add the freed PT/PDs to a linked list in the parameters structure and only really free them after adding the fence to the root PD.

Sure, I will do those changes.

Just for the curiosity, why wouldn't this approach work ? Wouldn't this delay the actual freeing of buffers TTM until the fence signal ?

- Shashank

Quoted 60 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c line 34

Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +0 -0
 ttm_bo_set_bulk_move(&entry->bo->tbo, NULL);
         spin_lock(&entry->vm->status_lock);
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c +1 -0
index 569681badd7c..54ec81d30034 100644
@@ -31,6 +31,7 @@
   struct amdgpu_tlb_fence {
       struct dma_fence    base;
       struct amdgpu_device    *adev;
+    struct amdgpu_vm    *vm;

Big NAK to that. The VM might not live long enough to see the end of the TLB flush.

Regards,
Christian.

Quoted 40 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c +5 -0
       struct dma_fence    *dependency;
       struct work_struct    work;
       spinlock_t        lock;
@@ -51,6 +52,7 @@ static const char 
 *amdgpu_tlb_fence_get_timeline_name(struct dma_fence *f)
   static void amdgpu_tlb_fence_work(struct work_struct *work)
   {
       struct amdgpu_tlb_fence *f = container_of(work, typeof(*f), work);
+    struct amdgpu_vm *vm = f->vm;
       int r;
         r = amdgpu_gmc_flush_gpu_tlb_pasid(f->adev, f->pasid, 2, true, 0);
@@ -62,6 +64,10 @@ static void amdgpu_tlb_fence_work(struct 
 work_struct *work)
         dma_fence_signal(&f->base);
       dma_fence_put(&f->base);
+
+    mutex_lock(&vm->tlb_flush_lock);
+    vm->tlb_fence_last = NULL;
+    mutex_unlock(&vm->tlb_flush_lock);
   }
     static const struct dma_fence_ops amdgpu_tlb_fence_ops = {
@@ -92,6 +98,7 @@ void amdgpu_vm_tlb_fence_create(struct 

amdgpu_device *adev, struct amdgpu_vm *vm
      f->adev = adev;
      f->dependency = *fence;
      f->pasid = vm->pasid;
+    f->vm = vm;
      INIT_WORK(&f->work, amdgpu_tlb_fence_work);
      spin_lock_init(&f->lock);
  @@ -99,8 +106,10 @@ void amdgpu_vm_tlb_fence_create(struct amdgpu_device *adev, struct amdgpu_vm *vm vm->tlb_fence_context, atomic64_read(&vm->tlb_seq));
        /* TODO: We probably need a separate wq here */
-    dma_fence_get(&f->base);
-    schedule_work(&f->work);
+    mutex_lock(&vm->tlb_flush_lock);
+    vm->tlb_fence_last = dma_fence_get(&f->base);
+    mutex_unlock(&vm->tlb_flush_lock);
  +    schedule_work(&f->work);
      *fence = &f->base;
  }