ttm_lock.c 7.2 KB

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