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
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.
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.
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.
20 * Copyright (c) 2009 Paul E. McKenney, IBM Corporation.
23 /* Promela validation variables. */
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. */
31 /* =0 says reader still running, =1 says done. */
33 /* =0 says updater still running, =1 says done. */
35 /* urcu definitions and variables, taken straight from the algorithm. */
37 #define RCU_GP_CTR_BIT (1 << 7)
38 #define RCU_GP_CTR_NEST_MASK (RCU_GP_CTR_BIT - 1)
41 byte urcu_active_readers = 0;
43 /* Model the RCU read-side critical section. */
45 proctype urcu_reader()
53 /* Absorb any early requests for memory barriers. */
57 :: !updater_done -> skip;
62 * Each pass through this loop executes one read-side statement
63 * from the following code fragment:
65 * rcu_read_lock(); [0a]
66 * rcu_read_lock(); [0b]
67 * p = rcu_dereference(global_p); [1]
69 * rcu_read_unlock(); [3b]
70 * rcu_read_unlock(); [3a]
72 * Because we are modeling a weak-memory machine, these statements
73 * can be seen in any order, the only restriction being that
74 * rcu_read_unlock() cannot precede the corresponding rcu_read_lock().
75 * The placement of the inner rcu_read_lock() and rcu_read_unlock()
76 * is non-deterministic, the above is but one possible placement.
77 * Intestingly enough, this model validates all possible placements
78 * of the inner rcu_read_lock() and rcu_read_unlock() statements,
79 * with the only constraint being that the rcu_read_lock() must
80 * precede the rcu_read_unlock().
82 * We also respond to memory-barrier requests, but only if our
83 * execution happens to be ordered. If the current state is
84 * misordered, we ignore memory-barrier requests.
89 :: reader_progress[0] < 2 -> /* [0a and 0b] */
90 tmp = urcu_active_readers;
92 :: (tmp & RCU_GP_CTR_NEST_MASK) == 0 ->
95 :: (reader_progress[1] +
97 reader_progress[3] == 0) && need_mb == 1 ->
99 :: !updater_done -> skip;
102 urcu_active_readers = tmp;
104 urcu_active_readers = tmp + 1;
106 reader_progress[0] = reader_progress[0] + 1;
107 :: reader_progress[1] == 0 -> /* [1] */
108 tmp_removed = removed;
109 reader_progress[1] = 1;
110 :: reader_progress[2] == 0 -> /* [2] */
112 reader_progress[2] = 1;
113 :: ((reader_progress[0] > reader_progress[3]) &&
114 (reader_progress[3] < 2)) -> /* [3a and 3b] */
115 tmp = urcu_active_readers - 1;
116 urcu_active_readers = tmp;
117 reader_progress[3] = reader_progress[3] + 1;
121 /* Process memory-barrier requests, if it is safe to do so. */
126 :: tmp < 4 && reader_progress[tmp] == 0 ->
129 :: tmp < 4 && reader_progress[tmp] != 0 ->
132 reader_progress[0] == reader_progress[3] ->
136 reader_progress[0] != reader_progress[3] ->
140 :: tmp < 4 && reader_progress[tmp] == 0 ->
142 :: tmp < 4 && reader_progress[tmp] != 0 ->
153 /* We get here if mb processing is safe. */
157 :: !updater_done -> skip;
164 * Check to see if we have modeled the entire RCU read-side
165 * critical section, and leave if so.
168 :: done == 1 -> break;
172 assert((tmp_free == 0) || (tmp_removed == 1));
174 /* Reader has completed. */
177 /* Process any late-arriving memory-barrier requests. */
181 :: !updater_done -> skip;
186 /* Model the RCU update process. */
188 proctype urcu_updater()
190 /* prior synchronize_rcu(), second counter flip. */
193 :: need_mb == 1 -> skip;
194 :: need_mb == 0 -> break;
196 urcu_gp_ctr = urcu_gp_ctr + RCU_GP_CTR_BIT;
199 :: need_mb == 1 -> skip;
200 :: need_mb == 0 -> break;
205 :: (urcu_active_readers & RCU_GP_CTR_NEST_MASK) != 0 &&
206 (urcu_active_readers & ~RCU_GP_CTR_NEST_MASK) !=
207 (urcu_gp_ctr & ~RCU_GP_CTR_NEST_MASK) ->
214 :: need_mb == 1 -> skip;
215 :: need_mb == 0 -> break;
218 /* Removal statement, e.g., list_del_rcu(). */
221 /* current synchronize_rcu(), first counter flip. */
224 :: need_mb == 1 -> skip;
225 :: need_mb == 0 -> break;
227 urcu_gp_ctr = urcu_gp_ctr + RCU_GP_CTR_BIT;
230 :: need_mb == 1 -> skip;
231 :: need_mb == 0 -> break;
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) ->
245 :: need_mb == 1 -> skip;
246 :: need_mb == 0 -> break;
249 /* current synchronize_rcu(), second counter flip. */
252 :: need_mb == 1 -> skip;
253 :: need_mb == 0 -> break;
255 urcu_gp_ctr = urcu_gp_ctr + RCU_GP_CTR_BIT;
258 :: need_mb == 1 -> skip;
259 :: need_mb == 0 -> break;
264 :: (urcu_active_readers & RCU_GP_CTR_NEST_MASK) != 0 &&
265 (urcu_active_readers & ~RCU_GP_CTR_NEST_MASK) !=
266 (urcu_gp_ctr & ~RCU_GP_CTR_NEST_MASK) ->
273 :: need_mb == 1 -> skip;
274 :: need_mb == 0 -> break;
277 /* free-up step, e.g., kfree(). */
281 * Signal updater done, ending any otherwise-infinite loops
282 * in the reading process.
288 * Initialize the array, spawn a reader and an updater. Because readers
289 * are independent of each other, only one reader is needed.
294 reader_progress[0] = 0;
295 reader_progress[1] = 0;
296 reader_progress[2] = 0;
297 reader_progress[3] = 0;