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 #include <drm/drm_print.h> 29 30 /** 31 * DOC: kms locking 32 * 33 * As KMS moves toward more fine grained locking, and atomic ioctl where 34 * userspace can indirectly control locking order, it becomes necessary 35 * to use &ww_mutex and acquire-contexts to avoid deadlocks. But because 36 * the locking is more distributed around the driver code, we want a bit 37 * of extra utility/tracking out of our acquire-ctx. This is provided 38 * by &struct drm_modeset_lock and &struct drm_modeset_acquire_ctx. 39 * 40 * For basic principles of &ww_mutex, see: Documentation/locking/ww-mutex-design.rst 41 * 42 * The basic usage pattern is to:: 43 * 44 * drm_modeset_acquire_init(ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE) 45 * retry: 46 * foreach (lock in random_ordered_set_of_locks) { 47 * ret = drm_modeset_lock(lock, ctx) 48 * if (ret == -EDEADLK) { 49 * ret = drm_modeset_backoff(ctx); 50 * if (!ret) 51 * goto retry; 52 * } 53 * if (ret) 54 * goto out; 55 * } 56 * ... do stuff ... 57 * out: 58 * drm_modeset_drop_locks(ctx); 59 * drm_modeset_acquire_fini(ctx); 60 * 61 * For convenience this control flow is implemented in 62 * DRM_MODESET_LOCK_ALL_BEGIN() and DRM_MODESET_LOCK_ALL_END() for the case 63 * where all modeset locks need to be taken through drm_modeset_lock_all_ctx(). 64 * 65 * If all that is needed is a single modeset lock, then the &struct 66 * drm_modeset_acquire_ctx is not needed and the locking can be simplified 67 * by passing a NULL instead of ctx in the drm_modeset_lock() call or 68 * calling drm_modeset_lock_single_interruptible(). To unlock afterwards 69 * call drm_modeset_unlock(). 70 * 71 * On top of these per-object locks using &ww_mutex there's also an overall 72 * &drm_mode_config.mutex, for protecting everything else. Mostly this means 73 * probe state of connectors, and preventing hotplug add/removal of connectors. 74 * 75 * Finally there's a bunch of dedicated locks to protect drm core internal 76 * lists and lookup data structures. 77 */ 78 79 static DEFINE_WW_CLASS(crtc_ww_class); 80 81 #if IS_ENABLED(CONFIG_DRM_DEBUG_MODESET_LOCK) 82 static noinline depot_stack_handle_t __drm_stack_depot_save(void) 83 { 84 unsigned long entries[8]; 85 unsigned int n; 86 87 n = stack_trace_save(entries, ARRAY_SIZE(entries), 1); 88 89 return stack_depot_save(entries, n, GFP_NOWAIT | __GFP_NOWARN); 90 } 91 92 static void __drm_stack_depot_print(depot_stack_handle_t stack_depot) 93 { 94 struct drm_printer p = drm_debug_printer("drm_modeset_lock"); 95 unsigned long *entries; 96 unsigned int nr_entries; 97 char *buf; 98 99 buf = kmalloc(PAGE_SIZE, GFP_NOWAIT | __GFP_NOWARN); 100 if (!buf) 101 return; 102 103 nr_entries = stack_depot_fetch(stack_depot, &entries); 104 stack_trace_snprint(buf, PAGE_SIZE, entries, nr_entries, 2); 105 106 drm_printf(&p, "attempting to lock a contended lock without backoff:\n%s", buf); 107 108 kfree(buf); 109 } 110 111 static void __drm_stack_depot_init(void) 112 { 113 stack_depot_init(); 114 } 115 #else /* CONFIG_DRM_DEBUG_MODESET_LOCK */ 116 static depot_stack_handle_t __drm_stack_depot_save(void) 117 { 118 return 0; 119 } 120 static void __drm_stack_depot_print(depot_stack_handle_t stack_depot) 121 { 122 } 123 static void __drm_stack_depot_init(void) 124 { 125 } 126 #endif /* CONFIG_DRM_DEBUG_MODESET_LOCK */ 127 128 /** 129 * drm_modeset_lock_all - take all modeset locks 130 * @dev: DRM device 131 * 132 * This function takes all modeset locks, suitable where a more fine-grained 133 * scheme isn't (yet) implemented. Locks must be dropped by calling the 134 * drm_modeset_unlock_all() function. 135 * 136 * This function is deprecated. It allocates a lock acquisition context and 137 * stores it in &drm_device.mode_config. This facilitate conversion of 138 * existing code because it removes the need to manually deal with the 139 * acquisition context, but it is also brittle because the context is global 140 * and care must be taken not to nest calls. New code should use the 141 * drm_modeset_lock_all_ctx() function and pass in the context explicitly. 142 */ 143 void drm_modeset_lock_all(struct drm_device *dev) 144 { 145 struct drm_mode_config *config = &dev->mode_config; 146 struct drm_modeset_acquire_ctx *ctx; 147 int ret; 148 149 #ifdef __linux__ 150 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL | __GFP_NOFAIL); 151 #else 152 ctx = kzalloc(sizeof(*ctx), M_WAITOK); 153 #endif 154 if (WARN_ON(!ctx)) 155 return; 156 157 mutex_lock(&config->mutex); 158 159 drm_modeset_acquire_init(ctx, 0); 160 161 retry: 162 ret = drm_modeset_lock_all_ctx(dev, ctx); 163 if (ret < 0) { 164 if (ret == -EDEADLK) { 165 drm_modeset_backoff(ctx); 166 goto retry; 167 } 168 169 drm_modeset_acquire_fini(ctx); 170 kfree(ctx); 171 return; 172 } 173 ww_acquire_done(&ctx->ww_ctx); 174 175 WARN_ON(config->acquire_ctx); 176 177 /* 178 * We hold the locks now, so it is safe to stash the acquisition 179 * context for drm_modeset_unlock_all(). 180 */ 181 config->acquire_ctx = ctx; 182 183 drm_warn_on_modeset_not_all_locked(dev); 184 } 185 EXPORT_SYMBOL(drm_modeset_lock_all); 186 187 /** 188 * drm_modeset_unlock_all - drop all modeset locks 189 * @dev: DRM device 190 * 191 * This function drops all modeset locks taken by a previous call to the 192 * drm_modeset_lock_all() function. 193 * 194 * This function is deprecated. It uses the lock acquisition context stored 195 * in &drm_device.mode_config. This facilitates conversion of existing 196 * code because it removes the need to manually deal with the acquisition 197 * context, but it is also brittle because the context is global and care must 198 * be taken not to nest calls. New code should pass the acquisition context 199 * directly to the drm_modeset_drop_locks() function. 200 */ 201 void drm_modeset_unlock_all(struct drm_device *dev) 202 { 203 struct drm_mode_config *config = &dev->mode_config; 204 struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx; 205 206 if (WARN_ON(!ctx)) 207 return; 208 209 config->acquire_ctx = NULL; 210 drm_modeset_drop_locks(ctx); 211 drm_modeset_acquire_fini(ctx); 212 213 kfree(ctx); 214 215 mutex_unlock(&dev->mode_config.mutex); 216 } 217 EXPORT_SYMBOL(drm_modeset_unlock_all); 218 219 /** 220 * drm_warn_on_modeset_not_all_locked - check that all modeset locks are locked 221 * @dev: device 222 * 223 * Useful as a debug assert. 224 */ 225 void drm_warn_on_modeset_not_all_locked(struct drm_device *dev) 226 { 227 struct drm_crtc *crtc; 228 229 /* Locking is currently fubar in the panic handler. */ 230 if (oops_in_progress) 231 return; 232 233 drm_for_each_crtc(crtc, dev) 234 WARN_ON(!drm_modeset_is_locked(&crtc->mutex)); 235 236 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex)); 237 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex)); 238 } 239 EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked); 240 241 /** 242 * drm_modeset_acquire_init - initialize acquire context 243 * @ctx: the acquire context 244 * @flags: 0 or %DRM_MODESET_ACQUIRE_INTERRUPTIBLE 245 * 246 * When passing %DRM_MODESET_ACQUIRE_INTERRUPTIBLE to @flags, 247 * all calls to drm_modeset_lock() will perform an interruptible 248 * wait. 249 */ 250 void drm_modeset_acquire_init(struct drm_modeset_acquire_ctx *ctx, 251 uint32_t flags) 252 { 253 memset(ctx, 0, sizeof(*ctx)); 254 ww_acquire_init(&ctx->ww_ctx, &crtc_ww_class); 255 INIT_LIST_HEAD(&ctx->locked); 256 257 if (flags & DRM_MODESET_ACQUIRE_INTERRUPTIBLE) 258 ctx->interruptible = true; 259 } 260 EXPORT_SYMBOL(drm_modeset_acquire_init); 261 262 /** 263 * drm_modeset_acquire_fini - cleanup acquire context 264 * @ctx: the acquire context 265 */ 266 void drm_modeset_acquire_fini(struct drm_modeset_acquire_ctx *ctx) 267 { 268 ww_acquire_fini(&ctx->ww_ctx); 269 } 270 EXPORT_SYMBOL(drm_modeset_acquire_fini); 271 272 /** 273 * drm_modeset_drop_locks - drop all locks 274 * @ctx: the acquire context 275 * 276 * Drop all locks currently held against this acquire context. 277 */ 278 void drm_modeset_drop_locks(struct drm_modeset_acquire_ctx *ctx) 279 { 280 if (WARN_ON(ctx->contended)) 281 __drm_stack_depot_print(ctx->stack_depot); 282 283 while (!list_empty(&ctx->locked)) { 284 struct drm_modeset_lock *lock; 285 286 lock = list_first_entry(&ctx->locked, 287 struct drm_modeset_lock, head); 288 289 drm_modeset_unlock(lock); 290 } 291 } 292 EXPORT_SYMBOL(drm_modeset_drop_locks); 293 294 static inline int modeset_lock(struct drm_modeset_lock *lock, 295 struct drm_modeset_acquire_ctx *ctx, 296 bool interruptible, bool slow) 297 { 298 int ret; 299 300 if (WARN_ON(ctx->contended)) 301 __drm_stack_depot_print(ctx->stack_depot); 302 303 if (ctx->trylock_only) { 304 lockdep_assert_held(&ctx->ww_ctx); 305 306 if (!ww_mutex_trylock(&lock->mutex, NULL)) 307 return -EBUSY; 308 else 309 return 0; 310 } else if (interruptible && slow) { 311 ret = ww_mutex_lock_slow_interruptible(&lock->mutex, &ctx->ww_ctx); 312 } else if (interruptible) { 313 ret = ww_mutex_lock_interruptible(&lock->mutex, &ctx->ww_ctx); 314 } else if (slow) { 315 ww_mutex_lock_slow(&lock->mutex, &ctx->ww_ctx); 316 ret = 0; 317 } else { 318 ret = ww_mutex_lock(&lock->mutex, &ctx->ww_ctx); 319 } 320 if (!ret) { 321 WARN_ON(!list_empty(&lock->head)); 322 list_add(&lock->head, &ctx->locked); 323 } else if (ret == -EALREADY) { 324 /* we already hold the lock.. this is fine. For atomic 325 * we will need to be able to drm_modeset_lock() things 326 * without having to keep track of what is already locked 327 * or not. 328 */ 329 ret = 0; 330 } else if (ret == -EDEADLK) { 331 ctx->contended = lock; 332 ctx->stack_depot = __drm_stack_depot_save(); 333 } 334 335 return ret; 336 } 337 338 /** 339 * drm_modeset_backoff - deadlock avoidance backoff 340 * @ctx: the acquire context 341 * 342 * If deadlock is detected (ie. drm_modeset_lock() returns -EDEADLK), 343 * you must call this function to drop all currently held locks and 344 * block until the contended lock becomes available. 345 * 346 * This function returns 0 on success, or -ERESTARTSYS if this context 347 * is initialized with %DRM_MODESET_ACQUIRE_INTERRUPTIBLE and the 348 * wait has been interrupted. 349 */ 350 int drm_modeset_backoff(struct drm_modeset_acquire_ctx *ctx) 351 { 352 struct drm_modeset_lock *contended = ctx->contended; 353 354 ctx->contended = NULL; 355 ctx->stack_depot = 0; 356 357 if (WARN_ON(!contended)) 358 return 0; 359 360 drm_modeset_drop_locks(ctx); 361 362 return modeset_lock(contended, ctx, ctx->interruptible, true); 363 } 364 EXPORT_SYMBOL(drm_modeset_backoff); 365 366 /** 367 * drm_modeset_lock_init - initialize lock 368 * @lock: lock to init 369 */ 370 void drm_modeset_lock_init(struct drm_modeset_lock *lock) 371 { 372 ww_mutex_init(&lock->mutex, &crtc_ww_class); 373 INIT_LIST_HEAD(&lock->head); 374 __drm_stack_depot_init(); 375 } 376 EXPORT_SYMBOL(drm_modeset_lock_init); 377 378 /** 379 * drm_modeset_lock - take modeset lock 380 * @lock: lock to take 381 * @ctx: acquire ctx 382 * 383 * If @ctx is not NULL, then its ww acquire context is used and the 384 * lock will be tracked by the context and can be released by calling 385 * drm_modeset_drop_locks(). If -EDEADLK is returned, this means a 386 * deadlock scenario has been detected and it is an error to attempt 387 * to take any more locks without first calling drm_modeset_backoff(). 388 * 389 * If the @ctx is not NULL and initialized with 390 * %DRM_MODESET_ACQUIRE_INTERRUPTIBLE, this function will fail with 391 * -ERESTARTSYS when interrupted. 392 * 393 * If @ctx is NULL then the function call behaves like a normal, 394 * uninterruptible non-nesting mutex_lock() call. 395 */ 396 int drm_modeset_lock(struct drm_modeset_lock *lock, 397 struct drm_modeset_acquire_ctx *ctx) 398 { 399 if (ctx) 400 return modeset_lock(lock, ctx, ctx->interruptible, false); 401 402 ww_mutex_lock(&lock->mutex, NULL); 403 return 0; 404 } 405 EXPORT_SYMBOL(drm_modeset_lock); 406 407 /** 408 * drm_modeset_lock_single_interruptible - take a single modeset lock 409 * @lock: lock to take 410 * 411 * This function behaves as drm_modeset_lock() with a NULL context, 412 * but performs interruptible waits. 413 * 414 * This function returns 0 on success, or -ERESTARTSYS when interrupted. 415 */ 416 int drm_modeset_lock_single_interruptible(struct drm_modeset_lock *lock) 417 { 418 return ww_mutex_lock_interruptible(&lock->mutex, NULL); 419 } 420 EXPORT_SYMBOL(drm_modeset_lock_single_interruptible); 421 422 /** 423 * drm_modeset_unlock - drop modeset lock 424 * @lock: lock to release 425 */ 426 void drm_modeset_unlock(struct drm_modeset_lock *lock) 427 { 428 list_del_init(&lock->head); 429 ww_mutex_unlock(&lock->mutex); 430 } 431 EXPORT_SYMBOL(drm_modeset_unlock); 432 433 /** 434 * drm_modeset_lock_all_ctx - take all modeset locks 435 * @dev: DRM device 436 * @ctx: lock acquisition context 437 * 438 * This function takes all modeset locks, suitable where a more fine-grained 439 * scheme isn't (yet) implemented. 440 * 441 * Unlike drm_modeset_lock_all(), it doesn't take the &drm_mode_config.mutex 442 * since that lock isn't required for modeset state changes. Callers which 443 * need to grab that lock too need to do so outside of the acquire context 444 * @ctx. 445 * 446 * Locks acquired with this function should be released by calling the 447 * drm_modeset_drop_locks() function on @ctx. 448 * 449 * See also: DRM_MODESET_LOCK_ALL_BEGIN() and DRM_MODESET_LOCK_ALL_END() 450 * 451 * Returns: 0 on success or a negative error-code on failure. 452 */ 453 int drm_modeset_lock_all_ctx(struct drm_device *dev, 454 struct drm_modeset_acquire_ctx *ctx) 455 { 456 struct drm_private_obj *privobj; 457 struct drm_crtc *crtc; 458 struct drm_plane *plane; 459 int ret; 460 461 ret = drm_modeset_lock(&dev->mode_config.connection_mutex, ctx); 462 if (ret) 463 return ret; 464 465 drm_for_each_crtc(crtc, dev) { 466 ret = drm_modeset_lock(&crtc->mutex, ctx); 467 if (ret) 468 return ret; 469 } 470 471 drm_for_each_plane(plane, dev) { 472 ret = drm_modeset_lock(&plane->mutex, ctx); 473 if (ret) 474 return ret; 475 } 476 477 drm_for_each_privobj(privobj, dev) { 478 ret = drm_modeset_lock(&privobj->lock, ctx); 479 if (ret) 480 return ret; 481 } 482 483 return 0; 484 } 485 EXPORT_SYMBOL(drm_modeset_lock_all_ctx); 486