2 * Copyright (C) 2014 Red Hat
3 * Author: Rob Clark <robdclark@gmail.com>
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
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>
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.
40 * For basic principles of &ww_mutex, see: Documentation/locking/ww-mutex-design.rst
42 * The basic usage pattern is to::
44 * drm_modeset_acquire_init(ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE)
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);
58 * drm_modeset_drop_locks(ctx);
59 * drm_modeset_acquire_fini(ctx);
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().
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().
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.
75 * Finally there's a bunch of dedicated locks to protect drm core internal
76 * lists and lookup data structures.
79 static DEFINE_WW_CLASS(crtc_ww_class);
81 #if IS_ENABLED(CONFIG_DRM_DEBUG_MODESET_LOCK)
82 static noinline depot_stack_handle_t __drm_stack_depot_save(void)
84 unsigned long entries[8];
87 n = stack_trace_save(entries, ARRAY_SIZE(entries), 1);
89 return stack_depot_save(entries, n, GFP_NOWAIT | __GFP_NOWARN);
92 static void __drm_stack_depot_print(depot_stack_handle_t stack_depot)
94 struct drm_printer p = drm_debug_printer("drm_modeset_lock");
95 unsigned long *entries;
96 unsigned int nr_entries;
99 buf = kmalloc(PAGE_SIZE, GFP_NOWAIT | __GFP_NOWARN);
103 nr_entries = stack_depot_fetch(stack_depot, &entries);
104 stack_trace_snprint(buf, PAGE_SIZE, entries, nr_entries, 2);
106 drm_printf(&p, "attempting to lock a contended lock without backoff:\n%s", buf);
110 #else /* CONFIG_DRM_DEBUG_MODESET_LOCK */
111 static depot_stack_handle_t __drm_stack_depot_save(void)
115 static void __drm_stack_depot_print(depot_stack_handle_t stack_depot)
118 #endif /* CONFIG_DRM_DEBUG_MODESET_LOCK */
121 * drm_modeset_lock_all - take all modeset locks
124 * This function takes all modeset locks, suitable where a more fine-grained
125 * scheme isn't (yet) implemented. Locks must be dropped by calling the
126 * drm_modeset_unlock_all() function.
128 * This function is deprecated. It allocates a lock acquisition context and
129 * stores it in &drm_device.mode_config. This facilitate conversion of
130 * existing code because it removes the need to manually deal with the
131 * acquisition context, but it is also brittle because the context is global
132 * and care must be taken not to nest calls. New code should use the
133 * drm_modeset_lock_all_ctx() function and pass in the context explicitly.
135 void drm_modeset_lock_all(struct drm_device *dev)
137 struct drm_mode_config *config = &dev->mode_config;
138 struct drm_modeset_acquire_ctx *ctx;
141 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL | __GFP_NOFAIL);
145 mutex_lock(&config->mutex);
147 drm_modeset_acquire_init(ctx, 0);
150 ret = drm_modeset_lock_all_ctx(dev, ctx);
152 if (ret == -EDEADLK) {
153 drm_modeset_backoff(ctx);
157 drm_modeset_acquire_fini(ctx);
161 ww_acquire_done(&ctx->ww_ctx);
163 WARN_ON(config->acquire_ctx);
166 * We hold the locks now, so it is safe to stash the acquisition
167 * context for drm_modeset_unlock_all().
169 config->acquire_ctx = ctx;
171 drm_warn_on_modeset_not_all_locked(dev);
173 EXPORT_SYMBOL(drm_modeset_lock_all);
176 * drm_modeset_unlock_all - drop all modeset locks
179 * This function drops all modeset locks taken by a previous call to the
180 * drm_modeset_lock_all() function.
182 * This function is deprecated. It uses the lock acquisition context stored
183 * in &drm_device.mode_config. This facilitates conversion of existing
184 * code because it removes the need to manually deal with the acquisition
185 * context, but it is also brittle because the context is global and care must
186 * be taken not to nest calls. New code should pass the acquisition context
187 * directly to the drm_modeset_drop_locks() function.
189 void drm_modeset_unlock_all(struct drm_device *dev)
191 struct drm_mode_config *config = &dev->mode_config;
192 struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
197 config->acquire_ctx = NULL;
198 drm_modeset_drop_locks(ctx);
199 drm_modeset_acquire_fini(ctx);
203 mutex_unlock(&dev->mode_config.mutex);
205 EXPORT_SYMBOL(drm_modeset_unlock_all);
208 * drm_warn_on_modeset_not_all_locked - check that all modeset locks are locked
211 * Useful as a debug assert.
213 void drm_warn_on_modeset_not_all_locked(struct drm_device *dev)
215 struct drm_crtc *crtc;
217 /* Locking is currently fubar in the panic handler. */
218 if (oops_in_progress)
221 drm_for_each_crtc(crtc, dev)
222 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
224 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
225 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
227 EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked);
230 * drm_modeset_acquire_init - initialize acquire context
231 * @ctx: the acquire context
232 * @flags: 0 or %DRM_MODESET_ACQUIRE_INTERRUPTIBLE
234 * When passing %DRM_MODESET_ACQUIRE_INTERRUPTIBLE to @flags,
235 * all calls to drm_modeset_lock() will perform an interruptible
238 void drm_modeset_acquire_init(struct drm_modeset_acquire_ctx *ctx,
241 memset(ctx, 0, sizeof(*ctx));
242 ww_acquire_init(&ctx->ww_ctx, &crtc_ww_class);
243 INIT_LIST_HEAD(&ctx->locked);
245 if (flags & DRM_MODESET_ACQUIRE_INTERRUPTIBLE)
246 ctx->interruptible = true;
248 EXPORT_SYMBOL(drm_modeset_acquire_init);
251 * drm_modeset_acquire_fini - cleanup acquire context
252 * @ctx: the acquire context
254 void drm_modeset_acquire_fini(struct drm_modeset_acquire_ctx *ctx)
256 ww_acquire_fini(&ctx->ww_ctx);
258 EXPORT_SYMBOL(drm_modeset_acquire_fini);
261 * drm_modeset_drop_locks - drop all locks
262 * @ctx: the acquire context
264 * Drop all locks currently held against this acquire context.
266 void drm_modeset_drop_locks(struct drm_modeset_acquire_ctx *ctx)
268 if (WARN_ON(ctx->contended))
269 __drm_stack_depot_print(ctx->stack_depot);
271 while (!list_empty(&ctx->locked)) {
272 struct drm_modeset_lock *lock;
274 lock = list_first_entry(&ctx->locked,
275 struct drm_modeset_lock, head);
277 drm_modeset_unlock(lock);
280 EXPORT_SYMBOL(drm_modeset_drop_locks);
282 static inline int modeset_lock(struct drm_modeset_lock *lock,
283 struct drm_modeset_acquire_ctx *ctx,
284 bool interruptible, bool slow)
288 if (WARN_ON(ctx->contended))
289 __drm_stack_depot_print(ctx->stack_depot);
291 if (ctx->trylock_only) {
292 lockdep_assert_held(&ctx->ww_ctx);
294 if (!ww_mutex_trylock(&lock->mutex, NULL))
298 } else if (interruptible && slow) {
299 ret = ww_mutex_lock_slow_interruptible(&lock->mutex, &ctx->ww_ctx);
300 } else if (interruptible) {
301 ret = ww_mutex_lock_interruptible(&lock->mutex, &ctx->ww_ctx);
303 ww_mutex_lock_slow(&lock->mutex, &ctx->ww_ctx);
306 ret = ww_mutex_lock(&lock->mutex, &ctx->ww_ctx);
309 WARN_ON(!list_empty(&lock->head));
310 list_add(&lock->head, &ctx->locked);
311 } else if (ret == -EALREADY) {
312 /* we already hold the lock.. this is fine. For atomic
313 * we will need to be able to drm_modeset_lock() things
314 * without having to keep track of what is already locked
318 } else if (ret == -EDEADLK) {
319 ctx->contended = lock;
320 ctx->stack_depot = __drm_stack_depot_save();
327 * drm_modeset_backoff - deadlock avoidance backoff
328 * @ctx: the acquire context
330 * If deadlock is detected (ie. drm_modeset_lock() returns -EDEADLK),
331 * you must call this function to drop all currently held locks and
332 * block until the contended lock becomes available.
334 * This function returns 0 on success, or -ERESTARTSYS if this context
335 * is initialized with %DRM_MODESET_ACQUIRE_INTERRUPTIBLE and the
336 * wait has been interrupted.
338 int drm_modeset_backoff(struct drm_modeset_acquire_ctx *ctx)
340 struct drm_modeset_lock *contended = ctx->contended;
342 ctx->contended = NULL;
343 ctx->stack_depot = 0;
345 if (WARN_ON(!contended))
348 drm_modeset_drop_locks(ctx);
350 return modeset_lock(contended, ctx, ctx->interruptible, true);
352 EXPORT_SYMBOL(drm_modeset_backoff);
355 * drm_modeset_lock_init - initialize lock
356 * @lock: lock to init
358 void drm_modeset_lock_init(struct drm_modeset_lock *lock)
360 ww_mutex_init(&lock->mutex, &crtc_ww_class);
361 INIT_LIST_HEAD(&lock->head);
363 EXPORT_SYMBOL(drm_modeset_lock_init);
366 * drm_modeset_lock - take modeset lock
367 * @lock: lock to take
370 * If @ctx is not NULL, then its ww acquire context is used and the
371 * lock will be tracked by the context and can be released by calling
372 * drm_modeset_drop_locks(). If -EDEADLK is returned, this means a
373 * deadlock scenario has been detected and it is an error to attempt
374 * to take any more locks without first calling drm_modeset_backoff().
376 * If the @ctx is not NULL and initialized with
377 * %DRM_MODESET_ACQUIRE_INTERRUPTIBLE, this function will fail with
378 * -ERESTARTSYS when interrupted.
380 * If @ctx is NULL then the function call behaves like a normal,
381 * uninterruptible non-nesting mutex_lock() call.
383 int drm_modeset_lock(struct drm_modeset_lock *lock,
384 struct drm_modeset_acquire_ctx *ctx)
387 return modeset_lock(lock, ctx, ctx->interruptible, false);
389 ww_mutex_lock(&lock->mutex, NULL);
392 EXPORT_SYMBOL(drm_modeset_lock);
395 * drm_modeset_lock_single_interruptible - take a single modeset lock
396 * @lock: lock to take
398 * This function behaves as drm_modeset_lock() with a NULL context,
399 * but performs interruptible waits.
401 * This function returns 0 on success, or -ERESTARTSYS when interrupted.
403 int drm_modeset_lock_single_interruptible(struct drm_modeset_lock *lock)
405 return ww_mutex_lock_interruptible(&lock->mutex, NULL);
407 EXPORT_SYMBOL(drm_modeset_lock_single_interruptible);
410 * drm_modeset_unlock - drop modeset lock
411 * @lock: lock to release
413 void drm_modeset_unlock(struct drm_modeset_lock *lock)
415 list_del_init(&lock->head);
416 ww_mutex_unlock(&lock->mutex);
418 EXPORT_SYMBOL(drm_modeset_unlock);
421 * drm_modeset_lock_all_ctx - take all modeset locks
423 * @ctx: lock acquisition context
425 * This function takes all modeset locks, suitable where a more fine-grained
426 * scheme isn't (yet) implemented.
428 * Unlike drm_modeset_lock_all(), it doesn't take the &drm_mode_config.mutex
429 * since that lock isn't required for modeset state changes. Callers which
430 * need to grab that lock too need to do so outside of the acquire context
433 * Locks acquired with this function should be released by calling the
434 * drm_modeset_drop_locks() function on @ctx.
436 * See also: DRM_MODESET_LOCK_ALL_BEGIN() and DRM_MODESET_LOCK_ALL_END()
438 * Returns: 0 on success or a negative error-code on failure.
440 int drm_modeset_lock_all_ctx(struct drm_device *dev,
441 struct drm_modeset_acquire_ctx *ctx)
443 struct drm_private_obj *privobj;
444 struct drm_crtc *crtc;
445 struct drm_plane *plane;
448 ret = drm_modeset_lock(&dev->mode_config.connection_mutex, ctx);
452 drm_for_each_crtc(crtc, dev) {
453 ret = drm_modeset_lock(&crtc->mutex, ctx);
458 drm_for_each_plane(plane, dev) {
459 ret = drm_modeset_lock(&plane->mutex, ctx);
464 drm_for_each_privobj(privobj, dev) {
465 ret = drm_modeset_lock(&privobj->lock, ctx);
472 EXPORT_SYMBOL(drm_modeset_lock_all_ctx);