rwsem.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /* rwsem.h: R/W semaphores implemented using XADD/CMPXCHG for x86_64+
  2. *
  3. * Written by David Howells (dhowells@redhat.com).
  4. * Ported by Andi Kleen <ak@suse.de> to x86-64.
  5. *
  6. * Derived from asm-i386/semaphore.h and asm-i386/rwsem.h
  7. *
  8. *
  9. * The MSW of the count is the negated number of active writers and waiting
  10. * lockers, and the LSW is the total number of active locks
  11. *
  12. * The lock count is initialized to 0 (no active and no waiting lockers).
  13. *
  14. * When a writer subtracts WRITE_BIAS, it'll get 0xffff0001 for the case of an
  15. * uncontended lock. This can be determined because XADD returns the old value.
  16. * Readers increment by 1 and see a positive value when uncontended, negative
  17. * if there are writers (and maybe) readers waiting (in which case it goes to
  18. * sleep).
  19. *
  20. * The value of WAITING_BIAS supports up to 32766 waiting processes. This can
  21. * be extended to 65534 by manually checking the whole MSW rather than relying
  22. * on the S flag.
  23. *
  24. * The value of ACTIVE_BIAS supports up to 65535 active processes.
  25. *
  26. * This should be totally fair - if anything is waiting, a process that wants a
  27. * lock will go to the back of the queue. When the currently active lock is
  28. * released, if there's a writer at the front of the queue, then that and only
  29. * that will be woken up; if there's a bunch of consecutive readers at the
  30. * front, then they'll all be woken up, but no other readers will be.
  31. */
  32. #ifndef _X8664_RWSEM_H
  33. #define _X8664_RWSEM_H
  34. #ifndef _LINUX_RWSEM_H
  35. #error "please don't include asm/rwsem.h directly, use linux/rwsem.h instead"
  36. #endif
  37. #ifdef __KERNEL__
  38. #include <linux/list.h>
  39. #include <linux/spinlock.h>
  40. struct rwsem_waiter;
  41. extern struct rw_semaphore *rwsem_down_read_failed(struct rw_semaphore *sem);
  42. extern struct rw_semaphore *rwsem_down_write_failed(struct rw_semaphore *sem);
  43. extern struct rw_semaphore *rwsem_wake(struct rw_semaphore *);
  44. extern struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem);
  45. /*
  46. * the semaphore definition
  47. */
  48. struct rw_semaphore {
  49. signed int count;
  50. #define RWSEM_UNLOCKED_VALUE 0x00000000
  51. #define RWSEM_ACTIVE_BIAS 0x00000001
  52. #define RWSEM_ACTIVE_MASK 0x0000ffff
  53. #define RWSEM_WAITING_BIAS (-0x00010000)
  54. #define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
  55. #define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
  56. spinlock_t wait_lock;
  57. struct list_head wait_list;
  58. #if RWSEM_DEBUG
  59. int debug;
  60. #endif
  61. };
  62. /*
  63. * initialisation
  64. */
  65. #if RWSEM_DEBUG
  66. #define __RWSEM_DEBUG_INIT , 0
  67. #else
  68. #define __RWSEM_DEBUG_INIT /* */
  69. #endif
  70. #define __RWSEM_INITIALIZER(name) \
  71. { RWSEM_UNLOCKED_VALUE, SPIN_LOCK_UNLOCKED, LIST_HEAD_INIT((name).wait_list) \
  72. __RWSEM_DEBUG_INIT }
  73. #define DECLARE_RWSEM(name) \
  74. struct rw_semaphore name = __RWSEM_INITIALIZER(name)
  75. static inline void init_rwsem(struct rw_semaphore *sem)
  76. {
  77. sem->count = RWSEM_UNLOCKED_VALUE;
  78. spin_lock_init(&sem->wait_lock);
  79. INIT_LIST_HEAD(&sem->wait_list);
  80. #if RWSEM_DEBUG
  81. sem->debug = 0;
  82. #endif
  83. }
  84. /*
  85. * lock for reading
  86. */
  87. static inline void __down_read(struct rw_semaphore *sem)
  88. {
  89. __asm__ __volatile__(
  90. "# beginning down_read\n\t"
  91. LOCK_PREFIX " incl (%%rdi)\n\t" /* adds 0x00000001, returns the old value */
  92. " js 2f\n\t" /* jump if we weren't granted the lock */
  93. "1:\n\t"
  94. LOCK_SECTION_START("") \
  95. "2:\n\t"
  96. " call rwsem_down_read_failed_thunk\n\t"
  97. " jmp 1b\n"
  98. LOCK_SECTION_END \
  99. "# ending down_read\n\t"
  100. : "+m"(sem->count)
  101. : "D"(sem)
  102. : "memory", "cc");
  103. }
  104. /*
  105. * trylock for reading -- returns 1 if successful, 0 if contention
  106. */
  107. static inline int __down_read_trylock(struct rw_semaphore *sem)
  108. {
  109. __s32 result, tmp;
  110. __asm__ __volatile__(
  111. "# beginning __down_read_trylock\n\t"
  112. " movl %0,%1\n\t"
  113. "1:\n\t"
  114. " movl %1,%2\n\t"
  115. " addl %3,%2\n\t"
  116. " jle 2f\n\t"
  117. LOCK_PREFIX " cmpxchgl %2,%0\n\t"
  118. " jnz 1b\n\t"
  119. "2:\n\t"
  120. "# ending __down_read_trylock\n\t"
  121. : "+m"(sem->count), "=&a"(result), "=&r"(tmp)
  122. : "i"(RWSEM_ACTIVE_READ_BIAS)
  123. : "memory", "cc");
  124. return result>=0 ? 1 : 0;
  125. }
  126. /*
  127. * lock for writing
  128. */
  129. static inline void __down_write(struct rw_semaphore *sem)
  130. {
  131. int tmp;
  132. tmp = RWSEM_ACTIVE_WRITE_BIAS;
  133. __asm__ __volatile__(
  134. "# beginning down_write\n\t"
  135. LOCK_PREFIX " xaddl %0,(%%rdi)\n\t" /* subtract 0x0000ffff, returns the old value */
  136. " testl %0,%0\n\t" /* was the count 0 before? */
  137. " jnz 2f\n\t" /* jump if we weren't granted the lock */
  138. "1:\n\t"
  139. LOCK_SECTION_START("")
  140. "2:\n\t"
  141. " call rwsem_down_write_failed_thunk\n\t"
  142. " jmp 1b\n"
  143. LOCK_SECTION_END
  144. "# ending down_write"
  145. : "=&r" (tmp)
  146. : "0"(tmp), "D"(sem)
  147. : "memory", "cc");
  148. }
  149. /*
  150. * trylock for writing -- returns 1 if successful, 0 if contention
  151. */
  152. static inline int __down_write_trylock(struct rw_semaphore *sem)
  153. {
  154. signed long ret = cmpxchg(&sem->count,
  155. RWSEM_UNLOCKED_VALUE,
  156. RWSEM_ACTIVE_WRITE_BIAS);
  157. if (ret == RWSEM_UNLOCKED_VALUE)
  158. return 1;
  159. return 0;
  160. }
  161. /*
  162. * unlock after reading
  163. */
  164. static inline void __up_read(struct rw_semaphore *sem)
  165. {
  166. __s32 tmp = -RWSEM_ACTIVE_READ_BIAS;
  167. __asm__ __volatile__(
  168. "# beginning __up_read\n\t"
  169. LOCK_PREFIX " xaddl %[tmp],(%%rdi)\n\t" /* subtracts 1, returns the old value */
  170. " js 2f\n\t" /* jump if the lock is being waited upon */
  171. "1:\n\t"
  172. LOCK_SECTION_START("")
  173. "2:\n\t"
  174. " decw %w[tmp]\n\t" /* do nothing if still outstanding active readers */
  175. " jnz 1b\n\t"
  176. " call rwsem_wake_thunk\n\t"
  177. " jmp 1b\n"
  178. LOCK_SECTION_END
  179. "# ending __up_read\n"
  180. : "+m"(sem->count), [tmp] "+r" (tmp)
  181. : "D"(sem)
  182. : "memory", "cc");
  183. }
  184. /*
  185. * unlock after writing
  186. */
  187. static inline void __up_write(struct rw_semaphore *sem)
  188. {
  189. unsigned tmp;
  190. __asm__ __volatile__(
  191. "# beginning __up_write\n\t"
  192. " movl %[bias],%[tmp]\n\t"
  193. LOCK_PREFIX " xaddl %[tmp],(%%rdi)\n\t" /* tries to transition 0xffff0001 -> 0x00000000 */
  194. " jnz 2f\n\t" /* jump if the lock is being waited upon */
  195. "1:\n\t"
  196. LOCK_SECTION_START("")
  197. "2:\n\t"
  198. " decw %w[tmp]\n\t" /* did the active count reduce to 0? */
  199. " jnz 1b\n\t" /* jump back if not */
  200. " call rwsem_wake_thunk\n\t"
  201. " jmp 1b\n"
  202. LOCK_SECTION_END
  203. "# ending __up_write\n"
  204. : "+m"(sem->count), [tmp] "=r" (tmp)
  205. : "D"(sem), [bias] "i"(-RWSEM_ACTIVE_WRITE_BIAS)
  206. : "memory", "cc");
  207. }
  208. /*
  209. * downgrade write lock to read lock
  210. */
  211. static inline void __downgrade_write(struct rw_semaphore *sem)
  212. {
  213. __asm__ __volatile__(
  214. "# beginning __downgrade_write\n\t"
  215. LOCK_PREFIX " addl %[bias],(%%rdi)\n\t" /* transitions 0xZZZZ0001 -> 0xYYYY0001 */
  216. " js 2f\n\t" /* jump if the lock is being waited upon */
  217. "1:\n\t"
  218. LOCK_SECTION_START("")
  219. "2:\n\t"
  220. " call rwsem_downgrade_thunk\n"
  221. " jmp 1b\n"
  222. LOCK_SECTION_END
  223. "# ending __downgrade_write\n"
  224. : "=m"(sem->count)
  225. : "D"(sem), [bias] "i"(-RWSEM_WAITING_BIAS), "m"(sem->count)
  226. : "memory", "cc");
  227. }
  228. /*
  229. * implement atomic add functionality
  230. */
  231. static inline void rwsem_atomic_add(int delta, struct rw_semaphore *sem)
  232. {
  233. __asm__ __volatile__(
  234. LOCK_PREFIX "addl %1,%0"
  235. :"=m"(sem->count)
  236. :"ir"(delta), "m"(sem->count));
  237. }
  238. /*
  239. * implement exchange and add functionality
  240. */
  241. static inline int rwsem_atomic_update(int delta, struct rw_semaphore *sem)
  242. {
  243. int tmp = delta;
  244. __asm__ __volatile__(
  245. LOCK_PREFIX "xaddl %0,(%2)"
  246. : "=r"(tmp), "=m"(sem->count)
  247. : "r"(sem), "m"(sem->count), "0" (tmp)
  248. : "memory");
  249. return tmp+delta;
  250. }
  251. #endif /* __KERNEL__ */
  252. #endif /* _X8664_RWSEM_H */