drm_modeset_lock.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /*
  2. * Copyright (C) 2014 Red Hat
  3. * Author: Rob Clark <[email protected]>
  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/drm_atomic.h>
  24. #include <drm/drm_crtc.h>
  25. #include <drm/drm_device.h>
  26. #include <drm/drm_modeset_lock.h>
  27. #include <drm/drm_print.h>
  28. /**
  29. * DOC: kms locking
  30. *
  31. * As KMS moves toward more fine grained locking, and atomic ioctl where
  32. * userspace can indirectly control locking order, it becomes necessary
  33. * to use &ww_mutex and acquire-contexts to avoid deadlocks. But because
  34. * the locking is more distributed around the driver code, we want a bit
  35. * of extra utility/tracking out of our acquire-ctx. This is provided
  36. * by &struct drm_modeset_lock and &struct drm_modeset_acquire_ctx.
  37. *
  38. * For basic principles of &ww_mutex, see: Documentation/locking/ww-mutex-design.rst
  39. *
  40. * The basic usage pattern is to::
  41. *
  42. * drm_modeset_acquire_init(ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE)
  43. * retry:
  44. * foreach (lock in random_ordered_set_of_locks) {
  45. * ret = drm_modeset_lock(lock, ctx)
  46. * if (ret == -EDEADLK) {
  47. * ret = drm_modeset_backoff(ctx);
  48. * if (!ret)
  49. * goto retry;
  50. * }
  51. * if (ret)
  52. * goto out;
  53. * }
  54. * ... do stuff ...
  55. * out:
  56. * drm_modeset_drop_locks(ctx);
  57. * drm_modeset_acquire_fini(ctx);
  58. *
  59. * For convenience this control flow is implemented in
  60. * DRM_MODESET_LOCK_ALL_BEGIN() and DRM_MODESET_LOCK_ALL_END() for the case
  61. * where all modeset locks need to be taken through drm_modeset_lock_all_ctx().
  62. *
  63. * If all that is needed is a single modeset lock, then the &struct
  64. * drm_modeset_acquire_ctx is not needed and the locking can be simplified
  65. * by passing a NULL instead of ctx in the drm_modeset_lock() call or
  66. * calling drm_modeset_lock_single_interruptible(). To unlock afterwards
  67. * call drm_modeset_unlock().
  68. *
  69. * On top of these per-object locks using &ww_mutex there's also an overall
  70. * &drm_mode_config.mutex, for protecting everything else. Mostly this means
  71. * probe state of connectors, and preventing hotplug add/removal of connectors.
  72. *
  73. * Finally there's a bunch of dedicated locks to protect drm core internal
  74. * lists and lookup data structures.
  75. */
  76. static DEFINE_WW_CLASS(crtc_ww_class);
  77. #if IS_ENABLED(CONFIG_DRM_DEBUG_MODESET_LOCK)
  78. static noinline depot_stack_handle_t __drm_stack_depot_save(void)
  79. {
  80. unsigned long entries[8];
  81. unsigned int n;
  82. n = stack_trace_save(entries, ARRAY_SIZE(entries), 1);
  83. return stack_depot_save(entries, n, GFP_NOWAIT | __GFP_NOWARN);
  84. }
  85. static void __drm_stack_depot_print(depot_stack_handle_t stack_depot)
  86. {
  87. struct drm_printer p = drm_debug_printer("drm_modeset_lock");
  88. unsigned long *entries;
  89. unsigned int nr_entries;
  90. char *buf;
  91. buf = kmalloc(PAGE_SIZE, GFP_NOWAIT | __GFP_NOWARN);
  92. if (!buf)
  93. return;
  94. nr_entries = stack_depot_fetch(stack_depot, &entries);
  95. stack_trace_snprint(buf, PAGE_SIZE, entries, nr_entries, 2);
  96. drm_printf(&p, "attempting to lock a contended lock without backoff:\n%s", buf);
  97. kfree(buf);
  98. }
  99. static void __drm_stack_depot_init(void)
  100. {
  101. stack_depot_init();
  102. }
  103. #else /* CONFIG_DRM_DEBUG_MODESET_LOCK */
  104. static depot_stack_handle_t __drm_stack_depot_save(void)
  105. {
  106. return 0;
  107. }
  108. static void __drm_stack_depot_print(depot_stack_handle_t stack_depot)
  109. {
  110. }
  111. static void __drm_stack_depot_init(void)
  112. {
  113. }
  114. #endif /* CONFIG_DRM_DEBUG_MODESET_LOCK */
  115. /**
  116. * drm_modeset_lock_all - take all modeset locks
  117. * @dev: DRM device
  118. *
  119. * This function takes all modeset locks, suitable where a more fine-grained
  120. * scheme isn't (yet) implemented. Locks must be dropped by calling the
  121. * drm_modeset_unlock_all() function.
  122. *
  123. * This function is deprecated. It allocates a lock acquisition context and
  124. * stores it in &drm_device.mode_config. This facilitate conversion of
  125. * existing code because it removes the need to manually deal with the
  126. * acquisition context, but it is also brittle because the context is global
  127. * and care must be taken not to nest calls. New code should use the
  128. * drm_modeset_lock_all_ctx() function and pass in the context explicitly.
  129. */
  130. void drm_modeset_lock_all(struct drm_device *dev)
  131. {
  132. struct drm_mode_config *config = &dev->mode_config;
  133. struct drm_modeset_acquire_ctx *ctx;
  134. int ret;
  135. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL | __GFP_NOFAIL);
  136. if (WARN_ON(!ctx))
  137. return;
  138. mutex_lock(&config->mutex);
  139. drm_modeset_acquire_init(ctx, 0);
  140. retry:
  141. ret = drm_modeset_lock_all_ctx(dev, ctx);
  142. if (ret < 0) {
  143. if (ret == -EDEADLK) {
  144. drm_modeset_backoff(ctx);
  145. goto retry;
  146. }
  147. drm_modeset_acquire_fini(ctx);
  148. kfree(ctx);
  149. return;
  150. }
  151. ww_acquire_done(&ctx->ww_ctx);
  152. WARN_ON(config->acquire_ctx);
  153. /*
  154. * We hold the locks now, so it is safe to stash the acquisition
  155. * context for drm_modeset_unlock_all().
  156. */
  157. config->acquire_ctx = ctx;
  158. drm_warn_on_modeset_not_all_locked(dev);
  159. }
  160. EXPORT_SYMBOL(drm_modeset_lock_all);
  161. /**
  162. * drm_modeset_unlock_all - drop all modeset locks
  163. * @dev: DRM device
  164. *
  165. * This function drops all modeset locks taken by a previous call to the
  166. * drm_modeset_lock_all() function.
  167. *
  168. * This function is deprecated. It uses the lock acquisition context stored
  169. * in &drm_device.mode_config. This facilitates conversion of existing
  170. * code because it removes the need to manually deal with the acquisition
  171. * context, but it is also brittle because the context is global and care must
  172. * be taken not to nest calls. New code should pass the acquisition context
  173. * directly to the drm_modeset_drop_locks() function.
  174. */
  175. void drm_modeset_unlock_all(struct drm_device *dev)
  176. {
  177. struct drm_mode_config *config = &dev->mode_config;
  178. struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
  179. if (WARN_ON(!ctx))
  180. return;
  181. config->acquire_ctx = NULL;
  182. drm_modeset_drop_locks(ctx);
  183. drm_modeset_acquire_fini(ctx);
  184. kfree(ctx);
  185. mutex_unlock(&dev->mode_config.mutex);
  186. }
  187. EXPORT_SYMBOL(drm_modeset_unlock_all);
  188. /**
  189. * drm_warn_on_modeset_not_all_locked - check that all modeset locks are locked
  190. * @dev: device
  191. *
  192. * Useful as a debug assert.
  193. */
  194. void drm_warn_on_modeset_not_all_locked(struct drm_device *dev)
  195. {
  196. struct drm_crtc *crtc;
  197. /* Locking is currently fubar in the panic handler. */
  198. if (oops_in_progress)
  199. return;
  200. drm_for_each_crtc(crtc, dev)
  201. WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
  202. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  203. WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
  204. }
  205. EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked);
  206. /**
  207. * drm_modeset_acquire_init - initialize acquire context
  208. * @ctx: the acquire context
  209. * @flags: 0 or %DRM_MODESET_ACQUIRE_INTERRUPTIBLE
  210. *
  211. * When passing %DRM_MODESET_ACQUIRE_INTERRUPTIBLE to @flags,
  212. * all calls to drm_modeset_lock() will perform an interruptible
  213. * wait.
  214. */
  215. void drm_modeset_acquire_init(struct drm_modeset_acquire_ctx *ctx,
  216. uint32_t flags)
  217. {
  218. memset(ctx, 0, sizeof(*ctx));
  219. ww_acquire_init(&ctx->ww_ctx, &crtc_ww_class);
  220. INIT_LIST_HEAD(&ctx->locked);
  221. if (flags & DRM_MODESET_ACQUIRE_INTERRUPTIBLE)
  222. ctx->interruptible = true;
  223. }
  224. EXPORT_SYMBOL(drm_modeset_acquire_init);
  225. /**
  226. * drm_modeset_acquire_fini - cleanup acquire context
  227. * @ctx: the acquire context
  228. */
  229. void drm_modeset_acquire_fini(struct drm_modeset_acquire_ctx *ctx)
  230. {
  231. ww_acquire_fini(&ctx->ww_ctx);
  232. }
  233. EXPORT_SYMBOL(drm_modeset_acquire_fini);
  234. /**
  235. * drm_modeset_drop_locks - drop all locks
  236. * @ctx: the acquire context
  237. *
  238. * Drop all locks currently held against this acquire context.
  239. */
  240. void drm_modeset_drop_locks(struct drm_modeset_acquire_ctx *ctx)
  241. {
  242. if (WARN_ON(ctx->contended))
  243. __drm_stack_depot_print(ctx->stack_depot);
  244. while (!list_empty(&ctx->locked)) {
  245. struct drm_modeset_lock *lock;
  246. lock = list_first_entry(&ctx->locked,
  247. struct drm_modeset_lock, head);
  248. drm_modeset_unlock(lock);
  249. }
  250. }
  251. EXPORT_SYMBOL(drm_modeset_drop_locks);
  252. static inline int modeset_lock(struct drm_modeset_lock *lock,
  253. struct drm_modeset_acquire_ctx *ctx,
  254. bool interruptible, bool slow)
  255. {
  256. int ret;
  257. if (WARN_ON(ctx->contended))
  258. __drm_stack_depot_print(ctx->stack_depot);
  259. if (ctx->trylock_only) {
  260. lockdep_assert_held(&ctx->ww_ctx);
  261. if (!ww_mutex_trylock(&lock->mutex, NULL))
  262. return -EBUSY;
  263. else
  264. return 0;
  265. } else if (interruptible && slow) {
  266. ret = ww_mutex_lock_slow_interruptible(&lock->mutex, &ctx->ww_ctx);
  267. } else if (interruptible) {
  268. ret = ww_mutex_lock_interruptible(&lock->mutex, &ctx->ww_ctx);
  269. } else if (slow) {
  270. ww_mutex_lock_slow(&lock->mutex, &ctx->ww_ctx);
  271. ret = 0;
  272. } else {
  273. ret = ww_mutex_lock(&lock->mutex, &ctx->ww_ctx);
  274. }
  275. if (!ret) {
  276. WARN_ON(!list_empty(&lock->head));
  277. list_add(&lock->head, &ctx->locked);
  278. } else if (ret == -EALREADY) {
  279. /* we already hold the lock.. this is fine. For atomic
  280. * we will need to be able to drm_modeset_lock() things
  281. * without having to keep track of what is already locked
  282. * or not.
  283. */
  284. ret = 0;
  285. } else if (ret == -EDEADLK) {
  286. ctx->contended = lock;
  287. ctx->stack_depot = __drm_stack_depot_save();
  288. }
  289. return ret;
  290. }
  291. /**
  292. * drm_modeset_backoff - deadlock avoidance backoff
  293. * @ctx: the acquire context
  294. *
  295. * If deadlock is detected (ie. drm_modeset_lock() returns -EDEADLK),
  296. * you must call this function to drop all currently held locks and
  297. * block until the contended lock becomes available.
  298. *
  299. * This function returns 0 on success, or -ERESTARTSYS if this context
  300. * is initialized with %DRM_MODESET_ACQUIRE_INTERRUPTIBLE and the
  301. * wait has been interrupted.
  302. */
  303. int drm_modeset_backoff(struct drm_modeset_acquire_ctx *ctx)
  304. {
  305. struct drm_modeset_lock *contended = ctx->contended;
  306. ctx->contended = NULL;
  307. ctx->stack_depot = 0;
  308. if (WARN_ON(!contended))
  309. return 0;
  310. drm_modeset_drop_locks(ctx);
  311. return modeset_lock(contended, ctx, ctx->interruptible, true);
  312. }
  313. EXPORT_SYMBOL(drm_modeset_backoff);
  314. /**
  315. * drm_modeset_lock_init - initialize lock
  316. * @lock: lock to init
  317. */
  318. void drm_modeset_lock_init(struct drm_modeset_lock *lock)
  319. {
  320. ww_mutex_init(&lock->mutex, &crtc_ww_class);
  321. INIT_LIST_HEAD(&lock->head);
  322. __drm_stack_depot_init();
  323. }
  324. EXPORT_SYMBOL(drm_modeset_lock_init);
  325. /**
  326. * drm_modeset_lock - take modeset lock
  327. * @lock: lock to take
  328. * @ctx: acquire ctx
  329. *
  330. * If @ctx is not NULL, then its ww acquire context is used and the
  331. * lock will be tracked by the context and can be released by calling
  332. * drm_modeset_drop_locks(). If -EDEADLK is returned, this means a
  333. * deadlock scenario has been detected and it is an error to attempt
  334. * to take any more locks without first calling drm_modeset_backoff().
  335. *
  336. * If the @ctx is not NULL and initialized with
  337. * %DRM_MODESET_ACQUIRE_INTERRUPTIBLE, this function will fail with
  338. * -ERESTARTSYS when interrupted.
  339. *
  340. * If @ctx is NULL then the function call behaves like a normal,
  341. * uninterruptible non-nesting mutex_lock() call.
  342. */
  343. int drm_modeset_lock(struct drm_modeset_lock *lock,
  344. struct drm_modeset_acquire_ctx *ctx)
  345. {
  346. if (ctx)
  347. return modeset_lock(lock, ctx, ctx->interruptible, false);
  348. ww_mutex_lock(&lock->mutex, NULL);
  349. return 0;
  350. }
  351. EXPORT_SYMBOL(drm_modeset_lock);
  352. /**
  353. * drm_modeset_lock_single_interruptible - take a single modeset lock
  354. * @lock: lock to take
  355. *
  356. * This function behaves as drm_modeset_lock() with a NULL context,
  357. * but performs interruptible waits.
  358. *
  359. * This function returns 0 on success, or -ERESTARTSYS when interrupted.
  360. */
  361. int drm_modeset_lock_single_interruptible(struct drm_modeset_lock *lock)
  362. {
  363. return ww_mutex_lock_interruptible(&lock->mutex, NULL);
  364. }
  365. EXPORT_SYMBOL(drm_modeset_lock_single_interruptible);
  366. /**
  367. * drm_modeset_unlock - drop modeset lock
  368. * @lock: lock to release
  369. */
  370. void drm_modeset_unlock(struct drm_modeset_lock *lock)
  371. {
  372. list_del_init(&lock->head);
  373. ww_mutex_unlock(&lock->mutex);
  374. }
  375. EXPORT_SYMBOL(drm_modeset_unlock);
  376. /**
  377. * drm_modeset_lock_all_ctx - take all modeset locks
  378. * @dev: DRM device
  379. * @ctx: lock acquisition context
  380. *
  381. * This function takes all modeset locks, suitable where a more fine-grained
  382. * scheme isn't (yet) implemented.
  383. *
  384. * Unlike drm_modeset_lock_all(), it doesn't take the &drm_mode_config.mutex
  385. * since that lock isn't required for modeset state changes. Callers which
  386. * need to grab that lock too need to do so outside of the acquire context
  387. * @ctx.
  388. *
  389. * Locks acquired with this function should be released by calling the
  390. * drm_modeset_drop_locks() function on @ctx.
  391. *
  392. * See also: DRM_MODESET_LOCK_ALL_BEGIN() and DRM_MODESET_LOCK_ALL_END()
  393. *
  394. * Returns: 0 on success or a negative error-code on failure.
  395. */
  396. int drm_modeset_lock_all_ctx(struct drm_device *dev,
  397. struct drm_modeset_acquire_ctx *ctx)
  398. {
  399. struct drm_private_obj *privobj;
  400. struct drm_crtc *crtc;
  401. struct drm_plane *plane;
  402. int ret;
  403. ret = drm_modeset_lock(&dev->mode_config.connection_mutex, ctx);
  404. if (ret)
  405. return ret;
  406. drm_for_each_crtc(crtc, dev) {
  407. ret = drm_modeset_lock(&crtc->mutex, ctx);
  408. if (ret)
  409. return ret;
  410. }
  411. drm_for_each_plane(plane, dev) {
  412. ret = drm_modeset_lock(&plane->mutex, ctx);
  413. if (ret)
  414. return ret;
  415. }
  416. drm_for_each_privobj(privobj, dev) {
  417. ret = drm_modeset_lock(&privobj->lock, ctx);
  418. if (ret)
  419. return ret;
  420. }
  421. return 0;
  422. }
  423. EXPORT_SYMBOL(drm_modeset_lock_all_ctx);