Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright (C) 2014 Red Hat
0003  * Author: Rob Clark <robdclark@gmail.com>
0004  *
0005  * Permission is hereby granted, free of charge, to any person obtaining a
0006  * copy of this software and associated documentation files (the "Software"),
0007  * to deal in the Software without restriction, including without limitation
0008  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
0009  * and/or sell copies of the Software, and to permit persons to whom the
0010  * Software is furnished to do so, subject to the following conditions:
0011  *
0012  * The above copyright notice and this permission notice shall be included in
0013  * all copies or substantial portions of the Software.
0014  *
0015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0016  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0017  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
0018  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
0019  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
0020  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
0021  * OTHER DEALINGS IN THE SOFTWARE.
0022  */
0023 
0024 #include <drm/drm_atomic.h>
0025 #include <drm/drm_crtc.h>
0026 #include <drm/drm_device.h>
0027 #include <drm/drm_modeset_lock.h>
0028 #include <drm/drm_print.h>
0029 
0030 /**
0031  * DOC: kms locking
0032  *
0033  * As KMS moves toward more fine grained locking, and atomic ioctl where
0034  * userspace can indirectly control locking order, it becomes necessary
0035  * to use &ww_mutex and acquire-contexts to avoid deadlocks.  But because
0036  * the locking is more distributed around the driver code, we want a bit
0037  * of extra utility/tracking out of our acquire-ctx.  This is provided
0038  * by &struct drm_modeset_lock and &struct drm_modeset_acquire_ctx.
0039  *
0040  * For basic principles of &ww_mutex, see: Documentation/locking/ww-mutex-design.rst
0041  *
0042  * The basic usage pattern is to::
0043  *
0044  *     drm_modeset_acquire_init(ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE)
0045  *     retry:
0046  *     foreach (lock in random_ordered_set_of_locks) {
0047  *         ret = drm_modeset_lock(lock, ctx)
0048  *         if (ret == -EDEADLK) {
0049  *             ret = drm_modeset_backoff(ctx);
0050  *             if (!ret)
0051  *                 goto retry;
0052  *         }
0053  *         if (ret)
0054  *             goto out;
0055  *     }
0056  *     ... do stuff ...
0057  *     out:
0058  *     drm_modeset_drop_locks(ctx);
0059  *     drm_modeset_acquire_fini(ctx);
0060  *
0061  * For convenience this control flow is implemented in
0062  * DRM_MODESET_LOCK_ALL_BEGIN() and DRM_MODESET_LOCK_ALL_END() for the case
0063  * where all modeset locks need to be taken through drm_modeset_lock_all_ctx().
0064  *
0065  * If all that is needed is a single modeset lock, then the &struct
0066  * drm_modeset_acquire_ctx is not needed and the locking can be simplified
0067  * by passing a NULL instead of ctx in the drm_modeset_lock() call or
0068  * calling  drm_modeset_lock_single_interruptible(). To unlock afterwards
0069  * call drm_modeset_unlock().
0070  *
0071  * On top of these per-object locks using &ww_mutex there's also an overall
0072  * &drm_mode_config.mutex, for protecting everything else. Mostly this means
0073  * probe state of connectors, and preventing hotplug add/removal of connectors.
0074  *
0075  * Finally there's a bunch of dedicated locks to protect drm core internal
0076  * lists and lookup data structures.
0077  */
0078 
0079 static DEFINE_WW_CLASS(crtc_ww_class);
0080 
0081 #if IS_ENABLED(CONFIG_DRM_DEBUG_MODESET_LOCK)
0082 static noinline depot_stack_handle_t __drm_stack_depot_save(void)
0083 {
0084     unsigned long entries[8];
0085     unsigned int n;
0086 
0087     n = stack_trace_save(entries, ARRAY_SIZE(entries), 1);
0088 
0089     return stack_depot_save(entries, n, GFP_NOWAIT | __GFP_NOWARN);
0090 }
0091 
0092 static void __drm_stack_depot_print(depot_stack_handle_t stack_depot)
0093 {
0094     struct drm_printer p = drm_debug_printer("drm_modeset_lock");
0095     unsigned long *entries;
0096     unsigned int nr_entries;
0097     char *buf;
0098 
0099     buf = kmalloc(PAGE_SIZE, GFP_NOWAIT | __GFP_NOWARN);
0100     if (!buf)
0101         return;
0102 
0103     nr_entries = stack_depot_fetch(stack_depot, &entries);
0104     stack_trace_snprint(buf, PAGE_SIZE, entries, nr_entries, 2);
0105 
0106     drm_printf(&p, "attempting to lock a contended lock without backoff:\n%s", buf);
0107 
0108     kfree(buf);
0109 }
0110 
0111 static void __drm_stack_depot_init(void)
0112 {
0113     stack_depot_init();
0114 }
0115 #else /* CONFIG_DRM_DEBUG_MODESET_LOCK */
0116 static depot_stack_handle_t __drm_stack_depot_save(void)
0117 {
0118     return 0;
0119 }
0120 static void __drm_stack_depot_print(depot_stack_handle_t stack_depot)
0121 {
0122 }
0123 static void __drm_stack_depot_init(void)
0124 {
0125 }
0126 #endif /* CONFIG_DRM_DEBUG_MODESET_LOCK */
0127 
0128 /**
0129  * drm_modeset_lock_all - take all modeset locks
0130  * @dev: DRM device
0131  *
0132  * This function takes all modeset locks, suitable where a more fine-grained
0133  * scheme isn't (yet) implemented. Locks must be dropped by calling the
0134  * drm_modeset_unlock_all() function.
0135  *
0136  * This function is deprecated. It allocates a lock acquisition context and
0137  * stores it in &drm_device.mode_config. This facilitate conversion of
0138  * existing code because it removes the need to manually deal with the
0139  * acquisition context, but it is also brittle because the context is global
0140  * and care must be taken not to nest calls. New code should use the
0141  * drm_modeset_lock_all_ctx() function and pass in the context explicitly.
0142  */
0143 void drm_modeset_lock_all(struct drm_device *dev)
0144 {
0145     struct drm_mode_config *config = &dev->mode_config;
0146     struct drm_modeset_acquire_ctx *ctx;
0147     int ret;
0148 
0149     ctx = kzalloc(sizeof(*ctx), GFP_KERNEL | __GFP_NOFAIL);
0150     if (WARN_ON(!ctx))
0151         return;
0152 
0153     mutex_lock(&config->mutex);
0154 
0155     drm_modeset_acquire_init(ctx, 0);
0156 
0157 retry:
0158     ret = drm_modeset_lock_all_ctx(dev, ctx);
0159     if (ret < 0) {
0160         if (ret == -EDEADLK) {
0161             drm_modeset_backoff(ctx);
0162             goto retry;
0163         }
0164 
0165         drm_modeset_acquire_fini(ctx);
0166         kfree(ctx);
0167         return;
0168     }
0169     ww_acquire_done(&ctx->ww_ctx);
0170 
0171     WARN_ON(config->acquire_ctx);
0172 
0173     /*
0174      * We hold the locks now, so it is safe to stash the acquisition
0175      * context for drm_modeset_unlock_all().
0176      */
0177     config->acquire_ctx = ctx;
0178 
0179     drm_warn_on_modeset_not_all_locked(dev);
0180 }
0181 EXPORT_SYMBOL(drm_modeset_lock_all);
0182 
0183 /**
0184  * drm_modeset_unlock_all - drop all modeset locks
0185  * @dev: DRM device
0186  *
0187  * This function drops all modeset locks taken by a previous call to the
0188  * drm_modeset_lock_all() function.
0189  *
0190  * This function is deprecated. It uses the lock acquisition context stored
0191  * in &drm_device.mode_config. This facilitates conversion of existing
0192  * code because it removes the need to manually deal with the acquisition
0193  * context, but it is also brittle because the context is global and care must
0194  * be taken not to nest calls. New code should pass the acquisition context
0195  * directly to the drm_modeset_drop_locks() function.
0196  */
0197 void drm_modeset_unlock_all(struct drm_device *dev)
0198 {
0199     struct drm_mode_config *config = &dev->mode_config;
0200     struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
0201 
0202     if (WARN_ON(!ctx))
0203         return;
0204 
0205     config->acquire_ctx = NULL;
0206     drm_modeset_drop_locks(ctx);
0207     drm_modeset_acquire_fini(ctx);
0208 
0209     kfree(ctx);
0210 
0211     mutex_unlock(&dev->mode_config.mutex);
0212 }
0213 EXPORT_SYMBOL(drm_modeset_unlock_all);
0214 
0215 /**
0216  * drm_warn_on_modeset_not_all_locked - check that all modeset locks are locked
0217  * @dev: device
0218  *
0219  * Useful as a debug assert.
0220  */
0221 void drm_warn_on_modeset_not_all_locked(struct drm_device *dev)
0222 {
0223     struct drm_crtc *crtc;
0224 
0225     /* Locking is currently fubar in the panic handler. */
0226     if (oops_in_progress)
0227         return;
0228 
0229     drm_for_each_crtc(crtc, dev)
0230         WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
0231 
0232     WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
0233     WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
0234 }
0235 EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked);
0236 
0237 /**
0238  * drm_modeset_acquire_init - initialize acquire context
0239  * @ctx: the acquire context
0240  * @flags: 0 or %DRM_MODESET_ACQUIRE_INTERRUPTIBLE
0241  *
0242  * When passing %DRM_MODESET_ACQUIRE_INTERRUPTIBLE to @flags,
0243  * all calls to drm_modeset_lock() will perform an interruptible
0244  * wait.
0245  */
0246 void drm_modeset_acquire_init(struct drm_modeset_acquire_ctx *ctx,
0247         uint32_t flags)
0248 {
0249     memset(ctx, 0, sizeof(*ctx));
0250     ww_acquire_init(&ctx->ww_ctx, &crtc_ww_class);
0251     INIT_LIST_HEAD(&ctx->locked);
0252 
0253     if (flags & DRM_MODESET_ACQUIRE_INTERRUPTIBLE)
0254         ctx->interruptible = true;
0255 }
0256 EXPORT_SYMBOL(drm_modeset_acquire_init);
0257 
0258 /**
0259  * drm_modeset_acquire_fini - cleanup acquire context
0260  * @ctx: the acquire context
0261  */
0262 void drm_modeset_acquire_fini(struct drm_modeset_acquire_ctx *ctx)
0263 {
0264     ww_acquire_fini(&ctx->ww_ctx);
0265 }
0266 EXPORT_SYMBOL(drm_modeset_acquire_fini);
0267 
0268 /**
0269  * drm_modeset_drop_locks - drop all locks
0270  * @ctx: the acquire context
0271  *
0272  * Drop all locks currently held against this acquire context.
0273  */
0274 void drm_modeset_drop_locks(struct drm_modeset_acquire_ctx *ctx)
0275 {
0276     if (WARN_ON(ctx->contended))
0277         __drm_stack_depot_print(ctx->stack_depot);
0278 
0279     while (!list_empty(&ctx->locked)) {
0280         struct drm_modeset_lock *lock;
0281 
0282         lock = list_first_entry(&ctx->locked,
0283                 struct drm_modeset_lock, head);
0284 
0285         drm_modeset_unlock(lock);
0286     }
0287 }
0288 EXPORT_SYMBOL(drm_modeset_drop_locks);
0289 
0290 static inline int modeset_lock(struct drm_modeset_lock *lock,
0291         struct drm_modeset_acquire_ctx *ctx,
0292         bool interruptible, bool slow)
0293 {
0294     int ret;
0295 
0296     if (WARN_ON(ctx->contended))
0297         __drm_stack_depot_print(ctx->stack_depot);
0298 
0299     if (ctx->trylock_only) {
0300         lockdep_assert_held(&ctx->ww_ctx);
0301 
0302         if (!ww_mutex_trylock(&lock->mutex, NULL))
0303             return -EBUSY;
0304         else
0305             return 0;
0306     } else if (interruptible && slow) {
0307         ret = ww_mutex_lock_slow_interruptible(&lock->mutex, &ctx->ww_ctx);
0308     } else if (interruptible) {
0309         ret = ww_mutex_lock_interruptible(&lock->mutex, &ctx->ww_ctx);
0310     } else if (slow) {
0311         ww_mutex_lock_slow(&lock->mutex, &ctx->ww_ctx);
0312         ret = 0;
0313     } else {
0314         ret = ww_mutex_lock(&lock->mutex, &ctx->ww_ctx);
0315     }
0316     if (!ret) {
0317         WARN_ON(!list_empty(&lock->head));
0318         list_add(&lock->head, &ctx->locked);
0319     } else if (ret == -EALREADY) {
0320         /* we already hold the lock.. this is fine.  For atomic
0321          * we will need to be able to drm_modeset_lock() things
0322          * without having to keep track of what is already locked
0323          * or not.
0324          */
0325         ret = 0;
0326     } else if (ret == -EDEADLK) {
0327         ctx->contended = lock;
0328         ctx->stack_depot = __drm_stack_depot_save();
0329     }
0330 
0331     return ret;
0332 }
0333 
0334 /**
0335  * drm_modeset_backoff - deadlock avoidance backoff
0336  * @ctx: the acquire context
0337  *
0338  * If deadlock is detected (ie. drm_modeset_lock() returns -EDEADLK),
0339  * you must call this function to drop all currently held locks and
0340  * block until the contended lock becomes available.
0341  *
0342  * This function returns 0 on success, or -ERESTARTSYS if this context
0343  * is initialized with %DRM_MODESET_ACQUIRE_INTERRUPTIBLE and the
0344  * wait has been interrupted.
0345  */
0346 int drm_modeset_backoff(struct drm_modeset_acquire_ctx *ctx)
0347 {
0348     struct drm_modeset_lock *contended = ctx->contended;
0349 
0350     ctx->contended = NULL;
0351     ctx->stack_depot = 0;
0352 
0353     if (WARN_ON(!contended))
0354         return 0;
0355 
0356     drm_modeset_drop_locks(ctx);
0357 
0358     return modeset_lock(contended, ctx, ctx->interruptible, true);
0359 }
0360 EXPORT_SYMBOL(drm_modeset_backoff);
0361 
0362 /**
0363  * drm_modeset_lock_init - initialize lock
0364  * @lock: lock to init
0365  */
0366 void drm_modeset_lock_init(struct drm_modeset_lock *lock)
0367 {
0368     ww_mutex_init(&lock->mutex, &crtc_ww_class);
0369     INIT_LIST_HEAD(&lock->head);
0370     __drm_stack_depot_init();
0371 }
0372 EXPORT_SYMBOL(drm_modeset_lock_init);
0373 
0374 /**
0375  * drm_modeset_lock - take modeset lock
0376  * @lock: lock to take
0377  * @ctx: acquire ctx
0378  *
0379  * If @ctx is not NULL, then its ww acquire context is used and the
0380  * lock will be tracked by the context and can be released by calling
0381  * drm_modeset_drop_locks().  If -EDEADLK is returned, this means a
0382  * deadlock scenario has been detected and it is an error to attempt
0383  * to take any more locks without first calling drm_modeset_backoff().
0384  *
0385  * If the @ctx is not NULL and initialized with
0386  * %DRM_MODESET_ACQUIRE_INTERRUPTIBLE, this function will fail with
0387  * -ERESTARTSYS when interrupted.
0388  *
0389  * If @ctx is NULL then the function call behaves like a normal,
0390  * uninterruptible non-nesting mutex_lock() call.
0391  */
0392 int drm_modeset_lock(struct drm_modeset_lock *lock,
0393         struct drm_modeset_acquire_ctx *ctx)
0394 {
0395     if (ctx)
0396         return modeset_lock(lock, ctx, ctx->interruptible, false);
0397 
0398     ww_mutex_lock(&lock->mutex, NULL);
0399     return 0;
0400 }
0401 EXPORT_SYMBOL(drm_modeset_lock);
0402 
0403 /**
0404  * drm_modeset_lock_single_interruptible - take a single modeset lock
0405  * @lock: lock to take
0406  *
0407  * This function behaves as drm_modeset_lock() with a NULL context,
0408  * but performs interruptible waits.
0409  *
0410  * This function returns 0 on success, or -ERESTARTSYS when interrupted.
0411  */
0412 int drm_modeset_lock_single_interruptible(struct drm_modeset_lock *lock)
0413 {
0414     return ww_mutex_lock_interruptible(&lock->mutex, NULL);
0415 }
0416 EXPORT_SYMBOL(drm_modeset_lock_single_interruptible);
0417 
0418 /**
0419  * drm_modeset_unlock - drop modeset lock
0420  * @lock: lock to release
0421  */
0422 void drm_modeset_unlock(struct drm_modeset_lock *lock)
0423 {
0424     list_del_init(&lock->head);
0425     ww_mutex_unlock(&lock->mutex);
0426 }
0427 EXPORT_SYMBOL(drm_modeset_unlock);
0428 
0429 /**
0430  * drm_modeset_lock_all_ctx - take all modeset locks
0431  * @dev: DRM device
0432  * @ctx: lock acquisition context
0433  *
0434  * This function takes all modeset locks, suitable where a more fine-grained
0435  * scheme isn't (yet) implemented.
0436  *
0437  * Unlike drm_modeset_lock_all(), it doesn't take the &drm_mode_config.mutex
0438  * since that lock isn't required for modeset state changes. Callers which
0439  * need to grab that lock too need to do so outside of the acquire context
0440  * @ctx.
0441  *
0442  * Locks acquired with this function should be released by calling the
0443  * drm_modeset_drop_locks() function on @ctx.
0444  *
0445  * See also: DRM_MODESET_LOCK_ALL_BEGIN() and DRM_MODESET_LOCK_ALL_END()
0446  *
0447  * Returns: 0 on success or a negative error-code on failure.
0448  */
0449 int drm_modeset_lock_all_ctx(struct drm_device *dev,
0450                  struct drm_modeset_acquire_ctx *ctx)
0451 {
0452     struct drm_private_obj *privobj;
0453     struct drm_crtc *crtc;
0454     struct drm_plane *plane;
0455     int ret;
0456 
0457     ret = drm_modeset_lock(&dev->mode_config.connection_mutex, ctx);
0458     if (ret)
0459         return ret;
0460 
0461     drm_for_each_crtc(crtc, dev) {
0462         ret = drm_modeset_lock(&crtc->mutex, ctx);
0463         if (ret)
0464             return ret;
0465     }
0466 
0467     drm_for_each_plane(plane, dev) {
0468         ret = drm_modeset_lock(&plane->mutex, ctx);
0469         if (ret)
0470             return ret;
0471     }
0472 
0473     drm_for_each_privobj(privobj, dev) {
0474         ret = drm_modeset_lock(&privobj->lock, ctx);
0475         if (ret)
0476             return ret;
0477     }
0478 
0479     return 0;
0480 }
0481 EXPORT_SYMBOL(drm_modeset_lock_all_ctx);