| 1 | // SPDX-FileCopyrightText: 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 2 | // SPDX-FileCopyrightText: 2009 Paul E. McKenney, IBM Corporation. |
| 3 | // |
| 4 | // SPDX-License-Identifier: LGPL-2.1-or-later |
| 5 | |
| 6 | /* |
| 7 | * Userspace RCU library, "bulletproof" version. |
| 8 | * |
| 9 | * IBM's contributions to this file may be relicensed under LGPLv2 or later. |
| 10 | */ |
| 11 | |
| 12 | #define URCU_NO_COMPAT_IDENTIFIERS |
| 13 | #define _LGPL_SOURCE |
| 14 | #include <stdio.h> |
| 15 | #include <pthread.h> |
| 16 | #include <signal.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | #include <errno.h> |
| 20 | #include <poll.h> |
| 21 | #include <unistd.h> |
| 22 | #include <stdbool.h> |
| 23 | #include <sys/mman.h> |
| 24 | |
| 25 | #include <urcu/assert.h> |
| 26 | #include <urcu/config.h> |
| 27 | #include <urcu/arch.h> |
| 28 | #include <urcu/wfcqueue.h> |
| 29 | #include <urcu/map/urcu-bp.h> |
| 30 | #include <urcu/static/urcu-bp.h> |
| 31 | #include <urcu/pointer.h> |
| 32 | #include <urcu/tls-compat.h> |
| 33 | |
| 34 | #include "urcu-die.h" |
| 35 | #include "urcu-utils.h" |
| 36 | |
| 37 | #define URCU_API_MAP |
| 38 | /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */ |
| 39 | #undef _LGPL_SOURCE |
| 40 | #include <urcu/urcu-bp.h> |
| 41 | #define _LGPL_SOURCE |
| 42 | |
| 43 | #ifndef MAP_ANONYMOUS |
| 44 | #define MAP_ANONYMOUS MAP_ANON |
| 45 | #endif |
| 46 | |
| 47 | #ifdef __linux__ |
| 48 | static |
| 49 | void *mremap_wrapper(void *old_address, size_t old_size, |
| 50 | size_t new_size, int flags) |
| 51 | { |
| 52 | return mremap(old_address, old_size, new_size, flags); |
| 53 | } |
| 54 | #else |
| 55 | |
| 56 | #define MREMAP_MAYMOVE 1 |
| 57 | #define MREMAP_FIXED 2 |
| 58 | |
| 59 | /* |
| 60 | * mremap wrapper for non-Linux systems not allowing MAYMOVE. |
| 61 | * This is not generic. |
| 62 | */ |
| 63 | static |
| 64 | void *mremap_wrapper(void *old_address __attribute__((unused)), |
| 65 | size_t old_size __attribute__((unused)), |
| 66 | size_t new_size __attribute__((unused)), |
| 67 | int flags) |
| 68 | { |
| 69 | urcu_posix_assert(!(flags & MREMAP_MAYMOVE)); |
| 70 | |
| 71 | return MAP_FAILED; |
| 72 | } |
| 73 | #endif |
| 74 | |
| 75 | /* Sleep delay in ms */ |
| 76 | #define RCU_SLEEP_DELAY_MS 10 |
| 77 | #define INIT_NR_THREADS 8 |
| 78 | #define ARENA_INIT_ALLOC \ |
| 79 | sizeof(struct registry_chunk) \ |
| 80 | + INIT_NR_THREADS * sizeof(struct urcu_bp_reader) |
| 81 | |
| 82 | /* |
| 83 | * Active attempts to check for reader Q.S. before calling sleep(). |
| 84 | */ |
| 85 | #define RCU_QS_ACTIVE_ATTEMPTS 100 |
| 86 | |
| 87 | static |
| 88 | int urcu_bp_refcount; |
| 89 | |
| 90 | /* If the headers do not support membarrier system call, fall back smp_mb. */ |
| 91 | #ifdef __NR_membarrier |
| 92 | # define membarrier(...) syscall(__NR_membarrier, __VA_ARGS__) |
| 93 | #else |
| 94 | # define membarrier(...) -ENOSYS |
| 95 | #endif |
| 96 | |
| 97 | enum membarrier_cmd { |
| 98 | MEMBARRIER_CMD_QUERY = 0, |
| 99 | MEMBARRIER_CMD_SHARED = (1 << 0), |
| 100 | /* reserved for MEMBARRIER_CMD_SHARED_EXPEDITED (1 << 1) */ |
| 101 | /* reserved for MEMBARRIER_CMD_PRIVATE (1 << 2) */ |
| 102 | MEMBARRIER_CMD_PRIVATE_EXPEDITED = (1 << 3), |
| 103 | MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED = (1 << 4), |
| 104 | }; |
| 105 | |
| 106 | static |
| 107 | void __attribute__((constructor)) _urcu_bp_init(void); |
| 108 | static |
| 109 | void urcu_bp_exit(void); |
| 110 | static |
| 111 | void __attribute__((destructor)) urcu_bp_exit_destructor(void); |
| 112 | static void urcu_call_rcu_exit(void); |
| 113 | |
| 114 | #ifndef CONFIG_RCU_FORCE_SYS_MEMBARRIER |
| 115 | int urcu_bp_has_sys_membarrier; |
| 116 | #endif |
| 117 | |
| 118 | /* |
| 119 | * rcu_gp_lock ensures mutual exclusion between threads calling |
| 120 | * synchronize_rcu(). |
| 121 | */ |
| 122 | static pthread_mutex_t rcu_gp_lock = PTHREAD_MUTEX_INITIALIZER; |
| 123 | /* |
| 124 | * rcu_registry_lock ensures mutual exclusion between threads |
| 125 | * registering and unregistering themselves to/from the registry, and |
| 126 | * with threads reading that registry from synchronize_rcu(). However, |
| 127 | * this lock is not held all the way through the completion of awaiting |
| 128 | * for the grace period. It is sporadically released between iterations |
| 129 | * on the registry. |
| 130 | * rcu_registry_lock may nest inside rcu_gp_lock. |
| 131 | */ |
| 132 | static pthread_mutex_t rcu_registry_lock = PTHREAD_MUTEX_INITIALIZER; |
| 133 | |
| 134 | static pthread_mutex_t init_lock = PTHREAD_MUTEX_INITIALIZER; |
| 135 | static int initialized; |
| 136 | |
| 137 | static pthread_key_t urcu_bp_key; |
| 138 | |
| 139 | struct urcu_bp_gp urcu_bp_gp = { .ctr = URCU_BP_GP_COUNT }; |
| 140 | |
| 141 | /* |
| 142 | * Pointer to registry elements. Written to only by each individual reader. Read |
| 143 | * by both the reader and the writers. |
| 144 | */ |
| 145 | DEFINE_URCU_TLS(struct urcu_bp_reader *, urcu_bp_reader); |
| 146 | |
| 147 | static CDS_LIST_HEAD(registry); |
| 148 | |
| 149 | struct registry_chunk { |
| 150 | size_t data_len; /* data length */ |
| 151 | size_t used; /* amount of data used */ |
| 152 | struct cds_list_head node; /* chunk_list node */ |
| 153 | char data[]; |
| 154 | }; |
| 155 | |
| 156 | struct registry_arena { |
| 157 | struct cds_list_head chunk_list; |
| 158 | }; |
| 159 | |
| 160 | static struct registry_arena registry_arena = { |
| 161 | .chunk_list = CDS_LIST_HEAD_INIT(registry_arena.chunk_list), |
| 162 | }; |
| 163 | |
| 164 | /* Saved fork signal mask, protected by rcu_gp_lock */ |
| 165 | static sigset_t saved_fork_signal_mask; |
| 166 | |
| 167 | static void mutex_lock(pthread_mutex_t *mutex) |
| 168 | { |
| 169 | int ret; |
| 170 | |
| 171 | #ifndef DISTRUST_SIGNALS_EXTREME |
| 172 | ret = pthread_mutex_lock(mutex); |
| 173 | if (ret) |
| 174 | urcu_die(ret); |
| 175 | #else /* #ifndef DISTRUST_SIGNALS_EXTREME */ |
| 176 | while ((ret = pthread_mutex_trylock(mutex)) != 0) { |
| 177 | if (ret != EBUSY && ret != EINTR) |
| 178 | urcu_die(ret); |
| 179 | poll(NULL,0,10); |
| 180 | } |
| 181 | #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */ |
| 182 | } |
| 183 | |
| 184 | static void mutex_unlock(pthread_mutex_t *mutex) |
| 185 | { |
| 186 | int ret; |
| 187 | |
| 188 | ret = pthread_mutex_unlock(mutex); |
| 189 | if (ret) |
| 190 | urcu_die(ret); |
| 191 | } |
| 192 | |
| 193 | static void smp_mb_master(void) |
| 194 | { |
| 195 | if (caa_likely(urcu_bp_has_sys_membarrier)) { |
| 196 | if (membarrier(MEMBARRIER_CMD_PRIVATE_EXPEDITED, 0)) |
| 197 | urcu_die(errno); |
| 198 | } else { |
| 199 | cmm_smp_mb(); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | /* |
| 204 | * Always called with rcu_registry lock held. Releases this lock between |
| 205 | * iterations and grabs it again. Holds the lock when it returns. |
| 206 | */ |
| 207 | static void wait_for_readers(struct cds_list_head *input_readers, |
| 208 | struct cds_list_head *cur_snap_readers, |
| 209 | struct cds_list_head *qsreaders) |
| 210 | { |
| 211 | unsigned int wait_loops = 0; |
| 212 | struct urcu_bp_reader *index, *tmp; |
| 213 | |
| 214 | /* |
| 215 | * Wait for each thread URCU_TLS(urcu_bp_reader).ctr to either |
| 216 | * indicate quiescence (not nested), or observe the current |
| 217 | * rcu_gp.ctr value. |
| 218 | */ |
| 219 | for (;;) { |
| 220 | if (wait_loops < RCU_QS_ACTIVE_ATTEMPTS) |
| 221 | wait_loops++; |
| 222 | |
| 223 | cds_list_for_each_entry_safe(index, tmp, input_readers, node) { |
| 224 | switch (urcu_bp_reader_state(&index->ctr)) { |
| 225 | case URCU_BP_READER_ACTIVE_CURRENT: |
| 226 | if (cur_snap_readers) { |
| 227 | cds_list_move(&index->node, |
| 228 | cur_snap_readers); |
| 229 | break; |
| 230 | } |
| 231 | /* Fall-through */ |
| 232 | case URCU_BP_READER_INACTIVE: |
| 233 | cds_list_move(&index->node, qsreaders); |
| 234 | break; |
| 235 | case URCU_BP_READER_ACTIVE_OLD: |
| 236 | /* |
| 237 | * Old snapshot. Leaving node in |
| 238 | * input_readers will make us busy-loop |
| 239 | * until the snapshot becomes current or |
| 240 | * the reader becomes inactive. |
| 241 | */ |
| 242 | break; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | if (cds_list_empty(input_readers)) { |
| 247 | break; |
| 248 | } else { |
| 249 | /* Temporarily unlock the registry lock. */ |
| 250 | mutex_unlock(&rcu_registry_lock); |
| 251 | if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) |
| 252 | (void) poll(NULL, 0, RCU_SLEEP_DELAY_MS); |
| 253 | else |
| 254 | caa_cpu_relax(); |
| 255 | /* Re-lock the registry lock before the next loop. */ |
| 256 | mutex_lock(&rcu_registry_lock); |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | void urcu_bp_synchronize_rcu(void) |
| 262 | { |
| 263 | CDS_LIST_HEAD(cur_snap_readers); |
| 264 | CDS_LIST_HEAD(qsreaders); |
| 265 | sigset_t newmask, oldmask; |
| 266 | int ret; |
| 267 | |
| 268 | ret = sigfillset(&newmask); |
| 269 | urcu_posix_assert(!ret); |
| 270 | ret = pthread_sigmask(SIG_BLOCK, &newmask, &oldmask); |
| 271 | urcu_posix_assert(!ret); |
| 272 | |
| 273 | mutex_lock(&rcu_gp_lock); |
| 274 | |
| 275 | mutex_lock(&rcu_registry_lock); |
| 276 | |
| 277 | if (cds_list_empty(®istry)) |
| 278 | goto out; |
| 279 | |
| 280 | /* All threads should read qparity before accessing data structure |
| 281 | * where new ptr points to. */ |
| 282 | /* Write new ptr before changing the qparity */ |
| 283 | smp_mb_master(); |
| 284 | |
| 285 | /* |
| 286 | * Wait for readers to observe original parity or be quiescent. |
| 287 | * wait_for_readers() can release and grab again rcu_registry_lock |
| 288 | * internally. |
| 289 | */ |
| 290 | wait_for_readers(®istry, &cur_snap_readers, &qsreaders); |
| 291 | |
| 292 | /* |
| 293 | * Adding a cmm_smp_mb() which is _not_ formally required, but makes the |
| 294 | * model easier to understand. It does not have a big performance impact |
| 295 | * anyway, given this is the write-side. |
| 296 | */ |
| 297 | cmm_smp_mb(); |
| 298 | |
| 299 | /* Switch parity: 0 -> 1, 1 -> 0 */ |
| 300 | CMM_STORE_SHARED(rcu_gp.ctr, rcu_gp.ctr ^ URCU_BP_GP_CTR_PHASE); |
| 301 | |
| 302 | /* |
| 303 | * Must commit qparity update to memory before waiting for other parity |
| 304 | * quiescent state. Failure to do so could result in the writer waiting |
| 305 | * forever while new readers are always accessing data (no progress). |
| 306 | * Ensured by CMM_STORE_SHARED and CMM_LOAD_SHARED. |
| 307 | */ |
| 308 | |
| 309 | /* |
| 310 | * Adding a cmm_smp_mb() which is _not_ formally required, but makes the |
| 311 | * model easier to understand. It does not have a big performance impact |
| 312 | * anyway, given this is the write-side. |
| 313 | */ |
| 314 | cmm_smp_mb(); |
| 315 | |
| 316 | /* |
| 317 | * Wait for readers to observe new parity or be quiescent. |
| 318 | * wait_for_readers() can release and grab again rcu_registry_lock |
| 319 | * internally. |
| 320 | */ |
| 321 | wait_for_readers(&cur_snap_readers, NULL, &qsreaders); |
| 322 | |
| 323 | /* |
| 324 | * Put quiescent reader list back into registry. |
| 325 | */ |
| 326 | cds_list_splice(&qsreaders, ®istry); |
| 327 | |
| 328 | /* |
| 329 | * Finish waiting for reader threads before letting the old ptr being |
| 330 | * freed. |
| 331 | */ |
| 332 | smp_mb_master(); |
| 333 | out: |
| 334 | mutex_unlock(&rcu_registry_lock); |
| 335 | mutex_unlock(&rcu_gp_lock); |
| 336 | ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL); |
| 337 | urcu_posix_assert(!ret); |
| 338 | } |
| 339 | |
| 340 | /* |
| 341 | * library wrappers to be used by non-LGPL compatible source code. |
| 342 | */ |
| 343 | |
| 344 | void urcu_bp_read_lock(void) |
| 345 | { |
| 346 | _urcu_bp_read_lock(); |
| 347 | } |
| 348 | |
| 349 | void urcu_bp_read_unlock(void) |
| 350 | { |
| 351 | _urcu_bp_read_unlock(); |
| 352 | } |
| 353 | |
| 354 | int urcu_bp_read_ongoing(void) |
| 355 | { |
| 356 | return _urcu_bp_read_ongoing(); |
| 357 | } |
| 358 | |
| 359 | /* |
| 360 | * Only grow for now. If empty, allocate a ARENA_INIT_ALLOC sized chunk. |
| 361 | * Else, try expanding the last chunk. If this fails, allocate a new |
| 362 | * chunk twice as big as the last chunk. |
| 363 | * Memory used by chunks _never_ moves. A chunk could theoretically be |
| 364 | * freed when all "used" slots are released, but we don't do it at this |
| 365 | * point. |
| 366 | */ |
| 367 | static |
| 368 | void expand_arena(struct registry_arena *arena) |
| 369 | { |
| 370 | struct registry_chunk *new_chunk, *last_chunk; |
| 371 | size_t old_chunk_len, new_chunk_len; |
| 372 | |
| 373 | /* No chunk. */ |
| 374 | if (cds_list_empty(&arena->chunk_list)) { |
| 375 | urcu_posix_assert(ARENA_INIT_ALLOC >= |
| 376 | sizeof(struct registry_chunk) |
| 377 | + sizeof(struct rcu_reader)); |
| 378 | new_chunk_len = ARENA_INIT_ALLOC; |
| 379 | new_chunk = (struct registry_chunk *) mmap(NULL, |
| 380 | new_chunk_len, |
| 381 | PROT_READ | PROT_WRITE, |
| 382 | MAP_ANONYMOUS | MAP_PRIVATE, |
| 383 | -1, 0); |
| 384 | if (new_chunk == MAP_FAILED) |
| 385 | abort(); |
| 386 | memset(new_chunk, 0, new_chunk_len); |
| 387 | new_chunk->data_len = |
| 388 | new_chunk_len - sizeof(struct registry_chunk); |
| 389 | cds_list_add_tail(&new_chunk->node, &arena->chunk_list); |
| 390 | return; /* We're done. */ |
| 391 | } |
| 392 | |
| 393 | /* Try expanding last chunk. */ |
| 394 | last_chunk = cds_list_entry(arena->chunk_list.prev, |
| 395 | struct registry_chunk, node); |
| 396 | old_chunk_len = |
| 397 | last_chunk->data_len + sizeof(struct registry_chunk); |
| 398 | new_chunk_len = old_chunk_len << 1; |
| 399 | |
| 400 | /* Don't allow memory mapping to move, just expand. */ |
| 401 | new_chunk = mremap_wrapper(last_chunk, old_chunk_len, |
| 402 | new_chunk_len, 0); |
| 403 | if (new_chunk != MAP_FAILED) { |
| 404 | /* Should not have moved. */ |
| 405 | urcu_posix_assert(new_chunk == last_chunk); |
| 406 | memset((char *) last_chunk + old_chunk_len, 0, |
| 407 | new_chunk_len - old_chunk_len); |
| 408 | last_chunk->data_len = |
| 409 | new_chunk_len - sizeof(struct registry_chunk); |
| 410 | return; /* We're done. */ |
| 411 | } |
| 412 | |
| 413 | /* Remap did not succeed, we need to add a new chunk. */ |
| 414 | new_chunk = (struct registry_chunk *) mmap(NULL, |
| 415 | new_chunk_len, |
| 416 | PROT_READ | PROT_WRITE, |
| 417 | MAP_ANONYMOUS | MAP_PRIVATE, |
| 418 | -1, 0); |
| 419 | if (new_chunk == MAP_FAILED) |
| 420 | abort(); |
| 421 | memset(new_chunk, 0, new_chunk_len); |
| 422 | new_chunk->data_len = |
| 423 | new_chunk_len - sizeof(struct registry_chunk); |
| 424 | cds_list_add_tail(&new_chunk->node, &arena->chunk_list); |
| 425 | } |
| 426 | |
| 427 | static |
| 428 | struct rcu_reader *arena_alloc(struct registry_arena *arena) |
| 429 | { |
| 430 | struct registry_chunk *chunk; |
| 431 | struct rcu_reader *rcu_reader_reg; |
| 432 | int expand_done = 0; /* Only allow to expand once per alloc */ |
| 433 | size_t len = sizeof(struct rcu_reader); |
| 434 | |
| 435 | retry: |
| 436 | cds_list_for_each_entry(chunk, &arena->chunk_list, node) { |
| 437 | if (chunk->data_len - chunk->used < len) |
| 438 | continue; |
| 439 | /* Find spot */ |
| 440 | for (rcu_reader_reg = (struct rcu_reader *) &chunk->data[0]; |
| 441 | rcu_reader_reg < (struct rcu_reader *) &chunk->data[chunk->data_len]; |
| 442 | rcu_reader_reg++) { |
| 443 | if (!rcu_reader_reg->alloc) { |
| 444 | rcu_reader_reg->alloc = 1; |
| 445 | chunk->used += len; |
| 446 | return rcu_reader_reg; |
| 447 | } |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | if (!expand_done) { |
| 452 | expand_arena(arena); |
| 453 | expand_done = 1; |
| 454 | goto retry; |
| 455 | } |
| 456 | |
| 457 | return NULL; |
| 458 | } |
| 459 | |
| 460 | /* Called with signals off and mutex locked */ |
| 461 | static |
| 462 | void add_thread(void) |
| 463 | { |
| 464 | struct rcu_reader *rcu_reader_reg; |
| 465 | int ret; |
| 466 | |
| 467 | rcu_reader_reg = arena_alloc(®istry_arena); |
| 468 | if (!rcu_reader_reg) |
| 469 | abort(); |
| 470 | ret = pthread_setspecific(urcu_bp_key, rcu_reader_reg); |
| 471 | if (ret) |
| 472 | abort(); |
| 473 | |
| 474 | /* Add to registry */ |
| 475 | rcu_reader_reg->tid = pthread_self(); |
| 476 | urcu_posix_assert(rcu_reader_reg->ctr == 0); |
| 477 | cds_list_add(&rcu_reader_reg->node, ®istry); |
| 478 | /* |
| 479 | * Reader threads are pointing to the reader registry. This is |
| 480 | * why its memory should never be relocated. |
| 481 | */ |
| 482 | URCU_TLS(urcu_bp_reader) = rcu_reader_reg; |
| 483 | } |
| 484 | |
| 485 | /* Called with mutex locked */ |
| 486 | static |
| 487 | void cleanup_thread(struct registry_chunk *chunk, |
| 488 | struct rcu_reader *rcu_reader_reg) |
| 489 | { |
| 490 | rcu_reader_reg->ctr = 0; |
| 491 | cds_list_del(&rcu_reader_reg->node); |
| 492 | rcu_reader_reg->tid = 0; |
| 493 | rcu_reader_reg->alloc = 0; |
| 494 | chunk->used -= sizeof(struct rcu_reader); |
| 495 | } |
| 496 | |
| 497 | static |
| 498 | struct registry_chunk *find_chunk(struct rcu_reader *rcu_reader_reg) |
| 499 | { |
| 500 | struct registry_chunk *chunk; |
| 501 | |
| 502 | cds_list_for_each_entry(chunk, ®istry_arena.chunk_list, node) { |
| 503 | if (rcu_reader_reg < (struct rcu_reader *) &chunk->data[0]) |
| 504 | continue; |
| 505 | if (rcu_reader_reg >= (struct rcu_reader *) &chunk->data[chunk->data_len]) |
| 506 | continue; |
| 507 | return chunk; |
| 508 | } |
| 509 | return NULL; |
| 510 | } |
| 511 | |
| 512 | /* Called with signals off and mutex locked */ |
| 513 | static |
| 514 | void remove_thread(struct rcu_reader *rcu_reader_reg) |
| 515 | { |
| 516 | cleanup_thread(find_chunk(rcu_reader_reg), rcu_reader_reg); |
| 517 | URCU_TLS(urcu_bp_reader) = NULL; |
| 518 | } |
| 519 | |
| 520 | /* Disable signals, take mutex, add to registry */ |
| 521 | void urcu_bp_register(void) |
| 522 | { |
| 523 | sigset_t newmask, oldmask; |
| 524 | int ret; |
| 525 | |
| 526 | ret = sigfillset(&newmask); |
| 527 | if (ret) |
| 528 | abort(); |
| 529 | ret = pthread_sigmask(SIG_BLOCK, &newmask, &oldmask); |
| 530 | if (ret) |
| 531 | abort(); |
| 532 | |
| 533 | /* |
| 534 | * Check if a signal concurrently registered our thread since |
| 535 | * the check in rcu_read_lock(). |
| 536 | */ |
| 537 | if (URCU_TLS(urcu_bp_reader)) |
| 538 | goto end; |
| 539 | |
| 540 | /* |
| 541 | * Take care of early registration before urcu_bp constructor. |
| 542 | */ |
| 543 | _urcu_bp_init(); |
| 544 | |
| 545 | mutex_lock(&rcu_registry_lock); |
| 546 | add_thread(); |
| 547 | mutex_unlock(&rcu_registry_lock); |
| 548 | end: |
| 549 | ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL); |
| 550 | if (ret) |
| 551 | abort(); |
| 552 | } |
| 553 | |
| 554 | void urcu_bp_register_thread(void) |
| 555 | { |
| 556 | if (caa_unlikely(!URCU_TLS(urcu_bp_reader))) |
| 557 | urcu_bp_register(); /* If not yet registered. */ |
| 558 | } |
| 559 | |
| 560 | /* Disable signals, take mutex, remove from registry */ |
| 561 | static |
| 562 | void urcu_bp_unregister(struct rcu_reader *rcu_reader_reg) |
| 563 | { |
| 564 | sigset_t newmask, oldmask; |
| 565 | int ret; |
| 566 | |
| 567 | ret = sigfillset(&newmask); |
| 568 | if (ret) |
| 569 | abort(); |
| 570 | ret = pthread_sigmask(SIG_BLOCK, &newmask, &oldmask); |
| 571 | if (ret) |
| 572 | abort(); |
| 573 | |
| 574 | mutex_lock(&rcu_registry_lock); |
| 575 | remove_thread(rcu_reader_reg); |
| 576 | mutex_unlock(&rcu_registry_lock); |
| 577 | ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL); |
| 578 | if (ret) |
| 579 | abort(); |
| 580 | urcu_bp_exit(); |
| 581 | } |
| 582 | |
| 583 | /* |
| 584 | * Remove thread from the registry when it exits, and flag it as |
| 585 | * destroyed so garbage collection can take care of it. |
| 586 | */ |
| 587 | static |
| 588 | void urcu_bp_thread_exit_notifier(void *rcu_key) |
| 589 | { |
| 590 | urcu_bp_unregister(rcu_key); |
| 591 | } |
| 592 | |
| 593 | #ifdef CONFIG_RCU_FORCE_SYS_MEMBARRIER |
| 594 | static |
| 595 | void urcu_bp_sys_membarrier_status(bool available) |
| 596 | { |
| 597 | if (!available) |
| 598 | abort(); |
| 599 | } |
| 600 | #else |
| 601 | static |
| 602 | void urcu_bp_sys_membarrier_status(bool available) |
| 603 | { |
| 604 | if (!available) |
| 605 | return; |
| 606 | urcu_bp_has_sys_membarrier = 1; |
| 607 | } |
| 608 | #endif |
| 609 | |
| 610 | static |
| 611 | void urcu_bp_sys_membarrier_init(void) |
| 612 | { |
| 613 | bool available = false; |
| 614 | int mask; |
| 615 | |
| 616 | mask = membarrier(MEMBARRIER_CMD_QUERY, 0); |
| 617 | if (mask >= 0) { |
| 618 | if (mask & MEMBARRIER_CMD_PRIVATE_EXPEDITED) { |
| 619 | if (membarrier(MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED, 0)) |
| 620 | urcu_die(errno); |
| 621 | available = true; |
| 622 | } |
| 623 | } |
| 624 | urcu_bp_sys_membarrier_status(available); |
| 625 | } |
| 626 | |
| 627 | static |
| 628 | void _urcu_bp_init(void) |
| 629 | { |
| 630 | mutex_lock(&init_lock); |
| 631 | if (!urcu_bp_refcount++) { |
| 632 | int ret; |
| 633 | |
| 634 | ret = pthread_key_create(&urcu_bp_key, |
| 635 | urcu_bp_thread_exit_notifier); |
| 636 | if (ret) |
| 637 | abort(); |
| 638 | urcu_bp_sys_membarrier_init(); |
| 639 | initialized = 1; |
| 640 | } |
| 641 | mutex_unlock(&init_lock); |
| 642 | } |
| 643 | |
| 644 | static |
| 645 | void urcu_bp_exit(void) |
| 646 | { |
| 647 | mutex_lock(&init_lock); |
| 648 | if (!--urcu_bp_refcount) { |
| 649 | struct registry_chunk *chunk, *tmp; |
| 650 | int ret; |
| 651 | |
| 652 | cds_list_for_each_entry_safe(chunk, tmp, |
| 653 | ®istry_arena.chunk_list, node) { |
| 654 | munmap((void *) chunk, chunk->data_len |
| 655 | + sizeof(struct registry_chunk)); |
| 656 | } |
| 657 | CDS_INIT_LIST_HEAD(®istry_arena.chunk_list); |
| 658 | ret = pthread_key_delete(urcu_bp_key); |
| 659 | if (ret) |
| 660 | abort(); |
| 661 | } |
| 662 | mutex_unlock(&init_lock); |
| 663 | } |
| 664 | |
| 665 | static |
| 666 | void urcu_bp_exit_destructor(void) |
| 667 | { |
| 668 | urcu_call_rcu_exit(); |
| 669 | urcu_bp_exit(); |
| 670 | } |
| 671 | |
| 672 | /* |
| 673 | * Holding the rcu_gp_lock and rcu_registry_lock across fork will make |
| 674 | * sure we fork() don't race with a concurrent thread executing with |
| 675 | * any of those locks held. This ensures that the registry and data |
| 676 | * protected by rcu_gp_lock are in a coherent state in the child. |
| 677 | */ |
| 678 | void urcu_bp_before_fork(void) |
| 679 | { |
| 680 | sigset_t newmask, oldmask; |
| 681 | int ret; |
| 682 | |
| 683 | ret = sigfillset(&newmask); |
| 684 | urcu_posix_assert(!ret); |
| 685 | ret = pthread_sigmask(SIG_BLOCK, &newmask, &oldmask); |
| 686 | urcu_posix_assert(!ret); |
| 687 | mutex_lock(&rcu_gp_lock); |
| 688 | mutex_lock(&rcu_registry_lock); |
| 689 | saved_fork_signal_mask = oldmask; |
| 690 | } |
| 691 | |
| 692 | void urcu_bp_after_fork_parent(void) |
| 693 | { |
| 694 | sigset_t oldmask; |
| 695 | int ret; |
| 696 | |
| 697 | oldmask = saved_fork_signal_mask; |
| 698 | mutex_unlock(&rcu_registry_lock); |
| 699 | mutex_unlock(&rcu_gp_lock); |
| 700 | ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL); |
| 701 | urcu_posix_assert(!ret); |
| 702 | } |
| 703 | |
| 704 | /* |
| 705 | * Prune all entries from registry except our own thread. Fits the Linux |
| 706 | * fork behavior. Called with rcu_gp_lock and rcu_registry_lock held. |
| 707 | */ |
| 708 | static |
| 709 | void urcu_bp_prune_registry(void) |
| 710 | { |
| 711 | struct registry_chunk *chunk; |
| 712 | struct urcu_bp_reader *rcu_reader_reg; |
| 713 | |
| 714 | cds_list_for_each_entry(chunk, ®istry_arena.chunk_list, node) { |
| 715 | for (rcu_reader_reg = (struct urcu_bp_reader *) &chunk->data[0]; |
| 716 | rcu_reader_reg < (struct urcu_bp_reader *) &chunk->data[chunk->data_len]; |
| 717 | rcu_reader_reg++) { |
| 718 | if (!rcu_reader_reg->alloc) |
| 719 | continue; |
| 720 | if (rcu_reader_reg->tid == pthread_self()) |
| 721 | continue; |
| 722 | cleanup_thread(chunk, rcu_reader_reg); |
| 723 | } |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | void urcu_bp_after_fork_child(void) |
| 728 | { |
| 729 | sigset_t oldmask; |
| 730 | int ret; |
| 731 | |
| 732 | urcu_bp_prune_registry(); |
| 733 | oldmask = saved_fork_signal_mask; |
| 734 | mutex_unlock(&rcu_registry_lock); |
| 735 | mutex_unlock(&rcu_gp_lock); |
| 736 | ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL); |
| 737 | urcu_posix_assert(!ret); |
| 738 | } |
| 739 | |
| 740 | void *urcu_bp_dereference_sym(void *p) |
| 741 | { |
| 742 | return _rcu_dereference(p); |
| 743 | } |
| 744 | |
| 745 | void *urcu_bp_set_pointer_sym(void **p, void *v) |
| 746 | { |
| 747 | cmm_wmb(); |
| 748 | uatomic_set(p, v); |
| 749 | return v; |
| 750 | } |
| 751 | |
| 752 | void *urcu_bp_xchg_pointer_sym(void **p, void *v) |
| 753 | { |
| 754 | cmm_wmb(); |
| 755 | return uatomic_xchg(p, v); |
| 756 | } |
| 757 | |
| 758 | void *urcu_bp_cmpxchg_pointer_sym(void **p, void *old, void *_new) |
| 759 | { |
| 760 | cmm_wmb(); |
| 761 | return uatomic_cmpxchg(p, old, _new); |
| 762 | } |
| 763 | |
| 764 | DEFINE_RCU_FLAVOR(rcu_flavor); |
| 765 | |
| 766 | #include "urcu-call-rcu-impl.h" |
| 767 | #include "urcu-defer-impl.h" |
| 768 | #include "urcu-poll-impl.h" |