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/kcsan-checks.h>
18 #include <linux/lockdep.h>
19 #include <linux/mutex.h>
20 #include <linux/preempt.h>
21 #include <linux/spinlock.h>
22
23 #include <asm/processor.h>
24
25 /*
26 * The seqlock seqcount_t interface does not prescribe a precise sequence of
27 * read begin/retry/end. For readers, typically there is a call to
28 * read_seqcount_begin() and read_seqcount_retry(), however, there are more
29 * esoteric cases which do not follow this pattern.
30 *
31 * As a consequence, we take the following best-effort approach for raw usage
32 * via seqcount_t under KCSAN: upon beginning a seq-reader critical section,
33 * pessimistically mark the next KCSAN_SEQLOCK_REGION_MAX memory accesses as
34 * atomics; if there is a matching read_seqcount_retry() call, no following
35 * memory operations are considered atomic. Usage of the seqlock_t interface
36 * is not affected.
37 */
38 #define KCSAN_SEQLOCK_REGION_MAX 1000
39
40 /*
41 * Sequence counters (seqcount_t)
42 *
43 * This is the raw counting mechanism, without any writer protection.
44 *
45 * Write side critical sections must be serialized and non-preemptible.
46 *
47 * If readers can be invoked from hardirq or softirq contexts,
48 * interrupts or bottom halves must also be respectively disabled before
49 * entering the write section.
50 *
51 * This mechanism can't be used if the protected data contains pointers,
52 * as the writer can invalidate a pointer that a reader is following.
53 *
54 * If the write serialization mechanism is one of the common kernel
55 * locking primitives, use a sequence counter with associated lock
56 * (seqcount_LOCKNAME_t) instead.
57 *
58 * If it's desired to automatically handle the sequence counter writer
59 * serialization and non-preemptibility requirements, use a sequential
60 * lock (seqlock_t) instead.
61 *
62 * See Documentation/locking/seqlock.rst
63 */
64 typedef struct seqcount {
65 unsigned sequence;
66 #ifdef CONFIG_DEBUG_LOCK_ALLOC
67 struct lockdep_map dep_map;
68 #endif
69 } seqcount_t;
70
__seqcount_init(seqcount_t * s,const char * name,struct lock_class_key * key)71 static inline void __seqcount_init(seqcount_t *s, const char *name,
72 struct lock_class_key *key)
73 {
74 /*
75 * Make sure we are not reinitializing a held lock:
76 */
77 lockdep_init_map(&s->dep_map, name, key, 0);
78 s->sequence = 0;
79 }
80
81 #ifdef CONFIG_DEBUG_LOCK_ALLOC
82
83 # define SEQCOUNT_DEP_MAP_INIT(lockname) \
84 .dep_map = { .name = #lockname }
85
86 /**
87 * seqcount_init() - runtime initializer for seqcount_t
88 * @s: Pointer to the seqcount_t instance
89 */
90 # define seqcount_init(s) \
91 do { \
92 static struct lock_class_key __key; \
93 __seqcount_init((s), #s, &__key); \
94 } while (0)
95
seqcount_lockdep_reader_access(const seqcount_t * s)96 static inline void seqcount_lockdep_reader_access(const seqcount_t *s)
97 {
98 seqcount_t *l = (seqcount_t *)s;
99 unsigned long flags;
100
101 local_irq_save(flags);
102 seqcount_acquire_read(&l->dep_map, 0, 0, _RET_IP_);
103 seqcount_release(&l->dep_map, _RET_IP_);
104 local_irq_restore(flags);
105 }
106
107 #else
108 # define SEQCOUNT_DEP_MAP_INIT(lockname)
109 # define seqcount_init(s) __seqcount_init(s, NULL, NULL)
110 # define seqcount_lockdep_reader_access(x)
111 #endif
112
113 /**
114 * SEQCNT_ZERO() - static initializer for seqcount_t
115 * @name: Name of the seqcount_t instance
116 */
117 #define SEQCNT_ZERO(name) { .sequence = 0, SEQCOUNT_DEP_MAP_INIT(name) }
118
119 /*
120 * Sequence counters with associated locks (seqcount_LOCKNAME_t)
121 *
122 * A sequence counter which associates the lock used for writer
123 * serialization at initialization time. This enables lockdep to validate
124 * that the write side critical section is properly serialized.
125 *
126 * For associated locks which do not implicitly disable preemption,
127 * preemption protection is enforced in the write side function.
128 *
129 * Lockdep is never used in any for the raw write variants.
130 *
131 * See Documentation/locking/seqlock.rst
132 */
133
134 /*
135 * For PREEMPT_RT, seqcount_LOCKNAME_t write side critical sections cannot
136 * disable preemption. It can lead to higher latencies, and the write side
137 * sections will not be able to acquire locks which become sleeping locks
138 * (e.g. spinlock_t).
139 *
140 * To remain preemptible while avoiding a possible livelock caused by the
141 * reader preempting the writer, use a different technique: let the reader
142 * detect if a seqcount_LOCKNAME_t writer is in progress. If that is the
143 * case, acquire then release the associated LOCKNAME writer serialization
144 * lock. This will allow any possibly-preempted writer to make progress
145 * until the end of its writer serialization lock critical section.
146 *
147 * This lock-unlock technique must be implemented for all of PREEMPT_RT
148 * sleeping locks. See Documentation/locking/locktypes.rst
149 */
150 #if defined(CONFIG_LOCKDEP) || defined(CONFIG_PREEMPT_RT)
151 #define __SEQ_LOCK(expr) expr
152 #else
153 #define __SEQ_LOCK(expr)
154 #endif
155
156 /*
157 * typedef seqcount_LOCKNAME_t - sequence counter with LOCKNAME associated
158 * @seqcount: The real sequence counter
159 * @lock: Pointer to the associated lock
160 *
161 * A plain sequence counter with external writer synchronization by
162 * LOCKNAME @lock. The lock is associated to the sequence counter in the
163 * static initializer or init function. This enables lockdep to validate
164 * that the write side critical section is properly serialized.
165 *
166 * LOCKNAME: raw_spinlock, spinlock, rwlock or mutex
167 */
168
169 /*
170 * seqcount_LOCKNAME_init() - runtime initializer for seqcount_LOCKNAME_t
171 * @s: Pointer to the seqcount_LOCKNAME_t instance
172 * @lock: Pointer to the associated lock
173 */
174
175 #define seqcount_LOCKNAME_init(s, _lock, lockname) \
176 do { \
177 seqcount_##lockname##_t *____s = (s); \
178 seqcount_init(&____s->seqcount); \
179 __SEQ_LOCK(____s->lock = (_lock)); \
180 } while (0)
181
182 #define seqcount_raw_spinlock_init(s, lock) seqcount_LOCKNAME_init(s, lock, raw_spinlock)
183 #define seqcount_spinlock_init(s, lock) seqcount_LOCKNAME_init(s, lock, spinlock)
184 #define seqcount_rwlock_init(s, lock) seqcount_LOCKNAME_init(s, lock, rwlock)
185 #define seqcount_mutex_init(s, lock) seqcount_LOCKNAME_init(s, lock, mutex)
186
187 /*
188 * SEQCOUNT_LOCKNAME() - Instantiate seqcount_LOCKNAME_t and helpers
189 * seqprop_LOCKNAME_*() - Property accessors for seqcount_LOCKNAME_t
190 *
191 * @lockname: "LOCKNAME" part of seqcount_LOCKNAME_t
192 * @locktype: LOCKNAME canonical C data type
193 * @preemptible: preemptibility of above locktype
194 * @lockmember: argument for lockdep_assert_held()
195 * @lockbase: associated lock release function (prefix only)
196 * @lock_acquire: associated lock acquisition function (full call)
197 */
198 #define SEQCOUNT_LOCKNAME(lockname, locktype, preemptible, lockmember, lockbase, lock_acquire) \
199 typedef struct seqcount_##lockname { \
200 seqcount_t seqcount; \
201 __SEQ_LOCK(locktype *lock); \
202 } seqcount_##lockname##_t; \
203 \
204 static __always_inline seqcount_t * \
205 __seqprop_##lockname##_ptr(seqcount_##lockname##_t *s) \
206 { \
207 return &s->seqcount; \
208 } \
209 \
210 static __always_inline unsigned \
211 __seqprop_##lockname##_sequence(const seqcount_##lockname##_t *s) \
212 { \
213 unsigned seq = READ_ONCE(s->seqcount.sequence); \
214 \
215 if (!IS_ENABLED(CONFIG_PREEMPT_RT)) \
216 return seq; \
217 \
218 if (preemptible && unlikely(seq & 1)) { \
219 __SEQ_LOCK(lock_acquire); \
220 __SEQ_LOCK(lockbase##_unlock(s->lock)); \
221 \
222 /* \
223 * Re-read the sequence counter since the (possibly \
224 * preempted) writer made progress. \
225 */ \
226 seq = READ_ONCE(s->seqcount.sequence); \
227 } \
228 \
229 return seq; \
230 } \
231 \
232 static __always_inline bool \
233 __seqprop_##lockname##_preemptible(const seqcount_##lockname##_t *s) \
234 { \
235 if (!IS_ENABLED(CONFIG_PREEMPT_RT)) \
236 return preemptible; \
237 \
238 /* PREEMPT_RT relies on the above LOCK+UNLOCK */ \
239 return false; \
240 } \
241 \
242 static __always_inline void \
243 __seqprop_##lockname##_assert(const seqcount_##lockname##_t *s) \
244 { \
245 __SEQ_LOCK(lockdep_assert_held(lockmember)); \
246 }
247
248 /*
249 * __seqprop() for seqcount_t
250 */
251
__seqprop_ptr(seqcount_t * s)252 static inline seqcount_t *__seqprop_ptr(seqcount_t *s)
253 {
254 return s;
255 }
256
__seqprop_sequence(const seqcount_t * s)257 static inline unsigned __seqprop_sequence(const seqcount_t *s)
258 {
259 return READ_ONCE(s->sequence);
260 }
261
__seqprop_preemptible(const seqcount_t * s)262 static inline bool __seqprop_preemptible(const seqcount_t *s)
263 {
264 return false;
265 }
266
__seqprop_assert(const seqcount_t * s)267 static inline void __seqprop_assert(const seqcount_t *s)
268 {
269 lockdep_assert_preemption_disabled();
270 }
271
272 #define __SEQ_RT IS_ENABLED(CONFIG_PREEMPT_RT)
273
274 SEQCOUNT_LOCKNAME(raw_spinlock, raw_spinlock_t, false, s->lock, raw_spin, raw_spin_lock(s->lock))
275 SEQCOUNT_LOCKNAME(spinlock, spinlock_t, __SEQ_RT, s->lock, spin, spin_lock(s->lock))
276 SEQCOUNT_LOCKNAME(rwlock, rwlock_t, __SEQ_RT, s->lock, read, read_lock(s->lock))
277 SEQCOUNT_LOCKNAME(mutex, struct mutex, true, s->lock, mutex, mutex_lock(s->lock))
278
279 /*
280 * SEQCNT_LOCKNAME_ZERO - static initializer for seqcount_LOCKNAME_t
281 * @name: Name of the seqcount_LOCKNAME_t instance
282 * @lock: Pointer to the associated LOCKNAME
283 */
284
285 #define SEQCOUNT_LOCKNAME_ZERO(seq_name, assoc_lock) { \
286 .seqcount = SEQCNT_ZERO(seq_name.seqcount), \
287 __SEQ_LOCK(.lock = (assoc_lock)) \
288 }
289
290 #define SEQCNT_RAW_SPINLOCK_ZERO(name, lock) SEQCOUNT_LOCKNAME_ZERO(name, lock)
291 #define SEQCNT_SPINLOCK_ZERO(name, lock) SEQCOUNT_LOCKNAME_ZERO(name, lock)
292 #define SEQCNT_RWLOCK_ZERO(name, lock) SEQCOUNT_LOCKNAME_ZERO(name, lock)
293 #define SEQCNT_MUTEX_ZERO(name, lock) SEQCOUNT_LOCKNAME_ZERO(name, lock)
294 #define SEQCNT_WW_MUTEX_ZERO(name, lock) SEQCOUNT_LOCKNAME_ZERO(name, lock)
295
296 #define __seqprop_case(s, lockname, prop) \
297 seqcount_##lockname##_t: __seqprop_##lockname##_##prop((void *)(s))
298
299 #define __seqprop(s, prop) _Generic(*(s), \
300 seqcount_t: __seqprop_##prop((void *)(s)), \
301 __seqprop_case((s), raw_spinlock, prop), \
302 __seqprop_case((s), spinlock, prop), \
303 __seqprop_case((s), rwlock, prop), \
304 __seqprop_case((s), mutex, prop))
305
306 #define seqprop_ptr(s) __seqprop(s, ptr)
307 #define seqprop_sequence(s) __seqprop(s, sequence)
308 #define seqprop_preemptible(s) __seqprop(s, preemptible)
309 #define seqprop_assert(s) __seqprop(s, assert)
310
311 /**
312 * __read_seqcount_begin() - begin a seqcount_t read section w/o barrier
313 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
314 *
315 * __read_seqcount_begin is like read_seqcount_begin, but has no smp_rmb()
316 * barrier. Callers should ensure that smp_rmb() or equivalent ordering is
317 * provided before actually loading any of the variables that are to be
318 * protected in this critical section.
319 *
320 * Use carefully, only in critical code, and comment how the barrier is
321 * provided.
322 *
323 * Return: count to be passed to read_seqcount_retry()
324 */
325 #define __read_seqcount_begin(s) \
326 ({ \
327 unsigned __seq; \
328 \
329 while ((__seq = seqprop_sequence(s)) & 1) \
330 cpu_relax(); \
331 \
332 kcsan_atomic_next(KCSAN_SEQLOCK_REGION_MAX); \
333 __seq; \
334 })
335
336 /**
337 * raw_read_seqcount_begin() - begin a seqcount_t read section w/o lockdep
338 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
339 *
340 * Return: count to be passed to read_seqcount_retry()
341 */
342 #define raw_read_seqcount_begin(s) \
343 ({ \
344 unsigned _seq = __read_seqcount_begin(s); \
345 \
346 smp_rmb(); \
347 _seq; \
348 })
349
350 /**
351 * read_seqcount_begin() - begin a seqcount_t read critical section
352 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
353 *
354 * Return: count to be passed to read_seqcount_retry()
355 */
356 #define read_seqcount_begin(s) \
357 ({ \
358 seqcount_lockdep_reader_access(seqprop_ptr(s)); \
359 raw_read_seqcount_begin(s); \
360 })
361
362 /**
363 * raw_read_seqcount() - read the raw seqcount_t counter value
364 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
365 *
366 * raw_read_seqcount opens a read critical section of the given
367 * seqcount_t, without any lockdep checking, and without checking or
368 * masking the sequence counter LSB. Calling code is responsible for
369 * handling that.
370 *
371 * Return: count to be passed to read_seqcount_retry()
372 */
373 #define raw_read_seqcount(s) \
374 ({ \
375 unsigned __seq = seqprop_sequence(s); \
376 \
377 smp_rmb(); \
378 kcsan_atomic_next(KCSAN_SEQLOCK_REGION_MAX); \
379 __seq; \
380 })
381
382 /**
383 * raw_seqcount_begin() - begin a seqcount_t read critical section w/o
384 * lockdep and w/o counter stabilization
385 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
386 *
387 * raw_seqcount_begin opens a read critical section of the given
388 * seqcount_t. Unlike read_seqcount_begin(), this function will not wait
389 * for the count to stabilize. If a writer is active when it begins, it
390 * will fail the read_seqcount_retry() at the end of the read critical
391 * section instead of stabilizing at the beginning of it.
392 *
393 * Use this only in special kernel hot paths where the read section is
394 * small and has a high probability of success through other external
395 * means. It will save a single branching instruction.
396 *
397 * Return: count to be passed to read_seqcount_retry()
398 */
399 #define raw_seqcount_begin(s) \
400 ({ \
401 /* \
402 * If the counter is odd, let read_seqcount_retry() fail \
403 * by decrementing the counter. \
404 */ \
405 raw_read_seqcount(s) & ~1; \
406 })
407
408 /**
409 * __read_seqcount_retry() - end a seqcount_t read section w/o barrier
410 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
411 * @start: count, from read_seqcount_begin()
412 *
413 * __read_seqcount_retry is like read_seqcount_retry, but has no smp_rmb()
414 * barrier. Callers should ensure that smp_rmb() or equivalent ordering is
415 * provided before actually loading any of the variables that are to be
416 * protected in this critical section.
417 *
418 * Use carefully, only in critical code, and comment how the barrier is
419 * provided.
420 *
421 * Return: true if a read section retry is required, else false
422 */
423 #define __read_seqcount_retry(s, start) \
424 do___read_seqcount_retry(seqprop_ptr(s), start)
425
do___read_seqcount_retry(const seqcount_t * s,unsigned start)426 static inline int do___read_seqcount_retry(const seqcount_t *s, unsigned start)
427 {
428 kcsan_atomic_next(0);
429 return unlikely(READ_ONCE(s->sequence) != start);
430 }
431
432 /**
433 * read_seqcount_retry() - end a seqcount_t read critical section
434 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
435 * @start: count, from read_seqcount_begin()
436 *
437 * read_seqcount_retry closes the read critical section of given
438 * seqcount_t. If the critical section was invalid, it must be ignored
439 * (and typically retried).
440 *
441 * Return: true if a read section retry is required, else false
442 */
443 #define read_seqcount_retry(s, start) \
444 do_read_seqcount_retry(seqprop_ptr(s), start)
445
do_read_seqcount_retry(const seqcount_t * s,unsigned start)446 static inline int do_read_seqcount_retry(const seqcount_t *s, unsigned start)
447 {
448 smp_rmb();
449 return do___read_seqcount_retry(s, start);
450 }
451
452 /**
453 * raw_write_seqcount_begin() - start a seqcount_t write section w/o lockdep
454 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
455 *
456 * Context: check write_seqcount_begin()
457 */
458 #define raw_write_seqcount_begin(s) \
459 do { \
460 if (seqprop_preemptible(s)) \
461 preempt_disable(); \
462 \
463 do_raw_write_seqcount_begin(seqprop_ptr(s)); \
464 } while (0)
465
do_raw_write_seqcount_begin(seqcount_t * s)466 static inline void do_raw_write_seqcount_begin(seqcount_t *s)
467 {
468 kcsan_nestable_atomic_begin();
469 s->sequence++;
470 smp_wmb();
471 }
472
473 /**
474 * raw_write_seqcount_end() - end a seqcount_t write section w/o lockdep
475 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
476 *
477 * Context: check write_seqcount_end()
478 */
479 #define raw_write_seqcount_end(s) \
480 do { \
481 do_raw_write_seqcount_end(seqprop_ptr(s)); \
482 \
483 if (seqprop_preemptible(s)) \
484 preempt_enable(); \
485 } while (0)
486
do_raw_write_seqcount_end(seqcount_t * s)487 static inline void do_raw_write_seqcount_end(seqcount_t *s)
488 {
489 smp_wmb();
490 s->sequence++;
491 kcsan_nestable_atomic_end();
492 }
493
494 /**
495 * write_seqcount_begin_nested() - start a seqcount_t write section with
496 * custom lockdep nesting level
497 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
498 * @subclass: lockdep nesting level
499 *
500 * See Documentation/locking/lockdep-design.rst
501 * Context: check write_seqcount_begin()
502 */
503 #define write_seqcount_begin_nested(s, subclass) \
504 do { \
505 seqprop_assert(s); \
506 \
507 if (seqprop_preemptible(s)) \
508 preempt_disable(); \
509 \
510 do_write_seqcount_begin_nested(seqprop_ptr(s), subclass); \
511 } while (0)
512
do_write_seqcount_begin_nested(seqcount_t * s,int subclass)513 static inline void do_write_seqcount_begin_nested(seqcount_t *s, int subclass)
514 {
515 seqcount_acquire(&s->dep_map, subclass, 0, _RET_IP_);
516 do_raw_write_seqcount_begin(s);
517 }
518
519 /**
520 * write_seqcount_begin() - start a seqcount_t write side critical section
521 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
522 *
523 * Context: sequence counter write side sections must be serialized and
524 * non-preemptible. Preemption will be automatically disabled if and
525 * only if the seqcount write serialization lock is associated, and
526 * preemptible. If readers can be invoked from hardirq or softirq
527 * context, interrupts or bottom halves must be respectively disabled.
528 */
529 #define write_seqcount_begin(s) \
530 do { \
531 seqprop_assert(s); \
532 \
533 if (seqprop_preemptible(s)) \
534 preempt_disable(); \
535 \
536 do_write_seqcount_begin(seqprop_ptr(s)); \
537 } while (0)
538
do_write_seqcount_begin(seqcount_t * s)539 static inline void do_write_seqcount_begin(seqcount_t *s)
540 {
541 do_write_seqcount_begin_nested(s, 0);
542 }
543
544 /**
545 * write_seqcount_end() - end a seqcount_t write side critical section
546 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
547 *
548 * Context: Preemption will be automatically re-enabled if and only if
549 * the seqcount write serialization lock is associated, and preemptible.
550 */
551 #define write_seqcount_end(s) \
552 do { \
553 do_write_seqcount_end(seqprop_ptr(s)); \
554 \
555 if (seqprop_preemptible(s)) \
556 preempt_enable(); \
557 } while (0)
558
do_write_seqcount_end(seqcount_t * s)559 static inline void do_write_seqcount_end(seqcount_t *s)
560 {
561 seqcount_release(&s->dep_map, _RET_IP_);
562 do_raw_write_seqcount_end(s);
563 }
564
565 /**
566 * raw_write_seqcount_barrier() - do a seqcount_t write barrier
567 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
568 *
569 * This can be used to provide an ordering guarantee instead of the usual
570 * consistency guarantee. It is one wmb cheaper, because it can collapse
571 * the two back-to-back wmb()s.
572 *
573 * Note that writes surrounding the barrier should be declared atomic (e.g.
574 * via WRITE_ONCE): a) to ensure the writes become visible to other threads
575 * atomically, avoiding compiler optimizations; b) to document which writes are
576 * meant to propagate to the reader critical section. This is necessary because
577 * neither writes before and after the barrier are enclosed in a seq-writer
578 * critical section that would ensure readers are aware of ongoing writes::
579 *
580 * seqcount_t seq;
581 * bool X = true, Y = false;
582 *
583 * void read(void)
584 * {
585 * bool x, y;
586 *
587 * do {
588 * int s = read_seqcount_begin(&seq);
589 *
590 * x = X; y = Y;
591 *
592 * } while (read_seqcount_retry(&seq, s));
593 *
594 * BUG_ON(!x && !y);
595 * }
596 *
597 * void write(void)
598 * {
599 * WRITE_ONCE(Y, true);
600 *
601 * raw_write_seqcount_barrier(seq);
602 *
603 * WRITE_ONCE(X, false);
604 * }
605 */
606 #define raw_write_seqcount_barrier(s) \
607 do_raw_write_seqcount_barrier(seqprop_ptr(s))
608
do_raw_write_seqcount_barrier(seqcount_t * s)609 static inline void do_raw_write_seqcount_barrier(seqcount_t *s)
610 {
611 kcsan_nestable_atomic_begin();
612 s->sequence++;
613 smp_wmb();
614 s->sequence++;
615 kcsan_nestable_atomic_end();
616 }
617
618 /**
619 * write_seqcount_invalidate() - invalidate in-progress seqcount_t read
620 * side operations
621 * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
622 *
623 * After write_seqcount_invalidate, no seqcount_t read side operations
624 * will complete successfully and see data older than this.
625 */
626 #define write_seqcount_invalidate(s) \
627 do_write_seqcount_invalidate(seqprop_ptr(s))
628
do_write_seqcount_invalidate(seqcount_t * s)629 static inline void do_write_seqcount_invalidate(seqcount_t *s)
630 {
631 smp_wmb();
632 kcsan_nestable_atomic_begin();
633 s->sequence+=2;
634 kcsan_nestable_atomic_end();
635 }
636
637 /*
638 * Latch sequence counters (seqcount_latch_t)
639 *
640 * A sequence counter variant where the counter even/odd value is used to
641 * switch between two copies of protected data. This allows the read path,
642 * typically NMIs, to safely interrupt the write side critical section.
643 *
644 * As the write sections are fully preemptible, no special handling for
645 * PREEMPT_RT is needed.
646 */
647 typedef struct {
648 seqcount_t seqcount;
649 } seqcount_latch_t;
650
651 /**
652 * SEQCNT_LATCH_ZERO() - static initializer for seqcount_latch_t
653 * @seq_name: Name of the seqcount_latch_t instance
654 */
655 #define SEQCNT_LATCH_ZERO(seq_name) { \
656 .seqcount = SEQCNT_ZERO(seq_name.seqcount), \
657 }
658
659 /**
660 * seqcount_latch_init() - runtime initializer for seqcount_latch_t
661 * @s: Pointer to the seqcount_latch_t instance
662 */
663 #define seqcount_latch_init(s) seqcount_init(&(s)->seqcount)
664
665 /**
666 * raw_read_seqcount_latch() - pick even/odd latch data copy
667 * @s: Pointer to seqcount_latch_t
668 *
669 * See raw_write_seqcount_latch() for details and a full reader/writer
670 * usage example.
671 *
672 * Return: sequence counter raw value. Use the lowest bit as an index for
673 * picking which data copy to read. The full counter must then be checked
674 * with raw_read_seqcount_latch_retry().
675 */
raw_read_seqcount_latch(const seqcount_latch_t * s)676 static __always_inline unsigned raw_read_seqcount_latch(const seqcount_latch_t *s)
677 {
678 /*
679 * Pairs with the first smp_wmb() in raw_write_seqcount_latch().
680 * Due to the dependent load, a full smp_rmb() is not needed.
681 */
682 return READ_ONCE(s->seqcount.sequence);
683 }
684
685 /**
686 * read_seqcount_latch() - pick even/odd latch data copy
687 * @s: Pointer to seqcount_latch_t
688 *
689 * See write_seqcount_latch() for details and a full reader/writer usage
690 * example.
691 *
692 * Return: sequence counter raw value. Use the lowest bit as an index for
693 * picking which data copy to read. The full counter must then be checked
694 * with read_seqcount_latch_retry().
695 */
read_seqcount_latch(const seqcount_latch_t * s)696 static __always_inline unsigned read_seqcount_latch(const seqcount_latch_t *s)
697 {
698 kcsan_atomic_next(KCSAN_SEQLOCK_REGION_MAX);
699 return raw_read_seqcount_latch(s);
700 }
701
702 /**
703 * raw_read_seqcount_latch_retry() - end a seqcount_latch_t read section
704 * @s: Pointer to seqcount_latch_t
705 * @start: count, from raw_read_seqcount_latch()
706 *
707 * Return: true if a read section retry is required, else false
708 */
709 static __always_inline int
raw_read_seqcount_latch_retry(const seqcount_latch_t * s,unsigned start)710 raw_read_seqcount_latch_retry(const seqcount_latch_t *s, unsigned start)
711 {
712 smp_rmb();
713 return unlikely(READ_ONCE(s->seqcount.sequence) != start);
714 }
715
716 /**
717 * read_seqcount_latch_retry() - end a seqcount_latch_t read section
718 * @s: Pointer to seqcount_latch_t
719 * @start: count, from read_seqcount_latch()
720 *
721 * Return: true if a read section retry is required, else false
722 */
723 static __always_inline int
read_seqcount_latch_retry(const seqcount_latch_t * s,unsigned start)724 read_seqcount_latch_retry(const seqcount_latch_t *s, unsigned start)
725 {
726 kcsan_atomic_next(0);
727 return raw_read_seqcount_latch_retry(s, start);
728 }
729
730 /**
731 * raw_write_seqcount_latch() - redirect latch readers to even/odd copy
732 * @s: Pointer to seqcount_latch_t
733 */
raw_write_seqcount_latch(seqcount_latch_t * s)734 static __always_inline void raw_write_seqcount_latch(seqcount_latch_t *s)
735 {
736 smp_wmb(); /* prior stores before incrementing "sequence" */
737 s->seqcount.sequence++;
738 smp_wmb(); /* increment "sequence" before following stores */
739 }
740
741 /**
742 * write_seqcount_latch_begin() - redirect latch readers to odd copy
743 * @s: Pointer to seqcount_latch_t
744 *
745 * The latch technique is a multiversion concurrency control method that allows
746 * queries during non-atomic modifications. If you can guarantee queries never
747 * interrupt the modification -- e.g. the concurrency is strictly between CPUs
748 * -- you most likely do not need this.
749 *
750 * Where the traditional RCU/lockless data structures rely on atomic
751 * modifications to ensure queries observe either the old or the new state the
752 * latch allows the same for non-atomic updates. The trade-off is doubling the
753 * cost of storage; we have to maintain two copies of the entire data
754 * structure.
755 *
756 * Very simply put: we first modify one copy and then the other. This ensures
757 * there is always one copy in a stable state, ready to give us an answer.
758 *
759 * The basic form is a data structure like::
760 *
761 * struct latch_struct {
762 * seqcount_latch_t seq;
763 * struct data_struct data[2];
764 * };
765 *
766 * Where a modification, which is assumed to be externally serialized, does the
767 * following::
768 *
769 * void latch_modify(struct latch_struct *latch, ...)
770 * {
771 * write_seqcount_latch_begin(&latch->seq);
772 * modify(latch->data[0], ...);
773 * write_seqcount_latch(&latch->seq);
774 * modify(latch->data[1], ...);
775 * write_seqcount_latch_end(&latch->seq);
776 * }
777 *
778 * The query will have a form like::
779 *
780 * struct entry *latch_query(struct latch_struct *latch, ...)
781 * {
782 * struct entry *entry;
783 * unsigned seq, idx;
784 *
785 * do {
786 * seq = read_seqcount_latch(&latch->seq);
787 *
788 * idx = seq & 0x01;
789 * entry = data_query(latch->data[idx], ...);
790 *
791 * // This includes needed smp_rmb()
792 * } while (read_seqcount_latch_retry(&latch->seq, seq));
793 *
794 * return entry;
795 * }
796 *
797 * So during the modification, queries are first redirected to data[1]. Then we
798 * modify data[0]. When that is complete, we redirect queries back to data[0]
799 * and we can modify data[1].
800 *
801 * NOTE:
802 *
803 * The non-requirement for atomic modifications does _NOT_ include
804 * the publishing of new entries in the case where data is a dynamic
805 * data structure.
806 *
807 * An iteration might start in data[0] and get suspended long enough
808 * to miss an entire modification sequence, once it resumes it might
809 * observe the new entry.
810 *
811 * NOTE2:
812 *
813 * When data is a dynamic data structure; one should use regular RCU
814 * patterns to manage the lifetimes of the objects within.
815 */
write_seqcount_latch_begin(seqcount_latch_t * s)816 static __always_inline void write_seqcount_latch_begin(seqcount_latch_t *s)
817 {
818 kcsan_nestable_atomic_begin();
819 raw_write_seqcount_latch(s);
820 }
821
822 /**
823 * write_seqcount_latch() - redirect latch readers to even copy
824 * @s: Pointer to seqcount_latch_t
825 */
write_seqcount_latch(seqcount_latch_t * s)826 static __always_inline void write_seqcount_latch(seqcount_latch_t *s)
827 {
828 raw_write_seqcount_latch(s);
829 }
830
831 /**
832 * write_seqcount_latch_end() - end a seqcount_latch_t write section
833 * @s: Pointer to seqcount_latch_t
834 *
835 * Marks the end of a seqcount_latch_t writer section, after all copies of the
836 * latch-protected data have been updated.
837 */
write_seqcount_latch_end(seqcount_latch_t * s)838 static __always_inline void write_seqcount_latch_end(seqcount_latch_t *s)
839 {
840 kcsan_nestable_atomic_end();
841 }
842
843 /*
844 * Sequential locks (seqlock_t)
845 *
846 * Sequence counters with an embedded spinlock for writer serialization
847 * and non-preemptibility.
848 *
849 * For more info, see:
850 * - Comments on top of seqcount_t
851 * - Documentation/locking/seqlock.rst
852 */
853 typedef struct {
854 /*
855 * Make sure that readers don't starve writers on PREEMPT_RT: use
856 * seqcount_spinlock_t instead of seqcount_t. Check __SEQ_LOCK().
857 */
858 seqcount_spinlock_t seqcount;
859 spinlock_t lock;
860 } seqlock_t;
861
862 #define __SEQLOCK_UNLOCKED(lockname) \
863 { \
864 .seqcount = SEQCNT_SPINLOCK_ZERO(lockname, &(lockname).lock), \
865 .lock = __SPIN_LOCK_UNLOCKED(lockname) \
866 }
867
868 /**
869 * seqlock_init() - dynamic initializer for seqlock_t
870 * @sl: Pointer to the seqlock_t instance
871 */
872 #define seqlock_init(sl) \
873 do { \
874 spin_lock_init(&(sl)->lock); \
875 seqcount_spinlock_init(&(sl)->seqcount, &(sl)->lock); \
876 } while (0)
877
878 /**
879 * DEFINE_SEQLOCK(sl) - Define a statically allocated seqlock_t
880 * @sl: Name of the seqlock_t instance
881 */
882 #define DEFINE_SEQLOCK(sl) \
883 seqlock_t sl = __SEQLOCK_UNLOCKED(sl)
884
885 /**
886 * read_seqbegin() - start a seqlock_t read side critical section
887 * @sl: Pointer to seqlock_t
888 *
889 * Return: count, to be passed to read_seqretry()
890 */
read_seqbegin(const seqlock_t * sl)891 static inline unsigned read_seqbegin(const seqlock_t *sl)
892 {
893 return read_seqcount_begin(&sl->seqcount);
894 }
895
896 /**
897 * read_seqretry() - end a seqlock_t read side section
898 * @sl: Pointer to seqlock_t
899 * @start: count, from read_seqbegin()
900 *
901 * read_seqretry closes the read side critical section of given seqlock_t.
902 * If the critical section was invalid, it must be ignored (and typically
903 * retried).
904 *
905 * Return: true if a read section retry is required, else false
906 */
read_seqretry(const seqlock_t * sl,unsigned start)907 static inline unsigned read_seqretry(const seqlock_t *sl, unsigned start)
908 {
909 return read_seqcount_retry(&sl->seqcount, start);
910 }
911
912 /*
913 * For all seqlock_t write side functions, use the the internal
914 * do_write_seqcount_begin() instead of generic write_seqcount_begin().
915 * This way, no redundant lockdep_assert_held() checks are added.
916 */
917
918 /**
919 * write_seqlock() - start a seqlock_t write side critical section
920 * @sl: Pointer to seqlock_t
921 *
922 * write_seqlock opens a write side critical section for the given
923 * seqlock_t. It also implicitly acquires the spinlock_t embedded inside
924 * that sequential lock. All seqlock_t write side sections are thus
925 * automatically serialized and non-preemptible.
926 *
927 * Context: if the seqlock_t read section, or other write side critical
928 * sections, can be invoked from hardirq or softirq contexts, use the
929 * _irqsave or _bh variants of this function instead.
930 */
write_seqlock(seqlock_t * sl)931 static inline void write_seqlock(seqlock_t *sl)
932 {
933 spin_lock(&sl->lock);
934 do_write_seqcount_begin(&sl->seqcount.seqcount);
935 }
936
937 /**
938 * write_sequnlock() - end a seqlock_t write side critical section
939 * @sl: Pointer to seqlock_t
940 *
941 * write_sequnlock closes the (serialized and non-preemptible) write side
942 * critical section of given seqlock_t.
943 */
write_sequnlock(seqlock_t * sl)944 static inline void write_sequnlock(seqlock_t *sl)
945 {
946 do_write_seqcount_end(&sl->seqcount.seqcount);
947 spin_unlock(&sl->lock);
948 }
949
950 /**
951 * write_seqlock_bh() - start a softirqs-disabled seqlock_t write section
952 * @sl: Pointer to seqlock_t
953 *
954 * _bh variant of write_seqlock(). Use only if the read side section, or
955 * other write side sections, can be invoked from softirq contexts.
956 */
write_seqlock_bh(seqlock_t * sl)957 static inline void write_seqlock_bh(seqlock_t *sl)
958 {
959 spin_lock_bh(&sl->lock);
960 do_write_seqcount_begin(&sl->seqcount.seqcount);
961 }
962
963 /**
964 * write_sequnlock_bh() - end a softirqs-disabled seqlock_t write section
965 * @sl: Pointer to seqlock_t
966 *
967 * write_sequnlock_bh closes the serialized, non-preemptible, and
968 * softirqs-disabled, seqlock_t write side critical section opened with
969 * write_seqlock_bh().
970 */
write_sequnlock_bh(seqlock_t * sl)971 static inline void write_sequnlock_bh(seqlock_t *sl)
972 {
973 do_write_seqcount_end(&sl->seqcount.seqcount);
974 spin_unlock_bh(&sl->lock);
975 }
976
977 /**
978 * write_seqlock_irq() - start a non-interruptible seqlock_t write section
979 * @sl: Pointer to seqlock_t
980 *
981 * _irq variant of write_seqlock(). Use only if the read side section, or
982 * other write sections, can be invoked from hardirq contexts.
983 */
write_seqlock_irq(seqlock_t * sl)984 static inline void write_seqlock_irq(seqlock_t *sl)
985 {
986 spin_lock_irq(&sl->lock);
987 do_write_seqcount_begin(&sl->seqcount.seqcount);
988 }
989
990 /**
991 * write_sequnlock_irq() - end a non-interruptible seqlock_t write section
992 * @sl: Pointer to seqlock_t
993 *
994 * write_sequnlock_irq closes the serialized and non-interruptible
995 * seqlock_t write side section opened with write_seqlock_irq().
996 */
write_sequnlock_irq(seqlock_t * sl)997 static inline void write_sequnlock_irq(seqlock_t *sl)
998 {
999 do_write_seqcount_end(&sl->seqcount.seqcount);
1000 spin_unlock_irq(&sl->lock);
1001 }
1002
__write_seqlock_irqsave(seqlock_t * sl)1003 static inline unsigned long __write_seqlock_irqsave(seqlock_t *sl)
1004 {
1005 unsigned long flags;
1006
1007 spin_lock_irqsave(&sl->lock, flags);
1008 do_write_seqcount_begin(&sl->seqcount.seqcount);
1009 return flags;
1010 }
1011
1012 /**
1013 * write_seqlock_irqsave() - start a non-interruptible seqlock_t write
1014 * section
1015 * @lock: Pointer to seqlock_t
1016 * @flags: Stack-allocated storage for saving caller's local interrupt
1017 * state, to be passed to write_sequnlock_irqrestore().
1018 *
1019 * _irqsave variant of write_seqlock(). Use it only if the read side
1020 * section, or other write sections, can be invoked from hardirq context.
1021 */
1022 #define write_seqlock_irqsave(lock, flags) \
1023 do { flags = __write_seqlock_irqsave(lock); } while (0)
1024
1025 /**
1026 * write_sequnlock_irqrestore() - end non-interruptible seqlock_t write
1027 * section
1028 * @sl: Pointer to seqlock_t
1029 * @flags: Caller's saved interrupt state, from write_seqlock_irqsave()
1030 *
1031 * write_sequnlock_irqrestore closes the serialized and non-interruptible
1032 * seqlock_t write section previously opened with write_seqlock_irqsave().
1033 */
1034 static inline void
write_sequnlock_irqrestore(seqlock_t * sl,unsigned long flags)1035 write_sequnlock_irqrestore(seqlock_t *sl, unsigned long flags)
1036 {
1037 do_write_seqcount_end(&sl->seqcount.seqcount);
1038 spin_unlock_irqrestore(&sl->lock, flags);
1039 }
1040
1041 /**
1042 * read_seqlock_excl() - begin a seqlock_t locking reader section
1043 * @sl: Pointer to seqlock_t
1044 *
1045 * read_seqlock_excl opens a seqlock_t locking reader critical section. A
1046 * locking reader exclusively locks out *both* other writers *and* other
1047 * locking readers, but it does not update the embedded sequence number.
1048 *
1049 * Locking readers act like a normal spin_lock()/spin_unlock().
1050 *
1051 * Context: if the seqlock_t write section, *or other read sections*, can
1052 * be invoked from hardirq or softirq contexts, use the _irqsave or _bh
1053 * variant of this function instead.
1054 *
1055 * The opened read section must be closed with read_sequnlock_excl().
1056 */
read_seqlock_excl(seqlock_t * sl)1057 static inline void read_seqlock_excl(seqlock_t *sl)
1058 {
1059 spin_lock(&sl->lock);
1060 }
1061
1062 /**
1063 * read_sequnlock_excl() - end a seqlock_t locking reader critical section
1064 * @sl: Pointer to seqlock_t
1065 */
read_sequnlock_excl(seqlock_t * sl)1066 static inline void read_sequnlock_excl(seqlock_t *sl)
1067 {
1068 spin_unlock(&sl->lock);
1069 }
1070
1071 /**
1072 * read_seqlock_excl_bh() - start a seqlock_t locking reader section with
1073 * softirqs disabled
1074 * @sl: Pointer to seqlock_t
1075 *
1076 * _bh variant of read_seqlock_excl(). Use this variant only if the
1077 * seqlock_t write side section, *or other read sections*, can be invoked
1078 * from softirq contexts.
1079 */
read_seqlock_excl_bh(seqlock_t * sl)1080 static inline void read_seqlock_excl_bh(seqlock_t *sl)
1081 {
1082 spin_lock_bh(&sl->lock);
1083 }
1084
1085 /**
1086 * read_sequnlock_excl_bh() - stop a seqlock_t softirq-disabled locking
1087 * reader section
1088 * @sl: Pointer to seqlock_t
1089 */
read_sequnlock_excl_bh(seqlock_t * sl)1090 static inline void read_sequnlock_excl_bh(seqlock_t *sl)
1091 {
1092 spin_unlock_bh(&sl->lock);
1093 }
1094
1095 /**
1096 * read_seqlock_excl_irq() - start a non-interruptible seqlock_t locking
1097 * reader section
1098 * @sl: Pointer to seqlock_t
1099 *
1100 * _irq variant of read_seqlock_excl(). Use this only if the seqlock_t
1101 * write side section, *or other read sections*, can be invoked from a
1102 * hardirq context.
1103 */
read_seqlock_excl_irq(seqlock_t * sl)1104 static inline void read_seqlock_excl_irq(seqlock_t *sl)
1105 {
1106 spin_lock_irq(&sl->lock);
1107 }
1108
1109 /**
1110 * read_sequnlock_excl_irq() - end an interrupts-disabled seqlock_t
1111 * locking reader section
1112 * @sl: Pointer to seqlock_t
1113 */
read_sequnlock_excl_irq(seqlock_t * sl)1114 static inline void read_sequnlock_excl_irq(seqlock_t *sl)
1115 {
1116 spin_unlock_irq(&sl->lock);
1117 }
1118
__read_seqlock_excl_irqsave(seqlock_t * sl)1119 static inline unsigned long __read_seqlock_excl_irqsave(seqlock_t *sl)
1120 {
1121 unsigned long flags;
1122
1123 spin_lock_irqsave(&sl->lock, flags);
1124 return flags;
1125 }
1126
1127 /**
1128 * read_seqlock_excl_irqsave() - start a non-interruptible seqlock_t
1129 * locking reader section
1130 * @lock: Pointer to seqlock_t
1131 * @flags: Stack-allocated storage for saving caller's local interrupt
1132 * state, to be passed to read_sequnlock_excl_irqrestore().
1133 *
1134 * _irqsave variant of read_seqlock_excl(). Use this only if the seqlock_t
1135 * write side section, *or other read sections*, can be invoked from a
1136 * hardirq context.
1137 */
1138 #define read_seqlock_excl_irqsave(lock, flags) \
1139 do { flags = __read_seqlock_excl_irqsave(lock); } while (0)
1140
1141 /**
1142 * read_sequnlock_excl_irqrestore() - end non-interruptible seqlock_t
1143 * locking reader section
1144 * @sl: Pointer to seqlock_t
1145 * @flags: Caller saved interrupt state, from read_seqlock_excl_irqsave()
1146 */
1147 static inline void
read_sequnlock_excl_irqrestore(seqlock_t * sl,unsigned long flags)1148 read_sequnlock_excl_irqrestore(seqlock_t *sl, unsigned long flags)
1149 {
1150 spin_unlock_irqrestore(&sl->lock, flags);
1151 }
1152
1153 /**
1154 * read_seqbegin_or_lock() - begin a seqlock_t lockless or locking reader
1155 * @lock: Pointer to seqlock_t
1156 * @seq : Marker and return parameter. If the passed value is even, the
1157 * reader will become a *lockless* seqlock_t reader as in read_seqbegin().
1158 * If the passed value is odd, the reader will become a *locking* reader
1159 * as in read_seqlock_excl(). In the first call to this function, the
1160 * caller *must* initialize and pass an even value to @seq; this way, a
1161 * lockless read can be optimistically tried first.
1162 *
1163 * read_seqbegin_or_lock is an API designed to optimistically try a normal
1164 * lockless seqlock_t read section first. If an odd counter is found, the
1165 * lockless read trial has failed, and the next read iteration transforms
1166 * itself into a full seqlock_t locking reader.
1167 *
1168 * This is typically used to avoid seqlock_t lockless readers starvation
1169 * (too much retry loops) in the case of a sharp spike in write side
1170 * activity.
1171 *
1172 * Context: if the seqlock_t write section, *or other read sections*, can
1173 * be invoked from hardirq or softirq contexts, use the _irqsave or _bh
1174 * variant of this function instead.
1175 *
1176 * Check Documentation/locking/seqlock.rst for template example code.
1177 *
1178 * Return: the encountered sequence counter value, through the @seq
1179 * parameter, which is overloaded as a return parameter. This returned
1180 * value must be checked with need_seqretry(). If the read section need to
1181 * be retried, this returned value must also be passed as the @seq
1182 * parameter of the next read_seqbegin_or_lock() iteration.
1183 */
read_seqbegin_or_lock(seqlock_t * lock,int * seq)1184 static inline void read_seqbegin_or_lock(seqlock_t *lock, int *seq)
1185 {
1186 if (!(*seq & 1)) /* Even */
1187 *seq = read_seqbegin(lock);
1188 else /* Odd */
1189 read_seqlock_excl(lock);
1190 }
1191
1192 /**
1193 * need_seqretry() - validate seqlock_t "locking or lockless" read section
1194 * @lock: Pointer to seqlock_t
1195 * @seq: sequence count, from read_seqbegin_or_lock()
1196 *
1197 * Return: true if a read section retry is required, false otherwise
1198 */
need_seqretry(seqlock_t * lock,int seq)1199 static inline int need_seqretry(seqlock_t *lock, int seq)
1200 {
1201 return !(seq & 1) && read_seqretry(lock, seq);
1202 }
1203
1204 /**
1205 * done_seqretry() - end seqlock_t "locking or lockless" reader section
1206 * @lock: Pointer to seqlock_t
1207 * @seq: count, from read_seqbegin_or_lock()
1208 *
1209 * done_seqretry finishes the seqlock_t read side critical section started
1210 * with read_seqbegin_or_lock() and validated by need_seqretry().
1211 */
done_seqretry(seqlock_t * lock,int seq)1212 static inline void done_seqretry(seqlock_t *lock, int seq)
1213 {
1214 if (seq & 1)
1215 read_sequnlock_excl(lock);
1216 }
1217
1218 /**
1219 * read_seqbegin_or_lock_irqsave() - begin a seqlock_t lockless reader, or
1220 * a non-interruptible locking reader
1221 * @lock: Pointer to seqlock_t
1222 * @seq: Marker and return parameter. Check read_seqbegin_or_lock().
1223 *
1224 * This is the _irqsave variant of read_seqbegin_or_lock(). Use it only if
1225 * the seqlock_t write section, *or other read sections*, can be invoked
1226 * from hardirq context.
1227 *
1228 * Note: Interrupts will be disabled only for "locking reader" mode.
1229 *
1230 * Return:
1231 *
1232 * 1. The saved local interrupts state in case of a locking reader, to
1233 * be passed to done_seqretry_irqrestore().
1234 *
1235 * 2. The encountered sequence counter value, returned through @seq
1236 * overloaded as a return parameter. Check read_seqbegin_or_lock().
1237 */
1238 static inline unsigned long
read_seqbegin_or_lock_irqsave(seqlock_t * lock,int * seq)1239 read_seqbegin_or_lock_irqsave(seqlock_t *lock, int *seq)
1240 {
1241 unsigned long flags = 0;
1242
1243 if (!(*seq & 1)) /* Even */
1244 *seq = read_seqbegin(lock);
1245 else /* Odd */
1246 read_seqlock_excl_irqsave(lock, flags);
1247
1248 return flags;
1249 }
1250
1251 /**
1252 * done_seqretry_irqrestore() - end a seqlock_t lockless reader, or a
1253 * non-interruptible locking reader section
1254 * @lock: Pointer to seqlock_t
1255 * @seq: Count, from read_seqbegin_or_lock_irqsave()
1256 * @flags: Caller's saved local interrupt state in case of a locking
1257 * reader, also from read_seqbegin_or_lock_irqsave()
1258 *
1259 * This is the _irqrestore variant of done_seqretry(). The read section
1260 * must've been opened with read_seqbegin_or_lock_irqsave(), and validated
1261 * by need_seqretry().
1262 */
1263 static inline void
done_seqretry_irqrestore(seqlock_t * lock,int seq,unsigned long flags)1264 done_seqretry_irqrestore(seqlock_t *lock, int seq, unsigned long flags)
1265 {
1266 if (seq & 1)
1267 read_sequnlock_excl_irqrestore(lock, flags);
1268 }
1269 #endif /* __LINUX_SEQLOCK_H */
1270