2 * mem.spin: Promela code to validate memory barriers with OOO memory.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * Copyright (c) 2009 Mathieu Desnoyers
21 /* Promela validation variables. */
23 /* specific defines "included" here */
24 /* DEFINES file "included" here */
31 #define get_pid() (_pid)
33 #define get_readerid() (get_pid())
36 * Produced process control and data flow. Updated after each instruction to
37 * show which variables are ready. Using one-hot bit encoding per variable to
38 * save state space. Used as triggers to execute the instructions having those
39 * variables as input. Leaving bits active to inhibit instruction execution.
40 * Scheme used to make instruction disabling and automatic dependency fall-back
44 #define CONSUME_TOKENS(state, bits, notbits) \
45 ((!(state & (notbits))) && (state & (bits)) == (bits))
47 #define PRODUCE_TOKENS(state, bits) \
48 state = state | (bits);
50 #define CLEAR_TOKENS(state, bits) \
51 state = state & ~(bits)
54 * Types of dependency :
58 * - True dependency, Read-after-Write (RAW)
60 * This type of dependency happens when a statement depends on the result of a
61 * previous statement. This applies to any statement which needs to read a
62 * variable written by a preceding statement.
64 * - False dependency, Write-after-Read (WAR)
66 * Typically, variable renaming can ensure that this dependency goes away.
67 * However, if the statements must read and then write from/to the same variable
68 * in the OOO memory model, renaming may be impossible, and therefore this
69 * causes a WAR dependency.
71 * - Output dependency, Write-after-Write (WAW)
73 * Two writes to the same variable in subsequent statements. Variable renaming
74 * can ensure this is not needed, but can be required when writing multiple
75 * times to the same OOO mem model variable.
79 * Execution of a given instruction depends on a previous instruction evaluating
80 * in a way that allows its execution. E.g. : branches.
82 * Useful considerations for joining dependencies after branch
86 * "We say box i dominates box j if every path (leading from input to output
87 * through the diagram) which passes through box j must also pass through box
88 * i. Thus box i dominates box j if box j is subordinate to box i in the
91 * http://www.hipersoft.rice.edu/grads/publications/dom14.pdf
92 * Other classic algorithm to calculate dominance : Lengauer-Tarjan (in gcc)
96 * Just as pre-dominance, but with arcs of the data flow inverted, and input vs
97 * output exchanged. Therefore, i post-dominating j ensures that every path
98 * passing by j will pass by i before reaching the output.
100 * Other considerations
102 * Note about "volatile" keyword dependency : The compiler will order volatile
103 * accesses so they appear in the right order on a given CPU. They can be
104 * reordered by the CPU instruction scheduling. This therefore cannot be
105 * considered as a depencency.
109 * Cooper, Keith D.; & Torczon, Linda. (2005). Engineering a Compiler. Morgan
110 * Kaufmann. ISBN 1-55860-698-X.
111 * Kennedy, Ken; & Allen, Randy. (2001). Optimizing Compilers for Modern
112 * Architectures: A Dependence-based Approach. Morgan Kaufmann. ISBN
114 * Muchnick, Steven S. (1997). Advanced Compiler Design and Implementation.
115 * Morgan Kaufmann. ISBN 1-55860-320-4.
119 * Note about loops and nested calls
121 * To keep this model simple, loops expressed in the framework will behave as if
122 * there was a core synchronizing instruction between loops. To see the effect
123 * of loop unrolling, manually unrolling loops is required. Note that if loops
124 * end or start with a core synchronizing instruction, the model is appropriate.
125 * Nested calls are not supported.
129 * Each process have its own data in cache. Caches are randomly updated.
130 * smp_wmb and smp_rmb forces cache updates (write and read), smp_mb forces
134 typedef per_proc_byte {
138 /* Bitfield has a maximum of 8 procs */
139 typedef per_proc_bit {
143 #define DECLARE_CACHED_VAR(type, x) \
145 per_proc_##type cached_##x; \
146 per_proc_bit cache_dirty_##x;
148 #define INIT_CACHED_VAR(x, v, j) \
150 cache_dirty_##x.bitfield = 0; \
154 cached_##x.val[j] = v; \
156 :: j >= NR_PROCS -> break \
159 #define IS_CACHE_DIRTY(x, id) (cache_dirty_##x.bitfield & (1 << id))
161 #define READ_CACHED_VAR(x) (cached_##x.val[get_pid()])
163 #define WRITE_CACHED_VAR(x, v) \
165 cached_##x.val[get_pid()] = v; \
166 cache_dirty_##x.bitfield = \
167 cache_dirty_##x.bitfield | (1 << get_pid()); \
170 #define CACHE_WRITE_TO_MEM(x, id) \
172 :: IS_CACHE_DIRTY(x, id) -> \
173 mem_##x = cached_##x.val[id]; \
174 cache_dirty_##x.bitfield = \
175 cache_dirty_##x.bitfield & (~(1 << id)); \
180 #define CACHE_READ_FROM_MEM(x, id) \
182 :: !IS_CACHE_DIRTY(x, id) -> \
183 cached_##x.val[id] = mem_##x;\
189 * May update other caches if cache is dirty, or not.
191 #define RANDOM_CACHE_WRITE_TO_MEM(x, id)\
193 :: 1 -> CACHE_WRITE_TO_MEM(x, id); \
197 #define RANDOM_CACHE_READ_FROM_MEM(x, id)\
199 :: 1 -> CACHE_READ_FROM_MEM(x, id); \
203 /* Must consume all prior read tokens. All subsequent reads depend on it. */
207 CACHE_READ_FROM_MEM(urcu_gp_ctr, get_pid());
211 CACHE_READ_FROM_MEM(urcu_active_readers[i], get_pid());
213 :: i >= NR_READERS -> break
215 CACHE_READ_FROM_MEM(generation_ptr, get_pid());
219 /* Must consume all prior write tokens. All subsequent writes depend on it. */
223 CACHE_WRITE_TO_MEM(urcu_gp_ctr, get_pid());
227 CACHE_WRITE_TO_MEM(urcu_active_readers[i], get_pid());
229 :: i >= NR_READERS -> break
231 CACHE_WRITE_TO_MEM(generation_ptr, get_pid());
235 /* Synchronization point. Must consume all prior read and write tokens. All
236 * subsequent reads and writes depend on it. */
246 #ifdef REMOTE_BARRIERS
248 bit reader_barrier[NR_READERS];
251 * We cannot leave the barriers dependencies in place in REMOTE_BARRIERS mode
252 * because they would add unexisting core synchronization and would therefore
253 * create an incomplete model.
254 * Therefore, we model the read-side memory barriers by completely disabling the
255 * memory barriers and their dependencies from the read-side. One at a time
256 * (different verification runs), we make a different instruction listen for
260 #define smp_mb_reader(i, j)
263 * Service 0, 1 or many barrier requests.
265 inline smp_mb_recv(i, j)
268 :: (reader_barrier[get_readerid()] == 1) ->
270 reader_barrier[get_readerid()] = 0;
276 inline smp_mb_send(i, j)
282 reader_barrier[i] = 1;
284 :: (reader_barrier[i] == 1) -> skip;
285 :: (reader_barrier[i] == 0) -> break;
288 :: i >= NR_READERS ->
296 #define smp_mb_send smp_mb
297 #define smp_mb_reader smp_mb
298 #define smp_mb_recv(i, j)
302 /* Keep in sync manually with smp_rmb, wmp_wmb, ooo_mem and init() */
303 DECLARE_CACHED_VAR(byte, urcu_gp_ctr);
304 /* Note ! currently only two readers */
305 DECLARE_CACHED_VAR(byte, urcu_active_readers[NR_READERS]);
306 /* pointer generation */
307 DECLARE_CACHED_VAR(byte, generation_ptr);
309 byte last_free_gen = 0;
311 byte read_generation[NR_READERS];
312 bit data_access[NR_READERS];
318 bit sighand_exec = 0;
320 inline wait_init_done()
323 :: init_done == 0 -> skip;
331 RANDOM_CACHE_WRITE_TO_MEM(urcu_gp_ctr, get_pid());
335 RANDOM_CACHE_WRITE_TO_MEM(urcu_active_readers[i],
338 :: i >= NR_READERS -> break
340 RANDOM_CACHE_WRITE_TO_MEM(generation_ptr, get_pid());
341 RANDOM_CACHE_READ_FROM_MEM(urcu_gp_ctr, get_pid());
345 RANDOM_CACHE_READ_FROM_MEM(urcu_active_readers[i],
348 :: i >= NR_READERS -> break
350 RANDOM_CACHE_READ_FROM_MEM(generation_ptr, get_pid());
355 * Bit encoding, urcu_reader :
358 int _proc_urcu_reader;
359 #define proc_urcu_reader _proc_urcu_reader
361 /* Body of PROCEDURE_READ_LOCK */
362 #define READ_PROD_A_READ (1 << 0)
363 #define READ_PROD_B_IF_TRUE (1 << 1)
364 #define READ_PROD_B_IF_FALSE (1 << 2)
365 #define READ_PROD_C_IF_TRUE_READ (1 << 3)
367 #define PROCEDURE_READ_LOCK(base, consumetoken, producetoken) \
368 :: CONSUME_TOKENS(proc_urcu_reader, consumetoken, READ_PROD_A_READ << base) -> \
370 tmp = READ_CACHED_VAR(urcu_active_readers[get_readerid()]); \
371 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_A_READ << base); \
372 :: CONSUME_TOKENS(proc_urcu_reader, \
373 READ_PROD_A_READ << base, /* RAW, pre-dominant */ \
374 (READ_PROD_B_IF_TRUE | READ_PROD_B_IF_FALSE) << base) -> \
376 :: (!(tmp & RCU_GP_CTR_NEST_MASK)) -> \
377 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_B_IF_TRUE << base); \
379 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_B_IF_FALSE << base); \
382 :: CONSUME_TOKENS(proc_urcu_reader, READ_PROD_B_IF_TRUE << base, \
383 READ_PROD_C_IF_TRUE_READ << base) -> \
385 tmp2 = READ_CACHED_VAR(urcu_gp_ctr); \
386 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_C_IF_TRUE_READ << base); \
387 :: CONSUME_TOKENS(proc_urcu_reader, \
388 (READ_PROD_C_IF_TRUE_READ /* pre-dominant */ \
389 | READ_PROD_A_READ) << base, /* WAR */ \
392 WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp2); \
393 PRODUCE_TOKENS(proc_urcu_reader, producetoken); \
394 /* IF_MERGE implies \
395 * post-dominance */ \
397 :: CONSUME_TOKENS(proc_urcu_reader, \
398 (READ_PROD_B_IF_FALSE /* pre-dominant */ \
399 | READ_PROD_A_READ) << base, /* WAR */ \
402 WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], \
404 PRODUCE_TOKENS(proc_urcu_reader, producetoken); \
405 /* IF_MERGE implies \
406 * post-dominance */ \
410 /* Body of PROCEDURE_READ_LOCK */
411 #define READ_PROC_READ_UNLOCK (1 << 0)
413 #define PROCEDURE_READ_UNLOCK(base, consumetoken, producetoken) \
414 :: CONSUME_TOKENS(proc_urcu_reader, \
416 READ_PROC_READ_UNLOCK << base) -> \
418 tmp2 = READ_CACHED_VAR(urcu_active_readers[get_readerid()]); \
419 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_READ_UNLOCK << base); \
420 :: CONSUME_TOKENS(proc_urcu_reader, \
422 | (READ_PROC_READ_UNLOCK << base), /* WAR */ \
425 WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp2 - 1); \
426 PRODUCE_TOKENS(proc_urcu_reader, producetoken); \
430 #define READ_PROD_NONE (1 << 0)
432 /* PROCEDURE_READ_LOCK base = << 1 : 1 to 5 */
433 #define READ_LOCK_BASE 1
434 #define READ_LOCK_OUT (1 << 5)
436 #define READ_PROC_FIRST_MB (1 << 6)
438 /* PROCEDURE_READ_LOCK (NESTED) base : << 7 : 7 to 11 */
439 #define READ_LOCK_NESTED_BASE 7
440 #define READ_LOCK_NESTED_OUT (1 << 11)
442 #define READ_PROC_READ_GEN (1 << 12)
444 /* PROCEDURE_READ_UNLOCK (NESTED) base = << 13 : 13 to 14 */
445 #define READ_UNLOCK_NESTED_BASE 13
446 #define READ_UNLOCK_NESTED_OUT (1 << 14)
448 #define READ_PROC_SECOND_MB (1 << 15)
450 /* PROCEDURE_READ_UNLOCK base = << 16 : 16 to 17 */
451 #define READ_UNLOCK_BASE 16
452 #define READ_UNLOCK_OUT (1 << 17)
454 /* PROCEDURE_READ_LOCK_UNROLL base = << 18 : 18 to 22 */
455 #define READ_LOCK_UNROLL_BASE 18
456 #define READ_LOCK_OUT_UNROLL (1 << 22)
458 #define READ_PROC_THIRD_MB (1 << 23)
460 #define READ_PROC_READ_GEN_UNROLL (1 << 24)
462 #define READ_PROC_FOURTH_MB (1 << 25)
464 /* PROCEDURE_READ_UNLOCK_UNROLL base = << 26 : 26 to 27 */
465 #define READ_UNLOCK_UNROLL_BASE 26
466 #define READ_UNLOCK_OUT_UNROLL (1 << 27)
469 /* Should not include branches */
470 #define READ_PROC_ALL_TOKENS (READ_PROD_NONE \
472 | READ_PROC_FIRST_MB \
473 | READ_LOCK_NESTED_OUT \
474 | READ_PROC_READ_GEN \
475 | READ_UNLOCK_NESTED_OUT \
476 | READ_PROC_SECOND_MB \
478 | READ_LOCK_OUT_UNROLL \
479 | READ_PROC_THIRD_MB \
480 | READ_PROC_READ_GEN_UNROLL \
481 | READ_PROC_FOURTH_MB \
482 | READ_UNLOCK_OUT_UNROLL)
484 /* Must clear all tokens, including branches */
485 #define READ_PROC_ALL_TOKENS_CLEAR ((1 << 28) - 1)
487 inline urcu_one_read(i, j, nest_i, tmp, tmp2)
489 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_NONE);
492 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FIRST_MB);
493 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_SECOND_MB);
494 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_THIRD_MB);
495 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FOURTH_MB);
498 #ifdef REMOTE_BARRIERS
499 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FIRST_MB);
500 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_SECOND_MB);
501 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_THIRD_MB);
502 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FOURTH_MB);
508 #ifdef REMOTE_BARRIERS
510 * Signal-based memory barrier will only execute when the
511 * execution order appears in program order.
517 :: CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE,
518 READ_LOCK_OUT | READ_LOCK_NESTED_OUT
519 | READ_PROC_READ_GEN | READ_UNLOCK_NESTED_OUT
521 | READ_LOCK_OUT_UNROLL
522 | READ_PROC_READ_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
523 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT,
525 | READ_PROC_READ_GEN | READ_UNLOCK_NESTED_OUT
527 | READ_LOCK_OUT_UNROLL
528 | READ_PROC_READ_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
529 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT | READ_LOCK_NESTED_OUT,
530 READ_PROC_READ_GEN | READ_UNLOCK_NESTED_OUT
532 | READ_LOCK_OUT_UNROLL
533 | READ_PROC_READ_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
534 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
535 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN,
536 READ_UNLOCK_NESTED_OUT
538 | READ_LOCK_OUT_UNROLL
539 | READ_PROC_READ_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
540 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
541 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN | READ_UNLOCK_NESTED_OUT,
543 | READ_LOCK_OUT_UNROLL
544 | READ_PROC_READ_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
545 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
546 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN | READ_UNLOCK_NESTED_OUT
549 | READ_PROC_READ_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
550 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
551 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN | READ_UNLOCK_NESTED_OUT
552 | READ_UNLOCK_OUT | READ_LOCK_OUT_UNROLL,
553 READ_PROC_READ_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
554 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
555 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN | READ_UNLOCK_NESTED_OUT
556 | READ_UNLOCK_OUT | READ_LOCK_OUT_UNROLL
557 | READ_PROC_READ_GEN_UNROLL,
558 READ_UNLOCK_OUT_UNROLL)
559 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
560 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN | READ_UNLOCK_NESTED_OUT
561 | READ_UNLOCK_OUT | READ_LOCK_OUT_UNROLL
562 | READ_PROC_READ_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL,
572 goto non_atomic3_skip;
575 goto non_atomic3_end;
578 #endif /* REMOTE_BARRIERS */
582 PROCEDURE_READ_LOCK(READ_LOCK_BASE, READ_PROD_NONE, READ_LOCK_OUT);
584 :: CONSUME_TOKENS(proc_urcu_reader,
585 READ_LOCK_OUT, /* post-dominant */
586 READ_PROC_FIRST_MB) ->
588 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FIRST_MB);
590 PROCEDURE_READ_LOCK(READ_LOCK_NESTED_BASE, READ_PROC_FIRST_MB | READ_LOCK_OUT,
591 READ_LOCK_NESTED_OUT);
593 :: CONSUME_TOKENS(proc_urcu_reader,
594 READ_PROC_FIRST_MB, /* mb() orders reads */
595 READ_PROC_READ_GEN) ->
597 read_generation[get_readerid()] =
598 READ_CACHED_VAR(generation_ptr);
601 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_READ_GEN);
603 /* Note : we remove the nested memory barrier from the read unlock
604 * model, given it is not usually needed. The implementation has the barrier
605 * because the performance impact added by a branch in the common case does not
609 PROCEDURE_READ_UNLOCK(READ_UNLOCK_NESTED_BASE,
612 | READ_LOCK_NESTED_OUT,
613 READ_UNLOCK_NESTED_OUT);
616 :: CONSUME_TOKENS(proc_urcu_reader,
617 READ_PROC_READ_GEN /* mb() orders reads */
618 | READ_PROC_FIRST_MB /* mb() ordered */
619 | READ_LOCK_OUT /* post-dominant */
620 | READ_LOCK_NESTED_OUT /* post-dominant */
621 | READ_UNLOCK_NESTED_OUT,
622 READ_PROC_SECOND_MB) ->
624 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_SECOND_MB);
626 PROCEDURE_READ_UNLOCK(READ_UNLOCK_BASE,
627 READ_PROC_SECOND_MB /* mb() orders reads */
628 | READ_PROC_FIRST_MB /* mb() orders reads */
629 | READ_LOCK_NESTED_OUT /* RAW */
630 | READ_LOCK_OUT /* RAW */
631 | READ_UNLOCK_NESTED_OUT, /* RAW */
634 /* Unrolling loop : second consecutive lock */
635 /* reading urcu_active_readers, which have been written by
636 * READ_UNLOCK_OUT : RAW */
637 PROCEDURE_READ_LOCK(READ_LOCK_UNROLL_BASE,
638 READ_UNLOCK_OUT /* RAW */
639 | READ_PROC_SECOND_MB /* mb() orders reads */
640 | READ_PROC_FIRST_MB /* mb() orders reads */
641 | READ_LOCK_NESTED_OUT /* RAW */
642 | READ_LOCK_OUT /* RAW */
643 | READ_UNLOCK_NESTED_OUT, /* RAW */
644 READ_LOCK_OUT_UNROLL);
647 :: CONSUME_TOKENS(proc_urcu_reader,
648 READ_PROC_FIRST_MB /* mb() ordered */
649 | READ_PROC_SECOND_MB /* mb() ordered */
650 | READ_LOCK_OUT_UNROLL /* post-dominant */
651 | READ_LOCK_NESTED_OUT
653 | READ_UNLOCK_NESTED_OUT
655 READ_PROC_THIRD_MB) ->
657 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_THIRD_MB);
659 :: CONSUME_TOKENS(proc_urcu_reader,
660 READ_PROC_FIRST_MB /* mb() orders reads */
661 | READ_PROC_SECOND_MB /* mb() orders reads */
662 | READ_PROC_THIRD_MB, /* mb() orders reads */
663 READ_PROC_READ_GEN_UNROLL) ->
665 read_generation[get_readerid()] =
666 READ_CACHED_VAR(generation_ptr);
669 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_READ_GEN_UNROLL);
671 :: CONSUME_TOKENS(proc_urcu_reader,
672 READ_PROC_READ_GEN_UNROLL /* mb() orders reads */
673 | READ_PROC_FIRST_MB /* mb() ordered */
674 | READ_PROC_SECOND_MB /* mb() ordered */
675 | READ_PROC_THIRD_MB /* mb() ordered */
676 | READ_LOCK_OUT_UNROLL /* post-dominant */
677 | READ_LOCK_NESTED_OUT
679 | READ_UNLOCK_NESTED_OUT
681 READ_PROC_FOURTH_MB) ->
683 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FOURTH_MB);
685 PROCEDURE_READ_UNLOCK(READ_UNLOCK_UNROLL_BASE,
686 READ_PROC_FOURTH_MB /* mb() orders reads */
687 | READ_PROC_THIRD_MB /* mb() orders reads */
688 | READ_LOCK_OUT_UNROLL /* RAW */
689 | READ_PROC_SECOND_MB /* mb() orders reads */
690 | READ_PROC_FIRST_MB /* mb() orders reads */
691 | READ_LOCK_NESTED_OUT /* RAW */
692 | READ_LOCK_OUT /* RAW */
693 | READ_UNLOCK_NESTED_OUT, /* RAW */
694 READ_UNLOCK_OUT_UNROLL);
695 :: CONSUME_TOKENS(proc_urcu_reader, READ_PROC_ALL_TOKENS, 0) ->
696 CLEAR_TOKENS(proc_urcu_reader, READ_PROC_ALL_TOKENS_CLEAR);
702 * Dependency between consecutive loops :
704 * WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp2 - 1)
705 * tmp = READ_CACHED_VAR(urcu_active_readers[get_readerid()]);
707 * _WHEN THE MB()s are in place_, they add full ordering of the
708 * generation pointer read wrt active reader count read, which ensures
709 * execution will not spill across loop execution.
710 * However, in the event mb()s are removed (execution using signal
711 * handler to promote barrier()() -> smp_mb()), nothing prevents one loop
712 * to spill its execution on other loop's execution.
716 data_access[get_readerid()] = 1;
717 data_access[get_readerid()] = 0;
720 data_access[get_readerid()] = 1;
721 data_access[get_readerid()] = 0;
722 goto non_atomic2_end;
729 active proctype urcu_reader()
736 assert(get_pid() < NR_PROCS);
742 * We do not test reader's progress here, because we are mainly
743 * interested in writer's progress. The reader never blocks
744 * anyway. We have to test for reader/writer's progress
745 * separately, otherwise we could think the writer is doing
746 * progress when it's blocked by an always progressing reader.
748 #ifdef READER_PROGRESS
751 urcu_one_read(i, j, nest_i, tmp, tmp2);
755 /* no name clash please */
756 #undef proc_urcu_reader
759 /* Model the RCU update process. */
762 * Bit encoding, urcu_writer :
763 * Currently only supports one reader.
766 int _proc_urcu_writer;
767 #define proc_urcu_writer _proc_urcu_writer
769 #define WRITE_PROD_NONE (1 << 0)
771 #define WRITE_PROC_FIRST_MB (1 << 1)
774 #define WRITE_PROC_FIRST_READ_GP (1 << 2)
775 #define WRITE_PROC_FIRST_WRITE_GP (1 << 3)
776 #define WRITE_PROC_FIRST_WAIT (1 << 4)
777 #define WRITE_PROC_FIRST_WAIT_LOOP (1 << 5)
780 #define WRITE_PROC_SECOND_READ_GP (1 << 6)
781 #define WRITE_PROC_SECOND_WRITE_GP (1 << 7)
782 #define WRITE_PROC_SECOND_WAIT (1 << 8)
783 #define WRITE_PROC_SECOND_WAIT_LOOP (1 << 9)
785 #define WRITE_PROC_SECOND_MB (1 << 10)
787 #define WRITE_PROC_ALL_TOKENS (WRITE_PROD_NONE \
788 | WRITE_PROC_FIRST_MB \
789 | WRITE_PROC_FIRST_READ_GP \
790 | WRITE_PROC_FIRST_WRITE_GP \
791 | WRITE_PROC_FIRST_WAIT \
792 | WRITE_PROC_SECOND_READ_GP \
793 | WRITE_PROC_SECOND_WRITE_GP \
794 | WRITE_PROC_SECOND_WAIT \
795 | WRITE_PROC_SECOND_MB)
797 #define WRITE_PROC_ALL_TOKENS_CLEAR ((1 << 11) - 1)
799 active proctype urcu_writer()
802 byte tmp, tmp2, tmpa;
807 assert(get_pid() < NR_PROCS);
810 :: (READ_CACHED_VAR(generation_ptr) < 5) ->
811 #ifdef WRITER_PROGRESS
816 old_gen = READ_CACHED_VAR(generation_ptr);
817 WRITE_CACHED_VAR(generation_ptr, old_gen + 1);
825 :: write_lock == 0 ->
834 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROD_NONE);
837 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_MB);
838 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_MB);
842 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_READ_GP);
843 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WRITE_GP);
844 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT);
848 :: CONSUME_TOKENS(proc_urcu_writer,
850 WRITE_PROC_FIRST_MB) ->
852 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_MB);
855 :: CONSUME_TOKENS(proc_urcu_writer,
857 WRITE_PROC_FIRST_READ_GP) ->
858 tmpa = READ_CACHED_VAR(urcu_gp_ctr);
859 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_READ_GP);
860 :: CONSUME_TOKENS(proc_urcu_writer,
861 WRITE_PROC_FIRST_MB | WRITE_PROC_FIRST_READ_GP,
862 WRITE_PROC_FIRST_WRITE_GP) ->
864 WRITE_CACHED_VAR(urcu_gp_ctr, tmpa ^ RCU_GP_CTR_BIT);
865 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WRITE_GP);
867 :: CONSUME_TOKENS(proc_urcu_writer,
868 //WRITE_PROC_FIRST_WRITE_GP /* TEST ADDING SYNC CORE */
869 WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
870 WRITE_PROC_FIRST_WAIT | WRITE_PROC_FIRST_WAIT_LOOP) ->
872 /* ONLY WAITING FOR READER 0 */
873 tmp2 = READ_CACHED_VAR(urcu_active_readers[0]);
875 :: (tmp2 & RCU_GP_CTR_NEST_MASK)
876 && ((tmp2 ^ RCU_GP_CTR_BIT) & RCU_GP_CTR_BIT) ->
877 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WAIT_LOOP);
879 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WAIT);
882 :: CONSUME_TOKENS(proc_urcu_writer,
883 //WRITE_PROC_FIRST_WRITE_GP /* TEST ADDING SYNC CORE */
884 WRITE_PROC_FIRST_WRITE_GP
885 | WRITE_PROC_FIRST_READ_GP
886 | WRITE_PROC_FIRST_WAIT_LOOP
887 | WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
889 #ifndef GEN_ERROR_WRITER_PROGRESS
894 /* This instruction loops to WRITE_PROC_FIRST_WAIT */
895 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WAIT_LOOP | WRITE_PROC_FIRST_WAIT);
898 :: CONSUME_TOKENS(proc_urcu_writer,
899 WRITE_PROC_FIRST_WAIT /* Control dependency : need to branch out of
900 * the loop to execute the next flip (CHECK) */
901 | WRITE_PROC_FIRST_WRITE_GP
902 | WRITE_PROC_FIRST_READ_GP
903 | WRITE_PROC_FIRST_MB,
904 WRITE_PROC_SECOND_READ_GP) ->
905 //smp_mb_send(i, j); //TEST
907 tmpa = READ_CACHED_VAR(urcu_gp_ctr);
908 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_READ_GP);
909 :: CONSUME_TOKENS(proc_urcu_writer,
911 | WRITE_PROC_FIRST_READ_GP
912 | WRITE_PROC_FIRST_WRITE_GP
913 | WRITE_PROC_SECOND_READ_GP,
914 WRITE_PROC_SECOND_WRITE_GP) ->
916 WRITE_CACHED_VAR(urcu_gp_ctr, tmpa ^ RCU_GP_CTR_BIT);
917 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WRITE_GP);
919 :: CONSUME_TOKENS(proc_urcu_writer,
920 //WRITE_PROC_FIRST_WRITE_GP /* TEST ADDING SYNC CORE */
921 WRITE_PROC_FIRST_WAIT
922 | WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
923 WRITE_PROC_SECOND_WAIT | WRITE_PROC_SECOND_WAIT_LOOP) ->
925 /* ONLY WAITING FOR READER 0 */
926 tmp2 = READ_CACHED_VAR(urcu_active_readers[0]);
928 :: (tmp2 & RCU_GP_CTR_NEST_MASK)
929 && ((tmp2 ^ 0) & RCU_GP_CTR_BIT) ->
930 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT_LOOP);
932 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT);
935 :: CONSUME_TOKENS(proc_urcu_writer,
936 //WRITE_PROC_FIRST_WRITE_GP /* TEST ADDING SYNC CORE */
937 WRITE_PROC_SECOND_WRITE_GP
938 | WRITE_PROC_FIRST_WRITE_GP
939 | WRITE_PROC_SECOND_READ_GP
940 | WRITE_PROC_FIRST_READ_GP
941 | WRITE_PROC_SECOND_WAIT_LOOP
942 | WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
944 #ifndef GEN_ERROR_WRITER_PROGRESS
949 /* This instruction loops to WRITE_PROC_SECOND_WAIT */
950 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT_LOOP | WRITE_PROC_SECOND_WAIT);
953 :: CONSUME_TOKENS(proc_urcu_writer,
954 WRITE_PROC_FIRST_WAIT
955 | WRITE_PROC_SECOND_WAIT
956 | WRITE_PROC_FIRST_READ_GP
957 | WRITE_PROC_SECOND_READ_GP
958 | WRITE_PROC_FIRST_WRITE_GP
959 | WRITE_PROC_SECOND_WRITE_GP
960 | WRITE_PROC_FIRST_MB,
961 WRITE_PROC_SECOND_MB) ->
963 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_MB);
965 :: CONSUME_TOKENS(proc_urcu_writer, WRITE_PROC_ALL_TOKENS, 0) ->
966 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_ALL_TOKENS_CLEAR);
971 /* free-up step, e.g., kfree(). */
973 last_free_gen = old_gen;
979 * Given the reader loops infinitely, let the writer also busy-loop
980 * with progress here so, with weak fairness, we can test the
986 #ifdef WRITER_PROGRESS
993 /* no name clash please */
994 #undef proc_urcu_writer
997 /* Leave after the readers and writers so the pid count is ok. */
1002 INIT_CACHED_VAR(urcu_gp_ctr, 1, j);
1003 INIT_CACHED_VAR(generation_ptr, 0, j);
1007 :: i < NR_READERS ->
1008 INIT_CACHED_VAR(urcu_active_readers[i], 0, j);
1009 read_generation[i] = 1;
1012 :: i >= NR_READERS -> break