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/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  *
52  *     ... do stuff ...
53  *
54  *     drm_modeset_drop_locks(&ctx);
55  *     drm_modeset_acquire_fini(&ctx);
56  */
57 
58 
59 /**
60  * drm_modeset_acquire_init - initialize acquire context
61  * @ctx: the acquire context
62  * @flags: for future
63  */
64 void drm_modeset_acquire_init(struct drm_modeset_acquire_ctx *ctx,
65 		uint32_t flags)
66 {
67 	ww_acquire_init(&ctx->ww_ctx, &crtc_ww_class);
68 	INIT_LIST_HEAD(&ctx->locked);
69 }
70 EXPORT_SYMBOL(drm_modeset_acquire_init);
71 
72 /**
73  * drm_modeset_acquire_fini - cleanup acquire context
74  * @ctx: the acquire context
75  */
76 void drm_modeset_acquire_fini(struct drm_modeset_acquire_ctx *ctx)
77 {
78 	ww_acquire_fini(&ctx->ww_ctx);
79 }
80 EXPORT_SYMBOL(drm_modeset_acquire_fini);
81 
82 /**
83  * drm_modeset_drop_locks - drop all locks
84  * @ctx: the acquire context
85  *
86  * Drop all locks currently held against this acquire context.
87  */
88 void drm_modeset_drop_locks(struct drm_modeset_acquire_ctx *ctx)
89 {
90 	WARN_ON(ctx->contended);
91 	while (!list_empty(&ctx->locked)) {
92 		struct drm_modeset_lock *lock;
93 
94 		lock = list_first_entry(&ctx->locked,
95 				struct drm_modeset_lock, head);
96 
97 		drm_modeset_unlock(lock);
98 	}
99 }
100 EXPORT_SYMBOL(drm_modeset_drop_locks);
101 
102 static inline int modeset_lock(struct drm_modeset_lock *lock,
103 		struct drm_modeset_acquire_ctx *ctx,
104 		bool interruptible, bool slow)
105 {
106 	int ret;
107 
108 	WARN_ON(ctx->contended);
109 
110 	if (interruptible && slow) {
111 		ret = ww_mutex_lock_slow_interruptible(&lock->mutex, &ctx->ww_ctx);
112 	} else if (interruptible) {
113 		ret = ww_mutex_lock_interruptible(&lock->mutex, &ctx->ww_ctx);
114 	} else if (slow) {
115 		ww_mutex_lock_slow(&lock->mutex, &ctx->ww_ctx);
116 		ret = 0;
117 	} else {
118 		ret = ww_mutex_lock(&lock->mutex, &ctx->ww_ctx);
119 	}
120 	if (!ret) {
121 		WARN_ON(!list_empty(&lock->head));
122 		list_add(&lock->head, &ctx->locked);
123 	} else if (ret == -EALREADY) {
124 		/* we already hold the lock.. this is fine.  For atomic
125 		 * we will need to be able to drm_modeset_lock() things
126 		 * without having to keep track of what is already locked
127 		 * or not.
128 		 */
129 		ret = 0;
130 	} else if (ret == -EDEADLK) {
131 		ctx->contended = lock;
132 	}
133 
134 	return ret;
135 }
136 
137 static int modeset_backoff(struct drm_modeset_acquire_ctx *ctx,
138 		bool interruptible)
139 {
140 	struct drm_modeset_lock *contended = ctx->contended;
141 
142 	ctx->contended = NULL;
143 
144 	if (WARN_ON(!contended))
145 		return 0;
146 
147 	drm_modeset_drop_locks(ctx);
148 
149 	return modeset_lock(contended, ctx, interruptible, true);
150 }
151 
152 /**
153  * drm_modeset_backoff - deadlock avoidance backoff
154  * @ctx: the acquire context
155  *
156  * If deadlock is detected (ie. drm_modeset_lock() returns -EDEADLK),
157  * you must call this function to drop all currently held locks and
158  * block until the contended lock becomes available.
159  */
160 void drm_modeset_backoff(struct drm_modeset_acquire_ctx *ctx)
161 {
162 	modeset_backoff(ctx, false);
163 }
164 EXPORT_SYMBOL(drm_modeset_backoff);
165 
166 /**
167  * drm_modeset_backoff_interruptible - deadlock avoidance backoff
168  * @ctx: the acquire context
169  *
170  * Interruptible version of drm_modeset_backoff()
171  */
172 int drm_modeset_backoff_interruptible(struct drm_modeset_acquire_ctx *ctx)
173 {
174 	return modeset_backoff(ctx, true);
175 }
176 EXPORT_SYMBOL(drm_modeset_backoff_interruptible);
177 
178 /**
179  * drm_modeset_lock - take modeset lock
180  * @lock: lock to take
181  * @ctx: acquire ctx
182  *
183  * If ctx is not NULL, then its ww acquire context is used and the
184  * lock will be tracked by the context and can be released by calling
185  * drm_modeset_drop_locks().  If -EDEADLK is returned, this means a
186  * deadlock scenario has been detected and it is an error to attempt
187  * to take any more locks without first calling drm_modeset_backoff().
188  */
189 int drm_modeset_lock(struct drm_modeset_lock *lock,
190 		struct drm_modeset_acquire_ctx *ctx)
191 {
192 	if (ctx)
193 		return modeset_lock(lock, ctx, false, false);
194 
195 	ww_mutex_lock(&lock->mutex, NULL);
196 	return 0;
197 }
198 EXPORT_SYMBOL(drm_modeset_lock);
199 
200 /**
201  * drm_modeset_lock_interruptible - take modeset lock
202  * @lock: lock to take
203  * @ctx: acquire ctx
204  *
205  * Interruptible version of drm_modeset_lock()
206  */
207 int drm_modeset_lock_interruptible(struct drm_modeset_lock *lock,
208 		struct drm_modeset_acquire_ctx *ctx)
209 {
210 	if (ctx)
211 		return modeset_lock(lock, ctx, true, false);
212 
213 	return ww_mutex_lock_interruptible(&lock->mutex, NULL);
214 }
215 EXPORT_SYMBOL(drm_modeset_lock_interruptible);
216 
217 /**
218  * drm_modeset_unlock - drop modeset lock
219  * @lock: lock to release
220  */
221 void drm_modeset_unlock(struct drm_modeset_lock *lock)
222 {
223 	list_del_init(&lock->head);
224 	ww_mutex_unlock(&lock->mutex);
225 }
226 EXPORT_SYMBOL(drm_modeset_unlock);
227 
228 /* Temporary.. until we have sufficiently fine grained locking, there
229  * are a couple scenarios where it is convenient to grab all crtc locks.
230  * It is planned to remove this:
231  */
232 int drm_modeset_lock_all_crtcs(struct drm_device *dev,
233 		struct drm_modeset_acquire_ctx *ctx)
234 {
235 	struct drm_mode_config *config = &dev->mode_config;
236 	struct drm_crtc *crtc;
237 	int ret = 0;
238 
239 	list_for_each_entry(crtc, &config->crtc_list, head) {
240 		ret = drm_modeset_lock(&crtc->mutex, ctx);
241 		if (ret)
242 			return ret;
243 	}
244 
245 	return 0;
246 }
247 EXPORT_SYMBOL(drm_modeset_lock_all_crtcs);
248