rwsem.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /* rwsem.h: R/W semaphores, public interface
  3. *
  4. * Written by David Howells (dhowells@redhat.com).
  5. * Derived from asm-i386/semaphore.h
  6. */
  7. #ifndef _LINUX_RWSEM_H
  8. #define _LINUX_RWSEM_H
  9. #include <linux/linkage.h>
  10. #include <linux/types.h>
  11. #include <linux/list.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/atomic.h>
  14. #include <linux/err.h>
  15. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  16. # define __RWSEM_DEP_MAP_INIT(lockname) \
  17. .dep_map = { \
  18. .name = #lockname, \
  19. .wait_type_inner = LD_WAIT_SLEEP, \
  20. },
  21. #else
  22. # define __RWSEM_DEP_MAP_INIT(lockname)
  23. #endif
  24. #ifndef CONFIG_PREEMPT_RT
  25. #ifdef CONFIG_RWSEM_SPIN_ON_OWNER
  26. #include <linux/osq_lock.h>
  27. #endif
  28. #include <linux/android_vendor.h>
  29. /*
  30. * For an uncontended rwsem, count and owner are the only fields a task
  31. * needs to touch when acquiring the rwsem. So they are put next to each
  32. * other to increase the chance that they will share the same cacheline.
  33. *
  34. * In a contended rwsem, the owner is likely the most frequently accessed
  35. * field in the structure as the optimistic waiter that holds the osq lock
  36. * will spin on owner. For an embedded rwsem, other hot fields in the
  37. * containing structure should be moved further away from the rwsem to
  38. * reduce the chance that they will share the same cacheline causing
  39. * cacheline bouncing problem.
  40. */
  41. struct rw_semaphore {
  42. atomic_long_t count;
  43. /*
  44. * Write owner or one of the read owners as well flags regarding
  45. * the current state of the rwsem. Can be used as a speculative
  46. * check to see if the write owner is running on the cpu.
  47. */
  48. atomic_long_t owner;
  49. #ifdef CONFIG_RWSEM_SPIN_ON_OWNER
  50. struct optimistic_spin_queue osq; /* spinner MCS lock */
  51. #endif
  52. raw_spinlock_t wait_lock;
  53. struct list_head wait_list;
  54. #ifdef CONFIG_DEBUG_RWSEMS
  55. void *magic;
  56. #endif
  57. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  58. struct lockdep_map dep_map;
  59. #endif
  60. ANDROID_VENDOR_DATA(1);
  61. ANDROID_OEM_DATA_ARRAY(1, 2);
  62. };
  63. /* In all implementations count != 0 means locked */
  64. static inline int rwsem_is_locked(struct rw_semaphore *sem)
  65. {
  66. return atomic_long_read(&sem->count) != 0;
  67. }
  68. #define RWSEM_UNLOCKED_VALUE 0L
  69. #define __RWSEM_COUNT_INIT(name) .count = ATOMIC_LONG_INIT(RWSEM_UNLOCKED_VALUE)
  70. /* Common initializer macros and functions */
  71. #ifdef CONFIG_DEBUG_RWSEMS
  72. # define __RWSEM_DEBUG_INIT(lockname) .magic = &lockname,
  73. #else
  74. # define __RWSEM_DEBUG_INIT(lockname)
  75. #endif
  76. #ifdef CONFIG_RWSEM_SPIN_ON_OWNER
  77. #define __RWSEM_OPT_INIT(lockname) .osq = OSQ_LOCK_UNLOCKED,
  78. #else
  79. #define __RWSEM_OPT_INIT(lockname)
  80. #endif
  81. #define __RWSEM_INITIALIZER(name) \
  82. { __RWSEM_COUNT_INIT(name), \
  83. .owner = ATOMIC_LONG_INIT(0), \
  84. __RWSEM_OPT_INIT(name) \
  85. .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock),\
  86. .wait_list = LIST_HEAD_INIT((name).wait_list), \
  87. __RWSEM_DEBUG_INIT(name) \
  88. __RWSEM_DEP_MAP_INIT(name) }
  89. #define DECLARE_RWSEM(name) \
  90. struct rw_semaphore name = __RWSEM_INITIALIZER(name)
  91. extern void __init_rwsem(struct rw_semaphore *sem, const char *name,
  92. struct lock_class_key *key);
  93. #define init_rwsem(sem) \
  94. do { \
  95. static struct lock_class_key __key; \
  96. \
  97. __init_rwsem((sem), #sem, &__key); \
  98. } while (0)
  99. /*
  100. * This is the same regardless of which rwsem implementation that is being used.
  101. * It is just a heuristic meant to be called by somebody already holding the
  102. * rwsem to see if somebody from an incompatible type is wanting access to the
  103. * lock.
  104. */
  105. static inline int rwsem_is_contended(struct rw_semaphore *sem)
  106. {
  107. return !list_empty(&sem->wait_list);
  108. }
  109. #else /* !CONFIG_PREEMPT_RT */
  110. #include <linux/rwbase_rt.h>
  111. struct rw_semaphore {
  112. struct rwbase_rt rwbase;
  113. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  114. struct lockdep_map dep_map;
  115. #endif
  116. };
  117. #define __RWSEM_INITIALIZER(name) \
  118. { \
  119. .rwbase = __RWBASE_INITIALIZER(name), \
  120. __RWSEM_DEP_MAP_INIT(name) \
  121. }
  122. #define DECLARE_RWSEM(lockname) \
  123. struct rw_semaphore lockname = __RWSEM_INITIALIZER(lockname)
  124. extern void __init_rwsem(struct rw_semaphore *rwsem, const char *name,
  125. struct lock_class_key *key);
  126. #define init_rwsem(sem) \
  127. do { \
  128. static struct lock_class_key __key; \
  129. \
  130. __init_rwsem((sem), #sem, &__key); \
  131. } while (0)
  132. static __always_inline int rwsem_is_locked(struct rw_semaphore *sem)
  133. {
  134. return rw_base_is_locked(&sem->rwbase);
  135. }
  136. static __always_inline int rwsem_is_contended(struct rw_semaphore *sem)
  137. {
  138. return rw_base_is_contended(&sem->rwbase);
  139. }
  140. #endif /* CONFIG_PREEMPT_RT */
  141. /*
  142. * The functions below are the same for all rwsem implementations including
  143. * the RT specific variant.
  144. */
  145. /*
  146. * lock for reading
  147. */
  148. extern void down_read(struct rw_semaphore *sem);
  149. extern int __must_check down_read_interruptible(struct rw_semaphore *sem);
  150. extern int __must_check down_read_killable(struct rw_semaphore *sem);
  151. /*
  152. * trylock for reading -- returns 1 if successful, 0 if contention
  153. */
  154. extern int down_read_trylock(struct rw_semaphore *sem);
  155. /*
  156. * lock for writing
  157. */
  158. extern void down_write(struct rw_semaphore *sem);
  159. extern int __must_check down_write_killable(struct rw_semaphore *sem);
  160. /*
  161. * trylock for writing -- returns 1 if successful, 0 if contention
  162. */
  163. extern int down_write_trylock(struct rw_semaphore *sem);
  164. /*
  165. * release a read lock
  166. */
  167. extern void up_read(struct rw_semaphore *sem);
  168. /*
  169. * release a write lock
  170. */
  171. extern void up_write(struct rw_semaphore *sem);
  172. /*
  173. * downgrade write lock to read lock
  174. */
  175. extern void downgrade_write(struct rw_semaphore *sem);
  176. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  177. /*
  178. * nested locking. NOTE: rwsems are not allowed to recurse
  179. * (which occurs if the same task tries to acquire the same
  180. * lock instance multiple times), but multiple locks of the
  181. * same lock class might be taken, if the order of the locks
  182. * is always the same. This ordering rule can be expressed
  183. * to lockdep via the _nested() APIs, but enumerating the
  184. * subclasses that are used. (If the nesting relationship is
  185. * static then another method for expressing nested locking is
  186. * the explicit definition of lock class keys and the use of
  187. * lockdep_set_class() at lock initialization time.
  188. * See Documentation/locking/lockdep-design.rst for more details.)
  189. */
  190. extern void down_read_nested(struct rw_semaphore *sem, int subclass);
  191. extern int __must_check down_read_killable_nested(struct rw_semaphore *sem, int subclass);
  192. extern void down_write_nested(struct rw_semaphore *sem, int subclass);
  193. extern int down_write_killable_nested(struct rw_semaphore *sem, int subclass);
  194. extern void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest_lock);
  195. # define down_write_nest_lock(sem, nest_lock) \
  196. do { \
  197. typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \
  198. _down_write_nest_lock(sem, &(nest_lock)->dep_map); \
  199. } while (0)
  200. /*
  201. * Take/release a lock when not the owner will release it.
  202. *
  203. * [ This API should be avoided as much as possible - the
  204. * proper abstraction for this case is completions. ]
  205. */
  206. extern void down_read_non_owner(struct rw_semaphore *sem);
  207. extern void up_read_non_owner(struct rw_semaphore *sem);
  208. #else
  209. # define down_read_nested(sem, subclass) down_read(sem)
  210. # define down_read_killable_nested(sem, subclass) down_read_killable(sem)
  211. # define down_write_nest_lock(sem, nest_lock) down_write(sem)
  212. # define down_write_nested(sem, subclass) down_write(sem)
  213. # define down_write_killable_nested(sem, subclass) down_write_killable(sem)
  214. # define down_read_non_owner(sem) down_read(sem)
  215. # define up_read_non_owner(sem) up_read(sem)
  216. #endif
  217. #endif /* _LINUX_RWSEM_H */