1#ifndef __LINUX_SEQLOCK_H
2#define __LINUX_SEQLOCK_H
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
42
43
44
45
46typedef struct seqcount {
47 unsigned sequence;
48#ifdef CONFIG_DEBUG_LOCK_ALLOC
49 struct lockdep_map dep_map;
50#endif
51} seqcount_t;
52
53static inline void __seqcount_init(seqcount_t *s, const char *name,
54 struct lock_class_key *key)
55{
56
57
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106static inline unsigned __read_seqcount_begin(const seqcount_t *s)
107{
108 unsigned ret;
109
110repeat:
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
121
122
123
124
125
126
127
128static 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
137
138
139
140
141
142
143
144static 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
153
154
155
156
157
158
159
160static 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
168
169
170
171
172
173
174
175
176
177
178
179
180static 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
189
190
191
192
193
194
195
196
197
198
199
200
201static inline int __read_seqcount_retry(const seqcount_t *s, unsigned start)
202{
203 return unlikely(s->sequence != start);
204}
205
206
207
208
209
210
211
212
213
214
215
216static 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
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
236
237
238
239
240static inline void raw_write_seqcount_latch(seqcount_t *s)
241{
242 smp_wmb();
243 s->sequence++;
244 smp_wmb();
245}
246
247
248
249
250
251static inline void write_seqcount_begin_nested(seqcount_t *s, int subclass)
252{
253 raw_write_seqcount_begin(s);
254 seqcount_acquire(&s->dep_map, subclass, 0, _RET_IP_);
255}
256
257static inline void write_seqcount_begin(seqcount_t *s)
258{
259 write_seqcount_begin_nested(s, 0);
260}
261
262static inline void write_seqcount_end(seqcount_t *s)
263{
264 seqcount_release(&s->dep_map, 1, _RET_IP_);
265 raw_write_seqcount_end(s);
266}
267
268
269
270
271
272
273
274
275static inline void write_seqcount_barrier(seqcount_t *s)
276{
277 smp_wmb();
278 s->sequence+=2;
279}
280
281typedef struct {
282 struct seqcount seqcount;
283 spinlock_t lock;
284} seqlock_t;
285
286
287
288
289
290#define __SEQLOCK_UNLOCKED(lockname) \
291 { \
292 .seqcount = SEQCNT_ZERO(lockname), \
293 .lock = __SPIN_LOCK_UNLOCKED(lockname) \
294 }
295
296#define seqlock_init(x) \
297 do { \
298 seqcount_init(&(x)->seqcount); \
299 spin_lock_init(&(x)->lock); \
300 } while (0)
301
302#define DEFINE_SEQLOCK(x) \
303 seqlock_t x = __SEQLOCK_UNLOCKED(x)
304
305
306
307
308static inline unsigned read_seqbegin(const seqlock_t *sl)
309{
310 return read_seqcount_begin(&sl->seqcount);
311}
312
313static inline unsigned read_seqretry(const seqlock_t *sl, unsigned start)
314{
315 return read_seqcount_retry(&sl->seqcount, start);
316}
317
318
319
320
321
322
323static inline void write_seqlock(seqlock_t *sl)
324{
325 spin_lock(&sl->lock);
326 write_seqcount_begin(&sl->seqcount);
327}
328
329static inline void write_sequnlock(seqlock_t *sl)
330{
331 write_seqcount_end(&sl->seqcount);
332 spin_unlock(&sl->lock);
333}
334
335static inline void write_seqlock_bh(seqlock_t *sl)
336{
337 spin_lock_bh(&sl->lock);
338 write_seqcount_begin(&sl->seqcount);
339}
340
341static inline void write_sequnlock_bh(seqlock_t *sl)
342{
343 write_seqcount_end(&sl->seqcount);
344 spin_unlock_bh(&sl->lock);
345}
346
347static inline void write_seqlock_irq(seqlock_t *sl)
348{
349 spin_lock_irq(&sl->lock);
350 write_seqcount_begin(&sl->seqcount);
351}
352
353static inline void write_sequnlock_irq(seqlock_t *sl)
354{
355 write_seqcount_end(&sl->seqcount);
356 spin_unlock_irq(&sl->lock);
357}
358
359static inline unsigned long __write_seqlock_irqsave(seqlock_t *sl)
360{
361 unsigned long flags;
362
363 spin_lock_irqsave(&sl->lock, flags);
364 write_seqcount_begin(&sl->seqcount);
365 return flags;
366}
367
368#define write_seqlock_irqsave(lock, flags) \
369 do { flags = __write_seqlock_irqsave(lock); } while (0)
370
371static inline void
372write_sequnlock_irqrestore(seqlock_t *sl, unsigned long flags)
373{
374 write_seqcount_end(&sl->seqcount);
375 spin_unlock_irqrestore(&sl->lock, flags);
376}
377
378
379
380
381
382
383static inline void read_seqlock_excl(seqlock_t *sl)
384{
385 spin_lock(&sl->lock);
386}
387
388static inline void read_sequnlock_excl(seqlock_t *sl)
389{
390 spin_unlock(&sl->lock);
391}
392
393
394
395
396
397
398
399
400
401
402
403static inline void read_seqbegin_or_lock(seqlock_t *lock, int *seq)
404{
405 if (!(*seq & 1))
406 *seq = read_seqbegin(lock);
407 else
408 read_seqlock_excl(lock);
409}
410
411static inline int need_seqretry(seqlock_t *lock, int seq)
412{
413 return !(seq & 1) && read_seqretry(lock, seq);
414}
415
416static inline void done_seqretry(seqlock_t *lock, int seq)
417{
418 if (seq & 1)
419 read_sequnlock_excl(lock);
420}
421
422static inline void read_seqlock_excl_bh(seqlock_t *sl)
423{
424 spin_lock_bh(&sl->lock);
425}
426
427static inline void read_sequnlock_excl_bh(seqlock_t *sl)
428{
429 spin_unlock_bh(&sl->lock);
430}
431
432static inline void read_seqlock_excl_irq(seqlock_t *sl)
433{
434 spin_lock_irq(&sl->lock);
435}
436
437static inline void read_sequnlock_excl_irq(seqlock_t *sl)
438{
439 spin_unlock_irq(&sl->lock);
440}
441
442static inline unsigned long __read_seqlock_excl_irqsave(seqlock_t *sl)
443{
444 unsigned long flags;
445
446 spin_lock_irqsave(&sl->lock, flags);
447 return flags;
448}
449
450#define read_seqlock_excl_irqsave(lock, flags) \
451 do { flags = __read_seqlock_excl_irqsave(lock); } while (0)
452
453static inline void
454read_sequnlock_excl_irqrestore(seqlock_t *sl, unsigned long flags)
455{
456 spin_unlock_irqrestore(&sl->lock, flags);
457}
458
459static inline unsigned long
460read_seqbegin_or_lock_irqsave(seqlock_t *lock, int *seq)
461{
462 unsigned long flags = 0;
463
464 if (!(*seq & 1))
465 *seq = read_seqbegin(lock);
466 else
467 read_seqlock_excl_irqsave(lock, flags);
468
469 return flags;
470}
471
472static inline void
473done_seqretry_irqrestore(seqlock_t *lock, int seq, unsigned long flags)
474{
475 if (seq & 1)
476 read_sequnlock_excl_irqrestore(lock, flags);
477}
478#endif
479