1 #define WRITER_PROGRESS
2 #define GEN_ERROR_WRITER_PROGRESS
4 // Poison value for freed memory
6 // Memory with correct data
10 #define read_poison (data_read_first[0] == POISON || data_read_second[0] == POISON)
12 #define RCU_GP_CTR_BIT (1 << 7)
13 #define RCU_GP_CTR_NEST_MASK (RCU_GP_CTR_BIT - 1)
16 //#define REMOTE_BARRIERS
20 //#define ARCH_POWERPC
22 * mem.spin: Promela code to validate memory barriers with OOO memory
23 * and out-of-order instruction scheduling.
25 * This program is free software; you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation; either version 2 of the License, or
28 * (at your option) any later version.
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
35 * You should have received a copy of the GNU General Public License
36 * along with this program; if not, write to the Free Software
37 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
39 * Copyright (c) 2009 Mathieu Desnoyers
42 /* Promela validation variables. */
44 /* specific defines "included" here */
45 /* DEFINES file "included" here */
52 #define get_pid() (_pid)
54 #define get_readerid() (get_pid())
57 * Produced process control and data flow. Updated after each instruction to
58 * show which variables are ready. Using one-hot bit encoding per variable to
59 * save state space. Used as triggers to execute the instructions having those
60 * variables as input. Leaving bits active to inhibit instruction execution.
61 * Scheme used to make instruction disabling and automatic dependency fall-back
65 #define CONSUME_TOKENS(state, bits, notbits) \
66 ((!(state & (notbits))) && (state & (bits)) == (bits))
68 #define PRODUCE_TOKENS(state, bits) \
69 state = state | (bits);
71 #define CLEAR_TOKENS(state, bits) \
72 state = state & ~(bits)
75 * Types of dependency :
79 * - True dependency, Read-after-Write (RAW)
81 * This type of dependency happens when a statement depends on the result of a
82 * previous statement. This applies to any statement which needs to read a
83 * variable written by a preceding statement.
85 * - False dependency, Write-after-Read (WAR)
87 * Typically, variable renaming can ensure that this dependency goes away.
88 * However, if the statements must read and then write from/to the same variable
89 * in the OOO memory model, renaming may be impossible, and therefore this
90 * causes a WAR dependency.
92 * - Output dependency, Write-after-Write (WAW)
94 * Two writes to the same variable in subsequent statements. Variable renaming
95 * can ensure this is not needed, but can be required when writing multiple
96 * times to the same OOO mem model variable.
100 * Execution of a given instruction depends on a previous instruction evaluating
101 * in a way that allows its execution. E.g. : branches.
103 * Useful considerations for joining dependencies after branch
107 * "We say box i dominates box j if every path (leading from input to output
108 * through the diagram) which passes through box j must also pass through box
109 * i. Thus box i dominates box j if box j is subordinate to box i in the
112 * http://www.hipersoft.rice.edu/grads/publications/dom14.pdf
113 * Other classic algorithm to calculate dominance : Lengauer-Tarjan (in gcc)
117 * Just as pre-dominance, but with arcs of the data flow inverted, and input vs
118 * output exchanged. Therefore, i post-dominating j ensures that every path
119 * passing by j will pass by i before reaching the output.
121 * Prefetch and speculative execution
123 * If an instruction depends on the result of a previous branch, but it does not
124 * have side-effects, it can be executed before the branch result is known.
125 * however, it must be restarted if a core-synchronizing instruction is issued.
126 * Note that instructions which depend on the speculative instruction result
127 * but that have side-effects must depend on the branch completion in addition
128 * to the speculatively executed instruction.
130 * Other considerations
132 * Note about "volatile" keyword dependency : The compiler will order volatile
133 * accesses so they appear in the right order on a given CPU. They can be
134 * reordered by the CPU instruction scheduling. This therefore cannot be
135 * considered as a depencency.
139 * Cooper, Keith D.; & Torczon, Linda. (2005). Engineering a Compiler. Morgan
140 * Kaufmann. ISBN 1-55860-698-X.
141 * Kennedy, Ken; & Allen, Randy. (2001). Optimizing Compilers for Modern
142 * Architectures: A Dependence-based Approach. Morgan Kaufmann. ISBN
144 * Muchnick, Steven S. (1997). Advanced Compiler Design and Implementation.
145 * Morgan Kaufmann. ISBN 1-55860-320-4.
149 * Note about loops and nested calls
151 * To keep this model simple, loops expressed in the framework will behave as if
152 * there was a core synchronizing instruction between loops. To see the effect
153 * of loop unrolling, manually unrolling loops is required. Note that if loops
154 * end or start with a core synchronizing instruction, the model is appropriate.
155 * Nested calls are not supported.
159 * Only Alpha has out-of-order cache bank loads. Other architectures (intel,
160 * powerpc, arm) ensure that dependent reads won't be reordered. c.f.
161 * http://www.linuxjournal.com/article/8212)
164 #define HAVE_OOO_CACHE_READ
168 * Each process have its own data in cache. Caches are randomly updated.
169 * smp_wmb and smp_rmb forces cache updates (write and read), smp_mb forces
173 typedef per_proc_byte {
177 typedef per_proc_bit {
181 /* Bitfield has a maximum of 8 procs */
182 typedef per_proc_bitfield {
186 #define DECLARE_CACHED_VAR(type, x) \
188 per_proc_##type cached_##x; \
189 per_proc_bitfield cache_dirty_##x;
191 #define INIT_CACHED_VAR(x, v, j) \
193 cache_dirty_##x.bitfield = 0; \
197 cached_##x.val[j] = v; \
199 :: j >= NR_PROCS -> break \
202 #define IS_CACHE_DIRTY(x, id) (cache_dirty_##x.bitfield & (1 << id))
204 #define READ_CACHED_VAR(x) (cached_##x.val[get_pid()])
206 #define WRITE_CACHED_VAR(x, v) \
208 cached_##x.val[get_pid()] = v; \
209 cache_dirty_##x.bitfield = \
210 cache_dirty_##x.bitfield | (1 << get_pid()); \
213 #define CACHE_WRITE_TO_MEM(x, id) \
215 :: IS_CACHE_DIRTY(x, id) -> \
216 mem_##x = cached_##x.val[id]; \
217 cache_dirty_##x.bitfield = \
218 cache_dirty_##x.bitfield & (~(1 << id)); \
223 #define CACHE_READ_FROM_MEM(x, id) \
225 :: !IS_CACHE_DIRTY(x, id) -> \
226 cached_##x.val[id] = mem_##x;\
232 * May update other caches if cache is dirty, or not.
234 #define RANDOM_CACHE_WRITE_TO_MEM(x, id)\
236 :: 1 -> CACHE_WRITE_TO_MEM(x, id); \
240 #define RANDOM_CACHE_READ_FROM_MEM(x, id)\
242 :: 1 -> CACHE_READ_FROM_MEM(x, id); \
246 /* Must consume all prior read tokens. All subsequent reads depend on it. */
250 CACHE_READ_FROM_MEM(urcu_gp_ctr, get_pid());
254 CACHE_READ_FROM_MEM(urcu_active_readers[i], get_pid());
256 :: i >= NR_READERS -> break
258 CACHE_READ_FROM_MEM(rcu_ptr, get_pid());
262 CACHE_READ_FROM_MEM(rcu_data[i], get_pid());
264 :: i >= SLAB_SIZE -> break
269 /* Must consume all prior write tokens. All subsequent writes depend on it. */
273 CACHE_WRITE_TO_MEM(urcu_gp_ctr, get_pid());
277 CACHE_WRITE_TO_MEM(urcu_active_readers[i], get_pid());
279 :: i >= NR_READERS -> break
281 CACHE_WRITE_TO_MEM(rcu_ptr, get_pid());
285 CACHE_WRITE_TO_MEM(rcu_data[i], get_pid());
287 :: i >= SLAB_SIZE -> break
292 /* Synchronization point. Must consume all prior read and write tokens. All
293 * subsequent reads and writes depend on it. */
302 #ifdef REMOTE_BARRIERS
304 bit reader_barrier[NR_READERS];
307 * We cannot leave the barriers dependencies in place in REMOTE_BARRIERS mode
308 * because they would add unexisting core synchronization and would therefore
309 * create an incomplete model.
310 * Therefore, we model the read-side memory barriers by completely disabling the
311 * memory barriers and their dependencies from the read-side. One at a time
312 * (different verification runs), we make a different instruction listen for
316 #define smp_mb_reader(i, j)
319 * Service 0, 1 or many barrier requests.
321 inline smp_mb_recv(i, j)
324 :: (reader_barrier[get_readerid()] == 1) ->
326 * We choose to ignore cycles caused by writer busy-looping,
327 * waiting for the reader, sending barrier requests, and the
328 * reader always services them without continuing execution.
330 progress_ignoring_mb1:
332 reader_barrier[get_readerid()] = 0;
335 * We choose to ignore writer's non-progress caused by the
336 * reader ignoring the writer's mb() requests.
338 progress_ignoring_mb2:
343 #define PROGRESS_LABEL(progressid) progress_writer_progid_##progressid:
345 #define smp_mb_send(i, j, progressid) \
350 :: i < NR_READERS -> \
351 reader_barrier[i] = 1; \
353 * Busy-looping waiting for reader barrier handling is of little\
354 * interest, given the reader has the ability to totally ignore \
355 * barrier requests. \
358 :: (reader_barrier[i] == 1) -> \
359 PROGRESS_LABEL(progressid) \
361 :: (reader_barrier[i] == 0) -> break; \
364 :: i >= NR_READERS -> \
372 #define smp_mb_send(i, j, progressid) smp_mb(i)
373 #define smp_mb_reader(i, j) smp_mb(i)
374 #define smp_mb_recv(i, j)
378 /* Keep in sync manually with smp_rmb, smp_wmb, ooo_mem and init() */
379 DECLARE_CACHED_VAR(byte, urcu_gp_ctr);
380 /* Note ! currently only one reader */
381 DECLARE_CACHED_VAR(byte, urcu_active_readers[NR_READERS]);
383 DECLARE_CACHED_VAR(bit, rcu_data[SLAB_SIZE]);
387 DECLARE_CACHED_VAR(bit, rcu_ptr);
388 bit ptr_read_first[NR_READERS];
389 bit ptr_read_second[NR_READERS];
391 DECLARE_CACHED_VAR(byte, rcu_ptr);
392 byte ptr_read_first[NR_READERS];
393 byte ptr_read_second[NR_READERS];
396 bit data_read_first[NR_READERS];
397 bit data_read_second[NR_READERS];
401 inline wait_init_done()
404 :: init_done == 0 -> skip;
412 RANDOM_CACHE_WRITE_TO_MEM(urcu_gp_ctr, get_pid());
416 RANDOM_CACHE_WRITE_TO_MEM(urcu_active_readers[i],
419 :: i >= NR_READERS -> break
421 RANDOM_CACHE_WRITE_TO_MEM(rcu_ptr, get_pid());
425 RANDOM_CACHE_WRITE_TO_MEM(rcu_data[i], get_pid());
427 :: i >= SLAB_SIZE -> break
429 #ifdef HAVE_OOO_CACHE_READ
430 RANDOM_CACHE_READ_FROM_MEM(urcu_gp_ctr, get_pid());
434 RANDOM_CACHE_READ_FROM_MEM(urcu_active_readers[i],
437 :: i >= NR_READERS -> break
439 RANDOM_CACHE_READ_FROM_MEM(rcu_ptr, get_pid());
443 RANDOM_CACHE_READ_FROM_MEM(rcu_data[i], get_pid());
445 :: i >= SLAB_SIZE -> break
449 #endif /* HAVE_OOO_CACHE_READ */
454 * Bit encoding, urcu_reader :
457 int _proc_urcu_reader;
458 #define proc_urcu_reader _proc_urcu_reader
460 /* Body of PROCEDURE_READ_LOCK */
461 #define READ_PROD_A_READ (1 << 0)
462 #define READ_PROD_B_IF_TRUE (1 << 1)
463 #define READ_PROD_B_IF_FALSE (1 << 2)
464 #define READ_PROD_C_IF_TRUE_READ (1 << 3)
466 #define PROCEDURE_READ_LOCK(base, consumetoken, consumetoken2, producetoken) \
467 :: CONSUME_TOKENS(proc_urcu_reader, (consumetoken | consumetoken2), READ_PROD_A_READ << base) -> \
469 tmp = READ_CACHED_VAR(urcu_active_readers[get_readerid()]); \
470 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_A_READ << base); \
471 :: CONSUME_TOKENS(proc_urcu_reader, \
472 READ_PROD_A_READ << base, /* RAW, pre-dominant */ \
473 (READ_PROD_B_IF_TRUE | READ_PROD_B_IF_FALSE) << base) -> \
475 :: (!(tmp & RCU_GP_CTR_NEST_MASK)) -> \
476 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_B_IF_TRUE << base); \
478 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_B_IF_FALSE << base); \
481 :: CONSUME_TOKENS(proc_urcu_reader, consumetoken, /* prefetch */ \
482 READ_PROD_C_IF_TRUE_READ << base) -> \
484 tmp2 = READ_CACHED_VAR(urcu_gp_ctr); \
485 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_C_IF_TRUE_READ << base); \
486 :: CONSUME_TOKENS(proc_urcu_reader, \
487 (READ_PROD_B_IF_TRUE \
488 | READ_PROD_C_IF_TRUE_READ /* pre-dominant */ \
489 | READ_PROD_A_READ) << base, /* WAR */ \
492 WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp2); \
493 PRODUCE_TOKENS(proc_urcu_reader, producetoken); \
494 /* IF_MERGE implies \
495 * post-dominance */ \
497 :: CONSUME_TOKENS(proc_urcu_reader, \
498 (READ_PROD_B_IF_FALSE /* pre-dominant */ \
499 | READ_PROD_A_READ) << base, /* WAR */ \
502 WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], \
504 PRODUCE_TOKENS(proc_urcu_reader, producetoken); \
505 /* IF_MERGE implies \
506 * post-dominance */ \
510 /* Body of PROCEDURE_READ_LOCK */
511 #define READ_PROC_READ_UNLOCK (1 << 0)
513 #define PROCEDURE_READ_UNLOCK(base, consumetoken, producetoken) \
514 :: CONSUME_TOKENS(proc_urcu_reader, \
516 READ_PROC_READ_UNLOCK << base) -> \
518 tmp = READ_CACHED_VAR(urcu_active_readers[get_readerid()]); \
519 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_READ_UNLOCK << base); \
520 :: CONSUME_TOKENS(proc_urcu_reader, \
522 | (READ_PROC_READ_UNLOCK << base), /* WAR */ \
525 WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp - 1); \
526 PRODUCE_TOKENS(proc_urcu_reader, producetoken); \
530 #define READ_PROD_NONE (1 << 0)
532 /* PROCEDURE_READ_LOCK base = << 1 : 1 to 5 */
533 #define READ_LOCK_BASE 1
534 #define READ_LOCK_OUT (1 << 5)
536 #define READ_PROC_FIRST_MB (1 << 6)
538 /* PROCEDURE_READ_LOCK (NESTED) base : << 7 : 7 to 11 */
539 #define READ_LOCK_NESTED_BASE 7
540 #define READ_LOCK_NESTED_OUT (1 << 11)
542 #define READ_PROC_READ_GEN (1 << 12)
543 #define READ_PROC_ACCESS_GEN (1 << 13)
545 /* PROCEDURE_READ_UNLOCK (NESTED) base = << 14 : 14 to 15 */
546 #define READ_UNLOCK_NESTED_BASE 14
547 #define READ_UNLOCK_NESTED_OUT (1 << 15)
549 #define READ_PROC_SECOND_MB (1 << 16)
551 /* PROCEDURE_READ_UNLOCK base = << 17 : 17 to 18 */
552 #define READ_UNLOCK_BASE 17
553 #define READ_UNLOCK_OUT (1 << 18)
555 /* PROCEDURE_READ_LOCK_UNROLL base = << 19 : 19 to 23 */
556 #define READ_LOCK_UNROLL_BASE 19
557 #define READ_LOCK_OUT_UNROLL (1 << 23)
559 #define READ_PROC_THIRD_MB (1 << 24)
561 #define READ_PROC_READ_GEN_UNROLL (1 << 25)
562 #define READ_PROC_ACCESS_GEN_UNROLL (1 << 26)
564 #define READ_PROC_FOURTH_MB (1 << 27)
566 /* PROCEDURE_READ_UNLOCK_UNROLL base = << 28 : 28 to 29 */
567 #define READ_UNLOCK_UNROLL_BASE 28
568 #define READ_UNLOCK_OUT_UNROLL (1 << 29)
571 /* Should not include branches */
572 #define READ_PROC_ALL_TOKENS (READ_PROD_NONE \
574 | READ_PROC_FIRST_MB \
575 | READ_LOCK_NESTED_OUT \
576 | READ_PROC_READ_GEN \
577 | READ_PROC_ACCESS_GEN \
578 | READ_UNLOCK_NESTED_OUT \
579 | READ_PROC_SECOND_MB \
581 | READ_LOCK_OUT_UNROLL \
582 | READ_PROC_THIRD_MB \
583 | READ_PROC_READ_GEN_UNROLL \
584 | READ_PROC_ACCESS_GEN_UNROLL \
585 | READ_PROC_FOURTH_MB \
586 | READ_UNLOCK_OUT_UNROLL)
588 /* Must clear all tokens, including branches */
589 #define READ_PROC_ALL_TOKENS_CLEAR ((1 << 30) - 1)
591 inline urcu_one_read(i, j, nest_i, tmp, tmp2)
593 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_NONE);
596 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FIRST_MB);
597 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_SECOND_MB);
598 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_THIRD_MB);
599 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FOURTH_MB);
602 #ifdef REMOTE_BARRIERS
603 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FIRST_MB);
604 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_SECOND_MB);
605 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_THIRD_MB);
606 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FOURTH_MB);
612 #ifdef REMOTE_BARRIERS
614 * Signal-based memory barrier will only execute when the
615 * execution order appears in program order.
621 :: CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE,
622 READ_LOCK_OUT | READ_LOCK_NESTED_OUT
623 | READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
625 | READ_LOCK_OUT_UNROLL
626 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
627 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT,
629 | READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
631 | READ_LOCK_OUT_UNROLL
632 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
633 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT | READ_LOCK_NESTED_OUT,
634 READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
636 | READ_LOCK_OUT_UNROLL
637 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
638 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
639 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN,
640 READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
642 | READ_LOCK_OUT_UNROLL
643 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
644 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
645 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN,
646 READ_UNLOCK_NESTED_OUT
648 | READ_LOCK_OUT_UNROLL
649 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
650 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
651 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN
652 | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT,
654 | READ_LOCK_OUT_UNROLL
655 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
656 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
657 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN
658 | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
661 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
662 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
663 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN
664 | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
665 | READ_UNLOCK_OUT | READ_LOCK_OUT_UNROLL,
666 READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
667 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
668 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN
669 | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
670 | READ_UNLOCK_OUT | READ_LOCK_OUT_UNROLL
671 | READ_PROC_READ_GEN_UNROLL,
672 READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
673 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
674 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN
675 | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
676 | READ_UNLOCK_OUT | READ_LOCK_OUT_UNROLL
677 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL,
678 READ_UNLOCK_OUT_UNROLL)
679 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
680 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
681 | READ_UNLOCK_OUT | READ_LOCK_OUT_UNROLL
682 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL,
691 goto non_atomic3_skip;
694 goto non_atomic3_end;
697 #endif /* REMOTE_BARRIERS */
701 PROCEDURE_READ_LOCK(READ_LOCK_BASE, READ_PROD_NONE, 0, READ_LOCK_OUT);
703 :: CONSUME_TOKENS(proc_urcu_reader,
704 READ_LOCK_OUT, /* post-dominant */
705 READ_PROC_FIRST_MB) ->
707 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FIRST_MB);
709 PROCEDURE_READ_LOCK(READ_LOCK_NESTED_BASE, READ_PROC_FIRST_MB, READ_LOCK_OUT,
710 READ_LOCK_NESTED_OUT);
712 :: CONSUME_TOKENS(proc_urcu_reader,
713 READ_PROC_FIRST_MB, /* mb() orders reads */
714 READ_PROC_READ_GEN) ->
716 ptr_read_first[get_readerid()] = READ_CACHED_VAR(rcu_ptr);
717 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_READ_GEN);
719 :: CONSUME_TOKENS(proc_urcu_reader,
720 READ_PROC_FIRST_MB /* mb() orders reads */
721 | READ_PROC_READ_GEN,
722 READ_PROC_ACCESS_GEN) ->
723 /* smp_read_barrier_depends */
726 data_read_first[get_readerid()] =
727 READ_CACHED_VAR(rcu_data[ptr_read_first[get_readerid()]]);
728 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_ACCESS_GEN);
731 /* Note : we remove the nested memory barrier from the read unlock
732 * model, given it is not usually needed. The implementation has the barrier
733 * because the performance impact added by a branch in the common case does not
737 PROCEDURE_READ_UNLOCK(READ_UNLOCK_NESTED_BASE,
740 | READ_LOCK_NESTED_OUT,
741 READ_UNLOCK_NESTED_OUT);
744 :: CONSUME_TOKENS(proc_urcu_reader,
745 READ_PROC_ACCESS_GEN /* mb() orders reads */
746 | READ_PROC_READ_GEN /* mb() orders reads */
747 | READ_PROC_FIRST_MB /* mb() ordered */
748 | READ_LOCK_OUT /* post-dominant */
749 | READ_LOCK_NESTED_OUT /* post-dominant */
750 | READ_UNLOCK_NESTED_OUT,
751 READ_PROC_SECOND_MB) ->
753 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_SECOND_MB);
755 PROCEDURE_READ_UNLOCK(READ_UNLOCK_BASE,
756 READ_PROC_SECOND_MB /* mb() orders reads */
757 | READ_PROC_FIRST_MB /* mb() orders reads */
758 | READ_LOCK_NESTED_OUT /* RAW */
759 | READ_LOCK_OUT /* RAW */
760 | READ_UNLOCK_NESTED_OUT, /* RAW */
763 /* Unrolling loop : second consecutive lock */
764 /* reading urcu_active_readers, which have been written by
765 * READ_UNLOCK_OUT : RAW */
766 PROCEDURE_READ_LOCK(READ_LOCK_UNROLL_BASE,
767 READ_PROC_SECOND_MB /* mb() orders reads */
768 | READ_PROC_FIRST_MB, /* mb() orders reads */
769 READ_LOCK_NESTED_OUT /* RAW */
770 | READ_LOCK_OUT /* RAW */
771 | READ_UNLOCK_NESTED_OUT /* RAW */
772 | READ_UNLOCK_OUT, /* RAW */
773 READ_LOCK_OUT_UNROLL);
776 :: CONSUME_TOKENS(proc_urcu_reader,
777 READ_PROC_FIRST_MB /* mb() ordered */
778 | READ_PROC_SECOND_MB /* mb() ordered */
779 | READ_LOCK_OUT_UNROLL /* post-dominant */
780 | READ_LOCK_NESTED_OUT
782 | READ_UNLOCK_NESTED_OUT
784 READ_PROC_THIRD_MB) ->
786 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_THIRD_MB);
788 :: CONSUME_TOKENS(proc_urcu_reader,
789 READ_PROC_FIRST_MB /* mb() orders reads */
790 | READ_PROC_SECOND_MB /* mb() orders reads */
791 | READ_PROC_THIRD_MB, /* mb() orders reads */
792 READ_PROC_READ_GEN_UNROLL) ->
794 ptr_read_second[get_readerid()] = READ_CACHED_VAR(rcu_ptr);
795 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_READ_GEN_UNROLL);
797 :: CONSUME_TOKENS(proc_urcu_reader,
798 READ_PROC_READ_GEN_UNROLL
799 | READ_PROC_FIRST_MB /* mb() orders reads */
800 | READ_PROC_SECOND_MB /* mb() orders reads */
801 | READ_PROC_THIRD_MB, /* mb() orders reads */
802 READ_PROC_ACCESS_GEN_UNROLL) ->
803 /* smp_read_barrier_depends */
806 data_read_second[get_readerid()] =
807 READ_CACHED_VAR(rcu_data[ptr_read_second[get_readerid()]]);
808 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_ACCESS_GEN_UNROLL);
810 :: CONSUME_TOKENS(proc_urcu_reader,
811 READ_PROC_READ_GEN_UNROLL /* mb() orders reads */
812 | READ_PROC_ACCESS_GEN_UNROLL /* mb() orders reads */
813 | READ_PROC_FIRST_MB /* mb() ordered */
814 | READ_PROC_SECOND_MB /* mb() ordered */
815 | READ_PROC_THIRD_MB /* mb() ordered */
816 | READ_LOCK_OUT_UNROLL /* post-dominant */
817 | READ_LOCK_NESTED_OUT
819 | READ_UNLOCK_NESTED_OUT
821 READ_PROC_FOURTH_MB) ->
823 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FOURTH_MB);
825 PROCEDURE_READ_UNLOCK(READ_UNLOCK_UNROLL_BASE,
826 READ_PROC_FOURTH_MB /* mb() orders reads */
827 | READ_PROC_THIRD_MB /* mb() orders reads */
828 | READ_LOCK_OUT_UNROLL /* RAW */
829 | READ_PROC_SECOND_MB /* mb() orders reads */
830 | READ_PROC_FIRST_MB /* mb() orders reads */
831 | READ_LOCK_NESTED_OUT /* RAW */
832 | READ_LOCK_OUT /* RAW */
833 | READ_UNLOCK_NESTED_OUT, /* RAW */
834 READ_UNLOCK_OUT_UNROLL);
835 :: CONSUME_TOKENS(proc_urcu_reader, READ_PROC_ALL_TOKENS, 0) ->
836 CLEAR_TOKENS(proc_urcu_reader, READ_PROC_ALL_TOKENS_CLEAR);
842 * Dependency between consecutive loops :
844 * WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp2 - 1)
845 * tmp = READ_CACHED_VAR(urcu_active_readers[get_readerid()]);
847 * _WHEN THE MB()s are in place_, they add full ordering of the
848 * generation pointer read wrt active reader count read, which ensures
849 * execution will not spill across loop execution.
850 * However, in the event mb()s are removed (execution using signal
851 * handler to promote barrier()() -> smp_mb()), nothing prevents one loop
852 * to spill its execution on other loop's execution.
875 active proctype urcu_reader()
882 assert(get_pid() < NR_PROCS);
888 * We do not test reader's progress here, because we are mainly
889 * interested in writer's progress. The reader never blocks
890 * anyway. We have to test for reader/writer's progress
891 * separately, otherwise we could think the writer is doing
892 * progress when it's blocked by an always progressing reader.
894 #ifdef READER_PROGRESS
897 urcu_one_read(i, j, nest_i, tmp, tmp2);
901 /* no name clash please */
902 #undef proc_urcu_reader
905 /* Model the RCU update process. */
908 * Bit encoding, urcu_writer :
909 * Currently only supports one reader.
912 int _proc_urcu_writer;
913 #define proc_urcu_writer _proc_urcu_writer
915 #define WRITE_PROD_NONE (1 << 0)
917 #define WRITE_DATA (1 << 1)
918 #define WRITE_PROC_WMB (1 << 2)
919 #define WRITE_XCHG_PTR (1 << 3)
921 #define WRITE_PROC_FIRST_MB (1 << 4)
924 #define WRITE_PROC_FIRST_READ_GP (1 << 5)
925 #define WRITE_PROC_FIRST_WRITE_GP (1 << 6)
926 #define WRITE_PROC_FIRST_WAIT (1 << 7)
927 #define WRITE_PROC_FIRST_WAIT_LOOP (1 << 8)
930 #define WRITE_PROC_SECOND_READ_GP (1 << 9)
931 #define WRITE_PROC_SECOND_WRITE_GP (1 << 10)
932 #define WRITE_PROC_SECOND_WAIT (1 << 11)
933 #define WRITE_PROC_SECOND_WAIT_LOOP (1 << 12)
935 #define WRITE_PROC_SECOND_MB (1 << 13)
937 #define WRITE_FREE (1 << 14)
939 #define WRITE_PROC_ALL_TOKENS (WRITE_PROD_NONE \
943 | WRITE_PROC_FIRST_MB \
944 | WRITE_PROC_FIRST_READ_GP \
945 | WRITE_PROC_FIRST_WRITE_GP \
946 | WRITE_PROC_FIRST_WAIT \
947 | WRITE_PROC_SECOND_READ_GP \
948 | WRITE_PROC_SECOND_WRITE_GP \
949 | WRITE_PROC_SECOND_WAIT \
950 | WRITE_PROC_SECOND_MB \
953 #define WRITE_PROC_ALL_TOKENS_CLEAR ((1 << 15) - 1)
956 * Mutexes are implied around writer execution. A single writer at a time.
958 active proctype urcu_writer()
961 byte tmp, tmp2, tmpa;
962 byte cur_data = 0, old_data, loop_nr = 0;
963 byte cur_gp_val = 0; /*
964 * Keep a local trace of the current parity so
965 * we don't add non-existing dependencies on the global
966 * GP update. Needed to test single flip case.
971 assert(get_pid() < NR_PROCS);
975 #ifdef WRITER_PROGRESS
978 loop_nr = loop_nr + 1;
980 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROD_NONE);
983 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_WMB);
987 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_MB);
988 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_MB);
992 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_READ_GP);
993 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WRITE_GP);
994 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT);
995 /* For single flip, we need to know the current parity */
996 cur_gp_val = cur_gp_val ^ RCU_GP_CTR_BIT;
1003 :: CONSUME_TOKENS(proc_urcu_writer,
1007 cur_data = (cur_data + 1) % SLAB_SIZE;
1008 WRITE_CACHED_VAR(rcu_data[cur_data], WINE);
1009 PRODUCE_TOKENS(proc_urcu_writer, WRITE_DATA);
1012 :: CONSUME_TOKENS(proc_urcu_writer,
1016 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_WMB);
1018 :: CONSUME_TOKENS(proc_urcu_writer,
1021 /* rcu_xchg_pointer() */
1023 old_data = READ_CACHED_VAR(rcu_ptr);
1024 WRITE_CACHED_VAR(rcu_ptr, cur_data);
1026 PRODUCE_TOKENS(proc_urcu_writer, WRITE_XCHG_PTR);
1028 :: CONSUME_TOKENS(proc_urcu_writer,
1029 WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR,
1030 WRITE_PROC_FIRST_MB) ->
1033 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_MB);
1036 :: CONSUME_TOKENS(proc_urcu_writer,
1037 WRITE_PROC_FIRST_MB,
1038 WRITE_PROC_FIRST_READ_GP) ->
1039 tmpa = READ_CACHED_VAR(urcu_gp_ctr);
1040 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_READ_GP);
1041 :: CONSUME_TOKENS(proc_urcu_writer,
1042 WRITE_PROC_FIRST_MB | WRITE_PROC_WMB
1043 | WRITE_PROC_FIRST_READ_GP,
1044 WRITE_PROC_FIRST_WRITE_GP) ->
1046 WRITE_CACHED_VAR(urcu_gp_ctr, tmpa ^ RCU_GP_CTR_BIT);
1047 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WRITE_GP);
1049 :: CONSUME_TOKENS(proc_urcu_writer,
1050 //WRITE_PROC_FIRST_WRITE_GP | /* TEST ADDING SYNC CORE */
1051 WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
1052 WRITE_PROC_FIRST_WAIT | WRITE_PROC_FIRST_WAIT_LOOP) ->
1054 //smp_mb(i); /* TEST */
1055 /* ONLY WAITING FOR READER 0 */
1056 tmp2 = READ_CACHED_VAR(urcu_active_readers[0]);
1058 /* In normal execution, we are always starting by
1059 * waiting for the even parity.
1061 cur_gp_val = RCU_GP_CTR_BIT;
1064 :: (tmp2 & RCU_GP_CTR_NEST_MASK)
1065 && ((tmp2 ^ cur_gp_val) & RCU_GP_CTR_BIT) ->
1066 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WAIT_LOOP);
1068 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WAIT);
1071 :: CONSUME_TOKENS(proc_urcu_writer,
1072 //WRITE_PROC_FIRST_WRITE_GP /* TEST ADDING SYNC CORE */
1073 WRITE_PROC_FIRST_WRITE_GP
1074 | WRITE_PROC_FIRST_READ_GP
1075 | WRITE_PROC_FIRST_WAIT_LOOP
1076 | WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR
1077 | WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
1079 #ifndef GEN_ERROR_WRITER_PROGRESS
1082 /* The memory barrier will invalidate the
1083 * second read done as prefetching. Note that all
1084 * instructions with side-effects depending on
1085 * WRITE_PROC_SECOND_READ_GP should also depend on
1086 * completion of this busy-waiting loop. */
1087 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_READ_GP);
1091 /* This instruction loops to WRITE_PROC_FIRST_WAIT */
1092 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WAIT_LOOP | WRITE_PROC_FIRST_WAIT);
1095 :: CONSUME_TOKENS(proc_urcu_writer,
1096 //WRITE_PROC_FIRST_WAIT | //test /* no dependency. Could pre-fetch, no side-effect. */
1097 WRITE_PROC_FIRST_WRITE_GP
1098 | WRITE_PROC_FIRST_READ_GP
1099 | WRITE_PROC_FIRST_MB,
1100 WRITE_PROC_SECOND_READ_GP) ->
1102 //smp_mb(i); /* TEST */
1103 tmpa = READ_CACHED_VAR(urcu_gp_ctr);
1104 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_READ_GP);
1105 :: CONSUME_TOKENS(proc_urcu_writer,
1106 WRITE_PROC_FIRST_WAIT /* dependency on first wait, because this
1107 * instruction has globally observable
1110 | WRITE_PROC_FIRST_MB
1112 | WRITE_PROC_FIRST_READ_GP
1113 | WRITE_PROC_FIRST_WRITE_GP
1114 | WRITE_PROC_SECOND_READ_GP,
1115 WRITE_PROC_SECOND_WRITE_GP) ->
1117 WRITE_CACHED_VAR(urcu_gp_ctr, tmpa ^ RCU_GP_CTR_BIT);
1118 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WRITE_GP);
1120 :: CONSUME_TOKENS(proc_urcu_writer,
1121 //WRITE_PROC_FIRST_WRITE_GP | /* TEST ADDING SYNC CORE */
1122 WRITE_PROC_FIRST_WAIT
1123 | WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
1124 WRITE_PROC_SECOND_WAIT | WRITE_PROC_SECOND_WAIT_LOOP) ->
1126 //smp_mb(i); /* TEST */
1127 /* ONLY WAITING FOR READER 0 */
1128 tmp2 = READ_CACHED_VAR(urcu_active_readers[0]);
1130 :: (tmp2 & RCU_GP_CTR_NEST_MASK)
1131 && ((tmp2 ^ 0) & RCU_GP_CTR_BIT) ->
1132 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT_LOOP);
1134 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT);
1137 :: CONSUME_TOKENS(proc_urcu_writer,
1138 //WRITE_PROC_FIRST_WRITE_GP | /* TEST ADDING SYNC CORE */
1139 WRITE_PROC_SECOND_WRITE_GP
1140 | WRITE_PROC_FIRST_WRITE_GP
1141 | WRITE_PROC_SECOND_READ_GP
1142 | WRITE_PROC_FIRST_READ_GP
1143 | WRITE_PROC_SECOND_WAIT_LOOP
1144 | WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR
1145 | WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
1147 #ifndef GEN_ERROR_WRITER_PROGRESS
1153 /* This instruction loops to WRITE_PROC_SECOND_WAIT */
1154 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT_LOOP | WRITE_PROC_SECOND_WAIT);
1157 :: CONSUME_TOKENS(proc_urcu_writer,
1158 WRITE_PROC_FIRST_WAIT
1159 | WRITE_PROC_SECOND_WAIT
1160 | WRITE_PROC_FIRST_READ_GP
1161 | WRITE_PROC_SECOND_READ_GP
1162 | WRITE_PROC_FIRST_WRITE_GP
1163 | WRITE_PROC_SECOND_WRITE_GP
1164 | WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR
1165 | WRITE_PROC_FIRST_MB,
1166 WRITE_PROC_SECOND_MB) ->
1169 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_MB);
1171 :: CONSUME_TOKENS(proc_urcu_writer,
1173 | WRITE_PROC_FIRST_WAIT
1174 | WRITE_PROC_SECOND_WAIT
1175 | WRITE_PROC_WMB /* No dependency on
1176 * WRITE_DATA because we
1178 * different location. */
1179 | WRITE_PROC_SECOND_MB
1180 | WRITE_PROC_FIRST_MB,
1182 WRITE_CACHED_VAR(rcu_data[old_data], POISON);
1183 PRODUCE_TOKENS(proc_urcu_writer, WRITE_FREE);
1185 :: CONSUME_TOKENS(proc_urcu_writer, WRITE_PROC_ALL_TOKENS, 0) ->
1186 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_ALL_TOKENS_CLEAR);
1192 * Note : Promela model adds implicit serialization of the
1193 * WRITE_FREE instruction. Normally, it would be permitted to
1194 * spill on the next loop execution. Given the validation we do
1195 * checks for the data entry read to be poisoned, it's ok if
1196 * we do not check "late arriving" memory poisoning.
1201 * Given the reader loops infinitely, let the writer also busy-loop
1202 * with progress here so, with weak fairness, we can test the
1203 * writer's progress.
1208 #ifdef WRITER_PROGRESS
1211 #ifdef READER_PROGRESS
1213 * Make sure we don't block the reader's progress.
1215 smp_mb_send(i, j, 5);
1220 /* Non-atomic parts of the loop */
1223 smp_mb_send(i, j, 1);
1224 goto smp_mb_send1_end;
1225 #ifndef GEN_ERROR_WRITER_PROGRESS
1227 smp_mb_send(i, j, 2);
1228 goto smp_mb_send2_end;
1230 smp_mb_send(i, j, 3);
1231 goto smp_mb_send3_end;
1234 smp_mb_send(i, j, 4);
1235 goto smp_mb_send4_end;
1240 /* no name clash please */
1241 #undef proc_urcu_writer
1244 /* Leave after the readers and writers so the pid count is ok. */
1249 INIT_CACHED_VAR(urcu_gp_ctr, 1, j);
1250 INIT_CACHED_VAR(rcu_ptr, 0, j);
1254 :: i < NR_READERS ->
1255 INIT_CACHED_VAR(urcu_active_readers[i], 0, j);
1256 ptr_read_first[i] = 1;
1257 ptr_read_second[i] = 1;
1258 data_read_first[i] = WINE;
1259 data_read_second[i] = WINE;
1261 :: i >= NR_READERS -> break
1263 INIT_CACHED_VAR(rcu_data[0], WINE, j);
1267 INIT_CACHED_VAR(rcu_data[i], POISON, j);
1269 :: i >= SLAB_SIZE -> break