1 #define READER_PROGRESS
3 // Poison value for freed memory
5 // Memory with correct data
9 #define read_poison (data_read_first[0] == POISON || data_read_second[0] == POISON)
11 #define RCU_GP_CTR_BIT (1 << 7)
12 #define RCU_GP_CTR_NEST_MASK (RCU_GP_CTR_BIT - 1)
15 //#define REMOTE_BARRIERS
19 //#define ARCH_POWERPC
21 * mem.spin: Promela code to validate memory barriers with OOO memory
22 * and out-of-order instruction scheduling.
24 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation; either version 2 of the License, or
27 * (at your option) any later version.
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with this program; if not, write to the Free Software
36 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
38 * Copyright (c) 2009 Mathieu Desnoyers
41 /* Promela validation variables. */
43 /* specific defines "included" here */
44 /* DEFINES file "included" here */
51 #define get_pid() (_pid)
53 #define get_readerid() (get_pid())
56 * Produced process control and data flow. Updated after each instruction to
57 * show which variables are ready. Using one-hot bit encoding per variable to
58 * save state space. Used as triggers to execute the instructions having those
59 * variables as input. Leaving bits active to inhibit instruction execution.
60 * Scheme used to make instruction disabling and automatic dependency fall-back
64 #define CONSUME_TOKENS(state, bits, notbits) \
65 ((!(state & (notbits))) && (state & (bits)) == (bits))
67 #define PRODUCE_TOKENS(state, bits) \
68 state = state | (bits);
70 #define CLEAR_TOKENS(state, bits) \
71 state = state & ~(bits)
74 * Types of dependency :
78 * - True dependency, Read-after-Write (RAW)
80 * This type of dependency happens when a statement depends on the result of a
81 * previous statement. This applies to any statement which needs to read a
82 * variable written by a preceding statement.
84 * - False dependency, Write-after-Read (WAR)
86 * Typically, variable renaming can ensure that this dependency goes away.
87 * However, if the statements must read and then write from/to the same variable
88 * in the OOO memory model, renaming may be impossible, and therefore this
89 * causes a WAR dependency.
91 * - Output dependency, Write-after-Write (WAW)
93 * Two writes to the same variable in subsequent statements. Variable renaming
94 * can ensure this is not needed, but can be required when writing multiple
95 * times to the same OOO mem model variable.
99 * Execution of a given instruction depends on a previous instruction evaluating
100 * in a way that allows its execution. E.g. : branches.
102 * Useful considerations for joining dependencies after branch
106 * "We say box i dominates box j if every path (leading from input to output
107 * through the diagram) which passes through box j must also pass through box
108 * i. Thus box i dominates box j if box j is subordinate to box i in the
111 * http://www.hipersoft.rice.edu/grads/publications/dom14.pdf
112 * Other classic algorithm to calculate dominance : Lengauer-Tarjan (in gcc)
116 * Just as pre-dominance, but with arcs of the data flow inverted, and input vs
117 * output exchanged. Therefore, i post-dominating j ensures that every path
118 * passing by j will pass by i before reaching the output.
120 * Prefetch and speculative execution
122 * If an instruction depends on the result of a previous branch, but it does not
123 * have side-effects, it can be executed before the branch result is known.
124 * however, it must be restarted if a core-synchronizing instruction is issued.
125 * Note that instructions which depend on the speculative instruction result
126 * but that have side-effects must depend on the branch completion in addition
127 * to the speculatively executed instruction.
129 * Other considerations
131 * Note about "volatile" keyword dependency : The compiler will order volatile
132 * accesses so they appear in the right order on a given CPU. They can be
133 * reordered by the CPU instruction scheduling. This therefore cannot be
134 * considered as a depencency.
138 * Cooper, Keith D.; & Torczon, Linda. (2005). Engineering a Compiler. Morgan
139 * Kaufmann. ISBN 1-55860-698-X.
140 * Kennedy, Ken; & Allen, Randy. (2001). Optimizing Compilers for Modern
141 * Architectures: A Dependence-based Approach. Morgan Kaufmann. ISBN
143 * Muchnick, Steven S. (1997). Advanced Compiler Design and Implementation.
144 * Morgan Kaufmann. ISBN 1-55860-320-4.
148 * Note about loops and nested calls
150 * To keep this model simple, loops expressed in the framework will behave as if
151 * there was a core synchronizing instruction between loops. To see the effect
152 * of loop unrolling, manually unrolling loops is required. Note that if loops
153 * end or start with a core synchronizing instruction, the model is appropriate.
154 * Nested calls are not supported.
158 * Only Alpha has out-of-order cache bank loads. Other architectures (intel,
159 * powerpc, arm) ensure that dependent reads won't be reordered. c.f.
160 * http://www.linuxjournal.com/article/8212)
163 #define HAVE_OOO_CACHE_READ
167 * Each process have its own data in cache. Caches are randomly updated.
168 * smp_wmb and smp_rmb forces cache updates (write and read), smp_mb forces
172 typedef per_proc_byte {
176 typedef per_proc_bit {
180 /* Bitfield has a maximum of 8 procs */
181 typedef per_proc_bitfield {
185 #define DECLARE_CACHED_VAR(type, x) \
187 per_proc_##type cached_##x; \
188 per_proc_bitfield cache_dirty_##x;
190 #define INIT_CACHED_VAR(x, v, j) \
192 cache_dirty_##x.bitfield = 0; \
196 cached_##x.val[j] = v; \
198 :: j >= NR_PROCS -> break \
201 #define IS_CACHE_DIRTY(x, id) (cache_dirty_##x.bitfield & (1 << id))
203 #define READ_CACHED_VAR(x) (cached_##x.val[get_pid()])
205 #define WRITE_CACHED_VAR(x, v) \
207 cached_##x.val[get_pid()] = v; \
208 cache_dirty_##x.bitfield = \
209 cache_dirty_##x.bitfield | (1 << get_pid()); \
212 #define CACHE_WRITE_TO_MEM(x, id) \
214 :: IS_CACHE_DIRTY(x, id) -> \
215 mem_##x = cached_##x.val[id]; \
216 cache_dirty_##x.bitfield = \
217 cache_dirty_##x.bitfield & (~(1 << id)); \
222 #define CACHE_READ_FROM_MEM(x, id) \
224 :: !IS_CACHE_DIRTY(x, id) -> \
225 cached_##x.val[id] = mem_##x;\
231 * May update other caches if cache is dirty, or not.
233 #define RANDOM_CACHE_WRITE_TO_MEM(x, id)\
235 :: 1 -> CACHE_WRITE_TO_MEM(x, id); \
239 #define RANDOM_CACHE_READ_FROM_MEM(x, id)\
241 :: 1 -> CACHE_READ_FROM_MEM(x, id); \
245 /* Must consume all prior read tokens. All subsequent reads depend on it. */
249 CACHE_READ_FROM_MEM(urcu_gp_ctr, get_pid());
253 CACHE_READ_FROM_MEM(urcu_active_readers[i], get_pid());
255 :: i >= NR_READERS -> break
257 CACHE_READ_FROM_MEM(rcu_ptr, get_pid());
261 CACHE_READ_FROM_MEM(rcu_data[i], get_pid());
263 :: i >= SLAB_SIZE -> break
268 /* Must consume all prior write tokens. All subsequent writes depend on it. */
272 CACHE_WRITE_TO_MEM(urcu_gp_ctr, get_pid());
276 CACHE_WRITE_TO_MEM(urcu_active_readers[i], get_pid());
278 :: i >= NR_READERS -> break
280 CACHE_WRITE_TO_MEM(rcu_ptr, get_pid());
284 CACHE_WRITE_TO_MEM(rcu_data[i], get_pid());
286 :: i >= SLAB_SIZE -> break
291 /* Synchronization point. Must consume all prior read and write tokens. All
292 * subsequent reads and writes depend on it. */
301 #ifdef REMOTE_BARRIERS
303 bit reader_barrier[NR_READERS];
306 * We cannot leave the barriers dependencies in place in REMOTE_BARRIERS mode
307 * because they would add unexisting core synchronization and would therefore
308 * create an incomplete model.
309 * Therefore, we model the read-side memory barriers by completely disabling the
310 * memory barriers and their dependencies from the read-side. One at a time
311 * (different verification runs), we make a different instruction listen for
315 #define smp_mb_reader(i, j)
318 * Service 0, 1 or many barrier requests.
320 inline smp_mb_recv(i, j)
323 :: (reader_barrier[get_readerid()] == 1) ->
325 * We choose to ignore cycles caused by writer busy-looping,
326 * waiting for the reader, sending barrier requests, and the
327 * reader always services them without continuing execution.
329 progress_ignoring_mb1:
331 reader_barrier[get_readerid()] = 0;
334 * We choose to ignore writer's non-progress caused by the
335 * reader ignoring the writer's mb() requests.
337 progress_ignoring_mb2:
342 #define PROGRESS_LABEL(progressid) progress_writer_progid_##progressid:
344 #define smp_mb_send(i, j, progressid) \
349 :: i < NR_READERS -> \
350 reader_barrier[i] = 1; \
352 * Busy-looping waiting for reader barrier handling is of little\
353 * interest, given the reader has the ability to totally ignore \
354 * barrier requests. \
357 :: (reader_barrier[i] == 1) -> \
358 PROGRESS_LABEL(progressid) \
360 :: (reader_barrier[i] == 0) -> break; \
363 :: i >= NR_READERS -> \
371 #define smp_mb_send(i, j, progressid) smp_mb(i)
372 #define smp_mb_reader(i, j) smp_mb(i)
373 #define smp_mb_recv(i, j)
377 /* Keep in sync manually with smp_rmb, smp_wmb, ooo_mem and init() */
378 DECLARE_CACHED_VAR(byte, urcu_gp_ctr);
379 /* Note ! currently only one reader */
380 DECLARE_CACHED_VAR(byte, urcu_active_readers[NR_READERS]);
382 DECLARE_CACHED_VAR(bit, rcu_data[SLAB_SIZE]);
386 DECLARE_CACHED_VAR(bit, rcu_ptr);
387 bit ptr_read_first[NR_READERS];
388 bit ptr_read_second[NR_READERS];
390 DECLARE_CACHED_VAR(byte, rcu_ptr);
391 byte ptr_read_first[NR_READERS];
392 byte ptr_read_second[NR_READERS];
395 bit data_read_first[NR_READERS];
396 bit data_read_second[NR_READERS];
400 inline wait_init_done()
403 :: init_done == 0 -> skip;
411 RANDOM_CACHE_WRITE_TO_MEM(urcu_gp_ctr, get_pid());
415 RANDOM_CACHE_WRITE_TO_MEM(urcu_active_readers[i],
418 :: i >= NR_READERS -> break
420 RANDOM_CACHE_WRITE_TO_MEM(rcu_ptr, get_pid());
424 RANDOM_CACHE_WRITE_TO_MEM(rcu_data[i], get_pid());
426 :: i >= SLAB_SIZE -> break
428 #ifdef HAVE_OOO_CACHE_READ
429 RANDOM_CACHE_READ_FROM_MEM(urcu_gp_ctr, get_pid());
433 RANDOM_CACHE_READ_FROM_MEM(urcu_active_readers[i],
436 :: i >= NR_READERS -> break
438 RANDOM_CACHE_READ_FROM_MEM(rcu_ptr, get_pid());
442 RANDOM_CACHE_READ_FROM_MEM(rcu_data[i], get_pid());
444 :: i >= SLAB_SIZE -> break
448 #endif /* HAVE_OOO_CACHE_READ */
453 * Bit encoding, urcu_reader :
456 int _proc_urcu_reader;
457 #define proc_urcu_reader _proc_urcu_reader
459 /* Body of PROCEDURE_READ_LOCK */
460 #define READ_PROD_A_READ (1 << 0)
461 #define READ_PROD_B_IF_TRUE (1 << 1)
462 #define READ_PROD_B_IF_FALSE (1 << 2)
463 #define READ_PROD_C_IF_TRUE_READ (1 << 3)
465 #define PROCEDURE_READ_LOCK(base, consumetoken, consumetoken2, producetoken) \
466 :: CONSUME_TOKENS(proc_urcu_reader, (consumetoken | consumetoken2), READ_PROD_A_READ << base) -> \
468 tmp = READ_CACHED_VAR(urcu_active_readers[get_readerid()]); \
469 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_A_READ << base); \
470 :: CONSUME_TOKENS(proc_urcu_reader, \
471 READ_PROD_A_READ << base, /* RAW, pre-dominant */ \
472 (READ_PROD_B_IF_TRUE | READ_PROD_B_IF_FALSE) << base) -> \
474 :: (!(tmp & RCU_GP_CTR_NEST_MASK)) -> \
475 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_B_IF_TRUE << base); \
477 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_B_IF_FALSE << base); \
480 :: CONSUME_TOKENS(proc_urcu_reader, consumetoken, /* prefetch */ \
481 READ_PROD_C_IF_TRUE_READ << base) -> \
483 tmp2 = READ_CACHED_VAR(urcu_gp_ctr); \
484 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_C_IF_TRUE_READ << base); \
485 :: CONSUME_TOKENS(proc_urcu_reader, \
486 (READ_PROD_B_IF_TRUE \
487 | READ_PROD_C_IF_TRUE_READ /* pre-dominant */ \
488 | READ_PROD_A_READ) << base, /* WAR */ \
491 WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp2); \
492 PRODUCE_TOKENS(proc_urcu_reader, producetoken); \
493 /* IF_MERGE implies \
494 * post-dominance */ \
496 :: CONSUME_TOKENS(proc_urcu_reader, \
497 (READ_PROD_B_IF_FALSE /* pre-dominant */ \
498 | READ_PROD_A_READ) << base, /* WAR */ \
501 WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], \
503 PRODUCE_TOKENS(proc_urcu_reader, producetoken); \
504 /* IF_MERGE implies \
505 * post-dominance */ \
509 /* Body of PROCEDURE_READ_LOCK */
510 #define READ_PROC_READ_UNLOCK (1 << 0)
512 #define PROCEDURE_READ_UNLOCK(base, consumetoken, producetoken) \
513 :: CONSUME_TOKENS(proc_urcu_reader, \
515 READ_PROC_READ_UNLOCK << base) -> \
517 tmp = READ_CACHED_VAR(urcu_active_readers[get_readerid()]); \
518 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_READ_UNLOCK << base); \
519 :: CONSUME_TOKENS(proc_urcu_reader, \
521 | (READ_PROC_READ_UNLOCK << base), /* WAR */ \
524 WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp - 1); \
525 PRODUCE_TOKENS(proc_urcu_reader, producetoken); \
529 #define READ_PROD_NONE (1 << 0)
531 /* PROCEDURE_READ_LOCK base = << 1 : 1 to 5 */
532 #define READ_LOCK_BASE 1
533 #define READ_LOCK_OUT (1 << 5)
535 #define READ_PROC_FIRST_MB (1 << 6)
537 /* PROCEDURE_READ_LOCK (NESTED) base : << 7 : 7 to 11 */
538 #define READ_LOCK_NESTED_BASE 7
539 #define READ_LOCK_NESTED_OUT (1 << 11)
541 #define READ_PROC_READ_GEN (1 << 12)
542 #define READ_PROC_ACCESS_GEN (1 << 13)
544 /* PROCEDURE_READ_UNLOCK (NESTED) base = << 14 : 14 to 15 */
545 #define READ_UNLOCK_NESTED_BASE 14
546 #define READ_UNLOCK_NESTED_OUT (1 << 15)
548 #define READ_PROC_SECOND_MB (1 << 16)
550 /* PROCEDURE_READ_UNLOCK base = << 17 : 17 to 18 */
551 #define READ_UNLOCK_BASE 17
552 #define READ_UNLOCK_OUT (1 << 18)
554 /* PROCEDURE_READ_LOCK_UNROLL base = << 19 : 19 to 23 */
555 #define READ_LOCK_UNROLL_BASE 19
556 #define READ_LOCK_OUT_UNROLL (1 << 23)
558 #define READ_PROC_THIRD_MB (1 << 24)
560 #define READ_PROC_READ_GEN_UNROLL (1 << 25)
561 #define READ_PROC_ACCESS_GEN_UNROLL (1 << 26)
563 #define READ_PROC_FOURTH_MB (1 << 27)
565 /* PROCEDURE_READ_UNLOCK_UNROLL base = << 28 : 28 to 29 */
566 #define READ_UNLOCK_UNROLL_BASE 28
567 #define READ_UNLOCK_OUT_UNROLL (1 << 29)
570 /* Should not include branches */
571 #define READ_PROC_ALL_TOKENS (READ_PROD_NONE \
573 | READ_PROC_FIRST_MB \
574 | READ_LOCK_NESTED_OUT \
575 | READ_PROC_READ_GEN \
576 | READ_PROC_ACCESS_GEN \
577 | READ_UNLOCK_NESTED_OUT \
578 | READ_PROC_SECOND_MB \
580 | READ_LOCK_OUT_UNROLL \
581 | READ_PROC_THIRD_MB \
582 | READ_PROC_READ_GEN_UNROLL \
583 | READ_PROC_ACCESS_GEN_UNROLL \
584 | READ_PROC_FOURTH_MB \
585 | READ_UNLOCK_OUT_UNROLL)
587 /* Must clear all tokens, including branches */
588 #define READ_PROC_ALL_TOKENS_CLEAR ((1 << 30) - 1)
590 inline urcu_one_read(i, j, nest_i, tmp, tmp2)
592 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_NONE);
595 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FIRST_MB);
596 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_SECOND_MB);
597 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_THIRD_MB);
598 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FOURTH_MB);
601 #ifdef REMOTE_BARRIERS
602 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FIRST_MB);
603 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_SECOND_MB);
604 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_THIRD_MB);
605 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FOURTH_MB);
611 #ifdef REMOTE_BARRIERS
613 * Signal-based memory barrier will only execute when the
614 * execution order appears in program order.
620 :: CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE,
621 READ_LOCK_OUT | READ_LOCK_NESTED_OUT
622 | READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
624 | READ_LOCK_OUT_UNROLL
625 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
626 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT,
628 | READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
630 | READ_LOCK_OUT_UNROLL
631 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
632 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT | READ_LOCK_NESTED_OUT,
633 READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
635 | READ_LOCK_OUT_UNROLL
636 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
637 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
638 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN,
639 READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
641 | READ_LOCK_OUT_UNROLL
642 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
643 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
644 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN,
645 READ_UNLOCK_NESTED_OUT
647 | READ_LOCK_OUT_UNROLL
648 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
649 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
650 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN
651 | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT,
653 | READ_LOCK_OUT_UNROLL
654 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
655 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
656 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN
657 | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
660 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
661 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
662 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN
663 | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
664 | READ_UNLOCK_OUT | READ_LOCK_OUT_UNROLL,
665 READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
666 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
667 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN
668 | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
669 | READ_UNLOCK_OUT | READ_LOCK_OUT_UNROLL
670 | READ_PROC_READ_GEN_UNROLL,
671 READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
672 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
673 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN
674 | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
675 | READ_UNLOCK_OUT | READ_LOCK_OUT_UNROLL
676 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL,
677 READ_UNLOCK_OUT_UNROLL)
678 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
679 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
680 | READ_UNLOCK_OUT | READ_LOCK_OUT_UNROLL
681 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL,
690 goto non_atomic3_skip;
693 goto non_atomic3_end;
696 #endif /* REMOTE_BARRIERS */
700 PROCEDURE_READ_LOCK(READ_LOCK_BASE, READ_PROD_NONE, 0, READ_LOCK_OUT);
702 :: CONSUME_TOKENS(proc_urcu_reader,
703 READ_LOCK_OUT, /* post-dominant */
704 READ_PROC_FIRST_MB) ->
706 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FIRST_MB);
708 PROCEDURE_READ_LOCK(READ_LOCK_NESTED_BASE, READ_PROC_FIRST_MB, READ_LOCK_OUT,
709 READ_LOCK_NESTED_OUT);
711 :: CONSUME_TOKENS(proc_urcu_reader,
712 READ_PROC_FIRST_MB, /* mb() orders reads */
713 READ_PROC_READ_GEN) ->
715 ptr_read_first[get_readerid()] = READ_CACHED_VAR(rcu_ptr);
716 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_READ_GEN);
718 :: CONSUME_TOKENS(proc_urcu_reader,
719 READ_PROC_FIRST_MB /* mb() orders reads */
720 | READ_PROC_READ_GEN,
721 READ_PROC_ACCESS_GEN) ->
722 /* smp_read_barrier_depends */
725 data_read_first[get_readerid()] =
726 READ_CACHED_VAR(rcu_data[ptr_read_first[get_readerid()]]);
727 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_ACCESS_GEN);
730 /* Note : we remove the nested memory barrier from the read unlock
731 * model, given it is not usually needed. The implementation has the barrier
732 * because the performance impact added by a branch in the common case does not
736 PROCEDURE_READ_UNLOCK(READ_UNLOCK_NESTED_BASE,
739 | READ_LOCK_NESTED_OUT,
740 READ_UNLOCK_NESTED_OUT);
743 :: CONSUME_TOKENS(proc_urcu_reader,
744 READ_PROC_ACCESS_GEN /* mb() orders reads */
745 | READ_PROC_READ_GEN /* mb() orders reads */
746 | READ_PROC_FIRST_MB /* mb() ordered */
747 | READ_LOCK_OUT /* post-dominant */
748 | READ_LOCK_NESTED_OUT /* post-dominant */
749 | READ_UNLOCK_NESTED_OUT,
750 READ_PROC_SECOND_MB) ->
752 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_SECOND_MB);
754 PROCEDURE_READ_UNLOCK(READ_UNLOCK_BASE,
755 READ_PROC_SECOND_MB /* mb() orders reads */
756 | READ_PROC_FIRST_MB /* mb() orders reads */
757 | READ_LOCK_NESTED_OUT /* RAW */
758 | READ_LOCK_OUT /* RAW */
759 | READ_UNLOCK_NESTED_OUT, /* RAW */
762 /* Unrolling loop : second consecutive lock */
763 /* reading urcu_active_readers, which have been written by
764 * READ_UNLOCK_OUT : RAW */
765 PROCEDURE_READ_LOCK(READ_LOCK_UNROLL_BASE,
766 READ_PROC_SECOND_MB /* mb() orders reads */
767 | READ_PROC_FIRST_MB, /* mb() orders reads */
768 READ_LOCK_NESTED_OUT /* RAW */
769 | READ_LOCK_OUT /* RAW */
770 | READ_UNLOCK_NESTED_OUT /* RAW */
771 | READ_UNLOCK_OUT, /* RAW */
772 READ_LOCK_OUT_UNROLL);
775 :: CONSUME_TOKENS(proc_urcu_reader,
776 READ_PROC_FIRST_MB /* mb() ordered */
777 | READ_PROC_SECOND_MB /* mb() ordered */
778 | READ_LOCK_OUT_UNROLL /* post-dominant */
779 | READ_LOCK_NESTED_OUT
781 | READ_UNLOCK_NESTED_OUT
783 READ_PROC_THIRD_MB) ->
785 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_THIRD_MB);
787 :: CONSUME_TOKENS(proc_urcu_reader,
788 READ_PROC_FIRST_MB /* mb() orders reads */
789 | READ_PROC_SECOND_MB /* mb() orders reads */
790 | READ_PROC_THIRD_MB, /* mb() orders reads */
791 READ_PROC_READ_GEN_UNROLL) ->
793 ptr_read_second[get_readerid()] = READ_CACHED_VAR(rcu_ptr);
794 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_READ_GEN_UNROLL);
796 :: CONSUME_TOKENS(proc_urcu_reader,
797 READ_PROC_READ_GEN_UNROLL
798 | READ_PROC_FIRST_MB /* mb() orders reads */
799 | READ_PROC_SECOND_MB /* mb() orders reads */
800 | READ_PROC_THIRD_MB, /* mb() orders reads */
801 READ_PROC_ACCESS_GEN_UNROLL) ->
802 /* smp_read_barrier_depends */
805 data_read_second[get_readerid()] =
806 READ_CACHED_VAR(rcu_data[ptr_read_second[get_readerid()]]);
807 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_ACCESS_GEN_UNROLL);
809 :: CONSUME_TOKENS(proc_urcu_reader,
810 READ_PROC_READ_GEN_UNROLL /* mb() orders reads */
811 | READ_PROC_ACCESS_GEN_UNROLL /* mb() orders reads */
812 | READ_PROC_FIRST_MB /* mb() ordered */
813 | READ_PROC_SECOND_MB /* mb() ordered */
814 | READ_PROC_THIRD_MB /* mb() ordered */
815 | READ_LOCK_OUT_UNROLL /* post-dominant */
816 | READ_LOCK_NESTED_OUT
818 | READ_UNLOCK_NESTED_OUT
820 READ_PROC_FOURTH_MB) ->
822 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FOURTH_MB);
824 PROCEDURE_READ_UNLOCK(READ_UNLOCK_UNROLL_BASE,
825 READ_PROC_FOURTH_MB /* mb() orders reads */
826 | READ_PROC_THIRD_MB /* mb() orders reads */
827 | READ_LOCK_OUT_UNROLL /* RAW */
828 | READ_PROC_SECOND_MB /* mb() orders reads */
829 | READ_PROC_FIRST_MB /* mb() orders reads */
830 | READ_LOCK_NESTED_OUT /* RAW */
831 | READ_LOCK_OUT /* RAW */
832 | READ_UNLOCK_NESTED_OUT, /* RAW */
833 READ_UNLOCK_OUT_UNROLL);
834 :: CONSUME_TOKENS(proc_urcu_reader, READ_PROC_ALL_TOKENS, 0) ->
835 CLEAR_TOKENS(proc_urcu_reader, READ_PROC_ALL_TOKENS_CLEAR);
841 * Dependency between consecutive loops :
843 * WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp2 - 1)
844 * tmp = READ_CACHED_VAR(urcu_active_readers[get_readerid()]);
846 * _WHEN THE MB()s are in place_, they add full ordering of the
847 * generation pointer read wrt active reader count read, which ensures
848 * execution will not spill across loop execution.
849 * However, in the event mb()s are removed (execution using signal
850 * handler to promote barrier()() -> smp_mb()), nothing prevents one loop
851 * to spill its execution on other loop's execution.
874 active proctype urcu_reader()
881 assert(get_pid() < NR_PROCS);
887 * We do not test reader's progress here, because we are mainly
888 * interested in writer's progress. The reader never blocks
889 * anyway. We have to test for reader/writer's progress
890 * separately, otherwise we could think the writer is doing
891 * progress when it's blocked by an always progressing reader.
893 #ifdef READER_PROGRESS
896 urcu_one_read(i, j, nest_i, tmp, tmp2);
900 /* no name clash please */
901 #undef proc_urcu_reader
904 /* Model the RCU update process. */
907 * Bit encoding, urcu_writer :
908 * Currently only supports one reader.
911 int _proc_urcu_writer;
912 #define proc_urcu_writer _proc_urcu_writer
914 #define WRITE_PROD_NONE (1 << 0)
916 #define WRITE_DATA (1 << 1)
917 #define WRITE_PROC_WMB (1 << 2)
918 #define WRITE_XCHG_PTR (1 << 3)
920 #define WRITE_PROC_FIRST_MB (1 << 4)
923 #define WRITE_PROC_FIRST_READ_GP (1 << 5)
924 #define WRITE_PROC_FIRST_WRITE_GP (1 << 6)
925 #define WRITE_PROC_FIRST_WAIT (1 << 7)
926 #define WRITE_PROC_FIRST_WAIT_LOOP (1 << 8)
929 #define WRITE_PROC_SECOND_READ_GP (1 << 9)
930 #define WRITE_PROC_SECOND_WRITE_GP (1 << 10)
931 #define WRITE_PROC_SECOND_WAIT (1 << 11)
932 #define WRITE_PROC_SECOND_WAIT_LOOP (1 << 12)
934 #define WRITE_PROC_SECOND_MB (1 << 13)
936 #define WRITE_FREE (1 << 14)
938 #define WRITE_PROC_ALL_TOKENS (WRITE_PROD_NONE \
942 | WRITE_PROC_FIRST_MB \
943 | WRITE_PROC_FIRST_READ_GP \
944 | WRITE_PROC_FIRST_WRITE_GP \
945 | WRITE_PROC_FIRST_WAIT \
946 | WRITE_PROC_SECOND_READ_GP \
947 | WRITE_PROC_SECOND_WRITE_GP \
948 | WRITE_PROC_SECOND_WAIT \
949 | WRITE_PROC_SECOND_MB \
952 #define WRITE_PROC_ALL_TOKENS_CLEAR ((1 << 15) - 1)
955 * Mutexes are implied around writer execution. A single writer at a time.
957 active proctype urcu_writer()
960 byte tmp, tmp2, tmpa;
961 byte cur_data = 0, old_data, loop_nr = 0;
962 byte cur_gp_val = 0; /*
963 * Keep a local trace of the current parity so
964 * we don't add non-existing dependencies on the global
965 * GP update. Needed to test single flip case.
970 assert(get_pid() < NR_PROCS);
974 #ifdef WRITER_PROGRESS
977 loop_nr = loop_nr + 1;
979 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROD_NONE);
982 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_WMB);
986 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_MB);
987 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_MB);
991 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_READ_GP);
992 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WRITE_GP);
993 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT);
994 /* For single flip, we need to know the current parity */
995 cur_gp_val = cur_gp_val ^ RCU_GP_CTR_BIT;
1002 :: CONSUME_TOKENS(proc_urcu_writer,
1006 cur_data = (cur_data + 1) % SLAB_SIZE;
1007 WRITE_CACHED_VAR(rcu_data[cur_data], WINE);
1008 PRODUCE_TOKENS(proc_urcu_writer, WRITE_DATA);
1011 :: CONSUME_TOKENS(proc_urcu_writer,
1015 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_WMB);
1017 :: CONSUME_TOKENS(proc_urcu_writer,
1020 /* rcu_xchg_pointer() */
1022 old_data = READ_CACHED_VAR(rcu_ptr);
1023 WRITE_CACHED_VAR(rcu_ptr, cur_data);
1025 PRODUCE_TOKENS(proc_urcu_writer, WRITE_XCHG_PTR);
1027 :: CONSUME_TOKENS(proc_urcu_writer,
1028 WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR,
1029 WRITE_PROC_FIRST_MB) ->
1032 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_MB);
1035 :: CONSUME_TOKENS(proc_urcu_writer,
1036 WRITE_PROC_FIRST_MB,
1037 WRITE_PROC_FIRST_READ_GP) ->
1038 tmpa = READ_CACHED_VAR(urcu_gp_ctr);
1039 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_READ_GP);
1040 :: CONSUME_TOKENS(proc_urcu_writer,
1041 WRITE_PROC_FIRST_MB | WRITE_PROC_WMB
1042 | WRITE_PROC_FIRST_READ_GP,
1043 WRITE_PROC_FIRST_WRITE_GP) ->
1045 WRITE_CACHED_VAR(urcu_gp_ctr, tmpa ^ RCU_GP_CTR_BIT);
1046 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WRITE_GP);
1048 :: CONSUME_TOKENS(proc_urcu_writer,
1049 //WRITE_PROC_FIRST_WRITE_GP | /* TEST ADDING SYNC CORE */
1050 WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
1051 WRITE_PROC_FIRST_WAIT | WRITE_PROC_FIRST_WAIT_LOOP) ->
1053 //smp_mb(i); /* TEST */
1054 /* ONLY WAITING FOR READER 0 */
1055 tmp2 = READ_CACHED_VAR(urcu_active_readers[0]);
1057 /* In normal execution, we are always starting by
1058 * waiting for the even parity.
1060 cur_gp_val = RCU_GP_CTR_BIT;
1063 :: (tmp2 & RCU_GP_CTR_NEST_MASK)
1064 && ((tmp2 ^ cur_gp_val) & RCU_GP_CTR_BIT) ->
1065 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WAIT_LOOP);
1067 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WAIT);
1070 :: CONSUME_TOKENS(proc_urcu_writer,
1071 //WRITE_PROC_FIRST_WRITE_GP /* TEST ADDING SYNC CORE */
1072 WRITE_PROC_FIRST_WRITE_GP
1073 | WRITE_PROC_FIRST_READ_GP
1074 | WRITE_PROC_FIRST_WAIT_LOOP
1075 | WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR
1076 | WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
1078 #ifndef GEN_ERROR_WRITER_PROGRESS
1081 /* The memory barrier will invalidate the
1082 * second read done as prefetching. Note that all
1083 * instructions with side-effects depending on
1084 * WRITE_PROC_SECOND_READ_GP should also depend on
1085 * completion of this busy-waiting loop. */
1086 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_READ_GP);
1090 /* This instruction loops to WRITE_PROC_FIRST_WAIT */
1091 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WAIT_LOOP | WRITE_PROC_FIRST_WAIT);
1094 :: CONSUME_TOKENS(proc_urcu_writer,
1095 //WRITE_PROC_FIRST_WAIT | //test /* no dependency. Could pre-fetch, no side-effect. */
1096 WRITE_PROC_FIRST_WRITE_GP
1097 | WRITE_PROC_FIRST_READ_GP
1098 | WRITE_PROC_FIRST_MB,
1099 WRITE_PROC_SECOND_READ_GP) ->
1101 //smp_mb(i); /* TEST */
1102 tmpa = READ_CACHED_VAR(urcu_gp_ctr);
1103 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_READ_GP);
1104 :: CONSUME_TOKENS(proc_urcu_writer,
1105 WRITE_PROC_FIRST_WAIT /* dependency on first wait, because this
1106 * instruction has globally observable
1109 | WRITE_PROC_FIRST_MB
1111 | WRITE_PROC_FIRST_READ_GP
1112 | WRITE_PROC_FIRST_WRITE_GP
1113 | WRITE_PROC_SECOND_READ_GP,
1114 WRITE_PROC_SECOND_WRITE_GP) ->
1116 WRITE_CACHED_VAR(urcu_gp_ctr, tmpa ^ RCU_GP_CTR_BIT);
1117 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WRITE_GP);
1119 :: CONSUME_TOKENS(proc_urcu_writer,
1120 //WRITE_PROC_FIRST_WRITE_GP | /* TEST ADDING SYNC CORE */
1121 WRITE_PROC_FIRST_WAIT
1122 | WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
1123 WRITE_PROC_SECOND_WAIT | WRITE_PROC_SECOND_WAIT_LOOP) ->
1125 //smp_mb(i); /* TEST */
1126 /* ONLY WAITING FOR READER 0 */
1127 tmp2 = READ_CACHED_VAR(urcu_active_readers[0]);
1129 :: (tmp2 & RCU_GP_CTR_NEST_MASK)
1130 && ((tmp2 ^ 0) & RCU_GP_CTR_BIT) ->
1131 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT_LOOP);
1133 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT);
1136 :: CONSUME_TOKENS(proc_urcu_writer,
1137 //WRITE_PROC_FIRST_WRITE_GP | /* TEST ADDING SYNC CORE */
1138 WRITE_PROC_SECOND_WRITE_GP
1139 | WRITE_PROC_FIRST_WRITE_GP
1140 | WRITE_PROC_SECOND_READ_GP
1141 | WRITE_PROC_FIRST_READ_GP
1142 | WRITE_PROC_SECOND_WAIT_LOOP
1143 | WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR
1144 | WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
1146 #ifndef GEN_ERROR_WRITER_PROGRESS
1152 /* This instruction loops to WRITE_PROC_SECOND_WAIT */
1153 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT_LOOP | WRITE_PROC_SECOND_WAIT);
1156 :: CONSUME_TOKENS(proc_urcu_writer,
1157 WRITE_PROC_FIRST_WAIT
1158 | WRITE_PROC_SECOND_WAIT
1159 | WRITE_PROC_FIRST_READ_GP
1160 | WRITE_PROC_SECOND_READ_GP
1161 | WRITE_PROC_FIRST_WRITE_GP
1162 | WRITE_PROC_SECOND_WRITE_GP
1163 | WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR
1164 | WRITE_PROC_FIRST_MB,
1165 WRITE_PROC_SECOND_MB) ->
1168 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_MB);
1170 :: CONSUME_TOKENS(proc_urcu_writer,
1172 | WRITE_PROC_FIRST_WAIT
1173 | WRITE_PROC_SECOND_WAIT
1174 | WRITE_PROC_WMB /* No dependency on
1175 * WRITE_DATA because we
1177 * different location. */
1178 | WRITE_PROC_SECOND_MB
1179 | WRITE_PROC_FIRST_MB,
1181 WRITE_CACHED_VAR(rcu_data[old_data], POISON);
1182 PRODUCE_TOKENS(proc_urcu_writer, WRITE_FREE);
1184 :: CONSUME_TOKENS(proc_urcu_writer, WRITE_PROC_ALL_TOKENS, 0) ->
1185 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_ALL_TOKENS_CLEAR);
1191 * Note : Promela model adds implicit serialization of the
1192 * WRITE_FREE instruction. Normally, it would be permitted to
1193 * spill on the next loop execution. Given the validation we do
1194 * checks for the data entry read to be poisoned, it's ok if
1195 * we do not check "late arriving" memory poisoning.
1200 * Given the reader loops infinitely, let the writer also busy-loop
1201 * with progress here so, with weak fairness, we can test the
1202 * writer's progress.
1207 #ifdef WRITER_PROGRESS
1210 #ifdef READER_PROGRESS
1212 * Make sure we don't block the reader's progress.
1214 smp_mb_send(i, j, 5);
1219 /* Non-atomic parts of the loop */
1222 smp_mb_send(i, j, 1);
1223 goto smp_mb_send1_end;
1224 #ifndef GEN_ERROR_WRITER_PROGRESS
1226 smp_mb_send(i, j, 2);
1227 goto smp_mb_send2_end;
1229 smp_mb_send(i, j, 3);
1230 goto smp_mb_send3_end;
1233 smp_mb_send(i, j, 4);
1234 goto smp_mb_send4_end;
1239 /* no name clash please */
1240 #undef proc_urcu_writer
1243 /* Leave after the readers and writers so the pid count is ok. */
1248 INIT_CACHED_VAR(urcu_gp_ctr, 1, j);
1249 INIT_CACHED_VAR(rcu_ptr, 0, j);
1253 :: i < NR_READERS ->
1254 INIT_CACHED_VAR(urcu_active_readers[i], 0, j);
1255 ptr_read_first[i] = 1;
1256 ptr_read_second[i] = 1;
1257 data_read_first[i] = WINE;
1258 data_read_second[i] = WINE;
1260 :: i >= NR_READERS -> break
1262 INIT_CACHED_VAR(rcu_data[0], WINE, j);
1266 INIT_CACHED_VAR(rcu_data[i], POISON, j);
1268 :: i >= SLAB_SIZE -> break