FAILED: patch "[PATCH] drm/amdkfd: correct vram_available calculation" failed to apply to 6.1-stable tree
Subsystems (4)
Sasha Levin
maintainerThe patch below does not apply to the 6.1-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.1.y
git checkout FETCH_HEAD
git cherry-pick -x 9131cc85d0dedd8ba4cae60dbe5985d081551f30
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '[email protected]' --subject-prefix 'PATCH 6.1.y' 'HEAD^..'
Thanks,
Sasha
------------------ original commit in Linus's tree ------------------
From 9131cc85d0dedd8ba4cae60dbe5985d081551f30 Mon Sep 17 00:00:00 2001
From: Zhen Han <[email protected]>
Date: Sun, 20 Sep 2026 02:31:31 +0000
Subject: [PATCH] drm/amdkfd: correct vram_available calculation
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.
Reuse get_available_memory() in reserve_mem_limit(). Move ALIGN_DOWN to the ioctl so the userspace ABI is unchanged. The VRAM counter is a parameter: the ioctl passes vram_used_aligned[], the gate passes vram_used[] so a 4K BO is not charged 2MB.
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.
Track per-XCP pins in vram_pin_size_xcp[] and kfd.vram_pinned[]. Pins that are not owned by a single partition go to vram_pin_size_common, which every partition subtracts.
A KFD VRAM BO always carries a partition, so the KFD credit is only correct for xcp >= 0. Call the helper for both branches and let it warn once and skip if that ever stops holding: without the credit the BO would be subtracted through vram_pin_size_common and never added back, which is the double counting this patch removes, and kfd.vram_pinned[] would be indexed out of bounds.
--- drivers/gpu/drm/amd/amdgpu/amdgpu.h | 11 ++- drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h | 20 +++- .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 94 +++++++++++-------- drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 33 +++++++ drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 12 ++- 5 files changed, 127 insertions(+), 43 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +10 -1
@@ -844,8 +844,17 @@ struct amdgpu_device {struct mutex mn_lock;DECLARE_HASHTABLE (mn_hash,7 ); -/* tracking pinned memory */ +/* tracking pinned memory +* +* vram_pin_size = sum(vram_pin_size_xcp[]) + vram_pin_size_common +* +* vram_pin_size total pin size in device +* vram_pin_size_xcp[i] pins owned by partition i (bo->xcp_id = i, i >= 0) +* vram_pin_size_common pins not owned by any partition, bo->xcp_id = -1 +*/ atomic64_t vram_pin_size; +atomic64_t vram_pin_size_xcp[MAX_XCP ]; +atomic64_t vram_pin_size_common;atomic64_t visible_pin_size;atomic64_t gart_pin_size;
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +19 -1
@@ -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 ,
@@ -106,7 +112,7 @@ struct amdgpu_kfd_dev {struct kfd_dev *dev;int64_t vram_used[MAX_XCP ];uint64_t vram_used_aligned[MAX_XCP ]; -atomic64_t vram_pinned; +atomic64_t vram_pinned[MAX_XCP ];bool init_complete;struct work_struct reset_work;
@@ -390,6 +396,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,int xcp_id); +void amdgpu_amdkfd_account_vram_unpin (struct amdgpu_bo *bo,int xcp_id);void amdgpu_amdkfd_reserve_system_mem (uint64_t size);#else static inline
@@ -407,6 +415,16 @@ static inlinevoid amdgpu_amdkfd_release_notify (struct amdgpu_bo *bo) { } + +static inline +void amdgpu_amdkfd_account_vram_pin (struct amdgpu_bo *bo,int xcp_id) +{ +} + +static inline +void amdgpu_amdkfd_account_vram_unpin (struct amdgpu_bo *bo,int xcp_id) +{ +}#endif #if IS_ENABLED (CONFIG_HSA_AMD_SVM )
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +55 -39
175 lines not on this page · raw message
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +33 -0
@@ -923,6 +923,19 @@ void amdgpu_bo_unref(struct amdgpu_bo **bo) *bo =NULL ; } +/* Map a BO onto vram_pin_size_xcp[]. bo->xcp_id < 0 means "any partition": +* such pins are not owned by a single partition and go to +* vram_pin_size_common, which every partition subtracts. +*/ +static int amdgpu_bo_get_xcp (struct amdgpu_bo *bo) +{ +s8 xcp = bo->xcp_id; + +if (xcp >=0 && xcp <MAX_XCP ) +return xcp; +return -1 ; +} +/** * amdgpu_bo_pin - pin an &amdgpu_bo buffer object * @bo: &amdgpu_bo buffer object to be pinned
@@ -1003,7 +1016,17 @@ int amdgpu_bo_pin(struct amdgpu_bo *bo, u32 domain)ttm_bo_pin (&bo->tbo);if (bo->tbo.resource->mem_type ==TTM_PL_VRAM ) { +int xcp =amdgpu_bo_get_xcp (bo); +atomic64_add (amdgpu_bo_size (bo), &adev->vram_pin_size); +if (xcp >=0 ) { +atomic64_add (amdgpu_bo_size (bo), + &adev->vram_pin_size_xcp[xcp]); + }else { +atomic64_add (amdgpu_bo_size (bo), + &adev->vram_pin_size_common); + } +amdgpu_amdkfd_account_vram_pin (bo, xcp);atomic64_add (amdgpu_vram_mgr_bo_visible_size (bo), &adev->visible_pin_size); }else if (bo->tbo.resource->mem_type ==TTM_PL_TT ) {
@@ -1036,7 +1059,17 @@ void amdgpu_bo_unpin(struct amdgpu_bo *bo)dma_buf_unpin (bo->tbo.base.import_attach);if (bo->tbo.resource->mem_type ==TTM_PL_VRAM ) { +int xcp =amdgpu_bo_get_xcp (bo); +atomic64_sub (amdgpu_bo_size (bo), &adev->vram_pin_size); +if (xcp >=0 ) { +atomic64_sub (amdgpu_bo_size (bo), + &adev->vram_pin_size_xcp[xcp]); + }else { +atomic64_sub (amdgpu_bo_size (bo), + &adev->vram_pin_size_common); + } +amdgpu_amdkfd_account_vram_unpin (bo, xcp);atomic64_sub (amdgpu_vram_mgr_bo_visible_size (bo), &adev->visible_pin_size); }else if (bo->tbo.resource->mem_type ==TTM_PL_TT ) {
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +10 -2
@@ -1110,11 +1110,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 ; }
Signature · 1 line
2.53.0