rwsem.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /* rwsem.h: R/W semaphores implemented using XADD/CMPXCHG for i486+
  2. *
  3. * Written by David Howells (dhowells@redhat.com).
  4. *
  5. * Derived from asm-x86/semaphore.h
  6. *
  7. *
  8. * The MSW of the count is the negated number of active writers and waiting
  9. * lockers, and the LSW is the total number of active locks
  10. *
  11. * The lock count is initialized to 0 (no active and no waiting lockers).
  12. *
  13. * When a writer subtracts WRITE_BIAS, it'll get 0xffff0001 for the case of an
  14. * uncontended lock. This can be determined because XADD returns the old value.
  15. * Readers increment by 1 and see a positive value when uncontended, negative
  16. * if there are writers (and maybe) readers waiting (in which case it goes to
  17. * sleep).
  18. *
  19. * The value of WAITING_BIAS supports up to 32766 waiting processes. This can
  20. * be extended to 65534 by manually checking the whole MSW rather than relying
  21. * on the S flag.
  22. *
  23. * The value of ACTIVE_BIAS supports up to 65535 active processes.
  24. *
  25. * This should be totally fair - if anything is waiting, a process that wants a
  26. * lock will go to the back of the queue. When the currently active lock is
  27. * released, if there's a writer at the front of the queue, then that and only
  28. * that will be woken up; if there's a bunch of consecutive readers at the
  29. * front, then they'll all be woken up, but no other readers will be.
  30. */
  31. #ifndef _ASM_X86_RWSEM_H
  32. #define _ASM_X86_RWSEM_H
  33. #ifndef _LINUX_RWSEM_H
  34. #error "please don't include asm/rwsem.h directly, use linux/rwsem.h instead"
  35. #endif
  36. #ifdef __KERNEL__
  37. #include <asm/asm.h>
  38. /*
  39. * The bias values and the counter type limits the number of
  40. * potential readers/writers to 32767 for 32 bits and 2147483647
  41. * for 64 bits.
  42. */
  43. #ifdef CONFIG_X86_64
  44. # define RWSEM_ACTIVE_MASK 0xffffffffL
  45. #else
  46. # define RWSEM_ACTIVE_MASK 0x0000ffffL
  47. #endif
  48. #define RWSEM_UNLOCKED_VALUE 0x00000000L
  49. #define RWSEM_ACTIVE_BIAS 0x00000001L
  50. #define RWSEM_WAITING_BIAS (-RWSEM_ACTIVE_MASK-1)
  51. #define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
  52. #define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
  53. /*
  54. * lock for reading
  55. */
  56. static inline void __down_read(struct rw_semaphore *sem)
  57. {
  58. asm volatile("# beginning down_read\n\t"
  59. LOCK_PREFIX _ASM_INC "(%1)\n\t"
  60. /* adds 0x00000001 */
  61. " jns 1f\n"
  62. " call call_rwsem_down_read_failed\n"
  63. "1:\n\t"
  64. "# ending down_read\n\t"
  65. : "+m" (sem->count)
  66. : "a" (sem)
  67. : "memory", "cc");
  68. }
  69. /*
  70. * trylock for reading -- returns 1 if successful, 0 if contention
  71. */
  72. static inline bool __down_read_trylock(struct rw_semaphore *sem)
  73. {
  74. long result, tmp;
  75. asm volatile("# beginning __down_read_trylock\n\t"
  76. " mov %0,%1\n\t"
  77. "1:\n\t"
  78. " mov %1,%2\n\t"
  79. " add %3,%2\n\t"
  80. " jle 2f\n\t"
  81. LOCK_PREFIX " cmpxchg %2,%0\n\t"
  82. " jnz 1b\n\t"
  83. "2:\n\t"
  84. "# ending __down_read_trylock\n\t"
  85. : "+m" (sem->count), "=&a" (result), "=&r" (tmp)
  86. : "i" (RWSEM_ACTIVE_READ_BIAS)
  87. : "memory", "cc");
  88. return result >= 0;
  89. }
  90. /*
  91. * lock for writing
  92. */
  93. #define ____down_write(sem, slow_path) \
  94. ({ \
  95. long tmp; \
  96. struct rw_semaphore* ret; \
  97. register void *__sp asm(_ASM_SP); \
  98. \
  99. asm volatile("# beginning down_write\n\t" \
  100. LOCK_PREFIX " xadd %1,(%4)\n\t" \
  101. /* adds 0xffff0001, returns the old value */ \
  102. " test " __ASM_SEL(%w1,%k1) "," __ASM_SEL(%w1,%k1) "\n\t" \
  103. /* was the active mask 0 before? */\
  104. " jz 1f\n" \
  105. " call " slow_path "\n" \
  106. "1:\n" \
  107. "# ending down_write" \
  108. : "+m" (sem->count), "=d" (tmp), "=a" (ret), "+r" (__sp) \
  109. : "a" (sem), "1" (RWSEM_ACTIVE_WRITE_BIAS) \
  110. : "memory", "cc"); \
  111. ret; \
  112. })
  113. static inline void __down_write(struct rw_semaphore *sem)
  114. {
  115. ____down_write(sem, "call_rwsem_down_write_failed");
  116. }
  117. static inline int __down_write_killable(struct rw_semaphore *sem)
  118. {
  119. if (IS_ERR(____down_write(sem, "call_rwsem_down_write_failed_killable")))
  120. return -EINTR;
  121. return 0;
  122. }
  123. /*
  124. * trylock for writing -- returns 1 if successful, 0 if contention
  125. */
  126. static inline bool __down_write_trylock(struct rw_semaphore *sem)
  127. {
  128. bool result;
  129. long tmp0, tmp1;
  130. asm volatile("# beginning __down_write_trylock\n\t"
  131. " mov %0,%1\n\t"
  132. "1:\n\t"
  133. " test " __ASM_SEL(%w1,%k1) "," __ASM_SEL(%w1,%k1) "\n\t"
  134. /* was the active mask 0 before? */
  135. " jnz 2f\n\t"
  136. " mov %1,%2\n\t"
  137. " add %4,%2\n\t"
  138. LOCK_PREFIX " cmpxchg %2,%0\n\t"
  139. " jnz 1b\n\t"
  140. "2:\n\t"
  141. CC_SET(e)
  142. "# ending __down_write_trylock\n\t"
  143. : "+m" (sem->count), "=&a" (tmp0), "=&r" (tmp1),
  144. CC_OUT(e) (result)
  145. : "er" (RWSEM_ACTIVE_WRITE_BIAS)
  146. : "memory");
  147. return result;
  148. }
  149. /*
  150. * unlock after reading
  151. */
  152. static inline void __up_read(struct rw_semaphore *sem)
  153. {
  154. long tmp;
  155. asm volatile("# beginning __up_read\n\t"
  156. LOCK_PREFIX " xadd %1,(%2)\n\t"
  157. /* subtracts 1, returns the old value */
  158. " jns 1f\n\t"
  159. " call call_rwsem_wake\n" /* expects old value in %edx */
  160. "1:\n"
  161. "# ending __up_read\n"
  162. : "+m" (sem->count), "=d" (tmp)
  163. : "a" (sem), "1" (-RWSEM_ACTIVE_READ_BIAS)
  164. : "memory", "cc");
  165. }
  166. /*
  167. * unlock after writing
  168. */
  169. static inline void __up_write(struct rw_semaphore *sem)
  170. {
  171. long tmp;
  172. asm volatile("# beginning __up_write\n\t"
  173. LOCK_PREFIX " xadd %1,(%2)\n\t"
  174. /* subtracts 0xffff0001, returns the old value */
  175. " jns 1f\n\t"
  176. " call call_rwsem_wake\n" /* expects old value in %edx */
  177. "1:\n\t"
  178. "# ending __up_write\n"
  179. : "+m" (sem->count), "=d" (tmp)
  180. : "a" (sem), "1" (-RWSEM_ACTIVE_WRITE_BIAS)
  181. : "memory", "cc");
  182. }
  183. /*
  184. * downgrade write lock to read lock
  185. */
  186. static inline void __downgrade_write(struct rw_semaphore *sem)
  187. {
  188. asm volatile("# beginning __downgrade_write\n\t"
  189. LOCK_PREFIX _ASM_ADD "%2,(%1)\n\t"
  190. /*
  191. * transitions 0xZZZZ0001 -> 0xYYYY0001 (i386)
  192. * 0xZZZZZZZZ00000001 -> 0xYYYYYYYY00000001 (x86_64)
  193. */
  194. " jns 1f\n\t"
  195. " call call_rwsem_downgrade_wake\n"
  196. "1:\n\t"
  197. "# ending __downgrade_write\n"
  198. : "+m" (sem->count)
  199. : "a" (sem), "er" (-RWSEM_WAITING_BIAS)
  200. : "memory", "cc");
  201. }
  202. #endif /* __KERNEL__ */
  203. #endif /* _ASM_X86_RWSEM_H */