seqlock.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. There are two types
  7. * of readers:
  8. * 1. Sequence readers which never block a writer but they may have to retry
  9. * if a writer is in progress by detecting change in sequence number.
  10. * Writers do not wait for a sequence reader.
  11. * 2. Locking readers which will wait if a writer or another locking reader
  12. * is in progress. A locking reader in progress will also block a writer
  13. * from going forward. Unlike the regular rwlock, the read lock here is
  14. * exclusive so that only one locking reader can get it.
  15. *
  16. * This is not as cache friendly as brlock. Also, this may not work well
  17. * for data that contains pointers, because any writer could
  18. * invalidate a pointer that a reader was following.
  19. *
  20. * Expected non-blocking reader usage:
  21. * do {
  22. * seq = read_seqbegin(&foo);
  23. * ...
  24. * } while (read_seqretry(&foo, seq));
  25. *
  26. *
  27. * On non-SMP the spin locks disappear but the writer still needs
  28. * to increment the sequence variables because an interrupt routine could
  29. * change the state of the data.
  30. *
  31. * Based on x86_64 vsyscall gettimeofday
  32. * by Keith Owens and Andrea Arcangeli
  33. */
  34. #include <linux/spinlock.h>
  35. #include <linux/preempt.h>
  36. #include <linux/lockdep.h>
  37. #include <asm/processor.h>
  38. /*
  39. * Version using sequence counter only.
  40. * This can be used when code has its own mutex protecting the
  41. * updating starting before the write_seqcountbeqin() and ending
  42. * after the write_seqcount_end().
  43. */
  44. typedef struct seqcount {
  45. unsigned sequence;
  46. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  47. struct lockdep_map dep_map;
  48. #endif
  49. } seqcount_t;
  50. static inline void __seqcount_init(seqcount_t *s, const char *name,
  51. struct lock_class_key *key)
  52. {
  53. /*
  54. * Make sure we are not reinitializing a held lock:
  55. */
  56. lockdep_init_map(&s->dep_map, name, key, 0);
  57. s->sequence = 0;
  58. }
  59. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  60. # define SEQCOUNT_DEP_MAP_INIT(lockname) \
  61. .dep_map = { .name = #lockname } \
  62. # define seqcount_init(s) \
  63. do { \
  64. static struct lock_class_key __key; \
  65. __seqcount_init((s), #s, &__key); \
  66. } while (0)
  67. static inline void seqcount_lockdep_reader_access(const seqcount_t *s)
  68. {
  69. seqcount_t *l = (seqcount_t *)s;
  70. unsigned long flags;
  71. local_irq_save(flags);
  72. seqcount_acquire_read(&l->dep_map, 0, 0, _RET_IP_);
  73. seqcount_release(&l->dep_map, 1, _RET_IP_);
  74. local_irq_restore(flags);
  75. }
  76. #else
  77. # define SEQCOUNT_DEP_MAP_INIT(lockname)
  78. # define seqcount_init(s) __seqcount_init(s, NULL, NULL)
  79. # define seqcount_lockdep_reader_access(x)
  80. #endif
  81. #define SEQCNT_ZERO(lockname) { .sequence = 0, SEQCOUNT_DEP_MAP_INIT(lockname)}
  82. /**
  83. * __read_seqcount_begin - begin a seq-read critical section (without barrier)
  84. * @s: pointer to seqcount_t
  85. * Returns: count to be passed to read_seqcount_retry
  86. *
  87. * __read_seqcount_begin is like read_seqcount_begin, but has no smp_rmb()
  88. * barrier. Callers should ensure that smp_rmb() or equivalent ordering is
  89. * provided before actually loading any of the variables that are to be
  90. * protected in this critical section.
  91. *
  92. * Use carefully, only in critical code, and comment how the barrier is
  93. * provided.
  94. */
  95. static inline unsigned __read_seqcount_begin(const seqcount_t *s)
  96. {
  97. unsigned ret;
  98. repeat:
  99. ret = ACCESS_ONCE(s->sequence);
  100. if (unlikely(ret & 1)) {
  101. cpu_relax();
  102. goto repeat;
  103. }
  104. return ret;
  105. }
  106. /**
  107. * raw_read_seqcount - Read the raw seqcount
  108. * @s: pointer to seqcount_t
  109. * Returns: count to be passed to read_seqcount_retry
  110. *
  111. * raw_read_seqcount opens a read critical section of the given
  112. * seqcount without any lockdep checking and without checking or
  113. * masking the LSB. Calling code is responsible for handling that.
  114. */
  115. static inline unsigned raw_read_seqcount(const seqcount_t *s)
  116. {
  117. unsigned ret = ACCESS_ONCE(s->sequence);
  118. smp_rmb();
  119. return ret;
  120. }
  121. /**
  122. * raw_read_seqcount_begin - start seq-read critical section w/o lockdep
  123. * @s: pointer to seqcount_t
  124. * Returns: count to be passed to read_seqcount_retry
  125. *
  126. * raw_read_seqcount_begin opens a read critical section of the given
  127. * seqcount, but without any lockdep checking. Validity of the critical
  128. * section is tested by checking read_seqcount_retry function.
  129. */
  130. static inline unsigned raw_read_seqcount_begin(const seqcount_t *s)
  131. {
  132. unsigned ret = __read_seqcount_begin(s);
  133. smp_rmb();
  134. return ret;
  135. }
  136. /**
  137. * read_seqcount_begin - begin a seq-read critical section
  138. * @s: pointer to seqcount_t
  139. * Returns: count to be passed to read_seqcount_retry
  140. *
  141. * read_seqcount_begin opens a read critical section of the given seqcount.
  142. * Validity of the critical section is tested by checking read_seqcount_retry
  143. * function.
  144. */
  145. static inline unsigned read_seqcount_begin(const seqcount_t *s)
  146. {
  147. seqcount_lockdep_reader_access(s);
  148. return raw_read_seqcount_begin(s);
  149. }
  150. /**
  151. * raw_seqcount_begin - begin a seq-read critical section
  152. * @s: pointer to seqcount_t
  153. * Returns: count to be passed to read_seqcount_retry
  154. *
  155. * raw_seqcount_begin opens a read critical section of the given seqcount.
  156. * Validity of the critical section is tested by checking read_seqcount_retry
  157. * function.
  158. *
  159. * Unlike read_seqcount_begin(), this function will not wait for the count
  160. * to stabilize. If a writer is active when we begin, we will fail the
  161. * read_seqcount_retry() instead of stabilizing at the beginning of the
  162. * critical section.
  163. */
  164. static inline unsigned raw_seqcount_begin(const seqcount_t *s)
  165. {
  166. unsigned ret = ACCESS_ONCE(s->sequence);
  167. smp_rmb();
  168. return ret & ~1;
  169. }
  170. /**
  171. * __read_seqcount_retry - end a seq-read critical section (without barrier)
  172. * @s: pointer to seqcount_t
  173. * @start: count, from read_seqcount_begin
  174. * Returns: 1 if retry is required, else 0
  175. *
  176. * __read_seqcount_retry is like read_seqcount_retry, but has no smp_rmb()
  177. * barrier. Callers should ensure that smp_rmb() or equivalent ordering is
  178. * provided before actually loading any of the variables that are to be
  179. * protected in this critical section.
  180. *
  181. * Use carefully, only in critical code, and comment how the barrier is
  182. * provided.
  183. */
  184. static inline int __read_seqcount_retry(const seqcount_t *s, unsigned start)
  185. {
  186. return unlikely(s->sequence != start);
  187. }
  188. /**
  189. * read_seqcount_retry - end a seq-read critical section
  190. * @s: pointer to seqcount_t
  191. * @start: count, from read_seqcount_begin
  192. * Returns: 1 if retry is required, else 0
  193. *
  194. * read_seqcount_retry closes a read critical section of the given seqcount.
  195. * If the critical section was invalid, it must be ignored (and typically
  196. * retried).
  197. */
  198. static inline int read_seqcount_retry(const seqcount_t *s, unsigned start)
  199. {
  200. smp_rmb();
  201. return __read_seqcount_retry(s, start);
  202. }
  203. static inline void raw_write_seqcount_begin(seqcount_t *s)
  204. {
  205. s->sequence++;
  206. smp_wmb();
  207. }
  208. static inline void raw_write_seqcount_end(seqcount_t *s)
  209. {
  210. smp_wmb();
  211. s->sequence++;
  212. }
  213. /*
  214. * raw_write_seqcount_latch - redirect readers to even/odd copy
  215. * @s: pointer to seqcount_t
  216. */
  217. static inline void raw_write_seqcount_latch(seqcount_t *s)
  218. {
  219. smp_wmb(); /* prior stores before incrementing "sequence" */
  220. s->sequence++;
  221. smp_wmb(); /* increment "sequence" before following stores */
  222. }
  223. /*
  224. * Sequence counter only version assumes that callers are using their
  225. * own mutexing.
  226. */
  227. static inline void write_seqcount_begin_nested(seqcount_t *s, int subclass)
  228. {
  229. raw_write_seqcount_begin(s);
  230. seqcount_acquire(&s->dep_map, subclass, 0, _RET_IP_);
  231. }
  232. static inline void write_seqcount_begin(seqcount_t *s)
  233. {
  234. write_seqcount_begin_nested(s, 0);
  235. }
  236. static inline void write_seqcount_end(seqcount_t *s)
  237. {
  238. seqcount_release(&s->dep_map, 1, _RET_IP_);
  239. raw_write_seqcount_end(s);
  240. }
  241. /**
  242. * write_seqcount_barrier - invalidate in-progress read-side seq operations
  243. * @s: pointer to seqcount_t
  244. *
  245. * After write_seqcount_barrier, no read-side seq operations will complete
  246. * successfully and see data older than this.
  247. */
  248. static inline void write_seqcount_barrier(seqcount_t *s)
  249. {
  250. smp_wmb();
  251. s->sequence+=2;
  252. }
  253. typedef struct {
  254. struct seqcount seqcount;
  255. spinlock_t lock;
  256. } seqlock_t;
  257. /*
  258. * These macros triggered gcc-3.x compile-time problems. We think these are
  259. * OK now. Be cautious.
  260. */
  261. #define __SEQLOCK_UNLOCKED(lockname) \
  262. { \
  263. .seqcount = SEQCNT_ZERO(lockname), \
  264. .lock = __SPIN_LOCK_UNLOCKED(lockname) \
  265. }
  266. #define seqlock_init(x) \
  267. do { \
  268. seqcount_init(&(x)->seqcount); \
  269. spin_lock_init(&(x)->lock); \
  270. } while (0)
  271. #define DEFINE_SEQLOCK(x) \
  272. seqlock_t x = __SEQLOCK_UNLOCKED(x)
  273. /*
  274. * Read side functions for starting and finalizing a read side section.
  275. */
  276. static inline unsigned read_seqbegin(const seqlock_t *sl)
  277. {
  278. return read_seqcount_begin(&sl->seqcount);
  279. }
  280. static inline unsigned read_seqretry(const seqlock_t *sl, unsigned start)
  281. {
  282. return read_seqcount_retry(&sl->seqcount, start);
  283. }
  284. /*
  285. * Lock out other writers and update the count.
  286. * Acts like a normal spin_lock/unlock.
  287. * Don't need preempt_disable() because that is in the spin_lock already.
  288. */
  289. static inline void write_seqlock(seqlock_t *sl)
  290. {
  291. spin_lock(&sl->lock);
  292. write_seqcount_begin(&sl->seqcount);
  293. }
  294. static inline void write_sequnlock(seqlock_t *sl)
  295. {
  296. write_seqcount_end(&sl->seqcount);
  297. spin_unlock(&sl->lock);
  298. }
  299. static inline void write_seqlock_bh(seqlock_t *sl)
  300. {
  301. spin_lock_bh(&sl->lock);
  302. write_seqcount_begin(&sl->seqcount);
  303. }
  304. static inline void write_sequnlock_bh(seqlock_t *sl)
  305. {
  306. write_seqcount_end(&sl->seqcount);
  307. spin_unlock_bh(&sl->lock);
  308. }
  309. static inline void write_seqlock_irq(seqlock_t *sl)
  310. {
  311. spin_lock_irq(&sl->lock);
  312. write_seqcount_begin(&sl->seqcount);
  313. }
  314. static inline void write_sequnlock_irq(seqlock_t *sl)
  315. {
  316. write_seqcount_end(&sl->seqcount);
  317. spin_unlock_irq(&sl->lock);
  318. }
  319. static inline unsigned long __write_seqlock_irqsave(seqlock_t *sl)
  320. {
  321. unsigned long flags;
  322. spin_lock_irqsave(&sl->lock, flags);
  323. write_seqcount_begin(&sl->seqcount);
  324. return flags;
  325. }
  326. #define write_seqlock_irqsave(lock, flags) \
  327. do { flags = __write_seqlock_irqsave(lock); } while (0)
  328. static inline void
  329. write_sequnlock_irqrestore(seqlock_t *sl, unsigned long flags)
  330. {
  331. write_seqcount_end(&sl->seqcount);
  332. spin_unlock_irqrestore(&sl->lock, flags);
  333. }
  334. /*
  335. * A locking reader exclusively locks out other writers and locking readers,
  336. * but doesn't update the sequence number. Acts like a normal spin_lock/unlock.
  337. * Don't need preempt_disable() because that is in the spin_lock already.
  338. */
  339. static inline void read_seqlock_excl(seqlock_t *sl)
  340. {
  341. spin_lock(&sl->lock);
  342. }
  343. static inline void read_sequnlock_excl(seqlock_t *sl)
  344. {
  345. spin_unlock(&sl->lock);
  346. }
  347. /**
  348. * read_seqbegin_or_lock - begin a sequence number check or locking block
  349. * @lock: sequence lock
  350. * @seq : sequence number to be checked
  351. *
  352. * First try it once optimistically without taking the lock. If that fails,
  353. * take the lock. The sequence number is also used as a marker for deciding
  354. * whether to be a reader (even) or writer (odd).
  355. * N.B. seq must be initialized to an even number to begin with.
  356. */
  357. static inline void read_seqbegin_or_lock(seqlock_t *lock, int *seq)
  358. {
  359. if (!(*seq & 1)) /* Even */
  360. *seq = read_seqbegin(lock);
  361. else /* Odd */
  362. read_seqlock_excl(lock);
  363. }
  364. static inline int need_seqretry(seqlock_t *lock, int seq)
  365. {
  366. return !(seq & 1) && read_seqretry(lock, seq);
  367. }
  368. static inline void done_seqretry(seqlock_t *lock, int seq)
  369. {
  370. if (seq & 1)
  371. read_sequnlock_excl(lock);
  372. }
  373. static inline void read_seqlock_excl_bh(seqlock_t *sl)
  374. {
  375. spin_lock_bh(&sl->lock);
  376. }
  377. static inline void read_sequnlock_excl_bh(seqlock_t *sl)
  378. {
  379. spin_unlock_bh(&sl->lock);
  380. }
  381. static inline void read_seqlock_excl_irq(seqlock_t *sl)
  382. {
  383. spin_lock_irq(&sl->lock);
  384. }
  385. static inline void read_sequnlock_excl_irq(seqlock_t *sl)
  386. {
  387. spin_unlock_irq(&sl->lock);
  388. }
  389. static inline unsigned long __read_seqlock_excl_irqsave(seqlock_t *sl)
  390. {
  391. unsigned long flags;
  392. spin_lock_irqsave(&sl->lock, flags);
  393. return flags;
  394. }
  395. #define read_seqlock_excl_irqsave(lock, flags) \
  396. do { flags = __read_seqlock_excl_irqsave(lock); } while (0)
  397. static inline void
  398. read_sequnlock_excl_irqrestore(seqlock_t *sl, unsigned long flags)
  399. {
  400. spin_unlock_irqrestore(&sl->lock, flags);
  401. }
  402. #endif /* __LINUX_SEQLOCK_H */