seqlock.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. typedef struct {
  31. unsigned sequence;
  32. spinlock_t lock;
  33. } seqlock_t;
  34. /*
  35. * These macros triggered gcc-3.x compile-time problems. We think these are
  36. * OK now. Be cautious.
  37. */
  38. #define __SEQLOCK_UNLOCKED(lockname) \
  39. { 0, __SPIN_LOCK_UNLOCKED(lockname) }
  40. #define seqlock_init(x) \
  41. do { \
  42. (x)->sequence = 0; \
  43. spin_lock_init(&(x)->lock); \
  44. } while (0)
  45. #define DEFINE_SEQLOCK(x) \
  46. seqlock_t x = __SEQLOCK_UNLOCKED(x)
  47. /* Lock out other writers and update the count.
  48. * Acts like a normal spin_lock/unlock.
  49. * Don't need preempt_disable() because that is in the spin_lock already.
  50. */
  51. static inline void write_seqlock(seqlock_t *sl)
  52. {
  53. spin_lock(&sl->lock);
  54. ++sl->sequence;
  55. smp_wmb();
  56. }
  57. static inline void write_sequnlock(seqlock_t *sl)
  58. {
  59. smp_wmb();
  60. sl->sequence++;
  61. spin_unlock(&sl->lock);
  62. }
  63. static inline int write_tryseqlock(seqlock_t *sl)
  64. {
  65. int ret = spin_trylock(&sl->lock);
  66. if (ret) {
  67. ++sl->sequence;
  68. smp_wmb();
  69. }
  70. return ret;
  71. }
  72. /* Start of read calculation -- fetch last complete writer token */
  73. static __always_inline unsigned read_seqbegin(const seqlock_t *sl)
  74. {
  75. unsigned ret;
  76. repeat:
  77. ret = ACCESS_ONCE(sl->sequence);
  78. if (unlikely(ret & 1)) {
  79. cpu_relax();
  80. goto repeat;
  81. }
  82. smp_rmb();
  83. return ret;
  84. }
  85. /*
  86. * Test if reader processed invalid data.
  87. *
  88. * If sequence value changed then writer changed data while in section.
  89. */
  90. static __always_inline int read_seqretry(const seqlock_t *sl, unsigned start)
  91. {
  92. smp_rmb();
  93. return unlikely(sl->sequence != start);
  94. }
  95. /*
  96. * Version using sequence counter only.
  97. * This can be used when code has its own mutex protecting the
  98. * updating starting before the write_seqcountbeqin() and ending
  99. * after the write_seqcount_end().
  100. */
  101. typedef struct seqcount {
  102. unsigned sequence;
  103. } seqcount_t;
  104. #define SEQCNT_ZERO { 0 }
  105. #define seqcount_init(x) do { *(x) = (seqcount_t) SEQCNT_ZERO; } while (0)
  106. /**
  107. * __read_seqcount_begin - begin a seq-read critical section (without barrier)
  108. * @s: pointer to seqcount_t
  109. * Returns: count to be passed to read_seqcount_retry
  110. *
  111. * __read_seqcount_begin is like read_seqcount_begin, but has no smp_rmb()
  112. * barrier. Callers should ensure that smp_rmb() or equivalent ordering is
  113. * provided before actually loading any of the variables that are to be
  114. * protected in this critical section.
  115. *
  116. * Use carefully, only in critical code, and comment how the barrier is
  117. * provided.
  118. */
  119. static inline unsigned __read_seqcount_begin(const seqcount_t *s)
  120. {
  121. unsigned ret;
  122. repeat:
  123. ret = s->sequence;
  124. if (unlikely(ret & 1)) {
  125. cpu_relax();
  126. goto repeat;
  127. }
  128. return ret;
  129. }
  130. /**
  131. * read_seqcount_begin - begin a seq-read critical section
  132. * @s: pointer to seqcount_t
  133. * Returns: count to be passed to read_seqcount_retry
  134. *
  135. * read_seqcount_begin opens a read critical section of the given seqcount.
  136. * Validity of the critical section is tested by checking read_seqcount_retry
  137. * function.
  138. */
  139. static inline unsigned read_seqcount_begin(const seqcount_t *s)
  140. {
  141. unsigned ret = __read_seqcount_begin(s);
  142. smp_rmb();
  143. return ret;
  144. }
  145. /**
  146. * __read_seqcount_retry - end a seq-read critical section (without barrier)
  147. * @s: pointer to seqcount_t
  148. * @start: count, from read_seqcount_begin
  149. * Returns: 1 if retry is required, else 0
  150. *
  151. * __read_seqcount_retry is like read_seqcount_retry, but has no smp_rmb()
  152. * barrier. Callers should ensure that smp_rmb() or equivalent ordering is
  153. * provided before actually loading any of the variables that are to be
  154. * protected in this critical section.
  155. *
  156. * Use carefully, only in critical code, and comment how the barrier is
  157. * provided.
  158. */
  159. static inline int __read_seqcount_retry(const seqcount_t *s, unsigned start)
  160. {
  161. return unlikely(s->sequence != start);
  162. }
  163. /**
  164. * read_seqcount_retry - end a seq-read critical section
  165. * @s: pointer to seqcount_t
  166. * @start: count, from read_seqcount_begin
  167. * Returns: 1 if retry is required, else 0
  168. *
  169. * read_seqcount_retry closes a read critical section of the given seqcount.
  170. * If the critical section was invalid, it must be ignored (and typically
  171. * retried).
  172. */
  173. static inline int read_seqcount_retry(const seqcount_t *s, unsigned start)
  174. {
  175. smp_rmb();
  176. return __read_seqcount_retry(s, start);
  177. }
  178. /*
  179. * Sequence counter only version assumes that callers are using their
  180. * own mutexing.
  181. */
  182. static inline void write_seqcount_begin(seqcount_t *s)
  183. {
  184. s->sequence++;
  185. smp_wmb();
  186. }
  187. static inline void write_seqcount_end(seqcount_t *s)
  188. {
  189. smp_wmb();
  190. s->sequence++;
  191. }
  192. /**
  193. * write_seqcount_barrier - invalidate in-progress read-side seq operations
  194. * @s: pointer to seqcount_t
  195. *
  196. * After write_seqcount_barrier, no read-side seq operations will complete
  197. * successfully and see data older than this.
  198. */
  199. static inline void write_seqcount_barrier(seqcount_t *s)
  200. {
  201. smp_wmb();
  202. s->sequence+=2;
  203. }
  204. /*
  205. * Possible sw/hw IRQ protected versions of the interfaces.
  206. */
  207. #define write_seqlock_irqsave(lock, flags) \
  208. do { local_irq_save(flags); write_seqlock(lock); } while (0)
  209. #define write_seqlock_irq(lock) \
  210. do { local_irq_disable(); write_seqlock(lock); } while (0)
  211. #define write_seqlock_bh(lock) \
  212. do { local_bh_disable(); write_seqlock(lock); } while (0)
  213. #define write_sequnlock_irqrestore(lock, flags) \
  214. do { write_sequnlock(lock); local_irq_restore(flags); } while(0)
  215. #define write_sequnlock_irq(lock) \
  216. do { write_sequnlock(lock); local_irq_enable(); } while(0)
  217. #define write_sequnlock_bh(lock) \
  218. do { write_sequnlock(lock); local_bh_enable(); } while(0)
  219. #define read_seqbegin_irqsave(lock, flags) \
  220. ({ local_irq_save(flags); read_seqbegin(lock); })
  221. #define read_seqretry_irqrestore(lock, iv, flags) \
  222. ({ \
  223. int ret = read_seqretry(lock, iv); \
  224. local_irq_restore(flags); \
  225. ret; \
  226. })
  227. #endif /* __LINUX_SEQLOCK_H */