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