rcu: Move lockless_dereference() out of rcupdate.h
[GitHub/moto-9609/android_kernel_motorola_exynos9610.git] / include / linux / seqlock.h
CommitLineData
1da177e4
LT
1#ifndef __LINUX_SEQLOCK_H
2#define __LINUX_SEQLOCK_H
3/*
4 * Reader/writer consistent mechanism without starving writers. This type of
d08df601 5 * lock for data where the reader wants a consistent set of information
1370e97b
WL
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.
1da177e4 15 *
1370e97b 16 * This is not as cache friendly as brlock. Also, this may not work well
1da177e4
LT
17 * for data that contains pointers, because any writer could
18 * invalidate a pointer that a reader was following.
19 *
1370e97b 20 * Expected non-blocking reader usage:
1da177e4
LT
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
1da177e4
LT
35#include <linux/spinlock.h>
36#include <linux/preempt.h>
1ca7d67c 37#include <linux/lockdep.h>
56a21052 38#include <asm/processor.h>
1da177e4 39
1da177e4
LT
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 */
1da177e4
LT
46typedef struct seqcount {
47 unsigned sequence;
1ca7d67c
JS
48#ifdef CONFIG_DEBUG_LOCK_ALLOC
49 struct lockdep_map dep_map;
50#endif
1da177e4
LT
51} seqcount_t;
52
1ca7d67c
JS
53static 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
73static 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
1da177e4 92
3c22cd57
NP
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 */
106static inline unsigned __read_seqcount_begin(const seqcount_t *s)
1da177e4 107{
88a411c0
IM
108 unsigned ret;
109
110repeat:
4d3199e4 111 ret = READ_ONCE(s->sequence);
88a411c0
IM
112 if (unlikely(ret & 1)) {
113 cpu_relax();
114 goto repeat;
115 }
1da177e4
LT
116 return ret;
117}
118
0ea5a520
TG
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 */
128static inline unsigned raw_read_seqcount(const seqcount_t *s)
129{
4d3199e4 130 unsigned ret = READ_ONCE(s->sequence);
0ea5a520
TG
131 smp_rmb();
132 return ret;
133}
134
1ca7d67c 135/**
0c3351d4 136 * raw_read_seqcount_begin - start seq-read critical section w/o lockdep
1ca7d67c
JS
137 * @s: pointer to seqcount_t
138 * Returns: count to be passed to read_seqcount_retry
139 *
0c3351d4 140 * raw_read_seqcount_begin opens a read critical section of the given
1ca7d67c
JS
141 * seqcount, but without any lockdep checking. Validity of the critical
142 * section is tested by checking read_seqcount_retry function.
143 */
0c3351d4 144static inline unsigned raw_read_seqcount_begin(const seqcount_t *s)
1ca7d67c
JS
145{
146 unsigned ret = __read_seqcount_begin(s);
147 smp_rmb();
148 return ret;
149}
150
3c22cd57
NP
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 */
160static inline unsigned read_seqcount_begin(const seqcount_t *s)
161{
1ca7d67c 162 seqcount_lockdep_reader_access(s);
0c3351d4 163 return raw_read_seqcount_begin(s);
3c22cd57
NP
164}
165
4f988f15
LT
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 */
180static inline unsigned raw_seqcount_begin(const seqcount_t *s)
181{
4d3199e4 182 unsigned ret = READ_ONCE(s->sequence);
4f988f15
LT
183 smp_rmb();
184 return ret & ~1;
185}
186
3c22cd57
NP
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 */
201static 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).
1da177e4 215 */
88a411c0 216static inline int read_seqcount_retry(const seqcount_t *s, unsigned start)
1da177e4
LT
217{
218 smp_rmb();
3c22cd57 219 return __read_seqcount_retry(s, start);
1da177e4
LT
220}
221
222
0c3351d4
JS
223
224static inline void raw_write_seqcount_begin(seqcount_t *s)
225{
226 s->sequence++;
227 smp_wmb();
228}
229
230static inline void raw_write_seqcount_end(seqcount_t *s)
231{
232 smp_wmb();
233 s->sequence++;
234}
235
6695b92a 236/**
9b0fd802
MD
237 * raw_write_seqcount_latch - redirect readers to even/odd copy
238 * @s: pointer to seqcount_t
6695b92a
PZ
239 *
240 * The latch technique is a multiversion concurrency control method that allows
241 * queries during non-atomic modifications. If you can guarantee queries never
242 * interrupt the modification -- e.g. the concurrency is strictly between CPUs
243 * -- you most likely do not need this.
244 *
245 * Where the traditional RCU/lockless data structures rely on atomic
246 * modifications to ensure queries observe either the old or the new state the
247 * latch allows the same for non-atomic updates. The trade-off is doubling the
248 * cost of storage; we have to maintain two copies of the entire data
249 * structure.
250 *
251 * Very simply put: we first modify one copy and then the other. This ensures
252 * there is always one copy in a stable state, ready to give us an answer.
253 *
254 * The basic form is a data structure like:
255 *
256 * struct latch_struct {
257 * seqcount_t seq;
258 * struct data_struct data[2];
259 * };
260 *
261 * Where a modification, which is assumed to be externally serialized, does the
262 * following:
263 *
264 * void latch_modify(struct latch_struct *latch, ...)
265 * {
266 * smp_wmb(); <- Ensure that the last data[1] update is visible
267 * latch->seq++;
268 * smp_wmb(); <- Ensure that the seqcount update is visible
269 *
270 * modify(latch->data[0], ...);
271 *
272 * smp_wmb(); <- Ensure that the data[0] update is visible
273 * latch->seq++;
274 * smp_wmb(); <- Ensure that the seqcount update is visible
275 *
276 * modify(latch->data[1], ...);
277 * }
278 *
279 * The query will have a form like:
280 *
281 * struct entry *latch_query(struct latch_struct *latch, ...)
282 * {
283 * struct entry *entry;
284 * unsigned seq, idx;
285 *
286 * do {
287 * seq = latch->seq;
288 * smp_rmb();
289 *
290 * idx = seq & 0x01;
291 * entry = data_query(latch->data[idx], ...);
292 *
293 * smp_rmb();
294 * } while (seq != latch->seq);
295 *
296 * return entry;
297 * }
298 *
299 * So during the modification, queries are first redirected to data[1]. Then we
300 * modify data[0]. When that is complete, we redirect queries back to data[0]
301 * and we can modify data[1].
302 *
303 * NOTE: The non-requirement for atomic modifications does _NOT_ include
304 * the publishing of new entries in the case where data is a dynamic
305 * data structure.
306 *
307 * An iteration might start in data[0] and get suspended long enough
308 * to miss an entire modification sequence, once it resumes it might
309 * observe the new entry.
310 *
311 * NOTE: When data is a dynamic data structure; one should use regular RCU
312 * patterns to manage the lifetimes of the objects within.
9b0fd802
MD
313 */
314static inline void raw_write_seqcount_latch(seqcount_t *s)
315{
316 smp_wmb(); /* prior stores before incrementing "sequence" */
317 s->sequence++;
318 smp_wmb(); /* increment "sequence" before following stores */
319}
320
1da177e4
LT
321/*
322 * Sequence counter only version assumes that callers are using their
323 * own mutexing.
324 */
1ca7d67c 325static inline void write_seqcount_begin_nested(seqcount_t *s, int subclass)
1da177e4 326{
0c3351d4 327 raw_write_seqcount_begin(s);
1ca7d67c
JS
328 seqcount_acquire(&s->dep_map, subclass, 0, _RET_IP_);
329}
330
331static inline void write_seqcount_begin(seqcount_t *s)
332{
333 write_seqcount_begin_nested(s, 0);
1da177e4
LT
334}
335
336static inline void write_seqcount_end(seqcount_t *s)
337{
1ca7d67c 338 seqcount_release(&s->dep_map, 1, _RET_IP_);
0c3351d4 339 raw_write_seqcount_end(s);
1da177e4
LT
340}
341
3c22cd57
NP
342/**
343 * write_seqcount_barrier - invalidate in-progress read-side seq operations
344 * @s: pointer to seqcount_t
345 *
346 * After write_seqcount_barrier, no read-side seq operations will complete
347 * successfully and see data older than this.
348 */
349static inline void write_seqcount_barrier(seqcount_t *s)
350{
351 smp_wmb();
352 s->sequence+=2;
353}
354
6617feca
TG
355typedef struct {
356 struct seqcount seqcount;
357 spinlock_t lock;
358} seqlock_t;
359
360/*
361 * These macros triggered gcc-3.x compile-time problems. We think these are
362 * OK now. Be cautious.
363 */
364#define __SEQLOCK_UNLOCKED(lockname) \
365 { \
1ca7d67c 366 .seqcount = SEQCNT_ZERO(lockname), \
6617feca
TG
367 .lock = __SPIN_LOCK_UNLOCKED(lockname) \
368 }
369
370#define seqlock_init(x) \
371 do { \
372 seqcount_init(&(x)->seqcount); \
373 spin_lock_init(&(x)->lock); \
374 } while (0)
375
376#define DEFINE_SEQLOCK(x) \
377 seqlock_t x = __SEQLOCK_UNLOCKED(x)
378
379/*
380 * Read side functions for starting and finalizing a read side section.
381 */
382static inline unsigned read_seqbegin(const seqlock_t *sl)
383{
384 return read_seqcount_begin(&sl->seqcount);
385}
386
387static inline unsigned read_seqretry(const seqlock_t *sl, unsigned start)
388{
389 return read_seqcount_retry(&sl->seqcount, start);
390}
391
1da177e4 392/*
6617feca
TG
393 * Lock out other writers and update the count.
394 * Acts like a normal spin_lock/unlock.
395 * Don't need preempt_disable() because that is in the spin_lock already.
1da177e4 396 */
6617feca
TG
397static inline void write_seqlock(seqlock_t *sl)
398{
399 spin_lock(&sl->lock);
400 write_seqcount_begin(&sl->seqcount);
401}
402
403static inline void write_sequnlock(seqlock_t *sl)
404{
405 write_seqcount_end(&sl->seqcount);
406 spin_unlock(&sl->lock);
407}
408
409static inline void write_seqlock_bh(seqlock_t *sl)
410{
411 spin_lock_bh(&sl->lock);
412 write_seqcount_begin(&sl->seqcount);
413}
414
415static inline void write_sequnlock_bh(seqlock_t *sl)
416{
417 write_seqcount_end(&sl->seqcount);
418 spin_unlock_bh(&sl->lock);
419}
420
421static inline void write_seqlock_irq(seqlock_t *sl)
422{
423 spin_lock_irq(&sl->lock);
424 write_seqcount_begin(&sl->seqcount);
425}
426
427static inline void write_sequnlock_irq(seqlock_t *sl)
428{
429 write_seqcount_end(&sl->seqcount);
430 spin_unlock_irq(&sl->lock);
431}
432
433static inline unsigned long __write_seqlock_irqsave(seqlock_t *sl)
434{
435 unsigned long flags;
436
437 spin_lock_irqsave(&sl->lock, flags);
438 write_seqcount_begin(&sl->seqcount);
439 return flags;
440}
441
1da177e4 442#define write_seqlock_irqsave(lock, flags) \
6617feca 443 do { flags = __write_seqlock_irqsave(lock); } while (0)
1da177e4 444
6617feca
TG
445static inline void
446write_sequnlock_irqrestore(seqlock_t *sl, unsigned long flags)
447{
448 write_seqcount_end(&sl->seqcount);
449 spin_unlock_irqrestore(&sl->lock, flags);
450}
1da177e4 451
1370e97b
WL
452/*
453 * A locking reader exclusively locks out other writers and locking readers,
454 * but doesn't update the sequence number. Acts like a normal spin_lock/unlock.
455 * Don't need preempt_disable() because that is in the spin_lock already.
456 */
457static inline void read_seqlock_excl(seqlock_t *sl)
458{
459 spin_lock(&sl->lock);
460}
461
462static inline void read_sequnlock_excl(seqlock_t *sl)
463{
464 spin_unlock(&sl->lock);
465}
466
2bc74feb
AV
467/**
468 * read_seqbegin_or_lock - begin a sequence number check or locking block
469 * @lock: sequence lock
470 * @seq : sequence number to be checked
471 *
472 * First try it once optimistically without taking the lock. If that fails,
473 * take the lock. The sequence number is also used as a marker for deciding
474 * whether to be a reader (even) or writer (odd).
475 * N.B. seq must be initialized to an even number to begin with.
476 */
477static inline void read_seqbegin_or_lock(seqlock_t *lock, int *seq)
478{
479 if (!(*seq & 1)) /* Even */
480 *seq = read_seqbegin(lock);
481 else /* Odd */
482 read_seqlock_excl(lock);
483}
484
485static inline int need_seqretry(seqlock_t *lock, int seq)
486{
487 return !(seq & 1) && read_seqretry(lock, seq);
488}
489
490static inline void done_seqretry(seqlock_t *lock, int seq)
491{
492 if (seq & 1)
493 read_sequnlock_excl(lock);
494}
495
1370e97b
WL
496static inline void read_seqlock_excl_bh(seqlock_t *sl)
497{
498 spin_lock_bh(&sl->lock);
499}
500
501static inline void read_sequnlock_excl_bh(seqlock_t *sl)
502{
503 spin_unlock_bh(&sl->lock);
504}
505
506static inline void read_seqlock_excl_irq(seqlock_t *sl)
507{
508 spin_lock_irq(&sl->lock);
509}
510
511static inline void read_sequnlock_excl_irq(seqlock_t *sl)
512{
513 spin_unlock_irq(&sl->lock);
514}
515
516static inline unsigned long __read_seqlock_excl_irqsave(seqlock_t *sl)
517{
518 unsigned long flags;
519
520 spin_lock_irqsave(&sl->lock, flags);
521 return flags;
522}
523
524#define read_seqlock_excl_irqsave(lock, flags) \
525 do { flags = __read_seqlock_excl_irqsave(lock); } while (0)
526
527static inline void
528read_sequnlock_excl_irqrestore(seqlock_t *sl, unsigned long flags)
529{
530 spin_unlock_irqrestore(&sl->lock, flags);
531}
532
ef8ac063
RR
533static inline unsigned long
534read_seqbegin_or_lock_irqsave(seqlock_t *lock, int *seq)
535{
536 unsigned long flags = 0;
537
538 if (!(*seq & 1)) /* Even */
539 *seq = read_seqbegin(lock);
540 else /* Odd */
541 read_seqlock_excl_irqsave(lock, flags);
542
543 return flags;
544}
545
546static inline void
547done_seqretry_irqrestore(seqlock_t *lock, int seq, unsigned long flags)
548{
549 if (seq & 1)
550 read_sequnlock_excl_irqrestore(lock, flags);
551}
1da177e4 552#endif /* __LINUX_SEQLOCK_H */