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
16 * mem.spin: Promela code to validate memory barriers with OOO memory
17 * and out-of-order instruction scheduling.
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
33 * Copyright (c) 2009 Mathieu Desnoyers
36 /* Promela validation variables. */
38 /* specific defines "included" here */
39 /* DEFINES file "included" here */
46 #define get_pid() (_pid)
48 #define get_readerid() (get_pid())
51 * Produced process control and data flow. Updated after each instruction to
52 * show which variables are ready. Using one-hot bit encoding per variable to
53 * save state space. Used as triggers to execute the instructions having those
54 * variables as input. Leaving bits active to inhibit instruction execution.
55 * Scheme used to make instruction disabling and automatic dependency fall-back
59 #define CONSUME_TOKENS(state, bits, notbits) \
60 ((!(state & (notbits))) && (state & (bits)) == (bits))
62 #define PRODUCE_TOKENS(state, bits) \
63 state = state | (bits);
65 #define CLEAR_TOKENS(state, bits) \
66 state = state & ~(bits)
69 * Types of dependency :
73 * - True dependency, Read-after-Write (RAW)
75 * This type of dependency happens when a statement depends on the result of a
76 * previous statement. This applies to any statement which needs to read a
77 * variable written by a preceding statement.
79 * - False dependency, Write-after-Read (WAR)
81 * Typically, variable renaming can ensure that this dependency goes away.
82 * However, if the statements must read and then write from/to the same variable
83 * in the OOO memory model, renaming may be impossible, and therefore this
84 * causes a WAR dependency.
86 * - Output dependency, Write-after-Write (WAW)
88 * Two writes to the same variable in subsequent statements. Variable renaming
89 * can ensure this is not needed, but can be required when writing multiple
90 * times to the same OOO mem model variable.
94 * Execution of a given instruction depends on a previous instruction evaluating
95 * in a way that allows its execution. E.g. : branches.
97 * Useful considerations for joining dependencies after branch
101 * "We say box i dominates box j if every path (leading from input to output
102 * through the diagram) which passes through box j must also pass through box
103 * i. Thus box i dominates box j if box j is subordinate to box i in the
106 * http://www.hipersoft.rice.edu/grads/publications/dom14.pdf
107 * Other classic algorithm to calculate dominance : Lengauer-Tarjan (in gcc)
111 * Just as pre-dominance, but with arcs of the data flow inverted, and input vs
112 * output exchanged. Therefore, i post-dominating j ensures that every path
113 * passing by j will pass by i before reaching the output.
115 * Other considerations
117 * Note about "volatile" keyword dependency : The compiler will order volatile
118 * accesses so they appear in the right order on a given CPU. They can be
119 * reordered by the CPU instruction scheduling. This therefore cannot be
120 * considered as a depencency.
124 * Cooper, Keith D.; & Torczon, Linda. (2005). Engineering a Compiler. Morgan
125 * Kaufmann. ISBN 1-55860-698-X.
126 * Kennedy, Ken; & Allen, Randy. (2001). Optimizing Compilers for Modern
127 * Architectures: A Dependence-based Approach. Morgan Kaufmann. ISBN
129 * Muchnick, Steven S. (1997). Advanced Compiler Design and Implementation.
130 * Morgan Kaufmann. ISBN 1-55860-320-4.
134 * Note about loops and nested calls
136 * To keep this model simple, loops expressed in the framework will behave as if
137 * there was a core synchronizing instruction between loops. To see the effect
138 * of loop unrolling, manually unrolling loops is required. Note that if loops
139 * end or start with a core synchronizing instruction, the model is appropriate.
140 * Nested calls are not supported.
144 * Each process have its own data in cache. Caches are randomly updated.
145 * smp_wmb and smp_rmb forces cache updates (write and read), smp_mb forces
149 typedef per_proc_byte {
153 typedef per_proc_bit {
157 /* Bitfield has a maximum of 8 procs */
158 typedef per_proc_bitfield {
162 #define DECLARE_CACHED_VAR(type, x) \
164 per_proc_##type cached_##x; \
165 per_proc_bitfield cache_dirty_##x;
167 #define INIT_CACHED_VAR(x, v, j) \
169 cache_dirty_##x.bitfield = 0; \
173 cached_##x.val[j] = v; \
175 :: j >= NR_PROCS -> break \
178 #define IS_CACHE_DIRTY(x, id) (cache_dirty_##x.bitfield & (1 << id))
180 #define READ_CACHED_VAR(x) (cached_##x.val[get_pid()])
182 #define WRITE_CACHED_VAR(x, v) \
184 cached_##x.val[get_pid()] = v; \
185 cache_dirty_##x.bitfield = \
186 cache_dirty_##x.bitfield | (1 << get_pid()); \
189 #define CACHE_WRITE_TO_MEM(x, id) \
191 :: IS_CACHE_DIRTY(x, id) -> \
192 mem_##x = cached_##x.val[id]; \
193 cache_dirty_##x.bitfield = \
194 cache_dirty_##x.bitfield & (~(1 << id)); \
199 #define CACHE_READ_FROM_MEM(x, id) \
201 :: !IS_CACHE_DIRTY(x, id) -> \
202 cached_##x.val[id] = mem_##x;\
208 * May update other caches if cache is dirty, or not.
210 #define RANDOM_CACHE_WRITE_TO_MEM(x, id)\
212 :: 1 -> CACHE_WRITE_TO_MEM(x, id); \
216 #define RANDOM_CACHE_READ_FROM_MEM(x, id)\
218 :: 1 -> CACHE_READ_FROM_MEM(x, id); \
222 /* Must consume all prior read tokens. All subsequent reads depend on it. */
226 CACHE_READ_FROM_MEM(urcu_gp_ctr, get_pid());
230 CACHE_READ_FROM_MEM(urcu_active_readers[i], get_pid());
232 :: i >= NR_READERS -> break
234 CACHE_READ_FROM_MEM(rcu_ptr, get_pid());
238 CACHE_READ_FROM_MEM(rcu_data[i], get_pid());
240 :: i >= SLAB_SIZE -> break
245 /* Must consume all prior write tokens. All subsequent writes depend on it. */
249 CACHE_WRITE_TO_MEM(urcu_gp_ctr, get_pid());
253 CACHE_WRITE_TO_MEM(urcu_active_readers[i], get_pid());
255 :: i >= NR_READERS -> break
257 CACHE_WRITE_TO_MEM(rcu_ptr, get_pid());
261 CACHE_WRITE_TO_MEM(rcu_data[i], get_pid());
263 :: i >= SLAB_SIZE -> break
268 /* Synchronization point. Must consume all prior read and write tokens. All
269 * subsequent reads and writes depend on it. */
278 #ifdef REMOTE_BARRIERS
280 bit reader_barrier[NR_READERS];
283 * We cannot leave the barriers dependencies in place in REMOTE_BARRIERS mode
284 * because they would add unexisting core synchronization and would therefore
285 * create an incomplete model.
286 * Therefore, we model the read-side memory barriers by completely disabling the
287 * memory barriers and their dependencies from the read-side. One at a time
288 * (different verification runs), we make a different instruction listen for
292 #define smp_mb_reader(i, j)
295 * Service 0, 1 or many barrier requests.
297 inline smp_mb_recv(i, j)
300 :: (reader_barrier[get_readerid()] == 1) ->
302 * We choose to ignore cycles caused by writer busy-looping,
303 * waiting for the reader, sending barrier requests, and the
304 * reader always services them without continuing execution.
306 progress_ignoring_mb1:
308 reader_barrier[get_readerid()] = 0;
311 * We choose to ignore writer's non-progress caused by the
312 * reader ignoring the writer's mb() requests.
314 progress_ignoring_mb2:
319 //#ifdef WRITER_PROGRESS
320 //#define PROGRESS_LABEL(progressid)
322 //#define PROGRESS_LABEL(progressid)
325 #define PROGRESS_LABEL(progressid) progress_writer_progid_##progressid:
327 #define smp_mb_send(i, j, progressid) \
332 :: i < NR_READERS -> \
333 reader_barrier[i] = 1; \
335 * Busy-looping waiting for reader barrier handling is of little\
336 * interest, given the reader has the ability to totally ignore \
337 * barrier requests. \
340 :: (reader_barrier[i] == 1) -> \
341 PROGRESS_LABEL(progressid) \
343 :: (reader_barrier[i] == 0) -> break; \
346 :: i >= NR_READERS -> \
354 #define smp_mb_send(i, j, progressid) smp_mb(i, j)
355 #define smp_mb_reader smp_mb
356 #define smp_mb_recv(i, j)
360 /* Keep in sync manually with smp_rmb, smp_wmb, ooo_mem and init() */
361 DECLARE_CACHED_VAR(byte, urcu_gp_ctr);
362 /* Note ! currently only one reader */
363 DECLARE_CACHED_VAR(byte, urcu_active_readers[NR_READERS]);
365 DECLARE_CACHED_VAR(bit, rcu_data[SLAB_SIZE]);
369 DECLARE_CACHED_VAR(bit, rcu_ptr);
370 bit ptr_read_first[NR_READERS];
371 bit ptr_read_second[NR_READERS];
373 DECLARE_CACHED_VAR(byte, rcu_ptr);
374 byte ptr_read_first[NR_READERS];
375 byte ptr_read_second[NR_READERS];
378 bit data_read_first[NR_READERS];
379 bit data_read_second[NR_READERS];
383 inline wait_init_done()
386 :: init_done == 0 -> skip;
394 RANDOM_CACHE_WRITE_TO_MEM(urcu_gp_ctr, get_pid());
398 RANDOM_CACHE_WRITE_TO_MEM(urcu_active_readers[i],
401 :: i >= NR_READERS -> break
403 RANDOM_CACHE_WRITE_TO_MEM(rcu_ptr, get_pid());
407 RANDOM_CACHE_WRITE_TO_MEM(rcu_data[i], get_pid());
409 :: i >= SLAB_SIZE -> break
411 RANDOM_CACHE_READ_FROM_MEM(urcu_gp_ctr, get_pid());
415 RANDOM_CACHE_READ_FROM_MEM(urcu_active_readers[i],
418 :: i >= NR_READERS -> break
420 RANDOM_CACHE_READ_FROM_MEM(rcu_ptr, get_pid());
424 RANDOM_CACHE_READ_FROM_MEM(rcu_data[i], get_pid());
426 :: i >= SLAB_SIZE -> break
432 * Bit encoding, urcu_reader :
435 int _proc_urcu_reader;
436 #define proc_urcu_reader _proc_urcu_reader
438 /* Body of PROCEDURE_READ_LOCK */
439 #define READ_PROD_A_READ (1 << 0)
440 #define READ_PROD_B_IF_TRUE (1 << 1)
441 #define READ_PROD_B_IF_FALSE (1 << 2)
442 #define READ_PROD_C_IF_TRUE_READ (1 << 3)
444 #define PROCEDURE_READ_LOCK(base, consumetoken, producetoken) \
445 :: CONSUME_TOKENS(proc_urcu_reader, consumetoken, READ_PROD_A_READ << base) -> \
447 tmp = READ_CACHED_VAR(urcu_active_readers[get_readerid()]); \
448 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_A_READ << base); \
449 :: CONSUME_TOKENS(proc_urcu_reader, \
450 READ_PROD_A_READ << base, /* RAW, pre-dominant */ \
451 (READ_PROD_B_IF_TRUE | READ_PROD_B_IF_FALSE) << base) -> \
453 :: (!(tmp & RCU_GP_CTR_NEST_MASK)) -> \
454 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_B_IF_TRUE << base); \
456 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_B_IF_FALSE << base); \
459 :: CONSUME_TOKENS(proc_urcu_reader, READ_PROD_B_IF_TRUE << base, \
460 READ_PROD_C_IF_TRUE_READ << base) -> \
462 tmp2 = READ_CACHED_VAR(urcu_gp_ctr); \
463 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_C_IF_TRUE_READ << base); \
464 :: CONSUME_TOKENS(proc_urcu_reader, \
465 (READ_PROD_C_IF_TRUE_READ /* pre-dominant */ \
466 | READ_PROD_A_READ) << base, /* WAR */ \
469 WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp2); \
470 PRODUCE_TOKENS(proc_urcu_reader, producetoken); \
471 /* IF_MERGE implies \
472 * post-dominance */ \
474 :: CONSUME_TOKENS(proc_urcu_reader, \
475 (READ_PROD_B_IF_FALSE /* pre-dominant */ \
476 | READ_PROD_A_READ) << base, /* WAR */ \
479 WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], \
481 PRODUCE_TOKENS(proc_urcu_reader, producetoken); \
482 /* IF_MERGE implies \
483 * post-dominance */ \
487 /* Body of PROCEDURE_READ_LOCK */
488 #define READ_PROC_READ_UNLOCK (1 << 0)
490 #define PROCEDURE_READ_UNLOCK(base, consumetoken, producetoken) \
491 :: CONSUME_TOKENS(proc_urcu_reader, \
493 READ_PROC_READ_UNLOCK << base) -> \
495 tmp2 = READ_CACHED_VAR(urcu_active_readers[get_readerid()]); \
496 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_READ_UNLOCK << base); \
497 :: CONSUME_TOKENS(proc_urcu_reader, \
499 | (READ_PROC_READ_UNLOCK << base), /* WAR */ \
502 WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp2 - 1); \
503 PRODUCE_TOKENS(proc_urcu_reader, producetoken); \
507 #define READ_PROD_NONE (1 << 0)
509 /* PROCEDURE_READ_LOCK base = << 1 : 1 to 5 */
510 #define READ_LOCK_BASE 1
511 #define READ_LOCK_OUT (1 << 5)
513 #define READ_PROC_FIRST_MB (1 << 6)
515 /* PROCEDURE_READ_LOCK (NESTED) base : << 7 : 7 to 11 */
516 #define READ_LOCK_NESTED_BASE 7
517 #define READ_LOCK_NESTED_OUT (1 << 11)
519 #define READ_PROC_READ_GEN (1 << 12)
520 #define READ_PROC_ACCESS_GEN (1 << 13)
522 /* PROCEDURE_READ_UNLOCK (NESTED) base = << 14 : 14 to 15 */
523 #define READ_UNLOCK_NESTED_BASE 14
524 #define READ_UNLOCK_NESTED_OUT (1 << 15)
526 #define READ_PROC_SECOND_MB (1 << 16)
528 /* PROCEDURE_READ_UNLOCK base = << 17 : 17 to 18 */
529 #define READ_UNLOCK_BASE 17
530 #define READ_UNLOCK_OUT (1 << 18)
532 /* PROCEDURE_READ_LOCK_UNROLL base = << 19 : 19 to 23 */
533 #define READ_LOCK_UNROLL_BASE 19
534 #define READ_LOCK_OUT_UNROLL (1 << 23)
536 #define READ_PROC_THIRD_MB (1 << 24)
538 #define READ_PROC_READ_GEN_UNROLL (1 << 25)
539 #define READ_PROC_ACCESS_GEN_UNROLL (1 << 26)
541 #define READ_PROC_FOURTH_MB (1 << 27)
543 /* PROCEDURE_READ_UNLOCK_UNROLL base = << 28 : 28 to 29 */
544 #define READ_UNLOCK_UNROLL_BASE 28
545 #define READ_UNLOCK_OUT_UNROLL (1 << 29)
548 /* Should not include branches */
549 #define READ_PROC_ALL_TOKENS (READ_PROD_NONE \
551 | READ_PROC_FIRST_MB \
552 | READ_LOCK_NESTED_OUT \
553 | READ_PROC_READ_GEN \
554 | READ_PROC_ACCESS_GEN \
555 | READ_UNLOCK_NESTED_OUT \
556 | READ_PROC_SECOND_MB \
558 | READ_LOCK_OUT_UNROLL \
559 | READ_PROC_THIRD_MB \
560 | READ_PROC_READ_GEN_UNROLL \
561 | READ_PROC_ACCESS_GEN_UNROLL \
562 | READ_PROC_FOURTH_MB \
563 | READ_UNLOCK_OUT_UNROLL)
565 /* Must clear all tokens, including branches */
566 #define READ_PROC_ALL_TOKENS_CLEAR ((1 << 30) - 1)
568 inline urcu_one_read(i, j, nest_i, tmp, tmp2)
570 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_NONE);
573 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FIRST_MB);
574 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_SECOND_MB);
575 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_THIRD_MB);
576 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FOURTH_MB);
579 #ifdef REMOTE_BARRIERS
580 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FIRST_MB);
581 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_SECOND_MB);
582 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_THIRD_MB);
583 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FOURTH_MB);
589 #ifdef REMOTE_BARRIERS
591 * Signal-based memory barrier will only execute when the
592 * execution order appears in program order.
598 :: CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE,
599 READ_LOCK_OUT | READ_LOCK_NESTED_OUT
600 | READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
602 | READ_LOCK_OUT_UNROLL
603 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
604 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT,
606 | READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
608 | READ_LOCK_OUT_UNROLL
609 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
610 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT | READ_LOCK_NESTED_OUT,
611 READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
613 | READ_LOCK_OUT_UNROLL
614 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
615 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
616 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN,
617 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
622 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN,
623 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
635 | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
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
642 | READ_UNLOCK_OUT | 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
647 | READ_UNLOCK_OUT | READ_LOCK_OUT_UNROLL
648 | READ_PROC_READ_GEN_UNROLL,
649 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
653 | READ_UNLOCK_OUT | READ_LOCK_OUT_UNROLL
654 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL,
655 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 | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
658 | READ_UNLOCK_OUT | READ_LOCK_OUT_UNROLL
659 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL,
668 goto non_atomic3_skip;
671 goto non_atomic3_end;
674 #endif /* REMOTE_BARRIERS */
678 PROCEDURE_READ_LOCK(READ_LOCK_BASE, READ_PROD_NONE, READ_LOCK_OUT);
680 :: CONSUME_TOKENS(proc_urcu_reader,
681 READ_LOCK_OUT, /* post-dominant */
682 READ_PROC_FIRST_MB) ->
684 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FIRST_MB);
686 PROCEDURE_READ_LOCK(READ_LOCK_NESTED_BASE, READ_PROC_FIRST_MB | READ_LOCK_OUT,
687 READ_LOCK_NESTED_OUT);
689 :: CONSUME_TOKENS(proc_urcu_reader,
690 READ_PROC_FIRST_MB, /* mb() orders reads */
691 READ_PROC_READ_GEN) ->
693 ptr_read_first[get_readerid()] = READ_CACHED_VAR(rcu_ptr);
694 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_READ_GEN);
696 :: CONSUME_TOKENS(proc_urcu_reader,
697 READ_PROC_FIRST_MB /* mb() orders reads */
698 | READ_PROC_READ_GEN,
699 READ_PROC_ACCESS_GEN) ->
700 /* smp_read_barrier_depends */
703 data_read_first[get_readerid()] =
704 READ_CACHED_VAR(rcu_data[ptr_read_first[get_readerid()]]);
705 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_ACCESS_GEN);
708 /* Note : we remove the nested memory barrier from the read unlock
709 * model, given it is not usually needed. The implementation has the barrier
710 * because the performance impact added by a branch in the common case does not
714 PROCEDURE_READ_UNLOCK(READ_UNLOCK_NESTED_BASE,
717 | READ_LOCK_NESTED_OUT,
718 READ_UNLOCK_NESTED_OUT);
721 :: CONSUME_TOKENS(proc_urcu_reader,
722 READ_PROC_ACCESS_GEN /* mb() orders reads */
723 | READ_PROC_READ_GEN /* mb() orders reads */
724 | READ_PROC_FIRST_MB /* mb() ordered */
725 | READ_LOCK_OUT /* post-dominant */
726 | READ_LOCK_NESTED_OUT /* post-dominant */
727 | READ_UNLOCK_NESTED_OUT,
728 READ_PROC_SECOND_MB) ->
730 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_SECOND_MB);
732 PROCEDURE_READ_UNLOCK(READ_UNLOCK_BASE,
733 READ_PROC_SECOND_MB /* mb() orders reads */
734 | READ_PROC_FIRST_MB /* mb() orders reads */
735 | READ_LOCK_NESTED_OUT /* RAW */
736 | READ_LOCK_OUT /* RAW */
737 | READ_UNLOCK_NESTED_OUT, /* RAW */
740 /* Unrolling loop : second consecutive lock */
741 /* reading urcu_active_readers, which have been written by
742 * READ_UNLOCK_OUT : RAW */
743 PROCEDURE_READ_LOCK(READ_LOCK_UNROLL_BASE,
744 READ_UNLOCK_OUT /* RAW */
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 */
750 READ_LOCK_OUT_UNROLL);
753 :: CONSUME_TOKENS(proc_urcu_reader,
754 READ_PROC_FIRST_MB /* mb() ordered */
755 | READ_PROC_SECOND_MB /* mb() ordered */
756 | READ_LOCK_OUT_UNROLL /* post-dominant */
757 | READ_LOCK_NESTED_OUT
759 | READ_UNLOCK_NESTED_OUT
761 READ_PROC_THIRD_MB) ->
763 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_THIRD_MB);
765 :: CONSUME_TOKENS(proc_urcu_reader,
766 READ_PROC_FIRST_MB /* mb() orders reads */
767 | READ_PROC_SECOND_MB /* mb() orders reads */
768 | READ_PROC_THIRD_MB, /* mb() orders reads */
769 READ_PROC_READ_GEN_UNROLL) ->
771 ptr_read_second[get_readerid()] = READ_CACHED_VAR(rcu_ptr);
772 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_READ_GEN_UNROLL);
774 :: CONSUME_TOKENS(proc_urcu_reader,
775 READ_PROC_READ_GEN_UNROLL
776 | READ_PROC_FIRST_MB /* mb() orders reads */
777 | READ_PROC_SECOND_MB /* mb() orders reads */
778 | READ_PROC_THIRD_MB, /* mb() orders reads */
779 READ_PROC_ACCESS_GEN_UNROLL) ->
780 /* smp_read_barrier_depends */
783 data_read_second[get_readerid()] =
784 READ_CACHED_VAR(rcu_data[ptr_read_second[get_readerid()]]);
785 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_ACCESS_GEN_UNROLL);
787 :: CONSUME_TOKENS(proc_urcu_reader,
788 READ_PROC_READ_GEN_UNROLL /* mb() orders reads */
789 | READ_PROC_ACCESS_GEN_UNROLL /* mb() orders reads */
790 | READ_PROC_FIRST_MB /* mb() ordered */
791 | READ_PROC_SECOND_MB /* mb() ordered */
792 | READ_PROC_THIRD_MB /* mb() ordered */
793 | READ_LOCK_OUT_UNROLL /* post-dominant */
794 | READ_LOCK_NESTED_OUT
796 | READ_UNLOCK_NESTED_OUT
798 READ_PROC_FOURTH_MB) ->
800 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FOURTH_MB);
802 PROCEDURE_READ_UNLOCK(READ_UNLOCK_UNROLL_BASE,
803 READ_PROC_FOURTH_MB /* mb() orders reads */
804 | READ_PROC_THIRD_MB /* mb() orders reads */
805 | READ_LOCK_OUT_UNROLL /* RAW */
806 | READ_PROC_SECOND_MB /* mb() orders reads */
807 | READ_PROC_FIRST_MB /* mb() orders reads */
808 | READ_LOCK_NESTED_OUT /* RAW */
809 | READ_LOCK_OUT /* RAW */
810 | READ_UNLOCK_NESTED_OUT, /* RAW */
811 READ_UNLOCK_OUT_UNROLL);
812 :: CONSUME_TOKENS(proc_urcu_reader, READ_PROC_ALL_TOKENS, 0) ->
813 CLEAR_TOKENS(proc_urcu_reader, READ_PROC_ALL_TOKENS_CLEAR);
819 * Dependency between consecutive loops :
821 * WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp2 - 1)
822 * tmp = READ_CACHED_VAR(urcu_active_readers[get_readerid()]);
824 * _WHEN THE MB()s are in place_, they add full ordering of the
825 * generation pointer read wrt active reader count read, which ensures
826 * execution will not spill across loop execution.
827 * However, in the event mb()s are removed (execution using signal
828 * handler to promote barrier()() -> smp_mb()), nothing prevents one loop
829 * to spill its execution on other loop's execution.
852 active proctype urcu_reader()
859 assert(get_pid() < NR_PROCS);
865 * We do not test reader's progress here, because we are mainly
866 * interested in writer's progress. The reader never blocks
867 * anyway. We have to test for reader/writer's progress
868 * separately, otherwise we could think the writer is doing
869 * progress when it's blocked by an always progressing reader.
871 #ifdef READER_PROGRESS
874 urcu_one_read(i, j, nest_i, tmp, tmp2);
878 /* no name clash please */
879 #undef proc_urcu_reader
882 /* Model the RCU update process. */
885 * Bit encoding, urcu_writer :
886 * Currently only supports one reader.
889 int _proc_urcu_writer;
890 #define proc_urcu_writer _proc_urcu_writer
892 #define WRITE_PROD_NONE (1 << 0)
894 #define WRITE_DATA (1 << 1)
895 #define WRITE_PROC_WMB (1 << 2)
896 #define WRITE_XCHG_PTR (1 << 3)
898 #define WRITE_PROC_FIRST_MB (1 << 4)
901 #define WRITE_PROC_FIRST_READ_GP (1 << 5)
902 #define WRITE_PROC_FIRST_WRITE_GP (1 << 6)
903 #define WRITE_PROC_FIRST_WAIT (1 << 7)
904 #define WRITE_PROC_FIRST_WAIT_LOOP (1 << 8)
907 #define WRITE_PROC_SECOND_READ_GP (1 << 9)
908 #define WRITE_PROC_SECOND_WRITE_GP (1 << 10)
909 #define WRITE_PROC_SECOND_WAIT (1 << 11)
910 #define WRITE_PROC_SECOND_WAIT_LOOP (1 << 12)
912 #define WRITE_PROC_SECOND_MB (1 << 13)
914 #define WRITE_FREE (1 << 14)
916 #define WRITE_PROC_ALL_TOKENS (WRITE_PROD_NONE \
920 | WRITE_PROC_FIRST_MB \
921 | WRITE_PROC_FIRST_READ_GP \
922 | WRITE_PROC_FIRST_WRITE_GP \
923 | WRITE_PROC_FIRST_WAIT \
924 | WRITE_PROC_SECOND_READ_GP \
925 | WRITE_PROC_SECOND_WRITE_GP \
926 | WRITE_PROC_SECOND_WAIT \
927 | WRITE_PROC_SECOND_MB \
930 #define WRITE_PROC_ALL_TOKENS_CLEAR ((1 << 15) - 1)
933 * Mutexes are implied around writer execution. A single writer at a time.
935 active proctype urcu_writer()
938 byte tmp, tmp2, tmpa;
939 byte cur_data = 0, old_data, loop_nr = 0;
940 byte cur_gp_val = 0; /*
941 * Keep a local trace of the current parity so
942 * we don't add non-existing dependencies on the global
943 * GP update. Needed to test single flip case.
948 assert(get_pid() < NR_PROCS);
952 #ifdef WRITER_PROGRESS
955 loop_nr = loop_nr + 1;
957 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROD_NONE);
960 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_WMB);
964 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_MB);
965 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_MB);
969 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_READ_GP);
970 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WRITE_GP);
971 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT);
972 /* For single flip, we need to know the current parity */
973 cur_gp_val = cur_gp_val ^ RCU_GP_CTR_BIT;
980 :: CONSUME_TOKENS(proc_urcu_writer,
984 cur_data = (cur_data + 1) % SLAB_SIZE;
985 WRITE_CACHED_VAR(rcu_data[cur_data], WINE);
986 PRODUCE_TOKENS(proc_urcu_writer, WRITE_DATA);
989 :: CONSUME_TOKENS(proc_urcu_writer,
993 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_WMB);
995 :: CONSUME_TOKENS(proc_urcu_writer,
998 /* rcu_xchg_pointer() */
1000 old_data = READ_CACHED_VAR(rcu_ptr);
1001 WRITE_CACHED_VAR(rcu_ptr, cur_data);
1003 PRODUCE_TOKENS(proc_urcu_writer, WRITE_XCHG_PTR);
1005 :: CONSUME_TOKENS(proc_urcu_writer,
1006 WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR,
1007 WRITE_PROC_FIRST_MB) ->
1010 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_MB);
1013 :: CONSUME_TOKENS(proc_urcu_writer,
1014 WRITE_PROC_FIRST_MB,
1015 WRITE_PROC_FIRST_READ_GP) ->
1016 tmpa = READ_CACHED_VAR(urcu_gp_ctr);
1017 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_READ_GP);
1018 :: CONSUME_TOKENS(proc_urcu_writer,
1019 WRITE_PROC_FIRST_MB | WRITE_PROC_WMB
1020 | WRITE_PROC_FIRST_READ_GP,
1021 WRITE_PROC_FIRST_WRITE_GP) ->
1023 WRITE_CACHED_VAR(urcu_gp_ctr, tmpa ^ RCU_GP_CTR_BIT);
1024 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WRITE_GP);
1026 :: CONSUME_TOKENS(proc_urcu_writer,
1027 //WRITE_PROC_FIRST_WRITE_GP /* TEST ADDING SYNC CORE */
1028 WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
1029 WRITE_PROC_FIRST_WAIT | WRITE_PROC_FIRST_WAIT_LOOP) ->
1031 /* ONLY WAITING FOR READER 0 */
1032 tmp2 = READ_CACHED_VAR(urcu_active_readers[0]);
1034 /* In normal execution, we are always starting by
1035 * waiting for the even parity.
1037 cur_gp_val = RCU_GP_CTR_BIT;
1040 :: (tmp2 & RCU_GP_CTR_NEST_MASK)
1041 && ((tmp2 ^ cur_gp_val) & RCU_GP_CTR_BIT) ->
1042 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WAIT_LOOP);
1044 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WAIT);
1047 :: CONSUME_TOKENS(proc_urcu_writer,
1048 //WRITE_PROC_FIRST_WRITE_GP /* TEST ADDING SYNC CORE */
1049 WRITE_PROC_FIRST_WRITE_GP
1050 | WRITE_PROC_FIRST_READ_GP
1051 | WRITE_PROC_FIRST_WAIT_LOOP
1052 | WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR
1053 | WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
1055 #ifndef GEN_ERROR_WRITER_PROGRESS
1061 /* This instruction loops to WRITE_PROC_FIRST_WAIT */
1062 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WAIT_LOOP | WRITE_PROC_FIRST_WAIT);
1065 :: CONSUME_TOKENS(proc_urcu_writer,
1066 WRITE_PROC_FIRST_WAIT /* Control dependency : need to branch out of
1067 * the loop to execute the next flip (CHECK) */
1068 | WRITE_PROC_FIRST_WRITE_GP
1069 | WRITE_PROC_FIRST_READ_GP
1070 | WRITE_PROC_FIRST_MB,
1071 WRITE_PROC_SECOND_READ_GP) ->
1073 tmpa = READ_CACHED_VAR(urcu_gp_ctr);
1074 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_READ_GP);
1075 :: CONSUME_TOKENS(proc_urcu_writer,
1078 | WRITE_PROC_FIRST_READ_GP
1079 | WRITE_PROC_FIRST_WRITE_GP
1080 | WRITE_PROC_SECOND_READ_GP,
1081 WRITE_PROC_SECOND_WRITE_GP) ->
1083 WRITE_CACHED_VAR(urcu_gp_ctr, tmpa ^ RCU_GP_CTR_BIT);
1084 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WRITE_GP);
1086 :: CONSUME_TOKENS(proc_urcu_writer,
1087 //WRITE_PROC_FIRST_WRITE_GP /* TEST ADDING SYNC CORE */
1088 WRITE_PROC_FIRST_WAIT
1089 | WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
1090 WRITE_PROC_SECOND_WAIT | WRITE_PROC_SECOND_WAIT_LOOP) ->
1092 /* ONLY WAITING FOR READER 0 */
1093 tmp2 = READ_CACHED_VAR(urcu_active_readers[0]);
1095 :: (tmp2 & RCU_GP_CTR_NEST_MASK)
1096 && ((tmp2 ^ 0) & RCU_GP_CTR_BIT) ->
1097 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT_LOOP);
1099 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT);
1102 :: CONSUME_TOKENS(proc_urcu_writer,
1103 //WRITE_PROC_FIRST_WRITE_GP /* TEST ADDING SYNC CORE */
1104 WRITE_PROC_SECOND_WRITE_GP
1105 | WRITE_PROC_FIRST_WRITE_GP
1106 | WRITE_PROC_SECOND_READ_GP
1107 | WRITE_PROC_FIRST_READ_GP
1108 | WRITE_PROC_SECOND_WAIT_LOOP
1109 | WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR
1110 | WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
1112 #ifndef GEN_ERROR_WRITER_PROGRESS
1118 /* This instruction loops to WRITE_PROC_SECOND_WAIT */
1119 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT_LOOP | WRITE_PROC_SECOND_WAIT);
1122 :: CONSUME_TOKENS(proc_urcu_writer,
1123 WRITE_PROC_FIRST_WAIT
1124 | WRITE_PROC_SECOND_WAIT
1125 | WRITE_PROC_FIRST_READ_GP
1126 | WRITE_PROC_SECOND_READ_GP
1127 | WRITE_PROC_FIRST_WRITE_GP
1128 | WRITE_PROC_SECOND_WRITE_GP
1129 | WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR
1130 | WRITE_PROC_FIRST_MB,
1131 WRITE_PROC_SECOND_MB) ->
1134 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_MB);
1136 :: CONSUME_TOKENS(proc_urcu_writer,
1138 | WRITE_PROC_FIRST_WAIT
1139 | WRITE_PROC_SECOND_WAIT
1140 | WRITE_PROC_WMB /* No dependency on
1141 * WRITE_DATA because we
1143 * different location. */
1144 | WRITE_PROC_SECOND_MB
1145 | WRITE_PROC_FIRST_MB,
1147 WRITE_CACHED_VAR(rcu_data[old_data], POISON);
1148 PRODUCE_TOKENS(proc_urcu_writer, WRITE_FREE);
1150 :: CONSUME_TOKENS(proc_urcu_writer, WRITE_PROC_ALL_TOKENS, 0) ->
1151 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_ALL_TOKENS_CLEAR);
1157 * Note : Promela model adds implicit serialization of the
1158 * WRITE_FREE instruction. Normally, it would be permitted to
1159 * spill on the next loop execution. Given the validation we do
1160 * checks for the data entry read to be poisoned, it's ok if
1161 * we do not check "late arriving" memory poisoning.
1166 * Given the reader loops infinitely, let the writer also busy-loop
1167 * with progress here so, with weak fairness, we can test the
1168 * writer's progress.
1173 #ifdef WRITER_PROGRESS
1176 #ifdef READER_PROGRESS
1178 * Make sure we don't block the reader's progress.
1180 smp_mb_send(i, j, 5);
1185 /* Non-atomic parts of the loop */
1188 smp_mb_send(i, j, 1);
1189 goto smp_mb_send1_end;
1190 #ifndef GEN_ERROR_WRITER_PROGRESS
1192 smp_mb_send(i, j, 2);
1193 goto smp_mb_send2_end;
1195 smp_mb_send(i, j, 3);
1196 goto smp_mb_send3_end;
1199 smp_mb_send(i, j, 4);
1200 goto smp_mb_send4_end;
1205 /* no name clash please */
1206 #undef proc_urcu_writer
1209 /* Leave after the readers and writers so the pid count is ok. */
1214 INIT_CACHED_VAR(urcu_gp_ctr, 1, j);
1215 INIT_CACHED_VAR(rcu_ptr, 0, j);
1219 :: i < NR_READERS ->
1220 INIT_CACHED_VAR(urcu_active_readers[i], 0, j);
1221 ptr_read_first[i] = 1;
1222 ptr_read_second[i] = 1;
1223 data_read_first[i] = WINE;
1224 data_read_second[i] = WINE;
1226 :: i >= NR_READERS -> break
1228 INIT_CACHED_VAR(rcu_data[0], WINE, j);
1232 INIT_CACHED_VAR(rcu_data[i], POISON, j);
1234 :: i >= SLAB_SIZE -> break