drm_modeset_lock.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. #include <drm/drmP.h>
  24. #include <drm/drm_crtc.h>
  25. #include <drm/drm_modeset_lock.h>
  26. /**
  27. * DOC: kms locking
  28. *
  29. * As KMS moves toward more fine grained locking, and atomic ioctl where
  30. * userspace can indirectly control locking order, it becomes necessary
  31. * to use &ww_mutex and acquire-contexts to avoid deadlocks. But because
  32. * the locking is more distributed around the driver code, we want a bit
  33. * of extra utility/tracking out of our acquire-ctx. This is provided
  34. * by &struct drm_modeset_lock and &struct drm_modeset_acquire_ctx.
  35. *
  36. * For basic principles of &ww_mutex, see: Documentation/locking/ww-mutex-design.txt
  37. *
  38. * The basic usage pattern is to::
  39. *
  40. * drm_modeset_acquire_init(&ctx)
  41. * retry:
  42. * foreach (lock in random_ordered_set_of_locks) {
  43. * ret = drm_modeset_lock(lock, &ctx)
  44. * if (ret == -EDEADLK) {
  45. * drm_modeset_backoff(&ctx);
  46. * goto retry;
  47. * }
  48. * }
  49. * ... do stuff ...
  50. * drm_modeset_drop_locks(&ctx);
  51. * drm_modeset_acquire_fini(&ctx);
  52. *
  53. * If all that is needed is a single modeset lock, then the &struct
  54. * drm_modeset_acquire_ctx is not needed and the locking can be simplified
  55. * by passing a NULL instead of ctx in the drm_modeset_lock()
  56. * call and, when done, by calling drm_modeset_unlock().
  57. *
  58. * On top of these per-object locks using &ww_mutex there's also an overall
  59. * &drm_mode_config.mutex, for protecting everything else. Mostly this means
  60. * probe state of connectors, and preventing hotplug add/removal of connectors.
  61. *
  62. * Finally there's a bunch of dedicated locks to protect drm core internal
  63. * lists and lookup data structures.
  64. */
  65. static DEFINE_WW_CLASS(crtc_ww_class);
  66. /**
  67. * drm_modeset_lock_all - take all modeset locks
  68. * @dev: DRM device
  69. *
  70. * This function takes all modeset locks, suitable where a more fine-grained
  71. * scheme isn't (yet) implemented. Locks must be dropped by calling the
  72. * drm_modeset_unlock_all() function.
  73. *
  74. * This function is deprecated. It allocates a lock acquisition context and
  75. * stores it in &drm_device.mode_config. This facilitate conversion of
  76. * existing code because it removes the need to manually deal with the
  77. * acquisition context, but it is also brittle because the context is global
  78. * and care must be taken not to nest calls. New code should use the
  79. * drm_modeset_lock_all_ctx() function and pass in the context explicitly.
  80. */
  81. void drm_modeset_lock_all(struct drm_device *dev)
  82. {
  83. struct drm_mode_config *config = &dev->mode_config;
  84. struct drm_modeset_acquire_ctx *ctx;
  85. int ret;
  86. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  87. if (WARN_ON(!ctx))
  88. return;
  89. mutex_lock(&config->mutex);
  90. drm_modeset_acquire_init(ctx, 0);
  91. retry:
  92. ret = drm_modeset_lock_all_ctx(dev, ctx);
  93. if (ret < 0) {
  94. if (ret == -EDEADLK) {
  95. drm_modeset_backoff(ctx);
  96. goto retry;
  97. }
  98. drm_modeset_acquire_fini(ctx);
  99. kfree(ctx);
  100. return;
  101. }
  102. WARN_ON(config->acquire_ctx);
  103. /*
  104. * We hold the locks now, so it is safe to stash the acquisition
  105. * context for drm_modeset_unlock_all().
  106. */
  107. config->acquire_ctx = ctx;
  108. drm_warn_on_modeset_not_all_locked(dev);
  109. }
  110. EXPORT_SYMBOL(drm_modeset_lock_all);
  111. /**
  112. * drm_modeset_unlock_all - drop all modeset locks
  113. * @dev: DRM device
  114. *
  115. * This function drops all modeset locks taken by a previous call to the
  116. * drm_modeset_lock_all() function.
  117. *
  118. * This function is deprecated. It uses the lock acquisition context stored
  119. * in &drm_device.mode_config. This facilitates conversion of existing
  120. * code because it removes the need to manually deal with the acquisition
  121. * context, but it is also brittle because the context is global and care must
  122. * be taken not to nest calls. New code should pass the acquisition context
  123. * directly to the drm_modeset_drop_locks() function.
  124. */
  125. void drm_modeset_unlock_all(struct drm_device *dev)
  126. {
  127. struct drm_mode_config *config = &dev->mode_config;
  128. struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
  129. if (WARN_ON(!ctx))
  130. return;
  131. config->acquire_ctx = NULL;
  132. drm_modeset_drop_locks(ctx);
  133. drm_modeset_acquire_fini(ctx);
  134. kfree(ctx);
  135. mutex_unlock(&dev->mode_config.mutex);
  136. }
  137. EXPORT_SYMBOL(drm_modeset_unlock_all);
  138. /**
  139. * drm_warn_on_modeset_not_all_locked - check that all modeset locks are locked
  140. * @dev: device
  141. *
  142. * Useful as a debug assert.
  143. */
  144. void drm_warn_on_modeset_not_all_locked(struct drm_device *dev)
  145. {
  146. struct drm_crtc *crtc;
  147. /* Locking is currently fubar in the panic handler. */
  148. if (oops_in_progress)
  149. return;
  150. drm_for_each_crtc(crtc, dev)
  151. WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
  152. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  153. WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
  154. }
  155. EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked);
  156. /**
  157. * drm_modeset_acquire_init - initialize acquire context
  158. * @ctx: the acquire context
  159. * @flags: for future
  160. */
  161. void drm_modeset_acquire_init(struct drm_modeset_acquire_ctx *ctx,
  162. uint32_t flags)
  163. {
  164. memset(ctx, 0, sizeof(*ctx));
  165. ww_acquire_init(&ctx->ww_ctx, &crtc_ww_class);
  166. INIT_LIST_HEAD(&ctx->locked);
  167. }
  168. EXPORT_SYMBOL(drm_modeset_acquire_init);
  169. /**
  170. * drm_modeset_acquire_fini - cleanup acquire context
  171. * @ctx: the acquire context
  172. */
  173. void drm_modeset_acquire_fini(struct drm_modeset_acquire_ctx *ctx)
  174. {
  175. ww_acquire_fini(&ctx->ww_ctx);
  176. }
  177. EXPORT_SYMBOL(drm_modeset_acquire_fini);
  178. /**
  179. * drm_modeset_drop_locks - drop all locks
  180. * @ctx: the acquire context
  181. *
  182. * Drop all locks currently held against this acquire context.
  183. */
  184. void drm_modeset_drop_locks(struct drm_modeset_acquire_ctx *ctx)
  185. {
  186. WARN_ON(ctx->contended);
  187. while (!list_empty(&ctx->locked)) {
  188. struct drm_modeset_lock *lock;
  189. lock = list_first_entry(&ctx->locked,
  190. struct drm_modeset_lock, head);
  191. drm_modeset_unlock(lock);
  192. }
  193. }
  194. EXPORT_SYMBOL(drm_modeset_drop_locks);
  195. static inline int modeset_lock(struct drm_modeset_lock *lock,
  196. struct drm_modeset_acquire_ctx *ctx,
  197. bool interruptible, bool slow)
  198. {
  199. int ret;
  200. WARN_ON(ctx->contended);
  201. if (ctx->trylock_only) {
  202. lockdep_assert_held(&ctx->ww_ctx);
  203. if (!ww_mutex_trylock(&lock->mutex))
  204. return -EBUSY;
  205. else
  206. return 0;
  207. } else if (interruptible && slow) {
  208. ret = ww_mutex_lock_slow_interruptible(&lock->mutex, &ctx->ww_ctx);
  209. } else if (interruptible) {
  210. ret = ww_mutex_lock_interruptible(&lock->mutex, &ctx->ww_ctx);
  211. } else if (slow) {
  212. ww_mutex_lock_slow(&lock->mutex, &ctx->ww_ctx);
  213. ret = 0;
  214. } else {
  215. ret = ww_mutex_lock(&lock->mutex, &ctx->ww_ctx);
  216. }
  217. if (!ret) {
  218. WARN_ON(!list_empty(&lock->head));
  219. list_add(&lock->head, &ctx->locked);
  220. } else if (ret == -EALREADY) {
  221. /* we already hold the lock.. this is fine. For atomic
  222. * we will need to be able to drm_modeset_lock() things
  223. * without having to keep track of what is already locked
  224. * or not.
  225. */
  226. ret = 0;
  227. } else if (ret == -EDEADLK) {
  228. ctx->contended = lock;
  229. }
  230. return ret;
  231. }
  232. static int modeset_backoff(struct drm_modeset_acquire_ctx *ctx,
  233. bool interruptible)
  234. {
  235. struct drm_modeset_lock *contended = ctx->contended;
  236. ctx->contended = NULL;
  237. if (WARN_ON(!contended))
  238. return 0;
  239. drm_modeset_drop_locks(ctx);
  240. return modeset_lock(contended, ctx, interruptible, true);
  241. }
  242. /**
  243. * drm_modeset_backoff - deadlock avoidance backoff
  244. * @ctx: the acquire context
  245. *
  246. * If deadlock is detected (ie. drm_modeset_lock() returns -EDEADLK),
  247. * you must call this function to drop all currently held locks and
  248. * block until the contended lock becomes available.
  249. */
  250. void drm_modeset_backoff(struct drm_modeset_acquire_ctx *ctx)
  251. {
  252. modeset_backoff(ctx, false);
  253. }
  254. EXPORT_SYMBOL(drm_modeset_backoff);
  255. /**
  256. * drm_modeset_backoff_interruptible - deadlock avoidance backoff
  257. * @ctx: the acquire context
  258. *
  259. * Interruptible version of drm_modeset_backoff()
  260. */
  261. int drm_modeset_backoff_interruptible(struct drm_modeset_acquire_ctx *ctx)
  262. {
  263. return modeset_backoff(ctx, true);
  264. }
  265. EXPORT_SYMBOL(drm_modeset_backoff_interruptible);
  266. /**
  267. * drm_modeset_lock_init - initialize lock
  268. * @lock: lock to init
  269. */
  270. void drm_modeset_lock_init(struct drm_modeset_lock *lock)
  271. {
  272. ww_mutex_init(&lock->mutex, &crtc_ww_class);
  273. INIT_LIST_HEAD(&lock->head);
  274. }
  275. EXPORT_SYMBOL(drm_modeset_lock_init);
  276. /**
  277. * drm_modeset_lock - take modeset lock
  278. * @lock: lock to take
  279. * @ctx: acquire ctx
  280. *
  281. * If @ctx is not NULL, then its ww acquire context is used and the
  282. * lock will be tracked by the context and can be released by calling
  283. * drm_modeset_drop_locks(). If -EDEADLK is returned, this means a
  284. * deadlock scenario has been detected and it is an error to attempt
  285. * to take any more locks without first calling drm_modeset_backoff().
  286. *
  287. * If @ctx is NULL then the function call behaves like a normal,
  288. * non-nesting mutex_lock() call.
  289. */
  290. int drm_modeset_lock(struct drm_modeset_lock *lock,
  291. struct drm_modeset_acquire_ctx *ctx)
  292. {
  293. if (ctx)
  294. return modeset_lock(lock, ctx, false, false);
  295. ww_mutex_lock(&lock->mutex, NULL);
  296. return 0;
  297. }
  298. EXPORT_SYMBOL(drm_modeset_lock);
  299. /**
  300. * drm_modeset_lock_interruptible - take modeset lock
  301. * @lock: lock to take
  302. * @ctx: acquire ctx
  303. *
  304. * Interruptible version of drm_modeset_lock()
  305. */
  306. int drm_modeset_lock_interruptible(struct drm_modeset_lock *lock,
  307. struct drm_modeset_acquire_ctx *ctx)
  308. {
  309. if (ctx)
  310. return modeset_lock(lock, ctx, true, false);
  311. return ww_mutex_lock_interruptible(&lock->mutex, NULL);
  312. }
  313. EXPORT_SYMBOL(drm_modeset_lock_interruptible);
  314. /**
  315. * drm_modeset_unlock - drop modeset lock
  316. * @lock: lock to release
  317. */
  318. void drm_modeset_unlock(struct drm_modeset_lock *lock)
  319. {
  320. list_del_init(&lock->head);
  321. ww_mutex_unlock(&lock->mutex);
  322. }
  323. EXPORT_SYMBOL(drm_modeset_unlock);
  324. /**
  325. * drm_modeset_lock_all_ctx - take all modeset locks
  326. * @dev: DRM device
  327. * @ctx: lock acquisition context
  328. *
  329. * This function takes all modeset locks, suitable where a more fine-grained
  330. * scheme isn't (yet) implemented.
  331. *
  332. * Unlike drm_modeset_lock_all(), it doesn't take the &drm_mode_config.mutex
  333. * since that lock isn't required for modeset state changes. Callers which
  334. * need to grab that lock too need to do so outside of the acquire context
  335. * @ctx.
  336. *
  337. * Locks acquired with this function should be released by calling the
  338. * drm_modeset_drop_locks() function on @ctx.
  339. *
  340. * Returns: 0 on success or a negative error-code on failure.
  341. */
  342. int drm_modeset_lock_all_ctx(struct drm_device *dev,
  343. struct drm_modeset_acquire_ctx *ctx)
  344. {
  345. struct drm_crtc *crtc;
  346. struct drm_plane *plane;
  347. int ret;
  348. ret = drm_modeset_lock(&dev->mode_config.connection_mutex, ctx);
  349. if (ret)
  350. return ret;
  351. drm_for_each_crtc(crtc, dev) {
  352. ret = drm_modeset_lock(&crtc->mutex, ctx);
  353. if (ret)
  354. return ret;
  355. }
  356. drm_for_each_plane(plane, dev) {
  357. ret = drm_modeset_lock(&plane->mutex, ctx);
  358. if (ret)
  359. return ret;
  360. }
  361. return 0;
  362. }
  363. EXPORT_SYMBOL(drm_modeset_lock_all_ctx);