3 // Poison value for freed memory
5 // Memory with correct data
9 #define read_poison (data_read_first[0] == POISON || data_read_second[0] == POISON)
11 #define RCU_GP_CTR_BIT (1 << 7)
12 #define RCU_GP_CTR_NEST_MASK (RCU_GP_CTR_BIT - 1)
15 //#define REMOTE_BARRIERS
17 * mem.spin: Promela code to validate memory barriers with OOO memory
18 * and out-of-order instruction scheduling.
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
34 * Copyright (c) 2009 Mathieu Desnoyers
37 /* Promela validation variables. */
39 /* specific defines "included" here */
40 /* DEFINES file "included" here */
47 #define get_pid() (_pid)
49 #define get_readerid() (get_pid())
52 * Produced process control and data flow. Updated after each instruction to
53 * show which variables are ready. Using one-hot bit encoding per variable to
54 * save state space. Used as triggers to execute the instructions having those
55 * variables as input. Leaving bits active to inhibit instruction execution.
56 * Scheme used to make instruction disabling and automatic dependency fall-back
60 #define CONSUME_TOKENS(state, bits, notbits) \
61 ((!(state & (notbits))) && (state & (bits)) == (bits))
63 #define PRODUCE_TOKENS(state, bits) \
64 state = state | (bits);
66 #define CLEAR_TOKENS(state, bits) \
67 state = state & ~(bits)
70 * Types of dependency :
74 * - True dependency, Read-after-Write (RAW)
76 * This type of dependency happens when a statement depends on the result of a
77 * previous statement. This applies to any statement which needs to read a
78 * variable written by a preceding statement.
80 * - False dependency, Write-after-Read (WAR)
82 * Typically, variable renaming can ensure that this dependency goes away.
83 * However, if the statements must read and then write from/to the same variable
84 * in the OOO memory model, renaming may be impossible, and therefore this
85 * causes a WAR dependency.
87 * - Output dependency, Write-after-Write (WAW)
89 * Two writes to the same variable in subsequent statements. Variable renaming
90 * can ensure this is not needed, but can be required when writing multiple
91 * times to the same OOO mem model variable.
95 * Execution of a given instruction depends on a previous instruction evaluating
96 * in a way that allows its execution. E.g. : branches.
98 * Useful considerations for joining dependencies after branch
102 * "We say box i dominates box j if every path (leading from input to output
103 * through the diagram) which passes through box j must also pass through box
104 * i. Thus box i dominates box j if box j is subordinate to box i in the
107 * http://www.hipersoft.rice.edu/grads/publications/dom14.pdf
108 * Other classic algorithm to calculate dominance : Lengauer-Tarjan (in gcc)
112 * Just as pre-dominance, but with arcs of the data flow inverted, and input vs
113 * output exchanged. Therefore, i post-dominating j ensures that every path
114 * passing by j will pass by i before reaching the output.
116 * Other considerations
118 * Note about "volatile" keyword dependency : The compiler will order volatile
119 * accesses so they appear in the right order on a given CPU. They can be
120 * reordered by the CPU instruction scheduling. This therefore cannot be
121 * considered as a depencency.
125 * Cooper, Keith D.; & Torczon, Linda. (2005). Engineering a Compiler. Morgan
126 * Kaufmann. ISBN 1-55860-698-X.
127 * Kennedy, Ken; & Allen, Randy. (2001). Optimizing Compilers for Modern
128 * Architectures: A Dependence-based Approach. Morgan Kaufmann. ISBN
130 * Muchnick, Steven S. (1997). Advanced Compiler Design and Implementation.
131 * Morgan Kaufmann. ISBN 1-55860-320-4.
135 * Note about loops and nested calls
137 * To keep this model simple, loops expressed in the framework will behave as if
138 * there was a core synchronizing instruction between loops. To see the effect
139 * of loop unrolling, manually unrolling loops is required. Note that if loops
140 * end or start with a core synchronizing instruction, the model is appropriate.
141 * Nested calls are not supported.
145 * Each process have its own data in cache. Caches are randomly updated.
146 * smp_wmb and smp_rmb forces cache updates (write and read), smp_mb forces
150 typedef per_proc_byte {
154 typedef per_proc_bit {
158 /* Bitfield has a maximum of 8 procs */
159 typedef per_proc_bitfield {
163 #define DECLARE_CACHED_VAR(type, x) \
165 per_proc_##type cached_##x; \
166 per_proc_bitfield cache_dirty_##x;
168 #define INIT_CACHED_VAR(x, v, j) \
170 cache_dirty_##x.bitfield = 0; \
174 cached_##x.val[j] = v; \
176 :: j >= NR_PROCS -> break \
179 #define IS_CACHE_DIRTY(x, id) (cache_dirty_##x.bitfield & (1 << id))
181 #define READ_CACHED_VAR(x) (cached_##x.val[get_pid()])
183 #define WRITE_CACHED_VAR(x, v) \
185 cached_##x.val[get_pid()] = v; \
186 cache_dirty_##x.bitfield = \
187 cache_dirty_##x.bitfield | (1 << get_pid()); \
190 #define CACHE_WRITE_TO_MEM(x, id) \
192 :: IS_CACHE_DIRTY(x, id) -> \
193 mem_##x = cached_##x.val[id]; \
194 cache_dirty_##x.bitfield = \
195 cache_dirty_##x.bitfield & (~(1 << id)); \
200 #define CACHE_READ_FROM_MEM(x, id) \
202 :: !IS_CACHE_DIRTY(x, id) -> \
203 cached_##x.val[id] = mem_##x;\
209 * May update other caches if cache is dirty, or not.
211 #define RANDOM_CACHE_WRITE_TO_MEM(x, id)\
213 :: 1 -> CACHE_WRITE_TO_MEM(x, id); \
217 #define RANDOM_CACHE_READ_FROM_MEM(x, id)\
219 :: 1 -> CACHE_READ_FROM_MEM(x, id); \
223 /* Must consume all prior read tokens. All subsequent reads depend on it. */
227 CACHE_READ_FROM_MEM(urcu_gp_ctr, get_pid());
231 CACHE_READ_FROM_MEM(urcu_active_readers[i], get_pid());
233 :: i >= NR_READERS -> break
235 CACHE_READ_FROM_MEM(rcu_ptr, get_pid());
239 CACHE_READ_FROM_MEM(rcu_data[i], get_pid());
241 :: i >= SLAB_SIZE -> break
246 /* Must consume all prior write tokens. All subsequent writes depend on it. */
250 CACHE_WRITE_TO_MEM(urcu_gp_ctr, get_pid());
254 CACHE_WRITE_TO_MEM(urcu_active_readers[i], get_pid());
256 :: i >= NR_READERS -> break
258 CACHE_WRITE_TO_MEM(rcu_ptr, get_pid());
262 CACHE_WRITE_TO_MEM(rcu_data[i], get_pid());
264 :: i >= SLAB_SIZE -> break
269 /* Synchronization point. Must consume all prior read and write tokens. All
270 * subsequent reads and writes depend on it. */
279 #ifdef REMOTE_BARRIERS
281 bit reader_barrier[NR_READERS];
284 * We cannot leave the barriers dependencies in place in REMOTE_BARRIERS mode
285 * because they would add unexisting core synchronization and would therefore
286 * create an incomplete model.
287 * Therefore, we model the read-side memory barriers by completely disabling the
288 * memory barriers and their dependencies from the read-side. One at a time
289 * (different verification runs), we make a different instruction listen for
293 #define smp_mb_reader(i, j)
296 * Service 0, 1 or many barrier requests.
298 inline smp_mb_recv(i, j)
301 :: (reader_barrier[get_readerid()] == 1) ->
303 * We choose to ignore cycles caused by writer busy-looping,
304 * waiting for the reader, sending barrier requests, and the
305 * reader always services them without continuing execution.
307 progress_ignoring_mb1:
309 reader_barrier[get_readerid()] = 0;
312 * We choose to ignore writer's non-progress caused by the
313 * reader ignoring the writer's mb() requests.
315 progress_ignoring_mb2:
320 //#ifdef WRITER_PROGRESS
321 //#define PROGRESS_LABEL(progressid)
323 //#define PROGRESS_LABEL(progressid)
326 #define PROGRESS_LABEL(progressid) progress_writer_progid_##progressid:
328 #define smp_mb_send(i, j, progressid) \
333 :: i < NR_READERS -> \
334 reader_barrier[i] = 1; \
336 * Busy-looping waiting for reader barrier handling is of little\
337 * interest, given the reader has the ability to totally ignore \
338 * barrier requests. \
341 :: (reader_barrier[i] == 1) -> \
342 PROGRESS_LABEL(progressid) \
344 :: (reader_barrier[i] == 0) -> break; \
347 :: i >= NR_READERS -> \
355 #define smp_mb_send(i, j, progressid) smp_mb(i, j)
356 #define smp_mb_reader smp_mb
357 #define smp_mb_recv(i, j)
361 /* Keep in sync manually with smp_rmb, smp_wmb, ooo_mem and init() */
362 DECLARE_CACHED_VAR(byte, urcu_gp_ctr);
363 /* Note ! currently only one reader */
364 DECLARE_CACHED_VAR(byte, urcu_active_readers[NR_READERS]);
366 DECLARE_CACHED_VAR(bit, rcu_data[SLAB_SIZE]);
370 DECLARE_CACHED_VAR(bit, rcu_ptr);
371 bit ptr_read_first[NR_READERS];
372 bit ptr_read_second[NR_READERS];
374 DECLARE_CACHED_VAR(byte, rcu_ptr);
375 byte ptr_read_first[NR_READERS];
376 byte ptr_read_second[NR_READERS];
379 bit data_read_first[NR_READERS];
380 bit data_read_second[NR_READERS];
384 inline wait_init_done()
387 :: init_done == 0 -> skip;
395 RANDOM_CACHE_WRITE_TO_MEM(urcu_gp_ctr, get_pid());
399 RANDOM_CACHE_WRITE_TO_MEM(urcu_active_readers[i],
402 :: i >= NR_READERS -> break
404 RANDOM_CACHE_WRITE_TO_MEM(rcu_ptr, get_pid());
408 RANDOM_CACHE_WRITE_TO_MEM(rcu_data[i], get_pid());
410 :: i >= SLAB_SIZE -> break
412 RANDOM_CACHE_READ_FROM_MEM(urcu_gp_ctr, get_pid());
416 RANDOM_CACHE_READ_FROM_MEM(urcu_active_readers[i],
419 :: i >= NR_READERS -> break
421 RANDOM_CACHE_READ_FROM_MEM(rcu_ptr, get_pid());
425 RANDOM_CACHE_READ_FROM_MEM(rcu_data[i], get_pid());
427 :: i >= SLAB_SIZE -> break
433 * Bit encoding, urcu_reader :
436 int _proc_urcu_reader;
437 #define proc_urcu_reader _proc_urcu_reader
439 /* Body of PROCEDURE_READ_LOCK */
440 #define READ_PROD_A_READ (1 << 0)
441 #define READ_PROD_B_IF_TRUE (1 << 1)
442 #define READ_PROD_B_IF_FALSE (1 << 2)
443 #define READ_PROD_C_IF_TRUE_READ (1 << 3)
445 #define PROCEDURE_READ_LOCK(base, consumetoken, producetoken) \
446 :: CONSUME_TOKENS(proc_urcu_reader, consumetoken, READ_PROD_A_READ << base) -> \
448 tmp = READ_CACHED_VAR(urcu_active_readers[get_readerid()]); \
449 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_A_READ << base); \
450 :: CONSUME_TOKENS(proc_urcu_reader, \
451 READ_PROD_A_READ << base, /* RAW, pre-dominant */ \
452 (READ_PROD_B_IF_TRUE | READ_PROD_B_IF_FALSE) << base) -> \
454 :: (!(tmp & RCU_GP_CTR_NEST_MASK)) -> \
455 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_B_IF_TRUE << base); \
457 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_B_IF_FALSE << base); \
460 :: CONSUME_TOKENS(proc_urcu_reader, READ_PROD_B_IF_TRUE << base, \
461 READ_PROD_C_IF_TRUE_READ << base) -> \
463 tmp2 = READ_CACHED_VAR(urcu_gp_ctr); \
464 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_C_IF_TRUE_READ << base); \
465 :: CONSUME_TOKENS(proc_urcu_reader, \
466 (READ_PROD_C_IF_TRUE_READ /* pre-dominant */ \
467 | READ_PROD_A_READ) << base, /* WAR */ \
470 WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp2); \
471 PRODUCE_TOKENS(proc_urcu_reader, producetoken); \
472 /* IF_MERGE implies \
473 * post-dominance */ \
475 :: CONSUME_TOKENS(proc_urcu_reader, \
476 (READ_PROD_B_IF_FALSE /* pre-dominant */ \
477 | READ_PROD_A_READ) << base, /* WAR */ \
480 WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], \
482 PRODUCE_TOKENS(proc_urcu_reader, producetoken); \
483 /* IF_MERGE implies \
484 * post-dominance */ \
488 /* Body of PROCEDURE_READ_LOCK */
489 #define READ_PROC_READ_UNLOCK (1 << 0)
491 #define PROCEDURE_READ_UNLOCK(base, consumetoken, producetoken) \
492 :: CONSUME_TOKENS(proc_urcu_reader, \
494 READ_PROC_READ_UNLOCK << base) -> \
496 tmp2 = READ_CACHED_VAR(urcu_active_readers[get_readerid()]); \
497 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_READ_UNLOCK << base); \
498 :: CONSUME_TOKENS(proc_urcu_reader, \
500 | (READ_PROC_READ_UNLOCK << base), /* WAR */ \
503 WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp2 - 1); \
504 PRODUCE_TOKENS(proc_urcu_reader, producetoken); \
508 #define READ_PROD_NONE (1 << 0)
510 /* PROCEDURE_READ_LOCK base = << 1 : 1 to 5 */
511 #define READ_LOCK_BASE 1
512 #define READ_LOCK_OUT (1 << 5)
514 #define READ_PROC_FIRST_MB (1 << 6)
516 /* PROCEDURE_READ_LOCK (NESTED) base : << 7 : 7 to 11 */
517 #define READ_LOCK_NESTED_BASE 7
518 #define READ_LOCK_NESTED_OUT (1 << 11)
520 #define READ_PROC_READ_GEN (1 << 12)
521 #define READ_PROC_ACCESS_GEN (1 << 13)
523 /* PROCEDURE_READ_UNLOCK (NESTED) base = << 14 : 14 to 15 */
524 #define READ_UNLOCK_NESTED_BASE 14
525 #define READ_UNLOCK_NESTED_OUT (1 << 15)
527 #define READ_PROC_SECOND_MB (1 << 16)
529 /* PROCEDURE_READ_UNLOCK base = << 17 : 17 to 18 */
530 #define READ_UNLOCK_BASE 17
531 #define READ_UNLOCK_OUT (1 << 18)
533 /* PROCEDURE_READ_LOCK_UNROLL base = << 19 : 19 to 23 */
534 #define READ_LOCK_UNROLL_BASE 19
535 #define READ_LOCK_OUT_UNROLL (1 << 23)
537 #define READ_PROC_THIRD_MB (1 << 24)
539 #define READ_PROC_READ_GEN_UNROLL (1 << 25)
540 #define READ_PROC_ACCESS_GEN_UNROLL (1 << 26)
542 #define READ_PROC_FOURTH_MB (1 << 27)
544 /* PROCEDURE_READ_UNLOCK_UNROLL base = << 28 : 28 to 29 */
545 #define READ_UNLOCK_UNROLL_BASE 28
546 #define READ_UNLOCK_OUT_UNROLL (1 << 29)
549 /* Should not include branches */
550 #define READ_PROC_ALL_TOKENS (READ_PROD_NONE \
552 | READ_PROC_FIRST_MB \
553 | READ_LOCK_NESTED_OUT \
554 | READ_PROC_READ_GEN \
555 | READ_PROC_ACCESS_GEN \
556 | READ_UNLOCK_NESTED_OUT \
557 | READ_PROC_SECOND_MB \
559 | READ_LOCK_OUT_UNROLL \
560 | READ_PROC_THIRD_MB \
561 | READ_PROC_READ_GEN_UNROLL \
562 | READ_PROC_ACCESS_GEN_UNROLL \
563 | READ_PROC_FOURTH_MB \
564 | READ_UNLOCK_OUT_UNROLL)
566 /* Must clear all tokens, including branches */
567 #define READ_PROC_ALL_TOKENS_CLEAR ((1 << 30) - 1)
569 inline urcu_one_read(i, j, nest_i, tmp, tmp2)
571 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_NONE);
574 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FIRST_MB);
575 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_SECOND_MB);
576 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_THIRD_MB);
577 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FOURTH_MB);
580 #ifdef REMOTE_BARRIERS
581 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FIRST_MB);
582 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_SECOND_MB);
583 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_THIRD_MB);
584 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FOURTH_MB);
590 #ifdef REMOTE_BARRIERS
592 * Signal-based memory barrier will only execute when the
593 * execution order appears in program order.
599 :: CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE,
600 READ_LOCK_OUT | READ_LOCK_NESTED_OUT
601 | READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
603 | READ_LOCK_OUT_UNROLL
604 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
605 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT,
607 | READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
609 | READ_LOCK_OUT_UNROLL
610 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
611 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT | READ_LOCK_NESTED_OUT,
612 READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
614 | READ_LOCK_OUT_UNROLL
615 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
616 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
617 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN,
618 READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
620 | READ_LOCK_OUT_UNROLL
621 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
622 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
623 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN,
624 READ_UNLOCK_NESTED_OUT
626 | READ_LOCK_OUT_UNROLL
627 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
628 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
629 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN
630 | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT,
632 | READ_LOCK_OUT_UNROLL
633 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
634 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
635 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN
636 | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
639 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
640 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
641 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN
642 | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
643 | READ_UNLOCK_OUT | READ_LOCK_OUT_UNROLL,
644 READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
645 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
646 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN
647 | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
648 | READ_UNLOCK_OUT | READ_LOCK_OUT_UNROLL
649 | READ_PROC_READ_GEN_UNROLL,
650 READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
651 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
652 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN
653 | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
654 | READ_UNLOCK_OUT | READ_LOCK_OUT_UNROLL
655 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL,
656 READ_UNLOCK_OUT_UNROLL)
657 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
658 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
659 | READ_UNLOCK_OUT | READ_LOCK_OUT_UNROLL
660 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL,
669 goto non_atomic3_skip;
672 goto non_atomic3_end;
675 #endif /* REMOTE_BARRIERS */
679 PROCEDURE_READ_LOCK(READ_LOCK_BASE, READ_PROD_NONE, READ_LOCK_OUT);
681 :: CONSUME_TOKENS(proc_urcu_reader,
682 READ_LOCK_OUT, /* post-dominant */
683 READ_PROC_FIRST_MB) ->
685 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FIRST_MB);
687 PROCEDURE_READ_LOCK(READ_LOCK_NESTED_BASE, READ_PROC_FIRST_MB | READ_LOCK_OUT,
688 READ_LOCK_NESTED_OUT);
690 :: CONSUME_TOKENS(proc_urcu_reader,
691 READ_PROC_FIRST_MB, /* mb() orders reads */
692 READ_PROC_READ_GEN) ->
694 ptr_read_first[get_readerid()] = READ_CACHED_VAR(rcu_ptr);
695 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_READ_GEN);
697 :: CONSUME_TOKENS(proc_urcu_reader,
698 READ_PROC_FIRST_MB /* mb() orders reads */
699 | READ_PROC_READ_GEN,
700 READ_PROC_ACCESS_GEN) ->
701 /* smp_read_barrier_depends */
704 data_read_first[get_readerid()] =
705 READ_CACHED_VAR(rcu_data[ptr_read_first[get_readerid()]]);
706 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_ACCESS_GEN);
709 /* Note : we remove the nested memory barrier from the read unlock
710 * model, given it is not usually needed. The implementation has the barrier
711 * because the performance impact added by a branch in the common case does not
715 PROCEDURE_READ_UNLOCK(READ_UNLOCK_NESTED_BASE,
718 | READ_LOCK_NESTED_OUT,
719 READ_UNLOCK_NESTED_OUT);
722 :: CONSUME_TOKENS(proc_urcu_reader,
723 READ_PROC_ACCESS_GEN /* mb() orders reads */
724 | READ_PROC_READ_GEN /* mb() orders reads */
725 | READ_PROC_FIRST_MB /* mb() ordered */
726 | READ_LOCK_OUT /* post-dominant */
727 | READ_LOCK_NESTED_OUT /* post-dominant */
728 | READ_UNLOCK_NESTED_OUT,
729 READ_PROC_SECOND_MB) ->
731 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_SECOND_MB);
733 PROCEDURE_READ_UNLOCK(READ_UNLOCK_BASE,
734 READ_PROC_SECOND_MB /* mb() orders reads */
735 | READ_PROC_FIRST_MB /* mb() orders reads */
736 | READ_LOCK_NESTED_OUT /* RAW */
737 | READ_LOCK_OUT /* RAW */
738 | READ_UNLOCK_NESTED_OUT, /* RAW */
741 /* Unrolling loop : second consecutive lock */
742 /* reading urcu_active_readers, which have been written by
743 * READ_UNLOCK_OUT : RAW */
744 PROCEDURE_READ_LOCK(READ_LOCK_UNROLL_BASE,
745 READ_UNLOCK_OUT /* RAW */
746 | READ_PROC_SECOND_MB /* mb() orders reads */
747 | READ_PROC_FIRST_MB /* mb() orders reads */
748 | READ_LOCK_NESTED_OUT /* RAW */
749 | READ_LOCK_OUT /* RAW */
750 | READ_UNLOCK_NESTED_OUT, /* RAW */
751 READ_LOCK_OUT_UNROLL);
754 :: CONSUME_TOKENS(proc_urcu_reader,
755 READ_PROC_FIRST_MB /* mb() ordered */
756 | READ_PROC_SECOND_MB /* mb() ordered */
757 | READ_LOCK_OUT_UNROLL /* post-dominant */
758 | READ_LOCK_NESTED_OUT
760 | READ_UNLOCK_NESTED_OUT
762 READ_PROC_THIRD_MB) ->
764 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_THIRD_MB);
766 :: CONSUME_TOKENS(proc_urcu_reader,
767 READ_PROC_FIRST_MB /* mb() orders reads */
768 | READ_PROC_SECOND_MB /* mb() orders reads */
769 | READ_PROC_THIRD_MB, /* mb() orders reads */
770 READ_PROC_READ_GEN_UNROLL) ->
772 ptr_read_second[get_readerid()] = READ_CACHED_VAR(rcu_ptr);
773 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_READ_GEN_UNROLL);
775 :: CONSUME_TOKENS(proc_urcu_reader,
776 READ_PROC_READ_GEN_UNROLL
777 | READ_PROC_FIRST_MB /* mb() orders reads */
778 | READ_PROC_SECOND_MB /* mb() orders reads */
779 | READ_PROC_THIRD_MB, /* mb() orders reads */
780 READ_PROC_ACCESS_GEN_UNROLL) ->
781 /* smp_read_barrier_depends */
784 data_read_second[get_readerid()] =
785 READ_CACHED_VAR(rcu_data[ptr_read_second[get_readerid()]]);
786 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_ACCESS_GEN_UNROLL);
788 :: CONSUME_TOKENS(proc_urcu_reader,
789 READ_PROC_READ_GEN_UNROLL /* mb() orders reads */
790 | READ_PROC_ACCESS_GEN_UNROLL /* mb() orders reads */
791 | READ_PROC_FIRST_MB /* mb() ordered */
792 | READ_PROC_SECOND_MB /* mb() ordered */
793 | READ_PROC_THIRD_MB /* mb() ordered */
794 | READ_LOCK_OUT_UNROLL /* post-dominant */
795 | READ_LOCK_NESTED_OUT
797 | READ_UNLOCK_NESTED_OUT
799 READ_PROC_FOURTH_MB) ->
801 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FOURTH_MB);
803 PROCEDURE_READ_UNLOCK(READ_UNLOCK_UNROLL_BASE,
804 READ_PROC_FOURTH_MB /* mb() orders reads */
805 | READ_PROC_THIRD_MB /* mb() orders reads */
806 | READ_LOCK_OUT_UNROLL /* RAW */
807 | READ_PROC_SECOND_MB /* mb() orders reads */
808 | READ_PROC_FIRST_MB /* mb() orders reads */
809 | READ_LOCK_NESTED_OUT /* RAW */
810 | READ_LOCK_OUT /* RAW */
811 | READ_UNLOCK_NESTED_OUT, /* RAW */
812 READ_UNLOCK_OUT_UNROLL);
813 :: CONSUME_TOKENS(proc_urcu_reader, READ_PROC_ALL_TOKENS, 0) ->
814 CLEAR_TOKENS(proc_urcu_reader, READ_PROC_ALL_TOKENS_CLEAR);
820 * Dependency between consecutive loops :
822 * WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp2 - 1)
823 * tmp = READ_CACHED_VAR(urcu_active_readers[get_readerid()]);
825 * _WHEN THE MB()s are in place_, they add full ordering of the
826 * generation pointer read wrt active reader count read, which ensures
827 * execution will not spill across loop execution.
828 * However, in the event mb()s are removed (execution using signal
829 * handler to promote barrier()() -> smp_mb()), nothing prevents one loop
830 * to spill its execution on other loop's execution.
853 active proctype urcu_reader()
860 assert(get_pid() < NR_PROCS);
866 * We do not test reader's progress here, because we are mainly
867 * interested in writer's progress. The reader never blocks
868 * anyway. We have to test for reader/writer's progress
869 * separately, otherwise we could think the writer is doing
870 * progress when it's blocked by an always progressing reader.
872 #ifdef READER_PROGRESS
875 urcu_one_read(i, j, nest_i, tmp, tmp2);
879 /* no name clash please */
880 #undef proc_urcu_reader
883 /* Model the RCU update process. */
886 * Bit encoding, urcu_writer :
887 * Currently only supports one reader.
890 int _proc_urcu_writer;
891 #define proc_urcu_writer _proc_urcu_writer
893 #define WRITE_PROD_NONE (1 << 0)
895 #define WRITE_DATA (1 << 1)
896 #define WRITE_PROC_WMB (1 << 2)
897 #define WRITE_XCHG_PTR (1 << 3)
899 #define WRITE_PROC_FIRST_MB (1 << 4)
902 #define WRITE_PROC_FIRST_READ_GP (1 << 5)
903 #define WRITE_PROC_FIRST_WRITE_GP (1 << 6)
904 #define WRITE_PROC_FIRST_WAIT (1 << 7)
905 #define WRITE_PROC_FIRST_WAIT_LOOP (1 << 8)
908 #define WRITE_PROC_SECOND_READ_GP (1 << 9)
909 #define WRITE_PROC_SECOND_WRITE_GP (1 << 10)
910 #define WRITE_PROC_SECOND_WAIT (1 << 11)
911 #define WRITE_PROC_SECOND_WAIT_LOOP (1 << 12)
913 #define WRITE_PROC_SECOND_MB (1 << 13)
915 #define WRITE_FREE (1 << 14)
917 #define WRITE_PROC_ALL_TOKENS (WRITE_PROD_NONE \
921 | WRITE_PROC_FIRST_MB \
922 | WRITE_PROC_FIRST_READ_GP \
923 | WRITE_PROC_FIRST_WRITE_GP \
924 | WRITE_PROC_FIRST_WAIT \
925 | WRITE_PROC_SECOND_READ_GP \
926 | WRITE_PROC_SECOND_WRITE_GP \
927 | WRITE_PROC_SECOND_WAIT \
928 | WRITE_PROC_SECOND_MB \
931 #define WRITE_PROC_ALL_TOKENS_CLEAR ((1 << 15) - 1)
934 * Mutexes are implied around writer execution. A single writer at a time.
936 active proctype urcu_writer()
939 byte tmp, tmp2, tmpa;
940 byte cur_data = 0, old_data, loop_nr = 0;
941 byte cur_gp_val = 0; /*
942 * Keep a local trace of the current parity so
943 * we don't add non-existing dependencies on the global
944 * GP update. Needed to test single flip case.
949 assert(get_pid() < NR_PROCS);
953 #ifdef WRITER_PROGRESS
956 loop_nr = loop_nr + 1;
958 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROD_NONE);
961 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_WMB);
965 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_MB);
966 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_MB);
970 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_READ_GP);
971 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WRITE_GP);
972 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT);
973 /* For single flip, we need to know the current parity */
974 cur_gp_val = cur_gp_val ^ RCU_GP_CTR_BIT;
981 :: CONSUME_TOKENS(proc_urcu_writer,
985 cur_data = (cur_data + 1) % SLAB_SIZE;
986 WRITE_CACHED_VAR(rcu_data[cur_data], WINE);
987 PRODUCE_TOKENS(proc_urcu_writer, WRITE_DATA);
990 :: CONSUME_TOKENS(proc_urcu_writer,
994 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_WMB);
996 :: CONSUME_TOKENS(proc_urcu_writer,
999 /* rcu_xchg_pointer() */
1001 old_data = READ_CACHED_VAR(rcu_ptr);
1002 WRITE_CACHED_VAR(rcu_ptr, cur_data);
1004 PRODUCE_TOKENS(proc_urcu_writer, WRITE_XCHG_PTR);
1006 :: CONSUME_TOKENS(proc_urcu_writer,
1007 WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR,
1008 WRITE_PROC_FIRST_MB) ->
1011 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_MB);
1014 :: CONSUME_TOKENS(proc_urcu_writer,
1015 WRITE_PROC_FIRST_MB,
1016 WRITE_PROC_FIRST_READ_GP) ->
1017 tmpa = READ_CACHED_VAR(urcu_gp_ctr);
1018 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_READ_GP);
1019 :: CONSUME_TOKENS(proc_urcu_writer,
1020 WRITE_PROC_FIRST_MB | WRITE_PROC_WMB
1021 | WRITE_PROC_FIRST_READ_GP,
1022 WRITE_PROC_FIRST_WRITE_GP) ->
1024 WRITE_CACHED_VAR(urcu_gp_ctr, tmpa ^ RCU_GP_CTR_BIT);
1025 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WRITE_GP);
1027 :: CONSUME_TOKENS(proc_urcu_writer,
1028 //WRITE_PROC_FIRST_WRITE_GP /* TEST ADDING SYNC CORE */
1029 WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
1030 WRITE_PROC_FIRST_WAIT | WRITE_PROC_FIRST_WAIT_LOOP) ->
1032 /* ONLY WAITING FOR READER 0 */
1033 tmp2 = READ_CACHED_VAR(urcu_active_readers[0]);
1035 /* In normal execution, we are always starting by
1036 * waiting for the even parity.
1038 cur_gp_val = RCU_GP_CTR_BIT;
1041 :: (tmp2 & RCU_GP_CTR_NEST_MASK)
1042 && ((tmp2 ^ cur_gp_val) & RCU_GP_CTR_BIT) ->
1043 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WAIT_LOOP);
1045 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WAIT);
1048 :: CONSUME_TOKENS(proc_urcu_writer,
1049 //WRITE_PROC_FIRST_WRITE_GP /* TEST ADDING SYNC CORE */
1050 WRITE_PROC_FIRST_WRITE_GP
1051 | WRITE_PROC_FIRST_READ_GP
1052 | WRITE_PROC_FIRST_WAIT_LOOP
1053 | WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR
1054 | WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
1056 #ifndef GEN_ERROR_WRITER_PROGRESS
1062 /* This instruction loops to WRITE_PROC_FIRST_WAIT */
1063 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WAIT_LOOP | WRITE_PROC_FIRST_WAIT);
1066 :: CONSUME_TOKENS(proc_urcu_writer,
1067 WRITE_PROC_FIRST_WAIT /* Control dependency : need to branch out of
1068 * the loop to execute the next flip (CHECK) */
1069 | WRITE_PROC_FIRST_WRITE_GP
1070 | WRITE_PROC_FIRST_READ_GP
1071 | WRITE_PROC_FIRST_MB,
1072 WRITE_PROC_SECOND_READ_GP) ->
1074 tmpa = READ_CACHED_VAR(urcu_gp_ctr);
1075 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_READ_GP);
1076 :: CONSUME_TOKENS(proc_urcu_writer,
1079 | WRITE_PROC_FIRST_READ_GP
1080 | WRITE_PROC_FIRST_WRITE_GP
1081 | WRITE_PROC_SECOND_READ_GP,
1082 WRITE_PROC_SECOND_WRITE_GP) ->
1084 WRITE_CACHED_VAR(urcu_gp_ctr, tmpa ^ RCU_GP_CTR_BIT);
1085 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WRITE_GP);
1087 :: CONSUME_TOKENS(proc_urcu_writer,
1088 //WRITE_PROC_FIRST_WRITE_GP /* TEST ADDING SYNC CORE */
1089 WRITE_PROC_FIRST_WAIT
1090 | WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
1091 WRITE_PROC_SECOND_WAIT | WRITE_PROC_SECOND_WAIT_LOOP) ->
1093 /* ONLY WAITING FOR READER 0 */
1094 tmp2 = READ_CACHED_VAR(urcu_active_readers[0]);
1096 :: (tmp2 & RCU_GP_CTR_NEST_MASK)
1097 && ((tmp2 ^ 0) & RCU_GP_CTR_BIT) ->
1098 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT_LOOP);
1100 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT);
1103 :: CONSUME_TOKENS(proc_urcu_writer,
1104 //WRITE_PROC_FIRST_WRITE_GP /* TEST ADDING SYNC CORE */
1105 WRITE_PROC_SECOND_WRITE_GP
1106 | WRITE_PROC_FIRST_WRITE_GP
1107 | WRITE_PROC_SECOND_READ_GP
1108 | WRITE_PROC_FIRST_READ_GP
1109 | WRITE_PROC_SECOND_WAIT_LOOP
1110 | WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR
1111 | WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
1113 #ifndef GEN_ERROR_WRITER_PROGRESS
1119 /* This instruction loops to WRITE_PROC_SECOND_WAIT */
1120 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT_LOOP | WRITE_PROC_SECOND_WAIT);
1123 :: CONSUME_TOKENS(proc_urcu_writer,
1124 WRITE_PROC_FIRST_WAIT
1125 | WRITE_PROC_SECOND_WAIT
1126 | WRITE_PROC_FIRST_READ_GP
1127 | WRITE_PROC_SECOND_READ_GP
1128 | WRITE_PROC_FIRST_WRITE_GP
1129 | WRITE_PROC_SECOND_WRITE_GP
1130 | WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR
1131 | WRITE_PROC_FIRST_MB,
1132 WRITE_PROC_SECOND_MB) ->
1135 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_MB);
1137 :: CONSUME_TOKENS(proc_urcu_writer,
1139 | WRITE_PROC_FIRST_WAIT
1140 | WRITE_PROC_SECOND_WAIT
1141 | WRITE_PROC_WMB /* No dependency on
1142 * WRITE_DATA because we
1144 * different location. */
1145 | WRITE_PROC_SECOND_MB
1146 | WRITE_PROC_FIRST_MB,
1148 WRITE_CACHED_VAR(rcu_data[old_data], POISON);
1149 PRODUCE_TOKENS(proc_urcu_writer, WRITE_FREE);
1151 :: CONSUME_TOKENS(proc_urcu_writer, WRITE_PROC_ALL_TOKENS, 0) ->
1152 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_ALL_TOKENS_CLEAR);
1158 * Note : Promela model adds implicit serialization of the
1159 * WRITE_FREE instruction. Normally, it would be permitted to
1160 * spill on the next loop execution. Given the validation we do
1161 * checks for the data entry read to be poisoned, it's ok if
1162 * we do not check "late arriving" memory poisoning.
1167 * Given the reader loops infinitely, let the writer also busy-loop
1168 * with progress here so, with weak fairness, we can test the
1169 * writer's progress.
1174 #ifdef WRITER_PROGRESS
1177 #ifdef READER_PROGRESS
1179 * Make sure we don't block the reader's progress.
1181 smp_mb_send(i, j, 5);
1186 /* Non-atomic parts of the loop */
1189 smp_mb_send(i, j, 1);
1190 goto smp_mb_send1_end;
1191 #ifndef GEN_ERROR_WRITER_PROGRESS
1193 smp_mb_send(i, j, 2);
1194 goto smp_mb_send2_end;
1196 smp_mb_send(i, j, 3);
1197 goto smp_mb_send3_end;
1200 smp_mb_send(i, j, 4);
1201 goto smp_mb_send4_end;
1206 /* no name clash please */
1207 #undef proc_urcu_writer
1210 /* Leave after the readers and writers so the pid count is ok. */
1215 INIT_CACHED_VAR(urcu_gp_ctr, 1, j);
1216 INIT_CACHED_VAR(rcu_ptr, 0, j);
1220 :: i < NR_READERS ->
1221 INIT_CACHED_VAR(urcu_active_readers[i], 0, j);
1222 ptr_read_first[i] = 1;
1223 ptr_read_second[i] = 1;
1224 data_read_first[i] = WINE;
1225 data_read_second[i] = WINE;
1227 :: i >= NR_READERS -> break
1229 INIT_CACHED_VAR(rcu_data[0], WINE, j);
1233 INIT_CACHED_VAR(rcu_data[i], POISON, j);
1235 :: i >= SLAB_SIZE -> break