1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LINUX_SEQLOCK_H
3 #define __LINUX_SEQLOCK_H
6 * seqcount_t / seqlock_t - a reader-writer consistency mechanism with
7 * lockless readers (read-only retry loops), and no writer starvation.
9 * See Documentation/locking/seqlock.rst
12 * - Based on x86_64 vsyscall gettimeofday: Keith Owens, Andrea Arcangeli
13 * - Sequence counters with associated locks, (C) 2020 Linutronix GmbH
16 #include <linux/compiler.h>
17 #include <linux/kcsan-checks.h>
18 #include <linux/lockdep.h>
19 #include <linux/mutex.h>
20 #include <linux/preempt.h>
21 #include <linux/seqlock_types.h>
22 #include <linux/spinlock.h>
24 #include <asm/processor.h>
27 * The seqlock seqcount_t interface does not prescribe a precise sequence of
28 * read begin/retry/end. For readers, typically there is a call to
29 * read_seqcount_begin() and read_seqcount_retry(), however, there are more
30 * esoteric cases which do not follow this pattern.
32 * As a consequence, we take the following best-effort approach for raw usage
33 * via seqcount_t under KCSAN: upon beginning a seq-reader critical section,
34 * pessimistically mark the next KCSAN_SEQLOCK_REGION_MAX memory accesses as
35 * atomics; if there is a matching read_seqcount_retry() call, no following
36 * memory operations are considered atomic. Usage of the seqlock_t interface
39 #define KCSAN_SEQLOCK_REGION_MAX 1000
41 static inline void __seqcount_init(seqcount_t *s, const char *name,
42 struct lock_class_key *key)
45 * Make sure we are not reinitializing a held lock:
47 lockdep_init_map(&s->dep_map, name, key, 0);
51 #ifdef CONFIG_DEBUG_LOCK_ALLOC
53 # define SEQCOUNT_DEP_MAP_INIT(lockname) \
54 .dep_map = { .name = #lockname }
57 * seqcount_init() - runtime initializer for seqcount_t
58 * @s: Pointer to the seqcount_t instance
60 # define seqcount_init(s) \
62 static struct lock_class_key __key; \
63 __seqcount_init((s), #s, &__key); \
66 static inline void seqcount_lockdep_reader_access(const seqcount_t *s)
68 seqcount_t *l = (seqcount_t *)s;
71 local_irq_save(flags);
72 seqcount_acquire_read(&l->dep_map, 0, 0, _RET_IP_);
73 seqcount_release(&l->dep_map, _RET_IP_);
74 local_irq_restore(flags);
78 # define SEQCOUNT_DEP_MAP_INIT(lockname)
79 # define seqcount_init(s) __seqcount_init(s, NULL, NULL)
80 # define seqcount_lockdep_reader_access(x)
84 * SEQCNT_ZERO() - static initializer for seqcount_t
85 * @name: Name of the seqcount_t instance
87 #define SEQCNT_ZERO(name) { .sequence = 0, SEQCOUNT_DEP_MAP_INIT(name) }
90 * Sequence counters with associated locks (seqcount_LOCKNAME_t)
92 * A sequence counter which associates the lock used for writer
93 * serialization at initialization time. This enables lockdep to validate
94 * that the write side critical section is properly serialized.
96 * For associated locks which do not implicitly disable preemption,
97 * preemption protection is enforced in the write side function.
99 * Lockdep is never used in any for the raw write variants.
101 * See Documentation/locking/seqlock.rst
105 * typedef seqcount_LOCKNAME_t - sequence counter with LOCKNAME associated
106 * @seqcount: The real sequence counter
107 * @lock: Pointer to the associated lock
109 * A plain sequence counter with external writer synchronization by
110 * LOCKNAME @lock. The lock is associated to the sequence counter in the
111 * static initializer or init function. This enables lockdep to validate
112 * that the write side critical section is properly serialized.
114 * LOCKNAME: raw_spinlock, spinlock, rwlock or mutex
118 * seqcount_LOCKNAME_init() - runtime initializer for seqcount_LOCKNAME_t
119 * @s: Pointer to the seqcount_LOCKNAME_t instance
120 * @lock: Pointer to the associated lock
123 #define seqcount_LOCKNAME_init(s, _lock, lockname) \
125 seqcount_##lockname##_t *____s = (s); \
126 seqcount_init(&____s->seqcount); \
127 __SEQ_LOCK(____s->lock = (_lock)); \
130 #define seqcount_raw_spinlock_init(s, lock) seqcount_LOCKNAME_init(s, lock, raw_spinlock)
131 #define seqcount_spinlock_init(s, lock) seqcount_LOCKNAME_init(s, lock, spinlock)
132 #define seqcount_rwlock_init(s, lock) seqcount_LOCKNAME_init(s, lock, rwlock)
133 #define seqcount_mutex_init(s, lock) seqcount_LOCKNAME_init(s, lock, mutex)
136 * SEQCOUNT_LOCKNAME() - Instantiate seqcount_LOCKNAME_t and helpers
137 * seqprop_LOCKNAME_*() - Property accessors for seqcount_LOCKNAME_t
139 * @lockname: "LOCKNAME" part of seqcount_LOCKNAME_t
140 * @locktype: LOCKNAME canonical C data type
141 * @preemptible: preemptibility of above locktype
142 * @lockbase: prefix for associated lock/unlock
144 #define SEQCOUNT_LOCKNAME(lockname, locktype, preemptible, lockbase) \
145 static __always_inline seqcount_t * \
146 __seqprop_##lockname##_ptr(seqcount_##lockname##_t *s) \
148 return &s->seqcount; \
151 static __always_inline const seqcount_t * \
152 __seqprop_##lockname##_const_ptr(const seqcount_##lockname##_t *s) \
154 return &s->seqcount; \
157 static __always_inline unsigned \
158 __seqprop_##lockname##_sequence(const seqcount_##lockname##_t *s) \
160 unsigned seq = READ_ONCE(s->seqcount.sequence); \
162 if (!IS_ENABLED(CONFIG_PREEMPT_RT)) \
165 if (preemptible && unlikely(seq & 1)) { \
166 __SEQ_LOCK(lockbase##_lock(s->lock)); \
167 __SEQ_LOCK(lockbase##_unlock(s->lock)); \
170 * Re-read the sequence counter since the (possibly \
171 * preempted) writer made progress. \
173 seq = READ_ONCE(s->seqcount.sequence); \
179 static __always_inline bool \
180 __seqprop_##lockname##_preemptible(const seqcount_##lockname##_t *s) \
182 if (!IS_ENABLED(CONFIG_PREEMPT_RT)) \
183 return preemptible; \
185 /* PREEMPT_RT relies on the above LOCK+UNLOCK */ \
189 static __always_inline void \
190 __seqprop_##lockname##_assert(const seqcount_##lockname##_t *s) \
192 __SEQ_LOCK(lockdep_assert_held(s->lock)); \
196 * __seqprop() for seqcount_t
199 static inline seqcount_t *__seqprop_ptr(seqcount_t *s)
204 static inline const seqcount_t *__seqprop_const_ptr(const seqcount_t *s)
209 static inline unsigned __seqprop_sequence(const seqcount_t *s)
211 return READ_ONCE(s->sequence);
214 static inline bool __seqprop_preemptible(const seqcount_t *s)
219 static inline void __seqprop_assert(const seqcount_t *s)
221 lockdep_assert_preemption_disabled();
224 #define __SEQ_RT IS_ENABLED(CONFIG_PREEMPT_RT)
226 SEQCOUNT_LOCKNAME(raw_spinlock, raw_spinlock_t, false, raw_spin)
227 SEQCOUNT_LOCKNAME(spinlock, spinlock_t, __SEQ_RT, spin)
228 SEQCOUNT_LOCKNAME(rwlock, rwlock_t, __SEQ_RT, read)
229 SEQCOUNT_LOCKNAME(mutex, struct mutex, true, mutex)
230 #undef SEQCOUNT_LOCKNAME
233 * SEQCNT_LOCKNAME_ZERO - static initializer for seqcount_LOCKNAME_t
234 * @name: Name of the seqcount_LOCKNAME_t instance
235 * @lock: Pointer to the associated LOCKNAME
238 #define SEQCOUNT_LOCKNAME_ZERO(seq_name, assoc_lock) { \
239 .seqcount = SEQCNT_ZERO(seq_name.seqcount), \
240 __SEQ_LOCK(.lock = (assoc_lock)) \
243 #define SEQCNT_RAW_SPINLOCK_ZERO(name, lock) SEQCOUNT_LOCKNAME_ZERO(name, lock)
244 #define SEQCNT_SPINLOCK_ZERO(name, lock) SEQCOUNT_LOCKNAME_ZERO(name, lock)
245 #define SEQCNT_RWLOCK_ZERO(name, lock) SEQCOUNT_LOCKNAME_ZERO(name, lock)
246 #define SEQCNT_MUTEX_ZERO(name, lock) SEQCOUNT_LOCKNAME_ZERO(name, lock)
247 #define SEQCNT_WW_MUTEX_ZERO(name, lock) SEQCOUNT_LOCKNAME_ZERO(name, lock)
249 #define __seqprop_case(s, lockname, prop) \
250 seqcount_##lockname##_t: __seqprop_##lockname##_##prop
252 #define __seqprop(s, prop) _Generic(*(s), \
253 seqcount_t: __seqprop_##prop, \
254 __seqprop_case((s), raw_spinlock, prop), \
255 __seqprop_case((s), spinlock, prop), \
256 __seqprop_case((s), rwlock, prop), \
257 __seqprop_case((s), mutex, prop))
259 #define seqprop_ptr(s) __seqprop(s, ptr)(s)
260 #define seqprop_const_ptr(s) __seqprop(s, const_ptr)(s)
261 #define seqprop_sequence(s) __seqprop(s, sequence)(s)
262 #define seqprop_preemptible(s) __seqprop(s, preemptible)(s)
263 #define seqprop_assert(s) __seqprop(s, assert)(s)
266 * __read_seqcount_begin() - begin a seqcount_t read section w/o barrier
267 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
269 * __read_seqcount_begin is like read_seqcount_begin, but has no smp_rmb()
270 * barrier. Callers should ensure that smp_rmb() or equivalent ordering is
271 * provided before actually loading any of the variables that are to be
272 * protected in this critical section.
274 * Use carefully, only in critical code, and comment how the barrier is
277 * Return: count to be passed to read_seqcount_retry()
279 #define __read_seqcount_begin(s) \
283 while ((__seq = seqprop_sequence(s)) & 1) \
286 kcsan_atomic_next(KCSAN_SEQLOCK_REGION_MAX); \
291 * raw_read_seqcount_begin() - begin a seqcount_t read section w/o lockdep
292 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
294 * Return: count to be passed to read_seqcount_retry()
296 #define raw_read_seqcount_begin(s) \
298 unsigned _seq = __read_seqcount_begin(s); \
305 * read_seqcount_begin() - begin a seqcount_t read critical section
306 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
308 * Return: count to be passed to read_seqcount_retry()
310 #define read_seqcount_begin(s) \
312 seqcount_lockdep_reader_access(seqprop_const_ptr(s)); \
313 raw_read_seqcount_begin(s); \
317 * raw_read_seqcount() - read the raw seqcount_t counter value
318 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
320 * raw_read_seqcount opens a read critical section of the given
321 * seqcount_t, without any lockdep checking, and without checking or
322 * masking the sequence counter LSB. Calling code is responsible for
325 * Return: count to be passed to read_seqcount_retry()
327 #define raw_read_seqcount(s) \
329 unsigned __seq = seqprop_sequence(s); \
332 kcsan_atomic_next(KCSAN_SEQLOCK_REGION_MAX); \
337 * raw_seqcount_begin() - begin a seqcount_t read critical section w/o
338 * lockdep and w/o counter stabilization
339 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
341 * raw_seqcount_begin opens a read critical section of the given
342 * seqcount_t. Unlike read_seqcount_begin(), this function will not wait
343 * for the count to stabilize. If a writer is active when it begins, it
344 * will fail the read_seqcount_retry() at the end of the read critical
345 * section instead of stabilizing at the beginning of it.
347 * Use this only in special kernel hot paths where the read section is
348 * small and has a high probability of success through other external
349 * means. It will save a single branching instruction.
351 * Return: count to be passed to read_seqcount_retry()
353 #define raw_seqcount_begin(s) \
356 * If the counter is odd, let read_seqcount_retry() fail \
357 * by decrementing the counter. \
359 raw_read_seqcount(s) & ~1; \
363 * __read_seqcount_retry() - end a seqcount_t read section w/o barrier
364 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
365 * @start: count, from read_seqcount_begin()
367 * __read_seqcount_retry is like read_seqcount_retry, but has no smp_rmb()
368 * barrier. Callers should ensure that smp_rmb() or equivalent ordering is
369 * provided before actually loading any of the variables that are to be
370 * protected in this critical section.
372 * Use carefully, only in critical code, and comment how the barrier is
375 * Return: true if a read section retry is required, else false
377 #define __read_seqcount_retry(s, start) \
378 do___read_seqcount_retry(seqprop_const_ptr(s), start)
380 static inline int do___read_seqcount_retry(const seqcount_t *s, unsigned start)
382 kcsan_atomic_next(0);
383 return unlikely(READ_ONCE(s->sequence) != start);
387 * read_seqcount_retry() - end a seqcount_t read critical section
388 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
389 * @start: count, from read_seqcount_begin()
391 * read_seqcount_retry closes the read critical section of given
392 * seqcount_t. If the critical section was invalid, it must be ignored
393 * (and typically retried).
395 * Return: true if a read section retry is required, else false
397 #define read_seqcount_retry(s, start) \
398 do_read_seqcount_retry(seqprop_const_ptr(s), start)
400 static inline int do_read_seqcount_retry(const seqcount_t *s, unsigned start)
403 return do___read_seqcount_retry(s, start);
407 * raw_write_seqcount_begin() - start a seqcount_t write section w/o lockdep
408 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
410 * Context: check write_seqcount_begin()
412 #define raw_write_seqcount_begin(s) \
414 if (seqprop_preemptible(s)) \
417 do_raw_write_seqcount_begin(seqprop_ptr(s)); \
420 static inline void do_raw_write_seqcount_begin(seqcount_t *s)
422 kcsan_nestable_atomic_begin();
428 * raw_write_seqcount_end() - end a seqcount_t write section w/o lockdep
429 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
431 * Context: check write_seqcount_end()
433 #define raw_write_seqcount_end(s) \
435 do_raw_write_seqcount_end(seqprop_ptr(s)); \
437 if (seqprop_preemptible(s)) \
441 static inline void do_raw_write_seqcount_end(seqcount_t *s)
445 kcsan_nestable_atomic_end();
449 * write_seqcount_begin_nested() - start a seqcount_t write section with
450 * custom lockdep nesting level
451 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
452 * @subclass: lockdep nesting level
454 * See Documentation/locking/lockdep-design.rst
455 * Context: check write_seqcount_begin()
457 #define write_seqcount_begin_nested(s, subclass) \
461 if (seqprop_preemptible(s)) \
464 do_write_seqcount_begin_nested(seqprop_ptr(s), subclass); \
467 static inline void do_write_seqcount_begin_nested(seqcount_t *s, int subclass)
469 seqcount_acquire(&s->dep_map, subclass, 0, _RET_IP_);
470 do_raw_write_seqcount_begin(s);
474 * write_seqcount_begin() - start a seqcount_t write side critical section
475 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
477 * Context: sequence counter write side sections must be serialized and
478 * non-preemptible. Preemption will be automatically disabled if and
479 * only if the seqcount write serialization lock is associated, and
480 * preemptible. If readers can be invoked from hardirq or softirq
481 * context, interrupts or bottom halves must be respectively disabled.
483 #define write_seqcount_begin(s) \
487 if (seqprop_preemptible(s)) \
490 do_write_seqcount_begin(seqprop_ptr(s)); \
493 static inline void do_write_seqcount_begin(seqcount_t *s)
495 do_write_seqcount_begin_nested(s, 0);
499 * write_seqcount_end() - end a seqcount_t write side critical section
500 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
502 * Context: Preemption will be automatically re-enabled if and only if
503 * the seqcount write serialization lock is associated, and preemptible.
505 #define write_seqcount_end(s) \
507 do_write_seqcount_end(seqprop_ptr(s)); \
509 if (seqprop_preemptible(s)) \
513 static inline void do_write_seqcount_end(seqcount_t *s)
515 seqcount_release(&s->dep_map, _RET_IP_);
516 do_raw_write_seqcount_end(s);
520 * raw_write_seqcount_barrier() - do a seqcount_t write barrier
521 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
523 * This can be used to provide an ordering guarantee instead of the usual
524 * consistency guarantee. It is one wmb cheaper, because it can collapse
525 * the two back-to-back wmb()s.
527 * Note that writes surrounding the barrier should be declared atomic (e.g.
528 * via WRITE_ONCE): a) to ensure the writes become visible to other threads
529 * atomically, avoiding compiler optimizations; b) to document which writes are
530 * meant to propagate to the reader critical section. This is necessary because
531 * neither writes before nor after the barrier are enclosed in a seq-writer
532 * critical section that would ensure readers are aware of ongoing writes::
535 * bool X = true, Y = false;
542 * int s = read_seqcount_begin(&seq);
546 * } while (read_seqcount_retry(&seq, s));
553 * WRITE_ONCE(Y, true);
555 * raw_write_seqcount_barrier(seq);
557 * WRITE_ONCE(X, false);
560 #define raw_write_seqcount_barrier(s) \
561 do_raw_write_seqcount_barrier(seqprop_ptr(s))
563 static inline void do_raw_write_seqcount_barrier(seqcount_t *s)
565 kcsan_nestable_atomic_begin();
569 kcsan_nestable_atomic_end();
573 * write_seqcount_invalidate() - invalidate in-progress seqcount_t read
575 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
577 * After write_seqcount_invalidate, no seqcount_t read side operations
578 * will complete successfully and see data older than this.
580 #define write_seqcount_invalidate(s) \
581 do_write_seqcount_invalidate(seqprop_ptr(s))
583 static inline void do_write_seqcount_invalidate(seqcount_t *s)
586 kcsan_nestable_atomic_begin();
588 kcsan_nestable_atomic_end();
592 * Latch sequence counters (seqcount_latch_t)
594 * A sequence counter variant where the counter even/odd value is used to
595 * switch between two copies of protected data. This allows the read path,
596 * typically NMIs, to safely interrupt the write side critical section.
598 * As the write sections are fully preemptible, no special handling for
599 * PREEMPT_RT is needed.
606 * SEQCNT_LATCH_ZERO() - static initializer for seqcount_latch_t
607 * @seq_name: Name of the seqcount_latch_t instance
609 #define SEQCNT_LATCH_ZERO(seq_name) { \
610 .seqcount = SEQCNT_ZERO(seq_name.seqcount), \
614 * seqcount_latch_init() - runtime initializer for seqcount_latch_t
615 * @s: Pointer to the seqcount_latch_t instance
617 #define seqcount_latch_init(s) seqcount_init(&(s)->seqcount)
620 * raw_read_seqcount_latch() - pick even/odd latch data copy
621 * @s: Pointer to seqcount_latch_t
623 * See raw_write_seqcount_latch() for details and a full reader/writer
626 * Return: sequence counter raw value. Use the lowest bit as an index for
627 * picking which data copy to read. The full counter must then be checked
628 * with raw_read_seqcount_latch_retry().
630 static __always_inline unsigned raw_read_seqcount_latch(const seqcount_latch_t *s)
633 * Pairs with the first smp_wmb() in raw_write_seqcount_latch().
634 * Due to the dependent load, a full smp_rmb() is not needed.
636 return READ_ONCE(s->seqcount.sequence);
640 * raw_read_seqcount_latch_retry() - end a seqcount_latch_t read section
641 * @s: Pointer to seqcount_latch_t
642 * @start: count, from raw_read_seqcount_latch()
644 * Return: true if a read section retry is required, else false
646 static __always_inline int
647 raw_read_seqcount_latch_retry(const seqcount_latch_t *s, unsigned start)
650 return unlikely(READ_ONCE(s->seqcount.sequence) != start);
654 * raw_write_seqcount_latch() - redirect latch readers to even/odd copy
655 * @s: Pointer to seqcount_latch_t
657 * The latch technique is a multiversion concurrency control method that allows
658 * queries during non-atomic modifications. If you can guarantee queries never
659 * interrupt the modification -- e.g. the concurrency is strictly between CPUs
660 * -- you most likely do not need this.
662 * Where the traditional RCU/lockless data structures rely on atomic
663 * modifications to ensure queries observe either the old or the new state the
664 * latch allows the same for non-atomic updates. The trade-off is doubling the
665 * cost of storage; we have to maintain two copies of the entire data
668 * Very simply put: we first modify one copy and then the other. This ensures
669 * there is always one copy in a stable state, ready to give us an answer.
671 * The basic form is a data structure like::
673 * struct latch_struct {
674 * seqcount_latch_t seq;
675 * struct data_struct data[2];
678 * Where a modification, which is assumed to be externally serialized, does the
681 * void latch_modify(struct latch_struct *latch, ...)
683 * smp_wmb(); // Ensure that the last data[1] update is visible
684 * latch->seq.sequence++;
685 * smp_wmb(); // Ensure that the seqcount update is visible
687 * modify(latch->data[0], ...);
689 * smp_wmb(); // Ensure that the data[0] update is visible
690 * latch->seq.sequence++;
691 * smp_wmb(); // Ensure that the seqcount update is visible
693 * modify(latch->data[1], ...);
696 * The query will have a form like::
698 * struct entry *latch_query(struct latch_struct *latch, ...)
700 * struct entry *entry;
704 * seq = raw_read_seqcount_latch(&latch->seq);
707 * entry = data_query(latch->data[idx], ...);
709 * // This includes needed smp_rmb()
710 * } while (raw_read_seqcount_latch_retry(&latch->seq, seq));
715 * So during the modification, queries are first redirected to data[1]. Then we
716 * modify data[0]. When that is complete, we redirect queries back to data[0]
717 * and we can modify data[1].
721 * The non-requirement for atomic modifications does _NOT_ include
722 * the publishing of new entries in the case where data is a dynamic
725 * An iteration might start in data[0] and get suspended long enough
726 * to miss an entire modification sequence, once it resumes it might
727 * observe the new entry.
731 * When data is a dynamic data structure; one should use regular RCU
732 * patterns to manage the lifetimes of the objects within.
734 static inline void raw_write_seqcount_latch(seqcount_latch_t *s)
736 smp_wmb(); /* prior stores before incrementing "sequence" */
737 s->seqcount.sequence++;
738 smp_wmb(); /* increment "sequence" before following stores */
741 #define __SEQLOCK_UNLOCKED(lockname) \
743 .seqcount = SEQCNT_SPINLOCK_ZERO(lockname, &(lockname).lock), \
744 .lock = __SPIN_LOCK_UNLOCKED(lockname) \
748 * seqlock_init() - dynamic initializer for seqlock_t
749 * @sl: Pointer to the seqlock_t instance
751 #define seqlock_init(sl) \
753 spin_lock_init(&(sl)->lock); \
754 seqcount_spinlock_init(&(sl)->seqcount, &(sl)->lock); \
758 * DEFINE_SEQLOCK(sl) - Define a statically allocated seqlock_t
759 * @sl: Name of the seqlock_t instance
761 #define DEFINE_SEQLOCK(sl) \
762 seqlock_t sl = __SEQLOCK_UNLOCKED(sl)
765 * read_seqbegin() - start a seqlock_t read side critical section
766 * @sl: Pointer to seqlock_t
768 * Return: count, to be passed to read_seqretry()
770 static inline unsigned read_seqbegin(const seqlock_t *sl)
772 unsigned ret = read_seqcount_begin(&sl->seqcount);
774 kcsan_atomic_next(0); /* non-raw usage, assume closing read_seqretry() */
775 kcsan_flat_atomic_begin();
780 * read_seqretry() - end a seqlock_t read side section
781 * @sl: Pointer to seqlock_t
782 * @start: count, from read_seqbegin()
784 * read_seqretry closes the read side critical section of given seqlock_t.
785 * If the critical section was invalid, it must be ignored (and typically
788 * Return: true if a read section retry is required, else false
790 static inline unsigned read_seqretry(const seqlock_t *sl, unsigned start)
793 * Assume not nested: read_seqretry() may be called multiple times when
794 * completing read critical section.
796 kcsan_flat_atomic_end();
798 return read_seqcount_retry(&sl->seqcount, start);
802 * For all seqlock_t write side functions, use the internal
803 * do_write_seqcount_begin() instead of generic write_seqcount_begin().
804 * This way, no redundant lockdep_assert_held() checks are added.
808 * write_seqlock() - start a seqlock_t write side critical section
809 * @sl: Pointer to seqlock_t
811 * write_seqlock opens a write side critical section for the given
812 * seqlock_t. It also implicitly acquires the spinlock_t embedded inside
813 * that sequential lock. All seqlock_t write side sections are thus
814 * automatically serialized and non-preemptible.
816 * Context: if the seqlock_t read section, or other write side critical
817 * sections, can be invoked from hardirq or softirq contexts, use the
818 * _irqsave or _bh variants of this function instead.
820 static inline void write_seqlock(seqlock_t *sl)
822 spin_lock(&sl->lock);
823 do_write_seqcount_begin(&sl->seqcount.seqcount);
827 * write_sequnlock() - end a seqlock_t write side critical section
828 * @sl: Pointer to seqlock_t
830 * write_sequnlock closes the (serialized and non-preemptible) write side
831 * critical section of given seqlock_t.
833 static inline void write_sequnlock(seqlock_t *sl)
835 do_write_seqcount_end(&sl->seqcount.seqcount);
836 spin_unlock(&sl->lock);
840 * write_seqlock_bh() - start a softirqs-disabled seqlock_t write section
841 * @sl: Pointer to seqlock_t
843 * _bh variant of write_seqlock(). Use only if the read side section, or
844 * other write side sections, can be invoked from softirq contexts.
846 static inline void write_seqlock_bh(seqlock_t *sl)
848 spin_lock_bh(&sl->lock);
849 do_write_seqcount_begin(&sl->seqcount.seqcount);
853 * write_sequnlock_bh() - end a softirqs-disabled seqlock_t write section
854 * @sl: Pointer to seqlock_t
856 * write_sequnlock_bh closes the serialized, non-preemptible, and
857 * softirqs-disabled, seqlock_t write side critical section opened with
858 * write_seqlock_bh().
860 static inline void write_sequnlock_bh(seqlock_t *sl)
862 do_write_seqcount_end(&sl->seqcount.seqcount);
863 spin_unlock_bh(&sl->lock);
867 * write_seqlock_irq() - start a non-interruptible seqlock_t write section
868 * @sl: Pointer to seqlock_t
870 * _irq variant of write_seqlock(). Use only if the read side section, or
871 * other write sections, can be invoked from hardirq contexts.
873 static inline void write_seqlock_irq(seqlock_t *sl)
875 spin_lock_irq(&sl->lock);
876 do_write_seqcount_begin(&sl->seqcount.seqcount);
880 * write_sequnlock_irq() - end a non-interruptible seqlock_t write section
881 * @sl: Pointer to seqlock_t
883 * write_sequnlock_irq closes the serialized and non-interruptible
884 * seqlock_t write side section opened with write_seqlock_irq().
886 static inline void write_sequnlock_irq(seqlock_t *sl)
888 do_write_seqcount_end(&sl->seqcount.seqcount);
889 spin_unlock_irq(&sl->lock);
892 static inline unsigned long __write_seqlock_irqsave(seqlock_t *sl)
896 spin_lock_irqsave(&sl->lock, flags);
897 do_write_seqcount_begin(&sl->seqcount.seqcount);
902 * write_seqlock_irqsave() - start a non-interruptible seqlock_t write
904 * @lock: Pointer to seqlock_t
905 * @flags: Stack-allocated storage for saving caller's local interrupt
906 * state, to be passed to write_sequnlock_irqrestore().
908 * _irqsave variant of write_seqlock(). Use it only if the read side
909 * section, or other write sections, can be invoked from hardirq context.
911 #define write_seqlock_irqsave(lock, flags) \
912 do { flags = __write_seqlock_irqsave(lock); } while (0)
915 * write_sequnlock_irqrestore() - end non-interruptible seqlock_t write
917 * @sl: Pointer to seqlock_t
918 * @flags: Caller's saved interrupt state, from write_seqlock_irqsave()
920 * write_sequnlock_irqrestore closes the serialized and non-interruptible
921 * seqlock_t write section previously opened with write_seqlock_irqsave().
924 write_sequnlock_irqrestore(seqlock_t *sl, unsigned long flags)
926 do_write_seqcount_end(&sl->seqcount.seqcount);
927 spin_unlock_irqrestore(&sl->lock, flags);
931 * read_seqlock_excl() - begin a seqlock_t locking reader section
932 * @sl: Pointer to seqlock_t
934 * read_seqlock_excl opens a seqlock_t locking reader critical section. A
935 * locking reader exclusively locks out *both* other writers *and* other
936 * locking readers, but it does not update the embedded sequence number.
938 * Locking readers act like a normal spin_lock()/spin_unlock().
940 * Context: if the seqlock_t write section, *or other read sections*, can
941 * be invoked from hardirq or softirq contexts, use the _irqsave or _bh
942 * variant of this function instead.
944 * The opened read section must be closed with read_sequnlock_excl().
946 static inline void read_seqlock_excl(seqlock_t *sl)
948 spin_lock(&sl->lock);
952 * read_sequnlock_excl() - end a seqlock_t locking reader critical section
953 * @sl: Pointer to seqlock_t
955 static inline void read_sequnlock_excl(seqlock_t *sl)
957 spin_unlock(&sl->lock);
961 * read_seqlock_excl_bh() - start a seqlock_t locking reader section with
963 * @sl: Pointer to seqlock_t
965 * _bh variant of read_seqlock_excl(). Use this variant only if the
966 * seqlock_t write side section, *or other read sections*, can be invoked
967 * from softirq contexts.
969 static inline void read_seqlock_excl_bh(seqlock_t *sl)
971 spin_lock_bh(&sl->lock);
975 * read_sequnlock_excl_bh() - stop a seqlock_t softirq-disabled locking
977 * @sl: Pointer to seqlock_t
979 static inline void read_sequnlock_excl_bh(seqlock_t *sl)
981 spin_unlock_bh(&sl->lock);
985 * read_seqlock_excl_irq() - start a non-interruptible seqlock_t locking
987 * @sl: Pointer to seqlock_t
989 * _irq variant of read_seqlock_excl(). Use this only if the seqlock_t
990 * write side section, *or other read sections*, can be invoked from a
993 static inline void read_seqlock_excl_irq(seqlock_t *sl)
995 spin_lock_irq(&sl->lock);
999 * read_sequnlock_excl_irq() - end an interrupts-disabled seqlock_t
1000 * locking reader section
1001 * @sl: Pointer to seqlock_t
1003 static inline void read_sequnlock_excl_irq(seqlock_t *sl)
1005 spin_unlock_irq(&sl->lock);
1008 static inline unsigned long __read_seqlock_excl_irqsave(seqlock_t *sl)
1010 unsigned long flags;
1012 spin_lock_irqsave(&sl->lock, flags);
1017 * read_seqlock_excl_irqsave() - start a non-interruptible seqlock_t
1018 * locking reader section
1019 * @lock: Pointer to seqlock_t
1020 * @flags: Stack-allocated storage for saving caller's local interrupt
1021 * state, to be passed to read_sequnlock_excl_irqrestore().
1023 * _irqsave variant of read_seqlock_excl(). Use this only if the seqlock_t
1024 * write side section, *or other read sections*, can be invoked from a
1027 #define read_seqlock_excl_irqsave(lock, flags) \
1028 do { flags = __read_seqlock_excl_irqsave(lock); } while (0)
1031 * read_sequnlock_excl_irqrestore() - end non-interruptible seqlock_t
1032 * locking reader section
1033 * @sl: Pointer to seqlock_t
1034 * @flags: Caller saved interrupt state, from read_seqlock_excl_irqsave()
1037 read_sequnlock_excl_irqrestore(seqlock_t *sl, unsigned long flags)
1039 spin_unlock_irqrestore(&sl->lock, flags);
1043 * read_seqbegin_or_lock() - begin a seqlock_t lockless or locking reader
1044 * @lock: Pointer to seqlock_t
1045 * @seq : Marker and return parameter. If the passed value is even, the
1046 * reader will become a *lockless* seqlock_t reader as in read_seqbegin().
1047 * If the passed value is odd, the reader will become a *locking* reader
1048 * as in read_seqlock_excl(). In the first call to this function, the
1049 * caller *must* initialize and pass an even value to @seq; this way, a
1050 * lockless read can be optimistically tried first.
1052 * read_seqbegin_or_lock is an API designed to optimistically try a normal
1053 * lockless seqlock_t read section first. If an odd counter is found, the
1054 * lockless read trial has failed, and the next read iteration transforms
1055 * itself into a full seqlock_t locking reader.
1057 * This is typically used to avoid seqlock_t lockless readers starvation
1058 * (too much retry loops) in the case of a sharp spike in write side
1061 * Context: if the seqlock_t write section, *or other read sections*, can
1062 * be invoked from hardirq or softirq contexts, use the _irqsave or _bh
1063 * variant of this function instead.
1065 * Check Documentation/locking/seqlock.rst for template example code.
1067 * Return: the encountered sequence counter value, through the @seq
1068 * parameter, which is overloaded as a return parameter. This returned
1069 * value must be checked with need_seqretry(). If the read section need to
1070 * be retried, this returned value must also be passed as the @seq
1071 * parameter of the next read_seqbegin_or_lock() iteration.
1073 static inline void read_seqbegin_or_lock(seqlock_t *lock, int *seq)
1075 if (!(*seq & 1)) /* Even */
1076 *seq = read_seqbegin(lock);
1078 read_seqlock_excl(lock);
1082 * need_seqretry() - validate seqlock_t "locking or lockless" read section
1083 * @lock: Pointer to seqlock_t
1084 * @seq: sequence count, from read_seqbegin_or_lock()
1086 * Return: true if a read section retry is required, false otherwise
1088 static inline int need_seqretry(seqlock_t *lock, int seq)
1090 return !(seq & 1) && read_seqretry(lock, seq);
1094 * done_seqretry() - end seqlock_t "locking or lockless" reader section
1095 * @lock: Pointer to seqlock_t
1096 * @seq: count, from read_seqbegin_or_lock()
1098 * done_seqretry finishes the seqlock_t read side critical section started
1099 * with read_seqbegin_or_lock() and validated by need_seqretry().
1101 static inline void done_seqretry(seqlock_t *lock, int seq)
1104 read_sequnlock_excl(lock);
1108 * read_seqbegin_or_lock_irqsave() - begin a seqlock_t lockless reader, or
1109 * a non-interruptible locking reader
1110 * @lock: Pointer to seqlock_t
1111 * @seq: Marker and return parameter. Check read_seqbegin_or_lock().
1113 * This is the _irqsave variant of read_seqbegin_or_lock(). Use it only if
1114 * the seqlock_t write section, *or other read sections*, can be invoked
1115 * from hardirq context.
1117 * Note: Interrupts will be disabled only for "locking reader" mode.
1121 * 1. The saved local interrupts state in case of a locking reader, to
1122 * be passed to done_seqretry_irqrestore().
1124 * 2. The encountered sequence counter value, returned through @seq
1125 * overloaded as a return parameter. Check read_seqbegin_or_lock().
1127 static inline unsigned long
1128 read_seqbegin_or_lock_irqsave(seqlock_t *lock, int *seq)
1130 unsigned long flags = 0;
1132 if (!(*seq & 1)) /* Even */
1133 *seq = read_seqbegin(lock);
1135 read_seqlock_excl_irqsave(lock, flags);
1141 * done_seqretry_irqrestore() - end a seqlock_t lockless reader, or a
1142 * non-interruptible locking reader section
1143 * @lock: Pointer to seqlock_t
1144 * @seq: Count, from read_seqbegin_or_lock_irqsave()
1145 * @flags: Caller's saved local interrupt state in case of a locking
1146 * reader, also from read_seqbegin_or_lock_irqsave()
1148 * This is the _irqrestore variant of done_seqretry(). The read section
1149 * must've been opened with read_seqbegin_or_lock_irqsave(), and validated
1150 * by need_seqretry().
1153 done_seqretry_irqrestore(seqlock_t *lock, int seq, unsigned long flags)
1156 read_sequnlock_excl_irqrestore(lock, flags);
1158 #endif /* __LINUX_SEQLOCK_H */