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)
141 #define SEQCOUNT_LOCKTYPE_ZERO(seq_name, assoc_lock) { \
142 .seqcount = SEQCNT_ZERO(seq_name.seqcount), \
143 __SEQ_LOCK(.lock = (assoc_lock)) \
147 * SEQCNT_SPINLOCK_ZERO - static initializer for seqcount_spinlock_t
148 * @name: Name of the seqcount_spinlock_t instance
149 * @lock: Pointer to the associated spinlock
151 #define SEQCNT_SPINLOCK_ZERO(name, lock) \
152 SEQCOUNT_LOCKTYPE_ZERO(name, lock)
155 * SEQCNT_RAW_SPINLOCK_ZERO - static initializer for seqcount_raw_spinlock_t
156 * @name: Name of the seqcount_raw_spinlock_t instance
157 * @lock: Pointer to the associated raw_spinlock
159 #define SEQCNT_RAW_SPINLOCK_ZERO(name, lock) \
160 SEQCOUNT_LOCKTYPE_ZERO(name, lock)
163 * SEQCNT_RWLOCK_ZERO - static initializer for seqcount_rwlock_t
164 * @name: Name of the seqcount_rwlock_t instance
165 * @lock: Pointer to the associated rwlock
167 #define SEQCNT_RWLOCK_ZERO(name, lock) \
168 SEQCOUNT_LOCKTYPE_ZERO(name, lock)
171 * SEQCNT_MUTEX_ZERO - static initializer for seqcount_mutex_t
172 * @name: Name of the seqcount_mutex_t instance
173 * @lock: Pointer to the associated mutex
175 #define SEQCNT_MUTEX_ZERO(name, lock) \
176 SEQCOUNT_LOCKTYPE_ZERO(name, lock)
179 * SEQCNT_WW_MUTEX_ZERO - static initializer for seqcount_ww_mutex_t
180 * @name: Name of the seqcount_ww_mutex_t instance
181 * @lock: Pointer to the associated ww_mutex
183 #define SEQCNT_WW_MUTEX_ZERO(name, lock) \
184 SEQCOUNT_LOCKTYPE_ZERO(name, lock)
187 * typedef seqcount_LOCKNAME_t - sequence counter with LOCKTYPR associated
188 * @seqcount: The real sequence counter
189 * @lock: Pointer to the associated spinlock
191 * A plain sequence counter with external writer synchronization by a
192 * spinlock. The spinlock is associated to the sequence count in the
193 * static initializer or init function. This enables lockdep to validate
194 * that the write side critical section is properly serialized.
198 * seqcount_LOCKNAME_init() - runtime initializer for seqcount_LOCKNAME_t
199 * @s: Pointer to the seqcount_LOCKNAME_t instance
200 * @lock: Pointer to the associated LOCKTYPE
204 * SEQCOUNT_LOCKTYPE() - Instantiate seqcount_LOCKNAME_t and helpers
205 * @locktype: actual typename
207 * @preemptible: preemptibility of above locktype
208 * @lockmember: argument for lockdep_assert_held()
210 #define SEQCOUNT_LOCKTYPE(locktype, lockname, preemptible, lockmember) \
211 typedef struct seqcount_##lockname { \
212 seqcount_t seqcount; \
213 __SEQ_LOCK(locktype *lock); \
214 } seqcount_##lockname##_t; \
216 static __always_inline void \
217 seqcount_##lockname##_init(seqcount_##lockname##_t *s, locktype *lock) \
219 seqcount_init(&s->seqcount); \
220 __SEQ_LOCK(s->lock = lock); \
223 static __always_inline seqcount_t * \
224 __seqcount_##lockname##_ptr(seqcount_##lockname##_t *s) \
226 return &s->seqcount; \
229 static __always_inline bool \
230 __seqcount_##lockname##_preemptible(seqcount_##lockname##_t *s) \
232 return preemptible; \
235 static __always_inline void \
236 __seqcount_##lockname##_assert(seqcount_##lockname##_t *s) \
238 __SEQ_LOCK(lockdep_assert_held(lockmember)); \
242 * __seqprop() for seqcount_t
245 static inline seqcount_t *__seqcount_ptr(seqcount_t *s)
250 static inline bool __seqcount_preemptible(seqcount_t *s)
255 static inline void __seqcount_assert(seqcount_t *s)
257 lockdep_assert_preemption_disabled();
260 SEQCOUNT_LOCKTYPE(raw_spinlock_t, raw_spinlock, false, s->lock)
261 SEQCOUNT_LOCKTYPE(spinlock_t, spinlock, false, s->lock)
262 SEQCOUNT_LOCKTYPE(rwlock_t, rwlock, false, s->lock)
263 SEQCOUNT_LOCKTYPE(struct mutex, mutex, true, s->lock)
264 SEQCOUNT_LOCKTYPE(struct ww_mutex, ww_mutex, true, &s->lock->base)
266 #define __seqprop_case(s, lockname, prop) \
267 seqcount_##lockname##_t: __seqcount_##lockname##_##prop((void *)(s))
269 #define __seqprop(s, prop) _Generic(*(s), \
270 seqcount_t: __seqcount_##prop((void *)(s)), \
271 __seqprop_case((s), raw_spinlock, prop), \
272 __seqprop_case((s), spinlock, prop), \
273 __seqprop_case((s), rwlock, prop), \
274 __seqprop_case((s), mutex, prop), \
275 __seqprop_case((s), ww_mutex, prop))
277 #define __to_seqcount_t(s) __seqprop(s, ptr)
278 #define __associated_lock_exists_and_is_preemptible(s) __seqprop(s, preemptible)
279 #define __assert_write_section_is_protected(s) __seqprop(s, assert)
282 * __read_seqcount_begin() - begin a seqcount_t read section w/o barrier
283 * @s: Pointer to seqcount_t or any of the seqcount_locktype_t variants
285 * __read_seqcount_begin is like read_seqcount_begin, but has no smp_rmb()
286 * barrier. Callers should ensure that smp_rmb() or equivalent ordering is
287 * provided before actually loading any of the variables that are to be
288 * protected in this critical section.
290 * Use carefully, only in critical code, and comment how the barrier is
293 * Return: count to be passed to read_seqcount_retry()
295 #define __read_seqcount_begin(s) \
296 __read_seqcount_t_begin(__to_seqcount_t(s))
298 static inline unsigned __read_seqcount_t_begin(const seqcount_t *s)
303 ret = READ_ONCE(s->sequence);
304 if (unlikely(ret & 1)) {
308 kcsan_atomic_next(KCSAN_SEQLOCK_REGION_MAX);
313 * raw_read_seqcount_begin() - begin a seqcount_t read section w/o lockdep
314 * @s: Pointer to seqcount_t or any of the seqcount_locktype_t variants
316 * Return: count to be passed to read_seqcount_retry()
318 #define raw_read_seqcount_begin(s) \
319 raw_read_seqcount_t_begin(__to_seqcount_t(s))
321 static inline unsigned raw_read_seqcount_t_begin(const seqcount_t *s)
323 unsigned ret = __read_seqcount_t_begin(s);
329 * read_seqcount_begin() - begin a seqcount_t read critical section
330 * @s: Pointer to seqcount_t or any of the seqcount_locktype_t variants
332 * Return: count to be passed to read_seqcount_retry()
334 #define read_seqcount_begin(s) \
335 read_seqcount_t_begin(__to_seqcount_t(s))
337 static inline unsigned read_seqcount_t_begin(const seqcount_t *s)
339 seqcount_lockdep_reader_access(s);
340 return raw_read_seqcount_t_begin(s);
344 * raw_read_seqcount() - read the raw seqcount_t counter value
345 * @s: Pointer to seqcount_t or any of the seqcount_locktype_t variants
347 * raw_read_seqcount opens a read critical section of the given
348 * seqcount_t, without any lockdep checking, and without checking or
349 * masking the sequence counter LSB. Calling code is responsible for
352 * Return: count to be passed to read_seqcount_retry()
354 #define raw_read_seqcount(s) \
355 raw_read_seqcount_t(__to_seqcount_t(s))
357 static inline unsigned raw_read_seqcount_t(const seqcount_t *s)
359 unsigned ret = READ_ONCE(s->sequence);
361 kcsan_atomic_next(KCSAN_SEQLOCK_REGION_MAX);
366 * raw_seqcount_begin() - begin a seqcount_t read critical section w/o
367 * lockdep and w/o counter stabilization
368 * @s: Pointer to seqcount_t or any of the seqcount_locktype_t variants
370 * raw_seqcount_begin opens a read critical section of the given
371 * seqcount_t. Unlike read_seqcount_begin(), this function will not wait
372 * for the count to stabilize. If a writer is active when it begins, it
373 * will fail the read_seqcount_retry() at the end of the read critical
374 * section instead of stabilizing at the beginning of it.
376 * Use this only in special kernel hot paths where the read section is
377 * small and has a high probability of success through other external
378 * means. It will save a single branching instruction.
380 * Return: count to be passed to read_seqcount_retry()
382 #define raw_seqcount_begin(s) \
383 raw_seqcount_t_begin(__to_seqcount_t(s))
385 static inline unsigned raw_seqcount_t_begin(const seqcount_t *s)
388 * If the counter is odd, let read_seqcount_retry() fail
389 * by decrementing the counter.
391 return raw_read_seqcount_t(s) & ~1;
395 * __read_seqcount_retry() - end a seqcount_t read section w/o barrier
396 * @s: Pointer to seqcount_t or any of the seqcount_locktype_t variants
397 * @start: count, from read_seqcount_begin()
399 * __read_seqcount_retry is like read_seqcount_retry, but has no smp_rmb()
400 * barrier. Callers should ensure that smp_rmb() or equivalent ordering is
401 * provided before actually loading any of the variables that are to be
402 * protected in this critical section.
404 * Use carefully, only in critical code, and comment how the barrier is
407 * Return: true if a read section retry is required, else false
409 #define __read_seqcount_retry(s, start) \
410 __read_seqcount_t_retry(__to_seqcount_t(s), start)
412 static inline int __read_seqcount_t_retry(const seqcount_t *s, unsigned start)
414 kcsan_atomic_next(0);
415 return unlikely(READ_ONCE(s->sequence) != start);
419 * read_seqcount_retry() - end a seqcount_t read critical section
420 * @s: Pointer to seqcount_t or any of the seqcount_locktype_t variants
421 * @start: count, from read_seqcount_begin()
423 * read_seqcount_retry closes the read critical section of given
424 * seqcount_t. If the critical section was invalid, it must be ignored
425 * (and typically retried).
427 * Return: true if a read section retry is required, else false
429 #define read_seqcount_retry(s, start) \
430 read_seqcount_t_retry(__to_seqcount_t(s), start)
432 static inline int read_seqcount_t_retry(const seqcount_t *s, unsigned start)
435 return __read_seqcount_t_retry(s, start);
439 * raw_write_seqcount_begin() - start a seqcount_t write section w/o lockdep
440 * @s: Pointer to seqcount_t or any of the seqcount_locktype_t variants
442 #define raw_write_seqcount_begin(s) \
444 if (__associated_lock_exists_and_is_preemptible(s)) \
447 raw_write_seqcount_t_begin(__to_seqcount_t(s)); \
450 static inline void raw_write_seqcount_t_begin(seqcount_t *s)
452 kcsan_nestable_atomic_begin();
458 * raw_write_seqcount_end() - end a seqcount_t write section w/o lockdep
459 * @s: Pointer to seqcount_t or any of the seqcount_locktype_t variants
461 #define raw_write_seqcount_end(s) \
463 raw_write_seqcount_t_end(__to_seqcount_t(s)); \
465 if (__associated_lock_exists_and_is_preemptible(s)) \
469 static inline void raw_write_seqcount_t_end(seqcount_t *s)
473 kcsan_nestable_atomic_end();
477 * write_seqcount_begin_nested() - start a seqcount_t write section with
478 * custom lockdep nesting level
479 * @s: Pointer to seqcount_t or any of the seqcount_locktype_t variants
480 * @subclass: lockdep nesting level
482 * See Documentation/locking/lockdep-design.rst
484 #define write_seqcount_begin_nested(s, subclass) \
486 __assert_write_section_is_protected(s); \
488 if (__associated_lock_exists_and_is_preemptible(s)) \
491 write_seqcount_t_begin_nested(__to_seqcount_t(s), subclass); \
494 static inline void write_seqcount_t_begin_nested(seqcount_t *s, int subclass)
496 raw_write_seqcount_t_begin(s);
497 seqcount_acquire(&s->dep_map, subclass, 0, _RET_IP_);
501 * write_seqcount_begin() - start a seqcount_t write side critical section
502 * @s: Pointer to seqcount_t or any of the seqcount_locktype_t variants
504 * write_seqcount_begin opens a write side critical section of the given
507 * Context: seqcount_t write side critical sections must be serialized and
508 * non-preemptible. If readers can be invoked from hardirq or softirq
509 * context, interrupts or bottom halves must be respectively disabled.
511 #define write_seqcount_begin(s) \
513 __assert_write_section_is_protected(s); \
515 if (__associated_lock_exists_and_is_preemptible(s)) \
518 write_seqcount_t_begin(__to_seqcount_t(s)); \
521 static inline void write_seqcount_t_begin(seqcount_t *s)
523 write_seqcount_t_begin_nested(s, 0);
527 * write_seqcount_end() - end a seqcount_t write side critical section
528 * @s: Pointer to seqcount_t or any of the seqcount_locktype_t variants
530 * The write section must've been opened with write_seqcount_begin().
532 #define write_seqcount_end(s) \
534 write_seqcount_t_end(__to_seqcount_t(s)); \
536 if (__associated_lock_exists_and_is_preemptible(s)) \
540 static inline void write_seqcount_t_end(seqcount_t *s)
542 seqcount_release(&s->dep_map, _RET_IP_);
543 raw_write_seqcount_t_end(s);
547 * raw_write_seqcount_barrier() - do a seqcount_t write barrier
548 * @s: Pointer to seqcount_t or any of the seqcount_locktype_t variants
550 * This can be used to provide an ordering guarantee instead of the usual
551 * consistency guarantee. It is one wmb cheaper, because it can collapse
552 * the two back-to-back wmb()s.
554 * Note that writes surrounding the barrier should be declared atomic (e.g.
555 * via WRITE_ONCE): a) to ensure the writes become visible to other threads
556 * atomically, avoiding compiler optimizations; b) to document which writes are
557 * meant to propagate to the reader critical section. This is necessary because
558 * neither writes before and after the barrier are enclosed in a seq-writer
559 * critical section that would ensure readers are aware of ongoing writes::
562 * bool X = true, Y = false;
569 * int s = read_seqcount_begin(&seq);
573 * } while (read_seqcount_retry(&seq, s));
580 * WRITE_ONCE(Y, true);
582 * raw_write_seqcount_barrier(seq);
584 * WRITE_ONCE(X, false);
587 #define raw_write_seqcount_barrier(s) \
588 raw_write_seqcount_t_barrier(__to_seqcount_t(s))
590 static inline void raw_write_seqcount_t_barrier(seqcount_t *s)
592 kcsan_nestable_atomic_begin();
596 kcsan_nestable_atomic_end();
600 * write_seqcount_invalidate() - invalidate in-progress seqcount_t read
602 * @s: Pointer to seqcount_t or any of the seqcount_locktype_t variants
604 * After write_seqcount_invalidate, no seqcount_t read side operations
605 * will complete successfully and see data older than this.
607 #define write_seqcount_invalidate(s) \
608 write_seqcount_t_invalidate(__to_seqcount_t(s))
610 static inline void write_seqcount_t_invalidate(seqcount_t *s)
613 kcsan_nestable_atomic_begin();
615 kcsan_nestable_atomic_end();
619 * raw_read_seqcount_latch() - pick even/odd seqcount_t latch data copy
620 * @s: Pointer to seqcount_t or any of the seqcount_locktype_t variants
622 * Use seqcount_t latching to switch between two storage places protected
623 * by a sequence counter. Doing so allows having interruptible, preemptible,
624 * seqcount_t write side critical sections.
626 * Check raw_write_seqcount_latch() for more details and a full reader and
627 * writer usage example.
629 * Return: sequence counter raw value. Use the lowest bit as an index for
630 * picking which data copy to read. The full counter value must then be
631 * checked with read_seqcount_retry().
633 #define raw_read_seqcount_latch(s) \
634 raw_read_seqcount_t_latch(__to_seqcount_t(s))
636 static inline int raw_read_seqcount_t_latch(seqcount_t *s)
638 /* Pairs with the first smp_wmb() in raw_write_seqcount_latch() */
639 int seq = READ_ONCE(s->sequence); /* ^^^ */
644 * raw_write_seqcount_latch() - redirect readers to even/odd copy
645 * @s: Pointer to seqcount_t or any of the seqcount_locktype_t variants
647 * The latch technique is a multiversion concurrency control method that allows
648 * queries during non-atomic modifications. If you can guarantee queries never
649 * interrupt the modification -- e.g. the concurrency is strictly between CPUs
650 * -- you most likely do not need this.
652 * Where the traditional RCU/lockless data structures rely on atomic
653 * modifications to ensure queries observe either the old or the new state the
654 * latch allows the same for non-atomic updates. The trade-off is doubling the
655 * cost of storage; we have to maintain two copies of the entire data
658 * Very simply put: we first modify one copy and then the other. This ensures
659 * there is always one copy in a stable state, ready to give us an answer.
661 * The basic form is a data structure like::
663 * struct latch_struct {
665 * struct data_struct data[2];
668 * Where a modification, which is assumed to be externally serialized, does the
671 * void latch_modify(struct latch_struct *latch, ...)
673 * smp_wmb(); // Ensure that the last data[1] update is visible
675 * smp_wmb(); // Ensure that the seqcount update is visible
677 * modify(latch->data[0], ...);
679 * smp_wmb(); // Ensure that the data[0] update is visible
681 * smp_wmb(); // Ensure that the seqcount update is visible
683 * modify(latch->data[1], ...);
686 * The query will have a form like::
688 * struct entry *latch_query(struct latch_struct *latch, ...)
690 * struct entry *entry;
694 * seq = raw_read_seqcount_latch(&latch->seq);
697 * entry = data_query(latch->data[idx], ...);
699 * // read_seqcount_retry() includes needed smp_rmb()
700 * } while (read_seqcount_retry(&latch->seq, seq));
705 * So during the modification, queries are first redirected to data[1]. Then we
706 * modify data[0]. When that is complete, we redirect queries back to data[0]
707 * and we can modify data[1].
711 * The non-requirement for atomic modifications does _NOT_ include
712 * the publishing of new entries in the case where data is a dynamic
715 * An iteration might start in data[0] and get suspended long enough
716 * to miss an entire modification sequence, once it resumes it might
717 * observe the new entry.
721 * When data is a dynamic data structure; one should use regular RCU
722 * patterns to manage the lifetimes of the objects within.
724 #define raw_write_seqcount_latch(s) \
725 raw_write_seqcount_t_latch(__to_seqcount_t(s))
727 static inline void raw_write_seqcount_t_latch(seqcount_t *s)
729 smp_wmb(); /* prior stores before incrementing "sequence" */
731 smp_wmb(); /* increment "sequence" before following stores */
735 * Sequential locks (seqlock_t)
737 * Sequence counters with an embedded spinlock for writer serialization
738 * and non-preemptibility.
740 * For more info, see:
741 * - Comments on top of seqcount_t
742 * - Documentation/locking/seqlock.rst
745 struct seqcount seqcount;
749 #define __SEQLOCK_UNLOCKED(lockname) \
751 .seqcount = SEQCNT_ZERO(lockname), \
752 .lock = __SPIN_LOCK_UNLOCKED(lockname) \
756 * seqlock_init() - dynamic initializer for seqlock_t
757 * @sl: Pointer to the seqlock_t instance
759 #define seqlock_init(sl) \
761 seqcount_init(&(sl)->seqcount); \
762 spin_lock_init(&(sl)->lock); \
766 * DEFINE_SEQLOCK() - Define a statically allocated seqlock_t
767 * @sl: Name of the seqlock_t instance
769 #define DEFINE_SEQLOCK(sl) \
770 seqlock_t sl = __SEQLOCK_UNLOCKED(sl)
773 * read_seqbegin() - start a seqlock_t read side critical section
774 * @sl: Pointer to seqlock_t
776 * Return: count, to be passed to read_seqretry()
778 static inline unsigned read_seqbegin(const seqlock_t *sl)
780 unsigned ret = read_seqcount_begin(&sl->seqcount);
782 kcsan_atomic_next(0); /* non-raw usage, assume closing read_seqretry() */
783 kcsan_flat_atomic_begin();
788 * read_seqretry() - end a seqlock_t read side section
789 * @sl: Pointer to seqlock_t
790 * @start: count, from read_seqbegin()
792 * read_seqretry closes the read side critical section of given seqlock_t.
793 * If the critical section was invalid, it must be ignored (and typically
796 * Return: true if a read section retry is required, else false
798 static inline unsigned read_seqretry(const seqlock_t *sl, unsigned start)
801 * Assume not nested: read_seqretry() may be called multiple times when
802 * completing read critical section.
804 kcsan_flat_atomic_end();
806 return read_seqcount_retry(&sl->seqcount, start);
810 * write_seqlock() - start a seqlock_t write side critical section
811 * @sl: Pointer to seqlock_t
813 * write_seqlock opens a write side critical section for the given
814 * seqlock_t. It also implicitly acquires the spinlock_t embedded inside
815 * that sequential lock. All seqlock_t write side sections are thus
816 * automatically serialized and non-preemptible.
818 * Context: if the seqlock_t read section, or other write side critical
819 * sections, can be invoked from hardirq or softirq contexts, use the
820 * _irqsave or _bh variants of this function instead.
822 static inline void write_seqlock(seqlock_t *sl)
824 spin_lock(&sl->lock);
825 write_seqcount_t_begin(&sl->seqcount);
829 * write_sequnlock() - end a seqlock_t write side critical section
830 * @sl: Pointer to seqlock_t
832 * write_sequnlock closes the (serialized and non-preemptible) write side
833 * critical section of given seqlock_t.
835 static inline void write_sequnlock(seqlock_t *sl)
837 write_seqcount_t_end(&sl->seqcount);
838 spin_unlock(&sl->lock);
842 * write_seqlock_bh() - start a softirqs-disabled seqlock_t write section
843 * @sl: Pointer to seqlock_t
845 * _bh variant of write_seqlock(). Use only if the read side section, or
846 * other write side sections, can be invoked from softirq contexts.
848 static inline void write_seqlock_bh(seqlock_t *sl)
850 spin_lock_bh(&sl->lock);
851 write_seqcount_t_begin(&sl->seqcount);
855 * write_sequnlock_bh() - end a softirqs-disabled seqlock_t write section
856 * @sl: Pointer to seqlock_t
858 * write_sequnlock_bh closes the serialized, non-preemptible, and
859 * softirqs-disabled, seqlock_t write side critical section opened with
860 * write_seqlock_bh().
862 static inline void write_sequnlock_bh(seqlock_t *sl)
864 write_seqcount_t_end(&sl->seqcount);
865 spin_unlock_bh(&sl->lock);
869 * write_seqlock_irq() - start a non-interruptible seqlock_t write section
870 * @sl: Pointer to seqlock_t
872 * _irq variant of write_seqlock(). Use only if the read side section, or
873 * other write sections, can be invoked from hardirq contexts.
875 static inline void write_seqlock_irq(seqlock_t *sl)
877 spin_lock_irq(&sl->lock);
878 write_seqcount_t_begin(&sl->seqcount);
882 * write_sequnlock_irq() - end a non-interruptible seqlock_t write section
883 * @sl: Pointer to seqlock_t
885 * write_sequnlock_irq closes the serialized and non-interruptible
886 * seqlock_t write side section opened with write_seqlock_irq().
888 static inline void write_sequnlock_irq(seqlock_t *sl)
890 write_seqcount_t_end(&sl->seqcount);
891 spin_unlock_irq(&sl->lock);
894 static inline unsigned long __write_seqlock_irqsave(seqlock_t *sl)
898 spin_lock_irqsave(&sl->lock, flags);
899 write_seqcount_t_begin(&sl->seqcount);
904 * write_seqlock_irqsave() - start a non-interruptible seqlock_t write
906 * @lock: Pointer to seqlock_t
907 * @flags: Stack-allocated storage for saving caller's local interrupt
908 * state, to be passed to write_sequnlock_irqrestore().
910 * _irqsave variant of write_seqlock(). Use it only if the read side
911 * section, or other write sections, can be invoked from hardirq context.
913 #define write_seqlock_irqsave(lock, flags) \
914 do { flags = __write_seqlock_irqsave(lock); } while (0)
917 * write_sequnlock_irqrestore() - end non-interruptible seqlock_t write
919 * @sl: Pointer to seqlock_t
920 * @flags: Caller's saved interrupt state, from write_seqlock_irqsave()
922 * write_sequnlock_irqrestore closes the serialized and non-interruptible
923 * seqlock_t write section previously opened with write_seqlock_irqsave().
926 write_sequnlock_irqrestore(seqlock_t *sl, unsigned long flags)
928 write_seqcount_t_end(&sl->seqcount);
929 spin_unlock_irqrestore(&sl->lock, flags);
933 * read_seqlock_excl() - begin a seqlock_t locking reader section
934 * @sl: Pointer to seqlock_t
936 * read_seqlock_excl opens a seqlock_t locking reader critical section. A
937 * locking reader exclusively locks out *both* other writers *and* other
938 * locking readers, but it does not update the embedded sequence number.
940 * Locking readers act like a normal spin_lock()/spin_unlock().
942 * Context: if the seqlock_t write section, *or other read sections*, can
943 * be invoked from hardirq or softirq contexts, use the _irqsave or _bh
944 * variant of this function instead.
946 * The opened read section must be closed with read_sequnlock_excl().
948 static inline void read_seqlock_excl(seqlock_t *sl)
950 spin_lock(&sl->lock);
954 * read_sequnlock_excl() - end a seqlock_t locking reader critical section
955 * @sl: Pointer to seqlock_t
957 static inline void read_sequnlock_excl(seqlock_t *sl)
959 spin_unlock(&sl->lock);
963 * read_seqlock_excl_bh() - start a seqlock_t locking reader section with
965 * @sl: Pointer to seqlock_t
967 * _bh variant of read_seqlock_excl(). Use this variant only if the
968 * seqlock_t write side section, *or other read sections*, can be invoked
969 * from softirq contexts.
971 static inline void read_seqlock_excl_bh(seqlock_t *sl)
973 spin_lock_bh(&sl->lock);
977 * read_sequnlock_excl_bh() - stop a seqlock_t softirq-disabled locking
979 * @sl: Pointer to seqlock_t
981 static inline void read_sequnlock_excl_bh(seqlock_t *sl)
983 spin_unlock_bh(&sl->lock);
987 * read_seqlock_excl_irq() - start a non-interruptible seqlock_t locking
989 * @sl: Pointer to seqlock_t
991 * _irq variant of read_seqlock_excl(). Use this only if the seqlock_t
992 * write side section, *or other read sections*, can be invoked from a
995 static inline void read_seqlock_excl_irq(seqlock_t *sl)
997 spin_lock_irq(&sl->lock);
1001 * read_sequnlock_excl_irq() - end an interrupts-disabled seqlock_t
1002 * locking reader section
1003 * @sl: Pointer to seqlock_t
1005 static inline void read_sequnlock_excl_irq(seqlock_t *sl)
1007 spin_unlock_irq(&sl->lock);
1010 static inline unsigned long __read_seqlock_excl_irqsave(seqlock_t *sl)
1012 unsigned long flags;
1014 spin_lock_irqsave(&sl->lock, flags);
1019 * read_seqlock_excl_irqsave() - start a non-interruptible seqlock_t
1020 * locking reader section
1021 * @lock: Pointer to seqlock_t
1022 * @flags: Stack-allocated storage for saving caller's local interrupt
1023 * state, to be passed to read_sequnlock_excl_irqrestore().
1025 * _irqsave variant of read_seqlock_excl(). Use this only if the seqlock_t
1026 * write side section, *or other read sections*, can be invoked from a
1029 #define read_seqlock_excl_irqsave(lock, flags) \
1030 do { flags = __read_seqlock_excl_irqsave(lock); } while (0)
1033 * read_sequnlock_excl_irqrestore() - end non-interruptible seqlock_t
1034 * locking reader section
1035 * @sl: Pointer to seqlock_t
1036 * @flags: Caller saved interrupt state, from read_seqlock_excl_irqsave()
1039 read_sequnlock_excl_irqrestore(seqlock_t *sl, unsigned long flags)
1041 spin_unlock_irqrestore(&sl->lock, flags);
1045 * read_seqbegin_or_lock() - begin a seqlock_t lockless or locking reader
1046 * @lock: Pointer to seqlock_t
1047 * @seq : Marker and return parameter. If the passed value is even, the
1048 * reader will become a *lockless* seqlock_t reader as in read_seqbegin().
1049 * If the passed value is odd, the reader will become a *locking* reader
1050 * as in read_seqlock_excl(). In the first call to this function, the
1051 * caller *must* initialize and pass an even value to @seq; this way, a
1052 * lockless read can be optimistically tried first.
1054 * read_seqbegin_or_lock is an API designed to optimistically try a normal
1055 * lockless seqlock_t read section first. If an odd counter is found, the
1056 * lockless read trial has failed, and the next read iteration transforms
1057 * itself into a full seqlock_t locking reader.
1059 * This is typically used to avoid seqlock_t lockless readers starvation
1060 * (too much retry loops) in the case of a sharp spike in write side
1063 * Context: if the seqlock_t write section, *or other read sections*, can
1064 * be invoked from hardirq or softirq contexts, use the _irqsave or _bh
1065 * variant of this function instead.
1067 * Check Documentation/locking/seqlock.rst for template example code.
1069 * Return: the encountered sequence counter value, through the @seq
1070 * parameter, which is overloaded as a return parameter. This returned
1071 * value must be checked with need_seqretry(). If the read section need to
1072 * be retried, this returned value must also be passed as the @seq
1073 * parameter of the next read_seqbegin_or_lock() iteration.
1075 static inline void read_seqbegin_or_lock(seqlock_t *lock, int *seq)
1077 if (!(*seq & 1)) /* Even */
1078 *seq = read_seqbegin(lock);
1080 read_seqlock_excl(lock);
1084 * need_seqretry() - validate seqlock_t "locking or lockless" read section
1085 * @lock: Pointer to seqlock_t
1086 * @seq: sequence count, from read_seqbegin_or_lock()
1088 * Return: true if a read section retry is required, false otherwise
1090 static inline int need_seqretry(seqlock_t *lock, int seq)
1092 return !(seq & 1) && read_seqretry(lock, seq);
1096 * done_seqretry() - end seqlock_t "locking or lockless" reader section
1097 * @lock: Pointer to seqlock_t
1098 * @seq: count, from read_seqbegin_or_lock()
1100 * done_seqretry finishes the seqlock_t read side critical section started
1101 * with read_seqbegin_or_lock() and validated by need_seqretry().
1103 static inline void done_seqretry(seqlock_t *lock, int seq)
1106 read_sequnlock_excl(lock);
1110 * read_seqbegin_or_lock_irqsave() - begin a seqlock_t lockless reader, or
1111 * a non-interruptible locking reader
1112 * @lock: Pointer to seqlock_t
1113 * @seq: Marker and return parameter. Check read_seqbegin_or_lock().
1115 * This is the _irqsave variant of read_seqbegin_or_lock(). Use it only if
1116 * the seqlock_t write section, *or other read sections*, can be invoked
1117 * from hardirq context.
1119 * Note: Interrupts will be disabled only for "locking reader" mode.
1123 * 1. The saved local interrupts state in case of a locking reader, to
1124 * be passed to done_seqretry_irqrestore().
1126 * 2. The encountered sequence counter value, returned through @seq
1127 * overloaded as a return parameter. Check read_seqbegin_or_lock().
1129 static inline unsigned long
1130 read_seqbegin_or_lock_irqsave(seqlock_t *lock, int *seq)
1132 unsigned long flags = 0;
1134 if (!(*seq & 1)) /* Even */
1135 *seq = read_seqbegin(lock);
1137 read_seqlock_excl_irqsave(lock, flags);
1143 * done_seqretry_irqrestore() - end a seqlock_t lockless reader, or a
1144 * non-interruptible locking reader section
1145 * @lock: Pointer to seqlock_t
1146 * @seq: Count, from read_seqbegin_or_lock_irqsave()
1147 * @flags: Caller's saved local interrupt state in case of a locking
1148 * reader, also from read_seqbegin_or_lock_irqsave()
1150 * This is the _irqrestore variant of done_seqretry(). The read section
1151 * must've been opened with read_seqbegin_or_lock_irqsave(), and validated
1152 * by need_seqretry().
1155 done_seqretry_irqrestore(seqlock_t *lock, int seq, unsigned long flags)
1158 read_sequnlock_excl_irqrestore(lock, flags);
1160 #endif /* __LINUX_SEQLOCK_H */