drm_modeset_lock.c 13 KB

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