rwsem.h 7.1 KB

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