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 #define PROGRESS_LABEL(progressid) progress_writer_progid_##progressid:
307 #define smp_mb_send(i, j, progressid) \
312 :: i < NR_READERS -> \
313 reader_barrier[i] = 1; \
315 * Busy-looping waiting for reader barrier handling is of little\
316 * interest, given the reader has the ability to totally ignore \
317 * barrier requests. \
320 :: (reader_barrier[i] == 1) -> \
321 PROGRESS_LABEL(progressid) \
323 :: (reader_barrier[i] == 0) -> break; \
326 :: i >= NR_READERS -> \
334 #define smp_mb_send(i, j, progressid) smp_mb(i, j)
335 #define smp_mb_reader smp_mb
336 #define smp_mb_recv(i, j)
340 /* Keep in sync manually with smp_rmb, smp_wmb, ooo_mem and init() */
341 DECLARE_CACHED_VAR(byte, urcu_gp_ctr);
342 /* Note ! currently only one reader */
343 DECLARE_CACHED_VAR(byte, urcu_active_readers[NR_READERS]);
345 DECLARE_CACHED_VAR(bit, rcu_data[SLAB_SIZE]);
349 DECLARE_CACHED_VAR(bit, rcu_ptr);
350 bit ptr_read_first[NR_READERS];
351 bit ptr_read_second[NR_READERS];
353 DECLARE_CACHED_VAR(byte, rcu_ptr);
354 byte ptr_read_first[NR_READERS];
355 byte ptr_read_second[NR_READERS];
358 bit data_read_first[NR_READERS];
359 bit data_read_second[NR_READERS];
363 inline wait_init_done()
366 :: init_done == 0 -> skip;
374 RANDOM_CACHE_WRITE_TO_MEM(urcu_gp_ctr, get_pid());
378 RANDOM_CACHE_WRITE_TO_MEM(urcu_active_readers[i],
381 :: i >= NR_READERS -> break
383 RANDOM_CACHE_WRITE_TO_MEM(rcu_ptr, get_pid());
387 RANDOM_CACHE_WRITE_TO_MEM(rcu_data[i], get_pid());
389 :: i >= SLAB_SIZE -> break
391 RANDOM_CACHE_READ_FROM_MEM(urcu_gp_ctr, get_pid());
395 RANDOM_CACHE_READ_FROM_MEM(urcu_active_readers[i],
398 :: i >= NR_READERS -> break
400 RANDOM_CACHE_READ_FROM_MEM(rcu_ptr, get_pid());
404 RANDOM_CACHE_READ_FROM_MEM(rcu_data[i], get_pid());
406 :: i >= SLAB_SIZE -> break
412 * Bit encoding, urcu_reader :
415 int _proc_urcu_reader;
416 #define proc_urcu_reader _proc_urcu_reader
418 /* Body of PROCEDURE_READ_LOCK */
419 #define READ_PROD_A_READ (1 << 0)
420 #define READ_PROD_B_IF_TRUE (1 << 1)
421 #define READ_PROD_B_IF_FALSE (1 << 2)
422 #define READ_PROD_C_IF_TRUE_READ (1 << 3)
424 #define PROCEDURE_READ_LOCK(base, consumetoken, producetoken) \
425 :: CONSUME_TOKENS(proc_urcu_reader, consumetoken, READ_PROD_A_READ << base) -> \
427 tmp = READ_CACHED_VAR(urcu_active_readers[get_readerid()]); \
428 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_A_READ << base); \
429 :: CONSUME_TOKENS(proc_urcu_reader, \
430 READ_PROD_A_READ << base, /* RAW, pre-dominant */ \
431 (READ_PROD_B_IF_TRUE | READ_PROD_B_IF_FALSE) << base) -> \
433 :: (!(tmp & RCU_GP_CTR_NEST_MASK)) -> \
434 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_B_IF_TRUE << base); \
436 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_B_IF_FALSE << base); \
439 :: CONSUME_TOKENS(proc_urcu_reader, READ_PROD_B_IF_TRUE << base, \
440 READ_PROD_C_IF_TRUE_READ << base) -> \
442 tmp2 = READ_CACHED_VAR(urcu_gp_ctr); \
443 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_C_IF_TRUE_READ << base); \
444 :: CONSUME_TOKENS(proc_urcu_reader, \
445 (READ_PROD_C_IF_TRUE_READ /* pre-dominant */ \
446 | READ_PROD_A_READ) << base, /* WAR */ \
449 WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp2); \
450 PRODUCE_TOKENS(proc_urcu_reader, producetoken); \
451 /* IF_MERGE implies \
452 * post-dominance */ \
454 :: CONSUME_TOKENS(proc_urcu_reader, \
455 (READ_PROD_B_IF_FALSE /* pre-dominant */ \
456 | READ_PROD_A_READ) << base, /* WAR */ \
459 WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], \
461 PRODUCE_TOKENS(proc_urcu_reader, producetoken); \
462 /* IF_MERGE implies \
463 * post-dominance */ \
467 /* Body of PROCEDURE_READ_LOCK */
468 #define READ_PROC_READ_UNLOCK (1 << 0)
470 #define PROCEDURE_READ_UNLOCK(base, consumetoken, producetoken) \
471 :: CONSUME_TOKENS(proc_urcu_reader, \
473 READ_PROC_READ_UNLOCK << base) -> \
475 tmp2 = READ_CACHED_VAR(urcu_active_readers[get_readerid()]); \
476 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_READ_UNLOCK << base); \
477 :: CONSUME_TOKENS(proc_urcu_reader, \
479 | (READ_PROC_READ_UNLOCK << base), /* WAR */ \
482 WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp2 - 1); \
483 PRODUCE_TOKENS(proc_urcu_reader, producetoken); \
487 #define READ_PROD_NONE (1 << 0)
489 /* PROCEDURE_READ_LOCK base = << 1 : 1 to 5 */
490 #define READ_LOCK_BASE 1
491 #define READ_LOCK_OUT (1 << 5)
493 #define READ_PROC_FIRST_MB (1 << 6)
495 /* PROCEDURE_READ_LOCK (NESTED) base : << 7 : 7 to 11 */
496 #define READ_LOCK_NESTED_BASE 7
497 #define READ_LOCK_NESTED_OUT (1 << 11)
499 #define READ_PROC_READ_GEN (1 << 12)
500 #define READ_PROC_ACCESS_GEN (1 << 13)
502 /* PROCEDURE_READ_UNLOCK (NESTED) base = << 14 : 14 to 15 */
503 #define READ_UNLOCK_NESTED_BASE 14
504 #define READ_UNLOCK_NESTED_OUT (1 << 15)
506 #define READ_PROC_SECOND_MB (1 << 16)
508 /* PROCEDURE_READ_UNLOCK base = << 17 : 17 to 18 */
509 #define READ_UNLOCK_BASE 17
510 #define READ_UNLOCK_OUT (1 << 18)
512 /* PROCEDURE_READ_LOCK_UNROLL base = << 19 : 19 to 23 */
513 #define READ_LOCK_UNROLL_BASE 19
514 #define READ_LOCK_OUT_UNROLL (1 << 23)
516 #define READ_PROC_THIRD_MB (1 << 24)
518 #define READ_PROC_READ_GEN_UNROLL (1 << 25)
519 #define READ_PROC_ACCESS_GEN_UNROLL (1 << 26)
521 #define READ_PROC_FOURTH_MB (1 << 27)
523 /* PROCEDURE_READ_UNLOCK_UNROLL base = << 28 : 28 to 29 */
524 #define READ_UNLOCK_UNROLL_BASE 28
525 #define READ_UNLOCK_OUT_UNROLL (1 << 29)
528 /* Should not include branches */
529 #define READ_PROC_ALL_TOKENS (READ_PROD_NONE \
531 | READ_PROC_FIRST_MB \
532 | READ_LOCK_NESTED_OUT \
533 | READ_PROC_READ_GEN \
534 | READ_PROC_ACCESS_GEN \
535 | READ_UNLOCK_NESTED_OUT \
536 | READ_PROC_SECOND_MB \
538 | READ_LOCK_OUT_UNROLL \
539 | READ_PROC_THIRD_MB \
540 | READ_PROC_READ_GEN_UNROLL \
541 | READ_PROC_ACCESS_GEN_UNROLL \
542 | READ_PROC_FOURTH_MB \
543 | READ_UNLOCK_OUT_UNROLL)
545 /* Must clear all tokens, including branches */
546 #define READ_PROC_ALL_TOKENS_CLEAR ((1 << 30) - 1)
548 inline urcu_one_read(i, j, nest_i, tmp, tmp2)
550 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_NONE);
553 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FIRST_MB);
554 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_SECOND_MB);
555 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_THIRD_MB);
556 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FOURTH_MB);
559 #ifdef REMOTE_BARRIERS
560 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FIRST_MB);
561 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_SECOND_MB);
562 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_THIRD_MB);
563 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FOURTH_MB);
569 #ifdef REMOTE_BARRIERS
571 * Signal-based memory barrier will only execute when the
572 * execution order appears in program order.
578 :: CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE,
579 READ_LOCK_OUT | READ_LOCK_NESTED_OUT
580 | READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
582 | READ_LOCK_OUT_UNROLL
583 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
584 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_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 | READ_LOCK_NESTED_OUT,
591 READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
593 | READ_LOCK_OUT_UNROLL
594 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
595 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
596 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN,
597 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 | READ_PROC_ACCESS_GEN,
603 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
609 | 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
614 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN
615 | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
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
622 | READ_UNLOCK_OUT | 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
626 | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
627 | READ_UNLOCK_OUT | READ_LOCK_OUT_UNROLL
628 | READ_PROC_READ_GEN_UNROLL,
629 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 | READ_PROC_ACCESS_GEN_UNROLL,
635 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 | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
638 | READ_UNLOCK_OUT | READ_LOCK_OUT_UNROLL
639 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL,
648 goto non_atomic3_skip;
651 goto non_atomic3_end;
654 #endif /* REMOTE_BARRIERS */
658 PROCEDURE_READ_LOCK(READ_LOCK_BASE, READ_PROD_NONE, READ_LOCK_OUT);
660 :: CONSUME_TOKENS(proc_urcu_reader,
661 READ_LOCK_OUT, /* post-dominant */
662 READ_PROC_FIRST_MB) ->
664 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FIRST_MB);
666 PROCEDURE_READ_LOCK(READ_LOCK_NESTED_BASE, READ_PROC_FIRST_MB | READ_LOCK_OUT,
667 READ_LOCK_NESTED_OUT);
669 :: CONSUME_TOKENS(proc_urcu_reader,
670 READ_PROC_FIRST_MB, /* mb() orders reads */
671 READ_PROC_READ_GEN) ->
673 ptr_read_first[get_readerid()] = READ_CACHED_VAR(rcu_ptr);
674 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_READ_GEN);
676 :: CONSUME_TOKENS(proc_urcu_reader,
677 READ_PROC_FIRST_MB /* mb() orders reads */
678 | READ_PROC_READ_GEN,
679 READ_PROC_ACCESS_GEN) ->
680 /* smp_read_barrier_depends */
683 data_read_first[get_readerid()] =
684 READ_CACHED_VAR(rcu_data[ptr_read_first[get_readerid()]]);
685 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_ACCESS_GEN);
688 /* Note : we remove the nested memory barrier from the read unlock
689 * model, given it is not usually needed. The implementation has the barrier
690 * because the performance impact added by a branch in the common case does not
694 PROCEDURE_READ_UNLOCK(READ_UNLOCK_NESTED_BASE,
697 | READ_LOCK_NESTED_OUT,
698 READ_UNLOCK_NESTED_OUT);
701 :: CONSUME_TOKENS(proc_urcu_reader,
702 READ_PROC_ACCESS_GEN /* mb() orders reads */
703 | READ_PROC_READ_GEN /* mb() orders reads */
704 | READ_PROC_FIRST_MB /* mb() ordered */
705 | READ_LOCK_OUT /* post-dominant */
706 | READ_LOCK_NESTED_OUT /* post-dominant */
707 | READ_UNLOCK_NESTED_OUT,
708 READ_PROC_SECOND_MB) ->
710 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_SECOND_MB);
712 PROCEDURE_READ_UNLOCK(READ_UNLOCK_BASE,
713 READ_PROC_SECOND_MB /* mb() orders reads */
714 | READ_PROC_FIRST_MB /* mb() orders reads */
715 | READ_LOCK_NESTED_OUT /* RAW */
716 | READ_LOCK_OUT /* RAW */
717 | READ_UNLOCK_NESTED_OUT, /* RAW */
720 /* Unrolling loop : second consecutive lock */
721 /* reading urcu_active_readers, which have been written by
722 * READ_UNLOCK_OUT : RAW */
723 PROCEDURE_READ_LOCK(READ_LOCK_UNROLL_BASE,
724 READ_UNLOCK_OUT /* RAW */
725 | READ_PROC_SECOND_MB /* mb() orders reads */
726 | READ_PROC_FIRST_MB /* mb() orders reads */
727 | READ_LOCK_NESTED_OUT /* RAW */
728 | READ_LOCK_OUT /* RAW */
729 | READ_UNLOCK_NESTED_OUT, /* RAW */
730 READ_LOCK_OUT_UNROLL);
733 :: CONSUME_TOKENS(proc_urcu_reader,
734 READ_PROC_FIRST_MB /* mb() ordered */
735 | READ_PROC_SECOND_MB /* mb() ordered */
736 | READ_LOCK_OUT_UNROLL /* post-dominant */
737 | READ_LOCK_NESTED_OUT
739 | READ_UNLOCK_NESTED_OUT
741 READ_PROC_THIRD_MB) ->
743 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_THIRD_MB);
745 :: CONSUME_TOKENS(proc_urcu_reader,
746 READ_PROC_FIRST_MB /* mb() orders reads */
747 | READ_PROC_SECOND_MB /* mb() orders reads */
748 | READ_PROC_THIRD_MB, /* mb() orders reads */
749 READ_PROC_READ_GEN_UNROLL) ->
751 ptr_read_second[get_readerid()] = READ_CACHED_VAR(rcu_ptr);
752 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_READ_GEN_UNROLL);
754 :: CONSUME_TOKENS(proc_urcu_reader,
755 READ_PROC_READ_GEN_UNROLL
756 | READ_PROC_FIRST_MB /* mb() orders reads */
757 | READ_PROC_SECOND_MB /* mb() orders reads */
758 | READ_PROC_THIRD_MB, /* mb() orders reads */
759 READ_PROC_ACCESS_GEN_UNROLL) ->
760 /* smp_read_barrier_depends */
763 data_read_second[get_readerid()] =
764 READ_CACHED_VAR(rcu_data[ptr_read_second[get_readerid()]]);
765 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_ACCESS_GEN_UNROLL);
767 :: CONSUME_TOKENS(proc_urcu_reader,
768 READ_PROC_READ_GEN_UNROLL /* mb() orders reads */
769 | READ_PROC_ACCESS_GEN_UNROLL /* mb() orders reads */
770 | READ_PROC_FIRST_MB /* mb() ordered */
771 | READ_PROC_SECOND_MB /* mb() ordered */
772 | READ_PROC_THIRD_MB /* mb() ordered */
773 | READ_LOCK_OUT_UNROLL /* post-dominant */
774 | READ_LOCK_NESTED_OUT
776 | READ_UNLOCK_NESTED_OUT
778 READ_PROC_FOURTH_MB) ->
780 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FOURTH_MB);
782 PROCEDURE_READ_UNLOCK(READ_UNLOCK_UNROLL_BASE,
783 READ_PROC_FOURTH_MB /* mb() orders reads */
784 | READ_PROC_THIRD_MB /* mb() orders reads */
785 | READ_LOCK_OUT_UNROLL /* RAW */
786 | READ_PROC_SECOND_MB /* mb() orders reads */
787 | READ_PROC_FIRST_MB /* mb() orders reads */
788 | READ_LOCK_NESTED_OUT /* RAW */
789 | READ_LOCK_OUT /* RAW */
790 | READ_UNLOCK_NESTED_OUT, /* RAW */
791 READ_UNLOCK_OUT_UNROLL);
792 :: CONSUME_TOKENS(proc_urcu_reader, READ_PROC_ALL_TOKENS, 0) ->
793 CLEAR_TOKENS(proc_urcu_reader, READ_PROC_ALL_TOKENS_CLEAR);
799 * Dependency between consecutive loops :
801 * WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp2 - 1)
802 * tmp = READ_CACHED_VAR(urcu_active_readers[get_readerid()]);
804 * _WHEN THE MB()s are in place_, they add full ordering of the
805 * generation pointer read wrt active reader count read, which ensures
806 * execution will not spill across loop execution.
807 * However, in the event mb()s are removed (execution using signal
808 * handler to promote barrier()() -> smp_mb()), nothing prevents one loop
809 * to spill its execution on other loop's execution.
832 active proctype urcu_reader()
839 assert(get_pid() < NR_PROCS);
845 * We do not test reader's progress here, because we are mainly
846 * interested in writer's progress. The reader never blocks
847 * anyway. We have to test for reader/writer's progress
848 * separately, otherwise we could think the writer is doing
849 * progress when it's blocked by an always progressing reader.
851 #ifdef READER_PROGRESS
854 urcu_one_read(i, j, nest_i, tmp, tmp2);
858 /* no name clash please */
859 #undef proc_urcu_reader
862 /* Model the RCU update process. */
865 * Bit encoding, urcu_writer :
866 * Currently only supports one reader.
869 int _proc_urcu_writer;
870 #define proc_urcu_writer _proc_urcu_writer
872 #define WRITE_PROD_NONE (1 << 0)
874 #define WRITE_DATA (1 << 1)
875 #define WRITE_PROC_WMB (1 << 2)
876 #define WRITE_XCHG_PTR (1 << 3)
878 #define WRITE_PROC_FIRST_MB (1 << 4)
881 #define WRITE_PROC_FIRST_READ_GP (1 << 5)
882 #define WRITE_PROC_FIRST_WRITE_GP (1 << 6)
883 #define WRITE_PROC_FIRST_WAIT (1 << 7)
884 #define WRITE_PROC_FIRST_WAIT_LOOP (1 << 8)
887 #define WRITE_PROC_SECOND_READ_GP (1 << 9)
888 #define WRITE_PROC_SECOND_WRITE_GP (1 << 10)
889 #define WRITE_PROC_SECOND_WAIT (1 << 11)
890 #define WRITE_PROC_SECOND_WAIT_LOOP (1 << 12)
892 #define WRITE_PROC_SECOND_MB (1 << 13)
894 #define WRITE_FREE (1 << 14)
896 #define WRITE_PROC_ALL_TOKENS (WRITE_PROD_NONE \
900 | WRITE_PROC_FIRST_MB \
901 | WRITE_PROC_FIRST_READ_GP \
902 | WRITE_PROC_FIRST_WRITE_GP \
903 | WRITE_PROC_FIRST_WAIT \
904 | WRITE_PROC_SECOND_READ_GP \
905 | WRITE_PROC_SECOND_WRITE_GP \
906 | WRITE_PROC_SECOND_WAIT \
907 | WRITE_PROC_SECOND_MB \
910 #define WRITE_PROC_ALL_TOKENS_CLEAR ((1 << 15) - 1)
913 * Mutexes are implied around writer execution. A single writer at a time.
915 active proctype urcu_writer()
918 byte tmp, tmp2, tmpa;
919 byte cur_data = 0, old_data, loop_nr = 0;
920 byte cur_gp_val = 0; /*
921 * Keep a local trace of the current parity so
922 * we don't add non-existing dependencies on the global
923 * GP update. Needed to test single flip case.
928 assert(get_pid() < NR_PROCS);
932 #ifdef WRITER_PROGRESS
935 loop_nr = loop_nr + 1;
937 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROD_NONE);
940 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_WMB);
944 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_MB);
945 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_MB);
949 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_READ_GP);
950 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WRITE_GP);
951 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT);
952 /* For single flip, we need to know the current parity */
953 cur_gp_val = cur_gp_val ^ RCU_GP_CTR_BIT;
960 :: CONSUME_TOKENS(proc_urcu_writer,
964 cur_data = (cur_data + 1) % SLAB_SIZE;
965 WRITE_CACHED_VAR(rcu_data[cur_data], WINE);
966 PRODUCE_TOKENS(proc_urcu_writer, WRITE_DATA);
969 :: CONSUME_TOKENS(proc_urcu_writer,
973 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_WMB);
975 :: CONSUME_TOKENS(proc_urcu_writer,
978 /* rcu_xchg_pointer() */
980 old_data = READ_CACHED_VAR(rcu_ptr);
981 WRITE_CACHED_VAR(rcu_ptr, cur_data);
983 PRODUCE_TOKENS(proc_urcu_writer, WRITE_XCHG_PTR);
985 :: CONSUME_TOKENS(proc_urcu_writer,
986 WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR,
987 WRITE_PROC_FIRST_MB) ->
990 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_MB);
993 :: CONSUME_TOKENS(proc_urcu_writer,
995 WRITE_PROC_FIRST_READ_GP) ->
996 tmpa = READ_CACHED_VAR(urcu_gp_ctr);
997 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_READ_GP);
998 :: CONSUME_TOKENS(proc_urcu_writer,
999 WRITE_PROC_FIRST_MB | WRITE_PROC_WMB
1000 | WRITE_PROC_FIRST_READ_GP,
1001 WRITE_PROC_FIRST_WRITE_GP) ->
1003 WRITE_CACHED_VAR(urcu_gp_ctr, tmpa ^ RCU_GP_CTR_BIT);
1004 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WRITE_GP);
1006 :: CONSUME_TOKENS(proc_urcu_writer,
1007 //WRITE_PROC_FIRST_WRITE_GP /* TEST ADDING SYNC CORE */
1008 WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
1009 WRITE_PROC_FIRST_WAIT | WRITE_PROC_FIRST_WAIT_LOOP) ->
1011 /* ONLY WAITING FOR READER 0 */
1012 tmp2 = READ_CACHED_VAR(urcu_active_readers[0]);
1014 /* In normal execution, we are always starting by
1015 * waiting for the even parity.
1017 cur_gp_val = RCU_GP_CTR_BIT;
1020 :: (tmp2 & RCU_GP_CTR_NEST_MASK)
1021 && ((tmp2 ^ cur_gp_val) & RCU_GP_CTR_BIT) ->
1022 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WAIT_LOOP);
1024 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WAIT);
1027 :: CONSUME_TOKENS(proc_urcu_writer,
1028 //WRITE_PROC_FIRST_WRITE_GP /* TEST ADDING SYNC CORE */
1029 WRITE_PROC_FIRST_WRITE_GP
1030 | WRITE_PROC_FIRST_READ_GP
1031 | WRITE_PROC_FIRST_WAIT_LOOP
1032 | WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR
1033 | WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
1035 #ifndef GEN_ERROR_WRITER_PROGRESS
1041 /* This instruction loops to WRITE_PROC_FIRST_WAIT */
1042 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WAIT_LOOP | WRITE_PROC_FIRST_WAIT);
1045 :: CONSUME_TOKENS(proc_urcu_writer,
1046 WRITE_PROC_FIRST_WAIT /* Control dependency : need to branch out of
1047 * the loop to execute the next flip (CHECK) */
1048 | WRITE_PROC_FIRST_WRITE_GP
1049 | WRITE_PROC_FIRST_READ_GP
1050 | WRITE_PROC_FIRST_MB,
1051 WRITE_PROC_SECOND_READ_GP) ->
1053 tmpa = READ_CACHED_VAR(urcu_gp_ctr);
1054 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_READ_GP);
1055 :: CONSUME_TOKENS(proc_urcu_writer,
1058 | WRITE_PROC_FIRST_READ_GP
1059 | WRITE_PROC_FIRST_WRITE_GP
1060 | WRITE_PROC_SECOND_READ_GP,
1061 WRITE_PROC_SECOND_WRITE_GP) ->
1063 WRITE_CACHED_VAR(urcu_gp_ctr, tmpa ^ RCU_GP_CTR_BIT);
1064 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WRITE_GP);
1066 :: CONSUME_TOKENS(proc_urcu_writer,
1067 //WRITE_PROC_FIRST_WRITE_GP /* TEST ADDING SYNC CORE */
1068 WRITE_PROC_FIRST_WAIT
1069 | WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
1070 WRITE_PROC_SECOND_WAIT | WRITE_PROC_SECOND_WAIT_LOOP) ->
1072 /* ONLY WAITING FOR READER 0 */
1073 tmp2 = READ_CACHED_VAR(urcu_active_readers[0]);
1075 :: (tmp2 & RCU_GP_CTR_NEST_MASK)
1076 && ((tmp2 ^ 0) & RCU_GP_CTR_BIT) ->
1077 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT_LOOP);
1079 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT);
1082 :: CONSUME_TOKENS(proc_urcu_writer,
1083 //WRITE_PROC_FIRST_WRITE_GP /* TEST ADDING SYNC CORE */
1084 WRITE_PROC_SECOND_WRITE_GP
1085 | WRITE_PROC_FIRST_WRITE_GP
1086 | WRITE_PROC_SECOND_READ_GP
1087 | WRITE_PROC_FIRST_READ_GP
1088 | WRITE_PROC_SECOND_WAIT_LOOP
1089 | WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR
1090 | WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
1092 #ifndef GEN_ERROR_WRITER_PROGRESS
1098 /* This instruction loops to WRITE_PROC_SECOND_WAIT */
1099 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT_LOOP | WRITE_PROC_SECOND_WAIT);
1102 :: CONSUME_TOKENS(proc_urcu_writer,
1103 WRITE_PROC_FIRST_WAIT
1104 | WRITE_PROC_SECOND_WAIT
1105 | WRITE_PROC_FIRST_READ_GP
1106 | WRITE_PROC_SECOND_READ_GP
1107 | WRITE_PROC_FIRST_WRITE_GP
1108 | WRITE_PROC_SECOND_WRITE_GP
1109 | WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR
1110 | WRITE_PROC_FIRST_MB,
1111 WRITE_PROC_SECOND_MB) ->
1114 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_MB);
1116 :: CONSUME_TOKENS(proc_urcu_writer,
1118 | WRITE_PROC_FIRST_WAIT
1119 | WRITE_PROC_SECOND_WAIT
1120 | WRITE_PROC_WMB /* No dependency on
1121 * WRITE_DATA because we
1123 * different location. */
1124 | WRITE_PROC_SECOND_MB
1125 | WRITE_PROC_FIRST_MB,
1127 WRITE_CACHED_VAR(rcu_data[old_data], POISON);
1128 PRODUCE_TOKENS(proc_urcu_writer, WRITE_FREE);
1130 :: CONSUME_TOKENS(proc_urcu_writer, WRITE_PROC_ALL_TOKENS, 0) ->
1131 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_ALL_TOKENS_CLEAR);
1137 * Note : Promela model adds implicit serialization of the
1138 * WRITE_FREE instruction. Normally, it would be permitted to
1139 * spill on the next loop execution. Given the validation we do
1140 * checks for the data entry read to be poisoned, it's ok if
1141 * we do not check "late arriving" memory poisoning.
1146 * Given the reader loops infinitely, let the writer also busy-loop
1147 * with progress here so, with weak fairness, we can test the
1148 * writer's progress.
1153 #ifdef WRITER_PROGRESS
1156 #ifdef READER_PROGRESS
1158 * Make sure we don't block the reader's progress.
1160 smp_mb_send(i, j, 5);
1165 /* Non-atomic parts of the loop */
1168 smp_mb_send(i, j, 1);
1169 goto smp_mb_send1_end;
1170 #ifndef GEN_ERROR_WRITER_PROGRESS
1172 smp_mb_send(i, j, 2);
1173 goto smp_mb_send2_end;
1175 smp_mb_send(i, j, 3);
1176 goto smp_mb_send3_end;
1179 smp_mb_send(i, j, 4);
1180 goto smp_mb_send4_end;
1185 /* no name clash please */
1186 #undef proc_urcu_writer
1189 /* Leave after the readers and writers so the pid count is ok. */
1194 INIT_CACHED_VAR(urcu_gp_ctr, 1, j);
1195 INIT_CACHED_VAR(rcu_ptr, 0, j);
1199 :: i < NR_READERS ->
1200 INIT_CACHED_VAR(urcu_active_readers[i], 0, j);
1201 ptr_read_first[i] = 1;
1202 ptr_read_second[i] = 1;
1203 data_read_first[i] = WINE;
1204 data_read_second[i] = WINE;
1206 :: i >= NR_READERS -> break
1208 INIT_CACHED_VAR(rcu_data[0], WINE, j);
1212 INIT_CACHED_VAR(rcu_data[i], POISON, j);
1214 :: i >= SLAB_SIZE -> break