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,
46 * Write side critical sections must be serialized and non-preemptible.
50 * entering the write section.
60 * serialization and non-preemptibility requirements, use a sequential
78 lockdep_init_map(&s->dep_map, name, key, 0); in __seqcount_init()
79 s->sequence = 0; in __seqcount_init()
88 * seqcount_init() - runtime initializer for seqcount_t
103 seqcount_acquire_read(&l->dep_map, 0, 0, _RET_IP_); in seqcount_lockdep_reader_access()
104 seqcount_release(&l->dep_map, _RET_IP_); in seqcount_lockdep_reader_access()
115 * SEQCNT_ZERO() - static initializer for seqcount_t
125 * that the write side critical section is properly serialized.
145 * lock. This will allow any possibly-preempted writer to make progress
146 * until the end of its writer serialization lock critical section.
148 * This lock-unlock technique must be implemented for all of PREEMPT_RT
158 * typedef seqcount_LOCKNAME_t - sequence counter with LOCKNAME associated
165 * that the write side critical section is properly serialized.
171 * seqcount_LOCKNAME_init() - runtime initializer for seqcount_LOCKNAME_t
179 seqcount_init(&____s->seqcount); \
180 __SEQ_LOCK(____s->lock = (_lock)); \
190 * SEQCOUNT_LOCKNAME() - Instantiate seqcount_LOCKNAME_t and helpers
191 * seqprop_LOCKNAME_*() - Property accessors for seqcount_LOCKNAME_t
209 return &s->seqcount; \
215 unsigned seq = READ_ONCE(s->seqcount.sequence); \
222 __SEQ_LOCK(lockbase##_unlock(s->lock)); \
225 * Re-read the sequence counter since the (possibly \
228 seq = READ_ONCE(s->seqcount.sequence); \
261 return READ_ONCE(s->sequence); in __seqprop_sequence()
276 …OUNT_LOCKNAME(raw_spinlock, raw_spinlock_t, false, s->lock, raw_spin, raw_spin_lock(s->…
277 SEQCOUNT_LOCKNAME(spinlock, spinlock_t, __SEQ_RT, s->lock, spin, spin_lock(s->l…
278 SEQCOUNT_LOCKNAME(rwlock, rwlock_t, __SEQ_RT, s->lock, read, read_lock(s->l…
279 SEQCOUNT_LOCKNAME(mutex, struct mutex, true, s->lock, mutex, mutex_lock(s->…
280 …UNT_LOCKNAME(ww_mutex, struct ww_mutex, true, &s->lock->base, ww_mutex, ww_mutex_lock(s->l…
283 * SEQCNT_LOCKNAME_ZERO - static initializer for seqcount_LOCKNAME_t
316 * __read_seqcount_begin() - begin a seqcount_t read section w/o barrier
322 * protected in this critical section.
341 * raw_read_seqcount_begin() - begin a seqcount_t read section w/o lockdep
355 * read_seqcount_begin() - begin a seqcount_t read critical section
367 * raw_read_seqcount() - read the raw seqcount_t counter value
370 * raw_read_seqcount opens a read critical section of the given
387 * raw_seqcount_begin() - begin a seqcount_t read critical section w/o
391 * raw_seqcount_begin opens a read critical section of the given
395 * section instead of stabilizing at the beginning of it.
397 * Use this only in special kernel hot paths where the read section is
413 * __read_seqcount_retry() - end a seqcount_t read section w/o barrier
420 * protected in this critical section.
425 * Return: true if a read section retry is required, else false
433 return unlikely(READ_ONCE(s->sequence) != start); in do___read_seqcount_retry()
437 * read_seqcount_retry() - end a seqcount_t read critical section
441 * read_seqcount_retry closes the read critical section of given
442 * seqcount_t. If the critical section was invalid, it must be ignored
445 * Return: true if a read section retry is required, else false
457 * raw_write_seqcount_begin() - start a seqcount_t write section w/o lockdep
473 s->sequence++; in do_raw_write_seqcount_begin()
478 * raw_write_seqcount_end() - end a seqcount_t write section w/o lockdep
494 s->sequence++; in do_raw_write_seqcount_end()
499 * write_seqcount_begin_nested() - start a seqcount_t write section with
504 * See Documentation/locking/lockdep-design.rst
520 seqcount_acquire(&s->dep_map, subclass, 0, _RET_IP_); in do_write_seqcount_begin_nested()
524 * write_seqcount_begin() - start a seqcount_t write side critical section
528 * non-preemptible. Preemption will be automatically disabled if and
549 * write_seqcount_end() - end a seqcount_t write side critical section
552 * Context: Preemption will be automatically re-enabled if and only if
565 seqcount_release(&s->dep_map, _RET_IP_); in do_write_seqcount_end()
570 * raw_write_seqcount_barrier() - do a seqcount_t write barrier
575 * the two back-to-back wmb()s.
580 * meant to propagate to the reader critical section. This is necessary because
581 * neither writes before and after the barrier are enclosed in a seq-writer
582 * critical section that would ensure readers are aware of ongoing writes::
616 s->sequence++; in do_raw_write_seqcount_barrier()
618 s->sequence++; in do_raw_write_seqcount_barrier()
623 * write_seqcount_invalidate() - invalidate in-progress seqcount_t read
637 s->sequence+=2; in do_write_seqcount_invalidate()
646 * typically NMIs, to safely interrupt the write side critical section.
656 * SEQCNT_LATCH_ZERO() - static initializer for seqcount_latch_t
664 * seqcount_latch_init() - runtime initializer for seqcount_latch_t
667 #define seqcount_latch_init(s) seqcount_init(&(s)->seqcount)
670 * raw_read_seqcount_latch() - pick even/odd latch data copy
686 return READ_ONCE(s->seqcount.sequence); in raw_read_seqcount_latch()
690 * read_seqcount_latch_retry() - end a seqcount_latch_t read section
694 * Return: true if a read section retry is required, else false
699 return read_seqcount_retry(&s->seqcount, start); in read_seqcount_latch_retry()
703 * raw_write_seqcount_latch() - redirect latch readers to even/odd copy
707 * queries during non-atomic modifications. If you can guarantee queries never
708 * interrupt the modification -- e.g. the concurrency is strictly between CPUs
709 * -- you most likely do not need this.
713 * latch allows the same for non-atomic updates. The trade-off is doubling the
733 * latch->seq.sequence++;
736 * modify(latch->data[0], ...);
739 * latch->seq.sequence++;
742 * modify(latch->data[1], ...);
753 * seq = raw_read_seqcount_latch(&latch->seq);
756 * entry = data_query(latch->data[idx], ...);
759 * } while (read_seqcount_latch_retry(&latch->seq, seq));
770 * The non-requirement for atomic modifications does _NOT_ include
786 s->seqcount.sequence++; in raw_write_seqcount_latch()
794 * and non-preemptibility.
797 * - Comments on top of seqcount_t
798 * - Documentation/locking/seqlock.rst
816 * seqlock_init() - dynamic initializer for seqlock_t
821 spin_lock_init(&(sl)->lock); \
822 seqcount_spinlock_init(&(sl)->seqcount, &(sl)->lock); \
826 * DEFINE_SEQLOCK(sl) - Define a statically allocated seqlock_t
833 * read_seqbegin() - start a seqlock_t read side critical section
840 unsigned ret = read_seqcount_begin(&sl->seqcount); in read_seqbegin()
842 kcsan_atomic_next(0); /* non-raw usage, assume closing read_seqretry() */ in read_seqbegin()
848 * read_seqretry() - end a seqlock_t read side section
852 * read_seqretry closes the read side critical section of given seqlock_t.
853 * If the critical section was invalid, it must be ignored (and typically
856 * Return: true if a read section retry is required, else false
862 * completing read critical section. in read_seqretry()
866 return read_seqcount_retry(&sl->seqcount, start); in read_seqretry()
876 * write_seqlock() - start a seqlock_t write side critical section
879 * write_seqlock opens a write side critical section for the given
882 * automatically serialized and non-preemptible.
884 * Context: if the seqlock_t read section, or other write side critical
890 spin_lock(&sl->lock); in write_seqlock()
891 do_write_seqcount_begin(&sl->seqcount.seqcount); in write_seqlock()
895 * write_sequnlock() - end a seqlock_t write side critical section
898 * write_sequnlock closes the (serialized and non-preemptible) write side
899 * critical section of given seqlock_t.
903 do_write_seqcount_end(&sl->seqcount.seqcount); in write_sequnlock()
904 spin_unlock(&sl->lock); in write_sequnlock()
908 * write_seqlock_bh() - start a softirqs-disabled seqlock_t write section
911 * _bh variant of write_seqlock(). Use only if the read side section, or
916 spin_lock_bh(&sl->lock); in write_seqlock_bh()
917 do_write_seqcount_begin(&sl->seqcount.seqcount); in write_seqlock_bh()
921 * write_sequnlock_bh() - end a softirqs-disabled seqlock_t write section
924 * write_sequnlock_bh closes the serialized, non-preemptible, and
925 * softirqs-disabled, seqlock_t write side critical section opened with
930 do_write_seqcount_end(&sl->seqcount.seqcount); in write_sequnlock_bh()
931 spin_unlock_bh(&sl->lock); in write_sequnlock_bh()
935 * write_seqlock_irq() - start a non-interruptible seqlock_t write section
938 * _irq variant of write_seqlock(). Use only if the read side section, or
943 spin_lock_irq(&sl->lock); in write_seqlock_irq()
944 do_write_seqcount_begin(&sl->seqcount.seqcount); in write_seqlock_irq()
948 * write_sequnlock_irq() - end a non-interruptible seqlock_t write section
951 * write_sequnlock_irq closes the serialized and non-interruptible
952 * seqlock_t write side section opened with write_seqlock_irq().
956 do_write_seqcount_end(&sl->seqcount.seqcount); in write_sequnlock_irq()
957 spin_unlock_irq(&sl->lock); in write_sequnlock_irq()
964 spin_lock_irqsave(&sl->lock, flags); in __write_seqlock_irqsave()
965 do_write_seqcount_begin(&sl->seqcount.seqcount); in __write_seqlock_irqsave()
970 * write_seqlock_irqsave() - start a non-interruptible seqlock_t write
971 * section
973 * @flags: Stack-allocated storage for saving caller's local interrupt
977 * section, or other write sections, can be invoked from hardirq context.
983 * write_sequnlock_irqrestore() - end non-interruptible seqlock_t write
984 * section
988 * write_sequnlock_irqrestore closes the serialized and non-interruptible
989 * seqlock_t write section previously opened with write_seqlock_irqsave().
994 do_write_seqcount_end(&sl->seqcount.seqcount); in write_sequnlock_irqrestore()
995 spin_unlock_irqrestore(&sl->lock, flags); in write_sequnlock_irqrestore()
999 * read_seqlock_excl() - begin a seqlock_t locking reader section
1002 * read_seqlock_excl opens a seqlock_t locking reader critical section. A
1008 * Context: if the seqlock_t write section, *or other read sections*, can
1012 * The opened read section must be closed with read_sequnlock_excl().
1016 spin_lock(&sl->lock); in read_seqlock_excl()
1020 * read_sequnlock_excl() - end a seqlock_t locking reader critical section
1025 spin_unlock(&sl->lock); in read_sequnlock_excl()
1029 * read_seqlock_excl_bh() - start a seqlock_t locking reader section with
1034 * seqlock_t write side section, *or other read sections*, can be invoked
1039 spin_lock_bh(&sl->lock); in read_seqlock_excl_bh()
1043 * read_sequnlock_excl_bh() - stop a seqlock_t softirq-disabled locking
1044 * reader section
1049 spin_unlock_bh(&sl->lock); in read_sequnlock_excl_bh()
1053 * read_seqlock_excl_irq() - start a non-interruptible seqlock_t locking
1054 * reader section
1058 * write side section, *or other read sections*, can be invoked from a
1063 spin_lock_irq(&sl->lock); in read_seqlock_excl_irq()
1067 * read_sequnlock_excl_irq() - end an interrupts-disabled seqlock_t
1068 * locking reader section
1073 spin_unlock_irq(&sl->lock); in read_sequnlock_excl_irq()
1080 spin_lock_irqsave(&sl->lock, flags); in __read_seqlock_excl_irqsave()
1085 * read_seqlock_excl_irqsave() - start a non-interruptible seqlock_t
1086 * locking reader section
1088 * @flags: Stack-allocated storage for saving caller's local interrupt
1092 * write side section, *or other read sections*, can be invoked from a
1099 * read_sequnlock_excl_irqrestore() - end non-interruptible seqlock_t
1100 * locking reader section
1107 spin_unlock_irqrestore(&sl->lock, flags); in read_sequnlock_excl_irqrestore()
1111 * read_seqbegin_or_lock() - begin a seqlock_t lockless or locking reader
1121 * lockless seqlock_t read section first. If an odd counter is found, the
1129 * Context: if the seqlock_t write section, *or other read sections*, can
1137 * value must be checked with need_seqretry(). If the read section need to
1150 * need_seqretry() - validate seqlock_t "locking or lockless" read section
1154 * Return: true if a read section retry is required, false otherwise
1162 * done_seqretry() - end seqlock_t "locking or lockless" reader section
1166 * done_seqretry finishes the seqlock_t read side critical section started
1176 * read_seqbegin_or_lock_irqsave() - begin a seqlock_t lockless reader, or
1177 * a non-interruptible locking reader
1182 * the seqlock_t write section, *or other read sections*, can be invoked
1209 * done_seqretry_irqrestore() - end a seqlock_t lockless reader, or a
1210 * non-interruptible locking reader section
1216 * This is the _irqrestore variant of done_seqretry(). The read section