| 1 | #ifndef _URCU_DEFER_IMPL_H |
| 2 | #define _URCU_DEFER_IMPL_H |
| 3 | |
| 4 | /* |
| 5 | * urcu-defer-impl.h |
| 6 | * |
| 7 | * Userspace RCU header - memory reclamation. |
| 8 | * |
| 9 | * TO BE INCLUDED ONLY FROM URCU LIBRARY CODE. See urcu-defer.h for linking |
| 10 | * dynamically with the userspace rcu reclamation library. |
| 11 | * |
| 12 | * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 13 | * Copyright (c) 2009 Paul E. McKenney, IBM Corporation. |
| 14 | * |
| 15 | * This library is free software; you can redistribute it and/or |
| 16 | * modify it under the terms of the GNU Lesser General Public |
| 17 | * License as published by the Free Software Foundation; either |
| 18 | * version 2.1 of the License, or (at your option) any later version. |
| 19 | * |
| 20 | * This library is distributed in the hope that it will be useful, |
| 21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 23 | * Lesser General Public License for more details. |
| 24 | * |
| 25 | * You should have received a copy of the GNU Lesser General Public |
| 26 | * License along with this library; if not, write to the Free Software |
| 27 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 28 | * |
| 29 | * IBM's contributions to this file may be relicensed under LGPLv2 or later. |
| 30 | */ |
| 31 | |
| 32 | #include <stdlib.h> |
| 33 | #include <pthread.h> |
| 34 | #include <stdio.h> |
| 35 | #include <signal.h> |
| 36 | #include <string.h> |
| 37 | #include <errno.h> |
| 38 | #include <poll.h> |
| 39 | #include <sys/time.h> |
| 40 | #include <unistd.h> |
| 41 | #include <stdint.h> |
| 42 | |
| 43 | #include "urcu/futex.h" |
| 44 | |
| 45 | #include <urcu/assert.h> |
| 46 | #include <urcu/compiler.h> |
| 47 | #include <urcu/arch.h> |
| 48 | #include <urcu/uatomic.h> |
| 49 | #include <urcu/list.h> |
| 50 | #include <urcu/system.h> |
| 51 | #include <urcu/tls-compat.h> |
| 52 | #include "urcu-die.h" |
| 53 | #include "urcu-utils.h" |
| 54 | |
| 55 | /* |
| 56 | * Number of entries in the per-thread defer queue. Must be power of 2. |
| 57 | */ |
| 58 | #define DEFER_QUEUE_SIZE (1 << 12) |
| 59 | #define DEFER_QUEUE_MASK (DEFER_QUEUE_SIZE - 1) |
| 60 | |
| 61 | /* |
| 62 | * Typically, data is aligned at least on the architecture size. |
| 63 | * Use lowest bit to indicate that the current callback is changing. |
| 64 | * Assumes that (void *)-2L is not used often. Used to encode non-aligned |
| 65 | * functions and non-aligned data using extra space. |
| 66 | * We encode the (void *)-2L fct as: -2L, fct, data. |
| 67 | * We encode the (void *)-2L data as either: |
| 68 | * fct | DQ_FCT_BIT, data (if fct is aligned), or |
| 69 | * -2L, fct, data (if fct is not aligned). |
| 70 | * Here, DQ_FCT_MARK == ~DQ_FCT_BIT. Required for the test order. |
| 71 | */ |
| 72 | #define DQ_FCT_BIT (1 << 0) |
| 73 | #define DQ_IS_FCT_BIT(x) ((unsigned long)(x) & DQ_FCT_BIT) |
| 74 | #define DQ_SET_FCT_BIT(x) \ |
| 75 | (x = (void *)((unsigned long)(x) | DQ_FCT_BIT)) |
| 76 | #define DQ_CLEAR_FCT_BIT(x) \ |
| 77 | (x = (void *)((unsigned long)(x) & ~DQ_FCT_BIT)) |
| 78 | #define DQ_FCT_MARK ((void *)(~DQ_FCT_BIT)) |
| 79 | |
| 80 | /* |
| 81 | * This code section can only be included in LGPL 2.1 compatible source code. |
| 82 | * See below for the function call wrappers which can be used in code meant to |
| 83 | * be only linked with the Userspace RCU library. This comes with a small |
| 84 | * performance degradation on the read-side due to the added function calls. |
| 85 | * This is required to permit relinking with newer versions of the library. |
| 86 | */ |
| 87 | |
| 88 | /* |
| 89 | * defer queue. |
| 90 | * Contains pointers. Encoded to save space when same callback is often used. |
| 91 | * When looking up the next item: |
| 92 | * - if DQ_FCT_BIT is set, set the current callback to DQ_CLEAR_FCT_BIT(ptr) |
| 93 | * - next element contains pointer to data. |
| 94 | * - else if item == DQ_FCT_MARK |
| 95 | * - set the current callback to next element ptr |
| 96 | * - following next element contains pointer to data. |
| 97 | * - else current element contains data |
| 98 | */ |
| 99 | struct defer_queue { |
| 100 | unsigned long head; /* add element at head */ |
| 101 | void *last_fct_in; /* last fct pointer encoded */ |
| 102 | unsigned long tail; /* next element to remove at tail */ |
| 103 | void *last_fct_out; /* last fct pointer encoded */ |
| 104 | void **q; |
| 105 | /* registry information */ |
| 106 | unsigned long last_head; |
| 107 | struct cds_list_head list; /* list of thread queues */ |
| 108 | }; |
| 109 | |
| 110 | /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */ |
| 111 | #include <urcu/defer.h> |
| 112 | |
| 113 | void __attribute__((destructor)) rcu_defer_exit(void); |
| 114 | |
| 115 | extern void synchronize_rcu(void); |
| 116 | |
| 117 | /* |
| 118 | * rcu_defer_mutex nests inside defer_thread_mutex. |
| 119 | */ |
| 120 | static pthread_mutex_t rcu_defer_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 121 | static pthread_mutex_t defer_thread_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 122 | |
| 123 | static int32_t defer_thread_futex; |
| 124 | static int32_t defer_thread_stop; |
| 125 | |
| 126 | /* |
| 127 | * Written to only by each individual deferer. Read by both the deferer and |
| 128 | * the reclamation tread. |
| 129 | */ |
| 130 | static DEFINE_URCU_TLS(struct defer_queue, defer_queue); |
| 131 | static CDS_LIST_HEAD(registry_defer); |
| 132 | static pthread_t tid_defer; |
| 133 | |
| 134 | static void mutex_lock_defer(pthread_mutex_t *mutex) |
| 135 | { |
| 136 | int ret; |
| 137 | |
| 138 | #ifndef DISTRUST_SIGNALS_EXTREME |
| 139 | ret = pthread_mutex_lock(mutex); |
| 140 | if (ret) |
| 141 | urcu_die(ret); |
| 142 | #else /* #ifndef DISTRUST_SIGNALS_EXTREME */ |
| 143 | while ((ret = pthread_mutex_trylock(mutex)) != 0) { |
| 144 | if (ret != EBUSY && ret != EINTR) |
| 145 | urcu_die(ret); |
| 146 | (void) poll(NULL,0,10); |
| 147 | } |
| 148 | #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */ |
| 149 | } |
| 150 | |
| 151 | /* |
| 152 | * Wake-up any waiting defer thread. Called from many concurrent threads. |
| 153 | */ |
| 154 | static void wake_up_defer(void) |
| 155 | { |
| 156 | if (caa_unlikely(uatomic_read(&defer_thread_futex) == -1)) { |
| 157 | uatomic_set(&defer_thread_futex, 0); |
| 158 | if (futex_noasync(&defer_thread_futex, FUTEX_WAKE, 1, |
| 159 | NULL, NULL, 0) < 0) |
| 160 | urcu_die(errno); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | static unsigned long rcu_defer_num_callbacks(void) |
| 165 | { |
| 166 | unsigned long num_items = 0, head; |
| 167 | struct defer_queue *index; |
| 168 | |
| 169 | mutex_lock_defer(&rcu_defer_mutex); |
| 170 | cds_list_for_each_entry(index, ®istry_defer, list) { |
| 171 | head = CMM_LOAD_SHARED(index->head); |
| 172 | num_items += head - index->tail; |
| 173 | } |
| 174 | mutex_unlock(&rcu_defer_mutex); |
| 175 | return num_items; |
| 176 | } |
| 177 | |
| 178 | /* |
| 179 | * Defer thread waiting. Single thread. |
| 180 | */ |
| 181 | static void wait_defer(void) |
| 182 | { |
| 183 | uatomic_dec(&defer_thread_futex); |
| 184 | /* Write futex before read queue */ |
| 185 | /* Write futex before read defer_thread_stop */ |
| 186 | cmm_smp_mb(); |
| 187 | if (_CMM_LOAD_SHARED(defer_thread_stop)) { |
| 188 | uatomic_set(&defer_thread_futex, 0); |
| 189 | pthread_exit(0); |
| 190 | } |
| 191 | if (rcu_defer_num_callbacks()) { |
| 192 | cmm_smp_mb(); /* Read queue before write futex */ |
| 193 | /* Callbacks are queued, don't wait. */ |
| 194 | uatomic_set(&defer_thread_futex, 0); |
| 195 | } else { |
| 196 | cmm_smp_rmb(); /* Read queue before read futex */ |
| 197 | if (uatomic_read(&defer_thread_futex) != -1) |
| 198 | return; |
| 199 | while (futex_noasync(&defer_thread_futex, FUTEX_WAIT, -1, |
| 200 | NULL, NULL, 0)) { |
| 201 | switch (errno) { |
| 202 | case EWOULDBLOCK: |
| 203 | /* Value already changed. */ |
| 204 | return; |
| 205 | case EINTR: |
| 206 | /* Retry if interrupted by signal. */ |
| 207 | break; /* Get out of switch. */ |
| 208 | default: |
| 209 | /* Unexpected error. */ |
| 210 | urcu_die(errno); |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | /* |
| 217 | * Must be called after Q.S. is reached. |
| 218 | */ |
| 219 | static void rcu_defer_barrier_queue(struct defer_queue *queue, |
| 220 | unsigned long head) |
| 221 | { |
| 222 | unsigned long i; |
| 223 | void (*fct)(void *p); |
| 224 | void *p; |
| 225 | |
| 226 | /* |
| 227 | * Tail is only modified when lock is held. |
| 228 | * Head is only modified by owner thread. |
| 229 | */ |
| 230 | |
| 231 | for (i = queue->tail; i != head;) { |
| 232 | cmm_smp_rmb(); /* read head before q[]. */ |
| 233 | p = CMM_LOAD_SHARED(queue->q[i++ & DEFER_QUEUE_MASK]); |
| 234 | if (caa_unlikely(DQ_IS_FCT_BIT(p))) { |
| 235 | DQ_CLEAR_FCT_BIT(p); |
| 236 | queue->last_fct_out = p; |
| 237 | p = CMM_LOAD_SHARED(queue->q[i++ & DEFER_QUEUE_MASK]); |
| 238 | } else if (caa_unlikely(p == DQ_FCT_MARK)) { |
| 239 | p = CMM_LOAD_SHARED(queue->q[i++ & DEFER_QUEUE_MASK]); |
| 240 | queue->last_fct_out = p; |
| 241 | p = CMM_LOAD_SHARED(queue->q[i++ & DEFER_QUEUE_MASK]); |
| 242 | } |
| 243 | fct = queue->last_fct_out; |
| 244 | fct(p); |
| 245 | } |
| 246 | cmm_smp_mb(); /* push tail after having used q[] */ |
| 247 | CMM_STORE_SHARED(queue->tail, i); |
| 248 | } |
| 249 | |
| 250 | static void _rcu_defer_barrier_thread(void) |
| 251 | { |
| 252 | unsigned long head, num_items; |
| 253 | |
| 254 | head = URCU_TLS(defer_queue).head; |
| 255 | num_items = head - URCU_TLS(defer_queue).tail; |
| 256 | if (caa_unlikely(!num_items)) |
| 257 | return; |
| 258 | synchronize_rcu(); |
| 259 | rcu_defer_barrier_queue(&URCU_TLS(defer_queue), head); |
| 260 | } |
| 261 | |
| 262 | void rcu_defer_barrier_thread(void) |
| 263 | { |
| 264 | mutex_lock_defer(&rcu_defer_mutex); |
| 265 | _rcu_defer_barrier_thread(); |
| 266 | mutex_unlock(&rcu_defer_mutex); |
| 267 | } |
| 268 | |
| 269 | /* |
| 270 | * rcu_defer_barrier - Execute all queued rcu callbacks. |
| 271 | * |
| 272 | * Execute all RCU callbacks queued before rcu_defer_barrier() execution. |
| 273 | * All callbacks queued on the local thread prior to a rcu_defer_barrier() call |
| 274 | * are guaranteed to be executed. |
| 275 | * Callbacks queued by other threads concurrently with rcu_defer_barrier() |
| 276 | * execution are not guaranteed to be executed in the current batch (could |
| 277 | * be left for the next batch). These callbacks queued by other threads are only |
| 278 | * guaranteed to be executed if there is explicit synchronization between |
| 279 | * the thread adding to the queue and the thread issuing the defer_barrier call. |
| 280 | */ |
| 281 | |
| 282 | void rcu_defer_barrier(void) |
| 283 | { |
| 284 | struct defer_queue *index; |
| 285 | unsigned long num_items = 0; |
| 286 | |
| 287 | if (cds_list_empty(®istry_defer)) |
| 288 | return; |
| 289 | |
| 290 | mutex_lock_defer(&rcu_defer_mutex); |
| 291 | cds_list_for_each_entry(index, ®istry_defer, list) { |
| 292 | index->last_head = CMM_LOAD_SHARED(index->head); |
| 293 | num_items += index->last_head - index->tail; |
| 294 | } |
| 295 | if (caa_likely(!num_items)) { |
| 296 | /* |
| 297 | * We skip the grace period because there are no queued |
| 298 | * callbacks to execute. |
| 299 | */ |
| 300 | goto end; |
| 301 | } |
| 302 | synchronize_rcu(); |
| 303 | cds_list_for_each_entry(index, ®istry_defer, list) |
| 304 | rcu_defer_barrier_queue(index, index->last_head); |
| 305 | end: |
| 306 | mutex_unlock(&rcu_defer_mutex); |
| 307 | } |
| 308 | |
| 309 | /* |
| 310 | * _defer_rcu - Queue a RCU callback. |
| 311 | */ |
| 312 | static void _defer_rcu(void (*fct)(void *p), void *p) |
| 313 | { |
| 314 | unsigned long head, tail; |
| 315 | |
| 316 | /* |
| 317 | * Head is only modified by ourself. Tail can be modified by reclamation |
| 318 | * thread. |
| 319 | */ |
| 320 | head = URCU_TLS(defer_queue).head; |
| 321 | tail = CMM_LOAD_SHARED(URCU_TLS(defer_queue).tail); |
| 322 | |
| 323 | /* |
| 324 | * If queue is full, or reached threshold. Empty queue ourself. |
| 325 | * Worse-case: must allow 2 supplementary entries for fct pointer. |
| 326 | */ |
| 327 | if (caa_unlikely(head - tail >= DEFER_QUEUE_SIZE - 2)) { |
| 328 | urcu_posix_assert(head - tail <= DEFER_QUEUE_SIZE); |
| 329 | rcu_defer_barrier_thread(); |
| 330 | urcu_posix_assert(head - CMM_LOAD_SHARED(URCU_TLS(defer_queue).tail) == 0); |
| 331 | } |
| 332 | |
| 333 | /* |
| 334 | * Encode: |
| 335 | * if the function is not changed and the data is aligned and it is |
| 336 | * not the marker: |
| 337 | * store the data |
| 338 | * otherwise if the function is aligned and its not the marker: |
| 339 | * store the function with DQ_FCT_BIT |
| 340 | * store the data |
| 341 | * otherwise: |
| 342 | * store the marker (DQ_FCT_MARK) |
| 343 | * store the function |
| 344 | * store the data |
| 345 | * |
| 346 | * Decode: see the comments before 'struct defer_queue' |
| 347 | * or the code in rcu_defer_barrier_queue(). |
| 348 | */ |
| 349 | if (caa_unlikely(URCU_TLS(defer_queue).last_fct_in != fct |
| 350 | || DQ_IS_FCT_BIT(p) |
| 351 | || p == DQ_FCT_MARK)) { |
| 352 | URCU_TLS(defer_queue).last_fct_in = fct; |
| 353 | if (caa_unlikely(DQ_IS_FCT_BIT(fct) || fct == DQ_FCT_MARK)) { |
| 354 | _CMM_STORE_SHARED(URCU_TLS(defer_queue).q[head++ & DEFER_QUEUE_MASK], |
| 355 | DQ_FCT_MARK); |
| 356 | _CMM_STORE_SHARED(URCU_TLS(defer_queue).q[head++ & DEFER_QUEUE_MASK], |
| 357 | fct); |
| 358 | } else { |
| 359 | DQ_SET_FCT_BIT(fct); |
| 360 | _CMM_STORE_SHARED(URCU_TLS(defer_queue).q[head++ & DEFER_QUEUE_MASK], |
| 361 | fct); |
| 362 | } |
| 363 | } |
| 364 | _CMM_STORE_SHARED(URCU_TLS(defer_queue).q[head++ & DEFER_QUEUE_MASK], p); |
| 365 | cmm_smp_wmb(); /* Publish new pointer before head */ |
| 366 | /* Write q[] before head. */ |
| 367 | CMM_STORE_SHARED(URCU_TLS(defer_queue).head, head); |
| 368 | cmm_smp_mb(); /* Write queue head before read futex */ |
| 369 | /* |
| 370 | * Wake-up any waiting defer thread. |
| 371 | */ |
| 372 | wake_up_defer(); |
| 373 | } |
| 374 | |
| 375 | static void *thr_defer(void *args __attribute__((unused))) |
| 376 | { |
| 377 | for (;;) { |
| 378 | /* |
| 379 | * "Be green". Don't wake up the CPU if there is no RCU work |
| 380 | * to perform whatsoever. Aims at saving laptop battery life by |
| 381 | * leaving the processor in sleep state when idle. |
| 382 | */ |
| 383 | wait_defer(); |
| 384 | /* Sleeping after wait_defer to let many callbacks enqueue */ |
| 385 | (void) poll(NULL,0,100); /* wait for 100ms */ |
| 386 | rcu_defer_barrier(); |
| 387 | } |
| 388 | |
| 389 | return NULL; |
| 390 | } |
| 391 | |
| 392 | /* |
| 393 | * library wrappers to be used by non-LGPL compatible source code. |
| 394 | */ |
| 395 | |
| 396 | void defer_rcu(void (*fct)(void *p), void *p) |
| 397 | { |
| 398 | _defer_rcu(fct, p); |
| 399 | } |
| 400 | |
| 401 | static void start_defer_thread(void) |
| 402 | { |
| 403 | int ret; |
| 404 | |
| 405 | ret = pthread_create(&tid_defer, NULL, thr_defer, NULL); |
| 406 | urcu_posix_assert(!ret); |
| 407 | } |
| 408 | |
| 409 | static void stop_defer_thread(void) |
| 410 | { |
| 411 | int ret; |
| 412 | void *tret; |
| 413 | |
| 414 | _CMM_STORE_SHARED(defer_thread_stop, 1); |
| 415 | /* Store defer_thread_stop before testing futex */ |
| 416 | cmm_smp_mb(); |
| 417 | wake_up_defer(); |
| 418 | |
| 419 | ret = pthread_join(tid_defer, &tret); |
| 420 | urcu_posix_assert(!ret); |
| 421 | |
| 422 | CMM_STORE_SHARED(defer_thread_stop, 0); |
| 423 | /* defer thread should always exit when futex value is 0 */ |
| 424 | urcu_posix_assert(uatomic_read(&defer_thread_futex) == 0); |
| 425 | } |
| 426 | |
| 427 | int rcu_defer_register_thread(void) |
| 428 | { |
| 429 | int was_empty; |
| 430 | |
| 431 | urcu_posix_assert(URCU_TLS(defer_queue).last_head == 0); |
| 432 | urcu_posix_assert(URCU_TLS(defer_queue).q == NULL); |
| 433 | URCU_TLS(defer_queue).q = malloc(sizeof(void *) * DEFER_QUEUE_SIZE); |
| 434 | if (!URCU_TLS(defer_queue).q) |
| 435 | return -ENOMEM; |
| 436 | |
| 437 | mutex_lock_defer(&defer_thread_mutex); |
| 438 | mutex_lock_defer(&rcu_defer_mutex); |
| 439 | was_empty = cds_list_empty(®istry_defer); |
| 440 | cds_list_add(&URCU_TLS(defer_queue).list, ®istry_defer); |
| 441 | mutex_unlock(&rcu_defer_mutex); |
| 442 | |
| 443 | if (was_empty) |
| 444 | start_defer_thread(); |
| 445 | mutex_unlock(&defer_thread_mutex); |
| 446 | return 0; |
| 447 | } |
| 448 | |
| 449 | void rcu_defer_unregister_thread(void) |
| 450 | { |
| 451 | int is_empty; |
| 452 | |
| 453 | mutex_lock_defer(&defer_thread_mutex); |
| 454 | mutex_lock_defer(&rcu_defer_mutex); |
| 455 | cds_list_del(&URCU_TLS(defer_queue).list); |
| 456 | _rcu_defer_barrier_thread(); |
| 457 | free(URCU_TLS(defer_queue).q); |
| 458 | URCU_TLS(defer_queue).q = NULL; |
| 459 | is_empty = cds_list_empty(®istry_defer); |
| 460 | mutex_unlock(&rcu_defer_mutex); |
| 461 | |
| 462 | if (is_empty) |
| 463 | stop_defer_thread(); |
| 464 | mutex_unlock(&defer_thread_mutex); |
| 465 | } |
| 466 | |
| 467 | void rcu_defer_exit(void) |
| 468 | { |
| 469 | urcu_posix_assert(cds_list_empty(®istry_defer)); |
| 470 | } |
| 471 | |
| 472 | #endif /* _URCU_DEFER_IMPL_H */ |