drm/amdkfd: Track unified memory when switching xnack mode
Subsystems (3)
PATCH v3 1/1 drm/amdkfd: Track unified memory when switching xnack mode
Unified memory usage with xnack off is tracked to avoid oversubscribe system memory. When switching xnack mode from off to on, subsequent free ranges allocated with xnack off will not unreserve memory when xnack is on, cause memory accounting unbalanced.
When switching xnack mode from on to off, need reserve already allocated svm range memory because subsequent free ranges will unreserve memory with xnack off.
--- drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 30 ++++++++++++++++++++---- drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 20 +++++++++++++++- drivers/gpu/drm/amd/amdkfd/kfd_svm.h | 2 ++ 3 files changed, 46 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +25 -5
@@ -1594,16 +1594,36 @@ static int kfd_ioctl_set_xnack_mode(struct file *filep,if (args->xnack_enabled >=0 ) {if (!list_empty (&p->pqm.queues)) {pr_debug ("Process has user queues running\n" ); -mutex_unlock (&p->mutex); -return -EBUSY ; + r = -EBUSY ; + goto out_unlock; } -if (args->xnack_enabled && !kfd_process_xnack_mode (p, true)) + +if (p->xnack_enabled == args->xnack_enabled) + goto out_unlock; + +if (args->xnack_enabled && !kfd_process_xnack_mode (p, true)) { r = -EPERM ; -else - p->xnack_enabled = args->xnack_enabled; + goto out_unlock; + } + +pr_debug ("switching xnack from %d to %d\n" , p->xnack_enabled, + args->xnack_enabled); + +mutex_lock (&p->svms.lock); + +/* Switching to XNACK on/off, unreserve/reserve memory of all +* svm ranges. Change xnack must be inside svms lock, to avoid +* race with svm_range_deferred_list_work unreserve memory. +*/ + p->xnack_enabled = args->xnack_enabled; +svm_range_list_unreserve_mem (p, p->xnack_enabled); + +mutex_unlock (&p->svms.lock); }else { args->xnack_enabled = p->xnack_enabled; } + +out_unlock:mutex_unlock (&p->mutex);return r;
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +19 -1
@@ -278,7 +278,7 @@ static void svm_range_free(struct svm_range *prange, bool update_mem_usage)svm_range_free_dma_mappings (prange);if (update_mem_usage && !p->xnack_enabled) { -pr_debug ("unreserve mem limit: %lld\n" , size); +pr_debug ("unreserve prange 0x%p size: 0x%llx\n" , prange, size);amdgpu_amdkfd_unreserve_mem_limit (NULL , size,KFD_IOC_ALLOC_MEM_FLAGS_USERPTR ); }
@@ -2956,6 +2956,24 @@ svm_range_restore_pages(struct amdgpu_device *adev, unsigned int pasid,return r; } +void svm_range_list_unreserve_mem (struct kfd_process *p,bool unreserve) +{ +struct svm_range *prange; +uint64_t size; + +list_for_each_entry (prange, &p->svms.list, list) { + size = (prange->last - prange->start +1 ) <<PAGE_SHIFT ; +pr_debug ("svms 0x%p %s prange 0x%p size 0x%llx\n" , &p->svms, + unreserve ?"unreserve" :"reserve" , prange, size); +if (unreserve) +amdgpu_amdkfd_unreserve_mem_limit (NULL , size, +KFD_IOC_ALLOC_MEM_FLAGS_USERPTR ); +else +amdgpu_amdkfd_reserve_mem_limit (NULL , size, +KFD_IOC_ALLOC_MEM_FLAGS_USERPTR ); + } +} +void svm_range_list_fini (struct kfd_process *p) {struct svm_range *prange;
drivers/gpu/drm/amd/amdkfd/kfd_svm.h +2 -0
@@ -203,10 +203,12 @@ void svm_range_list_lock_and_flush_work(struct svm_range_list *svms, struct mm_svoid svm_range_bo_unref_async (struct svm_range_bo *svm_bo);void svm_range_set_max_pages (struct amdgpu_device *adev); +void svm_range_list_unreserve_mem (struct kfd_process *p,bool unreserve);#else struct kfd_process ; +void svm_range_list_unreserve_mem (struct kfd_process *p,bool unreserve) { }static inline int svm_range_list_init (struct kfd_process *p) {
Signature · 1 line
2.35.1
Felix Kuehling
maintainerQuoted 4 lines · Philip Yang
Unified memory usage with xnack off is tracked to avoid oversubscribe system memory. When switching xnack mode from off to on, subsequent free ranges allocated with xnack off will not unreserve memory when xnack is on, cause memory accounting unbalanced.
Here you describe half the problem.
Quoted 4 lines
When switching xnack mode from on to off, need reserve already allocated svm range memory because subsequent free ranges will unreserve memory with xnack off.
But here you describe the _other_ half of the solution. That's a bit confusing. Maybe state the other half of the problem first and then explain the solution that fixes both halves of the problem.
Quoted 27 lines
Signed-off-by Philip Yang <[email protected]>--- drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 30 ++++++++++++++++++++---- drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 20 +++++++++++++++- drivers/gpu/drm/amd/amdkfd/kfd_svm.h | 2 ++ 3 files changed, 46 insertions(+), 6 deletions(-)drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +7
-3@@ -1594,16 +1594,36 @@ static int kfd_ioctl_set_xnack_mode(struct file *filep,if (args->xnack_enabled >=0 ) {if (!list_empty (&p->pqm.queues)) {pr_debug ("Process has user queues running\n" ); -mutex_unlock (&p->mutex); -return -EBUSY ; + r = -EBUSY ; + goto out_unlock; } -if (args->xnack_enabled && !kfd_process_xnack_mode (p, true)) + +if (p->xnack_enabled == args->xnack_enabled) + goto out_unlock; + +if (args->xnack_enabled && !kfd_process_xnack_mode (p, true)) {
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c static int kfd_ioctl_set_xnack_mode(struct file *filep, · line 1612
Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +7
-2r = -EPERM ; - else - p->xnack_enabled = args->xnack_enabled; + goto out_unlock; + } + +pr_debug ("switching xnack from %d to %d\n" , p->xnack_enabled, + args->xnack_enabled); + +mutex_lock (&p->svms.lock);
It would be cleaner to do the locking inside svm_range_list_unreserve_mem.
Quoted 39 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +11
-0+ +/* Switching to XNACK on/off, unreserve/reserve memory of all +* svm ranges. Change xnack must be inside svms lock, to avoid +* race with svm_range_deferred_list_work unreserve memory. +*/ + p->xnack_enabled = args->xnack_enabled; +svm_range_list_unreserve_mem (p, p->xnack_enabled); + +mutex_unlock (&p->svms.lock); }else { args->xnack_enabled = p->xnack_enabled; } + +out_unlock:mutex_unlock (&p->mutex);return r;drivers/gpu/drm/amd/amdkfd/kfd_svm.c +6
-1@@ -278,7 +278,7 @@ static void svm_range_free(struct svm_range *prange, bool update_mem_usage)svm_range_free_dma_mappings (prange);if (update_mem_usage && !p->xnack_enabled) { -pr_debug ("unreserve mem limit: %lld\n" , size); +pr_debug ("unreserve prange 0x%p size: 0x%llx\n" , prange, size);amdgpu_amdkfd_unreserve_mem_limit (NULL , size,KFD_IOC_ALLOC_MEM_FLAGS_USERPTR ); }@@ -2956,6 +2956,24 @@ svm_range_restore_pages(struct amdgpu_device *adev, unsigned int pasid,return r; } +void svm_range_list_unreserve_mem (struct kfd_process *p,bool unreserve) +{ +struct svm_range *prange; +uint64_t size; +
drivers/gpu/drm/amd/amdkfd/kfd_svm.c svm_range_restore_pages(struct amdgpu_device *adev, unsigned int pasid, · line 2973
Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +10
-0+list_for_each_entry (prange, &p->svms.list, list) { + size = (prange->last - prange->start +1 ) <<PAGE_SHIFT ; +pr_debug ("svms 0x%p %s prange 0x%p size 0x%llx\n" , &p->svms, + unreserve ?"unreserve" :"reserve" , prange, size); +if (unreserve) +amdgpu_amdkfd_unreserve_mem_limit (NULL , size, +KFD_IOC_ALLOC_MEM_FLAGS_USERPTR ); +else +amdgpu_amdkfd_reserve_mem_limit (NULL , size, +KFD_IOC_ALLOC_MEM_FLAGS_USERPTR );
You are assuming that this will succeed. If it fails, you will end up with unbalanced accounting. You'll need to detect failures and roll back any reservations when a failure happens. Then fail the entire XNACK mode change.
Quoted 11 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +3
-0+ } +} +void svm_range_list_fini (struct kfd_process *p) {struct svm_range *prange;drivers/gpu/drm/amd/amdkfd/kfd_svm.h +0
-0
drivers/gpu/drm/amd/amdkfd/kfd_svm.h void svm_range_list_lock_and_flush_work(struct svm_range_list *svms, struct mm_s · line 211
Quoted 9 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.h +2
-0@@ -203,10 +203,12 @@ void svm_range_list_lock_and_flush_work(struct svm_range_list *svms, struct mm_svoid svm_range_bo_unref_async (struct svm_range_bo *svm_bo);void svm_range_set_max_pages (struct amdgpu_device *adev); +void svm_range_list_unreserve_mem (struct kfd_process *p,bool unreserve);#else struct kfd_process ; +void svm_range_list_unreserve_mem (struct kfd_process *p,bool unreserve) { }
I think it would be cleaner to put the entire kfd_ioctl_set_xnack_mode under an #ifdef, similar to kfd_ioctl_svm.
Regards,
Felix
Quoted 3 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.h +0
-0static inline int svm_range_list_init (struct kfd_process *p) {