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