drm_modeset_lock.c 12 KB

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