rwsem.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 consequtive 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. extern asmregparm struct rw_semaphore *
  39. rwsem_down_read_failed(struct rw_semaphore *sem);
  40. extern asmregparm struct rw_semaphore *
  41. rwsem_down_write_failed(struct rw_semaphore *sem);
  42. extern asmregparm struct rw_semaphore *
  43. rwsem_wake(struct rw_semaphore *);
  44. extern asmregparm struct rw_semaphore *
  45. rwsem_downgrade_wake(struct rw_semaphore *sem);
  46. /*
  47. * the semaphore definition
  48. *
  49. * The bias values and the counter type limits the number of
  50. * potential readers/writers to 32767 for 32 bits and 2147483647
  51. * for 64 bits.
  52. */
  53. #ifdef CONFIG_X86_64
  54. # define RWSEM_ACTIVE_MASK 0xffffffffL
  55. #else
  56. # define RWSEM_ACTIVE_MASK 0x0000ffffL
  57. #endif
  58. #define RWSEM_UNLOCKED_VALUE 0x00000000L
  59. #define RWSEM_ACTIVE_BIAS 0x00000001L
  60. #define RWSEM_WAITING_BIAS (-RWSEM_ACTIVE_MASK-1)
  61. #define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
  62. #define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
  63. typedef signed long rwsem_count_t;
  64. struct rw_semaphore {
  65. rwsem_count_t count;
  66. spinlock_t wait_lock;
  67. struct list_head wait_list;
  68. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  69. struct lockdep_map dep_map;
  70. #endif
  71. };
  72. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  73. # define __RWSEM_DEP_MAP_INIT(lockname) , .dep_map = { .name = #lockname }
  74. #else
  75. # define __RWSEM_DEP_MAP_INIT(lockname)
  76. #endif
  77. #define __RWSEM_INITIALIZER(name) \
  78. { \
  79. RWSEM_UNLOCKED_VALUE, __SPIN_LOCK_UNLOCKED((name).wait_lock), \
  80. LIST_HEAD_INIT((name).wait_list) __RWSEM_DEP_MAP_INIT(name) \
  81. }
  82. #define DECLARE_RWSEM(name) \
  83. struct rw_semaphore name = __RWSEM_INITIALIZER(name)
  84. extern void __init_rwsem(struct rw_semaphore *sem, const char *name,
  85. struct lock_class_key *key);
  86. #define init_rwsem(sem) \
  87. do { \
  88. static struct lock_class_key __key; \
  89. \
  90. __init_rwsem((sem), #sem, &__key); \
  91. } while (0)
  92. /*
  93. * lock for reading
  94. */
  95. static inline void __down_read(struct rw_semaphore *sem)
  96. {
  97. asm volatile("# beginning down_read\n\t"
  98. LOCK_PREFIX _ASM_INC "(%1)\n\t"
  99. /* adds 0x00000001 */
  100. " jns 1f\n"
  101. " call call_rwsem_down_read_failed\n"
  102. "1:\n\t"
  103. "# ending down_read\n\t"
  104. : "+m" (sem->count)
  105. : "a" (sem)
  106. : "memory", "cc");
  107. }
  108. /*
  109. * trylock for reading -- returns 1 if successful, 0 if contention
  110. */
  111. static inline int __down_read_trylock(struct rw_semaphore *sem)
  112. {
  113. rwsem_count_t result, tmp;
  114. asm volatile("# beginning __down_read_trylock\n\t"
  115. " mov %0,%1\n\t"
  116. "1:\n\t"
  117. " mov %1,%2\n\t"
  118. " add %3,%2\n\t"
  119. " jle 2f\n\t"
  120. LOCK_PREFIX " cmpxchg %2,%0\n\t"
  121. " jnz 1b\n\t"
  122. "2:\n\t"
  123. "# ending __down_read_trylock\n\t"
  124. : "+m" (sem->count), "=&a" (result), "=&r" (tmp)
  125. : "i" (RWSEM_ACTIVE_READ_BIAS)
  126. : "memory", "cc");
  127. return result >= 0 ? 1 : 0;
  128. }
  129. /*
  130. * lock for writing
  131. */
  132. static inline void __down_write_nested(struct rw_semaphore *sem, int subclass)
  133. {
  134. rwsem_count_t tmp;
  135. asm volatile("# beginning down_write\n\t"
  136. LOCK_PREFIX " xadd %1,(%2)\n\t"
  137. /* adds 0xffff0001, returns the old value */
  138. " test %1,%1\n\t"
  139. /* was the count 0 before? */
  140. " jz 1f\n"
  141. " call call_rwsem_down_write_failed\n"
  142. "1:\n"
  143. "# ending down_write"
  144. : "+m" (sem->count), "=d" (tmp)
  145. : "a" (sem), "1" (RWSEM_ACTIVE_WRITE_BIAS)
  146. : "memory", "cc");
  147. }
  148. static inline void __down_write(struct rw_semaphore *sem)
  149. {
  150. __down_write_nested(sem, 0);
  151. }
  152. /*
  153. * trylock for writing -- returns 1 if successful, 0 if contention
  154. */
  155. static inline int __down_write_trylock(struct rw_semaphore *sem)
  156. {
  157. rwsem_count_t ret = cmpxchg(&sem->count,
  158. RWSEM_UNLOCKED_VALUE,
  159. RWSEM_ACTIVE_WRITE_BIAS);
  160. if (ret == RWSEM_UNLOCKED_VALUE)
  161. return 1;
  162. return 0;
  163. }
  164. /*
  165. * unlock after reading
  166. */
  167. static inline void __up_read(struct rw_semaphore *sem)
  168. {
  169. rwsem_count_t tmp;
  170. asm volatile("# beginning __up_read\n\t"
  171. LOCK_PREFIX " xadd %1,(%2)\n\t"
  172. /* subtracts 1, returns the old value */
  173. " jns 1f\n\t"
  174. " call call_rwsem_wake\n" /* expects old value in %edx */
  175. "1:\n"
  176. "# ending __up_read\n"
  177. : "+m" (sem->count), "=d" (tmp)
  178. : "a" (sem), "1" (-RWSEM_ACTIVE_READ_BIAS)
  179. : "memory", "cc");
  180. }
  181. /*
  182. * unlock after writing
  183. */
  184. static inline void __up_write(struct rw_semaphore *sem)
  185. {
  186. rwsem_count_t tmp;
  187. asm volatile("# beginning __up_write\n\t"
  188. LOCK_PREFIX " xadd %1,(%2)\n\t"
  189. /* subtracts 0xffff0001, returns the old value */
  190. " jns 1f\n\t"
  191. " call call_rwsem_wake\n" /* expects old value in %edx */
  192. "1:\n\t"
  193. "# ending __up_write\n"
  194. : "+m" (sem->count), "=d" (tmp)
  195. : "a" (sem), "1" (-RWSEM_ACTIVE_WRITE_BIAS)
  196. : "memory", "cc");
  197. }
  198. /*
  199. * downgrade write lock to read lock
  200. */
  201. static inline void __downgrade_write(struct rw_semaphore *sem)
  202. {
  203. asm volatile("# beginning __downgrade_write\n\t"
  204. LOCK_PREFIX _ASM_ADD "%2,(%1)\n\t"
  205. /*
  206. * transitions 0xZZZZ0001 -> 0xYYYY0001 (i386)
  207. * 0xZZZZZZZZ00000001 -> 0xYYYYYYYY00000001 (x86_64)
  208. */
  209. " jns 1f\n\t"
  210. " call call_rwsem_downgrade_wake\n"
  211. "1:\n\t"
  212. "# ending __downgrade_write\n"
  213. : "+m" (sem->count)
  214. : "a" (sem), "er" (-RWSEM_WAITING_BIAS)
  215. : "memory", "cc");
  216. }
  217. /*
  218. * implement atomic add functionality
  219. */
  220. static inline void rwsem_atomic_add(rwsem_count_t delta,
  221. struct rw_semaphore *sem)
  222. {
  223. asm volatile(LOCK_PREFIX _ASM_ADD "%1,%0"
  224. : "+m" (sem->count)
  225. : "er" (delta));
  226. }
  227. /*
  228. * implement exchange and add functionality
  229. */
  230. static inline rwsem_count_t rwsem_atomic_update(rwsem_count_t delta,
  231. struct rw_semaphore *sem)
  232. {
  233. rwsem_count_t tmp = delta;
  234. asm volatile(LOCK_PREFIX "xadd %0,%1"
  235. : "+r" (tmp), "+m" (sem->count)
  236. : : "memory");
  237. return tmp + delta;
  238. }
  239. static inline int rwsem_is_locked(struct rw_semaphore *sem)
  240. {
  241. return (sem->count != 0);
  242. }
  243. #endif /* __KERNEL__ */
  244. #endif /* _ASM_X86_RWSEM_H */