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 /* urcu definitions and variables, taken straight from the algorithm. */
33 #define RCU_GP_CTR_BIT (1 << 7)
34 #define RCU_GP_CTR_NEST_MASK (RCU_GP_CTR_BIT - 1)
37 byte urcu_active_readers = 0;
39 /* Model the RCU read-side critical section. */
41 proctype urcu_reader()
49 /* Absorb any early requests for memory barriers. */
58 * Each pass through this loop executes one read-side statement
59 * from the following code fragment:
61 * rcu_read_lock(); [0a]
62 * rcu_read_lock(); [0b]
63 * p = rcu_dereference(global_p); [1]
65 * rcu_read_unlock(); [3b]
66 * rcu_read_unlock(); [3a]
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().
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.
85 :: reader_progress[0] < 2 -> /* [0a and 0b] */
86 tmp = urcu_active_readers;
88 :: (tmp & RCU_GP_CTR_NEST_MASK) == 0 ->
91 :: (reader_progress[1] +
93 reader_progress[3] == 0) && need_mb == 1 ->
98 urcu_active_readers = tmp;
100 urcu_active_readers = tmp + 1;
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] */
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;
117 /* Process memory-barrier requests, if it is safe to do so. */
122 :: tmp < 4 && reader_progress[tmp] == 0 ->
125 :: tmp < 4 && reader_progress[tmp] != 0 ->
128 reader_progress[0] == reader_progress[3] ->
132 reader_progress[0] != reader_progress[3] ->
136 :: tmp < 4 && reader_progress[tmp] == 0 ->
138 :: tmp < 4 && reader_progress[tmp] != 0 ->
149 /* We get here if mb processing is safe. */
160 * Check to see if we have modeled the entire RCU read-side
161 * critical section, and leave if so.
164 :: done == 1 -> break;
168 assert((tmp_free == 0) || (tmp_removed == 1));
170 /* Process any late-arriving memory-barrier requests. */
179 /* Model the RCU update process. */
181 proctype urcu_updater()
183 /* Removal statement, e.g., list_del_rcu(). */
186 /* synchronize_rcu(), first counter flip. */
189 :: need_mb == 1 -> skip;
190 :: need_mb == 0 -> break;
192 urcu_gp_ctr = urcu_gp_ctr + RCU_GP_CTR_BIT;
195 :: need_mb == 1 -> skip;
196 :: need_mb == 0 -> break;
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);
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) ->
212 :: need_mb == 1 -> skip;
213 :: need_mb == 0 -> break;
216 /* Erroneous removal statement, e.g., list_del_rcu(). */
219 /* synchronize_rcu(), second counter flip. */
222 :: need_mb == 1 -> skip;
223 :: need_mb == 0 -> break;
225 urcu_gp_ctr = urcu_gp_ctr + RCU_GP_CTR_BIT;
228 :: need_mb == 1 -> skip;
229 :: need_mb == 0 -> break;
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);
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 /* free-up step, e.g., kfree(). */
254 * Initialize the array, spawn a reader and an updater. Because readers
255 * are independent of each other, only one reader is needed.
260 reader_progress[0] = 0;
261 reader_progress[1] = 0;
262 reader_progress[2] = 0;
263 reader_progress[3] = 0;