1 #ifndef __LINUX_SEQLOCK_H
2 #define __LINUX_SEQLOCK_H
4 * Reader/writer consistent mechanism without starving writers. This type of
5 * lock for data where the reader wants a consistent set of information
6 * and is willing to retry if the information changes. There are two types
8 * 1. Sequence readers which never block a writer but they may have to retry
9 * if a writer is in progress by detecting change in sequence number.
10 * Writers do not wait for a sequence reader.
11 * 2. Locking readers which will wait if a writer or another locking reader
12 * is in progress. A locking reader in progress will also block a writer
13 * from going forward. Unlike the regular rwlock, the read lock here is
14 * exclusive so that only one locking reader can get it.
16 * This is not as cache friendly as brlock. Also, this may not work well
17 * for data that contains pointers, because any writer could
18 * invalidate a pointer that a reader was following.
20 * Expected non-blocking reader usage:
22 * seq = read_seqbegin(&foo);
24 * } while (read_seqretry(&foo, seq));
27 * On non-SMP the spin locks disappear but the writer still needs
28 * to increment the sequence variables because an interrupt routine could
29 * change the state of the data.
31 * Based on x86_64 vsyscall gettimeofday
32 * by Keith Owens and Andrea Arcangeli
35 #include <linux/spinlock.h>
36 #include <linux/preempt.h>
37 #include <linux/lockdep.h>
38 #include <linux/compiler.h>
39 #include <asm/processor.h>
42 * Version using sequence counter only.
43 * This can be used when code has its own mutex protecting the
44 * updating starting before the write_seqcountbeqin() and ending
45 * after the write_seqcount_end().
47 typedef struct seqcount {
49 #ifdef CONFIG_DEBUG_LOCK_ALLOC
50 struct lockdep_map dep_map;
54 static inline void __seqcount_init(seqcount_t *s, const char *name,
55 struct lock_class_key *key)
58 * Make sure we are not reinitializing a held lock:
60 lockdep_init_map(&s->dep_map, name, key, 0);
64 #ifdef CONFIG_DEBUG_LOCK_ALLOC
65 # define SEQCOUNT_DEP_MAP_INIT(lockname) \
66 .dep_map = { .name = #lockname } \
68 # define seqcount_init(s) \
70 static struct lock_class_key __key; \
71 __seqcount_init((s), #s, &__key); \
74 static inline void seqcount_lockdep_reader_access(const seqcount_t *s)
76 seqcount_t *l = (seqcount_t *)s;
79 local_irq_save(flags);
80 seqcount_acquire_read(&l->dep_map, 0, 0, _RET_IP_);
81 seqcount_release(&l->dep_map, 1, _RET_IP_);
82 local_irq_restore(flags);
86 # define SEQCOUNT_DEP_MAP_INIT(lockname)
87 # define seqcount_init(s) __seqcount_init(s, NULL, NULL)
88 # define seqcount_lockdep_reader_access(x)
91 #define SEQCNT_ZERO(lockname) { .sequence = 0, SEQCOUNT_DEP_MAP_INIT(lockname)}
95 * __read_seqcount_begin - begin a seq-read critical section (without barrier)
96 * @s: pointer to seqcount_t
97 * Returns: count to be passed to read_seqcount_retry
99 * __read_seqcount_begin is like read_seqcount_begin, but has no smp_rmb()
100 * barrier. Callers should ensure that smp_rmb() or equivalent ordering is
101 * provided before actually loading any of the variables that are to be
102 * protected in this critical section.
104 * Use carefully, only in critical code, and comment how the barrier is
107 static inline unsigned __read_seqcount_begin(const seqcount_t *s)
112 ret = READ_ONCE(s->sequence);
113 if (unlikely(ret & 1)) {
121 * raw_read_seqcount - Read the raw seqcount
122 * @s: pointer to seqcount_t
123 * Returns: count to be passed to read_seqcount_retry
125 * raw_read_seqcount opens a read critical section of the given
126 * seqcount without any lockdep checking and without checking or
127 * masking the LSB. Calling code is responsible for handling that.
129 static inline unsigned raw_read_seqcount(const seqcount_t *s)
131 unsigned ret = READ_ONCE(s->sequence);
137 * raw_read_seqcount_begin - start seq-read critical section w/o lockdep
138 * @s: pointer to seqcount_t
139 * Returns: count to be passed to read_seqcount_retry
141 * raw_read_seqcount_begin opens a read critical section of the given
142 * seqcount, but without any lockdep checking. Validity of the critical
143 * section is tested by checking read_seqcount_retry function.
145 static inline unsigned raw_read_seqcount_begin(const seqcount_t *s)
147 unsigned ret = __read_seqcount_begin(s);
153 * read_seqcount_begin - begin a seq-read critical section
154 * @s: pointer to seqcount_t
155 * Returns: count to be passed to read_seqcount_retry
157 * read_seqcount_begin opens a read critical section of the given seqcount.
158 * Validity of the critical section is tested by checking read_seqcount_retry
161 static inline unsigned read_seqcount_begin(const seqcount_t *s)
163 seqcount_lockdep_reader_access(s);
164 return raw_read_seqcount_begin(s);
168 * raw_seqcount_begin - begin a seq-read critical section
169 * @s: pointer to seqcount_t
170 * Returns: count to be passed to read_seqcount_retry
172 * raw_seqcount_begin opens a read critical section of the given seqcount.
173 * Validity of the critical section is tested by checking read_seqcount_retry
176 * Unlike read_seqcount_begin(), this function will not wait for the count
177 * to stabilize. If a writer is active when we begin, we will fail the
178 * read_seqcount_retry() instead of stabilizing at the beginning of the
181 static inline unsigned raw_seqcount_begin(const seqcount_t *s)
183 unsigned ret = READ_ONCE(s->sequence);
189 * __read_seqcount_retry - end a seq-read critical section (without barrier)
190 * @s: pointer to seqcount_t
191 * @start: count, from read_seqcount_begin
192 * Returns: 1 if retry is required, else 0
194 * __read_seqcount_retry is like read_seqcount_retry, but has no smp_rmb()
195 * barrier. Callers should ensure that smp_rmb() or equivalent ordering is
196 * provided before actually loading any of the variables that are to be
197 * protected in this critical section.
199 * Use carefully, only in critical code, and comment how the barrier is
202 static inline int __read_seqcount_retry(const seqcount_t *s, unsigned start)
204 return unlikely(s->sequence != start);
208 * read_seqcount_retry - end a seq-read critical section
209 * @s: pointer to seqcount_t
210 * @start: count, from read_seqcount_begin
211 * Returns: 1 if retry is required, else 0
213 * read_seqcount_retry closes a read critical section of the given seqcount.
214 * If the critical section was invalid, it must be ignored (and typically
217 static inline int read_seqcount_retry(const seqcount_t *s, unsigned start)
220 return __read_seqcount_retry(s, start);
223 static inline void __raw_write_seqcount_begin(seqcount_t *s)
229 static inline void raw_write_seqcount_begin(seqcount_t *s)
231 preempt_disable_rt();
232 __raw_write_seqcount_begin(s);
235 static inline void __raw_write_seqcount_end(seqcount_t *s)
241 static inline void raw_write_seqcount_end(seqcount_t *s)
243 __raw_write_seqcount_end(s);
248 * raw_write_seqcount_barrier - do a seq write barrier
249 * @s: pointer to seqcount_t
251 * This can be used to provide an ordering guarantee instead of the
252 * usual consistency guarantee. It is one wmb cheaper, because we can
253 * collapse the two back-to-back wmb()s.
256 * bool X = true, Y = false;
263 * int s = read_seqcount_begin(&seq);
267 * } while (read_seqcount_retry(&seq, s));
276 * raw_write_seqcount_barrier(seq);
281 static inline void raw_write_seqcount_barrier(seqcount_t *s)
288 static inline int raw_read_seqcount_latch(seqcount_t *s)
290 return lockless_dereference(s->sequence);
294 * raw_write_seqcount_latch - redirect readers to even/odd copy
295 * @s: pointer to seqcount_t
297 * The latch technique is a multiversion concurrency control method that allows
298 * queries during non-atomic modifications. If you can guarantee queries never
299 * interrupt the modification -- e.g. the concurrency is strictly between CPUs
300 * -- you most likely do not need this.
302 * Where the traditional RCU/lockless data structures rely on atomic
303 * modifications to ensure queries observe either the old or the new state the
304 * latch allows the same for non-atomic updates. The trade-off is doubling the
305 * cost of storage; we have to maintain two copies of the entire data
308 * Very simply put: we first modify one copy and then the other. This ensures
309 * there is always one copy in a stable state, ready to give us an answer.
311 * The basic form is a data structure like:
313 * struct latch_struct {
315 * struct data_struct data[2];
318 * Where a modification, which is assumed to be externally serialized, does the
321 * void latch_modify(struct latch_struct *latch, ...)
323 * smp_wmb(); <- Ensure that the last data[1] update is visible
325 * smp_wmb(); <- Ensure that the seqcount update is visible
327 * modify(latch->data[0], ...);
329 * smp_wmb(); <- Ensure that the data[0] update is visible
331 * smp_wmb(); <- Ensure that the seqcount update is visible
333 * modify(latch->data[1], ...);
336 * The query will have a form like:
338 * struct entry *latch_query(struct latch_struct *latch, ...)
340 * struct entry *entry;
344 * seq = lockless_dereference(latch->seq);
347 * entry = data_query(latch->data[idx], ...);
350 * } while (seq != latch->seq);
355 * So during the modification, queries are first redirected to data[1]. Then we
356 * modify data[0]. When that is complete, we redirect queries back to data[0]
357 * and we can modify data[1].
359 * NOTE: The non-requirement for atomic modifications does _NOT_ include
360 * the publishing of new entries in the case where data is a dynamic
363 * An iteration might start in data[0] and get suspended long enough
364 * to miss an entire modification sequence, once it resumes it might
365 * observe the new entry.
367 * NOTE: When data is a dynamic data structure; one should use regular RCU
368 * patterns to manage the lifetimes of the objects within.
370 static inline void raw_write_seqcount_latch(seqcount_t *s)
372 smp_wmb(); /* prior stores before incrementing "sequence" */
374 smp_wmb(); /* increment "sequence" before following stores */
378 * Sequence counter only version assumes that callers are using their
381 static inline void write_seqcount_begin_nested(seqcount_t *s, int subclass)
383 raw_write_seqcount_begin(s);
384 seqcount_acquire(&s->dep_map, subclass, 0, _RET_IP_);
387 static inline void write_seqcount_begin(seqcount_t *s)
389 write_seqcount_begin_nested(s, 0);
392 static inline void write_seqcount_end(seqcount_t *s)
394 seqcount_release(&s->dep_map, 1, _RET_IP_);
395 raw_write_seqcount_end(s);
399 * write_seqcount_invalidate - invalidate in-progress read-side seq operations
400 * @s: pointer to seqcount_t
402 * After write_seqcount_invalidate, no read-side seq operations will complete
403 * successfully and see data older than this.
405 static inline void write_seqcount_invalidate(seqcount_t *s)
412 struct seqcount seqcount;
417 * These macros triggered gcc-3.x compile-time problems. We think these are
418 * OK now. Be cautious.
420 #define __SEQLOCK_UNLOCKED(lockname) \
422 .seqcount = SEQCNT_ZERO(lockname), \
423 .lock = __SPIN_LOCK_UNLOCKED(lockname) \
426 #define seqlock_init(x) \
428 seqcount_init(&(x)->seqcount); \
429 spin_lock_init(&(x)->lock); \
432 #define DEFINE_SEQLOCK(x) \
433 seqlock_t x = __SEQLOCK_UNLOCKED(x)
436 * Read side functions for starting and finalizing a read side section.
438 #ifndef CONFIG_PREEMPT_RT_FULL
439 static inline unsigned read_seqbegin(const seqlock_t *sl)
441 return read_seqcount_begin(&sl->seqcount);
445 * Starvation safe read side for RT
447 static inline unsigned read_seqbegin(seqlock_t *sl)
452 ret = ACCESS_ONCE(sl->seqcount.sequence);
453 if (unlikely(ret & 1)) {
455 * Take the lock and let the writer proceed (i.e. evtl
456 * boost it), otherwise we could loop here forever.
458 spin_unlock_wait(&sl->lock);
465 static inline unsigned read_seqretry(const seqlock_t *sl, unsigned start)
467 return read_seqcount_retry(&sl->seqcount, start);
471 * Lock out other writers and update the count.
472 * Acts like a normal spin_lock/unlock.
473 * Don't need preempt_disable() because that is in the spin_lock already.
475 static inline void write_seqlock(seqlock_t *sl)
477 spin_lock(&sl->lock);
478 __raw_write_seqcount_begin(&sl->seqcount);
481 static inline void write_sequnlock(seqlock_t *sl)
483 __raw_write_seqcount_end(&sl->seqcount);
484 spin_unlock(&sl->lock);
487 static inline void write_seqlock_bh(seqlock_t *sl)
489 spin_lock_bh(&sl->lock);
490 __raw_write_seqcount_begin(&sl->seqcount);
493 static inline void write_sequnlock_bh(seqlock_t *sl)
495 __raw_write_seqcount_end(&sl->seqcount);
496 spin_unlock_bh(&sl->lock);
499 static inline void write_seqlock_irq(seqlock_t *sl)
501 spin_lock_irq(&sl->lock);
502 __raw_write_seqcount_begin(&sl->seqcount);
505 static inline void write_sequnlock_irq(seqlock_t *sl)
507 __raw_write_seqcount_end(&sl->seqcount);
508 spin_unlock_irq(&sl->lock);
511 static inline unsigned long __write_seqlock_irqsave(seqlock_t *sl)
515 spin_lock_irqsave(&sl->lock, flags);
516 __raw_write_seqcount_begin(&sl->seqcount);
520 #define write_seqlock_irqsave(lock, flags) \
521 do { flags = __write_seqlock_irqsave(lock); } while (0)
524 write_sequnlock_irqrestore(seqlock_t *sl, unsigned long flags)
526 __raw_write_seqcount_end(&sl->seqcount);
527 spin_unlock_irqrestore(&sl->lock, flags);
531 * A locking reader exclusively locks out other writers and locking readers,
532 * but doesn't update the sequence number. Acts like a normal spin_lock/unlock.
533 * Don't need preempt_disable() because that is in the spin_lock already.
535 static inline void read_seqlock_excl(seqlock_t *sl)
537 spin_lock(&sl->lock);
540 static inline void read_sequnlock_excl(seqlock_t *sl)
542 spin_unlock(&sl->lock);
546 * read_seqbegin_or_lock - begin a sequence number check or locking block
547 * @lock: sequence lock
548 * @seq : sequence number to be checked
550 * First try it once optimistically without taking the lock. If that fails,
551 * take the lock. The sequence number is also used as a marker for deciding
552 * whether to be a reader (even) or writer (odd).
553 * N.B. seq must be initialized to an even number to begin with.
555 static inline void read_seqbegin_or_lock(seqlock_t *lock, int *seq)
557 if (!(*seq & 1)) /* Even */
558 *seq = read_seqbegin(lock);
560 read_seqlock_excl(lock);
563 static inline int need_seqretry(seqlock_t *lock, int seq)
565 return !(seq & 1) && read_seqretry(lock, seq);
568 static inline void done_seqretry(seqlock_t *lock, int seq)
571 read_sequnlock_excl(lock);
574 static inline void read_seqlock_excl_bh(seqlock_t *sl)
576 spin_lock_bh(&sl->lock);
579 static inline void read_sequnlock_excl_bh(seqlock_t *sl)
581 spin_unlock_bh(&sl->lock);
584 static inline void read_seqlock_excl_irq(seqlock_t *sl)
586 spin_lock_irq(&sl->lock);
589 static inline void read_sequnlock_excl_irq(seqlock_t *sl)
591 spin_unlock_irq(&sl->lock);
594 static inline unsigned long __read_seqlock_excl_irqsave(seqlock_t *sl)
598 spin_lock_irqsave(&sl->lock, flags);
602 #define read_seqlock_excl_irqsave(lock, flags) \
603 do { flags = __read_seqlock_excl_irqsave(lock); } while (0)
606 read_sequnlock_excl_irqrestore(seqlock_t *sl, unsigned long flags)
608 spin_unlock_irqrestore(&sl->lock, flags);
611 static inline unsigned long
612 read_seqbegin_or_lock_irqsave(seqlock_t *lock, int *seq)
614 unsigned long flags = 0;
616 if (!(*seq & 1)) /* Even */
617 *seq = read_seqbegin(lock);
619 read_seqlock_excl_irqsave(lock, flags);
625 done_seqretry_irqrestore(seqlock_t *lock, int seq, unsigned long flags)
628 read_sequnlock_excl_irqrestore(lock, flags);
630 #endif /* __LINUX_SEQLOCK_H */