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) \
189 #define DECLARE_PROC_CACHED_VAR(type, x)\
193 #define INIT_CACHED_VAR(x, v) \
196 #define INIT_PROC_CACHED_VAR(x, v) \
197 cache_dirty_##x = 0; \
200 #define IS_CACHE_DIRTY(x, id) (cache_dirty_##x)
202 #define READ_CACHED_VAR(x) (cached_##x)
204 #define WRITE_CACHED_VAR(x, v) \
207 cache_dirty_##x = 1; \
210 #define CACHE_WRITE_TO_MEM(x, id) \
212 :: IS_CACHE_DIRTY(x, id) -> \
213 mem_##x = cached_##x; \
214 cache_dirty_##x = 0; \
219 #define CACHE_READ_FROM_MEM(x, id) \
221 :: !IS_CACHE_DIRTY(x, id) -> \
222 cached_##x = mem_##x; \
228 * May update other caches if cache is dirty, or not.
230 #define RANDOM_CACHE_WRITE_TO_MEM(x, id)\
232 :: 1 -> CACHE_WRITE_TO_MEM(x, id); \
236 #define RANDOM_CACHE_READ_FROM_MEM(x, id)\
238 :: 1 -> CACHE_READ_FROM_MEM(x, id); \
242 /* Must consume all prior read tokens. All subsequent reads depend on it. */
246 CACHE_READ_FROM_MEM(urcu_gp_ctr, get_pid());
250 CACHE_READ_FROM_MEM(urcu_active_readers[i], get_pid());
252 :: i >= NR_READERS -> break
254 CACHE_READ_FROM_MEM(rcu_ptr, get_pid());
258 CACHE_READ_FROM_MEM(rcu_data[i], get_pid());
260 :: i >= SLAB_SIZE -> break
265 /* Must consume all prior write tokens. All subsequent writes depend on it. */
269 CACHE_WRITE_TO_MEM(urcu_gp_ctr, get_pid());
273 CACHE_WRITE_TO_MEM(urcu_active_readers[i], get_pid());
275 :: i >= NR_READERS -> break
277 CACHE_WRITE_TO_MEM(rcu_ptr, get_pid());
281 CACHE_WRITE_TO_MEM(rcu_data[i], get_pid());
283 :: i >= SLAB_SIZE -> break
288 /* Synchronization point. Must consume all prior read and write tokens. All
289 * subsequent reads and writes depend on it. */
298 #ifdef REMOTE_BARRIERS
300 bit reader_barrier[NR_READERS];
303 * We cannot leave the barriers dependencies in place in REMOTE_BARRIERS mode
304 * because they would add unexisting core synchronization and would therefore
305 * create an incomplete model.
306 * Therefore, we model the read-side memory barriers by completely disabling the
307 * memory barriers and their dependencies from the read-side. One at a time
308 * (different verification runs), we make a different instruction listen for
312 #define smp_mb_reader(i, j)
315 * Service 0, 1 or many barrier requests.
317 inline smp_mb_recv(i, j)
320 :: (reader_barrier[get_readerid()] == 1) ->
322 * We choose to ignore cycles caused by writer busy-looping,
323 * waiting for the reader, sending barrier requests, and the
324 * reader always services them without continuing execution.
326 progress_ignoring_mb1:
328 reader_barrier[get_readerid()] = 0;
331 * We choose to ignore writer's non-progress caused by the
332 * reader ignoring the writer's mb() requests.
334 progress_ignoring_mb2:
339 #define PROGRESS_LABEL(progressid) progress_writer_progid_##progressid:
341 #define smp_mb_send(i, j, progressid) \
346 :: i < NR_READERS -> \
347 reader_barrier[i] = 1; \
349 * Busy-looping waiting for reader barrier handling is of little\
350 * interest, given the reader has the ability to totally ignore \
351 * barrier requests. \
354 :: (reader_barrier[i] == 1) -> \
355 PROGRESS_LABEL(progressid) \
357 :: (reader_barrier[i] == 0) -> break; \
360 :: i >= NR_READERS -> \
368 #define smp_mb_send(i, j, progressid) smp_mb(i)
369 #define smp_mb_reader(i, j) smp_mb(i)
370 #define smp_mb_recv(i, j)
374 /* Keep in sync manually with smp_rmb, smp_wmb, ooo_mem and init() */
375 DECLARE_CACHED_VAR(byte, urcu_gp_ctr);
376 /* Note ! currently only one reader */
377 DECLARE_CACHED_VAR(byte, urcu_active_readers[NR_READERS]);
379 DECLARE_CACHED_VAR(bit, rcu_data[SLAB_SIZE]);
383 DECLARE_CACHED_VAR(bit, rcu_ptr);
384 bit ptr_read_first[NR_READERS];
385 bit ptr_read_second[NR_READERS];
387 DECLARE_CACHED_VAR(byte, rcu_ptr);
388 byte ptr_read_first[NR_READERS];
389 byte ptr_read_second[NR_READERS];
392 bit data_read_first[NR_READERS];
393 bit data_read_second[NR_READERS];
397 inline wait_init_done()
400 :: init_done == 0 -> skip;
408 RANDOM_CACHE_WRITE_TO_MEM(urcu_gp_ctr, get_pid());
412 RANDOM_CACHE_WRITE_TO_MEM(urcu_active_readers[i],
415 :: i >= NR_READERS -> break
417 RANDOM_CACHE_WRITE_TO_MEM(rcu_ptr, get_pid());
421 RANDOM_CACHE_WRITE_TO_MEM(rcu_data[i], get_pid());
423 :: i >= SLAB_SIZE -> break
425 #ifdef HAVE_OOO_CACHE_READ
426 RANDOM_CACHE_READ_FROM_MEM(urcu_gp_ctr, get_pid());
430 RANDOM_CACHE_READ_FROM_MEM(urcu_active_readers[i],
433 :: i >= NR_READERS -> break
435 RANDOM_CACHE_READ_FROM_MEM(rcu_ptr, get_pid());
439 RANDOM_CACHE_READ_FROM_MEM(rcu_data[i], get_pid());
441 :: i >= SLAB_SIZE -> break
445 #endif /* HAVE_OOO_CACHE_READ */
450 * Bit encoding, urcu_reader :
453 int _proc_urcu_reader;
454 #define proc_urcu_reader _proc_urcu_reader
456 /* Body of PROCEDURE_READ_LOCK */
457 #define READ_PROD_A_READ (1 << 0)
458 #define READ_PROD_B_IF_TRUE (1 << 1)
459 #define READ_PROD_B_IF_FALSE (1 << 2)
460 #define READ_PROD_C_IF_TRUE_READ (1 << 3)
462 #define PROCEDURE_READ_LOCK(base, consumetoken, consumetoken2, producetoken) \
463 :: CONSUME_TOKENS(proc_urcu_reader, (consumetoken | consumetoken2), READ_PROD_A_READ << base) -> \
465 tmp = READ_CACHED_VAR(urcu_active_readers[get_readerid()]); \
466 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_A_READ << base); \
467 :: CONSUME_TOKENS(proc_urcu_reader, \
468 READ_PROD_A_READ << base, /* RAW, pre-dominant */ \
469 (READ_PROD_B_IF_TRUE | READ_PROD_B_IF_FALSE) << base) -> \
471 :: (!(tmp & RCU_GP_CTR_NEST_MASK)) -> \
472 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_B_IF_TRUE << base); \
474 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_B_IF_FALSE << base); \
477 :: CONSUME_TOKENS(proc_urcu_reader, consumetoken, /* prefetch */ \
478 READ_PROD_C_IF_TRUE_READ << base) -> \
480 tmp2 = READ_CACHED_VAR(urcu_gp_ctr); \
481 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_C_IF_TRUE_READ << base); \
482 :: CONSUME_TOKENS(proc_urcu_reader, \
483 (READ_PROD_B_IF_TRUE \
484 | READ_PROD_C_IF_TRUE_READ /* pre-dominant */ \
485 | READ_PROD_A_READ) << base, /* WAR */ \
488 WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp2); \
489 PRODUCE_TOKENS(proc_urcu_reader, producetoken); \
490 /* IF_MERGE implies \
491 * post-dominance */ \
493 :: CONSUME_TOKENS(proc_urcu_reader, \
494 (READ_PROD_B_IF_FALSE /* pre-dominant */ \
495 | READ_PROD_A_READ) << base, /* WAR */ \
498 WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], \
500 PRODUCE_TOKENS(proc_urcu_reader, producetoken); \
501 /* IF_MERGE implies \
502 * post-dominance */ \
506 /* Body of PROCEDURE_READ_LOCK */
507 #define READ_PROC_READ_UNLOCK (1 << 0)
509 #define PROCEDURE_READ_UNLOCK(base, consumetoken, producetoken) \
510 :: CONSUME_TOKENS(proc_urcu_reader, \
512 READ_PROC_READ_UNLOCK << base) -> \
514 tmp = READ_CACHED_VAR(urcu_active_readers[get_readerid()]); \
515 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_READ_UNLOCK << base); \
516 :: CONSUME_TOKENS(proc_urcu_reader, \
518 | (READ_PROC_READ_UNLOCK << base), /* WAR */ \
521 WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp - 1); \
522 PRODUCE_TOKENS(proc_urcu_reader, producetoken); \
526 #define READ_PROD_NONE (1 << 0)
528 /* PROCEDURE_READ_LOCK base = << 1 : 1 to 5 */
529 #define READ_LOCK_BASE 1
530 #define READ_LOCK_OUT (1 << 5)
532 #define READ_PROC_FIRST_MB (1 << 6)
534 /* PROCEDURE_READ_LOCK (NESTED) base : << 7 : 7 to 11 */
535 #define READ_LOCK_NESTED_BASE 7
536 #define READ_LOCK_NESTED_OUT (1 << 11)
538 #define READ_PROC_READ_GEN (1 << 12)
539 #define READ_PROC_ACCESS_GEN (1 << 13)
541 /* PROCEDURE_READ_UNLOCK (NESTED) base = << 14 : 14 to 15 */
542 #define READ_UNLOCK_NESTED_BASE 14
543 #define READ_UNLOCK_NESTED_OUT (1 << 15)
545 #define READ_PROC_SECOND_MB (1 << 16)
547 /* PROCEDURE_READ_UNLOCK base = << 17 : 17 to 18 */
548 #define READ_UNLOCK_BASE 17
549 #define READ_UNLOCK_OUT (1 << 18)
551 /* PROCEDURE_READ_LOCK_UNROLL base = << 19 : 19 to 23 */
552 #define READ_LOCK_UNROLL_BASE 19
553 #define READ_LOCK_OUT_UNROLL (1 << 23)
555 #define READ_PROC_THIRD_MB (1 << 24)
557 #define READ_PROC_READ_GEN_UNROLL (1 << 25)
558 #define READ_PROC_ACCESS_GEN_UNROLL (1 << 26)
560 #define READ_PROC_FOURTH_MB (1 << 27)
562 /* PROCEDURE_READ_UNLOCK_UNROLL base = << 28 : 28 to 29 */
563 #define READ_UNLOCK_UNROLL_BASE 28
564 #define READ_UNLOCK_OUT_UNROLL (1 << 29)
567 /* Should not include branches */
568 #define READ_PROC_ALL_TOKENS (READ_PROD_NONE \
570 | READ_PROC_FIRST_MB \
571 | READ_LOCK_NESTED_OUT \
572 | READ_PROC_READ_GEN \
573 | READ_PROC_ACCESS_GEN \
574 | READ_UNLOCK_NESTED_OUT \
575 | READ_PROC_SECOND_MB \
577 | READ_LOCK_OUT_UNROLL \
578 | READ_PROC_THIRD_MB \
579 | READ_PROC_READ_GEN_UNROLL \
580 | READ_PROC_ACCESS_GEN_UNROLL \
581 | READ_PROC_FOURTH_MB \
582 | READ_UNLOCK_OUT_UNROLL)
584 /* Must clear all tokens, including branches */
585 #define READ_PROC_ALL_TOKENS_CLEAR ((1 << 30) - 1)
587 inline urcu_one_read(i, j, nest_i, tmp, tmp2)
589 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_NONE);
592 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FIRST_MB);
593 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_SECOND_MB);
594 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_THIRD_MB);
595 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FOURTH_MB);
598 #ifdef REMOTE_BARRIERS
599 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FIRST_MB);
600 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_SECOND_MB);
601 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_THIRD_MB);
602 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FOURTH_MB);
608 #ifdef REMOTE_BARRIERS
610 * Signal-based memory barrier will only execute when the
611 * execution order appears in program order.
617 :: CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE,
618 READ_LOCK_OUT | READ_LOCK_NESTED_OUT
619 | READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
621 | READ_LOCK_OUT_UNROLL
622 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
623 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT,
625 | READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
627 | READ_LOCK_OUT_UNROLL
628 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
629 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT | READ_LOCK_NESTED_OUT,
630 READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
632 | READ_LOCK_OUT_UNROLL
633 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
634 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
635 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN,
636 READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
638 | READ_LOCK_OUT_UNROLL
639 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
640 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
641 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN,
642 READ_UNLOCK_NESTED_OUT
644 | READ_LOCK_OUT_UNROLL
645 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
646 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
647 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN
648 | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT,
650 | READ_LOCK_OUT_UNROLL
651 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
652 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
653 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN
654 | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
657 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
658 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
659 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN
660 | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
661 | READ_UNLOCK_OUT | READ_LOCK_OUT_UNROLL,
662 READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
663 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
664 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN
665 | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
666 | READ_UNLOCK_OUT | READ_LOCK_OUT_UNROLL
667 | READ_PROC_READ_GEN_UNROLL,
668 READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
669 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
670 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN
671 | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
672 | READ_UNLOCK_OUT | READ_LOCK_OUT_UNROLL
673 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL,
674 READ_UNLOCK_OUT_UNROLL)
675 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
676 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
677 | READ_UNLOCK_OUT | READ_LOCK_OUT_UNROLL
678 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL,
687 goto non_atomic3_skip;
690 goto non_atomic3_end;
693 #endif /* REMOTE_BARRIERS */
697 PROCEDURE_READ_LOCK(READ_LOCK_BASE, READ_PROD_NONE, 0, READ_LOCK_OUT);
699 :: CONSUME_TOKENS(proc_urcu_reader,
700 READ_LOCK_OUT, /* post-dominant */
701 READ_PROC_FIRST_MB) ->
703 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FIRST_MB);
705 PROCEDURE_READ_LOCK(READ_LOCK_NESTED_BASE, READ_PROC_FIRST_MB, READ_LOCK_OUT,
706 READ_LOCK_NESTED_OUT);
708 :: CONSUME_TOKENS(proc_urcu_reader,
709 READ_PROC_FIRST_MB, /* mb() orders reads */
710 READ_PROC_READ_GEN) ->
712 ptr_read_first[get_readerid()] = READ_CACHED_VAR(rcu_ptr);
713 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_READ_GEN);
715 :: CONSUME_TOKENS(proc_urcu_reader,
716 READ_PROC_FIRST_MB /* mb() orders reads */
717 | READ_PROC_READ_GEN,
718 READ_PROC_ACCESS_GEN) ->
719 /* smp_read_barrier_depends */
722 data_read_first[get_readerid()] =
723 READ_CACHED_VAR(rcu_data[ptr_read_first[get_readerid()]]);
724 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_ACCESS_GEN);
727 /* Note : we remove the nested memory barrier from the read unlock
728 * model, given it is not usually needed. The implementation has the barrier
729 * because the performance impact added by a branch in the common case does not
733 PROCEDURE_READ_UNLOCK(READ_UNLOCK_NESTED_BASE,
736 | READ_LOCK_NESTED_OUT,
737 READ_UNLOCK_NESTED_OUT);
740 :: CONSUME_TOKENS(proc_urcu_reader,
741 READ_PROC_ACCESS_GEN /* mb() orders reads */
742 | READ_PROC_READ_GEN /* mb() orders reads */
743 | READ_PROC_FIRST_MB /* mb() ordered */
744 | READ_LOCK_OUT /* post-dominant */
745 | READ_LOCK_NESTED_OUT /* post-dominant */
746 | READ_UNLOCK_NESTED_OUT,
747 READ_PROC_SECOND_MB) ->
749 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_SECOND_MB);
751 PROCEDURE_READ_UNLOCK(READ_UNLOCK_BASE,
752 READ_PROC_SECOND_MB /* mb() orders reads */
753 | READ_PROC_FIRST_MB /* mb() orders reads */
754 | READ_LOCK_NESTED_OUT /* RAW */
755 | READ_LOCK_OUT /* RAW */
756 | READ_UNLOCK_NESTED_OUT, /* RAW */
759 /* Unrolling loop : second consecutive lock */
760 /* reading urcu_active_readers, which have been written by
761 * READ_UNLOCK_OUT : RAW */
762 PROCEDURE_READ_LOCK(READ_LOCK_UNROLL_BASE,
763 READ_PROC_SECOND_MB /* mb() orders reads */
764 | READ_PROC_FIRST_MB, /* mb() orders reads */
765 READ_LOCK_NESTED_OUT /* RAW */
766 | READ_LOCK_OUT /* RAW */
767 | READ_UNLOCK_NESTED_OUT /* RAW */
768 | READ_UNLOCK_OUT, /* RAW */
769 READ_LOCK_OUT_UNROLL);
772 :: CONSUME_TOKENS(proc_urcu_reader,
773 READ_PROC_FIRST_MB /* mb() ordered */
774 | READ_PROC_SECOND_MB /* mb() ordered */
775 | READ_LOCK_OUT_UNROLL /* post-dominant */
776 | READ_LOCK_NESTED_OUT
778 | READ_UNLOCK_NESTED_OUT
780 READ_PROC_THIRD_MB) ->
782 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_THIRD_MB);
784 :: CONSUME_TOKENS(proc_urcu_reader,
785 READ_PROC_FIRST_MB /* mb() orders reads */
786 | READ_PROC_SECOND_MB /* mb() orders reads */
787 | READ_PROC_THIRD_MB, /* mb() orders reads */
788 READ_PROC_READ_GEN_UNROLL) ->
790 ptr_read_second[get_readerid()] = READ_CACHED_VAR(rcu_ptr);
791 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_READ_GEN_UNROLL);
793 :: CONSUME_TOKENS(proc_urcu_reader,
794 READ_PROC_READ_GEN_UNROLL
795 | READ_PROC_FIRST_MB /* mb() orders reads */
796 | READ_PROC_SECOND_MB /* mb() orders reads */
797 | READ_PROC_THIRD_MB, /* mb() orders reads */
798 READ_PROC_ACCESS_GEN_UNROLL) ->
799 /* smp_read_barrier_depends */
802 data_read_second[get_readerid()] =
803 READ_CACHED_VAR(rcu_data[ptr_read_second[get_readerid()]]);
804 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_ACCESS_GEN_UNROLL);
806 :: CONSUME_TOKENS(proc_urcu_reader,
807 READ_PROC_READ_GEN_UNROLL /* mb() orders reads */
808 | READ_PROC_ACCESS_GEN_UNROLL /* mb() orders reads */
809 | READ_PROC_FIRST_MB /* mb() ordered */
810 | READ_PROC_SECOND_MB /* mb() ordered */
811 | READ_PROC_THIRD_MB /* mb() ordered */
812 | READ_LOCK_OUT_UNROLL /* post-dominant */
813 | READ_LOCK_NESTED_OUT
815 | READ_UNLOCK_NESTED_OUT
817 READ_PROC_FOURTH_MB) ->
819 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FOURTH_MB);
821 PROCEDURE_READ_UNLOCK(READ_UNLOCK_UNROLL_BASE,
822 READ_PROC_FOURTH_MB /* mb() orders reads */
823 | READ_PROC_THIRD_MB /* mb() orders reads */
824 | READ_LOCK_OUT_UNROLL /* RAW */
825 | READ_PROC_SECOND_MB /* mb() orders reads */
826 | READ_PROC_FIRST_MB /* mb() orders reads */
827 | READ_LOCK_NESTED_OUT /* RAW */
828 | READ_LOCK_OUT /* RAW */
829 | READ_UNLOCK_NESTED_OUT, /* RAW */
830 READ_UNLOCK_OUT_UNROLL);
831 :: CONSUME_TOKENS(proc_urcu_reader, READ_PROC_ALL_TOKENS, 0) ->
832 CLEAR_TOKENS(proc_urcu_reader, READ_PROC_ALL_TOKENS_CLEAR);
838 * Dependency between consecutive loops :
840 * WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp2 - 1)
841 * tmp = READ_CACHED_VAR(urcu_active_readers[get_readerid()]);
843 * _WHEN THE MB()s are in place_, they add full ordering of the
844 * generation pointer read wrt active reader count read, which ensures
845 * execution will not spill across loop execution.
846 * However, in the event mb()s are removed (execution using signal
847 * handler to promote barrier()() -> smp_mb()), nothing prevents one loop
848 * to spill its execution on other loop's execution.
871 active proctype urcu_reader()
876 /* Keep in sync manually with smp_rmb, smp_wmb, ooo_mem and init() */
877 DECLARE_PROC_CACHED_VAR(byte, urcu_gp_ctr);
878 /* Note ! currently only one reader */
879 DECLARE_PROC_CACHED_VAR(byte, urcu_active_readers[NR_READERS]);
881 DECLARE_PROC_CACHED_VAR(bit, rcu_data[SLAB_SIZE]);
885 DECLARE_PROC_CACHED_VAR(bit, rcu_ptr);
887 DECLARE_PROC_CACHED_VAR(byte, rcu_ptr);
891 INIT_PROC_CACHED_VAR(urcu_gp_ctr, 1);
892 INIT_PROC_CACHED_VAR(rcu_ptr, 0);
897 INIT_PROC_CACHED_VAR(urcu_active_readers[i], 0);
899 :: i >= NR_READERS -> break
901 INIT_PROC_CACHED_VAR(rcu_data[0], WINE);
905 INIT_PROC_CACHED_VAR(rcu_data[i], POISON);
907 :: i >= SLAB_SIZE -> break
913 assert(get_pid() < NR_PROCS);
919 * We do not test reader's progress here, because we are mainly
920 * interested in writer's progress. The reader never blocks
921 * anyway. We have to test for reader/writer's progress
922 * separately, otherwise we could think the writer is doing
923 * progress when it's blocked by an always progressing reader.
925 #ifdef READER_PROGRESS
928 urcu_one_read(i, j, nest_i, tmp, tmp2);
932 /* no name clash please */
933 #undef proc_urcu_reader
936 /* Model the RCU update process. */
939 * Bit encoding, urcu_writer :
940 * Currently only supports one reader.
943 int _proc_urcu_writer;
944 #define proc_urcu_writer _proc_urcu_writer
946 #define WRITE_PROD_NONE (1 << 0)
948 #define WRITE_DATA (1 << 1)
949 #define WRITE_PROC_WMB (1 << 2)
950 #define WRITE_XCHG_PTR (1 << 3)
952 #define WRITE_PROC_FIRST_MB (1 << 4)
955 #define WRITE_PROC_FIRST_READ_GP (1 << 5)
956 #define WRITE_PROC_FIRST_WRITE_GP (1 << 6)
957 #define WRITE_PROC_FIRST_WAIT (1 << 7)
958 #define WRITE_PROC_FIRST_WAIT_LOOP (1 << 8)
961 #define WRITE_PROC_SECOND_READ_GP (1 << 9)
962 #define WRITE_PROC_SECOND_WRITE_GP (1 << 10)
963 #define WRITE_PROC_SECOND_WAIT (1 << 11)
964 #define WRITE_PROC_SECOND_WAIT_LOOP (1 << 12)
966 #define WRITE_PROC_SECOND_MB (1 << 13)
968 #define WRITE_FREE (1 << 14)
970 #define WRITE_PROC_ALL_TOKENS (WRITE_PROD_NONE \
974 | WRITE_PROC_FIRST_MB \
975 | WRITE_PROC_FIRST_READ_GP \
976 | WRITE_PROC_FIRST_WRITE_GP \
977 | WRITE_PROC_FIRST_WAIT \
978 | WRITE_PROC_SECOND_READ_GP \
979 | WRITE_PROC_SECOND_WRITE_GP \
980 | WRITE_PROC_SECOND_WAIT \
981 | WRITE_PROC_SECOND_MB \
984 #define WRITE_PROC_ALL_TOKENS_CLEAR ((1 << 15) - 1)
987 * Mutexes are implied around writer execution. A single writer at a time.
989 active proctype urcu_writer()
992 byte tmp, tmp2, tmpa;
993 byte cur_data = 0, old_data, loop_nr = 0;
994 byte cur_gp_val = 0; /*
995 * Keep a local trace of the current parity so
996 * we don't add non-existing dependencies on the global
997 * GP update. Needed to test single flip case.
1000 /* Keep in sync manually with smp_rmb, smp_wmb, ooo_mem and init() */
1001 DECLARE_PROC_CACHED_VAR(byte, urcu_gp_ctr);
1002 /* Note ! currently only one reader */
1003 DECLARE_PROC_CACHED_VAR(byte, urcu_active_readers[NR_READERS]);
1005 DECLARE_PROC_CACHED_VAR(bit, rcu_data[SLAB_SIZE]);
1008 #if (SLAB_SIZE == 2)
1009 DECLARE_PROC_CACHED_VAR(bit, rcu_ptr);
1011 DECLARE_PROC_CACHED_VAR(byte, rcu_ptr);
1015 INIT_PROC_CACHED_VAR(urcu_gp_ctr, 1);
1016 INIT_PROC_CACHED_VAR(rcu_ptr, 0);
1020 :: i < NR_READERS ->
1021 INIT_PROC_CACHED_VAR(urcu_active_readers[i], 0);
1023 :: i >= NR_READERS -> break
1025 INIT_PROC_CACHED_VAR(rcu_data[0], WINE);
1029 INIT_PROC_CACHED_VAR(rcu_data[i], POISON);
1031 :: i >= SLAB_SIZE -> break
1038 assert(get_pid() < NR_PROCS);
1042 #ifdef WRITER_PROGRESS
1045 loop_nr = loop_nr + 1;
1047 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROD_NONE);
1050 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_WMB);
1054 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_MB);
1055 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_MB);
1059 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_READ_GP);
1060 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WRITE_GP);
1061 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT);
1062 /* For single flip, we need to know the current parity */
1063 cur_gp_val = cur_gp_val ^ RCU_GP_CTR_BIT;
1070 :: CONSUME_TOKENS(proc_urcu_writer,
1074 cur_data = (cur_data + 1) % SLAB_SIZE;
1075 WRITE_CACHED_VAR(rcu_data[cur_data], WINE);
1076 PRODUCE_TOKENS(proc_urcu_writer, WRITE_DATA);
1079 :: CONSUME_TOKENS(proc_urcu_writer,
1083 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_WMB);
1085 :: CONSUME_TOKENS(proc_urcu_writer,
1088 /* rcu_xchg_pointer() */
1090 old_data = READ_CACHED_VAR(rcu_ptr);
1091 WRITE_CACHED_VAR(rcu_ptr, cur_data);
1093 PRODUCE_TOKENS(proc_urcu_writer, WRITE_XCHG_PTR);
1095 :: CONSUME_TOKENS(proc_urcu_writer,
1096 WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR,
1097 WRITE_PROC_FIRST_MB) ->
1100 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_MB);
1103 :: CONSUME_TOKENS(proc_urcu_writer,
1104 WRITE_PROC_FIRST_MB,
1105 WRITE_PROC_FIRST_READ_GP) ->
1106 tmpa = READ_CACHED_VAR(urcu_gp_ctr);
1107 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_READ_GP);
1108 :: CONSUME_TOKENS(proc_urcu_writer,
1109 WRITE_PROC_FIRST_MB | WRITE_PROC_WMB
1110 | WRITE_PROC_FIRST_READ_GP,
1111 WRITE_PROC_FIRST_WRITE_GP) ->
1113 WRITE_CACHED_VAR(urcu_gp_ctr, tmpa ^ RCU_GP_CTR_BIT);
1114 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WRITE_GP);
1116 :: CONSUME_TOKENS(proc_urcu_writer,
1117 //WRITE_PROC_FIRST_WRITE_GP | /* TEST ADDING SYNC CORE */
1118 WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
1119 WRITE_PROC_FIRST_WAIT | WRITE_PROC_FIRST_WAIT_LOOP) ->
1121 //smp_mb(i); /* TEST */
1122 /* ONLY WAITING FOR READER 0 */
1123 tmp2 = READ_CACHED_VAR(urcu_active_readers[0]);
1125 /* In normal execution, we are always starting by
1126 * waiting for the even parity.
1128 cur_gp_val = RCU_GP_CTR_BIT;
1131 :: (tmp2 & RCU_GP_CTR_NEST_MASK)
1132 && ((tmp2 ^ cur_gp_val) & RCU_GP_CTR_BIT) ->
1133 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WAIT_LOOP);
1135 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WAIT);
1138 :: CONSUME_TOKENS(proc_urcu_writer,
1139 //WRITE_PROC_FIRST_WRITE_GP /* TEST ADDING SYNC CORE */
1140 WRITE_PROC_FIRST_WRITE_GP
1141 | WRITE_PROC_FIRST_READ_GP
1142 | WRITE_PROC_FIRST_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
1149 /* The memory barrier will invalidate the
1150 * second read done as prefetching. Note that all
1151 * instructions with side-effects depending on
1152 * WRITE_PROC_SECOND_READ_GP should also depend on
1153 * completion of this busy-waiting loop. */
1154 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_READ_GP);
1158 /* This instruction loops to WRITE_PROC_FIRST_WAIT */
1159 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WAIT_LOOP | WRITE_PROC_FIRST_WAIT);
1162 :: CONSUME_TOKENS(proc_urcu_writer,
1163 //WRITE_PROC_FIRST_WAIT | //test /* no dependency. Could pre-fetch, no side-effect. */
1164 WRITE_PROC_FIRST_WRITE_GP
1165 | WRITE_PROC_FIRST_READ_GP
1166 | WRITE_PROC_FIRST_MB,
1167 WRITE_PROC_SECOND_READ_GP) ->
1169 //smp_mb(i); /* TEST */
1170 tmpa = READ_CACHED_VAR(urcu_gp_ctr);
1171 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_READ_GP);
1172 :: CONSUME_TOKENS(proc_urcu_writer,
1173 WRITE_PROC_FIRST_WAIT /* dependency on first wait, because this
1174 * instruction has globally observable
1177 | WRITE_PROC_FIRST_MB
1179 | WRITE_PROC_FIRST_READ_GP
1180 | WRITE_PROC_FIRST_WRITE_GP
1181 | WRITE_PROC_SECOND_READ_GP,
1182 WRITE_PROC_SECOND_WRITE_GP) ->
1184 WRITE_CACHED_VAR(urcu_gp_ctr, tmpa ^ RCU_GP_CTR_BIT);
1185 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WRITE_GP);
1187 :: CONSUME_TOKENS(proc_urcu_writer,
1188 //WRITE_PROC_FIRST_WRITE_GP | /* TEST ADDING SYNC CORE */
1189 WRITE_PROC_FIRST_WAIT
1190 | WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
1191 WRITE_PROC_SECOND_WAIT | WRITE_PROC_SECOND_WAIT_LOOP) ->
1193 //smp_mb(i); /* TEST */
1194 /* ONLY WAITING FOR READER 0 */
1195 tmp2 = READ_CACHED_VAR(urcu_active_readers[0]);
1197 :: (tmp2 & RCU_GP_CTR_NEST_MASK)
1198 && ((tmp2 ^ 0) & RCU_GP_CTR_BIT) ->
1199 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT_LOOP);
1201 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT);
1204 :: CONSUME_TOKENS(proc_urcu_writer,
1205 //WRITE_PROC_FIRST_WRITE_GP | /* TEST ADDING SYNC CORE */
1206 WRITE_PROC_SECOND_WRITE_GP
1207 | WRITE_PROC_FIRST_WRITE_GP
1208 | WRITE_PROC_SECOND_READ_GP
1209 | WRITE_PROC_FIRST_READ_GP
1210 | WRITE_PROC_SECOND_WAIT_LOOP
1211 | WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR
1212 | WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
1214 #ifndef GEN_ERROR_WRITER_PROGRESS
1220 /* This instruction loops to WRITE_PROC_SECOND_WAIT */
1221 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT_LOOP | WRITE_PROC_SECOND_WAIT);
1224 :: CONSUME_TOKENS(proc_urcu_writer,
1225 WRITE_PROC_FIRST_WAIT
1226 | WRITE_PROC_SECOND_WAIT
1227 | WRITE_PROC_FIRST_READ_GP
1228 | WRITE_PROC_SECOND_READ_GP
1229 | WRITE_PROC_FIRST_WRITE_GP
1230 | WRITE_PROC_SECOND_WRITE_GP
1231 | WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR
1232 | WRITE_PROC_FIRST_MB,
1233 WRITE_PROC_SECOND_MB) ->
1236 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_MB);
1238 :: CONSUME_TOKENS(proc_urcu_writer,
1240 | WRITE_PROC_FIRST_WAIT
1241 | WRITE_PROC_SECOND_WAIT
1242 | WRITE_PROC_WMB /* No dependency on
1243 * WRITE_DATA because we
1245 * different location. */
1246 | WRITE_PROC_SECOND_MB
1247 | WRITE_PROC_FIRST_MB,
1249 WRITE_CACHED_VAR(rcu_data[old_data], POISON);
1250 PRODUCE_TOKENS(proc_urcu_writer, WRITE_FREE);
1252 :: CONSUME_TOKENS(proc_urcu_writer, WRITE_PROC_ALL_TOKENS, 0) ->
1253 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_ALL_TOKENS_CLEAR);
1259 * Note : Promela model adds implicit serialization of the
1260 * WRITE_FREE instruction. Normally, it would be permitted to
1261 * spill on the next loop execution. Given the validation we do
1262 * checks for the data entry read to be poisoned, it's ok if
1263 * we do not check "late arriving" memory poisoning.
1268 * Given the reader loops infinitely, let the writer also busy-loop
1269 * with progress here so, with weak fairness, we can test the
1270 * writer's progress.
1275 #ifdef WRITER_PROGRESS
1278 #ifdef READER_PROGRESS
1280 * Make sure we don't block the reader's progress.
1282 smp_mb_send(i, j, 5);
1287 /* Non-atomic parts of the loop */
1290 smp_mb_send(i, j, 1);
1291 goto smp_mb_send1_end;
1292 #ifndef GEN_ERROR_WRITER_PROGRESS
1294 smp_mb_send(i, j, 2);
1295 goto smp_mb_send2_end;
1297 smp_mb_send(i, j, 3);
1298 goto smp_mb_send3_end;
1301 smp_mb_send(i, j, 4);
1302 goto smp_mb_send4_end;
1307 /* no name clash please */
1308 #undef proc_urcu_writer
1311 /* Leave after the readers and writers so the pid count is ok. */
1316 INIT_CACHED_VAR(urcu_gp_ctr, 1);
1317 INIT_CACHED_VAR(rcu_ptr, 0);
1321 :: i < NR_READERS ->
1322 INIT_CACHED_VAR(urcu_active_readers[i], 0);
1323 ptr_read_first[i] = 1;
1324 ptr_read_second[i] = 1;
1325 data_read_first[i] = WINE;
1326 data_read_second[i] = WINE;
1328 :: i >= NR_READERS -> break
1330 INIT_CACHED_VAR(rcu_data[0], WINE);
1334 INIT_CACHED_VAR(rcu_data[i], POISON);
1336 :: i >= SLAB_SIZE -> break