rwsem.h 7.5 KB

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