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_LOCKTYPE_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_LOCKTYPE_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
134 #ifdef CONFIG_LOCKDEP
135 #define __SEQ_LOCK(expr) expr
137 #define __SEQ_LOCK(expr)
141 * typedef seqcount_LOCKNAME_t - sequence counter with LOCKTYPR associated
142 * @seqcount: The real sequence counter
143 * @lock: Pointer to the associated spinlock
145 * A plain sequence counter with external writer synchronization by a
146 * spinlock. The spinlock is associated to the sequence count in the
147 * static initializer or init function. This enables lockdep to validate
148 * that the write side critical section is properly serialized.
152 * seqcount_LOCKNAME_init() - runtime initializer for seqcount_LOCKNAME_t
153 * @s: Pointer to the seqcount_LOCKNAME_t instance
154 * @lock: Pointer to the associated LOCKTYPE
158 * SEQCOUNT_LOCKTYPE() - Instantiate seqcount_LOCKNAME_t and helpers
159 * @locktype: actual typename
161 * @preemptible: preemptibility of above locktype
162 * @lockmember: argument for lockdep_assert_held()
164 #define SEQCOUNT_LOCKTYPE(locktype, lockname, preemptible, lockmember) \
165 typedef struct seqcount_##lockname { \
166 seqcount_t seqcount; \
167 __SEQ_LOCK(locktype *lock); \
168 } seqcount_##lockname##_t; \
170 static __always_inline void \
171 seqcount_##lockname##_init(seqcount_##lockname##_t *s, locktype *lock) \
173 seqcount_init(&s->seqcount); \
174 __SEQ_LOCK(s->lock = lock); \
177 static __always_inline seqcount_t * \
178 __seqcount_##lockname##_ptr(seqcount_##lockname##_t *s) \
180 return &s->seqcount; \
183 static __always_inline bool \
184 __seqcount_##lockname##_preemptible(seqcount_##lockname##_t *s) \
186 return preemptible; \
189 static __always_inline void \
190 __seqcount_##lockname##_assert(seqcount_##lockname##_t *s) \
192 __SEQ_LOCK(lockdep_assert_held(lockmember)); \
196 * __seqprop() for seqcount_t
199 static inline seqcount_t *__seqcount_ptr(seqcount_t *s)
204 static inline bool __seqcount_preemptible(seqcount_t *s)
209 static inline void __seqcount_assert(seqcount_t *s)
211 lockdep_assert_preemption_disabled();
214 SEQCOUNT_LOCKTYPE(raw_spinlock_t, raw_spinlock, false, s->lock)
215 SEQCOUNT_LOCKTYPE(spinlock_t, spinlock, false, s->lock)
216 SEQCOUNT_LOCKTYPE(rwlock_t, rwlock, false, s->lock)
217 SEQCOUNT_LOCKTYPE(struct mutex, mutex, true, s->lock)
218 SEQCOUNT_LOCKTYPE(struct ww_mutex, ww_mutex, true, &s->lock->base)
221 * SEQCNT_LOCKNAME_ZERO - static initializer for seqcount_LOCKNAME_t
222 * @name: Name of the seqcount_LOCKNAME_t instance
223 * @lock: Pointer to the associated LOCKTYPE
226 #define SEQCOUNT_LOCKTYPE_ZERO(seq_name, assoc_lock) { \
227 .seqcount = SEQCNT_ZERO(seq_name.seqcount), \
228 __SEQ_LOCK(.lock = (assoc_lock)) \
231 #define SEQCNT_SPINLOCK_ZERO(name, lock) SEQCOUNT_LOCKTYPE_ZERO(name, lock)
232 #define SEQCNT_RAW_SPINLOCK_ZERO(name, lock) SEQCOUNT_LOCKTYPE_ZERO(name, lock)
233 #define SEQCNT_RWLOCK_ZERO(name, lock) SEQCOUNT_LOCKTYPE_ZERO(name, lock)
234 #define SEQCNT_MUTEX_ZERO(name, lock) SEQCOUNT_LOCKTYPE_ZERO(name, lock)
235 #define SEQCNT_WW_MUTEX_ZERO(name, lock) SEQCOUNT_LOCKTYPE_ZERO(name, lock)
238 #define __seqprop_case(s, lockname, prop) \
239 seqcount_##lockname##_t: __seqcount_##lockname##_##prop((void *)(s))
241 #define __seqprop(s, prop) _Generic(*(s), \
242 seqcount_t: __seqcount_##prop((void *)(s)), \
243 __seqprop_case((s), raw_spinlock, prop), \
244 __seqprop_case((s), spinlock, prop), \
245 __seqprop_case((s), rwlock, prop), \
246 __seqprop_case((s), mutex, prop), \
247 __seqprop_case((s), ww_mutex, prop))
249 #define __seqcount_ptr(s) __seqprop(s, ptr)
250 #define __seqcount_lock_preemptible(s) __seqprop(s, preemptible)
251 #define __seqcount_assert_lock_held(s) __seqprop(s, assert)
254 * __read_seqcount_begin() - begin a seqcount_t read section w/o barrier
255 * @s: Pointer to seqcount_t or any of the seqcount_locktype_t variants
257 * __read_seqcount_begin is like read_seqcount_begin, but has no smp_rmb()
258 * barrier. Callers should ensure that smp_rmb() or equivalent ordering is
259 * provided before actually loading any of the variables that are to be
260 * protected in this critical section.
262 * Use carefully, only in critical code, and comment how the barrier is
265 * Return: count to be passed to read_seqcount_retry()
267 #define __read_seqcount_begin(s) \
268 __read_seqcount_t_begin(__seqcount_ptr(s))
270 static inline unsigned __read_seqcount_t_begin(const seqcount_t *s)
275 ret = READ_ONCE(s->sequence);
276 if (unlikely(ret & 1)) {
280 kcsan_atomic_next(KCSAN_SEQLOCK_REGION_MAX);
285 * raw_read_seqcount_begin() - begin a seqcount_t read section w/o lockdep
286 * @s: Pointer to seqcount_t or any of the seqcount_locktype_t variants
288 * Return: count to be passed to read_seqcount_retry()
290 #define raw_read_seqcount_begin(s) \
291 raw_read_seqcount_t_begin(__seqcount_ptr(s))
293 static inline unsigned raw_read_seqcount_t_begin(const seqcount_t *s)
295 unsigned ret = __read_seqcount_t_begin(s);
301 * read_seqcount_begin() - begin a seqcount_t read critical section
302 * @s: Pointer to seqcount_t or any of the seqcount_locktype_t variants
304 * Return: count to be passed to read_seqcount_retry()
306 #define read_seqcount_begin(s) \
307 read_seqcount_t_begin(__seqcount_ptr(s))
309 static inline unsigned read_seqcount_t_begin(const seqcount_t *s)
311 seqcount_lockdep_reader_access(s);
312 return raw_read_seqcount_t_begin(s);
316 * raw_read_seqcount() - read the raw seqcount_t counter value
317 * @s: Pointer to seqcount_t or any of the seqcount_locktype_t variants
319 * raw_read_seqcount opens a read critical section of the given
320 * seqcount_t, without any lockdep checking, and without checking or
321 * masking the sequence counter LSB. Calling code is responsible for
324 * Return: count to be passed to read_seqcount_retry()
326 #define raw_read_seqcount(s) \
327 raw_read_seqcount_t(__seqcount_ptr(s))
329 static inline unsigned raw_read_seqcount_t(const seqcount_t *s)
331 unsigned ret = READ_ONCE(s->sequence);
333 kcsan_atomic_next(KCSAN_SEQLOCK_REGION_MAX);
338 * raw_seqcount_begin() - begin a seqcount_t read critical section w/o
339 * lockdep and w/o counter stabilization
340 * @s: Pointer to seqcount_t or any of the seqcount_locktype_t variants
342 * raw_seqcount_begin opens a read critical section of the given
343 * seqcount_t. Unlike read_seqcount_begin(), this function will not wait
344 * for the count to stabilize. If a writer is active when it begins, it
345 * will fail the read_seqcount_retry() at the end of the read critical
346 * section instead of stabilizing at the beginning of it.
348 * Use this only in special kernel hot paths where the read section is
349 * small and has a high probability of success through other external
350 * means. It will save a single branching instruction.
352 * Return: count to be passed to read_seqcount_retry()
354 #define raw_seqcount_begin(s) \
355 raw_seqcount_t_begin(__seqcount_ptr(s))
357 static inline unsigned raw_seqcount_t_begin(const seqcount_t *s)
360 * If the counter is odd, let read_seqcount_retry() fail
361 * by decrementing the counter.
363 return raw_read_seqcount_t(s) & ~1;
367 * __read_seqcount_retry() - end a seqcount_t read section w/o barrier
368 * @s: Pointer to seqcount_t or any of the seqcount_locktype_t variants
369 * @start: count, from read_seqcount_begin()
371 * __read_seqcount_retry is like read_seqcount_retry, but has no smp_rmb()
372 * barrier. Callers should ensure that smp_rmb() or equivalent ordering is
373 * provided before actually loading any of the variables that are to be
374 * protected in this critical section.
376 * Use carefully, only in critical code, and comment how the barrier is
379 * Return: true if a read section retry is required, else false
381 #define __read_seqcount_retry(s, start) \
382 __read_seqcount_t_retry(__seqcount_ptr(s), start)
384 static inline int __read_seqcount_t_retry(const seqcount_t *s, unsigned start)
386 kcsan_atomic_next(0);
387 return unlikely(READ_ONCE(s->sequence) != start);
391 * read_seqcount_retry() - end a seqcount_t read critical section
392 * @s: Pointer to seqcount_t or any of the seqcount_locktype_t variants
393 * @start: count, from read_seqcount_begin()
395 * read_seqcount_retry closes the read critical section of given
396 * seqcount_t. If the critical section was invalid, it must be ignored
397 * (and typically retried).
399 * Return: true if a read section retry is required, else false
401 #define read_seqcount_retry(s, start) \
402 read_seqcount_t_retry(__seqcount_ptr(s), start)
404 static inline int read_seqcount_t_retry(const seqcount_t *s, unsigned start)
407 return __read_seqcount_t_retry(s, start);
411 * raw_write_seqcount_begin() - start a seqcount_t write section w/o lockdep
412 * @s: Pointer to seqcount_t or any of the seqcount_locktype_t variants
414 #define raw_write_seqcount_begin(s) \
416 if (__seqcount_lock_preemptible(s)) \
419 raw_write_seqcount_t_begin(__seqcount_ptr(s)); \
422 static inline void raw_write_seqcount_t_begin(seqcount_t *s)
424 kcsan_nestable_atomic_begin();
430 * raw_write_seqcount_end() - end a seqcount_t write section w/o lockdep
431 * @s: Pointer to seqcount_t or any of the seqcount_locktype_t variants
433 #define raw_write_seqcount_end(s) \
435 raw_write_seqcount_t_end(__seqcount_ptr(s)); \
437 if (__seqcount_lock_preemptible(s)) \
441 static inline void raw_write_seqcount_t_end(seqcount_t *s)
445 kcsan_nestable_atomic_end();
449 * write_seqcount_begin_nested() - start a seqcount_t write section with
450 * custom lockdep nesting level
451 * @s: Pointer to seqcount_t or any of the seqcount_locktype_t variants
452 * @subclass: lockdep nesting level
454 * See Documentation/locking/lockdep-design.rst
456 #define write_seqcount_begin_nested(s, subclass) \
458 __seqcount_assert_lock_held(s); \
460 if (__seqcount_lock_preemptible(s)) \
463 write_seqcount_t_begin_nested(__seqcount_ptr(s), subclass); \
466 static inline void write_seqcount_t_begin_nested(seqcount_t *s, int subclass)
468 raw_write_seqcount_t_begin(s);
469 seqcount_acquire(&s->dep_map, subclass, 0, _RET_IP_);
473 * write_seqcount_begin() - start a seqcount_t write side critical section
474 * @s: Pointer to seqcount_t or any of the seqcount_locktype_t variants
476 * write_seqcount_begin opens a write side critical section of the given
479 * Context: seqcount_t write side critical sections must be serialized and
480 * non-preemptible. If readers can be invoked from hardirq or softirq
481 * context, interrupts or bottom halves must be respectively disabled.
483 #define write_seqcount_begin(s) \
485 __seqcount_assert_lock_held(s); \
487 if (__seqcount_lock_preemptible(s)) \
490 write_seqcount_t_begin(__seqcount_ptr(s)); \
493 static inline void write_seqcount_t_begin(seqcount_t *s)
495 write_seqcount_t_begin_nested(s, 0);
499 * write_seqcount_end() - end a seqcount_t write side critical section
500 * @s: Pointer to seqcount_t or any of the seqcount_locktype_t variants
502 * The write section must've been opened with write_seqcount_begin().
504 #define write_seqcount_end(s) \
506 write_seqcount_t_end(__seqcount_ptr(s)); \
508 if (__seqcount_lock_preemptible(s)) \
512 static inline void write_seqcount_t_end(seqcount_t *s)
514 seqcount_release(&s->dep_map, _RET_IP_);
515 raw_write_seqcount_t_end(s);
519 * raw_write_seqcount_barrier() - do a seqcount_t write barrier
520 * @s: Pointer to seqcount_t or any of the seqcount_locktype_t variants
522 * This can be used to provide an ordering guarantee instead of the usual
523 * consistency guarantee. It is one wmb cheaper, because it can collapse
524 * the two back-to-back wmb()s.
526 * Note that writes surrounding the barrier should be declared atomic (e.g.
527 * via WRITE_ONCE): a) to ensure the writes become visible to other threads
528 * atomically, avoiding compiler optimizations; b) to document which writes are
529 * meant to propagate to the reader critical section. This is necessary because
530 * neither writes before and after the barrier are enclosed in a seq-writer
531 * critical section that would ensure readers are aware of ongoing writes::
534 * bool X = true, Y = false;
541 * int s = read_seqcount_begin(&seq);
545 * } while (read_seqcount_retry(&seq, s));
552 * WRITE_ONCE(Y, true);
554 * raw_write_seqcount_barrier(seq);
556 * WRITE_ONCE(X, false);
559 #define raw_write_seqcount_barrier(s) \
560 raw_write_seqcount_t_barrier(__seqcount_ptr(s))
562 static inline void raw_write_seqcount_t_barrier(seqcount_t *s)
564 kcsan_nestable_atomic_begin();
568 kcsan_nestable_atomic_end();
572 * write_seqcount_invalidate() - invalidate in-progress seqcount_t read
574 * @s: Pointer to seqcount_t or any of the seqcount_locktype_t variants
576 * After write_seqcount_invalidate, no seqcount_t read side operations
577 * will complete successfully and see data older than this.
579 #define write_seqcount_invalidate(s) \
580 write_seqcount_t_invalidate(__seqcount_ptr(s))
582 static inline void write_seqcount_t_invalidate(seqcount_t *s)
585 kcsan_nestable_atomic_begin();
587 kcsan_nestable_atomic_end();
591 * raw_read_seqcount_latch() - pick even/odd seqcount_t latch data copy
592 * @s: Pointer to seqcount_t or any of the seqcount_locktype_t variants
594 * Use seqcount_t latching to switch between two storage places protected
595 * by a sequence counter. Doing so allows having interruptible, preemptible,
596 * seqcount_t write side critical sections.
598 * Check raw_write_seqcount_latch() for more details and a full reader and
599 * writer usage example.
601 * Return: sequence counter raw value. Use the lowest bit as an index for
602 * picking which data copy to read. The full counter value must then be
603 * checked with read_seqcount_retry().
605 #define raw_read_seqcount_latch(s) \
606 raw_read_seqcount_t_latch(__seqcount_ptr(s))
608 static inline int raw_read_seqcount_t_latch(seqcount_t *s)
610 /* Pairs with the first smp_wmb() in raw_write_seqcount_latch() */
611 int seq = READ_ONCE(s->sequence); /* ^^^ */
616 * raw_write_seqcount_latch() - redirect readers to even/odd copy
617 * @s: Pointer to seqcount_t or any of the seqcount_locktype_t variants
619 * The latch technique is a multiversion concurrency control method that allows
620 * queries during non-atomic modifications. If you can guarantee queries never
621 * interrupt the modification -- e.g. the concurrency is strictly between CPUs
622 * -- you most likely do not need this.
624 * Where the traditional RCU/lockless data structures rely on atomic
625 * modifications to ensure queries observe either the old or the new state the
626 * latch allows the same for non-atomic updates. The trade-off is doubling the
627 * cost of storage; we have to maintain two copies of the entire data
630 * Very simply put: we first modify one copy and then the other. This ensures
631 * there is always one copy in a stable state, ready to give us an answer.
633 * The basic form is a data structure like::
635 * struct latch_struct {
637 * struct data_struct data[2];
640 * Where a modification, which is assumed to be externally serialized, does the
643 * void latch_modify(struct latch_struct *latch, ...)
645 * smp_wmb(); // Ensure that the last data[1] update is visible
647 * smp_wmb(); // Ensure that the seqcount update is visible
649 * modify(latch->data[0], ...);
651 * smp_wmb(); // Ensure that the data[0] update is visible
653 * smp_wmb(); // Ensure that the seqcount update is visible
655 * modify(latch->data[1], ...);
658 * The query will have a form like::
660 * struct entry *latch_query(struct latch_struct *latch, ...)
662 * struct entry *entry;
666 * seq = raw_read_seqcount_latch(&latch->seq);
669 * entry = data_query(latch->data[idx], ...);
671 * // read_seqcount_retry() includes needed smp_rmb()
672 * } while (read_seqcount_retry(&latch->seq, seq));
677 * So during the modification, queries are first redirected to data[1]. Then we
678 * modify data[0]. When that is complete, we redirect queries back to data[0]
679 * and we can modify data[1].
683 * The non-requirement for atomic modifications does _NOT_ include
684 * the publishing of new entries in the case where data is a dynamic
687 * An iteration might start in data[0] and get suspended long enough
688 * to miss an entire modification sequence, once it resumes it might
689 * observe the new entry.
693 * When data is a dynamic data structure; one should use regular RCU
694 * patterns to manage the lifetimes of the objects within.
696 #define raw_write_seqcount_latch(s) \
697 raw_write_seqcount_t_latch(__seqcount_ptr(s))
699 static inline void raw_write_seqcount_t_latch(seqcount_t *s)
701 smp_wmb(); /* prior stores before incrementing "sequence" */
703 smp_wmb(); /* increment "sequence" before following stores */
707 * Sequential locks (seqlock_t)
709 * Sequence counters with an embedded spinlock for writer serialization
710 * and non-preemptibility.
712 * For more info, see:
713 * - Comments on top of seqcount_t
714 * - Documentation/locking/seqlock.rst
717 struct seqcount seqcount;
721 #define __SEQLOCK_UNLOCKED(lockname) \
723 .seqcount = SEQCNT_ZERO(lockname), \
724 .lock = __SPIN_LOCK_UNLOCKED(lockname) \
728 * seqlock_init() - dynamic initializer for seqlock_t
729 * @sl: Pointer to the seqlock_t instance
731 #define seqlock_init(sl) \
733 seqcount_init(&(sl)->seqcount); \
734 spin_lock_init(&(sl)->lock); \
738 * DEFINE_SEQLOCK() - Define a statically allocated seqlock_t
739 * @sl: Name of the seqlock_t instance
741 #define DEFINE_SEQLOCK(sl) \
742 seqlock_t sl = __SEQLOCK_UNLOCKED(sl)
745 * read_seqbegin() - start a seqlock_t read side critical section
746 * @sl: Pointer to seqlock_t
748 * Return: count, to be passed to read_seqretry()
750 static inline unsigned read_seqbegin(const seqlock_t *sl)
752 unsigned ret = read_seqcount_begin(&sl->seqcount);
754 kcsan_atomic_next(0); /* non-raw usage, assume closing read_seqretry() */
755 kcsan_flat_atomic_begin();
760 * read_seqretry() - end a seqlock_t read side section
761 * @sl: Pointer to seqlock_t
762 * @start: count, from read_seqbegin()
764 * read_seqretry closes the read side critical section of given seqlock_t.
765 * If the critical section was invalid, it must be ignored (and typically
768 * Return: true if a read section retry is required, else false
770 static inline unsigned read_seqretry(const seqlock_t *sl, unsigned start)
773 * Assume not nested: read_seqretry() may be called multiple times when
774 * completing read critical section.
776 kcsan_flat_atomic_end();
778 return read_seqcount_retry(&sl->seqcount, start);
782 * write_seqlock() - start a seqlock_t write side critical section
783 * @sl: Pointer to seqlock_t
785 * write_seqlock opens a write side critical section for the given
786 * seqlock_t. It also implicitly acquires the spinlock_t embedded inside
787 * that sequential lock. All seqlock_t write side sections are thus
788 * automatically serialized and non-preemptible.
790 * Context: if the seqlock_t read section, or other write side critical
791 * sections, can be invoked from hardirq or softirq contexts, use the
792 * _irqsave or _bh variants of this function instead.
794 static inline void write_seqlock(seqlock_t *sl)
796 spin_lock(&sl->lock);
797 write_seqcount_t_begin(&sl->seqcount);
801 * write_sequnlock() - end a seqlock_t write side critical section
802 * @sl: Pointer to seqlock_t
804 * write_sequnlock closes the (serialized and non-preemptible) write side
805 * critical section of given seqlock_t.
807 static inline void write_sequnlock(seqlock_t *sl)
809 write_seqcount_t_end(&sl->seqcount);
810 spin_unlock(&sl->lock);
814 * write_seqlock_bh() - start a softirqs-disabled seqlock_t write section
815 * @sl: Pointer to seqlock_t
817 * _bh variant of write_seqlock(). Use only if the read side section, or
818 * other write side sections, can be invoked from softirq contexts.
820 static inline void write_seqlock_bh(seqlock_t *sl)
822 spin_lock_bh(&sl->lock);
823 write_seqcount_t_begin(&sl->seqcount);
827 * write_sequnlock_bh() - end a softirqs-disabled seqlock_t write section
828 * @sl: Pointer to seqlock_t
830 * write_sequnlock_bh closes the serialized, non-preemptible, and
831 * softirqs-disabled, seqlock_t write side critical section opened with
832 * write_seqlock_bh().
834 static inline void write_sequnlock_bh(seqlock_t *sl)
836 write_seqcount_t_end(&sl->seqcount);
837 spin_unlock_bh(&sl->lock);
841 * write_seqlock_irq() - start a non-interruptible seqlock_t write section
842 * @sl: Pointer to seqlock_t
844 * _irq variant of write_seqlock(). Use only if the read side section, or
845 * other write sections, can be invoked from hardirq contexts.
847 static inline void write_seqlock_irq(seqlock_t *sl)
849 spin_lock_irq(&sl->lock);
850 write_seqcount_t_begin(&sl->seqcount);
854 * write_sequnlock_irq() - end a non-interruptible seqlock_t write section
855 * @sl: Pointer to seqlock_t
857 * write_sequnlock_irq closes the serialized and non-interruptible
858 * seqlock_t write side section opened with write_seqlock_irq().
860 static inline void write_sequnlock_irq(seqlock_t *sl)
862 write_seqcount_t_end(&sl->seqcount);
863 spin_unlock_irq(&sl->lock);
866 static inline unsigned long __write_seqlock_irqsave(seqlock_t *sl)
870 spin_lock_irqsave(&sl->lock, flags);
871 write_seqcount_t_begin(&sl->seqcount);
876 * write_seqlock_irqsave() - start a non-interruptible seqlock_t write
878 * @lock: Pointer to seqlock_t
879 * @flags: Stack-allocated storage for saving caller's local interrupt
880 * state, to be passed to write_sequnlock_irqrestore().
882 * _irqsave variant of write_seqlock(). Use it only if the read side
883 * section, or other write sections, can be invoked from hardirq context.
885 #define write_seqlock_irqsave(lock, flags) \
886 do { flags = __write_seqlock_irqsave(lock); } while (0)
889 * write_sequnlock_irqrestore() - end non-interruptible seqlock_t write
891 * @sl: Pointer to seqlock_t
892 * @flags: Caller's saved interrupt state, from write_seqlock_irqsave()
894 * write_sequnlock_irqrestore closes the serialized and non-interruptible
895 * seqlock_t write section previously opened with write_seqlock_irqsave().
898 write_sequnlock_irqrestore(seqlock_t *sl, unsigned long flags)
900 write_seqcount_t_end(&sl->seqcount);
901 spin_unlock_irqrestore(&sl->lock, flags);
905 * read_seqlock_excl() - begin a seqlock_t locking reader section
906 * @sl: Pointer to seqlock_t
908 * read_seqlock_excl opens a seqlock_t locking reader critical section. A
909 * locking reader exclusively locks out *both* other writers *and* other
910 * locking readers, but it does not update the embedded sequence number.
912 * Locking readers act like a normal spin_lock()/spin_unlock().
914 * Context: if the seqlock_t write section, *or other read sections*, can
915 * be invoked from hardirq or softirq contexts, use the _irqsave or _bh
916 * variant of this function instead.
918 * The opened read section must be closed with read_sequnlock_excl().
920 static inline void read_seqlock_excl(seqlock_t *sl)
922 spin_lock(&sl->lock);
926 * read_sequnlock_excl() - end a seqlock_t locking reader critical section
927 * @sl: Pointer to seqlock_t
929 static inline void read_sequnlock_excl(seqlock_t *sl)
931 spin_unlock(&sl->lock);
935 * read_seqlock_excl_bh() - start a seqlock_t locking reader section with
937 * @sl: Pointer to seqlock_t
939 * _bh variant of read_seqlock_excl(). Use this variant only if the
940 * seqlock_t write side section, *or other read sections*, can be invoked
941 * from softirq contexts.
943 static inline void read_seqlock_excl_bh(seqlock_t *sl)
945 spin_lock_bh(&sl->lock);
949 * read_sequnlock_excl_bh() - stop a seqlock_t softirq-disabled locking
951 * @sl: Pointer to seqlock_t
953 static inline void read_sequnlock_excl_bh(seqlock_t *sl)
955 spin_unlock_bh(&sl->lock);
959 * read_seqlock_excl_irq() - start a non-interruptible seqlock_t locking
961 * @sl: Pointer to seqlock_t
963 * _irq variant of read_seqlock_excl(). Use this only if the seqlock_t
964 * write side section, *or other read sections*, can be invoked from a
967 static inline void read_seqlock_excl_irq(seqlock_t *sl)
969 spin_lock_irq(&sl->lock);
973 * read_sequnlock_excl_irq() - end an interrupts-disabled seqlock_t
974 * locking reader section
975 * @sl: Pointer to seqlock_t
977 static inline void read_sequnlock_excl_irq(seqlock_t *sl)
979 spin_unlock_irq(&sl->lock);
982 static inline unsigned long __read_seqlock_excl_irqsave(seqlock_t *sl)
986 spin_lock_irqsave(&sl->lock, flags);
991 * read_seqlock_excl_irqsave() - start a non-interruptible seqlock_t
992 * locking reader section
993 * @lock: Pointer to seqlock_t
994 * @flags: Stack-allocated storage for saving caller's local interrupt
995 * state, to be passed to read_sequnlock_excl_irqrestore().
997 * _irqsave variant of read_seqlock_excl(). Use this only if the seqlock_t
998 * write side section, *or other read sections*, can be invoked from a
1001 #define read_seqlock_excl_irqsave(lock, flags) \
1002 do { flags = __read_seqlock_excl_irqsave(lock); } while (0)
1005 * read_sequnlock_excl_irqrestore() - end non-interruptible seqlock_t
1006 * locking reader section
1007 * @sl: Pointer to seqlock_t
1008 * @flags: Caller saved interrupt state, from read_seqlock_excl_irqsave()
1011 read_sequnlock_excl_irqrestore(seqlock_t *sl, unsigned long flags)
1013 spin_unlock_irqrestore(&sl->lock, flags);
1017 * read_seqbegin_or_lock() - begin a seqlock_t lockless or locking reader
1018 * @lock: Pointer to seqlock_t
1019 * @seq : Marker and return parameter. If the passed value is even, the
1020 * reader will become a *lockless* seqlock_t reader as in read_seqbegin().
1021 * If the passed value is odd, the reader will become a *locking* reader
1022 * as in read_seqlock_excl(). In the first call to this function, the
1023 * caller *must* initialize and pass an even value to @seq; this way, a
1024 * lockless read can be optimistically tried first.
1026 * read_seqbegin_or_lock is an API designed to optimistically try a normal
1027 * lockless seqlock_t read section first. If an odd counter is found, the
1028 * lockless read trial has failed, and the next read iteration transforms
1029 * itself into a full seqlock_t locking reader.
1031 * This is typically used to avoid seqlock_t lockless readers starvation
1032 * (too much retry loops) in the case of a sharp spike in write side
1035 * Context: if the seqlock_t write section, *or other read sections*, can
1036 * be invoked from hardirq or softirq contexts, use the _irqsave or _bh
1037 * variant of this function instead.
1039 * Check Documentation/locking/seqlock.rst for template example code.
1041 * Return: the encountered sequence counter value, through the @seq
1042 * parameter, which is overloaded as a return parameter. This returned
1043 * value must be checked with need_seqretry(). If the read section need to
1044 * be retried, this returned value must also be passed as the @seq
1045 * parameter of the next read_seqbegin_or_lock() iteration.
1047 static inline void read_seqbegin_or_lock(seqlock_t *lock, int *seq)
1049 if (!(*seq & 1)) /* Even */
1050 *seq = read_seqbegin(lock);
1052 read_seqlock_excl(lock);
1056 * need_seqretry() - validate seqlock_t "locking or lockless" read section
1057 * @lock: Pointer to seqlock_t
1058 * @seq: sequence count, from read_seqbegin_or_lock()
1060 * Return: true if a read section retry is required, false otherwise
1062 static inline int need_seqretry(seqlock_t *lock, int seq)
1064 return !(seq & 1) && read_seqretry(lock, seq);
1068 * done_seqretry() - end seqlock_t "locking or lockless" reader section
1069 * @lock: Pointer to seqlock_t
1070 * @seq: count, from read_seqbegin_or_lock()
1072 * done_seqretry finishes the seqlock_t read side critical section started
1073 * with read_seqbegin_or_lock() and validated by need_seqretry().
1075 static inline void done_seqretry(seqlock_t *lock, int seq)
1078 read_sequnlock_excl(lock);
1082 * read_seqbegin_or_lock_irqsave() - begin a seqlock_t lockless reader, or
1083 * a non-interruptible locking reader
1084 * @lock: Pointer to seqlock_t
1085 * @seq: Marker and return parameter. Check read_seqbegin_or_lock().
1087 * This is the _irqsave variant of read_seqbegin_or_lock(). Use it only if
1088 * the seqlock_t write section, *or other read sections*, can be invoked
1089 * from hardirq context.
1091 * Note: Interrupts will be disabled only for "locking reader" mode.
1095 * 1. The saved local interrupts state in case of a locking reader, to
1096 * be passed to done_seqretry_irqrestore().
1098 * 2. The encountered sequence counter value, returned through @seq
1099 * overloaded as a return parameter. Check read_seqbegin_or_lock().
1101 static inline unsigned long
1102 read_seqbegin_or_lock_irqsave(seqlock_t *lock, int *seq)
1104 unsigned long flags = 0;
1106 if (!(*seq & 1)) /* Even */
1107 *seq = read_seqbegin(lock);
1109 read_seqlock_excl_irqsave(lock, flags);
1115 * done_seqretry_irqrestore() - end a seqlock_t lockless reader, or a
1116 * non-interruptible locking reader section
1117 * @lock: Pointer to seqlock_t
1118 * @seq: Count, from read_seqbegin_or_lock_irqsave()
1119 * @flags: Caller's saved local interrupt state in case of a locking
1120 * reader, also from read_seqbegin_or_lock_irqsave()
1122 * This is the _irqrestore variant of done_seqretry(). The read section
1123 * must've been opened with read_seqbegin_or_lock_irqsave(), and validated
1124 * by need_seqretry().
1127 done_seqretry_irqrestore(seqlock_t *lock, int seq, unsigned long flags)
1130 read_sequnlock_excl_irqrestore(lock, flags);
1132 #endif /* __LINUX_SEQLOCK_H */