Using drm_exec for TTMs BO eviction
Subsystems (5)
Hi guys,
We recently ran into a problem with deadlocks during eviction and while back Thomas worked on a patch set which was going into the direction of solving this.
So I simplified it to just the functionality needed to resolve this issue at. The resulting patch set is just the initial first step of using drm_exec in TTM for locking BOs during eviction.
Should a deadlock happen the drm_exec object is now used to resolve it and prelock the contended object. This approach solves this the ENOMEM issue on contending evictions quite nicely.
Please review and comment,
Christian.
PATCH 1/5 dma-buf/dma-resv: Introduce dma_resv_trylock_ctx()
From: Thomas Hellström <[email protected]>
For the drm_exec_trylock() functionality, there is a need to be able to trylock a dma-resv object as part of a drm_exec transaction. Therefore expose a variant of dma_resv_trylock that also takes a struct ww_acquire_ctx parameter.
--- include/linux/dma-resv.h | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-)
include/linux/dma-resv.h +22 -1
@@ -405,6 +405,27 @@ static inline int dma_resv_lock_slow_interruptible(struct dma_resv *obj,return ww_mutex_lock_slow_interruptible (&obj->lock, ctx); } +/** +* dma_resv_trylock_ctx - trylock the reservation object +* @obj: the reservation object +* @ctx: The ww acquire context or NULL. +* +* Tries to lock the reservation object for exclusive access and modification. +* Note, that the lock is only against other writers, readers will run +* concurrently with a writer under RCU. The seqlock is used to notify readers +* if they overlap with a writer. The context parameter ensures that other +* ww transactions can perform deadlock backoff if necessary, and that +* subsequent attempts to dma_resv_lock() @obj for @ctx will return +* -EALREADY. +* +* Return: true if the lock was acquired, false otherwise. +*/ +static inline bool __must_check +dma_resv_trylock_ctx (struct dma_resv *obj,struct ww_acquire_ctx *ctx) +{ +return ww_mutex_trylock (&obj->lock, ctx); +} +/** * dma_resv_trylock - trylock the reservation object * @obj: the reservation object
@@ -421,7 +442,7 @@ static inline int dma_resv_lock_slow_interruptible(struct dma_resv *obj,*/ static inline bool __must_check dma_resv_trylock (struct dma_resv *obj) { -return ww_mutex_trylock (&obj->lock,NULL ); +return dma_resv_trylock_ctx (obj,NULL ); }/**
Signature · 1 line
2.34.1
PATCH 2/5 drm/exec: don't immediately add the prelocked obj
In reply to Christian KönigSome contended objects might never be locked again in the case of eviction handling for example.
Make sure that those doesn't show up in the list of locked objects until they are explicitely mentioned.
--- drivers/gpu/drm/drm_exec.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
drivers/gpu/drm/drm_exec.c +9 -9
@@ -61,8 +61,11 @@ static void drm_exec_unlock_all(struct drm_exec *exec)drm_gem_object_put (obj); } -drm_gem_object_put (exec->prelocked); - exec->prelocked =NULL ; +if (exec->prelocked) { +dma_resv_unlock (exec->prelocked->resv); +drm_gem_object_put (exec->prelocked); + exec->prelocked =NULL ; + } }/**
@@ -179,16 +182,9 @@ static int drm_exec_lock_contended(struct drm_exec *exec)dma_resv_lock_slow (obj->resv, &exec->ticket); } - ret =drm_exec_obj_locked (exec, obj); -if (unlikely (ret)) - goto error_unlock; - exec->prelocked = obj;return 0 ; -error_unlock: -dma_resv_unlock (obj->resv); - error_dropref:drm_gem_object_put (obj);return ret;
@@ -214,6 +210,10 @@ int drm_exec_lock_obj(struct drm_exec *exec, struct drm_gem_object *obj)return ret;if (exec->prelocked == obj) { + ret =drm_exec_obj_locked (exec, obj); +if (unlikely (ret)) +return ret; +drm_gem_object_put (exec->prelocked); exec->prelocked =NULL ;return 0 ;
Signature · 1 line
2.34.1
Thomas Hellström
reviewerRe: [PATCH 2/5] drm/exec: don't immediately add the prelocked obj
Quoted 7 lines · Christian König
Some contended objects might never be locked again in the case of eviction
handling for example.Make sure that those doesn't show up in the list of locked objects until
they are explicitely mentioned.
Could you be a bit more specific in the commit message about in what situations that is bad?
/Thomas
Quoted 55 lines
Signed-off-by Christian König <[email protected]>---
drivers/gpu/drm/drm_exec.c | 18 +++++++++---------1 file changed, 9 insertions(+), 9 deletions(-)drivers/gpu/drm/drm_exec.c +0
-0@@ -61,8 +61,11 @@ static void drm_exec_unlock_all(struct drm_exec *exec) drm_gem_object_put(obj); }drivers/gpu/drm/drm_exec.c +5
-2-drm_gem_object_put (exec->prelocked); - exec->prelocked =NULL ; +if (exec->prelocked) { +dma_resv_unlock (exec->prelocked->resv); +drm_gem_object_put (exec->prelocked); + exec->prelocked =NULL ; + }}
/**drivers/gpu/drm/drm_exec.c +0
-0@@ -179,16 +182,9 @@ static int drm_exec_lock_contended(struct drm_exec *exec) dma_resv_lock_slow(obj->resv, &exec->ticket); }drivers/gpu/drm/drm_exec.c +0
-4- ret =drm_exec_obj_locked (exec, obj); -if (unlikely (ret)) - goto error_unlock; -
exec->prelocked= obj;
return 0;drivers/gpu/drm/drm_exec.c +0
-3-error_unlock: -dma_resv_unlock (obj->resv); -error_dropref:
drm_gem_object_put(obj);
return ret;drivers/gpu/drm/drm_exec.c +0
-0@@ -214,6 +210,10 @@ int drm_exec_lock_obj(struct drm_exec *exec, struct drm_gem_object *obj) return ret;
if (exec->prelocked== obj) {drivers/gpu/drm/drm_exec.c +4
-0+ ret =drm_exec_obj_locked (exec, obj); +if (unlikely (ret)) +return ret; +
drm_gem_object_put(exec->prelocked);exec->prelocked= NULL; return 0;
Re: [PATCH 2/5] drm/exec: don't immediately add the prelocked obj
Am 03.07.24 um 17:51 schrieb Thomas Hellström:
Quoted 10 lines · Christian König
Quoted 7 lines
Some contended objects might never be locked again in the case of eviction
handling for example.Make sure that those doesn't show up in the list of locked objects until
they are explicitely mentioned.Could you be a bit more specific in the commit message about in what situations that is bad?
The prelocked object is not necessarily expected to be in the list of locked objects.
I ran into issues because amdgpu tried to validate all locked objects and so tried to also validate the prelocked one (which was only locked for eviction).
That obviously didn't made much sense.
Regards,
Christian.
Quoted 59 lines
/Thomas
Quoted 54 lines
Signed-off-by Christian König <[email protected]>--- drivers/gpu/drm/drm_exec.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)drivers/gpu/drm/drm_exec.c +5
-2@@ -61,8 +61,11 @@ static void drm_exec_unlock_all(struct drm_exec *exec) drm_gem_object_put(obj); } -drm_gem_object_put (exec->prelocked); - exec->prelocked =NULL ; +if (exec->prelocked) { +dma_resv_unlock (exec->prelocked->resv); +drm_gem_object_put (exec->prelocked); + exec->prelocked =NULL ; + } }/**
drivers/gpu/drm/drm_exec.c +0
-7@@ -179,16 +182,9 @@ static int drm_exec_lock_contended(struct drm_exec *exec) dma_resv_lock_slow(obj->resv, &exec->ticket); } - ret =drm_exec_obj_locked (exec, obj); -if (unlikely (ret)) - goto error_unlock; - exec->prelocked = obj; return 0; -error_unlock: -dma_resv_unlock (obj->resv); - error_dropref: drm_gem_object_put(obj);return ret;
drivers/gpu/drm/drm_exec.c +4
-0@@ -214,6 +210,10 @@ int drm_exec_lock_obj(struct drm_exec *exec, struct drm_gem_object *obj) return ret; if (exec->prelocked == obj) { + ret =drm_exec_obj_locked (exec, obj); +if (unlikely (ret)) +return ret; + drm_gem_object_put(exec->prelocked); exec->prelocked = NULL;return 0;
Thomas Hellström
reviewerRe: [PATCH 2/5] drm/exec: don't immediately add the prelocked obj
Quoted 17 lines · Christian König
Am 03.07.24 um 17:51 schrieb Thomas Hellström:
Quoted 5 lines · Christian König
Quoted 4 lines
Some contended objects might never be locked again in the case of eviction
handling for example.Quoted 7 lines
Quoted 4 lines
Make sure that those doesn't show up in the list of locked objects
until
they are explicitely mentioned.Could you be a bit more specific in the commit message about in what
situations that is bad?The prelocked object is not necessarily expected to be in the list of locked objects.
Quoted 6 lines
I ran into issues because amdgpu tried to validate all locked objects and so tried to also validate the prelocked one (which was only locked
for eviction).That obviously didn't made much sense.
Indeed. Could you add a similar description to the commit message?
/Thomas
Quoted 66 lines
Regards,
Christian.Quoted 61 lines
/Thomas
Quoted 56 lines
Signed-off-by Christian König <[email protected]>---
drivers/gpu/drm/drm_exec.c | 18 +++++++++---------1 file changed, 9 insertions(+), 9 deletions(-)drivers/gpu/drm/drm_exec.c +0
-0@@ -61,8 +61,11 @@ static void drm_exec_unlock_all(struct
drm_exec
*exec)
drm_gem_object_put(obj);
}drivers/gpu/drm/drm_exec.c +5
-2-drm_gem_object_put (exec->prelocked); - exec->prelocked =NULL ; +if (exec->prelocked) { +dma_resv_unlock (exec->prelocked->resv); +drm_gem_object_put (exec->prelocked); + exec->prelocked =NULL ; + }}
/**drivers/gpu/drm/drm_exec.c +0
-0@@ -179,16 +182,9 @@ static int drm_exec_lock_contended(struct drm_exec *exec) dma_resv_lock_slow(obj->resv, &exec->ticket); }drivers/gpu/drm/drm_exec.c +0
-4- ret =drm_exec_obj_locked (exec, obj); -if (unlikely (ret)) - goto error_unlock; -
exec->prelocked= obj;
return 0;drivers/gpu/drm/drm_exec.c +0
-3-error_unlock: -dma_resv_unlock (obj->resv); -error_dropref:
drm_gem_object_put(obj);
return ret;drivers/gpu/drm/drm_exec.c +0
-0@@ -214,6 +210,10 @@ int drm_exec_lock_obj(struct drm_exec *exec, struct drm_gem_object *obj) return ret;
if (exec->prelocked== obj) {drivers/gpu/drm/drm_exec.c +4
-0+ ret =drm_exec_obj_locked (exec, obj); +if (unlikely (ret)) +return ret; +
drm_gem_object_put(exec->prelocked);exec->prelocked= NULL; return 0;
PATCH 3/5 drm/exec: provide trylock interface for eviction
In reply to Christian KönigThe TTM eviction path has some additional requirements which make it necessary to trylock an object and then eventually keep or drop the lock again.
--- drivers/gpu/drm/drm_exec.c | 77 ++++++++++++++++++++++++++++++++++++++ include/drm/drm_exec.h | 5 +++ 2 files changed, 82 insertions(+)
drivers/gpu/drm/drm_exec.c +77 -0
@@ -336,5 +336,82 @@ int drm_exec_prepare_array(struct drm_exec *exec, }EXPORT_SYMBOL (drm_exec_prepare_array); +/** +* drm_exec_trylock_obj - try to lock a GEM object +* @exec: the drm_exec object with the state +* @obj: the GEM object to trylock +* +* Try to lock a GEM object but don't grab a reference yet. +* +* Since we can't handle contention here it's illegal to trylock the first +* object. +* +* This function is suposed to be used from atomic context and we don't +* know if the GEM object will actually be used or not. So we don't grab a +* reference yet. +* +* Returns: True if the object could be locked, false otherwise. +*/ +bool drm_exec_trylock_obj (struct drm_exec *exec,struct drm_gem_object *obj) +{ +if (WARN_ON (!exec->num_objects)) +return false; + +if (exec->prelocked == obj) +return true; + +return dma_resv_trylock_ctx (obj->resv, &exec->ticket); +} +EXPORT_SYMBOL (drm_exec_trylock_obj); + +/** +* drm_exec_keep_trylocked_obj - keep the trylocked obj +* @exec: the drm_exec object with the state +* @obj: the GEM object to trylock +* +* Keep a trylocked object in the drm_exec state object. Grabs a reference to +* the object and adds it to the container of locked objects. +*/ +int drm_exec_keep_trylocked_obj (struct drm_exec *exec, +struct drm_gem_object *obj) +{ +int ret; + + ret =drm_exec_obj_locked (exec, obj); +if (ret) { +dma_resv_unlock (obj->resv); +return ret; + } + +if (exec->prelocked == obj) { +drm_gem_object_put (exec->prelocked); + exec->prelocked =NULL ; + } + +return ret; +} +EXPORT_SYMBOL (drm_exec_keep_trylocked_obj); + +/** +* drm_exec_drop_trylocked_obj - drop the trylocked obj +* @exec: the drm_exec object with the state +* @obj: the GEM object to trylock +* +* Used to drop a trylocked object in the drm_exec state object, drop the +* reservation lock again and cleanup all references. +*/ +void drm_exec_drop_trylocked_obj (struct drm_exec *exec, +struct drm_gem_object *obj) +{ +/* +* We can't drop the reference of prelocked objects since we might still +* be in atomic context. Additionally it makes sense to keep the +* prelocked object around since we might need it again later on. +*/ +if (exec->prelocked != obj) +dma_resv_unlock (obj->resv); +} +EXPORT_SYMBOL (drm_exec_drop_trylocked_obj); +MODULE_DESCRIPTION ("DRM execution context" );MODULE_LICENSE ("Dual MIT/GPL" );
include/drm/drm_exec.h +5 -0
@@ -146,5 +146,10 @@ int drm_exec_prepare_array(struct drm_exec *exec,struct drm_gem_object **objects, unsigned int num_objects, unsigned int num_fences); +bool drm_exec_trylock_obj (struct drm_exec *exec,struct drm_gem_object *obj); +int drm_exec_keep_trylocked_obj (struct drm_exec *exec, +struct drm_gem_object *obj); +void drm_exec_drop_trylocked_obj (struct drm_exec *exec, +struct drm_gem_object *obj);#endif
Signature · 1 line
2.34.1
Thomas Hellström
reviewerRe: [PATCH 3/5] drm/exec: provide trylock interface for eviction
Quoted 29 lines · Christian König
The TTM eviction path has some additional requirements which make it necessary to trylock an object and then eventually keep or drop the lock
again.Signed-off-by Christian König <[email protected]>---
drivers/gpu/drm/drm_exec.c | 77
++++++++++++++++++++++++++++++++++++++
include/drm/drm_exec.h | 5 +++2 files changed, 82 insertions(+)drivers/gpu/drm/drm_exec.c +0
-0@@ -336,5 +336,82 @@ int drm_exec_prepare_array(struct drm_exec *exec, } EXPORT_SYMBOL(drm_exec_prepare_array);
+/**
+ *drm_exec_trylock_obj- try to lock a GEM object
+ * @exec: thedrm_execobject with the state
+ * @obj: the GEM object to trylock
+ *
+ * Try to lock a GEM object but don't grab a reference yet.
+ *
+ * Since we can't handle contention here it's illegal to trylock the
Quoted 8 lines
first
+ * object.
+ *
+ * This function is suposed to be used from atomic context and we don't
+ * know if the GEM object will actually be used or not. So we don't grab a
+ * reference yet.
With the pending LRU walker the *need* for atomic context here is gone.
drivers/gpu/drm/drm_exec.c int drm_exec_prepare_array(struct drm_exec *exec, · line 358
Quoted 8 lines
+ *
+ * Returns: True if the object could be locked, false otherwise.
+ */
+booldrm_exec_trylock_obj(struct drm_exec *exec, struct drm_gem_object *obj)drivers/gpu/drm/drm_exec.c +3
-0+{ +if (WARN_ON (!exec->num_objects)) +return false;
I think we were in the middle of the discussion here about how to handle this. IIRC the last suggestion was to
if (exec->contended)
return false;
and provide a
drm_exec_sanitize_for_trylock() function that could be used to pre-lock the contended lock (and perhaps provide any needed memory to register so that a lock in atomic context could be made)
The use-case I'm worried about moving forward is, again, bo creation where I think a push out of the validation will make the conversion of drm_exec buffer object creation in drivers much more complicated than it already is. Or perhaps I'm misunderstanding how that was supposed to work.
/Thomas
Quoted 14 lines
drivers/gpu/drm/drm_exec.c +14
-0+ +if (exec->prelocked == obj) +return true; + +return dma_resv_trylock_ctx (obj->resv, &exec->ticket); +} +EXPORT_SYMBOL (drm_exec_trylock_obj); + +/** +* drm_exec_keep_trylocked_obj - keep the trylocked obj +* @exec: the drm_exec object with the state +* @obj: the GEM object to trylock +* +* Keep a trylocked object in the drm_exec state object. Grabs a
Quoted 3 lines
reference to
+ * the object and adds it to the container of locked objects.
+ */
So these could be dropped.
Quoted 64 lines
drivers/gpu/drm/drm_exec.c +26
-0+int drm_exec_keep_trylocked_obj (struct drm_exec *exec, +struct drm_gem_object *obj) +{ +int ret; + + ret =drm_exec_obj_locked (exec, obj); +if (ret) { +dma_resv_unlock (obj->resv); +return ret; + } + +if (exec->prelocked == obj) { +drm_gem_object_put (exec->prelocked); + exec->prelocked =NULL ; + } + +return ret; +} +EXPORT_SYMBOL (drm_exec_keep_trylocked_obj); + +/** +* drm_exec_drop_trylocked_obj - drop the trylocked obj +* @exec: the drm_exec object with the state +* @obj: the GEM object to trylock +* +* Used to drop a trylocked object in the drm_exec state object, drop the
+ * reservation lock again and cleanup all references.
+ */drivers/gpu/drm/drm_exec.c +5
-0+void drm_exec_drop_trylocked_obj (struct drm_exec *exec, +struct drm_gem_object *obj) +{ +/* +* We can't drop the reference of prelocked objects since we might still
drivers/gpu/drm/drm_exec.c +1
-0+* be in atomic context. Additionally it makes sense to keep the
drivers/gpu/drm/drm_exec.c +1
-0+* prelocked object around since we might need it again later on.
drivers/gpu/drm/drm_exec.c +6
-0+*/ +if (exec->prelocked != obj) +dma_resv_unlock (obj->resv); +} +EXPORT_SYMBOL (drm_exec_drop_trylocked_obj); +MODULE_DESCRIPTION("DRM execution context");
MODULE_LICENSE("Dual MIT/GPL");include/drm/drm_exec.h +5
-0@@ -146,5 +146,10 @@ int drm_exec_prepare_array(struct drm_exec *exec, struct drm_gem_object **objects, unsigned int num_objects, unsigned int num_fences); +bool drm_exec_trylock_obj (struct drm_exec *exec,struct drm_gem_object *obj); +int drm_exec_keep_trylocked_obj (struct drm_exec *exec, +struct drm_gem_object *obj); +void drm_exec_drop_trylocked_obj (struct drm_exec *exec, +struct drm_gem_object *obj);
#endif
PATCH 4/5 drm/ttm: support using drm_exec during eviction
In reply to Christian KönigAllow specifying a drm_exec object in TTMs operation context which is used to lock objects during eviction.
This allows to handle deadlocks much more gracefully and with that avoid returning -ENOMEM on heavily contended domains.
--- drivers/gpu/drm/ttm/ttm_bo.c | 75 +++++++++++++++++++++++++++--------- include/drm/ttm/ttm_bo.h | 3 ++ 2 files changed, 60 insertions(+), 18 deletions(-)
drivers/gpu/drm/ttm/ttm_bo.c +57 -18
@@ -31,6 +31,8 @@#define pr_fmt (fmt) "[TTM] " fmt +#include <drm/drm_exec.h> +#include <drm/ttm/ttm_bo.h> #include <drm/ttm/ttm_placement.h> #include <drm/ttm/ttm_tt.h>
@@ -529,13 +531,21 @@ static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,return false; } -if (bo->base.resv == ctx->resv) { +if (ctx->exec) { + ret =drm_exec_trylock_obj (ctx->exec, &bo->base); + + *locked = false; +if (!ret && busy) + *busy = true; + + }else if (bo->base.resv == ctx->resv) {dma_resv_assert_held (bo->base.resv);if (ctx->allow_res_evict) ret = true; *locked = false;if (busy) *busy = false; + }else { ret =dma_resv_trylock (bo->base.resv); *locked = ret;
@@ -545,11 +555,13 @@ static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,if (ret && place && (bo->resource->mem_type != place->mem_type || !bo->bdev->funcs->eviction_valuable (bo, place))) { - ret = false; -if (*locked) { + +if (ctx->exec) +drm_exec_drop_trylocked_obj (ctx->exec, &bo->base); +else if (*locked)dma_resv_unlock (bo->base.resv); - *locked = false; - } + ret = false; + *locked = false; }return ret;
@@ -573,21 +585,32 @@ static int ttm_mem_evict_wait_busy(struct ttm_buffer_object *busy_bo,if (!busy_bo || !ticket)return -EBUSY ; -if (ctx->interruptible) +if (ctx->exec) + r =drm_exec_lock_obj (ctx->exec, &busy_bo->base); + +else if (ctx->interruptible) r =dma_resv_lock_interruptible (busy_bo->base.resv, ticket);else r =dma_resv_lock (busy_bo->base.resv, ticket); +if (!ctx->exec && r == -EDEADLK ) + r = -EBUSY ; + +if (r) +return r; +/* * TODO: It would be better to keep the BO locked until allocation is at * least tried one more time, but that would mean a much larger rework * of TTM. */ -if (!r) +if (ctx->exec) +drm_exec_unlock_obj (ctx->exec, &busy_bo->base); +else dma_resv_unlock (busy_bo->base.resv); -return r == -EDEADLK ? -EBUSY : r; +return 0 ; } int ttm_mem_evict_first(struct ttm_device *bdev,
@@ -618,7 +641,10 @@ int ttm_mem_evict_first(struct ttm_device *bdev, bo = res->bo;break ; } -if (locked) + +if (ctx->exec) +drm_exec_drop_trylocked_obj (ctx->exec, &bo->base); +else if (locked)dma_resv_unlock (res->bo->base.resv); }
@@ -635,18 +661,30 @@ int ttm_mem_evict_first(struct ttm_device *bdev,if (bo->deleted) { ret =ttm_bo_cleanup_refs (bo, ctx->interruptible, ctx->no_wait_gpu, locked); +if (ctx->exec) +drm_exec_drop_trylocked_obj (ctx->exec, &bo->base); +ttm_bo_put (bo);return ret; }spin_unlock (&bdev->lru_lock); +if (ctx->exec) { + ret =drm_exec_keep_trylocked_obj (ctx->exec, &bo->base); +if (ret) + goto error_drop; + } + ret =ttm_bo_evict (bo, ctx); -if (locked) +if (ctx->exec) +drm_exec_unlock_obj (ctx->exec, &bo->base); +else if (locked)ttm_bo_unreserve (bo);else ttm_bo_move_to_lru_tail_unlocked (bo); +error_drop:ttm_bo_put (bo);return ret; }
@@ -1139,13 +1177,17 @@ int ttm_bo_swapout(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx, bo->ttm->page_flags & TTM_TT_FLAG_EXTERNAL || bo->ttm->page_flags & TTM_TT_FLAG_SWAPPED || !ttm_bo_get_unless_zero(bo)) { -if (locked) +if (ctx->exec) +drm_exec_drop_trylocked_obj (ctx->exec, &bo->base); +else if (locked)dma_resv_unlock (bo->base.resv);return -EBUSY ; }if (bo->deleted) { ret =ttm_bo_cleanup_refs (bo, false, false, locked); +if (ctx->exec) +drm_exec_drop_trylocked_obj (ctx->exec, &bo->base);ttm_bo_put (bo);return ret == -EBUSY ? -ENOSPC : ret; }
@@ -1193,13 +1235,10 @@ int ttm_bo_swapout(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx,if (ttm_tt_is_populated (bo->ttm)) ret =ttm_tt_swapout (bo->bdev, bo->ttm, gfp_flags); out: - -/* -* Unreserve without putting on LRU to avoid swapping out an -* already swapped buffer. -*/ -if (locked) -dma_resv_unlock (bo->base.resv); +if (ctx->exec) +drm_exec_drop_trylocked_obj (ctx->exec, &bo->base); +else if (locked) +ttm_bo_unreserve (bo);ttm_bo_put (bo);return ret == -EBUSY ? -ENOSPC : ret; }
include/drm/ttm/ttm_bo.h +3 -0
@@ -180,6 +180,8 @@ struct ttm_bo_kmap_obj {* faults. Should only be used by TTM internally. * @resv: Reservation object to allow reserved evictions with. * @bytes_moved: Statistics on how many bytes have been moved. +* @exec: optional drm_exec object to use for locking BOs and tracking which are +* locked. * * Context for TTM operations like changing buffer placement or general memory * allocation.
@@ -192,6 +194,7 @@ struct ttm_operation_ctx {bool force_alloc;struct dma_resv *resv;uint64_t bytes_moved; +struct drm_exec *exec; };/**
Signature · 1 line
2.34.1
PATCH 5/5 drm/amdgpu: use drm_exec during BO validation
In reply to Christian KönigThis allows to detect deadlocks happening because of resource constraints.
Especially submissions which want to use all of GDS doesn't result in sporadic -ENOMEM any more.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 86 ++++++++++++++------------ 1 file changed, 46 insertions(+), 40 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +46 -40
@@ -782,7 +782,7 @@ static int amdgpu_cs_bo_validate(void *param, struct amdgpu_bo *bo)struct ttm_operation_ctx ctx = { .interruptible = true, .no_wait_gpu = false, - .resv = bo->tbo.base.resv + .exec = &p->exec, };uint32_t domain;int r;
@@ -834,7 +834,10 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p, union drm_amdgpu_cs *cs) {struct amdgpu_fpriv *fpriv = p->filp->driver_priv; -struct ttm_operation_ctx ctx = { true, false }; +struct ttm_operation_ctx ctx = { + .interruptible =true, + .exec = &p->exec + };struct amdgpu_vm *vm = &fpriv->vm;struct amdgpu_bo_list_entry *e;struct drm_gem_object *obj;
@@ -919,50 +922,56 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,if (unlikely (r)) goto out_free_user_pages; } - } - -amdgpu_bo_list_for_each_userptr_entry (e, p->bo_list) { -struct mm_struct *usermm; - usermm =amdgpu_ttm_tt_get_usermm (e->bo->tbo.ttm); -if (usermm && usermm != current->mm) { - r = -EPERM ; - goto out_free_user_pages; - } +amdgpu_bo_list_for_each_userptr_entry (e, p->bo_list) { +struct mm_struct *usermm; -if (amdgpu_ttm_tt_is_userptr (e->bo->tbo.ttm) && - e->user_invalidated && e->user_pages) { -amdgpu_bo_placement_from_domain (e->bo, -AMDGPU_GEM_DOMAIN_CPU ); - r =ttm_bo_validate (&e->bo->tbo, &e->bo->placement, - &ctx); -if (r) + usermm =amdgpu_ttm_tt_get_usermm (e->bo->tbo.ttm); +if (usermm && usermm != current->mm) { + r = -EPERM ; goto out_free_user_pages; + } + +if (amdgpu_ttm_tt_is_userptr (e->bo->tbo.ttm) && + e->user_invalidated && e->user_pages) { +amdgpu_bo_placement_from_domain (e->bo, +AMDGPU_GEM_DOMAIN_CPU ); + r =ttm_bo_validate (&e->bo->tbo, &e->bo->placement, + &ctx); +drm_exec_retry_on_contention (&p->exec); +if (r) + goto out_free_user_pages; + +amdgpu_ttm_tt_set_user_pages (e->bo->tbo.ttm, + e->user_pages); + } -amdgpu_ttm_tt_set_user_pages (e->bo->tbo.ttm, - e->user_pages); +kvfree (e->user_pages); + e->user_pages =NULL ; } -kvfree (e->user_pages); - e->user_pages =NULL ; - } +amdgpu_cs_get_threshold_for_moves (p->adev, &p->bytes_moved_threshold, + &p->bytes_moved_vis_threshold); + p->bytes_moved =0 ; + p->bytes_moved_vis =0 ; -amdgpu_cs_get_threshold_for_moves (p->adev, &p->bytes_moved_threshold, - &p->bytes_moved_vis_threshold); - p->bytes_moved =0 ; - p->bytes_moved_vis =0 ; + r =amdgpu_vm_validate (p->adev, &fpriv->vm,NULL , + amdgpu_cs_bo_validate, p); +drm_exec_retry_on_contention (&p->exec); +if (r) { +DRM_ERROR ("amdgpu_vm_validate() failed.\n" ); + goto out_free_user_pages; + } - r =amdgpu_vm_validate (p->adev, &fpriv->vm,NULL , - amdgpu_cs_bo_validate, p); -if (r) { -DRM_ERROR ("amdgpu_vm_validate() failed.\n" ); - goto out_free_user_pages; - } +drm_exec_for_each_locked_object (&p->exec, index, obj) { + r =amdgpu_cs_bo_validate (p,gem_to_amdgpu_bo (obj)); +drm_exec_retry_on_contention (&p->exec); +if (unlikely (r)) + goto out_free_user_pages; + } -drm_exec_for_each_locked_object (&p->exec, index, obj) { - r =amdgpu_cs_bo_validate (p,gem_to_amdgpu_bo (obj)); -if (unlikely (r)) - goto out_free_user_pages; +amdgpu_cs_report_moved_bytes (p->adev, p->bytes_moved, + p->bytes_moved_vis); }if (p->uf_bo) {
@@ -973,9 +982,6 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p, p->gang_leader->uf_addr +=amdgpu_bo_gpu_offset (p->uf_bo); } -amdgpu_cs_report_moved_bytes (p->adev, p->bytes_moved, - p->bytes_moved_vis); -for (i =0 ; i < p->gang_size; ++i) amdgpu_job_set_resources(p->jobs[i], p->bo_list->gds_obj, p->bo_list->gws_obj,
Signature · 1 line
2.34.1
Thomas Hellström
reviewerHi, Christian,
Quoted 10 lines · Christian König
Hi guys,
We recently ran into a problem with deadlocks during eviction and while back Thomas worked on a patch set which was going into the direction of solving this.
So I simplified it to just the functionality needed to resolve this issue at. The resulting patch set is just the initial first step of using
drm_execin TTM for locking BOs during eviction.
Quoted 6 lines
Should a deadlock happen the
drm_execobject is now used to resolve it and prelock the contended object. This approach solves this the ENOMEM issue on contending evictions quite nicely.Please review and comment,
Christian.
Overall it looks sane, but I think it makes sense to review and land the part of the shrinker series first that touches this eviction path and gets rid of a lot of code that's hard to understand and simplifies the locking a lot. (That part doesn't touch drm_exec), and it has been pending reviews for some time.
I don't think it's correct to bypass that. Then we could work out the drm_exec implications.
/Thomas
Quoted 2 lines
Thomas Hellström
reviewerQuoted 28 lines · Thomas Hellström
Hi, Christian,
Quoted 10 lines · Christian König
Hi guys,
We recently ran into a problem with deadlocks during eviction and while back Thomas worked on a patch set which was going into the direction of solving this.
So I simplified it to just the functionality needed to resolve this issue at. The resulting patch set is just the initial first step of using
drm_execin TTM for locking BOs during eviction.Quoted 6 lines
Should a deadlock happen the
drm_execobject is now used to resolve it and prelock the contended object. This approach solves this the ENOMEM issue on contending evictions quite nicely.Please review and comment,
Christian.Overall it looks sane, but I think it makes sense to review and land the part of the shrinker series first that touches this eviction path and gets rid of a lot of code that's hard to understand and simplifies
the locking a lot. (That part doesn't touchdrm_exec), and it has been
pending reviews for some time.
Quoted 2 lines
I don't think it's correct to bypass that. Then we could work out the
drm_execimplications.
It's
https://patchwork.freedesktop.org/series/131815/
And in particular patch 7 there brings in the restartable LRU functionality and sipmlifies eviction immensely and would make the usage of this patchset's rudimentary drm_exec support easier to understand and review.
/Thomas
Quoted 7 lines
/Thomas
Quoted 2 lines
Thomas Hellström
reviewerQuoted 41 lines · Thomas Hellström
Quoted 34 lines · Thomas Hellström
Hi, Christian,
Quoted 12 lines · Christian König
Hi guys,
We recently ran into a problem with deadlocks during eviction and while back Thomas worked on a patch set which was going into the direction of solving this.
So I simplified it to just the functionality needed to resolve this
issue at. The resulting patch set is just the initial first step of
usingdrm_execin TTM for locking BOs during eviction.Quoted 8 lines
Should a deadlock happen the
drm_execobject is now used to resolve
it and prelock the contended object. This approach solves this the
ENOMEM issue on contending evictions quite nicely.Please review and comment,
Christian.Overall it looks sane, but I think it makes sense to review and land
the part of the shrinker series first that touches this eviction path
and gets rid of a lot of code that's hard to understand and simplifies
the locking a lot. (That part doesn't touchdrm_exec), and it has been
pending reviews for some time.Quoted 3 lines
I don't think it's correct to bypass that. Then we could work out the
drm_execimplications.It's
Quoted 8 lines
https://patchwork.freedesktop.org/series/131815/
And in particular patch 7 there brings in the restartable LRU functionality and sipmlifies eviction immensely and would make the usage of this patchset's rudimentary
drm_execsupport easier to understand and review./Thomas
Hm. I actually think all review comments have been sorted out up to that patch, so what's missing is a resend of the new version, RB from Matt and Review / Ack from you, then that part could be partially merged.
/Thomas
Quoted 10 lines
Quoted 9 lines
/Thomas
Quoted 2 lines
Am 03.07.24 um 15:59 schrieb Thomas Hellström:
Quoted 25 lines · Thomas Hellström
Quoted 24 lines · Thomas Hellström
Quoted 23 lines
Hi, Christian,
Quoted 20 lines · Christian König
Hi guys,
We recently ran into a problem with deadlocks during eviction and while back Thomas worked on a patch set which was going into the direction of solving this.
So I simplified it to just the functionality needed to resolve this
issue at. The resulting patch set is just the initial first step of
usingdrm_execin TTM for locking BOs during eviction.Should a deadlock happen the
drm_execobject is now used to resolve
it and prelock the contended object. This approach solves this the
ENOMEM issue on contending evictions quite nicely.Please review and comment,
Christian.
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Overall it looks sane, but I think it makes sense to review and land
the part of the shrinker series first that touches this eviction path
and gets rid of a lot of code that's hard to understand and simplifies
the locking a lot. (That part doesn't touchdrm_exec), and it has been
pending reviews for some time.
That's actually exactly what I wanted to avoid.
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
I don't think it's correct to bypass that. Then we could work out the
drm_execimplications.
Quoted 8 lines
Quoted 8 lines
It's
https://patchwork.freedesktop.org/series/131815/
And in particular patch 7 there brings in the restartable LRU functionality and sipmlifies eviction immensely and would make the usage of this patchset's rudimentary
drm_execsupport easier to understand and review.
Yeah, seen Mathews comments on that stuff.
Looked like 99% sane to me the last time I checked, the only thing I'm still not very keen at is still the bulk and cursor interaction.
Quoted 6 lines
Quoted 2 lines
/Thomas
Hm. I actually think all review comments have been sorted out up to that patch, so what's missing is a resend of the new version, RB from Matt and Review / Ack from you, then that part could be partially merged.
Going to take another look at that.
Regards,
Christian.
Quoted 10 lines
/Thomas
Quoted 5 lines
Quoted 5 lines
/Thomas
Quoted 1 line
Thomas Hellström
reviewerQuoted 68 lines · Christian König
Am 03.07.24 um 15:59 schrieb Thomas Hellström:
Quoted 30 lines · Thomas Hellström
Quoted 29 lines · Thomas Hellström
Quoted 28 lines
Hi, Christian,
Quoted 25 lines · Christian König
Hi guys,
We recently ran into a problem with deadlocks during eviction and
while back Thomas worked on a patch set which was going into the
direction of solving this.So I simplified it to just the functionality needed to resolve
this
issue at. The resulting patch set is just the initial first step
of
usingdrm_execin TTM for locking BOs during eviction.Should a deadlock happen the
drm_execobject is now used to resolve
it and prelock the contended object. This approach solves this
the
ENOMEM issue on contending evictions quite nicely.Please review and comment,
Christian.Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Overall it looks sane, but I think it makes sense to review and land
the part of the shrinker series first that touches this eviction
path
and gets rid of a lot of code that's hard to understand and simplifies
the locking a lot. (That part doesn't touchdrm_exec), and it has
been
pending reviews for some time.That's actually exactly what I wanted to avoid.
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
I don't think it's correct to bypass that. Then we could work out
thedrm_execimplications.It's
Quoted 7 lines
Quoted 7 lines
https://patchwork.freedesktop.org/series/131815/
And in particular patch 7 there brings in the restartable LRU functionality and sipmlifies eviction immensely and would make the
usage of this patchset's rudimentarydrm_execsupport easier to understand and review.Yeah, seen Mathews comments on that stuff.
Looked like 99% sane to me the last time I checked, the only thing I'm
still not very keen at is still the bulk and cursor interaction.Quoted 1 line
Quoted 1 line
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
/Thomas
Hm. I actually think all review comments have been sorted out up to that patch, so what's missing is a resend of the new version, RB from
Matt and Review / Ack from you, then that part could be partially merged.Going to take another look at that.
I'll resend latest version.
/Thomas
Quoted 15 lines
Regards,
Christian.Quoted 10 lines
/Thomas
Quoted 5 lines
Quoted 5 lines
/Thomas
Quoted 1 line