11 #define read_free_race (read_generation[0] == last_free_gen)
12 #define read_free (free_done && data_access[0])
14 #elif (NR_READERS == 2)
16 #define read_free_race (read_generation[0] == last_free_gen || read_generation[1] == last_free_gen)
17 #define read_free (free_done && (data_access[0] || data_access[1]))
21 #error "Too many readers"
25 #define RCU_GP_CTR_BIT (1 << 7)
26 #define RCU_GP_CTR_NEST_MASK (RCU_GP_CTR_BIT - 1)
28 #ifndef READER_NEST_LEVEL
29 #define READER_NEST_LEVEL 2
32 #define REMOTE_BARRIERS
34 * mem.spin: Promela code to validate memory barriers with OOO memory.
36 * This program is free software; you can redistribute it and/or modify
37 * it under the terms of the GNU General Public License as published by
38 * the Free Software Foundation; either version 2 of the License, or
39 * (at your option) any later version.
41 * This program is distributed in the hope that it will be useful,
42 * but WITHOUT ANY WARRANTY; without even the implied warranty of
43 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
44 * GNU General Public License for more details.
46 * You should have received a copy of the GNU General Public License
47 * along with this program; if not, write to the Free Software
48 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
50 * Copyright (c) 2009 Mathieu Desnoyers
53 /* Promela validation variables. */
55 /* specific defines "included" here */
56 /* DEFINES file "included" here */
58 #define get_pid() (_pid)
61 * Each process have its own data in cache. Caches are randomly updated.
62 * smp_wmb and smp_rmb forces cache updates (write and read), smp_mb forces
66 typedef per_proc_byte {
70 /* Bitfield has a maximum of 8 procs */
71 typedef per_proc_bit {
75 #define DECLARE_CACHED_VAR(type, x) \
77 per_proc_##type cached_##x; \
78 per_proc_bit cache_dirty_##x;
80 #define INIT_CACHED_VAR(x, v, j) \
82 cache_dirty_##x.bitfield = 0; \
86 cached_##x.val[j] = v; \
88 :: j >= NR_PROCS -> break \
91 #define IS_CACHE_DIRTY(x, id) (cache_dirty_##x.bitfield & (1 << id))
93 #define READ_CACHED_VAR(x) (cached_##x.val[get_pid()])
95 #define WRITE_CACHED_VAR(x, v) \
97 cached_##x.val[get_pid()] = v; \
98 cache_dirty_##x.bitfield = \
99 cache_dirty_##x.bitfield | (1 << get_pid()); \
102 #define CACHE_WRITE_TO_MEM(x, id) \
104 :: IS_CACHE_DIRTY(x, id) -> \
105 mem_##x = cached_##x.val[id]; \
106 cache_dirty_##x.bitfield = \
107 cache_dirty_##x.bitfield & (~(1 << id)); \
112 #define CACHE_READ_FROM_MEM(x, id) \
114 :: !IS_CACHE_DIRTY(x, id) -> \
115 cached_##x.val[id] = mem_##x;\
121 * May update other caches if cache is dirty, or not.
123 #define RANDOM_CACHE_WRITE_TO_MEM(x, id)\
125 :: 1 -> CACHE_WRITE_TO_MEM(x, id); \
129 #define RANDOM_CACHE_READ_FROM_MEM(x, id)\
131 :: 1 -> CACHE_READ_FROM_MEM(x, id); \
136 * Remote barriers tests the scheme where a signal (or IPI) is sent to all
137 * reader threads to promote their compiler barrier to a smp_mb().
139 #ifdef REMOTE_BARRIERS
141 inline smp_rmb_pid(i, j)
144 CACHE_READ_FROM_MEM(urcu_gp_ctr, i);
148 CACHE_READ_FROM_MEM(urcu_active_readers[j], i);
150 :: j >= NR_READERS -> break
152 CACHE_READ_FROM_MEM(generation_ptr, i);
156 inline smp_wmb_pid(i, j)
159 CACHE_WRITE_TO_MEM(urcu_gp_ctr, i);
163 CACHE_WRITE_TO_MEM(urcu_active_readers[j], i);
165 :: j >= NR_READERS -> break
167 CACHE_WRITE_TO_MEM(generation_ptr, i);
171 inline smp_mb_pid(i, j)
189 * Readers do a simple barrier(), writers are doing a smp_mb() _and_ sending a
190 * signal or IPI to have all readers execute a smp_mb.
191 * We are not modeling the whole rendez-vous between readers and writers here,
192 * we just let the writer update each reader's caches remotely.
197 :: get_pid() >= NR_READERS ->
198 smp_mb_pid(get_pid(), j);
204 :: i >= NR_READERS -> break
206 smp_mb_pid(get_pid(), j);
216 CACHE_READ_FROM_MEM(urcu_gp_ctr, get_pid());
220 CACHE_READ_FROM_MEM(urcu_active_readers[i], get_pid());
222 :: i >= NR_READERS -> break
224 CACHE_READ_FROM_MEM(generation_ptr, get_pid());
231 CACHE_WRITE_TO_MEM(urcu_gp_ctr, get_pid());
235 CACHE_WRITE_TO_MEM(urcu_active_readers[i], get_pid());
237 :: i >= NR_READERS -> break
239 CACHE_WRITE_TO_MEM(generation_ptr, get_pid());
262 /* Keep in sync manually with smp_rmb, wmp_wmb, ooo_mem and init() */
263 DECLARE_CACHED_VAR(byte, urcu_gp_ctr);
264 /* Note ! currently only two readers */
265 DECLARE_CACHED_VAR(byte, urcu_active_readers[NR_READERS]);
266 /* pointer generation */
267 DECLARE_CACHED_VAR(byte, generation_ptr);
269 byte last_free_gen = 0;
271 byte read_generation[NR_READERS];
272 bit data_access[NR_READERS];
278 inline wait_init_done()
281 :: init_done == 0 -> skip;
289 RANDOM_CACHE_WRITE_TO_MEM(urcu_gp_ctr, get_pid());
293 RANDOM_CACHE_WRITE_TO_MEM(urcu_active_readers[i],
296 :: i >= NR_READERS -> break
298 RANDOM_CACHE_WRITE_TO_MEM(generation_ptr, get_pid());
299 RANDOM_CACHE_READ_FROM_MEM(urcu_gp_ctr, get_pid());
303 RANDOM_CACHE_READ_FROM_MEM(urcu_active_readers[i],
306 :: i >= NR_READERS -> break
308 RANDOM_CACHE_READ_FROM_MEM(generation_ptr, get_pid());
312 #define get_readerid() (get_pid())
313 #define get_writerid() (get_readerid() + NR_READERS)
315 inline wait_for_reader(tmp, tmp2, i, j)
319 tmp2 = READ_CACHED_VAR(urcu_active_readers[tmp]);
322 :: (tmp2 & RCU_GP_CTR_NEST_MASK)
323 && ((tmp2 ^ READ_CACHED_VAR(urcu_gp_ctr))
325 #ifndef GEN_ERROR_WRITER_PROGRESS
336 inline wait_for_quiescent_state(tmp, tmp2, i, j)
340 :: tmp < NR_READERS ->
341 wait_for_reader(tmp, tmp2, i, j);
343 :: (NR_READERS > 1) && (tmp < NR_READERS - 1)
349 :: tmp >= NR_READERS -> break
353 /* Model the RCU read-side critical section. */
355 inline urcu_one_read(i, j, nest_i, tmp, tmp2)
359 :: nest_i < READER_NEST_LEVEL ->
361 tmp = READ_CACHED_VAR(urcu_active_readers[get_readerid()]);
364 :: (!(tmp & RCU_GP_CTR_NEST_MASK))
366 tmp2 = READ_CACHED_VAR(urcu_gp_ctr);
368 WRITE_CACHED_VAR(urcu_active_readers[get_readerid()],
371 WRITE_CACHED_VAR(urcu_active_readers[get_readerid()],
376 :: nest_i >= READER_NEST_LEVEL -> break;
380 read_generation[get_readerid()] = READ_CACHED_VAR(generation_ptr);
382 data_access[get_readerid()] = 1;
384 data_access[get_readerid()] = 0;
388 :: nest_i < READER_NEST_LEVEL ->
390 tmp2 = READ_CACHED_VAR(urcu_active_readers[get_readerid()]);
392 WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp2 - 1);
394 :: nest_i >= READER_NEST_LEVEL -> break;
397 //smp_mc(i); /* added */
400 active [NR_READERS] proctype urcu_reader()
407 assert(get_pid() < NR_PROCS);
413 * We do not test reader's progress here, because we are mainly
414 * interested in writer's progress. The reader never blocks
415 * anyway. We have to test for reader/writer's progress
416 * separately, otherwise we could think the writer is doing
417 * progress when it's blocked by an always progressing reader.
419 #ifdef READER_PROGRESS
420 /* Only test progress of one random reader. They are all the
424 :: get_readerid() == 0 ->
432 urcu_one_read(i, j, nest_i, tmp, tmp2);
436 /* Model the RCU update process. */
438 active proctype urcu_writer()
446 assert(get_pid() < NR_PROCS);
449 :: (READ_CACHED_VAR(generation_ptr) < 5) ->
450 #ifdef WRITER_PROGRESS
455 old_gen = READ_CACHED_VAR(generation_ptr);
456 WRITE_CACHED_VAR(generation_ptr, old_gen + 1);
464 :: write_lock == 0 ->
473 tmp = READ_CACHED_VAR(urcu_gp_ctr);
475 WRITE_CACHED_VAR(urcu_gp_ctr, tmp ^ RCU_GP_CTR_BIT);
478 wait_for_quiescent_state(tmp, tmp2, i, j);
482 tmp = READ_CACHED_VAR(urcu_gp_ctr);
484 WRITE_CACHED_VAR(urcu_gp_ctr, tmp ^ RCU_GP_CTR_BIT);
487 wait_for_quiescent_state(tmp, tmp2, i, j);
491 /* free-up step, e.g., kfree(). */
493 last_free_gen = old_gen;
499 * Given the reader loops infinitely, let the writer also busy-loop
500 * with progress here so, with weak fairness, we can test the
506 #ifdef WRITER_PROGRESS
513 /* Leave after the readers and writers so the pid count is ok. */
518 INIT_CACHED_VAR(urcu_gp_ctr, 1, j);
519 INIT_CACHED_VAR(generation_ptr, 0, j);
524 INIT_CACHED_VAR(urcu_active_readers[i], 0, j);
525 read_generation[i] = 1;
528 :: i >= NR_READERS -> break