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
18 //#define ARCH_POWERPC
20 * mem.spin: Promela code to validate memory barriers with OOO memory
21 * and out-of-order instruction scheduling.
23 * This program is free software; you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation; either version 2 of the License, or
26 * (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
37 * Copyright (c) 2009 Mathieu Desnoyers
40 /* Promela validation variables. */
42 /* specific defines "included" here */
43 /* DEFINES file "included" here */
50 #define get_pid() (_pid)
52 #define get_readerid() (get_pid())
55 * Produced process control and data flow. Updated after each instruction to
56 * show which variables are ready. Using one-hot bit encoding per variable to
57 * save state space. Used as triggers to execute the instructions having those
58 * variables as input. Leaving bits active to inhibit instruction execution.
59 * Scheme used to make instruction disabling and automatic dependency fall-back
63 #define CONSUME_TOKENS(state, bits, notbits) \
64 ((!(state & (notbits))) && (state & (bits)) == (bits))
66 #define PRODUCE_TOKENS(state, bits) \
67 state = state | (bits);
69 #define CLEAR_TOKENS(state, bits) \
70 state = state & ~(bits)
73 * Types of dependency :
77 * - True dependency, Read-after-Write (RAW)
79 * This type of dependency happens when a statement depends on the result of a
80 * previous statement. This applies to any statement which needs to read a
81 * variable written by a preceding statement.
83 * - False dependency, Write-after-Read (WAR)
85 * Typically, variable renaming can ensure that this dependency goes away.
86 * However, if the statements must read and then write from/to the same variable
87 * in the OOO memory model, renaming may be impossible, and therefore this
88 * causes a WAR dependency.
90 * - Output dependency, Write-after-Write (WAW)
92 * Two writes to the same variable in subsequent statements. Variable renaming
93 * can ensure this is not needed, but can be required when writing multiple
94 * times to the same OOO mem model variable.
98 * Execution of a given instruction depends on a previous instruction evaluating
99 * in a way that allows its execution. E.g. : branches.
101 * Useful considerations for joining dependencies after branch
105 * "We say box i dominates box j if every path (leading from input to output
106 * through the diagram) which passes through box j must also pass through box
107 * i. Thus box i dominates box j if box j is subordinate to box i in the
110 * http://www.hipersoft.rice.edu/grads/publications/dom14.pdf
111 * Other classic algorithm to calculate dominance : Lengauer-Tarjan (in gcc)
115 * Just as pre-dominance, but with arcs of the data flow inverted, and input vs
116 * output exchanged. Therefore, i post-dominating j ensures that every path
117 * passing by j will pass by i before reaching the output.
119 * Other considerations
121 * Note about "volatile" keyword dependency : The compiler will order volatile
122 * accesses so they appear in the right order on a given CPU. They can be
123 * reordered by the CPU instruction scheduling. This therefore cannot be
124 * considered as a depencency.
128 * Cooper, Keith D.; & Torczon, Linda. (2005). Engineering a Compiler. Morgan
129 * Kaufmann. ISBN 1-55860-698-X.
130 * Kennedy, Ken; & Allen, Randy. (2001). Optimizing Compilers for Modern
131 * Architectures: A Dependence-based Approach. Morgan Kaufmann. ISBN
133 * Muchnick, Steven S. (1997). Advanced Compiler Design and Implementation.
134 * Morgan Kaufmann. ISBN 1-55860-320-4.
138 * Note about loops and nested calls
140 * To keep this model simple, loops expressed in the framework will behave as if
141 * there was a core synchronizing instruction between loops. To see the effect
142 * of loop unrolling, manually unrolling loops is required. Note that if loops
143 * end or start with a core synchronizing instruction, the model is appropriate.
144 * Nested calls are not supported.
148 * Only Alpha has out-of-order cache bank loads. Other architectures (intel,
149 * powerpc, arm) ensure that dependent reads won't be reordered. c.f.
150 * http://www.linuxjournal.com/article/8212)
152 #define HAVE_OOO_CACHE_READ
156 * Each process have its own data in cache. Caches are randomly updated.
157 * smp_wmb and smp_rmb forces cache updates (write and read), smp_mb forces
161 typedef per_proc_byte {
165 typedef per_proc_bit {
169 /* Bitfield has a maximum of 8 procs */
170 typedef per_proc_bitfield {
174 #define DECLARE_CACHED_VAR(type, x) \
176 per_proc_##type cached_##x; \
177 per_proc_bitfield cache_dirty_##x;
179 #define INIT_CACHED_VAR(x, v, j) \
181 cache_dirty_##x.bitfield = 0; \
185 cached_##x.val[j] = v; \
187 :: j >= NR_PROCS -> break \
190 #define IS_CACHE_DIRTY(x, id) (cache_dirty_##x.bitfield & (1 << id))
192 #define READ_CACHED_VAR(x) (cached_##x.val[get_pid()])
194 #define WRITE_CACHED_VAR(x, v) \
196 cached_##x.val[get_pid()] = v; \
197 cache_dirty_##x.bitfield = \
198 cache_dirty_##x.bitfield | (1 << get_pid()); \
201 #define CACHE_WRITE_TO_MEM(x, id) \
203 :: IS_CACHE_DIRTY(x, id) -> \
204 mem_##x = cached_##x.val[id]; \
205 cache_dirty_##x.bitfield = \
206 cache_dirty_##x.bitfield & (~(1 << id)); \
211 #define CACHE_READ_FROM_MEM(x, id) \
213 :: !IS_CACHE_DIRTY(x, id) -> \
214 cached_##x.val[id] = mem_##x;\
220 * May update other caches if cache is dirty, or not.
222 #define RANDOM_CACHE_WRITE_TO_MEM(x, id)\
224 :: 1 -> CACHE_WRITE_TO_MEM(x, id); \
228 #define RANDOM_CACHE_READ_FROM_MEM(x, id)\
230 :: 1 -> CACHE_READ_FROM_MEM(x, id); \
234 /* Must consume all prior read tokens. All subsequent reads depend on it. */
238 CACHE_READ_FROM_MEM(urcu_gp_ctr, get_pid());
242 CACHE_READ_FROM_MEM(urcu_active_readers[i], get_pid());
244 :: i >= NR_READERS -> break
246 CACHE_READ_FROM_MEM(rcu_ptr, get_pid());
250 CACHE_READ_FROM_MEM(rcu_data[i], get_pid());
252 :: i >= SLAB_SIZE -> break
257 /* Must consume all prior write tokens. All subsequent writes depend on it. */
261 CACHE_WRITE_TO_MEM(urcu_gp_ctr, get_pid());
265 CACHE_WRITE_TO_MEM(urcu_active_readers[i], get_pid());
267 :: i >= NR_READERS -> break
269 CACHE_WRITE_TO_MEM(rcu_ptr, get_pid());
273 CACHE_WRITE_TO_MEM(rcu_data[i], get_pid());
275 :: i >= SLAB_SIZE -> break
280 /* Synchronization point. Must consume all prior read and write tokens. All
281 * subsequent reads and writes depend on it. */
290 #ifdef REMOTE_BARRIERS
292 bit reader_barrier[NR_READERS];
295 * We cannot leave the barriers dependencies in place in REMOTE_BARRIERS mode
296 * because they would add unexisting core synchronization and would therefore
297 * create an incomplete model.
298 * Therefore, we model the read-side memory barriers by completely disabling the
299 * memory barriers and their dependencies from the read-side. One at a time
300 * (different verification runs), we make a different instruction listen for
304 #define smp_mb_reader(i, j)
307 * Service 0, 1 or many barrier requests.
309 inline smp_mb_recv(i, j)
312 :: (reader_barrier[get_readerid()] == 1) ->
314 * We choose to ignore cycles caused by writer busy-looping,
315 * waiting for the reader, sending barrier requests, and the
316 * reader always services them without continuing execution.
318 progress_ignoring_mb1:
320 reader_barrier[get_readerid()] = 0;
323 * We choose to ignore writer's non-progress caused by the
324 * reader ignoring the writer's mb() requests.
326 progress_ignoring_mb2:
331 #define PROGRESS_LABEL(progressid) progress_writer_progid_##progressid:
333 #define smp_mb_send(i, j, progressid) \
338 :: i < NR_READERS -> \
339 reader_barrier[i] = 1; \
341 * Busy-looping waiting for reader barrier handling is of little\
342 * interest, given the reader has the ability to totally ignore \
343 * barrier requests. \
346 :: (reader_barrier[i] == 1) -> \
347 PROGRESS_LABEL(progressid) \
349 :: (reader_barrier[i] == 0) -> break; \
352 :: i >= NR_READERS -> \
360 #define smp_mb_send(i, j, progressid) smp_mb(i)
361 #define smp_mb_reader smp_mb(i)
362 #define smp_mb_recv(i, j)
366 /* Keep in sync manually with smp_rmb, smp_wmb, ooo_mem and init() */
367 DECLARE_CACHED_VAR(byte, urcu_gp_ctr);
368 /* Note ! currently only one reader */
369 DECLARE_CACHED_VAR(byte, urcu_active_readers[NR_READERS]);
371 DECLARE_CACHED_VAR(bit, rcu_data[SLAB_SIZE]);
375 DECLARE_CACHED_VAR(bit, rcu_ptr);
376 bit ptr_read_first[NR_READERS];
377 bit ptr_read_second[NR_READERS];
379 DECLARE_CACHED_VAR(byte, rcu_ptr);
380 byte ptr_read_first[NR_READERS];
381 byte ptr_read_second[NR_READERS];
384 bit data_read_first[NR_READERS];
385 bit data_read_second[NR_READERS];
389 inline wait_init_done()
392 :: init_done == 0 -> skip;
400 RANDOM_CACHE_WRITE_TO_MEM(urcu_gp_ctr, get_pid());
404 RANDOM_CACHE_WRITE_TO_MEM(urcu_active_readers[i],
407 :: i >= NR_READERS -> break
409 RANDOM_CACHE_WRITE_TO_MEM(rcu_ptr, get_pid());
413 RANDOM_CACHE_WRITE_TO_MEM(rcu_data[i], get_pid());
415 :: i >= SLAB_SIZE -> break
417 #ifdef HAVE_OOO_CACHE_READ
418 RANDOM_CACHE_READ_FROM_MEM(urcu_gp_ctr, get_pid());
422 RANDOM_CACHE_READ_FROM_MEM(urcu_active_readers[i],
425 :: i >= NR_READERS -> break
427 RANDOM_CACHE_READ_FROM_MEM(rcu_ptr, get_pid());
431 RANDOM_CACHE_READ_FROM_MEM(rcu_data[i], get_pid());
433 :: i >= SLAB_SIZE -> break
437 #endif /* HAVE_OOO_CACHE_READ */
442 * Bit encoding, urcu_reader :
445 int _proc_urcu_reader;
446 #define proc_urcu_reader _proc_urcu_reader
448 /* Body of PROCEDURE_READ_LOCK */
449 #define READ_PROD_A_READ (1 << 0)
450 #define READ_PROD_B_IF_TRUE (1 << 1)
451 #define READ_PROD_B_IF_FALSE (1 << 2)
452 #define READ_PROD_C_IF_TRUE_READ (1 << 3)
454 #define PROCEDURE_READ_LOCK(base, consumetoken, producetoken) \
455 :: CONSUME_TOKENS(proc_urcu_reader, consumetoken, READ_PROD_A_READ << base) -> \
457 tmp = READ_CACHED_VAR(urcu_active_readers[get_readerid()]); \
458 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_A_READ << base); \
459 :: CONSUME_TOKENS(proc_urcu_reader, \
460 READ_PROD_A_READ << base, /* RAW, pre-dominant */ \
461 (READ_PROD_B_IF_TRUE | READ_PROD_B_IF_FALSE) << base) -> \
463 :: (!(tmp & RCU_GP_CTR_NEST_MASK)) -> \
464 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_B_IF_TRUE << base); \
466 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_B_IF_FALSE << base); \
469 :: CONSUME_TOKENS(proc_urcu_reader, READ_PROD_B_IF_TRUE << base, \
470 READ_PROD_C_IF_TRUE_READ << base) -> \
472 tmp2 = READ_CACHED_VAR(urcu_gp_ctr); \
473 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_C_IF_TRUE_READ << base); \
474 :: CONSUME_TOKENS(proc_urcu_reader, \
475 (READ_PROD_C_IF_TRUE_READ /* pre-dominant */ \
476 | READ_PROD_A_READ) << base, /* WAR */ \
479 WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp2); \
480 PRODUCE_TOKENS(proc_urcu_reader, producetoken); \
481 /* IF_MERGE implies \
482 * post-dominance */ \
484 :: CONSUME_TOKENS(proc_urcu_reader, \
485 (READ_PROD_B_IF_FALSE /* pre-dominant */ \
486 | READ_PROD_A_READ) << base, /* WAR */ \
489 WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], \
491 PRODUCE_TOKENS(proc_urcu_reader, producetoken); \
492 /* IF_MERGE implies \
493 * post-dominance */ \
497 /* Body of PROCEDURE_READ_LOCK */
498 #define READ_PROC_READ_UNLOCK (1 << 0)
500 #define PROCEDURE_READ_UNLOCK(base, consumetoken, producetoken) \
501 :: CONSUME_TOKENS(proc_urcu_reader, \
503 READ_PROC_READ_UNLOCK << base) -> \
505 tmp2 = READ_CACHED_VAR(urcu_active_readers[get_readerid()]); \
506 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_READ_UNLOCK << base); \
507 :: CONSUME_TOKENS(proc_urcu_reader, \
509 | (READ_PROC_READ_UNLOCK << base), /* WAR */ \
512 WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp2 - 1); \
513 PRODUCE_TOKENS(proc_urcu_reader, producetoken); \
517 #define READ_PROD_NONE (1 << 0)
519 /* PROCEDURE_READ_LOCK base = << 1 : 1 to 5 */
520 #define READ_LOCK_BASE 1
521 #define READ_LOCK_OUT (1 << 5)
523 #define READ_PROC_FIRST_MB (1 << 6)
525 /* PROCEDURE_READ_LOCK (NESTED) base : << 7 : 7 to 11 */
526 #define READ_LOCK_NESTED_BASE 7
527 #define READ_LOCK_NESTED_OUT (1 << 11)
529 #define READ_PROC_READ_GEN (1 << 12)
530 #define READ_PROC_ACCESS_GEN (1 << 13)
532 /* PROCEDURE_READ_UNLOCK (NESTED) base = << 14 : 14 to 15 */
533 #define READ_UNLOCK_NESTED_BASE 14
534 #define READ_UNLOCK_NESTED_OUT (1 << 15)
536 #define READ_PROC_SECOND_MB (1 << 16)
538 /* PROCEDURE_READ_UNLOCK base = << 17 : 17 to 18 */
539 #define READ_UNLOCK_BASE 17
540 #define READ_UNLOCK_OUT (1 << 18)
542 /* PROCEDURE_READ_LOCK_UNROLL base = << 19 : 19 to 23 */
543 #define READ_LOCK_UNROLL_BASE 19
544 #define READ_LOCK_OUT_UNROLL (1 << 23)
546 #define READ_PROC_THIRD_MB (1 << 24)
548 #define READ_PROC_READ_GEN_UNROLL (1 << 25)
549 #define READ_PROC_ACCESS_GEN_UNROLL (1 << 26)
551 #define READ_PROC_FOURTH_MB (1 << 27)
553 /* PROCEDURE_READ_UNLOCK_UNROLL base = << 28 : 28 to 29 */
554 #define READ_UNLOCK_UNROLL_BASE 28
555 #define READ_UNLOCK_OUT_UNROLL (1 << 29)
558 /* Should not include branches */
559 #define READ_PROC_ALL_TOKENS (READ_PROD_NONE \
561 | READ_PROC_FIRST_MB \
562 | READ_LOCK_NESTED_OUT \
563 | READ_PROC_READ_GEN \
564 | READ_PROC_ACCESS_GEN \
565 | READ_UNLOCK_NESTED_OUT \
566 | READ_PROC_SECOND_MB \
568 | READ_LOCK_OUT_UNROLL \
569 | READ_PROC_THIRD_MB \
570 | READ_PROC_READ_GEN_UNROLL \
571 | READ_PROC_ACCESS_GEN_UNROLL \
572 | READ_PROC_FOURTH_MB \
573 | READ_UNLOCK_OUT_UNROLL)
575 /* Must clear all tokens, including branches */
576 #define READ_PROC_ALL_TOKENS_CLEAR ((1 << 30) - 1)
578 inline urcu_one_read(i, j, nest_i, tmp, tmp2)
580 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_NONE);
583 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FIRST_MB);
584 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_SECOND_MB);
585 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_THIRD_MB);
586 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FOURTH_MB);
589 #ifdef REMOTE_BARRIERS
590 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FIRST_MB);
591 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_SECOND_MB);
592 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_THIRD_MB);
593 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FOURTH_MB);
599 #ifdef REMOTE_BARRIERS
601 * Signal-based memory barrier will only execute when the
602 * execution order appears in program order.
608 :: CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE,
609 READ_LOCK_OUT | READ_LOCK_NESTED_OUT
610 | READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
612 | READ_LOCK_OUT_UNROLL
613 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
614 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT,
616 | READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
618 | READ_LOCK_OUT_UNROLL
619 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
620 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT | READ_LOCK_NESTED_OUT,
621 READ_PROC_READ_GEN | 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
629 | READ_LOCK_OUT_UNROLL
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 | READ_PROC_ACCESS_GEN,
633 READ_UNLOCK_NESTED_OUT
635 | READ_LOCK_OUT_UNROLL
636 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
637 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
638 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN
639 | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT,
641 | READ_LOCK_OUT_UNROLL
642 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
643 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
644 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN
645 | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
648 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
649 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
650 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN
651 | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
652 | READ_UNLOCK_OUT | READ_LOCK_OUT_UNROLL,
653 READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
654 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
655 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN
656 | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
657 | READ_UNLOCK_OUT | READ_LOCK_OUT_UNROLL
658 | READ_PROC_READ_GEN_UNROLL,
659 READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
660 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
661 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN
662 | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
663 | READ_UNLOCK_OUT | READ_LOCK_OUT_UNROLL
664 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL,
665 READ_UNLOCK_OUT_UNROLL)
666 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
667 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
668 | READ_UNLOCK_OUT | READ_LOCK_OUT_UNROLL
669 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL,
678 goto non_atomic3_skip;
681 goto non_atomic3_end;
684 #endif /* REMOTE_BARRIERS */
688 PROCEDURE_READ_LOCK(READ_LOCK_BASE, READ_PROD_NONE, READ_LOCK_OUT);
690 :: CONSUME_TOKENS(proc_urcu_reader,
691 READ_LOCK_OUT, /* post-dominant */
692 READ_PROC_FIRST_MB) ->
694 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FIRST_MB);
696 PROCEDURE_READ_LOCK(READ_LOCK_NESTED_BASE, READ_PROC_FIRST_MB | READ_LOCK_OUT,
697 READ_LOCK_NESTED_OUT);
699 :: CONSUME_TOKENS(proc_urcu_reader,
700 READ_PROC_FIRST_MB, /* mb() orders reads */
701 READ_PROC_READ_GEN) ->
703 ptr_read_first[get_readerid()] = READ_CACHED_VAR(rcu_ptr);
704 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_READ_GEN);
706 :: CONSUME_TOKENS(proc_urcu_reader,
707 READ_PROC_FIRST_MB /* mb() orders reads */
708 | READ_PROC_READ_GEN,
709 READ_PROC_ACCESS_GEN) ->
710 /* smp_read_barrier_depends */
713 data_read_first[get_readerid()] =
714 READ_CACHED_VAR(rcu_data[ptr_read_first[get_readerid()]]);
715 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_ACCESS_GEN);
718 /* Note : we remove the nested memory barrier from the read unlock
719 * model, given it is not usually needed. The implementation has the barrier
720 * because the performance impact added by a branch in the common case does not
724 PROCEDURE_READ_UNLOCK(READ_UNLOCK_NESTED_BASE,
727 | READ_LOCK_NESTED_OUT,
728 READ_UNLOCK_NESTED_OUT);
731 :: CONSUME_TOKENS(proc_urcu_reader,
732 READ_PROC_ACCESS_GEN /* mb() orders reads */
733 | READ_PROC_READ_GEN /* mb() orders reads */
734 | READ_PROC_FIRST_MB /* mb() ordered */
735 | READ_LOCK_OUT /* post-dominant */
736 | READ_LOCK_NESTED_OUT /* post-dominant */
737 | READ_UNLOCK_NESTED_OUT,
738 READ_PROC_SECOND_MB) ->
740 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_SECOND_MB);
742 PROCEDURE_READ_UNLOCK(READ_UNLOCK_BASE,
743 READ_PROC_SECOND_MB /* mb() orders reads */
744 | READ_PROC_FIRST_MB /* mb() orders reads */
745 | READ_LOCK_NESTED_OUT /* RAW */
746 | READ_LOCK_OUT /* RAW */
747 | READ_UNLOCK_NESTED_OUT, /* RAW */
750 /* Unrolling loop : second consecutive lock */
751 /* reading urcu_active_readers, which have been written by
752 * READ_UNLOCK_OUT : RAW */
753 PROCEDURE_READ_LOCK(READ_LOCK_UNROLL_BASE,
754 READ_UNLOCK_OUT /* RAW */
755 | READ_PROC_SECOND_MB /* mb() orders reads */
756 | READ_PROC_FIRST_MB /* mb() orders reads */
757 | READ_LOCK_NESTED_OUT /* RAW */
758 | READ_LOCK_OUT /* RAW */
759 | READ_UNLOCK_NESTED_OUT, /* RAW */
760 READ_LOCK_OUT_UNROLL);
763 :: CONSUME_TOKENS(proc_urcu_reader,
764 READ_PROC_FIRST_MB /* mb() ordered */
765 | READ_PROC_SECOND_MB /* mb() ordered */
766 | READ_LOCK_OUT_UNROLL /* post-dominant */
767 | READ_LOCK_NESTED_OUT
769 | READ_UNLOCK_NESTED_OUT
771 READ_PROC_THIRD_MB) ->
773 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_THIRD_MB);
775 :: CONSUME_TOKENS(proc_urcu_reader,
776 READ_PROC_FIRST_MB /* mb() orders reads */
777 | READ_PROC_SECOND_MB /* mb() orders reads */
778 | READ_PROC_THIRD_MB, /* mb() orders reads */
779 READ_PROC_READ_GEN_UNROLL) ->
781 ptr_read_second[get_readerid()] = READ_CACHED_VAR(rcu_ptr);
782 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_READ_GEN_UNROLL);
784 :: CONSUME_TOKENS(proc_urcu_reader,
785 READ_PROC_READ_GEN_UNROLL
786 | READ_PROC_FIRST_MB /* mb() orders reads */
787 | READ_PROC_SECOND_MB /* mb() orders reads */
788 | READ_PROC_THIRD_MB, /* mb() orders reads */
789 READ_PROC_ACCESS_GEN_UNROLL) ->
790 /* smp_read_barrier_depends */
793 data_read_second[get_readerid()] =
794 READ_CACHED_VAR(rcu_data[ptr_read_second[get_readerid()]]);
795 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_ACCESS_GEN_UNROLL);
797 :: CONSUME_TOKENS(proc_urcu_reader,
798 READ_PROC_READ_GEN_UNROLL /* mb() orders reads */
799 | READ_PROC_ACCESS_GEN_UNROLL /* mb() orders reads */
800 | READ_PROC_FIRST_MB /* mb() ordered */
801 | READ_PROC_SECOND_MB /* mb() ordered */
802 | READ_PROC_THIRD_MB /* mb() ordered */
803 | READ_LOCK_OUT_UNROLL /* post-dominant */
804 | READ_LOCK_NESTED_OUT
806 | READ_UNLOCK_NESTED_OUT
808 READ_PROC_FOURTH_MB) ->
810 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FOURTH_MB);
812 PROCEDURE_READ_UNLOCK(READ_UNLOCK_UNROLL_BASE,
813 READ_PROC_FOURTH_MB /* mb() orders reads */
814 | READ_PROC_THIRD_MB /* mb() orders reads */
815 | READ_LOCK_OUT_UNROLL /* RAW */
816 | READ_PROC_SECOND_MB /* mb() orders reads */
817 | READ_PROC_FIRST_MB /* mb() orders reads */
818 | READ_LOCK_NESTED_OUT /* RAW */
819 | READ_LOCK_OUT /* RAW */
820 | READ_UNLOCK_NESTED_OUT, /* RAW */
821 READ_UNLOCK_OUT_UNROLL);
822 :: CONSUME_TOKENS(proc_urcu_reader, READ_PROC_ALL_TOKENS, 0) ->
823 CLEAR_TOKENS(proc_urcu_reader, READ_PROC_ALL_TOKENS_CLEAR);
829 * Dependency between consecutive loops :
831 * WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp2 - 1)
832 * tmp = READ_CACHED_VAR(urcu_active_readers[get_readerid()]);
834 * _WHEN THE MB()s are in place_, they add full ordering of the
835 * generation pointer read wrt active reader count read, which ensures
836 * execution will not spill across loop execution.
837 * However, in the event mb()s are removed (execution using signal
838 * handler to promote barrier()() -> smp_mb()), nothing prevents one loop
839 * to spill its execution on other loop's execution.
862 active proctype urcu_reader()
869 assert(get_pid() < NR_PROCS);
875 * We do not test reader's progress here, because we are mainly
876 * interested in writer's progress. The reader never blocks
877 * anyway. We have to test for reader/writer's progress
878 * separately, otherwise we could think the writer is doing
879 * progress when it's blocked by an always progressing reader.
881 #ifdef READER_PROGRESS
884 urcu_one_read(i, j, nest_i, tmp, tmp2);
888 /* no name clash please */
889 #undef proc_urcu_reader
892 /* Model the RCU update process. */
895 * Bit encoding, urcu_writer :
896 * Currently only supports one reader.
899 int _proc_urcu_writer;
900 #define proc_urcu_writer _proc_urcu_writer
902 #define WRITE_PROD_NONE (1 << 0)
904 #define WRITE_DATA (1 << 1)
905 #define WRITE_PROC_WMB (1 << 2)
906 #define WRITE_XCHG_PTR (1 << 3)
908 #define WRITE_PROC_FIRST_MB (1 << 4)
911 #define WRITE_PROC_FIRST_READ_GP (1 << 5)
912 #define WRITE_PROC_FIRST_WRITE_GP (1 << 6)
913 #define WRITE_PROC_FIRST_WAIT (1 << 7)
914 #define WRITE_PROC_FIRST_WAIT_LOOP (1 << 8)
917 #define WRITE_PROC_SECOND_READ_GP (1 << 9)
918 #define WRITE_PROC_SECOND_WRITE_GP (1 << 10)
919 #define WRITE_PROC_SECOND_WAIT (1 << 11)
920 #define WRITE_PROC_SECOND_WAIT_LOOP (1 << 12)
922 #define WRITE_PROC_SECOND_MB (1 << 13)
924 #define WRITE_FREE (1 << 14)
926 #define WRITE_PROC_ALL_TOKENS (WRITE_PROD_NONE \
930 | WRITE_PROC_FIRST_MB \
931 | WRITE_PROC_FIRST_READ_GP \
932 | WRITE_PROC_FIRST_WRITE_GP \
933 | WRITE_PROC_FIRST_WAIT \
934 | WRITE_PROC_SECOND_READ_GP \
935 | WRITE_PROC_SECOND_WRITE_GP \
936 | WRITE_PROC_SECOND_WAIT \
937 | WRITE_PROC_SECOND_MB \
940 #define WRITE_PROC_ALL_TOKENS_CLEAR ((1 << 15) - 1)
943 * Mutexes are implied around writer execution. A single writer at a time.
945 active proctype urcu_writer()
948 byte tmp, tmp2, tmpa;
949 byte cur_data = 0, old_data, loop_nr = 0;
950 byte cur_gp_val = 0; /*
951 * Keep a local trace of the current parity so
952 * we don't add non-existing dependencies on the global
953 * GP update. Needed to test single flip case.
958 assert(get_pid() < NR_PROCS);
962 #ifdef WRITER_PROGRESS
965 loop_nr = loop_nr + 1;
967 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROD_NONE);
970 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_WMB);
974 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_MB);
975 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_MB);
979 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_READ_GP);
980 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WRITE_GP);
981 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT);
982 /* For single flip, we need to know the current parity */
983 cur_gp_val = cur_gp_val ^ RCU_GP_CTR_BIT;
990 :: CONSUME_TOKENS(proc_urcu_writer,
994 cur_data = (cur_data + 1) % SLAB_SIZE;
995 WRITE_CACHED_VAR(rcu_data[cur_data], WINE);
996 PRODUCE_TOKENS(proc_urcu_writer, WRITE_DATA);
999 :: CONSUME_TOKENS(proc_urcu_writer,
1003 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_WMB);
1005 :: CONSUME_TOKENS(proc_urcu_writer,
1008 /* rcu_xchg_pointer() */
1010 old_data = READ_CACHED_VAR(rcu_ptr);
1011 WRITE_CACHED_VAR(rcu_ptr, cur_data);
1013 PRODUCE_TOKENS(proc_urcu_writer, WRITE_XCHG_PTR);
1015 :: CONSUME_TOKENS(proc_urcu_writer,
1016 WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR,
1017 WRITE_PROC_FIRST_MB) ->
1020 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_MB);
1023 :: CONSUME_TOKENS(proc_urcu_writer,
1024 WRITE_PROC_FIRST_MB,
1025 WRITE_PROC_FIRST_READ_GP) ->
1026 tmpa = READ_CACHED_VAR(urcu_gp_ctr);
1027 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_READ_GP);
1028 :: CONSUME_TOKENS(proc_urcu_writer,
1029 WRITE_PROC_FIRST_MB | WRITE_PROC_WMB
1030 | WRITE_PROC_FIRST_READ_GP,
1031 WRITE_PROC_FIRST_WRITE_GP) ->
1033 WRITE_CACHED_VAR(urcu_gp_ctr, tmpa ^ RCU_GP_CTR_BIT);
1034 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WRITE_GP);
1036 :: CONSUME_TOKENS(proc_urcu_writer,
1037 //WRITE_PROC_FIRST_WRITE_GP /* TEST ADDING SYNC CORE */
1038 WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
1039 WRITE_PROC_FIRST_WAIT | WRITE_PROC_FIRST_WAIT_LOOP) ->
1041 /* ONLY WAITING FOR READER 0 */
1042 tmp2 = READ_CACHED_VAR(urcu_active_readers[0]);
1044 /* In normal execution, we are always starting by
1045 * waiting for the even parity.
1047 cur_gp_val = RCU_GP_CTR_BIT;
1050 :: (tmp2 & RCU_GP_CTR_NEST_MASK)
1051 && ((tmp2 ^ cur_gp_val) & RCU_GP_CTR_BIT) ->
1052 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WAIT_LOOP);
1054 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WAIT);
1057 :: CONSUME_TOKENS(proc_urcu_writer,
1058 //WRITE_PROC_FIRST_WRITE_GP /* TEST ADDING SYNC CORE */
1059 WRITE_PROC_FIRST_WRITE_GP
1060 | WRITE_PROC_FIRST_READ_GP
1061 | WRITE_PROC_FIRST_WAIT_LOOP
1062 | WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR
1063 | WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
1065 #ifndef GEN_ERROR_WRITER_PROGRESS
1071 /* This instruction loops to WRITE_PROC_FIRST_WAIT */
1072 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WAIT_LOOP | WRITE_PROC_FIRST_WAIT);
1075 :: CONSUME_TOKENS(proc_urcu_writer,
1076 WRITE_PROC_FIRST_WAIT /* Control dependency : need to branch out of
1077 * the loop to execute the next flip (CHECK) */
1078 | WRITE_PROC_FIRST_WRITE_GP
1079 | WRITE_PROC_FIRST_READ_GP
1080 | WRITE_PROC_FIRST_MB,
1081 WRITE_PROC_SECOND_READ_GP) ->
1083 tmpa = READ_CACHED_VAR(urcu_gp_ctr);
1084 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_READ_GP);
1085 :: CONSUME_TOKENS(proc_urcu_writer,
1088 | WRITE_PROC_FIRST_READ_GP
1089 | WRITE_PROC_FIRST_WRITE_GP
1090 | WRITE_PROC_SECOND_READ_GP,
1091 WRITE_PROC_SECOND_WRITE_GP) ->
1093 WRITE_CACHED_VAR(urcu_gp_ctr, tmpa ^ RCU_GP_CTR_BIT);
1094 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WRITE_GP);
1096 :: CONSUME_TOKENS(proc_urcu_writer,
1097 //WRITE_PROC_FIRST_WRITE_GP /* TEST ADDING SYNC CORE */
1098 WRITE_PROC_FIRST_WAIT
1099 | WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
1100 WRITE_PROC_SECOND_WAIT | WRITE_PROC_SECOND_WAIT_LOOP) ->
1102 /* ONLY WAITING FOR READER 0 */
1103 tmp2 = READ_CACHED_VAR(urcu_active_readers[0]);
1105 :: (tmp2 & RCU_GP_CTR_NEST_MASK)
1106 && ((tmp2 ^ 0) & RCU_GP_CTR_BIT) ->
1107 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT_LOOP);
1109 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT);
1112 :: CONSUME_TOKENS(proc_urcu_writer,
1113 //WRITE_PROC_FIRST_WRITE_GP /* TEST ADDING SYNC CORE */
1114 WRITE_PROC_SECOND_WRITE_GP
1115 | WRITE_PROC_FIRST_WRITE_GP
1116 | WRITE_PROC_SECOND_READ_GP
1117 | WRITE_PROC_FIRST_READ_GP
1118 | WRITE_PROC_SECOND_WAIT_LOOP
1119 | WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR
1120 | WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
1122 #ifndef GEN_ERROR_WRITER_PROGRESS
1128 /* This instruction loops to WRITE_PROC_SECOND_WAIT */
1129 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT_LOOP | WRITE_PROC_SECOND_WAIT);
1132 :: CONSUME_TOKENS(proc_urcu_writer,
1133 WRITE_PROC_FIRST_WAIT
1134 | WRITE_PROC_SECOND_WAIT
1135 | WRITE_PROC_FIRST_READ_GP
1136 | WRITE_PROC_SECOND_READ_GP
1137 | WRITE_PROC_FIRST_WRITE_GP
1138 | WRITE_PROC_SECOND_WRITE_GP
1139 | WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR
1140 | WRITE_PROC_FIRST_MB,
1141 WRITE_PROC_SECOND_MB) ->
1144 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_MB);
1146 :: CONSUME_TOKENS(proc_urcu_writer,
1148 | WRITE_PROC_FIRST_WAIT
1149 | WRITE_PROC_SECOND_WAIT
1150 | WRITE_PROC_WMB /* No dependency on
1151 * WRITE_DATA because we
1153 * different location. */
1154 | WRITE_PROC_SECOND_MB
1155 | WRITE_PROC_FIRST_MB,
1157 WRITE_CACHED_VAR(rcu_data[old_data], POISON);
1158 PRODUCE_TOKENS(proc_urcu_writer, WRITE_FREE);
1160 :: CONSUME_TOKENS(proc_urcu_writer, WRITE_PROC_ALL_TOKENS, 0) ->
1161 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_ALL_TOKENS_CLEAR);
1167 * Note : Promela model adds implicit serialization of the
1168 * WRITE_FREE instruction. Normally, it would be permitted to
1169 * spill on the next loop execution. Given the validation we do
1170 * checks for the data entry read to be poisoned, it's ok if
1171 * we do not check "late arriving" memory poisoning.
1176 * Given the reader loops infinitely, let the writer also busy-loop
1177 * with progress here so, with weak fairness, we can test the
1178 * writer's progress.
1183 #ifdef WRITER_PROGRESS
1186 #ifdef READER_PROGRESS
1188 * Make sure we don't block the reader's progress.
1190 smp_mb_send(i, j, 5);
1195 /* Non-atomic parts of the loop */
1198 smp_mb_send(i, j, 1);
1199 goto smp_mb_send1_end;
1200 #ifndef GEN_ERROR_WRITER_PROGRESS
1202 smp_mb_send(i, j, 2);
1203 goto smp_mb_send2_end;
1205 smp_mb_send(i, j, 3);
1206 goto smp_mb_send3_end;
1209 smp_mb_send(i, j, 4);
1210 goto smp_mb_send4_end;
1215 /* no name clash please */
1216 #undef proc_urcu_writer
1219 /* Leave after the readers and writers so the pid count is ok. */
1224 INIT_CACHED_VAR(urcu_gp_ctr, 1, j);
1225 INIT_CACHED_VAR(rcu_ptr, 0, j);
1229 :: i < NR_READERS ->
1230 INIT_CACHED_VAR(urcu_active_readers[i], 0, j);
1231 ptr_read_first[i] = 1;
1232 ptr_read_second[i] = 1;
1233 data_read_first[i] = WINE;
1234 data_read_second[i] = WINE;
1236 :: i >= NR_READERS -> break
1238 INIT_CACHED_VAR(rcu_data[0], WINE, j);
1242 INIT_CACHED_VAR(rcu_data[i], POISON, j);
1244 :: i >= SLAB_SIZE -> break