drm/amdgpu: implement TLB flush fence
Subsystems (2)
PATCH v4 1/2 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)
V4: - add a wait for (f->dependency) in tlb_fence_work (Christian)
- move the misplaced fence_create call to the end (Philip)
--- 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 | 111 ++++++++++++++++++ 4 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c
drivers/gpu/drm/amd/amdgpu/Makefile +2 -1
@@ -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
@@ -988,6 +988,15 @@ int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm, r = vm->update_funcs->commit (¶ms, fence); +/* 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 ); + } + error_unlock:amdgpu_vm_eviction_unlock (vm);drm_dev_exit (idx);
@@ -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
@@ -341,6 +341,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;
@@ -594,5 +595,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 +111 -0
111 lines not on this page · raw message
Signature · 1 line
2.43.2
PATCH v4 2/2 drm/amdgpu: sync page table freeing with tlb flush
The idea behind this patch is to delay the freeing of PT entry objects until the TLB flush is done.
This patch:
- Adds a tlb_flush_waitlist which will keep the objects that need to be freed after tlb_flush
- Adds PT entries in this list in amdgpu_vm_pt_free_dfs, instead of freeing them immediately.
- Exports function amdgpu_vm_pt_free to be called dircetly.
- Adds a 'force' input bool to amdgpu_vm_pt_free_dfs to differentiate between immediate freeing of the BOs (like from amdgpu_vm_pt_free_root) vs delayed freeing.
V2: rebase
V4: (Christian)
- add only locked PTEs entries in TLB flush waitlist.
- do not create a separate function for list flush.
- do not create a new lock for TLB flush.
- there is no need to wait on tlb_flush_fence exclusively.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 10 ++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h | 4 ++++ drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c | 21 ++++++++++++++------- 3 files changed, 28 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +10 -0
@@ -990,11 +990,20 @@ int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm,/* Prepare a TLB flush fence to be attached to PTs */ if (!unlocked && params.needs_flush && vm->is_compute_context) { +struct amdgpu_vm_bo_base *entry, *next; +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 ); + +if (list_empty (&vm->tlb_flush_waitlist)) + goto error_unlock; + +/* Now actually free the waitlist */ +list_for_each_entry_safe (entry, next, &vm->tlb_flush_waitlist, vm_status) +amdgpu_vm_pt_free (entry); } error_unlock:
@@ -2214,6 +2223,7 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,INIT_LIST_HEAD (&vm->pt_freed);INIT_WORK (&vm->pt_free_work, amdgpu_vm_pt_free_work);INIT_KFIFO (vm->faults); +INIT_LIST_HEAD (&vm->tlb_flush_waitlist); r =amdgpu_seq64_alloc (adev, &vm->tlb_seq_va, &vm->tlb_seq_cpu_addr);if (r)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +4 -0
@@ -343,6 +343,9 @@ struct amdgpu_vm {uint64_t *tlb_seq_cpu_addr;uint64_t tlb_fence_context; +/* temporary storage of PT BOs until the TLB flush */ +struct list_head tlb_flush_waitlist; +atomic64_t kfd_last_flushed_seq;/* How many times we had to re-generate the page tables */
@@ -545,6 +548,7 @@ int amdgpu_vm_ptes_update(struct amdgpu_vm_update_params *params, uint64_t start, uint64_t end, uint64_t dst, uint64_t flags);void amdgpu_vm_pt_free_work (struct work_struct *work); +void amdgpu_vm_pt_free (struct amdgpu_vm_bo_base *entry);#if defined(CONFIG_DEBUG_FS )void amdgpu_debugfs_vm_bo_info (struct amdgpu_vm *vm,struct seq_file *m);
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +14 -7
@@ -636,7 +636,7 @@ 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) +void amdgpu_vm_pt_free (struct amdgpu_vm_bo_base *entry) {struct amdgpu_bo *shadow;
@@ -685,13 +685,15 @@ void amdgpu_vm_pt_free_work(struct work_struct *work)* @vm: amdgpu vm structure * @start: optional cursor where to start freeing PDs/PTs * @unlocked: vm resv unlock status +* @force: force free all PDs/PTs without waiting for TLB flush * * Free the page directory or page table level and all sub levels. */ static void amdgpu_vm_pt_free_dfs (struct amdgpu_device *adev,struct amdgpu_vm *vm,struct amdgpu_vm_pt_cursor *start, -bool unlocked) +bool unlocked, +bool force) {struct amdgpu_vm_pt_cursor cursor;struct amdgpu_vm_bo_base *entry;
@@ -708,11 +710,15 @@ static void amdgpu_vm_pt_free_dfs(struct amdgpu_device *adev,return ; } -for_each_amdgpu_vm_pt_dfs_safe (adev, vm, start, cursor, entry) -amdgpu_vm_pt_free (entry); +for_each_amdgpu_vm_pt_dfs_safe (adev, vm, start, cursor, entry) { +if (!force) +list_move (&entry->vm_status, &vm->tlb_flush_waitlist); +else +amdgpu_vm_pt_free (entry); + }if (start) -amdgpu_vm_pt_free (start->entry); +list_move (&start->entry->vm_status, &vm->tlb_flush_waitlist); }/**
@@ -724,7 +730,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 , false); +amdgpu_vm_pt_free_dfs (adev, vm,NULL , false, true); }/**
@@ -1059,7 +1065,8 @@ int amdgpu_vm_ptes_update(struct amdgpu_vm_update_params *params, params->needs_flush = true;amdgpu_vm_pt_free_dfs (adev, params->vm, &cursor, - params->unlocked); + params->unlocked, + false); }amdgpu_vm_pt_next (adev, &cursor); }
Signature · 1 line
2.43.2
Christian König
maintainerRe: [PATCH v4 2/2] drm/amdgpu: sync page table freeing with tlb flush
Am 01.03.24 um 12:07 schrieb Shashank Sharma:
Quoted 43 lines
The idea behind this patch is to delay the freeing of PT entry objects until the TLB flush is done.
This patch:
- Adds atlb_flush_waitlistwhich will keep the objects that need to be freed aftertlb_flush
- Adds PT entries in this list inamdgpu_vm_pt_free_dfs, instead of freeing them immediately.
- Exports functionamdgpu_vm_pt_freeto be called dircetly.
- Adds a 'force' input bool toamdgpu_vm_pt_free_dfsto differentiate between immediate freeing of the BOs (like fromamdgpu_vm_pt_free_root) vs delayed freeing.V2: rebase
V4: (Christian)
- add only locked PTEs entries in TLB flush waitlist.
- do not create a separate function for list flush.
- do not create a new lock for TLB flush.
- there is no need to wait ontlb_flush_fenceexclusively.Cc Christian König <[email protected]>Cc Alex Deucher <[email protected]>Cc Felix Kuehling <[email protected]>Cc Rajneesh Bhardwaj <[email protected]>Signed-off-by Shashank Sharma <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 10 ++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h | 4 ++++ drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c | 21 ++++++++++++++------- 3 files changed, 28 insertions(+), 7 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2
-0@@ -990,11 +990,20 @@ int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm,/* Prepare a TLB flush fence to be attached to PTs */ if (!unlocked && params.needs_flush && vm->is_compute_context) { +struct amdgpu_vm_bo_base *entry, *next; +amdgpu_vm_tlb_fence_create (adev, vm, fence);
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm, · line 1006
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +7
-0/* 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 ); + +if (list_empty (&vm->tlb_flush_waitlist)) + goto error_unlock; + +/* Now actually free the waitlist */ +list_for_each_entry_safe (entry, next, &vm->tlb_flush_waitlist, vm_status) +amdgpu_vm_pt_free (entry);
This needs to be outside of the "if". We also need to free the PDs/PTs in non compute VMs.
Quoted 70 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1
-0} error_unlock:@@ -2214,6 +2223,7 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,INIT_LIST_HEAD (&vm->pt_freed);INIT_WORK (&vm->pt_free_work, amdgpu_vm_pt_free_work);INIT_KFIFO (vm->faults); +INIT_LIST_HEAD (&vm->tlb_flush_waitlist); r =amdgpu_seq64_alloc (adev, &vm->tlb_seq_va, &vm->tlb_seq_cpu_addr);if (r)drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +4
-0@@ -343,6 +343,9 @@ struct amdgpu_vm {uint64_t *tlb_seq_cpu_addr;uint64_t tlb_fence_context; +/* temporary storage of PT BOs until the TLB flush */ +struct list_head tlb_flush_waitlist; +atomic64_t kfd_last_flushed_seq;/* How many times we had to re-generate the page tables */ @@ -545,6 +548,7 @@ int amdgpu_vm_ptes_update(struct amdgpu_vm_update_params *params, uint64_t start, uint64_t end, uint64_t dst, uint64_t flags);void amdgpu_vm_pt_free_work (struct work_struct *work); +void amdgpu_vm_pt_free (struct amdgpu_vm_bo_base *entry);#if defined(CONFIG_DEBUG_FS )void amdgpu_debugfs_vm_bo_info (struct amdgpu_vm *vm,struct seq_file *m);drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +5
-4@@ -636,7 +636,7 @@ 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) +void amdgpu_vm_pt_free (struct amdgpu_vm_bo_base *entry) {struct amdgpu_bo *shadow;@@ -685,13 +685,15 @@ void amdgpu_vm_pt_free_work(struct work_struct *work)* @vm: amdgpu vm structure * @start: optional cursor where to start freeing PDs/PTs * @unlocked: vm resv unlock status +* @force: force free all PDs/PTs without waiting for TLB flush * * Free the page directory or page table level and all sub levels. */ static void amdgpu_vm_pt_free_dfs (struct amdgpu_device *adev,struct amdgpu_vm *vm,struct amdgpu_vm_pt_cursor *start, -bool unlocked) +bool unlocked, +bool force) {struct amdgpu_vm_pt_cursor cursor;struct amdgpu_vm_bo_base *entry;@@ -708,11 +710,15 @@ static void amdgpu_vm_pt_free_dfs(struct amdgpu_device *adev,return ; } -for_each_amdgpu_vm_pt_dfs_safe (adev, vm, start, cursor, entry) -amdgpu_vm_pt_free (entry); +for_each_amdgpu_vm_pt_dfs_safe (adev, vm, start, cursor, entry) {
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c static void amdgpu_vm_pt_free_dfs(struct amdgpu_device *adev, · line 722
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +6
-1+if (!force) +list_move (&entry->vm_status, &vm->tlb_flush_waitlist); +else +amdgpu_vm_pt_free (entry); + }if (start) -amdgpu_vm_pt_free (start->entry); +list_move (&start->entry->vm_status, &vm->tlb_flush_waitlist); }
The complexity inside amdgpu_vm_pt_free_dfs() really starts to make no sense any more.
First of all ditch the usage in amdgpu_vm_pt_free_root(). Instead just inline the call to for_each_amdgpu_vm_pt_dfs_safe() to free up all of the page tables immediately.
Then the other use case in amdgpu_vm_ptes_update():
/* Make sure previous mapping is freed */
if (cursor.entry->bo) {
params->table_freed = true;
amdgpu_vm_pt_free_dfs(adev, params->vm, &cursor, params->unlocked); }
We should basically change params->table_freed into a list_head and put all to be freed entries on there. Something like that:
spin_lock(&vm->status_lock); for_each_amdgpu_vm_pt_dfs_safe(adev, vm, start, cursor, entry) list_move(&entry->vm_status, ¶ms->table_freed); spin_unlock(&vm->status_lock);
And then finally in amdgpu_vm_update_range() we should call amdgpu_vm_pt_free_dfs() to really free up all PDs/PTs (probably rename the function while at it).
Regards,
Christian.
Quoted 21 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +3
-2/** @@ -724,7 +730,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 , false); +amdgpu_vm_pt_free_dfs (adev, vm,NULL , false, true); }/** @@ -1059,7 +1065,8 @@ int amdgpu_vm_ptes_update(struct amdgpu_vm_update_params *params, params->needs_flush = true;amdgpu_vm_pt_free_dfs (adev, params->vm, &cursor, - params->unlocked); + params->unlocked, + false); }amdgpu_vm_pt_next (adev, &cursor); }
Re: [PATCH v4 2/2] drm/amdgpu: sync page table freeing with tlb flush
Quoted 51 lines · Christian König
Am 01.03.24 um 12:07 schrieb Shashank Sharma:
Quoted 48 lines
The idea behind this patch is to delay the freeing of PT entry objects until the TLB flush is done.
This patch:
- Adds atlb_flush_waitlistwhich will keep the objects that need to be freed aftertlb_flush
- Adds PT entries in this list inamdgpu_vm_pt_free_dfs, instead of freeing
them immediately.
- Exports functionamdgpu_vm_pt_freeto be called dircetly.
- Adds a 'force' input bool toamdgpu_vm_pt_free_dfsto differentiate between immediate freeing of the BOs (like fromamdgpu_vm_pt_free_root) vs delayed freeing.V2: rebase
V4: (Christian)
- add only locked PTEs entries in TLB flush waitlist.
- do not create a separate function for list flush.
- do not create a new lock for TLB flush.
- there is no need to wait ontlb_flush_fenceexclusively.Cc Christian König <[email protected]>Cc Alex Deucher <[email protected]>Cc Felix Kuehling <[email protected]>Cc Rajneesh Bhardwaj <[email protected]>Signed-off-by Shashank Sharma <[email protected]>---
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 10 ++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h | 4 ++++drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c| 21 ++++++++++++++-------3 files changed, 28 insertions(+), 7 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +4
-0@@ -990,11 +990,20 @@ int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm, /* Prepare a TLB flush fence to be attached to PTs */ if (!unlocked && params.needs_flush && vm->is_compute_context) { + struct amdgpu_vm_bo_base *entry, *next; + 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); + + if (list_empty(&vm->tlb_flush_waitlist))
Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm, · line 1006
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +5
-0+ goto error_unlock; + + /* Now actually free the waitlist */ + list_for_each_entry_safe(entry, next, &vm->tlb_flush_waitlist, vm_status) + amdgpu_vm_pt_free(entry);This needs to be outside of the "if". We also need to free the PDs/PTs in non compute VMs.
Noted,
Quoted 80 lines
Quoted 67 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0
-0}
error_unlock:drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1
-0@@ -2214,6 +2223,7 @@ int amdgpu_vm_init(struct amdgpu_device *adev,struct amdgpu_vm *vm, INIT_LIST_HEAD(&vm->pt_freed); INIT_WORK(&vm->pt_free_work, amdgpu_vm_pt_free_work); INIT_KFIFO(vm->faults); + INIT_LIST_HEAD(&vm->tlb_flush_waitlist); r = amdgpu_seq64_alloc(adev, &vm->tlb_seq_va, &vm->tlb_seq_cpu_addr); if (r)drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +0
-0@@ -343,6 +343,9 @@ struct amdgpu_vm { uint64_t *tlb_seq_cpu_addr; uint64_t tlb_fence_context;+ /* temporary storage of PT BOs until the TLB flush */ + struct list_head
tlb_flush_waitlist;
+
atomic64_tkfd_last_flushed_seq;
/* How many times we had to re-generate the page tables */drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +1
-0@@ -545,6 +548,7 @@ int amdgpu_vm_ptes_update(structamdgpu_vm_update_params *params, uint64_t start, uint64_t end, uint64_t dst, uint64_t flags); void amdgpu_vm_pt_free_work(struct work_struct *work); +void amdgpu_vm_pt_free (struct amdgpu_vm_bo_base *entry); #if defined(CONFIG_DEBUG_FS) void amdgpu_debugfs_vm_bo_info(struct amdgpu_vm *vm, struct seq_file *m);drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +1
-1@@ -636,7 +636,7 @@ static int amdgpu_vm_pt_alloc(structamdgpu_device *adev, * * @entry: PDE to free */ -static void amdgpu_vm_pt_free (struct amdgpu_vm_bo_base *entry) +void amdgpu_vm_pt_free (struct amdgpu_vm_bo_base *entry) { struct amdgpu_bo *shadow;@@ -685,13 +685,15 @@ void
amdgpu_vm_pt_free_work(struct work_struct *work)
* @vm: amdgpu vm structure
* @start: optional cursor where to start freeing PDs/PTs
* @unlocked: vm resv unlock status
+ * @force: force free all PDs/PTs without waiting for TLB flush *
* Free the page directory or page table level and all sub levels. */
static void amdgpu_vm_pt_free_dfs(structamdgpu_device*adev, structamdgpu_vm*vm,
structamdgpu_vm_pt_cursor*start,
- bool unlocked)
+ bool unlocked,
+ bool force)
{
structamdgpu_vm_pt_cursorcursor;
structamdgpu_vm_bo_base*entry;drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +0
-1@@ -708,11 +710,15 @@ static void amdgpu_vm_pt_free_dfs(structamdgpu_device *adev, return; } - for_each_amdgpu_vm_pt_dfs_safe(adev, vm, start, cursor, entry) - amdgpu_vm_pt_free(entry);drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c static void amdgpu_vm_pt_free_dfs(struct · line 722
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +7
-1+ for_each_amdgpu_vm_pt_dfs_safe(adev, vm, start, cursor, entry) { + if (!force) + list_move(&entry->vm_status, &vm->tlb_flush_waitlist); + else + amdgpu_vm_pt_free(entry); + } if (start) - amdgpu_vm_pt_free(start->entry); + list_move(&start->entry->vm_status, &vm->tlb_flush_waitlist); }The complexity inside
amdgpu_vm_pt_free_dfs()really starts to make no sense any more.
Quoted 1 line
First of all ditch the usage in
amdgpu_vm_pt_free_root(). Instead just inline the call tofor_each_amdgpu_vm_pt_dfs_safe()to free up all of the page tables immediately.
Noted,
Quoted 9 lines
Then the other use case in
amdgpu_vm_ptes_update():/* Make sure previous mapping is freed */
if (cursor.entry->bo) {
params->table_freed= true;
amdgpu_vm_pt_free_dfs(adev, params->vm, &cursor, params->unlocked); }
Quoted 8 lines
We should basically change
params->table_freedinto alist_headand put all to be freed entries on there. Something like that:
spin_lock(&vm->status_lock);for_each_amdgpu_vm_pt_dfs_safe(adev, vm, start, cursor, entry)list_move(&entry->vm_status, ¶ms->table_freed);spin_unlock(&vm->status_lock);And then finally in
amdgpu_vm_update_range()we should callamdgpu_vm_pt_free_dfs()to really free up all PDs/PTs (probably rename the function while at it).
- in amdgpu_vm_pt_free_dfs, there are two different lists for locked and unlocked PT entries
unlocked is saved in vm->pt_freed
locked is freed immediately, which we now want to add into another list (params->table_freed in your suggestion) and free after tlb_flush
which means we have to do something like this:
in amdgpu_vm_ptes_update {
/* Make sure previous mapping is freed */
if (cursor.entry->bo) {
for_each_amdgpu_vm_pt_dfs_safe(adev, vm, start, cursor, entry)
if (unlocked) {
list_move(&entry->vm_status, &vm->pt_free);
else
list_move(&entry->vm_status, ¶ms->table_freed);
}
}
and then we have to change the amdgpu_vm_pt_free_dfs() implementation to simply free BOs, not searching anything like:
if (unlocked) {
schedule_work(pt_free_work)
} else {
list_for_each(params->table_freed)
amdgpu_vm_pt_free(entry)
}
and then we would be able to call amdgpu_vm_pt_free_dfs() from amdgpu_vm_update_range().
Does that sound good to you ?
- Shashank
Quoted 25 lines
Regards,
Christian.Quoted 21 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +3
-2/**@@ -724,7 +730,7 @@ static void amdgpu_vm_pt_free_dfs(structamdgpu_device *adev, */ void amdgpu_vm_pt_free_root(struct amdgpu_device *adev, struct amdgpu_vm *vm) { - amdgpu_vm_pt_free_dfs(adev, vm, NULL, false); + amdgpu_vm_pt_free_dfs(adev, vm, NULL, false, true); } /**@@ -1059,7 +1065,8 @@ int amdgpu_vm_ptes_update(structamdgpu_vm_update_params *params, params->needs_flush = true; amdgpu_vm_pt_free_dfs(adev, params->vm, &cursor, - params->unlocked); + params->unlocked, + false); } amdgpu_vm_pt_next(adev, &cursor);}
Christian König
maintainerRe: [PATCH v4 2/2] drm/amdgpu: sync page table freeing with tlb flush
Am 01.03.24 um 21:17 schrieb Sharma, Shashank:
Quoted 182 lines · Christian König
Quoted 51 lines
Am 01.03.24 um 12:07 schrieb Shashank Sharma:
Quoted 48 lines
The idea behind this patch is to delay the freeing of PT entry objects until the TLB flush is done.
This patch:
- Adds atlb_flush_waitlistwhich will keep the objects that need to be freed aftertlb_flush
- Adds PT entries in this list inamdgpu_vm_pt_free_dfs, instead of freeing
them immediately.
- Exports functionamdgpu_vm_pt_freeto be called dircetly.
- Adds a 'force' input bool toamdgpu_vm_pt_free_dfsto differentiate between immediate freeing of the BOs (like fromamdgpu_vm_pt_free_root) vs delayed freeing.V2: rebase
V4: (Christian)
- add only locked PTEs entries in TLB flush waitlist.
- do not create a separate function for list flush.
- do not create a new lock for TLB flush.
- there is no need to wait ontlb_flush_fenceexclusively.Cc Christian König <[email protected]>Cc Alex Deucher <[email protected]>Cc Felix Kuehling <[email protected]>Cc Rajneesh Bhardwaj <[email protected]>Signed-off-by Shashank Sharma <[email protected]>---
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 10 ++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h | 4 ++++drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c| 21 ++++++++++++++-------3 files changed, 28 insertions(+), 7 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +4
-0@@ -990,11 +990,20 @@ int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm, /* Prepare a TLB flush fence to be attached to PTs */ if (!unlocked && params.needs_flush && vm->is_compute_context) { + struct amdgpu_vm_bo_base *entry, *next; + 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); + + if (list_empty(&vm->tlb_flush_waitlist))Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm, · line 1006
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +5
-0+ goto error_unlock; + + /* Now actually free the waitlist */ + list_for_each_entry_safe(entry, next, &vm->tlb_flush_waitlist, vm_status) + amdgpu_vm_pt_free(entry);This needs to be outside of the "if". We also need to free the PDs/PTs in non compute VMs.
Noted,
Quoted 80 lines
Quoted 67 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0
-0}
error_unlock:drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1
-0@@ -2214,6 +2223,7 @@ int amdgpu_vm_init(struct amdgpu_device *adev,struct amdgpu_vm *vm, INIT_LIST_HEAD(&vm->pt_freed); INIT_WORK(&vm->pt_free_work, amdgpu_vm_pt_free_work); INIT_KFIFO(vm->faults); + INIT_LIST_HEAD(&vm->tlb_flush_waitlist); r = amdgpu_seq64_alloc(adev, &vm->tlb_seq_va, &vm->tlb_seq_cpu_addr); if (r)drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +0
-0@@ -343,6 +343,9 @@ struct amdgpu_vm { uint64_t *tlb_seq_cpu_addr; uint64_t tlb_fence_context;+ /* temporary storage of PT BOs until the TLB flush */ + struct list_head
tlb_flush_waitlist;
+
atomic64_tkfd_last_flushed_seq;
/* How many times we had to re-generate the page tables */drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +1
-0@@ -545,6 +548,7 @@ int amdgpu_vm_ptes_update(structamdgpu_vm_update_params *params, uint64_t start, uint64_t end, uint64_t dst, uint64_t flags); void amdgpu_vm_pt_free_work(struct work_struct *work); +void amdgpu_vm_pt_free (struct amdgpu_vm_bo_base *entry); #if defined(CONFIG_DEBUG_FS) void amdgpu_debugfs_vm_bo_info(struct amdgpu_vm *vm, struct seq_file *m);drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +1
-1@@ -636,7 +636,7 @@ static int amdgpu_vm_pt_alloc(structamdgpu_device *adev, * * @entry: PDE to free */ -static void amdgpu_vm_pt_free (struct amdgpu_vm_bo_base *entry) +void amdgpu_vm_pt_free (struct amdgpu_vm_bo_base *entry) { struct amdgpu_bo *shadow;@@ -685,13 +685,15 @@ void
amdgpu_vm_pt_free_work(struct work_struct *work)
* @vm: amdgpu vm structure
* @start: optional cursor where to start freeing PDs/PTs
* @unlocked: vm resv unlock status
+ * @force: force free all PDs/PTs without waiting for TLB flush *
* Free the page directory or page table level and all sub levels. */
static void amdgpu_vm_pt_free_dfs(structamdgpu_device*adev, structamdgpu_vm*vm,
structamdgpu_vm_pt_cursor*start,
- bool unlocked)
+ bool unlocked,
+ bool force)
{
structamdgpu_vm_pt_cursorcursor;
structamdgpu_vm_bo_base*entry;drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +0
-1@@ -708,11 +710,15 @@ static void amdgpu_vm_pt_free_dfs(structamdgpu_device *adev, return; } - for_each_amdgpu_vm_pt_dfs_safe(adev, vm, start, cursor, entry) - amdgpu_vm_pt_free(entry);drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c static void amdgpu_vm_pt_free_dfs(struct · line 722
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +7
-1+ for_each_amdgpu_vm_pt_dfs_safe(adev, vm, start, cursor, entry) { + if (!force) + list_move(&entry->vm_status, &vm->tlb_flush_waitlist); + else + amdgpu_vm_pt_free(entry); + } if (start) - amdgpu_vm_pt_free(start->entry); + list_move(&start->entry->vm_status, &vm->tlb_flush_waitlist); }The complexity inside
amdgpu_vm_pt_free_dfs()really starts to make no sense any more.Quoted 1 line
First of all ditch the usage in
amdgpu_vm_pt_free_root(). Instead just inline the call tofor_each_amdgpu_vm_pt_dfs_safe()to free up all of the page tables immediately.Noted,
Quoted 9 lines
Then the other use case in
amdgpu_vm_ptes_update():/* Make sure previous mapping is freed */
if (cursor.entry->bo) {
params->table_freed= true;
amdgpu_vm_pt_free_dfs(adev, params->vm, &cursor, params->unlocked); }Quoted 8 lines
We should basically change
params->table_freedinto alist_headand put all to be freed entries on there. Something like that:
spin_lock(&vm->status_lock);for_each_amdgpu_vm_pt_dfs_safe(adev, vm, start, cursor, entry)list_move(&entry->vm_status, ¶ms->table_freed);spin_unlock(&vm->status_lock);And then finally in
amdgpu_vm_update_range()we should callamdgpu_vm_pt_free_dfs()to really free up all PDs/PTs (probably rename the function while at it).- in
amdgpu_vm_pt_free_dfs, there are two different lists for locked and unlocked PT entriesunlocked is saved in
vm->pt_freedlocked is freed immediately, which we now want to add into another list (
params->table_freedin your suggestion) and free aftertlb_flushwhich means we have to do something like this:
in
amdgpu_vm_ptes_update{/* Make sure previous mapping is freed */
if (cursor.entry->bo) {
Quoted 8 lines
for_each_amdgpu_vm_pt_dfs_safe(adev, vm, start, cursor, entry)if (unlocked) {
list_move(&entry->vm_status, &vm->pt_free);else
list_move(&entry->vm_status, ¶ms->table_freed);
No, here just move everything to params->tables_freed as necessary.
Quoted 8 lines
}
}
and then we have to change the
amdgpu_vm_pt_free_dfs()implementation to simply free BOs, not searching anything like:if (unlocked) {
And then do here a list_splice() to move everything from tables_freed to pt_free.
Quoted 7 lines
schedule_work(pt_free_work)} else {
list_for_each(params->table_freed)
Quoted 7 lines
amdgpu_vm_pt_free(entry)}
and then we would be able to call
amdgpu_vm_pt_free_dfs()fromamdgpu_vm_update_range().Does that sound good to you ?
Very close, I just want to avoid any extra unlocked handling in the low level PTE update function.
Essentially we only record which PDs/PTs to free in there because we have a cursor at the right location in that moment.
The state machine itself is something I try to keep out of the PTE update code.
Regards,
Christian.
Quoted 29 lines
- Shashank
Quoted 25 lines
Regards,
Christian.Quoted 21 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +3
-2/**@@ -724,7 +730,7 @@ static void amdgpu_vm_pt_free_dfs(structamdgpu_device *adev, */ void amdgpu_vm_pt_free_root(struct amdgpu_device *adev, struct amdgpu_vm *vm) { - amdgpu_vm_pt_free_dfs(adev, vm, NULL, false); + amdgpu_vm_pt_free_dfs(adev, vm, NULL, false, true); } /**@@ -1059,7 +1065,8 @@ int amdgpu_vm_ptes_update(structamdgpu_vm_update_params *params, params->needs_flush = true; amdgpu_vm_pt_free_dfs(adev, params->vm, &cursor, - params->unlocked); + params->unlocked, + false); } amdgpu_vm_pt_next(adev, &cursor);}
Re: [PATCH v4 2/2] drm/amdgpu: sync page table freeing with tlb flush
In reply to Shashank SharmaOn 2024-03-01 06:07, Shashank Sharma wrote:
The idea behind this patch is to delay the freeing of PT entry objects until the TLB flush is done. This patch: - Adds a tlb_flush_waitlist which will keep the objects that need to be freed after tlb_flush - Adds PT entries in this list in amdgpu_vm_pt_free_dfs, instead of freeing them immediately. - Exports function amdgpu_vm_pt_free to be called dircetly. - Adds a 'force' input bool to amdgpu_vm_pt_free_dfs to differentiate between immediate freeing of the BOs (like from amdgpu_vm_pt_free_root) vs delayed freeing. V2: rebase V4: (Christian) - add only locked PTEs entries in TLB flush waitlist. - do not create a separate function for list flush. - do not create a new lock for TLB flush. - there is no need to wait on tlb_flush_fence exclusively. Cc: Christian König <[email protected]> Cc: Alex Deucher <[email protected]> Cc: Felix Kuehling <[email protected]> Cc: Rajneesh Bhardwaj <[email protected]> Signed-off-by: Shashank Sharma <[email protected]> --- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 10 ++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h | 4 ++++ drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c | 21 ++++++++++++++------- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c index 310aae6fb49b..94581a1fe34f 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c @@ -990,11 +990,20 @@ int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm, /* Prepare a TLB flush fence to be attached to PTs */ if (!unlocked && params.needs_flush && vm->is_compute_context) { + struct amdgpu_vm_bo_base *entry, *next; + 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); + + if (list_empty(&vm->tlb_flush_waitlist)) + goto error_unlock; + + /* Now actually free the waitlist */ + list_for_each_entry_safe(entry, next, &vm->tlb_flush_waitlist, vm_status) + amdgpu_vm_pt_free(entry); } error_unlock: @@ -2214,6 +2223,7 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm, INIT_LIST_HEAD(&vm->pt_freed); INIT_WORK(&vm->pt_free_work, amdgpu_vm_pt_free_work); INIT_KFIFO(vm->faults); + INIT_LIST_HEAD(&vm->tlb_flush_waitlist); r = amdgpu_seq64_alloc(adev, &vm->tlb_seq_va, &vm->tlb_seq_cpu_addr); if (r) diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h index 298f604b8e5f..ba374c2c61bd 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h @@ -343,6 +343,9 @@ struct amdgpu_vm { uint64_t *tlb_seq_cpu_addr; uint64_t tlb_fence_context; + /* temporary storage of PT BOs until the TLB flush */ + struct list_head tlb_flush_waitlist; + atomic64_t kfd_last_flushed_seq; /* How many times we had to re-generate the page tables */ @@ -545,6 +548,7 @@ int amdgpu_vm_ptes_update(struct amdgpu_vm_update_params *params, uint64_t start, uint64_t end, uint64_t dst, uint64_t flags); void amdgpu_vm_pt_free_work(struct work_struct *work); +void amdgpu_vm_pt_free(struct amdgpu_vm_bo_base *entry); #if defined(CONFIG_DEBUG_FS) void amdgpu_debugfs_vm_bo_info(struct amdgpu_vm *vm, struct seq_file *m); diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c index 95dc0afdaffb..cb14e5686c0f 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c @@ -636,7 +636,7 @@ 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) +void amdgpu_vm_pt_free(struct amdgpu_vm_bo_base *entry) { struct amdgpu_bo *shadow; @@ -685,13 +685,15 @@ void amdgpu_vm_pt_free_work(struct work_struct *work) * @vm: amdgpu vm structure * @start: optional cursor where to start freeing PDs/PTs * @unlocked: vm resv unlock status + * @force: force free all PDs/PTs without waiting for TLB flush * * Free the page directory or page table level and all sub levels. */ static void amdgpu_vm_pt_free_dfs(struct amdgpu_device *adev, struct amdgpu_vm *vm, struct amdgpu_vm_pt_cursor *start, - bool unlocked) + bool unlocked, + bool force) { struct amdgpu_vm_pt_cursor cursor; struct amdgpu_vm_bo_base *entry; @@ -708,11 +710,15 @@ static void amdgpu_vm_pt_free_dfs(struct amdgpu_device *adev, return; } - for_each_amdgpu_vm_pt_dfs_safe(adev, vm, start, cursor, entry) - amdgpu_vm_pt_free(entry); I feel like if we attach tlb flush fence before free pt bo, then don't need tlb_flush_waitlist.
Regards,
Philip
+ for_each_amdgpu_vm_pt_dfs_safe(adev, vm, start, cursor, entry) { + if (!force) + list_move(&entry->vm_status, &vm->tlb_flush_waitlist); + else + amdgpu_vm_pt_free(entry); + } if (start) - amdgpu_vm_pt_free(start->entry); + list_move(&start->entry->vm_status, &vm->tlb_flush_waitlist); } /** @@ -724,7 +730,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, false); + amdgpu_vm_pt_free_dfs(adev, vm, NULL, false, true); } /** @@ -1059,7 +1065,8 @@ int amdgpu_vm_ptes_update(struct amdgpu_vm_update_params *params, params->needs_flush = true; amdgpu_vm_pt_free_dfs(adev, params->vm, &cursor, - params->unlocked); + params->unlocked, + false); } amdgpu_vm_pt_next(adev, &cursor); }
Am 01.03.24 um 12:07 schrieb Shashank Sharma:
Quoted 149 lines
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
- setdma_fence_erroronly in case of error
- addtlb_flushfence only when PT/PD BO is locked (Felix)
- usevm->pasidwhen f is NULL (Mukul)V4: - add a wait for (
f->dependency) intlb_fence_work(Christian)
- move the misplacedfence_createcall to the end (Philip)Cc Christian Koenig <[email protected]>Cc Felix Kuehling <[email protected]>Cc Rajneesh Bhardwaj <[email protected]>Cc Alex Deucher <[email protected]>Signed-off-by Christian König <[email protected]>Signed-off-by Shashank Sharma <[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 | 111 ++++++++++++++++++ 4 files changed, 127 insertions(+), 1 deletion(-)create mode 100644
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.cdrivers/gpu/drm/amd/amdgpu/Makefile +2
-1@@ -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@@ -988,6 +988,15 @@ int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm, r = vm->update_funcs->commit (¶ms, fence); +/* 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 ); + } + error_unlock:amdgpu_vm_eviction_unlock (vm);drm_dev_exit (idx);@@ -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@@ -341,6 +341,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;@@ -594,5 +595,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 +48
-0@@ -0,0 +1,111 @@ +// SPDX-License-Identifier: GPL-2.0 OR MIT +/* +* Copyright 2023 Advanced Micro Devices, Inc. +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR +* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +* OTHER DEALINGS IN THE SOFTWARE. +*/ + +#include <linux/dma-fence.h> +#include <linux/workqueue.h> + +#include "amdgpu.h" +#include "amdgpu_vm.h" +#include "amdgpu_gmc.h" + +struct amdgpu_tlb_fence { +struct dma_fence base; +struct amdgpu_device *adev; +struct dma_fence *dependency; +struct work_struct work; +spinlock_t lock; +uint16_t pasid; + +}; + +static const char *amdgpu_tlb_fence_get_driver_name (struct dma_fence *fence) +{ +return "amdgpu tlb fence" ; +} + +static const char *amdgpu_tlb_fence_get_timeline_name (struct dma_fence *f) +{ +return "amdgpu tlb timeline" ;
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c line 58
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c new file +10
-0+} + +static void amdgpu_tlb_fence_work (struct work_struct *work) +{ +struct amdgpu_tlb_fence *f =container_of (work,typeof (*f), work); +int r; + +if (f->dependency) { +dma_fence_wait (f->dependency, false); + f->dependency =dma_fence_get_stub ();
That doesn't correctly drops the f->depency reference.
You need something like "dma_fence_put(f->depency); f->dependency = NULL;" here instead.
Apart from that looks good to me,
Christian.
Quoted 53 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c new file +53
-0+ } + + r =amdgpu_gmc_flush_gpu_tlb_pasid (f->adev, f->pasid,2 , true,0 ); +if (r) { +dev_err (f->adev->dev,"TLB flush failed for PASID %d.\n" , + f->pasid); +dma_fence_set_error (&f->base, r); + } + +dma_fence_signal (&f->base); +dma_fence_put (&f->base); +} + +static const struct dma_fence_ops amdgpu_tlb_fence_ops = { + .use_64bit_seqno = true, + .get_driver_name = amdgpu_tlb_fence_get_driver_name, + .get_timeline_name = amdgpu_tlb_fence_get_timeline_name +}; + +void amdgpu_vm_tlb_fence_create (struct amdgpu_device *adev,struct amdgpu_vm *vm, +struct dma_fence **fence) +{ +struct amdgpu_tlb_fence *f; + + f =kmalloc (sizeof (*f),GFP_KERNEL ); +if (!f) { +/* +* 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); + +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->pasid = vm->pasid; +INIT_WORK (&f->work, amdgpu_tlb_fence_work); +spin_lock_init (&f->lock); + +dma_fence_init (&f->base, &amdgpu_tlb_fence_ops, &f->lock, + 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); + + *fence = &f->base; +}
Quoted 174 lines · Christian König
Am 01.03.24 um 12:07 schrieb Shashank Sharma:
Quoted 160 lines
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
- setdma_fence_erroronly in case of error
- addtlb_flushfence only when PT/PD BO is locked (Felix)
- usevm->pasidwhen f is NULL (Mukul)V4: - add a wait for (
f->dependency) intlb_fence_work(Christian)
- move the misplacedfence_createcall to the end (Philip)Cc Christian Koenig <[email protected]>Cc Felix Kuehling <[email protected]>Cc Rajneesh Bhardwaj <[email protected]>Cc Alex Deucher <[email protected]>Signed-off-by Christian König <[email protected]>Signed-off-by Shashank Sharma <[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 | 111 ++++++++++++++++++4 files changed, 127 insertions(+), 1 deletion(-)create mode 100644
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.cdrivers/gpu/drm/amd/amdgpu/Makefile +2
-1@@ -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 +0
-0@@ -988,6 +988,15 @@ int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm, r = vm->update_funcs->commit(¶ms, fence);+ /* 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);drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +6
-0+ + /* 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); + } +
error_unlock:
amdgpu_vm_eviction_unlock(vm);
drm_dev_exit(idx);drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1
-0@@ -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 +1
-0@@ -341,6 +341,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; @@ -594,5 +595,8 @@ void amdgpu_vm_update_fault_cache(struct amdgpu_device *adev, uint64_t addr,
uint32_tstatus,
unsigned int vmhub);
+void amdgpu_vm_tlb_fence_create(structamdgpu_device*adev, + structamdgpu_vm*vm,
+ structdma_fence**fence); #endifdrivers/gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c new file +5
-0@@ -0,0 +1,111 @@ +// SPDX-License-Identifier: GPL-2.0 OR MIT +/* +* Copyright 2023 Advanced Micro Devices, Inc. +* +* Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ */drivers/gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c new file +19
-0+ +#include <linux/dma-fence.h> +#include <linux/workqueue.h> + +#include "amdgpu.h" +#include "amdgpu_vm.h" +#include "amdgpu_gmc.h" + +struct amdgpu_tlb_fence { + struct dma_fence base; + struct amdgpu_device *adev; + struct dma_fence *dependency; + struct work_struct work; + spinlock_t lock; + uint16_t pasid; + +}; + +static const char *amdgpu_tlb_fence_get_driver_name(struct dma_fence*fence)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c new file +5
-0+{ + return "amdgpu tlb fence"; +} + +static const char *amdgpu_tlb_fence_get_timeline_name(struct
dma_fence*f)drivers/gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c new file +2
-0+{ + return "amdgpu tlb timeline";drivers/gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c line 58
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c new file +10
-0+} + +static void amdgpu_tlb_fence_work (struct work_struct *work) +{ + struct amdgpu_tlb_fence *f = container_of(work, typeof(*f), work); + int r; + + if (f->dependency) { + dma_fence_wait(f->dependency, false); + f->dependency = dma_fence_get_stub();That doesn't correctly drops the
f->depencyreference.
Quoted 3 lines
You need something like "
dma_fence_put(f->depency);f->dependency= NULL;" here instead.Apart from that looks good to me,
Noted, will update.
- Shashank
Quoted 58 lines
Christian.
Quoted 55 lines
+ }
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c new file +19
-0+ + r = amdgpu_gmc_flush_gpu_tlb_pasid(f->adev, f->pasid, 2, true, 0); + if (r) { + dev_err(f->adev->dev, "TLB flush failed for PASID %d.\n", + f->pasid); + dma_fence_set_error(&f->base, r); + } + + dma_fence_signal(&f->base); + dma_fence_put(&f->base); +} + +static const struct dma_fence_ops amdgpu_tlb_fence_ops = { + .use_64bit_seqno = true, + .get_driver_name = amdgpu_tlb_fence_get_driver_name, + .get_timeline_name = amdgpu_tlb_fence_get_timeline_name +}; + +void amdgpu_vm_tlb_fence_create(struct amdgpu_device *adev, struct
amdgpu_vm*vm,
+ structdma_fence**fence)drivers/gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c new file +7
-0+{ + struct amdgpu_tlb_fence *f; + + f = kmalloc(sizeof(*f), GFP_KERNEL); + if (!f) { + /* +* 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);drivers/gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c new file +21
-0+ + 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->pasid = vm->pasid; + INIT_WORK(&f->work, amdgpu_tlb_fence_work); + spin_lock_init(&f->lock); + + dma_fence_init(&f->base, &amdgpu_tlb_fence_ops, &f->lock, + 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); + + *fence = &f->base; +}
On 2024-03-01 06:07, Shashank Sharma wrote:
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) V4: - add a wait for (f->dependency) in tlb_fence_work (Christian) - move the misplaced fence_create call to the end (Philip) Cc: Christian Koenig <[email protected]> Cc: Felix Kuehling <[email protected]> Cc: Rajneesh Bhardwaj <[email protected]> Cc: Alex Deucher <[email protected]> Signed-off-by: Christian König <[email protected]> Signed-off-by: Shashank Sharma <[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 | 111 ++++++++++++++++++ 4 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c diff --git a/drivers/gpu/drm/amd/amdgpu/Makefile b/drivers/gpu/drm/amd/amdgpu/Makefile index fa26a4e3a99d..91ab4cf29b5b 100644 --- a/drivers/gpu/drm/amd/amdgpu/Makefile +++ b/drivers/gpu/drm/amd/amdgpu/Makefile @@ -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 \ diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c index 0960e0a665d3..310aae6fb49b 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c @@ -988,6 +988,15 @@ int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm, r = vm->update_funcs->commit(¶ms, fence); + /* 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); + } + Adding fence here seems too late, the fence has to add before calling amdgpu_vm_pt_free_dfs inside amdgpu_vm_ptes_update.error_unlock: amdgpu_vm_eviction_unlock(vm); drm_dev_exit(idx); @@ -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); diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h index 64b3f69efa57..298f604b8e5f 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h @@ -341,6 +341,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; @@ -594,5 +595,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 diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c new file mode 100644 index 000000000000..54c33c24fa46 --- /dev/null +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c @@ -0,0 +1,111 @@ +// SPDX-License-Identifier: GPL-2.0 OR MIT +/* + * Copyright 2023 Advanced Micro Devices, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + */ + +#include <linux/dma-fence.h> +#include <linux/workqueue.h> + +#include "amdgpu.h" +#include "amdgpu_vm.h" +#include "amdgpu_gmc.h" + +struct amdgpu_tlb_fence { + struct dma_fence base; + struct amdgpu_device *adev; + struct dma_fence *dependency; + struct work_struct work; + spinlock_t lock; + uint16_t pasid; + +}; + +static const char *amdgpu_tlb_fence_get_driver_name(struct dma_fence *fence) +{ + return "amdgpu tlb fence"; +} + +static const char *amdgpu_tlb_fence_get_timeline_name(struct dma_fence *f) +{ + return "amdgpu tlb timeline"; +} + +static void amdgpu_tlb_fence_work(struct work_struct *work) +{ + struct amdgpu_tlb_fence *f = container_of(work, typeof(*f), work); + int r; + + if (f->dependency) { + dma_fence_wait(f->dependency, false); + f->dependency = dma_fence_get_stub(); + } + + r = amdgpu_gmc_flush_gpu_tlb_pasid(f->adev, f->pasid, 2, true, 0); should flush all xcc of kfd node->xcc_mask, but cannot get kfd node here.
maybe we could signal tlb flush fence inside kfd_flush_tlb, and remove this work completely.
Regards,
Philip
+ if (r) { + dev_err(f->adev->dev, "TLB flush failed for PASID %d.\n", + f->pasid); + dma_fence_set_error(&f->base, r); + } + + dma_fence_signal(&f->base); + dma_fence_put(&f->base); +} + +static const struct dma_fence_ops amdgpu_tlb_fence_ops = { + .use_64bit_seqno = true, + .get_driver_name = amdgpu_tlb_fence_get_driver_name, + .get_timeline_name = amdgpu_tlb_fence_get_timeline_name +}; + +void amdgpu_vm_tlb_fence_create(struct amdgpu_device *adev, struct amdgpu_vm *vm, + struct dma_fence **fence) +{ + struct amdgpu_tlb_fence *f; + + f = kmalloc(sizeof(*f), GFP_KERNEL); + if (!f) { + /* + * 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); + + 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->pasid = vm->pasid; + INIT_WORK(&f->work, amdgpu_tlb_fence_work); + spin_lock_init(&f->lock); + + dma_fence_init(&f->base, &amdgpu_tlb_fence_ops, &f->lock, + 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); + + *fence = &f->base; +}