Document update in urcu.spin header
[urcu.git] / formal-model / urcu-controldataflow / urcu.spin
1 /*
2 * mem.spin: Promela code to validate memory barriers with OOO memory
3 * and out-of-order instruction scheduling.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 *
19 * Copyright (c) 2009 Mathieu Desnoyers
20 */
21
22 /* Promela validation variables. */
23
24 /* specific defines "included" here */
25 /* DEFINES file "included" here */
26
27 #define NR_READERS 1
28 #define NR_WRITERS 1
29
30 #define NR_PROCS 2
31
32 #define get_pid() (_pid)
33
34 #define get_readerid() (get_pid())
35
36 /*
37 * Produced process control and data flow. Updated after each instruction to
38 * show which variables are ready. Using one-hot bit encoding per variable to
39 * save state space. Used as triggers to execute the instructions having those
40 * variables as input. Leaving bits active to inhibit instruction execution.
41 * Scheme used to make instruction disabling and automatic dependency fall-back
42 * automatic.
43 */
44
45 #define CONSUME_TOKENS(state, bits, notbits) \
46 ((!(state & (notbits))) && (state & (bits)) == (bits))
47
48 #define PRODUCE_TOKENS(state, bits) \
49 state = state | (bits);
50
51 #define CLEAR_TOKENS(state, bits) \
52 state = state & ~(bits)
53
54 /*
55 * Types of dependency :
56 *
57 * Data dependency
58 *
59 * - True dependency, Read-after-Write (RAW)
60 *
61 * This type of dependency happens when a statement depends on the result of a
62 * previous statement. This applies to any statement which needs to read a
63 * variable written by a preceding statement.
64 *
65 * - False dependency, Write-after-Read (WAR)
66 *
67 * Typically, variable renaming can ensure that this dependency goes away.
68 * However, if the statements must read and then write from/to the same variable
69 * in the OOO memory model, renaming may be impossible, and therefore this
70 * causes a WAR dependency.
71 *
72 * - Output dependency, Write-after-Write (WAW)
73 *
74 * Two writes to the same variable in subsequent statements. Variable renaming
75 * can ensure this is not needed, but can be required when writing multiple
76 * times to the same OOO mem model variable.
77 *
78 * Control dependency
79 *
80 * Execution of a given instruction depends on a previous instruction evaluating
81 * in a way that allows its execution. E.g. : branches.
82 *
83 * Useful considerations for joining dependencies after branch
84 *
85 * - Pre-dominance
86 *
87 * "We say box i dominates box j if every path (leading from input to output
88 * through the diagram) which passes through box j must also pass through box
89 * i. Thus box i dominates box j if box j is subordinate to box i in the
90 * program."
91 *
92 * http://www.hipersoft.rice.edu/grads/publications/dom14.pdf
93 * Other classic algorithm to calculate dominance : Lengauer-Tarjan (in gcc)
94 *
95 * - Post-dominance
96 *
97 * Just as pre-dominance, but with arcs of the data flow inverted, and input vs
98 * output exchanged. Therefore, i post-dominating j ensures that every path
99 * passing by j will pass by i before reaching the output.
100 *
101 * Other considerations
102 *
103 * Note about "volatile" keyword dependency : The compiler will order volatile
104 * accesses so they appear in the right order on a given CPU. They can be
105 * reordered by the CPU instruction scheduling. This therefore cannot be
106 * considered as a depencency.
107 *
108 * References :
109 *
110 * Cooper, Keith D.; & Torczon, Linda. (2005). Engineering a Compiler. Morgan
111 * Kaufmann. ISBN 1-55860-698-X.
112 * Kennedy, Ken; & Allen, Randy. (2001). Optimizing Compilers for Modern
113 * Architectures: A Dependence-based Approach. Morgan Kaufmann. ISBN
114 * 1-55860-286-0.
115 * Muchnick, Steven S. (1997). Advanced Compiler Design and Implementation.
116 * Morgan Kaufmann. ISBN 1-55860-320-4.
117 */
118
119 /*
120 * Note about loops and nested calls
121 *
122 * To keep this model simple, loops expressed in the framework will behave as if
123 * there was a core synchronizing instruction between loops. To see the effect
124 * of loop unrolling, manually unrolling loops is required. Note that if loops
125 * end or start with a core synchronizing instruction, the model is appropriate.
126 * Nested calls are not supported.
127 */
128
129 /*
130 * Each process have its own data in cache. Caches are randomly updated.
131 * smp_wmb and smp_rmb forces cache updates (write and read), smp_mb forces
132 * both.
133 */
134
135 typedef per_proc_byte {
136 byte val[NR_PROCS];
137 };
138
139 /* Bitfield has a maximum of 8 procs */
140 typedef per_proc_bit {
141 byte bitfield;
142 };
143
144 #define DECLARE_CACHED_VAR(type, x) \
145 type mem_##x; \
146 per_proc_##type cached_##x; \
147 per_proc_bit cache_dirty_##x;
148
149 #define INIT_CACHED_VAR(x, v, j) \
150 mem_##x = v; \
151 cache_dirty_##x.bitfield = 0; \
152 j = 0; \
153 do \
154 :: j < NR_PROCS -> \
155 cached_##x.val[j] = v; \
156 j++ \
157 :: j >= NR_PROCS -> break \
158 od;
159
160 #define IS_CACHE_DIRTY(x, id) (cache_dirty_##x.bitfield & (1 << id))
161
162 #define READ_CACHED_VAR(x) (cached_##x.val[get_pid()])
163
164 #define WRITE_CACHED_VAR(x, v) \
165 atomic { \
166 cached_##x.val[get_pid()] = v; \
167 cache_dirty_##x.bitfield = \
168 cache_dirty_##x.bitfield | (1 << get_pid()); \
169 }
170
171 #define CACHE_WRITE_TO_MEM(x, id) \
172 if \
173 :: IS_CACHE_DIRTY(x, id) -> \
174 mem_##x = cached_##x.val[id]; \
175 cache_dirty_##x.bitfield = \
176 cache_dirty_##x.bitfield & (~(1 << id)); \
177 :: else -> \
178 skip \
179 fi;
180
181 #define CACHE_READ_FROM_MEM(x, id) \
182 if \
183 :: !IS_CACHE_DIRTY(x, id) -> \
184 cached_##x.val[id] = mem_##x;\
185 :: else -> \
186 skip \
187 fi;
188
189 /*
190 * May update other caches if cache is dirty, or not.
191 */
192 #define RANDOM_CACHE_WRITE_TO_MEM(x, id)\
193 if \
194 :: 1 -> CACHE_WRITE_TO_MEM(x, id); \
195 :: 1 -> skip \
196 fi;
197
198 #define RANDOM_CACHE_READ_FROM_MEM(x, id)\
199 if \
200 :: 1 -> CACHE_READ_FROM_MEM(x, id); \
201 :: 1 -> skip \
202 fi;
203
204 /* Must consume all prior read tokens. All subsequent reads depend on it. */
205 inline smp_rmb(i, j)
206 {
207 atomic {
208 CACHE_READ_FROM_MEM(urcu_gp_ctr, get_pid());
209 i = 0;
210 do
211 :: i < NR_READERS ->
212 CACHE_READ_FROM_MEM(urcu_active_readers[i], get_pid());
213 i++
214 :: i >= NR_READERS -> break
215 od;
216 CACHE_READ_FROM_MEM(generation_ptr, get_pid());
217 }
218 }
219
220 /* Must consume all prior write tokens. All subsequent writes depend on it. */
221 inline smp_wmb(i, j)
222 {
223 atomic {
224 CACHE_WRITE_TO_MEM(urcu_gp_ctr, get_pid());
225 i = 0;
226 do
227 :: i < NR_READERS ->
228 CACHE_WRITE_TO_MEM(urcu_active_readers[i], get_pid());
229 i++
230 :: i >= NR_READERS -> break
231 od;
232 CACHE_WRITE_TO_MEM(generation_ptr, get_pid());
233 }
234 }
235
236 /* Synchronization point. Must consume all prior read and write tokens. All
237 * subsequent reads and writes depend on it. */
238 inline smp_mb(i, j)
239 {
240 atomic {
241 smp_wmb(i, j);
242 smp_rmb(i, j);
243 }
244 }
245
246
247 #ifdef REMOTE_BARRIERS
248
249 bit reader_barrier[NR_READERS];
250
251 /*
252 * We cannot leave the barriers dependencies in place in REMOTE_BARRIERS mode
253 * because they would add unexisting core synchronization and would therefore
254 * create an incomplete model.
255 * Therefore, we model the read-side memory barriers by completely disabling the
256 * memory barriers and their dependencies from the read-side. One at a time
257 * (different verification runs), we make a different instruction listen for
258 * signals.
259 */
260
261 #define smp_mb_reader(i, j)
262
263 /*
264 * Service 0, 1 or many barrier requests.
265 */
266 inline smp_mb_recv(i, j)
267 {
268 do
269 :: (reader_barrier[get_readerid()] == 1) ->
270 smp_mb(i, j);
271 reader_barrier[get_readerid()] = 0;
272 :: 1 -> skip;
273 :: 1 -> break;
274 od;
275 }
276
277 inline smp_mb_send(i, j)
278 {
279 smp_mb(i, j);
280 i = 0;
281 do
282 :: i < NR_READERS ->
283 reader_barrier[i] = 1;
284 do
285 :: (reader_barrier[i] == 1) -> skip;
286 :: (reader_barrier[i] == 0) -> break;
287 od;
288 i++;
289 :: i >= NR_READERS ->
290 break
291 od;
292 smp_mb(i, j);
293 }
294
295 #else
296
297 #define smp_mb_send smp_mb
298 #define smp_mb_reader smp_mb
299 #define smp_mb_recv(i, j)
300
301 #endif
302
303 /* Keep in sync manually with smp_rmb, wmp_wmb, ooo_mem and init() */
304 DECLARE_CACHED_VAR(byte, urcu_gp_ctr);
305 /* Note ! currently only two readers */
306 DECLARE_CACHED_VAR(byte, urcu_active_readers[NR_READERS]);
307 /* pointer generation */
308 DECLARE_CACHED_VAR(byte, generation_ptr);
309
310 byte last_free_gen = 0;
311 bit free_done = 0;
312 byte read_generation[NR_READERS];
313 bit data_access[NR_READERS];
314
315 bit write_lock = 0;
316
317 bit init_done = 0;
318
319 bit sighand_exec = 0;
320
321 inline wait_init_done()
322 {
323 do
324 :: init_done == 0 -> skip;
325 :: else -> break;
326 od;
327 }
328
329 inline ooo_mem(i)
330 {
331 atomic {
332 RANDOM_CACHE_WRITE_TO_MEM(urcu_gp_ctr, get_pid());
333 i = 0;
334 do
335 :: i < NR_READERS ->
336 RANDOM_CACHE_WRITE_TO_MEM(urcu_active_readers[i],
337 get_pid());
338 i++
339 :: i >= NR_READERS -> break
340 od;
341 RANDOM_CACHE_WRITE_TO_MEM(generation_ptr, get_pid());
342 RANDOM_CACHE_READ_FROM_MEM(urcu_gp_ctr, get_pid());
343 i = 0;
344 do
345 :: i < NR_READERS ->
346 RANDOM_CACHE_READ_FROM_MEM(urcu_active_readers[i],
347 get_pid());
348 i++
349 :: i >= NR_READERS -> break
350 od;
351 RANDOM_CACHE_READ_FROM_MEM(generation_ptr, get_pid());
352 }
353 }
354
355 /*
356 * Bit encoding, urcu_reader :
357 */
358
359 int _proc_urcu_reader;
360 #define proc_urcu_reader _proc_urcu_reader
361
362 /* Body of PROCEDURE_READ_LOCK */
363 #define READ_PROD_A_READ (1 << 0)
364 #define READ_PROD_B_IF_TRUE (1 << 1)
365 #define READ_PROD_B_IF_FALSE (1 << 2)
366 #define READ_PROD_C_IF_TRUE_READ (1 << 3)
367
368 #define PROCEDURE_READ_LOCK(base, consumetoken, producetoken) \
369 :: CONSUME_TOKENS(proc_urcu_reader, consumetoken, READ_PROD_A_READ << base) -> \
370 ooo_mem(i); \
371 tmp = READ_CACHED_VAR(urcu_active_readers[get_readerid()]); \
372 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_A_READ << base); \
373 :: CONSUME_TOKENS(proc_urcu_reader, \
374 READ_PROD_A_READ << base, /* RAW, pre-dominant */ \
375 (READ_PROD_B_IF_TRUE | READ_PROD_B_IF_FALSE) << base) -> \
376 if \
377 :: (!(tmp & RCU_GP_CTR_NEST_MASK)) -> \
378 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_B_IF_TRUE << base); \
379 :: else -> \
380 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_B_IF_FALSE << base); \
381 fi; \
382 /* IF TRUE */ \
383 :: CONSUME_TOKENS(proc_urcu_reader, READ_PROD_B_IF_TRUE << base, \
384 READ_PROD_C_IF_TRUE_READ << base) -> \
385 ooo_mem(i); \
386 tmp2 = READ_CACHED_VAR(urcu_gp_ctr); \
387 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_C_IF_TRUE_READ << base); \
388 :: CONSUME_TOKENS(proc_urcu_reader, \
389 (READ_PROD_C_IF_TRUE_READ /* pre-dominant */ \
390 | READ_PROD_A_READ) << base, /* WAR */ \
391 producetoken) -> \
392 ooo_mem(i); \
393 WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp2); \
394 PRODUCE_TOKENS(proc_urcu_reader, producetoken); \
395 /* IF_MERGE implies \
396 * post-dominance */ \
397 /* ELSE */ \
398 :: CONSUME_TOKENS(proc_urcu_reader, \
399 (READ_PROD_B_IF_FALSE /* pre-dominant */ \
400 | READ_PROD_A_READ) << base, /* WAR */ \
401 producetoken) -> \
402 ooo_mem(i); \
403 WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], \
404 tmp + 1); \
405 PRODUCE_TOKENS(proc_urcu_reader, producetoken); \
406 /* IF_MERGE implies \
407 * post-dominance */ \
408 /* ENDIF */ \
409 skip
410
411 /* Body of PROCEDURE_READ_LOCK */
412 #define READ_PROC_READ_UNLOCK (1 << 0)
413
414 #define PROCEDURE_READ_UNLOCK(base, consumetoken, producetoken) \
415 :: CONSUME_TOKENS(proc_urcu_reader, \
416 consumetoken, \
417 READ_PROC_READ_UNLOCK << base) -> \
418 ooo_mem(i); \
419 tmp2 = READ_CACHED_VAR(urcu_active_readers[get_readerid()]); \
420 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_READ_UNLOCK << base); \
421 :: CONSUME_TOKENS(proc_urcu_reader, \
422 consumetoken \
423 | (READ_PROC_READ_UNLOCK << base), /* WAR */ \
424 producetoken) -> \
425 ooo_mem(i); \
426 WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp2 - 1); \
427 PRODUCE_TOKENS(proc_urcu_reader, producetoken); \
428 skip
429
430
431 #define READ_PROD_NONE (1 << 0)
432
433 /* PROCEDURE_READ_LOCK base = << 1 : 1 to 5 */
434 #define READ_LOCK_BASE 1
435 #define READ_LOCK_OUT (1 << 5)
436
437 #define READ_PROC_FIRST_MB (1 << 6)
438
439 /* PROCEDURE_READ_LOCK (NESTED) base : << 7 : 7 to 11 */
440 #define READ_LOCK_NESTED_BASE 7
441 #define READ_LOCK_NESTED_OUT (1 << 11)
442
443 #define READ_PROC_READ_GEN (1 << 12)
444
445 /* PROCEDURE_READ_UNLOCK (NESTED) base = << 13 : 13 to 14 */
446 #define READ_UNLOCK_NESTED_BASE 13
447 #define READ_UNLOCK_NESTED_OUT (1 << 14)
448
449 #define READ_PROC_SECOND_MB (1 << 15)
450
451 /* PROCEDURE_READ_UNLOCK base = << 16 : 16 to 17 */
452 #define READ_UNLOCK_BASE 16
453 #define READ_UNLOCK_OUT (1 << 17)
454
455 /* PROCEDURE_READ_LOCK_UNROLL base = << 18 : 18 to 22 */
456 #define READ_LOCK_UNROLL_BASE 18
457 #define READ_LOCK_OUT_UNROLL (1 << 22)
458
459 #define READ_PROC_THIRD_MB (1 << 23)
460
461 #define READ_PROC_READ_GEN_UNROLL (1 << 24)
462
463 #define READ_PROC_FOURTH_MB (1 << 25)
464
465 /* PROCEDURE_READ_UNLOCK_UNROLL base = << 26 : 26 to 27 */
466 #define READ_UNLOCK_UNROLL_BASE 26
467 #define READ_UNLOCK_OUT_UNROLL (1 << 27)
468
469
470 /* Should not include branches */
471 #define READ_PROC_ALL_TOKENS (READ_PROD_NONE \
472 | READ_LOCK_OUT \
473 | READ_PROC_FIRST_MB \
474 | READ_LOCK_NESTED_OUT \
475 | READ_PROC_READ_GEN \
476 | READ_UNLOCK_NESTED_OUT \
477 | READ_PROC_SECOND_MB \
478 | READ_UNLOCK_OUT \
479 | READ_LOCK_OUT_UNROLL \
480 | READ_PROC_THIRD_MB \
481 | READ_PROC_READ_GEN_UNROLL \
482 | READ_PROC_FOURTH_MB \
483 | READ_UNLOCK_OUT_UNROLL)
484
485 /* Must clear all tokens, including branches */
486 #define READ_PROC_ALL_TOKENS_CLEAR ((1 << 28) - 1)
487
488 inline urcu_one_read(i, j, nest_i, tmp, tmp2)
489 {
490 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_NONE);
491
492 #ifdef NO_MB
493 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FIRST_MB);
494 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_SECOND_MB);
495 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_THIRD_MB);
496 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FOURTH_MB);
497 #endif
498
499 #ifdef REMOTE_BARRIERS
500 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FIRST_MB);
501 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_SECOND_MB);
502 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_THIRD_MB);
503 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FOURTH_MB);
504 #endif
505
506 do
507 :: 1 ->
508
509 #ifdef REMOTE_BARRIERS
510 /*
511 * Signal-based memory barrier will only execute when the
512 * execution order appears in program order.
513 */
514 if
515 :: 1 ->
516 atomic {
517 if
518 :: CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE,
519 READ_LOCK_OUT | READ_LOCK_NESTED_OUT
520 | READ_PROC_READ_GEN | READ_UNLOCK_NESTED_OUT
521 | READ_UNLOCK_OUT
522 | READ_LOCK_OUT_UNROLL
523 | READ_PROC_READ_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
524 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT,
525 READ_LOCK_NESTED_OUT
526 | READ_PROC_READ_GEN | READ_UNLOCK_NESTED_OUT
527 | READ_UNLOCK_OUT
528 | READ_LOCK_OUT_UNROLL
529 | READ_PROC_READ_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
530 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT | READ_LOCK_NESTED_OUT,
531 READ_PROC_READ_GEN | READ_UNLOCK_NESTED_OUT
532 | READ_UNLOCK_OUT
533 | READ_LOCK_OUT_UNROLL
534 | READ_PROC_READ_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
535 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
536 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN,
537 READ_UNLOCK_NESTED_OUT
538 | READ_UNLOCK_OUT
539 | READ_LOCK_OUT_UNROLL
540 | READ_PROC_READ_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
541 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
542 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN | READ_UNLOCK_NESTED_OUT,
543 READ_UNLOCK_OUT
544 | READ_LOCK_OUT_UNROLL
545 | READ_PROC_READ_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
546 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
547 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN | READ_UNLOCK_NESTED_OUT
548 | READ_UNLOCK_OUT,
549 READ_LOCK_OUT_UNROLL
550 | READ_PROC_READ_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
551 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
552 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN | READ_UNLOCK_NESTED_OUT
553 | READ_UNLOCK_OUT | READ_LOCK_OUT_UNROLL,
554 READ_PROC_READ_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
555 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
556 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN | READ_UNLOCK_NESTED_OUT
557 | READ_UNLOCK_OUT | READ_LOCK_OUT_UNROLL
558 | READ_PROC_READ_GEN_UNROLL,
559 READ_UNLOCK_OUT_UNROLL)
560 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
561 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN | READ_UNLOCK_NESTED_OUT
562 | READ_UNLOCK_OUT | READ_LOCK_OUT_UNROLL
563 | READ_PROC_READ_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL,
564 0) ->
565 goto non_atomic3;
566 non_atomic3_end:
567 skip;
568 fi;
569 }
570 :: 1 -> skip;
571 fi;
572
573 goto non_atomic3_skip;
574 non_atomic3:
575 smp_mb_recv(i, j);
576 goto non_atomic3_end;
577 non_atomic3_skip:
578
579 #endif /* REMOTE_BARRIERS */
580
581 atomic {
582 if
583 PROCEDURE_READ_LOCK(READ_LOCK_BASE, READ_PROD_NONE, READ_LOCK_OUT);
584
585 :: CONSUME_TOKENS(proc_urcu_reader,
586 READ_LOCK_OUT, /* post-dominant */
587 READ_PROC_FIRST_MB) ->
588 smp_mb_reader(i, j);
589 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FIRST_MB);
590
591 PROCEDURE_READ_LOCK(READ_LOCK_NESTED_BASE, READ_PROC_FIRST_MB | READ_LOCK_OUT,
592 READ_LOCK_NESTED_OUT);
593
594 :: CONSUME_TOKENS(proc_urcu_reader,
595 READ_PROC_FIRST_MB, /* mb() orders reads */
596 READ_PROC_READ_GEN) ->
597 ooo_mem(i);
598 read_generation[get_readerid()] =
599 READ_CACHED_VAR(generation_ptr);
600 goto non_atomic;
601 non_atomic_end:
602 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_READ_GEN);
603
604 /* Note : we remove the nested memory barrier from the read unlock
605 * model, given it is not usually needed. The implementation has the barrier
606 * because the performance impact added by a branch in the common case does not
607 * justify it.
608 */
609
610 PROCEDURE_READ_UNLOCK(READ_UNLOCK_NESTED_BASE,
611 READ_PROC_FIRST_MB
612 | READ_LOCK_OUT
613 | READ_LOCK_NESTED_OUT,
614 READ_UNLOCK_NESTED_OUT);
615
616
617 :: CONSUME_TOKENS(proc_urcu_reader,
618 READ_PROC_READ_GEN /* mb() orders reads */
619 | READ_PROC_FIRST_MB /* mb() ordered */
620 | READ_LOCK_OUT /* post-dominant */
621 | READ_LOCK_NESTED_OUT /* post-dominant */
622 | READ_UNLOCK_NESTED_OUT,
623 READ_PROC_SECOND_MB) ->
624 smp_mb_reader(i, j);
625 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_SECOND_MB);
626
627 PROCEDURE_READ_UNLOCK(READ_UNLOCK_BASE,
628 READ_PROC_SECOND_MB /* mb() orders reads */
629 | READ_PROC_FIRST_MB /* mb() orders reads */
630 | READ_LOCK_NESTED_OUT /* RAW */
631 | READ_LOCK_OUT /* RAW */
632 | READ_UNLOCK_NESTED_OUT, /* RAW */
633 READ_UNLOCK_OUT);
634
635 /* Unrolling loop : second consecutive lock */
636 /* reading urcu_active_readers, which have been written by
637 * READ_UNLOCK_OUT : RAW */
638 PROCEDURE_READ_LOCK(READ_LOCK_UNROLL_BASE,
639 READ_UNLOCK_OUT /* RAW */
640 | READ_PROC_SECOND_MB /* mb() orders reads */
641 | READ_PROC_FIRST_MB /* mb() orders reads */
642 | READ_LOCK_NESTED_OUT /* RAW */
643 | READ_LOCK_OUT /* RAW */
644 | READ_UNLOCK_NESTED_OUT, /* RAW */
645 READ_LOCK_OUT_UNROLL);
646
647
648 :: CONSUME_TOKENS(proc_urcu_reader,
649 READ_PROC_FIRST_MB /* mb() ordered */
650 | READ_PROC_SECOND_MB /* mb() ordered */
651 | READ_LOCK_OUT_UNROLL /* post-dominant */
652 | READ_LOCK_NESTED_OUT
653 | READ_LOCK_OUT
654 | READ_UNLOCK_NESTED_OUT
655 | READ_UNLOCK_OUT,
656 READ_PROC_THIRD_MB) ->
657 smp_mb_reader(i, j);
658 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_THIRD_MB);
659
660 :: CONSUME_TOKENS(proc_urcu_reader,
661 READ_PROC_FIRST_MB /* mb() orders reads */
662 | READ_PROC_SECOND_MB /* mb() orders reads */
663 | READ_PROC_THIRD_MB, /* mb() orders reads */
664 READ_PROC_READ_GEN_UNROLL) ->
665 ooo_mem(i);
666 read_generation[get_readerid()] =
667 READ_CACHED_VAR(generation_ptr);
668 goto non_atomic2;
669 non_atomic2_end:
670 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_READ_GEN_UNROLL);
671
672 :: CONSUME_TOKENS(proc_urcu_reader,
673 READ_PROC_READ_GEN_UNROLL /* mb() orders reads */
674 | READ_PROC_FIRST_MB /* mb() ordered */
675 | READ_PROC_SECOND_MB /* mb() ordered */
676 | READ_PROC_THIRD_MB /* mb() ordered */
677 | READ_LOCK_OUT_UNROLL /* post-dominant */
678 | READ_LOCK_NESTED_OUT
679 | READ_LOCK_OUT
680 | READ_UNLOCK_NESTED_OUT
681 | READ_UNLOCK_OUT,
682 READ_PROC_FOURTH_MB) ->
683 smp_mb_reader(i, j);
684 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FOURTH_MB);
685
686 PROCEDURE_READ_UNLOCK(READ_UNLOCK_UNROLL_BASE,
687 READ_PROC_FOURTH_MB /* mb() orders reads */
688 | READ_PROC_THIRD_MB /* mb() orders reads */
689 | READ_LOCK_OUT_UNROLL /* RAW */
690 | READ_PROC_SECOND_MB /* mb() orders reads */
691 | READ_PROC_FIRST_MB /* mb() orders reads */
692 | READ_LOCK_NESTED_OUT /* RAW */
693 | READ_LOCK_OUT /* RAW */
694 | READ_UNLOCK_NESTED_OUT, /* RAW */
695 READ_UNLOCK_OUT_UNROLL);
696 :: CONSUME_TOKENS(proc_urcu_reader, READ_PROC_ALL_TOKENS, 0) ->
697 CLEAR_TOKENS(proc_urcu_reader, READ_PROC_ALL_TOKENS_CLEAR);
698 break;
699 fi;
700 }
701 od;
702 /*
703 * Dependency between consecutive loops :
704 * RAW dependency on
705 * WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp2 - 1)
706 * tmp = READ_CACHED_VAR(urcu_active_readers[get_readerid()]);
707 * between loops.
708 * _WHEN THE MB()s are in place_, they add full ordering of the
709 * generation pointer read wrt active reader count read, which ensures
710 * execution will not spill across loop execution.
711 * However, in the event mb()s are removed (execution using signal
712 * handler to promote barrier()() -> smp_mb()), nothing prevents one loop
713 * to spill its execution on other loop's execution.
714 */
715 goto end;
716 non_atomic:
717 data_access[get_readerid()] = 1;
718 data_access[get_readerid()] = 0;
719 goto non_atomic_end;
720 non_atomic2:
721 data_access[get_readerid()] = 1;
722 data_access[get_readerid()] = 0;
723 goto non_atomic2_end;
724 end:
725 skip;
726 }
727
728
729
730 active proctype urcu_reader()
731 {
732 byte i, j, nest_i;
733 byte tmp, tmp2;
734
735 wait_init_done();
736
737 assert(get_pid() < NR_PROCS);
738
739 end_reader:
740 do
741 :: 1 ->
742 /*
743 * We do not test reader's progress here, because we are mainly
744 * interested in writer's progress. The reader never blocks
745 * anyway. We have to test for reader/writer's progress
746 * separately, otherwise we could think the writer is doing
747 * progress when it's blocked by an always progressing reader.
748 */
749 #ifdef READER_PROGRESS
750 progress_reader:
751 #endif
752 urcu_one_read(i, j, nest_i, tmp, tmp2);
753 od;
754 }
755
756 /* no name clash please */
757 #undef proc_urcu_reader
758
759
760 /* Model the RCU update process. */
761
762 /*
763 * Bit encoding, urcu_writer :
764 * Currently only supports one reader.
765 */
766
767 int _proc_urcu_writer;
768 #define proc_urcu_writer _proc_urcu_writer
769
770 #define WRITE_PROD_NONE (1 << 0)
771
772 #define WRITE_PROC_FIRST_MB (1 << 1)
773
774 /* first flip */
775 #define WRITE_PROC_FIRST_READ_GP (1 << 2)
776 #define WRITE_PROC_FIRST_WRITE_GP (1 << 3)
777 #define WRITE_PROC_FIRST_WAIT (1 << 4)
778 #define WRITE_PROC_FIRST_WAIT_LOOP (1 << 5)
779
780 /* second flip */
781 #define WRITE_PROC_SECOND_READ_GP (1 << 6)
782 #define WRITE_PROC_SECOND_WRITE_GP (1 << 7)
783 #define WRITE_PROC_SECOND_WAIT (1 << 8)
784 #define WRITE_PROC_SECOND_WAIT_LOOP (1 << 9)
785
786 #define WRITE_PROC_SECOND_MB (1 << 10)
787
788 #define WRITE_PROC_ALL_TOKENS (WRITE_PROD_NONE \
789 | WRITE_PROC_FIRST_MB \
790 | WRITE_PROC_FIRST_READ_GP \
791 | WRITE_PROC_FIRST_WRITE_GP \
792 | WRITE_PROC_FIRST_WAIT \
793 | WRITE_PROC_SECOND_READ_GP \
794 | WRITE_PROC_SECOND_WRITE_GP \
795 | WRITE_PROC_SECOND_WAIT \
796 | WRITE_PROC_SECOND_MB)
797
798 #define WRITE_PROC_ALL_TOKENS_CLEAR ((1 << 11) - 1)
799
800 active proctype urcu_writer()
801 {
802 byte i, j;
803 byte tmp, tmp2, tmpa;
804 byte old_gen;
805
806 wait_init_done();
807
808 assert(get_pid() < NR_PROCS);
809
810 do
811 :: (READ_CACHED_VAR(generation_ptr) < 5) ->
812 #ifdef WRITER_PROGRESS
813 progress_writer1:
814 #endif
815 ooo_mem(i);
816 atomic {
817 old_gen = READ_CACHED_VAR(generation_ptr);
818 WRITE_CACHED_VAR(generation_ptr, old_gen + 1);
819 }
820 ooo_mem(i);
821
822 do
823 :: 1 ->
824 atomic {
825 if
826 :: write_lock == 0 ->
827 write_lock = 1;
828 break;
829 :: else ->
830 skip;
831 fi;
832 }
833 od;
834
835 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROD_NONE);
836
837 #ifdef NO_MB
838 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_MB);
839 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_MB);
840 #endif
841
842 #ifdef SINGLE_FLIP
843 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_READ_GP);
844 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WRITE_GP);
845 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT);
846 #endif
847
848 do
849 :: CONSUME_TOKENS(proc_urcu_writer,
850 WRITE_PROD_NONE,
851 WRITE_PROC_FIRST_MB) ->
852 smp_mb_send(i, j);
853 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_MB);
854
855 /* first flip */
856 :: CONSUME_TOKENS(proc_urcu_writer,
857 WRITE_PROC_FIRST_MB,
858 WRITE_PROC_FIRST_READ_GP) ->
859 tmpa = READ_CACHED_VAR(urcu_gp_ctr);
860 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_READ_GP);
861 :: CONSUME_TOKENS(proc_urcu_writer,
862 WRITE_PROC_FIRST_MB | WRITE_PROC_FIRST_READ_GP,
863 WRITE_PROC_FIRST_WRITE_GP) ->
864 ooo_mem(i);
865 WRITE_CACHED_VAR(urcu_gp_ctr, tmpa ^ RCU_GP_CTR_BIT);
866 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WRITE_GP);
867
868 :: CONSUME_TOKENS(proc_urcu_writer,
869 //WRITE_PROC_FIRST_WRITE_GP /* TEST ADDING SYNC CORE */
870 WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
871 WRITE_PROC_FIRST_WAIT | WRITE_PROC_FIRST_WAIT_LOOP) ->
872 ooo_mem(i);
873 /* ONLY WAITING FOR READER 0 */
874 tmp2 = READ_CACHED_VAR(urcu_active_readers[0]);
875 if
876 :: (tmp2 & RCU_GP_CTR_NEST_MASK)
877 && ((tmp2 ^ RCU_GP_CTR_BIT) & RCU_GP_CTR_BIT) ->
878 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WAIT_LOOP);
879 :: else ->
880 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WAIT);
881 fi;
882
883 :: CONSUME_TOKENS(proc_urcu_writer,
884 //WRITE_PROC_FIRST_WRITE_GP /* TEST ADDING SYNC CORE */
885 WRITE_PROC_FIRST_WRITE_GP
886 | WRITE_PROC_FIRST_READ_GP
887 | WRITE_PROC_FIRST_WAIT_LOOP
888 | WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
889 0) ->
890 #ifndef GEN_ERROR_WRITER_PROGRESS
891 smp_mb_send(i, j);
892 #else
893 ooo_mem(i);
894 #endif
895 /* This instruction loops to WRITE_PROC_FIRST_WAIT */
896 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WAIT_LOOP | WRITE_PROC_FIRST_WAIT);
897
898 /* second flip */
899 :: CONSUME_TOKENS(proc_urcu_writer,
900 WRITE_PROC_FIRST_WAIT /* Control dependency : need to branch out of
901 * the loop to execute the next flip (CHECK) */
902 | WRITE_PROC_FIRST_WRITE_GP
903 | WRITE_PROC_FIRST_READ_GP
904 | WRITE_PROC_FIRST_MB,
905 WRITE_PROC_SECOND_READ_GP) ->
906 //smp_mb_send(i, j); //TEST
907 ooo_mem(i);
908 tmpa = READ_CACHED_VAR(urcu_gp_ctr);
909 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_READ_GP);
910 :: CONSUME_TOKENS(proc_urcu_writer,
911 WRITE_PROC_FIRST_MB
912 | WRITE_PROC_FIRST_READ_GP
913 | WRITE_PROC_FIRST_WRITE_GP
914 | WRITE_PROC_SECOND_READ_GP,
915 WRITE_PROC_SECOND_WRITE_GP) ->
916 ooo_mem(i);
917 WRITE_CACHED_VAR(urcu_gp_ctr, tmpa ^ RCU_GP_CTR_BIT);
918 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WRITE_GP);
919
920 :: CONSUME_TOKENS(proc_urcu_writer,
921 //WRITE_PROC_FIRST_WRITE_GP /* TEST ADDING SYNC CORE */
922 WRITE_PROC_FIRST_WAIT
923 | WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
924 WRITE_PROC_SECOND_WAIT | WRITE_PROC_SECOND_WAIT_LOOP) ->
925 ooo_mem(i);
926 /* ONLY WAITING FOR READER 0 */
927 tmp2 = READ_CACHED_VAR(urcu_active_readers[0]);
928 if
929 :: (tmp2 & RCU_GP_CTR_NEST_MASK)
930 && ((tmp2 ^ 0) & RCU_GP_CTR_BIT) ->
931 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT_LOOP);
932 :: else ->
933 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT);
934 fi;
935
936 :: CONSUME_TOKENS(proc_urcu_writer,
937 //WRITE_PROC_FIRST_WRITE_GP /* TEST ADDING SYNC CORE */
938 WRITE_PROC_SECOND_WRITE_GP
939 | WRITE_PROC_FIRST_WRITE_GP
940 | WRITE_PROC_SECOND_READ_GP
941 | WRITE_PROC_FIRST_READ_GP
942 | WRITE_PROC_SECOND_WAIT_LOOP
943 | WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
944 0) ->
945 #ifndef GEN_ERROR_WRITER_PROGRESS
946 smp_mb_send(i, j);
947 #else
948 ooo_mem(i);
949 #endif
950 /* This instruction loops to WRITE_PROC_SECOND_WAIT */
951 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT_LOOP | WRITE_PROC_SECOND_WAIT);
952
953
954 :: CONSUME_TOKENS(proc_urcu_writer,
955 WRITE_PROC_FIRST_WAIT
956 | WRITE_PROC_SECOND_WAIT
957 | WRITE_PROC_FIRST_READ_GP
958 | WRITE_PROC_SECOND_READ_GP
959 | WRITE_PROC_FIRST_WRITE_GP
960 | WRITE_PROC_SECOND_WRITE_GP
961 | WRITE_PROC_FIRST_MB,
962 WRITE_PROC_SECOND_MB) ->
963 smp_mb_send(i, j);
964 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_MB);
965
966 :: CONSUME_TOKENS(proc_urcu_writer, WRITE_PROC_ALL_TOKENS, 0) ->
967 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_ALL_TOKENS_CLEAR);
968 break;
969 od;
970
971 write_lock = 0;
972 /* free-up step, e.g., kfree(). */
973 atomic {
974 last_free_gen = old_gen;
975 free_done = 1;
976 }
977 :: else -> break;
978 od;
979 /*
980 * Given the reader loops infinitely, let the writer also busy-loop
981 * with progress here so, with weak fairness, we can test the
982 * writer's progress.
983 */
984 end_writer:
985 do
986 :: 1 ->
987 #ifdef WRITER_PROGRESS
988 progress_writer2:
989 #endif
990 skip;
991 od;
992 }
993
994 /* no name clash please */
995 #undef proc_urcu_writer
996
997
998 /* Leave after the readers and writers so the pid count is ok. */
999 init {
1000 byte i, j;
1001
1002 atomic {
1003 INIT_CACHED_VAR(urcu_gp_ctr, 1, j);
1004 INIT_CACHED_VAR(generation_ptr, 0, j);
1005
1006 i = 0;
1007 do
1008 :: i < NR_READERS ->
1009 INIT_CACHED_VAR(urcu_active_readers[i], 0, j);
1010 read_generation[i] = 1;
1011 data_access[i] = 0;
1012 i++;
1013 :: i >= NR_READERS -> break
1014 od;
1015 init_done = 1;
1016 }
1017 }
This page took 0.049022 seconds and 4 git commands to generate.