drm/amdkfd: correct vram_available calculation
Subsystems (3)
PATCH v3 1/1 drm/amdkfd: correct vram_available calculation
In reply to a message not in the archiveAMD General
A new VRAM allocation needs to meet:
adev->kfd.vram_used + vram_needed <= vram_available
kfd.vram_used already includes BOs that are later pinned.
Pinning does not consume extra VRAM. Subtracting vram_pin_size without adding back kfd.vram_pinned counts those bytes twice and causes fake-OOM while VRAM is still free.
Add kfd.vram_pinned so reserve_mem_limit() matches get_available_memory().
kfd.vram_pinned is only updated in gpuvm_pin_bo(). dma-buf pin and vmap call amdgpu_bo_pin() only, so the counter stays zero and those BOs are still charged twice. Update the counter on first pin and last unpin in dma-buf pin/vmap and gpuvm_pin_bo.
---
v3:
- update kfd.vram_pinned from dma-buf pin/vmap and gpuvm_pin_bo on first pin / last unpin in amdgpu_dma_buf.c
v2:
- keep vram_pin_size; add kfd.vram_pinned in reserve_mem_limit (Mukul Joshi)
- leave get_available_memory unchanged
- drop former 2/2 pin/unpin changes in amdgpu_object.c (Christian König)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h | 2 + .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 38 +++++++++++++++---- drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c | 22 ++++++++++- 3 files changed, 53 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +2 -0
@@ -378,6 +378,8 @@ void amdgpu_amdkfd_gpuvm_put_bo_ref(struct amdgpu_bo *bo);* Return: ZERO if successful in pinning, Non-Zero in case of error. */ int amdgpu_amdkfd_gpuvm_pin_bo (struct amdgpu_bo *bo,u32 domain); +void amdgpu_amdkfd_account_vram_pin (struct amdgpu_bo *bo);void +amdgpu_amdkfd_account_vram_unpin (struct amdgpu_bo *bo);/** * amdgpu_amdkfd_gpuvm_unpin_bo() - Unpins BO using following criteria diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
index 89958d0f7e59..191985dbc569 100644
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +22 -1
@@ -237,7 +237,8 @@ int amdgpu_amdkfd_reserve_mem_limit(struct amdgpu_device *adev,if (adev && xcp_id >=0 && (!adev->apu_prefer_gtt || adev->gmc.is_app_apu)) {uint64_t vram_available = vram_size - reserved_for_pt - reserved_for_ras - -atomic64_read (&adev->vram_pin_size); +atomic64_read (&adev->vram_pin_size) + +atomic64_read (&adev->kfd.vram_pinned);if (adev->kfd.vram_used[xcp_id] + vram_needed > vram_available) { ret = -ENOMEM ; goto release;
@@ -1584,6 +1585,28 @@ static int init_kfd_vm(struct amdgpu_vm *vm, void **process_info,return ret; } +void amdgpu_amdkfd_account_vram_pin (struct amdgpu_bo *bo) { +if (!bo->kfd_bo || !bo->tbo.resource) +return ; +if (bo->tbo.resource->mem_type !=TTM_PL_VRAM ) +return ; + +atomic64_add (amdgpu_bo_size (bo), + &amdgpu_ttm_adev (bo->tbo.bdev)->kfd.vram_pinned); +} + +void amdgpu_amdkfd_account_vram_unpin (struct amdgpu_bo *bo) { +if (!bo->kfd_bo || !bo->tbo.resource) +return ; +if (bo->tbo.resource->mem_type !=TTM_PL_VRAM ) +return ; + +atomic64_sub (amdgpu_bo_size (bo), + &amdgpu_ttm_adev (bo->tbo.bdev)->kfd.vram_pinned); +} +/** * amdgpu_amdkfd_gpuvm_pin_bo() - Pins a BO using following criteria * @bo: Handle of buffer object being pinned @@ -1598,11 +1621,14 @@ static int init_kfd_vm(struct amdgpu_vm *vm, void **process_info, int amdgpu_amdkfd_gpuvm_pin_bo(struct amdgpu_bo *bo, u32 domain) {
int ret = 0;
+ bool first_pin;
ret = amdgpu_bo_reserve(bo, false);
if (unlikely(ret))
return ret;+ first_pin = !bo->tbo.pin_count;
+
if (bo->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS) {
/*
* If bo is not contiguous on VRAM, move to system memory first to ensure @@ -1626,9 +1652,8 @@ int amdgpu_amdkfd_gpuvm_pin_bo(struct amdgpu_bo *bo, u32 domain)
amdgpu_bo_sync_wait(bo, AMDGPU_FENCE_OWNER_KFD, false);- if (!ret && bo->tbo.resource->mem_type == TTM_PL_VRAM)
- atomic64_add(amdgpu_bo_size(bo),
- &amdgpu_ttm_adev(bo->tbo.bdev)->kfd.vram_pinned);
+ if (!ret && first_pin)
+ amdgpu_amdkfd_account_vram_pin(bo);
out:
amdgpu_bo_unreserve(bo);
@@ -1653,9 +1678,8 @@ void amdgpu_amdkfd_gpuvm_unpin_bo(struct amdgpu_bo *bo)
amdgpu_bo_unpin(bo);
- if (bo->tbo.resource->mem_type == TTM_PL_VRAM)
- atomic64_sub(amdgpu_bo_size(bo),
- &amdgpu_ttm_adev(bo->tbo.bdev)->kfd.vram_pinned);
+ if (!bo->tbo.pin_count)
+ amdgpu_amdkfd_account_vram_unpin(bo);
amdgpu_bo_unreserve(bo);
}
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +20 -2
@@ -303,6 +303,8 @@ static int amdgpu_dma_buf_pin(struct dma_buf_attachment *attach)struct dma_buf *dmabuf = attach->dmabuf;struct amdgpu_bo *bo =gem_to_amdgpu_bo (dmabuf->priv);u32 domains = bo->allowed_domains; +bool first_pin; +int r;dma_resv_assert_held (dmabuf->resv);
@@ -328,7 +330,12 @@ static int amdgpu_dma_buf_pin(struct dma_buf_attachment *attach)if (WARN_ON (!domains))return -EINVAL ; -return amdgpu_bo_pin (bo, domains); + first_pin = !bo->tbo.pin_count; + r =amdgpu_bo_pin (bo, domains); +if (!r && first_pin) +amdgpu_amdkfd_account_vram_pin (bo); + +return r; }/**
@@ -344,6 +351,8 @@ static void amdgpu_dma_buf_unpin(struct dma_buf_attachment *attach)struct amdgpu_bo *bo =gem_to_amdgpu_bo (obj);amdgpu_bo_unpin (bo); +if (!bo->tbo.pin_count) + amdgpu_amdkfd_account_vram_unpin(bo); }#endif
@@ -516,6 +525,7 @@ static int amdgpu_dma_buf_vmap(struct dma_buf *dma_buf, struct iosys_map *map) {struct drm_gem_object *obj = dma_buf->priv;struct amdgpu_bo *bo =gem_to_amdgpu_bo (obj); +bool first_pin;int ret;/*
@@ -523,12 +533,18 @@ static int amdgpu_dma_buf_vmap(struct dma_buf *dma_buf, struct iosys_map *map)* domain is not that important as long as it's mapable. Using * GTT and VRAM should be compatible with most use cases. */ + first_pin = !bo->tbo.pin_count; ret =amdgpu_bo_pin (bo,AMDGPU_GEM_DOMAIN_GTT |AMDGPU_GEM_DOMAIN_VRAM );if (ret)return ret; +if (first_pin) +amdgpu_amdkfd_account_vram_pin (bo); ret =drm_gem_dmabuf_vmap (dma_buf, map); -if (ret) +if (ret) {amdgpu_bo_unpin (bo); +if (!bo->tbo.pin_count) +amdgpu_amdkfd_account_vram_unpin (bo); + }return ret; }
@@ -540,6 +556,8 @@ static void amdgpu_dma_buf_vunmap(struct dma_buf *dma_buf, struct iosys_map *mapdrm_gem_dmabuf_vunmap (dma_buf, map);amdgpu_bo_unpin (bo); +if (!bo->tbo.pin_count) + amdgpu_amdkfd_account_vram_unpin(bo); }#endif
--
2.43.7
Quoted 130 lines · Zhen
AMD General
A new VRAM allocation needs to meet:
adev->kfd.vram_used + vram_needed <= vram_availablekfd.vram_used already includes BOs that are later pinned.
Pinning does not consume extra VRAM. Subtractingvram_pin_sizewithout adding back kfd.vram_pinned counts those bytes twice and causes fake-OOM while VRAM is still free.Add kfd.vram_pinned so
reserve_mem_limit()matchesget_available_memory().kfd.vram_pinned is only updated in
gpuvm_pin_bo(). dma-buf pin and vmap callamdgpu_bo_pin()only, so the counter stays zero and those BOs are still charged twice. Update the counter on first pin and last unpin in dma-buf pin/vmap andgpuvm_pin_bo.Signed-off-by Zhen Han <[email protected]>---
v3:
- update kfd.vram_pinned from dma-buf pin/vmap andgpuvm_pin_boon first pin / last unpin inamdgpu_dma_buf.c
v2:
- keepvram_pin_size; add kfd.vram_pinned inreserve_mem_limit(Mukul Joshi)
- leaveget_available_memoryunchanged
- drop former 2/2 pin/unpin changes inamdgpu_object.c(Christian König)drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h | 2 + .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 38 +++++++++++++++---- drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c | 22 ++++++++++- 3 files changed, 53 insertions(+), 9 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +2
-0@@ -378,6 +378,8 @@ void amdgpu_amdkfd_gpuvm_put_bo_ref(struct amdgpu_bo *bo);* Return: ZERO if successful in pinning, Non-Zero in case of error. */ int amdgpu_amdkfd_gpuvm_pin_bo (struct amdgpu_bo *bo,u32 domain); +void amdgpu_amdkfd_account_vram_pin (struct amdgpu_bo *bo);void +amdgpu_amdkfd_account_vram_unpin (struct amdgpu_bo *bo);/** * amdgpu_amdkfd_gpuvm_unpin_bo() - Unpins BO using following criteria diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c index
89958d0f7e59..191985dbc569100644drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +29
-7@@ -237,7 +237,8 @@ int amdgpu_amdkfd_reserve_mem_limit(struct amdgpu_device *adev,if (adev && xcp_id >=0 && (!adev->apu_prefer_gtt || adev->gmc.is_app_apu)) {uint64_t vram_available = vram_size - reserved_for_pt - reserved_for_ras - -atomic64_read (&adev->vram_pin_size); +atomic64_read (&adev->vram_pin_size) + +atomic64_read (&adev->kfd.vram_pinned);if (adev->kfd.vram_used[xcp_id] + vram_needed > vram_available) { ret = -ENOMEM ; goto release;@@ -1584,6 +1585,28 @@ static int init_kfd_vm(struct amdgpu_vm *vm, void **process_info,return ret; } +void amdgpu_amdkfd_account_vram_pin (struct amdgpu_bo *bo) { +if (!bo->kfd_bo || !bo->tbo.resource) +return ; +if (bo->tbo.resource->mem_type !=TTM_PL_VRAM ) +return ; + +atomic64_add (amdgpu_bo_size (bo), + &amdgpu_ttm_adev (bo->tbo.bdev)->kfd.vram_pinned); +} + +void amdgpu_amdkfd_account_vram_unpin (struct amdgpu_bo *bo) { +if (!bo->kfd_bo || !bo->tbo.resource) +return ; +if (bo->tbo.resource->mem_type !=TTM_PL_VRAM ) +return ; + +atomic64_sub (amdgpu_bo_size (bo), + &amdgpu_ttm_adev (bo->tbo.bdev)->kfd.vram_pinned); +} +/** * amdgpu_amdkfd_gpuvm_pin_bo() - Pins a BO using following criteria * @bo: Handle of buffer object being pinned @@ -1598,11 +1621,14 @@ static int init_kfd_vm(struct amdgpu_vm *vm, void **process_info, int amdgpu_amdkfd_gpuvm_pin_bo(struct amdgpu_bo *bo, u32 domain) { int ret = 0; +bool first_pin; ret = amdgpu_bo_reserve(bo, false); if (unlikely(ret)) return ret; +first_pin = !bo->tbo.pin_count; +if (bo->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS) { /* * If bo is not contiguous on VRAM, move to system memory first to ensure @@ -1626,9 +1652,8 @@ int amdgpu_amdkfd_gpuvm_pin_bo(struct amdgpu_bo *bo, u32 domain) amdgpu_bo_sync_wait(bo, AMDGPU_FENCE_OWNER_KFD, false); -if (!ret && bo->tbo.resource->mem_type == TTM_PL_VRAM) -atomic64_add(amdgpu_bo_size(bo), -&amdgpu_ttm_adev(bo->tbo.bdev)->kfd.vram_pinned); +if (!ret && first_pin) +amdgpu_amdkfd_account_vram_pin(bo); out: amdgpu_bo_unreserve(bo); @@ -1653,9 +1678,8 @@ void amdgpu_amdkfd_gpuvm_unpin_bo(struct amdgpu_bo *bo)amdgpu_bo_unpin (bo); -if (bo->tbo.resource->mem_type ==TTM_PL_VRAM ) -atomic64_sub (amdgpu_bo_size (bo), - &amdgpu_ttm_adev (bo->tbo.bdev)->kfd.vram_pinned); +if (!bo->tbo.pin_count) +amdgpu_amdkfd_account_vram_unpin (bo);amdgpu_bo_unreserve (bo); }drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +2
-0@@ -303,6 +303,8 @@ static int amdgpu_dma_buf_pin(struct dma_buf_attachment *attach)struct dma_buf *dmabuf = attach->dmabuf;struct amdgpu_bo *bo =gem_to_amdgpu_bo (dmabuf->priv);u32 domains = bo->allowed_domains; +bool first_pin; +int r;dma_resv_assert_held (dmabuf->resv);
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c static int amdgpu_dma_buf_pin(struct dma_buf_attachment *attach) · line 337
Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +5
-1@@ -328,7 +330,12 @@ static int amdgpu_dma_buf_pin(struct dma_buf_attachment *attach)if (WARN_ON (!domains))return -EINVAL ; -return amdgpu_bo_pin (bo, domains); + first_pin = !bo->tbo.pin_count; + r =amdgpu_bo_pin (bo, domains); +if (!r && first_pin) +amdgpu_amdkfd_account_vram_pin (bo); +
This looks incorrect. vram_pin_size already accounts for all the pinned ones. The original intent looks to be to avoid double counting only for KFD allocated BOs.
BTW, I don't see this patch in drm-next.
Thanks,
Lijo
Quoted 51 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +13
-2+return r; }/** @@ -344,6 +351,8 @@ static void amdgpu_dma_buf_unpin(struct dma_buf_attachment *attach)struct amdgpu_bo *bo =gem_to_amdgpu_bo (obj);amdgpu_bo_unpin (bo); +if (!bo->tbo.pin_count) + amdgpu_amdkfd_account_vram_unpin(bo); }#endif @@ -516,6 +525,7 @@ static int amdgpu_dma_buf_vmap(struct dma_buf *dma_buf, struct iosys_map *map) {struct drm_gem_object *obj = dma_buf->priv;struct amdgpu_bo *bo =gem_to_amdgpu_bo (obj); +bool first_pin;int ret;/* @@ -523,12 +533,18 @@ static int amdgpu_dma_buf_vmap(struct dma_buf *dma_buf, struct iosys_map *map)* domain is not that important as long as it's mapable. Using * GTT and VRAM should be compatible with most use cases. */ + first_pin = !bo->tbo.pin_count; ret =amdgpu_bo_pin (bo,AMDGPU_GEM_DOMAIN_GTT |AMDGPU_GEM_DOMAIN_VRAM );if (ret)return ret; +if (first_pin) +amdgpu_amdkfd_account_vram_pin (bo); ret =drm_gem_dmabuf_vmap (dma_buf, map); -if (ret) +if (ret) {amdgpu_bo_unpin (bo); +if (!bo->tbo.pin_count) +amdgpu_amdkfd_account_vram_unpin (bo); + }return ret; }@@ -540,6 +556,8 @@ static void amdgpu_dma_buf_vunmap(struct dma_buf *dma_buf, struct iosys_map *mapdrm_gem_dmabuf_vunmap (dma_buf, map);amdgpu_bo_unpin (bo); +if (!bo->tbo.pin_count) + amdgpu_amdkfd_account_vram_unpin(bo); }#endif --2.43.7
PATCH v4 1/1 drm/amdkfd: correct vram_available calculation
In reply to a message not in the archiveAMD General
reserve_mem_limit() computes available VRAM as
vram_size - reserved - vram_pin_size - kfd.vram_used
However, kfd.vram_used already includes BOs that are later pinned.
Pinning does not consume extra VRAM. Subtracting vram_pin_size counts those bytes twice and causes fake-OOM while VRAM is still free.
New design reuses get_available_memory() for getting available VRAM in reserve_mem_limit(). Move ALIGN_DOWN to the ioctl so the userspace ABI is unchanged.
kfd.vram_pinned is only updated in gpuvm_pin_bo(). dma-buf pin calls amdgpu_bo_pin() only, so the counter stays zero. Update it from amdgpu_bo_pin() through a KFD helper, on first pin and last unpin, matching vram_pin_size.
---
v4:
- use get_available_memory() in reserve_mem_limit()
- move ALIGN_DOWN from get_available_memory() to the ioctl
- drop v3 extra call in amdgpu_dma_buf.c
- update kfd.vram_pinned from amdgpu_bo_pin() via a KFD helper
v3:
- update kfd.vram_pinned from dma-buf pin/vmap and gpuvm_pin_bo on first pin / last unpin in amdgpu_dma_buf.c
v2:
- keep vram_pin_size; add kfd.vram_pinned in reserve_mem_limit (Mukul Joshi)
- leave get_available_memory unchanged
- drop former 2/2 pin/unpin changes in amdgpu_object.c
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h | 18 +++++ .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 81 +++++++++++-------- drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 2 + drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 12 ++- 4 files changed, 76 insertions(+), 37 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +10 -0
@@ -42,6 +42,12 @@extern uint64_t amdgpu_amdkfd_total_mem_size; +/* +* Align VRAM availability to 2MB to avoid fragmentation caused by 4K +* allocations in the tail 2MB BO chunk. +*/ +#define VRAM_AVAILABLITY_ALIGN (1 << 21) +enum TLB_FLUSH_TYPE {TLB_FLUSH_LEGACY =0 ,TLB_FLUSH_LIGHTWEIGHT ,
@@ -456,6 +462,8 @@ void amdgpu_amdkfd_gpuvm_destroy_cb(struct amdgpu_device *adev,* Allows KFD to release its resources associated with the GEM object. */ void amdgpu_amdkfd_release_notify (struct amdgpu_bo *bo); +void amdgpu_amdkfd_account_vram_pin (struct amdgpu_bo *bo);void +amdgpu_amdkfd_account_vram_unpin (struct amdgpu_bo *bo);void amdgpu_amdkfd_reserve_system_mem (uint64_t size);#else static inline @@ -473,6 +481,16 @@ static inline void amdgpu_amdkfd_release_notify(struct amdgpu_bo *bo) { } + +static inline
+void amdgpu_amdkfd_account_vram_pin(struct amdgpu_bo *bo) { } +
+static inline
+void amdgpu_amdkfd_account_vram_unpin(struct amdgpu_bo *bo) { }
#endif
void amdgpu_amdkfd_rlc_spm_cntl(struct amdgpu_device *adev, int xcc_id, bool cntl);
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +46 -35
158 lines not on this page · raw message
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +2 -0
@@ -1004,6 +1004,7 @@ int amdgpu_bo_pin(struct amdgpu_bo *bo, u32 domain)atomic64_add (amdgpu_bo_size (bo), &adev->vram_pin_size);atomic64_add (amdgpu_vram_mgr_bo_visible_size (bo), &adev->visible_pin_size); +amdgpu_amdkfd_account_vram_pin (bo); }else if (bo->tbo.resource->mem_type ==TTM_PL_TT ) {atomic64_add (amdgpu_bo_size (bo), &adev->gart_pin_size); }
@@ -1039,6 +1040,7 @@ void amdgpu_bo_unpin(struct amdgpu_bo *bo)atomic64_sub (amdgpu_bo_size (bo), &adev->vram_pin_size);atomic64_sub (amdgpu_vram_mgr_bo_visible_size (bo), &adev->visible_pin_size); +amdgpu_amdkfd_account_vram_unpin (bo); }else if (bo->tbo.resource->mem_type ==TTM_PL_TT ) {atomic64_sub (amdgpu_bo_size (bo), &adev->gart_pin_size); }
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +10 -2
@@ -1073,11 +1073,19 @@ static int kfd_ioctl_get_available_memory(struct file *filep, {struct kfd_ioctl_get_available_memory_args *args = data;struct kfd_process_device *pdd =kfd_lock_pdd_by_id (p, args->gpu_id); +struct amdgpu_device *adev; +size_t available;if (!pdd)return -EINVAL ; - args->available =amdgpu_amdkfd_get_available_memory (pdd->dev->adev, - pdd->dev->node_id); + + adev = pdd->dev->adev; + available =amdgpu_amdkfd_get_available_memory (adev, pdd->dev->node_id); +if (adev->apu_prefer_gtt) + args->available =ALIGN_DOWN (available,PAGE_SIZE ); +else + args->available =ALIGN_DOWN (available,VRAM_AVAILABLITY_ALIGN ); +kfd_unlock_pdd (pdd);return 0 ; }
--
2.43.7