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/spinlock.h>
23 #include <asm/processor.h>
26 * The seqlock seqcount_t interface does not prescribe a precise sequence of
27 * read begin/retry/end. For readers, typically there is a call to
28 * read_seqcount_begin() and read_seqcount_retry(), however, there are more
29 * esoteric cases which do not follow this pattern.
31 * As a consequence, we take the following best-effort approach for raw usage
32 * via seqcount_t under KCSAN: upon beginning a seq-reader critical section,
33 * pessimistically mark the next KCSAN_SEQLOCK_REGION_MAX memory accesses as
34 * atomics; if there is a matching read_seqcount_retry() call, no following
35 * memory operations are considered atomic. Usage of the seqlock_t interface
38 #define KCSAN_SEQLOCK_REGION_MAX 1000
41 * Sequence counters (seqcount_t)
43 * This is the raw counting mechanism, without any writer protection.
45 * Write side critical sections must be serialized and non-preemptible.
47 * If readers can be invoked from hardirq or softirq contexts,
48 * interrupts or bottom halves must also be respectively disabled before
49 * entering the write section.
51 * This mechanism can't be used if the protected data contains pointers,
52 * as the writer can invalidate a pointer that a reader is following.
54 * If the write serialization mechanism is one of the common kernel
55 * locking primitives, use a sequence counter with associated lock
56 * (seqcount_LOCKNAME_t) instead.
58 * If it's desired to automatically handle the sequence counter writer
59 * serialization and non-preemptibility requirements, use a sequential
60 * lock (seqlock_t) instead.
62 * See Documentation/locking/seqlock.rst
64 typedef struct seqcount {
66 #ifdef CONFIG_DEBUG_LOCK_ALLOC
67 struct lockdep_map dep_map;
71 static inline void __seqcount_init(seqcount_t *s, const char *name,
72 struct lock_class_key *key)
75 * Make sure we are not reinitializing a held lock:
77 lockdep_init_map(&s->dep_map, name, key, 0);
81 #ifdef CONFIG_DEBUG_LOCK_ALLOC
83 # define SEQCOUNT_DEP_MAP_INIT(lockname) \
84 .dep_map = { .name = #lockname }
87 * seqcount_init() - runtime initializer for seqcount_t
88 * @s: Pointer to the seqcount_t instance
90 # define seqcount_init(s) \
92 static struct lock_class_key __key; \
93 __seqcount_init((s), #s, &__key); \
96 static inline void seqcount_lockdep_reader_access(const seqcount_t *s)
98 seqcount_t *l = (seqcount_t *)s;
101 local_irq_save(flags);
102 seqcount_acquire_read(&l->dep_map, 0, 0, _RET_IP_);
103 seqcount_release(&l->dep_map, _RET_IP_);
104 local_irq_restore(flags);
108 # define SEQCOUNT_DEP_MAP_INIT(lockname)
109 # define seqcount_init(s) __seqcount_init(s, NULL, NULL)
110 # define seqcount_lockdep_reader_access(x)
114 * SEQCNT_ZERO() - static initializer for seqcount_t
115 * @name: Name of the seqcount_t instance
117 #define SEQCNT_ZERO(name) { .sequence = 0, SEQCOUNT_DEP_MAP_INIT(name) }
120 * Sequence counters with associated locks (seqcount_LOCKNAME_t)
122 * A sequence counter which associates the lock used for writer
123 * serialization at initialization time. This enables lockdep to validate
124 * that the write side critical section is properly serialized.
126 * For associated locks which do not implicitly disable preemption,
127 * preemption protection is enforced in the write side function.
129 * Lockdep is never used in any for the raw write variants.
131 * See Documentation/locking/seqlock.rst
134 #ifdef CONFIG_LOCKDEP
135 #define __SEQ_LOCK(expr) expr
137 #define __SEQ_LOCK(expr)
141 * typedef seqcount_LOCKNAME_t - sequence counter with LOCKNAME associated
142 * @seqcount: The real sequence counter
143 * @lock: Pointer to the associated lock
145 * A plain sequence counter with external writer synchronization by
146 * LOCKNAME @lock. The lock is associated to the sequence counter in the
147 * static initializer or init function. This enables lockdep to validate
148 * that the write side critical section is properly serialized.
150 * LOCKNAME: raw_spinlock, spinlock, rwlock, mutex, or ww_mutex.
154 * seqcount_LOCKNAME_init() - runtime initializer for seqcount_LOCKNAME_t
155 * @s: Pointer to the seqcount_LOCKNAME_t instance
156 * @lock: Pointer to the associated lock
160 * SEQCOUNT_LOCKNAME() - Instantiate seqcount_LOCKNAME_t and helpers
161 * seqprop_LOCKNAME_*() - Property accessors for seqcount_LOCKNAME_t
163 * @lockname: "LOCKNAME" part of seqcount_LOCKNAME_t
164 * @locktype: LOCKNAME canonical C data type
165 * @preemptible: preemptibility of above lockname
166 * @lockmember: argument for lockdep_assert_held()
168 #define SEQCOUNT_LOCKNAME(lockname, locktype, preemptible, lockmember) \
169 typedef struct seqcount_##lockname { \
170 seqcount_t seqcount; \
171 __SEQ_LOCK(locktype *lock); \
172 } seqcount_##lockname##_t; \
174 static __always_inline void \
175 seqcount_##lockname##_init(seqcount_##lockname##_t *s, locktype *lock) \
177 seqcount_init(&s->seqcount); \
178 __SEQ_LOCK(s->lock = lock); \
181 static __always_inline seqcount_t * \
182 __seqprop_##lockname##_ptr(seqcount_##lockname##_t *s) \
184 return &s->seqcount; \
187 static __always_inline unsigned \
188 __seqprop_##lockname##_sequence(const seqcount_##lockname##_t *s) \
190 return READ_ONCE(s->seqcount.sequence); \
193 static __always_inline bool \
194 __seqprop_##lockname##_preemptible(const seqcount_##lockname##_t *s) \
196 return preemptible; \
199 static __always_inline void \
200 __seqprop_##lockname##_assert(const seqcount_##lockname##_t *s) \
202 __SEQ_LOCK(lockdep_assert_held(lockmember)); \
206 * __seqprop() for seqcount_t
209 static inline seqcount_t *__seqprop_ptr(seqcount_t *s)
214 static inline unsigned __seqprop_sequence(const seqcount_t *s)
216 return READ_ONCE(s->sequence);
219 static inline bool __seqprop_preemptible(const seqcount_t *s)
224 static inline void __seqprop_assert(const seqcount_t *s)
226 lockdep_assert_preemption_disabled();
229 SEQCOUNT_LOCKNAME(raw_spinlock, raw_spinlock_t, false, s->lock)
230 SEQCOUNT_LOCKNAME(spinlock, spinlock_t, false, s->lock)
231 SEQCOUNT_LOCKNAME(rwlock, rwlock_t, false, s->lock)
232 SEQCOUNT_LOCKNAME(mutex, struct mutex, true, s->lock)
233 SEQCOUNT_LOCKNAME(ww_mutex, struct ww_mutex, true, &s->lock->base)
236 * SEQCNT_LOCKNAME_ZERO - static initializer for seqcount_LOCKNAME_t
237 * @name: Name of the seqcount_LOCKNAME_t instance
238 * @lock: Pointer to the associated LOCKNAME
241 #define SEQCOUNT_LOCKNAME_ZERO(seq_name, assoc_lock) { \
242 .seqcount = SEQCNT_ZERO(seq_name.seqcount), \
243 __SEQ_LOCK(.lock = (assoc_lock)) \
246 #define SEQCNT_SPINLOCK_ZERO(name, lock) SEQCOUNT_LOCKNAME_ZERO(name, lock)
247 #define SEQCNT_RAW_SPINLOCK_ZERO(name, lock) SEQCOUNT_LOCKNAME_ZERO(name, lock)
248 #define SEQCNT_RWLOCK_ZERO(name, lock) SEQCOUNT_LOCKNAME_ZERO(name, lock)
249 #define SEQCNT_MUTEX_ZERO(name, lock) SEQCOUNT_LOCKNAME_ZERO(name, lock)
250 #define SEQCNT_WW_MUTEX_ZERO(name, lock) SEQCOUNT_LOCKNAME_ZERO(name, lock)
252 #define __seqprop_case(s, lockname, prop) \
253 seqcount_##lockname##_t: __seqprop_##lockname##_##prop((void *)(s))
255 #define __seqprop(s, prop) _Generic(*(s), \
256 seqcount_t: __seqprop_##prop((void *)(s)), \
257 __seqprop_case((s), raw_spinlock, prop), \
258 __seqprop_case((s), spinlock, prop), \
259 __seqprop_case((s), rwlock, prop), \
260 __seqprop_case((s), mutex, prop), \
261 __seqprop_case((s), ww_mutex, prop))
263 #define __seqcount_ptr(s) __seqprop(s, ptr)
264 #define __seqcount_sequence(s) __seqprop(s, sequence)
265 #define __seqcount_lock_preemptible(s) __seqprop(s, preemptible)
266 #define __seqcount_assert_lock_held(s) __seqprop(s, assert)
269 * __read_seqcount_begin() - begin a seqcount_t read section w/o barrier
270 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
272 * __read_seqcount_begin is like read_seqcount_begin, but has no smp_rmb()
273 * barrier. Callers should ensure that smp_rmb() or equivalent ordering is
274 * provided before actually loading any of the variables that are to be
275 * protected in this critical section.
277 * Use carefully, only in critical code, and comment how the barrier is
280 * Return: count to be passed to read_seqcount_retry()
282 #define __read_seqcount_begin(s) \
286 while ((seq = __seqcount_sequence(s)) & 1) \
289 kcsan_atomic_next(KCSAN_SEQLOCK_REGION_MAX); \
294 * raw_read_seqcount_begin() - begin a seqcount_t read section w/o lockdep
295 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
297 * Return: count to be passed to read_seqcount_retry()
299 #define raw_read_seqcount_begin(s) \
301 unsigned seq = __read_seqcount_begin(s); \
308 * read_seqcount_begin() - begin a seqcount_t read critical section
309 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
311 * Return: count to be passed to read_seqcount_retry()
313 #define read_seqcount_begin(s) \
315 seqcount_lockdep_reader_access(__seqcount_ptr(s)); \
316 raw_read_seqcount_begin(s); \
320 * raw_read_seqcount() - read the raw seqcount_t counter value
321 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
323 * raw_read_seqcount opens a read critical section of the given
324 * seqcount_t, without any lockdep checking, and without checking or
325 * masking the sequence counter LSB. Calling code is responsible for
328 * Return: count to be passed to read_seqcount_retry()
330 #define raw_read_seqcount(s) \
332 unsigned seq = __seqcount_sequence(s); \
335 kcsan_atomic_next(KCSAN_SEQLOCK_REGION_MAX); \
340 * raw_seqcount_begin() - begin a seqcount_t read critical section w/o
341 * lockdep and w/o counter stabilization
342 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
344 * raw_seqcount_begin opens a read critical section of the given
345 * seqcount_t. Unlike read_seqcount_begin(), this function will not wait
346 * for the count to stabilize. If a writer is active when it begins, it
347 * will fail the read_seqcount_retry() at the end of the read critical
348 * section instead of stabilizing at the beginning of it.
350 * Use this only in special kernel hot paths where the read section is
351 * small and has a high probability of success through other external
352 * means. It will save a single branching instruction.
354 * Return: count to be passed to read_seqcount_retry()
356 #define raw_seqcount_begin(s) \
359 * If the counter is odd, let read_seqcount_retry() fail \
360 * by decrementing the counter. \
362 raw_read_seqcount(s) & ~1; \
366 * __read_seqcount_retry() - end a seqcount_t read section w/o barrier
367 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
368 * @start: count, from read_seqcount_begin()
370 * __read_seqcount_retry is like read_seqcount_retry, but has no smp_rmb()
371 * barrier. Callers should ensure that smp_rmb() or equivalent ordering is
372 * provided before actually loading any of the variables that are to be
373 * protected in this critical section.
375 * Use carefully, only in critical code, and comment how the barrier is
378 * Return: true if a read section retry is required, else false
380 #define __read_seqcount_retry(s, start) \
381 __read_seqcount_t_retry(__seqcount_ptr(s), start)
383 static inline int __read_seqcount_t_retry(const seqcount_t *s, unsigned start)
385 kcsan_atomic_next(0);
386 return unlikely(READ_ONCE(s->sequence) != start);
390 * read_seqcount_retry() - end a seqcount_t read critical section
391 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
392 * @start: count, from read_seqcount_begin()
394 * read_seqcount_retry closes the read critical section of given
395 * seqcount_t. If the critical section was invalid, it must be ignored
396 * (and typically retried).
398 * Return: true if a read section retry is required, else false
400 #define read_seqcount_retry(s, start) \
401 read_seqcount_t_retry(__seqcount_ptr(s), start)
403 static inline int read_seqcount_t_retry(const seqcount_t *s, unsigned start)
406 return __read_seqcount_t_retry(s, start);
410 * raw_write_seqcount_begin() - start a seqcount_t write section w/o lockdep
411 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
413 #define raw_write_seqcount_begin(s) \
415 if (__seqcount_lock_preemptible(s)) \
418 raw_write_seqcount_t_begin(__seqcount_ptr(s)); \
421 static inline void raw_write_seqcount_t_begin(seqcount_t *s)
423 kcsan_nestable_atomic_begin();
429 * raw_write_seqcount_end() - end a seqcount_t write section w/o lockdep
430 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
432 #define raw_write_seqcount_end(s) \
434 raw_write_seqcount_t_end(__seqcount_ptr(s)); \
436 if (__seqcount_lock_preemptible(s)) \
440 static inline void raw_write_seqcount_t_end(seqcount_t *s)
444 kcsan_nestable_atomic_end();
448 * write_seqcount_begin_nested() - start a seqcount_t write section with
449 * custom lockdep nesting level
450 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
451 * @subclass: lockdep nesting level
453 * See Documentation/locking/lockdep-design.rst
455 #define write_seqcount_begin_nested(s, subclass) \
457 __seqcount_assert_lock_held(s); \
459 if (__seqcount_lock_preemptible(s)) \
462 write_seqcount_t_begin_nested(__seqcount_ptr(s), subclass); \
465 static inline void write_seqcount_t_begin_nested(seqcount_t *s, int subclass)
467 raw_write_seqcount_t_begin(s);
468 seqcount_acquire(&s->dep_map, subclass, 0, _RET_IP_);
472 * write_seqcount_begin() - start a seqcount_t write side critical section
473 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
475 * write_seqcount_begin opens a write side critical section of the given
478 * Context: seqcount_t write side critical sections must be serialized and
479 * non-preemptible. If readers can be invoked from hardirq or softirq
480 * context, interrupts or bottom halves must be respectively disabled.
482 #define write_seqcount_begin(s) \
484 __seqcount_assert_lock_held(s); \
486 if (__seqcount_lock_preemptible(s)) \
489 write_seqcount_t_begin(__seqcount_ptr(s)); \
492 static inline void write_seqcount_t_begin(seqcount_t *s)
494 write_seqcount_t_begin_nested(s, 0);
498 * write_seqcount_end() - end a seqcount_t write side critical section
499 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
501 * The write section must've been opened with write_seqcount_begin().
503 #define write_seqcount_end(s) \
505 write_seqcount_t_end(__seqcount_ptr(s)); \
507 if (__seqcount_lock_preemptible(s)) \
511 static inline void write_seqcount_t_end(seqcount_t *s)
513 seqcount_release(&s->dep_map, _RET_IP_);
514 raw_write_seqcount_t_end(s);
518 * raw_write_seqcount_barrier() - do a seqcount_t write barrier
519 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
521 * This can be used to provide an ordering guarantee instead of the usual
522 * consistency guarantee. It is one wmb cheaper, because it can collapse
523 * the two back-to-back wmb()s.
525 * Note that writes surrounding the barrier should be declared atomic (e.g.
526 * via WRITE_ONCE): a) to ensure the writes become visible to other threads
527 * atomically, avoiding compiler optimizations; b) to document which writes are
528 * meant to propagate to the reader critical section. This is necessary because
529 * neither writes before and after the barrier are enclosed in a seq-writer
530 * critical section that would ensure readers are aware of ongoing writes::
533 * bool X = true, Y = false;
540 * int s = read_seqcount_begin(&seq);
544 * } while (read_seqcount_retry(&seq, s));
551 * WRITE_ONCE(Y, true);
553 * raw_write_seqcount_barrier(seq);
555 * WRITE_ONCE(X, false);
558 #define raw_write_seqcount_barrier(s) \
559 raw_write_seqcount_t_barrier(__seqcount_ptr(s))
561 static inline void raw_write_seqcount_t_barrier(seqcount_t *s)
563 kcsan_nestable_atomic_begin();
567 kcsan_nestable_atomic_end();
571 * write_seqcount_invalidate() - invalidate in-progress seqcount_t read
573 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
575 * After write_seqcount_invalidate, no seqcount_t read side operations
576 * will complete successfully and see data older than this.
578 #define write_seqcount_invalidate(s) \
579 write_seqcount_t_invalidate(__seqcount_ptr(s))
581 static inline void write_seqcount_t_invalidate(seqcount_t *s)
584 kcsan_nestable_atomic_begin();
586 kcsan_nestable_atomic_end();
590 * Latch sequence counters (seqcount_latch_t)
592 * A sequence counter variant where the counter even/odd value is used to
593 * switch between two copies of protected data. This allows the read path,
594 * typically NMIs, to safely interrupt the write side critical section.
596 * As the write sections are fully preemptible, no special handling for
597 * PREEMPT_RT is needed.
604 * SEQCNT_LATCH_ZERO() - static initializer for seqcount_latch_t
605 * @seq_name: Name of the seqcount_latch_t instance
607 #define SEQCNT_LATCH_ZERO(seq_name) { \
608 .seqcount = SEQCNT_ZERO(seq_name.seqcount), \
612 * seqcount_latch_init() - runtime initializer for seqcount_latch_t
613 * @s: Pointer to the seqcount_latch_t instance
615 static inline void seqcount_latch_init(seqcount_latch_t *s)
617 seqcount_init(&s->seqcount);
621 * raw_read_seqcount_latch() - pick even/odd latch data copy
622 * @s: Pointer to seqcount_latch_t
624 * See raw_write_seqcount_latch() for details and a full reader/writer
627 * Return: sequence counter raw value. Use the lowest bit as an index for
628 * picking which data copy to read. The full counter must then be checked
629 * with read_seqcount_latch_retry().
631 static inline unsigned raw_read_seqcount_latch(const seqcount_latch_t *s)
634 * Pairs with the first smp_wmb() in raw_write_seqcount_latch().
635 * Due to the dependent load, a full smp_rmb() is not needed.
637 return READ_ONCE(s->seqcount.sequence);
641 * read_seqcount_latch_retry() - end a seqcount_latch_t read section
642 * @s: Pointer to seqcount_latch_t
643 * @start: count, from raw_read_seqcount_latch()
645 * Return: true if a read section retry is required, else false
648 read_seqcount_latch_retry(const seqcount_latch_t *s, unsigned start)
650 return read_seqcount_retry(&s->seqcount, 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 (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 */
742 * Sequential locks (seqlock_t)
744 * Sequence counters with an embedded spinlock for writer serialization
745 * and non-preemptibility.
747 * For more info, see:
748 * - Comments on top of seqcount_t
749 * - Documentation/locking/seqlock.rst
752 struct seqcount seqcount;
756 #define __SEQLOCK_UNLOCKED(lockname) \
758 .seqcount = SEQCNT_ZERO(lockname), \
759 .lock = __SPIN_LOCK_UNLOCKED(lockname) \
763 * seqlock_init() - dynamic initializer for seqlock_t
764 * @sl: Pointer to the seqlock_t instance
766 #define seqlock_init(sl) \
768 seqcount_init(&(sl)->seqcount); \
769 spin_lock_init(&(sl)->lock); \
773 * DEFINE_SEQLOCK() - Define a statically allocated seqlock_t
774 * @sl: Name of the seqlock_t instance
776 #define DEFINE_SEQLOCK(sl) \
777 seqlock_t sl = __SEQLOCK_UNLOCKED(sl)
780 * read_seqbegin() - start a seqlock_t read side critical section
781 * @sl: Pointer to seqlock_t
783 * Return: count, to be passed to read_seqretry()
785 static inline unsigned read_seqbegin(const seqlock_t *sl)
787 unsigned ret = read_seqcount_begin(&sl->seqcount);
789 kcsan_atomic_next(0); /* non-raw usage, assume closing read_seqretry() */
790 kcsan_flat_atomic_begin();
795 * read_seqretry() - end a seqlock_t read side section
796 * @sl: Pointer to seqlock_t
797 * @start: count, from read_seqbegin()
799 * read_seqretry closes the read side critical section of given seqlock_t.
800 * If the critical section was invalid, it must be ignored (and typically
803 * Return: true if a read section retry is required, else false
805 static inline unsigned read_seqretry(const seqlock_t *sl, unsigned start)
808 * Assume not nested: read_seqretry() may be called multiple times when
809 * completing read critical section.
811 kcsan_flat_atomic_end();
813 return read_seqcount_retry(&sl->seqcount, start);
817 * write_seqlock() - start a seqlock_t write side critical section
818 * @sl: Pointer to seqlock_t
820 * write_seqlock opens a write side critical section for the given
821 * seqlock_t. It also implicitly acquires the spinlock_t embedded inside
822 * that sequential lock. All seqlock_t write side sections are thus
823 * automatically serialized and non-preemptible.
825 * Context: if the seqlock_t read section, or other write side critical
826 * sections, can be invoked from hardirq or softirq contexts, use the
827 * _irqsave or _bh variants of this function instead.
829 static inline void write_seqlock(seqlock_t *sl)
831 spin_lock(&sl->lock);
832 write_seqcount_t_begin(&sl->seqcount);
836 * write_sequnlock() - end a seqlock_t write side critical section
837 * @sl: Pointer to seqlock_t
839 * write_sequnlock closes the (serialized and non-preemptible) write side
840 * critical section of given seqlock_t.
842 static inline void write_sequnlock(seqlock_t *sl)
844 write_seqcount_t_end(&sl->seqcount);
845 spin_unlock(&sl->lock);
849 * write_seqlock_bh() - start a softirqs-disabled seqlock_t write section
850 * @sl: Pointer to seqlock_t
852 * _bh variant of write_seqlock(). Use only if the read side section, or
853 * other write side sections, can be invoked from softirq contexts.
855 static inline void write_seqlock_bh(seqlock_t *sl)
857 spin_lock_bh(&sl->lock);
858 write_seqcount_t_begin(&sl->seqcount);
862 * write_sequnlock_bh() - end a softirqs-disabled seqlock_t write section
863 * @sl: Pointer to seqlock_t
865 * write_sequnlock_bh closes the serialized, non-preemptible, and
866 * softirqs-disabled, seqlock_t write side critical section opened with
867 * write_seqlock_bh().
869 static inline void write_sequnlock_bh(seqlock_t *sl)
871 write_seqcount_t_end(&sl->seqcount);
872 spin_unlock_bh(&sl->lock);
876 * write_seqlock_irq() - start a non-interruptible seqlock_t write section
877 * @sl: Pointer to seqlock_t
879 * _irq variant of write_seqlock(). Use only if the read side section, or
880 * other write sections, can be invoked from hardirq contexts.
882 static inline void write_seqlock_irq(seqlock_t *sl)
884 spin_lock_irq(&sl->lock);
885 write_seqcount_t_begin(&sl->seqcount);
889 * write_sequnlock_irq() - end a non-interruptible seqlock_t write section
890 * @sl: Pointer to seqlock_t
892 * write_sequnlock_irq closes the serialized and non-interruptible
893 * seqlock_t write side section opened with write_seqlock_irq().
895 static inline void write_sequnlock_irq(seqlock_t *sl)
897 write_seqcount_t_end(&sl->seqcount);
898 spin_unlock_irq(&sl->lock);
901 static inline unsigned long __write_seqlock_irqsave(seqlock_t *sl)
905 spin_lock_irqsave(&sl->lock, flags);
906 write_seqcount_t_begin(&sl->seqcount);
911 * write_seqlock_irqsave() - start a non-interruptible seqlock_t write
913 * @lock: Pointer to seqlock_t
914 * @flags: Stack-allocated storage for saving caller's local interrupt
915 * state, to be passed to write_sequnlock_irqrestore().
917 * _irqsave variant of write_seqlock(). Use it only if the read side
918 * section, or other write sections, can be invoked from hardirq context.
920 #define write_seqlock_irqsave(lock, flags) \
921 do { flags = __write_seqlock_irqsave(lock); } while (0)
924 * write_sequnlock_irqrestore() - end non-interruptible seqlock_t write
926 * @sl: Pointer to seqlock_t
927 * @flags: Caller's saved interrupt state, from write_seqlock_irqsave()
929 * write_sequnlock_irqrestore closes the serialized and non-interruptible
930 * seqlock_t write section previously opened with write_seqlock_irqsave().
933 write_sequnlock_irqrestore(seqlock_t *sl, unsigned long flags)
935 write_seqcount_t_end(&sl->seqcount);
936 spin_unlock_irqrestore(&sl->lock, flags);
940 * read_seqlock_excl() - begin a seqlock_t locking reader section
941 * @sl: Pointer to seqlock_t
943 * read_seqlock_excl opens a seqlock_t locking reader critical section. A
944 * locking reader exclusively locks out *both* other writers *and* other
945 * locking readers, but it does not update the embedded sequence number.
947 * Locking readers act like a normal spin_lock()/spin_unlock().
949 * Context: if the seqlock_t write section, *or other read sections*, can
950 * be invoked from hardirq or softirq contexts, use the _irqsave or _bh
951 * variant of this function instead.
953 * The opened read section must be closed with read_sequnlock_excl().
955 static inline void read_seqlock_excl(seqlock_t *sl)
957 spin_lock(&sl->lock);
961 * read_sequnlock_excl() - end a seqlock_t locking reader critical section
962 * @sl: Pointer to seqlock_t
964 static inline void read_sequnlock_excl(seqlock_t *sl)
966 spin_unlock(&sl->lock);
970 * read_seqlock_excl_bh() - start a seqlock_t locking reader section with
972 * @sl: Pointer to seqlock_t
974 * _bh variant of read_seqlock_excl(). Use this variant only if the
975 * seqlock_t write side section, *or other read sections*, can be invoked
976 * from softirq contexts.
978 static inline void read_seqlock_excl_bh(seqlock_t *sl)
980 spin_lock_bh(&sl->lock);
984 * read_sequnlock_excl_bh() - stop a seqlock_t softirq-disabled locking
986 * @sl: Pointer to seqlock_t
988 static inline void read_sequnlock_excl_bh(seqlock_t *sl)
990 spin_unlock_bh(&sl->lock);
994 * read_seqlock_excl_irq() - start a non-interruptible seqlock_t locking
996 * @sl: Pointer to seqlock_t
998 * _irq variant of read_seqlock_excl(). Use this only if the seqlock_t
999 * write side section, *or other read sections*, can be invoked from a
1002 static inline void read_seqlock_excl_irq(seqlock_t *sl)
1004 spin_lock_irq(&sl->lock);
1008 * read_sequnlock_excl_irq() - end an interrupts-disabled seqlock_t
1009 * locking reader section
1010 * @sl: Pointer to seqlock_t
1012 static inline void read_sequnlock_excl_irq(seqlock_t *sl)
1014 spin_unlock_irq(&sl->lock);
1017 static inline unsigned long __read_seqlock_excl_irqsave(seqlock_t *sl)
1019 unsigned long flags;
1021 spin_lock_irqsave(&sl->lock, flags);
1026 * read_seqlock_excl_irqsave() - start a non-interruptible seqlock_t
1027 * locking reader section
1028 * @lock: Pointer to seqlock_t
1029 * @flags: Stack-allocated storage for saving caller's local interrupt
1030 * state, to be passed to read_sequnlock_excl_irqrestore().
1032 * _irqsave variant of read_seqlock_excl(). Use this only if the seqlock_t
1033 * write side section, *or other read sections*, can be invoked from a
1036 #define read_seqlock_excl_irqsave(lock, flags) \
1037 do { flags = __read_seqlock_excl_irqsave(lock); } while (0)
1040 * read_sequnlock_excl_irqrestore() - end non-interruptible seqlock_t
1041 * locking reader section
1042 * @sl: Pointer to seqlock_t
1043 * @flags: Caller saved interrupt state, from read_seqlock_excl_irqsave()
1046 read_sequnlock_excl_irqrestore(seqlock_t *sl, unsigned long flags)
1048 spin_unlock_irqrestore(&sl->lock, flags);
1052 * read_seqbegin_or_lock() - begin a seqlock_t lockless or locking reader
1053 * @lock: Pointer to seqlock_t
1054 * @seq : Marker and return parameter. If the passed value is even, the
1055 * reader will become a *lockless* seqlock_t reader as in read_seqbegin().
1056 * If the passed value is odd, the reader will become a *locking* reader
1057 * as in read_seqlock_excl(). In the first call to this function, the
1058 * caller *must* initialize and pass an even value to @seq; this way, a
1059 * lockless read can be optimistically tried first.
1061 * read_seqbegin_or_lock is an API designed to optimistically try a normal
1062 * lockless seqlock_t read section first. If an odd counter is found, the
1063 * lockless read trial has failed, and the next read iteration transforms
1064 * itself into a full seqlock_t locking reader.
1066 * This is typically used to avoid seqlock_t lockless readers starvation
1067 * (too much retry loops) in the case of a sharp spike in write side
1070 * Context: if the seqlock_t write section, *or other read sections*, can
1071 * be invoked from hardirq or softirq contexts, use the _irqsave or _bh
1072 * variant of this function instead.
1074 * Check Documentation/locking/seqlock.rst for template example code.
1076 * Return: the encountered sequence counter value, through the @seq
1077 * parameter, which is overloaded as a return parameter. This returned
1078 * value must be checked with need_seqretry(). If the read section need to
1079 * be retried, this returned value must also be passed as the @seq
1080 * parameter of the next read_seqbegin_or_lock() iteration.
1082 static inline void read_seqbegin_or_lock(seqlock_t *lock, int *seq)
1084 if (!(*seq & 1)) /* Even */
1085 *seq = read_seqbegin(lock);
1087 read_seqlock_excl(lock);
1091 * need_seqretry() - validate seqlock_t "locking or lockless" read section
1092 * @lock: Pointer to seqlock_t
1093 * @seq: sequence count, from read_seqbegin_or_lock()
1095 * Return: true if a read section retry is required, false otherwise
1097 static inline int need_seqretry(seqlock_t *lock, int seq)
1099 return !(seq & 1) && read_seqretry(lock, seq);
1103 * done_seqretry() - end seqlock_t "locking or lockless" reader section
1104 * @lock: Pointer to seqlock_t
1105 * @seq: count, from read_seqbegin_or_lock()
1107 * done_seqretry finishes the seqlock_t read side critical section started
1108 * with read_seqbegin_or_lock() and validated by need_seqretry().
1110 static inline void done_seqretry(seqlock_t *lock, int seq)
1113 read_sequnlock_excl(lock);
1117 * read_seqbegin_or_lock_irqsave() - begin a seqlock_t lockless reader, or
1118 * a non-interruptible locking reader
1119 * @lock: Pointer to seqlock_t
1120 * @seq: Marker and return parameter. Check read_seqbegin_or_lock().
1122 * This is the _irqsave variant of read_seqbegin_or_lock(). Use it only if
1123 * the seqlock_t write section, *or other read sections*, can be invoked
1124 * from hardirq context.
1126 * Note: Interrupts will be disabled only for "locking reader" mode.
1130 * 1. The saved local interrupts state in case of a locking reader, to
1131 * be passed to done_seqretry_irqrestore().
1133 * 2. The encountered sequence counter value, returned through @seq
1134 * overloaded as a return parameter. Check read_seqbegin_or_lock().
1136 static inline unsigned long
1137 read_seqbegin_or_lock_irqsave(seqlock_t *lock, int *seq)
1139 unsigned long flags = 0;
1141 if (!(*seq & 1)) /* Even */
1142 *seq = read_seqbegin(lock);
1144 read_seqlock_excl_irqsave(lock, flags);
1150 * done_seqretry_irqrestore() - end a seqlock_t lockless reader, or a
1151 * non-interruptible locking reader section
1152 * @lock: Pointer to seqlock_t
1153 * @seq: Count, from read_seqbegin_or_lock_irqsave()
1154 * @flags: Caller's saved local interrupt state in case of a locking
1155 * reader, also from read_seqbegin_or_lock_irqsave()
1157 * This is the _irqrestore variant of done_seqretry(). The read section
1158 * must've been opened with read_seqbegin_or_lock_irqsave(), and validated
1159 * by need_seqretry().
1162 done_seqretry_irqrestore(seqlock_t *lock, int seq, unsigned long flags)
1165 read_sequnlock_excl_irqrestore(lock, flags);
1167 #endif /* __LINUX_SEQLOCK_H */