rwsem.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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, returns the old value */
  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. tmp = RWSEM_ACTIVE_WRITE_BIAS;
  140. asm volatile("# beginning down_write\n\t"
  141. LOCK_PREFIX " xadd %1,(%2)\n\t"
  142. /* subtract 0x0000ffff, returns the old value */
  143. " test %1,%1\n\t"
  144. /* was the count 0 before? */
  145. " jz 1f\n"
  146. " call call_rwsem_down_write_failed\n"
  147. "1:\n"
  148. "# ending down_write"
  149. : "+m" (sem->count), "=d" (tmp)
  150. : "a" (sem), "1" (tmp)
  151. : "memory", "cc");
  152. }
  153. static inline void __down_write(struct rw_semaphore *sem)
  154. {
  155. __down_write_nested(sem, 0);
  156. }
  157. /*
  158. * trylock for writing -- returns 1 if successful, 0 if contention
  159. */
  160. static inline int __down_write_trylock(struct rw_semaphore *sem)
  161. {
  162. rwsem_count_t ret = cmpxchg(&sem->count,
  163. RWSEM_UNLOCKED_VALUE,
  164. RWSEM_ACTIVE_WRITE_BIAS);
  165. if (ret == RWSEM_UNLOCKED_VALUE)
  166. return 1;
  167. return 0;
  168. }
  169. /*
  170. * unlock after reading
  171. */
  172. static inline void __up_read(struct rw_semaphore *sem)
  173. {
  174. rwsem_count_t tmp = -RWSEM_ACTIVE_READ_BIAS;
  175. asm volatile("# beginning __up_read\n\t"
  176. LOCK_PREFIX " xadd %1,(%2)\n\t"
  177. /* subtracts 1, returns the old value */
  178. " jns 1f\n\t"
  179. " call call_rwsem_wake\n"
  180. "1:\n"
  181. "# ending __up_read\n"
  182. : "+m" (sem->count), "=d" (tmp)
  183. : "a" (sem), "1" (tmp)
  184. : "memory", "cc");
  185. }
  186. /*
  187. * unlock after writing
  188. */
  189. static inline void __up_write(struct rw_semaphore *sem)
  190. {
  191. rwsem_count_t tmp;
  192. asm volatile("# beginning __up_write\n\t"
  193. LOCK_PREFIX " xadd %1,(%2)\n\t"
  194. /* tries to transition
  195. 0xffff0001 -> 0x00000000 */
  196. " jz 1f\n"
  197. " call call_rwsem_wake\n"
  198. "1:\n\t"
  199. "# ending __up_write\n"
  200. : "+m" (sem->count), "=d" (tmp)
  201. : "a" (sem), "1" (-RWSEM_ACTIVE_WRITE_BIAS)
  202. : "memory", "cc");
  203. }
  204. /*
  205. * downgrade write lock to read lock
  206. */
  207. static inline void __downgrade_write(struct rw_semaphore *sem)
  208. {
  209. asm volatile("# beginning __downgrade_write\n\t"
  210. LOCK_PREFIX _ASM_ADD "%2,(%1)\n\t"
  211. /*
  212. * transitions 0xZZZZ0001 -> 0xYYYY0001 (i386)
  213. * 0xZZZZZZZZ00000001 -> 0xYYYYYYYY00000001 (x86_64)
  214. */
  215. " jns 1f\n\t"
  216. " call call_rwsem_downgrade_wake\n"
  217. "1:\n\t"
  218. "# ending __downgrade_write\n"
  219. : "+m" (sem->count)
  220. : "a" (sem), "er" (-RWSEM_WAITING_BIAS)
  221. : "memory", "cc");
  222. }
  223. /*
  224. * implement atomic add functionality
  225. */
  226. static inline void rwsem_atomic_add(rwsem_count_t delta,
  227. struct rw_semaphore *sem)
  228. {
  229. asm volatile(LOCK_PREFIX _ASM_ADD "%1,%0"
  230. : "+m" (sem->count)
  231. : "er" (delta));
  232. }
  233. /*
  234. * implement exchange and add functionality
  235. */
  236. static inline rwsem_count_t rwsem_atomic_update(rwsem_count_t delta,
  237. struct rw_semaphore *sem)
  238. {
  239. rwsem_count_t tmp = delta;
  240. asm volatile(LOCK_PREFIX "xadd %0,%1"
  241. : "+r" (tmp), "+m" (sem->count)
  242. : : "memory");
  243. return tmp + delta;
  244. }
  245. static inline int rwsem_is_locked(struct rw_semaphore *sem)
  246. {
  247. return (sem->count != 0);
  248. }
  249. #endif /* __KERNEL__ */
  250. #endif /* _ASM_X86_RWSEM_H */