drm/amdkfd: Move TLB flushing logic into amdgpu
Subsystems (3)
Felix Kuehling
maintainerPATCH 1/2 drm/amdkfd: Move TLB flushing logic into amdgpu
This will make it possible for amdgpu GEM ioctls to flush TLBs on compute VMs.
This removes VMID-based TLB flushing and always uses PASID-based flushing. This still works because it scans the VMID-PASID mapping registers to find the right VMID. It's only slightly less efficient. This is not a production use case.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.c | 30 +++++++++------------- drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h | 6 ++--- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h | 1 + drivers/gpu/drm/amd/amdkfd/kfd_priv.h | 9 +++++-- drivers/gpu/drm/amd/amdkfd/kfd_process.c | 28 -------------------- 5 files changed, 22 insertions(+), 52 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.c +12 -18
@@ -726,31 +726,25 @@ bool amdgpu_amdkfd_is_kfd_vmid(struct amdgpu_device *adev, u32 vmid)return false; } -int amdgpu_amdkfd_flush_gpu_tlb_vmid (struct amdgpu_device *adev, -uint16_t vmid) -{ -if (adev->family ==AMDGPU_FAMILY_AI ) { -int i; - -for (i =0 ; i < adev->num_vmhubs; i++) -amdgpu_gmc_flush_gpu_tlb (adev, vmid, i,0 ); - }else { -amdgpu_gmc_flush_gpu_tlb (adev, vmid,AMDGPU_GFXHUB_0 ,0 ); - } - -return 0 ; -} - -int amdgpu_amdkfd_flush_gpu_tlb_pasid (struct amdgpu_device *adev, -uint16_t pasid,enum TLB_FLUSH_TYPE flush_type) +int amdgpu_amdkfd_flush_tlb (struct amdgpu_device *adev,struct amdgpu_vm *vm, +enum TLB_FLUSH_TYPE type) { +uint64_t tlb_seq =amdgpu_vm_tlb_seq (vm);bool all_hub = false; +/* +* It can be that we race and lose here, but that is extremely unlikely +* and the worst thing which could happen is that we flush the changes +* into the TLB once more which is harmless. +*/ +if (atomic64_xchg (&vm->kfd_last_flushed_seq, tlb_seq) == tlb_seq) +return 0 ; +if (adev->family ==AMDGPU_FAMILY_AI || adev->family ==AMDGPU_FAMILY_RV ) all_hub = true; -return amdgpu_gmc_flush_gpu_tlb_pasid (adev, pasid, flush_type, all_hub); +return amdgpu_gmc_flush_gpu_tlb_pasid (adev, vm->pasid, type, all_hub); }bool amdgpu_amdkfd_have_atomics_support (struct amdgpu_device *adev)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +2 -4
@@ -157,10 +157,8 @@ int amdgpu_amdkfd_submit_ib(struct amdgpu_device *adev, uint32_t *ib_cmd, uint32_t ib_len);void amdgpu_amdkfd_set_compute_idle (struct amdgpu_device *adev,bool idle);bool amdgpu_amdkfd_have_atomics_support (struct amdgpu_device *adev); -int amdgpu_amdkfd_flush_gpu_tlb_vmid (struct amdgpu_device *adev, -uint16_t vmid); -int amdgpu_amdkfd_flush_gpu_tlb_pasid (struct amdgpu_device *adev, -uint16_t pasid,enum TLB_FLUSH_TYPE flush_type); +int amdgpu_amdkfd_flush_tlb (struct amdgpu_device *adev,struct amdgpu_vm *vm, +enum TLB_FLUSH_TYPE type);bool amdgpu_amdkfd_is_kfd_vmid (struct amdgpu_device *adev,u32 vmid);
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +1 -0
@@ -290,6 +290,7 @@ struct amdgpu_vm {/* Last finished delayed update */ atomic64_t tlb_seq;struct dma_fence *last_tlb_flush; +atomic64_t kfd_last_flushed_seq;/* Last unlocked submission to the scheduler entities */ struct dma_fence *last_unlocked;
drivers/gpu/drm/amd/amdkfd/kfd_priv.h +7 -2
@@ -715,7 +715,6 @@ struct kfd_process_device {/* VM context for GPUVM allocations */ struct file *drm_file;void *drm_priv; -atomic64_t tlb_seq;/* GPUVM allocations storage */ struct idr alloc_idr;
@@ -1344,7 +1343,13 @@ void kfd_signal_reset_event(struct kfd_dev *dev);void kfd_signal_poison_consumed_event (struct kfd_dev *dev,u32 pasid); -void kfd_flush_tlb (struct kfd_process_device *pdd,enum TLB_FLUSH_TYPE type); +static inline void kfd_flush_tlb (struct kfd_process_device *pdd,enum TLB_FLUSH_TYPE type) +{ +struct amdgpu_device *adev = pdd->dev->adev; +struct amdgpu_vm *vm =drm_priv_to_vm (pdd->drm_priv); + +amdgpu_amdkfd_flush_tlb (adev, vm, type); +}static inline bool kfd_flush_tlb_after_unmap (struct kfd_dev *dev) {
drivers/gpu/drm/amd/amdkfd/kfd_process.c +0 -28
@@ -1591,7 +1591,6 @@ int kfd_process_device_init_vm(struct kfd_process_device *pdd,return ret; } pdd->drm_priv = drm_file->private_data; -atomic64_set (&pdd->tlb_seq,0 ); ret =kfd_process_device_reserve_ib_mem (pdd);if (ret)
@@ -1994,33 +1993,6 @@ int kfd_reserved_mem_mmap(struct kfd_dev *dev, struct kfd_process *process, KFD_CWSR_TBA_TMA_SIZE, vma->vm_page_prot); } -void kfd_flush_tlb (struct kfd_process_device *pdd,enum TLB_FLUSH_TYPE type) -{ -struct amdgpu_vm *vm =drm_priv_to_vm (pdd->drm_priv); -uint64_t tlb_seq =amdgpu_vm_tlb_seq (vm); -struct kfd_dev *dev = pdd->dev; - -/* -* It can be that we race and lose here, but that is extremely unlikely -* and the worst thing which could happen is that we flush the changes -* into the TLB once more which is harmless. -*/ -if (atomic64_xchg (&pdd->tlb_seq, tlb_seq) == tlb_seq) -return ; - -if (dev->dqm->sched_policy ==KFD_SCHED_POLICY_NO_HWS ) { -/* Nothing to flush until a VMID is assigned, which -* only happens when the first queue is created. -*/ -if (pdd->qpd.vmid) -amdgpu_amdkfd_flush_gpu_tlb_vmid (dev->adev, - pdd->qpd.vmid); - }else { -amdgpu_amdkfd_flush_gpu_tlb_pasid (dev->adev, - pdd->process->pasid, type); - } -} -struct kfd_process_device *kfd_process_device_data_by_id (struct kfd_process *p,uint32_t gpu_id) {int i;
Signature · 1 line
2.34.1
Felix Kuehling
maintainerPATCH 2/2 drm/amdgpu: Synchronize after mapping into a compute VM
Compute VMs use user mode queues for command submission. They cannot use a CS ioctl to synchronize with pending PTE updates and flush TLBs. Do this synchronization in amdgpu_gem_va_ioctl for compute VMs.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +14 -2
@@ -601,7 +601,7 @@ int amdgpu_gem_metadata_ioctl(struct drm_device *dev, void *data,static void amdgpu_gem_va_update_vm (struct amdgpu_device *adev,struct amdgpu_vm *vm,struct amdgpu_bo_va *bo_va, -uint32_t operation) +uint32_t operation,uint32_t flags) {int r;
@@ -620,6 +620,18 @@ static void amdgpu_gem_va_update_vm(struct amdgpu_device *adev, } r =amdgpu_vm_update_pdes (adev, vm, false); +if (r) + goto error; + +if (vm->is_compute_context) { +if (bo_va->last_pt_update) + r =dma_fence_wait (bo_va->last_pt_update, true); +if (!r && vm->last_update) + r =dma_fence_wait (vm->last_update, true); +if (!r) + r =amdgpu_amdkfd_flush_tlb (adev, vm, +TLB_FLUSH_LEGACY ); + } error:if (r && r != -ERESTARTSYS )
@@ -789,7 +801,7 @@ int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data, }if (!r && !(args->flags &AMDGPU_VM_DELAY_UPDATE ) && !amdgpu_vm_debug)amdgpu_gem_va_update_vm (adev, &fpriv->vm, bo_va, - args->operation); + args->operation, args->flags); error_backoff:ttm_eu_backoff_reservation (&ticket, &list);
Signature · 1 line
2.34.1
Re: [PATCH 2/2] drm/amdgpu: Synchronize after mapping into a compute VM
Quoted 30 lines · Felix Kuehling
Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.
Compute VMs use user mode queues for command submission. They cannot use a CS ioctl to synchronize with pending PTE updates and flush TLBs. Do this synchronization in
amdgpu_gem_va_ioctlfor compute VMs.Signed-off-by Felix Kuehling <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +2
-1@@ -601,7 +601,7 @@ int amdgpu_gem_metadata_ioctl(struct drm_device *dev, void *data,static void amdgpu_gem_va_update_vm (struct amdgpu_device *adev,struct amdgpu_vm *vm,struct amdgpu_bo_va *bo_va, -uint32_t operation) +uint32_t operation,uint32_t flags) {int r;@@ -620,6 +620,18 @@ static void amdgpu_gem_va_update_vm(struct amdgpu_device *adev, } r =amdgpu_vm_update_pdes (adev, vm, false); +if (r)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c static void amdgpu_gem_va_update_vm(struct amdgpu_device *adev, · line 633
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +10
-0+ goto error; + +if (vm->is_compute_context) { +if (bo_va->last_pt_update) + r =dma_fence_wait (bo_va->last_pt_update, true); +if (!r && vm->last_update) + r =dma_fence_wait (vm->last_update, true); +if (!r) + r =amdgpu_amdkfd_flush_tlb (adev, vm, +TLB_FLUSH_LEGACY );
When kfd flush its vm tlb it uses TLB_FLUSH_LEGACY after map and TLB_FLUSH_HEAVYWEIGHT after unmap. Here use amdgpu_gen_va_ioctl to map/unmap buffer in kfd vm. Should we keep consistent with kfd:
use TLB_FLUSH_LEGACY if args->operation is AMDGPU_VA_OP_MAP, uses TLB_FLUSH_HEAVYWEIGHT if args->operation is AMDGPU_VA_OP_UNMAP?
Quoted 16 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +2
-2+ } error:if (r && r != -ERESTARTSYS )@@ -789,7 +801,7 @@ int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data, }if (!r && !(args->flags &AMDGPU_VM_DELAY_UPDATE ) && !amdgpu_vm_debug)amdgpu_gem_va_update_vm (adev, &fpriv->vm, bo_va, - args->operation); + args->operation, args->flags); error_backoff:ttm_eu_backoff_reservation (&ticket, &list); --2.34.1
Re: [PATCH 2/2] drm/amdgpu: Synchronize after mapping into a compute VM
In reply to Felix KuehlingAm 25.02.23 um 00:36 schrieb Felix Kuehling:
Quoted 28 lines
Compute VMs use user mode queues for command submission. They cannot use a CS ioctl to synchronize with pending PTE updates and flush TLBs. Do this synchronization in
amdgpu_gem_va_ioctlfor compute VMs.Signed-off-by Felix Kuehling <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +3
-1@@ -601,7 +601,7 @@ int amdgpu_gem_metadata_ioctl(struct drm_device *dev, void *data,static void amdgpu_gem_va_update_vm (struct amdgpu_device *adev,struct amdgpu_vm *vm,struct amdgpu_bo_va *bo_va, -uint32_t operation) +uint32_t operation,uint32_t flags) {int r;@@ -620,6 +620,18 @@ static void amdgpu_gem_va_update_vm(struct amdgpu_device *adev, } r =amdgpu_vm_update_pdes (adev, vm, false); +if (r) + goto error;
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c static void amdgpu_gem_va_update_vm(struct amdgpu_device *adev, · line 634
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +10
-0+ +if (vm->is_compute_context) { +if (bo_va->last_pt_update) + r =dma_fence_wait (bo_va->last_pt_update, true); +if (!r && vm->last_update) + r =dma_fence_wait (vm->last_update, true); +if (!r) + r =amdgpu_amdkfd_flush_tlb (adev, vm, +TLB_FLUSH_LEGACY ); + }
Mhm, that's not really something we can do here. The GEM VA IOCTL is supposed to be async and never block.
Can we do that on the KFD side in some IOCTL instead?
Regards,
Christian.
Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +1
-1error:if (r && r != -ERESTARTSYS )@@ -789,7 +801,7 @@ int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data, }if (!r && !(args->flags &AMDGPU_VM_DELAY_UPDATE ) && !amdgpu_vm_debug)amdgpu_gem_va_update_vm (adev, &fpriv->vm, bo_va, - args->operation); + args->operation, args->flags); error_backoff:ttm_eu_backoff_reservation (&ticket, &list);
Felix Kuehling
maintainerRe: [PATCH 2/2] drm/amdgpu: Synchronize after mapping into a compute VM
Quoted 41 lines · Christian König
Am 25.02.23 um 00:36 schrieb Felix Kuehling:
Quoted 29 lines
Compute VMs use user mode queues for command submission. They cannot use a CS ioctl to synchronize with pending PTE updates and flush TLBs. Do this synchronization in
amdgpu_gem_va_ioctlfor compute VMs.Signed-off-by Felix Kuehling <[email protected]>---
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c| 16 ++++++++++++++--1 file changed, 14 insertions(+), 2 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +1
-1@@ -601,7 +601,7 @@ int amdgpu_gem_metadata_ioctl(struct drm_device *dev, void *data, static void amdgpu_gem_va_update_vm(struct amdgpu_device *adev, struct amdgpu_vm *vm, struct amdgpu_bo_va *bo_va, - uint32_t operation) + uint32_t operation, uint32_t flags) { int r;@@ -620,6 +620,18 @@ static void amdgpu_gem_va_update_vm(struct
amdgpu_device*adev, }
r =amdgpu_vm_update_pdes(adev, vm, false);
+ if (r)
+ goto error;
+
+ if (vm->is_compute_context) {Quoted 8 lines
+ if (
bo_va->last_pt_update)
+ r =dma_fence_wait(bo_va->last_pt_update, true); + if (!r &&vm->last_update)
+ r =dma_fence_wait(vm->last_update, true); + if (!r)
+ r =amdgpu_amdkfd_flush_tlb(adev, vm, + TLB_FLUSH_LEGACY);
+ }Mhm, that's not really something we can do here. The GEM VA IOCTL is supposed to be async and never block.
Quoted 1 line
Can we do that on the KFD side in some IOCTL instead?
Not really. There is no existing KFD ioctl I would call after GEM_VA. The whole point was to use GEM ioctls to manages virtual address mappings for KFD and avoid adding more KFD ioctls that duplicate similar functionality.
If you wanted to tie it into the existing amdgpu memory manager, I guess we'd need a dummy command submission that does the synchronization and TLB flush and the wait for the fence from that.
Regards,
Felix
Quoted 16 lines
Regards,
Christian.Quoted 11 lines
error:
if (r && r !=-ERESTARTSYS)drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +1
-1@@ -789,7 +801,7 @@ int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data, } if (!r && !(args->flags & AMDGPU_VM_DELAY_UPDATE) && !amdgpu_vm_debug) amdgpu_gem_va_update_vm(adev, &fpriv->vm, bo_va, - args->operation); + args->operation, args->flags);
error_backoff:
ttm_eu_backoff_reservation(&ticket, &list);