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