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
135 * For PREEMPT_RT, seqcount_LOCKNAME_t write side critical sections cannot
136 * disable preemption. It can lead to higher latencies, and the write side
137 * sections will not be able to acquire locks which become sleeping locks
140 * To remain preemptible while avoiding a possible livelock caused by the
141 * reader preempting the writer, use a different technique: let the reader
142 * detect if a seqcount_LOCKNAME_t writer is in progress. If that is the
143 * case, acquire then release the associated LOCKNAME writer serialization
144 * lock. This will allow any possibly-preempted writer to make progress
145 * until the end of its writer serialization lock critical section.
147 * This lock-unlock technique must be implemented for all of PREEMPT_RT
148 * sleeping locks. See Documentation/locking/locktypes.rst
150 #if defined(CONFIG_LOCKDEP) || defined(CONFIG_PREEMPT_RT)
151 #define __SEQ_LOCK(expr) expr
153 #define __SEQ_LOCK(expr)
157 * typedef seqcount_LOCKNAME_t - sequence counter with LOCKNAME associated
158 * @seqcount: The real sequence counter
159 * @lock: Pointer to the associated lock
161 * A plain sequence counter with external writer synchronization by
162 * LOCKNAME @lock. The lock is associated to the sequence counter in the
163 * static initializer or init function. This enables lockdep to validate
164 * that the write side critical section is properly serialized.
166 * LOCKNAME: raw_spinlock, spinlock, rwlock or mutex
170 * seqcount_LOCKNAME_init() - runtime initializer for seqcount_LOCKNAME_t
171 * @s: Pointer to the seqcount_LOCKNAME_t instance
172 * @lock: Pointer to the associated lock
175 #define seqcount_LOCKNAME_init(s, _lock, lockname) \
177 seqcount_##lockname##_t *____s = (s); \
178 seqcount_init(&____s->seqcount); \
179 __SEQ_LOCK(____s->lock = (_lock)); \
182 #define seqcount_raw_spinlock_init(s, lock) seqcount_LOCKNAME_init(s, lock, raw_spinlock)
183 #define seqcount_spinlock_init(s, lock) seqcount_LOCKNAME_init(s, lock, spinlock)
184 #define seqcount_rwlock_init(s, lock) seqcount_LOCKNAME_init(s, lock, rwlock)
185 #define seqcount_mutex_init(s, lock) seqcount_LOCKNAME_init(s, lock, mutex)
188 * SEQCOUNT_LOCKNAME() - Instantiate seqcount_LOCKNAME_t and helpers
189 * seqprop_LOCKNAME_*() - Property accessors for seqcount_LOCKNAME_t
191 * @lockname: "LOCKNAME" part of seqcount_LOCKNAME_t
192 * @locktype: LOCKNAME canonical C data type
193 * @preemptible: preemptibility of above locktype
194 * @lockbase: prefix for associated lock/unlock
196 #define SEQCOUNT_LOCKNAME(lockname, locktype, preemptible, lockbase) \
197 typedef struct seqcount_##lockname { \
198 seqcount_t seqcount; \
199 __SEQ_LOCK(locktype *lock); \
200 } seqcount_##lockname##_t; \
202 static __always_inline seqcount_t * \
203 __seqprop_##lockname##_ptr(seqcount_##lockname##_t *s) \
205 return &s->seqcount; \
208 static __always_inline const seqcount_t * \
209 __seqprop_##lockname##_const_ptr(const seqcount_##lockname##_t *s) \
211 return &s->seqcount; \
214 static __always_inline unsigned \
215 __seqprop_##lockname##_sequence(const seqcount_##lockname##_t *s) \
217 unsigned seq = READ_ONCE(s->seqcount.sequence); \
219 if (!IS_ENABLED(CONFIG_PREEMPT_RT)) \
222 if (preemptible && unlikely(seq & 1)) { \
223 __SEQ_LOCK(lockbase##_lock(s->lock)); \
224 __SEQ_LOCK(lockbase##_unlock(s->lock)); \
227 * Re-read the sequence counter since the (possibly \
228 * preempted) writer made progress. \
230 seq = READ_ONCE(s->seqcount.sequence); \
236 static __always_inline bool \
237 __seqprop_##lockname##_preemptible(const seqcount_##lockname##_t *s) \
239 if (!IS_ENABLED(CONFIG_PREEMPT_RT)) \
240 return preemptible; \
242 /* PREEMPT_RT relies on the above LOCK+UNLOCK */ \
246 static __always_inline void \
247 __seqprop_##lockname##_assert(const seqcount_##lockname##_t *s) \
249 __SEQ_LOCK(lockdep_assert_held(s->lock)); \
253 * __seqprop() for seqcount_t
256 static inline seqcount_t *__seqprop_ptr(seqcount_t *s)
261 static inline const seqcount_t *__seqprop_const_ptr(const seqcount_t *s)
266 static inline unsigned __seqprop_sequence(const seqcount_t *s)
268 return READ_ONCE(s->sequence);
271 static inline bool __seqprop_preemptible(const seqcount_t *s)
276 static inline void __seqprop_assert(const seqcount_t *s)
278 lockdep_assert_preemption_disabled();
281 #define __SEQ_RT IS_ENABLED(CONFIG_PREEMPT_RT)
283 SEQCOUNT_LOCKNAME(raw_spinlock, raw_spinlock_t, false, raw_spin)
284 SEQCOUNT_LOCKNAME(spinlock, spinlock_t, __SEQ_RT, spin)
285 SEQCOUNT_LOCKNAME(rwlock, rwlock_t, __SEQ_RT, read)
286 SEQCOUNT_LOCKNAME(mutex, struct mutex, true, mutex)
289 * SEQCNT_LOCKNAME_ZERO - static initializer for seqcount_LOCKNAME_t
290 * @name: Name of the seqcount_LOCKNAME_t instance
291 * @lock: Pointer to the associated LOCKNAME
294 #define SEQCOUNT_LOCKNAME_ZERO(seq_name, assoc_lock) { \
295 .seqcount = SEQCNT_ZERO(seq_name.seqcount), \
296 __SEQ_LOCK(.lock = (assoc_lock)) \
299 #define SEQCNT_RAW_SPINLOCK_ZERO(name, lock) SEQCOUNT_LOCKNAME_ZERO(name, lock)
300 #define SEQCNT_SPINLOCK_ZERO(name, lock) SEQCOUNT_LOCKNAME_ZERO(name, lock)
301 #define SEQCNT_RWLOCK_ZERO(name, lock) SEQCOUNT_LOCKNAME_ZERO(name, lock)
302 #define SEQCNT_MUTEX_ZERO(name, lock) SEQCOUNT_LOCKNAME_ZERO(name, lock)
303 #define SEQCNT_WW_MUTEX_ZERO(name, lock) SEQCOUNT_LOCKNAME_ZERO(name, lock)
305 #define __seqprop_case(s, lockname, prop) \
306 seqcount_##lockname##_t: __seqprop_##lockname##_##prop
308 #define __seqprop(s, prop) _Generic(*(s), \
309 seqcount_t: __seqprop_##prop, \
310 __seqprop_case((s), raw_spinlock, prop), \
311 __seqprop_case((s), spinlock, prop), \
312 __seqprop_case((s), rwlock, prop), \
313 __seqprop_case((s), mutex, prop))
315 #define seqprop_ptr(s) __seqprop(s, ptr)(s)
316 #define seqprop_const_ptr(s) __seqprop(s, const_ptr)(s)
317 #define seqprop_sequence(s) __seqprop(s, sequence)(s)
318 #define seqprop_preemptible(s) __seqprop(s, preemptible)(s)
319 #define seqprop_assert(s) __seqprop(s, assert)(s)
322 * __read_seqcount_begin() - begin a seqcount_t read section w/o barrier
323 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
325 * __read_seqcount_begin is like read_seqcount_begin, but has no smp_rmb()
326 * barrier. Callers should ensure that smp_rmb() or equivalent ordering is
327 * provided before actually loading any of the variables that are to be
328 * protected in this critical section.
330 * Use carefully, only in critical code, and comment how the barrier is
333 * Return: count to be passed to read_seqcount_retry()
335 #define __read_seqcount_begin(s) \
339 while ((__seq = seqprop_sequence(s)) & 1) \
342 kcsan_atomic_next(KCSAN_SEQLOCK_REGION_MAX); \
347 * raw_read_seqcount_begin() - begin a seqcount_t read section w/o lockdep
348 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
350 * Return: count to be passed to read_seqcount_retry()
352 #define raw_read_seqcount_begin(s) \
354 unsigned _seq = __read_seqcount_begin(s); \
361 * read_seqcount_begin() - begin a seqcount_t read critical section
362 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
364 * Return: count to be passed to read_seqcount_retry()
366 #define read_seqcount_begin(s) \
368 seqcount_lockdep_reader_access(seqprop_const_ptr(s)); \
369 raw_read_seqcount_begin(s); \
373 * raw_read_seqcount() - read the raw seqcount_t counter value
374 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
376 * raw_read_seqcount opens a read critical section of the given
377 * seqcount_t, without any lockdep checking, and without checking or
378 * masking the sequence counter LSB. Calling code is responsible for
381 * Return: count to be passed to read_seqcount_retry()
383 #define raw_read_seqcount(s) \
385 unsigned __seq = seqprop_sequence(s); \
388 kcsan_atomic_next(KCSAN_SEQLOCK_REGION_MAX); \
393 * raw_seqcount_begin() - begin a seqcount_t read critical section w/o
394 * lockdep and w/o counter stabilization
395 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
397 * raw_seqcount_begin opens a read critical section of the given
398 * seqcount_t. Unlike read_seqcount_begin(), this function will not wait
399 * for the count to stabilize. If a writer is active when it begins, it
400 * will fail the read_seqcount_retry() at the end of the read critical
401 * section instead of stabilizing at the beginning of it.
403 * Use this only in special kernel hot paths where the read section is
404 * small and has a high probability of success through other external
405 * means. It will save a single branching instruction.
407 * Return: count to be passed to read_seqcount_retry()
409 #define raw_seqcount_begin(s) \
412 * If the counter is odd, let read_seqcount_retry() fail \
413 * by decrementing the counter. \
415 raw_read_seqcount(s) & ~1; \
419 * __read_seqcount_retry() - end a seqcount_t read section w/o barrier
420 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
421 * @start: count, from read_seqcount_begin()
423 * __read_seqcount_retry is like read_seqcount_retry, but has no smp_rmb()
424 * barrier. Callers should ensure that smp_rmb() or equivalent ordering is
425 * provided before actually loading any of the variables that are to be
426 * protected in this critical section.
428 * Use carefully, only in critical code, and comment how the barrier is
431 * Return: true if a read section retry is required, else false
433 #define __read_seqcount_retry(s, start) \
434 do___read_seqcount_retry(seqprop_const_ptr(s), start)
436 static inline int do___read_seqcount_retry(const seqcount_t *s, unsigned start)
438 kcsan_atomic_next(0);
439 return unlikely(READ_ONCE(s->sequence) != start);
443 * read_seqcount_retry() - end a seqcount_t read critical section
444 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
445 * @start: count, from read_seqcount_begin()
447 * read_seqcount_retry closes the read critical section of given
448 * seqcount_t. If the critical section was invalid, it must be ignored
449 * (and typically retried).
451 * Return: true if a read section retry is required, else false
453 #define read_seqcount_retry(s, start) \
454 do_read_seqcount_retry(seqprop_const_ptr(s), start)
456 static inline int do_read_seqcount_retry(const seqcount_t *s, unsigned start)
459 return do___read_seqcount_retry(s, start);
463 * raw_write_seqcount_begin() - start a seqcount_t write section w/o lockdep
464 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
466 * Context: check write_seqcount_begin()
468 #define raw_write_seqcount_begin(s) \
470 if (seqprop_preemptible(s)) \
473 do_raw_write_seqcount_begin(seqprop_ptr(s)); \
476 static inline void do_raw_write_seqcount_begin(seqcount_t *s)
478 kcsan_nestable_atomic_begin();
484 * raw_write_seqcount_end() - end a seqcount_t write section w/o lockdep
485 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
487 * Context: check write_seqcount_end()
489 #define raw_write_seqcount_end(s) \
491 do_raw_write_seqcount_end(seqprop_ptr(s)); \
493 if (seqprop_preemptible(s)) \
497 static inline void do_raw_write_seqcount_end(seqcount_t *s)
501 kcsan_nestable_atomic_end();
505 * write_seqcount_begin_nested() - start a seqcount_t write section with
506 * custom lockdep nesting level
507 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
508 * @subclass: lockdep nesting level
510 * See Documentation/locking/lockdep-design.rst
511 * Context: check write_seqcount_begin()
513 #define write_seqcount_begin_nested(s, subclass) \
517 if (seqprop_preemptible(s)) \
520 do_write_seqcount_begin_nested(seqprop_ptr(s), subclass); \
523 static inline void do_write_seqcount_begin_nested(seqcount_t *s, int subclass)
525 seqcount_acquire(&s->dep_map, subclass, 0, _RET_IP_);
526 do_raw_write_seqcount_begin(s);
530 * write_seqcount_begin() - start a seqcount_t write side critical section
531 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
533 * Context: sequence counter write side sections must be serialized and
534 * non-preemptible. Preemption will be automatically disabled if and
535 * only if the seqcount write serialization lock is associated, and
536 * preemptible. If readers can be invoked from hardirq or softirq
537 * context, interrupts or bottom halves must be respectively disabled.
539 #define write_seqcount_begin(s) \
543 if (seqprop_preemptible(s)) \
546 do_write_seqcount_begin(seqprop_ptr(s)); \
549 static inline void do_write_seqcount_begin(seqcount_t *s)
551 do_write_seqcount_begin_nested(s, 0);
555 * write_seqcount_end() - end a seqcount_t write side critical section
556 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
558 * Context: Preemption will be automatically re-enabled if and only if
559 * the seqcount write serialization lock is associated, and preemptible.
561 #define write_seqcount_end(s) \
563 do_write_seqcount_end(seqprop_ptr(s)); \
565 if (seqprop_preemptible(s)) \
569 static inline void do_write_seqcount_end(seqcount_t *s)
571 seqcount_release(&s->dep_map, _RET_IP_);
572 do_raw_write_seqcount_end(s);
576 * raw_write_seqcount_barrier() - do a seqcount_t write barrier
577 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
579 * This can be used to provide an ordering guarantee instead of the usual
580 * consistency guarantee. It is one wmb cheaper, because it can collapse
581 * the two back-to-back wmb()s.
583 * Note that writes surrounding the barrier should be declared atomic (e.g.
584 * via WRITE_ONCE): a) to ensure the writes become visible to other threads
585 * atomically, avoiding compiler optimizations; b) to document which writes are
586 * meant to propagate to the reader critical section. This is necessary because
587 * neither writes before nor after the barrier are enclosed in a seq-writer
588 * critical section that would ensure readers are aware of ongoing writes::
591 * bool X = true, Y = false;
598 * int s = read_seqcount_begin(&seq);
602 * } while (read_seqcount_retry(&seq, s));
609 * WRITE_ONCE(Y, true);
611 * raw_write_seqcount_barrier(seq);
613 * WRITE_ONCE(X, false);
616 #define raw_write_seqcount_barrier(s) \
617 do_raw_write_seqcount_barrier(seqprop_ptr(s))
619 static inline void do_raw_write_seqcount_barrier(seqcount_t *s)
621 kcsan_nestable_atomic_begin();
625 kcsan_nestable_atomic_end();
629 * write_seqcount_invalidate() - invalidate in-progress seqcount_t read
631 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
633 * After write_seqcount_invalidate, no seqcount_t read side operations
634 * will complete successfully and see data older than this.
636 #define write_seqcount_invalidate(s) \
637 do_write_seqcount_invalidate(seqprop_ptr(s))
639 static inline void do_write_seqcount_invalidate(seqcount_t *s)
642 kcsan_nestable_atomic_begin();
644 kcsan_nestable_atomic_end();
648 * Latch sequence counters (seqcount_latch_t)
650 * A sequence counter variant where the counter even/odd value is used to
651 * switch between two copies of protected data. This allows the read path,
652 * typically NMIs, to safely interrupt the write side critical section.
654 * As the write sections are fully preemptible, no special handling for
655 * PREEMPT_RT is needed.
662 * SEQCNT_LATCH_ZERO() - static initializer for seqcount_latch_t
663 * @seq_name: Name of the seqcount_latch_t instance
665 #define SEQCNT_LATCH_ZERO(seq_name) { \
666 .seqcount = SEQCNT_ZERO(seq_name.seqcount), \
670 * seqcount_latch_init() - runtime initializer for seqcount_latch_t
671 * @s: Pointer to the seqcount_latch_t instance
673 #define seqcount_latch_init(s) seqcount_init(&(s)->seqcount)
676 * raw_read_seqcount_latch() - pick even/odd latch data copy
677 * @s: Pointer to seqcount_latch_t
679 * See raw_write_seqcount_latch() for details and a full reader/writer
682 * Return: sequence counter raw value. Use the lowest bit as an index for
683 * picking which data copy to read. The full counter must then be checked
684 * with raw_read_seqcount_latch_retry().
686 static __always_inline unsigned raw_read_seqcount_latch(const seqcount_latch_t *s)
689 * Pairs with the first smp_wmb() in raw_write_seqcount_latch().
690 * Due to the dependent load, a full smp_rmb() is not needed.
692 return READ_ONCE(s->seqcount.sequence);
696 * raw_read_seqcount_latch_retry() - end a seqcount_latch_t read section
697 * @s: Pointer to seqcount_latch_t
698 * @start: count, from raw_read_seqcount_latch()
700 * Return: true if a read section retry is required, else false
702 static __always_inline int
703 raw_read_seqcount_latch_retry(const seqcount_latch_t *s, unsigned start)
706 return unlikely(READ_ONCE(s->seqcount.sequence) != start);
710 * raw_write_seqcount_latch() - redirect latch readers to even/odd copy
711 * @s: Pointer to seqcount_latch_t
713 * The latch technique is a multiversion concurrency control method that allows
714 * queries during non-atomic modifications. If you can guarantee queries never
715 * interrupt the modification -- e.g. the concurrency is strictly between CPUs
716 * -- you most likely do not need this.
718 * Where the traditional RCU/lockless data structures rely on atomic
719 * modifications to ensure queries observe either the old or the new state the
720 * latch allows the same for non-atomic updates. The trade-off is doubling the
721 * cost of storage; we have to maintain two copies of the entire data
724 * Very simply put: we first modify one copy and then the other. This ensures
725 * there is always one copy in a stable state, ready to give us an answer.
727 * The basic form is a data structure like::
729 * struct latch_struct {
730 * seqcount_latch_t seq;
731 * struct data_struct data[2];
734 * Where a modification, which is assumed to be externally serialized, does the
737 * void latch_modify(struct latch_struct *latch, ...)
739 * smp_wmb(); // Ensure that the last data[1] update is visible
740 * latch->seq.sequence++;
741 * smp_wmb(); // Ensure that the seqcount update is visible
743 * modify(latch->data[0], ...);
745 * smp_wmb(); // Ensure that the data[0] update is visible
746 * latch->seq.sequence++;
747 * smp_wmb(); // Ensure that the seqcount update is visible
749 * modify(latch->data[1], ...);
752 * The query will have a form like::
754 * struct entry *latch_query(struct latch_struct *latch, ...)
756 * struct entry *entry;
760 * seq = raw_read_seqcount_latch(&latch->seq);
763 * entry = data_query(latch->data[idx], ...);
765 * // This includes needed smp_rmb()
766 * } while (raw_read_seqcount_latch_retry(&latch->seq, seq));
771 * So during the modification, queries are first redirected to data[1]. Then we
772 * modify data[0]. When that is complete, we redirect queries back to data[0]
773 * and we can modify data[1].
777 * The non-requirement for atomic modifications does _NOT_ include
778 * the publishing of new entries in the case where data is a dynamic
781 * An iteration might start in data[0] and get suspended long enough
782 * to miss an entire modification sequence, once it resumes it might
783 * observe the new entry.
787 * When data is a dynamic data structure; one should use regular RCU
788 * patterns to manage the lifetimes of the objects within.
790 static inline void raw_write_seqcount_latch(seqcount_latch_t *s)
792 smp_wmb(); /* prior stores before incrementing "sequence" */
793 s->seqcount.sequence++;
794 smp_wmb(); /* increment "sequence" before following stores */
798 * Sequential locks (seqlock_t)
800 * Sequence counters with an embedded spinlock for writer serialization
801 * and non-preemptibility.
803 * For more info, see:
804 * - Comments on top of seqcount_t
805 * - Documentation/locking/seqlock.rst
809 * Make sure that readers don't starve writers on PREEMPT_RT: use
810 * seqcount_spinlock_t instead of seqcount_t. Check __SEQ_LOCK().
812 seqcount_spinlock_t seqcount;
816 #define __SEQLOCK_UNLOCKED(lockname) \
818 .seqcount = SEQCNT_SPINLOCK_ZERO(lockname, &(lockname).lock), \
819 .lock = __SPIN_LOCK_UNLOCKED(lockname) \
823 * seqlock_init() - dynamic initializer for seqlock_t
824 * @sl: Pointer to the seqlock_t instance
826 #define seqlock_init(sl) \
828 spin_lock_init(&(sl)->lock); \
829 seqcount_spinlock_init(&(sl)->seqcount, &(sl)->lock); \
833 * DEFINE_SEQLOCK(sl) - Define a statically allocated seqlock_t
834 * @sl: Name of the seqlock_t instance
836 #define DEFINE_SEQLOCK(sl) \
837 seqlock_t sl = __SEQLOCK_UNLOCKED(sl)
840 * read_seqbegin() - start a seqlock_t read side critical section
841 * @sl: Pointer to seqlock_t
843 * Return: count, to be passed to read_seqretry()
845 static inline unsigned read_seqbegin(const seqlock_t *sl)
847 unsigned ret = read_seqcount_begin(&sl->seqcount);
849 kcsan_atomic_next(0); /* non-raw usage, assume closing read_seqretry() */
850 kcsan_flat_atomic_begin();
855 * read_seqretry() - end a seqlock_t read side section
856 * @sl: Pointer to seqlock_t
857 * @start: count, from read_seqbegin()
859 * read_seqretry closes the read side critical section of given seqlock_t.
860 * If the critical section was invalid, it must be ignored (and typically
863 * Return: true if a read section retry is required, else false
865 static inline unsigned read_seqretry(const seqlock_t *sl, unsigned start)
868 * Assume not nested: read_seqretry() may be called multiple times when
869 * completing read critical section.
871 kcsan_flat_atomic_end();
873 return read_seqcount_retry(&sl->seqcount, start);
877 * For all seqlock_t write side functions, use the internal
878 * do_write_seqcount_begin() instead of generic write_seqcount_begin().
879 * This way, no redundant lockdep_assert_held() checks are added.
883 * write_seqlock() - start a seqlock_t write side critical section
884 * @sl: Pointer to seqlock_t
886 * write_seqlock opens a write side critical section for the given
887 * seqlock_t. It also implicitly acquires the spinlock_t embedded inside
888 * that sequential lock. All seqlock_t write side sections are thus
889 * automatically serialized and non-preemptible.
891 * Context: if the seqlock_t read section, or other write side critical
892 * sections, can be invoked from hardirq or softirq contexts, use the
893 * _irqsave or _bh variants of this function instead.
895 static inline void write_seqlock(seqlock_t *sl)
897 spin_lock(&sl->lock);
898 do_write_seqcount_begin(&sl->seqcount.seqcount);
902 * write_sequnlock() - end a seqlock_t write side critical section
903 * @sl: Pointer to seqlock_t
905 * write_sequnlock closes the (serialized and non-preemptible) write side
906 * critical section of given seqlock_t.
908 static inline void write_sequnlock(seqlock_t *sl)
910 do_write_seqcount_end(&sl->seqcount.seqcount);
911 spin_unlock(&sl->lock);
915 * write_seqlock_bh() - start a softirqs-disabled seqlock_t write section
916 * @sl: Pointer to seqlock_t
918 * _bh variant of write_seqlock(). Use only if the read side section, or
919 * other write side sections, can be invoked from softirq contexts.
921 static inline void write_seqlock_bh(seqlock_t *sl)
923 spin_lock_bh(&sl->lock);
924 do_write_seqcount_begin(&sl->seqcount.seqcount);
928 * write_sequnlock_bh() - end a softirqs-disabled seqlock_t write section
929 * @sl: Pointer to seqlock_t
931 * write_sequnlock_bh closes the serialized, non-preemptible, and
932 * softirqs-disabled, seqlock_t write side critical section opened with
933 * write_seqlock_bh().
935 static inline void write_sequnlock_bh(seqlock_t *sl)
937 do_write_seqcount_end(&sl->seqcount.seqcount);
938 spin_unlock_bh(&sl->lock);
942 * write_seqlock_irq() - start a non-interruptible seqlock_t write section
943 * @sl: Pointer to seqlock_t
945 * _irq variant of write_seqlock(). Use only if the read side section, or
946 * other write sections, can be invoked from hardirq contexts.
948 static inline void write_seqlock_irq(seqlock_t *sl)
950 spin_lock_irq(&sl->lock);
951 do_write_seqcount_begin(&sl->seqcount.seqcount);
955 * write_sequnlock_irq() - end a non-interruptible seqlock_t write section
956 * @sl: Pointer to seqlock_t
958 * write_sequnlock_irq closes the serialized and non-interruptible
959 * seqlock_t write side section opened with write_seqlock_irq().
961 static inline void write_sequnlock_irq(seqlock_t *sl)
963 do_write_seqcount_end(&sl->seqcount.seqcount);
964 spin_unlock_irq(&sl->lock);
967 static inline unsigned long __write_seqlock_irqsave(seqlock_t *sl)
971 spin_lock_irqsave(&sl->lock, flags);
972 do_write_seqcount_begin(&sl->seqcount.seqcount);
977 * write_seqlock_irqsave() - start a non-interruptible seqlock_t write
979 * @lock: Pointer to seqlock_t
980 * @flags: Stack-allocated storage for saving caller's local interrupt
981 * state, to be passed to write_sequnlock_irqrestore().
983 * _irqsave variant of write_seqlock(). Use it only if the read side
984 * section, or other write sections, can be invoked from hardirq context.
986 #define write_seqlock_irqsave(lock, flags) \
987 do { flags = __write_seqlock_irqsave(lock); } while (0)
990 * write_sequnlock_irqrestore() - end non-interruptible seqlock_t write
992 * @sl: Pointer to seqlock_t
993 * @flags: Caller's saved interrupt state, from write_seqlock_irqsave()
995 * write_sequnlock_irqrestore closes the serialized and non-interruptible
996 * seqlock_t write section previously opened with write_seqlock_irqsave().
999 write_sequnlock_irqrestore(seqlock_t *sl, unsigned long flags)
1001 do_write_seqcount_end(&sl->seqcount.seqcount);
1002 spin_unlock_irqrestore(&sl->lock, flags);
1006 * read_seqlock_excl() - begin a seqlock_t locking reader section
1007 * @sl: Pointer to seqlock_t
1009 * read_seqlock_excl opens a seqlock_t locking reader critical section. A
1010 * locking reader exclusively locks out *both* other writers *and* other
1011 * locking readers, but it does not update the embedded sequence number.
1013 * Locking readers act like a normal spin_lock()/spin_unlock().
1015 * Context: if the seqlock_t write section, *or other read sections*, can
1016 * be invoked from hardirq or softirq contexts, use the _irqsave or _bh
1017 * variant of this function instead.
1019 * The opened read section must be closed with read_sequnlock_excl().
1021 static inline void read_seqlock_excl(seqlock_t *sl)
1023 spin_lock(&sl->lock);
1027 * read_sequnlock_excl() - end a seqlock_t locking reader critical section
1028 * @sl: Pointer to seqlock_t
1030 static inline void read_sequnlock_excl(seqlock_t *sl)
1032 spin_unlock(&sl->lock);
1036 * read_seqlock_excl_bh() - start a seqlock_t locking reader section with
1038 * @sl: Pointer to seqlock_t
1040 * _bh variant of read_seqlock_excl(). Use this variant only if the
1041 * seqlock_t write side section, *or other read sections*, can be invoked
1042 * from softirq contexts.
1044 static inline void read_seqlock_excl_bh(seqlock_t *sl)
1046 spin_lock_bh(&sl->lock);
1050 * read_sequnlock_excl_bh() - stop a seqlock_t softirq-disabled locking
1052 * @sl: Pointer to seqlock_t
1054 static inline void read_sequnlock_excl_bh(seqlock_t *sl)
1056 spin_unlock_bh(&sl->lock);
1060 * read_seqlock_excl_irq() - start a non-interruptible seqlock_t locking
1062 * @sl: Pointer to seqlock_t
1064 * _irq variant of read_seqlock_excl(). Use this only if the seqlock_t
1065 * write side section, *or other read sections*, can be invoked from a
1068 static inline void read_seqlock_excl_irq(seqlock_t *sl)
1070 spin_lock_irq(&sl->lock);
1074 * read_sequnlock_excl_irq() - end an interrupts-disabled seqlock_t
1075 * locking reader section
1076 * @sl: Pointer to seqlock_t
1078 static inline void read_sequnlock_excl_irq(seqlock_t *sl)
1080 spin_unlock_irq(&sl->lock);
1083 static inline unsigned long __read_seqlock_excl_irqsave(seqlock_t *sl)
1085 unsigned long flags;
1087 spin_lock_irqsave(&sl->lock, flags);
1092 * read_seqlock_excl_irqsave() - start a non-interruptible seqlock_t
1093 * locking reader section
1094 * @lock: Pointer to seqlock_t
1095 * @flags: Stack-allocated storage for saving caller's local interrupt
1096 * state, to be passed to read_sequnlock_excl_irqrestore().
1098 * _irqsave variant of read_seqlock_excl(). Use this only if the seqlock_t
1099 * write side section, *or other read sections*, can be invoked from a
1102 #define read_seqlock_excl_irqsave(lock, flags) \
1103 do { flags = __read_seqlock_excl_irqsave(lock); } while (0)
1106 * read_sequnlock_excl_irqrestore() - end non-interruptible seqlock_t
1107 * locking reader section
1108 * @sl: Pointer to seqlock_t
1109 * @flags: Caller saved interrupt state, from read_seqlock_excl_irqsave()
1112 read_sequnlock_excl_irqrestore(seqlock_t *sl, unsigned long flags)
1114 spin_unlock_irqrestore(&sl->lock, flags);
1118 * read_seqbegin_or_lock() - begin a seqlock_t lockless or locking reader
1119 * @lock: Pointer to seqlock_t
1120 * @seq : Marker and return parameter. If the passed value is even, the
1121 * reader will become a *lockless* seqlock_t reader as in read_seqbegin().
1122 * If the passed value is odd, the reader will become a *locking* reader
1123 * as in read_seqlock_excl(). In the first call to this function, the
1124 * caller *must* initialize and pass an even value to @seq; this way, a
1125 * lockless read can be optimistically tried first.
1127 * read_seqbegin_or_lock is an API designed to optimistically try a normal
1128 * lockless seqlock_t read section first. If an odd counter is found, the
1129 * lockless read trial has failed, and the next read iteration transforms
1130 * itself into a full seqlock_t locking reader.
1132 * This is typically used to avoid seqlock_t lockless readers starvation
1133 * (too much retry loops) in the case of a sharp spike in write side
1136 * Context: if the seqlock_t write section, *or other read sections*, can
1137 * be invoked from hardirq or softirq contexts, use the _irqsave or _bh
1138 * variant of this function instead.
1140 * Check Documentation/locking/seqlock.rst for template example code.
1142 * Return: the encountered sequence counter value, through the @seq
1143 * parameter, which is overloaded as a return parameter. This returned
1144 * value must be checked with need_seqretry(). If the read section need to
1145 * be retried, this returned value must also be passed as the @seq
1146 * parameter of the next read_seqbegin_or_lock() iteration.
1148 static inline void read_seqbegin_or_lock(seqlock_t *lock, int *seq)
1150 if (!(*seq & 1)) /* Even */
1151 *seq = read_seqbegin(lock);
1153 read_seqlock_excl(lock);
1157 * need_seqretry() - validate seqlock_t "locking or lockless" read section
1158 * @lock: Pointer to seqlock_t
1159 * @seq: sequence count, from read_seqbegin_or_lock()
1161 * Return: true if a read section retry is required, false otherwise
1163 static inline int need_seqretry(seqlock_t *lock, int seq)
1165 return !(seq & 1) && read_seqretry(lock, seq);
1169 * done_seqretry() - end seqlock_t "locking or lockless" reader section
1170 * @lock: Pointer to seqlock_t
1171 * @seq: count, from read_seqbegin_or_lock()
1173 * done_seqretry finishes the seqlock_t read side critical section started
1174 * with read_seqbegin_or_lock() and validated by need_seqretry().
1176 static inline void done_seqretry(seqlock_t *lock, int seq)
1179 read_sequnlock_excl(lock);
1183 * read_seqbegin_or_lock_irqsave() - begin a seqlock_t lockless reader, or
1184 * a non-interruptible locking reader
1185 * @lock: Pointer to seqlock_t
1186 * @seq: Marker and return parameter. Check read_seqbegin_or_lock().
1188 * This is the _irqsave variant of read_seqbegin_or_lock(). Use it only if
1189 * the seqlock_t write section, *or other read sections*, can be invoked
1190 * from hardirq context.
1192 * Note: Interrupts will be disabled only for "locking reader" mode.
1196 * 1. The saved local interrupts state in case of a locking reader, to
1197 * be passed to done_seqretry_irqrestore().
1199 * 2. The encountered sequence counter value, returned through @seq
1200 * overloaded as a return parameter. Check read_seqbegin_or_lock().
1202 static inline unsigned long
1203 read_seqbegin_or_lock_irqsave(seqlock_t *lock, int *seq)
1205 unsigned long flags = 0;
1207 if (!(*seq & 1)) /* Even */
1208 *seq = read_seqbegin(lock);
1210 read_seqlock_excl_irqsave(lock, flags);
1216 * done_seqretry_irqrestore() - end a seqlock_t lockless reader, or a
1217 * non-interruptible locking reader section
1218 * @lock: Pointer to seqlock_t
1219 * @seq: Count, from read_seqbegin_or_lock_irqsave()
1220 * @flags: Caller's saved local interrupt state in case of a locking
1221 * reader, also from read_seqbegin_or_lock_irqsave()
1223 * This is the _irqrestore variant of done_seqretry(). The read section
1224 * must've been opened with read_seqbegin_or_lock_irqsave(), and validated
1225 * by need_seqretry().
1228 done_seqretry_irqrestore(seqlock_t *lock, int seq, unsigned long flags)
1231 read_sequnlock_excl_irqrestore(lock, flags);
1233 #endif /* __LINUX_SEQLOCK_H */