drm_modeset_lock.c 13 KB

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