seqlock.h 7.4 KB

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