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