Commit | Line | Data |
---|---|---|
b257a10b MD |
1 | /* |
2 | * urcu.c | |
3 | * | |
4 | * Userspace RCU library | |
5 | * | |
af02d47e MD |
6 | * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca> |
7 | * Copyright (c) 2009 Paul E. McKenney, IBM Corporation. | |
b257a10b | 8 | * |
af02d47e MD |
9 | * This library is free software; you can redistribute it and/or |
10 | * modify it under the terms of the GNU Lesser General Public | |
11 | * License as published by the Free Software Foundation; either | |
12 | * version 2.1 of the License, or (at your option) any later version. | |
13 | * | |
14 | * This library is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 | * Lesser General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU Lesser General Public | |
20 | * License along with this library; if not, write to the Free Software | |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
54843abc PM |
22 | * |
23 | * IBM's contributions to this file may be relicensed under LGPLv2 or later. | |
b257a10b MD |
24 | */ |
25 | ||
27b012e2 MD |
26 | #include <stdio.h> |
27 | #include <pthread.h> | |
28 | #include <signal.h> | |
29 | #include <assert.h> | |
f69f195a MD |
30 | #include <stdlib.h> |
31 | #include <string.h> | |
09a9f986 | 32 | #include <errno.h> |
e8043c1b | 33 | #include <poll.h> |
27b012e2 | 34 | |
121a5d44 MD |
35 | #include "urcu-static.h" |
36 | /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */ | |
27b012e2 MD |
37 | #include "urcu.h" |
38 | ||
02be5561 | 39 | #ifndef RCU_MB |
834a45ba MD |
40 | static int init_done; |
41 | ||
02be5561 MD |
42 | void __attribute__((constructor)) rcu_init(void); |
43 | void __attribute__((destructor)) rcu_exit(void); | |
e90a6e9c | 44 | #else |
02be5561 | 45 | void rcu_init(void) |
e90a6e9c MD |
46 | { |
47 | } | |
48 | #endif | |
8a5fb4c9 | 49 | |
02be5561 | 50 | static pthread_mutex_t rcu_mutex = PTHREAD_MUTEX_INITIALIZER; |
27b012e2 | 51 | |
bc6c15bb MD |
52 | int gp_futex; |
53 | ||
128166c9 MD |
54 | /* |
55 | * Global grace period counter. | |
02be5561 | 56 | * Contains the current RCU_GP_CTR_PHASE. |
afb8f2c9 | 57 | * Also has a RCU_GP_COUNT of 1, to accelerate the reader fast path. |
b0d5e790 | 58 | * Written to only by writer with mutex taken. Read by both writer and readers. |
128166c9 | 59 | */ |
02be5561 | 60 | long rcu_gp_ctr = RCU_GP_COUNT; |
27b012e2 | 61 | |
b0d5e790 MD |
62 | /* |
63 | * Written to only by each individual reader. Read by both the reader and the | |
64 | * writers. | |
65 | */ | |
02be5561 | 66 | struct rcu_reader __thread rcu_reader; |
27b012e2 | 67 | |
cf380c2f | 68 | #ifdef DEBUG_YIELD |
9d335088 MD |
69 | unsigned int yield_active; |
70 | unsigned int __thread rand_yield; | |
cf380c2f MD |
71 | #endif |
72 | ||
e3b0cef0 | 73 | static LIST_HEAD(registry); |
27b012e2 | 74 | |
02be5561 | 75 | static void internal_rcu_lock(void) |
41718ff9 MD |
76 | { |
77 | int ret; | |
09a9f986 PM |
78 | |
79 | #ifndef DISTRUST_SIGNALS_EXTREME | |
02be5561 | 80 | ret = pthread_mutex_lock(&rcu_mutex); |
41718ff9 MD |
81 | if (ret) { |
82 | perror("Error in pthread mutex lock"); | |
83 | exit(-1); | |
84 | } | |
09a9f986 | 85 | #else /* #ifndef DISTRUST_SIGNALS_EXTREME */ |
02be5561 | 86 | while ((ret = pthread_mutex_trylock(&rcu_mutex)) != 0) { |
09a9f986 PM |
87 | if (ret != EBUSY && ret != EINTR) { |
88 | printf("ret = %d, errno = %d\n", ret, errno); | |
89 | perror("Error in pthread mutex lock"); | |
90 | exit(-1); | |
91 | } | |
02be5561 | 92 | if (rcu_reader.need_mb) { |
09a9f986 | 93 | smp_mb(); |
02be5561 | 94 | rcu_reader.need_mb = 0; |
09a9f986 PM |
95 | smp_mb(); |
96 | } | |
97 | poll(NULL,0,10); | |
98 | } | |
99 | #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */ | |
41718ff9 MD |
100 | } |
101 | ||
02be5561 | 102 | static void internal_rcu_unlock(void) |
41718ff9 MD |
103 | { |
104 | int ret; | |
105 | ||
02be5561 | 106 | ret = pthread_mutex_unlock(&rcu_mutex); |
41718ff9 MD |
107 | if (ret) { |
108 | perror("Error in pthread mutex unlock"); | |
109 | exit(-1); | |
110 | } | |
111 | } | |
112 | ||
27b012e2 | 113 | /* |
02be5561 | 114 | * called with rcu_mutex held. |
27b012e2 | 115 | */ |
02be5561 | 116 | static void switch_next_rcu_qparity(void) |
27b012e2 | 117 | { |
02be5561 | 118 | STORE_SHARED(rcu_gp_ctr, rcu_gp_ctr ^ RCU_GP_CTR_PHASE); |
27b012e2 MD |
119 | } |
120 | ||
02be5561 | 121 | #ifdef RCU_MB |
9d7e3f89 | 122 | static void smp_mb_heavy() |
40e140c9 MD |
123 | { |
124 | smp_mb(); | |
125 | } | |
9d7e3f89 | 126 | #else |
78ff9419 | 127 | static void force_mb_all_readers(void) |
27b012e2 | 128 | { |
02be5561 | 129 | struct rcu_reader *index; |
e3b0cef0 | 130 | |
27b012e2 | 131 | /* |
b715b99e | 132 | * Ask for each threads to execute a smp_mb() so we can consider the |
27b012e2 MD |
133 | * compiler barriers around rcu read lock as real memory barriers. |
134 | */ | |
e3b0cef0 | 135 | if (list_empty(®istry)) |
27b012e2 | 136 | return; |
3a86deba MD |
137 | /* |
138 | * pthread_kill has a smp_mb(). But beware, we assume it performs | |
157dca95 MD |
139 | * a cache flush on architectures with non-coherent cache. Let's play |
140 | * safe and don't assume anything : we use smp_mc() to make sure the | |
141 | * cache flush is enforced. | |
3a86deba | 142 | */ |
e3b0cef0 MD |
143 | list_for_each_entry(index, ®istry, head) { |
144 | index->need_mb = 1; | |
09a9f986 | 145 | smp_mc(); /* write need_mb before sending the signal */ |
02be5561 | 146 | pthread_kill(index->tid, SIGRCU); |
09a9f986 | 147 | } |
27b012e2 MD |
148 | /* |
149 | * Wait for sighandler (and thus mb()) to execute on every thread. | |
09a9f986 PM |
150 | * |
151 | * Note that the pthread_kill() will never be executed on systems | |
152 | * that correctly deliver signals in a timely manner. However, it | |
153 | * is not uncommon for kernels to have bugs that can result in | |
154 | * lost or unduly delayed signals. | |
155 | * | |
156 | * If you are seeing the below pthread_kill() executing much at | |
157 | * all, we suggest testing the underlying kernel and filing the | |
158 | * relevant bug report. For Linux kernels, we recommend getting | |
159 | * the Linux Test Project (LTP). | |
27b012e2 | 160 | */ |
e3b0cef0 MD |
161 | list_for_each_entry(index, ®istry, head) { |
162 | while (index->need_mb) { | |
02be5561 | 163 | pthread_kill(index->tid, SIGRCU); |
09a9f986 PM |
164 | poll(NULL, 0, 1); |
165 | } | |
166 | } | |
167 | smp_mb(); /* read ->need_mb before ending the barrier */ | |
27b012e2 | 168 | } |
9d7e3f89 MD |
169 | |
170 | static void smp_mb_heavy() | |
171 | { | |
172 | force_mb_all_readers(); | |
173 | } | |
02be5561 | 174 | #endif /* #else #ifdef RCU_MB */ |
27b012e2 | 175 | |
bc6c15bb MD |
176 | /* |
177 | * synchronize_rcu() waiting. Single thread. | |
178 | */ | |
cfe78e25 | 179 | static void wait_gp(void) |
bc6c15bb | 180 | { |
cfe78e25 | 181 | /* Read reader_gp before read futex */ |
9d7e3f89 | 182 | smp_mb_heavy(); |
cfe78e25 | 183 | if (uatomic_read(&gp_futex) == -1) |
0854ccff | 184 | futex_async(&gp_futex, FUTEX_WAIT, -1, |
cfe78e25 | 185 | NULL, NULL, 0); |
bc6c15bb MD |
186 | } |
187 | ||
1430ee0b | 188 | void wait_for_quiescent_state(void) |
27b012e2 | 189 | { |
cfe78e25 MD |
190 | LIST_HEAD(qsreaders); |
191 | int wait_loops = 0; | |
02be5561 | 192 | struct rcu_reader *index, *tmp; |
27b012e2 | 193 | |
e3b0cef0 | 194 | if (list_empty(®istry)) |
27b012e2 | 195 | return; |
40e140c9 | 196 | /* |
02be5561 | 197 | * Wait for each thread rcu_reader.ctr count to become 0. |
27b012e2 | 198 | */ |
cfe78e25 MD |
199 | for (;;) { |
200 | wait_loops++; | |
201 | if (wait_loops == RCU_QS_ACTIVE_ATTEMPTS) { | |
202 | uatomic_dec(&gp_futex); | |
203 | /* Write futex before read reader_gp */ | |
9d7e3f89 | 204 | smp_mb_heavy(); |
cfe78e25 MD |
205 | } |
206 | ||
23758cc9 | 207 | list_for_each_entry_safe(index, tmp, ®istry, head) { |
cfe78e25 MD |
208 | if (!rcu_old_gp_ongoing(&index->ctr)) |
209 | list_move(&index->head, &qsreaders); | |
210 | } | |
211 | ||
e8043c1b | 212 | #ifndef HAS_INCOHERENT_CACHES |
cfe78e25 MD |
213 | if (list_empty(®istry)) { |
214 | if (wait_loops == RCU_QS_ACTIVE_ATTEMPTS) { | |
215 | /* Read reader_gp before write futex */ | |
9d7e3f89 | 216 | smp_mb_heavy(); |
cfe78e25 | 217 | uatomic_set(&gp_futex, 0); |
bc6c15bb | 218 | } |
cfe78e25 MD |
219 | break; |
220 | } else { | |
221 | if (wait_loops == RCU_QS_ACTIVE_ATTEMPTS) | |
222 | wait_gp(); | |
223 | else | |
224 | cpu_relax(); | |
bc6c15bb | 225 | } |
e8043c1b | 226 | #else /* #ifndef HAS_INCOHERENT_CACHES */ |
27b012e2 | 227 | /* |
40e140c9 | 228 | * BUSY-LOOP. Force the reader thread to commit its |
02be5561 | 229 | * rcu_reader.ctr update to memory if we wait for too long. |
27b012e2 | 230 | */ |
cfe78e25 MD |
231 | if (list_empty(®istry)) { |
232 | if (wait_loops == RCU_QS_ACTIVE_ATTEMPTS) { | |
233 | /* Read reader_gp before write futex */ | |
9d7e3f89 | 234 | smp_mb_heavy(); |
cfe78e25 MD |
235 | uatomic_set(&gp_futex, 0); |
236 | } | |
237 | break; | |
238 | } else { | |
239 | switch (wait_loops) { | |
bc6c15bb | 240 | case RCU_QS_ACTIVE_ATTEMPTS: |
cfe78e25 MD |
241 | wait_gp(); |
242 | break; /* only escape switch */ | |
bc6c15bb | 243 | case KICK_READER_LOOPS: |
9d7e3f89 | 244 | smp_mb_heavy(); |
40e140c9 | 245 | wait_loops = 0; |
cfe78e25 | 246 | break; /* only escape switch */ |
bc6c15bb | 247 | default: |
3b55dbf4 | 248 | cpu_relax(); |
40e140c9 MD |
249 | } |
250 | } | |
e8043c1b | 251 | #endif /* #else #ifndef HAS_INCOHERENT_CACHES */ |
27b012e2 | 252 | } |
cfe78e25 | 253 | /* put back the reader list in the registry */ |
23758cc9 | 254 | list_splice(&qsreaders, ®istry); |
27b012e2 MD |
255 | } |
256 | ||
9598a481 | 257 | void synchronize_rcu(void) |
2bc59bd7 | 258 | { |
02be5561 | 259 | internal_rcu_lock(); |
135530fd | 260 | |
9598a481 | 261 | /* All threads should read qparity before accessing data structure |
02be5561 | 262 | * where new ptr points to. Must be done within internal_rcu_lock |
135530fd | 263 | * because it iterates on reader threads.*/ |
9598a481 | 264 | /* Write new ptr before changing the qparity */ |
9d7e3f89 | 265 | smp_mb_heavy(); |
9598a481 | 266 | |
02be5561 | 267 | switch_next_rcu_qparity(); /* 0 -> 1 */ |
2bc59bd7 PM |
268 | |
269 | /* | |
9598a481 MD |
270 | * Must commit qparity update to memory before waiting for parity |
271 | * 0 quiescent state. Failure to do so could result in the writer | |
272 | * waiting forever while new readers are always accessing data (no | |
273 | * progress). | |
b0d5e790 | 274 | * Ensured by STORE_SHARED and LOAD_SHARED. |
2bc59bd7 | 275 | */ |
2bc59bd7 | 276 | |
67c2d80b | 277 | /* |
5dba80f9 MD |
278 | * Adding a smp_mb() which is _not_ formally required, but makes the |
279 | * model easier to understand. It does not have a big performance impact | |
280 | * anyway, given this is the write-side. | |
67c2d80b | 281 | */ |
5dba80f9 | 282 | smp_mb(); |
67c2d80b | 283 | |
9598a481 MD |
284 | /* |
285 | * Wait for previous parity to be empty of readers. | |
286 | */ | |
287 | wait_for_quiescent_state(); /* Wait readers in parity 0 */ | |
9598a481 MD |
288 | |
289 | /* | |
290 | * Must finish waiting for quiescent state for parity 0 before | |
291 | * committing qparity update to memory. Failure to do so could result in | |
292 | * the writer waiting forever while new readers are always accessing | |
293 | * data (no progress). | |
b0d5e790 | 294 | * Ensured by STORE_SHARED and LOAD_SHARED. |
9598a481 | 295 | */ |
9598a481 | 296 | |
5dba80f9 MD |
297 | /* |
298 | * Adding a smp_mb() which is _not_ formally required, but makes the | |
299 | * model easier to understand. It does not have a big performance impact | |
300 | * anyway, given this is the write-side. | |
301 | */ | |
302 | smp_mb(); | |
67c2d80b | 303 | |
02be5561 | 304 | switch_next_rcu_qparity(); /* 1 -> 0 */ |
9598a481 MD |
305 | |
306 | /* | |
307 | * Must commit qparity update to memory before waiting for parity | |
308 | * 1 quiescent state. Failure to do so could result in the writer | |
309 | * waiting forever while new readers are always accessing data (no | |
310 | * progress). | |
b0d5e790 | 311 | * Ensured by STORE_SHARED and LOAD_SHARED. |
9598a481 | 312 | */ |
9598a481 | 313 | |
5dba80f9 MD |
314 | /* |
315 | * Adding a smp_mb() which is _not_ formally required, but makes the | |
316 | * model easier to understand. It does not have a big performance impact | |
317 | * anyway, given this is the write-side. | |
318 | */ | |
319 | smp_mb(); | |
67c2d80b | 320 | |
9598a481 MD |
321 | /* |
322 | * Wait for previous parity to be empty of readers. | |
323 | */ | |
324 | wait_for_quiescent_state(); /* Wait readers in parity 1 */ | |
9598a481 | 325 | |
9598a481 | 326 | /* Finish waiting for reader threads before letting the old ptr being |
02be5561 | 327 | * freed. Must be done within internal_rcu_lock because it iterates on |
135530fd | 328 | * reader threads. */ |
9d7e3f89 | 329 | smp_mb_heavy(); |
135530fd | 330 | |
02be5561 | 331 | internal_rcu_unlock(); |
2bc59bd7 PM |
332 | } |
333 | ||
121a5d44 MD |
334 | /* |
335 | * library wrappers to be used by non-LGPL compatible source code. | |
336 | */ | |
337 | ||
338 | void rcu_read_lock(void) | |
339 | { | |
340 | _rcu_read_lock(); | |
341 | } | |
342 | ||
343 | void rcu_read_unlock(void) | |
344 | { | |
345 | _rcu_read_unlock(); | |
346 | } | |
347 | ||
121a5d44 | 348 | void rcu_register_thread(void) |
27b012e2 | 349 | { |
02be5561 MD |
350 | rcu_reader.tid = pthread_self(); |
351 | assert(rcu_reader.need_mb == 0); | |
352 | assert(rcu_reader.ctr == 0); | |
353 | ||
354 | internal_rcu_lock(); | |
355 | rcu_init(); /* In case gcc does not support constructor attribute */ | |
356 | list_add(&rcu_reader.head, ®istry); | |
357 | internal_rcu_unlock(); | |
27b012e2 MD |
358 | } |
359 | ||
121a5d44 | 360 | void rcu_unregister_thread(void) |
27b012e2 | 361 | { |
02be5561 MD |
362 | internal_rcu_lock(); |
363 | list_del(&rcu_reader.head); | |
364 | internal_rcu_unlock(); | |
27b012e2 MD |
365 | } |
366 | ||
02be5561 MD |
367 | #ifndef RCU_MB |
368 | static void sigrcu_handler(int signo, siginfo_t *siginfo, void *context) | |
27b012e2 | 369 | { |
40e140c9 MD |
370 | /* |
371 | * Executing this smp_mb() is the only purpose of this signal handler. | |
372 | * It punctually promotes barrier() into smp_mb() on every thread it is | |
373 | * executed on. | |
374 | */ | |
b715b99e | 375 | smp_mb(); |
02be5561 | 376 | rcu_reader.need_mb = 0; |
09a9f986 | 377 | smp_mb(); |
27b012e2 MD |
378 | } |
379 | ||
8a5fb4c9 | 380 | /* |
02be5561 | 381 | * rcu_init constructor. Called when the library is linked, but also when |
8a5fb4c9 MD |
382 | * reader threads are calling rcu_register_thread(). |
383 | * Should only be called by a single thread at a given time. This is ensured by | |
02be5561 | 384 | * holing the internal_rcu_lock() from rcu_register_thread() or by running at |
8a5fb4c9 MD |
385 | * library load time, which should not be executed by multiple threads nor |
386 | * concurrently with rcu_register_thread() anyway. | |
387 | */ | |
02be5561 | 388 | void rcu_init(void) |
27b012e2 MD |
389 | { |
390 | struct sigaction act; | |
391 | int ret; | |
392 | ||
8a5fb4c9 MD |
393 | if (init_done) |
394 | return; | |
395 | init_done = 1; | |
396 | ||
02be5561 | 397 | act.sa_sigaction = sigrcu_handler; |
dd052bd3 | 398 | act.sa_flags = SA_SIGINFO | SA_RESTART; |
c297c21c | 399 | sigemptyset(&act.sa_mask); |
02be5561 | 400 | ret = sigaction(SIGRCU, &act, NULL); |
f69f195a MD |
401 | if (ret) { |
402 | perror("Error in sigaction"); | |
27b012e2 MD |
403 | exit(-1); |
404 | } | |
405 | } | |
406 | ||
02be5561 | 407 | void rcu_exit(void) |
27b012e2 MD |
408 | { |
409 | struct sigaction act; | |
410 | int ret; | |
411 | ||
02be5561 | 412 | ret = sigaction(SIGRCU, NULL, &act); |
f69f195a MD |
413 | if (ret) { |
414 | perror("Error in sigaction"); | |
27b012e2 MD |
415 | exit(-1); |
416 | } | |
02be5561 | 417 | assert(act.sa_sigaction == sigrcu_handler); |
e3b0cef0 | 418 | assert(list_empty(®istry)); |
27b012e2 | 419 | } |
02be5561 | 420 | #endif /* #ifndef RCU_MB */ |