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_acquire_init - initialize acquire context
61 * @ctx: the acquire context
64 void drm_modeset_acquire_init(struct drm_modeset_acquire_ctx
*ctx
,
67 memset(ctx
, 0, sizeof(*ctx
));
68 ww_acquire_init(&ctx
->ww_ctx
, &crtc_ww_class
);
69 INIT_LIST_HEAD(&ctx
->locked
);
71 EXPORT_SYMBOL(drm_modeset_acquire_init
);
74 * drm_modeset_acquire_fini - cleanup acquire context
75 * @ctx: the acquire context
77 void drm_modeset_acquire_fini(struct drm_modeset_acquire_ctx
*ctx
)
79 ww_acquire_fini(&ctx
->ww_ctx
);
81 EXPORT_SYMBOL(drm_modeset_acquire_fini
);
84 * drm_modeset_drop_locks - drop all locks
85 * @ctx: the acquire context
87 * Drop all locks currently held against this acquire context.
89 void drm_modeset_drop_locks(struct drm_modeset_acquire_ctx
*ctx
)
91 WARN_ON(ctx
->contended
);
92 while (!list_empty(&ctx
->locked
)) {
93 struct drm_modeset_lock
*lock
;
95 lock
= list_first_entry(&ctx
->locked
,
96 struct drm_modeset_lock
, head
);
98 drm_modeset_unlock(lock
);
101 EXPORT_SYMBOL(drm_modeset_drop_locks
);
103 static inline int modeset_lock(struct drm_modeset_lock
*lock
,
104 struct drm_modeset_acquire_ctx
*ctx
,
105 bool interruptible
, bool slow
)
109 WARN_ON(ctx
->contended
);
111 if (interruptible
&& slow
) {
112 ret
= ww_mutex_lock_slow_interruptible(&lock
->mutex
, &ctx
->ww_ctx
);
113 } else if (interruptible
) {
114 ret
= ww_mutex_lock_interruptible(&lock
->mutex
, &ctx
->ww_ctx
);
116 ww_mutex_lock_slow(&lock
->mutex
, &ctx
->ww_ctx
);
119 ret
= ww_mutex_lock(&lock
->mutex
, &ctx
->ww_ctx
);
122 WARN_ON(!list_empty(&lock
->head
));
123 list_add(&lock
->head
, &ctx
->locked
);
124 } else if (ret
== -EALREADY
) {
125 /* we already hold the lock.. this is fine. For atomic
126 * we will need to be able to drm_modeset_lock() things
127 * without having to keep track of what is already locked
131 } else if (ret
== -EDEADLK
) {
132 ctx
->contended
= lock
;
138 static int modeset_backoff(struct drm_modeset_acquire_ctx
*ctx
,
141 struct drm_modeset_lock
*contended
= ctx
->contended
;
143 ctx
->contended
= NULL
;
145 if (WARN_ON(!contended
))
148 drm_modeset_drop_locks(ctx
);
150 return modeset_lock(contended
, ctx
, interruptible
, true);
154 * drm_modeset_backoff - deadlock avoidance backoff
155 * @ctx: the acquire context
157 * If deadlock is detected (ie. drm_modeset_lock() returns -EDEADLK),
158 * you must call this function to drop all currently held locks and
159 * block until the contended lock becomes available.
161 void drm_modeset_backoff(struct drm_modeset_acquire_ctx
*ctx
)
163 modeset_backoff(ctx
, false);
165 EXPORT_SYMBOL(drm_modeset_backoff
);
168 * drm_modeset_backoff_interruptible - deadlock avoidance backoff
169 * @ctx: the acquire context
171 * Interruptible version of drm_modeset_backoff()
173 int drm_modeset_backoff_interruptible(struct drm_modeset_acquire_ctx
*ctx
)
175 return modeset_backoff(ctx
, true);
177 EXPORT_SYMBOL(drm_modeset_backoff_interruptible
);
180 * drm_modeset_lock - take modeset lock
181 * @lock: lock to take
184 * If ctx is not NULL, then its ww acquire context is used and the
185 * lock will be tracked by the context and can be released by calling
186 * drm_modeset_drop_locks(). If -EDEADLK is returned, this means a
187 * deadlock scenario has been detected and it is an error to attempt
188 * to take any more locks without first calling drm_modeset_backoff().
190 int drm_modeset_lock(struct drm_modeset_lock
*lock
,
191 struct drm_modeset_acquire_ctx
*ctx
)
194 return modeset_lock(lock
, ctx
, false, false);
196 ww_mutex_lock(&lock
->mutex
, NULL
);
199 EXPORT_SYMBOL(drm_modeset_lock
);
202 * drm_modeset_lock_interruptible - take modeset lock
203 * @lock: lock to take
206 * Interruptible version of drm_modeset_lock()
208 int drm_modeset_lock_interruptible(struct drm_modeset_lock
*lock
,
209 struct drm_modeset_acquire_ctx
*ctx
)
212 return modeset_lock(lock
, ctx
, true, false);
214 return ww_mutex_lock_interruptible(&lock
->mutex
, NULL
);
216 EXPORT_SYMBOL(drm_modeset_lock_interruptible
);
219 * drm_modeset_unlock - drop modeset lock
220 * @lock: lock to release
222 void drm_modeset_unlock(struct drm_modeset_lock
*lock
)
224 list_del_init(&lock
->head
);
225 ww_mutex_unlock(&lock
->mutex
);
227 EXPORT_SYMBOL(drm_modeset_unlock
);
229 /* Temporary.. until we have sufficiently fine grained locking, there
230 * are a couple scenarios where it is convenient to grab all crtc locks.
231 * It is planned to remove this:
233 int drm_modeset_lock_all_crtcs(struct drm_device
*dev
,
234 struct drm_modeset_acquire_ctx
*ctx
)
236 struct drm_mode_config
*config
= &dev
->mode_config
;
237 struct drm_crtc
*crtc
;
240 list_for_each_entry(crtc
, &config
->crtc_list
, head
) {
241 ret
= drm_modeset_lock(&crtc
->mutex
, ctx
);
248 EXPORT_SYMBOL(drm_modeset_lock_all_crtcs
);