ttm_lock.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /* SPDX-License-Identifier: GPL-2.0 OR MIT */
  2. /**************************************************************************
  3. *
  4. * Copyright (c) 2007-2009 VMware, Inc., Palo Alto, CA., USA
  5. * All Rights Reserved.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the
  9. * "Software"), to deal in the Software without restriction, including
  10. * without limitation the rights to use, copy, modify, merge, publish,
  11. * distribute, sub license, and/or sell copies of the Software, and to
  12. * permit persons to whom the Software is furnished to do so, subject to
  13. * the following conditions:
  14. *
  15. * The above copyright notice and this permission notice (including the
  16. * next paragraph) shall be included in all copies or substantial portions
  17. * of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  22. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  23. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  24. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  25. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. *
  27. **************************************************************************/
  28. /*
  29. * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
  30. */
  31. #include <drm/ttm/ttm_module.h>
  32. #include <linux/atomic.h>
  33. #include <linux/errno.h>
  34. #include <linux/wait.h>
  35. #include <linux/sched/signal.h>
  36. #include "ttm_lock.h"
  37. #include "ttm_object.h"
  38. #define TTM_WRITE_LOCK_PENDING (1 << 0)
  39. #define TTM_VT_LOCK_PENDING (1 << 1)
  40. #define TTM_SUSPEND_LOCK_PENDING (1 << 2)
  41. #define TTM_VT_LOCK (1 << 3)
  42. #define TTM_SUSPEND_LOCK (1 << 4)
  43. void ttm_lock_init(struct ttm_lock *lock)
  44. {
  45. spin_lock_init(&lock->lock);
  46. init_waitqueue_head(&lock->queue);
  47. lock->rw = 0;
  48. lock->flags = 0;
  49. lock->kill_takers = false;
  50. lock->signal = SIGKILL;
  51. }
  52. void ttm_read_unlock(struct ttm_lock *lock)
  53. {
  54. spin_lock(&lock->lock);
  55. if (--lock->rw == 0)
  56. wake_up_all(&lock->queue);
  57. spin_unlock(&lock->lock);
  58. }
  59. static bool __ttm_read_lock(struct ttm_lock *lock)
  60. {
  61. bool locked = false;
  62. spin_lock(&lock->lock);
  63. if (unlikely(lock->kill_takers)) {
  64. send_sig(lock->signal, current, 0);
  65. spin_unlock(&lock->lock);
  66. return false;
  67. }
  68. if (lock->rw >= 0 && lock->flags == 0) {
  69. ++lock->rw;
  70. locked = true;
  71. }
  72. spin_unlock(&lock->lock);
  73. return locked;
  74. }
  75. int ttm_read_lock(struct ttm_lock *lock, bool interruptible)
  76. {
  77. int ret = 0;
  78. if (interruptible)
  79. ret = wait_event_interruptible(lock->queue,
  80. __ttm_read_lock(lock));
  81. else
  82. wait_event(lock->queue, __ttm_read_lock(lock));
  83. return ret;
  84. }
  85. static bool __ttm_read_trylock(struct ttm_lock *lock, bool *locked)
  86. {
  87. bool block = true;
  88. *locked = false;
  89. spin_lock(&lock->lock);
  90. if (unlikely(lock->kill_takers)) {
  91. send_sig(lock->signal, current, 0);
  92. spin_unlock(&lock->lock);
  93. return false;
  94. }
  95. if (lock->rw >= 0 && lock->flags == 0) {
  96. ++lock->rw;
  97. block = false;
  98. *locked = true;
  99. } else if (lock->flags == 0) {
  100. block = false;
  101. }
  102. spin_unlock(&lock->lock);
  103. return !block;
  104. }
  105. int ttm_read_trylock(struct ttm_lock *lock, bool interruptible)
  106. {
  107. int ret = 0;
  108. bool locked;
  109. if (interruptible)
  110. ret = wait_event_interruptible
  111. (lock->queue, __ttm_read_trylock(lock, &locked));
  112. else
  113. wait_event(lock->queue, __ttm_read_trylock(lock, &locked));
  114. if (unlikely(ret != 0)) {
  115. BUG_ON(locked);
  116. return ret;
  117. }
  118. return (locked) ? 0 : -EBUSY;
  119. }
  120. void ttm_write_unlock(struct ttm_lock *lock)
  121. {
  122. spin_lock(&lock->lock);
  123. lock->rw = 0;
  124. wake_up_all(&lock->queue);
  125. spin_unlock(&lock->lock);
  126. }
  127. static bool __ttm_write_lock(struct ttm_lock *lock)
  128. {
  129. bool locked = false;
  130. spin_lock(&lock->lock);
  131. if (unlikely(lock->kill_takers)) {
  132. send_sig(lock->signal, current, 0);
  133. spin_unlock(&lock->lock);
  134. return false;
  135. }
  136. if (lock->rw == 0 && ((lock->flags & ~TTM_WRITE_LOCK_PENDING) == 0)) {
  137. lock->rw = -1;
  138. lock->flags &= ~TTM_WRITE_LOCK_PENDING;
  139. locked = true;
  140. } else {
  141. lock->flags |= TTM_WRITE_LOCK_PENDING;
  142. }
  143. spin_unlock(&lock->lock);
  144. return locked;
  145. }
  146. int ttm_write_lock(struct ttm_lock *lock, bool interruptible)
  147. {
  148. int ret = 0;
  149. if (interruptible) {
  150. ret = wait_event_interruptible(lock->queue,
  151. __ttm_write_lock(lock));
  152. if (unlikely(ret != 0)) {
  153. spin_lock(&lock->lock);
  154. lock->flags &= ~TTM_WRITE_LOCK_PENDING;
  155. wake_up_all(&lock->queue);
  156. spin_unlock(&lock->lock);
  157. }
  158. } else
  159. wait_event(lock->queue, __ttm_write_lock(lock));
  160. return ret;
  161. }
  162. static int __ttm_vt_unlock(struct ttm_lock *lock)
  163. {
  164. int ret = 0;
  165. spin_lock(&lock->lock);
  166. if (unlikely(!(lock->flags & TTM_VT_LOCK)))
  167. ret = -EINVAL;
  168. lock->flags &= ~TTM_VT_LOCK;
  169. wake_up_all(&lock->queue);
  170. spin_unlock(&lock->lock);
  171. return ret;
  172. }
  173. static void ttm_vt_lock_remove(struct ttm_base_object **p_base)
  174. {
  175. struct ttm_base_object *base = *p_base;
  176. struct ttm_lock *lock = container_of(base, struct ttm_lock, base);
  177. int ret;
  178. *p_base = NULL;
  179. ret = __ttm_vt_unlock(lock);
  180. BUG_ON(ret != 0);
  181. }
  182. static bool __ttm_vt_lock(struct ttm_lock *lock)
  183. {
  184. bool locked = false;
  185. spin_lock(&lock->lock);
  186. if (lock->rw == 0) {
  187. lock->flags &= ~TTM_VT_LOCK_PENDING;
  188. lock->flags |= TTM_VT_LOCK;
  189. locked = true;
  190. } else {
  191. lock->flags |= TTM_VT_LOCK_PENDING;
  192. }
  193. spin_unlock(&lock->lock);
  194. return locked;
  195. }
  196. int ttm_vt_lock(struct ttm_lock *lock,
  197. bool interruptible,
  198. struct ttm_object_file *tfile)
  199. {
  200. int ret = 0;
  201. if (interruptible) {
  202. ret = wait_event_interruptible(lock->queue,
  203. __ttm_vt_lock(lock));
  204. if (unlikely(ret != 0)) {
  205. spin_lock(&lock->lock);
  206. lock->flags &= ~TTM_VT_LOCK_PENDING;
  207. wake_up_all(&lock->queue);
  208. spin_unlock(&lock->lock);
  209. return ret;
  210. }
  211. } else
  212. wait_event(lock->queue, __ttm_vt_lock(lock));
  213. /*
  214. * Add a base-object, the destructor of which will
  215. * make sure the lock is released if the client dies
  216. * while holding it.
  217. */
  218. ret = ttm_base_object_init(tfile, &lock->base, false,
  219. ttm_lock_type, &ttm_vt_lock_remove, NULL);
  220. if (ret)
  221. (void)__ttm_vt_unlock(lock);
  222. else
  223. lock->vt_holder = tfile;
  224. return ret;
  225. }
  226. int ttm_vt_unlock(struct ttm_lock *lock)
  227. {
  228. return ttm_ref_object_base_unref(lock->vt_holder,
  229. lock->base.hash.key, TTM_REF_USAGE);
  230. }
  231. void ttm_suspend_unlock(struct ttm_lock *lock)
  232. {
  233. spin_lock(&lock->lock);
  234. lock->flags &= ~TTM_SUSPEND_LOCK;
  235. wake_up_all(&lock->queue);
  236. spin_unlock(&lock->lock);
  237. }
  238. static bool __ttm_suspend_lock(struct ttm_lock *lock)
  239. {
  240. bool locked = false;
  241. spin_lock(&lock->lock);
  242. if (lock->rw == 0) {
  243. lock->flags &= ~TTM_SUSPEND_LOCK_PENDING;
  244. lock->flags |= TTM_SUSPEND_LOCK;
  245. locked = true;
  246. } else {
  247. lock->flags |= TTM_SUSPEND_LOCK_PENDING;
  248. }
  249. spin_unlock(&lock->lock);
  250. return locked;
  251. }
  252. void ttm_suspend_lock(struct ttm_lock *lock)
  253. {
  254. wait_event(lock->queue, __ttm_suspend_lock(lock));
  255. }