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 * Other considerations
103 * Note about "volatile" keyword dependency : The compiler will order volatile
104 * accesses so they appear in the right order on a given CPU. They can be
105 * reordered by the CPU instruction scheduling. This therefore cannot be
106 * considered as a depencency.
110 * Cooper, Keith D.; & Torczon, Linda. (2005). Engineering a Compiler. Morgan
111 * Kaufmann. ISBN 1-55860-698-X.
112 * Kennedy, Ken; & Allen, Randy. (2001). Optimizing Compilers for Modern
113 * Architectures: A Dependence-based Approach. Morgan Kaufmann. ISBN
115 * Muchnick, Steven S. (1997). Advanced Compiler Design and Implementation.
116 * Morgan Kaufmann. ISBN 1-55860-320-4.
120 * Note about loops and nested calls
122 * To keep this model simple, loops expressed in the framework will behave as if
123 * there was a core synchronizing instruction between loops. To see the effect
124 * of loop unrolling, manually unrolling loops is required. Note that if loops
125 * end or start with a core synchronizing instruction, the model is appropriate.
126 * Nested calls are not supported.
130 * Each process have its own data in cache. Caches are randomly updated.
131 * smp_wmb and smp_rmb forces cache updates (write and read), smp_mb forces
135 typedef per_proc_byte {
139 typedef per_proc_bit {
143 /* Bitfield has a maximum of 8 procs */
144 typedef per_proc_bitfield {
148 #define DECLARE_CACHED_VAR(type, x) \
150 per_proc_##type cached_##x; \
151 per_proc_bitfield cache_dirty_##x;
153 #define INIT_CACHED_VAR(x, v, j) \
155 cache_dirty_##x.bitfield = 0; \
159 cached_##x.val[j] = v; \
161 :: j >= NR_PROCS -> break \
164 #define IS_CACHE_DIRTY(x, id) (cache_dirty_##x.bitfield & (1 << id))
166 #define READ_CACHED_VAR(x) (cached_##x.val[get_pid()])
168 #define WRITE_CACHED_VAR(x, v) \
170 cached_##x.val[get_pid()] = v; \
171 cache_dirty_##x.bitfield = \
172 cache_dirty_##x.bitfield | (1 << get_pid()); \
175 #define CACHE_WRITE_TO_MEM(x, id) \
177 :: IS_CACHE_DIRTY(x, id) -> \
178 mem_##x = cached_##x.val[id]; \
179 cache_dirty_##x.bitfield = \
180 cache_dirty_##x.bitfield & (~(1 << id)); \
185 #define CACHE_READ_FROM_MEM(x, id) \
187 :: !IS_CACHE_DIRTY(x, id) -> \
188 cached_##x.val[id] = mem_##x;\
194 * May update other caches if cache is dirty, or not.
196 #define RANDOM_CACHE_WRITE_TO_MEM(x, id)\
198 :: 1 -> CACHE_WRITE_TO_MEM(x, id); \
202 #define RANDOM_CACHE_READ_FROM_MEM(x, id)\
204 :: 1 -> CACHE_READ_FROM_MEM(x, id); \
208 /* Must consume all prior read tokens. All subsequent reads depend on it. */
212 CACHE_READ_FROM_MEM(urcu_gp_ctr, get_pid());
216 CACHE_READ_FROM_MEM(urcu_active_readers[i], get_pid());
218 :: i >= NR_READERS -> break
220 CACHE_READ_FROM_MEM(rcu_ptr, get_pid());
224 CACHE_READ_FROM_MEM(rcu_data[i], get_pid());
226 :: i >= SLAB_SIZE -> break
231 /* Must consume all prior write tokens. All subsequent writes depend on it. */
235 CACHE_WRITE_TO_MEM(urcu_gp_ctr, get_pid());
239 CACHE_WRITE_TO_MEM(urcu_active_readers[i], get_pid());
241 :: i >= NR_READERS -> break
243 CACHE_WRITE_TO_MEM(rcu_ptr, get_pid());
247 CACHE_WRITE_TO_MEM(rcu_data[i], get_pid());
249 :: i >= SLAB_SIZE -> break
254 /* Synchronization point. Must consume all prior read and write tokens. All
255 * subsequent reads and writes depend on it. */
264 #ifdef REMOTE_BARRIERS
266 bit reader_barrier[NR_READERS];
269 * We cannot leave the barriers dependencies in place in REMOTE_BARRIERS mode
270 * because they would add unexisting core synchronization and would therefore
271 * create an incomplete model.
272 * Therefore, we model the read-side memory barriers by completely disabling the
273 * memory barriers and their dependencies from the read-side. One at a time
274 * (different verification runs), we make a different instruction listen for
278 #define smp_mb_reader(i, j)
281 * Service 0, 1 or many barrier requests.
283 inline smp_mb_recv(i, j)
286 :: (reader_barrier[get_readerid()] == 1) ->
288 * We choose to ignore cycles caused by writer busy-looping,
289 * waiting for the reader, sending barrier requests, and the
290 * reader always services them without continuing execution.
292 progress_ignoring_mb1:
294 reader_barrier[get_readerid()] = 0;
297 * We choose to ignore writer's non-progress caused by the
298 * reader ignoring the writer's mb() requests.
300 progress_ignoring_mb2:
305 //#ifdef WRITER_PROGRESS
306 //#define PROGRESS_LABEL(progressid)
308 //#define PROGRESS_LABEL(progressid)
311 #define PROGRESS_LABEL(progressid) progress_writer_progid_##progressid:
313 #define smp_mb_send(i, j, progressid) \
318 :: i < NR_READERS -> \
319 reader_barrier[i] = 1; \
321 * Busy-looping waiting for reader barrier handling is of little\
322 * interest, given the reader has the ability to totally ignore \
323 * barrier requests. \
326 :: (reader_barrier[i] == 1) -> \
327 PROGRESS_LABEL(progressid) \
329 :: (reader_barrier[i] == 0) -> break; \
332 :: i >= NR_READERS -> \
340 #define smp_mb_send(i, j, progressid) smp_mb(i, j)
341 #define smp_mb_reader smp_mb
342 #define smp_mb_recv(i, j)
346 /* Keep in sync manually with smp_rmb, smp_wmb, ooo_mem and init() */
347 DECLARE_CACHED_VAR(byte, urcu_gp_ctr);
348 /* Note ! currently only one reader */
349 DECLARE_CACHED_VAR(byte, urcu_active_readers[NR_READERS]);
351 DECLARE_CACHED_VAR(bit, rcu_data[SLAB_SIZE]);
355 DECLARE_CACHED_VAR(bit, rcu_ptr);
356 bit ptr_read_first[NR_READERS];
357 bit ptr_read_second[NR_READERS];
359 DECLARE_CACHED_VAR(byte, rcu_ptr);
360 byte ptr_read_first[NR_READERS];
361 byte ptr_read_second[NR_READERS];
364 bit data_read_first[NR_READERS];
365 bit data_read_second[NR_READERS];
369 inline wait_init_done()
372 :: init_done == 0 -> skip;
380 RANDOM_CACHE_WRITE_TO_MEM(urcu_gp_ctr, get_pid());
384 RANDOM_CACHE_WRITE_TO_MEM(urcu_active_readers[i],
387 :: i >= NR_READERS -> break
389 RANDOM_CACHE_WRITE_TO_MEM(rcu_ptr, get_pid());
393 RANDOM_CACHE_WRITE_TO_MEM(rcu_data[i], get_pid());
395 :: i >= SLAB_SIZE -> break
397 RANDOM_CACHE_READ_FROM_MEM(urcu_gp_ctr, get_pid());
401 RANDOM_CACHE_READ_FROM_MEM(urcu_active_readers[i],
404 :: i >= NR_READERS -> break
406 RANDOM_CACHE_READ_FROM_MEM(rcu_ptr, get_pid());
410 RANDOM_CACHE_READ_FROM_MEM(rcu_data[i], get_pid());
412 :: i >= SLAB_SIZE -> break
418 * Bit encoding, urcu_reader :
421 int _proc_urcu_reader;
422 #define proc_urcu_reader _proc_urcu_reader
424 /* Body of PROCEDURE_READ_LOCK */
425 #define READ_PROD_A_READ (1 << 0)
426 #define READ_PROD_B_IF_TRUE (1 << 1)
427 #define READ_PROD_B_IF_FALSE (1 << 2)
428 #define READ_PROD_C_IF_TRUE_READ (1 << 3)
430 #define PROCEDURE_READ_LOCK(base, consumetoken, producetoken) \
431 :: CONSUME_TOKENS(proc_urcu_reader, consumetoken, READ_PROD_A_READ << base) -> \
433 tmp = READ_CACHED_VAR(urcu_active_readers[get_readerid()]); \
434 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_A_READ << base); \
435 :: CONSUME_TOKENS(proc_urcu_reader, \
436 READ_PROD_A_READ << base, /* RAW, pre-dominant */ \
437 (READ_PROD_B_IF_TRUE | READ_PROD_B_IF_FALSE) << base) -> \
439 :: (!(tmp & RCU_GP_CTR_NEST_MASK)) -> \
440 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_B_IF_TRUE << base); \
442 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_B_IF_FALSE << base); \
445 :: CONSUME_TOKENS(proc_urcu_reader, READ_PROD_B_IF_TRUE << base, \
446 READ_PROD_C_IF_TRUE_READ << base) -> \
448 tmp2 = READ_CACHED_VAR(urcu_gp_ctr); \
449 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_C_IF_TRUE_READ << base); \
450 :: CONSUME_TOKENS(proc_urcu_reader, \
451 (READ_PROD_C_IF_TRUE_READ /* pre-dominant */ \
452 | READ_PROD_A_READ) << base, /* WAR */ \
455 WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp2); \
456 PRODUCE_TOKENS(proc_urcu_reader, producetoken); \
457 /* IF_MERGE implies \
458 * post-dominance */ \
460 :: CONSUME_TOKENS(proc_urcu_reader, \
461 (READ_PROD_B_IF_FALSE /* pre-dominant */ \
462 | READ_PROD_A_READ) << base, /* WAR */ \
465 WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], \
467 PRODUCE_TOKENS(proc_urcu_reader, producetoken); \
468 /* IF_MERGE implies \
469 * post-dominance */ \
473 /* Body of PROCEDURE_READ_LOCK */
474 #define READ_PROC_READ_UNLOCK (1 << 0)
476 #define PROCEDURE_READ_UNLOCK(base, consumetoken, producetoken) \
477 :: CONSUME_TOKENS(proc_urcu_reader, \
479 READ_PROC_READ_UNLOCK << base) -> \
481 tmp2 = READ_CACHED_VAR(urcu_active_readers[get_readerid()]); \
482 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_READ_UNLOCK << base); \
483 :: CONSUME_TOKENS(proc_urcu_reader, \
485 | (READ_PROC_READ_UNLOCK << base), /* WAR */ \
488 WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp2 - 1); \
489 PRODUCE_TOKENS(proc_urcu_reader, producetoken); \
493 #define READ_PROD_NONE (1 << 0)
495 /* PROCEDURE_READ_LOCK base = << 1 : 1 to 5 */
496 #define READ_LOCK_BASE 1
497 #define READ_LOCK_OUT (1 << 5)
499 #define READ_PROC_FIRST_MB (1 << 6)
501 /* PROCEDURE_READ_LOCK (NESTED) base : << 7 : 7 to 11 */
502 #define READ_LOCK_NESTED_BASE 7
503 #define READ_LOCK_NESTED_OUT (1 << 11)
505 #define READ_PROC_READ_GEN (1 << 12)
506 #define READ_PROC_ACCESS_GEN (1 << 13)
508 /* PROCEDURE_READ_UNLOCK (NESTED) base = << 14 : 14 to 15 */
509 #define READ_UNLOCK_NESTED_BASE 14
510 #define READ_UNLOCK_NESTED_OUT (1 << 15)
512 #define READ_PROC_SECOND_MB (1 << 16)
514 /* PROCEDURE_READ_UNLOCK base = << 17 : 17 to 18 */
515 #define READ_UNLOCK_BASE 17
516 #define READ_UNLOCK_OUT (1 << 18)
518 /* PROCEDURE_READ_LOCK_UNROLL base = << 19 : 19 to 23 */
519 #define READ_LOCK_UNROLL_BASE 19
520 #define READ_LOCK_OUT_UNROLL (1 << 23)
522 #define READ_PROC_THIRD_MB (1 << 24)
524 #define READ_PROC_READ_GEN_UNROLL (1 << 25)
525 #define READ_PROC_ACCESS_GEN_UNROLL (1 << 26)
527 #define READ_PROC_FOURTH_MB (1 << 27)
529 /* PROCEDURE_READ_UNLOCK_UNROLL base = << 28 : 28 to 29 */
530 #define READ_UNLOCK_UNROLL_BASE 28
531 #define READ_UNLOCK_OUT_UNROLL (1 << 29)
534 /* Should not include branches */
535 #define READ_PROC_ALL_TOKENS (READ_PROD_NONE \
537 | READ_PROC_FIRST_MB \
538 | READ_LOCK_NESTED_OUT \
539 | READ_PROC_READ_GEN \
540 | READ_PROC_ACCESS_GEN \
541 | READ_UNLOCK_NESTED_OUT \
542 | READ_PROC_SECOND_MB \
544 | READ_LOCK_OUT_UNROLL \
545 | READ_PROC_THIRD_MB \
546 | READ_PROC_READ_GEN_UNROLL \
547 | READ_PROC_ACCESS_GEN_UNROLL \
548 | READ_PROC_FOURTH_MB \
549 | READ_UNLOCK_OUT_UNROLL)
551 /* Must clear all tokens, including branches */
552 #define READ_PROC_ALL_TOKENS_CLEAR ((1 << 30) - 1)
554 inline urcu_one_read(i, j, nest_i, tmp, tmp2)
556 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_NONE);
559 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FIRST_MB);
560 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_SECOND_MB);
561 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_THIRD_MB);
562 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FOURTH_MB);
565 #ifdef REMOTE_BARRIERS
566 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FIRST_MB);
567 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_SECOND_MB);
568 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_THIRD_MB);
569 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FOURTH_MB);
575 #ifdef REMOTE_BARRIERS
577 * Signal-based memory barrier will only execute when the
578 * execution order appears in program order.
584 :: CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE,
585 READ_LOCK_OUT | READ_LOCK_NESTED_OUT
586 | READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
588 | READ_LOCK_OUT_UNROLL
589 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
590 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT,
592 | READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
594 | READ_LOCK_OUT_UNROLL
595 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
596 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT | READ_LOCK_NESTED_OUT,
597 READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
599 | READ_LOCK_OUT_UNROLL
600 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
601 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
602 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN,
603 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
608 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN,
609 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
614 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN
615 | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT,
617 | READ_LOCK_OUT_UNROLL
618 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
619 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
620 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN
621 | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
624 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
625 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
626 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN
627 | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
628 | READ_UNLOCK_OUT | 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
633 | READ_UNLOCK_OUT | READ_LOCK_OUT_UNROLL
634 | READ_PROC_READ_GEN_UNROLL,
635 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
639 | READ_UNLOCK_OUT | READ_LOCK_OUT_UNROLL
640 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL,
641 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 | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
644 | READ_UNLOCK_OUT | READ_LOCK_OUT_UNROLL
645 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL,
654 goto non_atomic3_skip;
657 goto non_atomic3_end;
660 #endif /* REMOTE_BARRIERS */
664 PROCEDURE_READ_LOCK(READ_LOCK_BASE, READ_PROD_NONE, READ_LOCK_OUT);
666 :: CONSUME_TOKENS(proc_urcu_reader,
667 READ_LOCK_OUT, /* post-dominant */
668 READ_PROC_FIRST_MB) ->
670 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FIRST_MB);
672 PROCEDURE_READ_LOCK(READ_LOCK_NESTED_BASE, READ_PROC_FIRST_MB | READ_LOCK_OUT,
673 READ_LOCK_NESTED_OUT);
675 :: CONSUME_TOKENS(proc_urcu_reader,
676 READ_PROC_FIRST_MB, /* mb() orders reads */
677 READ_PROC_READ_GEN) ->
679 ptr_read_first[get_readerid()] = READ_CACHED_VAR(rcu_ptr);
680 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_READ_GEN);
682 :: CONSUME_TOKENS(proc_urcu_reader,
683 READ_PROC_FIRST_MB /* mb() orders reads */
684 | READ_PROC_READ_GEN,
685 READ_PROC_ACCESS_GEN) ->
686 /* smp_read_barrier_depends */
689 data_read_first[get_readerid()] =
690 READ_CACHED_VAR(rcu_data[ptr_read_first[get_readerid()]]);
691 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_ACCESS_GEN);
694 /* Note : we remove the nested memory barrier from the read unlock
695 * model, given it is not usually needed. The implementation has the barrier
696 * because the performance impact added by a branch in the common case does not
700 PROCEDURE_READ_UNLOCK(READ_UNLOCK_NESTED_BASE,
703 | READ_LOCK_NESTED_OUT,
704 READ_UNLOCK_NESTED_OUT);
707 :: CONSUME_TOKENS(proc_urcu_reader,
708 READ_PROC_ACCESS_GEN /* mb() orders reads */
709 | READ_PROC_READ_GEN /* mb() orders reads */
710 | READ_PROC_FIRST_MB /* mb() ordered */
711 | READ_LOCK_OUT /* post-dominant */
712 | READ_LOCK_NESTED_OUT /* post-dominant */
713 | READ_UNLOCK_NESTED_OUT,
714 READ_PROC_SECOND_MB) ->
716 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_SECOND_MB);
718 PROCEDURE_READ_UNLOCK(READ_UNLOCK_BASE,
719 READ_PROC_SECOND_MB /* mb() orders reads */
720 | READ_PROC_FIRST_MB /* mb() orders reads */
721 | READ_LOCK_NESTED_OUT /* RAW */
722 | READ_LOCK_OUT /* RAW */
723 | READ_UNLOCK_NESTED_OUT, /* RAW */
726 /* Unrolling loop : second consecutive lock */
727 /* reading urcu_active_readers, which have been written by
728 * READ_UNLOCK_OUT : RAW */
729 PROCEDURE_READ_LOCK(READ_LOCK_UNROLL_BASE,
730 READ_UNLOCK_OUT /* RAW */
731 | READ_PROC_SECOND_MB /* mb() orders reads */
732 | READ_PROC_FIRST_MB /* mb() orders reads */
733 | READ_LOCK_NESTED_OUT /* RAW */
734 | READ_LOCK_OUT /* RAW */
735 | READ_UNLOCK_NESTED_OUT, /* RAW */
736 READ_LOCK_OUT_UNROLL);
739 :: CONSUME_TOKENS(proc_urcu_reader,
740 READ_PROC_FIRST_MB /* mb() ordered */
741 | READ_PROC_SECOND_MB /* mb() ordered */
742 | READ_LOCK_OUT_UNROLL /* post-dominant */
743 | READ_LOCK_NESTED_OUT
745 | READ_UNLOCK_NESTED_OUT
747 READ_PROC_THIRD_MB) ->
749 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_THIRD_MB);
751 :: CONSUME_TOKENS(proc_urcu_reader,
752 READ_PROC_FIRST_MB /* mb() orders reads */
753 | READ_PROC_SECOND_MB /* mb() orders reads */
754 | READ_PROC_THIRD_MB, /* mb() orders reads */
755 READ_PROC_READ_GEN_UNROLL) ->
757 ptr_read_second[get_readerid()] = READ_CACHED_VAR(rcu_ptr);
758 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_READ_GEN_UNROLL);
760 :: CONSUME_TOKENS(proc_urcu_reader,
761 READ_PROC_READ_GEN_UNROLL
762 | READ_PROC_FIRST_MB /* mb() orders reads */
763 | READ_PROC_SECOND_MB /* mb() orders reads */
764 | READ_PROC_THIRD_MB, /* mb() orders reads */
765 READ_PROC_ACCESS_GEN_UNROLL) ->
766 /* smp_read_barrier_depends */
769 data_read_second[get_readerid()] =
770 READ_CACHED_VAR(rcu_data[ptr_read_second[get_readerid()]]);
771 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_ACCESS_GEN_UNROLL);
773 :: CONSUME_TOKENS(proc_urcu_reader,
774 READ_PROC_READ_GEN_UNROLL /* mb() orders reads */
775 | READ_PROC_ACCESS_GEN_UNROLL /* mb() orders reads */
776 | READ_PROC_FIRST_MB /* mb() ordered */
777 | READ_PROC_SECOND_MB /* mb() ordered */
778 | READ_PROC_THIRD_MB /* mb() ordered */
779 | READ_LOCK_OUT_UNROLL /* post-dominant */
780 | READ_LOCK_NESTED_OUT
782 | READ_UNLOCK_NESTED_OUT
784 READ_PROC_FOURTH_MB) ->
786 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FOURTH_MB);
788 PROCEDURE_READ_UNLOCK(READ_UNLOCK_UNROLL_BASE,
789 READ_PROC_FOURTH_MB /* mb() orders reads */
790 | READ_PROC_THIRD_MB /* mb() orders reads */
791 | READ_LOCK_OUT_UNROLL /* RAW */
792 | READ_PROC_SECOND_MB /* mb() orders reads */
793 | READ_PROC_FIRST_MB /* mb() orders reads */
794 | READ_LOCK_NESTED_OUT /* RAW */
795 | READ_LOCK_OUT /* RAW */
796 | READ_UNLOCK_NESTED_OUT, /* RAW */
797 READ_UNLOCK_OUT_UNROLL);
798 :: CONSUME_TOKENS(proc_urcu_reader, READ_PROC_ALL_TOKENS, 0) ->
799 CLEAR_TOKENS(proc_urcu_reader, READ_PROC_ALL_TOKENS_CLEAR);
805 * Dependency between consecutive loops :
807 * WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp2 - 1)
808 * tmp = READ_CACHED_VAR(urcu_active_readers[get_readerid()]);
810 * _WHEN THE MB()s are in place_, they add full ordering of the
811 * generation pointer read wrt active reader count read, which ensures
812 * execution will not spill across loop execution.
813 * However, in the event mb()s are removed (execution using signal
814 * handler to promote barrier()() -> smp_mb()), nothing prevents one loop
815 * to spill its execution on other loop's execution.
838 active proctype urcu_reader()
845 assert(get_pid() < NR_PROCS);
851 * We do not test reader's progress here, because we are mainly
852 * interested in writer's progress. The reader never blocks
853 * anyway. We have to test for reader/writer's progress
854 * separately, otherwise we could think the writer is doing
855 * progress when it's blocked by an always progressing reader.
857 #ifdef READER_PROGRESS
860 urcu_one_read(i, j, nest_i, tmp, tmp2);
864 /* no name clash please */
865 #undef proc_urcu_reader
868 /* Model the RCU update process. */
871 * Bit encoding, urcu_writer :
872 * Currently only supports one reader.
875 int _proc_urcu_writer;
876 #define proc_urcu_writer _proc_urcu_writer
878 #define WRITE_PROD_NONE (1 << 0)
880 #define WRITE_DATA (1 << 1)
881 #define WRITE_PROC_WMB (1 << 2)
882 #define WRITE_XCHG_PTR (1 << 3)
884 #define WRITE_PROC_FIRST_MB (1 << 4)
887 #define WRITE_PROC_FIRST_READ_GP (1 << 5)
888 #define WRITE_PROC_FIRST_WRITE_GP (1 << 6)
889 #define WRITE_PROC_FIRST_WAIT (1 << 7)
890 #define WRITE_PROC_FIRST_WAIT_LOOP (1 << 8)
893 #define WRITE_PROC_SECOND_READ_GP (1 << 9)
894 #define WRITE_PROC_SECOND_WRITE_GP (1 << 10)
895 #define WRITE_PROC_SECOND_WAIT (1 << 11)
896 #define WRITE_PROC_SECOND_WAIT_LOOP (1 << 12)
898 #define WRITE_PROC_SECOND_MB (1 << 13)
900 #define WRITE_FREE (1 << 14)
902 #define WRITE_PROC_ALL_TOKENS (WRITE_PROD_NONE \
906 | WRITE_PROC_FIRST_MB \
907 | WRITE_PROC_FIRST_READ_GP \
908 | WRITE_PROC_FIRST_WRITE_GP \
909 | WRITE_PROC_FIRST_WAIT \
910 | WRITE_PROC_SECOND_READ_GP \
911 | WRITE_PROC_SECOND_WRITE_GP \
912 | WRITE_PROC_SECOND_WAIT \
913 | WRITE_PROC_SECOND_MB \
916 #define WRITE_PROC_ALL_TOKENS_CLEAR ((1 << 15) - 1)
919 * Mutexes are implied around writer execution. A single writer at a time.
921 active proctype urcu_writer()
924 byte tmp, tmp2, tmpa;
925 byte cur_data = 0, old_data, loop_nr = 0;
926 byte cur_gp_val = 0; /*
927 * Keep a local trace of the current parity so
928 * we don't add non-existing dependencies on the global
929 * GP update. Needed to test single flip case.
934 assert(get_pid() < NR_PROCS);
938 #ifdef WRITER_PROGRESS
941 loop_nr = loop_nr + 1;
943 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROD_NONE);
946 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_WMB);
950 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_MB);
951 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_MB);
955 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_READ_GP);
956 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WRITE_GP);
957 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT);
958 /* For single flip, we need to know the current parity */
959 cur_gp_val = cur_gp_val ^ RCU_GP_CTR_BIT;
966 :: CONSUME_TOKENS(proc_urcu_writer,
970 cur_data = (cur_data + 1) % SLAB_SIZE;
971 WRITE_CACHED_VAR(rcu_data[cur_data], WINE);
972 PRODUCE_TOKENS(proc_urcu_writer, WRITE_DATA);
975 :: CONSUME_TOKENS(proc_urcu_writer,
979 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_WMB);
981 :: CONSUME_TOKENS(proc_urcu_writer,
984 /* rcu_xchg_pointer() */
986 old_data = READ_CACHED_VAR(rcu_ptr);
987 WRITE_CACHED_VAR(rcu_ptr, cur_data);
989 PRODUCE_TOKENS(proc_urcu_writer, WRITE_XCHG_PTR);
991 :: CONSUME_TOKENS(proc_urcu_writer,
992 WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR,
993 WRITE_PROC_FIRST_MB) ->
996 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_MB);
999 :: CONSUME_TOKENS(proc_urcu_writer,
1000 WRITE_PROC_FIRST_MB,
1001 WRITE_PROC_FIRST_READ_GP) ->
1002 tmpa = READ_CACHED_VAR(urcu_gp_ctr);
1003 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_READ_GP);
1004 :: CONSUME_TOKENS(proc_urcu_writer,
1005 WRITE_PROC_FIRST_MB | WRITE_PROC_WMB
1006 | WRITE_PROC_FIRST_READ_GP,
1007 WRITE_PROC_FIRST_WRITE_GP) ->
1009 WRITE_CACHED_VAR(urcu_gp_ctr, tmpa ^ RCU_GP_CTR_BIT);
1010 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WRITE_GP);
1012 :: CONSUME_TOKENS(proc_urcu_writer,
1013 //WRITE_PROC_FIRST_WRITE_GP /* TEST ADDING SYNC CORE */
1014 WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
1015 WRITE_PROC_FIRST_WAIT | WRITE_PROC_FIRST_WAIT_LOOP) ->
1017 /* ONLY WAITING FOR READER 0 */
1018 tmp2 = READ_CACHED_VAR(urcu_active_readers[0]);
1020 /* In normal execution, we are always starting by
1021 * waiting for the even parity.
1023 cur_gp_val = RCU_GP_CTR_BIT;
1026 :: (tmp2 & RCU_GP_CTR_NEST_MASK)
1027 && ((tmp2 ^ cur_gp_val) & RCU_GP_CTR_BIT) ->
1028 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WAIT_LOOP);
1030 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WAIT);
1033 :: CONSUME_TOKENS(proc_urcu_writer,
1034 //WRITE_PROC_FIRST_WRITE_GP /* TEST ADDING SYNC CORE */
1035 WRITE_PROC_FIRST_WRITE_GP
1036 | WRITE_PROC_FIRST_READ_GP
1037 | WRITE_PROC_FIRST_WAIT_LOOP
1038 | WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR
1039 | WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
1041 #ifndef GEN_ERROR_WRITER_PROGRESS
1047 /* This instruction loops to WRITE_PROC_FIRST_WAIT */
1048 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WAIT_LOOP | WRITE_PROC_FIRST_WAIT);
1051 :: CONSUME_TOKENS(proc_urcu_writer,
1052 WRITE_PROC_FIRST_WAIT /* Control dependency : need to branch out of
1053 * the loop to execute the next flip (CHECK) */
1054 | WRITE_PROC_FIRST_WRITE_GP
1055 | WRITE_PROC_FIRST_READ_GP
1056 | WRITE_PROC_FIRST_MB,
1057 WRITE_PROC_SECOND_READ_GP) ->
1059 tmpa = READ_CACHED_VAR(urcu_gp_ctr);
1060 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_READ_GP);
1061 :: CONSUME_TOKENS(proc_urcu_writer,
1064 | WRITE_PROC_FIRST_READ_GP
1065 | WRITE_PROC_FIRST_WRITE_GP
1066 | WRITE_PROC_SECOND_READ_GP,
1067 WRITE_PROC_SECOND_WRITE_GP) ->
1069 WRITE_CACHED_VAR(urcu_gp_ctr, tmpa ^ RCU_GP_CTR_BIT);
1070 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WRITE_GP);
1072 :: CONSUME_TOKENS(proc_urcu_writer,
1073 //WRITE_PROC_FIRST_WRITE_GP /* TEST ADDING SYNC CORE */
1074 WRITE_PROC_FIRST_WAIT
1075 | WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
1076 WRITE_PROC_SECOND_WAIT | WRITE_PROC_SECOND_WAIT_LOOP) ->
1078 /* ONLY WAITING FOR READER 0 */
1079 tmp2 = READ_CACHED_VAR(urcu_active_readers[0]);
1081 :: (tmp2 & RCU_GP_CTR_NEST_MASK)
1082 && ((tmp2 ^ 0) & RCU_GP_CTR_BIT) ->
1083 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT_LOOP);
1085 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT);
1088 :: CONSUME_TOKENS(proc_urcu_writer,
1089 //WRITE_PROC_FIRST_WRITE_GP /* TEST ADDING SYNC CORE */
1090 WRITE_PROC_SECOND_WRITE_GP
1091 | WRITE_PROC_FIRST_WRITE_GP
1092 | WRITE_PROC_SECOND_READ_GP
1093 | WRITE_PROC_FIRST_READ_GP
1094 | WRITE_PROC_SECOND_WAIT_LOOP
1095 | WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR
1096 | WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
1098 #ifndef GEN_ERROR_WRITER_PROGRESS
1104 /* This instruction loops to WRITE_PROC_SECOND_WAIT */
1105 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT_LOOP | WRITE_PROC_SECOND_WAIT);
1108 :: CONSUME_TOKENS(proc_urcu_writer,
1109 WRITE_PROC_FIRST_WAIT
1110 | WRITE_PROC_SECOND_WAIT
1111 | WRITE_PROC_FIRST_READ_GP
1112 | WRITE_PROC_SECOND_READ_GP
1113 | WRITE_PROC_FIRST_WRITE_GP
1114 | WRITE_PROC_SECOND_WRITE_GP
1115 | WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR
1116 | WRITE_PROC_FIRST_MB,
1117 WRITE_PROC_SECOND_MB) ->
1120 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_MB);
1122 :: CONSUME_TOKENS(proc_urcu_writer,
1124 | WRITE_PROC_FIRST_WAIT
1125 | WRITE_PROC_SECOND_WAIT
1126 | WRITE_PROC_WMB /* No dependency on
1127 * WRITE_DATA because we
1129 * different location. */
1130 | WRITE_PROC_SECOND_MB
1131 | WRITE_PROC_FIRST_MB,
1133 WRITE_CACHED_VAR(rcu_data[old_data], POISON);
1134 PRODUCE_TOKENS(proc_urcu_writer, WRITE_FREE);
1136 :: CONSUME_TOKENS(proc_urcu_writer, WRITE_PROC_ALL_TOKENS, 0) ->
1137 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_ALL_TOKENS_CLEAR);
1143 * Note : Promela model adds implicit serialization of the
1144 * WRITE_FREE instruction. Normally, it would be permitted to
1145 * spill on the next loop execution. Given the validation we do
1146 * checks for the data entry read to be poisoned, it's ok if
1147 * we do not check "late arriving" memory poisoning.
1152 * Given the reader loops infinitely, let the writer also busy-loop
1153 * with progress here so, with weak fairness, we can test the
1154 * writer's progress.
1159 #ifdef WRITER_PROGRESS
1162 #ifdef READER_PROGRESS
1164 * Make sure we don't block the reader's progress.
1166 smp_mb_send(i, j, 5);
1171 /* Non-atomic parts of the loop */
1174 smp_mb_send(i, j, 1);
1175 goto smp_mb_send1_end;
1176 #ifndef GEN_ERROR_WRITER_PROGRESS
1178 smp_mb_send(i, j, 2);
1179 goto smp_mb_send2_end;
1181 smp_mb_send(i, j, 3);
1182 goto smp_mb_send3_end;
1185 smp_mb_send(i, j, 4);
1186 goto smp_mb_send4_end;
1191 /* no name clash please */
1192 #undef proc_urcu_writer
1195 /* Leave after the readers and writers so the pid count is ok. */
1200 INIT_CACHED_VAR(urcu_gp_ctr, 1, j);
1201 INIT_CACHED_VAR(rcu_ptr, 0, j);
1205 :: i < NR_READERS ->
1206 INIT_CACHED_VAR(urcu_active_readers[i], 0, j);
1207 ptr_read_first[i] = 1;
1208 ptr_read_second[i] = 1;
1209 data_read_first[i] = WINE;
1210 data_read_second[i] = WINE;
1212 :: i >= NR_READERS -> break
1214 INIT_CACHED_VAR(rcu_data[0], WINE, j);
1218 INIT_CACHED_VAR(rcu_data[i], POISON, j);
1220 :: i >= SLAB_SIZE -> break