drm_modeset_lock.c revision 1.3.2.2 1 /* $NetBSD: drm_modeset_lock.c,v 1.3.2.2 2018/09/06 06:56:09 pgoyette Exp $ */
2
3 /*
4 * Copyright (C) 2014 Red Hat
5 * Author: Rob Clark <robdclark (at) gmail.com>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 #include <sys/cdefs.h>
27 __KERNEL_RCSID(0, "$NetBSD: drm_modeset_lock.c,v 1.3.2.2 2018/09/06 06:56:09 pgoyette Exp $");
28
29 #include <linux/export.h>
30
31 #include <drm/drmP.h>
32 #include <drm/drm_crtc.h>
33 #include <drm/drm_modeset_lock.h>
34
35 /**
36 * DOC: kms locking
37 *
38 * As KMS moves toward more fine grained locking, and atomic ioctl where
39 * userspace can indirectly control locking order, it becomes necessary
40 * to use ww_mutex and acquire-contexts to avoid deadlocks. But because
41 * the locking is more distributed around the driver code, we want a bit
42 * of extra utility/tracking out of our acquire-ctx. This is provided
43 * by drm_modeset_lock / drm_modeset_acquire_ctx.
44 *
45 * For basic principles of ww_mutex, see: Documentation/locking/ww-mutex-design.txt
46 *
47 * The basic usage pattern is to:
48 *
49 * drm_modeset_acquire_init(&ctx)
50 * retry:
51 * foreach (lock in random_ordered_set_of_locks) {
52 * ret = drm_modeset_lock(lock, &ctx)
53 * if (ret == -EDEADLK) {
54 * drm_modeset_backoff(&ctx);
55 * goto retry;
56 * }
57 * }
58 *
59 * ... do stuff ...
60 *
61 * drm_modeset_drop_locks(&ctx);
62 * drm_modeset_acquire_fini(&ctx);
63 */
64
65 /**
66 * drm_modeset_lock_all - take all modeset locks
67 * @dev: drm device
68 *
69 * This function takes all modeset locks, suitable where a more fine-grained
70 * scheme isn't (yet) implemented. Locks must be dropped with
71 * drm_modeset_unlock_all.
72 */
73 void drm_modeset_lock_all(struct drm_device *dev)
74 {
75 struct drm_mode_config *config = &dev->mode_config;
76 struct drm_modeset_acquire_ctx *ctx;
77 int ret;
78
79 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL | __GFP_NOFAIL);
80 if (WARN_ON(!ctx))
81 return;
82
83 mutex_lock(&config->mutex);
84
85 drm_modeset_acquire_init(ctx, 0);
86
87 retry:
88 ret = drm_modeset_lock(&config->connection_mutex, ctx);
89 if (ret)
90 goto fail;
91 ret = drm_modeset_lock_all_crtcs(dev, ctx);
92 if (ret)
93 goto fail;
94
95 WARN_ON(config->acquire_ctx);
96
97 /* now we hold the locks, so now that it is safe, stash the
98 * ctx for drm_modeset_unlock_all():
99 */
100 config->acquire_ctx = ctx;
101
102 drm_warn_on_modeset_not_all_locked(dev);
103
104 return;
105
106 fail:
107 if (ret == -EDEADLK) {
108 drm_modeset_backoff(ctx);
109 goto retry;
110 }
111
112 kfree(ctx);
113 }
114 EXPORT_SYMBOL(drm_modeset_lock_all);
115
116 /**
117 * drm_modeset_unlock_all - drop all modeset locks
118 * @dev: device
119 *
120 * This function drop all modeset locks taken by drm_modeset_lock_all.
121 */
122 void drm_modeset_unlock_all(struct drm_device *dev)
123 {
124 struct drm_mode_config *config = &dev->mode_config;
125 struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
126
127 if (WARN_ON(!ctx))
128 return;
129
130 config->acquire_ctx = NULL;
131 drm_modeset_drop_locks(ctx);
132 drm_modeset_acquire_fini(ctx);
133
134 kfree(ctx);
135
136 mutex_unlock(&dev->mode_config.mutex);
137 }
138 EXPORT_SYMBOL(drm_modeset_unlock_all);
139
140 /**
141 * drm_modeset_lock_crtc - lock crtc with hidden acquire ctx for a plane update
142 * @crtc: DRM CRTC
143 * @plane: DRM plane to be updated on @crtc
144 *
145 * This function locks the given crtc and plane (which should be either the
146 * primary or cursor plane) using a hidden acquire context. This is necessary so
147 * that drivers internally using the atomic interfaces can grab further locks
148 * with the lock acquire context.
149 *
150 * Note that @plane can be NULL, e.g. when the cursor support hasn't yet been
151 * converted to universal planes yet.
152 */
153 void drm_modeset_lock_crtc(struct drm_crtc *crtc,
154 struct drm_plane *plane)
155 {
156 struct drm_modeset_acquire_ctx *ctx;
157 int ret;
158
159 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
160 if (WARN_ON(!ctx))
161 return;
162
163 drm_modeset_acquire_init(ctx, 0);
164
165 retry:
166 ret = drm_modeset_lock(&crtc->mutex, ctx);
167 if (ret)
168 goto fail;
169
170 if (plane) {
171 ret = drm_modeset_lock(&plane->mutex, ctx);
172 if (ret)
173 goto fail;
174
175 if (plane->crtc) {
176 ret = drm_modeset_lock(&plane->crtc->mutex, ctx);
177 if (ret)
178 goto fail;
179 }
180 }
181
182 WARN_ON(crtc->acquire_ctx);
183
184 /* now we hold the locks, so now that it is safe, stash the
185 * ctx for drm_modeset_unlock_crtc():
186 */
187 crtc->acquire_ctx = ctx;
188
189 return;
190
191 fail:
192 if (ret == -EDEADLK) {
193 drm_modeset_backoff(ctx);
194 goto retry;
195 }
196 }
197 EXPORT_SYMBOL(drm_modeset_lock_crtc);
198
199 /**
200 * drm_modeset_legacy_acquire_ctx - find acquire ctx for legacy ioctls
201 * @crtc: drm crtc
202 *
203 * Legacy ioctl operations like cursor updates or page flips only have per-crtc
204 * locking, and store the acquire ctx in the corresponding crtc. All other
205 * legacy operations take all locks and use a global acquire context. This
206 * function grabs the right one.
207 */
208 struct drm_modeset_acquire_ctx *
209 drm_modeset_legacy_acquire_ctx(struct drm_crtc *crtc)
210 {
211 if (crtc->acquire_ctx)
212 return crtc->acquire_ctx;
213
214 WARN_ON(!crtc->dev->mode_config.acquire_ctx);
215
216 return crtc->dev->mode_config.acquire_ctx;
217 }
218 EXPORT_SYMBOL(drm_modeset_legacy_acquire_ctx);
219
220 /**
221 * drm_modeset_unlock_crtc - drop crtc lock
222 * @crtc: drm crtc
223 *
224 * This drops the crtc lock acquire with drm_modeset_lock_crtc() and all other
225 * locks acquired through the hidden context.
226 */
227 void drm_modeset_unlock_crtc(struct drm_crtc *crtc)
228 {
229 struct drm_modeset_acquire_ctx *ctx = crtc->acquire_ctx;
230
231 if (WARN_ON(!ctx))
232 return;
233
234 crtc->acquire_ctx = NULL;
235 drm_modeset_drop_locks(ctx);
236 drm_modeset_acquire_fini(ctx);
237
238 kfree(ctx);
239 }
240 EXPORT_SYMBOL(drm_modeset_unlock_crtc);
241
242 /**
243 * drm_warn_on_modeset_not_all_locked - check that all modeset locks are locked
244 * @dev: device
245 *
246 * Useful as a debug assert.
247 */
248 void drm_warn_on_modeset_not_all_locked(struct drm_device *dev)
249 {
250 struct drm_crtc *crtc;
251
252 /* Locking is currently fubar in the panic handler. */
253 if (oops_in_progress)
254 return;
255
256 drm_for_each_crtc(crtc, dev)
257 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
258
259 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
260 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
261 }
262 EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked);
263
264 /**
265 * drm_modeset_acquire_init - initialize acquire context
266 * @ctx: the acquire context
267 * @flags: for future
268 */
269 void drm_modeset_acquire_init(struct drm_modeset_acquire_ctx *ctx,
270 uint32_t flags)
271 {
272 memset(ctx, 0, sizeof(*ctx));
273 ww_acquire_init(&ctx->ww_ctx, &crtc_ww_class);
274 INIT_LIST_HEAD(&ctx->locked);
275 }
276 EXPORT_SYMBOL(drm_modeset_acquire_init);
277
278 /**
279 * drm_modeset_acquire_fini - cleanup acquire context
280 * @ctx: the acquire context
281 */
282 void drm_modeset_acquire_fini(struct drm_modeset_acquire_ctx *ctx)
283 {
284 ww_acquire_fini(&ctx->ww_ctx);
285 }
286 EXPORT_SYMBOL(drm_modeset_acquire_fini);
287
288 /**
289 * drm_modeset_drop_locks - drop all locks
290 * @ctx: the acquire context
291 *
292 * Drop all locks currently held against this acquire context.
293 */
294 void drm_modeset_drop_locks(struct drm_modeset_acquire_ctx *ctx)
295 {
296 WARN_ON(ctx->contended);
297 while (!list_empty(&ctx->locked)) {
298 struct drm_modeset_lock *lock;
299
300 lock = list_first_entry(&ctx->locked,
301 struct drm_modeset_lock, head);
302
303 drm_modeset_unlock(lock);
304 }
305 }
306 EXPORT_SYMBOL(drm_modeset_drop_locks);
307
308 static inline int modeset_lock(struct drm_modeset_lock *lock,
309 struct drm_modeset_acquire_ctx *ctx,
310 bool interruptible, bool slow)
311 {
312 int ret;
313
314 WARN_ON(ctx->contended);
315
316 if (ctx->trylock_only) {
317 lockdep_assert_held(&ctx->ww_ctx);
318
319 if (!ww_mutex_trylock(&lock->mutex))
320 return -EBUSY;
321 else
322 return 0;
323 } else if (interruptible && slow) {
324 ret = ww_mutex_lock_slow_interruptible(&lock->mutex, &ctx->ww_ctx);
325 } else if (interruptible) {
326 ret = ww_mutex_lock_interruptible(&lock->mutex, &ctx->ww_ctx);
327 } else if (slow) {
328 ww_mutex_lock_slow(&lock->mutex, &ctx->ww_ctx);
329 ret = 0;
330 } else {
331 ret = ww_mutex_lock(&lock->mutex, &ctx->ww_ctx);
332 }
333 if (!ret) {
334 WARN_ON(!list_empty(&lock->head));
335 list_add(&lock->head, &ctx->locked);
336 } else if (ret == -EALREADY) {
337 /* we already hold the lock.. this is fine. For atomic
338 * we will need to be able to drm_modeset_lock() things
339 * without having to keep track of what is already locked
340 * or not.
341 */
342 ret = 0;
343 } else if (ret == -EDEADLK) {
344 ctx->contended = lock;
345 }
346
347 return ret;
348 }
349
350 static int modeset_backoff(struct drm_modeset_acquire_ctx *ctx,
351 bool interruptible)
352 {
353 struct drm_modeset_lock *contended = ctx->contended;
354
355 ctx->contended = NULL;
356
357 if (WARN_ON(!contended))
358 return 0;
359
360 drm_modeset_drop_locks(ctx);
361
362 return modeset_lock(contended, ctx, interruptible, true);
363 }
364
365 /**
366 * drm_modeset_backoff - deadlock avoidance backoff
367 * @ctx: the acquire context
368 *
369 * If deadlock is detected (ie. drm_modeset_lock() returns -EDEADLK),
370 * you must call this function to drop all currently held locks and
371 * block until the contended lock becomes available.
372 */
373 void drm_modeset_backoff(struct drm_modeset_acquire_ctx *ctx)
374 {
375 modeset_backoff(ctx, false);
376 }
377 EXPORT_SYMBOL(drm_modeset_backoff);
378
379 /**
380 * drm_modeset_backoff_interruptible - deadlock avoidance backoff
381 * @ctx: the acquire context
382 *
383 * Interruptible version of drm_modeset_backoff()
384 */
385 int drm_modeset_backoff_interruptible(struct drm_modeset_acquire_ctx *ctx)
386 {
387 return modeset_backoff(ctx, true);
388 }
389 EXPORT_SYMBOL(drm_modeset_backoff_interruptible);
390
391 /**
392 * drm_modeset_lock - take modeset lock
393 * @lock: lock to take
394 * @ctx: acquire ctx
395 *
396 * If ctx is not NULL, then its ww acquire context is used and the
397 * lock will be tracked by the context and can be released by calling
398 * drm_modeset_drop_locks(). If -EDEADLK is returned, this means a
399 * deadlock scenario has been detected and it is an error to attempt
400 * to take any more locks without first calling drm_modeset_backoff().
401 */
402 int drm_modeset_lock(struct drm_modeset_lock *lock,
403 struct drm_modeset_acquire_ctx *ctx)
404 {
405 if (ctx)
406 return modeset_lock(lock, ctx, false, false);
407
408 ww_mutex_lock(&lock->mutex, NULL);
409 return 0;
410 }
411 EXPORT_SYMBOL(drm_modeset_lock);
412
413 /**
414 * drm_modeset_lock_interruptible - take modeset lock
415 * @lock: lock to take
416 * @ctx: acquire ctx
417 *
418 * Interruptible version of drm_modeset_lock()
419 */
420 int drm_modeset_lock_interruptible(struct drm_modeset_lock *lock,
421 struct drm_modeset_acquire_ctx *ctx)
422 {
423 if (ctx)
424 return modeset_lock(lock, ctx, true, false);
425
426 return ww_mutex_lock_interruptible(&lock->mutex, NULL);
427 }
428 EXPORT_SYMBOL(drm_modeset_lock_interruptible);
429
430 /**
431 * drm_modeset_unlock - drop modeset lock
432 * @lock: lock to release
433 */
434 void drm_modeset_unlock(struct drm_modeset_lock *lock)
435 {
436 list_del_init(&lock->head);
437 ww_mutex_unlock(&lock->mutex);
438 }
439 EXPORT_SYMBOL(drm_modeset_unlock);
440
441 /* In some legacy codepaths it's convenient to just grab all the crtc and plane
442 * related locks. */
443 int drm_modeset_lock_all_crtcs(struct drm_device *dev,
444 struct drm_modeset_acquire_ctx *ctx)
445 {
446 struct drm_crtc *crtc;
447 struct drm_plane *plane;
448 int ret = 0;
449
450 drm_for_each_crtc(crtc, dev) {
451 ret = drm_modeset_lock(&crtc->mutex, ctx);
452 if (ret)
453 return ret;
454 }
455
456 drm_for_each_plane(plane, dev) {
457 ret = drm_modeset_lock(&plane->mutex, ctx);
458 if (ret)
459 return ret;
460 }
461
462 return 0;
463 }
464 EXPORT_SYMBOL(drm_modeset_lock_all_crtcs);
465