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 ->
132 :: tmp < 4 && reader_progress[tmp] == 0 ->
134 :: tmp < 4 && reader_progress[tmp] != 0 ->
145 /* We get here if mb processing is safe. */
156 * Check to see if we have modeled the entire RCU read-side
157 * critical section, and leave if so.
160 :: done == 1 -> break;
164 assert((tmp_free == 0) || (tmp_removed == 1));
166 /* Process any late-arriving memory-barrier requests. */
175 /* Model the RCU update process. */
177 proctype urcu_updater()
179 /* Removal statement, e.g., list_del_rcu(). */
182 /* synchronize_rcu(), first counter flip. */
185 :: need_mb == 1 -> skip;
186 :: need_mb == 0 -> break;
188 urcu_gp_ctr = urcu_gp_ctr + RCU_GP_CTR_BIT;
191 :: need_mb == 1 -> skip;
192 :: need_mb == 0 -> break;
196 printf("urcu_gp_ctr=%x urcu_active_readers=%x\n", urcu_gp_ctr, urcu_active_readers);
197 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);
199 :: (urcu_active_readers & RCU_GP_CTR_NEST_MASK) != 0 &&
200 (urcu_active_readers & ~RCU_GP_CTR_NEST_MASK) !=
201 (urcu_gp_ctr & ~RCU_GP_CTR_NEST_MASK) ->
208 :: need_mb == 1 -> skip;
209 :: need_mb == 0 -> break;
212 /* Erroneous removal statement, e.g., list_del_rcu(). */
215 /* synchronize_rcu(), second counter flip. */
218 :: need_mb == 1 -> skip;
219 :: need_mb == 0 -> break;
221 urcu_gp_ctr = urcu_gp_ctr + RCU_GP_CTR_BIT;
224 :: need_mb == 1 -> skip;
225 :: need_mb == 0 -> break;
229 printf("urcu_gp_ctr=%x urcu_active_readers=%x\n", urcu_gp_ctr, urcu_active_readers);
230 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);
232 :: (urcu_active_readers & RCU_GP_CTR_NEST_MASK) != 0 &&
233 (urcu_active_readers & ~RCU_GP_CTR_NEST_MASK) !=
234 (urcu_gp_ctr & ~RCU_GP_CTR_NEST_MASK) ->
241 :: need_mb == 1 -> skip;
242 :: need_mb == 0 -> break;
245 /* free-up step, e.g., kfree(). */
250 * Initialize the array, spawn a reader and an updater. Because readers
251 * are independent of each other, only one reader is needed.
256 reader_progress[0] = 0;
257 reader_progress[1] = 0;
258 reader_progress[2] = 0;
259 reader_progress[3] = 0;