drm_modeset_lock.c 14 KB

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