drm/amdgpu: place VRAM-capable dma-buf imports in the PREEMPT domain
Subsystems (2)
PATCH 1/2 drm/amdgpu: place VRAM-capable dma-buf imports in the PREEMPT domain
An imported dma-buf BO backed by a VRAM-capable exporter holds no host pages of its own, yet when validated into TTM_PL_TT it is charged against the GTT page budget; importing many such buffers can exhaust that budget and fail with -ENOMEM even though no host memory is used. Set AMDGPU_GEM_CREATE_PREEMPTIBLE on these imports so they land in the PREEMPT domain instead, which is GTT-like and still bound (keeping the exporter resident) but carries no host-page limit.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c | 9 +++++++++ 1 file changed, 9 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +9 -0
@@ -472,6 +472,15 @@ amdgpu_dma_buf_create_obj(struct drm_device *dev, struct dma_buf *dma_buf)AMDGPU_GEM_CREATE_COHERENT |AMDGPU_GEM_CREATE_EXT_COHERENT | AMDGPU_GEM_CREATE_UNCACHED); + +/* +* A VRAM-capable import is backed by the exporter's device +* memory, not host pages of its own. Place it in the PREEMPT +* domain (GTT-like, but with no host-page budget) so many such +* imports cannot exhaust the GTT limit. +*/ +if (other->preferred_domains &AMDGPU_GEM_DOMAIN_VRAM ) + flags |=AMDGPU_GEM_CREATE_PREEMPTIBLE ; } ret = amdgpu_gem_object_create(adev, dma_buf->size, PAGE_SIZE,
Signature · 1 line
2.55.0
PATCH 2/2 drm/amdgpu: fix bo_va leak / WARN_ON in gem_object_open error path
amdgpu_gem_object_open() maps the BO into the caller's VM early (amdgpu_vm_bo_add(), which sets abo->vm_bo) and only later validates and attaches an eviction fence for dynamic dma-buf imports in compute VMs via amdgpu_amdkfd_bo_validate_and_fence().
When that validate/fence step fails, the function returned the error without undoing the VM mapping. Since .open() failed, drm_gem_handle_create_tail() aborts the import and drops the last reference, freeing the BO while it still has a bo_va attached. This trips WARN_ON(abo->vm_bo) in amdgpu_bo_release_notify() and leaks the bo_va:
amdgpu 0004:04:00.0: validate_and_fence failed: -12
WARNING: CPU: 96 PID: 2868883 at amd/amdgpu/amdgpu_object.c:1343
amdgpu_bo_release_notify+0x15d/0x180 [amdgpu]
ttm_bo_put+0x149/0x350 [amdttm]
drm_gem_prime_fd_to_handle+0x132/0x1c0 [drm]
drm_ioctl+0x251/0x3d0 [drm]Roll back the VM mapping on the error path: re-reserve the BO and PD, then either delete the freshly added bo_va (also detaching the eviction fence) or drop the extra ref_count taken for a pre-existing bo_va. Also clean up the bo_va in the out_unlock path when the eviction fence attach itself fails right after amdgpu_vm_bo_add(), which had the same leak.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 53 +++++++++++++++++++++++++ 1 file changed, 53 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +53 -0
@@ -237,6 +237,7 @@ static int amdgpu_gem_object_open(struct drm_gem_object *obj,struct amdgpu_bo_va *bo_va;struct mm_struct *mm;struct drm_exec exec; +bool bo_va_added = false;int r; mm =amdgpu_ttm_tt_get_usermm (abo->tbo.ttm);
@@ -264,6 +265,7 @@ static int amdgpu_gem_object_open(struct drm_gem_object *obj, bo_va =amdgpu_vm_bo_find (vm, abo);if (!bo_va) { bo_va =amdgpu_vm_bo_add (adev, vm, abo); + bo_va_added = true; r =amdgpu_evf_mgr_attach_fence (&fpriv->evf_mgr, abo);if (r) goto out_unlock;
@@ -304,9 +306,60 @@ static int amdgpu_gem_object_open(struct drm_gem_object *obj, } }mutex_unlock (&vm->process_info->lock); +if (r) + goto out_vm_bo_rollback; +return 0 ; + +out_vm_bo_rollback: +/* +* The open failed after we already mapped the BO into this VM. Undo +* that mapping, otherwise drm_gem_handle_create_tail() aborts the +* import and frees the BO while it still has a bo_va attached +* (abo->vm_bo != NULL), tripping WARN_ON(abo->vm_bo) in +* amdgpu_bo_release_notify() and leaking the bo_va. +*/ +drm_exec_init (&exec,DRM_EXEC_IGNORE_DUPLICATES ,0 ); + drm_exec_until_all_locked(&exec) { +int r2 =drm_exec_prepare_obj (&exec, &abo->tbo.base,1 ); + +drm_exec_retry_on_contention (&exec); +if (unlikely (r2)) + goto out_rollback_unlock; + + r2 =amdgpu_vm_lock_pd (vm, &exec,0 ); +drm_exec_retry_on_contention (&exec); +if (unlikely (r2)) + goto out_rollback_unlock; + } + + bo_va =amdgpu_vm_bo_find (vm, abo); +if (bo_va) { +if (bo_va_added) { +if (!amdgpu_vm_is_bo_always_valid (vm, abo)) +amdgpu_evf_mgr_detach_fence (&fpriv->evf_mgr, abo); +amdgpu_vm_bo_del (adev, bo_va); +amdgpu_vm_bo_update_shared (abo); + }else { + --bo_va->ref_count; + } + } + +out_rollback_unlock: +drm_exec_fini (&exec);return r; out_unlock: +/* +* If we already added a bo_va (e.g. the eviction fence attach below +* failed), remove it while still holding the reservations so the BO is +* not freed with a dangling abo->vm_bo. +*/ +if (bo_va_added) { + bo_va =amdgpu_vm_bo_find (vm, abo); +if (bo_va) +amdgpu_vm_bo_del (adev, bo_va); +amdgpu_vm_bo_update_shared (abo); + }drm_exec_fini (&exec);return r; }
Signature · 1 line
2.55.0
Re: [PATCH 2/2] drm/amdgpu: fix bo_va leak / WARN_ON in gem_object_open error path
Quoted 18 lines · Mukul Joshi
amdgpu_gem_object_open()maps the BO into the caller's VM early (amdgpu_vm_bo_add(), which setsabo->vm_bo) and only later validates and attaches an eviction fence for dynamic dma-buf imports in compute VMs viaamdgpu_amdkfd_bo_validate_and_fence().When that validate/fence step fails, the function returned the error without undoing the VM mapping. Since .open() failed,
drm_gem_handle_create_tail()aborts the import and drops the last reference, freeing the BO while it still has abo_vaattached. This tripsWARN_ON(abo->vm_bo)inamdgpu_bo_release_notify()and leaks thebo_va:amdgpu 0004:04:00.0: validate_and_fence failed: -12 WARNING: CPU: 96 PID: 2868883 at amd/amdgpu/amdgpu_object.c:1343 amdgpu_bo_release_notify+0x15d/0x180 [amdgpu] ttm_bo_put+0x149/0x350 [amdttm] drm_gem_prime_fd_to_handle+0x132/0x1c0 [drm] drm_ioctl+0x251/0x3d0 [drm]
Quoted 5 lines
Roll back the VM mapping on the error path: re-reserve the BO and PD, then either delete the freshly added
bo_va(also detaching the eviction fence) or drop the extraref_counttaken for a pre-existingbo_va. Also clean up thebo_vain theout_unlockpath when the eviction fence attach itself fails right afteramdgpu_vm_bo_add(), which had the same leak.
That looks like a lot of complexity for a rollback. Would it be easier to move validate_and_fence before mapping the BO? That way, if validate_and_fence fails, there is less to roll back.
Regards,
Felix
Quoted 87 lines
Signed-off-by Mukul Joshi <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 53 +++++++++++++++++++++++++ 1 file changed, 53 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +53
-0@@ -237,6 +237,7 @@ static int amdgpu_gem_object_open(struct drm_gem_object *obj,struct amdgpu_bo_va *bo_va;struct mm_struct *mm;struct drm_exec exec; +bool bo_va_added = false;int r; mm =amdgpu_ttm_tt_get_usermm (abo->tbo.ttm);@@ -264,6 +265,7 @@ static int amdgpu_gem_object_open(struct drm_gem_object *obj, bo_va =amdgpu_vm_bo_find (vm, abo);if (!bo_va) { bo_va =amdgpu_vm_bo_add (adev, vm, abo); + bo_va_added = true; r =amdgpu_evf_mgr_attach_fence (&fpriv->evf_mgr, abo);if (r) goto out_unlock;@@ -304,9 +306,60 @@ static int amdgpu_gem_object_open(struct drm_gem_object *obj, } }mutex_unlock (&vm->process_info->lock); +if (r) + goto out_vm_bo_rollback; +return 0 ; + +out_vm_bo_rollback: +/* +* The open failed after we already mapped the BO into this VM. Undo +* that mapping, otherwise drm_gem_handle_create_tail() aborts the +* import and frees the BO while it still has a bo_va attached +* (abo->vm_bo != NULL), tripping WARN_ON(abo->vm_bo) in +* amdgpu_bo_release_notify() and leaking the bo_va. +*/ +drm_exec_init (&exec,DRM_EXEC_IGNORE_DUPLICATES ,0 ); + drm_exec_until_all_locked(&exec) { +int r2 =drm_exec_prepare_obj (&exec, &abo->tbo.base,1 ); + +drm_exec_retry_on_contention (&exec); +if (unlikely (r2)) + goto out_rollback_unlock; + + r2 =amdgpu_vm_lock_pd (vm, &exec,0 ); +drm_exec_retry_on_contention (&exec); +if (unlikely (r2)) + goto out_rollback_unlock; + } + + bo_va =amdgpu_vm_bo_find (vm, abo); +if (bo_va) { +if (bo_va_added) { +if (!amdgpu_vm_is_bo_always_valid (vm, abo)) +amdgpu_evf_mgr_detach_fence (&fpriv->evf_mgr, abo); +amdgpu_vm_bo_del (adev, bo_va); +amdgpu_vm_bo_update_shared (abo); + }else { + --bo_va->ref_count; + } + } + +out_rollback_unlock: +drm_exec_fini (&exec);return r; out_unlock: +/* +* If we already added a bo_va (e.g. the eviction fence attach below +* failed), remove it while still holding the reservations so the BO is +* not freed with a dangling abo->vm_bo. +*/ +if (bo_va_added) { + bo_va =amdgpu_vm_bo_find (vm, abo); +if (bo_va) +amdgpu_vm_bo_del (adev, bo_va); +amdgpu_vm_bo_update_shared (abo); + }drm_exec_fini (&exec);return r; }
Re: [PATCH 2/2] drm/amdgpu: fix bo_va leak / WARN_ON in gem_object_open error path
Quoted 19 lines · Felix
Quoted 18 lines · Mukul Joshi
amdgpu_gem_object_open()maps the BO into the caller's VM early (amdgpu_vm_bo_add(), which setsabo->vm_bo) and only later validates and attaches an eviction fence for dynamic dma-buf imports in compute VMs viaamdgpu_amdkfd_bo_validate_and_fence().When that validate/fence step fails, the function returned the error without undoing the VM mapping. Since .open() failed,
drm_gem_handle_create_tail()aborts the import and drops the last reference, freeing the BO while it still has abo_vaattached. This tripsWARN_ON(abo->vm_bo)inamdgpu_bo_release_notify()and leaks thebo_va:amdgpu 0004:04:00.0:
validate_and_fencefailed: -12WARNING: CPU: 96 PID: 2868883 at amd/amdgpu/amdgpu_object.c:1343 amdgpu_bo_release_notify+0x15d/0x180 [amdgpu] ttm_bo_put+0x149/0x350 [amdttm] drm_gem_prime_fd_to_handle+0x132/0x1c0 [drm] drm_ioctl+0x251/0x3d0 [drm]
Quoted 8 lines
Quoted 5 lines
Roll back the VM mapping on the error path: re-reserve the BO and PD, then either delete the freshly added
bo_va(also detaching the eviction fence) or drop the extraref_counttaken for a pre-existingbo_va. Also clean up thebo_vain theout_unlockpath when the eviction fence attach itself fails right afteramdgpu_vm_bo_add(), which had the same leak.That looks like a lot of complexity for a rollback. Would it be easier to move
validate_and_fencebefore mapping the BO? That way, ifvalidate_and_fencefails, there is less to roll back.
Sure. That makes sense. I think that approach is being attempted by another engineer.
I will ask him to send the patch out for review.
I will drop this approach for now.
Regards,
Mukul
Quoted 95 lines
Regards,
FelixQuoted 91 lines
Signed-off-by Mukul Joshi <[email protected]>---
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c| 53 +++++++++++++++++++++++++1 file changed, 53 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +2
-0@@ -237,6 +237,7 @@ static int amdgpu_gem_object_open(struct drm_gem_object *obj, struct amdgpu_bo_va *bo_va; struct mm_struct *mm; struct drm_exec exec; + bool bo_va_added = false; int r; mm = amdgpu_ttm_tt_get_usermm(abo->tbo.ttm);@@ -264,6 +265,7 @@ static int amdgpu_gem_object_open(structdrm_gem_object *obj, bo_va = amdgpu_vm_bo_find(vm, abo); if (!bo_va) { bo_va = amdgpu_vm_bo_add(adev, vm, abo); + bo_va_added = true; r = amdgpu_evf_mgr_attach_fence(&fpriv->evf_mgr, abo); if (r)goto
out_unlock;drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +7
-0@@ -304,9 +306,60 @@ static int amdgpu_gem_object_open(structdrm_gem_object *obj, } } mutex_unlock(&vm->process_info->lock); + if (r) + goto out_vm_bo_rollback; + return 0; + +out_vm_bo_rollback: + /* +* The open failed after we already mapped the BO into this VM. Undo
+ * that mapping, otherwisedrm_gem_handle_create_tail()aborts the + * import and frees the BO while it still has abo_vaattached + * (abo->vm_bo!= NULL), trippingWARN_ON(abo->vm_bo)in + *amdgpu_bo_release_notify()and leaking thebo_va.
+ */
+drm_exec_init(&exec, DRM_EXEC_IGNORE_DUPLICATES, 0);
+drm_exec_until_all_locked(&exec){
+ int r2 =drm_exec_prepare_obj(&exec, &abo->tbo.base, 1);drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +25
-0+ + drm_exec_retry_on_contention(&exec); + if (unlikely(r2)) + goto out_rollback_unlock; + + r2 = amdgpu_vm_lock_pd(vm, &exec, 0); + drm_exec_retry_on_contention(&exec); + if (unlikely(r2)) + goto out_rollback_unlock; + } + + bo_va = amdgpu_vm_bo_find(vm, abo); + if (bo_va) { + if (bo_va_added) { + if (!amdgpu_vm_is_bo_always_valid(vm, abo)) +amdgpu_evf_mgr_detach_fence (&fpriv->evf_mgr, abo); + amdgpu_vm_bo_del(adev, bo_va); + amdgpu_vm_bo_update_shared(abo); + } else { + --bo_va->ref_count; + } + } + +out_rollback_unlock: + drm_exec_fini(&exec);return r;
out_unlock:
+ /*
+ * If we already added abo_va(e.g. the eviction fence attach below
+ * failed), remove it while still holding the reservations so the BO is
+ * not freed with a danglingabo->vm_bo.
+ */
+ if (bo_va_added) {
+bo_va=amdgpu_vm_bo_find(vm, abo);
+ if (bo_va)
+amdgpu_vm_bo_del(adev, bo_va);
+amdgpu_vm_bo_update_shared(abo);
+ }
drm_exec_fini(&exec);
return r;
}
Quoted 9 lines · Mukul Joshi
An imported dma-buf BO backed by a VRAM-capable exporter holds no host pages of its own, yet when validated into
TTM_PL_TTit is charged against the GTT page budget; importing many such buffers can exhaust that budget and fail with-ENOMEMeven though no host memory is used. SetAMDGPU_GEM_CREATE_PREEMPTIBLEon these imports so they land in the PREEMPT domain instead, which is GTT-like and still bound (keeping the exporter resident) but carries no host-page limit.Signed-off-by Mukul Joshi <[email protected]>
Quoted 24 lines
--- drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c | 9 +++++++++ 1 file changed, 9 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +9
-0@@ -472,6 +472,15 @@ amdgpu_dma_buf_create_obj(struct drm_device *dev, struct dma_buf *dma_buf)AMDGPU_GEM_CREATE_COHERENT |AMDGPU_GEM_CREATE_EXT_COHERENT | AMDGPU_GEM_CREATE_UNCACHED); + +/* +* A VRAM-capable import is backed by the exporter's device +* memory, not host pages of its own. Place it in the PREEMPT +* domain (GTT-like, but with no host-page budget) so many such +* imports cannot exhaust the GTT limit. +*/ +if (other->preferred_domains &AMDGPU_GEM_DOMAIN_VRAM ) + flags |=AMDGPU_GEM_CREATE_PREEMPTIBLE ; } ret = amdgpu_gem_object_create(adev, dma_buf->size, PAGE_SIZE,
Christian König
maintainerQuoted 20 lines · Mukul Joshi
An imported dma-buf BO backed by a VRAM-capable exporter holds no host pages of its own, yet when validated into
TTM_PL_TTit is charged against the GTT page budget; importing many such buffers can exhaust that budget and fail with-ENOMEMeven though no host memory is used. SetAMDGPU_GEM_CREATE_PREEMPTIBLEon these imports so they land in the PREEMPT domain instead, which is GTT-like and still bound (keeping the exporter resident) but carries no host-page limit.Signed-off-by Mukul Joshi <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c | 9 +++++++++ 1 file changed, 9 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +0
-0@@ -472,6 +472,15 @@ amdgpu_dma_buf_create_obj(struct drm_device *dev, struct dma_buf *dma_buf)AMDGPU_GEM_CREATE_COHERENT | AMDGPU_GEM_CREATE_EXT_COHERENT |
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c amdgpu_dma_buf_create_obj(struct drm_device *dev, struct dma_buf *dma_buf) · line 483
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +9
-0AMDGPU_GEM_CREATE_UNCACHED); + +/* +* A VRAM-capable import is backed by the exporter's device +* memory, not host pages of its own. Place it in the PREEMPT +* domain (GTT-like, but with no host-page budget) so many such +* imports cannot exhaust the GTT limit. +*/ +if (other->preferred_domains &AMDGPU_GEM_DOMAIN_VRAM ) + flags |=AMDGPU_GEM_CREATE_PREEMPTIBLE ;
You also need to check if the attachment results in P2P access, otherwise the DMA-buf will be moved to system memory.
Take a look at the amdgpu_gem_prime_import(), since the BO is not published yet just unconditionally clearing the AMDGPU_GEM_CREATE_PREEMPTIBLE flag there again should do the trick.
Apart from that looks good to me,
Christian.
Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +0
-0} ret = amdgpu_gem_object_create(adev, dma_buf->size, PAGE_SIZE,
Quoted 34 lines · Christian König
Quoted 20 lines · Mukul Joshi
An imported dma-buf BO backed by a VRAM-capable exporter holds no host pages of its own, yet when validated into
TTM_PL_TTit is charged against the GTT page budget; importing many such buffers can exhaust that budget and fail with-ENOMEMeven though no host memory is used. SetAMDGPU_GEM_CREATE_PREEMPTIBLEon these imports so they land in the PREEMPT domain instead, which is GTT-like and still bound (keeping the exporter resident) but carries no host-page limit.Signed-off-by Mukul Joshi<[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c | 9 +++++++++ 1 file changed, 9 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +0
-0@@ -472,6 +472,15 @@ amdgpu_dma_buf_create_obj(struct drm_device *dev, struct dma_buf *dma_buf)AMDGPU_GEM_CREATE_COHERENT | AMDGPU_GEM_CREATE_EXT_COHERENT |drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c amdgpu_dma_buf_create_obj(struct drm_device *dev, struct dma_buf *dma_buf) · line 483
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +9
-0AMDGPU_GEM_CREATE_UNCACHED); + +/* +* A VRAM-capable import is backed by the exporter's device +* memory, not host pages of its own. Place it in the PREEMPT +* domain (GTT-like, but with no host-page budget) so many such +* imports cannot exhaust the GTT limit. +*/ +if (other->preferred_domains &AMDGPU_GEM_DOMAIN_VRAM ) + flags |=AMDGPU_GEM_CREATE_PREEMPTIBLE ;You also need to check if the attachment results in P2P access, otherwise the DMA-buf will be moved to system memory.
Quoted 3 lines
Take a look at the
amdgpu_gem_prime_import(), since the BO is not published yet just unconditionally clearing theAMDGPU_GEM_CREATE_PREEMPTIBLEflag there again should do the trick.Apart from that looks good to me,
Thanks Christian for the review. I incorporated the change and sent out a v2 just now.
Regards,
Mukul
Quoted 5 lines
Christian.
Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +0
-0} ret = amdgpu_gem_object_create(adev, dma_buf->size, PAGE_SIZE,