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, 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. WARN_ON(config->acquire_ctx);
  108. /*
  109. * We hold the locks now, so it is safe to stash the acquisition
  110. * context for drm_modeset_unlock_all().
  111. */
  112. config->acquire_ctx = ctx;
  113. drm_warn_on_modeset_not_all_locked(dev);
  114. }
  115. EXPORT_SYMBOL(drm_modeset_lock_all);
  116. /**
  117. * drm_modeset_unlock_all - drop all modeset locks
  118. * @dev: DRM device
  119. *
  120. * This function drops all modeset locks taken by a previous call to the
  121. * drm_modeset_lock_all() function.
  122. *
  123. * This function is deprecated. It uses the lock acquisition context stored
  124. * in &drm_device.mode_config. This facilitates conversion of existing
  125. * code because it removes the need to manually deal with the acquisition
  126. * context, but it is also brittle because the context is global and care must
  127. * be taken not to nest calls. New code should pass the acquisition context
  128. * directly to the drm_modeset_drop_locks() function.
  129. */
  130. void drm_modeset_unlock_all(struct drm_device *dev)
  131. {
  132. struct drm_mode_config *config = &dev->mode_config;
  133. struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
  134. if (WARN_ON(!ctx))
  135. return;
  136. config->acquire_ctx = NULL;
  137. drm_modeset_drop_locks(ctx);
  138. drm_modeset_acquire_fini(ctx);
  139. kfree(ctx);
  140. mutex_unlock(&dev->mode_config.mutex);
  141. }
  142. EXPORT_SYMBOL(drm_modeset_unlock_all);
  143. /**
  144. * drm_warn_on_modeset_not_all_locked - check that all modeset locks are locked
  145. * @dev: device
  146. *
  147. * Useful as a debug assert.
  148. */
  149. void drm_warn_on_modeset_not_all_locked(struct drm_device *dev)
  150. {
  151. struct drm_crtc *crtc;
  152. /* Locking is currently fubar in the panic handler. */
  153. if (oops_in_progress)
  154. return;
  155. drm_for_each_crtc(crtc, dev)
  156. WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
  157. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  158. WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
  159. }
  160. EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked);
  161. /**
  162. * drm_modeset_acquire_init - initialize acquire context
  163. * @ctx: the acquire context
  164. * @flags: 0 or %DRM_MODESET_ACQUIRE_INTERRUPTIBLE
  165. *
  166. * When passing %DRM_MODESET_ACQUIRE_INTERRUPTIBLE to @flags,
  167. * all calls to drm_modeset_lock() will perform an interruptible
  168. * wait.
  169. */
  170. void drm_modeset_acquire_init(struct drm_modeset_acquire_ctx *ctx,
  171. uint32_t flags)
  172. {
  173. memset(ctx, 0, sizeof(*ctx));
  174. ww_acquire_init(&ctx->ww_ctx, &crtc_ww_class);
  175. INIT_LIST_HEAD(&ctx->locked);
  176. if (flags & DRM_MODESET_ACQUIRE_INTERRUPTIBLE)
  177. ctx->interruptible = true;
  178. }
  179. EXPORT_SYMBOL(drm_modeset_acquire_init);
  180. /**
  181. * drm_modeset_acquire_fini - cleanup acquire context
  182. * @ctx: the acquire context
  183. */
  184. void drm_modeset_acquire_fini(struct drm_modeset_acquire_ctx *ctx)
  185. {
  186. ww_acquire_fini(&ctx->ww_ctx);
  187. }
  188. EXPORT_SYMBOL(drm_modeset_acquire_fini);
  189. /**
  190. * drm_modeset_drop_locks - drop all locks
  191. * @ctx: the acquire context
  192. *
  193. * Drop all locks currently held against this acquire context.
  194. */
  195. void drm_modeset_drop_locks(struct drm_modeset_acquire_ctx *ctx)
  196. {
  197. WARN_ON(ctx->contended);
  198. while (!list_empty(&ctx->locked)) {
  199. struct drm_modeset_lock *lock;
  200. lock = list_first_entry(&ctx->locked,
  201. struct drm_modeset_lock, head);
  202. drm_modeset_unlock(lock);
  203. }
  204. }
  205. EXPORT_SYMBOL(drm_modeset_drop_locks);
  206. static inline int modeset_lock(struct drm_modeset_lock *lock,
  207. struct drm_modeset_acquire_ctx *ctx,
  208. bool interruptible, bool slow)
  209. {
  210. int ret;
  211. WARN_ON(ctx->contended);
  212. if (ctx->trylock_only) {
  213. lockdep_assert_held(&ctx->ww_ctx);
  214. if (!ww_mutex_trylock(&lock->mutex))
  215. return -EBUSY;
  216. else
  217. return 0;
  218. } else if (interruptible && slow) {
  219. ret = ww_mutex_lock_slow_interruptible(&lock->mutex, &ctx->ww_ctx);
  220. } else if (interruptible) {
  221. ret = ww_mutex_lock_interruptible(&lock->mutex, &ctx->ww_ctx);
  222. } else if (slow) {
  223. ww_mutex_lock_slow(&lock->mutex, &ctx->ww_ctx);
  224. ret = 0;
  225. } else {
  226. ret = ww_mutex_lock(&lock->mutex, &ctx->ww_ctx);
  227. }
  228. if (!ret) {
  229. WARN_ON(!list_empty(&lock->head));
  230. list_add(&lock->head, &ctx->locked);
  231. } else if (ret == -EALREADY) {
  232. /* we already hold the lock.. this is fine. For atomic
  233. * we will need to be able to drm_modeset_lock() things
  234. * without having to keep track of what is already locked
  235. * or not.
  236. */
  237. ret = 0;
  238. } else if (ret == -EDEADLK) {
  239. ctx->contended = lock;
  240. }
  241. return ret;
  242. }
  243. /**
  244. * drm_modeset_backoff - deadlock avoidance backoff
  245. * @ctx: the acquire context
  246. *
  247. * If deadlock is detected (ie. drm_modeset_lock() returns -EDEADLK),
  248. * you must call this function to drop all currently held locks and
  249. * block until the contended lock becomes available.
  250. *
  251. * This function returns 0 on success, or -ERESTARTSYS if this context
  252. * is initialized with %DRM_MODESET_ACQUIRE_INTERRUPTIBLE and the
  253. * wait has been interrupted.
  254. */
  255. int drm_modeset_backoff(struct drm_modeset_acquire_ctx *ctx)
  256. {
  257. struct drm_modeset_lock *contended = ctx->contended;
  258. ctx->contended = NULL;
  259. if (WARN_ON(!contended))
  260. return 0;
  261. drm_modeset_drop_locks(ctx);
  262. return modeset_lock(contended, ctx, ctx->interruptible, true);
  263. }
  264. EXPORT_SYMBOL(drm_modeset_backoff);
  265. /**
  266. * drm_modeset_lock_init - initialize lock
  267. * @lock: lock to init
  268. */
  269. void drm_modeset_lock_init(struct drm_modeset_lock *lock)
  270. {
  271. ww_mutex_init(&lock->mutex, &crtc_ww_class);
  272. INIT_LIST_HEAD(&lock->head);
  273. }
  274. EXPORT_SYMBOL(drm_modeset_lock_init);
  275. /**
  276. * drm_modeset_lock - take modeset lock
  277. * @lock: lock to take
  278. * @ctx: acquire ctx
  279. *
  280. * If @ctx is not NULL, then its ww acquire context is used and the
  281. * lock will be tracked by the context and can be released by calling
  282. * drm_modeset_drop_locks(). If -EDEADLK is returned, this means a
  283. * deadlock scenario has been detected and it is an error to attempt
  284. * to take any more locks without first calling drm_modeset_backoff().
  285. *
  286. * If the @ctx is not NULL and initialized with
  287. * %DRM_MODESET_ACQUIRE_INTERRUPTIBLE, this function will fail with
  288. * -ERESTARTSYS when interrupted.
  289. *
  290. * If @ctx is NULL then the function call behaves like a normal,
  291. * uninterruptible non-nesting mutex_lock() call.
  292. */
  293. int drm_modeset_lock(struct drm_modeset_lock *lock,
  294. struct drm_modeset_acquire_ctx *ctx)
  295. {
  296. if (ctx)
  297. return modeset_lock(lock, ctx, ctx->interruptible, false);
  298. ww_mutex_lock(&lock->mutex, NULL);
  299. return 0;
  300. }
  301. EXPORT_SYMBOL(drm_modeset_lock);
  302. /**
  303. * drm_modeset_lock_single_interruptible - take a single modeset lock
  304. * @lock: lock to take
  305. *
  306. * This function behaves as drm_modeset_lock() with a NULL context,
  307. * but performs interruptible waits.
  308. *
  309. * This function returns 0 on success, or -ERESTARTSYS when interrupted.
  310. */
  311. int drm_modeset_lock_single_interruptible(struct drm_modeset_lock *lock)
  312. {
  313. return ww_mutex_lock_interruptible(&lock->mutex, NULL);
  314. }
  315. EXPORT_SYMBOL(drm_modeset_lock_single_interruptible);
  316. /**
  317. * drm_modeset_unlock - drop modeset lock
  318. * @lock: lock to release
  319. */
  320. void drm_modeset_unlock(struct drm_modeset_lock *lock)
  321. {
  322. list_del_init(&lock->head);
  323. ww_mutex_unlock(&lock->mutex);
  324. }
  325. EXPORT_SYMBOL(drm_modeset_unlock);
  326. /**
  327. * drm_modeset_lock_all_ctx - take all modeset locks
  328. * @dev: DRM device
  329. * @ctx: lock acquisition context
  330. *
  331. * This function takes all modeset locks, suitable where a more fine-grained
  332. * scheme isn't (yet) implemented.
  333. *
  334. * Unlike drm_modeset_lock_all(), it doesn't take the &drm_mode_config.mutex
  335. * since that lock isn't required for modeset state changes. Callers which
  336. * need to grab that lock too need to do so outside of the acquire context
  337. * @ctx.
  338. *
  339. * Locks acquired with this function should be released by calling the
  340. * drm_modeset_drop_locks() function on @ctx.
  341. *
  342. * Returns: 0 on success or a negative error-code on failure.
  343. */
  344. int drm_modeset_lock_all_ctx(struct drm_device *dev,
  345. struct drm_modeset_acquire_ctx *ctx)
  346. {
  347. struct drm_crtc *crtc;
  348. struct drm_plane *plane;
  349. int ret;
  350. ret = drm_modeset_lock(&dev->mode_config.connection_mutex, ctx);
  351. if (ret)
  352. return ret;
  353. drm_for_each_crtc(crtc, dev) {
  354. ret = drm_modeset_lock(&crtc->mutex, ctx);
  355. if (ret)
  356. return ret;
  357. }
  358. drm_for_each_plane(plane, dev) {
  359. ret = drm_modeset_lock(&plane->mutex, ctx);
  360. if (ret)
  361. return ret;
  362. }
  363. return 0;
  364. }
  365. EXPORT_SYMBOL(drm_modeset_lock_all_ctx);