2 * mem.spin: Promela code to validate memory barriers with OOO memory
3 * and out-of-order instruction scheduling.
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.
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.
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.
19 * Copyright (c) 2009 Mathieu Desnoyers
22 /* Promela validation variables. */
24 /* specific defines "included" here */
25 /* DEFINES file "included" here */
32 #define get_pid() (_pid)
34 #define get_readerid() (get_pid())
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
45 #define CONSUME_TOKENS(state, bits, notbits) \
46 ((!(state & (notbits))) && (state & (bits)) == (bits))
48 #define PRODUCE_TOKENS(state, bits) \
49 state = state | (bits);
51 #define CLEAR_TOKENS(state, bits) \
52 state = state & ~(bits)
55 * Types of dependency :
59 * - True dependency, Read-after-Write (RAW)
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.
65 * - False dependency, Write-after-Read (WAR)
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.
72 * - Output dependency, Write-after-Write (WAW)
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.
80 * Execution of a given instruction depends on a previous instruction evaluating
81 * in a way that allows its execution. E.g. : branches.
83 * Useful considerations for joining dependencies after branch
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
92 * http://www.hipersoft.rice.edu/grads/publications/dom14.pdf
93 * Other classic algorithm to calculate dominance : Lengauer-Tarjan (in gcc)
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.
101 * Other considerations
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.
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
115 * Muchnick, Steven S. (1997). Advanced Compiler Design and Implementation.
116 * Morgan Kaufmann. ISBN 1-55860-320-4.
120 * Note about loops and nested calls
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.
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
135 typedef per_proc_byte {
139 /* Bitfield has a maximum of 8 procs */
140 typedef per_proc_bit {
144 #define DECLARE_CACHED_VAR(type, x) \
146 per_proc_##type cached_##x; \
147 per_proc_bit cache_dirty_##x;
149 #define INIT_CACHED_VAR(x, v, j) \
151 cache_dirty_##x.bitfield = 0; \
155 cached_##x.val[j] = v; \
157 :: j >= NR_PROCS -> break \
160 #define IS_CACHE_DIRTY(x, id) (cache_dirty_##x.bitfield & (1 << id))
162 #define READ_CACHED_VAR(x) (cached_##x.val[get_pid()])
164 #define WRITE_CACHED_VAR(x, v) \
166 cached_##x.val[get_pid()] = v; \
167 cache_dirty_##x.bitfield = \
168 cache_dirty_##x.bitfield | (1 << get_pid()); \
171 #define CACHE_WRITE_TO_MEM(x, id) \
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)); \
181 #define CACHE_READ_FROM_MEM(x, id) \
183 :: !IS_CACHE_DIRTY(x, id) -> \
184 cached_##x.val[id] = mem_##x;\
190 * May update other caches if cache is dirty, or not.
192 #define RANDOM_CACHE_WRITE_TO_MEM(x, id)\
194 :: 1 -> CACHE_WRITE_TO_MEM(x, id); \
198 #define RANDOM_CACHE_READ_FROM_MEM(x, id)\
200 :: 1 -> CACHE_READ_FROM_MEM(x, id); \
204 /* Must consume all prior read tokens. All subsequent reads depend on it. */
208 CACHE_READ_FROM_MEM(urcu_gp_ctr, get_pid());
212 CACHE_READ_FROM_MEM(urcu_active_readers[i], get_pid());
214 :: i >= NR_READERS -> break
216 CACHE_READ_FROM_MEM(generation_ptr, get_pid());
220 /* Must consume all prior write tokens. All subsequent writes depend on it. */
224 CACHE_WRITE_TO_MEM(urcu_gp_ctr, get_pid());
228 CACHE_WRITE_TO_MEM(urcu_active_readers[i], get_pid());
230 :: i >= NR_READERS -> break
232 CACHE_WRITE_TO_MEM(generation_ptr, get_pid());
236 /* Synchronization point. Must consume all prior read and write tokens. All
237 * subsequent reads and writes depend on it. */
247 #ifdef REMOTE_BARRIERS
249 bit reader_barrier[NR_READERS];
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
261 #define smp_mb_reader(i, j)
264 * Service 0, 1 or many barrier requests.
266 inline smp_mb_recv(i, j)
269 :: (reader_barrier[get_readerid()] == 1) ->
271 reader_barrier[get_readerid()] = 0;
277 inline smp_mb_send(i, j)
283 reader_barrier[i] = 1;
285 :: (reader_barrier[i] == 1) -> skip;
286 :: (reader_barrier[i] == 0) -> break;
289 :: i >= NR_READERS ->
297 #define smp_mb_send smp_mb
298 #define smp_mb_reader smp_mb
299 #define smp_mb_recv(i, j)
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);
310 byte last_free_gen = 0;
312 byte read_generation[NR_READERS];
313 bit data_access[NR_READERS];
319 bit sighand_exec = 0;
321 inline wait_init_done()
324 :: init_done == 0 -> skip;
332 RANDOM_CACHE_WRITE_TO_MEM(urcu_gp_ctr, get_pid());
336 RANDOM_CACHE_WRITE_TO_MEM(urcu_active_readers[i],
339 :: i >= NR_READERS -> break
341 RANDOM_CACHE_WRITE_TO_MEM(generation_ptr, get_pid());
342 RANDOM_CACHE_READ_FROM_MEM(urcu_gp_ctr, get_pid());
346 RANDOM_CACHE_READ_FROM_MEM(urcu_active_readers[i],
349 :: i >= NR_READERS -> break
351 RANDOM_CACHE_READ_FROM_MEM(generation_ptr, get_pid());
356 * Bit encoding, urcu_reader :
359 int _proc_urcu_reader;
360 #define proc_urcu_reader _proc_urcu_reader
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)
368 #define PROCEDURE_READ_LOCK(base, consumetoken, producetoken) \
369 :: CONSUME_TOKENS(proc_urcu_reader, consumetoken, READ_PROD_A_READ << base) -> \
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) -> \
377 :: (!(tmp & RCU_GP_CTR_NEST_MASK)) -> \
378 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_B_IF_TRUE << base); \
380 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_B_IF_FALSE << base); \
383 :: CONSUME_TOKENS(proc_urcu_reader, READ_PROD_B_IF_TRUE << base, \
384 READ_PROD_C_IF_TRUE_READ << base) -> \
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 */ \
393 WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp2); \
394 PRODUCE_TOKENS(proc_urcu_reader, producetoken); \
395 /* IF_MERGE implies \
396 * post-dominance */ \
398 :: CONSUME_TOKENS(proc_urcu_reader, \
399 (READ_PROD_B_IF_FALSE /* pre-dominant */ \
400 | READ_PROD_A_READ) << base, /* WAR */ \
403 WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], \
405 PRODUCE_TOKENS(proc_urcu_reader, producetoken); \
406 /* IF_MERGE implies \
407 * post-dominance */ \
411 /* Body of PROCEDURE_READ_LOCK */
412 #define READ_PROC_READ_UNLOCK (1 << 0)
414 #define PROCEDURE_READ_UNLOCK(base, consumetoken, producetoken) \
415 :: CONSUME_TOKENS(proc_urcu_reader, \
417 READ_PROC_READ_UNLOCK << base) -> \
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, \
423 | (READ_PROC_READ_UNLOCK << base), /* WAR */ \
426 WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp2 - 1); \
427 PRODUCE_TOKENS(proc_urcu_reader, producetoken); \
431 #define READ_PROD_NONE (1 << 0)
433 /* PROCEDURE_READ_LOCK base = << 1 : 1 to 5 */
434 #define READ_LOCK_BASE 1
435 #define READ_LOCK_OUT (1 << 5)
437 #define READ_PROC_FIRST_MB (1 << 6)
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)
443 #define READ_PROC_READ_GEN (1 << 12)
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)
449 #define READ_PROC_SECOND_MB (1 << 15)
451 /* PROCEDURE_READ_UNLOCK base = << 16 : 16 to 17 */
452 #define READ_UNLOCK_BASE 16
453 #define READ_UNLOCK_OUT (1 << 17)
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)
459 #define READ_PROC_THIRD_MB (1 << 23)
461 #define READ_PROC_READ_GEN_UNROLL (1 << 24)
463 #define READ_PROC_FOURTH_MB (1 << 25)
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)
470 /* Should not include branches */
471 #define READ_PROC_ALL_TOKENS (READ_PROD_NONE \
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 \
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)
485 /* Must clear all tokens, including branches */
486 #define READ_PROC_ALL_TOKENS_CLEAR ((1 << 28) - 1)
488 inline urcu_one_read(i, j, nest_i, tmp, tmp2)
490 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_NONE);
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);
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);
509 #ifdef REMOTE_BARRIERS
511 * Signal-based memory barrier will only execute when the
512 * execution order appears in program order.
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
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,
526 | READ_PROC_READ_GEN | READ_UNLOCK_NESTED_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
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
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,
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
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,
573 goto non_atomic3_skip;
576 goto non_atomic3_end;
579 #endif /* REMOTE_BARRIERS */
583 PROCEDURE_READ_LOCK(READ_LOCK_BASE, READ_PROD_NONE, READ_LOCK_OUT);
585 :: CONSUME_TOKENS(proc_urcu_reader,
586 READ_LOCK_OUT, /* post-dominant */
587 READ_PROC_FIRST_MB) ->
589 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FIRST_MB);
591 PROCEDURE_READ_LOCK(READ_LOCK_NESTED_BASE, READ_PROC_FIRST_MB | READ_LOCK_OUT,
592 READ_LOCK_NESTED_OUT);
594 :: CONSUME_TOKENS(proc_urcu_reader,
595 READ_PROC_FIRST_MB, /* mb() orders reads */
596 READ_PROC_READ_GEN) ->
598 read_generation[get_readerid()] =
599 READ_CACHED_VAR(generation_ptr);
602 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_READ_GEN);
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
610 PROCEDURE_READ_UNLOCK(READ_UNLOCK_NESTED_BASE,
613 | READ_LOCK_NESTED_OUT,
614 READ_UNLOCK_NESTED_OUT);
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) ->
625 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_SECOND_MB);
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 */
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);
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
654 | READ_UNLOCK_NESTED_OUT
656 READ_PROC_THIRD_MB) ->
658 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_THIRD_MB);
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) ->
666 read_generation[get_readerid()] =
667 READ_CACHED_VAR(generation_ptr);
670 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_READ_GEN_UNROLL);
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
680 | READ_UNLOCK_NESTED_OUT
682 READ_PROC_FOURTH_MB) ->
684 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FOURTH_MB);
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);
703 * Dependency between consecutive loops :
705 * WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp2 - 1)
706 * tmp = READ_CACHED_VAR(urcu_active_readers[get_readerid()]);
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.
717 data_access[get_readerid()] = 1;
718 data_access[get_readerid()] = 0;
721 data_access[get_readerid()] = 1;
722 data_access[get_readerid()] = 0;
723 goto non_atomic2_end;
730 active proctype urcu_reader()
737 assert(get_pid() < NR_PROCS);
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.
749 #ifdef READER_PROGRESS
752 urcu_one_read(i, j, nest_i, tmp, tmp2);
756 /* no name clash please */
757 #undef proc_urcu_reader
760 /* Model the RCU update process. */
763 * Bit encoding, urcu_writer :
764 * Currently only supports one reader.
767 int _proc_urcu_writer;
768 #define proc_urcu_writer _proc_urcu_writer
770 #define WRITE_PROD_NONE (1 << 0)
772 #define WRITE_PROC_FIRST_MB (1 << 1)
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)
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)
786 #define WRITE_PROC_SECOND_MB (1 << 10)
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)
798 #define WRITE_PROC_ALL_TOKENS_CLEAR ((1 << 11) - 1)
800 active proctype urcu_writer()
803 byte tmp, tmp2, tmpa;
808 assert(get_pid() < NR_PROCS);
811 :: (READ_CACHED_VAR(generation_ptr) < 5) ->
812 #ifdef WRITER_PROGRESS
817 old_gen = READ_CACHED_VAR(generation_ptr);
818 WRITE_CACHED_VAR(generation_ptr, old_gen + 1);
826 :: write_lock == 0 ->
835 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROD_NONE);
838 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_MB);
839 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_MB);
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);
849 :: CONSUME_TOKENS(proc_urcu_writer,
851 WRITE_PROC_FIRST_MB) ->
853 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_MB);
856 :: CONSUME_TOKENS(proc_urcu_writer,
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) ->
865 WRITE_CACHED_VAR(urcu_gp_ctr, tmpa ^ RCU_GP_CTR_BIT);
866 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WRITE_GP);
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) ->
873 /* ONLY WAITING FOR READER 0 */
874 tmp2 = READ_CACHED_VAR(urcu_active_readers[0]);
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);
880 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WAIT);
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 */
890 #ifndef GEN_ERROR_WRITER_PROGRESS
895 /* This instruction loops to WRITE_PROC_FIRST_WAIT */
896 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WAIT_LOOP | WRITE_PROC_FIRST_WAIT);
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
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,
912 | WRITE_PROC_FIRST_READ_GP
913 | WRITE_PROC_FIRST_WRITE_GP
914 | WRITE_PROC_SECOND_READ_GP,
915 WRITE_PROC_SECOND_WRITE_GP) ->
917 WRITE_CACHED_VAR(urcu_gp_ctr, tmpa ^ RCU_GP_CTR_BIT);
918 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WRITE_GP);
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) ->
926 /* ONLY WAITING FOR READER 0 */
927 tmp2 = READ_CACHED_VAR(urcu_active_readers[0]);
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);
933 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT);
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 */
945 #ifndef GEN_ERROR_WRITER_PROGRESS
950 /* This instruction loops to WRITE_PROC_SECOND_WAIT */
951 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT_LOOP | WRITE_PROC_SECOND_WAIT);
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) ->
964 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_MB);
966 :: CONSUME_TOKENS(proc_urcu_writer, WRITE_PROC_ALL_TOKENS, 0) ->
967 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_ALL_TOKENS_CLEAR);
972 /* free-up step, e.g., kfree(). */
974 last_free_gen = old_gen;
980 * Given the reader loops infinitely, let the writer also busy-loop
981 * with progress here so, with weak fairness, we can test the
987 #ifdef WRITER_PROGRESS
994 /* no name clash please */
995 #undef proc_urcu_writer
998 /* Leave after the readers and writers so the pid count is ok. */
1003 INIT_CACHED_VAR(urcu_gp_ctr, 1, j);
1004 INIT_CACHED_VAR(generation_ptr, 0, j);
1008 :: i < NR_READERS ->
1009 INIT_CACHED_VAR(urcu_active_readers[i], 0, j);
1010 read_generation[i] = 1;
1013 :: i >= NR_READERS -> break