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 <linux/export.h>
25
26#include <drm/drm_atomic.h>
27#include <drm/drm_crtc.h>
28#include <drm/drm_device.h>
29#include <drm/drm_modeset_lock.h>
30#include <drm/drm_print.h>
31
32/**
33 * DOC: kms locking
34 *
35 * As KMS moves toward more fine grained locking, and atomic ioctl where
36 * userspace can indirectly control locking order, it becomes necessary
37 * to use &ww_mutex and acquire-contexts to avoid deadlocks. But because
38 * the locking is more distributed around the driver code, we want a bit
39 * of extra utility/tracking out of our acquire-ctx. This is provided
40 * by &struct drm_modeset_lock and &struct drm_modeset_acquire_ctx.
41 *
42 * For basic principles of &ww_mutex, see: Documentation/locking/ww-mutex-design.rst
43 *
44 * The basic usage pattern is to::
45 *
46 * drm_modeset_acquire_init(ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE)
47 * retry:
48 * foreach (lock in random_ordered_set_of_locks) {
49 * ret = drm_modeset_lock(lock, ctx)
50 * if (ret == -EDEADLK) {
51 * ret = drm_modeset_backoff(ctx);
52 * if (!ret)
53 * goto retry;
54 * }
55 * if (ret)
56 * goto out;
57 * }
58 * ... do stuff ...
59 * out:
60 * drm_modeset_drop_locks(ctx);
61 * drm_modeset_acquire_fini(ctx);
62 *
63 * For convenience this control flow is implemented in
64 * DRM_MODESET_LOCK_ALL_BEGIN() and DRM_MODESET_LOCK_ALL_END() for the case
65 * where all modeset locks need to be taken through drm_modeset_lock_all_ctx().
66 *
67 * If all that is needed is a single modeset lock, then the &struct
68 * drm_modeset_acquire_ctx is not needed and the locking can be simplified
69 * by passing a NULL instead of ctx in the drm_modeset_lock() call or
70 * calling drm_modeset_lock_single_interruptible(). To unlock afterwards
71 * call drm_modeset_unlock().
72 *
73 * On top of these per-object locks using &ww_mutex there's also an overall
74 * &drm_mode_config.mutex, for protecting everything else. Mostly this means
75 * probe state of connectors, and preventing hotplug add/removal of connectors.
76 *
77 * Finally there's a bunch of dedicated locks to protect drm core internal
78 * lists and lookup data structures.
79 */
80
81static DEFINE_WW_CLASS(crtc_ww_class);
82
83#if IS_ENABLED(CONFIG_DRM_DEBUG_MODESET_LOCK)
84static noinline depot_stack_handle_t __drm_stack_depot_save(void)
85{
86 unsigned long entries[8];
87 unsigned int n;
88
89 n = stack_trace_save(entries, ARRAY_SIZE(entries), 1);
90
91 return stack_depot_save(entries, n, GFP_NOWAIT | __GFP_NOWARN);
92}
93
94static void __drm_stack_depot_print(depot_stack_handle_t stack_depot)
95{
96 struct drm_printer p = drm_dbg_printer(NULL, DRM_UT_KMS, "drm_modeset_lock");
97 unsigned long *entries;
98 unsigned int nr_entries;
99 char *buf;
100
101 buf = kmalloc(PAGE_SIZE, GFP_NOWAIT | __GFP_NOWARN);
102 if (!buf)
103 return;
104
105 nr_entries = stack_depot_fetch(stack_depot, &entries);
106 stack_trace_snprint(buf, PAGE_SIZE, entries, nr_entries, 2);
107
108 drm_printf(&p, "attempting to lock a contended lock without backoff:\n%s", buf);
109
110 kfree(buf);
111}
112
113static void __drm_stack_depot_init(void)
114{
115 stack_depot_init();
116}
117#else /* CONFIG_DRM_DEBUG_MODESET_LOCK */
118static depot_stack_handle_t __drm_stack_depot_save(void)
119{
120 return 0;
121}
122static void __drm_stack_depot_print(depot_stack_handle_t stack_depot)
123{
124}
125static void __drm_stack_depot_init(void)
126{
127}
128#endif /* CONFIG_DRM_DEBUG_MODESET_LOCK */
129
130/**
131 * drm_modeset_lock_all - take all modeset locks
132 * @dev: DRM device
133 *
134 * This function takes all modeset locks, suitable where a more fine-grained
135 * scheme isn't (yet) implemented. Locks must be dropped by calling the
136 * drm_modeset_unlock_all() function.
137 *
138 * This function is deprecated. It allocates a lock acquisition context and
139 * stores it in &drm_device.mode_config. This facilitate conversion of
140 * existing code because it removes the need to manually deal with the
141 * acquisition context, but it is also brittle because the context is global
142 * and care must be taken not to nest calls. New code should use the
143 * drm_modeset_lock_all_ctx() function and pass in the context explicitly.
144 */
145void drm_modeset_lock_all(struct drm_device *dev)
146{
147 struct drm_mode_config *config = &dev->mode_config;
148 struct drm_modeset_acquire_ctx *ctx;
149 int ret;
150
151 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL | __GFP_NOFAIL);
152 if (WARN_ON(!ctx))
153 return;
154
155 mutex_lock(lock: &config->mutex);
156
157 drm_modeset_acquire_init(ctx, flags: 0);
158
159retry:
160 ret = drm_modeset_lock_all_ctx(dev, ctx);
161 if (ret < 0) {
162 if (ret == -EDEADLK) {
163 drm_modeset_backoff(ctx);
164 goto retry;
165 }
166
167 drm_modeset_acquire_fini(ctx);
168 kfree(objp: ctx);
169 return;
170 }
171 ww_acquire_done(ctx: &ctx->ww_ctx);
172
173 WARN_ON(config->acquire_ctx);
174
175 /*
176 * We hold the locks now, so it is safe to stash the acquisition
177 * context for drm_modeset_unlock_all().
178 */
179 config->acquire_ctx = ctx;
180
181 drm_warn_on_modeset_not_all_locked(dev);
182}
183EXPORT_SYMBOL(drm_modeset_lock_all);
184
185/**
186 * drm_modeset_unlock_all - drop all modeset locks
187 * @dev: DRM device
188 *
189 * This function drops all modeset locks taken by a previous call to the
190 * drm_modeset_lock_all() function.
191 *
192 * This function is deprecated. It uses the lock acquisition context stored
193 * in &drm_device.mode_config. This facilitates conversion of existing
194 * code because it removes the need to manually deal with the acquisition
195 * context, but it is also brittle because the context is global and care must
196 * be taken not to nest calls. New code should pass the acquisition context
197 * directly to the drm_modeset_drop_locks() function.
198 */
199void drm_modeset_unlock_all(struct drm_device *dev)
200{
201 struct drm_mode_config *config = &dev->mode_config;
202 struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
203
204 if (WARN_ON(!ctx))
205 return;
206
207 config->acquire_ctx = NULL;
208 drm_modeset_drop_locks(ctx);
209 drm_modeset_acquire_fini(ctx);
210
211 kfree(objp: ctx);
212
213 mutex_unlock(lock: &dev->mode_config.mutex);
214}
215EXPORT_SYMBOL(drm_modeset_unlock_all);
216
217/**
218 * drm_warn_on_modeset_not_all_locked - check that all modeset locks are locked
219 * @dev: device
220 *
221 * Useful as a debug assert.
222 */
223void drm_warn_on_modeset_not_all_locked(struct drm_device *dev)
224{
225 struct drm_crtc *crtc;
226
227 /* Locking is currently fubar in the panic handler. */
228 if (oops_in_progress)
229 return;
230
231 drm_for_each_crtc(crtc, dev)
232 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
233
234 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
235 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
236}
237EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked);
238
239/**
240 * drm_modeset_acquire_init - initialize acquire context
241 * @ctx: the acquire context
242 * @flags: 0 or %DRM_MODESET_ACQUIRE_INTERRUPTIBLE
243 *
244 * When passing %DRM_MODESET_ACQUIRE_INTERRUPTIBLE to @flags,
245 * all calls to drm_modeset_lock() will perform an interruptible
246 * wait.
247 */
248void drm_modeset_acquire_init(struct drm_modeset_acquire_ctx *ctx,
249 uint32_t flags)
250{
251 memset(s: ctx, c: 0, n: sizeof(*ctx));
252 ww_acquire_init(ctx: &ctx->ww_ctx, ww_class: &crtc_ww_class);
253 INIT_LIST_HEAD(list: &ctx->locked);
254
255 if (flags & DRM_MODESET_ACQUIRE_INTERRUPTIBLE)
256 ctx->interruptible = true;
257}
258EXPORT_SYMBOL(drm_modeset_acquire_init);
259
260/**
261 * drm_modeset_acquire_fini - cleanup acquire context
262 * @ctx: the acquire context
263 */
264void drm_modeset_acquire_fini(struct drm_modeset_acquire_ctx *ctx)
265{
266 ww_acquire_fini(ctx: &ctx->ww_ctx);
267}
268EXPORT_SYMBOL(drm_modeset_acquire_fini);
269
270/**
271 * drm_modeset_drop_locks - drop all locks
272 * @ctx: the acquire context
273 *
274 * Drop all locks currently held against this acquire context.
275 */
276void drm_modeset_drop_locks(struct drm_modeset_acquire_ctx *ctx)
277{
278 if (WARN_ON(ctx->contended))
279 __drm_stack_depot_print(stack_depot: ctx->stack_depot);
280
281 while (!list_empty(head: &ctx->locked)) {
282 struct drm_modeset_lock *lock;
283
284 lock = list_first_entry(&ctx->locked,
285 struct drm_modeset_lock, head);
286
287 drm_modeset_unlock(lock);
288 }
289}
290EXPORT_SYMBOL(drm_modeset_drop_locks);
291
292static inline int modeset_lock(struct drm_modeset_lock *lock,
293 struct drm_modeset_acquire_ctx *ctx,
294 bool interruptible, bool slow)
295{
296 int ret;
297
298 if (WARN_ON(ctx->contended))
299 __drm_stack_depot_print(stack_depot: ctx->stack_depot);
300
301 if (ctx->trylock_only) {
302 lockdep_assert_held(&ctx->ww_ctx);
303
304 if (!ww_mutex_trylock(lock: &lock->mutex, NULL))
305 return -EBUSY;
306 else
307 return 0;
308 } else if (interruptible && slow) {
309 ret = ww_mutex_lock_slow_interruptible(lock: &lock->mutex, ctx: &ctx->ww_ctx);
310 } else if (interruptible) {
311 ret = ww_mutex_lock_interruptible(lock: &lock->mutex, ctx: &ctx->ww_ctx);
312 } else if (slow) {
313 ww_mutex_lock_slow(lock: &lock->mutex, ctx: &ctx->ww_ctx);
314 ret = 0;
315 } else {
316 ret = ww_mutex_lock(lock: &lock->mutex, ctx: &ctx->ww_ctx);
317 }
318 if (!ret) {
319 WARN_ON(!list_empty(&lock->head));
320 list_add(new: &lock->head, head: &ctx->locked);
321 } else if (ret == -EALREADY) {
322 /* we already hold the lock.. this is fine. For atomic
323 * we will need to be able to drm_modeset_lock() things
324 * without having to keep track of what is already locked
325 * or not.
326 */
327 ret = 0;
328 } else if (ret == -EDEADLK) {
329 ctx->contended = lock;
330 ctx->stack_depot = __drm_stack_depot_save();
331 }
332
333 return ret;
334}
335
336/**
337 * drm_modeset_backoff - deadlock avoidance backoff
338 * @ctx: the acquire context
339 *
340 * If deadlock is detected (ie. drm_modeset_lock() returns -EDEADLK),
341 * you must call this function to drop all currently held locks and
342 * block until the contended lock becomes available.
343 *
344 * This function returns 0 on success, or -ERESTARTSYS if this context
345 * is initialized with %DRM_MODESET_ACQUIRE_INTERRUPTIBLE and the
346 * wait has been interrupted.
347 */
348int drm_modeset_backoff(struct drm_modeset_acquire_ctx *ctx)
349{
350 struct drm_modeset_lock *contended = ctx->contended;
351
352 ctx->contended = NULL;
353 ctx->stack_depot = 0;
354
355 if (WARN_ON(!contended))
356 return 0;
357
358 drm_modeset_drop_locks(ctx);
359
360 return modeset_lock(lock: contended, ctx, interruptible: ctx->interruptible, slow: true);
361}
362EXPORT_SYMBOL(drm_modeset_backoff);
363
364/**
365 * drm_modeset_lock_init - initialize lock
366 * @lock: lock to init
367 */
368void drm_modeset_lock_init(struct drm_modeset_lock *lock)
369{
370 ww_mutex_init(lock: &lock->mutex, ww_class: &crtc_ww_class);
371 INIT_LIST_HEAD(list: &lock->head);
372 __drm_stack_depot_init();
373}
374EXPORT_SYMBOL(drm_modeset_lock_init);
375
376/**
377 * drm_modeset_lock - take modeset lock
378 * @lock: lock to take
379 * @ctx: acquire ctx
380 *
381 * If @ctx is not NULL, then its ww acquire context is used and the
382 * lock will be tracked by the context and can be released by calling
383 * drm_modeset_drop_locks(). If -EDEADLK is returned, this means a
384 * deadlock scenario has been detected and it is an error to attempt
385 * to take any more locks without first calling drm_modeset_backoff().
386 *
387 * If the @ctx is not NULL and initialized with
388 * %DRM_MODESET_ACQUIRE_INTERRUPTIBLE, this function will fail with
389 * -ERESTARTSYS when interrupted.
390 *
391 * If @ctx is NULL then the function call behaves like a normal,
392 * uninterruptible non-nesting mutex_lock() call.
393 */
394int drm_modeset_lock(struct drm_modeset_lock *lock,
395 struct drm_modeset_acquire_ctx *ctx)
396{
397 if (ctx)
398 return modeset_lock(lock, ctx, interruptible: ctx->interruptible, slow: false);
399
400 ww_mutex_lock(lock: &lock->mutex, NULL);
401 return 0;
402}
403EXPORT_SYMBOL(drm_modeset_lock);
404
405/**
406 * drm_modeset_lock_single_interruptible - take a single modeset lock
407 * @lock: lock to take
408 *
409 * This function behaves as drm_modeset_lock() with a NULL context,
410 * but performs interruptible waits.
411 *
412 * This function returns 0 on success, or -ERESTARTSYS when interrupted.
413 */
414int drm_modeset_lock_single_interruptible(struct drm_modeset_lock *lock)
415{
416 return ww_mutex_lock_interruptible(lock: &lock->mutex, NULL);
417}
418EXPORT_SYMBOL(drm_modeset_lock_single_interruptible);
419
420/**
421 * drm_modeset_unlock - drop modeset lock
422 * @lock: lock to release
423 */
424void drm_modeset_unlock(struct drm_modeset_lock *lock)
425{
426 list_del_init(entry: &lock->head);
427 ww_mutex_unlock(lock: &lock->mutex);
428}
429EXPORT_SYMBOL(drm_modeset_unlock);
430
431/**
432 * drm_modeset_lock_all_ctx - take all modeset locks
433 * @dev: DRM device
434 * @ctx: lock acquisition context
435 *
436 * This function takes all modeset locks, suitable where a more fine-grained
437 * scheme isn't (yet) implemented.
438 *
439 * Unlike drm_modeset_lock_all(), it doesn't take the &drm_mode_config.mutex
440 * since that lock isn't required for modeset state changes. Callers which
441 * need to grab that lock too need to do so outside of the acquire context
442 * @ctx.
443 *
444 * Locks acquired with this function should be released by calling the
445 * drm_modeset_drop_locks() function on @ctx.
446 *
447 * See also: DRM_MODESET_LOCK_ALL_BEGIN() and DRM_MODESET_LOCK_ALL_END()
448 *
449 * Returns: 0 on success or a negative error-code on failure.
450 */
451int drm_modeset_lock_all_ctx(struct drm_device *dev,
452 struct drm_modeset_acquire_ctx *ctx)
453{
454 struct drm_private_obj *privobj;
455 struct drm_crtc *crtc;
456 struct drm_plane *plane;
457 int ret;
458
459 ret = drm_modeset_lock(&dev->mode_config.connection_mutex, ctx);
460 if (ret)
461 return ret;
462
463 drm_for_each_crtc(crtc, dev) {
464 ret = drm_modeset_lock(&crtc->mutex, ctx);
465 if (ret)
466 return ret;
467 }
468
469 drm_for_each_plane(plane, dev) {
470 ret = drm_modeset_lock(&plane->mutex, ctx);
471 if (ret)
472 return ret;
473 }
474
475 drm_for_each_privobj(privobj, dev) {
476 ret = drm_modeset_lock(&privobj->lock, ctx);
477 if (ret)
478 return ret;
479 }
480
481 return 0;
482}
483EXPORT_SYMBOL(drm_modeset_lock_all_ctx);
484