drm_modeset_lock.c 12 KB

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