drm_modeset_lock.c 14 KB

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