seqlock.rst 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. ======================================
  2. Sequence counters and sequential locks
  3. ======================================
  4. Introduction
  5. ============
  6. Sequence counters are a reader-writer consistency mechanism with
  7. lockless readers (read-only retry loops), and no writer starvation. They
  8. are used for data that's rarely written to (e.g. system time), where the
  9. reader wants a consistent set of information and is willing to retry if
  10. that information changes.
  11. A data set is consistent when the sequence count at the beginning of the
  12. read side critical section is even and the same sequence count value is
  13. read again at the end of the critical section. The data in the set must
  14. be copied out inside the read side critical section. If the sequence
  15. count has changed between the start and the end of the critical section,
  16. the reader must retry.
  17. Writers increment the sequence count at the start and the end of their
  18. critical section. After starting the critical section the sequence count
  19. is odd and indicates to the readers that an update is in progress. At
  20. the end of the write side critical section the sequence count becomes
  21. even again which lets readers make progress.
  22. A sequence counter write side critical section must never be preempted
  23. or interrupted by read side sections. Otherwise the reader will spin for
  24. the entire scheduler tick due to the odd sequence count value and the
  25. interrupted writer. If that reader belongs to a real-time scheduling
  26. class, it can spin forever and the kernel will livelock.
  27. This mechanism cannot be used if the protected data contains pointers,
  28. as the writer can invalidate a pointer that the reader is following.
  29. .. _seqcount_t:
  30. Sequence counters (``seqcount_t``)
  31. ==================================
  32. This is the the raw counting mechanism, which does not protect against
  33. multiple writers. Write side critical sections must thus be serialized
  34. by an external lock.
  35. If the write serialization primitive is not implicitly disabling
  36. preemption, preemption must be explicitly disabled before entering the
  37. write side section. If the read section can be invoked from hardirq or
  38. softirq contexts, interrupts or bottom halves must also be respectively
  39. disabled before entering the write section.
  40. If it's desired to automatically handle the sequence counter
  41. requirements of writer serialization and non-preemptibility, use
  42. :ref:`seqlock_t` instead.
  43. Initialization::
  44. /* dynamic */
  45. seqcount_t foo_seqcount;
  46. seqcount_init(&foo_seqcount);
  47. /* static */
  48. static seqcount_t foo_seqcount = SEQCNT_ZERO(foo_seqcount);
  49. /* C99 struct init */
  50. struct {
  51. .seq = SEQCNT_ZERO(foo.seq),
  52. } foo;
  53. Write path::
  54. /* Serialized context with disabled preemption */
  55. write_seqcount_begin(&foo_seqcount);
  56. /* ... [[write-side critical section]] ... */
  57. write_seqcount_end(&foo_seqcount);
  58. Read path::
  59. do {
  60. seq = read_seqcount_begin(&foo_seqcount);
  61. /* ... [[read-side critical section]] ... */
  62. } while (read_seqcount_retry(&foo_seqcount, seq));
  63. .. _seqcount_locktype_t:
  64. Sequence counters with associated locks (``seqcount_LOCKNAME_t``)
  65. -----------------------------------------------------------------
  66. As discussed at :ref:`seqcount_t`, sequence count write side critical
  67. sections must be serialized and non-preemptible. This variant of
  68. sequence counters associate the lock used for writer serialization at
  69. initialization time, which enables lockdep to validate that the write
  70. side critical sections are properly serialized.
  71. This lock association is a NOOP if lockdep is disabled and has neither
  72. storage nor runtime overhead. If lockdep is enabled, the lock pointer is
  73. stored in struct seqcount and lockdep's "lock is held" assertions are
  74. injected at the beginning of the write side critical section to validate
  75. that it is properly protected.
  76. For lock types which do not implicitly disable preemption, preemption
  77. protection is enforced in the write side function.
  78. The following sequence counters with associated locks are defined:
  79. - ``seqcount_spinlock_t``
  80. - ``seqcount_raw_spinlock_t``
  81. - ``seqcount_rwlock_t``
  82. - ``seqcount_mutex_t``
  83. - ``seqcount_ww_mutex_t``
  84. The sequence counter read and write APIs can take either a plain
  85. seqcount_t or any of the seqcount_LOCKNAME_t variants above.
  86. Initialization (replace "LOCKNAME" with one of the supported locks)::
  87. /* dynamic */
  88. seqcount_LOCKNAME_t foo_seqcount;
  89. seqcount_LOCKNAME_init(&foo_seqcount, &lock);
  90. /* static */
  91. static seqcount_LOCKNAME_t foo_seqcount =
  92. SEQCNT_LOCKNAME_ZERO(foo_seqcount, &lock);
  93. /* C99 struct init */
  94. struct {
  95. .seq = SEQCNT_LOCKNAME_ZERO(foo.seq, &lock),
  96. } foo;
  97. Write path: same as in :ref:`seqcount_t`, while running from a context
  98. with the associated write serialization lock acquired.
  99. Read path: same as in :ref:`seqcount_t`.
  100. .. _seqcount_latch_t:
  101. Latch sequence counters (``seqcount_latch_t``)
  102. ----------------------------------------------
  103. Latch sequence counters are a multiversion concurrency control mechanism
  104. where the embedded seqcount_t counter even/odd value is used to switch
  105. between two copies of protected data. This allows the sequence counter
  106. read path to safely interrupt its own write side critical section.
  107. Use seqcount_latch_t when the write side sections cannot be protected
  108. from interruption by readers. This is typically the case when the read
  109. side can be invoked from NMI handlers.
  110. Check `raw_write_seqcount_latch()` for more information.
  111. .. _seqlock_t:
  112. Sequential locks (``seqlock_t``)
  113. ================================
  114. This contains the :ref:`seqcount_t` mechanism earlier discussed, plus an
  115. embedded spinlock for writer serialization and non-preemptibility.
  116. If the read side section can be invoked from hardirq or softirq context,
  117. use the write side function variants which disable interrupts or bottom
  118. halves respectively.
  119. Initialization::
  120. /* dynamic */
  121. seqlock_t foo_seqlock;
  122. seqlock_init(&foo_seqlock);
  123. /* static */
  124. static DEFINE_SEQLOCK(foo_seqlock);
  125. /* C99 struct init */
  126. struct {
  127. .seql = __SEQLOCK_UNLOCKED(foo.seql)
  128. } foo;
  129. Write path::
  130. write_seqlock(&foo_seqlock);
  131. /* ... [[write-side critical section]] ... */
  132. write_sequnlock(&foo_seqlock);
  133. Read path, three categories:
  134. 1. Normal Sequence readers which never block a writer but they must
  135. retry if a writer is in progress by detecting change in the sequence
  136. number. Writers do not wait for a sequence reader::
  137. do {
  138. seq = read_seqbegin(&foo_seqlock);
  139. /* ... [[read-side critical section]] ... */
  140. } while (read_seqretry(&foo_seqlock, seq));
  141. 2. Locking readers which will wait if a writer or another locking reader
  142. is in progress. A locking reader in progress will also block a writer
  143. from entering its critical section. This read lock is
  144. exclusive. Unlike rwlock_t, only one locking reader can acquire it::
  145. read_seqlock_excl(&foo_seqlock);
  146. /* ... [[read-side critical section]] ... */
  147. read_sequnlock_excl(&foo_seqlock);
  148. 3. Conditional lockless reader (as in 1), or locking reader (as in 2),
  149. according to a passed marker. This is used to avoid lockless readers
  150. starvation (too much retry loops) in case of a sharp spike in write
  151. activity. First, a lockless read is tried (even marker passed). If
  152. that trial fails (odd sequence counter is returned, which is used as
  153. the next iteration marker), the lockless read is transformed to a
  154. full locking read and no retry loop is necessary::
  155. /* marker; even initialization */
  156. int seq = 0;
  157. do {
  158. read_seqbegin_or_lock(&foo_seqlock, &seq);
  159. /* ... [[read-side critical section]] ... */
  160. } while (need_seqretry(&foo_seqlock, seq));
  161. done_seqretry(&foo_seqlock, seq);
  162. API documentation
  163. =================
  164. .. kernel-doc:: include/linux/seqlock.h