4 * Userspace RCU library, "bulletproof" version.
6 * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 * Copyright (c) 2009 Paul E. McKenney, IBM Corporation.
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.
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.
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
23 * IBM's contributions to this file may be relicensed under LGPLv2 or later.
39 #include "urcu/wfcqueue.h"
40 #include "urcu/map/urcu-bp.h"
41 #include "urcu/static/urcu-bp.h"
42 #include "urcu-pointer.h"
43 #include "urcu/tls-compat.h"
47 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
53 #define MAP_ANONYMOUS MAP_ANON
58 void *mremap_wrapper(void *old_address
, size_t old_size
,
59 size_t new_size
, int flags
)
61 return mremap(old_address
, old_size
, new_size
, flags
);
65 #define MREMAP_MAYMOVE 1
66 #define MREMAP_FIXED 2
69 * mremap wrapper for non-Linux systems not allowing MAYMOVE.
70 * This is not generic.
73 void *mremap_wrapper(void *old_address
, size_t old_size
,
74 size_t new_size
, int flags
)
76 assert(!(flags
& MREMAP_MAYMOVE
));
82 /* Sleep delay in ms */
83 #define RCU_SLEEP_DELAY_MS 10
84 #define INIT_NR_THREADS 8
85 #define ARENA_INIT_ALLOC \
86 sizeof(struct registry_chunk) \
87 + INIT_NR_THREADS * sizeof(struct rcu_reader)
90 * Active attempts to check for reader Q.S. before calling sleep().
92 #define RCU_QS_ACTIVE_ATTEMPTS 100
98 void __attribute__((constructor
)) rcu_bp_init(void);
100 void __attribute__((destructor
)) rcu_bp_exit(void);
103 * rcu_gp_lock ensures mutual exclusion between threads calling
106 static pthread_mutex_t rcu_gp_lock
= PTHREAD_MUTEX_INITIALIZER
;
108 * rcu_registry_lock ensures mutual exclusion between threads
109 * registering and unregistering themselves to/from the registry, and
110 * with threads reading that registry from synchronize_rcu(). However,
111 * this lock is not held all the way through the completion of awaiting
112 * for the grace period. It is sporadically released between iterations
114 * rcu_registry_lock may nest inside rcu_gp_lock.
116 static pthread_mutex_t rcu_registry_lock
= PTHREAD_MUTEX_INITIALIZER
;
118 static pthread_mutex_t init_lock
= PTHREAD_MUTEX_INITIALIZER
;
119 static int initialized
;
121 static pthread_key_t urcu_bp_key
;
123 struct rcu_gp rcu_gp
= { .ctr
= RCU_GP_COUNT
};
126 * Pointer to registry elements. Written to only by each individual reader. Read
127 * by both the reader and the writers.
129 DEFINE_URCU_TLS(struct rcu_reader
*, rcu_reader
);
131 static CDS_LIST_HEAD(registry
);
133 struct registry_chunk
{
134 size_t data_len
; /* data length */
135 size_t used
; /* amount of data used */
136 struct cds_list_head node
; /* chunk_list node */
140 struct registry_arena
{
141 struct cds_list_head chunk_list
;
144 static struct registry_arena registry_arena
= {
145 .chunk_list
= CDS_LIST_HEAD_INIT(registry_arena
.chunk_list
),
148 /* Saved fork signal mask, protected by rcu_gp_lock */
149 static sigset_t saved_fork_signal_mask
;
151 static void mutex_lock(pthread_mutex_t
*mutex
)
155 #ifndef DISTRUST_SIGNALS_EXTREME
156 ret
= pthread_mutex_lock(mutex
);
159 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
160 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
161 if (ret
!= EBUSY
&& ret
!= EINTR
)
165 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
168 static void mutex_unlock(pthread_mutex_t
*mutex
)
172 ret
= pthread_mutex_unlock(mutex
);
178 * Always called with rcu_registry lock held. Releases this lock between
179 * iterations and grabs it again. Holds the lock when it returns.
181 static void wait_for_readers(struct cds_list_head
*input_readers
,
182 struct cds_list_head
*cur_snap_readers
,
183 struct cds_list_head
*qsreaders
)
185 unsigned int wait_loops
= 0;
186 struct rcu_reader
*index
, *tmp
;
189 * Wait for each thread URCU_TLS(rcu_reader).ctr to either
190 * indicate quiescence (not nested), or observe the current
194 if (wait_loops
< RCU_QS_ACTIVE_ATTEMPTS
)
197 cds_list_for_each_entry_safe(index
, tmp
, input_readers
, node
) {
198 switch (rcu_reader_state(&index
->ctr
)) {
199 case RCU_READER_ACTIVE_CURRENT
:
200 if (cur_snap_readers
) {
201 cds_list_move(&index
->node
,
206 case RCU_READER_INACTIVE
:
207 cds_list_move(&index
->node
, qsreaders
);
209 case RCU_READER_ACTIVE_OLD
:
211 * Old snapshot. Leaving node in
212 * input_readers will make us busy-loop
213 * until the snapshot becomes current or
214 * the reader becomes inactive.
220 if (cds_list_empty(input_readers
)) {
223 /* Temporarily unlock the registry lock. */
224 mutex_unlock(&rcu_registry_lock
);
225 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
)
226 (void) poll(NULL
, 0, RCU_SLEEP_DELAY_MS
);
229 /* Re-lock the registry lock before the next loop. */
230 mutex_lock(&rcu_registry_lock
);
235 void synchronize_rcu(void)
237 CDS_LIST_HEAD(cur_snap_readers
);
238 CDS_LIST_HEAD(qsreaders
);
239 sigset_t newmask
, oldmask
;
242 ret
= sigfillset(&newmask
);
244 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
247 mutex_lock(&rcu_gp_lock
);
249 mutex_lock(&rcu_registry_lock
);
251 if (cds_list_empty(®istry
))
254 /* All threads should read qparity before accessing data structure
255 * where new ptr points to. */
256 /* Write new ptr before changing the qparity */
260 * Wait for readers to observe original parity or be quiescent.
261 * wait_for_readers() can release and grab again rcu_registry_lock
264 wait_for_readers(®istry
, &cur_snap_readers
, &qsreaders
);
267 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
268 * model easier to understand. It does not have a big performance impact
269 * anyway, given this is the write-side.
273 /* Switch parity: 0 -> 1, 1 -> 0 */
274 CMM_STORE_SHARED(rcu_gp
.ctr
, rcu_gp
.ctr
^ RCU_GP_CTR_PHASE
);
277 * Must commit qparity update to memory before waiting for other parity
278 * quiescent state. Failure to do so could result in the writer waiting
279 * forever while new readers are always accessing data (no progress).
280 * Ensured by CMM_STORE_SHARED and CMM_LOAD_SHARED.
284 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
285 * model easier to understand. It does not have a big performance impact
286 * anyway, given this is the write-side.
291 * Wait for readers to observe new parity or be quiescent.
292 * wait_for_readers() can release and grab again rcu_registry_lock
295 wait_for_readers(&cur_snap_readers
, NULL
, &qsreaders
);
298 * Put quiescent reader list back into registry.
300 cds_list_splice(&qsreaders
, ®istry
);
303 * Finish waiting for reader threads before letting the old ptr being
308 mutex_unlock(&rcu_registry_lock
);
309 mutex_unlock(&rcu_gp_lock
);
310 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
315 * library wrappers to be used by non-LGPL compatible source code.
318 void rcu_read_lock(void)
323 void rcu_read_unlock(void)
328 int rcu_read_ongoing(void)
330 return _rcu_read_ongoing();
334 * Only grow for now. If empty, allocate a ARENA_INIT_ALLOC sized chunk.
335 * Else, try expanding the last chunk. If this fails, allocate a new
336 * chunk twice as big as the last chunk.
337 * Memory used by chunks _never_ moves. A chunk could theoretically be
338 * freed when all "used" slots are released, but we don't do it at this
342 void expand_arena(struct registry_arena
*arena
)
344 struct registry_chunk
*new_chunk
, *last_chunk
;
345 size_t old_chunk_len
, new_chunk_len
;
348 if (cds_list_empty(&arena
->chunk_list
)) {
349 assert(ARENA_INIT_ALLOC
>=
350 sizeof(struct registry_chunk
)
351 + sizeof(struct rcu_reader
));
352 new_chunk_len
= ARENA_INIT_ALLOC
;
353 new_chunk
= mmap(NULL
, new_chunk_len
,
354 PROT_READ
| PROT_WRITE
,
355 MAP_ANONYMOUS
| MAP_PRIVATE
,
357 if (new_chunk
== MAP_FAILED
)
359 bzero(new_chunk
, new_chunk_len
);
360 new_chunk
->data_len
=
361 new_chunk_len
- sizeof(struct registry_chunk
);
362 cds_list_add_tail(&new_chunk
->node
, &arena
->chunk_list
);
363 return; /* We're done. */
366 /* Try expanding last chunk. */
367 last_chunk
= cds_list_entry(arena
->chunk_list
.prev
,
368 struct registry_chunk
, node
);
370 last_chunk
->data_len
+ sizeof(struct registry_chunk
);
371 new_chunk_len
= old_chunk_len
<< 1;
373 /* Don't allow memory mapping to move, just expand. */
374 new_chunk
= mremap_wrapper(last_chunk
, old_chunk_len
,
376 if (new_chunk
!= MAP_FAILED
) {
377 /* Should not have moved. */
378 assert(new_chunk
== last_chunk
);
379 bzero((char *) last_chunk
+ old_chunk_len
,
380 new_chunk_len
- old_chunk_len
);
381 last_chunk
->data_len
=
382 new_chunk_len
- sizeof(struct registry_chunk
);
383 return; /* We're done. */
386 /* Remap did not succeed, we need to add a new chunk. */
387 new_chunk
= mmap(NULL
, new_chunk_len
,
388 PROT_READ
| PROT_WRITE
,
389 MAP_ANONYMOUS
| MAP_PRIVATE
,
391 if (new_chunk
== MAP_FAILED
)
393 bzero(new_chunk
, new_chunk_len
);
394 new_chunk
->data_len
=
395 new_chunk_len
- sizeof(struct registry_chunk
);
396 cds_list_add_tail(&new_chunk
->node
, &arena
->chunk_list
);
400 struct rcu_reader
*arena_alloc(struct registry_arena
*arena
)
402 struct registry_chunk
*chunk
;
403 struct rcu_reader
*rcu_reader_reg
;
404 int expand_done
= 0; /* Only allow to expand once per alloc */
405 size_t len
= sizeof(struct rcu_reader
);
408 cds_list_for_each_entry(chunk
, &arena
->chunk_list
, node
) {
409 if (chunk
->data_len
- chunk
->used
< len
)
412 for (rcu_reader_reg
= (struct rcu_reader
*) &chunk
->data
[0];
413 rcu_reader_reg
< (struct rcu_reader
*) &chunk
->data
[chunk
->data_len
];
415 if (!rcu_reader_reg
->alloc
) {
416 rcu_reader_reg
->alloc
= 1;
418 return rcu_reader_reg
;
432 /* Called with signals off and mutex locked */
434 void add_thread(void)
436 struct rcu_reader
*rcu_reader_reg
;
439 rcu_reader_reg
= arena_alloc(®istry_arena
);
442 ret
= pthread_setspecific(urcu_bp_key
, rcu_reader_reg
);
446 /* Add to registry */
447 rcu_reader_reg
->tid
= pthread_self();
448 assert(rcu_reader_reg
->ctr
== 0);
449 cds_list_add(&rcu_reader_reg
->node
, ®istry
);
451 * Reader threads are pointing to the reader registry. This is
452 * why its memory should never be relocated.
454 URCU_TLS(rcu_reader
) = rcu_reader_reg
;
457 /* Called with mutex locked */
459 void cleanup_thread(struct registry_chunk
*chunk
,
460 struct rcu_reader
*rcu_reader_reg
)
462 rcu_reader_reg
->ctr
= 0;
463 cds_list_del(&rcu_reader_reg
->node
);
464 rcu_reader_reg
->tid
= 0;
465 rcu_reader_reg
->alloc
= 0;
466 chunk
->used
-= sizeof(struct rcu_reader
);
470 struct registry_chunk
*find_chunk(struct rcu_reader
*rcu_reader_reg
)
472 struct registry_chunk
*chunk
;
474 cds_list_for_each_entry(chunk
, ®istry_arena
.chunk_list
, node
) {
475 if (rcu_reader_reg
< (struct rcu_reader
*) &chunk
->data
[0])
477 if (rcu_reader_reg
>= (struct rcu_reader
*) &chunk
->data
[chunk
->data_len
])
484 /* Called with signals off and mutex locked */
486 void remove_thread(struct rcu_reader
*rcu_reader_reg
)
488 cleanup_thread(find_chunk(rcu_reader_reg
), rcu_reader_reg
);
489 URCU_TLS(rcu_reader
) = NULL
;
492 /* Disable signals, take mutex, add to registry */
493 void rcu_bp_register(void)
495 sigset_t newmask
, oldmask
;
498 ret
= sigfillset(&newmask
);
501 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
506 * Check if a signal concurrently registered our thread since
507 * the check in rcu_read_lock().
509 if (URCU_TLS(rcu_reader
))
513 * Take care of early registration before urcu_bp constructor.
517 mutex_lock(&rcu_registry_lock
);
519 mutex_unlock(&rcu_registry_lock
);
521 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
526 /* Disable signals, take mutex, remove from registry */
528 void rcu_bp_unregister(struct rcu_reader
*rcu_reader_reg
)
530 sigset_t newmask
, oldmask
;
533 ret
= sigfillset(&newmask
);
536 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
540 mutex_lock(&rcu_registry_lock
);
541 remove_thread(rcu_reader_reg
);
542 mutex_unlock(&rcu_registry_lock
);
543 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
550 * Remove thread from the registry when it exits, and flag it as
551 * destroyed so garbage collection can take care of it.
554 void urcu_bp_thread_exit_notifier(void *rcu_key
)
556 rcu_bp_unregister(rcu_key
);
560 void rcu_bp_init(void)
562 mutex_lock(&init_lock
);
563 if (!rcu_bp_refcount
++) {
566 ret
= pthread_key_create(&urcu_bp_key
,
567 urcu_bp_thread_exit_notifier
);
572 mutex_unlock(&init_lock
);
576 void rcu_bp_exit(void)
578 mutex_lock(&init_lock
);
579 if (!--rcu_bp_refcount
) {
580 struct registry_chunk
*chunk
, *tmp
;
583 cds_list_for_each_entry_safe(chunk
, tmp
,
584 ®istry_arena
.chunk_list
, node
) {
585 munmap(chunk
, chunk
->data_len
586 + sizeof(struct registry_chunk
));
588 ret
= pthread_key_delete(urcu_bp_key
);
592 mutex_unlock(&init_lock
);
596 * Holding the rcu_gp_lock and rcu_registry_lock across fork will make
597 * sure we fork() don't race with a concurrent thread executing with
598 * any of those locks held. This ensures that the registry and data
599 * protected by rcu_gp_lock are in a coherent state in the child.
601 void rcu_bp_before_fork(void)
603 sigset_t newmask
, oldmask
;
606 ret
= sigfillset(&newmask
);
608 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
610 mutex_lock(&rcu_gp_lock
);
611 mutex_lock(&rcu_registry_lock
);
612 saved_fork_signal_mask
= oldmask
;
615 void rcu_bp_after_fork_parent(void)
620 oldmask
= saved_fork_signal_mask
;
621 mutex_unlock(&rcu_registry_lock
);
622 mutex_unlock(&rcu_gp_lock
);
623 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
628 * Prune all entries from registry except our own thread. Fits the Linux
629 * fork behavior. Called with rcu_gp_lock and rcu_registry_lock held.
632 void urcu_bp_prune_registry(void)
634 struct registry_chunk
*chunk
;
635 struct rcu_reader
*rcu_reader_reg
;
637 cds_list_for_each_entry(chunk
, ®istry_arena
.chunk_list
, node
) {
638 for (rcu_reader_reg
= (struct rcu_reader
*) &chunk
->data
[0];
639 rcu_reader_reg
< (struct rcu_reader
*) &chunk
->data
[chunk
->data_len
];
641 if (!rcu_reader_reg
->alloc
)
643 if (rcu_reader_reg
->tid
== pthread_self())
645 cleanup_thread(chunk
, rcu_reader_reg
);
650 void rcu_bp_after_fork_child(void)
655 urcu_bp_prune_registry();
656 oldmask
= saved_fork_signal_mask
;
657 mutex_unlock(&rcu_registry_lock
);
658 mutex_unlock(&rcu_gp_lock
);
659 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
663 void *rcu_dereference_sym_bp(void *p
)
665 return _rcu_dereference(p
);
668 void *rcu_set_pointer_sym_bp(void **p
, void *v
)
675 void *rcu_xchg_pointer_sym_bp(void **p
, void *v
)
678 return uatomic_xchg(p
, v
);
681 void *rcu_cmpxchg_pointer_sym_bp(void **p
, void *old
, void *_new
)
684 return uatomic_cmpxchg(p
, old
, _new
);
687 DEFINE_RCU_FLAVOR(rcu_flavor
);
689 #include "urcu-call-rcu-impl.h"
690 #include "urcu-defer-impl.h"