drm_modeset_lock.c 12 KB

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