seqlock.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #ifndef __LINUX_SEQLOCK_H
  2. #define __LINUX_SEQLOCK_H
  3. /*
  4. * Reader/writer consistent mechanism without starving writers. This type of
  5. * lock for data where the reader wants a consistent set of information
  6. * and is willing to retry if the information changes. Readers never
  7. * block but they may have to retry if a writer is in
  8. * progress. Writers do not wait for readers.
  9. *
  10. * This is not as cache friendly as brlock. Also, this will not work
  11. * for data that contains pointers, because any writer could
  12. * invalidate a pointer that a reader was following.
  13. *
  14. * Expected reader usage:
  15. * do {
  16. * seq = read_seqbegin(&foo);
  17. * ...
  18. * } while (read_seqretry(&foo, seq));
  19. *
  20. *
  21. * On non-SMP the spin locks disappear but the writer still needs
  22. * to increment the sequence variables because an interrupt routine could
  23. * change the state of the data.
  24. *
  25. * Based on x86_64 vsyscall gettimeofday
  26. * by Keith Owens and Andrea Arcangeli
  27. */
  28. #include <linux/spinlock.h>
  29. #include <linux/preempt.h>
  30. #include <asm/processor.h>
  31. /*
  32. * Version using sequence counter only.
  33. * This can be used when code has its own mutex protecting the
  34. * updating starting before the write_seqcountbeqin() and ending
  35. * after the write_seqcount_end().
  36. */
  37. typedef struct seqcount {
  38. unsigned sequence;
  39. } seqcount_t;
  40. #define SEQCNT_ZERO { 0 }
  41. #define seqcount_init(x) do { *(x) = (seqcount_t) SEQCNT_ZERO; } while (0)
  42. /**
  43. * __read_seqcount_begin - begin a seq-read critical section (without barrier)
  44. * @s: pointer to seqcount_t
  45. * Returns: count to be passed to read_seqcount_retry
  46. *
  47. * __read_seqcount_begin is like read_seqcount_begin, but has no smp_rmb()
  48. * barrier. Callers should ensure that smp_rmb() or equivalent ordering is
  49. * provided before actually loading any of the variables that are to be
  50. * protected in this critical section.
  51. *
  52. * Use carefully, only in critical code, and comment how the barrier is
  53. * provided.
  54. */
  55. static inline unsigned __read_seqcount_begin(const seqcount_t *s)
  56. {
  57. unsigned ret;
  58. repeat:
  59. ret = ACCESS_ONCE(s->sequence);
  60. if (unlikely(ret & 1)) {
  61. cpu_relax();
  62. goto repeat;
  63. }
  64. return ret;
  65. }
  66. /**
  67. * read_seqcount_begin - begin a seq-read critical section
  68. * @s: pointer to seqcount_t
  69. * Returns: count to be passed to read_seqcount_retry
  70. *
  71. * read_seqcount_begin opens a read critical section of the given seqcount.
  72. * Validity of the critical section is tested by checking read_seqcount_retry
  73. * function.
  74. */
  75. static inline unsigned read_seqcount_begin(const seqcount_t *s)
  76. {
  77. unsigned ret = __read_seqcount_begin(s);
  78. smp_rmb();
  79. return ret;
  80. }
  81. /**
  82. * raw_seqcount_begin - begin a seq-read critical section
  83. * @s: pointer to seqcount_t
  84. * Returns: count to be passed to read_seqcount_retry
  85. *
  86. * raw_seqcount_begin opens a read critical section of the given seqcount.
  87. * Validity of the critical section is tested by checking read_seqcount_retry
  88. * function.
  89. *
  90. * Unlike read_seqcount_begin(), this function will not wait for the count
  91. * to stabilize. If a writer is active when we begin, we will fail the
  92. * read_seqcount_retry() instead of stabilizing at the beginning of the
  93. * critical section.
  94. */
  95. static inline unsigned raw_seqcount_begin(const seqcount_t *s)
  96. {
  97. unsigned ret = ACCESS_ONCE(s->sequence);
  98. smp_rmb();
  99. return ret & ~1;
  100. }
  101. /**
  102. * __read_seqcount_retry - end a seq-read critical section (without barrier)
  103. * @s: pointer to seqcount_t
  104. * @start: count, from read_seqcount_begin
  105. * Returns: 1 if retry is required, else 0
  106. *
  107. * __read_seqcount_retry is like read_seqcount_retry, but has no smp_rmb()
  108. * barrier. Callers should ensure that smp_rmb() or equivalent ordering is
  109. * provided before actually loading any of the variables that are to be
  110. * protected in this critical section.
  111. *
  112. * Use carefully, only in critical code, and comment how the barrier is
  113. * provided.
  114. */
  115. static inline int __read_seqcount_retry(const seqcount_t *s, unsigned start)
  116. {
  117. return unlikely(s->sequence != start);
  118. }
  119. /**
  120. * read_seqcount_retry - end a seq-read critical section
  121. * @s: pointer to seqcount_t
  122. * @start: count, from read_seqcount_begin
  123. * Returns: 1 if retry is required, else 0
  124. *
  125. * read_seqcount_retry closes a read critical section of the given seqcount.
  126. * If the critical section was invalid, it must be ignored (and typically
  127. * retried).
  128. */
  129. static inline int read_seqcount_retry(const seqcount_t *s, unsigned start)
  130. {
  131. smp_rmb();
  132. return __read_seqcount_retry(s, start);
  133. }
  134. /*
  135. * Sequence counter only version assumes that callers are using their
  136. * own mutexing.
  137. */
  138. static inline void write_seqcount_begin(seqcount_t *s)
  139. {
  140. s->sequence++;
  141. smp_wmb();
  142. }
  143. static inline void write_seqcount_end(seqcount_t *s)
  144. {
  145. smp_wmb();
  146. s->sequence++;
  147. }
  148. /**
  149. * write_seqcount_barrier - invalidate in-progress read-side seq operations
  150. * @s: pointer to seqcount_t
  151. *
  152. * After write_seqcount_barrier, no read-side seq operations will complete
  153. * successfully and see data older than this.
  154. */
  155. static inline void write_seqcount_barrier(seqcount_t *s)
  156. {
  157. smp_wmb();
  158. s->sequence+=2;
  159. }
  160. typedef struct {
  161. struct seqcount seqcount;
  162. spinlock_t lock;
  163. } seqlock_t;
  164. /*
  165. * These macros triggered gcc-3.x compile-time problems. We think these are
  166. * OK now. Be cautious.
  167. */
  168. #define __SEQLOCK_UNLOCKED(lockname) \
  169. { \
  170. .seqcount = SEQCNT_ZERO, \
  171. .lock = __SPIN_LOCK_UNLOCKED(lockname) \
  172. }
  173. #define seqlock_init(x) \
  174. do { \
  175. seqcount_init(&(x)->seqcount); \
  176. spin_lock_init(&(x)->lock); \
  177. } while (0)
  178. #define DEFINE_SEQLOCK(x) \
  179. seqlock_t x = __SEQLOCK_UNLOCKED(x)
  180. /*
  181. * Read side functions for starting and finalizing a read side section.
  182. */
  183. static inline unsigned read_seqbegin(const seqlock_t *sl)
  184. {
  185. return read_seqcount_begin(&sl->seqcount);
  186. }
  187. static inline unsigned read_seqretry(const seqlock_t *sl, unsigned start)
  188. {
  189. return read_seqcount_retry(&sl->seqcount, start);
  190. }
  191. /*
  192. * Lock out other writers and update the count.
  193. * Acts like a normal spin_lock/unlock.
  194. * Don't need preempt_disable() because that is in the spin_lock already.
  195. */
  196. static inline void write_seqlock(seqlock_t *sl)
  197. {
  198. spin_lock(&sl->lock);
  199. write_seqcount_begin(&sl->seqcount);
  200. }
  201. static inline void write_sequnlock(seqlock_t *sl)
  202. {
  203. write_seqcount_end(&sl->seqcount);
  204. spin_unlock(&sl->lock);
  205. }
  206. static inline void write_seqlock_bh(seqlock_t *sl)
  207. {
  208. spin_lock_bh(&sl->lock);
  209. write_seqcount_begin(&sl->seqcount);
  210. }
  211. static inline void write_sequnlock_bh(seqlock_t *sl)
  212. {
  213. write_seqcount_end(&sl->seqcount);
  214. spin_unlock_bh(&sl->lock);
  215. }
  216. static inline void write_seqlock_irq(seqlock_t *sl)
  217. {
  218. spin_lock_irq(&sl->lock);
  219. write_seqcount_begin(&sl->seqcount);
  220. }
  221. static inline void write_sequnlock_irq(seqlock_t *sl)
  222. {
  223. write_seqcount_end(&sl->seqcount);
  224. spin_unlock_irq(&sl->lock);
  225. }
  226. static inline unsigned long __write_seqlock_irqsave(seqlock_t *sl)
  227. {
  228. unsigned long flags;
  229. spin_lock_irqsave(&sl->lock, flags);
  230. write_seqcount_begin(&sl->seqcount);
  231. return flags;
  232. }
  233. #define write_seqlock_irqsave(lock, flags) \
  234. do { flags = __write_seqlock_irqsave(lock); } while (0)
  235. static inline void
  236. write_sequnlock_irqrestore(seqlock_t *sl, unsigned long flags)
  237. {
  238. write_seqcount_end(&sl->seqcount);
  239. spin_unlock_irqrestore(&sl->lock, flags);
  240. }
  241. #endif /* __LINUX_SEQLOCK_H */