seqlock.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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 <linux/compiler.h>
  38. #include <asm/processor.h>
  39. /*
  40. * Version using sequence counter only.
  41. * This can be used when code has its own mutex protecting the
  42. * updating starting before the write_seqcountbeqin() and ending
  43. * after the write_seqcount_end().
  44. */
  45. typedef struct seqcount {
  46. unsigned sequence;
  47. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  48. struct lockdep_map dep_map;
  49. #endif
  50. } seqcount_t;
  51. static inline void __seqcount_init(seqcount_t *s, const char *name,
  52. struct lock_class_key *key)
  53. {
  54. /*
  55. * Make sure we are not reinitializing a held lock:
  56. */
  57. lockdep_init_map(&s->dep_map, name, key, 0);
  58. s->sequence = 0;
  59. }
  60. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  61. # define SEQCOUNT_DEP_MAP_INIT(lockname) \
  62. .dep_map = { .name = #lockname } \
  63. # define seqcount_init(s) \
  64. do { \
  65. static struct lock_class_key __key; \
  66. __seqcount_init((s), #s, &__key); \
  67. } while (0)
  68. static inline void seqcount_lockdep_reader_access(const seqcount_t *s)
  69. {
  70. seqcount_t *l = (seqcount_t *)s;
  71. unsigned long flags;
  72. local_irq_save(flags);
  73. seqcount_acquire_read(&l->dep_map, 0, 0, _RET_IP_);
  74. seqcount_release(&l->dep_map, 1, _RET_IP_);
  75. local_irq_restore(flags);
  76. }
  77. #else
  78. # define SEQCOUNT_DEP_MAP_INIT(lockname)
  79. # define seqcount_init(s) __seqcount_init(s, NULL, NULL)
  80. # define seqcount_lockdep_reader_access(x)
  81. #endif
  82. #define SEQCNT_ZERO(lockname) { .sequence = 0, SEQCOUNT_DEP_MAP_INIT(lockname)}
  83. /**
  84. * __read_seqcount_begin - begin a seq-read critical section (without barrier)
  85. * @s: pointer to seqcount_t
  86. * Returns: count to be passed to read_seqcount_retry
  87. *
  88. * __read_seqcount_begin is like read_seqcount_begin, but has no smp_rmb()
  89. * barrier. Callers should ensure that smp_rmb() or equivalent ordering is
  90. * provided before actually loading any of the variables that are to be
  91. * protected in this critical section.
  92. *
  93. * Use carefully, only in critical code, and comment how the barrier is
  94. * provided.
  95. */
  96. static inline unsigned __read_seqcount_begin(const seqcount_t *s)
  97. {
  98. unsigned ret;
  99. repeat:
  100. ret = READ_ONCE(s->sequence);
  101. if (unlikely(ret & 1)) {
  102. cpu_relax();
  103. goto repeat;
  104. }
  105. return ret;
  106. }
  107. /**
  108. * raw_read_seqcount - Read the raw seqcount
  109. * @s: pointer to seqcount_t
  110. * Returns: count to be passed to read_seqcount_retry
  111. *
  112. * raw_read_seqcount opens a read critical section of the given
  113. * seqcount without any lockdep checking and without checking or
  114. * masking the LSB. Calling code is responsible for handling that.
  115. */
  116. static inline unsigned raw_read_seqcount(const seqcount_t *s)
  117. {
  118. unsigned ret = READ_ONCE(s->sequence);
  119. smp_rmb();
  120. return ret;
  121. }
  122. /**
  123. * raw_read_seqcount_begin - start seq-read critical section w/o lockdep
  124. * @s: pointer to seqcount_t
  125. * Returns: count to be passed to read_seqcount_retry
  126. *
  127. * raw_read_seqcount_begin opens a read critical section of the given
  128. * seqcount, but without any lockdep checking. Validity of the critical
  129. * section is tested by checking read_seqcount_retry function.
  130. */
  131. static inline unsigned raw_read_seqcount_begin(const seqcount_t *s)
  132. {
  133. unsigned ret = __read_seqcount_begin(s);
  134. smp_rmb();
  135. return ret;
  136. }
  137. /**
  138. * read_seqcount_begin - begin a seq-read critical section
  139. * @s: pointer to seqcount_t
  140. * Returns: count to be passed to read_seqcount_retry
  141. *
  142. * read_seqcount_begin opens a read critical section of the given seqcount.
  143. * Validity of the critical section is tested by checking read_seqcount_retry
  144. * function.
  145. */
  146. static inline unsigned read_seqcount_begin(const seqcount_t *s)
  147. {
  148. seqcount_lockdep_reader_access(s);
  149. return raw_read_seqcount_begin(s);
  150. }
  151. /**
  152. * raw_seqcount_begin - begin a seq-read critical section
  153. * @s: pointer to seqcount_t
  154. * Returns: count to be passed to read_seqcount_retry
  155. *
  156. * raw_seqcount_begin opens a read critical section of the given seqcount.
  157. * Validity of the critical section is tested by checking read_seqcount_retry
  158. * function.
  159. *
  160. * Unlike read_seqcount_begin(), this function will not wait for the count
  161. * to stabilize. If a writer is active when we begin, we will fail the
  162. * read_seqcount_retry() instead of stabilizing at the beginning of the
  163. * critical section.
  164. */
  165. static inline unsigned raw_seqcount_begin(const seqcount_t *s)
  166. {
  167. unsigned ret = READ_ONCE(s->sequence);
  168. smp_rmb();
  169. return ret & ~1;
  170. }
  171. /**
  172. * __read_seqcount_retry - end a seq-read critical section (without barrier)
  173. * @s: pointer to seqcount_t
  174. * @start: count, from read_seqcount_begin
  175. * Returns: 1 if retry is required, else 0
  176. *
  177. * __read_seqcount_retry is like read_seqcount_retry, but has no smp_rmb()
  178. * barrier. Callers should ensure that smp_rmb() or equivalent ordering is
  179. * provided before actually loading any of the variables that are to be
  180. * protected in this critical section.
  181. *
  182. * Use carefully, only in critical code, and comment how the barrier is
  183. * provided.
  184. */
  185. static inline int __read_seqcount_retry(const seqcount_t *s, unsigned start)
  186. {
  187. return unlikely(s->sequence != start);
  188. }
  189. /**
  190. * read_seqcount_retry - end a seq-read critical section
  191. * @s: pointer to seqcount_t
  192. * @start: count, from read_seqcount_begin
  193. * Returns: 1 if retry is required, else 0
  194. *
  195. * read_seqcount_retry closes a read critical section of the given seqcount.
  196. * If the critical section was invalid, it must be ignored (and typically
  197. * retried).
  198. */
  199. static inline int read_seqcount_retry(const seqcount_t *s, unsigned start)
  200. {
  201. smp_rmb();
  202. return __read_seqcount_retry(s, start);
  203. }
  204. static inline void raw_write_seqcount_begin(seqcount_t *s)
  205. {
  206. s->sequence++;
  207. smp_wmb();
  208. }
  209. static inline void raw_write_seqcount_end(seqcount_t *s)
  210. {
  211. smp_wmb();
  212. s->sequence++;
  213. }
  214. /**
  215. * raw_write_seqcount_barrier - do a seq write barrier
  216. * @s: pointer to seqcount_t
  217. *
  218. * This can be used to provide an ordering guarantee instead of the
  219. * usual consistency guarantee. It is one wmb cheaper, because we can
  220. * collapse the two back-to-back wmb()s.
  221. *
  222. * seqcount_t seq;
  223. * bool X = true, Y = false;
  224. *
  225. * void read(void)
  226. * {
  227. * bool x, y;
  228. *
  229. * do {
  230. * int s = read_seqcount_begin(&seq);
  231. *
  232. * x = X; y = Y;
  233. *
  234. * } while (read_seqcount_retry(&seq, s));
  235. *
  236. * BUG_ON(!x && !y);
  237. * }
  238. *
  239. * void write(void)
  240. * {
  241. * Y = true;
  242. *
  243. * raw_write_seqcount_barrier(seq);
  244. *
  245. * X = false;
  246. * }
  247. */
  248. static inline void raw_write_seqcount_barrier(seqcount_t *s)
  249. {
  250. s->sequence++;
  251. smp_wmb();
  252. s->sequence++;
  253. }
  254. static inline int raw_read_seqcount_latch(seqcount_t *s)
  255. {
  256. int seq = READ_ONCE(s->sequence);
  257. /* Pairs with the first smp_wmb() in raw_write_seqcount_latch() */
  258. smp_read_barrier_depends();
  259. return seq;
  260. }
  261. /**
  262. * raw_write_seqcount_latch - redirect readers to even/odd copy
  263. * @s: pointer to seqcount_t
  264. *
  265. * The latch technique is a multiversion concurrency control method that allows
  266. * queries during non-atomic modifications. If you can guarantee queries never
  267. * interrupt the modification -- e.g. the concurrency is strictly between CPUs
  268. * -- you most likely do not need this.
  269. *
  270. * Where the traditional RCU/lockless data structures rely on atomic
  271. * modifications to ensure queries observe either the old or the new state the
  272. * latch allows the same for non-atomic updates. The trade-off is doubling the
  273. * cost of storage; we have to maintain two copies of the entire data
  274. * structure.
  275. *
  276. * Very simply put: we first modify one copy and then the other. This ensures
  277. * there is always one copy in a stable state, ready to give us an answer.
  278. *
  279. * The basic form is a data structure like:
  280. *
  281. * struct latch_struct {
  282. * seqcount_t seq;
  283. * struct data_struct data[2];
  284. * };
  285. *
  286. * Where a modification, which is assumed to be externally serialized, does the
  287. * following:
  288. *
  289. * void latch_modify(struct latch_struct *latch, ...)
  290. * {
  291. * smp_wmb(); <- Ensure that the last data[1] update is visible
  292. * latch->seq++;
  293. * smp_wmb(); <- Ensure that the seqcount update is visible
  294. *
  295. * modify(latch->data[0], ...);
  296. *
  297. * smp_wmb(); <- Ensure that the data[0] update is visible
  298. * latch->seq++;
  299. * smp_wmb(); <- Ensure that the seqcount update is visible
  300. *
  301. * modify(latch->data[1], ...);
  302. * }
  303. *
  304. * The query will have a form like:
  305. *
  306. * struct entry *latch_query(struct latch_struct *latch, ...)
  307. * {
  308. * struct entry *entry;
  309. * unsigned seq, idx;
  310. *
  311. * do {
  312. * seq = raw_read_seqcount_latch(&latch->seq);
  313. *
  314. * idx = seq & 0x01;
  315. * entry = data_query(latch->data[idx], ...);
  316. *
  317. * smp_rmb();
  318. * } while (seq != latch->seq);
  319. *
  320. * return entry;
  321. * }
  322. *
  323. * So during the modification, queries are first redirected to data[1]. Then we
  324. * modify data[0]. When that is complete, we redirect queries back to data[0]
  325. * and we can modify data[1].
  326. *
  327. * NOTE: The non-requirement for atomic modifications does _NOT_ include
  328. * the publishing of new entries in the case where data is a dynamic
  329. * data structure.
  330. *
  331. * An iteration might start in data[0] and get suspended long enough
  332. * to miss an entire modification sequence, once it resumes it might
  333. * observe the new entry.
  334. *
  335. * NOTE: When data is a dynamic data structure; one should use regular RCU
  336. * patterns to manage the lifetimes of the objects within.
  337. */
  338. static inline void raw_write_seqcount_latch(seqcount_t *s)
  339. {
  340. smp_wmb(); /* prior stores before incrementing "sequence" */
  341. s->sequence++;
  342. smp_wmb(); /* increment "sequence" before following stores */
  343. }
  344. /*
  345. * Sequence counter only version assumes that callers are using their
  346. * own mutexing.
  347. */
  348. static inline void write_seqcount_begin_nested(seqcount_t *s, int subclass)
  349. {
  350. raw_write_seqcount_begin(s);
  351. seqcount_acquire(&s->dep_map, subclass, 0, _RET_IP_);
  352. }
  353. static inline void write_seqcount_begin(seqcount_t *s)
  354. {
  355. write_seqcount_begin_nested(s, 0);
  356. }
  357. static inline void write_seqcount_end(seqcount_t *s)
  358. {
  359. seqcount_release(&s->dep_map, 1, _RET_IP_);
  360. raw_write_seqcount_end(s);
  361. }
  362. /**
  363. * write_seqcount_invalidate - invalidate in-progress read-side seq operations
  364. * @s: pointer to seqcount_t
  365. *
  366. * After write_seqcount_invalidate, no read-side seq operations will complete
  367. * successfully and see data older than this.
  368. */
  369. static inline void write_seqcount_invalidate(seqcount_t *s)
  370. {
  371. smp_wmb();
  372. s->sequence+=2;
  373. }
  374. typedef struct {
  375. struct seqcount seqcount;
  376. spinlock_t lock;
  377. } seqlock_t;
  378. /*
  379. * These macros triggered gcc-3.x compile-time problems. We think these are
  380. * OK now. Be cautious.
  381. */
  382. #define __SEQLOCK_UNLOCKED(lockname) \
  383. { \
  384. .seqcount = SEQCNT_ZERO(lockname), \
  385. .lock = __SPIN_LOCK_UNLOCKED(lockname) \
  386. }
  387. #define seqlock_init(x) \
  388. do { \
  389. seqcount_init(&(x)->seqcount); \
  390. spin_lock_init(&(x)->lock); \
  391. } while (0)
  392. #define DEFINE_SEQLOCK(x) \
  393. seqlock_t x = __SEQLOCK_UNLOCKED(x)
  394. /*
  395. * Read side functions for starting and finalizing a read side section.
  396. */
  397. static inline unsigned read_seqbegin(const seqlock_t *sl)
  398. {
  399. return read_seqcount_begin(&sl->seqcount);
  400. }
  401. static inline unsigned read_seqretry(const seqlock_t *sl, unsigned start)
  402. {
  403. return read_seqcount_retry(&sl->seqcount, start);
  404. }
  405. /*
  406. * Lock out other writers and update the count.
  407. * Acts like a normal spin_lock/unlock.
  408. * Don't need preempt_disable() because that is in the spin_lock already.
  409. */
  410. static inline void write_seqlock(seqlock_t *sl)
  411. {
  412. spin_lock(&sl->lock);
  413. write_seqcount_begin(&sl->seqcount);
  414. }
  415. static inline void write_sequnlock(seqlock_t *sl)
  416. {
  417. write_seqcount_end(&sl->seqcount);
  418. spin_unlock(&sl->lock);
  419. }
  420. static inline void write_seqlock_bh(seqlock_t *sl)
  421. {
  422. spin_lock_bh(&sl->lock);
  423. write_seqcount_begin(&sl->seqcount);
  424. }
  425. static inline void write_sequnlock_bh(seqlock_t *sl)
  426. {
  427. write_seqcount_end(&sl->seqcount);
  428. spin_unlock_bh(&sl->lock);
  429. }
  430. static inline void write_seqlock_irq(seqlock_t *sl)
  431. {
  432. spin_lock_irq(&sl->lock);
  433. write_seqcount_begin(&sl->seqcount);
  434. }
  435. static inline void write_sequnlock_irq(seqlock_t *sl)
  436. {
  437. write_seqcount_end(&sl->seqcount);
  438. spin_unlock_irq(&sl->lock);
  439. }
  440. static inline unsigned long __write_seqlock_irqsave(seqlock_t *sl)
  441. {
  442. unsigned long flags;
  443. spin_lock_irqsave(&sl->lock, flags);
  444. write_seqcount_begin(&sl->seqcount);
  445. return flags;
  446. }
  447. #define write_seqlock_irqsave(lock, flags) \
  448. do { flags = __write_seqlock_irqsave(lock); } while (0)
  449. static inline void
  450. write_sequnlock_irqrestore(seqlock_t *sl, unsigned long flags)
  451. {
  452. write_seqcount_end(&sl->seqcount);
  453. spin_unlock_irqrestore(&sl->lock, flags);
  454. }
  455. /*
  456. * A locking reader exclusively locks out other writers and locking readers,
  457. * but doesn't update the sequence number. Acts like a normal spin_lock/unlock.
  458. * Don't need preempt_disable() because that is in the spin_lock already.
  459. */
  460. static inline void read_seqlock_excl(seqlock_t *sl)
  461. {
  462. spin_lock(&sl->lock);
  463. }
  464. static inline void read_sequnlock_excl(seqlock_t *sl)
  465. {
  466. spin_unlock(&sl->lock);
  467. }
  468. /**
  469. * read_seqbegin_or_lock - begin a sequence number check or locking block
  470. * @lock: sequence lock
  471. * @seq : sequence number to be checked
  472. *
  473. * First try it once optimistically without taking the lock. If that fails,
  474. * take the lock. The sequence number is also used as a marker for deciding
  475. * whether to be a reader (even) or writer (odd).
  476. * N.B. seq must be initialized to an even number to begin with.
  477. */
  478. static inline void read_seqbegin_or_lock(seqlock_t *lock, int *seq)
  479. {
  480. if (!(*seq & 1)) /* Even */
  481. *seq = read_seqbegin(lock);
  482. else /* Odd */
  483. read_seqlock_excl(lock);
  484. }
  485. static inline int need_seqretry(seqlock_t *lock, int seq)
  486. {
  487. return !(seq & 1) && read_seqretry(lock, seq);
  488. }
  489. static inline void done_seqretry(seqlock_t *lock, int seq)
  490. {
  491. if (seq & 1)
  492. read_sequnlock_excl(lock);
  493. }
  494. static inline void read_seqlock_excl_bh(seqlock_t *sl)
  495. {
  496. spin_lock_bh(&sl->lock);
  497. }
  498. static inline void read_sequnlock_excl_bh(seqlock_t *sl)
  499. {
  500. spin_unlock_bh(&sl->lock);
  501. }
  502. static inline void read_seqlock_excl_irq(seqlock_t *sl)
  503. {
  504. spin_lock_irq(&sl->lock);
  505. }
  506. static inline void read_sequnlock_excl_irq(seqlock_t *sl)
  507. {
  508. spin_unlock_irq(&sl->lock);
  509. }
  510. static inline unsigned long __read_seqlock_excl_irqsave(seqlock_t *sl)
  511. {
  512. unsigned long flags;
  513. spin_lock_irqsave(&sl->lock, flags);
  514. return flags;
  515. }
  516. #define read_seqlock_excl_irqsave(lock, flags) \
  517. do { flags = __read_seqlock_excl_irqsave(lock); } while (0)
  518. static inline void
  519. read_sequnlock_excl_irqrestore(seqlock_t *sl, unsigned long flags)
  520. {
  521. spin_unlock_irqrestore(&sl->lock, flags);
  522. }
  523. static inline unsigned long
  524. read_seqbegin_or_lock_irqsave(seqlock_t *lock, int *seq)
  525. {
  526. unsigned long flags = 0;
  527. if (!(*seq & 1)) /* Even */
  528. *seq = read_seqbegin(lock);
  529. else /* Odd */
  530. read_seqlock_excl_irqsave(lock, flags);
  531. return flags;
  532. }
  533. static inline void
  534. done_seqretry_irqrestore(seqlock_t *lock, int seq, unsigned long flags)
  535. {
  536. if (seq & 1)
  537. read_sequnlock_excl_irqrestore(lock, flags);
  538. }
  539. #endif /* __LINUX_SEQLOCK_H */