| 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 | done = 1; |
| 129 | break; |
| 130 | od; |
| 131 | do |
| 132 | :: tmp < 4 && reader_progress[tmp] == 0 -> |
| 133 | tmp = tmp + 1; |
| 134 | :: tmp < 4 && reader_progress[tmp] != 0 -> |
| 135 | break; |
| 136 | :: tmp >= 4 -> |
| 137 | mbok = 1; |
| 138 | break; |
| 139 | od |
| 140 | |
| 141 | } |
| 142 | |
| 143 | if |
| 144 | :: mbok == 1 -> |
| 145 | /* We get here if mb processing is safe. */ |
| 146 | do |
| 147 | :: need_mb == 1 -> |
| 148 | need_mb = 0; |
| 149 | :: 1 -> skip; |
| 150 | :: 1 -> break; |
| 151 | od; |
| 152 | :: else -> skip; |
| 153 | fi; |
| 154 | |
| 155 | /* |
| 156 | * Check to see if we have modeled the entire RCU read-side |
| 157 | * critical section, and leave if so. |
| 158 | */ |
| 159 | if |
| 160 | :: done == 1 -> break; |
| 161 | :: else -> skip; |
| 162 | fi |
| 163 | od; |
| 164 | assert((tmp_free == 0) || (tmp_removed == 1)); |
| 165 | |
| 166 | /* Process any late-arriving memory-barrier requests. */ |
| 167 | do |
| 168 | :: need_mb == 1 -> |
| 169 | need_mb = 0; |
| 170 | :: 1 -> skip; |
| 171 | :: 1 -> break; |
| 172 | od; |
| 173 | } |
| 174 | |
| 175 | /* Model the RCU update process. */ |
| 176 | |
| 177 | proctype urcu_updater() |
| 178 | { |
| 179 | /* Removal statement, e.g., list_del_rcu(). */ |
| 180 | removed = 1; |
| 181 | |
| 182 | /* synchronize_rcu(), first counter flip. */ |
| 183 | need_mb = 1; |
| 184 | do |
| 185 | :: need_mb == 1 -> skip; |
| 186 | :: need_mb == 0 -> break; |
| 187 | od; |
| 188 | urcu_gp_ctr = urcu_gp_ctr + RCU_GP_CTR_BIT; |
| 189 | need_mb = 1; |
| 190 | do |
| 191 | :: need_mb == 1 -> skip; |
| 192 | :: need_mb == 0 -> break; |
| 193 | od; |
| 194 | do |
| 195 | :: 1 -> |
| 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); |
| 198 | if |
| 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) -> |
| 202 | skip; |
| 203 | :: else -> break; |
| 204 | fi |
| 205 | od; |
| 206 | need_mb = 1; |
| 207 | do |
| 208 | :: need_mb == 1 -> skip; |
| 209 | :: need_mb == 0 -> break; |
| 210 | od; |
| 211 | |
| 212 | /* Erroneous removal statement, e.g., list_del_rcu(). */ |
| 213 | /* removed = 1; */ |
| 214 | |
| 215 | /* synchronize_rcu(), second counter flip. */ |
| 216 | need_mb = 1; |
| 217 | do |
| 218 | :: need_mb == 1 -> skip; |
| 219 | :: need_mb == 0 -> break; |
| 220 | od; |
| 221 | urcu_gp_ctr = urcu_gp_ctr + RCU_GP_CTR_BIT; |
| 222 | need_mb = 1; |
| 223 | do |
| 224 | :: need_mb == 1 -> skip; |
| 225 | :: need_mb == 0 -> break; |
| 226 | od; |
| 227 | do |
| 228 | :: 1 -> |
| 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); |
| 231 | if |
| 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) -> |
| 235 | skip; |
| 236 | :: else -> break; |
| 237 | fi; |
| 238 | od; |
| 239 | need_mb = 1; |
| 240 | do |
| 241 | :: need_mb == 1 -> skip; |
| 242 | :: need_mb == 0 -> break; |
| 243 | od; |
| 244 | |
| 245 | /* free-up step, e.g., kfree(). */ |
| 246 | free = 1; |
| 247 | } |
| 248 | |
| 249 | /* |
| 250 | * Initialize the array, spawn a reader and an updater. Because readers |
| 251 | * are independent of each other, only one reader is needed. |
| 252 | */ |
| 253 | |
| 254 | init { |
| 255 | atomic { |
| 256 | reader_progress[0] = 0; |
| 257 | reader_progress[1] = 0; |
| 258 | reader_progress[2] = 0; |
| 259 | reader_progress[3] = 0; |
| 260 | run urcu_reader(); |
| 261 | run urcu_updater(); |
| 262 | } |
| 263 | } |