drm/amdkfd: Page aligned VRAM reserve size
Subsystems (3)
PATCH 1/1 drm/amdkfd: Page aligned VRAM reserve size
Use page aligned size to reserve VRAM usage because page aligned TTM BO size is used to unreserve VRAM usage, otherwise this cause vram_used accounting unbalanced.
Change vram_used definition type to int64_t to be able to trigger WARN_ONCE(adev && adev->kfd.vram_used < 0, "..."), to help debug the accouting issue with warning and backtrace.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h | 2 +- drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +1 -1
@@ -97,7 +97,7 @@ struct amdgpu_amdkfd_fence {struct amdgpu_kfd_dev {struct kfd_dev *dev; -uint64_t vram_used; +int64_t vram_used;uint64_t vram_used_aligned;bool init_complete;struct work_struct reset_work;
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +1 -1
@@ -151,7 +151,7 @@ int amdgpu_amdkfd_reserve_mem_limit(struct amdgpu_device *adev,* to avoid fragmentation caused by 4K allocations in the tail * 2M BO chunk. */ - vram_needed = size; + vram_needed =PAGE_ALIGN (size); }else if (alloc_flag &KFD_IOC_ALLOC_MEM_FLAGS_USERPTR ) { system_mem_needed = size; } else if (!(alloc_flag &
Signature · 1 line
2.35.1
Felix Kuehling
maintainerAm 2023-01-09 um 19:01 schrieb Philip Yang:
Quoted 32 lines
Use page aligned size to reserve VRAM usage because page aligned TTM BO size is used to unreserve VRAM usage, otherwise this cause
vram_usedaccounting unbalanced.Change
vram_useddefinition type toint64_tto be able to triggerWARN_ONCE(adev && adev->kfd.vram_used < 0, "..."), to help debug the accouting issue with warning and backtrace.Signed-off-by Philip Yang <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h | 2 +- drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +1
-1@@ -97,7 +97,7 @@ struct amdgpu_amdkfd_fence {struct amdgpu_kfd_dev {struct kfd_dev *dev; -uint64_t vram_used; +int64_t vram_used;uint64_t vram_used_aligned;bool init_complete;struct work_struct reset_work;drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +0
-0
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c int amdgpu_amdkfd_reserve_mem_limit(struct amdgpu_device *adev, · line 154
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +1
-1@@ -151,7 +151,7 @@ int amdgpu_amdkfd_reserve_mem_limit(struct amdgpu_device *adev,* to avoid fragmentation caused by 4K allocations in the tail * 2M BO chunk. */ - vram_needed = size; + vram_needed =PAGE_ALIGN (size);
This only solves part of the problem. size is used in other places in this function that should all use the page-aligned size. I think we should do the page-alignment at a much higher level, in kfd_ioctl_alloc_memory_of_gpu. That way all the kernel code can safely assume that buffer sizes are page aligned, and we avoid future surprises.
Regards,
Felix
Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +0
-0}else if (alloc_flag &KFD_IOC_ALLOC_MEM_FLAGS_USERPTR ) { system_mem_needed = size; } else if (!(alloc_flag &
Quoted 32 lines · Felix Kuehling
Am 2023-01-09 um 19:01 schrieb Philip Yang:
Quoted 31 lines
Use page aligned size to reserve VRAM usage because page aligned TTM BO size is used to unreserve VRAM usage, otherwise this cause
vram_usedaccounting unbalanced.Change
vram_useddefinition type toint64_tto be able to triggerWARN_ONCE(adev && adev->kfd.vram_used < 0, "..."), to help debug the accouting issue with warning and backtrace.Signed-off-by Philip Yang <[email protected]>---
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h | 2 +-drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c| 2 +-2 files changed, 2 insertions(+), 2 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +1
-1@@ -97,7 +97,7 @@ struct amdgpu_amdkfd_fence { struct amdgpu_kfd_dev { struct kfd_dev *dev; - uint64_t vram_used; + int64_t vram_used; uint64_t vram_used_aligned; bool init_complete; struct work_struct reset_work;drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +0
-0@@ -151,7 +151,7 @@ int amdgpu_amdkfd_reserve_mem_limit(struct
Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c int amdgpu_amdkfd_reserve_mem_limit(struct amdgpu_device *adev, · line 154
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +1
-1amdgpu_device *adev, * to avoid fragmentation caused by 4K allocations in the tail * 2M BO chunk. */ - vram_needed = size; + vram_needed = PAGE_ALIGN(size);This only solves part of the problem. size is used in other places in this function that should all use the page-aligned size. I think we should do the page-alignment at a much higher level, in
kfd_ioctl_alloc_memory_of_gpu. That way all the kernel code can safely assume that buffer sizes are page aligned, and we avoid future surprises.
yes, the error handling unreserve should use aligned_size too, and size is also used as number of pages in amdgpu_bo_create for DOMAIN_GWS etc, we can not pass aligned size at higher level, I will send v2 patch for review.
Regards,
Philip
Quoted 8 lines
Regards,
FelixQuoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +0
-0} else if (alloc_flag & KFD_IOC_ALLOC_MEM_FLAGS_USERPTR) { system_mem_needed = size;} else if (!(alloc_flag &
PATCH 1/1 drm/amdkfd: Page aligned memory reserve size
In reply to Philip YangUse page aligned size to reserve memory usage because page aligned TTM BO size is used to unreserve memory usage, otherwise no page aligned size causes memory usage accounting unbalanced.
Change vram_used definition type to int64_t to be able to trigger WARN_ONCE(adev && adev->kfd.vram_used < 0, "..."), to help debug the accouting issue with warning and backtrace.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h | 2 +- drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 9 ++++++--- drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 7 +++++-- 3 files changed, 12 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +1 -1
@@ -97,7 +97,7 @@ struct amdgpu_amdkfd_fence {struct amdgpu_kfd_dev {struct kfd_dev *dev; -uint64_t vram_used; +int64_t vram_used;uint64_t vram_used_aligned;bool init_complete;struct work_struct reset_work;
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +6 -3
@@ -1598,6 +1598,7 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(struct amdgpu_bo *bo;struct drm_gem_object *gobj =NULL ;u32 domain, alloc_domain; +uint64_t aligned_size;u64 alloc_flags;int ret;
@@ -1653,13 +1654,15 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(* the memory. */ if ((*mem)->aql_queue) - size = size >>1 ; + size >>=1 ; + + aligned_size =PAGE_ALIGN (size); (*mem)->alloc_flags = flags;amdgpu_sync_create (&(*mem)->sync); - ret =amdgpu_amdkfd_reserve_mem_limit (adev, size, flags); + ret =amdgpu_amdkfd_reserve_mem_limit (adev, aligned_size, flags);if (ret) {pr_debug ("Insufficient memory\n" ); goto err_reserve_limit;
@@ -1725,7 +1728,7 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(/* Don't unreserve system mem limit twice */ goto err_reserve_limit; err_bo_create: -amdgpu_amdkfd_unreserve_mem_limit (adev, size, flags); +amdgpu_amdkfd_unreserve_mem_limit (adev, aligned_size, flags); err_reserve_limit:mutex_destroy (&(*mem)->lock);if (gobj)
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +5 -2
@@ -1127,8 +1127,11 @@ static int kfd_ioctl_alloc_memory_of_gpu(struct file *filep, }/* Update the VRAM usage count */ -if (flags &KFD_IOC_ALLOC_MEM_FLAGS_VRAM ) -WRITE_ONCE (pdd->vram_usage, pdd->vram_usage + args->size); +if (flags &KFD_IOC_ALLOC_MEM_FLAGS_VRAM ) { +if (flags &KFD_IOC_ALLOC_MEM_FLAGS_AQL_QUEUE_MEM ) + args->size >>=1 ; +WRITE_ONCE (pdd->vram_usage, pdd->vram_usage +PAGE_ALIGN (args->size)); + }mutex_unlock (&p->mutex);
Signature · 1 line
2.35.1
Felix Kuehling
maintainerRe: [PATCH] drm/amdkfd: Page aligned memory reserve size
Am 2023-01-10 um 12:11 schrieb Philip Yang:
Quoted 41 lines
Use page aligned size to reserve memory usage because page aligned TTM BO size is used to unreserve memory usage, otherwise no page aligned size causes memory usage accounting unbalanced.
Change
vram_useddefinition type toint64_tto be able to triggerWARN_ONCE(adev && adev->kfd.vram_used < 0, "..."), to help debug the accouting issue with warning and backtrace.Signed-off-by Philip Yang <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h | 2 +- drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 9 ++++++--- drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 7 +++++-- 3 files changed, 12 insertions(+), 6 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +1
-1@@ -97,7 +97,7 @@ struct amdgpu_amdkfd_fence {struct amdgpu_kfd_dev {struct kfd_dev *dev; -uint64_t vram_used; +int64_t vram_used;uint64_t vram_used_aligned;bool init_complete;struct work_struct reset_work;drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +1
-0@@ -1598,6 +1598,7 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(struct amdgpu_bo *bo;struct drm_gem_object *gobj =NULL ;u32 domain, alloc_domain; +uint64_t aligned_size;u64 alloc_flags;int ret;
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu( · line 1659
Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +3
-1@@ -1653,13 +1654,15 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(* the memory. */ if ((*mem)->aql_queue) - size = size >>1 ; + size >>=1 ; + + aligned_size =PAGE_ALIGN (size);
Why do you need a new variable for this? Can't you just update size to be page-aligned here? Is the unaligned size still needed anywhere?
Quoted 24 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +2
-2(*mem)->alloc_flags = flags;amdgpu_sync_create (&(*mem)->sync); - ret =amdgpu_amdkfd_reserve_mem_limit (adev, size, flags); + ret =amdgpu_amdkfd_reserve_mem_limit (adev, aligned_size, flags);if (ret) {pr_debug ("Insufficient memory\n" ); goto err_reserve_limit;@@ -1725,7 +1728,7 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(/* Don't unreserve system mem limit twice */ goto err_reserve_limit; err_bo_create: -amdgpu_amdkfd_unreserve_mem_limit (adev, size, flags); +amdgpu_amdkfd_unreserve_mem_limit (adev, aligned_size, flags); err_reserve_limit:mutex_destroy (&(*mem)->lock);if (gobj)drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +0
-0
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c static int kfd_ioctl_alloc_memory_of_gpu(struct file *filep, · line 1132
Quoted 8 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +3
-2@@ -1127,8 +1127,11 @@ static int kfd_ioctl_alloc_memory_of_gpu(struct file *filep, }/* Update the VRAM usage count */ -if (flags &KFD_IOC_ALLOC_MEM_FLAGS_VRAM ) -WRITE_ONCE (pdd->vram_usage, pdd->vram_usage + args->size); +if (flags &KFD_IOC_ALLOC_MEM_FLAGS_VRAM ) { +if (flags &KFD_IOC_ALLOC_MEM_FLAGS_AQL_QUEUE_MEM ) + args->size >>=1 ;
This will return the updated size to user mode. That's probably not what you want. It may be harmless, but technically it breaks the ABI. It would be better to use a local variable for the updated size.
Regards,
Felix
Quoted 5 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +2
-0+WRITE_ONCE (pdd->vram_usage, pdd->vram_usage +PAGE_ALIGN (args->size)); + }mutex_unlock (&p->mutex);
Re: [PATCH] drm/amdkfd: Page aligned memory reserve size
Quoted 41 lines · Felix Kuehling
Am 2023-01-10 um 12:11 schrieb Philip Yang:
Quoted 40 lines
Use page aligned size to reserve memory usage because page aligned TTM BO size is used to unreserve memory usage, otherwise no page aligned size causes memory usage accounting unbalanced.
Change
vram_useddefinition type toint64_tto be able to triggerWARN_ONCE(adev && adev->kfd.vram_used < 0, "..."), to help debug the accouting issue with warning and backtrace.Signed-off-by Philip Yang <[email protected]>---
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h | 2 +-drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c| 9 ++++++--- drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 7 +++++--3 files changed, 12 insertions(+), 6 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +1
-1@@ -97,7 +97,7 @@ struct amdgpu_amdkfd_fence { struct amdgpu_kfd_dev { struct kfd_dev *dev; - uint64_t vram_used; + int64_t vram_used; uint64_t vram_used_aligned; bool init_complete; struct work_struct reset_work;drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +1
-0@@ -1598,6 +1598,7 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu( struct amdgpu_bo *bo; struct drm_gem_object *gobj = NULL; u32 domain, alloc_domain; + uint64_t aligned_size; u64 alloc_flags; int ret;@@ -1653,13 +1654,15 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(
* the memory.
Quoted 8 lines
Quoted 6 lines
*/
if ((*mem)->aql_queue)
- size = size >> 1;
+ size >>= 1;
+
+aligned_size=PAGE_ALIGN(size);Why do you need a new variable for this? Can't you just update size to be page-aligned here? Is the unaligned size still needed anywhere?
amdgpu_gem_object_create ->...-> amdgpu_bo_create needs the original size for domain GWS etc, as the size is used as number of pages, not bytes.
Quoted 25 lines
Quoted 25 lines
(*mem)->alloc_flags = flags; amdgpu_sync_create(&(*mem)->sync);
- ret =amdgpu_amdkfd_reserve_mem_limit(adev, size, flags); + ret =amdgpu_amdkfd_reserve_mem_limit(adev, aligned_size, flags); if (ret) {
pr_debug("Insufficient memory\n");
gotoerr_reserve_limit;drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +0
-0@@ -1725,7 +1728,7 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu( /* Don't unreserve system mem limit twice */ goto err_reserve_limit;
err_bo_create:
-amdgpu_amdkfd_unreserve_mem_limit(adev, size, flags); +amdgpu_amdkfd_unreserve_mem_limit(adev, aligned_size, flags);err_reserve_limit:
mutex_destroy(&(*mem)->lock);
if (gobj)drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +0
-0@@ -1127,8 +1127,11 @@ static int kfd_ioctl_alloc_memory_of_gpu(struct file *filep, }
Quoted 8 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c static int kfd_ioctl_alloc_memory_of_gpu(struct file *filep, · line 1132
Quoted 6 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +3
-2/* Update the VRAM usage count */ - if (flags & KFD_IOC_ALLOC_MEM_FLAGS_VRAM) - WRITE_ONCE(pdd->vram_usage, pdd->vram_usage + args->size); + if (flags & KFD_IOC_ALLOC_MEM_FLAGS_VRAM) { + if (flags & KFD_IOC_ALLOC_MEM_FLAGS_AQL_QUEUE_MEM) + args->size >>= 1;This will return the updated size to user mode. That's probably not what you want. It may be harmless, but technically it breaks the ABI. It would be better to use a local variable for the updated size.
Submit v3 patch to fix this.
Thanks,
Philip
Quoted 9 lines
Regards,
FelixQuoted 4 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +2
-0+ WRITE_ONCE(pdd->vram_usage, pdd->vram_usage +PAGE_ALIGN (args->size)); + } mutex_unlock(&p->mutex);
Felix Kuehling
maintainerRe: [PATCH] drm/amdkfd: Page aligned memory reserve size
Am 2023-01-10 um 15:44 schrieb Philip Yang:
Quoted 51 lines · Felix Kuehling
Quoted 41 lines
Am 2023-01-10 um 12:11 schrieb Philip Yang:
Quoted 40 lines
Use page aligned size to reserve memory usage because page aligned TTM BO size is used to unreserve memory usage, otherwise no page aligned size causes memory usage accounting unbalanced.
Change
vram_useddefinition type toint64_tto be able to triggerWARN_ONCE(adev && adev->kfd.vram_used < 0, "..."), to help debug the accouting issue with warning and backtrace.Signed-off-by Philip Yang <[email protected]>---
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h | 2 +-drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c| 9 ++++++--- drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 7 +++++--3 files changed, 12 insertions(+), 6 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +1
-1@@ -97,7 +97,7 @@ struct amdgpu_amdkfd_fence { struct amdgpu_kfd_dev { struct kfd_dev *dev; - uint64_t vram_used; + int64_t vram_used; uint64_t vram_used_aligned; bool init_complete; struct work_struct reset_work;drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +1
-0@@ -1598,6 +1598,7 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu( struct amdgpu_bo *bo; struct drm_gem_object *gobj = NULL; u32 domain, alloc_domain; + uint64_t aligned_size; u64 alloc_flags; int ret;@@ -1653,13 +1654,15 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(
* the memory.Quoted 8 lines
Quoted 6 lines
*/
if ((*mem)->aql_queue)
- size = size >> 1;
+ size >>= 1;
+
+aligned_size=PAGE_ALIGN(size);Why do you need a new variable for this? Can't you just update size to be page-aligned here? Is the unaligned size still needed anywhere?
Quoted 1 line
amdgpu_gem_object_create->...->amdgpu_bo_createneeds the original size for domain GWS etc, as the size is used as number of pages, not bytes.
I don't think GWS is ever allocated through this code path. This type of memory is not exposed in the KFD ioctl API. KFD allocates a GWS BO using amdgpu_amdkfd_alloc_gws.
Regards,
Felix
Quoted 49 lines
Quoted 25 lines
Quoted 25 lines
(*mem)->alloc_flags = flags; amdgpu_sync_create(&(*mem)->sync);
- ret =amdgpu_amdkfd_reserve_mem_limit(adev, size, flags); + ret =amdgpu_amdkfd_reserve_mem_limit(adev, aligned_size, flags); if (ret) {
pr_debug("Insufficient memory\n");
gotoerr_reserve_limit;drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +0
-0@@ -1725,7 +1728,7 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu( /* Don't unreserve system mem limit twice */ goto err_reserve_limit;
err_bo_create:
-amdgpu_amdkfd_unreserve_mem_limit(adev, size, flags); +amdgpu_amdkfd_unreserve_mem_limit(adev, aligned_size, flags);err_reserve_limit:
mutex_destroy(&(*mem)->lock);
if (gobj)drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +0
-0@@ -1127,8 +1127,11 @@ static int kfd_ioctl_alloc_memory_of_gpu(struct file *filep, }Quoted 8 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c static int kfd_ioctl_alloc_memory_of_gpu(struct file *filep, · line 1132
Quoted 6 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +3
-2/* Update the VRAM usage count */ - if (flags & KFD_IOC_ALLOC_MEM_FLAGS_VRAM) - WRITE_ONCE(pdd->vram_usage, pdd->vram_usage + args->size); + if (flags & KFD_IOC_ALLOC_MEM_FLAGS_VRAM) { + if (flags & KFD_IOC_ALLOC_MEM_FLAGS_AQL_QUEUE_MEM) + args->size >>= 1;This will return the updated size to user mode. That's probably not what you want. It may be harmless, but technically it breaks the ABI. It would be better to use a local variable for the updated size.
Submit v3 patch to fix this.
Thanks,
Philip
Quoted 9 lines
Regards,
FelixQuoted 4 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +2
-0+ WRITE_ONCE(pdd->vram_usage, pdd->vram_usage +PAGE_ALIGN (args->size)); + } mutex_unlock(&p->mutex);
Re: [PATCH] drm/amdkfd: Page aligned memory reserve size
Quoted 52 lines · Felix Kuehling
Am 2023-01-10 um 15:44 schrieb Philip Yang:
Quoted 51 lines · Felix Kuehling
Quoted 49 lines
Am 2023-01-10 um 12:11 schrieb Philip Yang:
Quoted 38 lines
Use page aligned size to reserve memory usage because page aligned TTM BO size is used to unreserve memory usage, otherwise no page aligned size causes memory usage accounting unbalanced.
Change
vram_useddefinition type toint64_tto be able to triggerWARN_ONCE(adev && adev->kfd.vram_used < 0, "..."), to help debug the accouting issue with warning and backtrace.Signed-off-by Philip Yang <[email protected]>---
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h | 2 +-drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c| 9 ++++++--- drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 7 +++++--3 files changed, 12 insertions(+), 6 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +1
-1@@ -97,7 +97,7 @@ struct amdgpu_amdkfd_fence { struct amdgpu_kfd_dev { struct kfd_dev *dev; - uint64_t vram_used; + int64_t vram_used; uint64_t vram_used_aligned; bool init_complete; struct work_struct reset_work;drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +1
-0@@ -1598,6 +1598,7 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu( struct amdgpu_bo *bo; struct drm_gem_object *gobj = NULL; u32 domain, alloc_domain; + uint64_t aligned_size; u64 alloc_flags; int ret;Quoted 8 lines
@@ -1653,13 +1654,15 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(
* the memory.
*/
if ((*mem)->aql_queue)
- size = size >> 1;
+ size >>= 1;
+
+aligned_size=PAGE_ALIGN(size);Why do you need a new variable for this? Can't you just update size to be page-aligned here? Is the unaligned size still needed anywhere?
Quoted 3 lines
Quoted 1 line
amdgpu_gem_object_create->...->amdgpu_bo_createneeds the original size for domain GWS etc, as the size is used as number of pages, not bytes.I don't think GWS is ever allocated through this code path. This type of memory is not exposed in the KFD ioctl API. KFD allocates a GWS BO using
amdgpu_amdkfd_alloc_gws.
yes, as size is used in pr_debug, it is better to show the original size in debug log to match application, also notice the pr_debug size should base on aql_queue flag, will send v4 patch with the fix.
Regards,
Philip
Quoted 54 lines
Regards,
FelixQuoted 49 lines
Quoted 25 lines
Quoted 25 lines
(*mem)->alloc_flags = flags; amdgpu_sync_create(&(*mem)->sync);
- ret =amdgpu_amdkfd_reserve_mem_limit(adev, size, flags); + ret =amdgpu_amdkfd_reserve_mem_limit(adev, aligned_size, flags); if (ret) {
pr_debug("Insufficient memory\n");
gotoerr_reserve_limit;drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +0
-0@@ -1725,7 +1728,7 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu( /* Don't unreserve system mem limit twice */ goto err_reserve_limit;
err_bo_create:
-amdgpu_amdkfd_unreserve_mem_limit(adev, size, flags); +amdgpu_amdkfd_unreserve_mem_limit(adev, aligned_size, flags);err_reserve_limit:
mutex_destroy(&(*mem)->lock);
if (gobj)drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +0
-0@@ -1127,8 +1127,11 @@ static int kfd_ioctl_alloc_memory_of_gpu(struct file *filep, }Quoted 8 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c static int kfd_ioctl_alloc_memory_of_gpu(struct file *filep, · line 1132
Quoted 6 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +3
-2/* Update the VRAM usage count */ - if (flags & KFD_IOC_ALLOC_MEM_FLAGS_VRAM) - WRITE_ONCE(pdd->vram_usage, pdd->vram_usage + args->size); + if (flags & KFD_IOC_ALLOC_MEM_FLAGS_VRAM) { + if (flags & KFD_IOC_ALLOC_MEM_FLAGS_AQL_QUEUE_MEM) + args->size >>= 1;This will return the updated size to user mode. That's probably not what you want. It may be harmless, but technically it breaks the ABI. It would be better to use a local variable for the updated size.
Submit v3 patch to fix this.
Thanks,
Philip
Quoted 9 lines
Regards,
FelixQuoted 4 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +2
-0+WRITE_ONCE (pdd->vram_usage, pdd->vram_usage +PAGE_ALIGN (args->size)); + } mutex_unlock(&p->mutex);
PATCH v3 1/1 drm/amdkfd: Page aligned memory reserve size
In reply to Philip YangUse page aligned size to reserve memory usage because page aligned TTM BO size is used to unreserve memory usage, otherwise no page aligned size causes memory usage accounting unbalanced.
Change vram_used definition type to int64_t to be able to trigger WARN_ONCE(adev && adev->kfd.vram_used < 0, "..."), to help debug the accounting issue with warning and backtrace.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h | 2 +- drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 9 ++++++--- drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 9 +++++++-- 3 files changed, 14 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +1 -1
@@ -97,7 +97,7 @@ struct amdgpu_amdkfd_fence {struct amdgpu_kfd_dev {struct kfd_dev *dev; -uint64_t vram_used; +int64_t vram_used;uint64_t vram_used_aligned;bool init_complete;struct work_struct reset_work;
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +6 -3
@@ -1598,6 +1598,7 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(struct amdgpu_bo *bo;struct drm_gem_object *gobj =NULL ;u32 domain, alloc_domain; +uint64_t aligned_size;u64 alloc_flags;int ret;
@@ -1653,13 +1654,15 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(* the memory. */ if ((*mem)->aql_queue) - size = size >>1 ; + size >>=1 ; + + aligned_size =PAGE_ALIGN (size); (*mem)->alloc_flags = flags;amdgpu_sync_create (&(*mem)->sync); - ret =amdgpu_amdkfd_reserve_mem_limit (adev, size, flags); + ret =amdgpu_amdkfd_reserve_mem_limit (adev, aligned_size, flags);if (ret) {pr_debug ("Insufficient memory\n" ); goto err_reserve_limit;
@@ -1725,7 +1728,7 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(/* Don't unreserve system mem limit twice */ goto err_reserve_limit; err_bo_create: -amdgpu_amdkfd_unreserve_mem_limit (adev, size, flags); +amdgpu_amdkfd_unreserve_mem_limit (adev, aligned_size, flags); err_reserve_limit:mutex_destroy (&(*mem)->lock);if (gobj)
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +7 -2
@@ -1127,8 +1127,13 @@ static int kfd_ioctl_alloc_memory_of_gpu(struct file *filep, }/* Update the VRAM usage count */ -if (flags &KFD_IOC_ALLOC_MEM_FLAGS_VRAM ) -WRITE_ONCE (pdd->vram_usage, pdd->vram_usage + args->size); +if (flags &KFD_IOC_ALLOC_MEM_FLAGS_VRAM ) { +uint64_t size = args->size; + +if (flags &KFD_IOC_ALLOC_MEM_FLAGS_AQL_QUEUE_MEM ) + size >>=1 ; +WRITE_ONCE (pdd->vram_usage, pdd->vram_usage +PAGE_ALIGN (size)); + }mutex_unlock (&p->mutex);
Signature · 1 line
2.35.1
PATCH v4 1/1 drm/amdkfd: Page aligned memory reserve size
In reply to Philip YangUse page aligned size to reserve memory usage because page aligned TTM BO size is used to unreserve memory usage, otherwise no page aligned size causes memory usage accounting unbalanced.
Change vram_used definition type to int64_t to be able to trigger WARN_ONCE(adev && adev->kfd.vram_used < 0, "..."), to help debug the accounting issue with warning and backtrace.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h | 2 +- drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 12 +++++++----- drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 9 +++++++-- 3 files changed, 15 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +1 -1
@@ -97,7 +97,7 @@ struct amdgpu_amdkfd_fence {struct amdgpu_kfd_dev {struct kfd_dev *dev; -uint64_t vram_used; +int64_t vram_used;uint64_t vram_used_aligned;bool init_complete;struct work_struct reset_work;
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +7 -5
@@ -1598,6 +1598,7 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(struct amdgpu_bo *bo;struct drm_gem_object *gobj =NULL ;u32 domain, alloc_domain; +uint64_t aligned_size;u64 alloc_flags;int ret;
@@ -1653,22 +1654,23 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(* the memory. */ if ((*mem)->aql_queue) - size = size >>1 ; + size >>=1 ; + aligned_size =PAGE_ALIGN (size); (*mem)->alloc_flags = flags;amdgpu_sync_create (&(*mem)->sync); - ret =amdgpu_amdkfd_reserve_mem_limit (adev, size, flags); + ret =amdgpu_amdkfd_reserve_mem_limit (adev, aligned_size, flags);if (ret) {pr_debug ("Insufficient memory\n" ); goto err_reserve_limit; }pr_debug ("\tcreate BO VA 0x%llx size 0x%llx domain %s\n" , - va, size,domain_string (alloc_domain)); + va, (*mem)->aql_queue ? size <<1 : size,domain_string (alloc_domain)); - ret =amdgpu_gem_object_create (adev, size,1 , alloc_domain, alloc_flags, + ret =amdgpu_gem_object_create (adev, aligned_size,1 , alloc_domain, alloc_flags, bo_type,NULL , &gobj);if (ret) { pr_debug("Failed to create BO on domain %s. ret %d\n",
@@ -1725,7 +1727,7 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(/* Don't unreserve system mem limit twice */ goto err_reserve_limit; err_bo_create: -amdgpu_amdkfd_unreserve_mem_limit (adev, size, flags); +amdgpu_amdkfd_unreserve_mem_limit (adev, aligned_size, flags); err_reserve_limit:mutex_destroy (&(*mem)->lock);if (gobj)
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +7 -2
@@ -1127,8 +1127,13 @@ static int kfd_ioctl_alloc_memory_of_gpu(struct file *filep, }/* Update the VRAM usage count */ -if (flags &KFD_IOC_ALLOC_MEM_FLAGS_VRAM ) -WRITE_ONCE (pdd->vram_usage, pdd->vram_usage + args->size); +if (flags &KFD_IOC_ALLOC_MEM_FLAGS_VRAM ) { +uint64_t size = args->size; + +if (flags &KFD_IOC_ALLOC_MEM_FLAGS_AQL_QUEUE_MEM ) + size >>=1 ; +WRITE_ONCE (pdd->vram_usage, pdd->vram_usage +PAGE_ALIGN (size)); + }mutex_unlock (&p->mutex);
Signature · 1 line
2.35.1
Re: [PATCH v4] drm/amdkfd: Page aligned memory reserve size
Am 2023-01-10 um 17:41 schrieb Philip Yang:
Quoted 9 lines
Use page aligned size to reserve memory usage because page aligned TTM BO size is used to unreserve memory usage, otherwise no page aligned size causes memory usage accounting unbalanced.
Change
vram_useddefinition type toint64_tto be able to triggerWARN_ONCE(adev && adev->kfd.vram_used < 0, "..."), to help debug the accounting issue with warning and backtrace.Signed-off-by Philip Yang <[email protected]>
Quoted 88 lines
--- drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h | 2 +- drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 12 +++++++----- drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 9 +++++++-- 3 files changed, 15 insertions(+), 8 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +1
-1@@ -97,7 +97,7 @@ struct amdgpu_amdkfd_fence {struct amdgpu_kfd_dev {struct kfd_dev *dev; -uint64_t vram_used; +int64_t vram_used;uint64_t vram_used_aligned;bool init_complete;struct work_struct reset_work;drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +7
-5@@ -1598,6 +1598,7 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(struct amdgpu_bo *bo;struct drm_gem_object *gobj =NULL ;u32 domain, alloc_domain; +uint64_t aligned_size;u64 alloc_flags;int ret;@@ -1653,22 +1654,23 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(* the memory. */ if ((*mem)->aql_queue) - size = size >>1 ; + size >>=1 ; + aligned_size =PAGE_ALIGN (size); (*mem)->alloc_flags = flags;amdgpu_sync_create (&(*mem)->sync); - ret =amdgpu_amdkfd_reserve_mem_limit (adev, size, flags); + ret =amdgpu_amdkfd_reserve_mem_limit (adev, aligned_size, flags);if (ret) {pr_debug ("Insufficient memory\n" ); goto err_reserve_limit; }pr_debug ("\tcreate BO VA 0x%llx size 0x%llx domain %s\n" , - va, size,domain_string (alloc_domain)); + va, (*mem)->aql_queue ? size <<1 : size,domain_string (alloc_domain)); - ret =amdgpu_gem_object_create (adev, size,1 , alloc_domain, alloc_flags, + ret =amdgpu_gem_object_create (adev, aligned_size,1 , alloc_domain, alloc_flags, bo_type,NULL , &gobj);if (ret) { pr_debug("Failed to create BO on domain %s. ret %d\n",@@ -1725,7 +1727,7 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(/* Don't unreserve system mem limit twice */ goto err_reserve_limit; err_bo_create: -amdgpu_amdkfd_unreserve_mem_limit (adev, size, flags); +amdgpu_amdkfd_unreserve_mem_limit (adev, aligned_size, flags); err_reserve_limit:mutex_destroy (&(*mem)->lock);if (gobj)drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +7
-2@@ -1127,8 +1127,13 @@ static int kfd_ioctl_alloc_memory_of_gpu(struct file *filep, }/* Update the VRAM usage count */ -if (flags &KFD_IOC_ALLOC_MEM_FLAGS_VRAM ) -WRITE_ONCE (pdd->vram_usage, pdd->vram_usage + args->size); +if (flags &KFD_IOC_ALLOC_MEM_FLAGS_VRAM ) { +uint64_t size = args->size; + +if (flags &KFD_IOC_ALLOC_MEM_FLAGS_AQL_QUEUE_MEM ) + size >>=1 ; +WRITE_ONCE (pdd->vram_usage, pdd->vram_usage +PAGE_ALIGN (size)); + }mutex_unlock (&p->mutex);