drm_modeset_lock.c 12 KB

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