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 	if (oops_in_progress)
263 		return;
264 
265 	drm_for_each_crtc(crtc, dev)
266 		WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
267 
268 	WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
269 	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
270 }
271 EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked);
272 
273 /**
274  * drm_modeset_acquire_init - initialize acquire context
275  * @ctx: the acquire context
276  * @flags: for future
277  */
278 void drm_modeset_acquire_init(struct drm_modeset_acquire_ctx *ctx,
279 		uint32_t flags)
280 {
281 	memset(ctx, 0, sizeof(*ctx));
282 	ww_acquire_init(&ctx->ww_ctx, &crtc_ww_class);
283 	INIT_LIST_HEAD(&ctx->locked);
284 }
285 EXPORT_SYMBOL(drm_modeset_acquire_init);
286 
287 /**
288  * drm_modeset_acquire_fini - cleanup acquire context
289  * @ctx: the acquire context
290  */
291 void drm_modeset_acquire_fini(struct drm_modeset_acquire_ctx *ctx)
292 {
293 	ww_acquire_fini(&ctx->ww_ctx);
294 }
295 EXPORT_SYMBOL(drm_modeset_acquire_fini);
296 
297 /**
298  * drm_modeset_drop_locks - drop all locks
299  * @ctx: the acquire context
300  *
301  * Drop all locks currently held against this acquire context.
302  */
303 void drm_modeset_drop_locks(struct drm_modeset_acquire_ctx *ctx)
304 {
305 	WARN_ON(ctx->contended);
306 	while (!list_empty(&ctx->locked)) {
307 		struct drm_modeset_lock *lock;
308 
309 		lock = list_first_entry(&ctx->locked,
310 				struct drm_modeset_lock, head);
311 
312 		drm_modeset_unlock(lock);
313 	}
314 }
315 EXPORT_SYMBOL(drm_modeset_drop_locks);
316 
317 static inline int modeset_lock(struct drm_modeset_lock *lock,
318 		struct drm_modeset_acquire_ctx *ctx,
319 		bool interruptible, bool slow)
320 {
321 	int ret;
322 
323 	WARN_ON(ctx->contended);
324 
325 	if (ctx->trylock_only) {
326 		lockdep_assert_held(&ctx->ww_ctx);
327 
328 		if (!ww_mutex_trylock(&lock->mutex))
329 			return -EBUSY;
330 		else
331 			return 0;
332 	} else if (interruptible && slow) {
333 		ret = ww_mutex_lock_slow_interruptible(&lock->mutex, &ctx->ww_ctx);
334 	} else if (interruptible) {
335 		ret = ww_mutex_lock_interruptible(&lock->mutex, &ctx->ww_ctx);
336 	} else if (slow) {
337 		ww_mutex_lock_slow(&lock->mutex, &ctx->ww_ctx);
338 		ret = 0;
339 	} else {
340 		ret = ww_mutex_lock(&lock->mutex, &ctx->ww_ctx);
341 	}
342 	if (!ret) {
343 		WARN_ON(!list_empty(&lock->head));
344 		list_add(&lock->head, &ctx->locked);
345 	} else if (ret == -EALREADY) {
346 		/* we already hold the lock.. this is fine.  For atomic
347 		 * we will need to be able to drm_modeset_lock() things
348 		 * without having to keep track of what is already locked
349 		 * or not.
350 		 */
351 		ret = 0;
352 	} else if (ret == -EDEADLK) {
353 		ctx->contended = lock;
354 	}
355 
356 	return ret;
357 }
358 
359 static int modeset_backoff(struct drm_modeset_acquire_ctx *ctx,
360 		bool interruptible)
361 {
362 	struct drm_modeset_lock *contended = ctx->contended;
363 
364 	ctx->contended = NULL;
365 
366 	if (WARN_ON(!contended))
367 		return 0;
368 
369 	drm_modeset_drop_locks(ctx);
370 
371 	return modeset_lock(contended, ctx, interruptible, true);
372 }
373 
374 /**
375  * drm_modeset_backoff - deadlock avoidance backoff
376  * @ctx: the acquire context
377  *
378  * If deadlock is detected (ie. drm_modeset_lock() returns -EDEADLK),
379  * you must call this function to drop all currently held locks and
380  * block until the contended lock becomes available.
381  */
382 void drm_modeset_backoff(struct drm_modeset_acquire_ctx *ctx)
383 {
384 	modeset_backoff(ctx, false);
385 }
386 EXPORT_SYMBOL(drm_modeset_backoff);
387 
388 /**
389  * drm_modeset_backoff_interruptible - deadlock avoidance backoff
390  * @ctx: the acquire context
391  *
392  * Interruptible version of drm_modeset_backoff()
393  */
394 int drm_modeset_backoff_interruptible(struct drm_modeset_acquire_ctx *ctx)
395 {
396 	return modeset_backoff(ctx, true);
397 }
398 EXPORT_SYMBOL(drm_modeset_backoff_interruptible);
399 
400 /**
401  * drm_modeset_lock - take modeset lock
402  * @lock: lock to take
403  * @ctx: acquire ctx
404  *
405  * If ctx is not NULL, then its ww acquire context is used and the
406  * lock will be tracked by the context and can be released by calling
407  * drm_modeset_drop_locks().  If -EDEADLK is returned, this means a
408  * deadlock scenario has been detected and it is an error to attempt
409  * to take any more locks without first calling drm_modeset_backoff().
410  */
411 int drm_modeset_lock(struct drm_modeset_lock *lock,
412 		struct drm_modeset_acquire_ctx *ctx)
413 {
414 	if (ctx)
415 		return modeset_lock(lock, ctx, false, false);
416 
417 	ww_mutex_lock(&lock->mutex, NULL);
418 	return 0;
419 }
420 EXPORT_SYMBOL(drm_modeset_lock);
421 
422 /**
423  * drm_modeset_lock_interruptible - take modeset lock
424  * @lock: lock to take
425  * @ctx: acquire ctx
426  *
427  * Interruptible version of drm_modeset_lock()
428  */
429 int drm_modeset_lock_interruptible(struct drm_modeset_lock *lock,
430 		struct drm_modeset_acquire_ctx *ctx)
431 {
432 	if (ctx)
433 		return modeset_lock(lock, ctx, true, false);
434 
435 	return ww_mutex_lock_interruptible(&lock->mutex, NULL);
436 }
437 EXPORT_SYMBOL(drm_modeset_lock_interruptible);
438 
439 /**
440  * drm_modeset_unlock - drop modeset lock
441  * @lock: lock to release
442  */
443 void drm_modeset_unlock(struct drm_modeset_lock *lock)
444 {
445 	list_del_init(&lock->head);
446 	ww_mutex_unlock(&lock->mutex);
447 }
448 EXPORT_SYMBOL(drm_modeset_unlock);
449 
450 /**
451  * drm_modeset_lock_all_ctx - take all modeset locks
452  * @dev: DRM device
453  * @ctx: lock acquisition context
454  *
455  * This function takes all modeset locks, suitable where a more fine-grained
456  * scheme isn't (yet) implemented.
457  *
458  * Unlike drm_modeset_lock_all(), it doesn't take the dev->mode_config.mutex
459  * since that lock isn't required for modeset state changes. Callers which
460  * need to grab that lock too need to do so outside of the acquire context
461  * @ctx.
462  *
463  * Locks acquired with this function should be released by calling the
464  * drm_modeset_drop_locks() function on @ctx.
465  *
466  * Returns: 0 on success or a negative error-code on failure.
467  */
468 int drm_modeset_lock_all_ctx(struct drm_device *dev,
469 			     struct drm_modeset_acquire_ctx *ctx)
470 {
471 	struct drm_crtc *crtc;
472 	struct drm_plane *plane;
473 	int ret;
474 
475 	ret = drm_modeset_lock(&dev->mode_config.connection_mutex, ctx);
476 	if (ret)
477 		return ret;
478 
479 	drm_for_each_crtc(crtc, dev) {
480 		ret = drm_modeset_lock(&crtc->mutex, ctx);
481 		if (ret)
482 			return ret;
483 	}
484 
485 	drm_for_each_plane(plane, dev) {
486 		ret = drm_modeset_lock(&plane->mutex, ctx);
487 		if (ret)
488 			return ret;
489 	}
490 
491 	return 0;
492 }
493 EXPORT_SYMBOL(drm_modeset_lock_all_ctx);
494