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>
22 #include <linux/ww_mutex.h>
24 #include <asm/processor.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
29 * read_seqcount_begin() and read_seqcount_retry(), however, there are more
30 * esoteric cases which do not follow this pattern.
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,
34 * pessimistically mark the next KCSAN_SEQLOCK_REGION_MAX memory accesses as
35 * atomics; if there is a matching read_seqcount_retry() call, no following
36 * memory operations are considered atomic. Usage of the seqlock_t interface
39 #define KCSAN_SEQLOCK_REGION_MAX 1000
42 * Sequence counters (seqcount_t)
44 * This is the raw counting mechanism, without any writer protection.
46 * Write side critical sections must be serialized and non-preemptible.
48 * If readers can be invoked from hardirq or softirq contexts,
49 * interrupts or bottom halves must also be respectively disabled before
50 * entering the write section.
52 * This mechanism can't be used if the protected data contains pointers,
53 * as the writer can invalidate a pointer that a reader is following.
55 * If the write serialization mechanism is one of the common kernel
56 * locking primitives, use a sequence counter with associated lock
57 * (seqcount_LOCKTYPE_t) instead.
59 * If it's desired to automatically handle the sequence counter writer
60 * serialization and non-preemptibility requirements, use a sequential
61 * lock (seqlock_t) instead.
63 * See Documentation/locking/seqlock.rst
65 typedef struct seqcount {
67 #ifdef CONFIG_DEBUG_LOCK_ALLOC
68 struct lockdep_map dep_map;
72 static inline void __seqcount_init(seqcount_t *s, const char *name,
73 struct lock_class_key *key)
76 * Make sure we are not reinitializing a held lock:
78 lockdep_init_map(&s->dep_map, name, key, 0);
82 #ifdef CONFIG_DEBUG_LOCK_ALLOC
84 # define SEQCOUNT_DEP_MAP_INIT(lockname) \
85 .dep_map = { .name = #lockname }
88 * seqcount_init() - runtime initializer for seqcount_t
89 * @s: Pointer to the seqcount_t instance
91 # define seqcount_init(s) \
93 static struct lock_class_key __key; \
94 __seqcount_init((s), #s, &__key); \
97 static inline void seqcount_lockdep_reader_access(const seqcount_t *s)
99 seqcount_t *l = (seqcount_t *)s;
102 local_irq_save(flags);
103 seqcount_acquire_read(&l->dep_map, 0, 0, _RET_IP_);
104 seqcount_release(&l->dep_map, _RET_IP_);
105 local_irq_restore(flags);
109 # define SEQCOUNT_DEP_MAP_INIT(lockname)
110 # define seqcount_init(s) __seqcount_init(s, NULL, NULL)
111 # define seqcount_lockdep_reader_access(x)
115 * SEQCNT_ZERO() - static initializer for seqcount_t
116 * @name: Name of the seqcount_t instance
118 #define SEQCNT_ZERO(name) { .sequence = 0, SEQCOUNT_DEP_MAP_INIT(name) }
121 * Sequence counters with associated locks (seqcount_LOCKTYPE_t)
123 * A sequence counter which associates the lock used for writer
124 * serialization at initialization time. This enables lockdep to validate
125 * that the write side critical section is properly serialized.
127 * For associated locks which do not implicitly disable preemption,
128 * preemption protection is enforced in the write side function.
130 * Lockdep is never used in any for the raw write variants.
132 * See Documentation/locking/seqlock.rst
135 #ifdef CONFIG_LOCKDEP
136 #define __SEQ_LOCK(expr) expr
138 #define __SEQ_LOCK(expr)
142 * typedef seqcount_LOCKNAME_t - sequence counter with LOCKTYPR associated
143 * @seqcount: The real sequence counter
144 * @lock: Pointer to the associated spinlock
146 * A plain sequence counter with external writer synchronization by a
147 * spinlock. The spinlock is associated to the sequence count in the
148 * static initializer or init function. This enables lockdep to validate
149 * that the write side critical section is properly serialized.
153 * seqcount_LOCKNAME_init() - runtime initializer for seqcount_LOCKNAME_t
154 * @s: Pointer to the seqcount_LOCKNAME_t instance
155 * @lock: Pointer to the associated LOCKTYPE
159 * SEQCOUNT_LOCKTYPE() - Instantiate seqcount_LOCKNAME_t and helpers
160 * @locktype: actual typename
162 * @preemptible: preemptibility of above locktype
163 * @lockmember: argument for lockdep_assert_held()
165 #define SEQCOUNT_LOCKTYPE(locktype, lockname, preemptible, lockmember) \
166 typedef struct seqcount_##lockname { \
167 seqcount_t seqcount; \
168 __SEQ_LOCK(locktype *lock); \
169 } seqcount_##lockname##_t; \
171 static __always_inline void \
172 seqcount_##lockname##_init(seqcount_##lockname##_t *s, locktype *lock) \
174 seqcount_init(&s->seqcount); \
175 __SEQ_LOCK(s->lock = lock); \
178 static __always_inline seqcount_t * \
179 __seqcount_##lockname##_ptr(seqcount_##lockname##_t *s) \
181 return &s->seqcount; \
184 static __always_inline bool \
185 __seqcount_##lockname##_preemptible(seqcount_##lockname##_t *s) \
187 return preemptible; \
190 static __always_inline void \
191 __seqcount_##lockname##_assert(seqcount_##lockname##_t *s) \
193 __SEQ_LOCK(lockdep_assert_held(lockmember)); \
197 * __seqprop() for seqcount_t
200 static inline seqcount_t *__seqcount_ptr(seqcount_t *s)
205 static inline bool __seqcount_preemptible(seqcount_t *s)
210 static inline void __seqcount_assert(seqcount_t *s)
212 lockdep_assert_preemption_disabled();
215 SEQCOUNT_LOCKTYPE(raw_spinlock_t, raw_spinlock, false, s->lock)
216 SEQCOUNT_LOCKTYPE(spinlock_t, spinlock, false, s->lock)
217 SEQCOUNT_LOCKTYPE(rwlock_t, rwlock, false, s->lock)
218 SEQCOUNT_LOCKTYPE(struct mutex, mutex, true, s->lock)
219 SEQCOUNT_LOCKTYPE(struct ww_mutex, ww_mutex, true, &s->lock->base)
222 * SEQCNT_LOCKNAME_ZERO - static initializer for seqcount_LOCKNAME_t
223 * @name: Name of the seqcount_LOCKNAME_t instance
224 * @lock: Pointer to the associated LOCKTYPE
227 #define SEQCOUNT_LOCKTYPE_ZERO(seq_name, assoc_lock) { \
228 .seqcount = SEQCNT_ZERO(seq_name.seqcount), \
229 __SEQ_LOCK(.lock = (assoc_lock)) \
232 #define SEQCNT_SPINLOCK_ZERO(name, lock) SEQCOUNT_LOCKTYPE_ZERO(name, lock)
233 #define SEQCNT_RAW_SPINLOCK_ZERO(name, lock) SEQCOUNT_LOCKTYPE_ZERO(name, lock)
234 #define SEQCNT_RWLOCK_ZERO(name, lock) SEQCOUNT_LOCKTYPE_ZERO(name, lock)
235 #define SEQCNT_MUTEX_ZERO(name, lock) SEQCOUNT_LOCKTYPE_ZERO(name, lock)
236 #define SEQCNT_WW_MUTEX_ZERO(name, lock) SEQCOUNT_LOCKTYPE_ZERO(name, lock)
239 #define __seqprop_case(s, lockname, prop) \
240 seqcount_##lockname##_t: __seqcount_##lockname##_##prop((void *)(s))
242 #define __seqprop(s, prop) _Generic(*(s), \
243 seqcount_t: __seqcount_##prop((void *)(s)), \
244 __seqprop_case((s), raw_spinlock, prop), \
245 __seqprop_case((s), spinlock, prop), \
246 __seqprop_case((s), rwlock, prop), \
247 __seqprop_case((s), mutex, prop), \
248 __seqprop_case((s), ww_mutex, prop))
250 #define __to_seqcount_t(s) __seqprop(s, ptr)
251 #define __associated_lock_exists_and_is_preemptible(s) __seqprop(s, preemptible)
252 #define __assert_write_section_is_protected(s) __seqprop(s, assert)
255 * __read_seqcount_begin() - begin a seqcount_t read section w/o barrier
256 * @s: Pointer to seqcount_t or any of the seqcount_locktype_t variants
258 * __read_seqcount_begin is like read_seqcount_begin, but has no smp_rmb()
259 * barrier. Callers should ensure that smp_rmb() or equivalent ordering is
260 * provided before actually loading any of the variables that are to be
261 * protected in this critical section.
263 * Use carefully, only in critical code, and comment how the barrier is
266 * Return: count to be passed to read_seqcount_retry()
268 #define __read_seqcount_begin(s) \
269 __read_seqcount_t_begin(__to_seqcount_t(s))
271 static inline unsigned __read_seqcount_t_begin(const seqcount_t *s)
276 ret = READ_ONCE(s->sequence);
277 if (unlikely(ret & 1)) {
281 kcsan_atomic_next(KCSAN_SEQLOCK_REGION_MAX);
286 * raw_read_seqcount_begin() - begin a seqcount_t read section w/o lockdep
287 * @s: Pointer to seqcount_t or any of the seqcount_locktype_t variants
289 * Return: count to be passed to read_seqcount_retry()
291 #define raw_read_seqcount_begin(s) \
292 raw_read_seqcount_t_begin(__to_seqcount_t(s))
294 static inline unsigned raw_read_seqcount_t_begin(const seqcount_t *s)
296 unsigned ret = __read_seqcount_t_begin(s);
302 * read_seqcount_begin() - begin a seqcount_t read critical section
303 * @s: Pointer to seqcount_t or any of the seqcount_locktype_t variants
305 * Return: count to be passed to read_seqcount_retry()
307 #define read_seqcount_begin(s) \
308 read_seqcount_t_begin(__to_seqcount_t(s))
310 static inline unsigned read_seqcount_t_begin(const seqcount_t *s)
312 seqcount_lockdep_reader_access(s);
313 return raw_read_seqcount_t_begin(s);
317 * raw_read_seqcount() - read the raw seqcount_t counter value
318 * @s: Pointer to seqcount_t or any of the seqcount_locktype_t variants
320 * raw_read_seqcount opens a read critical section of the given
321 * seqcount_t, without any lockdep checking, and without checking or
322 * masking the sequence counter LSB. Calling code is responsible for
325 * Return: count to be passed to read_seqcount_retry()
327 #define raw_read_seqcount(s) \
328 raw_read_seqcount_t(__to_seqcount_t(s))
330 static inline unsigned raw_read_seqcount_t(const seqcount_t *s)
332 unsigned ret = READ_ONCE(s->sequence);
334 kcsan_atomic_next(KCSAN_SEQLOCK_REGION_MAX);
339 * raw_seqcount_begin() - begin a seqcount_t read critical section w/o
340 * lockdep and w/o counter stabilization
341 * @s: Pointer to seqcount_t or any of the seqcount_locktype_t variants
343 * raw_seqcount_begin opens a read critical section of the given
344 * seqcount_t. Unlike read_seqcount_begin(), this function will not wait
345 * for the count to stabilize. If a writer is active when it begins, it
346 * will fail the read_seqcount_retry() at the end of the read critical
347 * section instead of stabilizing at the beginning of it.
349 * Use this only in special kernel hot paths where the read section is
350 * small and has a high probability of success through other external
351 * means. It will save a single branching instruction.
353 * Return: count to be passed to read_seqcount_retry()
355 #define raw_seqcount_begin(s) \
356 raw_seqcount_t_begin(__to_seqcount_t(s))
358 static inline unsigned raw_seqcount_t_begin(const seqcount_t *s)
361 * If the counter is odd, let read_seqcount_retry() fail
362 * by decrementing the counter.
364 return raw_read_seqcount_t(s) & ~1;
368 * __read_seqcount_retry() - end a seqcount_t read section w/o barrier
369 * @s: Pointer to seqcount_t or any of the seqcount_locktype_t variants
370 * @start: count, from read_seqcount_begin()
372 * __read_seqcount_retry is like read_seqcount_retry, but has no smp_rmb()
373 * barrier. Callers should ensure that smp_rmb() or equivalent ordering is
374 * provided before actually loading any of the variables that are to be
375 * protected in this critical section.
377 * Use carefully, only in critical code, and comment how the barrier is
380 * Return: true if a read section retry is required, else false
382 #define __read_seqcount_retry(s, start) \
383 __read_seqcount_t_retry(__to_seqcount_t(s), start)
385 static inline int __read_seqcount_t_retry(const seqcount_t *s, unsigned start)
387 kcsan_atomic_next(0);
388 return unlikely(READ_ONCE(s->sequence) != start);
392 * read_seqcount_retry() - end a seqcount_t read critical section
393 * @s: Pointer to seqcount_t or any of the seqcount_locktype_t variants
394 * @start: count, from read_seqcount_begin()
396 * read_seqcount_retry closes the read critical section of given
397 * seqcount_t. If the critical section was invalid, it must be ignored
398 * (and typically retried).
400 * Return: true if a read section retry is required, else false
402 #define read_seqcount_retry(s, start) \
403 read_seqcount_t_retry(__to_seqcount_t(s), start)
405 static inline int read_seqcount_t_retry(const seqcount_t *s, unsigned start)
408 return __read_seqcount_t_retry(s, start);
412 * raw_write_seqcount_begin() - start a seqcount_t write section w/o lockdep
413 * @s: Pointer to seqcount_t or any of the seqcount_locktype_t variants
415 #define raw_write_seqcount_begin(s) \
417 if (__associated_lock_exists_and_is_preemptible(s)) \
420 raw_write_seqcount_t_begin(__to_seqcount_t(s)); \
423 static inline void raw_write_seqcount_t_begin(seqcount_t *s)
425 kcsan_nestable_atomic_begin();
431 * raw_write_seqcount_end() - end a seqcount_t write section w/o lockdep
432 * @s: Pointer to seqcount_t or any of the seqcount_locktype_t variants
434 #define raw_write_seqcount_end(s) \
436 raw_write_seqcount_t_end(__to_seqcount_t(s)); \
438 if (__associated_lock_exists_and_is_preemptible(s)) \
442 static inline void raw_write_seqcount_t_end(seqcount_t *s)
446 kcsan_nestable_atomic_end();
450 * write_seqcount_begin_nested() - start a seqcount_t write section with
451 * custom lockdep nesting level
452 * @s: Pointer to seqcount_t or any of the seqcount_locktype_t variants
453 * @subclass: lockdep nesting level
455 * See Documentation/locking/lockdep-design.rst
457 #define write_seqcount_begin_nested(s, subclass) \
459 __assert_write_section_is_protected(s); \
461 if (__associated_lock_exists_and_is_preemptible(s)) \
464 write_seqcount_t_begin_nested(__to_seqcount_t(s), subclass); \
467 static inline void write_seqcount_t_begin_nested(seqcount_t *s, int subclass)
469 raw_write_seqcount_t_begin(s);
470 seqcount_acquire(&s->dep_map, subclass, 0, _RET_IP_);
474 * write_seqcount_begin() - start a seqcount_t write side critical section
475 * @s: Pointer to seqcount_t or any of the seqcount_locktype_t variants
477 * write_seqcount_begin opens a write side critical section of the given
480 * Context: seqcount_t write side critical sections must be serialized and
481 * non-preemptible. If readers can be invoked from hardirq or softirq
482 * context, interrupts or bottom halves must be respectively disabled.
484 #define write_seqcount_begin(s) \
486 __assert_write_section_is_protected(s); \
488 if (__associated_lock_exists_and_is_preemptible(s)) \
491 write_seqcount_t_begin(__to_seqcount_t(s)); \
494 static inline void write_seqcount_t_begin(seqcount_t *s)
496 write_seqcount_t_begin_nested(s, 0);
500 * write_seqcount_end() - end a seqcount_t write side critical section
501 * @s: Pointer to seqcount_t or any of the seqcount_locktype_t variants
503 * The write section must've been opened with write_seqcount_begin().
505 #define write_seqcount_end(s) \
507 write_seqcount_t_end(__to_seqcount_t(s)); \
509 if (__associated_lock_exists_and_is_preemptible(s)) \
513 static inline void write_seqcount_t_end(seqcount_t *s)
515 seqcount_release(&s->dep_map, _RET_IP_);
516 raw_write_seqcount_t_end(s);
520 * raw_write_seqcount_barrier() - do a seqcount_t write barrier
521 * @s: Pointer to seqcount_t or any of the seqcount_locktype_t variants
523 * This can be used to provide an ordering guarantee instead of the usual
524 * consistency guarantee. It is one wmb cheaper, because it can collapse
525 * the two back-to-back wmb()s.
527 * Note that writes surrounding the barrier should be declared atomic (e.g.
528 * via WRITE_ONCE): a) to ensure the writes become visible to other threads
529 * atomically, avoiding compiler optimizations; b) to document which writes are
530 * meant to propagate to the reader critical section. This is necessary because
531 * neither writes before and after the barrier are enclosed in a seq-writer
532 * critical section that would ensure readers are aware of ongoing writes::
535 * bool X = true, Y = false;
542 * int s = read_seqcount_begin(&seq);
546 * } while (read_seqcount_retry(&seq, s));
553 * WRITE_ONCE(Y, true);
555 * raw_write_seqcount_barrier(seq);
557 * WRITE_ONCE(X, false);
560 #define raw_write_seqcount_barrier(s) \
561 raw_write_seqcount_t_barrier(__to_seqcount_t(s))
563 static inline void raw_write_seqcount_t_barrier(seqcount_t *s)
565 kcsan_nestable_atomic_begin();
569 kcsan_nestable_atomic_end();
573 * write_seqcount_invalidate() - invalidate in-progress seqcount_t read
575 * @s: Pointer to seqcount_t or any of the seqcount_locktype_t variants
577 * After write_seqcount_invalidate, no seqcount_t read side operations
578 * will complete successfully and see data older than this.
580 #define write_seqcount_invalidate(s) \
581 write_seqcount_t_invalidate(__to_seqcount_t(s))
583 static inline void write_seqcount_t_invalidate(seqcount_t *s)
586 kcsan_nestable_atomic_begin();
588 kcsan_nestable_atomic_end();
592 * raw_read_seqcount_latch() - pick even/odd seqcount_t latch data copy
593 * @s: Pointer to seqcount_t or any of the seqcount_locktype_t variants
595 * Use seqcount_t latching to switch between two storage places protected
596 * by a sequence counter. Doing so allows having interruptible, preemptible,
597 * seqcount_t write side critical sections.
599 * Check raw_write_seqcount_latch() for more details and a full reader and
600 * writer usage example.
602 * Return: sequence counter raw value. Use the lowest bit as an index for
603 * picking which data copy to read. The full counter value must then be
604 * checked with read_seqcount_retry().
606 #define raw_read_seqcount_latch(s) \
607 raw_read_seqcount_t_latch(__to_seqcount_t(s))
609 static inline int raw_read_seqcount_t_latch(seqcount_t *s)
611 /* Pairs with the first smp_wmb() in raw_write_seqcount_latch() */
612 int seq = READ_ONCE(s->sequence); /* ^^^ */
617 * raw_write_seqcount_latch() - redirect readers to even/odd copy
618 * @s: Pointer to seqcount_t or any of the seqcount_locktype_t variants
620 * The latch technique is a multiversion concurrency control method that allows
621 * queries during non-atomic modifications. If you can guarantee queries never
622 * interrupt the modification -- e.g. the concurrency is strictly between CPUs
623 * -- you most likely do not need this.
625 * Where the traditional RCU/lockless data structures rely on atomic
626 * modifications to ensure queries observe either the old or the new state the
627 * latch allows the same for non-atomic updates. The trade-off is doubling the
628 * cost of storage; we have to maintain two copies of the entire data
631 * Very simply put: we first modify one copy and then the other. This ensures
632 * there is always one copy in a stable state, ready to give us an answer.
634 * The basic form is a data structure like::
636 * struct latch_struct {
638 * struct data_struct data[2];
641 * Where a modification, which is assumed to be externally serialized, does the
644 * void latch_modify(struct latch_struct *latch, ...)
646 * smp_wmb(); // Ensure that the last data[1] update is visible
648 * smp_wmb(); // Ensure that the seqcount update is visible
650 * modify(latch->data[0], ...);
652 * smp_wmb(); // Ensure that the data[0] update is visible
654 * smp_wmb(); // Ensure that the seqcount update is visible
656 * modify(latch->data[1], ...);
659 * The query will have a form like::
661 * struct entry *latch_query(struct latch_struct *latch, ...)
663 * struct entry *entry;
667 * seq = raw_read_seqcount_latch(&latch->seq);
670 * entry = data_query(latch->data[idx], ...);
672 * // read_seqcount_retry() includes needed smp_rmb()
673 * } while (read_seqcount_retry(&latch->seq, seq));
678 * So during the modification, queries are first redirected to data[1]. Then we
679 * modify data[0]. When that is complete, we redirect queries back to data[0]
680 * and we can modify data[1].
684 * The non-requirement for atomic modifications does _NOT_ include
685 * the publishing of new entries in the case where data is a dynamic
688 * An iteration might start in data[0] and get suspended long enough
689 * to miss an entire modification sequence, once it resumes it might
690 * observe the new entry.
694 * When data is a dynamic data structure; one should use regular RCU
695 * patterns to manage the lifetimes of the objects within.
697 #define raw_write_seqcount_latch(s) \
698 raw_write_seqcount_t_latch(__to_seqcount_t(s))
700 static inline void raw_write_seqcount_t_latch(seqcount_t *s)
702 smp_wmb(); /* prior stores before incrementing "sequence" */
704 smp_wmb(); /* increment "sequence" before following stores */
708 * Sequential locks (seqlock_t)
710 * Sequence counters with an embedded spinlock for writer serialization
711 * and non-preemptibility.
713 * For more info, see:
714 * - Comments on top of seqcount_t
715 * - Documentation/locking/seqlock.rst
718 struct seqcount seqcount;
722 #define __SEQLOCK_UNLOCKED(lockname) \
724 .seqcount = SEQCNT_ZERO(lockname), \
725 .lock = __SPIN_LOCK_UNLOCKED(lockname) \
729 * seqlock_init() - dynamic initializer for seqlock_t
730 * @sl: Pointer to the seqlock_t instance
732 #define seqlock_init(sl) \
734 seqcount_init(&(sl)->seqcount); \
735 spin_lock_init(&(sl)->lock); \
739 * DEFINE_SEQLOCK() - Define a statically allocated seqlock_t
740 * @sl: Name of the seqlock_t instance
742 #define DEFINE_SEQLOCK(sl) \
743 seqlock_t sl = __SEQLOCK_UNLOCKED(sl)
746 * read_seqbegin() - start a seqlock_t read side critical section
747 * @sl: Pointer to seqlock_t
749 * Return: count, to be passed to read_seqretry()
751 static inline unsigned read_seqbegin(const seqlock_t *sl)
753 unsigned ret = read_seqcount_begin(&sl->seqcount);
755 kcsan_atomic_next(0); /* non-raw usage, assume closing read_seqretry() */
756 kcsan_flat_atomic_begin();
761 * read_seqretry() - end a seqlock_t read side section
762 * @sl: Pointer to seqlock_t
763 * @start: count, from read_seqbegin()
765 * read_seqretry closes the read side critical section of given seqlock_t.
766 * If the critical section was invalid, it must be ignored (and typically
769 * Return: true if a read section retry is required, else false
771 static inline unsigned read_seqretry(const seqlock_t *sl, unsigned start)
774 * Assume not nested: read_seqretry() may be called multiple times when
775 * completing read critical section.
777 kcsan_flat_atomic_end();
779 return read_seqcount_retry(&sl->seqcount, start);
783 * write_seqlock() - start a seqlock_t write side critical section
784 * @sl: Pointer to seqlock_t
786 * write_seqlock opens a write side critical section for the given
787 * seqlock_t. It also implicitly acquires the spinlock_t embedded inside
788 * that sequential lock. All seqlock_t write side sections are thus
789 * automatically serialized and non-preemptible.
791 * Context: if the seqlock_t read section, or other write side critical
792 * sections, can be invoked from hardirq or softirq contexts, use the
793 * _irqsave or _bh variants of this function instead.
795 static inline void write_seqlock(seqlock_t *sl)
797 spin_lock(&sl->lock);
798 write_seqcount_t_begin(&sl->seqcount);
802 * write_sequnlock() - end a seqlock_t write side critical section
803 * @sl: Pointer to seqlock_t
805 * write_sequnlock closes the (serialized and non-preemptible) write side
806 * critical section of given seqlock_t.
808 static inline void write_sequnlock(seqlock_t *sl)
810 write_seqcount_t_end(&sl->seqcount);
811 spin_unlock(&sl->lock);
815 * write_seqlock_bh() - start a softirqs-disabled seqlock_t write section
816 * @sl: Pointer to seqlock_t
818 * _bh variant of write_seqlock(). Use only if the read side section, or
819 * other write side sections, can be invoked from softirq contexts.
821 static inline void write_seqlock_bh(seqlock_t *sl)
823 spin_lock_bh(&sl->lock);
824 write_seqcount_t_begin(&sl->seqcount);
828 * write_sequnlock_bh() - end a softirqs-disabled seqlock_t write section
829 * @sl: Pointer to seqlock_t
831 * write_sequnlock_bh closes the serialized, non-preemptible, and
832 * softirqs-disabled, seqlock_t write side critical section opened with
833 * write_seqlock_bh().
835 static inline void write_sequnlock_bh(seqlock_t *sl)
837 write_seqcount_t_end(&sl->seqcount);
838 spin_unlock_bh(&sl->lock);
842 * write_seqlock_irq() - start a non-interruptible seqlock_t write section
843 * @sl: Pointer to seqlock_t
845 * _irq variant of write_seqlock(). Use only if the read side section, or
846 * other write sections, can be invoked from hardirq contexts.
848 static inline void write_seqlock_irq(seqlock_t *sl)
850 spin_lock_irq(&sl->lock);
851 write_seqcount_t_begin(&sl->seqcount);
855 * write_sequnlock_irq() - end a non-interruptible seqlock_t write section
856 * @sl: Pointer to seqlock_t
858 * write_sequnlock_irq closes the serialized and non-interruptible
859 * seqlock_t write side section opened with write_seqlock_irq().
861 static inline void write_sequnlock_irq(seqlock_t *sl)
863 write_seqcount_t_end(&sl->seqcount);
864 spin_unlock_irq(&sl->lock);
867 static inline unsigned long __write_seqlock_irqsave(seqlock_t *sl)
871 spin_lock_irqsave(&sl->lock, flags);
872 write_seqcount_t_begin(&sl->seqcount);
877 * write_seqlock_irqsave() - start a non-interruptible seqlock_t write
879 * @lock: Pointer to seqlock_t
880 * @flags: Stack-allocated storage for saving caller's local interrupt
881 * state, to be passed to write_sequnlock_irqrestore().
883 * _irqsave variant of write_seqlock(). Use it only if the read side
884 * section, or other write sections, can be invoked from hardirq context.
886 #define write_seqlock_irqsave(lock, flags) \
887 do { flags = __write_seqlock_irqsave(lock); } while (0)
890 * write_sequnlock_irqrestore() - end non-interruptible seqlock_t write
892 * @sl: Pointer to seqlock_t
893 * @flags: Caller's saved interrupt state, from write_seqlock_irqsave()
895 * write_sequnlock_irqrestore closes the serialized and non-interruptible
896 * seqlock_t write section previously opened with write_seqlock_irqsave().
899 write_sequnlock_irqrestore(seqlock_t *sl, unsigned long flags)
901 write_seqcount_t_end(&sl->seqcount);
902 spin_unlock_irqrestore(&sl->lock, flags);
906 * read_seqlock_excl() - begin a seqlock_t locking reader section
907 * @sl: Pointer to seqlock_t
909 * read_seqlock_excl opens a seqlock_t locking reader critical section. A
910 * locking reader exclusively locks out *both* other writers *and* other
911 * locking readers, but it does not update the embedded sequence number.
913 * Locking readers act like a normal spin_lock()/spin_unlock().
915 * Context: if the seqlock_t write section, *or other read sections*, can
916 * be invoked from hardirq or softirq contexts, use the _irqsave or _bh
917 * variant of this function instead.
919 * The opened read section must be closed with read_sequnlock_excl().
921 static inline void read_seqlock_excl(seqlock_t *sl)
923 spin_lock(&sl->lock);
927 * read_sequnlock_excl() - end a seqlock_t locking reader critical section
928 * @sl: Pointer to seqlock_t
930 static inline void read_sequnlock_excl(seqlock_t *sl)
932 spin_unlock(&sl->lock);
936 * read_seqlock_excl_bh() - start a seqlock_t locking reader section with
938 * @sl: Pointer to seqlock_t
940 * _bh variant of read_seqlock_excl(). Use this variant only if the
941 * seqlock_t write side section, *or other read sections*, can be invoked
942 * from softirq contexts.
944 static inline void read_seqlock_excl_bh(seqlock_t *sl)
946 spin_lock_bh(&sl->lock);
950 * read_sequnlock_excl_bh() - stop a seqlock_t softirq-disabled locking
952 * @sl: Pointer to seqlock_t
954 static inline void read_sequnlock_excl_bh(seqlock_t *sl)
956 spin_unlock_bh(&sl->lock);
960 * read_seqlock_excl_irq() - start a non-interruptible seqlock_t locking
962 * @sl: Pointer to seqlock_t
964 * _irq variant of read_seqlock_excl(). Use this only if the seqlock_t
965 * write side section, *or other read sections*, can be invoked from a
968 static inline void read_seqlock_excl_irq(seqlock_t *sl)
970 spin_lock_irq(&sl->lock);
974 * read_sequnlock_excl_irq() - end an interrupts-disabled seqlock_t
975 * locking reader section
976 * @sl: Pointer to seqlock_t
978 static inline void read_sequnlock_excl_irq(seqlock_t *sl)
980 spin_unlock_irq(&sl->lock);
983 static inline unsigned long __read_seqlock_excl_irqsave(seqlock_t *sl)
987 spin_lock_irqsave(&sl->lock, flags);
992 * read_seqlock_excl_irqsave() - start a non-interruptible seqlock_t
993 * locking reader section
994 * @lock: Pointer to seqlock_t
995 * @flags: Stack-allocated storage for saving caller's local interrupt
996 * state, to be passed to read_sequnlock_excl_irqrestore().
998 * _irqsave variant of read_seqlock_excl(). Use this only if the seqlock_t
999 * write side section, *or other read sections*, can be invoked from a
1002 #define read_seqlock_excl_irqsave(lock, flags) \
1003 do { flags = __read_seqlock_excl_irqsave(lock); } while (0)
1006 * read_sequnlock_excl_irqrestore() - end non-interruptible seqlock_t
1007 * locking reader section
1008 * @sl: Pointer to seqlock_t
1009 * @flags: Caller saved interrupt state, from read_seqlock_excl_irqsave()
1012 read_sequnlock_excl_irqrestore(seqlock_t *sl, unsigned long flags)
1014 spin_unlock_irqrestore(&sl->lock, flags);
1018 * read_seqbegin_or_lock() - begin a seqlock_t lockless or locking reader
1019 * @lock: Pointer to seqlock_t
1020 * @seq : Marker and return parameter. If the passed value is even, the
1021 * reader will become a *lockless* seqlock_t reader as in read_seqbegin().
1022 * If the passed value is odd, the reader will become a *locking* reader
1023 * as in read_seqlock_excl(). In the first call to this function, the
1024 * caller *must* initialize and pass an even value to @seq; this way, a
1025 * lockless read can be optimistically tried first.
1027 * read_seqbegin_or_lock is an API designed to optimistically try a normal
1028 * lockless seqlock_t read section first. If an odd counter is found, the
1029 * lockless read trial has failed, and the next read iteration transforms
1030 * itself into a full seqlock_t locking reader.
1032 * This is typically used to avoid seqlock_t lockless readers starvation
1033 * (too much retry loops) in the case of a sharp spike in write side
1036 * Context: if the seqlock_t write section, *or other read sections*, can
1037 * be invoked from hardirq or softirq contexts, use the _irqsave or _bh
1038 * variant of this function instead.
1040 * Check Documentation/locking/seqlock.rst for template example code.
1042 * Return: the encountered sequence counter value, through the @seq
1043 * parameter, which is overloaded as a return parameter. This returned
1044 * value must be checked with need_seqretry(). If the read section need to
1045 * be retried, this returned value must also be passed as the @seq
1046 * parameter of the next read_seqbegin_or_lock() iteration.
1048 static inline void read_seqbegin_or_lock(seqlock_t *lock, int *seq)
1050 if (!(*seq & 1)) /* Even */
1051 *seq = read_seqbegin(lock);
1053 read_seqlock_excl(lock);
1057 * need_seqretry() - validate seqlock_t "locking or lockless" read section
1058 * @lock: Pointer to seqlock_t
1059 * @seq: sequence count, from read_seqbegin_or_lock()
1061 * Return: true if a read section retry is required, false otherwise
1063 static inline int need_seqretry(seqlock_t *lock, int seq)
1065 return !(seq & 1) && read_seqretry(lock, seq);
1069 * done_seqretry() - end seqlock_t "locking or lockless" reader section
1070 * @lock: Pointer to seqlock_t
1071 * @seq: count, from read_seqbegin_or_lock()
1073 * done_seqretry finishes the seqlock_t read side critical section started
1074 * with read_seqbegin_or_lock() and validated by need_seqretry().
1076 static inline void done_seqretry(seqlock_t *lock, int seq)
1079 read_sequnlock_excl(lock);
1083 * read_seqbegin_or_lock_irqsave() - begin a seqlock_t lockless reader, or
1084 * a non-interruptible locking reader
1085 * @lock: Pointer to seqlock_t
1086 * @seq: Marker and return parameter. Check read_seqbegin_or_lock().
1088 * This is the _irqsave variant of read_seqbegin_or_lock(). Use it only if
1089 * the seqlock_t write section, *or other read sections*, can be invoked
1090 * from hardirq context.
1092 * Note: Interrupts will be disabled only for "locking reader" mode.
1096 * 1. The saved local interrupts state in case of a locking reader, to
1097 * be passed to done_seqretry_irqrestore().
1099 * 2. The encountered sequence counter value, returned through @seq
1100 * overloaded as a return parameter. Check read_seqbegin_or_lock().
1102 static inline unsigned long
1103 read_seqbegin_or_lock_irqsave(seqlock_t *lock, int *seq)
1105 unsigned long flags = 0;
1107 if (!(*seq & 1)) /* Even */
1108 *seq = read_seqbegin(lock);
1110 read_seqlock_excl_irqsave(lock, flags);
1116 * done_seqretry_irqrestore() - end a seqlock_t lockless reader, or a
1117 * non-interruptible locking reader section
1118 * @lock: Pointer to seqlock_t
1119 * @seq: Count, from read_seqbegin_or_lock_irqsave()
1120 * @flags: Caller's saved local interrupt state in case of a locking
1121 * reader, also from read_seqbegin_or_lock_irqsave()
1123 * This is the _irqrestore variant of done_seqretry(). The read section
1124 * must've been opened with read_seqbegin_or_lock_irqsave(), and validated
1125 * by need_seqretry().
1128 done_seqretry_irqrestore(seqlock_t *lock, int seq, unsigned long flags)
1131 read_sequnlock_excl_irqrestore(lock, flags);
1133 #endif /* __LINUX_SEQLOCK_H */