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.
25 #include <drm/drm_crtc.h>
26 #include <drm/drm_modeset_lock.h>
31 * As KMS moves toward more fine grained locking, and atomic ioctl where
32 * userspace can indirectly control locking order, it becomes necessary
33 * to use ww_mutex and acquire-contexts to avoid deadlocks. But because
34 * the locking is more distributed around the driver code, we want a bit
35 * of extra utility/tracking out of our acquire-ctx. This is provided
36 * by drm_modeset_lock / drm_modeset_acquire_ctx.
38 * For basic principles of ww_mutex, see: Documentation/locking/ww-mutex-design.txt
40 * The basic usage pattern is to:
42 * drm_modeset_acquire_init(&ctx)
44 * foreach (lock in random_ordered_set_of_locks) {
45 * ret = drm_modeset_lock(lock, &ctx)
46 * if (ret == -EDEADLK) {
47 * drm_modeset_backoff(&ctx);
54 * drm_modeset_drop_locks(&ctx);
55 * drm_modeset_acquire_fini(&ctx);
60 * __drm_modeset_lock_all - internal helper to grab all modeset locks
62 * @trylock: trylock mode for atomic contexts
64 * This is a special version of drm_modeset_lock_all() which can also be used in
65 * atomic contexts. Then @trylock must be set to true.
68 * 0 on success or negative error code on failure.
70 int __drm_modeset_lock_all(struct drm_device
*dev
,
73 struct drm_mode_config
*config
= &dev
->mode_config
;
74 struct drm_modeset_acquire_ctx
*ctx
;
77 ctx
= kzalloc(sizeof(*ctx
),
78 trylock
? GFP_ATOMIC
: GFP_KERNEL
);
83 if (!mutex_trylock(&config
->mutex
))
86 mutex_lock(&config
->mutex
);
89 drm_modeset_acquire_init(ctx
, 0);
90 ctx
->trylock_only
= trylock
;
93 ret
= drm_modeset_lock(&config
->connection_mutex
, ctx
);
96 ret
= drm_modeset_lock_all_crtcs(dev
, ctx
);
100 WARN_ON(config
->acquire_ctx
);
102 /* now we hold the locks, so now that it is safe, stash the
103 * ctx for drm_modeset_unlock_all():
105 config
->acquire_ctx
= ctx
;
107 drm_warn_on_modeset_not_all_locked(dev
);
112 if (ret
== -EDEADLK
) {
113 drm_modeset_backoff(ctx
);
119 EXPORT_SYMBOL(__drm_modeset_lock_all
);
122 * drm_modeset_lock_all - take all modeset locks
125 * This function takes all modeset locks, suitable where a more fine-grained
126 * scheme isn't (yet) implemented. Locks must be dropped with
127 * drm_modeset_unlock_all.
129 void drm_modeset_lock_all(struct drm_device
*dev
)
131 WARN_ON(__drm_modeset_lock_all(dev
, false) != 0);
133 EXPORT_SYMBOL(drm_modeset_lock_all
);
136 * drm_modeset_unlock_all - drop all modeset locks
139 * This function drop all modeset locks taken by drm_modeset_lock_all.
141 void drm_modeset_unlock_all(struct drm_device
*dev
)
143 struct drm_mode_config
*config
= &dev
->mode_config
;
144 struct drm_modeset_acquire_ctx
*ctx
= config
->acquire_ctx
;
149 config
->acquire_ctx
= NULL
;
150 drm_modeset_drop_locks(ctx
);
151 drm_modeset_acquire_fini(ctx
);
155 mutex_unlock(&dev
->mode_config
.mutex
);
157 EXPORT_SYMBOL(drm_modeset_unlock_all
);
160 * drm_modeset_lock_crtc - lock crtc with hidden acquire ctx for a plane update
162 * @plane: DRM plane to be updated on @crtc
164 * This function locks the given crtc and plane (which should be either the
165 * primary or cursor plane) using a hidden acquire context. This is necessary so
166 * that drivers internally using the atomic interfaces can grab further locks
167 * with the lock acquire context.
169 * Note that @plane can be NULL, e.g. when the cursor support hasn't yet been
170 * converted to universal planes yet.
172 void drm_modeset_lock_crtc(struct drm_crtc
*crtc
,
173 struct drm_plane
*plane
)
175 struct drm_modeset_acquire_ctx
*ctx
;
178 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
182 drm_modeset_acquire_init(ctx
, 0);
185 ret
= drm_modeset_lock(&crtc
->mutex
, ctx
);
190 ret
= drm_modeset_lock(&plane
->mutex
, ctx
);
195 ret
= drm_modeset_lock(&plane
->crtc
->mutex
, ctx
);
201 WARN_ON(crtc
->acquire_ctx
);
203 /* now we hold the locks, so now that it is safe, stash the
204 * ctx for drm_modeset_unlock_crtc():
206 crtc
->acquire_ctx
= ctx
;
211 if (ret
== -EDEADLK
) {
212 drm_modeset_backoff(ctx
);
216 EXPORT_SYMBOL(drm_modeset_lock_crtc
);
219 * drm_modeset_legacy_acquire_ctx - find acquire ctx for legacy ioctls
222 * Legacy ioctl operations like cursor updates or page flips only have per-crtc
223 * locking, and store the acquire ctx in the corresponding crtc. All other
224 * legacy operations take all locks and use a global acquire context. This
225 * function grabs the right one.
227 struct drm_modeset_acquire_ctx
*
228 drm_modeset_legacy_acquire_ctx(struct drm_crtc
*crtc
)
230 if (crtc
->acquire_ctx
)
231 return crtc
->acquire_ctx
;
233 WARN_ON(!crtc
->dev
->mode_config
.acquire_ctx
);
235 return crtc
->dev
->mode_config
.acquire_ctx
;
237 EXPORT_SYMBOL(drm_modeset_legacy_acquire_ctx
);
240 * drm_modeset_unlock_crtc - drop crtc lock
243 * This drops the crtc lock acquire with drm_modeset_lock_crtc() and all other
244 * locks acquired through the hidden context.
246 void drm_modeset_unlock_crtc(struct drm_crtc
*crtc
)
248 struct drm_modeset_acquire_ctx
*ctx
= crtc
->acquire_ctx
;
253 crtc
->acquire_ctx
= NULL
;
254 drm_modeset_drop_locks(ctx
);
255 drm_modeset_acquire_fini(ctx
);
259 EXPORT_SYMBOL(drm_modeset_unlock_crtc
);
262 * drm_warn_on_modeset_not_all_locked - check that all modeset locks are locked
265 * Useful as a debug assert.
267 void drm_warn_on_modeset_not_all_locked(struct drm_device
*dev
)
269 struct drm_crtc
*crtc
;
271 /* Locking is currently fubar in the panic handler. */
272 if (oops_in_progress
)
275 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
)
276 WARN_ON(!drm_modeset_is_locked(&crtc
->mutex
));
278 WARN_ON(!drm_modeset_is_locked(&dev
->mode_config
.connection_mutex
));
279 WARN_ON(!mutex_is_locked(&dev
->mode_config
.mutex
));
281 EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked
);
284 * drm_modeset_acquire_init - initialize acquire context
285 * @ctx: the acquire context
288 void drm_modeset_acquire_init(struct drm_modeset_acquire_ctx
*ctx
,
291 memset(ctx
, 0, sizeof(*ctx
));
292 ww_acquire_init(&ctx
->ww_ctx
, &crtc_ww_class
);
293 INIT_LIST_HEAD(&ctx
->locked
);
295 EXPORT_SYMBOL(drm_modeset_acquire_init
);
298 * drm_modeset_acquire_fini - cleanup acquire context
299 * @ctx: the acquire context
301 void drm_modeset_acquire_fini(struct drm_modeset_acquire_ctx
*ctx
)
303 ww_acquire_fini(&ctx
->ww_ctx
);
305 EXPORT_SYMBOL(drm_modeset_acquire_fini
);
308 * drm_modeset_drop_locks - drop all locks
309 * @ctx: the acquire context
311 * Drop all locks currently held against this acquire context.
313 void drm_modeset_drop_locks(struct drm_modeset_acquire_ctx
*ctx
)
315 WARN_ON(ctx
->contended
);
316 while (!list_empty(&ctx
->locked
)) {
317 struct drm_modeset_lock
*lock
;
319 lock
= list_first_entry(&ctx
->locked
,
320 struct drm_modeset_lock
, head
);
322 drm_modeset_unlock(lock
);
325 EXPORT_SYMBOL(drm_modeset_drop_locks
);
327 static inline int modeset_lock(struct drm_modeset_lock
*lock
,
328 struct drm_modeset_acquire_ctx
*ctx
,
329 bool interruptible
, bool slow
)
333 WARN_ON(ctx
->contended
);
335 if (ctx
->trylock_only
) {
336 if (!ww_mutex_trylock(&lock
->mutex
))
340 } else if (interruptible
&& slow
) {
341 ret
= ww_mutex_lock_slow_interruptible(&lock
->mutex
, &ctx
->ww_ctx
);
342 } else if (interruptible
) {
343 ret
= ww_mutex_lock_interruptible(&lock
->mutex
, &ctx
->ww_ctx
);
345 ww_mutex_lock_slow(&lock
->mutex
, &ctx
->ww_ctx
);
348 ret
= ww_mutex_lock(&lock
->mutex
, &ctx
->ww_ctx
);
351 WARN_ON(!list_empty(&lock
->head
));
352 list_add(&lock
->head
, &ctx
->locked
);
353 } else if (ret
== -EALREADY
) {
354 /* we already hold the lock.. this is fine. For atomic
355 * we will need to be able to drm_modeset_lock() things
356 * without having to keep track of what is already locked
360 } else if (ret
== -EDEADLK
) {
361 ctx
->contended
= lock
;
367 static int modeset_backoff(struct drm_modeset_acquire_ctx
*ctx
,
370 struct drm_modeset_lock
*contended
= ctx
->contended
;
372 ctx
->contended
= NULL
;
374 if (WARN_ON(!contended
))
377 drm_modeset_drop_locks(ctx
);
379 return modeset_lock(contended
, ctx
, interruptible
, true);
383 * drm_modeset_backoff - deadlock avoidance backoff
384 * @ctx: the acquire context
386 * If deadlock is detected (ie. drm_modeset_lock() returns -EDEADLK),
387 * you must call this function to drop all currently held locks and
388 * block until the contended lock becomes available.
390 void drm_modeset_backoff(struct drm_modeset_acquire_ctx
*ctx
)
392 modeset_backoff(ctx
, false);
394 EXPORT_SYMBOL(drm_modeset_backoff
);
397 * drm_modeset_backoff_interruptible - deadlock avoidance backoff
398 * @ctx: the acquire context
400 * Interruptible version of drm_modeset_backoff()
402 int drm_modeset_backoff_interruptible(struct drm_modeset_acquire_ctx
*ctx
)
404 return modeset_backoff(ctx
, true);
406 EXPORT_SYMBOL(drm_modeset_backoff_interruptible
);
409 * drm_modeset_lock - take modeset lock
410 * @lock: lock to take
413 * If ctx is not NULL, then its ww acquire context is used and the
414 * lock will be tracked by the context and can be released by calling
415 * drm_modeset_drop_locks(). If -EDEADLK is returned, this means a
416 * deadlock scenario has been detected and it is an error to attempt
417 * to take any more locks without first calling drm_modeset_backoff().
419 int drm_modeset_lock(struct drm_modeset_lock
*lock
,
420 struct drm_modeset_acquire_ctx
*ctx
)
423 return modeset_lock(lock
, ctx
, false, false);
425 ww_mutex_lock(&lock
->mutex
, NULL
);
428 EXPORT_SYMBOL(drm_modeset_lock
);
431 * drm_modeset_lock_interruptible - take modeset lock
432 * @lock: lock to take
435 * Interruptible version of drm_modeset_lock()
437 int drm_modeset_lock_interruptible(struct drm_modeset_lock
*lock
,
438 struct drm_modeset_acquire_ctx
*ctx
)
441 return modeset_lock(lock
, ctx
, true, false);
443 return ww_mutex_lock_interruptible(&lock
->mutex
, NULL
);
445 EXPORT_SYMBOL(drm_modeset_lock_interruptible
);
448 * drm_modeset_unlock - drop modeset lock
449 * @lock: lock to release
451 void drm_modeset_unlock(struct drm_modeset_lock
*lock
)
453 list_del_init(&lock
->head
);
454 ww_mutex_unlock(&lock
->mutex
);
456 EXPORT_SYMBOL(drm_modeset_unlock
);
458 /* In some legacy codepaths it's convenient to just grab all the crtc and plane
460 int drm_modeset_lock_all_crtcs(struct drm_device
*dev
,
461 struct drm_modeset_acquire_ctx
*ctx
)
463 struct drm_mode_config
*config
= &dev
->mode_config
;
464 struct drm_crtc
*crtc
;
465 struct drm_plane
*plane
;
468 list_for_each_entry(crtc
, &config
->crtc_list
, head
) {
469 ret
= drm_modeset_lock(&crtc
->mutex
, ctx
);
474 list_for_each_entry(plane
, &config
->plane_list
, head
) {
475 ret
= drm_modeset_lock(&plane
->mutex
, ctx
);
482 EXPORT_SYMBOL(drm_modeset_lock_all_crtcs
);