XenevaOS
Loading...
Searching...
No Matches
seqlock.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef __LINUX_SEQLOCK_H
3#define __LINUX_SEQLOCK_H
4
5/*
6 * seqcount_t / seqlock_t - a reader-writer consistency mechanism with
7 * lockless readers (read-only retry loops), and no writer starvation.
8 *
9 * See Documentation/locking/seqlock.rst
10 *
11 * Copyrights:
12 * - Based on x86_64 vsyscall gettimeofday: Keith Owens, Andrea Arcangeli
13 * - Sequence counters with associated locks, (C) 2020 Linutronix GmbH
14 */
15
16#include <linux/compiler.h>
17#include <linux/cleanup.h>
18#include <linux/kcsan-checks.h>
19#include <linux/lockdep.h>
20#include <linux/mutex.h>
21#include <linux/preempt.h>
22#include <linux/seqlock_types.h>
23#include <linux/spinlock.h>
24
25#include <asm/processor.h>
26
27/*
28 * The seqlock seqcount_t interface does not prescribe a precise sequence of
29 * read begin/retry/end. For readers, typically there is a call to
30 * read_seqcount_begin() and read_seqcount_retry(), however, there are more
31 * esoteric cases which do not follow this pattern.
32 *
33 * As a consequence, we take the following best-effort approach for raw usage
34 * via seqcount_t under KCSAN: upon beginning a seq-reader critical section,
35 * pessimistically mark the next KCSAN_SEQLOCK_REGION_MAX memory accesses as
36 * atomics; if there is a matching read_seqcount_retry() call, no following
37 * memory operations are considered atomic. Usage of the seqlock_t interface
38 * is not affected.
39 */
40#define KCSAN_SEQLOCK_REGION_MAX 1000
41
42static inline void __seqcount_init(seqcount_t *s, const char *name,
43 struct lock_class_key *key)
44{
45 /*
46 * Make sure we are not reinitializing a held lock:
47 */
48 lockdep_init_map(&s->dep_map, name, key, 0);
49 s->sequence = 0;
50}
51
52#ifdef CONFIG_DEBUG_LOCK_ALLOC
53
54# define SEQCOUNT_DEP_MAP_INIT(lockname) \
55 .dep_map = { .name = #lockname }
56
61# define seqcount_init(s) \
62 do { \
63 static struct lock_class_key __key; \
64 __seqcount_init((s), #s, &__key); \
65 } while (0)
66
67static inline void seqcount_lockdep_reader_access(const seqcount_t *s)
68{
69 seqcount_t *l = (seqcount_t *)s;
70 unsigned long flags;
71
72 local_irq_save(flags);
73 seqcount_acquire_read(&l->dep_map, 0, 0, _RET_IP_);
74 seqcount_release(&l->dep_map, _RET_IP_);
75 local_irq_restore(flags);
76}
77
78#else
79# define SEQCOUNT_DEP_MAP_INIT(lockname)
80# define seqcount_init(s) __seqcount_init(s, NULL, NULL)
81# define seqcount_lockdep_reader_access(x)
82#endif
83
88#define SEQCNT_ZERO(name) { .sequence = 0, SEQCOUNT_DEP_MAP_INIT(name) }
89
90/*
91 * Sequence counters with associated locks (seqcount_LOCKNAME_t)
92 *
93 * A sequence counter which associates the lock used for writer
94 * serialization at initialization time. This enables lockdep to validate
95 * that the write side critical section is properly serialized.
96 *
97 * For associated locks which do not implicitly disable preemption,
98 * preemption protection is enforced in the write side function.
99 *
100 * Lockdep is never used in any for the raw write variants.
101 *
102 * See Documentation/locking/seqlock.rst
103 */
104
105/*
106 * typedef seqcount_LOCKNAME_t - sequence counter with LOCKNAME associated
107 * @seqcount: The real sequence counter
108 * @lock: Pointer to the associated lock
109 *
110 * A plain sequence counter with external writer synchronization by
111 * LOCKNAME @lock. The lock is associated to the sequence counter in the
112 * static initializer or init function. This enables lockdep to validate
113 * that the write side critical section is properly serialized.
114 *
115 * LOCKNAME: raw_spinlock, spinlock, rwlock or mutex
116 */
117
118/*
119 * seqcount_LOCKNAME_init() - runtime initializer for seqcount_LOCKNAME_t
120 * @s: Pointer to the seqcount_LOCKNAME_t instance
121 * @lock: Pointer to the associated lock
122 */
123
124#define seqcount_LOCKNAME_init(s, _lock, lockname) \
125 do { \
126 seqcount_##lockname##_t *____s = (s); \
127 seqcount_init(&____s->seqcount); \
128 __SEQ_LOCK(____s->lock = (_lock)); \
129 } while (0)
130
131#define seqcount_raw_spinlock_init(s, lock) seqcount_LOCKNAME_init(s, lock, raw_spinlock)
132#define seqcount_spinlock_init(s, lock) seqcount_LOCKNAME_init(s, lock, spinlock)
133#define seqcount_rwlock_init(s, lock) seqcount_LOCKNAME_init(s, lock, rwlock)
134#define seqcount_mutex_init(s, lock) seqcount_LOCKNAME_init(s, lock, mutex)
135
136/*
137 * SEQCOUNT_LOCKNAME() - Instantiate seqcount_LOCKNAME_t and helpers
138 * seqprop_LOCKNAME_*() - Property accessors for seqcount_LOCKNAME_t
139 *
140 * @lockname: "LOCKNAME" part of seqcount_LOCKNAME_t
141 * @locktype: LOCKNAME canonical C data type
142 * @preemptible: preemptibility of above locktype
143 * @lockbase: prefix for associated lock/unlock
144 */
145#define SEQCOUNT_LOCKNAME(lockname, locktype, preemptible, lockbase) \
146static __always_inline seqcount_t * \
147__seqprop_##lockname##_ptr(seqcount_##lockname##_t *s) \
148{ \
149 return &s->seqcount; \
150} \
151 \
152static __always_inline const seqcount_t * \
153__seqprop_##lockname##_const_ptr(const seqcount_##lockname##_t *s) \
154{ \
155 return &s->seqcount; \
156} \
157 \
158static __always_inline unsigned \
159__seqprop_##lockname##_sequence(const seqcount_##lockname##_t *s) \
160{ \
161 unsigned seq = smp_load_acquire(&s->seqcount.sequence); \
162 \
163 if (!IS_ENABLED(CONFIG_PREEMPT_RT)) \
164 return seq; \
165 \
166 if (preemptible && unlikely(seq & 1)) { \
167 __SEQ_LOCK(lockbase##_lock(s->lock)); \
168 __SEQ_LOCK(lockbase##_unlock(s->lock)); \
169 \
170 /* \
171 * Re-read the sequence counter since the (possibly \
172 * preempted) writer made progress. \
173 */ \
174 seq = smp_load_acquire(&s->seqcount.sequence); \
175 } \
176 \
177 return seq; \
178} \
179 \
180static __always_inline bool \
181__seqprop_##lockname##_preemptible(const seqcount_##lockname##_t *s) \
182{ \
183 if (!IS_ENABLED(CONFIG_PREEMPT_RT)) \
184 return preemptible; \
185 \
186 /* PREEMPT_RT relies on the above LOCK+UNLOCK */ \
187 return false; \
188} \
189 \
190static __always_inline void \
191__seqprop_##lockname##_assert(const seqcount_##lockname##_t *s) \
192{ \
193 __SEQ_LOCK(lockdep_assert_held(s->lock)); \
194}
195
196/*
197 * __seqprop() for seqcount_t
198 */
199
200static inline seqcount_t *__seqprop_ptr(seqcount_t *s)
201{
202 return s;
203}
204
205static inline const seqcount_t *__seqprop_const_ptr(const seqcount_t *s)
206{
207 return s;
208}
209
210static inline unsigned __seqprop_sequence(const seqcount_t *s)
211{
212 return smp_load_acquire(&s->sequence);
213}
214
215static inline bool __seqprop_preemptible(const seqcount_t *s)
216{
217 return false;
218}
219
220static inline void __seqprop_assert(const seqcount_t *s)
221{
223}
224
225#define __SEQ_RT IS_ENABLED(CONFIG_PREEMPT_RT)
226
227SEQCOUNT_LOCKNAME(raw_spinlock, raw_spinlock_t, false, raw_spin)
228SEQCOUNT_LOCKNAME(spinlock, spinlock_t, __SEQ_RT, spin)
230SEQCOUNT_LOCKNAME(mutex, struct mutex, true, mutex)
231#undef SEQCOUNT_LOCKNAME
232
233/*
234 * SEQCNT_LOCKNAME_ZERO - static initializer for seqcount_LOCKNAME_t
235 * @name: Name of the seqcount_LOCKNAME_t instance
236 * @lock: Pointer to the associated LOCKNAME
237 */
238
239#define SEQCOUNT_LOCKNAME_ZERO(seq_name, assoc_lock) { \
240 .seqcount = SEQCNT_ZERO(seq_name.seqcount), \
241 __SEQ_LOCK(.lock = (assoc_lock)) \
244#define SEQCNT_RAW_SPINLOCK_ZERO(name, lock) SEQCOUNT_LOCKNAME_ZERO(name, lock)
245#define SEQCNT_SPINLOCK_ZERO(name, lock) SEQCOUNT_LOCKNAME_ZERO(name, lock)
246#define SEQCNT_RWLOCK_ZERO(name, lock) SEQCOUNT_LOCKNAME_ZERO(name, lock)
247#define SEQCNT_MUTEX_ZERO(name, lock) SEQCOUNT_LOCKNAME_ZERO(name, lock)
248#define SEQCNT_WW_MUTEX_ZERO(name, lock) SEQCOUNT_LOCKNAME_ZERO(name, lock)
249
250#define __seqprop_case(s, lockname, prop) \
251 seqcount_##lockname##_t: __seqprop_##lockname##_##prop
252
253#define __seqprop(s, prop) _Generic(*(s), \
254 seqcount_t: __seqprop_##prop, \
255 __seqprop_case((s), raw_spinlock, prop), \
256 __seqprop_case((s), spinlock, prop), \
257 __seqprop_case((s), rwlock, prop), \
258 __seqprop_case((s), mutex, prop))
260#define seqprop_ptr(s) __seqprop(s, ptr)(s)
261#define seqprop_const_ptr(s) __seqprop(s, const_ptr)(s)
262#define seqprop_sequence(s) __seqprop(s, sequence)(s)
263#define seqprop_preemptible(s) __seqprop(s, preemptible)(s)
264#define seqprop_assert(s) __seqprop(s, assert)(s)
265
272#define __read_seqcount_begin(s) \
273({ \
274 unsigned __seq; \
275 \
276 while (unlikely((__seq = seqprop_sequence(s)) & 1)) \
277 cpu_relax(); \
278 \
279 kcsan_atomic_next(KCSAN_SEQLOCK_REGION_MAX); \
280 __seq; \
281})
282
289#define raw_read_seqcount_begin(s) __read_seqcount_begin(s)
290
297#define read_seqcount_begin(s) \
298({ \
299 seqcount_lockdep_reader_access(seqprop_const_ptr(s)); \
300 raw_read_seqcount_begin(s); \
301})
302
314#define raw_read_seqcount(s) \
315({ \
316 unsigned __seq = seqprop_sequence(s); \
317 \
318 kcsan_atomic_next(KCSAN_SEQLOCK_REGION_MAX); \
319 __seq; \
320})
321
339#define raw_seqcount_try_begin(s, start) \
340({ \
341 start = raw_read_seqcount(s); \
342 !(start & 1); \
343})
344
362#define raw_seqcount_begin(s) \
363({ \
364 /* \
365 * If the counter is odd, let read_seqcount_retry() fail \
366 * by decrementing the counter. \
367 */ \
368 raw_read_seqcount(s) & ~1; \
369})
370
386#define __read_seqcount_retry(s, start) \
387 do___read_seqcount_retry(seqprop_const_ptr(s), start)
388
389static inline int do___read_seqcount_retry(const seqcount_t *s, unsigned start)
390{
391 kcsan_atomic_next(0);
392 return unlikely(READ_ONCE(s->sequence) != start);
393}
394
406#define read_seqcount_retry(s, start) \
407 do_read_seqcount_retry(seqprop_const_ptr(s), start)
408
409static inline int do_read_seqcount_retry(const seqcount_t *s, unsigned start)
410{
411 smp_rmb();
412 return do___read_seqcount_retry(s, start);
413}
414
421#define raw_write_seqcount_begin(s) \
422do { \
423 if (seqprop_preemptible(s)) \
424 preempt_disable(); \
425 \
426 do_raw_write_seqcount_begin(seqprop_ptr(s)); \
427} while (0)
428
429static inline void do_raw_write_seqcount_begin(seqcount_t *s)
430{
431 kcsan_nestable_atomic_begin();
432 s->sequence++;
433 smp_wmb();
434}
435
442#define raw_write_seqcount_end(s) \
443do { \
444 do_raw_write_seqcount_end(seqprop_ptr(s)); \
445 \
446 if (seqprop_preemptible(s)) \
447 preempt_enable(); \
448} while (0)
449
450static inline void do_raw_write_seqcount_end(seqcount_t *s)
451{
452 smp_wmb();
453 s->sequence++;
454 kcsan_nestable_atomic_end();
455}
456
466#define write_seqcount_begin_nested(s, subclass) \
467do { \
468 seqprop_assert(s); \
469 \
470 if (seqprop_preemptible(s)) \
471 preempt_disable(); \
472 \
473 do_write_seqcount_begin_nested(seqprop_ptr(s), subclass); \
474} while (0)
475
476static inline void do_write_seqcount_begin_nested(seqcount_t *s, int subclass)
477{
478 seqcount_acquire(&s->dep_map, subclass, 0, _RET_IP_);
479 do_raw_write_seqcount_begin(s);
480}
481
492#define write_seqcount_begin(s) \
493do { \
494 seqprop_assert(s); \
495 \
496 if (seqprop_preemptible(s)) \
497 preempt_disable(); \
498 \
499 do_write_seqcount_begin(seqprop_ptr(s)); \
500} while (0)
501
502static inline void do_write_seqcount_begin(seqcount_t *s)
503{
504 do_write_seqcount_begin_nested(s, 0);
505}
506
514#define write_seqcount_end(s) \
515do { \
516 do_write_seqcount_end(seqprop_ptr(s)); \
517 \
518 if (seqprop_preemptible(s)) \
519 preempt_enable(); \
520} while (0)
521
522static inline void do_write_seqcount_end(seqcount_t *s)
523{
524 seqcount_release(&s->dep_map, _RET_IP_);
525 do_raw_write_seqcount_end(s);
526}
527
569#define raw_write_seqcount_barrier(s) \
570 do_raw_write_seqcount_barrier(seqprop_ptr(s))
571
572static inline void do_raw_write_seqcount_barrier(seqcount_t *s)
573{
574 kcsan_nestable_atomic_begin();
575 s->sequence++;
576 smp_wmb();
577 s->sequence++;
578 kcsan_nestable_atomic_end();
579}
580
589#define write_seqcount_invalidate(s) \
590 do_write_seqcount_invalidate(seqprop_ptr(s))
591
592static inline void do_write_seqcount_invalidate(seqcount_t *s)
593{
594 smp_wmb();
595 kcsan_nestable_atomic_begin();
596 s->sequence+=2;
597 kcsan_nestable_atomic_end();
598}
599
600/*
601 * Latch sequence counters (seqcount_latch_t)
602 *
603 * A sequence counter variant where the counter even/odd value is used to
604 * switch between two copies of protected data. This allows the read path,
605 * typically NMIs, to safely interrupt the write side critical section.
606 *
607 * As the write sections are fully preemptible, no special handling for
608 * PREEMPT_RT is needed.
609 */
610typedef struct {
618#define SEQCNT_LATCH_ZERO(seq_name) { \
619 .seqcount = SEQCNT_ZERO(seq_name.seqcount), \
621
626#define seqcount_latch_init(s) seqcount_init(&(s)->seqcount)
627
639static __always_inline unsigned raw_read_seqcount_latch(const seqcount_latch_t *s)
640{
641 /*
642 * Pairs with the first smp_wmb() in raw_write_seqcount_latch().
643 * Due to the dependent load, a full smp_rmb() is not needed.
644 */
645 return READ_ONCE(s->seqcount.sequence);
646}
647
659static __always_inline unsigned read_seqcount_latch(const seqcount_latch_t *s)
660{
661 kcsan_atomic_next(KCSAN_SEQLOCK_REGION_MAX);
662 return raw_read_seqcount_latch(s);
663}
664
672static __always_inline int
673raw_read_seqcount_latch_retry(const seqcount_latch_t *s, unsigned start)
674{
675 smp_rmb();
676 return unlikely(READ_ONCE(s->seqcount.sequence) != start);
677}
678
686static __always_inline int
687read_seqcount_latch_retry(const seqcount_latch_t *s, unsigned start)
688{
689 kcsan_atomic_next(0);
690 return raw_read_seqcount_latch_retry(s, start);
691}
692
697static __always_inline void raw_write_seqcount_latch(seqcount_latch_t *s)
698{
699 smp_wmb(); /* prior stores before incrementing "sequence" */
700 s->seqcount.sequence++;
701 smp_wmb(); /* increment "sequence" before following stores */
702}
703
779static __always_inline void write_seqcount_latch_begin(seqcount_latch_t *s)
780{
781 kcsan_nestable_atomic_begin();
782 raw_write_seqcount_latch(s);
783}
784
789static __always_inline void write_seqcount_latch(seqcount_latch_t *s)
790{
791 raw_write_seqcount_latch(s);
792}
793
801static __always_inline void write_seqcount_latch_end(seqcount_latch_t *s)
802{
803 kcsan_nestable_atomic_end();
804}
805
806#define __SEQLOCK_UNLOCKED(lockname) \
807 { \
808 .seqcount = SEQCNT_SPINLOCK_ZERO(lockname, &(lockname).lock), \
809 .lock = __SPIN_LOCK_UNLOCKED(lockname) \
811
816#define seqlock_init(sl) \
817 do { \
818 spin_lock_init(&(sl)->lock); \
819 seqcount_spinlock_init(&(sl)->seqcount, &(sl)->lock); \
820 } while (0)
821
826#define DEFINE_SEQLOCK(sl) \
827 seqlock_t sl = __SEQLOCK_UNLOCKED(sl)
828
835static inline unsigned read_seqbegin(const seqlock_t *sl)
837{
838 return read_seqcount_begin(&sl->seqcount);
839}
840
852static inline unsigned read_seqretry(const seqlock_t *sl, unsigned start)
854{
855 return read_seqcount_retry(&sl->seqcount, start);
856}
857
858/*
859 * For all seqlock_t write side functions, use the internal
860 * do_write_seqcount_begin() instead of generic write_seqcount_begin().
861 * This way, no redundant lockdep_assert_held() checks are added.
862 */
863
877static inline void write_seqlock(seqlock_t *sl)
879{
880 spin_lock(&sl->lock);
881 do_write_seqcount_begin(&sl->seqcount.seqcount);
882}
883
891static inline void write_sequnlock(seqlock_t *sl)
893{
894 do_write_seqcount_end(&sl->seqcount.seqcount);
895 spin_unlock(&sl->lock);
896}
897
905static inline void write_seqlock_bh(seqlock_t *sl)
907{
908 spin_lock_bh(&sl->lock);
909 do_write_seqcount_begin(&sl->seqcount.seqcount);
910}
911
920static inline void write_sequnlock_bh(seqlock_t *sl)
922{
923 do_write_seqcount_end(&sl->seqcount.seqcount);
924 spin_unlock_bh(&sl->lock);
925}
926
934static inline void write_seqlock_irq(seqlock_t *sl)
936{
937 spin_lock_irq(&sl->lock);
938 do_write_seqcount_begin(&sl->seqcount.seqcount);
939}
940
948static inline void write_sequnlock_irq(seqlock_t *sl)
950{
951 do_write_seqcount_end(&sl->seqcount.seqcount);
952 spin_unlock_irq(&sl->lock);
953}
954
955static inline unsigned long __write_seqlock_irqsave(seqlock_t *sl)
957{
958 unsigned long flags;
959
960 spin_lock_irqsave(&sl->lock, flags);
961 do_write_seqcount_begin(&sl->seqcount.seqcount);
962 return flags;
963}
964
975#define write_seqlock_irqsave(lock, flags) \
976 do { flags = __write_seqlock_irqsave(lock); } while (0)
977
987static inline void
988write_sequnlock_irqrestore(seqlock_t *sl, unsigned long flags)
990{
991 do_write_seqcount_end(&sl->seqcount.seqcount);
992 spin_unlock_irqrestore(&sl->lock, flags);
993}
994
1011static inline void read_seqlock_excl(seqlock_t *sl)
1013{
1014 spin_lock(&sl->lock);
1015}
1016
1021static inline void read_sequnlock_excl(seqlock_t *sl)
1023{
1024 spin_unlock(&sl->lock);
1025}
1026
1036static inline void read_seqlock_excl_bh(seqlock_t *sl)
1038{
1039 spin_lock_bh(&sl->lock);
1040}
1041
1047static inline void read_sequnlock_excl_bh(seqlock_t *sl)
1049{
1050 spin_unlock_bh(&sl->lock);
1051}
1052
1062static inline void read_seqlock_excl_irq(seqlock_t *sl)
1064{
1065 spin_lock_irq(&sl->lock);
1066}
1067
1073static inline void read_sequnlock_excl_irq(seqlock_t *sl)
1075{
1076 spin_unlock_irq(&sl->lock);
1077}
1078
1079static inline unsigned long __read_seqlock_excl_irqsave(seqlock_t *sl)
1081{
1082 unsigned long flags;
1083
1084 spin_lock_irqsave(&sl->lock, flags);
1085 return flags;
1086}
1087
1099#define read_seqlock_excl_irqsave(lock, flags) \
1100 do { flags = __read_seqlock_excl_irqsave(lock); } while (0)
1101
1108static inline void
1109read_sequnlock_excl_irqrestore(seqlock_t *sl, unsigned long flags)
1111{
1112 spin_unlock_irqrestore(&sl->lock, flags);
1113}
1114
1146static inline void read_seqbegin_or_lock(seqlock_t *lock, int *seq)
1148{
1149 if (!(*seq & 1)) /* Even */
1150 *seq = read_seqbegin(lock);
1151 else /* Odd */
1152 read_seqlock_excl(lock);
1153}
1154
1162static inline int need_seqretry(seqlock_t *lock, int seq)
1164{
1165 return !(seq & 1) && read_seqretry(lock, seq);
1166}
1167
1176static inline void done_seqretry(seqlock_t *lock, int seq)
1178{
1179 if (seq & 1)
1180 read_sequnlock_excl(lock);
1181}
1182
1203static inline unsigned long
1204read_seqbegin_or_lock_irqsave(seqlock_t *lock, int *seq)
1206{
1207 unsigned long flags = 0;
1208
1209 if (!(*seq & 1)) /* Even */
1210 *seq = read_seqbegin(lock);
1211 else /* Odd */
1213
1214 return flags;
1215}
1216
1229static inline void
1230done_seqretry_irqrestore(seqlock_t *lock, int seq, unsigned long flags)
1233 if (seq & 1)
1234 read_sequnlock_excl_irqrestore(lock, flags);
1236
1237enum ss_state {
1244struct ss_tmp {
1245 enum ss_state state;
1246 unsigned long data;
1249};
1250
1251static __always_inline void __scoped_seqlock_cleanup(struct ss_tmp *sst)
1253{
1254 if (sst->lock)
1255 spin_unlock(sst->lock);
1256 if (sst->lock_irqsave)
1257 spin_unlock_irqrestore(sst->lock_irqsave, sst->data);
1258}
1259
1260extern void __scoped_seqlock_invalid_target(void);
1261
1262#if (defined(CONFIG_CC_IS_GCC) && CONFIG_GCC_VERSION < 90000) || \
1263 defined(CONFIG_KASAN) || defined(CONFIG_UBSAN_ALIGNMENT)
1264/*
1265 * For some reason some GCC-8 architectures (nios2, alpha) have trouble
1266 * determining that the ss_done state is impossible in __scoped_seqlock_next()
1267 * below.
1268 *
1269 * Similarly KASAN and UBSAN_ALIGNMENT are known to confuse compilers enough
1270 * to break this. But we don't care about code quality for such builds anyway.
1271 */
1272static inline void __scoped_seqlock_bug(void) { }
1273#else
1274/*
1275 * Canary for compiler optimization -- if the compiler doesn't realize this is
1276 * an impossible state, it very likely generates sub-optimal code here.
1277 */
1278extern void __scoped_seqlock_bug(void);
1279#endif
1280
1281static __always_inline void
1282__scoped_seqlock_next(struct ss_tmp *sst, seqlock_t *lock, enum ss_state target)
1284{
1285 switch (sst->state) {
1286 case ss_done:
1288 return;
1289
1290 case ss_lock:
1291 case ss_lock_irqsave:
1292 sst->state = ss_done;
1293 return;
1294
1295 case ss_lockless:
1296 if (!read_seqretry(lock, sst->data)) {
1297 sst->state = ss_done;
1298 return;
1299 }
1300 break;
1301 }
1302
1303 switch (target) {
1304 case ss_done:
1306 return;
1307
1308 case ss_lock:
1309 sst->lock = &lock->lock;
1310 spin_lock(sst->lock);
1311 sst->state = ss_lock;
1312 return;
1313
1314 case ss_lock_irqsave:
1315 sst->lock_irqsave = &lock->lock;
1316 spin_lock_irqsave(sst->lock_irqsave, sst->data);
1317 sst->state = ss_lock_irqsave;
1318 return;
1319
1320 case ss_lockless:
1321 sst->data = read_seqbegin(lock);
1322 return;
1323 }
1324}
1325
1326/*
1327 * Context analysis no-op helper to release seqlock at the end of the for-scope;
1328 * the alias analysis of the compiler will recognize that the pointer @s is an
1329 * alias to @_seqlock passed to read_seqbegin(_seqlock) below.
1330 */
1331static __always_inline void __scoped_seqlock_cleanup_ctx(struct ss_tmp **s)
1333
1334#define __scoped_seqlock_read(_seqlock, _target, _s) \
1335 for (struct ss_tmp _s __cleanup(__scoped_seqlock_cleanup) = \
1336 { .state = ss_lockless, .data = read_seqbegin(_seqlock) }, \
1337 *__UNIQUE_ID(ctx) __cleanup(__scoped_seqlock_cleanup_ctx) =\
1338 (struct ss_tmp *)_seqlock; \
1339 _s.state != ss_done; \
1340 __scoped_seqlock_next(&_s, _seqlock, _target))
1341
1359#define scoped_seqlock_read(_seqlock, _target) \
1360 __scoped_seqlock_read(_seqlock, _target, __UNIQUE_ID(seqlock))
1361
1364#define class_seqlock_init_constructor(_T) WITH_LOCK_GUARD_1_ATTRS(seqlock_init, _T)
1365
1366#endif /* __LINUX_SEQLOCK_H */
uint8_t flags
Definition virtio.h:0
#define read
Definition acwin.h:180
#define DEFINE_LOCK_GUARD_1(_name, _type, _lock, _unlock,...)
Definition cleanup.h:255
#define DECLARE_LOCK_GUARD_1_ATTRS(_name, _lock, _unlock)
Definition cleanup.h:290
#define __acquires(...)
Definition compiler-context-analysis.h:299
#define __releases_shared(...)
Definition compiler-context-analysis.h:423
#define __no_context_analysis
Definition compiler-context-analysis.h:191
#define __releases(...)
Definition compiler-context-analysis.h:335
#define __acquires_shared(...)
Definition compiler-context-analysis.h:399
#define unlikely(x)
Definition compiler.h:30
#define __always_inline
Definition compiler_attributes.h:55
struct drm_crtc struct drm_plane struct drm_plane const struct drm_crtc_funcs const char * name
Definition drm_crtc.h:1242
#define _RET_IP_
Definition instruction_pointer.h:22
#define smp_load_acquire(p)
Definition kernel.h:611
#define READ_ONCE(x)
Definition kernel.h:605
#define smp_rmb()
Definition kernel.h:620
#define spin_unlock_irqrestore(l, f)
Definition kernel.h:227
#define spin_lock_irqsave(l, f)
Definition kernel.h:226
int spinlock_t
Definition kernel.h:44
#define smp_wmb()
Definition kernel.h:623
#define spin_lock(l)
Definition spinlock.h:36
#define spin_lock_bh(l)
Definition spinlock.h:40
#define spin_unlock_bh(l)
Definition spinlock.h:41
#define spin_unlock(l)
Definition spinlock.h:37
spinlock_t rwlock_t
Definition spinlock.h:200
#define spin_lock_irq(l)
Definition spinlock.h:38
#define spin_unlock_irq(l)
Definition spinlock.h:39
#define lockdep_assert_preemption_disabled()
Definition lockdep.h:77
#define lockdep_init_map(lock, name, key, sub)
Definition lockdep.h:57
#define seqcount_acquire(l, s, t, i)
Definition lockdep.h:62
#define seqcount_release(l, i)
Definition lockdep.h:63
void __scoped_seqlock_bug(void)
#define SEQCOUNT_LOCKNAME(lockname, locktype, preemptible, lockbase)
Definition seqlock.h:145
void __scoped_seqlock_invalid_target(void)
#define read_seqcount_retry(s, start)
Definition seqlock.h:400
#define seqlock_init(sl)
Definition seqlock.h:810
#define KCSAN_SEQLOCK_REGION_MAX
Definition seqlock.h:40
#define read_seqlock_excl_irqsave(lock, flags)
Definition seqlock.h:1093
#define seqcount_lockdep_reader_access(x)
Definition seqlock.h:81
ss_state
Definition seqlock.h:1231
@ ss_lock
Definition seqlock.h:1233
@ ss_done
Definition seqlock.h:1232
@ ss_lockless
Definition seqlock.h:1235
@ ss_lock_irqsave
Definition seqlock.h:1234
#define __SEQ_RT
Definition seqlock.h:222
#define read_seqcount_begin(s)
Definition seqlock.h:294
struct seqlock seqlock_t
Definition seqlock_types.h:108
Definition lockdep.h:160
Definition mutex.h:59
Definition spinlock.h:210
Definition seqlock.h:604
seqcount_t seqcount
Definition seqlock.h:605
Definition seqlock_types.h:49
unsigned sequence
Definition seqlock_types.h:50
Definition seqlock.h:1238
unsigned long data
Definition seqlock.h:1240
enum ss_state state
Definition seqlock.h:1239
spinlock_t * lock
Definition seqlock.h:1241
spinlock_t * lock_irqsave
Definition seqlock.h:1242