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 = smp_load_acquire(&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 = smp_load_acquire(&s->seqcount.sequence); \
192 __SEQ_LOCK(lockdep_assert_held(s->lock)); \
211 return smp_load_acquire(&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
283 * raw_read_seqcount_begin() - begin a seqcount_t read section w/o lockdep
291 * read_seqcount_begin() - begin a seqcount_t read critical section
303 * raw_read_seqcount() - read the raw seqcount_t counter value
306 * raw_read_seqcount opens a read critical section of the given
322 * raw_seqcount_try_begin() - begin a seqcount_t read critical section
332 * the lock and there is a slowpath that does that.
336 * Return: true when a read critical section is started.
345 * raw_seqcount_begin() - begin a seqcount_t read critical section w/o
349 * raw_seqcount_begin opens a read critical section of the given
351 * for the count to stabilize. If a writer is active when it begins, it
356 * small and has a high probability of success through other external
357 * means. It will save a single branching instruction.
371 * __read_seqcount_retry() - end a seqcount_t read section w/o barrier
383 * Return: true if a read section retry is required, else false
391 return unlikely(READ_ONCE(s->sequence) != start);
395 * read_seqcount_retry() - end a seqcount_t read critical section
403 * Return: true if a read section retry is required, else false
415 * raw_write_seqcount_begin() - start a seqcount_t write section w/o lockdep
431 s->sequence++; in do_raw_write_seqcount_begin()
436 * raw_write_seqcount_end() - end a seqcount_t write section w/o lockdep
452 s->sequence++; in do_raw_write_seqcount_end()
457 * write_seqcount_begin_nested() - start a seqcount_t write section with
462 * See Documentation/locking/lockdep-design.rst
477 seqcount_acquire(&s->dep_map, subclass, 0, _RET_IP_); in do_write_seqcount_begin_nested()
482 * write_seqcount_begin() - start a seqcount_t write side critical section
485 * Context: sequence counter write side sections must be serialized and
486 * non-preemptible. Preemption will be automatically disabled if and
507 * write_seqcount_end() - end a seqcount_t write side critical section
510 * Context: Preemption will be automatically re-enabled if and only if
523 seqcount_release(&s->dep_map, _RET_IP_); in do_write_seqcount_end()
528 * raw_write_seqcount_barrier() - do a seqcount_t write barrier
533 * the two back-to-back wmb()s.
536 * via WRITE_ONCE): a) to ensure the writes become visible to other threads
539 * neither writes before nor after the barrier are enclosed in a seq-writer
574 s->sequence++; in do_raw_write_seqcount_barrier()
576 s->sequence++; in do_raw_write_seqcount_barrier()
581 * write_seqcount_invalidate() - invalidate in-progress seqcount_t read
582 * side operations
585 * After write_seqcount_invalidate, no seqcount_t read side operations
595 s->sequence+=2; in do_write_seqcount_invalidate()
602 * A sequence counter variant where the counter even/odd value is used to
604 * typically NMIs, to safely interrupt the write side critical section.
614 * SEQCNT_LATCH_ZERO() - static initializer for seqcount_latch_t
622 * seqcount_latch_init() - runtime initializer for seqcount_latch_t
625 #define seqcount_latch_init(s) seqcount_init(&(s)->seqcount)
628 * raw_read_seqcount_latch() - pick even/odd latch data copy
631 * See raw_write_seqcount_latch() for details and a full reader/writer
642 * Due to the dependent load, a full smp_rmb() is not needed. in raw_read_seqcount_latch()
644 return READ_ONCE(s->seqcount.sequence); in raw_read_seqcount_latch()
648 * read_seqcount_latch() - pick even/odd latch data copy
651 * See write_seqcount_latch() for details and a full reader/writer usage
665 * raw_read_seqcount_latch_retry() - end a seqcount_latch_t read section
669 * Return: true if a read section retry is required, else false
675 return unlikely(READ_ONCE(s->seqcount.sequence) != start); in raw_read_seqcount_latch_retry()
679 * read_seqcount_latch_retry() - end a seqcount_latch_t read section
683 * Return: true if a read section retry is required, else false
693 * raw_write_seqcount_latch() - redirect latch readers to even/odd copy
699 s->seqcount.sequence++; in raw_write_seqcount_latch()
704 * write_seqcount_latch_begin() - redirect latch readers to odd copy
707 * The latch technique is a multiversion concurrency control method that allows
708 * queries during non-atomic modifications. If you can guarantee queries never
709 * interrupt the modification -- e.g. the concurrency is strictly between CPUs
710 * -- you most likely do not need this.
714 * latch allows the same for non-atomic updates. The trade-off is doubling the
719 * there is always one copy in a stable state, ready to give us an answer.
721 * The basic form is a data structure like::
728 * Where a modification, which is assumed to be externally serialized, does the
733 * write_seqcount_latch_begin(&latch->seq);
734 * modify(latch->data[0], ...);
735 * write_seqcount_latch(&latch->seq);
736 * modify(latch->data[1], ...);
737 * write_seqcount_latch_end(&latch->seq);
740 * The query will have a form like::
748 * seq = read_seqcount_latch(&latch->seq);
751 * entry = data_query(latch->data[idx], ...);
754 * } while (read_seqcount_latch_retry(&latch->seq, seq));
765 * The non-requirement for atomic modifications does _NOT_ include
766 * the publishing of new entries in the case where data is a dynamic
775 * When data is a dynamic data structure; one should use regular RCU
785 * write_seqcount_latch() - redirect latch readers to even copy
794 * write_seqcount_latch_end() - end a seqcount_latch_t write section
797 * Marks the end of a seqcount_latch_t writer section, after all copies of the
798 * latch-protected data have been updated.
812 * seqlock_init() - dynamic initializer for seqlock_t
817 spin_lock_init(&(sl)->lock); \
818 seqcount_spinlock_init(&(sl)->seqcount, &(sl)->lock); \
822 * DEFINE_SEQLOCK(sl) - Define a statically allocated seqlock_t
829 * read_seqbegin() - start a seqlock_t read side critical section
836 return read_seqcount_begin(&sl->seqcount); in read_seqbegin()
840 * read_seqretry() - end a seqlock_t read side section
844 * read_seqretry closes the read side critical section of given seqlock_t.
848 * Return: true if a read section retry is required, else false
852 return read_seqcount_retry(&sl->seqcount, start); in read_seqretry()
856 * For all seqlock_t write side functions, use the internal
862 * write_seqlock() - start a seqlock_t write side critical section
865 * write_seqlock opens a write side critical section for the given
867 * that sequential lock. All seqlock_t write side sections are thus
868 * automatically serialized and non-preemptible.
870 * Context: if the seqlock_t read section, or other write side critical
876 spin_lock(&sl->lock); in write_seqlock()
877 do_write_seqcount_begin(&sl->seqcount.seqcount); in write_seqlock()
881 * write_sequnlock() - end a seqlock_t write side critical section
884 * write_sequnlock closes the (serialized and non-preemptible) write side
889 do_write_seqcount_end(&sl->seqcount.seqcount); in write_sequnlock()
890 spin_unlock(&sl->lock); in write_sequnlock()
894 * write_seqlock_bh() - start a softirqs-disabled seqlock_t write section
897 * _bh variant of write_seqlock(). Use only if the read side section, or
898 * other write side sections, can be invoked from softirq contexts.
902 spin_lock_bh(&sl->lock); in write_seqlock_bh()
903 do_write_seqcount_begin(&sl->seqcount.seqcount); in write_seqlock_bh()
907 * write_sequnlock_bh() - end a softirqs-disabled seqlock_t write section
910 * write_sequnlock_bh closes the serialized, non-preemptible, and
911 * softirqs-disabled, seqlock_t write side critical section opened with
916 do_write_seqcount_end(&sl->seqcount.seqcount); in write_sequnlock_bh()
917 spin_unlock_bh(&sl->lock); in write_sequnlock_bh()
921 * write_seqlock_irq() - start a non-interruptible seqlock_t write section
924 * _irq variant of write_seqlock(). Use only if the read side section, or
929 spin_lock_irq(&sl->lock); in write_seqlock_irq()
930 do_write_seqcount_begin(&sl->seqcount.seqcount); in write_seqlock_irq()
934 * write_sequnlock_irq() - end a non-interruptible seqlock_t write section
937 * write_sequnlock_irq closes the serialized and non-interruptible
938 * seqlock_t write side section opened with write_seqlock_irq().
942 do_write_seqcount_end(&sl->seqcount.seqcount); in write_sequnlock_irq()
943 spin_unlock_irq(&sl->lock); in write_sequnlock_irq()
950 spin_lock_irqsave(&sl->lock, flags); in __write_seqlock_irqsave()
951 do_write_seqcount_begin(&sl->seqcount.seqcount); in __write_seqlock_irqsave()
956 * write_seqlock_irqsave() - start a non-interruptible seqlock_t write
959 * @flags: Stack-allocated storage for saving caller's local interrupt
962 * _irqsave variant of write_seqlock(). Use it only if the read side
969 * write_sequnlock_irqrestore() - end non-interruptible seqlock_t write
974 * write_sequnlock_irqrestore closes the serialized and non-interruptible
980 do_write_seqcount_end(&sl->seqcount.seqcount); in write_sequnlock_irqrestore()
981 spin_unlock_irqrestore(&sl->lock, flags); in write_sequnlock_irqrestore()
985 * read_seqlock_excl() - begin a seqlock_t locking reader section
988 * read_seqlock_excl opens a seqlock_t locking reader critical section. A
992 * Locking readers act like a normal spin_lock()/spin_unlock().
1002 spin_lock(&sl->lock); in read_seqlock_excl()
1006 * read_sequnlock_excl() - end a seqlock_t locking reader critical section
1011 spin_unlock(&sl->lock); in read_sequnlock_excl()
1015 * read_seqlock_excl_bh() - start a seqlock_t locking reader section with
1020 * seqlock_t write side section, *or other read sections*, can be invoked
1025 spin_lock_bh(&sl->lock); in read_seqlock_excl_bh()
1029 * read_sequnlock_excl_bh() - stop a seqlock_t softirq-disabled locking
1035 spin_unlock_bh(&sl->lock); in read_sequnlock_excl_bh()
1039 * read_seqlock_excl_irq() - start a non-interruptible seqlock_t locking
1044 * write side section, *or other read sections*, can be invoked from a
1049 spin_lock_irq(&sl->lock); in read_seqlock_excl_irq()
1053 * read_sequnlock_excl_irq() - end an interrupts-disabled seqlock_t
1059 spin_unlock_irq(&sl->lock); in read_sequnlock_excl_irq()
1066 spin_lock_irqsave(&sl->lock, flags); in __read_seqlock_excl_irqsave()
1071 * read_seqlock_excl_irqsave() - start a non-interruptible seqlock_t
1074 * @flags: Stack-allocated storage for saving caller's local interrupt
1078 * write side section, *or other read sections*, can be invoked from a
1085 * read_sequnlock_excl_irqrestore() - end non-interruptible seqlock_t
1093 spin_unlock_irqrestore(&sl->lock, flags); in read_sequnlock_excl_irqrestore()
1097 * read_seqbegin_or_lock() - begin a seqlock_t lockless or locking reader
1100 * reader will become a *lockless* seqlock_t reader as in read_seqbegin().
1101 * If the passed value is odd, the reader will become a *locking* reader
1103 * caller *must* initialize and pass an even value to @seq; this way, a
1106 * read_seqbegin_or_lock is an API designed to optimistically try a normal
1109 * itself into a full seqlock_t locking reader.
1112 * (too much retry loops) in the case of a sharp spike in write side
1122 * parameter, which is overloaded as a return parameter. This returned
1136 * need_seqretry() - validate seqlock_t "locking or lockless" read section
1140 * Return: true if a read section retry is required, false otherwise
1148 * done_seqretry() - end seqlock_t "locking or lockless" reader section
1152 * done_seqretry finishes the seqlock_t read side critical section started
1162 * read_seqbegin_or_lock_irqsave() - begin a seqlock_t lockless reader, or
1163 * a non-interruptible locking reader
1175 * 1. The saved local interrupts state in case of a locking reader, to
1179 * overloaded as a return parameter. Check read_seqbegin_or_lock().
1195 * done_seqretry_irqrestore() - end a seqlock_t lockless reader, or a
1196 * non-interruptible locking reader section
1199 * @flags: Caller's saved local interrupt state in case of a locking