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/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 - take all modeset locks
63 * This function takes all modeset locks, suitable where a more fine-grained
64 * scheme isn't (yet) implemented. Locks must be dropped with
65 * drm_modeset_unlock_all.
67 void drm_modeset_lock_all(struct drm_device *dev)
69 struct drm_mode_config *config = &dev->mode_config;
70 struct drm_modeset_acquire_ctx *ctx;
73 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
77 mutex_lock(&config->mutex);
79 drm_modeset_acquire_init(ctx, 0);
82 ret = drm_modeset_lock(&config->connection_mutex, ctx);
85 ret = drm_modeset_lock_all_crtcs(dev, ctx);
89 WARN_ON(config->acquire_ctx);
91 /* now we hold the locks, so now that it is safe, stash the
92 * ctx for drm_modeset_unlock_all():
94 config->acquire_ctx = ctx;
96 drm_warn_on_modeset_not_all_locked(dev);
101 if (ret == -EDEADLK) {
102 drm_modeset_backoff(ctx);
106 EXPORT_SYMBOL(drm_modeset_lock_all);
109 * drm_modeset_unlock_all - drop all modeset locks
112 * This function drop all modeset locks taken by drm_modeset_lock_all.
114 void drm_modeset_unlock_all(struct drm_device *dev)
116 struct drm_mode_config *config = &dev->mode_config;
117 struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
122 config->acquire_ctx = NULL;
123 drm_modeset_drop_locks(ctx);
124 drm_modeset_acquire_fini(ctx);
128 mutex_unlock(&dev->mode_config.mutex);
130 EXPORT_SYMBOL(drm_modeset_unlock_all);
133 * drm_warn_on_modeset_not_all_locked - check that all modeset locks are locked
136 * Useful as a debug assert.
138 void drm_warn_on_modeset_not_all_locked(struct drm_device *dev)
140 struct drm_crtc *crtc;
142 /* Locking is currently fubar in the panic handler. */
143 if (oops_in_progress)
146 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
147 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
149 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
150 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
152 EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked);
155 * drm_modeset_acquire_init - initialize acquire context
156 * @ctx: the acquire context
159 void drm_modeset_acquire_init(struct drm_modeset_acquire_ctx *ctx,
162 memset(ctx, 0, sizeof(*ctx));
163 ww_acquire_init(&ctx->ww_ctx, &crtc_ww_class);
164 INIT_LIST_HEAD(&ctx->locked);
166 EXPORT_SYMBOL(drm_modeset_acquire_init);
169 * drm_modeset_acquire_fini - cleanup acquire context
170 * @ctx: the acquire context
172 void drm_modeset_acquire_fini(struct drm_modeset_acquire_ctx *ctx)
174 ww_acquire_fini(&ctx->ww_ctx);
176 EXPORT_SYMBOL(drm_modeset_acquire_fini);
179 * drm_modeset_drop_locks - drop all locks
180 * @ctx: the acquire context
182 * Drop all locks currently held against this acquire context.
184 void drm_modeset_drop_locks(struct drm_modeset_acquire_ctx *ctx)
186 WARN_ON(ctx->contended);
187 while (!list_empty(&ctx->locked)) {
188 struct drm_modeset_lock *lock;
190 lock = list_first_entry(&ctx->locked,
191 struct drm_modeset_lock, head);
193 drm_modeset_unlock(lock);
196 EXPORT_SYMBOL(drm_modeset_drop_locks);
198 static inline int modeset_lock(struct drm_modeset_lock *lock,
199 struct drm_modeset_acquire_ctx *ctx,
200 bool interruptible, bool slow)
204 WARN_ON(ctx->contended);
206 if (interruptible && slow) {
207 ret = ww_mutex_lock_slow_interruptible(&lock->mutex, &ctx->ww_ctx);
208 } else if (interruptible) {
209 ret = ww_mutex_lock_interruptible(&lock->mutex, &ctx->ww_ctx);
211 ww_mutex_lock_slow(&lock->mutex, &ctx->ww_ctx);
214 ret = ww_mutex_lock(&lock->mutex, &ctx->ww_ctx);
217 WARN_ON(!list_empty(&lock->head));
218 list_add(&lock->head, &ctx->locked);
219 } else if (ret == -EALREADY) {
220 /* we already hold the lock.. this is fine. For atomic
221 * we will need to be able to drm_modeset_lock() things
222 * without having to keep track of what is already locked
226 } else if (ret == -EDEADLK) {
227 ctx->contended = lock;
233 static int modeset_backoff(struct drm_modeset_acquire_ctx *ctx,
236 struct drm_modeset_lock *contended = ctx->contended;
238 ctx->contended = NULL;
240 if (WARN_ON(!contended))
243 drm_modeset_drop_locks(ctx);
245 return modeset_lock(contended, ctx, interruptible, true);
249 * drm_modeset_backoff - deadlock avoidance backoff
250 * @ctx: the acquire context
252 * If deadlock is detected (ie. drm_modeset_lock() returns -EDEADLK),
253 * you must call this function to drop all currently held locks and
254 * block until the contended lock becomes available.
256 void drm_modeset_backoff(struct drm_modeset_acquire_ctx *ctx)
258 modeset_backoff(ctx, false);
260 EXPORT_SYMBOL(drm_modeset_backoff);
263 * drm_modeset_backoff_interruptible - deadlock avoidance backoff
264 * @ctx: the acquire context
266 * Interruptible version of drm_modeset_backoff()
268 int drm_modeset_backoff_interruptible(struct drm_modeset_acquire_ctx *ctx)
270 return modeset_backoff(ctx, true);
272 EXPORT_SYMBOL(drm_modeset_backoff_interruptible);
275 * drm_modeset_lock - take modeset lock
276 * @lock: lock to take
279 * If ctx is not NULL, then its ww acquire context is used and the
280 * lock will be tracked by the context and can be released by calling
281 * drm_modeset_drop_locks(). If -EDEADLK is returned, this means a
282 * deadlock scenario has been detected and it is an error to attempt
283 * to take any more locks without first calling drm_modeset_backoff().
285 int drm_modeset_lock(struct drm_modeset_lock *lock,
286 struct drm_modeset_acquire_ctx *ctx)
289 return modeset_lock(lock, ctx, false, false);
291 ww_mutex_lock(&lock->mutex, NULL);
294 EXPORT_SYMBOL(drm_modeset_lock);
297 * drm_modeset_lock_interruptible - take modeset lock
298 * @lock: lock to take
301 * Interruptible version of drm_modeset_lock()
303 int drm_modeset_lock_interruptible(struct drm_modeset_lock *lock,
304 struct drm_modeset_acquire_ctx *ctx)
307 return modeset_lock(lock, ctx, true, false);
309 return ww_mutex_lock_interruptible(&lock->mutex, NULL);
311 EXPORT_SYMBOL(drm_modeset_lock_interruptible);
314 * drm_modeset_unlock - drop modeset lock
315 * @lock: lock to release
317 void drm_modeset_unlock(struct drm_modeset_lock *lock)
319 list_del_init(&lock->head);
320 ww_mutex_unlock(&lock->mutex);
322 EXPORT_SYMBOL(drm_modeset_unlock);
324 /* Temporary.. until we have sufficiently fine grained locking, there
325 * are a couple scenarios where it is convenient to grab all crtc locks.
326 * It is planned to remove this:
328 int drm_modeset_lock_all_crtcs(struct drm_device *dev,
329 struct drm_modeset_acquire_ctx *ctx)
331 struct drm_mode_config *config = &dev->mode_config;
332 struct drm_crtc *crtc;
335 list_for_each_entry(crtc, &config->crtc_list, head) {
336 ret = drm_modeset_lock(&crtc->mutex, ctx);
343 EXPORT_SYMBOL(drm_modeset_lock_all_crtcs);