drm_modeset_lock.c 12 KB

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