drm_modeset_lock.c 11 KB

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