1 /* 2 * Copyright (C) 2014 Red Hat 3 * Author: Rob Clark <robdclark@gmail.com> 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a 6 * copy of this software and associated documentation files (the "Software"), 7 * to deal in the Software without restriction, including without limitation 8 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 9 * and/or sell copies of the Software, and to permit persons to whom the 10 * Software is furnished to do so, subject to the following conditions: 11 * 12 * The above copyright notice and this permission notice shall be included in 13 * all copies or substantial portions of the Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 21 * OTHER DEALINGS IN THE SOFTWARE. 22 */ 23 24 #include <drm/drm_atomic.h> 25 #include <drm/drm_crtc.h> 26 #include <drm/drm_device.h> 27 #include <drm/drm_modeset_lock.h> 28 29 /** 30 * DOC: kms locking 31 * 32 * As KMS moves toward more fine grained locking, and atomic ioctl where 33 * userspace can indirectly control locking order, it becomes necessary 34 * to use &ww_mutex and acquire-contexts to avoid deadlocks. But because 35 * the locking is more distributed around the driver code, we want a bit 36 * of extra utility/tracking out of our acquire-ctx. This is provided 37 * by &struct drm_modeset_lock and &struct drm_modeset_acquire_ctx. 38 * 39 * For basic principles of &ww_mutex, see: Documentation/locking/ww-mutex-design.rst 40 * 41 * The basic usage pattern is to:: 42 * 43 * drm_modeset_acquire_init(ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE) 44 * retry: 45 * foreach (lock in random_ordered_set_of_locks) { 46 * ret = drm_modeset_lock(lock, ctx) 47 * if (ret == -EDEADLK) { 48 * ret = drm_modeset_backoff(ctx); 49 * if (!ret) 50 * goto retry; 51 * } 52 * if (ret) 53 * goto out; 54 * } 55 * ... do stuff ... 56 * out: 57 * drm_modeset_drop_locks(ctx); 58 * drm_modeset_acquire_fini(ctx); 59 * 60 * For convenience this control flow is implemented in 61 * DRM_MODESET_LOCK_ALL_BEGIN() and DRM_MODESET_LOCK_ALL_END() for the case 62 * where all modeset locks need to be taken through drm_modeset_lock_all_ctx(). 63 * 64 * If all that is needed is a single modeset lock, then the &struct 65 * drm_modeset_acquire_ctx is not needed and the locking can be simplified 66 * by passing a NULL instead of ctx in the drm_modeset_lock() call or 67 * calling drm_modeset_lock_single_interruptible(). To unlock afterwards 68 * call drm_modeset_unlock(). 69 * 70 * On top of these per-object locks using &ww_mutex there's also an overall 71 * &drm_mode_config.mutex, for protecting everything else. Mostly this means 72 * probe state of connectors, and preventing hotplug add/removal of connectors. 73 * 74 * Finally there's a bunch of dedicated locks to protect drm core internal 75 * lists and lookup data structures. 76 */ 77 78 static DEFINE_WW_CLASS(crtc_ww_class); 79 80 /** 81 * drm_modeset_lock_all - take all modeset locks 82 * @dev: DRM device 83 * 84 * This function takes all modeset locks, suitable where a more fine-grained 85 * scheme isn't (yet) implemented. Locks must be dropped by calling the 86 * drm_modeset_unlock_all() function. 87 * 88 * This function is deprecated. It allocates a lock acquisition context and 89 * stores it in &drm_device.mode_config. This facilitate conversion of 90 * existing code because it removes the need to manually deal with the 91 * acquisition context, but it is also brittle because the context is global 92 * and care must be taken not to nest calls. New code should use the 93 * drm_modeset_lock_all_ctx() function and pass in the context explicitly. 94 */ 95 void drm_modeset_lock_all(struct drm_device *dev) 96 { 97 struct drm_mode_config *config = &dev->mode_config; 98 struct drm_modeset_acquire_ctx *ctx; 99 int ret; 100 101 #ifdef __linux__ 102 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL | __GFP_NOFAIL); 103 #else 104 ctx = kzalloc(sizeof(*ctx), M_WAITOK); 105 #endif 106 if (WARN_ON(!ctx)) 107 return; 108 109 mutex_lock(&config->mutex); 110 111 drm_modeset_acquire_init(ctx, 0); 112 113 retry: 114 ret = drm_modeset_lock_all_ctx(dev, ctx); 115 if (ret < 0) { 116 if (ret == -EDEADLK) { 117 drm_modeset_backoff(ctx); 118 goto retry; 119 } 120 121 drm_modeset_acquire_fini(ctx); 122 kfree(ctx); 123 return; 124 } 125 ww_acquire_done(&ctx->ww_ctx); 126 127 WARN_ON(config->acquire_ctx); 128 129 /* 130 * We hold the locks now, so it is safe to stash the acquisition 131 * context for drm_modeset_unlock_all(). 132 */ 133 config->acquire_ctx = ctx; 134 135 drm_warn_on_modeset_not_all_locked(dev); 136 } 137 EXPORT_SYMBOL(drm_modeset_lock_all); 138 139 /** 140 * drm_modeset_unlock_all - drop all modeset locks 141 * @dev: DRM device 142 * 143 * This function drops all modeset locks taken by a previous call to the 144 * drm_modeset_lock_all() function. 145 * 146 * This function is deprecated. It uses the lock acquisition context stored 147 * in &drm_device.mode_config. This facilitates conversion of existing 148 * code because it removes the need to manually deal with the acquisition 149 * context, but it is also brittle because the context is global and care must 150 * be taken not to nest calls. New code should pass the acquisition context 151 * directly to the drm_modeset_drop_locks() function. 152 */ 153 void drm_modeset_unlock_all(struct drm_device *dev) 154 { 155 struct drm_mode_config *config = &dev->mode_config; 156 struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx; 157 158 if (WARN_ON(!ctx)) 159 return; 160 161 config->acquire_ctx = NULL; 162 drm_modeset_drop_locks(ctx); 163 drm_modeset_acquire_fini(ctx); 164 165 kfree(ctx); 166 167 mutex_unlock(&dev->mode_config.mutex); 168 } 169 EXPORT_SYMBOL(drm_modeset_unlock_all); 170 171 /** 172 * drm_warn_on_modeset_not_all_locked - check that all modeset locks are locked 173 * @dev: device 174 * 175 * Useful as a debug assert. 176 */ 177 void drm_warn_on_modeset_not_all_locked(struct drm_device *dev) 178 { 179 struct drm_crtc *crtc; 180 181 /* Locking is currently fubar in the panic handler. */ 182 if (oops_in_progress) 183 return; 184 185 drm_for_each_crtc(crtc, dev) 186 WARN_ON(!drm_modeset_is_locked(&crtc->mutex)); 187 188 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex)); 189 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex)); 190 } 191 EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked); 192 193 /** 194 * drm_modeset_acquire_init - initialize acquire context 195 * @ctx: the acquire context 196 * @flags: 0 or %DRM_MODESET_ACQUIRE_INTERRUPTIBLE 197 * 198 * When passing %DRM_MODESET_ACQUIRE_INTERRUPTIBLE to @flags, 199 * all calls to drm_modeset_lock() will perform an interruptible 200 * wait. 201 */ 202 void drm_modeset_acquire_init(struct drm_modeset_acquire_ctx *ctx, 203 uint32_t flags) 204 { 205 memset(ctx, 0, sizeof(*ctx)); 206 ww_acquire_init(&ctx->ww_ctx, &crtc_ww_class); 207 INIT_LIST_HEAD(&ctx->locked); 208 209 if (flags & DRM_MODESET_ACQUIRE_INTERRUPTIBLE) 210 ctx->interruptible = true; 211 } 212 EXPORT_SYMBOL(drm_modeset_acquire_init); 213 214 /** 215 * drm_modeset_acquire_fini - cleanup acquire context 216 * @ctx: the acquire context 217 */ 218 void drm_modeset_acquire_fini(struct drm_modeset_acquire_ctx *ctx) 219 { 220 ww_acquire_fini(&ctx->ww_ctx); 221 } 222 EXPORT_SYMBOL(drm_modeset_acquire_fini); 223 224 /** 225 * drm_modeset_drop_locks - drop all locks 226 * @ctx: the acquire context 227 * 228 * Drop all locks currently held against this acquire context. 229 */ 230 void drm_modeset_drop_locks(struct drm_modeset_acquire_ctx *ctx) 231 { 232 WARN_ON(ctx->contended); 233 while (!list_empty(&ctx->locked)) { 234 struct drm_modeset_lock *lock; 235 236 lock = list_first_entry(&ctx->locked, 237 struct drm_modeset_lock, head); 238 239 drm_modeset_unlock(lock); 240 } 241 } 242 EXPORT_SYMBOL(drm_modeset_drop_locks); 243 244 static inline int modeset_lock(struct drm_modeset_lock *lock, 245 struct drm_modeset_acquire_ctx *ctx, 246 bool interruptible, bool slow) 247 { 248 int ret; 249 250 WARN_ON(ctx->contended); 251 252 if (ctx->trylock_only) { 253 lockdep_assert_held(&ctx->ww_ctx); 254 255 if (!ww_mutex_trylock(&lock->mutex)) 256 return -EBUSY; 257 else 258 return 0; 259 } else if (interruptible && slow) { 260 ret = ww_mutex_lock_slow_interruptible(&lock->mutex, &ctx->ww_ctx); 261 } else if (interruptible) { 262 ret = ww_mutex_lock_interruptible(&lock->mutex, &ctx->ww_ctx); 263 } else if (slow) { 264 ww_mutex_lock_slow(&lock->mutex, &ctx->ww_ctx); 265 ret = 0; 266 } else { 267 ret = ww_mutex_lock(&lock->mutex, &ctx->ww_ctx); 268 } 269 if (!ret) { 270 WARN_ON(!list_empty(&lock->head)); 271 list_add(&lock->head, &ctx->locked); 272 } else if (ret == -EALREADY) { 273 /* we already hold the lock.. this is fine. For atomic 274 * we will need to be able to drm_modeset_lock() things 275 * without having to keep track of what is already locked 276 * or not. 277 */ 278 ret = 0; 279 } else if (ret == -EDEADLK) { 280 ctx->contended = lock; 281 } 282 283 return ret; 284 } 285 286 /** 287 * drm_modeset_backoff - deadlock avoidance backoff 288 * @ctx: the acquire context 289 * 290 * If deadlock is detected (ie. drm_modeset_lock() returns -EDEADLK), 291 * you must call this function to drop all currently held locks and 292 * block until the contended lock becomes available. 293 * 294 * This function returns 0 on success, or -ERESTARTSYS if this context 295 * is initialized with %DRM_MODESET_ACQUIRE_INTERRUPTIBLE and the 296 * wait has been interrupted. 297 */ 298 int drm_modeset_backoff(struct drm_modeset_acquire_ctx *ctx) 299 { 300 struct drm_modeset_lock *contended = ctx->contended; 301 302 ctx->contended = NULL; 303 304 if (WARN_ON(!contended)) 305 return 0; 306 307 drm_modeset_drop_locks(ctx); 308 309 return modeset_lock(contended, ctx, ctx->interruptible, true); 310 } 311 EXPORT_SYMBOL(drm_modeset_backoff); 312 313 /** 314 * drm_modeset_lock_init - initialize lock 315 * @lock: lock to init 316 */ 317 void drm_modeset_lock_init(struct drm_modeset_lock *lock) 318 { 319 ww_mutex_init(&lock->mutex, &crtc_ww_class); 320 INIT_LIST_HEAD(&lock->head); 321 } 322 EXPORT_SYMBOL(drm_modeset_lock_init); 323 324 /** 325 * drm_modeset_lock - take modeset lock 326 * @lock: lock to take 327 * @ctx: acquire ctx 328 * 329 * If @ctx is not NULL, then its ww acquire context is used and the 330 * lock will be tracked by the context and can be released by calling 331 * drm_modeset_drop_locks(). If -EDEADLK is returned, this means a 332 * deadlock scenario has been detected and it is an error to attempt 333 * to take any more locks without first calling drm_modeset_backoff(). 334 * 335 * If the @ctx is not NULL and initialized with 336 * %DRM_MODESET_ACQUIRE_INTERRUPTIBLE, this function will fail with 337 * -ERESTARTSYS when interrupted. 338 * 339 * If @ctx is NULL then the function call behaves like a normal, 340 * uninterruptible non-nesting mutex_lock() call. 341 */ 342 int drm_modeset_lock(struct drm_modeset_lock *lock, 343 struct drm_modeset_acquire_ctx *ctx) 344 { 345 if (ctx) 346 return modeset_lock(lock, ctx, ctx->interruptible, false); 347 348 ww_mutex_lock(&lock->mutex, NULL); 349 return 0; 350 } 351 EXPORT_SYMBOL(drm_modeset_lock); 352 353 /** 354 * drm_modeset_lock_single_interruptible - take a single modeset lock 355 * @lock: lock to take 356 * 357 * This function behaves as drm_modeset_lock() with a NULL context, 358 * but performs interruptible waits. 359 * 360 * This function returns 0 on success, or -ERESTARTSYS when interrupted. 361 */ 362 int drm_modeset_lock_single_interruptible(struct drm_modeset_lock *lock) 363 { 364 return ww_mutex_lock_interruptible(&lock->mutex, NULL); 365 } 366 EXPORT_SYMBOL(drm_modeset_lock_single_interruptible); 367 368 /** 369 * drm_modeset_unlock - drop modeset lock 370 * @lock: lock to release 371 */ 372 void drm_modeset_unlock(struct drm_modeset_lock *lock) 373 { 374 list_del_init(&lock->head); 375 ww_mutex_unlock(&lock->mutex); 376 } 377 EXPORT_SYMBOL(drm_modeset_unlock); 378 379 /** 380 * drm_modeset_lock_all_ctx - take all modeset locks 381 * @dev: DRM device 382 * @ctx: lock acquisition context 383 * 384 * This function takes all modeset locks, suitable where a more fine-grained 385 * scheme isn't (yet) implemented. 386 * 387 * Unlike drm_modeset_lock_all(), it doesn't take the &drm_mode_config.mutex 388 * since that lock isn't required for modeset state changes. Callers which 389 * need to grab that lock too need to do so outside of the acquire context 390 * @ctx. 391 * 392 * Locks acquired with this function should be released by calling the 393 * drm_modeset_drop_locks() function on @ctx. 394 * 395 * See also: DRM_MODESET_LOCK_ALL_BEGIN() and DRM_MODESET_LOCK_ALL_END() 396 * 397 * Returns: 0 on success or a negative error-code on failure. 398 */ 399 int drm_modeset_lock_all_ctx(struct drm_device *dev, 400 struct drm_modeset_acquire_ctx *ctx) 401 { 402 struct drm_private_obj *privobj; 403 struct drm_crtc *crtc; 404 struct drm_plane *plane; 405 int ret; 406 407 ret = drm_modeset_lock(&dev->mode_config.connection_mutex, ctx); 408 if (ret) 409 return ret; 410 411 drm_for_each_crtc(crtc, dev) { 412 ret = drm_modeset_lock(&crtc->mutex, ctx); 413 if (ret) 414 return ret; 415 } 416 417 drm_for_each_plane(plane, dev) { 418 ret = drm_modeset_lock(&plane->mutex, ctx); 419 if (ret) 420 return ret; 421 } 422 423 drm_for_each_privobj(privobj, dev) { 424 ret = drm_modeset_lock(&privobj->lock, ctx); 425 if (ret) 426 return ret; 427 } 428 429 return 0; 430 } 431 EXPORT_SYMBOL(drm_modeset_lock_all_ctx); 432