| 1 | /* |
| 2 | * urcu.c |
| 3 | * |
| 4 | * Userspace RCU library |
| 5 | * |
| 6 | * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 7 | * Copyright (c) 2009 Paul E. McKenney, IBM Corporation. |
| 8 | * |
| 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 |
| 22 | * |
| 23 | * IBM's contributions to this file may be relicensed under LGPLv2 or later. |
| 24 | */ |
| 25 | |
| 26 | #define _BSD_SOURCE |
| 27 | #define _GNU_SOURCE |
| 28 | #define _LGPL_SOURCE |
| 29 | #define _DEFAULT_SOURCE |
| 30 | #include <stdio.h> |
| 31 | #include <pthread.h> |
| 32 | #include <signal.h> |
| 33 | #include <assert.h> |
| 34 | #include <stdlib.h> |
| 35 | #include <stdint.h> |
| 36 | #include <string.h> |
| 37 | #include <errno.h> |
| 38 | #include <poll.h> |
| 39 | |
| 40 | #include "urcu/wfqueue.h" |
| 41 | #include "urcu/map/urcu.h" |
| 42 | #include "urcu/static/urcu.h" |
| 43 | #include "urcu-pointer.h" |
| 44 | #include "urcu/tls-compat.h" |
| 45 | |
| 46 | #include "urcu-die.h" |
| 47 | |
| 48 | /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */ |
| 49 | #undef _LGPL_SOURCE |
| 50 | #include "urcu.h" |
| 51 | #define _LGPL_SOURCE |
| 52 | |
| 53 | /* |
| 54 | * If a reader is really non-cooperative and refuses to commit its |
| 55 | * rcu_active_readers count to memory (there is no barrier in the reader |
| 56 | * per-se), kick it after 10 loops waiting for it. |
| 57 | */ |
| 58 | #define KICK_READER_LOOPS 10 |
| 59 | |
| 60 | /* |
| 61 | * Active attempts to check for reader Q.S. before calling futex(). |
| 62 | */ |
| 63 | #define RCU_QS_ACTIVE_ATTEMPTS 100 |
| 64 | |
| 65 | #ifdef RCU_MEMBARRIER |
| 66 | static int init_done; |
| 67 | int has_sys_membarrier; |
| 68 | |
| 69 | void __attribute__((constructor)) rcu_init(void); |
| 70 | #endif |
| 71 | |
| 72 | #ifdef RCU_MB |
| 73 | void rcu_init(void) |
| 74 | { |
| 75 | } |
| 76 | #endif |
| 77 | |
| 78 | #ifdef RCU_SIGNAL |
| 79 | static int init_done; |
| 80 | |
| 81 | void __attribute__((constructor)) rcu_init(void); |
| 82 | void __attribute__((destructor)) rcu_exit(void); |
| 83 | #endif |
| 84 | |
| 85 | /* |
| 86 | * rcu_gp_lock ensures mutual exclusion between threads calling |
| 87 | * synchronize_rcu(). |
| 88 | */ |
| 89 | static pthread_mutex_t rcu_gp_lock = PTHREAD_MUTEX_INITIALIZER; |
| 90 | /* |
| 91 | * rcu_registry_lock ensures mutual exclusion between threads |
| 92 | * registering and unregistering themselves to/from the registry, and |
| 93 | * with threads reading that registry from synchronize_rcu(). However, |
| 94 | * this lock is not held all the way through the completion of awaiting |
| 95 | * for the grace period. It is sporadically released between iterations |
| 96 | * on the registry. |
| 97 | * rcu_registry_lock may nest inside rcu_gp_lock. |
| 98 | */ |
| 99 | static pthread_mutex_t rcu_registry_lock = PTHREAD_MUTEX_INITIALIZER; |
| 100 | |
| 101 | int32_t gp_futex; |
| 102 | |
| 103 | /* |
| 104 | * Global grace period counter. |
| 105 | * Contains the current RCU_GP_CTR_PHASE. |
| 106 | * Also has a RCU_GP_COUNT of 1, to accelerate the reader fast path. |
| 107 | * Written to only by writer with mutex taken. Read by both writer and readers. |
| 108 | */ |
| 109 | unsigned long rcu_gp_ctr = RCU_GP_COUNT; |
| 110 | /* |
| 111 | * Written to only by each individual reader. Read by both the reader and the |
| 112 | * writers. |
| 113 | */ |
| 114 | __DEFINE_URCU_TLS_GLOBAL(struct rcu_reader, rcu_reader); |
| 115 | |
| 116 | #ifdef DEBUG_YIELD |
| 117 | unsigned int yield_active; |
| 118 | __DEFINE_URCU_TLS_GLOBAL(unsigned int, rand_yield); |
| 119 | #endif |
| 120 | |
| 121 | static CDS_LIST_HEAD(registry); |
| 122 | |
| 123 | static void mutex_lock(pthread_mutex_t *mutex) |
| 124 | { |
| 125 | int ret; |
| 126 | |
| 127 | #ifndef DISTRUST_SIGNALS_EXTREME |
| 128 | ret = pthread_mutex_lock(mutex); |
| 129 | if (ret) |
| 130 | urcu_die(ret); |
| 131 | #else /* #ifndef DISTRUST_SIGNALS_EXTREME */ |
| 132 | while ((ret = pthread_mutex_trylock(mutex)) != 0) { |
| 133 | if (ret != EBUSY && ret != EINTR) |
| 134 | urcu_die(ret); |
| 135 | if (CMM_LOAD_SHARED(URCU_TLS(rcu_reader).need_mb)) { |
| 136 | cmm_smp_mb(); |
| 137 | _CMM_STORE_SHARED(URCU_TLS(rcu_reader).need_mb, 0); |
| 138 | cmm_smp_mb(); |
| 139 | } |
| 140 | poll(NULL,0,10); |
| 141 | } |
| 142 | #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */ |
| 143 | } |
| 144 | |
| 145 | static void mutex_unlock(pthread_mutex_t *mutex) |
| 146 | { |
| 147 | int ret; |
| 148 | |
| 149 | ret = pthread_mutex_unlock(mutex); |
| 150 | if (ret) |
| 151 | urcu_die(ret); |
| 152 | } |
| 153 | |
| 154 | #ifdef RCU_MEMBARRIER |
| 155 | static void smp_mb_master(int group) |
| 156 | { |
| 157 | if (caa_likely(has_sys_membarrier)) |
| 158 | membarrier(MEMBARRIER_EXPEDITED); |
| 159 | else |
| 160 | cmm_smp_mb(); |
| 161 | } |
| 162 | #endif |
| 163 | |
| 164 | #ifdef RCU_MB |
| 165 | static void smp_mb_master(int group) |
| 166 | { |
| 167 | cmm_smp_mb(); |
| 168 | } |
| 169 | #endif |
| 170 | |
| 171 | #ifdef RCU_SIGNAL |
| 172 | static void force_mb_all_readers(void) |
| 173 | { |
| 174 | struct rcu_reader *index; |
| 175 | |
| 176 | /* |
| 177 | * Ask for each threads to execute a cmm_smp_mb() so we can consider the |
| 178 | * compiler barriers around rcu read lock as real memory barriers. |
| 179 | */ |
| 180 | if (cds_list_empty(®istry)) |
| 181 | return; |
| 182 | /* |
| 183 | * pthread_kill has a cmm_smp_mb(). But beware, we assume it performs |
| 184 | * a cache flush on architectures with non-coherent cache. Let's play |
| 185 | * safe and don't assume anything : we use cmm_smp_mc() to make sure the |
| 186 | * cache flush is enforced. |
| 187 | */ |
| 188 | cds_list_for_each_entry(index, ®istry, node) { |
| 189 | CMM_STORE_SHARED(index->need_mb, 1); |
| 190 | pthread_kill(index->tid, SIGRCU); |
| 191 | } |
| 192 | /* |
| 193 | * Wait for sighandler (and thus mb()) to execute on every thread. |
| 194 | * |
| 195 | * Note that the pthread_kill() will never be executed on systems |
| 196 | * that correctly deliver signals in a timely manner. However, it |
| 197 | * is not uncommon for kernels to have bugs that can result in |
| 198 | * lost or unduly delayed signals. |
| 199 | * |
| 200 | * If you are seeing the below pthread_kill() executing much at |
| 201 | * all, we suggest testing the underlying kernel and filing the |
| 202 | * relevant bug report. For Linux kernels, we recommend getting |
| 203 | * the Linux Test Project (LTP). |
| 204 | */ |
| 205 | cds_list_for_each_entry(index, ®istry, node) { |
| 206 | while (CMM_LOAD_SHARED(index->need_mb)) { |
| 207 | pthread_kill(index->tid, SIGRCU); |
| 208 | poll(NULL, 0, 1); |
| 209 | } |
| 210 | } |
| 211 | cmm_smp_mb(); /* read ->need_mb before ending the barrier */ |
| 212 | } |
| 213 | |
| 214 | static void smp_mb_master(int group) |
| 215 | { |
| 216 | force_mb_all_readers(); |
| 217 | } |
| 218 | #endif /* #ifdef RCU_SIGNAL */ |
| 219 | |
| 220 | /* |
| 221 | * synchronize_rcu() waiting. Single thread. |
| 222 | */ |
| 223 | static void wait_gp(void) |
| 224 | { |
| 225 | /* Read reader_gp before read futex */ |
| 226 | smp_mb_master(RCU_MB_GROUP); |
| 227 | if (uatomic_read(&gp_futex) != -1) |
| 228 | return; |
| 229 | while (futex_async(&gp_futex, FUTEX_WAIT, -1, |
| 230 | NULL, NULL, 0)) { |
| 231 | switch (errno) { |
| 232 | case EWOULDBLOCK: |
| 233 | /* Value already changed. */ |
| 234 | return; |
| 235 | case EINTR: |
| 236 | /* Retry if interrupted by signal. */ |
| 237 | break; /* Get out of switch. */ |
| 238 | default: |
| 239 | /* Unexpected error. */ |
| 240 | urcu_die(errno); |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | /* |
| 246 | * Always called with rcu_registry lock held. Releases this lock between |
| 247 | * iterations and grabs it again. Holds the lock when it returns. |
| 248 | */ |
| 249 | void update_counter_and_wait(void) |
| 250 | { |
| 251 | CDS_LIST_HEAD(qsreaders); |
| 252 | unsigned int wait_loops = 0; |
| 253 | struct rcu_reader *index, *tmp; |
| 254 | #ifdef HAS_INCOHERENT_CACHES |
| 255 | unsigned int wait_gp_loops = 0; |
| 256 | #endif /* HAS_INCOHERENT_CACHES */ |
| 257 | |
| 258 | /* Switch parity: 0 -> 1, 1 -> 0 */ |
| 259 | CMM_STORE_SHARED(rcu_gp_ctr, rcu_gp_ctr ^ RCU_GP_CTR_PHASE); |
| 260 | |
| 261 | /* |
| 262 | * Must commit rcu_gp_ctr update to memory before waiting for quiescent |
| 263 | * state. Failure to do so could result in the writer waiting forever |
| 264 | * while new readers are always accessing data (no progress). Enforce |
| 265 | * compiler-order of store to rcu_gp_ctr before load rcu_reader ctr. |
| 266 | */ |
| 267 | cmm_barrier(); |
| 268 | |
| 269 | /* |
| 270 | * |
| 271 | * Adding a cmm_smp_mb() which is _not_ formally required, but makes the |
| 272 | * model easier to understand. It does not have a big performance impact |
| 273 | * anyway, given this is the write-side. |
| 274 | */ |
| 275 | cmm_smp_mb(); |
| 276 | |
| 277 | /* |
| 278 | * Wait for each thread URCU_TLS(rcu_reader).ctr count to become 0. |
| 279 | */ |
| 280 | for (;;) { |
| 281 | if (wait_loops < RCU_QS_ACTIVE_ATTEMPTS) |
| 282 | wait_loops++; |
| 283 | if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) { |
| 284 | uatomic_dec(&gp_futex); |
| 285 | /* Write futex before read reader_gp */ |
| 286 | smp_mb_master(RCU_MB_GROUP); |
| 287 | } |
| 288 | |
| 289 | cds_list_for_each_entry_safe(index, tmp, ®istry, node) { |
| 290 | if (!rcu_gp_ongoing(&index->ctr)) |
| 291 | cds_list_move(&index->node, &qsreaders); |
| 292 | } |
| 293 | |
| 294 | #ifndef HAS_INCOHERENT_CACHES |
| 295 | if (cds_list_empty(®istry)) { |
| 296 | if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) { |
| 297 | /* Read reader_gp before write futex */ |
| 298 | smp_mb_master(RCU_MB_GROUP); |
| 299 | uatomic_set(&gp_futex, 0); |
| 300 | } |
| 301 | break; |
| 302 | } else { |
| 303 | /* Temporarily unlock the registry lock. */ |
| 304 | mutex_unlock(&rcu_registry_lock); |
| 305 | if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) |
| 306 | wait_gp(); |
| 307 | else |
| 308 | caa_cpu_relax(); |
| 309 | /* Re-lock the registry lock before the next loop. */ |
| 310 | mutex_lock(&rcu_registry_lock); |
| 311 | } |
| 312 | #else /* #ifndef HAS_INCOHERENT_CACHES */ |
| 313 | /* |
| 314 | * BUSY-LOOP. Force the reader thread to commit its |
| 315 | * URCU_TLS(rcu_reader).ctr update to memory if we wait |
| 316 | * for too long. |
| 317 | */ |
| 318 | if (cds_list_empty(®istry)) { |
| 319 | if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) { |
| 320 | /* Read reader_gp before write futex */ |
| 321 | smp_mb_master(RCU_MB_GROUP); |
| 322 | uatomic_set(&gp_futex, 0); |
| 323 | } |
| 324 | break; |
| 325 | } else { |
| 326 | if (wait_gp_loops == KICK_READER_LOOPS) { |
| 327 | smp_mb_master(RCU_MB_GROUP); |
| 328 | wait_gp_loops = 0; |
| 329 | } |
| 330 | /* Temporarily unlock the registry lock. */ |
| 331 | mutex_unlock(&rcu_registry_lock); |
| 332 | if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) { |
| 333 | wait_gp(); |
| 334 | wait_gp_loops++; |
| 335 | } else { |
| 336 | caa_cpu_relax(); |
| 337 | } |
| 338 | /* Re-lock the registry lock before the next loop. */ |
| 339 | mutex_lock(&rcu_registry_lock); |
| 340 | } |
| 341 | #endif /* #else #ifndef HAS_INCOHERENT_CACHES */ |
| 342 | } |
| 343 | /* put back the reader list in the registry */ |
| 344 | cds_list_splice(&qsreaders, ®istry); |
| 345 | } |
| 346 | |
| 347 | void synchronize_rcu(void) |
| 348 | { |
| 349 | mutex_lock(&rcu_gp_lock); |
| 350 | mutex_lock(&rcu_registry_lock); |
| 351 | |
| 352 | if (cds_list_empty(®istry)) |
| 353 | goto out; |
| 354 | |
| 355 | /* |
| 356 | * All threads should read qparity before accessing data structure |
| 357 | * where new ptr points to. Must be done within rcu_registry_lock |
| 358 | * because it iterates on reader threads. |
| 359 | */ |
| 360 | /* Write new ptr before changing the qparity */ |
| 361 | smp_mb_master(RCU_MB_GROUP); |
| 362 | |
| 363 | /* |
| 364 | * Wait for previous parity to be empty of readers. |
| 365 | * update_counter_and_wait() can release and grab again |
| 366 | * rcu_registry_lock interally. |
| 367 | */ |
| 368 | update_counter_and_wait(); /* 0 -> 1, wait readers in parity 0 */ |
| 369 | |
| 370 | /* |
| 371 | * Must finish waiting for quiescent state for parity 0 before |
| 372 | * committing next rcu_gp_ctr update to memory. Failure to do so could |
| 373 | * result in the writer waiting forever while new readers are always |
| 374 | * accessing data (no progress). Enforce compiler-order of load |
| 375 | * URCU_TLS(rcu_reader).ctr before store to rcu_gp_ctr. |
| 376 | */ |
| 377 | cmm_barrier(); |
| 378 | |
| 379 | /* |
| 380 | * Adding a cmm_smp_mb() which is _not_ formally required, but makes the |
| 381 | * model easier to understand. It does not have a big performance impact |
| 382 | * anyway, given this is the write-side. |
| 383 | */ |
| 384 | cmm_smp_mb(); |
| 385 | |
| 386 | /* |
| 387 | * Wait for previous parity to be empty of readers. |
| 388 | * update_counter_and_wait() can release and grab again |
| 389 | * rcu_registry_lock interally. |
| 390 | */ |
| 391 | update_counter_and_wait(); /* 1 -> 0, wait readers in parity 1 */ |
| 392 | |
| 393 | /* |
| 394 | * Finish waiting for reader threads before letting the old ptr |
| 395 | * being freed. Must be done within rcu_registry_lock because it |
| 396 | * iterates on reader threads. |
| 397 | */ |
| 398 | smp_mb_master(RCU_MB_GROUP); |
| 399 | out: |
| 400 | mutex_unlock(&rcu_registry_lock); |
| 401 | mutex_unlock(&rcu_gp_lock); |
| 402 | } |
| 403 | |
| 404 | /* |
| 405 | * library wrappers to be used by non-LGPL compatible source code. |
| 406 | */ |
| 407 | |
| 408 | void rcu_read_lock(void) |
| 409 | { |
| 410 | _rcu_read_lock(); |
| 411 | } |
| 412 | |
| 413 | void rcu_read_unlock(void) |
| 414 | { |
| 415 | _rcu_read_unlock(); |
| 416 | } |
| 417 | |
| 418 | void rcu_register_thread(void) |
| 419 | { |
| 420 | URCU_TLS(rcu_reader).tid = pthread_self(); |
| 421 | assert(URCU_TLS(rcu_reader).need_mb == 0); |
| 422 | assert(!(URCU_TLS(rcu_reader).ctr & RCU_GP_CTR_NEST_MASK)); |
| 423 | |
| 424 | mutex_lock(&rcu_registry_lock); |
| 425 | rcu_init(); /* In case gcc does not support constructor attribute */ |
| 426 | cds_list_add(&URCU_TLS(rcu_reader).node, ®istry); |
| 427 | mutex_unlock(&rcu_registry_lock); |
| 428 | } |
| 429 | |
| 430 | void rcu_unregister_thread(void) |
| 431 | { |
| 432 | mutex_lock(&rcu_registry_lock); |
| 433 | cds_list_del(&URCU_TLS(rcu_reader).node); |
| 434 | mutex_unlock(&rcu_registry_lock); |
| 435 | } |
| 436 | |
| 437 | #ifdef RCU_MEMBARRIER |
| 438 | void rcu_init(void) |
| 439 | { |
| 440 | if (init_done) |
| 441 | return; |
| 442 | init_done = 1; |
| 443 | if (!membarrier(MEMBARRIER_EXPEDITED | MEMBARRIER_QUERY)) |
| 444 | has_sys_membarrier = 1; |
| 445 | } |
| 446 | #endif |
| 447 | |
| 448 | #ifdef RCU_SIGNAL |
| 449 | static void sigrcu_handler(int signo, siginfo_t *siginfo, void *context) |
| 450 | { |
| 451 | /* |
| 452 | * Executing this cmm_smp_mb() is the only purpose of this signal handler. |
| 453 | * It punctually promotes cmm_barrier() into cmm_smp_mb() on every thread it is |
| 454 | * executed on. |
| 455 | */ |
| 456 | cmm_smp_mb(); |
| 457 | _CMM_STORE_SHARED(URCU_TLS(rcu_reader).need_mb, 0); |
| 458 | cmm_smp_mb(); |
| 459 | } |
| 460 | |
| 461 | /* |
| 462 | * rcu_init constructor. Called when the library is linked, but also when |
| 463 | * reader threads are calling rcu_register_thread(). |
| 464 | * Should only be called by a single thread at a given time. This is ensured by |
| 465 | * holing the rcu_registry_lock from rcu_register_thread() or by running |
| 466 | * at library load time, which should not be executed by multiple |
| 467 | * threads nor concurrently with rcu_register_thread() anyway. |
| 468 | */ |
| 469 | void rcu_init(void) |
| 470 | { |
| 471 | struct sigaction act; |
| 472 | int ret; |
| 473 | |
| 474 | if (init_done) |
| 475 | return; |
| 476 | init_done = 1; |
| 477 | |
| 478 | act.sa_sigaction = sigrcu_handler; |
| 479 | act.sa_flags = SA_SIGINFO | SA_RESTART; |
| 480 | sigemptyset(&act.sa_mask); |
| 481 | ret = sigaction(SIGRCU, &act, NULL); |
| 482 | if (ret) |
| 483 | urcu_die(errno); |
| 484 | } |
| 485 | |
| 486 | void rcu_exit(void) |
| 487 | { |
| 488 | struct sigaction act; |
| 489 | int ret; |
| 490 | |
| 491 | ret = sigaction(SIGRCU, NULL, &act); |
| 492 | if (ret) |
| 493 | urcu_die(errno); |
| 494 | assert(act.sa_sigaction == sigrcu_handler); |
| 495 | assert(cds_list_empty(®istry)); |
| 496 | } |
| 497 | |
| 498 | #endif /* #ifdef RCU_SIGNAL */ |
| 499 | |
| 500 | DEFINE_RCU_FLAVOR(rcu_flavor); |
| 501 | |
| 502 | #include "urcu-call-rcu-impl.h" |
| 503 | #include "urcu-defer-impl.h" |