rwsem.h 6.6 KB

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