Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / 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 <asm/processor.h>
39
40 /*
41  * Version using sequence counter only.
42  * This can be used when code has its own mutex protecting the
43  * updating starting before the write_seqcountbeqin() and ending
44  * after the write_seqcount_end().
45  */
46 typedef struct seqcount {
47         unsigned sequence;
48 #ifdef CONFIG_DEBUG_LOCK_ALLOC
49         struct lockdep_map dep_map;
50 #endif
51 } seqcount_t;
52
53 static inline void __seqcount_init(seqcount_t *s, const char *name,
54                                           struct lock_class_key *key)
55 {
56         /*
57          * Make sure we are not reinitializing a held lock:
58          */
59         lockdep_init_map(&s->dep_map, name, key, 0);
60         s->sequence = 0;
61 }
62
63 #ifdef CONFIG_DEBUG_LOCK_ALLOC
64 # define SEQCOUNT_DEP_MAP_INIT(lockname) \
65                 .dep_map = { .name = #lockname } \
66
67 # define seqcount_init(s)                               \
68         do {                                            \
69                 static struct lock_class_key __key;     \
70                 __seqcount_init((s), #s, &__key);       \
71         } while (0)
72
73 static inline void seqcount_lockdep_reader_access(const seqcount_t *s)
74 {
75         seqcount_t *l = (seqcount_t *)s;
76         unsigned long flags;
77
78         local_irq_save(flags);
79         seqcount_acquire_read(&l->dep_map, 0, 0, _RET_IP_);
80         seqcount_release(&l->dep_map, 1, _RET_IP_);
81         local_irq_restore(flags);
82 }
83
84 #else
85 # define SEQCOUNT_DEP_MAP_INIT(lockname)
86 # define seqcount_init(s) __seqcount_init(s, NULL, NULL)
87 # define seqcount_lockdep_reader_access(x)
88 #endif
89
90 #define SEQCNT_ZERO(lockname) { .sequence = 0, SEQCOUNT_DEP_MAP_INIT(lockname)}
91
92
93 /**
94  * __read_seqcount_begin - begin a seq-read critical section (without barrier)
95  * @s: pointer to seqcount_t
96  * Returns: count to be passed to read_seqcount_retry
97  *
98  * __read_seqcount_begin is like read_seqcount_begin, but has no smp_rmb()
99  * barrier. Callers should ensure that smp_rmb() or equivalent ordering is
100  * provided before actually loading any of the variables that are to be
101  * protected in this critical section.
102  *
103  * Use carefully, only in critical code, and comment how the barrier is
104  * provided.
105  */
106 static inline unsigned __read_seqcount_begin(const seqcount_t *s)
107 {
108         unsigned ret;
109
110 repeat:
111         ret = READ_ONCE(s->sequence);
112         if (unlikely(ret & 1)) {
113                 cpu_relax();
114                 goto repeat;
115         }
116         return ret;
117 }
118
119 /**
120  * raw_read_seqcount - Read the raw seqcount
121  * @s: pointer to seqcount_t
122  * Returns: count to be passed to read_seqcount_retry
123  *
124  * raw_read_seqcount opens a read critical section of the given
125  * seqcount without any lockdep checking and without checking or
126  * masking the LSB. Calling code is responsible for handling that.
127  */
128 static inline unsigned raw_read_seqcount(const seqcount_t *s)
129 {
130         unsigned ret = READ_ONCE(s->sequence);
131         smp_rmb();
132         return ret;
133 }
134
135 /**
136  * raw_read_seqcount_begin - start seq-read critical section w/o lockdep
137  * @s: pointer to seqcount_t
138  * Returns: count to be passed to read_seqcount_retry
139  *
140  * raw_read_seqcount_begin opens a read critical section of the given
141  * seqcount, but without any lockdep checking. Validity of the critical
142  * section is tested by checking read_seqcount_retry function.
143  */
144 static inline unsigned raw_read_seqcount_begin(const seqcount_t *s)
145 {
146         unsigned ret = __read_seqcount_begin(s);
147         smp_rmb();
148         return ret;
149 }
150
151 /**
152  * read_seqcount_begin - begin a seq-read critical section
153  * @s: pointer to seqcount_t
154  * Returns: count to be passed to read_seqcount_retry
155  *
156  * read_seqcount_begin opens a read critical section of the given seqcount.
157  * Validity of the critical section is tested by checking read_seqcount_retry
158  * function.
159  */
160 static inline unsigned read_seqcount_begin(const seqcount_t *s)
161 {
162         seqcount_lockdep_reader_access(s);
163         return raw_read_seqcount_begin(s);
164 }
165
166 /**
167  * raw_seqcount_begin - begin a seq-read critical section
168  * @s: pointer to seqcount_t
169  * Returns: count to be passed to read_seqcount_retry
170  *
171  * raw_seqcount_begin opens a read critical section of the given seqcount.
172  * Validity of the critical section is tested by checking read_seqcount_retry
173  * function.
174  *
175  * Unlike read_seqcount_begin(), this function will not wait for the count
176  * to stabilize. If a writer is active when we begin, we will fail the
177  * read_seqcount_retry() instead of stabilizing at the beginning of the
178  * critical section.
179  */
180 static inline unsigned raw_seqcount_begin(const seqcount_t *s)
181 {
182         unsigned ret = READ_ONCE(s->sequence);
183         smp_rmb();
184         return ret & ~1;
185 }
186
187 /**
188  * __read_seqcount_retry - end a seq-read critical section (without barrier)
189  * @s: pointer to seqcount_t
190  * @start: count, from read_seqcount_begin
191  * Returns: 1 if retry is required, else 0
192  *
193  * __read_seqcount_retry is like read_seqcount_retry, but has no smp_rmb()
194  * barrier. Callers should ensure that smp_rmb() or equivalent ordering is
195  * provided before actually loading any of the variables that are to be
196  * protected in this critical section.
197  *
198  * Use carefully, only in critical code, and comment how the barrier is
199  * provided.
200  */
201 static inline int __read_seqcount_retry(const seqcount_t *s, unsigned start)
202 {
203         return unlikely(s->sequence != start);
204 }
205
206 /**
207  * read_seqcount_retry - end a seq-read critical section
208  * @s: pointer to seqcount_t
209  * @start: count, from read_seqcount_begin
210  * Returns: 1 if retry is required, else 0
211  *
212  * read_seqcount_retry closes a read critical section of the given seqcount.
213  * If the critical section was invalid, it must be ignored (and typically
214  * retried).
215  */
216 static inline int read_seqcount_retry(const seqcount_t *s, unsigned start)
217 {
218         smp_rmb();
219         return __read_seqcount_retry(s, start);
220 }
221
222 static inline void __raw_write_seqcount_begin(seqcount_t *s)
223 {
224         s->sequence++;
225         smp_wmb();
226 }
227
228 static inline void raw_write_seqcount_begin(seqcount_t *s)
229 {
230         preempt_disable_rt();
231         __raw_write_seqcount_begin(s);
232 }
233
234 static inline void __raw_write_seqcount_end(seqcount_t *s)
235 {
236         smp_wmb();
237         s->sequence++;
238 }
239
240 static inline void raw_write_seqcount_end(seqcount_t *s)
241 {
242         __raw_write_seqcount_end(s);
243         preempt_enable_rt();
244 }
245
246 /*
247  * raw_write_seqcount_latch - redirect readers to even/odd copy
248  * @s: pointer to seqcount_t
249  */
250 static inline void raw_write_seqcount_latch(seqcount_t *s)
251 {
252        smp_wmb();      /* prior stores before incrementing "sequence" */
253        s->sequence++;
254        smp_wmb();      /* increment "sequence" before following stores */
255 }
256
257 /*
258  * Sequence counter only version assumes that callers are using their
259  * own mutexing.
260  */
261 static inline void write_seqcount_begin_nested(seqcount_t *s, int subclass)
262 {
263         raw_write_seqcount_begin(s);
264         seqcount_acquire(&s->dep_map, subclass, 0, _RET_IP_);
265 }
266
267 static inline void write_seqcount_begin(seqcount_t *s)
268 {
269         write_seqcount_begin_nested(s, 0);
270 }
271
272 static inline void write_seqcount_end(seqcount_t *s)
273 {
274         seqcount_release(&s->dep_map, 1, _RET_IP_);
275         raw_write_seqcount_end(s);
276 }
277
278 /**
279  * write_seqcount_barrier - invalidate in-progress read-side seq operations
280  * @s: pointer to seqcount_t
281  *
282  * After write_seqcount_barrier, no read-side seq operations will complete
283  * successfully and see data older than this.
284  */
285 static inline void write_seqcount_barrier(seqcount_t *s)
286 {
287         smp_wmb();
288         s->sequence+=2;
289 }
290
291 typedef struct {
292         struct seqcount seqcount;
293         spinlock_t lock;
294 } seqlock_t;
295
296 /*
297  * These macros triggered gcc-3.x compile-time problems.  We think these are
298  * OK now.  Be cautious.
299  */
300 #define __SEQLOCK_UNLOCKED(lockname)                    \
301         {                                               \
302                 .seqcount = SEQCNT_ZERO(lockname),      \
303                 .lock = __SPIN_LOCK_UNLOCKED(lockname)  \
304         }
305
306 #define seqlock_init(x)                                 \
307         do {                                            \
308                 seqcount_init(&(x)->seqcount);          \
309                 spin_lock_init(&(x)->lock);             \
310         } while (0)
311
312 #define DEFINE_SEQLOCK(x) \
313                 seqlock_t x = __SEQLOCK_UNLOCKED(x)
314
315 /*
316  * Read side functions for starting and finalizing a read side section.
317  */
318 #ifndef CONFIG_PREEMPT_RT_FULL
319 static inline unsigned read_seqbegin(const seqlock_t *sl)
320 {
321         return read_seqcount_begin(&sl->seqcount);
322 }
323 #else
324 /*
325  * Starvation safe read side for RT
326  */
327 static inline unsigned read_seqbegin(seqlock_t *sl)
328 {
329         unsigned ret;
330
331 repeat:
332         ret = ACCESS_ONCE(sl->seqcount.sequence);
333         if (unlikely(ret & 1)) {
334                 /*
335                  * Take the lock and let the writer proceed (i.e. evtl
336                  * boost it), otherwise we could loop here forever.
337                  */
338                 spin_unlock_wait(&sl->lock);
339                 goto repeat;
340         }
341         return ret;
342 }
343 #endif
344
345 static inline unsigned read_seqretry(const seqlock_t *sl, unsigned start)
346 {
347         return read_seqcount_retry(&sl->seqcount, start);
348 }
349
350 /*
351  * Lock out other writers and update the count.
352  * Acts like a normal spin_lock/unlock.
353  * Don't need preempt_disable() because that is in the spin_lock already.
354  */
355 static inline void write_seqlock(seqlock_t *sl)
356 {
357         spin_lock(&sl->lock);
358         __raw_write_seqcount_begin(&sl->seqcount);
359 }
360
361 static inline void write_sequnlock(seqlock_t *sl)
362 {
363         __raw_write_seqcount_end(&sl->seqcount);
364         spin_unlock(&sl->lock);
365 }
366
367 static inline void write_seqlock_bh(seqlock_t *sl)
368 {
369         spin_lock_bh(&sl->lock);
370         __raw_write_seqcount_begin(&sl->seqcount);
371 }
372
373 static inline void write_sequnlock_bh(seqlock_t *sl)
374 {
375         __raw_write_seqcount_end(&sl->seqcount);
376         spin_unlock_bh(&sl->lock);
377 }
378
379 static inline void write_seqlock_irq(seqlock_t *sl)
380 {
381         spin_lock_irq(&sl->lock);
382         __raw_write_seqcount_begin(&sl->seqcount);
383 }
384
385 static inline void write_sequnlock_irq(seqlock_t *sl)
386 {
387         __raw_write_seqcount_end(&sl->seqcount);
388         spin_unlock_irq(&sl->lock);
389 }
390
391 static inline unsigned long __write_seqlock_irqsave(seqlock_t *sl)
392 {
393         unsigned long flags;
394
395         spin_lock_irqsave(&sl->lock, flags);
396         __raw_write_seqcount_begin(&sl->seqcount);
397         return flags;
398 }
399
400 #define write_seqlock_irqsave(lock, flags)                              \
401         do { flags = __write_seqlock_irqsave(lock); } while (0)
402
403 static inline void
404 write_sequnlock_irqrestore(seqlock_t *sl, unsigned long flags)
405 {
406         __raw_write_seqcount_end(&sl->seqcount);
407         spin_unlock_irqrestore(&sl->lock, flags);
408 }
409
410 /*
411  * A locking reader exclusively locks out other writers and locking readers,
412  * but doesn't update the sequence number. Acts like a normal spin_lock/unlock.
413  * Don't need preempt_disable() because that is in the spin_lock already.
414  */
415 static inline void read_seqlock_excl(seqlock_t *sl)
416 {
417         spin_lock(&sl->lock);
418 }
419
420 static inline void read_sequnlock_excl(seqlock_t *sl)
421 {
422         spin_unlock(&sl->lock);
423 }
424
425 /**
426  * read_seqbegin_or_lock - begin a sequence number check or locking block
427  * @lock: sequence lock
428  * @seq : sequence number to be checked
429  *
430  * First try it once optimistically without taking the lock. If that fails,
431  * take the lock. The sequence number is also used as a marker for deciding
432  * whether to be a reader (even) or writer (odd).
433  * N.B. seq must be initialized to an even number to begin with.
434  */
435 static inline void read_seqbegin_or_lock(seqlock_t *lock, int *seq)
436 {
437         if (!(*seq & 1))        /* Even */
438                 *seq = read_seqbegin(lock);
439         else                    /* Odd */
440                 read_seqlock_excl(lock);
441 }
442
443 static inline int need_seqretry(seqlock_t *lock, int seq)
444 {
445         return !(seq & 1) && read_seqretry(lock, seq);
446 }
447
448 static inline void done_seqretry(seqlock_t *lock, int seq)
449 {
450         if (seq & 1)
451                 read_sequnlock_excl(lock);
452 }
453
454 static inline void read_seqlock_excl_bh(seqlock_t *sl)
455 {
456         spin_lock_bh(&sl->lock);
457 }
458
459 static inline void read_sequnlock_excl_bh(seqlock_t *sl)
460 {
461         spin_unlock_bh(&sl->lock);
462 }
463
464 static inline void read_seqlock_excl_irq(seqlock_t *sl)
465 {
466         spin_lock_irq(&sl->lock);
467 }
468
469 static inline void read_sequnlock_excl_irq(seqlock_t *sl)
470 {
471         spin_unlock_irq(&sl->lock);
472 }
473
474 static inline unsigned long __read_seqlock_excl_irqsave(seqlock_t *sl)
475 {
476         unsigned long flags;
477
478         spin_lock_irqsave(&sl->lock, flags);
479         return flags;
480 }
481
482 #define read_seqlock_excl_irqsave(lock, flags)                          \
483         do { flags = __read_seqlock_excl_irqsave(lock); } while (0)
484
485 static inline void
486 read_sequnlock_excl_irqrestore(seqlock_t *sl, unsigned long flags)
487 {
488         spin_unlock_irqrestore(&sl->lock, flags);
489 }
490
491 static inline unsigned long
492 read_seqbegin_or_lock_irqsave(seqlock_t *lock, int *seq)
493 {
494         unsigned long flags = 0;
495
496         if (!(*seq & 1))        /* Even */
497                 *seq = read_seqbegin(lock);
498         else                    /* Odd */
499                 read_seqlock_excl_irqsave(lock, flags);
500
501         return flags;
502 }
503
504 static inline void
505 done_seqretry_irqrestore(seqlock_t *lock, int seq, unsigned long flags)
506 {
507         if (seq & 1)
508                 read_sequnlock_excl_irqrestore(lock, flags);
509 }
510 #endif /* __LINUX_SEQLOCK_H */