drm_modeset_lock.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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 drm_modeset_lock / 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. *
  50. * ... do stuff ...
  51. *
  52. * drm_modeset_drop_locks(&ctx);
  53. * drm_modeset_acquire_fini(&ctx);
  54. */
  55. /**
  56. * __drm_modeset_lock_all - internal helper to grab all modeset locks
  57. * @dev: DRM device
  58. * @trylock: trylock mode for atomic contexts
  59. *
  60. * This is a special version of drm_modeset_lock_all() which can also be used in
  61. * atomic contexts. Then @trylock must be set to true.
  62. *
  63. * Returns:
  64. * 0 on success or negative error code on failure.
  65. */
  66. int __drm_modeset_lock_all(struct drm_device *dev,
  67. bool trylock)
  68. {
  69. struct drm_mode_config *config = &dev->mode_config;
  70. struct drm_modeset_acquire_ctx *ctx;
  71. int ret;
  72. ctx = kzalloc(sizeof(*ctx),
  73. trylock ? GFP_ATOMIC : GFP_KERNEL);
  74. if (!ctx)
  75. return -ENOMEM;
  76. if (trylock) {
  77. if (!mutex_trylock(&config->mutex))
  78. return -EBUSY;
  79. } else {
  80. mutex_lock(&config->mutex);
  81. }
  82. drm_modeset_acquire_init(ctx, 0);
  83. ctx->trylock_only = trylock;
  84. retry:
  85. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  86. if (ret)
  87. goto fail;
  88. ret = drm_modeset_lock_all_crtcs(dev, ctx);
  89. if (ret)
  90. goto fail;
  91. WARN_ON(config->acquire_ctx);
  92. /* now we hold the locks, so now that it is safe, stash the
  93. * ctx for drm_modeset_unlock_all():
  94. */
  95. config->acquire_ctx = ctx;
  96. drm_warn_on_modeset_not_all_locked(dev);
  97. return 0;
  98. fail:
  99. if (ret == -EDEADLK) {
  100. drm_modeset_backoff(ctx);
  101. goto retry;
  102. }
  103. return ret;
  104. }
  105. EXPORT_SYMBOL(__drm_modeset_lock_all);
  106. /**
  107. * drm_modeset_lock_all - take all modeset locks
  108. * @dev: drm device
  109. *
  110. * This function takes all modeset locks, suitable where a more fine-grained
  111. * scheme isn't (yet) implemented. Locks must be dropped with
  112. * drm_modeset_unlock_all.
  113. */
  114. void drm_modeset_lock_all(struct drm_device *dev)
  115. {
  116. WARN_ON(__drm_modeset_lock_all(dev, false) != 0);
  117. }
  118. EXPORT_SYMBOL(drm_modeset_lock_all);
  119. /**
  120. * drm_modeset_unlock_all - drop all modeset locks
  121. * @dev: device
  122. *
  123. * This function drop all modeset locks taken by drm_modeset_lock_all.
  124. */
  125. void drm_modeset_unlock_all(struct drm_device *dev)
  126. {
  127. struct drm_mode_config *config = &dev->mode_config;
  128. struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
  129. if (WARN_ON(!ctx))
  130. return;
  131. config->acquire_ctx = NULL;
  132. drm_modeset_drop_locks(ctx);
  133. drm_modeset_acquire_fini(ctx);
  134. kfree(ctx);
  135. mutex_unlock(&dev->mode_config.mutex);
  136. }
  137. EXPORT_SYMBOL(drm_modeset_unlock_all);
  138. /**
  139. * drm_modeset_lock_crtc - lock crtc with hidden acquire ctx
  140. * @crtc: drm crtc
  141. *
  142. * This function locks the given crtc using a hidden acquire context. This is
  143. * necessary so that drivers internally using the atomic interfaces can grab
  144. * further locks with the lock acquire context.
  145. */
  146. void drm_modeset_lock_crtc(struct drm_crtc *crtc)
  147. {
  148. struct drm_modeset_acquire_ctx *ctx;
  149. int ret;
  150. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  151. if (WARN_ON(!ctx))
  152. return;
  153. drm_modeset_acquire_init(ctx, 0);
  154. retry:
  155. ret = drm_modeset_lock(&crtc->mutex, ctx);
  156. if (ret)
  157. goto fail;
  158. WARN_ON(crtc->acquire_ctx);
  159. /* now we hold the locks, so now that it is safe, stash the
  160. * ctx for drm_modeset_unlock_crtc():
  161. */
  162. crtc->acquire_ctx = ctx;
  163. return;
  164. fail:
  165. if (ret == -EDEADLK) {
  166. drm_modeset_backoff(ctx);
  167. goto retry;
  168. }
  169. }
  170. EXPORT_SYMBOL(drm_modeset_lock_crtc);
  171. /**
  172. * drm_modeset_legacy_acquire_ctx - find acquire ctx for legacy ioctls
  173. * @crtc: drm crtc
  174. *
  175. * Legacy ioctl operations like cursor updates or page flips only have per-crtc
  176. * locking, and store the acquire ctx in the corresponding crtc. All other
  177. * legacy operations take all locks and use a global acquire context. This
  178. * function grabs the right one.
  179. */
  180. struct drm_modeset_acquire_ctx *
  181. drm_modeset_legacy_acquire_ctx(struct drm_crtc *crtc)
  182. {
  183. if (crtc->acquire_ctx)
  184. return crtc->acquire_ctx;
  185. WARN_ON(!crtc->dev->mode_config.acquire_ctx);
  186. return crtc->dev->mode_config.acquire_ctx;
  187. }
  188. EXPORT_SYMBOL(drm_modeset_legacy_acquire_ctx);
  189. /**
  190. * drm_modeset_unlock_crtc - drop crtc lock
  191. * @crtc: drm crtc
  192. *
  193. * This drops the crtc lock acquire with drm_modeset_lock_crtc() and all other
  194. * locks acquired through the hidden context.
  195. */
  196. void drm_modeset_unlock_crtc(struct drm_crtc *crtc)
  197. {
  198. struct drm_modeset_acquire_ctx *ctx = crtc->acquire_ctx;
  199. if (WARN_ON(!ctx))
  200. return;
  201. crtc->acquire_ctx = NULL;
  202. drm_modeset_drop_locks(ctx);
  203. drm_modeset_acquire_fini(ctx);
  204. kfree(ctx);
  205. }
  206. EXPORT_SYMBOL(drm_modeset_unlock_crtc);
  207. /**
  208. * drm_warn_on_modeset_not_all_locked - check that all modeset locks are locked
  209. * @dev: device
  210. *
  211. * Useful as a debug assert.
  212. */
  213. void drm_warn_on_modeset_not_all_locked(struct drm_device *dev)
  214. {
  215. struct drm_crtc *crtc;
  216. /* Locking is currently fubar in the panic handler. */
  217. if (oops_in_progress)
  218. return;
  219. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
  220. WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
  221. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  222. WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
  223. }
  224. EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked);
  225. /**
  226. * drm_modeset_acquire_init - initialize acquire context
  227. * @ctx: the acquire context
  228. * @flags: for future
  229. */
  230. void drm_modeset_acquire_init(struct drm_modeset_acquire_ctx *ctx,
  231. uint32_t flags)
  232. {
  233. memset(ctx, 0, sizeof(*ctx));
  234. ww_acquire_init(&ctx->ww_ctx, &crtc_ww_class);
  235. INIT_LIST_HEAD(&ctx->locked);
  236. }
  237. EXPORT_SYMBOL(drm_modeset_acquire_init);
  238. /**
  239. * drm_modeset_acquire_fini - cleanup acquire context
  240. * @ctx: the acquire context
  241. */
  242. void drm_modeset_acquire_fini(struct drm_modeset_acquire_ctx *ctx)
  243. {
  244. ww_acquire_fini(&ctx->ww_ctx);
  245. }
  246. EXPORT_SYMBOL(drm_modeset_acquire_fini);
  247. /**
  248. * drm_modeset_drop_locks - drop all locks
  249. * @ctx: the acquire context
  250. *
  251. * Drop all locks currently held against this acquire context.
  252. */
  253. void drm_modeset_drop_locks(struct drm_modeset_acquire_ctx *ctx)
  254. {
  255. WARN_ON(ctx->contended);
  256. while (!list_empty(&ctx->locked)) {
  257. struct drm_modeset_lock *lock;
  258. lock = list_first_entry(&ctx->locked,
  259. struct drm_modeset_lock, head);
  260. drm_modeset_unlock(lock);
  261. }
  262. }
  263. EXPORT_SYMBOL(drm_modeset_drop_locks);
  264. static inline int modeset_lock(struct drm_modeset_lock *lock,
  265. struct drm_modeset_acquire_ctx *ctx,
  266. bool interruptible, bool slow)
  267. {
  268. int ret;
  269. WARN_ON(ctx->contended);
  270. if (ctx->trylock_only) {
  271. if (!ww_mutex_trylock(&lock->mutex))
  272. return -EBUSY;
  273. else
  274. return 0;
  275. } else if (interruptible && slow) {
  276. ret = ww_mutex_lock_slow_interruptible(&lock->mutex, &ctx->ww_ctx);
  277. } else if (interruptible) {
  278. ret = ww_mutex_lock_interruptible(&lock->mutex, &ctx->ww_ctx);
  279. } else if (slow) {
  280. ww_mutex_lock_slow(&lock->mutex, &ctx->ww_ctx);
  281. ret = 0;
  282. } else {
  283. ret = ww_mutex_lock(&lock->mutex, &ctx->ww_ctx);
  284. }
  285. if (!ret) {
  286. WARN_ON(!list_empty(&lock->head));
  287. list_add(&lock->head, &ctx->locked);
  288. } else if (ret == -EALREADY) {
  289. /* we already hold the lock.. this is fine. For atomic
  290. * we will need to be able to drm_modeset_lock() things
  291. * without having to keep track of what is already locked
  292. * or not.
  293. */
  294. ret = 0;
  295. } else if (ret == -EDEADLK) {
  296. ctx->contended = lock;
  297. }
  298. return ret;
  299. }
  300. static int modeset_backoff(struct drm_modeset_acquire_ctx *ctx,
  301. bool interruptible)
  302. {
  303. struct drm_modeset_lock *contended = ctx->contended;
  304. ctx->contended = NULL;
  305. if (WARN_ON(!contended))
  306. return 0;
  307. drm_modeset_drop_locks(ctx);
  308. return modeset_lock(contended, ctx, interruptible, true);
  309. }
  310. /**
  311. * drm_modeset_backoff - deadlock avoidance backoff
  312. * @ctx: the acquire context
  313. *
  314. * If deadlock is detected (ie. drm_modeset_lock() returns -EDEADLK),
  315. * you must call this function to drop all currently held locks and
  316. * block until the contended lock becomes available.
  317. */
  318. void drm_modeset_backoff(struct drm_modeset_acquire_ctx *ctx)
  319. {
  320. modeset_backoff(ctx, false);
  321. }
  322. EXPORT_SYMBOL(drm_modeset_backoff);
  323. /**
  324. * drm_modeset_backoff_interruptible - deadlock avoidance backoff
  325. * @ctx: the acquire context
  326. *
  327. * Interruptible version of drm_modeset_backoff()
  328. */
  329. int drm_modeset_backoff_interruptible(struct drm_modeset_acquire_ctx *ctx)
  330. {
  331. return modeset_backoff(ctx, true);
  332. }
  333. EXPORT_SYMBOL(drm_modeset_backoff_interruptible);
  334. /**
  335. * drm_modeset_lock - take modeset lock
  336. * @lock: lock to take
  337. * @ctx: acquire ctx
  338. *
  339. * If ctx is not NULL, then its ww acquire context is used and the
  340. * lock will be tracked by the context and can be released by calling
  341. * drm_modeset_drop_locks(). If -EDEADLK is returned, this means a
  342. * deadlock scenario has been detected and it is an error to attempt
  343. * to take any more locks without first calling drm_modeset_backoff().
  344. */
  345. int drm_modeset_lock(struct drm_modeset_lock *lock,
  346. struct drm_modeset_acquire_ctx *ctx)
  347. {
  348. if (ctx)
  349. return modeset_lock(lock, ctx, false, false);
  350. ww_mutex_lock(&lock->mutex, NULL);
  351. return 0;
  352. }
  353. EXPORT_SYMBOL(drm_modeset_lock);
  354. /**
  355. * drm_modeset_lock_interruptible - take modeset lock
  356. * @lock: lock to take
  357. * @ctx: acquire ctx
  358. *
  359. * Interruptible version of drm_modeset_lock()
  360. */
  361. int drm_modeset_lock_interruptible(struct drm_modeset_lock *lock,
  362. struct drm_modeset_acquire_ctx *ctx)
  363. {
  364. if (ctx)
  365. return modeset_lock(lock, ctx, true, false);
  366. return ww_mutex_lock_interruptible(&lock->mutex, NULL);
  367. }
  368. EXPORT_SYMBOL(drm_modeset_lock_interruptible);
  369. /**
  370. * drm_modeset_unlock - drop modeset lock
  371. * @lock: lock to release
  372. */
  373. void drm_modeset_unlock(struct drm_modeset_lock *lock)
  374. {
  375. list_del_init(&lock->head);
  376. ww_mutex_unlock(&lock->mutex);
  377. }
  378. EXPORT_SYMBOL(drm_modeset_unlock);
  379. /* Temporary.. until we have sufficiently fine grained locking, there
  380. * are a couple scenarios where it is convenient to grab all crtc locks.
  381. * It is planned to remove this:
  382. */
  383. int drm_modeset_lock_all_crtcs(struct drm_device *dev,
  384. struct drm_modeset_acquire_ctx *ctx)
  385. {
  386. struct drm_mode_config *config = &dev->mode_config;
  387. struct drm_crtc *crtc;
  388. int ret = 0;
  389. list_for_each_entry(crtc, &config->crtc_list, head) {
  390. ret = drm_modeset_lock(&crtc->mutex, ctx);
  391. if (ret)
  392. return ret;
  393. }
  394. return 0;
  395. }
  396. EXPORT_SYMBOL(drm_modeset_lock_all_crtcs);