drm_modeset_lock.c 13 KB

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