drm_modeset_lock.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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);
  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. if (!ww_mutex_trylock(&lock->mutex))
  264. return -EBUSY;
  265. else
  266. return 0;
  267. } else if (interruptible && slow) {
  268. ret = ww_mutex_lock_slow_interruptible(&lock->mutex, &ctx->ww_ctx);
  269. } else if (interruptible) {
  270. ret = ww_mutex_lock_interruptible(&lock->mutex, &ctx->ww_ctx);
  271. } else if (slow) {
  272. ww_mutex_lock_slow(&lock->mutex, &ctx->ww_ctx);
  273. ret = 0;
  274. } else {
  275. ret = ww_mutex_lock(&lock->mutex, &ctx->ww_ctx);
  276. }
  277. if (!ret) {
  278. WARN_ON(!list_empty(&lock->head));
  279. list_add(&lock->head, &ctx->locked);
  280. } else if (ret == -EALREADY) {
  281. /* we already hold the lock.. this is fine. For atomic
  282. * we will need to be able to drm_modeset_lock() things
  283. * without having to keep track of what is already locked
  284. * or not.
  285. */
  286. ret = 0;
  287. } else if (ret == -EDEADLK) {
  288. ctx->contended = lock;
  289. }
  290. return ret;
  291. }
  292. static int modeset_backoff(struct drm_modeset_acquire_ctx *ctx,
  293. bool interruptible)
  294. {
  295. struct drm_modeset_lock *contended = ctx->contended;
  296. ctx->contended = NULL;
  297. if (WARN_ON(!contended))
  298. return 0;
  299. drm_modeset_drop_locks(ctx);
  300. return modeset_lock(contended, ctx, interruptible, true);
  301. }
  302. /**
  303. * drm_modeset_backoff - deadlock avoidance backoff
  304. * @ctx: the acquire context
  305. *
  306. * If deadlock is detected (ie. drm_modeset_lock() returns -EDEADLK),
  307. * you must call this function to drop all currently held locks and
  308. * block until the contended lock becomes available.
  309. */
  310. void drm_modeset_backoff(struct drm_modeset_acquire_ctx *ctx)
  311. {
  312. modeset_backoff(ctx, false);
  313. }
  314. EXPORT_SYMBOL(drm_modeset_backoff);
  315. /**
  316. * drm_modeset_backoff_interruptible - deadlock avoidance backoff
  317. * @ctx: the acquire context
  318. *
  319. * Interruptible version of drm_modeset_backoff()
  320. */
  321. int drm_modeset_backoff_interruptible(struct drm_modeset_acquire_ctx *ctx)
  322. {
  323. return modeset_backoff(ctx, true);
  324. }
  325. EXPORT_SYMBOL(drm_modeset_backoff_interruptible);
  326. /**
  327. * drm_modeset_lock - take modeset lock
  328. * @lock: lock to take
  329. * @ctx: acquire ctx
  330. *
  331. * If ctx is not NULL, then its ww acquire context is used and the
  332. * lock will be tracked by the context and can be released by calling
  333. * drm_modeset_drop_locks(). If -EDEADLK is returned, this means a
  334. * deadlock scenario has been detected and it is an error to attempt
  335. * to take any more locks without first calling drm_modeset_backoff().
  336. */
  337. int drm_modeset_lock(struct drm_modeset_lock *lock,
  338. struct drm_modeset_acquire_ctx *ctx)
  339. {
  340. if (ctx)
  341. return modeset_lock(lock, ctx, false, false);
  342. ww_mutex_lock(&lock->mutex, NULL);
  343. return 0;
  344. }
  345. EXPORT_SYMBOL(drm_modeset_lock);
  346. /**
  347. * drm_modeset_lock_interruptible - take modeset lock
  348. * @lock: lock to take
  349. * @ctx: acquire ctx
  350. *
  351. * Interruptible version of drm_modeset_lock()
  352. */
  353. int drm_modeset_lock_interruptible(struct drm_modeset_lock *lock,
  354. struct drm_modeset_acquire_ctx *ctx)
  355. {
  356. if (ctx)
  357. return modeset_lock(lock, ctx, true, false);
  358. return ww_mutex_lock_interruptible(&lock->mutex, NULL);
  359. }
  360. EXPORT_SYMBOL(drm_modeset_lock_interruptible);
  361. /**
  362. * drm_modeset_unlock - drop modeset lock
  363. * @lock: lock to release
  364. */
  365. void drm_modeset_unlock(struct drm_modeset_lock *lock)
  366. {
  367. list_del_init(&lock->head);
  368. ww_mutex_unlock(&lock->mutex);
  369. }
  370. EXPORT_SYMBOL(drm_modeset_unlock);
  371. /* In some legacy codepaths it's convenient to just grab all the crtc and plane
  372. * related locks. */
  373. int drm_modeset_lock_all_crtcs(struct drm_device *dev,
  374. struct drm_modeset_acquire_ctx *ctx)
  375. {
  376. struct drm_crtc *crtc;
  377. struct drm_plane *plane;
  378. int ret = 0;
  379. drm_for_each_crtc(crtc, dev) {
  380. ret = drm_modeset_lock(&crtc->mutex, ctx);
  381. if (ret)
  382. return ret;
  383. }
  384. drm_for_each_plane(plane, dev) {
  385. ret = drm_modeset_lock(&plane->mutex, ctx);
  386. if (ret)
  387. return ret;
  388. }
  389. return 0;
  390. }
  391. EXPORT_SYMBOL(drm_modeset_lock_all_crtcs);