srcu.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Sleepable Read-Copy Update mechanism for mutual exclusion
  4. *
  5. * Copyright (C) IBM Corporation, 2006
  6. * Copyright (C) Fujitsu, 2012
  7. *
  8. * Author: Paul McKenney <[email protected]>
  9. * Lai Jiangshan <[email protected]>
  10. *
  11. * For detailed explanation of Read-Copy Update mechanism see -
  12. * Documentation/RCU/ *.txt
  13. *
  14. */
  15. #ifndef _LINUX_SRCU_H
  16. #define _LINUX_SRCU_H
  17. #include <linux/mutex.h>
  18. #include <linux/rcupdate.h>
  19. #include <linux/workqueue.h>
  20. #include <linux/rcu_segcblist.h>
  21. struct srcu_struct;
  22. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  23. int __init_srcu_struct(struct srcu_struct *ssp, const char *name,
  24. struct lock_class_key *key);
  25. #define init_srcu_struct(ssp) \
  26. ({ \
  27. static struct lock_class_key __srcu_key; \
  28. \
  29. __init_srcu_struct((ssp), #ssp, &__srcu_key); \
  30. })
  31. #define __SRCU_DEP_MAP_INIT(srcu_name) .dep_map = { .name = #srcu_name },
  32. #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  33. int init_srcu_struct(struct srcu_struct *ssp);
  34. #define __SRCU_DEP_MAP_INIT(srcu_name)
  35. #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  36. #ifdef CONFIG_TINY_SRCU
  37. #include <linux/srcutiny.h>
  38. #elif defined(CONFIG_TREE_SRCU)
  39. #include <linux/srcutree.h>
  40. #elif defined(CONFIG_SRCU)
  41. #error "Unknown SRCU implementation specified to kernel configuration"
  42. #else
  43. /* Dummy definition for things like notifiers. Actual use gets link error. */
  44. struct srcu_struct { };
  45. #endif
  46. void call_srcu(struct srcu_struct *ssp, struct rcu_head *head,
  47. void (*func)(struct rcu_head *head));
  48. void cleanup_srcu_struct(struct srcu_struct *ssp);
  49. int __srcu_read_lock(struct srcu_struct *ssp) __acquires(ssp);
  50. void __srcu_read_unlock(struct srcu_struct *ssp, int idx) __releases(ssp);
  51. void synchronize_srcu(struct srcu_struct *ssp);
  52. unsigned long get_state_synchronize_srcu(struct srcu_struct *ssp);
  53. unsigned long start_poll_synchronize_srcu(struct srcu_struct *ssp);
  54. bool poll_state_synchronize_srcu(struct srcu_struct *ssp, unsigned long cookie);
  55. #ifdef CONFIG_SRCU
  56. void srcu_init(void);
  57. #else /* #ifdef CONFIG_SRCU */
  58. static inline void srcu_init(void) { }
  59. #endif /* #else #ifdef CONFIG_SRCU */
  60. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  61. /**
  62. * srcu_read_lock_held - might we be in SRCU read-side critical section?
  63. * @ssp: The srcu_struct structure to check
  64. *
  65. * If CONFIG_DEBUG_LOCK_ALLOC is selected, returns nonzero iff in an SRCU
  66. * read-side critical section. In absence of CONFIG_DEBUG_LOCK_ALLOC,
  67. * this assumes we are in an SRCU read-side critical section unless it can
  68. * prove otherwise.
  69. *
  70. * Checks debug_lockdep_rcu_enabled() to prevent false positives during boot
  71. * and while lockdep is disabled.
  72. *
  73. * Note that SRCU is based on its own statemachine and it doesn't
  74. * relies on normal RCU, it can be called from the CPU which
  75. * is in the idle loop from an RCU point of view or offline.
  76. */
  77. static inline int srcu_read_lock_held(const struct srcu_struct *ssp)
  78. {
  79. if (!debug_lockdep_rcu_enabled())
  80. return 1;
  81. return lock_is_held(&ssp->dep_map);
  82. }
  83. #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  84. static inline int srcu_read_lock_held(const struct srcu_struct *ssp)
  85. {
  86. return 1;
  87. }
  88. #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  89. /**
  90. * srcu_dereference_check - fetch SRCU-protected pointer for later dereferencing
  91. * @p: the pointer to fetch and protect for later dereferencing
  92. * @ssp: pointer to the srcu_struct, which is used to check that we
  93. * really are in an SRCU read-side critical section.
  94. * @c: condition to check for update-side use
  95. *
  96. * If PROVE_RCU is enabled, invoking this outside of an RCU read-side
  97. * critical section will result in an RCU-lockdep splat, unless @c evaluates
  98. * to 1. The @c argument will normally be a logical expression containing
  99. * lockdep_is_held() calls.
  100. */
  101. #define srcu_dereference_check(p, ssp, c) \
  102. __rcu_dereference_check((p), __UNIQUE_ID(rcu), \
  103. (c) || srcu_read_lock_held(ssp), __rcu)
  104. /**
  105. * srcu_dereference - fetch SRCU-protected pointer for later dereferencing
  106. * @p: the pointer to fetch and protect for later dereferencing
  107. * @ssp: pointer to the srcu_struct, which is used to check that we
  108. * really are in an SRCU read-side critical section.
  109. *
  110. * Makes rcu_dereference_check() do the dirty work. If PROVE_RCU
  111. * is enabled, invoking this outside of an RCU read-side critical
  112. * section will result in an RCU-lockdep splat.
  113. */
  114. #define srcu_dereference(p, ssp) srcu_dereference_check((p), (ssp), 0)
  115. /**
  116. * srcu_dereference_notrace - no tracing and no lockdep calls from here
  117. * @p: the pointer to fetch and protect for later dereferencing
  118. * @ssp: pointer to the srcu_struct, which is used to check that we
  119. * really are in an SRCU read-side critical section.
  120. */
  121. #define srcu_dereference_notrace(p, ssp) srcu_dereference_check((p), (ssp), 1)
  122. /**
  123. * srcu_read_lock - register a new reader for an SRCU-protected structure.
  124. * @ssp: srcu_struct in which to register the new reader.
  125. *
  126. * Enter an SRCU read-side critical section. Note that SRCU read-side
  127. * critical sections may be nested. However, it is illegal to
  128. * call anything that waits on an SRCU grace period for the same
  129. * srcu_struct, whether directly or indirectly. Please note that
  130. * one way to indirectly wait on an SRCU grace period is to acquire
  131. * a mutex that is held elsewhere while calling synchronize_srcu() or
  132. * synchronize_srcu_expedited().
  133. *
  134. * Note that srcu_read_lock() and the matching srcu_read_unlock() must
  135. * occur in the same context, for example, it is illegal to invoke
  136. * srcu_read_unlock() in an irq handler if the matching srcu_read_lock()
  137. * was invoked in process context.
  138. */
  139. static inline int srcu_read_lock(struct srcu_struct *ssp) __acquires(ssp)
  140. {
  141. int retval;
  142. retval = __srcu_read_lock(ssp);
  143. rcu_lock_acquire(&(ssp)->dep_map);
  144. return retval;
  145. }
  146. /* Used by tracing, cannot be traced and cannot invoke lockdep. */
  147. static inline notrace int
  148. srcu_read_lock_notrace(struct srcu_struct *ssp) __acquires(ssp)
  149. {
  150. int retval;
  151. retval = __srcu_read_lock(ssp);
  152. return retval;
  153. }
  154. /**
  155. * srcu_read_unlock - unregister a old reader from an SRCU-protected structure.
  156. * @ssp: srcu_struct in which to unregister the old reader.
  157. * @idx: return value from corresponding srcu_read_lock().
  158. *
  159. * Exit an SRCU read-side critical section.
  160. */
  161. static inline void srcu_read_unlock(struct srcu_struct *ssp, int idx)
  162. __releases(ssp)
  163. {
  164. WARN_ON_ONCE(idx & ~0x1);
  165. rcu_lock_release(&(ssp)->dep_map);
  166. __srcu_read_unlock(ssp, idx);
  167. }
  168. /* Used by tracing, cannot be traced and cannot call lockdep. */
  169. static inline notrace void
  170. srcu_read_unlock_notrace(struct srcu_struct *ssp, int idx) __releases(ssp)
  171. {
  172. __srcu_read_unlock(ssp, idx);
  173. }
  174. /**
  175. * smp_mb__after_srcu_read_unlock - ensure full ordering after srcu_read_unlock
  176. *
  177. * Converts the preceding srcu_read_unlock into a two-way memory barrier.
  178. *
  179. * Call this after srcu_read_unlock, to guarantee that all memory operations
  180. * that occur after smp_mb__after_srcu_read_unlock will appear to happen after
  181. * the preceding srcu_read_unlock.
  182. */
  183. static inline void smp_mb__after_srcu_read_unlock(void)
  184. {
  185. /* __srcu_read_unlock has smp_mb() internally so nothing to do here. */
  186. }
  187. #endif