2 // Poison value for freed memory
4 // Memory with correct data
8 #define read_poison (data_read_first[0] == POISON || data_read_second[0] == POISON)
10 #define RCU_GP_CTR_BIT (1 << 7)
11 #define RCU_GP_CTR_NEST_MASK (RCU_GP_CTR_BIT - 1)
14 #define REMOTE_BARRIERS
18 //#define ARCH_POWERPC
20 * mem.spin: Promela code to validate memory barriers with OOO memory
21 * and out-of-order instruction scheduling.
23 * This program is free software; you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation; either version 2 of the License, or
26 * (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
37 * Copyright (c) 2009 Mathieu Desnoyers
40 /* Promela validation variables. */
42 /* specific defines "included" here */
43 /* DEFINES file "included" here */
50 #define get_pid() (_pid)
52 #define get_readerid() (get_pid())
55 * Produced process control and data flow. Updated after each instruction to
56 * show which variables are ready. Using one-hot bit encoding per variable to
57 * save state space. Used as triggers to execute the instructions having those
58 * variables as input. Leaving bits active to inhibit instruction execution.
59 * Scheme used to make instruction disabling and automatic dependency fall-back
63 #define CONSUME_TOKENS(state, bits, notbits) \
64 ((!(state & (notbits))) && (state & (bits)) == (bits))
66 #define PRODUCE_TOKENS(state, bits) \
67 state = state | (bits);
69 #define CLEAR_TOKENS(state, bits) \
70 state = state & ~(bits)
73 * Types of dependency :
77 * - True dependency, Read-after-Write (RAW)
79 * This type of dependency happens when a statement depends on the result of a
80 * previous statement. This applies to any statement which needs to read a
81 * variable written by a preceding statement.
83 * - False dependency, Write-after-Read (WAR)
85 * Typically, variable renaming can ensure that this dependency goes away.
86 * However, if the statements must read and then write from/to the same variable
87 * in the OOO memory model, renaming may be impossible, and therefore this
88 * causes a WAR dependency.
90 * - Output dependency, Write-after-Write (WAW)
92 * Two writes to the same variable in subsequent statements. Variable renaming
93 * can ensure this is not needed, but can be required when writing multiple
94 * times to the same OOO mem model variable.
98 * Execution of a given instruction depends on a previous instruction evaluating
99 * in a way that allows its execution. E.g. : branches.
101 * Useful considerations for joining dependencies after branch
105 * "We say box i dominates box j if every path (leading from input to output
106 * through the diagram) which passes through box j must also pass through box
107 * i. Thus box i dominates box j if box j is subordinate to box i in the
110 * http://www.hipersoft.rice.edu/grads/publications/dom14.pdf
111 * Other classic algorithm to calculate dominance : Lengauer-Tarjan (in gcc)
115 * Just as pre-dominance, but with arcs of the data flow inverted, and input vs
116 * output exchanged. Therefore, i post-dominating j ensures that every path
117 * passing by j will pass by i before reaching the output.
119 * Prefetch and speculative execution
121 * If an instruction depends on the result of a previous branch, but it does not
122 * have side-effects, it can be executed before the branch result is known.
123 * however, it must be restarted if a core-synchronizing instruction is issued.
124 * Note that instructions which depend on the speculative instruction result
125 * but that have side-effects must depend on the branch completion in addition
126 * to the speculatively executed instruction.
128 * Other considerations
130 * Note about "volatile" keyword dependency : The compiler will order volatile
131 * accesses so they appear in the right order on a given CPU. They can be
132 * reordered by the CPU instruction scheduling. This therefore cannot be
133 * considered as a depencency.
137 * Cooper, Keith D.; & Torczon, Linda. (2005). Engineering a Compiler. Morgan
138 * Kaufmann. ISBN 1-55860-698-X.
139 * Kennedy, Ken; & Allen, Randy. (2001). Optimizing Compilers for Modern
140 * Architectures: A Dependence-based Approach. Morgan Kaufmann. ISBN
142 * Muchnick, Steven S. (1997). Advanced Compiler Design and Implementation.
143 * Morgan Kaufmann. ISBN 1-55860-320-4.
147 * Note about loops and nested calls
149 * To keep this model simple, loops expressed in the framework will behave as if
150 * there was a core synchronizing instruction between loops. To see the effect
151 * of loop unrolling, manually unrolling loops is required. Note that if loops
152 * end or start with a core synchronizing instruction, the model is appropriate.
153 * Nested calls are not supported.
157 * Only Alpha has out-of-order cache bank loads. Other architectures (intel,
158 * powerpc, arm) ensure that dependent reads won't be reordered. c.f.
159 * http://www.linuxjournal.com/article/8212)
162 #define HAVE_OOO_CACHE_READ
166 * Each process have its own data in cache. Caches are randomly updated.
167 * smp_wmb and smp_rmb forces cache updates (write and read), smp_mb forces
171 typedef per_proc_byte {
175 typedef per_proc_bit {
179 /* Bitfield has a maximum of 8 procs */
180 typedef per_proc_bitfield {
184 #define DECLARE_CACHED_VAR(type, x) \
187 #define DECLARE_PROC_CACHED_VAR(type, x)\
191 #define INIT_CACHED_VAR(x, v) \
194 #define INIT_PROC_CACHED_VAR(x, v) \
195 cache_dirty_##x = 0; \
198 #define IS_CACHE_DIRTY(x, id) (cache_dirty_##x)
200 #define READ_CACHED_VAR(x) (cached_##x)
202 #define WRITE_CACHED_VAR(x, v) \
205 cache_dirty_##x = 1; \
208 #define CACHE_WRITE_TO_MEM(x, id) \
210 :: IS_CACHE_DIRTY(x, id) -> \
211 mem_##x = cached_##x; \
212 cache_dirty_##x = 0; \
217 #define CACHE_READ_FROM_MEM(x, id) \
219 :: !IS_CACHE_DIRTY(x, id) -> \
220 cached_##x = mem_##x; \
226 * May update other caches if cache is dirty, or not.
228 #define RANDOM_CACHE_WRITE_TO_MEM(x, id)\
230 :: 1 -> CACHE_WRITE_TO_MEM(x, id); \
234 #define RANDOM_CACHE_READ_FROM_MEM(x, id)\
236 :: 1 -> CACHE_READ_FROM_MEM(x, id); \
240 /* Must consume all prior read tokens. All subsequent reads depend on it. */
244 CACHE_READ_FROM_MEM(urcu_gp_ctr, get_pid());
248 CACHE_READ_FROM_MEM(urcu_active_readers[i], get_pid());
250 :: i >= NR_READERS -> break
252 CACHE_READ_FROM_MEM(rcu_ptr, get_pid());
256 CACHE_READ_FROM_MEM(rcu_data[i], get_pid());
258 :: i >= SLAB_SIZE -> break
263 /* Must consume all prior write tokens. All subsequent writes depend on it. */
267 CACHE_WRITE_TO_MEM(urcu_gp_ctr, get_pid());
271 CACHE_WRITE_TO_MEM(urcu_active_readers[i], get_pid());
273 :: i >= NR_READERS -> break
275 CACHE_WRITE_TO_MEM(rcu_ptr, get_pid());
279 CACHE_WRITE_TO_MEM(rcu_data[i], get_pid());
281 :: i >= SLAB_SIZE -> break
286 /* Synchronization point. Must consume all prior read and write tokens. All
287 * subsequent reads and writes depend on it. */
296 #ifdef REMOTE_BARRIERS
298 bit reader_barrier[NR_READERS];
301 * We cannot leave the barriers dependencies in place in REMOTE_BARRIERS mode
302 * because they would add unexisting core synchronization and would therefore
303 * create an incomplete model.
304 * Therefore, we model the read-side memory barriers by completely disabling the
305 * memory barriers and their dependencies from the read-side. One at a time
306 * (different verification runs), we make a different instruction listen for
310 #define smp_mb_reader(i, j)
313 * Service 0, 1 or many barrier requests.
315 inline smp_mb_recv(i, j)
318 :: (reader_barrier[get_readerid()] == 1) ->
320 * We choose to ignore cycles caused by writer busy-looping,
321 * waiting for the reader, sending barrier requests, and the
322 * reader always services them without continuing execution.
324 progress_ignoring_mb1:
326 reader_barrier[get_readerid()] = 0;
329 * We choose to ignore writer's non-progress caused by the
330 * reader ignoring the writer's mb() requests.
332 progress_ignoring_mb2:
337 #define PROGRESS_LABEL(progressid) progress_writer_progid_##progressid:
339 #define smp_mb_send(i, j, progressid) \
344 :: i < NR_READERS -> \
345 reader_barrier[i] = 1; \
347 * Busy-looping waiting for reader barrier handling is of little\
348 * interest, given the reader has the ability to totally ignore \
349 * barrier requests. \
352 :: (reader_barrier[i] == 1) -> \
353 PROGRESS_LABEL(progressid) \
355 :: (reader_barrier[i] == 0) -> break; \
358 :: i >= NR_READERS -> \
366 #define smp_mb_send(i, j, progressid) smp_mb(i)
367 #define smp_mb_reader(i, j) smp_mb(i)
368 #define smp_mb_recv(i, j)
372 /* Keep in sync manually with smp_rmb, smp_wmb, ooo_mem and init() */
373 DECLARE_CACHED_VAR(byte, urcu_gp_ctr);
374 /* Note ! currently only one reader */
375 DECLARE_CACHED_VAR(byte, urcu_active_readers[NR_READERS]);
377 DECLARE_CACHED_VAR(bit, rcu_data[SLAB_SIZE]);
381 DECLARE_CACHED_VAR(bit, rcu_ptr);
382 bit ptr_read_first[NR_READERS];
383 bit ptr_read_second[NR_READERS];
385 DECLARE_CACHED_VAR(byte, rcu_ptr);
386 byte ptr_read_first[NR_READERS];
387 byte ptr_read_second[NR_READERS];
390 bit data_read_first[NR_READERS];
391 bit data_read_second[NR_READERS];
395 inline wait_init_done()
398 :: init_done == 0 -> skip;
406 RANDOM_CACHE_WRITE_TO_MEM(urcu_gp_ctr, get_pid());
410 RANDOM_CACHE_WRITE_TO_MEM(urcu_active_readers[i],
413 :: i >= NR_READERS -> break
415 RANDOM_CACHE_WRITE_TO_MEM(rcu_ptr, get_pid());
419 RANDOM_CACHE_WRITE_TO_MEM(rcu_data[i], get_pid());
421 :: i >= SLAB_SIZE -> break
423 #ifdef HAVE_OOO_CACHE_READ
424 RANDOM_CACHE_READ_FROM_MEM(urcu_gp_ctr, get_pid());
428 RANDOM_CACHE_READ_FROM_MEM(urcu_active_readers[i],
431 :: i >= NR_READERS -> break
433 RANDOM_CACHE_READ_FROM_MEM(rcu_ptr, get_pid());
437 RANDOM_CACHE_READ_FROM_MEM(rcu_data[i], get_pid());
439 :: i >= SLAB_SIZE -> break
443 #endif /* HAVE_OOO_CACHE_READ */
448 * Bit encoding, urcu_reader :
451 int _proc_urcu_reader;
452 #define proc_urcu_reader _proc_urcu_reader
454 /* Body of PROCEDURE_READ_LOCK */
455 #define READ_PROD_A_READ (1 << 0)
456 #define READ_PROD_B_IF_TRUE (1 << 1)
457 #define READ_PROD_B_IF_FALSE (1 << 2)
458 #define READ_PROD_C_IF_TRUE_READ (1 << 3)
460 #define PROCEDURE_READ_LOCK(base, consumetoken, consumetoken2, producetoken) \
461 :: CONSUME_TOKENS(proc_urcu_reader, (consumetoken | consumetoken2), READ_PROD_A_READ << base) -> \
463 tmp = READ_CACHED_VAR(urcu_active_readers[get_readerid()]); \
464 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_A_READ << base); \
465 :: CONSUME_TOKENS(proc_urcu_reader, \
466 READ_PROD_A_READ << base, /* RAW, pre-dominant */ \
467 (READ_PROD_B_IF_TRUE | READ_PROD_B_IF_FALSE) << base) -> \
469 :: (!(tmp & RCU_GP_CTR_NEST_MASK)) -> \
470 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_B_IF_TRUE << base); \
472 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_B_IF_FALSE << base); \
475 :: CONSUME_TOKENS(proc_urcu_reader, consumetoken, /* prefetch */ \
476 READ_PROD_C_IF_TRUE_READ << base) -> \
478 tmp2 = READ_CACHED_VAR(urcu_gp_ctr); \
479 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_C_IF_TRUE_READ << base); \
480 :: CONSUME_TOKENS(proc_urcu_reader, \
481 (READ_PROD_B_IF_TRUE \
482 | READ_PROD_C_IF_TRUE_READ /* pre-dominant */ \
483 | READ_PROD_A_READ) << base, /* WAR */ \
486 WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp2); \
487 PRODUCE_TOKENS(proc_urcu_reader, producetoken); \
488 /* IF_MERGE implies \
489 * post-dominance */ \
491 :: CONSUME_TOKENS(proc_urcu_reader, \
492 (READ_PROD_B_IF_FALSE /* pre-dominant */ \
493 | READ_PROD_A_READ) << base, /* WAR */ \
496 WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], \
498 PRODUCE_TOKENS(proc_urcu_reader, producetoken); \
499 /* IF_MERGE implies \
500 * post-dominance */ \
504 /* Body of PROCEDURE_READ_LOCK */
505 #define READ_PROC_READ_UNLOCK (1 << 0)
507 #define PROCEDURE_READ_UNLOCK(base, consumetoken, producetoken) \
508 :: CONSUME_TOKENS(proc_urcu_reader, \
510 READ_PROC_READ_UNLOCK << base) -> \
512 tmp = READ_CACHED_VAR(urcu_active_readers[get_readerid()]); \
513 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_READ_UNLOCK << base); \
514 :: CONSUME_TOKENS(proc_urcu_reader, \
516 | (READ_PROC_READ_UNLOCK << base), /* WAR */ \
519 WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp - 1); \
520 PRODUCE_TOKENS(proc_urcu_reader, producetoken); \
524 #define READ_PROD_NONE (1 << 0)
526 /* PROCEDURE_READ_LOCK base = << 1 : 1 to 5 */
527 #define READ_LOCK_BASE 1
528 #define READ_LOCK_OUT (1 << 5)
530 #define READ_PROC_FIRST_MB (1 << 6)
532 /* PROCEDURE_READ_LOCK (NESTED) base : << 7 : 7 to 11 */
533 #define READ_LOCK_NESTED_BASE 7
534 #define READ_LOCK_NESTED_OUT (1 << 11)
536 #define READ_PROC_READ_GEN (1 << 12)
537 #define READ_PROC_ACCESS_GEN (1 << 13)
539 /* PROCEDURE_READ_UNLOCK (NESTED) base = << 14 : 14 to 15 */
540 #define READ_UNLOCK_NESTED_BASE 14
541 #define READ_UNLOCK_NESTED_OUT (1 << 15)
543 #define READ_PROC_SECOND_MB (1 << 16)
545 /* PROCEDURE_READ_UNLOCK base = << 17 : 17 to 18 */
546 #define READ_UNLOCK_BASE 17
547 #define READ_UNLOCK_OUT (1 << 18)
549 /* PROCEDURE_READ_LOCK_UNROLL base = << 19 : 19 to 23 */
550 #define READ_LOCK_UNROLL_BASE 19
551 #define READ_LOCK_OUT_UNROLL (1 << 23)
553 #define READ_PROC_THIRD_MB (1 << 24)
555 #define READ_PROC_READ_GEN_UNROLL (1 << 25)
556 #define READ_PROC_ACCESS_GEN_UNROLL (1 << 26)
558 #define READ_PROC_FOURTH_MB (1 << 27)
560 /* PROCEDURE_READ_UNLOCK_UNROLL base = << 28 : 28 to 29 */
561 #define READ_UNLOCK_UNROLL_BASE 28
562 #define READ_UNLOCK_OUT_UNROLL (1 << 29)
565 /* Should not include branches */
566 #define READ_PROC_ALL_TOKENS (READ_PROD_NONE \
568 | READ_PROC_FIRST_MB \
569 | READ_LOCK_NESTED_OUT \
570 | READ_PROC_READ_GEN \
571 | READ_PROC_ACCESS_GEN \
572 | READ_UNLOCK_NESTED_OUT \
573 | READ_PROC_SECOND_MB \
575 | READ_LOCK_OUT_UNROLL \
576 | READ_PROC_THIRD_MB \
577 | READ_PROC_READ_GEN_UNROLL \
578 | READ_PROC_ACCESS_GEN_UNROLL \
579 | READ_PROC_FOURTH_MB \
580 | READ_UNLOCK_OUT_UNROLL)
582 /* Must clear all tokens, including branches */
583 #define READ_PROC_ALL_TOKENS_CLEAR ((1 << 30) - 1)
585 inline urcu_one_read(i, j, nest_i, tmp, tmp2)
587 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_NONE);
590 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FIRST_MB);
591 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_SECOND_MB);
592 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_THIRD_MB);
593 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FOURTH_MB);
596 #ifdef REMOTE_BARRIERS
597 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FIRST_MB);
598 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_SECOND_MB);
599 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_THIRD_MB);
600 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FOURTH_MB);
606 #ifdef REMOTE_BARRIERS
608 * Signal-based memory barrier will only execute when the
609 * execution order appears in program order.
615 :: CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE,
616 READ_LOCK_OUT | READ_LOCK_NESTED_OUT
617 | READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
619 | READ_LOCK_OUT_UNROLL
620 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
621 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_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 | READ_LOCK_NESTED_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
633 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN,
634 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 | READ_PROC_ACCESS_GEN,
640 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
646 | READ_PROC_ACCESS_GEN | 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
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
659 | READ_UNLOCK_OUT | READ_LOCK_OUT_UNROLL,
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,
666 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 | READ_PROC_ACCESS_GEN_UNROLL,
672 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 | 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 | READ_UNLOCK_OUT_UNROLL,
685 goto non_atomic3_skip;
688 goto non_atomic3_end;
691 #endif /* REMOTE_BARRIERS */
695 PROCEDURE_READ_LOCK(READ_LOCK_BASE, READ_PROD_NONE, 0, READ_LOCK_OUT);
697 :: CONSUME_TOKENS(proc_urcu_reader,
698 READ_LOCK_OUT, /* post-dominant */
699 READ_PROC_FIRST_MB) ->
701 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FIRST_MB);
703 PROCEDURE_READ_LOCK(READ_LOCK_NESTED_BASE, READ_PROC_FIRST_MB, READ_LOCK_OUT,
704 READ_LOCK_NESTED_OUT);
706 :: CONSUME_TOKENS(proc_urcu_reader,
707 READ_PROC_FIRST_MB, /* mb() orders reads */
708 READ_PROC_READ_GEN) ->
710 ptr_read_first[get_readerid()] = READ_CACHED_VAR(rcu_ptr);
711 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_READ_GEN);
713 :: CONSUME_TOKENS(proc_urcu_reader,
714 READ_PROC_FIRST_MB /* mb() orders reads */
715 | READ_PROC_READ_GEN,
716 READ_PROC_ACCESS_GEN) ->
717 /* smp_read_barrier_depends */
720 data_read_first[get_readerid()] =
721 READ_CACHED_VAR(rcu_data[ptr_read_first[get_readerid()]]);
722 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_ACCESS_GEN);
725 /* Note : we remove the nested memory barrier from the read unlock
726 * model, given it is not usually needed. The implementation has the barrier
727 * because the performance impact added by a branch in the common case does not
731 PROCEDURE_READ_UNLOCK(READ_UNLOCK_NESTED_BASE,
734 | READ_LOCK_NESTED_OUT,
735 READ_UNLOCK_NESTED_OUT);
738 :: CONSUME_TOKENS(proc_urcu_reader,
739 READ_PROC_ACCESS_GEN /* mb() orders reads */
740 | READ_PROC_READ_GEN /* mb() orders reads */
741 | READ_PROC_FIRST_MB /* mb() ordered */
742 | READ_LOCK_OUT /* post-dominant */
743 | READ_LOCK_NESTED_OUT /* post-dominant */
744 | READ_UNLOCK_NESTED_OUT,
745 READ_PROC_SECOND_MB) ->
747 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_SECOND_MB);
749 PROCEDURE_READ_UNLOCK(READ_UNLOCK_BASE,
750 READ_PROC_SECOND_MB /* mb() orders reads */
751 | READ_PROC_FIRST_MB /* mb() orders reads */
752 | READ_LOCK_NESTED_OUT /* RAW */
753 | READ_LOCK_OUT /* RAW */
754 | READ_UNLOCK_NESTED_OUT, /* RAW */
757 /* Unrolling loop : second consecutive lock */
758 /* reading urcu_active_readers, which have been written by
759 * READ_UNLOCK_OUT : RAW */
760 PROCEDURE_READ_LOCK(READ_LOCK_UNROLL_BASE,
761 READ_PROC_SECOND_MB /* mb() orders reads */
762 | READ_PROC_FIRST_MB, /* mb() orders reads */
763 READ_LOCK_NESTED_OUT /* RAW */
764 | READ_LOCK_OUT /* RAW */
765 | READ_UNLOCK_NESTED_OUT /* RAW */
766 | READ_UNLOCK_OUT, /* RAW */
767 READ_LOCK_OUT_UNROLL);
770 :: CONSUME_TOKENS(proc_urcu_reader,
771 READ_PROC_FIRST_MB /* mb() ordered */
772 | READ_PROC_SECOND_MB /* mb() ordered */
773 | READ_LOCK_OUT_UNROLL /* post-dominant */
774 | READ_LOCK_NESTED_OUT
776 | READ_UNLOCK_NESTED_OUT
778 READ_PROC_THIRD_MB) ->
780 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_THIRD_MB);
782 :: CONSUME_TOKENS(proc_urcu_reader,
783 READ_PROC_FIRST_MB /* mb() orders reads */
784 | READ_PROC_SECOND_MB /* mb() orders reads */
785 | READ_PROC_THIRD_MB, /* mb() orders reads */
786 READ_PROC_READ_GEN_UNROLL) ->
788 ptr_read_second[get_readerid()] = READ_CACHED_VAR(rcu_ptr);
789 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_READ_GEN_UNROLL);
791 :: CONSUME_TOKENS(proc_urcu_reader,
792 READ_PROC_READ_GEN_UNROLL
793 | READ_PROC_FIRST_MB /* mb() orders reads */
794 | READ_PROC_SECOND_MB /* mb() orders reads */
795 | READ_PROC_THIRD_MB, /* mb() orders reads */
796 READ_PROC_ACCESS_GEN_UNROLL) ->
797 /* smp_read_barrier_depends */
800 data_read_second[get_readerid()] =
801 READ_CACHED_VAR(rcu_data[ptr_read_second[get_readerid()]]);
802 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_ACCESS_GEN_UNROLL);
804 :: CONSUME_TOKENS(proc_urcu_reader,
805 READ_PROC_READ_GEN_UNROLL /* mb() orders reads */
806 | READ_PROC_ACCESS_GEN_UNROLL /* mb() orders reads */
807 | READ_PROC_FIRST_MB /* mb() ordered */
808 | READ_PROC_SECOND_MB /* mb() ordered */
809 | READ_PROC_THIRD_MB /* mb() ordered */
810 | READ_LOCK_OUT_UNROLL /* post-dominant */
811 | READ_LOCK_NESTED_OUT
813 | READ_UNLOCK_NESTED_OUT
815 READ_PROC_FOURTH_MB) ->
817 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FOURTH_MB);
819 PROCEDURE_READ_UNLOCK(READ_UNLOCK_UNROLL_BASE,
820 READ_PROC_FOURTH_MB /* mb() orders reads */
821 | READ_PROC_THIRD_MB /* mb() orders reads */
822 | READ_LOCK_OUT_UNROLL /* RAW */
823 | READ_PROC_SECOND_MB /* mb() orders reads */
824 | READ_PROC_FIRST_MB /* mb() orders reads */
825 | READ_LOCK_NESTED_OUT /* RAW */
826 | READ_LOCK_OUT /* RAW */
827 | READ_UNLOCK_NESTED_OUT, /* RAW */
828 READ_UNLOCK_OUT_UNROLL);
829 :: CONSUME_TOKENS(proc_urcu_reader, READ_PROC_ALL_TOKENS, 0) ->
830 CLEAR_TOKENS(proc_urcu_reader, READ_PROC_ALL_TOKENS_CLEAR);
836 * Dependency between consecutive loops :
838 * WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp2 - 1)
839 * tmp = READ_CACHED_VAR(urcu_active_readers[get_readerid()]);
841 * _WHEN THE MB()s are in place_, they add full ordering of the
842 * generation pointer read wrt active reader count read, which ensures
843 * execution will not spill across loop execution.
844 * However, in the event mb()s are removed (execution using signal
845 * handler to promote barrier()() -> smp_mb()), nothing prevents one loop
846 * to spill its execution on other loop's execution.
869 active proctype urcu_reader()
874 /* Keep in sync manually with smp_rmb, smp_wmb, ooo_mem and init() */
875 DECLARE_PROC_CACHED_VAR(byte, urcu_gp_ctr);
876 /* Note ! currently only one reader */
877 DECLARE_PROC_CACHED_VAR(byte, urcu_active_readers[NR_READERS]);
879 DECLARE_PROC_CACHED_VAR(bit, rcu_data[SLAB_SIZE]);
883 DECLARE_PROC_CACHED_VAR(bit, rcu_ptr);
885 DECLARE_PROC_CACHED_VAR(byte, rcu_ptr);
889 INIT_PROC_CACHED_VAR(urcu_gp_ctr, 1);
890 INIT_PROC_CACHED_VAR(rcu_ptr, 0);
895 INIT_PROC_CACHED_VAR(urcu_active_readers[i], 0);
897 :: i >= NR_READERS -> break
899 INIT_PROC_CACHED_VAR(rcu_data[0], WINE);
903 INIT_PROC_CACHED_VAR(rcu_data[i], POISON);
905 :: i >= SLAB_SIZE -> break
911 assert(get_pid() < NR_PROCS);
917 * We do not test reader's progress here, because we are mainly
918 * interested in writer's progress. The reader never blocks
919 * anyway. We have to test for reader/writer's progress
920 * separately, otherwise we could think the writer is doing
921 * progress when it's blocked by an always progressing reader.
923 #ifdef READER_PROGRESS
926 urcu_one_read(i, j, nest_i, tmp, tmp2);
930 /* no name clash please */
931 #undef proc_urcu_reader
934 /* Model the RCU update process. */
937 * Bit encoding, urcu_writer :
938 * Currently only supports one reader.
941 int _proc_urcu_writer;
942 #define proc_urcu_writer _proc_urcu_writer
944 #define WRITE_PROD_NONE (1 << 0)
946 #define WRITE_DATA (1 << 1)
947 #define WRITE_PROC_WMB (1 << 2)
948 #define WRITE_XCHG_PTR (1 << 3)
950 #define WRITE_PROC_FIRST_MB (1 << 4)
953 #define WRITE_PROC_FIRST_READ_GP (1 << 5)
954 #define WRITE_PROC_FIRST_WRITE_GP (1 << 6)
955 #define WRITE_PROC_FIRST_WAIT (1 << 7)
956 #define WRITE_PROC_FIRST_WAIT_LOOP (1 << 8)
959 #define WRITE_PROC_SECOND_READ_GP (1 << 9)
960 #define WRITE_PROC_SECOND_WRITE_GP (1 << 10)
961 #define WRITE_PROC_SECOND_WAIT (1 << 11)
962 #define WRITE_PROC_SECOND_WAIT_LOOP (1 << 12)
964 #define WRITE_PROC_SECOND_MB (1 << 13)
966 #define WRITE_FREE (1 << 14)
968 #define WRITE_PROC_ALL_TOKENS (WRITE_PROD_NONE \
972 | WRITE_PROC_FIRST_MB \
973 | WRITE_PROC_FIRST_READ_GP \
974 | WRITE_PROC_FIRST_WRITE_GP \
975 | WRITE_PROC_FIRST_WAIT \
976 | WRITE_PROC_SECOND_READ_GP \
977 | WRITE_PROC_SECOND_WRITE_GP \
978 | WRITE_PROC_SECOND_WAIT \
979 | WRITE_PROC_SECOND_MB \
982 #define WRITE_PROC_ALL_TOKENS_CLEAR ((1 << 15) - 1)
985 * Mutexes are implied around writer execution. A single writer at a time.
987 active proctype urcu_writer()
990 byte tmp, tmp2, tmpa;
991 byte cur_data = 0, old_data, loop_nr = 0;
992 byte cur_gp_val = 0; /*
993 * Keep a local trace of the current parity so
994 * we don't add non-existing dependencies on the global
995 * GP update. Needed to test single flip case.
998 /* Keep in sync manually with smp_rmb, smp_wmb, ooo_mem and init() */
999 DECLARE_PROC_CACHED_VAR(byte, urcu_gp_ctr);
1000 /* Note ! currently only one reader */
1001 DECLARE_PROC_CACHED_VAR(byte, urcu_active_readers[NR_READERS]);
1003 DECLARE_PROC_CACHED_VAR(bit, rcu_data[SLAB_SIZE]);
1006 #if (SLAB_SIZE == 2)
1007 DECLARE_PROC_CACHED_VAR(bit, rcu_ptr);
1009 DECLARE_PROC_CACHED_VAR(byte, rcu_ptr);
1013 INIT_PROC_CACHED_VAR(urcu_gp_ctr, 1);
1014 INIT_PROC_CACHED_VAR(rcu_ptr, 0);
1018 :: i < NR_READERS ->
1019 INIT_PROC_CACHED_VAR(urcu_active_readers[i], 0);
1021 :: i >= NR_READERS -> break
1023 INIT_PROC_CACHED_VAR(rcu_data[0], WINE);
1027 INIT_PROC_CACHED_VAR(rcu_data[i], POISON);
1029 :: i >= SLAB_SIZE -> break
1036 assert(get_pid() < NR_PROCS);
1040 #ifdef WRITER_PROGRESS
1043 loop_nr = loop_nr + 1;
1045 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROD_NONE);
1048 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_WMB);
1052 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_MB);
1053 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_MB);
1057 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_READ_GP);
1058 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WRITE_GP);
1059 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT);
1060 /* For single flip, we need to know the current parity */
1061 cur_gp_val = cur_gp_val ^ RCU_GP_CTR_BIT;
1068 :: CONSUME_TOKENS(proc_urcu_writer,
1072 cur_data = (cur_data + 1) % SLAB_SIZE;
1073 WRITE_CACHED_VAR(rcu_data[cur_data], WINE);
1074 PRODUCE_TOKENS(proc_urcu_writer, WRITE_DATA);
1077 :: CONSUME_TOKENS(proc_urcu_writer,
1081 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_WMB);
1083 :: CONSUME_TOKENS(proc_urcu_writer,
1086 /* rcu_xchg_pointer() */
1088 old_data = READ_CACHED_VAR(rcu_ptr);
1089 WRITE_CACHED_VAR(rcu_ptr, cur_data);
1091 PRODUCE_TOKENS(proc_urcu_writer, WRITE_XCHG_PTR);
1093 :: CONSUME_TOKENS(proc_urcu_writer,
1094 WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR,
1095 WRITE_PROC_FIRST_MB) ->
1098 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_MB);
1101 :: CONSUME_TOKENS(proc_urcu_writer,
1102 WRITE_PROC_FIRST_MB,
1103 WRITE_PROC_FIRST_READ_GP) ->
1104 tmpa = READ_CACHED_VAR(urcu_gp_ctr);
1105 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_READ_GP);
1106 :: CONSUME_TOKENS(proc_urcu_writer,
1107 WRITE_PROC_FIRST_MB | WRITE_PROC_WMB
1108 | WRITE_PROC_FIRST_READ_GP,
1109 WRITE_PROC_FIRST_WRITE_GP) ->
1111 WRITE_CACHED_VAR(urcu_gp_ctr, tmpa ^ RCU_GP_CTR_BIT);
1112 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WRITE_GP);
1114 :: CONSUME_TOKENS(proc_urcu_writer,
1115 //WRITE_PROC_FIRST_WRITE_GP | /* TEST ADDING SYNC CORE */
1116 WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
1117 WRITE_PROC_FIRST_WAIT | WRITE_PROC_FIRST_WAIT_LOOP) ->
1119 //smp_mb(i); /* TEST */
1120 /* ONLY WAITING FOR READER 0 */
1121 tmp2 = READ_CACHED_VAR(urcu_active_readers[0]);
1123 /* In normal execution, we are always starting by
1124 * waiting for the even parity.
1126 cur_gp_val = RCU_GP_CTR_BIT;
1129 :: (tmp2 & RCU_GP_CTR_NEST_MASK)
1130 && ((tmp2 ^ cur_gp_val) & RCU_GP_CTR_BIT) ->
1131 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WAIT_LOOP);
1133 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WAIT);
1136 :: CONSUME_TOKENS(proc_urcu_writer,
1137 //WRITE_PROC_FIRST_WRITE_GP /* TEST ADDING SYNC CORE */
1138 WRITE_PROC_FIRST_WRITE_GP
1139 | WRITE_PROC_FIRST_READ_GP
1140 | WRITE_PROC_FIRST_WAIT_LOOP
1141 | WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR
1142 | WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
1144 #ifndef GEN_ERROR_WRITER_PROGRESS
1147 /* The memory barrier will invalidate the
1148 * second read done as prefetching. Note that all
1149 * instructions with side-effects depending on
1150 * WRITE_PROC_SECOND_READ_GP should also depend on
1151 * completion of this busy-waiting loop. */
1152 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_READ_GP);
1156 /* This instruction loops to WRITE_PROC_FIRST_WAIT */
1157 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WAIT_LOOP | WRITE_PROC_FIRST_WAIT);
1160 :: CONSUME_TOKENS(proc_urcu_writer,
1161 //WRITE_PROC_FIRST_WAIT | //test /* no dependency. Could pre-fetch, no side-effect. */
1162 WRITE_PROC_FIRST_WRITE_GP
1163 | WRITE_PROC_FIRST_READ_GP
1164 | WRITE_PROC_FIRST_MB,
1165 WRITE_PROC_SECOND_READ_GP) ->
1167 //smp_mb(i); /* TEST */
1168 tmpa = READ_CACHED_VAR(urcu_gp_ctr);
1169 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_READ_GP);
1170 :: CONSUME_TOKENS(proc_urcu_writer,
1171 WRITE_PROC_FIRST_WAIT /* dependency on first wait, because this
1172 * instruction has globally observable
1175 | WRITE_PROC_FIRST_MB
1177 | WRITE_PROC_FIRST_READ_GP
1178 | WRITE_PROC_FIRST_WRITE_GP
1179 | WRITE_PROC_SECOND_READ_GP,
1180 WRITE_PROC_SECOND_WRITE_GP) ->
1182 WRITE_CACHED_VAR(urcu_gp_ctr, tmpa ^ RCU_GP_CTR_BIT);
1183 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WRITE_GP);
1185 :: CONSUME_TOKENS(proc_urcu_writer,
1186 //WRITE_PROC_FIRST_WRITE_GP | /* TEST ADDING SYNC CORE */
1187 WRITE_PROC_FIRST_WAIT
1188 | WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
1189 WRITE_PROC_SECOND_WAIT | WRITE_PROC_SECOND_WAIT_LOOP) ->
1191 //smp_mb(i); /* TEST */
1192 /* ONLY WAITING FOR READER 0 */
1193 tmp2 = READ_CACHED_VAR(urcu_active_readers[0]);
1195 :: (tmp2 & RCU_GP_CTR_NEST_MASK)
1196 && ((tmp2 ^ 0) & RCU_GP_CTR_BIT) ->
1197 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT_LOOP);
1199 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT);
1202 :: CONSUME_TOKENS(proc_urcu_writer,
1203 //WRITE_PROC_FIRST_WRITE_GP | /* TEST ADDING SYNC CORE */
1204 WRITE_PROC_SECOND_WRITE_GP
1205 | WRITE_PROC_FIRST_WRITE_GP
1206 | WRITE_PROC_SECOND_READ_GP
1207 | WRITE_PROC_FIRST_READ_GP
1208 | WRITE_PROC_SECOND_WAIT_LOOP
1209 | WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR
1210 | WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
1212 #ifndef GEN_ERROR_WRITER_PROGRESS
1218 /* This instruction loops to WRITE_PROC_SECOND_WAIT */
1219 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT_LOOP | WRITE_PROC_SECOND_WAIT);
1222 :: CONSUME_TOKENS(proc_urcu_writer,
1223 WRITE_PROC_FIRST_WAIT
1224 | WRITE_PROC_SECOND_WAIT
1225 | WRITE_PROC_FIRST_READ_GP
1226 | WRITE_PROC_SECOND_READ_GP
1227 | WRITE_PROC_FIRST_WRITE_GP
1228 | WRITE_PROC_SECOND_WRITE_GP
1229 | WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR
1230 | WRITE_PROC_FIRST_MB,
1231 WRITE_PROC_SECOND_MB) ->
1234 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_MB);
1236 :: CONSUME_TOKENS(proc_urcu_writer,
1238 | WRITE_PROC_FIRST_WAIT
1239 | WRITE_PROC_SECOND_WAIT
1240 | WRITE_PROC_WMB /* No dependency on
1241 * WRITE_DATA because we
1243 * different location. */
1244 | WRITE_PROC_SECOND_MB
1245 | WRITE_PROC_FIRST_MB,
1247 WRITE_CACHED_VAR(rcu_data[old_data], POISON);
1248 PRODUCE_TOKENS(proc_urcu_writer, WRITE_FREE);
1250 :: CONSUME_TOKENS(proc_urcu_writer, WRITE_PROC_ALL_TOKENS, 0) ->
1251 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_ALL_TOKENS_CLEAR);
1257 * Note : Promela model adds implicit serialization of the
1258 * WRITE_FREE instruction. Normally, it would be permitted to
1259 * spill on the next loop execution. Given the validation we do
1260 * checks for the data entry read to be poisoned, it's ok if
1261 * we do not check "late arriving" memory poisoning.
1266 * Given the reader loops infinitely, let the writer also busy-loop
1267 * with progress here so, with weak fairness, we can test the
1268 * writer's progress.
1273 #ifdef WRITER_PROGRESS
1276 #ifdef READER_PROGRESS
1278 * Make sure we don't block the reader's progress.
1280 smp_mb_send(i, j, 5);
1285 /* Non-atomic parts of the loop */
1288 smp_mb_send(i, j, 1);
1289 goto smp_mb_send1_end;
1290 #ifndef GEN_ERROR_WRITER_PROGRESS
1292 smp_mb_send(i, j, 2);
1293 goto smp_mb_send2_end;
1295 smp_mb_send(i, j, 3);
1296 goto smp_mb_send3_end;
1299 smp_mb_send(i, j, 4);
1300 goto smp_mb_send4_end;
1305 /* no name clash please */
1306 #undef proc_urcu_writer
1309 /* Leave after the readers and writers so the pid count is ok. */
1314 INIT_CACHED_VAR(urcu_gp_ctr, 1);
1315 INIT_CACHED_VAR(rcu_ptr, 0);
1319 :: i < NR_READERS ->
1320 INIT_CACHED_VAR(urcu_active_readers[i], 0);
1321 ptr_read_first[i] = 1;
1322 ptr_read_second[i] = 1;
1323 data_read_first[i] = WINE;
1324 data_read_second[i] = WINE;
1326 :: i >= NR_READERS -> break
1328 INIT_CACHED_VAR(rcu_data[0], WINE);
1332 INIT_CACHED_VAR(rcu_data[i], POISON);
1334 :: i >= SLAB_SIZE -> break