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 * Other considerations
123 * Note about "volatile" keyword dependency : The compiler will order volatile
124 * accesses so they appear in the right order on a given CPU. They can be
125 * reordered by the CPU instruction scheduling. This therefore cannot be
126 * considered as a depencency.
130 * Cooper, Keith D.; & Torczon, Linda. (2005). Engineering a Compiler. Morgan
131 * Kaufmann. ISBN 1-55860-698-X.
132 * Kennedy, Ken; & Allen, Randy. (2001). Optimizing Compilers for Modern
133 * Architectures: A Dependence-based Approach. Morgan Kaufmann. ISBN
135 * Muchnick, Steven S. (1997). Advanced Compiler Design and Implementation.
136 * Morgan Kaufmann. ISBN 1-55860-320-4.
140 * Note about loops and nested calls
142 * To keep this model simple, loops expressed in the framework will behave as if
143 * there was a core synchronizing instruction between loops. To see the effect
144 * of loop unrolling, manually unrolling loops is required. Note that if loops
145 * end or start with a core synchronizing instruction, the model is appropriate.
146 * Nested calls are not supported.
150 * Only Alpha has out-of-order cache bank loads. Other architectures (intel,
151 * powerpc, arm) ensure that dependent reads won't be reordered. c.f.
152 * http://www.linuxjournal.com/article/8212)
154 #define HAVE_OOO_CACHE_READ
158 * Each process have its own data in cache. Caches are randomly updated.
159 * smp_wmb and smp_rmb forces cache updates (write and read), smp_mb forces
163 typedef per_proc_byte {
167 typedef per_proc_bit {
171 /* Bitfield has a maximum of 8 procs */
172 typedef per_proc_bitfield {
176 #define DECLARE_CACHED_VAR(type, x) \
178 per_proc_##type cached_##x; \
179 per_proc_bitfield cache_dirty_##x;
181 #define INIT_CACHED_VAR(x, v, j) \
183 cache_dirty_##x.bitfield = 0; \
187 cached_##x.val[j] = v; \
189 :: j >= NR_PROCS -> break \
192 #define IS_CACHE_DIRTY(x, id) (cache_dirty_##x.bitfield & (1 << id))
194 #define READ_CACHED_VAR(x) (cached_##x.val[get_pid()])
196 #define WRITE_CACHED_VAR(x, v) \
198 cached_##x.val[get_pid()] = v; \
199 cache_dirty_##x.bitfield = \
200 cache_dirty_##x.bitfield | (1 << get_pid()); \
203 #define CACHE_WRITE_TO_MEM(x, id) \
205 :: IS_CACHE_DIRTY(x, id) -> \
206 mem_##x = cached_##x.val[id]; \
207 cache_dirty_##x.bitfield = \
208 cache_dirty_##x.bitfield & (~(1 << id)); \
213 #define CACHE_READ_FROM_MEM(x, id) \
215 :: !IS_CACHE_DIRTY(x, id) -> \
216 cached_##x.val[id] = mem_##x;\
222 * May update other caches if cache is dirty, or not.
224 #define RANDOM_CACHE_WRITE_TO_MEM(x, id)\
226 :: 1 -> CACHE_WRITE_TO_MEM(x, id); \
230 #define RANDOM_CACHE_READ_FROM_MEM(x, id)\
232 :: 1 -> CACHE_READ_FROM_MEM(x, id); \
236 /* Must consume all prior read tokens. All subsequent reads depend on it. */
240 CACHE_READ_FROM_MEM(urcu_gp_ctr, get_pid());
244 CACHE_READ_FROM_MEM(urcu_active_readers[i], get_pid());
246 :: i >= NR_READERS -> break
248 CACHE_READ_FROM_MEM(rcu_ptr, get_pid());
252 CACHE_READ_FROM_MEM(rcu_data[i], get_pid());
254 :: i >= SLAB_SIZE -> break
259 /* Must consume all prior write tokens. All subsequent writes depend on it. */
263 CACHE_WRITE_TO_MEM(urcu_gp_ctr, get_pid());
267 CACHE_WRITE_TO_MEM(urcu_active_readers[i], get_pid());
269 :: i >= NR_READERS -> break
271 CACHE_WRITE_TO_MEM(rcu_ptr, get_pid());
275 CACHE_WRITE_TO_MEM(rcu_data[i], get_pid());
277 :: i >= SLAB_SIZE -> break
282 /* Synchronization point. Must consume all prior read and write tokens. All
283 * subsequent reads and writes depend on it. */
292 #ifdef REMOTE_BARRIERS
294 bit reader_barrier[NR_READERS];
297 * We cannot leave the barriers dependencies in place in REMOTE_BARRIERS mode
298 * because they would add unexisting core synchronization and would therefore
299 * create an incomplete model.
300 * Therefore, we model the read-side memory barriers by completely disabling the
301 * memory barriers and their dependencies from the read-side. One at a time
302 * (different verification runs), we make a different instruction listen for
306 #define smp_mb_reader(i, j)
309 * Service 0, 1 or many barrier requests.
311 inline smp_mb_recv(i, j)
314 :: (reader_barrier[get_readerid()] == 1) ->
316 * We choose to ignore cycles caused by writer busy-looping,
317 * waiting for the reader, sending barrier requests, and the
318 * reader always services them without continuing execution.
320 progress_ignoring_mb1:
322 reader_barrier[get_readerid()] = 0;
325 * We choose to ignore writer's non-progress caused by the
326 * reader ignoring the writer's mb() requests.
328 progress_ignoring_mb2:
333 #define PROGRESS_LABEL(progressid) progress_writer_progid_##progressid:
335 #define smp_mb_send(i, j, progressid) \
340 :: i < NR_READERS -> \
341 reader_barrier[i] = 1; \
343 * Busy-looping waiting for reader barrier handling is of little\
344 * interest, given the reader has the ability to totally ignore \
345 * barrier requests. \
348 :: (reader_barrier[i] == 1) -> \
349 PROGRESS_LABEL(progressid) \
351 :: (reader_barrier[i] == 0) -> break; \
354 :: i >= NR_READERS -> \
362 #define smp_mb_send(i, j, progressid) smp_mb(i)
363 #define smp_mb_reader smp_mb(i)
364 #define smp_mb_recv(i, j)
368 /* Keep in sync manually with smp_rmb, smp_wmb, ooo_mem and init() */
369 DECLARE_CACHED_VAR(byte, urcu_gp_ctr);
370 /* Note ! currently only one reader */
371 DECLARE_CACHED_VAR(byte, urcu_active_readers[NR_READERS]);
373 DECLARE_CACHED_VAR(bit, rcu_data[SLAB_SIZE]);
377 DECLARE_CACHED_VAR(bit, rcu_ptr);
378 bit ptr_read_first[NR_READERS];
379 bit ptr_read_second[NR_READERS];
381 DECLARE_CACHED_VAR(byte, rcu_ptr);
382 byte ptr_read_first[NR_READERS];
383 byte ptr_read_second[NR_READERS];
386 bit data_read_first[NR_READERS];
387 bit data_read_second[NR_READERS];
391 inline wait_init_done()
394 :: init_done == 0 -> skip;
402 RANDOM_CACHE_WRITE_TO_MEM(urcu_gp_ctr, get_pid());
406 RANDOM_CACHE_WRITE_TO_MEM(urcu_active_readers[i],
409 :: i >= NR_READERS -> break
411 RANDOM_CACHE_WRITE_TO_MEM(rcu_ptr, get_pid());
415 RANDOM_CACHE_WRITE_TO_MEM(rcu_data[i], get_pid());
417 :: i >= SLAB_SIZE -> break
419 #ifdef HAVE_OOO_CACHE_READ
420 RANDOM_CACHE_READ_FROM_MEM(urcu_gp_ctr, get_pid());
424 RANDOM_CACHE_READ_FROM_MEM(urcu_active_readers[i],
427 :: i >= NR_READERS -> break
429 RANDOM_CACHE_READ_FROM_MEM(rcu_ptr, get_pid());
433 RANDOM_CACHE_READ_FROM_MEM(rcu_data[i], get_pid());
435 :: i >= SLAB_SIZE -> break
439 #endif /* HAVE_OOO_CACHE_READ */
444 * Bit encoding, urcu_reader :
447 int _proc_urcu_reader;
448 #define proc_urcu_reader _proc_urcu_reader
450 /* Body of PROCEDURE_READ_LOCK */
451 #define READ_PROD_A_READ (1 << 0)
452 #define READ_PROD_B_IF_TRUE (1 << 1)
453 #define READ_PROD_B_IF_FALSE (1 << 2)
454 #define READ_PROD_C_IF_TRUE_READ (1 << 3)
456 #define PROCEDURE_READ_LOCK(base, consumetoken, producetoken) \
457 :: CONSUME_TOKENS(proc_urcu_reader, consumetoken, READ_PROD_A_READ << base) -> \
459 tmp = READ_CACHED_VAR(urcu_active_readers[get_readerid()]); \
460 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_A_READ << base); \
461 :: CONSUME_TOKENS(proc_urcu_reader, \
462 READ_PROD_A_READ << base, /* RAW, pre-dominant */ \
463 (READ_PROD_B_IF_TRUE | READ_PROD_B_IF_FALSE) << base) -> \
465 :: (!(tmp & RCU_GP_CTR_NEST_MASK)) -> \
466 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_B_IF_TRUE << base); \
468 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_B_IF_FALSE << base); \
471 :: CONSUME_TOKENS(proc_urcu_reader, READ_PROD_B_IF_TRUE << base, \
472 READ_PROD_C_IF_TRUE_READ << base) -> \
474 tmp2 = READ_CACHED_VAR(urcu_gp_ctr); \
475 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_C_IF_TRUE_READ << base); \
476 :: CONSUME_TOKENS(proc_urcu_reader, \
477 (READ_PROD_C_IF_TRUE_READ /* pre-dominant */ \
478 | READ_PROD_A_READ) << base, /* WAR */ \
481 WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp2); \
482 PRODUCE_TOKENS(proc_urcu_reader, producetoken); \
483 /* IF_MERGE implies \
484 * post-dominance */ \
486 :: CONSUME_TOKENS(proc_urcu_reader, \
487 (READ_PROD_B_IF_FALSE /* pre-dominant */ \
488 | READ_PROD_A_READ) << base, /* WAR */ \
491 WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], \
493 PRODUCE_TOKENS(proc_urcu_reader, producetoken); \
494 /* IF_MERGE implies \
495 * post-dominance */ \
499 /* Body of PROCEDURE_READ_LOCK */
500 #define READ_PROC_READ_UNLOCK (1 << 0)
502 #define PROCEDURE_READ_UNLOCK(base, consumetoken, producetoken) \
503 :: CONSUME_TOKENS(proc_urcu_reader, \
505 READ_PROC_READ_UNLOCK << base) -> \
507 tmp2 = READ_CACHED_VAR(urcu_active_readers[get_readerid()]); \
508 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_READ_UNLOCK << base); \
509 :: CONSUME_TOKENS(proc_urcu_reader, \
511 | (READ_PROC_READ_UNLOCK << base), /* WAR */ \
514 WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp2 - 1); \
515 PRODUCE_TOKENS(proc_urcu_reader, producetoken); \
519 #define READ_PROD_NONE (1 << 0)
521 /* PROCEDURE_READ_LOCK base = << 1 : 1 to 5 */
522 #define READ_LOCK_BASE 1
523 #define READ_LOCK_OUT (1 << 5)
525 #define READ_PROC_FIRST_MB (1 << 6)
527 /* PROCEDURE_READ_LOCK (NESTED) base : << 7 : 7 to 11 */
528 #define READ_LOCK_NESTED_BASE 7
529 #define READ_LOCK_NESTED_OUT (1 << 11)
531 #define READ_PROC_READ_GEN (1 << 12)
532 #define READ_PROC_ACCESS_GEN (1 << 13)
534 /* PROCEDURE_READ_UNLOCK (NESTED) base = << 14 : 14 to 15 */
535 #define READ_UNLOCK_NESTED_BASE 14
536 #define READ_UNLOCK_NESTED_OUT (1 << 15)
538 #define READ_PROC_SECOND_MB (1 << 16)
540 /* PROCEDURE_READ_UNLOCK base = << 17 : 17 to 18 */
541 #define READ_UNLOCK_BASE 17
542 #define READ_UNLOCK_OUT (1 << 18)
544 /* PROCEDURE_READ_LOCK_UNROLL base = << 19 : 19 to 23 */
545 #define READ_LOCK_UNROLL_BASE 19
546 #define READ_LOCK_OUT_UNROLL (1 << 23)
548 #define READ_PROC_THIRD_MB (1 << 24)
550 #define READ_PROC_READ_GEN_UNROLL (1 << 25)
551 #define READ_PROC_ACCESS_GEN_UNROLL (1 << 26)
553 #define READ_PROC_FOURTH_MB (1 << 27)
555 /* PROCEDURE_READ_UNLOCK_UNROLL base = << 28 : 28 to 29 */
556 #define READ_UNLOCK_UNROLL_BASE 28
557 #define READ_UNLOCK_OUT_UNROLL (1 << 29)
560 /* Should not include branches */
561 #define READ_PROC_ALL_TOKENS (READ_PROD_NONE \
563 | READ_PROC_FIRST_MB \
564 | READ_LOCK_NESTED_OUT \
565 | READ_PROC_READ_GEN \
566 | READ_PROC_ACCESS_GEN \
567 | READ_UNLOCK_NESTED_OUT \
568 | READ_PROC_SECOND_MB \
570 | READ_LOCK_OUT_UNROLL \
571 | READ_PROC_THIRD_MB \
572 | READ_PROC_READ_GEN_UNROLL \
573 | READ_PROC_ACCESS_GEN_UNROLL \
574 | READ_PROC_FOURTH_MB \
575 | READ_UNLOCK_OUT_UNROLL)
577 /* Must clear all tokens, including branches */
578 #define READ_PROC_ALL_TOKENS_CLEAR ((1 << 30) - 1)
580 inline urcu_one_read(i, j, nest_i, tmp, tmp2)
582 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_NONE);
585 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FIRST_MB);
586 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_SECOND_MB);
587 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_THIRD_MB);
588 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FOURTH_MB);
591 #ifdef REMOTE_BARRIERS
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);
601 #ifdef REMOTE_BARRIERS
603 * Signal-based memory barrier will only execute when the
604 * execution order appears in program order.
610 :: CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE,
611 READ_LOCK_OUT | READ_LOCK_NESTED_OUT
612 | READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
614 | READ_LOCK_OUT_UNROLL
615 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
616 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT,
618 | READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
620 | READ_LOCK_OUT_UNROLL
621 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
622 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | 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
628 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN,
629 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
634 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN,
635 READ_UNLOCK_NESTED_OUT
637 | READ_LOCK_OUT_UNROLL
638 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
639 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
640 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN
641 | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT,
643 | READ_LOCK_OUT_UNROLL
644 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
645 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
646 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN
647 | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
650 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
651 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
652 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN
653 | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
654 | READ_UNLOCK_OUT | 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
659 | READ_UNLOCK_OUT | READ_LOCK_OUT_UNROLL
660 | READ_PROC_READ_GEN_UNROLL,
661 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,
667 READ_UNLOCK_OUT_UNROLL)
668 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
669 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN | 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 | READ_UNLOCK_OUT_UNROLL,
680 goto non_atomic3_skip;
683 goto non_atomic3_end;
686 #endif /* REMOTE_BARRIERS */
690 PROCEDURE_READ_LOCK(READ_LOCK_BASE, READ_PROD_NONE, READ_LOCK_OUT);
692 :: CONSUME_TOKENS(proc_urcu_reader,
693 READ_LOCK_OUT, /* post-dominant */
694 READ_PROC_FIRST_MB) ->
696 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FIRST_MB);
698 PROCEDURE_READ_LOCK(READ_LOCK_NESTED_BASE, READ_PROC_FIRST_MB | READ_LOCK_OUT,
699 READ_LOCK_NESTED_OUT);
701 :: CONSUME_TOKENS(proc_urcu_reader,
702 READ_PROC_FIRST_MB, /* mb() orders reads */
703 READ_PROC_READ_GEN) ->
705 ptr_read_first[get_readerid()] = READ_CACHED_VAR(rcu_ptr);
706 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_READ_GEN);
708 :: CONSUME_TOKENS(proc_urcu_reader,
709 READ_PROC_FIRST_MB /* mb() orders reads */
710 | READ_PROC_READ_GEN,
711 READ_PROC_ACCESS_GEN) ->
712 /* smp_read_barrier_depends */
715 data_read_first[get_readerid()] =
716 READ_CACHED_VAR(rcu_data[ptr_read_first[get_readerid()]]);
717 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_ACCESS_GEN);
720 /* Note : we remove the nested memory barrier from the read unlock
721 * model, given it is not usually needed. The implementation has the barrier
722 * because the performance impact added by a branch in the common case does not
726 PROCEDURE_READ_UNLOCK(READ_UNLOCK_NESTED_BASE,
729 | READ_LOCK_NESTED_OUT,
730 READ_UNLOCK_NESTED_OUT);
733 :: CONSUME_TOKENS(proc_urcu_reader,
734 READ_PROC_ACCESS_GEN /* mb() orders reads */
735 | READ_PROC_READ_GEN /* mb() orders reads */
736 | READ_PROC_FIRST_MB /* mb() ordered */
737 | READ_LOCK_OUT /* post-dominant */
738 | READ_LOCK_NESTED_OUT /* post-dominant */
739 | READ_UNLOCK_NESTED_OUT,
740 READ_PROC_SECOND_MB) ->
742 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_SECOND_MB);
744 PROCEDURE_READ_UNLOCK(READ_UNLOCK_BASE,
745 READ_PROC_SECOND_MB /* mb() orders reads */
746 | READ_PROC_FIRST_MB /* mb() orders reads */
747 | READ_LOCK_NESTED_OUT /* RAW */
748 | READ_LOCK_OUT /* RAW */
749 | READ_UNLOCK_NESTED_OUT, /* RAW */
752 /* Unrolling loop : second consecutive lock */
753 /* reading urcu_active_readers, which have been written by
754 * READ_UNLOCK_OUT : RAW */
755 PROCEDURE_READ_LOCK(READ_LOCK_UNROLL_BASE,
756 READ_UNLOCK_OUT /* RAW */
757 | READ_PROC_SECOND_MB /* mb() orders reads */
758 | READ_PROC_FIRST_MB /* mb() orders reads */
759 | READ_LOCK_NESTED_OUT /* RAW */
760 | READ_LOCK_OUT /* RAW */
761 | READ_UNLOCK_NESTED_OUT, /* RAW */
762 READ_LOCK_OUT_UNROLL);
765 :: CONSUME_TOKENS(proc_urcu_reader,
766 READ_PROC_FIRST_MB /* mb() ordered */
767 | READ_PROC_SECOND_MB /* mb() ordered */
768 | READ_LOCK_OUT_UNROLL /* post-dominant */
769 | READ_LOCK_NESTED_OUT
771 | READ_UNLOCK_NESTED_OUT
773 READ_PROC_THIRD_MB) ->
775 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_THIRD_MB);
777 :: CONSUME_TOKENS(proc_urcu_reader,
778 READ_PROC_FIRST_MB /* mb() orders reads */
779 | READ_PROC_SECOND_MB /* mb() orders reads */
780 | READ_PROC_THIRD_MB, /* mb() orders reads */
781 READ_PROC_READ_GEN_UNROLL) ->
783 ptr_read_second[get_readerid()] = READ_CACHED_VAR(rcu_ptr);
784 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_READ_GEN_UNROLL);
786 :: CONSUME_TOKENS(proc_urcu_reader,
787 READ_PROC_READ_GEN_UNROLL
788 | READ_PROC_FIRST_MB /* mb() orders reads */
789 | READ_PROC_SECOND_MB /* mb() orders reads */
790 | READ_PROC_THIRD_MB, /* mb() orders reads */
791 READ_PROC_ACCESS_GEN_UNROLL) ->
792 /* smp_read_barrier_depends */
795 data_read_second[get_readerid()] =
796 READ_CACHED_VAR(rcu_data[ptr_read_second[get_readerid()]]);
797 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_ACCESS_GEN_UNROLL);
799 :: CONSUME_TOKENS(proc_urcu_reader,
800 READ_PROC_READ_GEN_UNROLL /* mb() orders reads */
801 | READ_PROC_ACCESS_GEN_UNROLL /* mb() orders reads */
802 | READ_PROC_FIRST_MB /* mb() ordered */
803 | READ_PROC_SECOND_MB /* mb() ordered */
804 | READ_PROC_THIRD_MB /* mb() ordered */
805 | READ_LOCK_OUT_UNROLL /* post-dominant */
806 | READ_LOCK_NESTED_OUT
808 | READ_UNLOCK_NESTED_OUT
810 READ_PROC_FOURTH_MB) ->
812 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FOURTH_MB);
814 PROCEDURE_READ_UNLOCK(READ_UNLOCK_UNROLL_BASE,
815 READ_PROC_FOURTH_MB /* mb() orders reads */
816 | READ_PROC_THIRD_MB /* mb() orders reads */
817 | READ_LOCK_OUT_UNROLL /* RAW */
818 | READ_PROC_SECOND_MB /* mb() orders reads */
819 | READ_PROC_FIRST_MB /* mb() orders reads */
820 | READ_LOCK_NESTED_OUT /* RAW */
821 | READ_LOCK_OUT /* RAW */
822 | READ_UNLOCK_NESTED_OUT, /* RAW */
823 READ_UNLOCK_OUT_UNROLL);
824 :: CONSUME_TOKENS(proc_urcu_reader, READ_PROC_ALL_TOKENS, 0) ->
825 CLEAR_TOKENS(proc_urcu_reader, READ_PROC_ALL_TOKENS_CLEAR);
831 * Dependency between consecutive loops :
833 * WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp2 - 1)
834 * tmp = READ_CACHED_VAR(urcu_active_readers[get_readerid()]);
836 * _WHEN THE MB()s are in place_, they add full ordering of the
837 * generation pointer read wrt active reader count read, which ensures
838 * execution will not spill across loop execution.
839 * However, in the event mb()s are removed (execution using signal
840 * handler to promote barrier()() -> smp_mb()), nothing prevents one loop
841 * to spill its execution on other loop's execution.
864 active proctype urcu_reader()
871 assert(get_pid() < NR_PROCS);
877 * We do not test reader's progress here, because we are mainly
878 * interested in writer's progress. The reader never blocks
879 * anyway. We have to test for reader/writer's progress
880 * separately, otherwise we could think the writer is doing
881 * progress when it's blocked by an always progressing reader.
883 #ifdef READER_PROGRESS
886 urcu_one_read(i, j, nest_i, tmp, tmp2);
890 /* no name clash please */
891 #undef proc_urcu_reader
894 /* Model the RCU update process. */
897 * Bit encoding, urcu_writer :
898 * Currently only supports one reader.
901 int _proc_urcu_writer;
902 #define proc_urcu_writer _proc_urcu_writer
904 #define WRITE_PROD_NONE (1 << 0)
906 #define WRITE_DATA (1 << 1)
907 #define WRITE_PROC_WMB (1 << 2)
908 #define WRITE_XCHG_PTR (1 << 3)
910 #define WRITE_PROC_FIRST_MB (1 << 4)
913 #define WRITE_PROC_FIRST_READ_GP (1 << 5)
914 #define WRITE_PROC_FIRST_WRITE_GP (1 << 6)
915 #define WRITE_PROC_FIRST_WAIT (1 << 7)
916 #define WRITE_PROC_FIRST_WAIT_LOOP (1 << 8)
919 #define WRITE_PROC_SECOND_READ_GP (1 << 9)
920 #define WRITE_PROC_SECOND_WRITE_GP (1 << 10)
921 #define WRITE_PROC_SECOND_WAIT (1 << 11)
922 #define WRITE_PROC_SECOND_WAIT_LOOP (1 << 12)
924 #define WRITE_PROC_SECOND_MB (1 << 13)
926 #define WRITE_FREE (1 << 14)
928 #define WRITE_PROC_ALL_TOKENS (WRITE_PROD_NONE \
932 | WRITE_PROC_FIRST_MB \
933 | WRITE_PROC_FIRST_READ_GP \
934 | WRITE_PROC_FIRST_WRITE_GP \
935 | WRITE_PROC_FIRST_WAIT \
936 | WRITE_PROC_SECOND_READ_GP \
937 | WRITE_PROC_SECOND_WRITE_GP \
938 | WRITE_PROC_SECOND_WAIT \
939 | WRITE_PROC_SECOND_MB \
942 #define WRITE_PROC_ALL_TOKENS_CLEAR ((1 << 15) - 1)
945 * Mutexes are implied around writer execution. A single writer at a time.
947 active proctype urcu_writer()
950 byte tmp, tmp2, tmpa;
951 byte cur_data = 0, old_data, loop_nr = 0;
952 byte cur_gp_val = 0; /*
953 * Keep a local trace of the current parity so
954 * we don't add non-existing dependencies on the global
955 * GP update. Needed to test single flip case.
960 assert(get_pid() < NR_PROCS);
964 #ifdef WRITER_PROGRESS
967 loop_nr = loop_nr + 1;
969 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROD_NONE);
972 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_WMB);
976 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_MB);
977 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_MB);
981 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_READ_GP);
982 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WRITE_GP);
983 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT);
984 /* For single flip, we need to know the current parity */
985 cur_gp_val = cur_gp_val ^ RCU_GP_CTR_BIT;
992 :: CONSUME_TOKENS(proc_urcu_writer,
996 cur_data = (cur_data + 1) % SLAB_SIZE;
997 WRITE_CACHED_VAR(rcu_data[cur_data], WINE);
998 PRODUCE_TOKENS(proc_urcu_writer, WRITE_DATA);
1001 :: CONSUME_TOKENS(proc_urcu_writer,
1005 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_WMB);
1007 :: CONSUME_TOKENS(proc_urcu_writer,
1010 /* rcu_xchg_pointer() */
1012 old_data = READ_CACHED_VAR(rcu_ptr);
1013 WRITE_CACHED_VAR(rcu_ptr, cur_data);
1015 PRODUCE_TOKENS(proc_urcu_writer, WRITE_XCHG_PTR);
1017 :: CONSUME_TOKENS(proc_urcu_writer,
1018 WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR,
1019 WRITE_PROC_FIRST_MB) ->
1022 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_MB);
1025 :: CONSUME_TOKENS(proc_urcu_writer,
1026 WRITE_PROC_FIRST_MB,
1027 WRITE_PROC_FIRST_READ_GP) ->
1028 tmpa = READ_CACHED_VAR(urcu_gp_ctr);
1029 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_READ_GP);
1030 :: CONSUME_TOKENS(proc_urcu_writer,
1031 WRITE_PROC_FIRST_MB | WRITE_PROC_WMB
1032 | WRITE_PROC_FIRST_READ_GP,
1033 WRITE_PROC_FIRST_WRITE_GP) ->
1035 WRITE_CACHED_VAR(urcu_gp_ctr, tmpa ^ RCU_GP_CTR_BIT);
1036 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WRITE_GP);
1038 :: CONSUME_TOKENS(proc_urcu_writer,
1039 //WRITE_PROC_FIRST_WRITE_GP /* TEST ADDING SYNC CORE */
1040 WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
1041 WRITE_PROC_FIRST_WAIT | WRITE_PROC_FIRST_WAIT_LOOP) ->
1043 /* ONLY WAITING FOR READER 0 */
1044 tmp2 = READ_CACHED_VAR(urcu_active_readers[0]);
1046 /* In normal execution, we are always starting by
1047 * waiting for the even parity.
1049 cur_gp_val = RCU_GP_CTR_BIT;
1052 :: (tmp2 & RCU_GP_CTR_NEST_MASK)
1053 && ((tmp2 ^ cur_gp_val) & RCU_GP_CTR_BIT) ->
1054 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WAIT_LOOP);
1056 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WAIT);
1059 :: CONSUME_TOKENS(proc_urcu_writer,
1060 //WRITE_PROC_FIRST_WRITE_GP /* TEST ADDING SYNC CORE */
1061 WRITE_PROC_FIRST_WRITE_GP
1062 | WRITE_PROC_FIRST_READ_GP
1063 | WRITE_PROC_FIRST_WAIT_LOOP
1064 | WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR
1065 | WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
1067 #ifndef GEN_ERROR_WRITER_PROGRESS
1073 /* This instruction loops to WRITE_PROC_FIRST_WAIT */
1074 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WAIT_LOOP | WRITE_PROC_FIRST_WAIT);
1077 :: CONSUME_TOKENS(proc_urcu_writer,
1078 WRITE_PROC_FIRST_WAIT /* Control dependency : need to branch out of
1079 * the loop to execute the next flip (CHECK) */
1080 | WRITE_PROC_FIRST_WRITE_GP
1081 | WRITE_PROC_FIRST_READ_GP
1082 | WRITE_PROC_FIRST_MB,
1083 WRITE_PROC_SECOND_READ_GP) ->
1085 tmpa = READ_CACHED_VAR(urcu_gp_ctr);
1086 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_READ_GP);
1087 :: CONSUME_TOKENS(proc_urcu_writer,
1090 | WRITE_PROC_FIRST_READ_GP
1091 | WRITE_PROC_FIRST_WRITE_GP
1092 | WRITE_PROC_SECOND_READ_GP,
1093 WRITE_PROC_SECOND_WRITE_GP) ->
1095 WRITE_CACHED_VAR(urcu_gp_ctr, tmpa ^ RCU_GP_CTR_BIT);
1096 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WRITE_GP);
1098 :: CONSUME_TOKENS(proc_urcu_writer,
1099 //WRITE_PROC_FIRST_WRITE_GP /* TEST ADDING SYNC CORE */
1100 WRITE_PROC_FIRST_WAIT
1101 | WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
1102 WRITE_PROC_SECOND_WAIT | WRITE_PROC_SECOND_WAIT_LOOP) ->
1104 /* ONLY WAITING FOR READER 0 */
1105 tmp2 = READ_CACHED_VAR(urcu_active_readers[0]);
1107 :: (tmp2 & RCU_GP_CTR_NEST_MASK)
1108 && ((tmp2 ^ 0) & RCU_GP_CTR_BIT) ->
1109 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT_LOOP);
1111 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT);
1114 :: CONSUME_TOKENS(proc_urcu_writer,
1115 //WRITE_PROC_FIRST_WRITE_GP /* TEST ADDING SYNC CORE */
1116 WRITE_PROC_SECOND_WRITE_GP
1117 | WRITE_PROC_FIRST_WRITE_GP
1118 | WRITE_PROC_SECOND_READ_GP
1119 | WRITE_PROC_FIRST_READ_GP
1120 | WRITE_PROC_SECOND_WAIT_LOOP
1121 | WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR
1122 | WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
1124 #ifndef GEN_ERROR_WRITER_PROGRESS
1130 /* This instruction loops to WRITE_PROC_SECOND_WAIT */
1131 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT_LOOP | WRITE_PROC_SECOND_WAIT);
1134 :: CONSUME_TOKENS(proc_urcu_writer,
1135 WRITE_PROC_FIRST_WAIT
1136 | WRITE_PROC_SECOND_WAIT
1137 | WRITE_PROC_FIRST_READ_GP
1138 | WRITE_PROC_SECOND_READ_GP
1139 | WRITE_PROC_FIRST_WRITE_GP
1140 | WRITE_PROC_SECOND_WRITE_GP
1141 | WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR
1142 | WRITE_PROC_FIRST_MB,
1143 WRITE_PROC_SECOND_MB) ->
1146 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_MB);
1148 :: CONSUME_TOKENS(proc_urcu_writer,
1150 | WRITE_PROC_FIRST_WAIT
1151 | WRITE_PROC_SECOND_WAIT
1152 | WRITE_PROC_WMB /* No dependency on
1153 * WRITE_DATA because we
1155 * different location. */
1156 | WRITE_PROC_SECOND_MB
1157 | WRITE_PROC_FIRST_MB,
1159 WRITE_CACHED_VAR(rcu_data[old_data], POISON);
1160 PRODUCE_TOKENS(proc_urcu_writer, WRITE_FREE);
1162 :: CONSUME_TOKENS(proc_urcu_writer, WRITE_PROC_ALL_TOKENS, 0) ->
1163 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_ALL_TOKENS_CLEAR);
1169 * Note : Promela model adds implicit serialization of the
1170 * WRITE_FREE instruction. Normally, it would be permitted to
1171 * spill on the next loop execution. Given the validation we do
1172 * checks for the data entry read to be poisoned, it's ok if
1173 * we do not check "late arriving" memory poisoning.
1178 * Given the reader loops infinitely, let the writer also busy-loop
1179 * with progress here so, with weak fairness, we can test the
1180 * writer's progress.
1185 #ifdef WRITER_PROGRESS
1188 #ifdef READER_PROGRESS
1190 * Make sure we don't block the reader's progress.
1192 smp_mb_send(i, j, 5);
1197 /* Non-atomic parts of the loop */
1200 smp_mb_send(i, j, 1);
1201 goto smp_mb_send1_end;
1202 #ifndef GEN_ERROR_WRITER_PROGRESS
1204 smp_mb_send(i, j, 2);
1205 goto smp_mb_send2_end;
1207 smp_mb_send(i, j, 3);
1208 goto smp_mb_send3_end;
1211 smp_mb_send(i, j, 4);
1212 goto smp_mb_send4_end;
1217 /* no name clash please */
1218 #undef proc_urcu_writer
1221 /* Leave after the readers and writers so the pid count is ok. */
1226 INIT_CACHED_VAR(urcu_gp_ctr, 1, j);
1227 INIT_CACHED_VAR(rcu_ptr, 0, j);
1231 :: i < NR_READERS ->
1232 INIT_CACHED_VAR(urcu_active_readers[i], 0, j);
1233 ptr_read_first[i] = 1;
1234 ptr_read_second[i] = 1;
1235 data_read_first[i] = WINE;
1236 data_read_second[i] = WINE;
1238 :: i >= NR_READERS -> break
1240 INIT_CACHED_VAR(rcu_data[0], WINE, j);
1244 INIT_CACHED_VAR(rcu_data[i], POISON, j);
1246 :: i >= SLAB_SIZE -> break