2 * mem.spin: Promela code to validate memory barriers with OOO memory
3 * and out-of-order instruction scheduling.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 * Copyright (c) 2009 Mathieu Desnoyers
22 /* Promela validation variables. */
24 /* specific defines "included" here */
25 /* DEFINES file "included" here */
32 #define get_pid() (_pid)
34 #define get_readerid() (get_pid())
37 * Produced process control and data flow. Updated after each instruction to
38 * show which variables are ready. Using one-hot bit encoding per variable to
39 * save state space. Used as triggers to execute the instructions having those
40 * variables as input. Leaving bits active to inhibit instruction execution.
41 * Scheme used to make instruction disabling and automatic dependency fall-back
45 #define CONSUME_TOKENS(state, bits, notbits) \
46 ((!(state & (notbits))) && (state & (bits)) == (bits))
48 #define PRODUCE_TOKENS(state, bits) \
49 state = state | (bits);
51 #define CLEAR_TOKENS(state, bits) \
52 state = state & ~(bits)
55 * Types of dependency :
59 * - True dependency, Read-after-Write (RAW)
61 * This type of dependency happens when a statement depends on the result of a
62 * previous statement. This applies to any statement which needs to read a
63 * variable written by a preceding statement.
65 * - False dependency, Write-after-Read (WAR)
67 * Typically, variable renaming can ensure that this dependency goes away.
68 * However, if the statements must read and then write from/to the same variable
69 * in the OOO memory model, renaming may be impossible, and therefore this
70 * causes a WAR dependency.
72 * - Output dependency, Write-after-Write (WAW)
74 * Two writes to the same variable in subsequent statements. Variable renaming
75 * can ensure this is not needed, but can be required when writing multiple
76 * times to the same OOO mem model variable.
80 * Execution of a given instruction depends on a previous instruction evaluating
81 * in a way that allows its execution. E.g. : branches.
83 * Useful considerations for joining dependencies after branch
87 * "We say box i dominates box j if every path (leading from input to output
88 * through the diagram) which passes through box j must also pass through box
89 * i. Thus box i dominates box j if box j is subordinate to box i in the
92 * http://www.hipersoft.rice.edu/grads/publications/dom14.pdf
93 * Other classic algorithm to calculate dominance : Lengauer-Tarjan (in gcc)
97 * Just as pre-dominance, but with arcs of the data flow inverted, and input vs
98 * output exchanged. Therefore, i post-dominating j ensures that every path
99 * passing by j will pass by i before reaching the output.
101 * Prefetch and speculative execution
103 * If an instruction depends on the result of a previous branch, but it does not
104 * have side-effects, it can be executed before the branch result is known.
105 * however, it must be restarted if a core-synchronizing instruction is issued.
106 * Note that instructions which depend on the speculative instruction result
107 * but that have side-effects must depend on the branch completion in addition
108 * to the speculatively executed instruction.
110 * Other considerations
112 * Note about "volatile" keyword dependency : The compiler will order volatile
113 * accesses so they appear in the right order on a given CPU. They can be
114 * reordered by the CPU instruction scheduling. This therefore cannot be
115 * considered as a depencency.
119 * Cooper, Keith D.; & Torczon, Linda. (2005). Engineering a Compiler. Morgan
120 * Kaufmann. ISBN 1-55860-698-X.
121 * Kennedy, Ken; & Allen, Randy. (2001). Optimizing Compilers for Modern
122 * Architectures: A Dependence-based Approach. Morgan Kaufmann. ISBN
124 * Muchnick, Steven S. (1997). Advanced Compiler Design and Implementation.
125 * Morgan Kaufmann. ISBN 1-55860-320-4.
129 * Note about loops and nested calls
131 * To keep this model simple, loops expressed in the framework will behave as if
132 * there was a core synchronizing instruction between loops. To see the effect
133 * of loop unrolling, manually unrolling loops is required. Note that if loops
134 * end or start with a core synchronizing instruction, the model is appropriate.
135 * Nested calls are not supported.
139 * Only Alpha has out-of-order cache bank loads. Other architectures (intel,
140 * powerpc, arm) ensure that dependent reads won't be reordered. c.f.
141 * http://www.linuxjournal.com/article/8212)
144 #define HAVE_OOO_CACHE_READ
148 * Each process have its own data in cache. Caches are randomly updated.
149 * smp_wmb and smp_rmb forces cache updates (write and read), smp_mb forces
153 typedef per_proc_byte {
157 typedef per_proc_bit {
161 /* Bitfield has a maximum of 8 procs */
162 typedef per_proc_bitfield {
166 #define DECLARE_CACHED_VAR(type, x) \
168 per_proc_##type cached_##x; \
169 per_proc_bitfield cache_dirty_##x;
171 #define INIT_CACHED_VAR(x, v, j) \
173 cache_dirty_##x.bitfield = 0; \
177 cached_##x.val[j] = v; \
179 :: j >= NR_PROCS -> break \
182 #define IS_CACHE_DIRTY(x, id) (cache_dirty_##x.bitfield & (1 << id))
184 #define READ_CACHED_VAR(x) (cached_##x.val[get_pid()])
186 #define WRITE_CACHED_VAR(x, v) \
188 cached_##x.val[get_pid()] = v; \
189 cache_dirty_##x.bitfield = \
190 cache_dirty_##x.bitfield | (1 << get_pid()); \
193 #define CACHE_WRITE_TO_MEM(x, id) \
195 :: IS_CACHE_DIRTY(x, id) -> \
196 mem_##x = cached_##x.val[id]; \
197 cache_dirty_##x.bitfield = \
198 cache_dirty_##x.bitfield & (~(1 << id)); \
203 #define CACHE_READ_FROM_MEM(x, id) \
205 :: !IS_CACHE_DIRTY(x, id) -> \
206 cached_##x.val[id] = mem_##x;\
212 * May update other caches if cache is dirty, or not.
214 #define RANDOM_CACHE_WRITE_TO_MEM(x, id)\
216 :: 1 -> CACHE_WRITE_TO_MEM(x, id); \
220 #define RANDOM_CACHE_READ_FROM_MEM(x, id)\
222 :: 1 -> CACHE_READ_FROM_MEM(x, id); \
226 /* Must consume all prior read tokens. All subsequent reads depend on it. */
230 CACHE_READ_FROM_MEM(urcu_gp_ctr, get_pid());
234 CACHE_READ_FROM_MEM(urcu_active_readers[i], get_pid());
236 :: i >= NR_READERS -> break
238 CACHE_READ_FROM_MEM(rcu_ptr, get_pid());
242 CACHE_READ_FROM_MEM(rcu_data[i], get_pid());
244 :: i >= SLAB_SIZE -> break
249 /* Must consume all prior write tokens. All subsequent writes depend on it. */
253 CACHE_WRITE_TO_MEM(urcu_gp_ctr, get_pid());
257 CACHE_WRITE_TO_MEM(urcu_active_readers[i], get_pid());
259 :: i >= NR_READERS -> break
261 CACHE_WRITE_TO_MEM(rcu_ptr, get_pid());
265 CACHE_WRITE_TO_MEM(rcu_data[i], get_pid());
267 :: i >= SLAB_SIZE -> break
272 /* Synchronization point. Must consume all prior read and write tokens. All
273 * subsequent reads and writes depend on it. */
282 #ifdef REMOTE_BARRIERS
284 bit reader_barrier[NR_READERS];
287 * We cannot leave the barriers dependencies in place in REMOTE_BARRIERS mode
288 * because they would add unexisting core synchronization and would therefore
289 * create an incomplete model.
290 * Therefore, we model the read-side memory barriers by completely disabling the
291 * memory barriers and their dependencies from the read-side. One at a time
292 * (different verification runs), we make a different instruction listen for
296 #define smp_mb_reader(i, j)
299 * Service 0, 1 or many barrier requests.
301 inline smp_mb_recv(i, j)
304 :: (reader_barrier[get_readerid()] == 1) ->
306 * We choose to ignore cycles caused by writer busy-looping,
307 * waiting for the reader, sending barrier requests, and the
308 * reader always services them without continuing execution.
310 progress_ignoring_mb1:
312 reader_barrier[get_readerid()] = 0;
315 * We choose to ignore writer's non-progress caused by the
316 * reader ignoring the writer's mb() requests.
318 progress_ignoring_mb2:
323 #define PROGRESS_LABEL(progressid) progress_writer_progid_##progressid:
325 #define smp_mb_send(i, j, progressid) \
330 :: i < NR_READERS -> \
331 reader_barrier[i] = 1; \
333 * Busy-looping waiting for reader barrier handling is of little\
334 * interest, given the reader has the ability to totally ignore \
335 * barrier requests. \
338 :: (reader_barrier[i] == 1) -> \
339 PROGRESS_LABEL(progressid) \
341 :: (reader_barrier[i] == 0) -> break; \
344 :: i >= NR_READERS -> \
352 #define smp_mb_send(i, j, progressid) smp_mb(i)
353 #define smp_mb_reader(i, j) smp_mb(i)
354 #define smp_mb_recv(i, j)
358 /* Keep in sync manually with smp_rmb, smp_wmb, ooo_mem and init() */
359 DECLARE_CACHED_VAR(byte, urcu_gp_ctr);
360 /* Note ! currently only one reader */
361 DECLARE_CACHED_VAR(byte, urcu_active_readers[NR_READERS]);
363 DECLARE_CACHED_VAR(bit, rcu_data[SLAB_SIZE]);
367 DECLARE_CACHED_VAR(bit, rcu_ptr);
368 bit ptr_read_first[NR_READERS];
369 bit ptr_read_second[NR_READERS];
371 DECLARE_CACHED_VAR(byte, rcu_ptr);
372 byte ptr_read_first[NR_READERS];
373 byte ptr_read_second[NR_READERS];
376 bit data_read_first[NR_READERS];
377 bit data_read_second[NR_READERS];
381 inline wait_init_done()
384 :: init_done == 0 -> skip;
392 RANDOM_CACHE_WRITE_TO_MEM(urcu_gp_ctr, get_pid());
396 RANDOM_CACHE_WRITE_TO_MEM(urcu_active_readers[i],
399 :: i >= NR_READERS -> break
401 RANDOM_CACHE_WRITE_TO_MEM(rcu_ptr, get_pid());
405 RANDOM_CACHE_WRITE_TO_MEM(rcu_data[i], get_pid());
407 :: i >= SLAB_SIZE -> break
409 #ifdef HAVE_OOO_CACHE_READ
410 RANDOM_CACHE_READ_FROM_MEM(urcu_gp_ctr, get_pid());
414 RANDOM_CACHE_READ_FROM_MEM(urcu_active_readers[i],
417 :: i >= NR_READERS -> break
419 RANDOM_CACHE_READ_FROM_MEM(rcu_ptr, get_pid());
423 RANDOM_CACHE_READ_FROM_MEM(rcu_data[i], get_pid());
425 :: i >= SLAB_SIZE -> break
429 #endif /* HAVE_OOO_CACHE_READ */
434 * Bit encoding, urcu_reader :
437 int _proc_urcu_reader;
438 #define proc_urcu_reader _proc_urcu_reader
440 /* Body of PROCEDURE_READ_LOCK */
441 #define READ_PROD_A_READ (1 << 0)
442 #define READ_PROD_B_IF_TRUE (1 << 1)
443 #define READ_PROD_B_IF_FALSE (1 << 2)
444 #define READ_PROD_C_IF_TRUE_READ (1 << 3)
446 #define PROCEDURE_READ_LOCK(base, consumetoken, consumetoken2, producetoken) \
447 :: CONSUME_TOKENS(proc_urcu_reader, (consumetoken | consumetoken2), READ_PROD_A_READ << base) -> \
449 tmp = READ_CACHED_VAR(urcu_active_readers[get_readerid()]); \
450 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_A_READ << base); \
451 :: CONSUME_TOKENS(proc_urcu_reader, \
452 READ_PROD_A_READ << base, /* RAW, pre-dominant */ \
453 (READ_PROD_B_IF_TRUE | READ_PROD_B_IF_FALSE) << base) -> \
455 :: (!(tmp & RCU_GP_CTR_NEST_MASK)) -> \
456 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_B_IF_TRUE << base); \
458 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_B_IF_FALSE << base); \
461 :: CONSUME_TOKENS(proc_urcu_reader, consumetoken, /* prefetch */ \
462 READ_PROD_C_IF_TRUE_READ << base) -> \
464 tmp2 = READ_CACHED_VAR(urcu_gp_ctr); \
465 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_C_IF_TRUE_READ << base); \
466 :: CONSUME_TOKENS(proc_urcu_reader, \
467 (READ_PROD_B_IF_TRUE \
468 | READ_PROD_C_IF_TRUE_READ /* pre-dominant */ \
469 | READ_PROD_A_READ) << base, /* WAR */ \
472 WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp2); \
473 PRODUCE_TOKENS(proc_urcu_reader, producetoken); \
474 /* IF_MERGE implies \
475 * post-dominance */ \
477 :: CONSUME_TOKENS(proc_urcu_reader, \
478 (READ_PROD_B_IF_FALSE /* pre-dominant */ \
479 | READ_PROD_A_READ) << base, /* WAR */ \
482 WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], \
484 PRODUCE_TOKENS(proc_urcu_reader, producetoken); \
485 /* IF_MERGE implies \
486 * post-dominance */ \
490 /* Body of PROCEDURE_READ_LOCK */
491 #define READ_PROC_READ_UNLOCK (1 << 0)
493 #define PROCEDURE_READ_UNLOCK(base, consumetoken, producetoken) \
494 :: CONSUME_TOKENS(proc_urcu_reader, \
496 READ_PROC_READ_UNLOCK << base) -> \
498 tmp = READ_CACHED_VAR(urcu_active_readers[get_readerid()]); \
499 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_READ_UNLOCK << base); \
500 :: CONSUME_TOKENS(proc_urcu_reader, \
502 | (READ_PROC_READ_UNLOCK << base), /* WAR */ \
505 WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp - 1); \
506 PRODUCE_TOKENS(proc_urcu_reader, producetoken); \
510 #define READ_PROD_NONE (1 << 0)
512 /* PROCEDURE_READ_LOCK base = << 1 : 1 to 5 */
513 #define READ_LOCK_BASE 1
514 #define READ_LOCK_OUT (1 << 5)
516 #define READ_PROC_FIRST_MB (1 << 6)
518 /* PROCEDURE_READ_LOCK (NESTED) base : << 7 : 7 to 11 */
519 #define READ_LOCK_NESTED_BASE 7
520 #define READ_LOCK_NESTED_OUT (1 << 11)
522 #define READ_PROC_READ_GEN (1 << 12)
523 #define READ_PROC_ACCESS_GEN (1 << 13)
525 /* PROCEDURE_READ_UNLOCK (NESTED) base = << 14 : 14 to 15 */
526 #define READ_UNLOCK_NESTED_BASE 14
527 #define READ_UNLOCK_NESTED_OUT (1 << 15)
529 #define READ_PROC_SECOND_MB (1 << 16)
531 /* PROCEDURE_READ_UNLOCK base = << 17 : 17 to 18 */
532 #define READ_UNLOCK_BASE 17
533 #define READ_UNLOCK_OUT (1 << 18)
535 /* PROCEDURE_READ_LOCK_UNROLL base = << 19 : 19 to 23 */
536 #define READ_LOCK_UNROLL_BASE 19
537 #define READ_LOCK_OUT_UNROLL (1 << 23)
539 #define READ_PROC_THIRD_MB (1 << 24)
541 #define READ_PROC_READ_GEN_UNROLL (1 << 25)
542 #define READ_PROC_ACCESS_GEN_UNROLL (1 << 26)
544 #define READ_PROC_FOURTH_MB (1 << 27)
546 /* PROCEDURE_READ_UNLOCK_UNROLL base = << 28 : 28 to 29 */
547 #define READ_UNLOCK_UNROLL_BASE 28
548 #define READ_UNLOCK_OUT_UNROLL (1 << 29)
551 /* Should not include branches */
552 #define READ_PROC_ALL_TOKENS (READ_PROD_NONE \
554 | READ_PROC_FIRST_MB \
555 | READ_LOCK_NESTED_OUT \
556 | READ_PROC_READ_GEN \
557 | READ_PROC_ACCESS_GEN \
558 | READ_UNLOCK_NESTED_OUT \
559 | READ_PROC_SECOND_MB \
561 | READ_LOCK_OUT_UNROLL \
562 | READ_PROC_THIRD_MB \
563 | READ_PROC_READ_GEN_UNROLL \
564 | READ_PROC_ACCESS_GEN_UNROLL \
565 | READ_PROC_FOURTH_MB \
566 | READ_UNLOCK_OUT_UNROLL)
568 /* Must clear all tokens, including branches */
569 #define READ_PROC_ALL_TOKENS_CLEAR ((1 << 30) - 1)
571 inline urcu_one_read(i, j, nest_i, tmp, tmp2)
573 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_NONE);
576 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FIRST_MB);
577 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_SECOND_MB);
578 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_THIRD_MB);
579 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FOURTH_MB);
582 #ifdef REMOTE_BARRIERS
583 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FIRST_MB);
584 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_SECOND_MB);
585 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_THIRD_MB);
586 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FOURTH_MB);
592 #ifdef REMOTE_BARRIERS
594 * Signal-based memory barrier will only execute when the
595 * execution order appears in program order.
601 :: CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE,
602 READ_LOCK_OUT | READ_LOCK_NESTED_OUT
603 | READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
605 | READ_LOCK_OUT_UNROLL
606 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
607 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT,
609 | READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
611 | READ_LOCK_OUT_UNROLL
612 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
613 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT | READ_LOCK_NESTED_OUT,
614 READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
616 | READ_LOCK_OUT_UNROLL
617 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
618 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
619 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN,
620 READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
622 | READ_LOCK_OUT_UNROLL
623 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
624 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
625 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN,
626 READ_UNLOCK_NESTED_OUT
628 | READ_LOCK_OUT_UNROLL
629 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
630 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
631 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN
632 | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT,
634 | READ_LOCK_OUT_UNROLL
635 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
636 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
637 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN
638 | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
641 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
642 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
643 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN
644 | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
645 | READ_UNLOCK_OUT | READ_LOCK_OUT_UNROLL,
646 READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
647 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
648 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN
649 | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
650 | READ_UNLOCK_OUT | READ_LOCK_OUT_UNROLL
651 | READ_PROC_READ_GEN_UNROLL,
652 READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
653 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
654 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN
655 | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
656 | READ_UNLOCK_OUT | READ_LOCK_OUT_UNROLL
657 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL,
658 READ_UNLOCK_OUT_UNROLL)
659 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
660 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN | 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,
671 goto non_atomic3_skip;
674 goto non_atomic3_end;
677 #endif /* REMOTE_BARRIERS */
681 PROCEDURE_READ_LOCK(READ_LOCK_BASE, READ_PROD_NONE, 0, READ_LOCK_OUT);
683 :: CONSUME_TOKENS(proc_urcu_reader,
684 READ_LOCK_OUT, /* post-dominant */
685 READ_PROC_FIRST_MB) ->
687 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FIRST_MB);
689 PROCEDURE_READ_LOCK(READ_LOCK_NESTED_BASE, READ_PROC_FIRST_MB, READ_LOCK_OUT,
690 READ_LOCK_NESTED_OUT);
692 :: CONSUME_TOKENS(proc_urcu_reader,
693 READ_PROC_FIRST_MB, /* mb() orders reads */
694 READ_PROC_READ_GEN) ->
696 ptr_read_first[get_readerid()] = READ_CACHED_VAR(rcu_ptr);
697 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_READ_GEN);
699 :: CONSUME_TOKENS(proc_urcu_reader,
700 READ_PROC_FIRST_MB /* mb() orders reads */
701 | READ_PROC_READ_GEN,
702 READ_PROC_ACCESS_GEN) ->
703 /* smp_read_barrier_depends */
706 data_read_first[get_readerid()] =
707 READ_CACHED_VAR(rcu_data[ptr_read_first[get_readerid()]]);
708 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_ACCESS_GEN);
711 /* Note : we remove the nested memory barrier from the read unlock
712 * model, given it is not usually needed. The implementation has the barrier
713 * because the performance impact added by a branch in the common case does not
717 PROCEDURE_READ_UNLOCK(READ_UNLOCK_NESTED_BASE,
720 | READ_LOCK_NESTED_OUT,
721 READ_UNLOCK_NESTED_OUT);
724 :: CONSUME_TOKENS(proc_urcu_reader,
725 READ_PROC_ACCESS_GEN /* mb() orders reads */
726 | READ_PROC_READ_GEN /* mb() orders reads */
727 | READ_PROC_FIRST_MB /* mb() ordered */
728 | READ_LOCK_OUT /* post-dominant */
729 | READ_LOCK_NESTED_OUT /* post-dominant */
730 | READ_UNLOCK_NESTED_OUT,
731 READ_PROC_SECOND_MB) ->
733 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_SECOND_MB);
735 PROCEDURE_READ_UNLOCK(READ_UNLOCK_BASE,
736 READ_PROC_SECOND_MB /* mb() orders reads */
737 | READ_PROC_FIRST_MB /* mb() orders reads */
738 | READ_LOCK_NESTED_OUT /* RAW */
739 | READ_LOCK_OUT /* RAW */
740 | READ_UNLOCK_NESTED_OUT, /* RAW */
743 /* Unrolling loop : second consecutive lock */
744 /* reading urcu_active_readers, which have been written by
745 * READ_UNLOCK_OUT : RAW */
746 PROCEDURE_READ_LOCK(READ_LOCK_UNROLL_BASE,
747 READ_PROC_SECOND_MB /* mb() orders reads */
748 | READ_PROC_FIRST_MB, /* mb() orders reads */
749 READ_LOCK_NESTED_OUT /* RAW */
750 | READ_LOCK_OUT /* RAW */
751 | READ_UNLOCK_NESTED_OUT /* RAW */
752 | READ_UNLOCK_OUT, /* RAW */
753 READ_LOCK_OUT_UNROLL);
756 :: CONSUME_TOKENS(proc_urcu_reader,
757 READ_PROC_FIRST_MB /* mb() ordered */
758 | READ_PROC_SECOND_MB /* mb() ordered */
759 | READ_LOCK_OUT_UNROLL /* post-dominant */
760 | READ_LOCK_NESTED_OUT
762 | READ_UNLOCK_NESTED_OUT
764 READ_PROC_THIRD_MB) ->
766 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_THIRD_MB);
768 :: CONSUME_TOKENS(proc_urcu_reader,
769 READ_PROC_FIRST_MB /* mb() orders reads */
770 | READ_PROC_SECOND_MB /* mb() orders reads */
771 | READ_PROC_THIRD_MB, /* mb() orders reads */
772 READ_PROC_READ_GEN_UNROLL) ->
774 ptr_read_second[get_readerid()] = READ_CACHED_VAR(rcu_ptr);
775 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_READ_GEN_UNROLL);
777 :: CONSUME_TOKENS(proc_urcu_reader,
778 READ_PROC_READ_GEN_UNROLL
779 | READ_PROC_FIRST_MB /* mb() orders reads */
780 | READ_PROC_SECOND_MB /* mb() orders reads */
781 | READ_PROC_THIRD_MB, /* mb() orders reads */
782 READ_PROC_ACCESS_GEN_UNROLL) ->
783 /* smp_read_barrier_depends */
786 data_read_second[get_readerid()] =
787 READ_CACHED_VAR(rcu_data[ptr_read_second[get_readerid()]]);
788 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_ACCESS_GEN_UNROLL);
790 :: CONSUME_TOKENS(proc_urcu_reader,
791 READ_PROC_READ_GEN_UNROLL /* mb() orders reads */
792 | READ_PROC_ACCESS_GEN_UNROLL /* mb() orders reads */
793 | READ_PROC_FIRST_MB /* mb() ordered */
794 | READ_PROC_SECOND_MB /* mb() ordered */
795 | READ_PROC_THIRD_MB /* mb() ordered */
796 | READ_LOCK_OUT_UNROLL /* post-dominant */
797 | READ_LOCK_NESTED_OUT
799 | READ_UNLOCK_NESTED_OUT
801 READ_PROC_FOURTH_MB) ->
803 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FOURTH_MB);
805 PROCEDURE_READ_UNLOCK(READ_UNLOCK_UNROLL_BASE,
806 READ_PROC_FOURTH_MB /* mb() orders reads */
807 | READ_PROC_THIRD_MB /* mb() orders reads */
808 | READ_LOCK_OUT_UNROLL /* RAW */
809 | READ_PROC_SECOND_MB /* mb() orders reads */
810 | READ_PROC_FIRST_MB /* mb() orders reads */
811 | READ_LOCK_NESTED_OUT /* RAW */
812 | READ_LOCK_OUT /* RAW */
813 | READ_UNLOCK_NESTED_OUT, /* RAW */
814 READ_UNLOCK_OUT_UNROLL);
815 :: CONSUME_TOKENS(proc_urcu_reader, READ_PROC_ALL_TOKENS, 0) ->
816 CLEAR_TOKENS(proc_urcu_reader, READ_PROC_ALL_TOKENS_CLEAR);
822 * Dependency between consecutive loops :
824 * WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp2 - 1)
825 * tmp = READ_CACHED_VAR(urcu_active_readers[get_readerid()]);
827 * _WHEN THE MB()s are in place_, they add full ordering of the
828 * generation pointer read wrt active reader count read, which ensures
829 * execution will not spill across loop execution.
830 * However, in the event mb()s are removed (execution using signal
831 * handler to promote barrier()() -> smp_mb()), nothing prevents one loop
832 * to spill its execution on other loop's execution.
855 active proctype urcu_reader()
862 assert(get_pid() < NR_PROCS);
868 * We do not test reader's progress here, because we are mainly
869 * interested in writer's progress. The reader never blocks
870 * anyway. We have to test for reader/writer's progress
871 * separately, otherwise we could think the writer is doing
872 * progress when it's blocked by an always progressing reader.
874 #ifdef READER_PROGRESS
877 urcu_one_read(i, j, nest_i, tmp, tmp2);
881 /* no name clash please */
882 #undef proc_urcu_reader
885 /* Model the RCU update process. */
888 * Bit encoding, urcu_writer :
889 * Currently only supports one reader.
892 int _proc_urcu_writer;
893 #define proc_urcu_writer _proc_urcu_writer
895 #define WRITE_PROD_NONE (1 << 0)
897 #define WRITE_DATA (1 << 1)
898 #define WRITE_PROC_WMB (1 << 2)
899 #define WRITE_XCHG_PTR (1 << 3)
901 #define WRITE_PROC_FIRST_MB (1 << 4)
904 #define WRITE_PROC_FIRST_READ_GP (1 << 5)
905 #define WRITE_PROC_FIRST_WRITE_GP (1 << 6)
906 #define WRITE_PROC_FIRST_WAIT (1 << 7)
907 #define WRITE_PROC_FIRST_WAIT_LOOP (1 << 8)
910 #define WRITE_PROC_SECOND_READ_GP (1 << 9)
911 #define WRITE_PROC_SECOND_WRITE_GP (1 << 10)
912 #define WRITE_PROC_SECOND_WAIT (1 << 11)
913 #define WRITE_PROC_SECOND_WAIT_LOOP (1 << 12)
915 #define WRITE_PROC_SECOND_MB (1 << 13)
917 #define WRITE_FREE (1 << 14)
919 #define WRITE_PROC_ALL_TOKENS (WRITE_PROD_NONE \
923 | WRITE_PROC_FIRST_MB \
924 | WRITE_PROC_FIRST_READ_GP \
925 | WRITE_PROC_FIRST_WRITE_GP \
926 | WRITE_PROC_FIRST_WAIT \
927 | WRITE_PROC_SECOND_READ_GP \
928 | WRITE_PROC_SECOND_WRITE_GP \
929 | WRITE_PROC_SECOND_WAIT \
930 | WRITE_PROC_SECOND_MB \
933 #define WRITE_PROC_ALL_TOKENS_CLEAR ((1 << 15) - 1)
936 * Mutexes are implied around writer execution. A single writer at a time.
938 active proctype urcu_writer()
941 byte tmp, tmp2, tmpa;
942 byte cur_data = 0, old_data, loop_nr = 0;
943 byte cur_gp_val = 0; /*
944 * Keep a local trace of the current parity so
945 * we don't add non-existing dependencies on the global
946 * GP update. Needed to test single flip case.
951 assert(get_pid() < NR_PROCS);
955 #ifdef WRITER_PROGRESS
958 loop_nr = loop_nr + 1;
960 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROD_NONE);
963 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_WMB);
967 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_MB);
968 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_MB);
972 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_READ_GP);
973 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WRITE_GP);
974 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT);
975 /* For single flip, we need to know the current parity */
976 cur_gp_val = cur_gp_val ^ RCU_GP_CTR_BIT;
983 :: CONSUME_TOKENS(proc_urcu_writer,
987 cur_data = (cur_data + 1) % SLAB_SIZE;
988 WRITE_CACHED_VAR(rcu_data[cur_data], WINE);
989 PRODUCE_TOKENS(proc_urcu_writer, WRITE_DATA);
992 :: CONSUME_TOKENS(proc_urcu_writer,
996 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_WMB);
998 :: CONSUME_TOKENS(proc_urcu_writer,
1001 /* rcu_xchg_pointer() */
1003 old_data = READ_CACHED_VAR(rcu_ptr);
1004 WRITE_CACHED_VAR(rcu_ptr, cur_data);
1006 PRODUCE_TOKENS(proc_urcu_writer, WRITE_XCHG_PTR);
1008 :: CONSUME_TOKENS(proc_urcu_writer,
1009 WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR,
1010 WRITE_PROC_FIRST_MB) ->
1013 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_MB);
1016 :: CONSUME_TOKENS(proc_urcu_writer,
1017 WRITE_PROC_FIRST_MB,
1018 WRITE_PROC_FIRST_READ_GP) ->
1019 tmpa = READ_CACHED_VAR(urcu_gp_ctr);
1020 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_READ_GP);
1021 :: CONSUME_TOKENS(proc_urcu_writer,
1022 WRITE_PROC_FIRST_MB | WRITE_PROC_WMB
1023 | WRITE_PROC_FIRST_READ_GP,
1024 WRITE_PROC_FIRST_WRITE_GP) ->
1026 WRITE_CACHED_VAR(urcu_gp_ctr, tmpa ^ RCU_GP_CTR_BIT);
1027 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WRITE_GP);
1029 :: CONSUME_TOKENS(proc_urcu_writer,
1030 //WRITE_PROC_FIRST_WRITE_GP | /* TEST ADDING SYNC CORE */
1031 WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
1032 WRITE_PROC_FIRST_WAIT | WRITE_PROC_FIRST_WAIT_LOOP) ->
1034 //smp_mb(i); /* TEST */
1035 /* ONLY WAITING FOR READER 0 */
1036 tmp2 = READ_CACHED_VAR(urcu_active_readers[0]);
1038 /* In normal execution, we are always starting by
1039 * waiting for the even parity.
1041 cur_gp_val = RCU_GP_CTR_BIT;
1044 :: (tmp2 & RCU_GP_CTR_NEST_MASK)
1045 && ((tmp2 ^ cur_gp_val) & RCU_GP_CTR_BIT) ->
1046 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WAIT_LOOP);
1048 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WAIT);
1051 :: CONSUME_TOKENS(proc_urcu_writer,
1052 //WRITE_PROC_FIRST_WRITE_GP /* TEST ADDING SYNC CORE */
1053 WRITE_PROC_FIRST_WRITE_GP
1054 | WRITE_PROC_FIRST_READ_GP
1055 | WRITE_PROC_FIRST_WAIT_LOOP
1056 | WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR
1057 | WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
1059 #ifndef GEN_ERROR_WRITER_PROGRESS
1062 /* The memory barrier will invalidate the
1063 * second read done as prefetching. Note that all
1064 * instructions with side-effects depending on
1065 * WRITE_PROC_SECOND_READ_GP should also depend on
1066 * completion of this busy-waiting loop. */
1067 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_READ_GP);
1071 /* This instruction loops to WRITE_PROC_FIRST_WAIT */
1072 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WAIT_LOOP | WRITE_PROC_FIRST_WAIT);
1075 :: CONSUME_TOKENS(proc_urcu_writer,
1076 //WRITE_PROC_FIRST_WAIT | //test /* no dependency. Could pre-fetch, no side-effect. */
1077 WRITE_PROC_FIRST_WRITE_GP
1078 | WRITE_PROC_FIRST_READ_GP
1079 | WRITE_PROC_FIRST_MB,
1080 WRITE_PROC_SECOND_READ_GP) ->
1082 //smp_mb(i); /* TEST */
1083 tmpa = READ_CACHED_VAR(urcu_gp_ctr);
1084 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_READ_GP);
1085 :: CONSUME_TOKENS(proc_urcu_writer,
1086 WRITE_PROC_FIRST_WAIT /* dependency on first wait, because this
1087 * instruction has globally observable
1090 | WRITE_PROC_FIRST_MB
1092 | WRITE_PROC_FIRST_READ_GP
1093 | WRITE_PROC_FIRST_WRITE_GP
1094 | WRITE_PROC_SECOND_READ_GP,
1095 WRITE_PROC_SECOND_WRITE_GP) ->
1097 WRITE_CACHED_VAR(urcu_gp_ctr, tmpa ^ RCU_GP_CTR_BIT);
1098 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WRITE_GP);
1100 :: CONSUME_TOKENS(proc_urcu_writer,
1101 //WRITE_PROC_FIRST_WRITE_GP | /* TEST ADDING SYNC CORE */
1102 WRITE_PROC_FIRST_WAIT
1103 | WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
1104 WRITE_PROC_SECOND_WAIT | WRITE_PROC_SECOND_WAIT_LOOP) ->
1106 //smp_mb(i); /* TEST */
1107 /* ONLY WAITING FOR READER 0 */
1108 tmp2 = READ_CACHED_VAR(urcu_active_readers[0]);
1110 :: (tmp2 & RCU_GP_CTR_NEST_MASK)
1111 && ((tmp2 ^ 0) & RCU_GP_CTR_BIT) ->
1112 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT_LOOP);
1114 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT);
1117 :: CONSUME_TOKENS(proc_urcu_writer,
1118 //WRITE_PROC_FIRST_WRITE_GP | /* TEST ADDING SYNC CORE */
1119 WRITE_PROC_SECOND_WRITE_GP
1120 | WRITE_PROC_FIRST_WRITE_GP
1121 | WRITE_PROC_SECOND_READ_GP
1122 | WRITE_PROC_FIRST_READ_GP
1123 | WRITE_PROC_SECOND_WAIT_LOOP
1124 | WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR
1125 | WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
1127 #ifndef GEN_ERROR_WRITER_PROGRESS
1133 /* This instruction loops to WRITE_PROC_SECOND_WAIT */
1134 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT_LOOP | WRITE_PROC_SECOND_WAIT);
1137 :: CONSUME_TOKENS(proc_urcu_writer,
1138 WRITE_PROC_FIRST_WAIT
1139 | WRITE_PROC_SECOND_WAIT
1140 | WRITE_PROC_FIRST_READ_GP
1141 | WRITE_PROC_SECOND_READ_GP
1142 | WRITE_PROC_FIRST_WRITE_GP
1143 | WRITE_PROC_SECOND_WRITE_GP
1144 | WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR
1145 | WRITE_PROC_FIRST_MB,
1146 WRITE_PROC_SECOND_MB) ->
1149 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_MB);
1151 :: CONSUME_TOKENS(proc_urcu_writer,
1153 | WRITE_PROC_FIRST_WAIT
1154 | WRITE_PROC_SECOND_WAIT
1155 | WRITE_PROC_WMB /* No dependency on
1156 * WRITE_DATA because we
1158 * different location. */
1159 | WRITE_PROC_SECOND_MB
1160 | WRITE_PROC_FIRST_MB,
1162 WRITE_CACHED_VAR(rcu_data[old_data], POISON);
1163 PRODUCE_TOKENS(proc_urcu_writer, WRITE_FREE);
1165 :: CONSUME_TOKENS(proc_urcu_writer, WRITE_PROC_ALL_TOKENS, 0) ->
1166 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_ALL_TOKENS_CLEAR);
1172 * Note : Promela model adds implicit serialization of the
1173 * WRITE_FREE instruction. Normally, it would be permitted to
1174 * spill on the next loop execution. Given the validation we do
1175 * checks for the data entry read to be poisoned, it's ok if
1176 * we do not check "late arriving" memory poisoning.
1181 * Given the reader loops infinitely, let the writer also busy-loop
1182 * with progress here so, with weak fairness, we can test the
1183 * writer's progress.
1188 #ifdef WRITER_PROGRESS
1191 #ifdef READER_PROGRESS
1193 * Make sure we don't block the reader's progress.
1195 smp_mb_send(i, j, 5);
1200 /* Non-atomic parts of the loop */
1203 smp_mb_send(i, j, 1);
1204 goto smp_mb_send1_end;
1205 #ifndef GEN_ERROR_WRITER_PROGRESS
1207 smp_mb_send(i, j, 2);
1208 goto smp_mb_send2_end;
1210 smp_mb_send(i, j, 3);
1211 goto smp_mb_send3_end;
1214 smp_mb_send(i, j, 4);
1215 goto smp_mb_send4_end;
1220 /* no name clash please */
1221 #undef proc_urcu_writer
1224 /* Leave after the readers and writers so the pid count is ok. */
1229 INIT_CACHED_VAR(urcu_gp_ctr, 1, j);
1230 INIT_CACHED_VAR(rcu_ptr, 0, j);
1234 :: i < NR_READERS ->
1235 INIT_CACHED_VAR(urcu_active_readers[i], 0, j);
1236 ptr_read_first[i] = 1;
1237 ptr_read_second[i] = 1;
1238 data_read_first[i] = WINE;
1239 data_read_second[i] = WINE;
1241 :: i >= NR_READERS -> break
1243 INIT_CACHED_VAR(rcu_data[0], WINE, j);
1247 INIT_CACHED_VAR(rcu_data[i], POISON, j);
1249 :: i >= SLAB_SIZE -> break