2 // Poison value for freed memory
4 // Memory with correct data
8 #define read_poison (data_read_first[0] == POISON || data_read_second[0] == POISON)
10 #define RCU_GP_CTR_BIT (1 << 7)
11 #define RCU_GP_CTR_NEST_MASK (RCU_GP_CTR_BIT - 1)
14 #define REMOTE_BARRIERS
16 * mem.spin: Promela code to validate memory barriers with OOO memory
17 * and out-of-order instruction scheduling.
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
33 * Copyright (c) 2009 Mathieu Desnoyers
36 /* Promela validation variables. */
38 /* specific defines "included" here */
39 /* DEFINES file "included" here */
46 #define get_pid() (_pid)
48 #define get_readerid() (get_pid())
51 * Produced process control and data flow. Updated after each instruction to
52 * show which variables are ready. Using one-hot bit encoding per variable to
53 * save state space. Used as triggers to execute the instructions having those
54 * variables as input. Leaving bits active to inhibit instruction execution.
55 * Scheme used to make instruction disabling and automatic dependency fall-back
59 #define CONSUME_TOKENS(state, bits, notbits) \
60 ((!(state & (notbits))) && (state & (bits)) == (bits))
62 #define PRODUCE_TOKENS(state, bits) \
63 state = state | (bits);
65 #define CLEAR_TOKENS(state, bits) \
66 state = state & ~(bits)
69 * Types of dependency :
73 * - True dependency, Read-after-Write (RAW)
75 * This type of dependency happens when a statement depends on the result of a
76 * previous statement. This applies to any statement which needs to read a
77 * variable written by a preceding statement.
79 * - False dependency, Write-after-Read (WAR)
81 * Typically, variable renaming can ensure that this dependency goes away.
82 * However, if the statements must read and then write from/to the same variable
83 * in the OOO memory model, renaming may be impossible, and therefore this
84 * causes a WAR dependency.
86 * - Output dependency, Write-after-Write (WAW)
88 * Two writes to the same variable in subsequent statements. Variable renaming
89 * can ensure this is not needed, but can be required when writing multiple
90 * times to the same OOO mem model variable.
94 * Execution of a given instruction depends on a previous instruction evaluating
95 * in a way that allows its execution. E.g. : branches.
97 * Useful considerations for joining dependencies after branch
101 * "We say box i dominates box j if every path (leading from input to output
102 * through the diagram) which passes through box j must also pass through box
103 * i. Thus box i dominates box j if box j is subordinate to box i in the
106 * http://www.hipersoft.rice.edu/grads/publications/dom14.pdf
107 * Other classic algorithm to calculate dominance : Lengauer-Tarjan (in gcc)
111 * Just as pre-dominance, but with arcs of the data flow inverted, and input vs
112 * output exchanged. Therefore, i post-dominating j ensures that every path
113 * passing by j will pass by i before reaching the output.
115 * Other considerations
117 * Note about "volatile" keyword dependency : The compiler will order volatile
118 * accesses so they appear in the right order on a given CPU. They can be
119 * reordered by the CPU instruction scheduling. This therefore cannot be
120 * considered as a depencency.
124 * Cooper, Keith D.; & Torczon, Linda. (2005). Engineering a Compiler. Morgan
125 * Kaufmann. ISBN 1-55860-698-X.
126 * Kennedy, Ken; & Allen, Randy. (2001). Optimizing Compilers for Modern
127 * Architectures: A Dependence-based Approach. Morgan Kaufmann. ISBN
129 * Muchnick, Steven S. (1997). Advanced Compiler Design and Implementation.
130 * Morgan Kaufmann. ISBN 1-55860-320-4.
134 * Note about loops and nested calls
136 * To keep this model simple, loops expressed in the framework will behave as if
137 * there was a core synchronizing instruction between loops. To see the effect
138 * of loop unrolling, manually unrolling loops is required. Note that if loops
139 * end or start with a core synchronizing instruction, the model is appropriate.
140 * Nested calls are not supported.
144 * Each process have its own data in cache. Caches are randomly updated.
145 * smp_wmb and smp_rmb forces cache updates (write and read), smp_mb forces
149 typedef per_proc_byte {
153 typedef per_proc_bit {
157 /* Bitfield has a maximum of 8 procs */
158 typedef per_proc_bitfield {
162 #define DECLARE_CACHED_VAR(type, x) \
164 per_proc_##type cached_##x; \
165 per_proc_bitfield cache_dirty_##x;
167 #define INIT_CACHED_VAR(x, v, j) \
169 cache_dirty_##x.bitfield = 0; \
173 cached_##x.val[j] = v; \
175 :: j >= NR_PROCS -> break \
178 #define IS_CACHE_DIRTY(x, id) (cache_dirty_##x.bitfield & (1 << id))
180 #define READ_CACHED_VAR(x) (cached_##x.val[get_pid()])
182 #define WRITE_CACHED_VAR(x, v) \
184 cached_##x.val[get_pid()] = v; \
185 cache_dirty_##x.bitfield = \
186 cache_dirty_##x.bitfield | (1 << get_pid()); \
189 #define CACHE_WRITE_TO_MEM(x, id) \
191 :: IS_CACHE_DIRTY(x, id) -> \
192 mem_##x = cached_##x.val[id]; \
193 cache_dirty_##x.bitfield = \
194 cache_dirty_##x.bitfield & (~(1 << id)); \
199 #define CACHE_READ_FROM_MEM(x, id) \
201 :: !IS_CACHE_DIRTY(x, id) -> \
202 cached_##x.val[id] = mem_##x;\
208 * May update other caches if cache is dirty, or not.
210 #define RANDOM_CACHE_WRITE_TO_MEM(x, id)\
212 :: 1 -> CACHE_WRITE_TO_MEM(x, id); \
216 #define RANDOM_CACHE_READ_FROM_MEM(x, id)\
218 :: 1 -> CACHE_READ_FROM_MEM(x, id); \
222 /* Must consume all prior read tokens. All subsequent reads depend on it. */
226 CACHE_READ_FROM_MEM(urcu_gp_ctr, get_pid());
230 CACHE_READ_FROM_MEM(urcu_active_readers[i], get_pid());
232 :: i >= NR_READERS -> break
234 CACHE_READ_FROM_MEM(rcu_ptr, get_pid());
238 CACHE_READ_FROM_MEM(rcu_data[i], get_pid());
240 :: i >= SLAB_SIZE -> break
245 /* Must consume all prior write tokens. All subsequent writes depend on it. */
249 CACHE_WRITE_TO_MEM(urcu_gp_ctr, get_pid());
253 CACHE_WRITE_TO_MEM(urcu_active_readers[i], get_pid());
255 :: i >= NR_READERS -> break
257 CACHE_WRITE_TO_MEM(rcu_ptr, get_pid());
261 CACHE_WRITE_TO_MEM(rcu_data[i], get_pid());
263 :: i >= SLAB_SIZE -> break
268 /* Synchronization point. Must consume all prior read and write tokens. All
269 * subsequent reads and writes depend on it. */
278 #ifdef REMOTE_BARRIERS
280 bit reader_barrier[NR_READERS];
283 * We cannot leave the barriers dependencies in place in REMOTE_BARRIERS mode
284 * because they would add unexisting core synchronization and would therefore
285 * create an incomplete model.
286 * Therefore, we model the read-side memory barriers by completely disabling the
287 * memory barriers and their dependencies from the read-side. One at a time
288 * (different verification runs), we make a different instruction listen for
292 #define smp_mb_reader(i, j)
295 * Service 0, 1 or many barrier requests.
297 inline smp_mb_recv(i, j)
300 :: (reader_barrier[get_readerid()] == 1) ->
302 reader_barrier[get_readerid()] = 0;
304 /* We choose to ignore writer's non-progress caused from the
305 * reader ignoring the writer's mb() requests */
306 #ifdef WRITER_PROGRESS
307 progress_writer_from_reader:
313 #ifdef WRITER_PROGRESS
314 //#define PROGRESS_LABEL(progressid) progress_writer_progid_##progressid:
315 #define PROGRESS_LABEL(progressid)
317 #define PROGRESS_LABEL(progressid)
320 #define smp_mb_send(i, j, progressid) \
325 :: i < NR_READERS -> \
326 reader_barrier[i] = 1; \
328 * Busy-looping waiting for reader barrier handling is of little\
329 * interest, given the reader has the ability to totally ignore \
330 * barrier requests. \
332 PROGRESS_LABEL(progressid) \
334 :: (reader_barrier[i] == 1) -> skip; \
335 :: (reader_barrier[i] == 0) -> break; \
338 :: i >= NR_READERS -> \
346 #define smp_mb_send(i, j, progressid) smp_mb(i, j)
347 #define smp_mb_reader smp_mb
348 #define smp_mb_recv(i, j)
352 /* Keep in sync manually with smp_rmb, smp_wmb, ooo_mem and init() */
353 DECLARE_CACHED_VAR(byte, urcu_gp_ctr);
354 /* Note ! currently only one reader */
355 DECLARE_CACHED_VAR(byte, urcu_active_readers[NR_READERS]);
357 DECLARE_CACHED_VAR(bit, rcu_data[SLAB_SIZE]);
361 DECLARE_CACHED_VAR(bit, rcu_ptr);
362 bit ptr_read_first[NR_READERS];
363 bit ptr_read_second[NR_READERS];
365 DECLARE_CACHED_VAR(byte, rcu_ptr);
366 byte ptr_read_first[NR_READERS];
367 byte ptr_read_second[NR_READERS];
370 bit data_read_first[NR_READERS];
371 bit data_read_second[NR_READERS];
375 inline wait_init_done()
378 :: init_done == 0 -> skip;
386 RANDOM_CACHE_WRITE_TO_MEM(urcu_gp_ctr, get_pid());
390 RANDOM_CACHE_WRITE_TO_MEM(urcu_active_readers[i],
393 :: i >= NR_READERS -> break
395 RANDOM_CACHE_WRITE_TO_MEM(rcu_ptr, get_pid());
399 RANDOM_CACHE_WRITE_TO_MEM(rcu_data[i], get_pid());
401 :: i >= SLAB_SIZE -> break
403 RANDOM_CACHE_READ_FROM_MEM(urcu_gp_ctr, get_pid());
407 RANDOM_CACHE_READ_FROM_MEM(urcu_active_readers[i],
410 :: i >= NR_READERS -> break
412 RANDOM_CACHE_READ_FROM_MEM(rcu_ptr, get_pid());
416 RANDOM_CACHE_READ_FROM_MEM(rcu_data[i], get_pid());
418 :: i >= SLAB_SIZE -> break
424 * Bit encoding, urcu_reader :
427 int _proc_urcu_reader;
428 #define proc_urcu_reader _proc_urcu_reader
430 /* Body of PROCEDURE_READ_LOCK */
431 #define READ_PROD_A_READ (1 << 0)
432 #define READ_PROD_B_IF_TRUE (1 << 1)
433 #define READ_PROD_B_IF_FALSE (1 << 2)
434 #define READ_PROD_C_IF_TRUE_READ (1 << 3)
436 #define PROCEDURE_READ_LOCK(base, consumetoken, producetoken) \
437 :: CONSUME_TOKENS(proc_urcu_reader, consumetoken, READ_PROD_A_READ << base) -> \
439 tmp = READ_CACHED_VAR(urcu_active_readers[get_readerid()]); \
440 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_A_READ << base); \
441 :: CONSUME_TOKENS(proc_urcu_reader, \
442 READ_PROD_A_READ << base, /* RAW, pre-dominant */ \
443 (READ_PROD_B_IF_TRUE | READ_PROD_B_IF_FALSE) << base) -> \
445 :: (!(tmp & RCU_GP_CTR_NEST_MASK)) -> \
446 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_B_IF_TRUE << base); \
448 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_B_IF_FALSE << base); \
451 :: CONSUME_TOKENS(proc_urcu_reader, READ_PROD_B_IF_TRUE << base, \
452 READ_PROD_C_IF_TRUE_READ << base) -> \
454 tmp2 = READ_CACHED_VAR(urcu_gp_ctr); \
455 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_C_IF_TRUE_READ << base); \
456 :: CONSUME_TOKENS(proc_urcu_reader, \
457 (READ_PROD_C_IF_TRUE_READ /* pre-dominant */ \
458 | READ_PROD_A_READ) << base, /* WAR */ \
461 WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp2); \
462 PRODUCE_TOKENS(proc_urcu_reader, producetoken); \
463 /* IF_MERGE implies \
464 * post-dominance */ \
466 :: CONSUME_TOKENS(proc_urcu_reader, \
467 (READ_PROD_B_IF_FALSE /* pre-dominant */ \
468 | READ_PROD_A_READ) << base, /* WAR */ \
471 WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], \
473 PRODUCE_TOKENS(proc_urcu_reader, producetoken); \
474 /* IF_MERGE implies \
475 * post-dominance */ \
479 /* Body of PROCEDURE_READ_LOCK */
480 #define READ_PROC_READ_UNLOCK (1 << 0)
482 #define PROCEDURE_READ_UNLOCK(base, consumetoken, producetoken) \
483 :: CONSUME_TOKENS(proc_urcu_reader, \
485 READ_PROC_READ_UNLOCK << base) -> \
487 tmp2 = READ_CACHED_VAR(urcu_active_readers[get_readerid()]); \
488 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_READ_UNLOCK << base); \
489 :: CONSUME_TOKENS(proc_urcu_reader, \
491 | (READ_PROC_READ_UNLOCK << base), /* WAR */ \
494 WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp2 - 1); \
495 PRODUCE_TOKENS(proc_urcu_reader, producetoken); \
499 #define READ_PROD_NONE (1 << 0)
501 /* PROCEDURE_READ_LOCK base = << 1 : 1 to 5 */
502 #define READ_LOCK_BASE 1
503 #define READ_LOCK_OUT (1 << 5)
505 #define READ_PROC_FIRST_MB (1 << 6)
507 /* PROCEDURE_READ_LOCK (NESTED) base : << 7 : 7 to 11 */
508 #define READ_LOCK_NESTED_BASE 7
509 #define READ_LOCK_NESTED_OUT (1 << 11)
511 #define READ_PROC_READ_GEN (1 << 12)
512 #define READ_PROC_ACCESS_GEN (1 << 13)
514 /* PROCEDURE_READ_UNLOCK (NESTED) base = << 14 : 14 to 15 */
515 #define READ_UNLOCK_NESTED_BASE 14
516 #define READ_UNLOCK_NESTED_OUT (1 << 15)
518 #define READ_PROC_SECOND_MB (1 << 16)
520 /* PROCEDURE_READ_UNLOCK base = << 17 : 17 to 18 */
521 #define READ_UNLOCK_BASE 17
522 #define READ_UNLOCK_OUT (1 << 18)
524 /* PROCEDURE_READ_LOCK_UNROLL base = << 19 : 19 to 23 */
525 #define READ_LOCK_UNROLL_BASE 19
526 #define READ_LOCK_OUT_UNROLL (1 << 23)
528 #define READ_PROC_THIRD_MB (1 << 24)
530 #define READ_PROC_READ_GEN_UNROLL (1 << 25)
531 #define READ_PROC_ACCESS_GEN_UNROLL (1 << 26)
533 #define READ_PROC_FOURTH_MB (1 << 27)
535 /* PROCEDURE_READ_UNLOCK_UNROLL base = << 28 : 28 to 29 */
536 #define READ_UNLOCK_UNROLL_BASE 28
537 #define READ_UNLOCK_OUT_UNROLL (1 << 29)
540 /* Should not include branches */
541 #define READ_PROC_ALL_TOKENS (READ_PROD_NONE \
543 | READ_PROC_FIRST_MB \
544 | READ_LOCK_NESTED_OUT \
545 | READ_PROC_READ_GEN \
546 | READ_PROC_ACCESS_GEN \
547 | READ_UNLOCK_NESTED_OUT \
548 | READ_PROC_SECOND_MB \
550 | READ_LOCK_OUT_UNROLL \
551 | READ_PROC_THIRD_MB \
552 | READ_PROC_READ_GEN_UNROLL \
553 | READ_PROC_ACCESS_GEN_UNROLL \
554 | READ_PROC_FOURTH_MB \
555 | READ_UNLOCK_OUT_UNROLL)
557 /* Must clear all tokens, including branches */
558 #define READ_PROC_ALL_TOKENS_CLEAR ((1 << 30) - 1)
560 inline urcu_one_read(i, j, nest_i, tmp, tmp2)
562 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_NONE);
565 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FIRST_MB);
566 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_SECOND_MB);
567 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_THIRD_MB);
568 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FOURTH_MB);
571 #ifdef REMOTE_BARRIERS
572 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FIRST_MB);
573 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_SECOND_MB);
574 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_THIRD_MB);
575 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FOURTH_MB);
581 #ifdef REMOTE_BARRIERS
583 * Signal-based memory barrier will only execute when the
584 * execution order appears in program order.
590 :: CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE,
591 READ_LOCK_OUT | READ_LOCK_NESTED_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,
598 | READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
600 | READ_LOCK_OUT_UNROLL
601 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
602 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT | READ_LOCK_NESTED_OUT,
603 READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
605 | READ_LOCK_OUT_UNROLL
606 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
607 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
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 | READ_PROC_ACCESS_GEN,
615 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,
623 | READ_LOCK_OUT_UNROLL
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
630 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
631 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
632 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN
633 | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
634 | READ_UNLOCK_OUT | READ_LOCK_OUT_UNROLL,
635 READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
636 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
637 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN
638 | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
639 | READ_UNLOCK_OUT | READ_LOCK_OUT_UNROLL
640 | READ_PROC_READ_GEN_UNROLL,
641 READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
642 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
643 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN
644 | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
645 | READ_UNLOCK_OUT | READ_LOCK_OUT_UNROLL
646 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL,
647 READ_UNLOCK_OUT_UNROLL)
648 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
649 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
650 | READ_UNLOCK_OUT | READ_LOCK_OUT_UNROLL
651 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL,
661 goto non_atomic3_skip;
664 goto non_atomic3_end;
667 #endif /* REMOTE_BARRIERS */
671 PROCEDURE_READ_LOCK(READ_LOCK_BASE, READ_PROD_NONE, READ_LOCK_OUT);
673 :: CONSUME_TOKENS(proc_urcu_reader,
674 READ_LOCK_OUT, /* post-dominant */
675 READ_PROC_FIRST_MB) ->
677 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FIRST_MB);
679 PROCEDURE_READ_LOCK(READ_LOCK_NESTED_BASE, READ_PROC_FIRST_MB | READ_LOCK_OUT,
680 READ_LOCK_NESTED_OUT);
682 :: CONSUME_TOKENS(proc_urcu_reader,
683 READ_PROC_FIRST_MB, /* mb() orders reads */
684 READ_PROC_READ_GEN) ->
686 ptr_read_first[get_readerid()] = READ_CACHED_VAR(rcu_ptr);
687 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_READ_GEN);
689 :: CONSUME_TOKENS(proc_urcu_reader,
690 READ_PROC_FIRST_MB /* mb() orders reads */
691 | READ_PROC_READ_GEN,
692 READ_PROC_ACCESS_GEN) ->
693 /* smp_read_barrier_depends */
696 data_read_first[get_readerid()] =
697 READ_CACHED_VAR(rcu_data[ptr_read_first[get_readerid()]]);
698 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_ACCESS_GEN);
701 /* Note : we remove the nested memory barrier from the read unlock
702 * model, given it is not usually needed. The implementation has the barrier
703 * because the performance impact added by a branch in the common case does not
707 PROCEDURE_READ_UNLOCK(READ_UNLOCK_NESTED_BASE,
710 | READ_LOCK_NESTED_OUT,
711 READ_UNLOCK_NESTED_OUT);
714 :: CONSUME_TOKENS(proc_urcu_reader,
715 READ_PROC_ACCESS_GEN /* mb() orders reads */
716 | READ_PROC_READ_GEN /* mb() orders reads */
717 | READ_PROC_FIRST_MB /* mb() ordered */
718 | READ_LOCK_OUT /* post-dominant */
719 | READ_LOCK_NESTED_OUT /* post-dominant */
720 | READ_UNLOCK_NESTED_OUT,
721 READ_PROC_SECOND_MB) ->
723 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_SECOND_MB);
725 PROCEDURE_READ_UNLOCK(READ_UNLOCK_BASE,
726 READ_PROC_SECOND_MB /* mb() orders reads */
727 | READ_PROC_FIRST_MB /* mb() orders reads */
728 | READ_LOCK_NESTED_OUT /* RAW */
729 | READ_LOCK_OUT /* RAW */
730 | READ_UNLOCK_NESTED_OUT, /* RAW */
733 /* Unrolling loop : second consecutive lock */
734 /* reading urcu_active_readers, which have been written by
735 * READ_UNLOCK_OUT : RAW */
736 PROCEDURE_READ_LOCK(READ_LOCK_UNROLL_BASE,
737 READ_UNLOCK_OUT /* RAW */
738 | READ_PROC_SECOND_MB /* mb() orders reads */
739 | READ_PROC_FIRST_MB /* mb() orders reads */
740 | READ_LOCK_NESTED_OUT /* RAW */
741 | READ_LOCK_OUT /* RAW */
742 | READ_UNLOCK_NESTED_OUT, /* RAW */
743 READ_LOCK_OUT_UNROLL);
746 :: CONSUME_TOKENS(proc_urcu_reader,
747 READ_PROC_FIRST_MB /* mb() ordered */
748 | READ_PROC_SECOND_MB /* mb() ordered */
749 | READ_LOCK_OUT_UNROLL /* post-dominant */
750 | READ_LOCK_NESTED_OUT
752 | READ_UNLOCK_NESTED_OUT
754 READ_PROC_THIRD_MB) ->
756 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_THIRD_MB);
758 :: CONSUME_TOKENS(proc_urcu_reader,
759 READ_PROC_FIRST_MB /* mb() orders reads */
760 | READ_PROC_SECOND_MB /* mb() orders reads */
761 | READ_PROC_THIRD_MB, /* mb() orders reads */
762 READ_PROC_READ_GEN_UNROLL) ->
764 ptr_read_second[get_readerid()] = READ_CACHED_VAR(rcu_ptr);
765 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_READ_GEN_UNROLL);
767 :: CONSUME_TOKENS(proc_urcu_reader,
768 READ_PROC_READ_GEN_UNROLL
769 | READ_PROC_FIRST_MB /* mb() orders reads */
770 | READ_PROC_SECOND_MB /* mb() orders reads */
771 | READ_PROC_THIRD_MB, /* mb() orders reads */
772 READ_PROC_ACCESS_GEN_UNROLL) ->
773 /* smp_read_barrier_depends */
776 data_read_second[get_readerid()] =
777 READ_CACHED_VAR(rcu_data[ptr_read_second[get_readerid()]]);
778 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_ACCESS_GEN_UNROLL);
780 :: CONSUME_TOKENS(proc_urcu_reader,
781 READ_PROC_READ_GEN_UNROLL /* mb() orders reads */
782 | READ_PROC_ACCESS_GEN_UNROLL /* mb() orders reads */
783 | READ_PROC_FIRST_MB /* mb() ordered */
784 | READ_PROC_SECOND_MB /* mb() ordered */
785 | READ_PROC_THIRD_MB /* mb() ordered */
786 | READ_LOCK_OUT_UNROLL /* post-dominant */
787 | READ_LOCK_NESTED_OUT
789 | READ_UNLOCK_NESTED_OUT
791 READ_PROC_FOURTH_MB) ->
793 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FOURTH_MB);
795 PROCEDURE_READ_UNLOCK(READ_UNLOCK_UNROLL_BASE,
796 READ_PROC_FOURTH_MB /* mb() orders reads */
797 | READ_PROC_THIRD_MB /* mb() orders reads */
798 | READ_LOCK_OUT_UNROLL /* RAW */
799 | READ_PROC_SECOND_MB /* mb() orders reads */
800 | READ_PROC_FIRST_MB /* mb() orders reads */
801 | READ_LOCK_NESTED_OUT /* RAW */
802 | READ_LOCK_OUT /* RAW */
803 | READ_UNLOCK_NESTED_OUT, /* RAW */
804 READ_UNLOCK_OUT_UNROLL);
805 :: CONSUME_TOKENS(proc_urcu_reader, READ_PROC_ALL_TOKENS, 0) ->
806 CLEAR_TOKENS(proc_urcu_reader, READ_PROC_ALL_TOKENS_CLEAR);
812 * Dependency between consecutive loops :
814 * WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp2 - 1)
815 * tmp = READ_CACHED_VAR(urcu_active_readers[get_readerid()]);
817 * _WHEN THE MB()s are in place_, they add full ordering of the
818 * generation pointer read wrt active reader count read, which ensures
819 * execution will not spill across loop execution.
820 * However, in the event mb()s are removed (execution using signal
821 * handler to promote barrier()() -> smp_mb()), nothing prevents one loop
822 * to spill its execution on other loop's execution.
845 active proctype urcu_reader()
852 assert(get_pid() < NR_PROCS);
858 * We do not test reader's progress here, because we are mainly
859 * interested in writer's progress. The reader never blocks
860 * anyway. We have to test for reader/writer's progress
861 * separately, otherwise we could think the writer is doing
862 * progress when it's blocked by an always progressing reader.
864 #ifdef READER_PROGRESS
867 urcu_one_read(i, j, nest_i, tmp, tmp2);
871 /* no name clash please */
872 #undef proc_urcu_reader
875 /* Model the RCU update process. */
878 * Bit encoding, urcu_writer :
879 * Currently only supports one reader.
882 int _proc_urcu_writer;
883 #define proc_urcu_writer _proc_urcu_writer
885 #define WRITE_PROD_NONE (1 << 0)
887 #define WRITE_DATA (1 << 1)
888 #define WRITE_PROC_WMB (1 << 2)
889 #define WRITE_XCHG_PTR (1 << 3)
891 #define WRITE_PROC_FIRST_MB (1 << 4)
894 #define WRITE_PROC_FIRST_READ_GP (1 << 5)
895 #define WRITE_PROC_FIRST_WRITE_GP (1 << 6)
896 #define WRITE_PROC_FIRST_WAIT (1 << 7)
897 #define WRITE_PROC_FIRST_WAIT_LOOP (1 << 8)
900 #define WRITE_PROC_SECOND_READ_GP (1 << 9)
901 #define WRITE_PROC_SECOND_WRITE_GP (1 << 10)
902 #define WRITE_PROC_SECOND_WAIT (1 << 11)
903 #define WRITE_PROC_SECOND_WAIT_LOOP (1 << 12)
905 #define WRITE_PROC_SECOND_MB (1 << 13)
907 #define WRITE_FREE (1 << 14)
909 #define WRITE_PROC_ALL_TOKENS (WRITE_PROD_NONE \
913 | WRITE_PROC_FIRST_MB \
914 | WRITE_PROC_FIRST_READ_GP \
915 | WRITE_PROC_FIRST_WRITE_GP \
916 | WRITE_PROC_FIRST_WAIT \
917 | WRITE_PROC_SECOND_READ_GP \
918 | WRITE_PROC_SECOND_WRITE_GP \
919 | WRITE_PROC_SECOND_WAIT \
920 | WRITE_PROC_SECOND_MB \
923 #define WRITE_PROC_ALL_TOKENS_CLEAR ((1 << 15) - 1)
926 * Mutexes are implied around writer execution. A single writer at a time.
928 active proctype urcu_writer()
931 byte tmp, tmp2, tmpa;
932 byte cur_data = 0, old_data, loop_nr = 0;
933 byte cur_gp_val = 0; /*
934 * Keep a local trace of the current parity so
935 * we don't add non-existing dependencies on the global
936 * GP update. Needed to test single flip case.
941 assert(get_pid() < NR_PROCS);
945 #ifdef WRITER_PROGRESS
948 loop_nr = loop_nr + 1;
950 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROD_NONE);
953 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_WMB);
957 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_MB);
958 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_MB);
962 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_READ_GP);
963 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WRITE_GP);
964 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT);
965 /* For single flip, we need to know the current parity */
966 cur_gp_val = cur_gp_val ^ RCU_GP_CTR_BIT;
973 :: CONSUME_TOKENS(proc_urcu_writer,
977 cur_data = (cur_data + 1) % SLAB_SIZE;
978 WRITE_CACHED_VAR(rcu_data[cur_data], WINE);
979 PRODUCE_TOKENS(proc_urcu_writer, WRITE_DATA);
982 :: CONSUME_TOKENS(proc_urcu_writer,
986 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_WMB);
988 :: CONSUME_TOKENS(proc_urcu_writer,
991 /* rcu_xchg_pointer() */
993 old_data = READ_CACHED_VAR(rcu_ptr);
994 WRITE_CACHED_VAR(rcu_ptr, cur_data);
996 PRODUCE_TOKENS(proc_urcu_writer, WRITE_XCHG_PTR);
998 :: CONSUME_TOKENS(proc_urcu_writer,
999 WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR,
1000 WRITE_PROC_FIRST_MB) ->
1003 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_MB);
1006 :: CONSUME_TOKENS(proc_urcu_writer,
1007 WRITE_PROC_FIRST_MB,
1008 WRITE_PROC_FIRST_READ_GP) ->
1009 tmpa = READ_CACHED_VAR(urcu_gp_ctr);
1010 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_READ_GP);
1011 :: CONSUME_TOKENS(proc_urcu_writer,
1012 WRITE_PROC_FIRST_MB | WRITE_PROC_WMB
1013 | WRITE_PROC_FIRST_READ_GP,
1014 WRITE_PROC_FIRST_WRITE_GP) ->
1016 WRITE_CACHED_VAR(urcu_gp_ctr, tmpa ^ RCU_GP_CTR_BIT);
1017 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WRITE_GP);
1019 :: CONSUME_TOKENS(proc_urcu_writer,
1020 //WRITE_PROC_FIRST_WRITE_GP /* TEST ADDING SYNC CORE */
1021 WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
1022 WRITE_PROC_FIRST_WAIT | WRITE_PROC_FIRST_WAIT_LOOP) ->
1024 /* ONLY WAITING FOR READER 0 */
1025 tmp2 = READ_CACHED_VAR(urcu_active_readers[0]);
1027 /* In normal execution, we are always starting by
1028 * waiting for the even parity.
1030 cur_gp_val = RCU_GP_CTR_BIT;
1033 :: (tmp2 & RCU_GP_CTR_NEST_MASK)
1034 && ((tmp2 ^ cur_gp_val) & RCU_GP_CTR_BIT) ->
1035 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WAIT_LOOP);
1037 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WAIT);
1040 :: CONSUME_TOKENS(proc_urcu_writer,
1041 //WRITE_PROC_FIRST_WRITE_GP /* TEST ADDING SYNC CORE */
1042 WRITE_PROC_FIRST_WRITE_GP
1043 | WRITE_PROC_FIRST_READ_GP
1044 | WRITE_PROC_FIRST_WAIT_LOOP
1045 | WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR
1046 | WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
1048 #ifndef GEN_ERROR_WRITER_PROGRESS
1054 /* This instruction loops to WRITE_PROC_FIRST_WAIT */
1055 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WAIT_LOOP | WRITE_PROC_FIRST_WAIT);
1058 :: CONSUME_TOKENS(proc_urcu_writer,
1059 WRITE_PROC_FIRST_WAIT /* Control dependency : need to branch out of
1060 * the loop to execute the next flip (CHECK) */
1061 | WRITE_PROC_FIRST_WRITE_GP
1062 | WRITE_PROC_FIRST_READ_GP
1063 | WRITE_PROC_FIRST_MB,
1064 WRITE_PROC_SECOND_READ_GP) ->
1066 tmpa = READ_CACHED_VAR(urcu_gp_ctr);
1067 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_READ_GP);
1068 :: CONSUME_TOKENS(proc_urcu_writer,
1071 | WRITE_PROC_FIRST_READ_GP
1072 | WRITE_PROC_FIRST_WRITE_GP
1073 | WRITE_PROC_SECOND_READ_GP,
1074 WRITE_PROC_SECOND_WRITE_GP) ->
1076 WRITE_CACHED_VAR(urcu_gp_ctr, tmpa ^ RCU_GP_CTR_BIT);
1077 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WRITE_GP);
1079 :: CONSUME_TOKENS(proc_urcu_writer,
1080 //WRITE_PROC_FIRST_WRITE_GP /* TEST ADDING SYNC CORE */
1081 WRITE_PROC_FIRST_WAIT
1082 | WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
1083 WRITE_PROC_SECOND_WAIT | WRITE_PROC_SECOND_WAIT_LOOP) ->
1085 /* ONLY WAITING FOR READER 0 */
1086 tmp2 = READ_CACHED_VAR(urcu_active_readers[0]);
1088 :: (tmp2 & RCU_GP_CTR_NEST_MASK)
1089 && ((tmp2 ^ 0) & RCU_GP_CTR_BIT) ->
1090 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT_LOOP);
1092 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT);
1095 :: CONSUME_TOKENS(proc_urcu_writer,
1096 //WRITE_PROC_FIRST_WRITE_GP /* TEST ADDING SYNC CORE */
1097 WRITE_PROC_SECOND_WRITE_GP
1098 | WRITE_PROC_FIRST_WRITE_GP
1099 | WRITE_PROC_SECOND_READ_GP
1100 | WRITE_PROC_FIRST_READ_GP
1101 | WRITE_PROC_SECOND_WAIT_LOOP
1102 | WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR
1103 | WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
1105 #ifndef GEN_ERROR_WRITER_PROGRESS
1111 /* This instruction loops to WRITE_PROC_SECOND_WAIT */
1112 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT_LOOP | WRITE_PROC_SECOND_WAIT);
1115 :: CONSUME_TOKENS(proc_urcu_writer,
1116 WRITE_PROC_FIRST_WAIT
1117 | WRITE_PROC_SECOND_WAIT
1118 | WRITE_PROC_FIRST_READ_GP
1119 | WRITE_PROC_SECOND_READ_GP
1120 | WRITE_PROC_FIRST_WRITE_GP
1121 | WRITE_PROC_SECOND_WRITE_GP
1122 | WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR
1123 | WRITE_PROC_FIRST_MB,
1124 WRITE_PROC_SECOND_MB) ->
1127 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_MB);
1129 :: CONSUME_TOKENS(proc_urcu_writer,
1131 | WRITE_PROC_FIRST_WAIT
1132 | WRITE_PROC_SECOND_WAIT
1133 | WRITE_PROC_WMB /* No dependency on
1134 * WRITE_DATA because we
1136 * different location. */
1137 | WRITE_PROC_SECOND_MB
1138 | WRITE_PROC_FIRST_MB,
1140 WRITE_CACHED_VAR(rcu_data[old_data], POISON);
1141 PRODUCE_TOKENS(proc_urcu_writer, WRITE_FREE);
1143 :: CONSUME_TOKENS(proc_urcu_writer, WRITE_PROC_ALL_TOKENS, 0) ->
1144 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_ALL_TOKENS_CLEAR);
1150 * Note : Promela model adds implicit serialization of the
1151 * WRITE_FREE instruction. Normally, it would be permitted to
1152 * spill on the next loop execution. Given the validation we do
1153 * checks for the data entry read to be poisoned, it's ok if
1154 * we do not check "late arriving" memory poisoning.
1159 * Given the reader loops infinitely, let the writer also busy-loop
1160 * with progress here so, with weak fairness, we can test the
1161 * writer's progress.
1166 #ifdef WRITER_PROGRESS
1172 /* Non-atomic parts of the loop */
1175 smp_mb_send(i, j, 1);
1176 goto smp_mb_send1_end;
1177 #ifndef GEN_ERROR_WRITER_PROGRESS
1179 smp_mb_send(i, j, 2);
1180 goto smp_mb_send2_end;
1182 smp_mb_send(i, j, 3);
1183 goto smp_mb_send3_end;
1186 smp_mb_send(i, j, 4);
1187 goto smp_mb_send4_end;
1192 /* no name clash please */
1193 #undef proc_urcu_writer
1196 /* Leave after the readers and writers so the pid count is ok. */
1201 INIT_CACHED_VAR(urcu_gp_ctr, 1, j);
1202 INIT_CACHED_VAR(rcu_ptr, 0, j);
1206 :: i < NR_READERS ->
1207 INIT_CACHED_VAR(urcu_active_readers[i], 0, j);
1208 ptr_read_first[i] = 1;
1209 ptr_read_second[i] = 1;
1210 data_read_first[i] = WINE;
1211 data_read_second[i] = WINE;
1213 :: i >= NR_READERS -> break
1215 INIT_CACHED_VAR(rcu_data[0], WINE, j);
1219 INIT_CACHED_VAR(rcu_data[i], POISON, j);
1221 :: i >= SLAB_SIZE -> break