xref: /dragonfly/sys/dev/drm/drm_modeset_lock.c (revision abf903a5)
1 /*
2  * Copyright (C) 2014 Red Hat
3  * Author: Rob Clark <robdclark@gmail.com>
4  *
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:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
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.
22  */
23 
24 #include <drm/drmP.h>
25 #include <drm/drm_crtc.h>
26 #include <drm/drm_modeset_lock.h>
27 
28 /**
29  * DOC: kms locking
30  *
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.
37  *
38  * For basic principles of &ww_mutex, see: Documentation/locking/ww-mutex-design.txt
39  *
40  * The basic usage pattern is to::
41  *
42  *     drm_modeset_acquire_init(&ctx)
43  *     retry:
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);
48  *             goto retry;
49  *         }
50  *     }
51  *     ... do stuff ...
52  *     drm_modeset_drop_locks(&ctx);
53  *     drm_modeset_acquire_fini(&ctx);
54  *
55  *  On top of of these per-object locks using &ww_mutex there's also an overall
56  *  dev->mode_config.lock, for protecting everything else. Mostly this means
57  *  probe state of connectors, and preventing hotplug add/removal of connectors.
58  *
59  *  Finally there's a bunch of dedicated locks to protect drm core internal
60  *  lists and lookup data structures.
61  */
62 
63 /**
64  * drm_modeset_lock_all - take all modeset locks
65  * @dev: DRM device
66  *
67  * This function takes all modeset locks, suitable where a more fine-grained
68  * scheme isn't (yet) implemented. Locks must be dropped by calling the
69  * drm_modeset_unlock_all() function.
70  *
71  * This function is deprecated. It allocates a lock acquisition context and
72  * stores it in the DRM device's ->mode_config. This facilitate conversion of
73  * existing code because it removes the need to manually deal with the
74  * acquisition context, but it is also brittle because the context is global
75  * and care must be taken not to nest calls. New code should use the
76  * drm_modeset_lock_all_ctx() function and pass in the context explicitly.
77  */
78 void drm_modeset_lock_all(struct drm_device *dev)
79 {
80 	struct drm_mode_config *config = &dev->mode_config;
81 	struct drm_modeset_acquire_ctx *ctx;
82 	int ret;
83 
84 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
85 	if (WARN_ON(!ctx))
86 		return;
87 
88 	mutex_lock(&config->mutex);
89 
90 	drm_modeset_acquire_init(ctx, 0);
91 
92 retry:
93 	ret = drm_modeset_lock_all_ctx(dev, ctx);
94 	if (ret < 0) {
95 		if (ret == -EDEADLK) {
96 			drm_modeset_backoff(ctx);
97 			goto retry;
98 		}
99 
100 		drm_modeset_acquire_fini(ctx);
101 		kfree(ctx);
102 		return;
103 	}
104 
105 	WARN_ON(config->acquire_ctx);
106 
107 	/*
108 	 * We hold the locks now, so it is safe to stash the acquisition
109 	 * context for drm_modeset_unlock_all().
110 	 */
111 	config->acquire_ctx = ctx;
112 
113 	drm_warn_on_modeset_not_all_locked(dev);
114 }
115 EXPORT_SYMBOL(drm_modeset_lock_all);
116 
117 /**
118  * drm_modeset_unlock_all - drop all modeset locks
119  * @dev: DRM device
120  *
121  * This function drops all modeset locks taken by a previous call to the
122  * drm_modeset_lock_all() function.
123  *
124  * This function is deprecated. It uses the lock acquisition context stored
125  * in the DRM device's ->mode_config. This facilitates conversion of existing
126  * code because it removes the need to manually deal with the acquisition
127  * context, but it is also brittle because the context is global and care must
128  * be taken not to nest calls. New code should pass the acquisition context
129  * directly to the drm_modeset_drop_locks() function.
130  */
131 void drm_modeset_unlock_all(struct drm_device *dev)
132 {
133 	struct drm_mode_config *config = &dev->mode_config;
134 	struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
135 
136 	if (WARN_ON(!ctx))
137 		return;
138 
139 	config->acquire_ctx = NULL;
140 	drm_modeset_drop_locks(ctx);
141 	drm_modeset_acquire_fini(ctx);
142 
143 	kfree(ctx);
144 
145 	mutex_unlock(&dev->mode_config.mutex);
146 }
147 EXPORT_SYMBOL(drm_modeset_unlock_all);
148 
149 /**
150  * drm_modeset_lock_crtc - lock crtc with hidden acquire ctx for a plane update
151  * @crtc: DRM CRTC
152  * @plane: DRM plane to be updated on @crtc
153  *
154  * This function locks the given crtc and plane (which should be either the
155  * primary or cursor plane) using a hidden acquire context. This is necessary so
156  * that drivers internally using the atomic interfaces can grab further locks
157  * with the lock acquire context.
158  *
159  * Note that @plane can be NULL, e.g. when the cursor support hasn't yet been
160  * converted to universal planes yet.
161  */
162 void drm_modeset_lock_crtc(struct drm_crtc *crtc,
163 			   struct drm_plane *plane)
164 {
165 	struct drm_modeset_acquire_ctx *ctx;
166 	int ret;
167 
168 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
169 	if (WARN_ON(!ctx))
170 		return;
171 
172 	drm_modeset_acquire_init(ctx, 0);
173 
174 retry:
175 	ret = drm_modeset_lock(&crtc->mutex, ctx);
176 	if (ret)
177 		goto fail;
178 
179 	if (plane) {
180 		ret = drm_modeset_lock(&plane->mutex, ctx);
181 		if (ret)
182 			goto fail;
183 
184 		if (plane->crtc) {
185 			ret = drm_modeset_lock(&plane->crtc->mutex, ctx);
186 			if (ret)
187 				goto fail;
188 		}
189 	}
190 
191 	WARN_ON(crtc->acquire_ctx);
192 
193 	/* now we hold the locks, so now that it is safe, stash the
194 	 * ctx for drm_modeset_unlock_crtc():
195 	 */
196 	crtc->acquire_ctx = ctx;
197 
198 	return;
199 
200 fail:
201 	if (ret == -EDEADLK) {
202 		drm_modeset_backoff(ctx);
203 		goto retry;
204 	}
205 }
206 EXPORT_SYMBOL(drm_modeset_lock_crtc);
207 
208 /**
209  * drm_modeset_legacy_acquire_ctx - find acquire ctx for legacy ioctls
210  * @crtc: drm crtc
211  *
212  * Legacy ioctl operations like cursor updates or page flips only have per-crtc
213  * locking, and store the acquire ctx in the corresponding crtc. All other
214  * legacy operations take all locks and use a global acquire context. This
215  * function grabs the right one.
216  */
217 struct drm_modeset_acquire_ctx *
218 drm_modeset_legacy_acquire_ctx(struct drm_crtc *crtc)
219 {
220 	if (crtc->acquire_ctx)
221 		return crtc->acquire_ctx;
222 
223 	WARN_ON(!crtc->dev->mode_config.acquire_ctx);
224 
225 	return crtc->dev->mode_config.acquire_ctx;
226 }
227 EXPORT_SYMBOL(drm_modeset_legacy_acquire_ctx);
228 
229 /**
230  * drm_modeset_unlock_crtc - drop crtc lock
231  * @crtc: drm crtc
232  *
233  * This drops the crtc lock acquire with drm_modeset_lock_crtc() and all other
234  * locks acquired through the hidden context.
235  */
236 void drm_modeset_unlock_crtc(struct drm_crtc *crtc)
237 {
238 	struct drm_modeset_acquire_ctx *ctx = crtc->acquire_ctx;
239 
240 	if (WARN_ON(!ctx))
241 		return;
242 
243 	crtc->acquire_ctx = NULL;
244 	drm_modeset_drop_locks(ctx);
245 	drm_modeset_acquire_fini(ctx);
246 
247 	kfree(ctx);
248 }
249 EXPORT_SYMBOL(drm_modeset_unlock_crtc);
250 
251 /**
252  * drm_warn_on_modeset_not_all_locked - check that all modeset locks are locked
253  * @dev: device
254  *
255  * Useful as a debug assert.
256  */
257 void drm_warn_on_modeset_not_all_locked(struct drm_device *dev)
258 {
259 	struct drm_crtc *crtc;
260 
261 	/* Locking is currently fubar in the panic handler. */
262 #ifdef __DragonFly__
263 	if (panicstr)
264 		return;
265 #else
266 	if (oops_in_progress)
267 		return;
268 #endif
269 
270 	drm_for_each_crtc(crtc, dev)
271 		WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
272 
273 	WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
274 	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
275 }
276 EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked);
277 
278 /**
279  * drm_modeset_acquire_init - initialize acquire context
280  * @ctx: the acquire context
281  * @flags: for future
282  */
283 void drm_modeset_acquire_init(struct drm_modeset_acquire_ctx *ctx,
284 		uint32_t flags)
285 {
286 	memset(ctx, 0, sizeof(*ctx));
287 	ww_acquire_init(&ctx->ww_ctx, &crtc_ww_class);
288 	INIT_LIST_HEAD(&ctx->locked);
289 }
290 EXPORT_SYMBOL(drm_modeset_acquire_init);
291 
292 /**
293  * drm_modeset_acquire_fini - cleanup acquire context
294  * @ctx: the acquire context
295  */
296 void drm_modeset_acquire_fini(struct drm_modeset_acquire_ctx *ctx)
297 {
298 	ww_acquire_fini(&ctx->ww_ctx);
299 }
300 EXPORT_SYMBOL(drm_modeset_acquire_fini);
301 
302 /**
303  * drm_modeset_drop_locks - drop all locks
304  * @ctx: the acquire context
305  *
306  * Drop all locks currently held against this acquire context.
307  */
308 void drm_modeset_drop_locks(struct drm_modeset_acquire_ctx *ctx)
309 {
310 	WARN_ON(ctx->contended);
311 	while (!list_empty(&ctx->locked)) {
312 		struct drm_modeset_lock_info *info;
313 
314 		info = list_first_entry(&ctx->locked,
315 				struct drm_modeset_lock_info, ctx_entry);
316 
317 		drm_modeset_unlock(info->lock);
318 	}
319 }
320 EXPORT_SYMBOL(drm_modeset_drop_locks);
321 
322 static inline int modeset_lock(struct drm_modeset_lock *lock,
323 		struct drm_modeset_acquire_ctx *ctx,
324 		bool interruptible, bool slow)
325 {
326 	int ret;
327 
328 	WARN_ON(ctx->contended);
329 
330 	if (ctx->trylock_only) {
331 #if 0
332 		lockdep_assert_held(&ctx->ww_ctx);
333 #endif
334 
335 		if (!ww_mutex_trylock(&lock->mutex))
336 			return -EBUSY;
337 		else
338 			return 0;
339 	} else if (interruptible && slow) {
340 		ret = ww_mutex_lock_slow_interruptible(&lock->mutex, &ctx->ww_ctx);
341 	} else if (interruptible) {
342 		ret = ww_mutex_lock_interruptible(&lock->mutex, &ctx->ww_ctx);
343 	} else if (slow) {
344 		ww_mutex_lock_slow(&lock->mutex, &ctx->ww_ctx);
345 		ret = 0;
346 	} else {
347 		ret = ww_mutex_lock(&lock->mutex, &ctx->ww_ctx);
348 	}
349 	if (ret == -EALREADY) {
350 		/* we already hold the lock.. this is fine.  For atomic
351 		 * we will need to be able to drm_modeset_lock() things
352 		 * without having to keep track of what is already locked
353 		 * or not.
354 		 */
355 		ret = 0;
356 	} else if (ret == -EDEADLK) {
357 		ctx->contended = lock;
358 	}
359 	if (ret == 0) {
360 		struct drm_modeset_lock_info *info;
361 
362 		info = kzalloc(sizeof(*info), GFP_KERNEL);
363 		INIT_LIST_HEAD(&info->ctx_entry);
364 		INIT_LIST_HEAD(&info->lock_entry);
365 		info->lock = lock;
366 		info->ctx = ctx;
367 		list_add(&info->ctx_entry, &ctx->locked);
368 		list_add(&info->lock_entry, &lock->locked);
369 	}
370 
371 	return ret;
372 }
373 
374 static int modeset_backoff(struct drm_modeset_acquire_ctx *ctx,
375 		bool interruptible)
376 {
377 	struct drm_modeset_lock *contended = ctx->contended;
378 
379 	ctx->contended = NULL;
380 
381 	if (WARN_ON(!contended))
382 		return 0;
383 
384 	drm_modeset_drop_locks(ctx);
385 
386 	return modeset_lock(contended, ctx, interruptible, true);
387 }
388 
389 /**
390  * drm_modeset_backoff - deadlock avoidance backoff
391  * @ctx: the acquire context
392  *
393  * If deadlock is detected (ie. drm_modeset_lock() returns -EDEADLK),
394  * you must call this function to drop all currently held locks and
395  * block until the contended lock becomes available.
396  */
397 void drm_modeset_backoff(struct drm_modeset_acquire_ctx *ctx)
398 {
399 	modeset_backoff(ctx, false);
400 }
401 EXPORT_SYMBOL(drm_modeset_backoff);
402 
403 /**
404  * drm_modeset_backoff_interruptible - deadlock avoidance backoff
405  * @ctx: the acquire context
406  *
407  * Interruptible version of drm_modeset_backoff()
408  */
409 int drm_modeset_backoff_interruptible(struct drm_modeset_acquire_ctx *ctx)
410 {
411 	return modeset_backoff(ctx, true);
412 }
413 EXPORT_SYMBOL(drm_modeset_backoff_interruptible);
414 
415 /**
416  * drm_modeset_lock - take modeset lock
417  * @lock: lock to take
418  * @ctx: acquire ctx
419  *
420  * If ctx is not NULL, then its ww acquire context is used and the
421  * lock will be tracked by the context and can be released by calling
422  * drm_modeset_drop_locks().  If -EDEADLK is returned, this means a
423  * deadlock scenario has been detected and it is an error to attempt
424  * to take any more locks without first calling drm_modeset_backoff().
425  */
426 int drm_modeset_lock(struct drm_modeset_lock *lock,
427 		struct drm_modeset_acquire_ctx *ctx)
428 {
429 	if (ctx)
430 		return modeset_lock(lock, ctx, false, false);
431 
432 	ww_mutex_lock(&lock->mutex, NULL);
433 	return 0;
434 }
435 EXPORT_SYMBOL(drm_modeset_lock);
436 
437 /**
438  * drm_modeset_lock_interruptible - take modeset lock
439  * @lock: lock to take
440  * @ctx: acquire ctx
441  *
442  * Interruptible version of drm_modeset_lock()
443  */
444 int drm_modeset_lock_interruptible(struct drm_modeset_lock *lock,
445 		struct drm_modeset_acquire_ctx *ctx)
446 {
447 	if (ctx)
448 		return modeset_lock(lock, ctx, true, false);
449 
450 	return ww_mutex_lock_interruptible(&lock->mutex, NULL);
451 }
452 EXPORT_SYMBOL(drm_modeset_lock_interruptible);
453 
454 /**
455  * drm_modeset_unlock - drop modeset lock
456  * @lock: lock to release
457  */
458 void drm_modeset_unlock(struct drm_modeset_lock *lock)
459 {
460 	struct drm_modeset_lock_info *info;
461 
462 	/* undo in reverse order */
463 	if (!list_empty(&lock->locked)) {
464 		info = list_last_entry(&lock->locked,
465 				struct drm_modeset_lock_info, lock_entry);
466 		list_del_init(&info->lock_entry);
467 		if (info->ctx)
468 			list_del_init(&info->ctx_entry);
469 		kfree(info);
470 	}
471 	ww_mutex_unlock(&lock->mutex);
472 }
473 EXPORT_SYMBOL(drm_modeset_unlock);
474 
475 /**
476  * drm_modeset_lock_all_ctx - take all modeset locks
477  * @dev: DRM device
478  * @ctx: lock acquisition context
479  *
480  * This function takes all modeset locks, suitable where a more fine-grained
481  * scheme isn't (yet) implemented.
482  *
483  * Unlike drm_modeset_lock_all(), it doesn't take the dev->mode_config.mutex
484  * since that lock isn't required for modeset state changes. Callers which
485  * need to grab that lock too need to do so outside of the acquire context
486  * @ctx.
487  *
488  * Locks acquired with this function should be released by calling the
489  * drm_modeset_drop_locks() function on @ctx.
490  *
491  * Returns: 0 on success or a negative error-code on failure.
492  */
493 int drm_modeset_lock_all_ctx(struct drm_device *dev,
494 			     struct drm_modeset_acquire_ctx *ctx)
495 {
496 	struct drm_crtc *crtc;
497 	struct drm_plane *plane;
498 	int ret;
499 
500 	ret = drm_modeset_lock(&dev->mode_config.connection_mutex, ctx);
501 	if (ret)
502 		return ret;
503 
504 	drm_for_each_crtc(crtc, dev) {
505 		ret = drm_modeset_lock(&crtc->mutex, ctx);
506 		if (ret)
507 			return ret;
508 	}
509 
510 	drm_for_each_plane(plane, dev) {
511 		ret = drm_modeset_lock(&plane->mutex, ctx);
512 		if (ret)
513 			return ret;
514 	}
515 
516 	return 0;
517 }
518 EXPORT_SYMBOL(drm_modeset_lock_all_ctx);
519