drm_modeset_lock.c 6.9 KB

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