1 #define WRITER_PROGRESS
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 reader_barrier[get_readerid()] = 0;
305 /* We choose to ignore writer's non-progress caused from the
306 * reader ignoring the writer's mb() requests */
307 #ifdef WRITER_PROGRESS
308 progress_writer_from_reader:
314 #ifdef WRITER_PROGRESS
315 //#define PROGRESS_LABEL(progressid) progress_writer_progid_##progressid:
316 #define PROGRESS_LABEL(progressid)
318 #define PROGRESS_LABEL(progressid)
321 #define smp_mb_send(i, j, progressid) \
326 :: i < NR_READERS -> \
327 reader_barrier[i] = 1; \
329 * Busy-looping waiting for reader barrier handling is of little\
330 * interest, given the reader has the ability to totally ignore \
331 * barrier requests. \
333 PROGRESS_LABEL(progressid) \
335 :: (reader_barrier[i] == 1) -> skip; \
336 :: (reader_barrier[i] == 0) -> break; \
339 :: i >= NR_READERS -> \
347 #define smp_mb_send(i, j, progressid) smp_mb(i, j)
348 #define smp_mb_reader smp_mb
349 #define smp_mb_recv(i, j)
353 /* Keep in sync manually with smp_rmb, smp_wmb, ooo_mem and init() */
354 DECLARE_CACHED_VAR(byte, urcu_gp_ctr);
355 /* Note ! currently only one reader */
356 DECLARE_CACHED_VAR(byte, urcu_active_readers[NR_READERS]);
358 DECLARE_CACHED_VAR(bit, rcu_data[SLAB_SIZE]);
362 DECLARE_CACHED_VAR(bit, rcu_ptr);
363 bit ptr_read_first[NR_READERS];
364 bit ptr_read_second[NR_READERS];
366 DECLARE_CACHED_VAR(byte, rcu_ptr);
367 byte ptr_read_first[NR_READERS];
368 byte ptr_read_second[NR_READERS];
371 bit data_read_first[NR_READERS];
372 bit data_read_second[NR_READERS];
376 inline wait_init_done()
379 :: init_done == 0 -> skip;
387 RANDOM_CACHE_WRITE_TO_MEM(urcu_gp_ctr, get_pid());
391 RANDOM_CACHE_WRITE_TO_MEM(urcu_active_readers[i],
394 :: i >= NR_READERS -> break
396 RANDOM_CACHE_WRITE_TO_MEM(rcu_ptr, get_pid());
400 RANDOM_CACHE_WRITE_TO_MEM(rcu_data[i], get_pid());
402 :: i >= SLAB_SIZE -> break
404 RANDOM_CACHE_READ_FROM_MEM(urcu_gp_ctr, get_pid());
408 RANDOM_CACHE_READ_FROM_MEM(urcu_active_readers[i],
411 :: i >= NR_READERS -> break
413 RANDOM_CACHE_READ_FROM_MEM(rcu_ptr, get_pid());
417 RANDOM_CACHE_READ_FROM_MEM(rcu_data[i], get_pid());
419 :: i >= SLAB_SIZE -> break
425 * Bit encoding, urcu_reader :
428 int _proc_urcu_reader;
429 #define proc_urcu_reader _proc_urcu_reader
431 /* Body of PROCEDURE_READ_LOCK */
432 #define READ_PROD_A_READ (1 << 0)
433 #define READ_PROD_B_IF_TRUE (1 << 1)
434 #define READ_PROD_B_IF_FALSE (1 << 2)
435 #define READ_PROD_C_IF_TRUE_READ (1 << 3)
437 #define PROCEDURE_READ_LOCK(base, consumetoken, producetoken) \
438 :: CONSUME_TOKENS(proc_urcu_reader, consumetoken, READ_PROD_A_READ << base) -> \
440 tmp = READ_CACHED_VAR(urcu_active_readers[get_readerid()]); \
441 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_A_READ << base); \
442 :: CONSUME_TOKENS(proc_urcu_reader, \
443 READ_PROD_A_READ << base, /* RAW, pre-dominant */ \
444 (READ_PROD_B_IF_TRUE | READ_PROD_B_IF_FALSE) << base) -> \
446 :: (!(tmp & RCU_GP_CTR_NEST_MASK)) -> \
447 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_B_IF_TRUE << base); \
449 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_B_IF_FALSE << base); \
452 :: CONSUME_TOKENS(proc_urcu_reader, READ_PROD_B_IF_TRUE << base, \
453 READ_PROD_C_IF_TRUE_READ << base) -> \
455 tmp2 = READ_CACHED_VAR(urcu_gp_ctr); \
456 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_C_IF_TRUE_READ << base); \
457 :: CONSUME_TOKENS(proc_urcu_reader, \
458 (READ_PROD_C_IF_TRUE_READ /* pre-dominant */ \
459 | READ_PROD_A_READ) << base, /* WAR */ \
462 WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp2); \
463 PRODUCE_TOKENS(proc_urcu_reader, producetoken); \
464 /* IF_MERGE implies \
465 * post-dominance */ \
467 :: CONSUME_TOKENS(proc_urcu_reader, \
468 (READ_PROD_B_IF_FALSE /* pre-dominant */ \
469 | READ_PROD_A_READ) << base, /* WAR */ \
472 WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], \
474 PRODUCE_TOKENS(proc_urcu_reader, producetoken); \
475 /* IF_MERGE implies \
476 * post-dominance */ \
480 /* Body of PROCEDURE_READ_LOCK */
481 #define READ_PROC_READ_UNLOCK (1 << 0)
483 #define PROCEDURE_READ_UNLOCK(base, consumetoken, producetoken) \
484 :: CONSUME_TOKENS(proc_urcu_reader, \
486 READ_PROC_READ_UNLOCK << base) -> \
488 tmp2 = READ_CACHED_VAR(urcu_active_readers[get_readerid()]); \
489 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_READ_UNLOCK << base); \
490 :: CONSUME_TOKENS(proc_urcu_reader, \
492 | (READ_PROC_READ_UNLOCK << base), /* WAR */ \
495 WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp2 - 1); \
496 PRODUCE_TOKENS(proc_urcu_reader, producetoken); \
500 #define READ_PROD_NONE (1 << 0)
502 /* PROCEDURE_READ_LOCK base = << 1 : 1 to 5 */
503 #define READ_LOCK_BASE 1
504 #define READ_LOCK_OUT (1 << 5)
506 #define READ_PROC_FIRST_MB (1 << 6)
508 /* PROCEDURE_READ_LOCK (NESTED) base : << 7 : 7 to 11 */
509 #define READ_LOCK_NESTED_BASE 7
510 #define READ_LOCK_NESTED_OUT (1 << 11)
512 #define READ_PROC_READ_GEN (1 << 12)
513 #define READ_PROC_ACCESS_GEN (1 << 13)
515 /* PROCEDURE_READ_UNLOCK (NESTED) base = << 14 : 14 to 15 */
516 #define READ_UNLOCK_NESTED_BASE 14
517 #define READ_UNLOCK_NESTED_OUT (1 << 15)
519 #define READ_PROC_SECOND_MB (1 << 16)
521 /* PROCEDURE_READ_UNLOCK base = << 17 : 17 to 18 */
522 #define READ_UNLOCK_BASE 17
523 #define READ_UNLOCK_OUT (1 << 18)
525 /* PROCEDURE_READ_LOCK_UNROLL base = << 19 : 19 to 23 */
526 #define READ_LOCK_UNROLL_BASE 19
527 #define READ_LOCK_OUT_UNROLL (1 << 23)
529 #define READ_PROC_THIRD_MB (1 << 24)
531 #define READ_PROC_READ_GEN_UNROLL (1 << 25)
532 #define READ_PROC_ACCESS_GEN_UNROLL (1 << 26)
534 #define READ_PROC_FOURTH_MB (1 << 27)
536 /* PROCEDURE_READ_UNLOCK_UNROLL base = << 28 : 28 to 29 */
537 #define READ_UNLOCK_UNROLL_BASE 28
538 #define READ_UNLOCK_OUT_UNROLL (1 << 29)
541 /* Should not include branches */
542 #define READ_PROC_ALL_TOKENS (READ_PROD_NONE \
544 | READ_PROC_FIRST_MB \
545 | READ_LOCK_NESTED_OUT \
546 | READ_PROC_READ_GEN \
547 | READ_PROC_ACCESS_GEN \
548 | READ_UNLOCK_NESTED_OUT \
549 | READ_PROC_SECOND_MB \
551 | READ_LOCK_OUT_UNROLL \
552 | READ_PROC_THIRD_MB \
553 | READ_PROC_READ_GEN_UNROLL \
554 | READ_PROC_ACCESS_GEN_UNROLL \
555 | READ_PROC_FOURTH_MB \
556 | READ_UNLOCK_OUT_UNROLL)
558 /* Must clear all tokens, including branches */
559 #define READ_PROC_ALL_TOKENS_CLEAR ((1 << 30) - 1)
561 inline urcu_one_read(i, j, nest_i, tmp, tmp2)
563 PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_NONE);
566 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FIRST_MB);
567 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_SECOND_MB);
568 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_THIRD_MB);
569 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FOURTH_MB);
572 #ifdef REMOTE_BARRIERS
573 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FIRST_MB);
574 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_SECOND_MB);
575 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_THIRD_MB);
576 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FOURTH_MB);
582 #ifdef REMOTE_BARRIERS
584 * Signal-based memory barrier will only execute when the
585 * execution order appears in program order.
591 :: CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE,
592 READ_LOCK_OUT | READ_LOCK_NESTED_OUT
593 | READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
595 | READ_LOCK_OUT_UNROLL
596 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
597 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT,
599 | READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
601 | READ_LOCK_OUT_UNROLL
602 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
603 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT | READ_LOCK_NESTED_OUT,
604 READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
606 | READ_LOCK_OUT_UNROLL
607 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
608 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
609 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN,
610 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
615 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN,
616 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
621 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN
622 | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT,
624 | READ_LOCK_OUT_UNROLL
625 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
626 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
627 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN
628 | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
631 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL)
632 || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT
633 | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN
634 | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
635 | READ_UNLOCK_OUT | 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
640 | READ_UNLOCK_OUT | READ_LOCK_OUT_UNROLL
641 | READ_PROC_READ_GEN_UNROLL,
642 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
646 | READ_UNLOCK_OUT | READ_LOCK_OUT_UNROLL
647 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL,
648 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 | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT
651 | READ_UNLOCK_OUT | READ_LOCK_OUT_UNROLL
652 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL,
662 goto non_atomic3_skip;
665 goto non_atomic3_end;
668 #endif /* REMOTE_BARRIERS */
672 PROCEDURE_READ_LOCK(READ_LOCK_BASE, READ_PROD_NONE, READ_LOCK_OUT);
674 :: CONSUME_TOKENS(proc_urcu_reader,
675 READ_LOCK_OUT, /* post-dominant */
676 READ_PROC_FIRST_MB) ->
678 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FIRST_MB);
680 PROCEDURE_READ_LOCK(READ_LOCK_NESTED_BASE, READ_PROC_FIRST_MB | READ_LOCK_OUT,
681 READ_LOCK_NESTED_OUT);
683 :: CONSUME_TOKENS(proc_urcu_reader,
684 READ_PROC_FIRST_MB, /* mb() orders reads */
685 READ_PROC_READ_GEN) ->
687 ptr_read_first[get_readerid()] = READ_CACHED_VAR(rcu_ptr);
688 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_READ_GEN);
690 :: CONSUME_TOKENS(proc_urcu_reader,
691 READ_PROC_FIRST_MB /* mb() orders reads */
692 | READ_PROC_READ_GEN,
693 READ_PROC_ACCESS_GEN) ->
694 /* smp_read_barrier_depends */
697 data_read_first[get_readerid()] =
698 READ_CACHED_VAR(rcu_data[ptr_read_first[get_readerid()]]);
699 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_ACCESS_GEN);
702 /* Note : we remove the nested memory barrier from the read unlock
703 * model, given it is not usually needed. The implementation has the barrier
704 * because the performance impact added by a branch in the common case does not
708 PROCEDURE_READ_UNLOCK(READ_UNLOCK_NESTED_BASE,
711 | READ_LOCK_NESTED_OUT,
712 READ_UNLOCK_NESTED_OUT);
715 :: CONSUME_TOKENS(proc_urcu_reader,
716 READ_PROC_ACCESS_GEN /* mb() orders reads */
717 | READ_PROC_READ_GEN /* mb() orders reads */
718 | READ_PROC_FIRST_MB /* mb() ordered */
719 | READ_LOCK_OUT /* post-dominant */
720 | READ_LOCK_NESTED_OUT /* post-dominant */
721 | READ_UNLOCK_NESTED_OUT,
722 READ_PROC_SECOND_MB) ->
724 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_SECOND_MB);
726 PROCEDURE_READ_UNLOCK(READ_UNLOCK_BASE,
727 READ_PROC_SECOND_MB /* mb() orders reads */
728 | READ_PROC_FIRST_MB /* mb() orders reads */
729 | READ_LOCK_NESTED_OUT /* RAW */
730 | READ_LOCK_OUT /* RAW */
731 | READ_UNLOCK_NESTED_OUT, /* RAW */
734 /* Unrolling loop : second consecutive lock */
735 /* reading urcu_active_readers, which have been written by
736 * READ_UNLOCK_OUT : RAW */
737 PROCEDURE_READ_LOCK(READ_LOCK_UNROLL_BASE,
738 READ_UNLOCK_OUT /* RAW */
739 | READ_PROC_SECOND_MB /* mb() orders reads */
740 | READ_PROC_FIRST_MB /* mb() orders reads */
741 | READ_LOCK_NESTED_OUT /* RAW */
742 | READ_LOCK_OUT /* RAW */
743 | READ_UNLOCK_NESTED_OUT, /* RAW */
744 READ_LOCK_OUT_UNROLL);
747 :: CONSUME_TOKENS(proc_urcu_reader,
748 READ_PROC_FIRST_MB /* mb() ordered */
749 | READ_PROC_SECOND_MB /* mb() ordered */
750 | READ_LOCK_OUT_UNROLL /* post-dominant */
751 | READ_LOCK_NESTED_OUT
753 | READ_UNLOCK_NESTED_OUT
755 READ_PROC_THIRD_MB) ->
757 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_THIRD_MB);
759 :: CONSUME_TOKENS(proc_urcu_reader,
760 READ_PROC_FIRST_MB /* mb() orders reads */
761 | READ_PROC_SECOND_MB /* mb() orders reads */
762 | READ_PROC_THIRD_MB, /* mb() orders reads */
763 READ_PROC_READ_GEN_UNROLL) ->
765 ptr_read_second[get_readerid()] = READ_CACHED_VAR(rcu_ptr);
766 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_READ_GEN_UNROLL);
768 :: CONSUME_TOKENS(proc_urcu_reader,
769 READ_PROC_READ_GEN_UNROLL
770 | READ_PROC_FIRST_MB /* mb() orders reads */
771 | READ_PROC_SECOND_MB /* mb() orders reads */
772 | READ_PROC_THIRD_MB, /* mb() orders reads */
773 READ_PROC_ACCESS_GEN_UNROLL) ->
774 /* smp_read_barrier_depends */
777 data_read_second[get_readerid()] =
778 READ_CACHED_VAR(rcu_data[ptr_read_second[get_readerid()]]);
779 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_ACCESS_GEN_UNROLL);
781 :: CONSUME_TOKENS(proc_urcu_reader,
782 READ_PROC_READ_GEN_UNROLL /* mb() orders reads */
783 | READ_PROC_ACCESS_GEN_UNROLL /* mb() orders reads */
784 | READ_PROC_FIRST_MB /* mb() ordered */
785 | READ_PROC_SECOND_MB /* mb() ordered */
786 | READ_PROC_THIRD_MB /* mb() ordered */
787 | READ_LOCK_OUT_UNROLL /* post-dominant */
788 | READ_LOCK_NESTED_OUT
790 | READ_UNLOCK_NESTED_OUT
792 READ_PROC_FOURTH_MB) ->
794 PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FOURTH_MB);
796 PROCEDURE_READ_UNLOCK(READ_UNLOCK_UNROLL_BASE,
797 READ_PROC_FOURTH_MB /* mb() orders reads */
798 | READ_PROC_THIRD_MB /* mb() orders reads */
799 | READ_LOCK_OUT_UNROLL /* RAW */
800 | READ_PROC_SECOND_MB /* mb() orders reads */
801 | READ_PROC_FIRST_MB /* mb() orders reads */
802 | READ_LOCK_NESTED_OUT /* RAW */
803 | READ_LOCK_OUT /* RAW */
804 | READ_UNLOCK_NESTED_OUT, /* RAW */
805 READ_UNLOCK_OUT_UNROLL);
806 :: CONSUME_TOKENS(proc_urcu_reader, READ_PROC_ALL_TOKENS, 0) ->
807 CLEAR_TOKENS(proc_urcu_reader, READ_PROC_ALL_TOKENS_CLEAR);
813 * Dependency between consecutive loops :
815 * WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp2 - 1)
816 * tmp = READ_CACHED_VAR(urcu_active_readers[get_readerid()]);
818 * _WHEN THE MB()s are in place_, they add full ordering of the
819 * generation pointer read wrt active reader count read, which ensures
820 * execution will not spill across loop execution.
821 * However, in the event mb()s are removed (execution using signal
822 * handler to promote barrier()() -> smp_mb()), nothing prevents one loop
823 * to spill its execution on other loop's execution.
846 active proctype urcu_reader()
853 assert(get_pid() < NR_PROCS);
859 * We do not test reader's progress here, because we are mainly
860 * interested in writer's progress. The reader never blocks
861 * anyway. We have to test for reader/writer's progress
862 * separately, otherwise we could think the writer is doing
863 * progress when it's blocked by an always progressing reader.
865 #ifdef READER_PROGRESS
868 urcu_one_read(i, j, nest_i, tmp, tmp2);
872 /* no name clash please */
873 #undef proc_urcu_reader
876 /* Model the RCU update process. */
879 * Bit encoding, urcu_writer :
880 * Currently only supports one reader.
883 int _proc_urcu_writer;
884 #define proc_urcu_writer _proc_urcu_writer
886 #define WRITE_PROD_NONE (1 << 0)
888 #define WRITE_DATA (1 << 1)
889 #define WRITE_PROC_WMB (1 << 2)
890 #define WRITE_XCHG_PTR (1 << 3)
892 #define WRITE_PROC_FIRST_MB (1 << 4)
895 #define WRITE_PROC_FIRST_READ_GP (1 << 5)
896 #define WRITE_PROC_FIRST_WRITE_GP (1 << 6)
897 #define WRITE_PROC_FIRST_WAIT (1 << 7)
898 #define WRITE_PROC_FIRST_WAIT_LOOP (1 << 8)
901 #define WRITE_PROC_SECOND_READ_GP (1 << 9)
902 #define WRITE_PROC_SECOND_WRITE_GP (1 << 10)
903 #define WRITE_PROC_SECOND_WAIT (1 << 11)
904 #define WRITE_PROC_SECOND_WAIT_LOOP (1 << 12)
906 #define WRITE_PROC_SECOND_MB (1 << 13)
908 #define WRITE_FREE (1 << 14)
910 #define WRITE_PROC_ALL_TOKENS (WRITE_PROD_NONE \
914 | WRITE_PROC_FIRST_MB \
915 | WRITE_PROC_FIRST_READ_GP \
916 | WRITE_PROC_FIRST_WRITE_GP \
917 | WRITE_PROC_FIRST_WAIT \
918 | WRITE_PROC_SECOND_READ_GP \
919 | WRITE_PROC_SECOND_WRITE_GP \
920 | WRITE_PROC_SECOND_WAIT \
921 | WRITE_PROC_SECOND_MB \
924 #define WRITE_PROC_ALL_TOKENS_CLEAR ((1 << 15) - 1)
927 * Mutexes are implied around writer execution. A single writer at a time.
929 active proctype urcu_writer()
932 byte tmp, tmp2, tmpa;
933 byte cur_data = 0, old_data, loop_nr = 0;
934 byte cur_gp_val = 0; /*
935 * Keep a local trace of the current parity so
936 * we don't add non-existing dependencies on the global
937 * GP update. Needed to test single flip case.
942 assert(get_pid() < NR_PROCS);
946 #ifdef WRITER_PROGRESS
949 loop_nr = loop_nr + 1;
951 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROD_NONE);
954 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_WMB);
958 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_MB);
959 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_MB);
963 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_READ_GP);
964 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WRITE_GP);
965 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT);
966 /* For single flip, we need to know the current parity */
967 cur_gp_val = cur_gp_val ^ RCU_GP_CTR_BIT;
974 :: CONSUME_TOKENS(proc_urcu_writer,
978 cur_data = (cur_data + 1) % SLAB_SIZE;
979 WRITE_CACHED_VAR(rcu_data[cur_data], WINE);
980 PRODUCE_TOKENS(proc_urcu_writer, WRITE_DATA);
983 :: CONSUME_TOKENS(proc_urcu_writer,
987 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_WMB);
989 :: CONSUME_TOKENS(proc_urcu_writer,
992 /* rcu_xchg_pointer() */
994 old_data = READ_CACHED_VAR(rcu_ptr);
995 WRITE_CACHED_VAR(rcu_ptr, cur_data);
997 PRODUCE_TOKENS(proc_urcu_writer, WRITE_XCHG_PTR);
999 :: CONSUME_TOKENS(proc_urcu_writer,
1000 WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR,
1001 WRITE_PROC_FIRST_MB) ->
1004 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_MB);
1007 :: CONSUME_TOKENS(proc_urcu_writer,
1008 WRITE_PROC_FIRST_MB,
1009 WRITE_PROC_FIRST_READ_GP) ->
1010 tmpa = READ_CACHED_VAR(urcu_gp_ctr);
1011 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_READ_GP);
1012 :: CONSUME_TOKENS(proc_urcu_writer,
1013 WRITE_PROC_FIRST_MB | WRITE_PROC_WMB
1014 | WRITE_PROC_FIRST_READ_GP,
1015 WRITE_PROC_FIRST_WRITE_GP) ->
1017 WRITE_CACHED_VAR(urcu_gp_ctr, tmpa ^ RCU_GP_CTR_BIT);
1018 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WRITE_GP);
1020 :: CONSUME_TOKENS(proc_urcu_writer,
1021 //WRITE_PROC_FIRST_WRITE_GP /* TEST ADDING SYNC CORE */
1022 WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
1023 WRITE_PROC_FIRST_WAIT | WRITE_PROC_FIRST_WAIT_LOOP) ->
1025 /* ONLY WAITING FOR READER 0 */
1026 tmp2 = READ_CACHED_VAR(urcu_active_readers[0]);
1028 /* In normal execution, we are always starting by
1029 * waiting for the even parity.
1031 cur_gp_val = RCU_GP_CTR_BIT;
1034 :: (tmp2 & RCU_GP_CTR_NEST_MASK)
1035 && ((tmp2 ^ cur_gp_val) & RCU_GP_CTR_BIT) ->
1036 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WAIT_LOOP);
1038 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WAIT);
1041 :: CONSUME_TOKENS(proc_urcu_writer,
1042 //WRITE_PROC_FIRST_WRITE_GP /* TEST ADDING SYNC CORE */
1043 WRITE_PROC_FIRST_WRITE_GP
1044 | WRITE_PROC_FIRST_READ_GP
1045 | WRITE_PROC_FIRST_WAIT_LOOP
1046 | WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR
1047 | WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
1049 #ifndef GEN_ERROR_WRITER_PROGRESS
1055 /* This instruction loops to WRITE_PROC_FIRST_WAIT */
1056 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WAIT_LOOP | WRITE_PROC_FIRST_WAIT);
1059 :: CONSUME_TOKENS(proc_urcu_writer,
1060 WRITE_PROC_FIRST_WAIT /* Control dependency : need to branch out of
1061 * the loop to execute the next flip (CHECK) */
1062 | WRITE_PROC_FIRST_WRITE_GP
1063 | WRITE_PROC_FIRST_READ_GP
1064 | WRITE_PROC_FIRST_MB,
1065 WRITE_PROC_SECOND_READ_GP) ->
1067 tmpa = READ_CACHED_VAR(urcu_gp_ctr);
1068 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_READ_GP);
1069 :: CONSUME_TOKENS(proc_urcu_writer,
1072 | WRITE_PROC_FIRST_READ_GP
1073 | WRITE_PROC_FIRST_WRITE_GP
1074 | WRITE_PROC_SECOND_READ_GP,
1075 WRITE_PROC_SECOND_WRITE_GP) ->
1077 WRITE_CACHED_VAR(urcu_gp_ctr, tmpa ^ RCU_GP_CTR_BIT);
1078 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WRITE_GP);
1080 :: CONSUME_TOKENS(proc_urcu_writer,
1081 //WRITE_PROC_FIRST_WRITE_GP /* TEST ADDING SYNC CORE */
1082 WRITE_PROC_FIRST_WAIT
1083 | WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
1084 WRITE_PROC_SECOND_WAIT | WRITE_PROC_SECOND_WAIT_LOOP) ->
1086 /* ONLY WAITING FOR READER 0 */
1087 tmp2 = READ_CACHED_VAR(urcu_active_readers[0]);
1089 :: (tmp2 & RCU_GP_CTR_NEST_MASK)
1090 && ((tmp2 ^ 0) & RCU_GP_CTR_BIT) ->
1091 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT_LOOP);
1093 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT);
1096 :: CONSUME_TOKENS(proc_urcu_writer,
1097 //WRITE_PROC_FIRST_WRITE_GP /* TEST ADDING SYNC CORE */
1098 WRITE_PROC_SECOND_WRITE_GP
1099 | WRITE_PROC_FIRST_WRITE_GP
1100 | WRITE_PROC_SECOND_READ_GP
1101 | WRITE_PROC_FIRST_READ_GP
1102 | WRITE_PROC_SECOND_WAIT_LOOP
1103 | WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR
1104 | WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */
1106 #ifndef GEN_ERROR_WRITER_PROGRESS
1112 /* This instruction loops to WRITE_PROC_SECOND_WAIT */
1113 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT_LOOP | WRITE_PROC_SECOND_WAIT);
1116 :: CONSUME_TOKENS(proc_urcu_writer,
1117 WRITE_PROC_FIRST_WAIT
1118 | WRITE_PROC_SECOND_WAIT
1119 | WRITE_PROC_FIRST_READ_GP
1120 | WRITE_PROC_SECOND_READ_GP
1121 | WRITE_PROC_FIRST_WRITE_GP
1122 | WRITE_PROC_SECOND_WRITE_GP
1123 | WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR
1124 | WRITE_PROC_FIRST_MB,
1125 WRITE_PROC_SECOND_MB) ->
1128 PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_MB);
1130 :: CONSUME_TOKENS(proc_urcu_writer,
1132 | WRITE_PROC_FIRST_WAIT
1133 | WRITE_PROC_SECOND_WAIT
1134 | WRITE_PROC_WMB /* No dependency on
1135 * WRITE_DATA because we
1137 * different location. */
1138 | WRITE_PROC_SECOND_MB
1139 | WRITE_PROC_FIRST_MB,
1141 WRITE_CACHED_VAR(rcu_data[old_data], POISON);
1142 PRODUCE_TOKENS(proc_urcu_writer, WRITE_FREE);
1144 :: CONSUME_TOKENS(proc_urcu_writer, WRITE_PROC_ALL_TOKENS, 0) ->
1145 CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_ALL_TOKENS_CLEAR);
1151 * Note : Promela model adds implicit serialization of the
1152 * WRITE_FREE instruction. Normally, it would be permitted to
1153 * spill on the next loop execution. Given the validation we do
1154 * checks for the data entry read to be poisoned, it's ok if
1155 * we do not check "late arriving" memory poisoning.
1160 * Given the reader loops infinitely, let the writer also busy-loop
1161 * with progress here so, with weak fairness, we can test the
1162 * writer's progress.
1167 #ifdef WRITER_PROGRESS
1173 /* Non-atomic parts of the loop */
1176 smp_mb_send(i, j, 1);
1177 goto smp_mb_send1_end;
1178 #ifndef GEN_ERROR_WRITER_PROGRESS
1180 smp_mb_send(i, j, 2);
1181 goto smp_mb_send2_end;
1183 smp_mb_send(i, j, 3);
1184 goto smp_mb_send3_end;
1187 smp_mb_send(i, j, 4);
1188 goto smp_mb_send4_end;
1193 /* no name clash please */
1194 #undef proc_urcu_writer
1197 /* Leave after the readers and writers so the pid count is ok. */
1202 INIT_CACHED_VAR(urcu_gp_ctr, 1, j);
1203 INIT_CACHED_VAR(rcu_ptr, 0, j);
1207 :: i < NR_READERS ->
1208 INIT_CACHED_VAR(urcu_active_readers[i], 0, j);
1209 ptr_read_first[i] = 1;
1210 ptr_read_second[i] = 1;
1211 data_read_first[i] = WINE;
1212 data_read_second[i] = WINE;
1214 :: i >= NR_READERS -> break
1216 INIT_CACHED_VAR(rcu_data[0], WINE, j);
1220 INIT_CACHED_VAR(rcu_data[i], POISON, j);
1222 :: i >= SLAB_SIZE -> break