Fix formal model nesting
[urcu.git] / formal-model / urcu.spin
1 /*
2 * urcu.spin: Promela code to validate urcu. See commit number
3 * 3a9e6e9df706b8d39af94d2f027210e2e7d4106e of Mathieu Desnoyer's
4 * git archive at git://lttng.org/userspace-rcu.git
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 *
20 * Copyright (c) 2009 Paul E. McKenney, IBM Corporation.
21 */
22
23 /* Promela validation variables. */
24
25 bit removed = 0; /* Has RCU removal happened, e.g., list_del_rcu()? */
26 bit free = 0; /* Has RCU reclamation happened, e.g., kfree()? */
27 bit need_mb = 0; /* =1 says need reader mb, =0 for reader response. */
28 byte reader_progress[4];
29 /* Count of read-side statement executions. */
30
31 /* urcu definitions and variables, taken straight from the algorithm. */
32
33 #define RCU_GP_CTR_BIT (1 << 7)
34 #define RCU_GP_CTR_NEST_MASK (RCU_GP_CTR_BIT - 1)
35
36 byte urcu_gp_ctr = 1;
37 byte urcu_active_readers = 0;
38
39 /* Model the RCU read-side critical section. */
40
41 proctype urcu_reader()
42 {
43 bit done = 0;
44 bit mbok;
45 byte tmp;
46 byte tmp_removed;
47 byte tmp_free;
48
49 /* Absorb any early requests for memory barriers. */
50 do
51 :: need_mb == 1 ->
52 need_mb = 0;
53 :: 1 -> skip;
54 :: 1 -> break;
55 od;
56
57 /*
58 * Each pass through this loop executes one read-side statement
59 * from the following code fragment:
60 *
61 * rcu_read_lock(); [0a]
62 * rcu_read_lock(); [0b]
63 * p = rcu_dereference(global_p); [1]
64 * x = p->data; [2]
65 * rcu_read_unlock(); [3b]
66 * rcu_read_unlock(); [3a]
67 *
68 * Because we are modeling a weak-memory machine, these statements
69 * can be seen in any order, the only restriction being that
70 * rcu_read_unlock() cannot precede the corresponding rcu_read_lock().
71 * The placement of the inner rcu_read_lock() and rcu_read_unlock()
72 * is non-deterministic, the above is but one possible placement.
73 * Intestingly enough, this model validates all possible placements
74 * of the inner rcu_read_lock() and rcu_read_unlock() statements,
75 * with the only constraint being that the rcu_read_lock() must
76 * precede the rcu_read_unlock().
77 *
78 * We also respond to memory-barrier requests, but only if our
79 * execution happens to be ordered. If the current state is
80 * misordered, we ignore memory-barrier requests.
81 */
82 do
83 :: 1 ->
84 if
85 :: reader_progress[0] < 2 -> /* [0a and 0b] */
86 tmp = urcu_active_readers;
87 if
88 :: (tmp & RCU_GP_CTR_NEST_MASK) == 0 ->
89 tmp = urcu_gp_ctr;
90 do
91 :: (reader_progress[1] +
92 reader_progress[2] +
93 reader_progress[3] == 0) && need_mb == 1 ->
94 need_mb = 0;
95 :: 1 -> skip;
96 :: 1 -> break;
97 od;
98 urcu_active_readers = tmp;
99 :: else ->
100 urcu_active_readers = tmp + 1;
101 fi;
102 reader_progress[0] = reader_progress[0] + 1;
103 :: reader_progress[1] == 0 -> /* [1] */
104 tmp_removed = removed;
105 reader_progress[1] = 1;
106 :: reader_progress[2] == 0 -> /* [2] */
107 tmp_free = free;
108 reader_progress[2] = 1;
109 :: ((reader_progress[0] > reader_progress[3]) &&
110 (reader_progress[3] < 2)) -> /* [3a and 3b] */
111 tmp = urcu_active_readers - 1;
112 urcu_active_readers = tmp;
113 reader_progress[3] = reader_progress[3] + 1;
114 :: else -> break;
115 fi;
116
117 /* Process memory-barrier requests, if it is safe to do so. */
118 atomic {
119 mbok = 0;
120 tmp = 0;
121 do
122 :: tmp < 4 && reader_progress[tmp] == 0 ->
123 tmp = tmp + 1;
124 break;
125 :: tmp < 4 && reader_progress[tmp] != 0 ->
126 tmp = tmp + 1;
127 :: tmp >= 4 &&
128 reader_progress[0] == reader_progress[3] ->
129 done = 1;
130 break;
131 :: tmp >= 4 &&
132 reader_progress[0] != reader_progress[3] ->
133 break;
134 od;
135 do
136 :: tmp < 4 && reader_progress[tmp] == 0 ->
137 tmp = tmp + 1;
138 :: tmp < 4 && reader_progress[tmp] != 0 ->
139 break;
140 :: tmp >= 4 ->
141 mbok = 1;
142 break;
143 od
144
145 }
146
147 if
148 :: mbok == 1 ->
149 /* We get here if mb processing is safe. */
150 do
151 :: need_mb == 1 ->
152 need_mb = 0;
153 :: 1 -> skip;
154 :: 1 -> break;
155 od;
156 :: else -> skip;
157 fi;
158
159 /*
160 * Check to see if we have modeled the entire RCU read-side
161 * critical section, and leave if so.
162 */
163 if
164 :: done == 1 -> break;
165 :: else -> skip;
166 fi
167 od;
168 assert((tmp_free == 0) || (tmp_removed == 1));
169
170 /* Process any late-arriving memory-barrier requests. */
171 do
172 :: need_mb == 1 ->
173 need_mb = 0;
174 :: 1 -> skip;
175 :: 1 -> break;
176 od;
177 }
178
179 /* Model the RCU update process. */
180
181 proctype urcu_updater()
182 {
183 /* Removal statement, e.g., list_del_rcu(). */
184 removed = 1;
185
186 /* synchronize_rcu(), first counter flip. */
187 need_mb = 1;
188 do
189 :: need_mb == 1 -> skip;
190 :: need_mb == 0 -> break;
191 od;
192 urcu_gp_ctr = urcu_gp_ctr + RCU_GP_CTR_BIT;
193 need_mb = 1;
194 do
195 :: need_mb == 1 -> skip;
196 :: need_mb == 0 -> break;
197 od;
198 do
199 :: 1 ->
200 printf("urcu_gp_ctr=%x urcu_active_readers=%x\n", urcu_gp_ctr, urcu_active_readers);
201 printf("urcu_gp_ctr&0x7f=%x urcu_active_readers&0x7f=%x\n", urcu_gp_ctr & ~RCU_GP_CTR_NEST_MASK, urcu_active_readers & ~RCU_GP_CTR_NEST_MASK);
202 if
203 :: (urcu_active_readers & RCU_GP_CTR_NEST_MASK) != 0 &&
204 (urcu_active_readers & ~RCU_GP_CTR_NEST_MASK) !=
205 (urcu_gp_ctr & ~RCU_GP_CTR_NEST_MASK) ->
206 skip;
207 :: else -> break;
208 fi
209 od;
210 need_mb = 1;
211 do
212 :: need_mb == 1 -> skip;
213 :: need_mb == 0 -> break;
214 od;
215
216 /* Erroneous removal statement, e.g., list_del_rcu(). */
217 /* removed = 1; */
218
219 /* synchronize_rcu(), second counter flip. */
220 need_mb = 1;
221 do
222 :: need_mb == 1 -> skip;
223 :: need_mb == 0 -> break;
224 od;
225 urcu_gp_ctr = urcu_gp_ctr + RCU_GP_CTR_BIT;
226 need_mb = 1;
227 do
228 :: need_mb == 1 -> skip;
229 :: need_mb == 0 -> break;
230 od;
231 do
232 :: 1 ->
233 printf("urcu_gp_ctr=%x urcu_active_readers=%x\n", urcu_gp_ctr, urcu_active_readers);
234 printf("urcu_gp_ctr&0x7f=%x urcu_active_readers&0x7f=%x\n", urcu_gp_ctr & ~RCU_GP_CTR_NEST_MASK, urcu_active_readers & ~RCU_GP_CTR_NEST_MASK);
235 if
236 :: (urcu_active_readers & RCU_GP_CTR_NEST_MASK) != 0 &&
237 (urcu_active_readers & ~RCU_GP_CTR_NEST_MASK) !=
238 (urcu_gp_ctr & ~RCU_GP_CTR_NEST_MASK) ->
239 skip;
240 :: else -> break;
241 fi;
242 od;
243 need_mb = 1;
244 do
245 :: need_mb == 1 -> skip;
246 :: need_mb == 0 -> break;
247 od;
248
249 /* free-up step, e.g., kfree(). */
250 free = 1;
251 }
252
253 /*
254 * Initialize the array, spawn a reader and an updater. Because readers
255 * are independent of each other, only one reader is needed.
256 */
257
258 init {
259 atomic {
260 reader_progress[0] = 0;
261 reader_progress[1] = 0;
262 reader_progress[2] = 0;
263 reader_progress[3] = 0;
264 run urcu_reader();
265 run urcu_updater();
266 }
267 }
This page took 0.035484 seconds and 5 git commands to generate.