]> rtime.felk.cvut.cz Git - zynq/linux.git/blob - include/linux/seqlock.h
Apply preempt_rt patch-4.9-rt1.patch.xz
[zynq/linux.git] / include / linux / seqlock.h
1 #ifndef __LINUX_SEQLOCK_H
2 #define __LINUX_SEQLOCK_H
3 /*
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
7  * of readers:
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.
15  *
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.
19  *
20  * Expected non-blocking reader usage:
21  *      do {
22  *          seq = read_seqbegin(&foo);
23  *      ...
24  *      } while (read_seqretry(&foo, seq));
25  *
26  *
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.
30  *
31  * Based on x86_64 vsyscall gettimeofday 
32  * by Keith Owens and Andrea Arcangeli
33  */
34
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>
40
41 /*
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().
46  */
47 typedef struct seqcount {
48         unsigned sequence;
49 #ifdef CONFIG_DEBUG_LOCK_ALLOC
50         struct lockdep_map dep_map;
51 #endif
52 } seqcount_t;
53
54 static inline void __seqcount_init(seqcount_t *s, const char *name,
55                                           struct lock_class_key *key)
56 {
57         /*
58          * Make sure we are not reinitializing a held lock:
59          */
60         lockdep_init_map(&s->dep_map, name, key, 0);
61         s->sequence = 0;
62 }
63
64 #ifdef CONFIG_DEBUG_LOCK_ALLOC
65 # define SEQCOUNT_DEP_MAP_INIT(lockname) \
66                 .dep_map = { .name = #lockname } \
67
68 # define seqcount_init(s)                               \
69         do {                                            \
70                 static struct lock_class_key __key;     \
71                 __seqcount_init((s), #s, &__key);       \
72         } while (0)
73
74 static inline void seqcount_lockdep_reader_access(const seqcount_t *s)
75 {
76         seqcount_t *l = (seqcount_t *)s;
77         unsigned long flags;
78
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);
83 }
84
85 #else
86 # define SEQCOUNT_DEP_MAP_INIT(lockname)
87 # define seqcount_init(s) __seqcount_init(s, NULL, NULL)
88 # define seqcount_lockdep_reader_access(x)
89 #endif
90
91 #define SEQCNT_ZERO(lockname) { .sequence = 0, SEQCOUNT_DEP_MAP_INIT(lockname)}
92
93
94 /**
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
98  *
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.
103  *
104  * Use carefully, only in critical code, and comment how the barrier is
105  * provided.
106  */
107 static inline unsigned __read_seqcount_begin(const seqcount_t *s)
108 {
109         unsigned ret;
110
111 repeat:
112         ret = READ_ONCE(s->sequence);
113         if (unlikely(ret & 1)) {
114                 cpu_relax();
115                 goto repeat;
116         }
117         return ret;
118 }
119
120 /**
121  * raw_read_seqcount - Read the raw seqcount
122  * @s: pointer to seqcount_t
123  * Returns: count to be passed to read_seqcount_retry
124  *
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.
128  */
129 static inline unsigned raw_read_seqcount(const seqcount_t *s)
130 {
131         unsigned ret = READ_ONCE(s->sequence);
132         smp_rmb();
133         return ret;
134 }
135
136 /**
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
140  *
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.
144  */
145 static inline unsigned raw_read_seqcount_begin(const seqcount_t *s)
146 {
147         unsigned ret = __read_seqcount_begin(s);
148         smp_rmb();
149         return ret;
150 }
151
152 /**
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
156  *
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
159  * function.
160  */
161 static inline unsigned read_seqcount_begin(const seqcount_t *s)
162 {
163         seqcount_lockdep_reader_access(s);
164         return raw_read_seqcount_begin(s);
165 }
166
167 /**
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
171  *
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
174  * function.
175  *
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
179  * critical section.
180  */
181 static inline unsigned raw_seqcount_begin(const seqcount_t *s)
182 {
183         unsigned ret = READ_ONCE(s->sequence);
184         smp_rmb();
185         return ret & ~1;
186 }
187
188 /**
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
193  *
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.
198  *
199  * Use carefully, only in critical code, and comment how the barrier is
200  * provided.
201  */
202 static inline int __read_seqcount_retry(const seqcount_t *s, unsigned start)
203 {
204         return unlikely(s->sequence != start);
205 }
206
207 /**
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
212  *
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
215  * retried).
216  */
217 static inline int read_seqcount_retry(const seqcount_t *s, unsigned start)
218 {
219         smp_rmb();
220         return __read_seqcount_retry(s, start);
221 }
222
223 static inline void __raw_write_seqcount_begin(seqcount_t *s)
224 {
225         s->sequence++;
226         smp_wmb();
227 }
228
229 static inline void raw_write_seqcount_begin(seqcount_t *s)
230 {
231         preempt_disable_rt();
232         __raw_write_seqcount_begin(s);
233 }
234
235 static inline void __raw_write_seqcount_end(seqcount_t *s)
236 {
237         smp_wmb();
238         s->sequence++;
239 }
240
241 static inline void raw_write_seqcount_end(seqcount_t *s)
242 {
243         __raw_write_seqcount_end(s);
244         preempt_enable_rt();
245 }
246
247 /**
248  * raw_write_seqcount_barrier - do a seq write barrier
249  * @s: pointer to seqcount_t
250  *
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.
254  *
255  *      seqcount_t seq;
256  *      bool X = true, Y = false;
257  *
258  *      void read(void)
259  *      {
260  *              bool x, y;
261  *
262  *              do {
263  *                      int s = read_seqcount_begin(&seq);
264  *
265  *                      x = X; y = Y;
266  *
267  *              } while (read_seqcount_retry(&seq, s));
268  *
269  *              BUG_ON(!x && !y);
270  *      }
271  *
272  *      void write(void)
273  *      {
274  *              Y = true;
275  *
276  *              raw_write_seqcount_barrier(seq);
277  *
278  *              X = false;
279  *      }
280  */
281 static inline void raw_write_seqcount_barrier(seqcount_t *s)
282 {
283         s->sequence++;
284         smp_wmb();
285         s->sequence++;
286 }
287
288 static inline int raw_read_seqcount_latch(seqcount_t *s)
289 {
290         int seq = READ_ONCE(s->sequence);
291         /* Pairs with the first smp_wmb() in raw_write_seqcount_latch() */
292         smp_read_barrier_depends();
293         return seq;
294 }
295
296 /**
297  * raw_write_seqcount_latch - redirect readers to even/odd copy
298  * @s: pointer to seqcount_t
299  *
300  * The latch technique is a multiversion concurrency control method that allows
301  * queries during non-atomic modifications. If you can guarantee queries never
302  * interrupt the modification -- e.g. the concurrency is strictly between CPUs
303  * -- you most likely do not need this.
304  *
305  * Where the traditional RCU/lockless data structures rely on atomic
306  * modifications to ensure queries observe either the old or the new state the
307  * latch allows the same for non-atomic updates. The trade-off is doubling the
308  * cost of storage; we have to maintain two copies of the entire data
309  * structure.
310  *
311  * Very simply put: we first modify one copy and then the other. This ensures
312  * there is always one copy in a stable state, ready to give us an answer.
313  *
314  * The basic form is a data structure like:
315  *
316  * struct latch_struct {
317  *      seqcount_t              seq;
318  *      struct data_struct      data[2];
319  * };
320  *
321  * Where a modification, which is assumed to be externally serialized, does the
322  * following:
323  *
324  * void latch_modify(struct latch_struct *latch, ...)
325  * {
326  *      smp_wmb();      <- Ensure that the last data[1] update is visible
327  *      latch->seq++;
328  *      smp_wmb();      <- Ensure that the seqcount update is visible
329  *
330  *      modify(latch->data[0], ...);
331  *
332  *      smp_wmb();      <- Ensure that the data[0] update is visible
333  *      latch->seq++;
334  *      smp_wmb();      <- Ensure that the seqcount update is visible
335  *
336  *      modify(latch->data[1], ...);
337  * }
338  *
339  * The query will have a form like:
340  *
341  * struct entry *latch_query(struct latch_struct *latch, ...)
342  * {
343  *      struct entry *entry;
344  *      unsigned seq, idx;
345  *
346  *      do {
347  *              seq = raw_read_seqcount_latch(&latch->seq);
348  *
349  *              idx = seq & 0x01;
350  *              entry = data_query(latch->data[idx], ...);
351  *
352  *              smp_rmb();
353  *      } while (seq != latch->seq);
354  *
355  *      return entry;
356  * }
357  *
358  * So during the modification, queries are first redirected to data[1]. Then we
359  * modify data[0]. When that is complete, we redirect queries back to data[0]
360  * and we can modify data[1].
361  *
362  * NOTE: The non-requirement for atomic modifications does _NOT_ include
363  *       the publishing of new entries in the case where data is a dynamic
364  *       data structure.
365  *
366  *       An iteration might start in data[0] and get suspended long enough
367  *       to miss an entire modification sequence, once it resumes it might
368  *       observe the new entry.
369  *
370  * NOTE: When data is a dynamic data structure; one should use regular RCU
371  *       patterns to manage the lifetimes of the objects within.
372  */
373 static inline void raw_write_seqcount_latch(seqcount_t *s)
374 {
375        smp_wmb();      /* prior stores before incrementing "sequence" */
376        s->sequence++;
377        smp_wmb();      /* increment "sequence" before following stores */
378 }
379
380 /*
381  * Sequence counter only version assumes that callers are using their
382  * own mutexing.
383  */
384 static inline void write_seqcount_begin_nested(seqcount_t *s, int subclass)
385 {
386         raw_write_seqcount_begin(s);
387         seqcount_acquire(&s->dep_map, subclass, 0, _RET_IP_);
388 }
389
390 static inline void write_seqcount_begin(seqcount_t *s)
391 {
392         write_seqcount_begin_nested(s, 0);
393 }
394
395 static inline void write_seqcount_end(seqcount_t *s)
396 {
397         seqcount_release(&s->dep_map, 1, _RET_IP_);
398         raw_write_seqcount_end(s);
399 }
400
401 /**
402  * write_seqcount_invalidate - invalidate in-progress read-side seq operations
403  * @s: pointer to seqcount_t
404  *
405  * After write_seqcount_invalidate, no read-side seq operations will complete
406  * successfully and see data older than this.
407  */
408 static inline void write_seqcount_invalidate(seqcount_t *s)
409 {
410         smp_wmb();
411         s->sequence+=2;
412 }
413
414 typedef struct {
415         struct seqcount seqcount;
416         spinlock_t lock;
417 } seqlock_t;
418
419 /*
420  * These macros triggered gcc-3.x compile-time problems.  We think these are
421  * OK now.  Be cautious.
422  */
423 #define __SEQLOCK_UNLOCKED(lockname)                    \
424         {                                               \
425                 .seqcount = SEQCNT_ZERO(lockname),      \
426                 .lock = __SPIN_LOCK_UNLOCKED(lockname)  \
427         }
428
429 #define seqlock_init(x)                                 \
430         do {                                            \
431                 seqcount_init(&(x)->seqcount);          \
432                 spin_lock_init(&(x)->lock);             \
433         } while (0)
434
435 #define DEFINE_SEQLOCK(x) \
436                 seqlock_t x = __SEQLOCK_UNLOCKED(x)
437
438 /*
439  * Read side functions for starting and finalizing a read side section.
440  */
441 #ifndef CONFIG_PREEMPT_RT_FULL
442 static inline unsigned read_seqbegin(const seqlock_t *sl)
443 {
444         return read_seqcount_begin(&sl->seqcount);
445 }
446 #else
447 /*
448  * Starvation safe read side for RT
449  */
450 static inline unsigned read_seqbegin(seqlock_t *sl)
451 {
452         unsigned ret;
453
454 repeat:
455         ret = ACCESS_ONCE(sl->seqcount.sequence);
456         if (unlikely(ret & 1)) {
457                 /*
458                  * Take the lock and let the writer proceed (i.e. evtl
459                  * boost it), otherwise we could loop here forever.
460                  */
461                 spin_unlock_wait(&sl->lock);
462                 goto repeat;
463         }
464         return ret;
465 }
466 #endif
467
468 static inline unsigned read_seqretry(const seqlock_t *sl, unsigned start)
469 {
470         return read_seqcount_retry(&sl->seqcount, start);
471 }
472
473 /*
474  * Lock out other writers and update the count.
475  * Acts like a normal spin_lock/unlock.
476  * Don't need preempt_disable() because that is in the spin_lock already.
477  */
478 static inline void write_seqlock(seqlock_t *sl)
479 {
480         spin_lock(&sl->lock);
481         __raw_write_seqcount_begin(&sl->seqcount);
482 }
483
484 static inline int try_write_seqlock(seqlock_t *sl)
485 {
486         if (spin_trylock(&sl->lock)) {
487                 __raw_write_seqcount_begin(&sl->seqcount);
488                 return 1;
489         }
490         return 0;
491 }
492
493 static inline void write_sequnlock(seqlock_t *sl)
494 {
495         __raw_write_seqcount_end(&sl->seqcount);
496         spin_unlock(&sl->lock);
497 }
498
499 static inline void write_seqlock_bh(seqlock_t *sl)
500 {
501         spin_lock_bh(&sl->lock);
502         __raw_write_seqcount_begin(&sl->seqcount);
503 }
504
505 static inline void write_sequnlock_bh(seqlock_t *sl)
506 {
507         __raw_write_seqcount_end(&sl->seqcount);
508         spin_unlock_bh(&sl->lock);
509 }
510
511 static inline void write_seqlock_irq(seqlock_t *sl)
512 {
513         spin_lock_irq(&sl->lock);
514         __raw_write_seqcount_begin(&sl->seqcount);
515 }
516
517 static inline void write_sequnlock_irq(seqlock_t *sl)
518 {
519         __raw_write_seqcount_end(&sl->seqcount);
520         spin_unlock_irq(&sl->lock);
521 }
522
523 static inline unsigned long __write_seqlock_irqsave(seqlock_t *sl)
524 {
525         unsigned long flags;
526
527         spin_lock_irqsave(&sl->lock, flags);
528         __raw_write_seqcount_begin(&sl->seqcount);
529         return flags;
530 }
531
532 #define write_seqlock_irqsave(lock, flags)                              \
533         do { flags = __write_seqlock_irqsave(lock); } while (0)
534
535 static inline void
536 write_sequnlock_irqrestore(seqlock_t *sl, unsigned long flags)
537 {
538         __raw_write_seqcount_end(&sl->seqcount);
539         spin_unlock_irqrestore(&sl->lock, flags);
540 }
541
542 /*
543  * A locking reader exclusively locks out other writers and locking readers,
544  * but doesn't update the sequence number. Acts like a normal spin_lock/unlock.
545  * Don't need preempt_disable() because that is in the spin_lock already.
546  */
547 static inline void read_seqlock_excl(seqlock_t *sl)
548 {
549         spin_lock(&sl->lock);
550 }
551
552 static inline void read_sequnlock_excl(seqlock_t *sl)
553 {
554         spin_unlock(&sl->lock);
555 }
556
557 /**
558  * read_seqbegin_or_lock - begin a sequence number check or locking block
559  * @lock: sequence lock
560  * @seq : sequence number to be checked
561  *
562  * First try it once optimistically without taking the lock. If that fails,
563  * take the lock. The sequence number is also used as a marker for deciding
564  * whether to be a reader (even) or writer (odd).
565  * N.B. seq must be initialized to an even number to begin with.
566  */
567 static inline void read_seqbegin_or_lock(seqlock_t *lock, int *seq)
568 {
569         if (!(*seq & 1))        /* Even */
570                 *seq = read_seqbegin(lock);
571         else                    /* Odd */
572                 read_seqlock_excl(lock);
573 }
574
575 static inline int need_seqretry(seqlock_t *lock, int seq)
576 {
577         return !(seq & 1) && read_seqretry(lock, seq);
578 }
579
580 static inline void done_seqretry(seqlock_t *lock, int seq)
581 {
582         if (seq & 1)
583                 read_sequnlock_excl(lock);
584 }
585
586 static inline void read_seqlock_excl_bh(seqlock_t *sl)
587 {
588         spin_lock_bh(&sl->lock);
589 }
590
591 static inline void read_sequnlock_excl_bh(seqlock_t *sl)
592 {
593         spin_unlock_bh(&sl->lock);
594 }
595
596 static inline void read_seqlock_excl_irq(seqlock_t *sl)
597 {
598         spin_lock_irq(&sl->lock);
599 }
600
601 static inline void read_sequnlock_excl_irq(seqlock_t *sl)
602 {
603         spin_unlock_irq(&sl->lock);
604 }
605
606 static inline unsigned long __read_seqlock_excl_irqsave(seqlock_t *sl)
607 {
608         unsigned long flags;
609
610         spin_lock_irqsave(&sl->lock, flags);
611         return flags;
612 }
613
614 #define read_seqlock_excl_irqsave(lock, flags)                          \
615         do { flags = __read_seqlock_excl_irqsave(lock); } while (0)
616
617 static inline void
618 read_sequnlock_excl_irqrestore(seqlock_t *sl, unsigned long flags)
619 {
620         spin_unlock_irqrestore(&sl->lock, flags);
621 }
622
623 static inline unsigned long
624 read_seqbegin_or_lock_irqsave(seqlock_t *lock, int *seq)
625 {
626         unsigned long flags = 0;
627
628         if (!(*seq & 1))        /* Even */
629                 *seq = read_seqbegin(lock);
630         else                    /* Odd */
631                 read_seqlock_excl_irqsave(lock, flags);
632
633         return flags;
634 }
635
636 static inline void
637 done_seqretry_irqrestore(seqlock_t *lock, int seq, unsigned long flags)
638 {
639         if (seq & 1)
640                 read_sequnlock_excl_irqrestore(lock, flags);
641 }
642 #endif /* __LINUX_SEQLOCK_H */