Commit | Line | Data |
---|---|---|
b6b17880 MD |
1 | #define NO_WMB |
2 | ||
3 | // Poison value for freed memory | |
4 | #define POISON 1 | |
5 | // Memory with correct data | |
6 | #define WINE 0 | |
7 | #define SLAB_SIZE 2 | |
8 | ||
9 | #define read_poison (data_read_first[0] == POISON || data_read_second[0] == POISON) | |
10 | ||
11 | #define RCU_GP_CTR_BIT (1 << 7) | |
12 | #define RCU_GP_CTR_NEST_MASK (RCU_GP_CTR_BIT - 1) | |
13 | ||
14 | //disabled | |
15 | #define REMOTE_BARRIERS | |
16 | ||
17 | //#define ARCH_ALPHA | |
18 | #define ARCH_INTEL | |
19 | //#define ARCH_POWERPC | |
20 | /* | |
21 | * mem.spin: Promela code to validate memory barriers with OOO memory | |
22 | * and out-of-order instruction scheduling. | |
23 | * | |
24 | * This program is free software; you can redistribute it and/or modify | |
25 | * it under the terms of the GNU General Public License as published by | |
26 | * the Free Software Foundation; either version 2 of the License, or | |
27 | * (at your option) any later version. | |
28 | * | |
29 | * This program is distributed in the hope that it will be useful, | |
30 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
31 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
32 | * GNU General Public License for more details. | |
33 | * | |
34 | * You should have received a copy of the GNU General Public License | |
35 | * along with this program; if not, write to the Free Software | |
36 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
37 | * | |
38 | * Copyright (c) 2009 Mathieu Desnoyers | |
39 | */ | |
40 | ||
41 | /* Promela validation variables. */ | |
42 | ||
43 | /* specific defines "included" here */ | |
44 | /* DEFINES file "included" here */ | |
45 | ||
46 | #define NR_READERS 1 | |
47 | #define NR_WRITERS 1 | |
48 | ||
49 | #define NR_PROCS 2 | |
50 | ||
51 | #define get_pid() (_pid) | |
52 | ||
53 | #define get_readerid() (get_pid()) | |
54 | ||
55 | /* | |
56 | * Produced process control and data flow. Updated after each instruction to | |
57 | * show which variables are ready. Using one-hot bit encoding per variable to | |
58 | * save state space. Used as triggers to execute the instructions having those | |
59 | * variables as input. Leaving bits active to inhibit instruction execution. | |
60 | * Scheme used to make instruction disabling and automatic dependency fall-back | |
61 | * automatic. | |
62 | */ | |
63 | ||
64 | #define CONSUME_TOKENS(state, bits, notbits) \ | |
65 | ((!(state & (notbits))) && (state & (bits)) == (bits)) | |
66 | ||
67 | #define PRODUCE_TOKENS(state, bits) \ | |
68 | state = state | (bits); | |
69 | ||
70 | #define CLEAR_TOKENS(state, bits) \ | |
71 | state = state & ~(bits) | |
72 | ||
73 | /* | |
74 | * Types of dependency : | |
75 | * | |
76 | * Data dependency | |
77 | * | |
78 | * - True dependency, Read-after-Write (RAW) | |
79 | * | |
80 | * This type of dependency happens when a statement depends on the result of a | |
81 | * previous statement. This applies to any statement which needs to read a | |
82 | * variable written by a preceding statement. | |
83 | * | |
84 | * - False dependency, Write-after-Read (WAR) | |
85 | * | |
86 | * Typically, variable renaming can ensure that this dependency goes away. | |
87 | * However, if the statements must read and then write from/to the same variable | |
88 | * in the OOO memory model, renaming may be impossible, and therefore this | |
89 | * causes a WAR dependency. | |
90 | * | |
91 | * - Output dependency, Write-after-Write (WAW) | |
92 | * | |
93 | * Two writes to the same variable in subsequent statements. Variable renaming | |
94 | * can ensure this is not needed, but can be required when writing multiple | |
95 | * times to the same OOO mem model variable. | |
96 | * | |
97 | * Control dependency | |
98 | * | |
99 | * Execution of a given instruction depends on a previous instruction evaluating | |
100 | * in a way that allows its execution. E.g. : branches. | |
101 | * | |
102 | * Useful considerations for joining dependencies after branch | |
103 | * | |
104 | * - Pre-dominance | |
105 | * | |
106 | * "We say box i dominates box j if every path (leading from input to output | |
107 | * through the diagram) which passes through box j must also pass through box | |
108 | * i. Thus box i dominates box j if box j is subordinate to box i in the | |
109 | * program." | |
110 | * | |
111 | * http://www.hipersoft.rice.edu/grads/publications/dom14.pdf | |
112 | * Other classic algorithm to calculate dominance : Lengauer-Tarjan (in gcc) | |
113 | * | |
114 | * - Post-dominance | |
115 | * | |
116 | * Just as pre-dominance, but with arcs of the data flow inverted, and input vs | |
117 | * output exchanged. Therefore, i post-dominating j ensures that every path | |
118 | * passing by j will pass by i before reaching the output. | |
119 | * | |
120 | * Prefetch and speculative execution | |
121 | * | |
122 | * If an instruction depends on the result of a previous branch, but it does not | |
123 | * have side-effects, it can be executed before the branch result is known. | |
124 | * however, it must be restarted if a core-synchronizing instruction is issued. | |
125 | * Note that instructions which depend on the speculative instruction result | |
126 | * but that have side-effects must depend on the branch completion in addition | |
127 | * to the speculatively executed instruction. | |
128 | * | |
129 | * Other considerations | |
130 | * | |
131 | * Note about "volatile" keyword dependency : The compiler will order volatile | |
132 | * accesses so they appear in the right order on a given CPU. They can be | |
133 | * reordered by the CPU instruction scheduling. This therefore cannot be | |
134 | * considered as a depencency. | |
135 | * | |
136 | * References : | |
137 | * | |
138 | * Cooper, Keith D.; & Torczon, Linda. (2005). Engineering a Compiler. Morgan | |
139 | * Kaufmann. ISBN 1-55860-698-X. | |
140 | * Kennedy, Ken; & Allen, Randy. (2001). Optimizing Compilers for Modern | |
141 | * Architectures: A Dependence-based Approach. Morgan Kaufmann. ISBN | |
142 | * 1-55860-286-0. | |
143 | * Muchnick, Steven S. (1997). Advanced Compiler Design and Implementation. | |
144 | * Morgan Kaufmann. ISBN 1-55860-320-4. | |
145 | */ | |
146 | ||
147 | /* | |
148 | * Note about loops and nested calls | |
149 | * | |
150 | * To keep this model simple, loops expressed in the framework will behave as if | |
151 | * there was a core synchronizing instruction between loops. To see the effect | |
152 | * of loop unrolling, manually unrolling loops is required. Note that if loops | |
153 | * end or start with a core synchronizing instruction, the model is appropriate. | |
154 | * Nested calls are not supported. | |
155 | */ | |
156 | ||
157 | /* | |
158 | * Only Alpha has out-of-order cache bank loads. Other architectures (intel, | |
159 | * powerpc, arm) ensure that dependent reads won't be reordered. c.f. | |
160 | * http://www.linuxjournal.com/article/8212) | |
161 | */ | |
162 | #ifdef ARCH_ALPHA | |
163 | #define HAVE_OOO_CACHE_READ | |
164 | #endif | |
165 | ||
166 | /* | |
167 | * Each process have its own data in cache. Caches are randomly updated. | |
168 | * smp_wmb and smp_rmb forces cache updates (write and read), smp_mb forces | |
169 | * both. | |
170 | */ | |
171 | ||
172 | typedef per_proc_byte { | |
173 | byte val[NR_PROCS]; | |
174 | }; | |
175 | ||
176 | typedef per_proc_bit { | |
177 | bit val[NR_PROCS]; | |
178 | }; | |
179 | ||
180 | /* Bitfield has a maximum of 8 procs */ | |
181 | typedef per_proc_bitfield { | |
182 | byte bitfield; | |
183 | }; | |
184 | ||
185 | #define DECLARE_CACHED_VAR(type, x) \ | |
186 | type mem_##x; \ | |
187 | per_proc_##type cached_##x; \ | |
188 | per_proc_bitfield cache_dirty_##x; | |
189 | ||
190 | #define INIT_CACHED_VAR(x, v, j) \ | |
191 | mem_##x = v; \ | |
192 | cache_dirty_##x.bitfield = 0; \ | |
193 | j = 0; \ | |
194 | do \ | |
195 | :: j < NR_PROCS -> \ | |
196 | cached_##x.val[j] = v; \ | |
197 | j++ \ | |
198 | :: j >= NR_PROCS -> break \ | |
199 | od; | |
200 | ||
201 | #define IS_CACHE_DIRTY(x, id) (cache_dirty_##x.bitfield & (1 << id)) | |
202 | ||
203 | #define READ_CACHED_VAR(x) (cached_##x.val[get_pid()]) | |
204 | ||
205 | #define WRITE_CACHED_VAR(x, v) \ | |
206 | atomic { \ | |
207 | cached_##x.val[get_pid()] = v; \ | |
208 | cache_dirty_##x.bitfield = \ | |
209 | cache_dirty_##x.bitfield | (1 << get_pid()); \ | |
210 | } | |
211 | ||
212 | #define CACHE_WRITE_TO_MEM(x, id) \ | |
213 | if \ | |
214 | :: IS_CACHE_DIRTY(x, id) -> \ | |
215 | mem_##x = cached_##x.val[id]; \ | |
216 | cache_dirty_##x.bitfield = \ | |
217 | cache_dirty_##x.bitfield & (~(1 << id)); \ | |
218 | :: else -> \ | |
219 | skip \ | |
220 | fi; | |
221 | ||
222 | #define CACHE_READ_FROM_MEM(x, id) \ | |
223 | if \ | |
224 | :: !IS_CACHE_DIRTY(x, id) -> \ | |
225 | cached_##x.val[id] = mem_##x;\ | |
226 | :: else -> \ | |
227 | skip \ | |
228 | fi; | |
229 | ||
230 | /* | |
231 | * May update other caches if cache is dirty, or not. | |
232 | */ | |
233 | #define RANDOM_CACHE_WRITE_TO_MEM(x, id)\ | |
234 | if \ | |
235 | :: 1 -> CACHE_WRITE_TO_MEM(x, id); \ | |
236 | :: 1 -> skip \ | |
237 | fi; | |
238 | ||
239 | #define RANDOM_CACHE_READ_FROM_MEM(x, id)\ | |
240 | if \ | |
241 | :: 1 -> CACHE_READ_FROM_MEM(x, id); \ | |
242 | :: 1 -> skip \ | |
243 | fi; | |
244 | ||
245 | /* Must consume all prior read tokens. All subsequent reads depend on it. */ | |
246 | inline smp_rmb(i) | |
247 | { | |
248 | atomic { | |
249 | CACHE_READ_FROM_MEM(urcu_gp_ctr, get_pid()); | |
250 | i = 0; | |
251 | do | |
252 | :: i < NR_READERS -> | |
253 | CACHE_READ_FROM_MEM(urcu_active_readers[i], get_pid()); | |
254 | i++ | |
255 | :: i >= NR_READERS -> break | |
256 | od; | |
257 | CACHE_READ_FROM_MEM(rcu_ptr, get_pid()); | |
258 | i = 0; | |
259 | do | |
260 | :: i < SLAB_SIZE -> | |
261 | CACHE_READ_FROM_MEM(rcu_data[i], get_pid()); | |
262 | i++ | |
263 | :: i >= SLAB_SIZE -> break | |
264 | od; | |
265 | } | |
266 | } | |
267 | ||
268 | /* Must consume all prior write tokens. All subsequent writes depend on it. */ | |
269 | inline smp_wmb(i) | |
270 | { | |
271 | atomic { | |
272 | CACHE_WRITE_TO_MEM(urcu_gp_ctr, get_pid()); | |
273 | i = 0; | |
274 | do | |
275 | :: i < NR_READERS -> | |
276 | CACHE_WRITE_TO_MEM(urcu_active_readers[i], get_pid()); | |
277 | i++ | |
278 | :: i >= NR_READERS -> break | |
279 | od; | |
280 | CACHE_WRITE_TO_MEM(rcu_ptr, get_pid()); | |
281 | i = 0; | |
282 | do | |
283 | :: i < SLAB_SIZE -> | |
284 | CACHE_WRITE_TO_MEM(rcu_data[i], get_pid()); | |
285 | i++ | |
286 | :: i >= SLAB_SIZE -> break | |
287 | od; | |
288 | } | |
289 | } | |
290 | ||
291 | /* Synchronization point. Must consume all prior read and write tokens. All | |
292 | * subsequent reads and writes depend on it. */ | |
293 | inline smp_mb(i) | |
294 | { | |
295 | atomic { | |
296 | smp_wmb(i); | |
297 | smp_rmb(i); | |
298 | } | |
299 | } | |
300 | ||
301 | #ifdef REMOTE_BARRIERS | |
302 | ||
303 | bit reader_barrier[NR_READERS]; | |
304 | ||
305 | /* | |
306 | * We cannot leave the barriers dependencies in place in REMOTE_BARRIERS mode | |
307 | * because they would add unexisting core synchronization and would therefore | |
308 | * create an incomplete model. | |
309 | * Therefore, we model the read-side memory barriers by completely disabling the | |
310 | * memory barriers and their dependencies from the read-side. One at a time | |
311 | * (different verification runs), we make a different instruction listen for | |
312 | * signals. | |
313 | */ | |
314 | ||
315 | #define smp_mb_reader(i, j) | |
316 | ||
317 | /* | |
318 | * Service 0, 1 or many barrier requests. | |
319 | */ | |
320 | inline smp_mb_recv(i, j) | |
321 | { | |
322 | do | |
323 | :: (reader_barrier[get_readerid()] == 1) -> | |
324 | /* | |
325 | * We choose to ignore cycles caused by writer busy-looping, | |
326 | * waiting for the reader, sending barrier requests, and the | |
327 | * reader always services them without continuing execution. | |
328 | */ | |
329 | progress_ignoring_mb1: | |
330 | smp_mb(i); | |
331 | reader_barrier[get_readerid()] = 0; | |
332 | :: 1 -> | |
333 | /* | |
334 | * We choose to ignore writer's non-progress caused by the | |
335 | * reader ignoring the writer's mb() requests. | |
336 | */ | |
337 | progress_ignoring_mb2: | |
338 | break; | |
339 | od; | |
340 | } | |
341 | ||
342 | #define PROGRESS_LABEL(progressid) progress_writer_progid_##progressid: | |
343 | ||
344 | #define smp_mb_send(i, j, progressid) \ | |
345 | { \ | |
346 | smp_mb(i); \ | |
347 | i = 0; \ | |
348 | do \ | |
349 | :: i < NR_READERS -> \ | |
350 | reader_barrier[i] = 1; \ | |
351 | /* \ | |
352 | * Busy-looping waiting for reader barrier handling is of little\ | |
353 | * interest, given the reader has the ability to totally ignore \ | |
354 | * barrier requests. \ | |
355 | */ \ | |
356 | do \ | |
357 | :: (reader_barrier[i] == 1) -> \ | |
358 | PROGRESS_LABEL(progressid) \ | |
359 | skip; \ | |
360 | :: (reader_barrier[i] == 0) -> break; \ | |
361 | od; \ | |
362 | i++; \ | |
363 | :: i >= NR_READERS -> \ | |
364 | break \ | |
365 | od; \ | |
366 | smp_mb(i); \ | |
367 | } | |
368 | ||
369 | #else | |
370 | ||
371 | #define smp_mb_send(i, j, progressid) smp_mb(i) | |
372 | #define smp_mb_reader(i, j) smp_mb(i) | |
373 | #define smp_mb_recv(i, j) | |
374 | ||
375 | #endif | |
376 | ||
377 | /* Keep in sync manually with smp_rmb, smp_wmb, ooo_mem and init() */ | |
378 | DECLARE_CACHED_VAR(byte, urcu_gp_ctr); | |
379 | /* Note ! currently only one reader */ | |
380 | DECLARE_CACHED_VAR(byte, urcu_active_readers[NR_READERS]); | |
381 | /* RCU data */ | |
382 | DECLARE_CACHED_VAR(bit, rcu_data[SLAB_SIZE]); | |
383 | ||
384 | /* RCU pointer */ | |
385 | #if (SLAB_SIZE == 2) | |
386 | DECLARE_CACHED_VAR(bit, rcu_ptr); | |
387 | bit ptr_read_first[NR_READERS]; | |
388 | bit ptr_read_second[NR_READERS]; | |
389 | #else | |
390 | DECLARE_CACHED_VAR(byte, rcu_ptr); | |
391 | byte ptr_read_first[NR_READERS]; | |
392 | byte ptr_read_second[NR_READERS]; | |
393 | #endif | |
394 | ||
395 | bit data_read_first[NR_READERS]; | |
396 | bit data_read_second[NR_READERS]; | |
397 | ||
398 | bit init_done = 0; | |
399 | ||
400 | inline wait_init_done() | |
401 | { | |
402 | do | |
403 | :: init_done == 0 -> skip; | |
404 | :: else -> break; | |
405 | od; | |
406 | } | |
407 | ||
408 | inline ooo_mem(i) | |
409 | { | |
410 | atomic { | |
411 | RANDOM_CACHE_WRITE_TO_MEM(urcu_gp_ctr, get_pid()); | |
412 | i = 0; | |
413 | do | |
414 | :: i < NR_READERS -> | |
415 | RANDOM_CACHE_WRITE_TO_MEM(urcu_active_readers[i], | |
416 | get_pid()); | |
417 | i++ | |
418 | :: i >= NR_READERS -> break | |
419 | od; | |
420 | RANDOM_CACHE_WRITE_TO_MEM(rcu_ptr, get_pid()); | |
421 | i = 0; | |
422 | do | |
423 | :: i < SLAB_SIZE -> | |
424 | RANDOM_CACHE_WRITE_TO_MEM(rcu_data[i], get_pid()); | |
425 | i++ | |
426 | :: i >= SLAB_SIZE -> break | |
427 | od; | |
428 | #ifdef HAVE_OOO_CACHE_READ | |
429 | RANDOM_CACHE_READ_FROM_MEM(urcu_gp_ctr, get_pid()); | |
430 | i = 0; | |
431 | do | |
432 | :: i < NR_READERS -> | |
433 | RANDOM_CACHE_READ_FROM_MEM(urcu_active_readers[i], | |
434 | get_pid()); | |
435 | i++ | |
436 | :: i >= NR_READERS -> break | |
437 | od; | |
438 | RANDOM_CACHE_READ_FROM_MEM(rcu_ptr, get_pid()); | |
439 | i = 0; | |
440 | do | |
441 | :: i < SLAB_SIZE -> | |
442 | RANDOM_CACHE_READ_FROM_MEM(rcu_data[i], get_pid()); | |
443 | i++ | |
444 | :: i >= SLAB_SIZE -> break | |
445 | od; | |
446 | #else | |
447 | smp_rmb(i); | |
448 | #endif /* HAVE_OOO_CACHE_READ */ | |
449 | } | |
450 | } | |
451 | ||
452 | /* | |
453 | * Bit encoding, urcu_reader : | |
454 | */ | |
455 | ||
456 | int _proc_urcu_reader; | |
457 | #define proc_urcu_reader _proc_urcu_reader | |
458 | ||
459 | /* Body of PROCEDURE_READ_LOCK */ | |
460 | #define READ_PROD_A_READ (1 << 0) | |
461 | #define READ_PROD_B_IF_TRUE (1 << 1) | |
462 | #define READ_PROD_B_IF_FALSE (1 << 2) | |
463 | #define READ_PROD_C_IF_TRUE_READ (1 << 3) | |
464 | ||
465 | #define PROCEDURE_READ_LOCK(base, consumetoken, consumetoken2, producetoken) \ | |
466 | :: CONSUME_TOKENS(proc_urcu_reader, (consumetoken | consumetoken2), READ_PROD_A_READ << base) -> \ | |
467 | ooo_mem(i); \ | |
468 | tmp = READ_CACHED_VAR(urcu_active_readers[get_readerid()]); \ | |
469 | PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_A_READ << base); \ | |
470 | :: CONSUME_TOKENS(proc_urcu_reader, \ | |
471 | READ_PROD_A_READ << base, /* RAW, pre-dominant */ \ | |
472 | (READ_PROD_B_IF_TRUE | READ_PROD_B_IF_FALSE) << base) -> \ | |
473 | if \ | |
474 | :: (!(tmp & RCU_GP_CTR_NEST_MASK)) -> \ | |
475 | PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_B_IF_TRUE << base); \ | |
476 | :: else -> \ | |
477 | PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_B_IF_FALSE << base); \ | |
478 | fi; \ | |
479 | /* IF TRUE */ \ | |
480 | :: CONSUME_TOKENS(proc_urcu_reader, consumetoken, /* prefetch */ \ | |
481 | READ_PROD_C_IF_TRUE_READ << base) -> \ | |
482 | ooo_mem(i); \ | |
483 | tmp2 = READ_CACHED_VAR(urcu_gp_ctr); \ | |
484 | PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_C_IF_TRUE_READ << base); \ | |
485 | :: CONSUME_TOKENS(proc_urcu_reader, \ | |
486 | (READ_PROD_B_IF_TRUE \ | |
487 | | READ_PROD_C_IF_TRUE_READ /* pre-dominant */ \ | |
488 | | READ_PROD_A_READ) << base, /* WAR */ \ | |
489 | producetoken) -> \ | |
490 | ooo_mem(i); \ | |
491 | WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp2); \ | |
492 | PRODUCE_TOKENS(proc_urcu_reader, producetoken); \ | |
493 | /* IF_MERGE implies \ | |
494 | * post-dominance */ \ | |
495 | /* ELSE */ \ | |
496 | :: CONSUME_TOKENS(proc_urcu_reader, \ | |
497 | (READ_PROD_B_IF_FALSE /* pre-dominant */ \ | |
498 | | READ_PROD_A_READ) << base, /* WAR */ \ | |
499 | producetoken) -> \ | |
500 | ooo_mem(i); \ | |
501 | WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], \ | |
502 | tmp + 1); \ | |
503 | PRODUCE_TOKENS(proc_urcu_reader, producetoken); \ | |
504 | /* IF_MERGE implies \ | |
505 | * post-dominance */ \ | |
506 | /* ENDIF */ \ | |
507 | skip | |
508 | ||
509 | /* Body of PROCEDURE_READ_LOCK */ | |
510 | #define READ_PROC_READ_UNLOCK (1 << 0) | |
511 | ||
512 | #define PROCEDURE_READ_UNLOCK(base, consumetoken, producetoken) \ | |
513 | :: CONSUME_TOKENS(proc_urcu_reader, \ | |
514 | consumetoken, \ | |
515 | READ_PROC_READ_UNLOCK << base) -> \ | |
516 | ooo_mem(i); \ | |
517 | tmp = READ_CACHED_VAR(urcu_active_readers[get_readerid()]); \ | |
518 | PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_READ_UNLOCK << base); \ | |
519 | :: CONSUME_TOKENS(proc_urcu_reader, \ | |
520 | consumetoken \ | |
521 | | (READ_PROC_READ_UNLOCK << base), /* WAR */ \ | |
522 | producetoken) -> \ | |
523 | ooo_mem(i); \ | |
524 | WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp - 1); \ | |
525 | PRODUCE_TOKENS(proc_urcu_reader, producetoken); \ | |
526 | skip | |
527 | ||
528 | ||
529 | #define READ_PROD_NONE (1 << 0) | |
530 | ||
531 | /* PROCEDURE_READ_LOCK base = << 1 : 1 to 5 */ | |
532 | #define READ_LOCK_BASE 1 | |
533 | #define READ_LOCK_OUT (1 << 5) | |
534 | ||
535 | #define READ_PROC_FIRST_MB (1 << 6) | |
536 | ||
537 | /* PROCEDURE_READ_LOCK (NESTED) base : << 7 : 7 to 11 */ | |
538 | #define READ_LOCK_NESTED_BASE 7 | |
539 | #define READ_LOCK_NESTED_OUT (1 << 11) | |
540 | ||
541 | #define READ_PROC_READ_GEN (1 << 12) | |
542 | #define READ_PROC_ACCESS_GEN (1 << 13) | |
543 | ||
544 | /* PROCEDURE_READ_UNLOCK (NESTED) base = << 14 : 14 to 15 */ | |
545 | #define READ_UNLOCK_NESTED_BASE 14 | |
546 | #define READ_UNLOCK_NESTED_OUT (1 << 15) | |
547 | ||
548 | #define READ_PROC_SECOND_MB (1 << 16) | |
549 | ||
550 | /* PROCEDURE_READ_UNLOCK base = << 17 : 17 to 18 */ | |
551 | #define READ_UNLOCK_BASE 17 | |
552 | #define READ_UNLOCK_OUT (1 << 18) | |
553 | ||
554 | /* PROCEDURE_READ_LOCK_UNROLL base = << 19 : 19 to 23 */ | |
555 | #define READ_LOCK_UNROLL_BASE 19 | |
556 | #define READ_LOCK_OUT_UNROLL (1 << 23) | |
557 | ||
558 | #define READ_PROC_THIRD_MB (1 << 24) | |
559 | ||
560 | #define READ_PROC_READ_GEN_UNROLL (1 << 25) | |
561 | #define READ_PROC_ACCESS_GEN_UNROLL (1 << 26) | |
562 | ||
563 | #define READ_PROC_FOURTH_MB (1 << 27) | |
564 | ||
565 | /* PROCEDURE_READ_UNLOCK_UNROLL base = << 28 : 28 to 29 */ | |
566 | #define READ_UNLOCK_UNROLL_BASE 28 | |
567 | #define READ_UNLOCK_OUT_UNROLL (1 << 29) | |
568 | ||
569 | ||
570 | /* Should not include branches */ | |
571 | #define READ_PROC_ALL_TOKENS (READ_PROD_NONE \ | |
572 | | READ_LOCK_OUT \ | |
573 | | READ_PROC_FIRST_MB \ | |
574 | | READ_LOCK_NESTED_OUT \ | |
575 | | READ_PROC_READ_GEN \ | |
576 | | READ_PROC_ACCESS_GEN \ | |
577 | | READ_UNLOCK_NESTED_OUT \ | |
578 | | READ_PROC_SECOND_MB \ | |
579 | | READ_UNLOCK_OUT \ | |
580 | | READ_LOCK_OUT_UNROLL \ | |
581 | | READ_PROC_THIRD_MB \ | |
582 | | READ_PROC_READ_GEN_UNROLL \ | |
583 | | READ_PROC_ACCESS_GEN_UNROLL \ | |
584 | | READ_PROC_FOURTH_MB \ | |
585 | | READ_UNLOCK_OUT_UNROLL) | |
586 | ||
587 | /* Must clear all tokens, including branches */ | |
588 | #define READ_PROC_ALL_TOKENS_CLEAR ((1 << 30) - 1) | |
589 | ||
590 | inline urcu_one_read(i, j, nest_i, tmp, tmp2) | |
591 | { | |
592 | PRODUCE_TOKENS(proc_urcu_reader, READ_PROD_NONE); | |
593 | ||
594 | #ifdef NO_MB | |
595 | PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FIRST_MB); | |
596 | PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_SECOND_MB); | |
597 | PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_THIRD_MB); | |
598 | PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FOURTH_MB); | |
599 | #endif | |
600 | ||
601 | #ifdef REMOTE_BARRIERS | |
602 | PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FIRST_MB); | |
603 | PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_SECOND_MB); | |
604 | PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_THIRD_MB); | |
605 | PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FOURTH_MB); | |
606 | #endif | |
607 | ||
608 | do | |
609 | :: 1 -> | |
610 | ||
611 | #ifdef REMOTE_BARRIERS | |
612 | /* | |
613 | * Signal-based memory barrier will only execute when the | |
614 | * execution order appears in program order. | |
615 | */ | |
616 | if | |
617 | :: 1 -> | |
618 | atomic { | |
619 | if | |
620 | :: CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE, | |
621 | READ_LOCK_OUT | READ_LOCK_NESTED_OUT | |
622 | | READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT | |
623 | | READ_UNLOCK_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 | |
628 | | READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT | |
629 | | READ_UNLOCK_OUT | |
630 | | READ_LOCK_OUT_UNROLL | |
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 | READ_LOCK_NESTED_OUT, | |
633 | READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT | |
634 | | READ_UNLOCK_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 | |
640 | | READ_UNLOCK_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 | READ_PROC_ACCESS_GEN, | |
645 | READ_UNLOCK_NESTED_OUT | |
646 | | READ_UNLOCK_OUT | |
647 | | READ_LOCK_OUT_UNROLL | |
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 | |
653 | | READ_LOCK_OUT_UNROLL | |
654 | | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL) | |
655 | || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT | |
656 | | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN | |
657 | | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT | |
658 | | READ_UNLOCK_OUT, | |
659 | READ_LOCK_OUT_UNROLL | |
660 | | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL) | |
661 | || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT | |
662 | | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN | |
663 | | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT | |
664 | | READ_UNLOCK_OUT | READ_LOCK_OUT_UNROLL, | |
665 | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | 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 | |
668 | | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT | |
669 | | READ_UNLOCK_OUT | READ_LOCK_OUT_UNROLL | |
670 | | READ_PROC_READ_GEN_UNROLL, | |
671 | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL) | |
672 | || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT | |
673 | | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN | |
674 | | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT | |
675 | | READ_UNLOCK_OUT | READ_LOCK_OUT_UNROLL | |
676 | | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL, | |
677 | READ_UNLOCK_OUT_UNROLL) | |
678 | || CONSUME_TOKENS(proc_urcu_reader, READ_PROD_NONE | READ_LOCK_OUT | |
679 | | READ_LOCK_NESTED_OUT | READ_PROC_READ_GEN | READ_PROC_ACCESS_GEN | READ_UNLOCK_NESTED_OUT | |
680 | | READ_UNLOCK_OUT | READ_LOCK_OUT_UNROLL | |
681 | | READ_PROC_READ_GEN_UNROLL | READ_PROC_ACCESS_GEN_UNROLL | READ_UNLOCK_OUT_UNROLL, | |
682 | 0) -> | |
683 | goto non_atomic3; | |
684 | non_atomic3_end: | |
685 | skip; | |
686 | fi; | |
687 | } | |
688 | fi; | |
689 | ||
690 | goto non_atomic3_skip; | |
691 | non_atomic3: | |
692 | smp_mb_recv(i, j); | |
693 | goto non_atomic3_end; | |
694 | non_atomic3_skip: | |
695 | ||
696 | #endif /* REMOTE_BARRIERS */ | |
697 | ||
698 | atomic { | |
699 | if | |
700 | PROCEDURE_READ_LOCK(READ_LOCK_BASE, READ_PROD_NONE, 0, READ_LOCK_OUT); | |
701 | ||
702 | :: CONSUME_TOKENS(proc_urcu_reader, | |
703 | READ_LOCK_OUT, /* post-dominant */ | |
704 | READ_PROC_FIRST_MB) -> | |
705 | smp_mb_reader(i, j); | |
706 | PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FIRST_MB); | |
707 | ||
708 | PROCEDURE_READ_LOCK(READ_LOCK_NESTED_BASE, READ_PROC_FIRST_MB, READ_LOCK_OUT, | |
709 | READ_LOCK_NESTED_OUT); | |
710 | ||
711 | :: CONSUME_TOKENS(proc_urcu_reader, | |
712 | READ_PROC_FIRST_MB, /* mb() orders reads */ | |
713 | READ_PROC_READ_GEN) -> | |
714 | ooo_mem(i); | |
715 | ptr_read_first[get_readerid()] = READ_CACHED_VAR(rcu_ptr); | |
716 | PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_READ_GEN); | |
717 | ||
718 | :: CONSUME_TOKENS(proc_urcu_reader, | |
719 | READ_PROC_FIRST_MB /* mb() orders reads */ | |
720 | | READ_PROC_READ_GEN, | |
721 | READ_PROC_ACCESS_GEN) -> | |
722 | /* smp_read_barrier_depends */ | |
723 | goto rmb1; | |
724 | rmb1_end: | |
725 | data_read_first[get_readerid()] = | |
726 | READ_CACHED_VAR(rcu_data[ptr_read_first[get_readerid()]]); | |
727 | PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_ACCESS_GEN); | |
728 | ||
729 | ||
730 | /* Note : we remove the nested memory barrier from the read unlock | |
731 | * model, given it is not usually needed. The implementation has the barrier | |
732 | * because the performance impact added by a branch in the common case does not | |
733 | * justify it. | |
734 | */ | |
735 | ||
736 | PROCEDURE_READ_UNLOCK(READ_UNLOCK_NESTED_BASE, | |
737 | READ_PROC_FIRST_MB | |
738 | | READ_LOCK_OUT | |
739 | | READ_LOCK_NESTED_OUT, | |
740 | READ_UNLOCK_NESTED_OUT); | |
741 | ||
742 | ||
743 | :: CONSUME_TOKENS(proc_urcu_reader, | |
744 | READ_PROC_ACCESS_GEN /* mb() orders reads */ | |
745 | | READ_PROC_READ_GEN /* mb() orders reads */ | |
746 | | READ_PROC_FIRST_MB /* mb() ordered */ | |
747 | | READ_LOCK_OUT /* post-dominant */ | |
748 | | READ_LOCK_NESTED_OUT /* post-dominant */ | |
749 | | READ_UNLOCK_NESTED_OUT, | |
750 | READ_PROC_SECOND_MB) -> | |
751 | smp_mb_reader(i, j); | |
752 | PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_SECOND_MB); | |
753 | ||
754 | PROCEDURE_READ_UNLOCK(READ_UNLOCK_BASE, | |
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_UNLOCK_OUT); | |
761 | ||
762 | /* Unrolling loop : second consecutive lock */ | |
763 | /* reading urcu_active_readers, which have been written by | |
764 | * READ_UNLOCK_OUT : RAW */ | |
765 | PROCEDURE_READ_LOCK(READ_LOCK_UNROLL_BASE, | |
766 | READ_PROC_SECOND_MB /* mb() orders reads */ | |
767 | | READ_PROC_FIRST_MB, /* mb() orders reads */ | |
768 | READ_LOCK_NESTED_OUT /* RAW */ | |
769 | | READ_LOCK_OUT /* RAW */ | |
770 | | READ_UNLOCK_NESTED_OUT /* RAW */ | |
771 | | READ_UNLOCK_OUT, /* RAW */ | |
772 | READ_LOCK_OUT_UNROLL); | |
773 | ||
774 | ||
775 | :: CONSUME_TOKENS(proc_urcu_reader, | |
776 | READ_PROC_FIRST_MB /* mb() ordered */ | |
777 | | READ_PROC_SECOND_MB /* mb() ordered */ | |
778 | | READ_LOCK_OUT_UNROLL /* post-dominant */ | |
779 | | READ_LOCK_NESTED_OUT | |
780 | | READ_LOCK_OUT | |
781 | | READ_UNLOCK_NESTED_OUT | |
782 | | READ_UNLOCK_OUT, | |
783 | READ_PROC_THIRD_MB) -> | |
784 | smp_mb_reader(i, j); | |
785 | PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_THIRD_MB); | |
786 | ||
787 | :: CONSUME_TOKENS(proc_urcu_reader, | |
788 | READ_PROC_FIRST_MB /* mb() orders reads */ | |
789 | | READ_PROC_SECOND_MB /* mb() orders reads */ | |
790 | | READ_PROC_THIRD_MB, /* mb() orders reads */ | |
791 | READ_PROC_READ_GEN_UNROLL) -> | |
792 | ooo_mem(i); | |
793 | ptr_read_second[get_readerid()] = READ_CACHED_VAR(rcu_ptr); | |
794 | PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_READ_GEN_UNROLL); | |
795 | ||
796 | :: CONSUME_TOKENS(proc_urcu_reader, | |
797 | READ_PROC_READ_GEN_UNROLL | |
798 | | READ_PROC_FIRST_MB /* mb() orders reads */ | |
799 | | READ_PROC_SECOND_MB /* mb() orders reads */ | |
800 | | READ_PROC_THIRD_MB, /* mb() orders reads */ | |
801 | READ_PROC_ACCESS_GEN_UNROLL) -> | |
802 | /* smp_read_barrier_depends */ | |
803 | goto rmb2; | |
804 | rmb2_end: | |
805 | data_read_second[get_readerid()] = | |
806 | READ_CACHED_VAR(rcu_data[ptr_read_second[get_readerid()]]); | |
807 | PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_ACCESS_GEN_UNROLL); | |
808 | ||
809 | :: CONSUME_TOKENS(proc_urcu_reader, | |
810 | READ_PROC_READ_GEN_UNROLL /* mb() orders reads */ | |
811 | | READ_PROC_ACCESS_GEN_UNROLL /* mb() orders reads */ | |
812 | | READ_PROC_FIRST_MB /* mb() ordered */ | |
813 | | READ_PROC_SECOND_MB /* mb() ordered */ | |
814 | | READ_PROC_THIRD_MB /* mb() ordered */ | |
815 | | READ_LOCK_OUT_UNROLL /* post-dominant */ | |
816 | | READ_LOCK_NESTED_OUT | |
817 | | READ_LOCK_OUT | |
818 | | READ_UNLOCK_NESTED_OUT | |
819 | | READ_UNLOCK_OUT, | |
820 | READ_PROC_FOURTH_MB) -> | |
821 | smp_mb_reader(i, j); | |
822 | PRODUCE_TOKENS(proc_urcu_reader, READ_PROC_FOURTH_MB); | |
823 | ||
824 | PROCEDURE_READ_UNLOCK(READ_UNLOCK_UNROLL_BASE, | |
825 | READ_PROC_FOURTH_MB /* mb() orders reads */ | |
826 | | READ_PROC_THIRD_MB /* mb() orders reads */ | |
827 | | READ_LOCK_OUT_UNROLL /* RAW */ | |
828 | | READ_PROC_SECOND_MB /* mb() orders reads */ | |
829 | | READ_PROC_FIRST_MB /* mb() orders reads */ | |
830 | | READ_LOCK_NESTED_OUT /* RAW */ | |
831 | | READ_LOCK_OUT /* RAW */ | |
832 | | READ_UNLOCK_NESTED_OUT, /* RAW */ | |
833 | READ_UNLOCK_OUT_UNROLL); | |
834 | :: CONSUME_TOKENS(proc_urcu_reader, READ_PROC_ALL_TOKENS, 0) -> | |
835 | CLEAR_TOKENS(proc_urcu_reader, READ_PROC_ALL_TOKENS_CLEAR); | |
836 | break; | |
837 | fi; | |
838 | } | |
839 | od; | |
840 | /* | |
841 | * Dependency between consecutive loops : | |
842 | * RAW dependency on | |
843 | * WRITE_CACHED_VAR(urcu_active_readers[get_readerid()], tmp2 - 1) | |
844 | * tmp = READ_CACHED_VAR(urcu_active_readers[get_readerid()]); | |
845 | * between loops. | |
846 | * _WHEN THE MB()s are in place_, they add full ordering of the | |
847 | * generation pointer read wrt active reader count read, which ensures | |
848 | * execution will not spill across loop execution. | |
849 | * However, in the event mb()s are removed (execution using signal | |
850 | * handler to promote barrier()() -> smp_mb()), nothing prevents one loop | |
851 | * to spill its execution on other loop's execution. | |
852 | */ | |
853 | goto end; | |
854 | rmb1: | |
855 | #ifndef NO_RMB | |
856 | smp_rmb(i); | |
857 | #else | |
858 | ooo_mem(i); | |
859 | #endif | |
860 | goto rmb1_end; | |
861 | rmb2: | |
862 | #ifndef NO_RMB | |
863 | smp_rmb(i); | |
864 | #else | |
865 | ooo_mem(i); | |
866 | #endif | |
867 | goto rmb2_end; | |
868 | end: | |
869 | skip; | |
870 | } | |
871 | ||
872 | ||
873 | ||
874 | active proctype urcu_reader() | |
875 | { | |
876 | byte i, j, nest_i; | |
877 | byte tmp, tmp2; | |
878 | ||
879 | wait_init_done(); | |
880 | ||
881 | assert(get_pid() < NR_PROCS); | |
882 | ||
883 | end_reader: | |
884 | do | |
885 | :: 1 -> | |
886 | /* | |
887 | * We do not test reader's progress here, because we are mainly | |
888 | * interested in writer's progress. The reader never blocks | |
889 | * anyway. We have to test for reader/writer's progress | |
890 | * separately, otherwise we could think the writer is doing | |
891 | * progress when it's blocked by an always progressing reader. | |
892 | */ | |
893 | #ifdef READER_PROGRESS | |
894 | progress_reader: | |
895 | #endif | |
896 | urcu_one_read(i, j, nest_i, tmp, tmp2); | |
897 | od; | |
898 | } | |
899 | ||
900 | /* no name clash please */ | |
901 | #undef proc_urcu_reader | |
902 | ||
903 | ||
904 | /* Model the RCU update process. */ | |
905 | ||
906 | /* | |
907 | * Bit encoding, urcu_writer : | |
908 | * Currently only supports one reader. | |
909 | */ | |
910 | ||
911 | int _proc_urcu_writer; | |
912 | #define proc_urcu_writer _proc_urcu_writer | |
913 | ||
914 | #define WRITE_PROD_NONE (1 << 0) | |
915 | ||
916 | #define WRITE_DATA (1 << 1) | |
917 | #define WRITE_PROC_WMB (1 << 2) | |
918 | #define WRITE_XCHG_PTR (1 << 3) | |
919 | ||
920 | #define WRITE_PROC_FIRST_MB (1 << 4) | |
921 | ||
922 | /* first flip */ | |
923 | #define WRITE_PROC_FIRST_READ_GP (1 << 5) | |
924 | #define WRITE_PROC_FIRST_WRITE_GP (1 << 6) | |
925 | #define WRITE_PROC_FIRST_WAIT (1 << 7) | |
926 | #define WRITE_PROC_FIRST_WAIT_LOOP (1 << 8) | |
927 | ||
928 | /* second flip */ | |
929 | #define WRITE_PROC_SECOND_READ_GP (1 << 9) | |
930 | #define WRITE_PROC_SECOND_WRITE_GP (1 << 10) | |
931 | #define WRITE_PROC_SECOND_WAIT (1 << 11) | |
932 | #define WRITE_PROC_SECOND_WAIT_LOOP (1 << 12) | |
933 | ||
934 | #define WRITE_PROC_SECOND_MB (1 << 13) | |
935 | ||
936 | #define WRITE_FREE (1 << 14) | |
937 | ||
938 | #define WRITE_PROC_ALL_TOKENS (WRITE_PROD_NONE \ | |
939 | | WRITE_DATA \ | |
940 | | WRITE_PROC_WMB \ | |
941 | | WRITE_XCHG_PTR \ | |
942 | | WRITE_PROC_FIRST_MB \ | |
943 | | WRITE_PROC_FIRST_READ_GP \ | |
944 | | WRITE_PROC_FIRST_WRITE_GP \ | |
945 | | WRITE_PROC_FIRST_WAIT \ | |
946 | | WRITE_PROC_SECOND_READ_GP \ | |
947 | | WRITE_PROC_SECOND_WRITE_GP \ | |
948 | | WRITE_PROC_SECOND_WAIT \ | |
949 | | WRITE_PROC_SECOND_MB \ | |
950 | | WRITE_FREE) | |
951 | ||
952 | #define WRITE_PROC_ALL_TOKENS_CLEAR ((1 << 15) - 1) | |
953 | ||
954 | /* | |
955 | * Mutexes are implied around writer execution. A single writer at a time. | |
956 | */ | |
957 | active proctype urcu_writer() | |
958 | { | |
959 | byte i, j; | |
960 | byte tmp, tmp2, tmpa; | |
961 | byte cur_data = 0, old_data, loop_nr = 0; | |
962 | byte cur_gp_val = 0; /* | |
963 | * Keep a local trace of the current parity so | |
964 | * we don't add non-existing dependencies on the global | |
965 | * GP update. Needed to test single flip case. | |
966 | */ | |
967 | ||
968 | wait_init_done(); | |
969 | ||
970 | assert(get_pid() < NR_PROCS); | |
971 | ||
972 | do | |
973 | :: (loop_nr < 3) -> | |
974 | #ifdef WRITER_PROGRESS | |
975 | progress_writer1: | |
976 | #endif | |
977 | loop_nr = loop_nr + 1; | |
978 | ||
979 | PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROD_NONE); | |
980 | ||
981 | #ifdef NO_WMB | |
982 | PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_WMB); | |
983 | #endif | |
984 | ||
985 | #ifdef NO_MB | |
986 | PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_MB); | |
987 | PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_MB); | |
988 | #endif | |
989 | ||
990 | #ifdef SINGLE_FLIP | |
991 | PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_READ_GP); | |
992 | PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WRITE_GP); | |
993 | PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT); | |
994 | /* For single flip, we need to know the current parity */ | |
995 | cur_gp_val = cur_gp_val ^ RCU_GP_CTR_BIT; | |
996 | #endif | |
997 | ||
998 | do :: 1 -> | |
999 | atomic { | |
1000 | if | |
1001 | ||
1002 | :: CONSUME_TOKENS(proc_urcu_writer, | |
1003 | WRITE_PROD_NONE, | |
1004 | WRITE_DATA) -> | |
1005 | ooo_mem(i); | |
1006 | cur_data = (cur_data + 1) % SLAB_SIZE; | |
1007 | WRITE_CACHED_VAR(rcu_data[cur_data], WINE); | |
1008 | PRODUCE_TOKENS(proc_urcu_writer, WRITE_DATA); | |
1009 | ||
1010 | ||
1011 | :: CONSUME_TOKENS(proc_urcu_writer, | |
1012 | WRITE_DATA, | |
1013 | WRITE_PROC_WMB) -> | |
1014 | smp_wmb(i); | |
1015 | PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_WMB); | |
1016 | ||
1017 | :: CONSUME_TOKENS(proc_urcu_writer, | |
1018 | WRITE_PROC_WMB, | |
1019 | WRITE_XCHG_PTR) -> | |
1020 | /* rcu_xchg_pointer() */ | |
1021 | atomic { | |
1022 | old_data = READ_CACHED_VAR(rcu_ptr); | |
1023 | WRITE_CACHED_VAR(rcu_ptr, cur_data); | |
1024 | } | |
1025 | PRODUCE_TOKENS(proc_urcu_writer, WRITE_XCHG_PTR); | |
1026 | ||
1027 | :: CONSUME_TOKENS(proc_urcu_writer, | |
1028 | WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR, | |
1029 | WRITE_PROC_FIRST_MB) -> | |
1030 | goto smp_mb_send1; | |
1031 | smp_mb_send1_end: | |
1032 | PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_MB); | |
1033 | ||
1034 | /* first flip */ | |
1035 | :: CONSUME_TOKENS(proc_urcu_writer, | |
1036 | WRITE_PROC_FIRST_MB, | |
1037 | WRITE_PROC_FIRST_READ_GP) -> | |
1038 | tmpa = READ_CACHED_VAR(urcu_gp_ctr); | |
1039 | PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_READ_GP); | |
1040 | :: CONSUME_TOKENS(proc_urcu_writer, | |
1041 | WRITE_PROC_FIRST_MB | WRITE_PROC_WMB | |
1042 | | WRITE_PROC_FIRST_READ_GP, | |
1043 | WRITE_PROC_FIRST_WRITE_GP) -> | |
1044 | ooo_mem(i); | |
1045 | WRITE_CACHED_VAR(urcu_gp_ctr, tmpa ^ RCU_GP_CTR_BIT); | |
1046 | PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WRITE_GP); | |
1047 | ||
1048 | :: CONSUME_TOKENS(proc_urcu_writer, | |
1049 | //WRITE_PROC_FIRST_WRITE_GP | /* TEST ADDING SYNC CORE */ | |
1050 | WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */ | |
1051 | WRITE_PROC_FIRST_WAIT | WRITE_PROC_FIRST_WAIT_LOOP) -> | |
1052 | ooo_mem(i); | |
1053 | //smp_mb(i); /* TEST */ | |
1054 | /* ONLY WAITING FOR READER 0 */ | |
1055 | tmp2 = READ_CACHED_VAR(urcu_active_readers[0]); | |
1056 | #ifndef SINGLE_FLIP | |
1057 | /* In normal execution, we are always starting by | |
1058 | * waiting for the even parity. | |
1059 | */ | |
1060 | cur_gp_val = RCU_GP_CTR_BIT; | |
1061 | #endif | |
1062 | if | |
1063 | :: (tmp2 & RCU_GP_CTR_NEST_MASK) | |
1064 | && ((tmp2 ^ cur_gp_val) & RCU_GP_CTR_BIT) -> | |
1065 | PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WAIT_LOOP); | |
1066 | :: else -> | |
1067 | PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WAIT); | |
1068 | fi; | |
1069 | ||
1070 | :: CONSUME_TOKENS(proc_urcu_writer, | |
1071 | //WRITE_PROC_FIRST_WRITE_GP /* TEST ADDING SYNC CORE */ | |
1072 | WRITE_PROC_FIRST_WRITE_GP | |
1073 | | WRITE_PROC_FIRST_READ_GP | |
1074 | | WRITE_PROC_FIRST_WAIT_LOOP | |
1075 | | WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR | |
1076 | | WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */ | |
1077 | 0) -> | |
1078 | #ifndef GEN_ERROR_WRITER_PROGRESS | |
1079 | goto smp_mb_send2; | |
1080 | smp_mb_send2_end: | |
1081 | /* The memory barrier will invalidate the | |
1082 | * second read done as prefetching. Note that all | |
1083 | * instructions with side-effects depending on | |
1084 | * WRITE_PROC_SECOND_READ_GP should also depend on | |
1085 | * completion of this busy-waiting loop. */ | |
1086 | CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_READ_GP); | |
1087 | #else | |
1088 | ooo_mem(i); | |
1089 | #endif | |
1090 | /* This instruction loops to WRITE_PROC_FIRST_WAIT */ | |
1091 | CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_FIRST_WAIT_LOOP | WRITE_PROC_FIRST_WAIT); | |
1092 | ||
1093 | /* second flip */ | |
1094 | :: CONSUME_TOKENS(proc_urcu_writer, | |
1095 | //WRITE_PROC_FIRST_WAIT | //test /* no dependency. Could pre-fetch, no side-effect. */ | |
1096 | WRITE_PROC_FIRST_WRITE_GP | |
1097 | | WRITE_PROC_FIRST_READ_GP | |
1098 | | WRITE_PROC_FIRST_MB, | |
1099 | WRITE_PROC_SECOND_READ_GP) -> | |
1100 | ooo_mem(i); | |
1101 | //smp_mb(i); /* TEST */ | |
1102 | tmpa = READ_CACHED_VAR(urcu_gp_ctr); | |
1103 | PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_READ_GP); | |
1104 | :: CONSUME_TOKENS(proc_urcu_writer, | |
1105 | WRITE_PROC_FIRST_WAIT /* dependency on first wait, because this | |
1106 | * instruction has globally observable | |
1107 | * side-effects. | |
1108 | */ | |
1109 | | WRITE_PROC_FIRST_MB | |
1110 | | WRITE_PROC_WMB | |
1111 | | WRITE_PROC_FIRST_READ_GP | |
1112 | | WRITE_PROC_FIRST_WRITE_GP | |
1113 | | WRITE_PROC_SECOND_READ_GP, | |
1114 | WRITE_PROC_SECOND_WRITE_GP) -> | |
1115 | ooo_mem(i); | |
1116 | WRITE_CACHED_VAR(urcu_gp_ctr, tmpa ^ RCU_GP_CTR_BIT); | |
1117 | PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WRITE_GP); | |
1118 | ||
1119 | :: CONSUME_TOKENS(proc_urcu_writer, | |
1120 | //WRITE_PROC_FIRST_WRITE_GP | /* TEST ADDING SYNC CORE */ | |
1121 | WRITE_PROC_FIRST_WAIT | |
1122 | | WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */ | |
1123 | WRITE_PROC_SECOND_WAIT | WRITE_PROC_SECOND_WAIT_LOOP) -> | |
1124 | ooo_mem(i); | |
1125 | //smp_mb(i); /* TEST */ | |
1126 | /* ONLY WAITING FOR READER 0 */ | |
1127 | tmp2 = READ_CACHED_VAR(urcu_active_readers[0]); | |
1128 | if | |
1129 | :: (tmp2 & RCU_GP_CTR_NEST_MASK) | |
1130 | && ((tmp2 ^ 0) & RCU_GP_CTR_BIT) -> | |
1131 | PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT_LOOP); | |
1132 | :: else -> | |
1133 | PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT); | |
1134 | fi; | |
1135 | ||
1136 | :: CONSUME_TOKENS(proc_urcu_writer, | |
1137 | //WRITE_PROC_FIRST_WRITE_GP | /* TEST ADDING SYNC CORE */ | |
1138 | WRITE_PROC_SECOND_WRITE_GP | |
1139 | | WRITE_PROC_FIRST_WRITE_GP | |
1140 | | WRITE_PROC_SECOND_READ_GP | |
1141 | | WRITE_PROC_FIRST_READ_GP | |
1142 | | WRITE_PROC_SECOND_WAIT_LOOP | |
1143 | | WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR | |
1144 | | WRITE_PROC_FIRST_MB, /* can be reordered before/after flips */ | |
1145 | 0) -> | |
1146 | #ifndef GEN_ERROR_WRITER_PROGRESS | |
1147 | goto smp_mb_send3; | |
1148 | smp_mb_send3_end: | |
1149 | #else | |
1150 | ooo_mem(i); | |
1151 | #endif | |
1152 | /* This instruction loops to WRITE_PROC_SECOND_WAIT */ | |
1153 | CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_WAIT_LOOP | WRITE_PROC_SECOND_WAIT); | |
1154 | ||
1155 | ||
1156 | :: CONSUME_TOKENS(proc_urcu_writer, | |
1157 | WRITE_PROC_FIRST_WAIT | |
1158 | | WRITE_PROC_SECOND_WAIT | |
1159 | | WRITE_PROC_FIRST_READ_GP | |
1160 | | WRITE_PROC_SECOND_READ_GP | |
1161 | | WRITE_PROC_FIRST_WRITE_GP | |
1162 | | WRITE_PROC_SECOND_WRITE_GP | |
1163 | | WRITE_DATA | WRITE_PROC_WMB | WRITE_XCHG_PTR | |
1164 | | WRITE_PROC_FIRST_MB, | |
1165 | WRITE_PROC_SECOND_MB) -> | |
1166 | goto smp_mb_send4; | |
1167 | smp_mb_send4_end: | |
1168 | PRODUCE_TOKENS(proc_urcu_writer, WRITE_PROC_SECOND_MB); | |
1169 | ||
1170 | :: CONSUME_TOKENS(proc_urcu_writer, | |
1171 | WRITE_XCHG_PTR | |
1172 | | WRITE_PROC_FIRST_WAIT | |
1173 | | WRITE_PROC_SECOND_WAIT | |
1174 | | WRITE_PROC_WMB /* No dependency on | |
1175 | * WRITE_DATA because we | |
1176 | * write to a | |
1177 | * different location. */ | |
1178 | | WRITE_PROC_SECOND_MB | |
1179 | | WRITE_PROC_FIRST_MB, | |
1180 | WRITE_FREE) -> | |
1181 | WRITE_CACHED_VAR(rcu_data[old_data], POISON); | |
1182 | PRODUCE_TOKENS(proc_urcu_writer, WRITE_FREE); | |
1183 | ||
1184 | :: CONSUME_TOKENS(proc_urcu_writer, WRITE_PROC_ALL_TOKENS, 0) -> | |
1185 | CLEAR_TOKENS(proc_urcu_writer, WRITE_PROC_ALL_TOKENS_CLEAR); | |
1186 | break; | |
1187 | fi; | |
1188 | } | |
1189 | od; | |
1190 | /* | |
1191 | * Note : Promela model adds implicit serialization of the | |
1192 | * WRITE_FREE instruction. Normally, it would be permitted to | |
1193 | * spill on the next loop execution. Given the validation we do | |
1194 | * checks for the data entry read to be poisoned, it's ok if | |
1195 | * we do not check "late arriving" memory poisoning. | |
1196 | */ | |
1197 | :: else -> break; | |
1198 | od; | |
1199 | /* | |
1200 | * Given the reader loops infinitely, let the writer also busy-loop | |
1201 | * with progress here so, with weak fairness, we can test the | |
1202 | * writer's progress. | |
1203 | */ | |
1204 | end_writer: | |
1205 | do | |
1206 | :: 1 -> | |
1207 | #ifdef WRITER_PROGRESS | |
1208 | progress_writer2: | |
1209 | #endif | |
1210 | #ifdef READER_PROGRESS | |
1211 | /* | |
1212 | * Make sure we don't block the reader's progress. | |
1213 | */ | |
1214 | smp_mb_send(i, j, 5); | |
1215 | #endif | |
1216 | skip; | |
1217 | od; | |
1218 | ||
1219 | /* Non-atomic parts of the loop */ | |
1220 | goto end; | |
1221 | smp_mb_send1: | |
1222 | smp_mb_send(i, j, 1); | |
1223 | goto smp_mb_send1_end; | |
1224 | #ifndef GEN_ERROR_WRITER_PROGRESS | |
1225 | smp_mb_send2: | |
1226 | smp_mb_send(i, j, 2); | |
1227 | goto smp_mb_send2_end; | |
1228 | smp_mb_send3: | |
1229 | smp_mb_send(i, j, 3); | |
1230 | goto smp_mb_send3_end; | |
1231 | #endif | |
1232 | smp_mb_send4: | |
1233 | smp_mb_send(i, j, 4); | |
1234 | goto smp_mb_send4_end; | |
1235 | end: | |
1236 | skip; | |
1237 | } | |
1238 | ||
1239 | /* no name clash please */ | |
1240 | #undef proc_urcu_writer | |
1241 | ||
1242 | ||
1243 | /* Leave after the readers and writers so the pid count is ok. */ | |
1244 | init { | |
1245 | byte i, j; | |
1246 | ||
1247 | atomic { | |
1248 | INIT_CACHED_VAR(urcu_gp_ctr, 1, j); | |
1249 | INIT_CACHED_VAR(rcu_ptr, 0, j); | |
1250 | ||
1251 | i = 0; | |
1252 | do | |
1253 | :: i < NR_READERS -> | |
1254 | INIT_CACHED_VAR(urcu_active_readers[i], 0, j); | |
1255 | ptr_read_first[i] = 1; | |
1256 | ptr_read_second[i] = 1; | |
1257 | data_read_first[i] = WINE; | |
1258 | data_read_second[i] = WINE; | |
1259 | i++; | |
1260 | :: i >= NR_READERS -> break | |
1261 | od; | |
1262 | INIT_CACHED_VAR(rcu_data[0], WINE, j); | |
1263 | i = 1; | |
1264 | do | |
1265 | :: i < SLAB_SIZE -> | |
1266 | INIT_CACHED_VAR(rcu_data[i], POISON, j); | |
1267 | i++ | |
1268 | :: i >= SLAB_SIZE -> break | |
1269 | od; | |
1270 | ||
1271 | init_done = 1; | |
1272 | } | |
1273 | } |