seqlock.rst 7.6 KB

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