TTM needs to trylock dma_resv objects in a couple of places. However this functionality is missing in drm_exec. Introduce it. Note that in addition to the -EBUSY error returned on failure to take the lock, the operation may return -ENOMEM if there was a failure to allocate memory for the drm_exec held lock array. This failure mode could be avoided if the drm_exec structure instead maintained a linked list of locked objects, similar to i915. Cc: Christian König <christian.koenig@xxxxxxx> Cc: Somalapuram Amaranath <Amaranath.Somalapuram@xxxxxxx> Cc: Matthew Brost <matthew.brost@xxxxxxxxx> Cc: <dri-devel@xxxxxxxxxxxxxxxxxxxxx> Signed-off-by: Thomas Hellström <thomas.hellstrom@xxxxxxxxxxxxxxx> --- drivers/gpu/drm/drm_exec.c | 50 +++++++++++++++++++++++++++++++++++--- include/drm/drm_exec.h | 1 + 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/drivers/gpu/drm/drm_exec.c b/drivers/gpu/drm/drm_exec.c index 3770a5d30213..1383680ffa4a 100644 --- a/drivers/gpu/drm/drm_exec.c +++ b/drivers/gpu/drm/drm_exec.c @@ -139,14 +139,17 @@ EXPORT_SYMBOL(drm_exec_cleanup); /* Track the locked object in the array */ static int drm_exec_obj_locked(struct drm_exec *exec, - struct drm_gem_object *obj) + struct drm_gem_object *obj, + gfp_t gfp) { + might_alloc(gfp); + if (unlikely(exec->num_objects == exec->max_objects)) { size_t size = exec->max_objects * sizeof(void *); void *tmp; tmp = kvrealloc(exec->objects, size, size + PAGE_SIZE, - GFP_KERNEL); + gfp); if (!tmp) return -ENOMEM; @@ -179,7 +182,7 @@ 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); + ret = drm_exec_obj_locked(exec, obj, GFP_KERNEL); if (unlikely(ret)) goto error_unlock; @@ -215,6 +218,45 @@ int drm_exec_handle_contended(struct drm_exec *exec) } EXPORT_SYMBOL(drm_exec_handle_contended); +/** + * drm_exec_trylock_obj - trylock a GEM object for use + * @exec: the drm_exec object with the state. + * @obj: the GEM object to lock. + * + * Trylock a GEM object for use and grab a reference to it. + * + * Returns: -EALREADY when object is already locked (can be suppressed by + * setting the DRM_EXEC_IGNORE_DUPLICATES flag), -ENOMEM when memory + * allocation failed, and zero for success. If the object was already + * locked, -EBUSY will be returned. + */ +int drm_exec_trylock_obj(struct drm_exec *exec, struct drm_gem_object *obj) +{ + int ret; + + might_alloc(GFP_ATOMIC); + + if (exec->prelocked == obj) { + drm_gem_object_put(exec->prelocked); + exec->prelocked = NULL; + return 0; + } + + if (!dma_resv_trylock_ctx(obj->resv, &exec->ticket)) { + if (dma_resv_locking_ctx(obj->resv) == &exec->ticket) + return (exec->flags & DRM_EXEC_IGNORE_DUPLICATES) ? 0 : -EALREADY; + else + return -EBUSY; + } + + ret = drm_exec_obj_locked(exec, obj, GFP_ATOMIC | __GFP_NOWARN); + if (ret) + dma_resv_unlock(obj->resv); + + return ret; +} +EXPORT_SYMBOL(drm_exec_trylock_obj); + /** * drm_exec_lock_obj - lock a GEM object for use * @exec: the drm_exec object with the state @@ -254,7 +296,7 @@ int drm_exec_lock_obj(struct drm_exec *exec, struct drm_gem_object *obj) if (unlikely(ret)) return ret; - ret = drm_exec_obj_locked(exec, obj); + ret = drm_exec_obj_locked(exec, obj, GFP_KERNEL); if (ret) goto error_unlock; diff --git a/include/drm/drm_exec.h b/include/drm/drm_exec.h index fafb40d96e38..ea0f2117ee0c 100644 --- a/include/drm/drm_exec.h +++ b/include/drm/drm_exec.h @@ -152,6 +152,7 @@ void drm_exec_init(struct drm_exec *exec, u32 flags, unsigned nr); void drm_exec_fini(struct drm_exec *exec); bool drm_exec_cleanup(struct drm_exec *exec); int drm_exec_lock_obj(struct drm_exec *exec, struct drm_gem_object *obj); +int drm_exec_trylock_obj(struct drm_exec *exec, struct drm_gem_object *obj); void drm_exec_unlock_obj(struct drm_exec *exec, struct drm_gem_object *obj); int drm_exec_prepare_obj(struct drm_exec *exec, struct drm_gem_object *obj, unsigned int num_fences); -- 2.44.0