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