Lines Matching +full:- +full:section
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>
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,
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
94 * that the write side critical section is properly serialized.
105 * typedef seqcount_LOCKNAME_t - sequence counter with LOCKNAME associated
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
328 * section entirely if odd, instead of doing the speculation knowing it will
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
353 * section instead of stabilizing at the beginning of it.
355 * Use this only in special kernel hot paths where the read section is
371 * __read_seqcount_retry() - end a seqcount_t read section w/o barrier
378 * protected in this critical section.
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
399 * read_seqcount_retry closes the read critical section of given
400 * seqcount_t. If the critical section was invalid, it must be ignored
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
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.
538 * meant to propagate to the reader critical section. This is necessary because
539 * neither writes before nor after the barrier are enclosed in a seq-writer
540 * critical section that would ensure readers are aware of ongoing writes::
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
595 s->sequence+=2; in do_write_seqcount_invalidate()
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
644 return READ_ONCE(s->seqcount.sequence); in raw_read_seqcount_latch()
648 * read_seqcount_latch() - pick even/odd latch data copy
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
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
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);
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
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.
845 * If the critical section was invalid, it must be ignored (and typically
848 * Return: true if a read section retry is required, else false
852 return read_seqcount_retry(&sl->seqcount, start); in read_seqretry()
862 * write_seqlock() - start a seqlock_t write side critical section
865 * write_seqlock opens a write side critical section for the given
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
885 * critical section of given seqlock_t.
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
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
957 * section
959 * @flags: Stack-allocated storage for saving caller's local interrupt
963 * section, or other write sections, can be invoked from hardirq context.
969 * write_sequnlock_irqrestore() - end non-interruptible seqlock_t write
970 * section
974 * write_sequnlock_irqrestore closes the serialized and non-interruptible
975 * seqlock_t write section previously opened with write_seqlock_irqsave().
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
994 * Context: if the seqlock_t write section, *or other read sections*, can
998 * The opened read section must be closed with read_sequnlock_excl().
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
1030 * reader section
1035 spin_unlock_bh(&sl->lock); in read_sequnlock_excl_bh()
1039 * read_seqlock_excl_irq() - start a non-interruptible seqlock_t locking
1040 * reader section
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
1054 * locking reader section
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
1072 * locking reader section
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
1086 * locking reader section
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
1107 * lockless seqlock_t read section first. If an odd counter is found, the
1115 * Context: if the seqlock_t write section, *or other read sections*, can
1123 * value must be checked with need_seqretry(). If the read section need to
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
1168 * the seqlock_t write section, *or other read sections*, can be invoked
1195 * done_seqretry_irqrestore() - end a seqlock_t lockless reader, or a
1196 * non-interruptible locking reader section
1202 * This is the _irqrestore variant of done_seqretry(). The read section