Lines Matching +full:a +full:- +full:side
1 /* SPDX-License-Identifier: GPL-2.0 */
6 * seqcount_t / seqlock_t - a reader-writer consistency mechanism with
7 * lockless readers (read-only retry loops), and no writer starvation.
12 * - Based on x86_64 vsyscall gettimeofday: Keith Owens, Andrea Arcangeli
13 * - Sequence counters with associated locks, (C) 2020 Linutronix GmbH
17 #include <linux/kcsan-checks.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
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,
35 * atomics; if there is a matching read_seqcount_retry() call, no following
45 * Make sure we are not reinitializing a held lock: in __seqcount_init()
47 lockdep_init_map(&s->dep_map, name, key, 0); in __seqcount_init()
48 s->sequence = 0; in __seqcount_init()
57 * seqcount_init() - runtime initializer for seqcount_t
72 seqcount_acquire_read(&l->dep_map, 0, 0, _RET_IP_); in seqcount_lockdep_reader_access()
73 seqcount_release(&l->dep_map, _RET_IP_); in seqcount_lockdep_reader_access()
84 * SEQCNT_ZERO() - static initializer for seqcount_t
92 * A sequence counter which associates the lock used for writer
94 * that the write side critical section is properly serialized.
97 * preemption protection is enforced in the write side function.
105 * typedef seqcount_LOCKNAME_t - sequence counter with LOCKNAME associated
109 * A plain sequence counter with external writer synchronization by
112 * that the write side critical section is properly serialized.
118 * seqcount_LOCKNAME_init() - runtime initializer for seqcount_LOCKNAME_t
126 seqcount_init(&____s->seqcount); \
127 __SEQ_LOCK(____s->lock = (_lock)); \
136 * SEQCOUNT_LOCKNAME() - Instantiate seqcount_LOCKNAME_t and helpers
137 * seqprop_LOCKNAME_*() - Property accessors for seqcount_LOCKNAME_t
148 return &s->seqcount; \
154 return &s->seqcount; \
160 unsigned seq = READ_ONCE(s->seqcount.sequence); \
166 __SEQ_LOCK(lockbase##_lock(s->lock)); \
167 __SEQ_LOCK(lockbase##_unlock(s->lock)); \
170 * Re-read the sequence counter since the (possibly \
173 seq = READ_ONCE(s->seqcount.sequence); \
192 __SEQ_LOCK(lockdep_assert_held(s->lock)); \
211 return READ_ONCE(s->sequence); in __seqprop_sequence()
233 * SEQCNT_LOCKNAME_ZERO - static initializer for seqcount_LOCKNAME_t in SEQCOUNT_LOCKNAME()
266 * __read_seqcount_begin() - begin a seqcount_t read section w/o barrier
291 * raw_read_seqcount_begin() - begin a seqcount_t read section w/o lockdep
305 * read_seqcount_begin() - begin a seqcount_t read critical section
317 * raw_read_seqcount() - read the raw seqcount_t counter value
320 * raw_read_seqcount opens a read critical section of the given
337 * raw_seqcount_begin() - begin a seqcount_t read critical section w/o
341 * raw_seqcount_begin opens a read critical section of the given
343 * for the count to stabilize. If a writer is active when it begins, it
348 * small and has a high probability of success through other external
349 * means. It will save a single branching instruction.
363 * __read_seqcount_retry() - end a seqcount_t read section w/o barrier
375 * Return: true if a read section retry is required, else false
383 return unlikely(READ_ONCE(s->sequence) != start);
387 * read_seqcount_retry() - end a seqcount_t read critical section
395 * Return: true if a read section retry is required, else false
407 * raw_write_seqcount_begin() - start a seqcount_t write section w/o lockdep
423 s->sequence++; in do_raw_write_seqcount_begin()
428 * raw_write_seqcount_end() - end a seqcount_t write section w/o lockdep
444 s->sequence++; in do_raw_write_seqcount_end()
449 * write_seqcount_begin_nested() - start a seqcount_t write section with
454 * See Documentation/locking/lockdep-design.rst
469 seqcount_acquire(&s->dep_map, subclass, 0, _RET_IP_); in do_write_seqcount_begin_nested()
474 * write_seqcount_begin() - start a seqcount_t write side critical section
477 * Context: sequence counter write side sections must be serialized and
478 * non-preemptible. Preemption will be automatically disabled if and
499 * write_seqcount_end() - end a seqcount_t write side critical section
502 * Context: Preemption will be automatically re-enabled if and only if
515 seqcount_release(&s->dep_map, _RET_IP_); in do_write_seqcount_end()
520 * raw_write_seqcount_barrier() - do a seqcount_t write barrier
525 * the two back-to-back wmb()s.
528 * via WRITE_ONCE): a) to ensure the writes become visible to other threads
531 * neither writes before nor after the barrier are enclosed in a seq-writer
566 s->sequence++; in do_raw_write_seqcount_barrier()
568 s->sequence++; in do_raw_write_seqcount_barrier()
573 * write_seqcount_invalidate() - invalidate in-progress seqcount_t read
574 * side operations
577 * After write_seqcount_invalidate, no seqcount_t read side operations
587 s->sequence+=2; in do_write_seqcount_invalidate()
594 * A sequence counter variant where the counter even/odd value is used to
596 * typically NMIs, to safely interrupt the write side critical section.
606 * SEQCNT_LATCH_ZERO() - static initializer for seqcount_latch_t
614 * seqcount_latch_init() - runtime initializer for seqcount_latch_t
617 #define seqcount_latch_init(s) seqcount_init(&(s)->seqcount)
620 * raw_read_seqcount_latch() - pick even/odd latch data copy
623 * See raw_write_seqcount_latch() for details and a full reader/writer
634 * Due to the dependent load, a full smp_rmb() is not needed. in raw_read_seqcount_latch()
636 return READ_ONCE(s->seqcount.sequence); in raw_read_seqcount_latch()
640 * raw_read_seqcount_latch_retry() - end a seqcount_latch_t read section
644 * Return: true if a read section retry is required, else false
650 return unlikely(READ_ONCE(s->seqcount.sequence) != start); in raw_read_seqcount_latch_retry()
654 * raw_write_seqcount_latch() - redirect latch readers to even/odd copy
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.
664 * latch allows the same for non-atomic updates. The trade-off is doubling the
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::
678 * Where a modification, which is assumed to be externally serialized, does the
684 * latch->seq.sequence++;
687 * modify(latch->data[0], ...);
690 * latch->seq.sequence++;
693 * modify(latch->data[1], ...);
696 * The query will have a form like::
704 * seq = raw_read_seqcount_latch(&latch->seq);
707 * entry = data_query(latch->data[idx], ...);
710 * } while (raw_read_seqcount_latch_retry(&latch->seq, seq));
721 * The non-requirement for atomic modifications does _NOT_ include
722 * the publishing of new entries in the case where data is a dynamic
731 * When data is a dynamic data structure; one should use regular RCU
737 s->seqcount.sequence++; in raw_write_seqcount_latch()
748 * seqlock_init() - dynamic initializer for seqlock_t
753 spin_lock_init(&(sl)->lock); \
754 seqcount_spinlock_init(&(sl)->seqcount, &(sl)->lock); \
758 * DEFINE_SEQLOCK(sl) - Define a statically allocated seqlock_t
765 * read_seqbegin() - start a seqlock_t read side critical section
772 unsigned ret = read_seqcount_begin(&sl->seqcount); in read_seqbegin()
774 kcsan_atomic_next(0); /* non-raw usage, assume closing read_seqretry() */ in read_seqbegin()
780 * read_seqretry() - end a seqlock_t read side section
784 * read_seqretry closes the read side critical section of given seqlock_t.
788 * Return: true if a read section retry is required, else false
798 return read_seqcount_retry(&sl->seqcount, start); in read_seqretry()
802 * For all seqlock_t write side functions, use the internal
808 * write_seqlock() - start a seqlock_t write side critical section
811 * write_seqlock opens a write side critical section for the given
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
822 spin_lock(&sl->lock); in write_seqlock()
823 do_write_seqcount_begin(&sl->seqcount.seqcount); in write_seqlock()
827 * write_sequnlock() - end a seqlock_t write side critical section
830 * write_sequnlock closes the (serialized and non-preemptible) write side
835 do_write_seqcount_end(&sl->seqcount.seqcount); in write_sequnlock()
836 spin_unlock(&sl->lock); in write_sequnlock()
840 * write_seqlock_bh() - start a softirqs-disabled seqlock_t write section
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.
848 spin_lock_bh(&sl->lock); in write_seqlock_bh()
849 do_write_seqcount_begin(&sl->seqcount.seqcount); in write_seqlock_bh()
853 * write_sequnlock_bh() - end a softirqs-disabled seqlock_t write section
856 * write_sequnlock_bh closes the serialized, non-preemptible, and
857 * softirqs-disabled, seqlock_t write side critical section opened with
862 do_write_seqcount_end(&sl->seqcount.seqcount); in write_sequnlock_bh()
863 spin_unlock_bh(&sl->lock); in write_sequnlock_bh()
867 * write_seqlock_irq() - start a non-interruptible seqlock_t write section
870 * _irq variant of write_seqlock(). Use only if the read side section, or
875 spin_lock_irq(&sl->lock); in write_seqlock_irq()
876 do_write_seqcount_begin(&sl->seqcount.seqcount); in write_seqlock_irq()
880 * write_sequnlock_irq() - end a non-interruptible seqlock_t write section
883 * write_sequnlock_irq closes the serialized and non-interruptible
884 * seqlock_t write side section opened with write_seqlock_irq().
888 do_write_seqcount_end(&sl->seqcount.seqcount); in write_sequnlock_irq()
889 spin_unlock_irq(&sl->lock); in write_sequnlock_irq()
896 spin_lock_irqsave(&sl->lock, flags); in __write_seqlock_irqsave()
897 do_write_seqcount_begin(&sl->seqcount.seqcount); in __write_seqlock_irqsave()
902 * write_seqlock_irqsave() - start a non-interruptible seqlock_t write
905 * @flags: Stack-allocated storage for saving caller's local interrupt
908 * _irqsave variant of write_seqlock(). Use it only if the read side
915 * write_sequnlock_irqrestore() - end non-interruptible seqlock_t write
920 * write_sequnlock_irqrestore closes the serialized and non-interruptible
926 do_write_seqcount_end(&sl->seqcount.seqcount); in write_sequnlock_irqrestore()
927 spin_unlock_irqrestore(&sl->lock, flags); in write_sequnlock_irqrestore()
931 * read_seqlock_excl() - begin a seqlock_t locking reader section
934 * read_seqlock_excl opens a seqlock_t locking reader critical section. A
938 * Locking readers act like a normal spin_lock()/spin_unlock().
948 spin_lock(&sl->lock); in read_seqlock_excl()
952 * read_sequnlock_excl() - end a seqlock_t locking reader critical section
957 spin_unlock(&sl->lock); in read_sequnlock_excl()
961 * read_seqlock_excl_bh() - start a seqlock_t locking reader section with
966 * seqlock_t write side section, *or other read sections*, can be invoked
971 spin_lock_bh(&sl->lock); in read_seqlock_excl_bh()
975 * read_sequnlock_excl_bh() - stop a seqlock_t softirq-disabled locking
981 spin_unlock_bh(&sl->lock); in read_sequnlock_excl_bh()
985 * read_seqlock_excl_irq() - start a non-interruptible seqlock_t locking
990 * write side section, *or other read sections*, can be invoked from a
995 spin_lock_irq(&sl->lock); in read_seqlock_excl_irq()
999 * read_sequnlock_excl_irq() - end an interrupts-disabled seqlock_t
1005 spin_unlock_irq(&sl->lock); in read_sequnlock_excl_irq()
1012 spin_lock_irqsave(&sl->lock, flags); in __read_seqlock_excl_irqsave()
1017 * read_seqlock_excl_irqsave() - start a non-interruptible seqlock_t
1020 * @flags: Stack-allocated storage for saving caller's local interrupt
1024 * write side section, *or other read sections*, can be invoked from a
1031 * read_sequnlock_excl_irqrestore() - end non-interruptible seqlock_t
1039 spin_unlock_irqrestore(&sl->lock, flags); in read_sequnlock_excl_irqrestore()
1043 * read_seqbegin_or_lock() - begin a seqlock_t lockless or locking reader
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
1049 * caller *must* initialize and pass an even value to @seq; this way, a
1052 * read_seqbegin_or_lock is an API designed to optimistically try a normal
1055 * itself into a full seqlock_t locking reader.
1058 * (too much retry loops) in the case of a sharp spike in write side
1068 * parameter, which is overloaded as a return parameter. This returned
1082 * need_seqretry() - validate seqlock_t "locking or lockless" read section
1086 * Return: true if a read section retry is required, false otherwise
1094 * done_seqretry() - end seqlock_t "locking or lockless" reader section
1098 * done_seqretry finishes the seqlock_t read side critical section started
1108 * read_seqbegin_or_lock_irqsave() - begin a seqlock_t lockless reader, or
1109 * a non-interruptible locking reader
1121 * 1. The saved local interrupts state in case of a locking reader, to
1125 * overloaded as a return parameter. Check read_seqbegin_or_lock().
1141 * done_seqretry_irqrestore() - end a seqlock_t lockless reader, or a
1142 * non-interruptible locking reader section
1145 * @flags: Caller's saved local interrupt state in case of a locking