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