Commit
26401a4999f1596a515b2e704e7066ac09306488
drm/exec: don't immediately add prelocked obj to array of locked objs
When a contention is detected the drm_exec object grabs a reference to
the contended object, unlocks all other objects and pre-locks the
contended one before anything else.
The problem is that it is possible that this pre-locked object was about
to be destroyed and drm_exec is now holding the last reference to it.
So we never actually see this pre-locked object again to be explicitly
locked and so also never reserve a slot for a dma_fence object on it.
Should the driver now use drm_exec_for_each_locked_object() to add a new
fence to all locked objects we eventually see a warning or even a
BUG_ON() to prevent random memory corruption.
Fix this by keeping the prelocked object out of the array of locked objs
until it was explicitly mentioned by the driver again.
If the prelocked object is never mentioned again by the driver
just unlock and drop the reference at the end of the locking loop.
Signed-off-by: Christian König <christian.koenig@amd.com>
Reported-by: Vitaly Prosyak <vitaly.prosyak@amd.com>
Reviewed-by: Vitaly Prosyak <vitaly.prosyak@amd.com>
Tested-by: Vitaly Prosyak <vitaly.prosyak@amd.com>
Fixes: 09593216bff1 ("drm: execution context for GEM buffers v7")
CC: stable@vger.kernel.org # v6.6+
Link: https://lore.kernel.org/r/20261007124657.9664-1-christian.koenig@amd.com
Christian König <ckoenig.leichtzumerken@gmail.com> · 2026-10-07 12:46 UTC · committed by Christian König
2 files changed,
+52
−36
modified drivers/gpu/drm/drm_exec.c
| @@ -48,6 +48,16 @@ | |||
| 48 | 48 | * See struct dma_exec for more details. | |
| 49 | 49 | */ | |
| 50 | 50 | ||
| 51 | + | /* Unlock and put the prelocked obj */ | |
| 52 | + | static void drm_exec_drop_prelocked(struct drm_exec *exec) | |
| 53 | + | { | |
| 54 | + | if (exec->prelocked) { | |
| 55 | + | dma_resv_unlock(exec->prelocked->resv); | |
| 56 | + | drm_gem_object_put(exec->prelocked); | |
| 57 | + | exec->prelocked = NULL; | |
| 58 | + | } | |
| 59 | + | } | |
| 60 | + | ||
| 51 | 61 | /* Unlock all objects and drop references */ | |
| 52 | 62 | static void drm_exec_unlock_all(struct drm_exec *exec) | |
| 53 | 63 | { | |
| @@ -58,8 +68,7 @@ static void drm_exec_unlock_all(struct drm_exec *exec) | |||
| 58 | 68 | drm_gem_object_put(obj); | |
| 59 | 69 | } | |
| 60 | 70 | ||
| 61 | - | drm_gem_object_put(exec->prelocked); | |
| 62 | - | exec->prelocked = NULL; | |
| 71 | + | drm_exec_drop_prelocked(exec); | |
| 63 | 72 | } | |
| 64 | 73 | ||
| 65 | 74 | /** | |
| @@ -118,6 +127,14 @@ EXPORT_SYMBOL(drm_exec_fini); | |||
| 118 | 127 | bool drm_exec_cleanup(struct drm_exec *exec) | |
| 119 | 128 | { | |
| 120 | 129 | if (likely(!exec->contended)) { | |
| 130 | + | ||
| 131 | + | /* | |
| 132 | + | * The contended and prelocked object might never be passed to | |
| 133 | + | * drm_exec_lock_obj() again. This can happen when the object | |
| 134 | + | * is about to be cleaned up and we now hold the last reference | |
| 135 | + | * to it. Make sure that we don't unnecessarily keep it around. | |
| 136 | + | */ | |
| 137 | + | drm_exec_drop_prelocked(exec); | |
| 121 | 138 | ww_acquire_done(&exec->ticket); | |
| 122 | 139 | return false; | |
| 123 | 140 | } | |
| @@ -175,16 +192,16 @@ static int drm_exec_lock_contended(struct drm_exec *exec) | |||
| 175 | 192 | dma_resv_lock_slow(obj->resv, &exec->ticket); | |
| 176 | 193 | } | |
| 177 | 194 | ||
| 178 | - | ret = drm_exec_obj_locked(exec, obj); | |
| 179 | - | if (unlikely(ret)) | |
| 180 | - | goto error_unlock; | |
| 195 | + | /* | |
| 196 | + | * It is perfectly possible that we see another contention before the | |
| 197 | + | * first one is fully handled. | |
| 198 | + | */ | |
| 199 | + | drm_exec_drop_prelocked(exec); | |
| 181 | 200 | ||
| 201 | + | /* We move the reference from contended to prelocked here */ | |
| 182 | 202 | exec->prelocked = obj; | |
| 183 | 203 | return 0; | |
| 184 | 204 | ||
| 185 | - | error_unlock: | |
| 186 | - | dma_resv_unlock(obj->resv); | |
| 187 | - | ||
| 188 | 205 | error_dropref: | |
| 189 | 206 | drm_gem_object_put(obj); | |
| 190 | 207 | return ret; | |
| @@ -212,35 +229,31 @@ int drm_exec_lock_obj(struct drm_exec *exec, struct drm_gem_object *obj) | |||
| 212 | 229 | if (exec->prelocked == obj) { | |
| 213 | 230 | drm_gem_object_put(exec->prelocked); | |
| 214 | 231 | exec->prelocked = NULL; | |
| 215 | - | return 0; | |
| 216 | - | } | |
| 232 | + | } else { | |
| 233 | + | if (exec->flags & DRM_EXEC_INTERRUPTIBLE_WAIT) | |
| 234 | + | ret = dma_resv_lock_interruptible(obj->resv, | |
| 235 | + | &exec->ticket); | |
| 236 | + | else | |
| 237 | + | ret = dma_resv_lock(obj->resv, &exec->ticket); | |
| 238 | + | ||
| 239 | + | if (unlikely(ret == -EDEADLK)) { | |
| 240 | + | drm_gem_object_get(obj); | |
| 241 | + | exec->contended = obj; | |
| 242 | + | return -EDEADLK; | |
| 243 | + | } | |
| 217 | 244 | ||
| 218 | - | if (exec->flags & DRM_EXEC_INTERRUPTIBLE_WAIT) | |
| 219 | - | ret = dma_resv_lock_interruptible(obj->resv, &exec->ticket); | |
| 220 | - | else | |
| 221 | - | ret = dma_resv_lock(obj->resv, &exec->ticket); | |
| 245 | + | if (unlikely(ret == -EALREADY) && | |
| 246 | + | exec->flags & DRM_EXEC_IGNORE_DUPLICATES) | |
| 247 | + | return 0; | |
| 222 | 248 | ||
| 223 | - | if (unlikely(ret == -EDEADLK)) { | |
| 224 | - | drm_gem_object_get(obj); | |
| 225 | - | exec->contended = obj; | |
| 226 | - | return -EDEADLK; | |
| 249 | + | if (unlikely(ret)) | |
| 250 | + | return ret; | |
| 227 | 251 | } | |
| 228 | 252 | ||
| 229 | - | if (unlikely(ret == -EALREADY) && | |
| 230 | - | exec->flags & DRM_EXEC_IGNORE_DUPLICATES) | |
| 231 | - | return 0; | |
| 232 | - | ||
| 233 | - | if (unlikely(ret)) | |
| 234 | - | return ret; | |
| 235 | - | ||
| 236 | 253 | ret = drm_exec_obj_locked(exec, obj); | |
| 237 | 254 | if (ret) | |
| 238 | - | goto error_unlock; | |
| 239 | - | ||
| 240 | - | return 0; | |
| 255 | + | dma_resv_unlock(obj->resv); | |
| 241 | 256 | ||
| 242 | - | error_unlock: | |
| 243 | - | dma_resv_unlock(obj->resv); | |
| 244 | 257 | return ret; | |
| 245 | 258 | } | |
| 246 | 259 | EXPORT_SYMBOL(drm_exec_lock_obj); | |
modified include/drm/drm_exec.h
| @@ -73,13 +73,15 @@ drm_exec_obj(struct drm_exec *exec, unsigned long index) | |||
| 73 | 73 | ||
| 74 | 74 | /* Helper for drm_exec_for_each_locked_object(). Internal use only. */ | |
| 75 | 75 | #define __drm_exec_for_each_locked_object(exec, obj, __index) \ | |
| 76 | - | for (unsigned long __index = 0; ((obj) = drm_exec_obj(exec, __index)); ++__index) | |
| 76 | + | for (unsigned long __index = 0; ((obj) = drm_exec_obj(exec, __index)); \ | |
| 77 | + | ++__index) | |
| 77 | 78 | /** | |
| 78 | 79 | * drm_exec_for_each_locked_object - iterate over all the locked objects | |
| 79 | 80 | * @exec: drm_exec object | |
| 80 | 81 | * @obj: the current GEM object | |
| 81 | 82 | * | |
| 82 | - | * Iterate over all the locked GEM objects inside the drm_exec object. | |
| 83 | + | * Iterate over all the explicitly locked GEM objects inside the drm_exec | |
| 84 | + | * container, except for the contended and implicit prelocked one. | |
| 83 | 85 | */ | |
| 84 | 86 | #define drm_exec_for_each_locked_object(exec, obj) \ | |
| 85 | 87 | __drm_exec_for_each_locked_object(exec, obj, __UNIQUE_ID(drm_exec)) | |
| @@ -94,12 +96,13 @@ drm_exec_obj(struct drm_exec *exec, unsigned long index) | |||
| 94 | 96 | * @exec: drm_exec object | |
| 95 | 97 | * @obj: the current GEM object | |
| 96 | 98 | * | |
| 97 | - | * Iterate over all the locked GEM objects inside the drm_exec object in | |
| 98 | - | * reverse locking order. Note that the internal index may wrap around, | |
| 99 | - | * but that will be caught by drm_exec_obj(), returning a NULL object. | |
| 99 | + | * Iterate over all the explicitly locked GEM objects inside the drm_exec | |
| 100 | + | * container in reverse locking order. Note that the internal index may wrap | |
| 101 | + | * around, but that will be caught by drm_exec_obj(), returning a NULL object. | |
| 100 | 102 | */ | |
| 101 | 103 | #define drm_exec_for_each_locked_object_reverse(exec, obj) \ | |
| 102 | - | __drm_exec_for_each_locked_object_reverse(exec, obj, __UNIQUE_ID(drm_exec)) | |
| 104 | + | __drm_exec_for_each_locked_object_reverse(exec, obj, \ | |
| 105 | + | __UNIQUE_ID(drm_exec)) | |
| 103 | 106 | ||
| 104 | 107 | /** | |
| 105 | 108 | * drm_exec_until_all_locked - loop until all GEM objects are locked | |