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 * RCU_MEMBARRIER is only possibly available on Linux.
101 #include <urcu/syscall-compat.h>
104 /* If the headers do not support SYS_membarrier, fall back on RCU_MB */
105 #ifdef SYS_membarrier
106 # define membarrier(...) syscall(SYS_membarrier, __VA_ARGS__)
108 # define membarrier(...) -ENOSYS
111 enum membarrier_cmd
{
112 MEMBARRIER_CMD_QUERY
= 0,
113 MEMBARRIER_CMD_SHARED
= (1 << 0),
117 void __attribute__((constructor
)) rcu_bp_init(void);
119 void __attribute__((destructor
)) rcu_bp_exit(void);
121 int urcu_bp_has_sys_membarrier
;
124 * rcu_gp_lock ensures mutual exclusion between threads calling
127 static pthread_mutex_t rcu_gp_lock
= PTHREAD_MUTEX_INITIALIZER
;
129 * rcu_registry_lock ensures mutual exclusion between threads
130 * registering and unregistering themselves to/from the registry, and
131 * with threads reading that registry from synchronize_rcu(). However,
132 * this lock is not held all the way through the completion of awaiting
133 * for the grace period. It is sporadically released between iterations
135 * rcu_registry_lock may nest inside rcu_gp_lock.
137 static pthread_mutex_t rcu_registry_lock
= PTHREAD_MUTEX_INITIALIZER
;
139 static pthread_mutex_t init_lock
= PTHREAD_MUTEX_INITIALIZER
;
140 static int initialized
;
142 static pthread_key_t urcu_bp_key
;
144 struct rcu_gp rcu_gp
= { .ctr
= RCU_GP_COUNT
};
147 * Pointer to registry elements. Written to only by each individual reader. Read
148 * by both the reader and the writers.
150 DEFINE_URCU_TLS(struct rcu_reader
*, rcu_reader
);
152 static CDS_LIST_HEAD(registry
);
154 struct registry_chunk
{
155 size_t data_len
; /* data length */
156 size_t used
; /* amount of data used */
157 struct cds_list_head node
; /* chunk_list node */
161 struct registry_arena
{
162 struct cds_list_head chunk_list
;
165 static struct registry_arena registry_arena
= {
166 .chunk_list
= CDS_LIST_HEAD_INIT(registry_arena
.chunk_list
),
169 /* Saved fork signal mask, protected by rcu_gp_lock */
170 static sigset_t saved_fork_signal_mask
;
172 static void mutex_lock(pthread_mutex_t
*mutex
)
176 #ifndef DISTRUST_SIGNALS_EXTREME
177 ret
= pthread_mutex_lock(mutex
);
180 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
181 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
182 if (ret
!= EBUSY
&& ret
!= EINTR
)
186 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
189 static void mutex_unlock(pthread_mutex_t
*mutex
)
193 ret
= pthread_mutex_unlock(mutex
);
198 static void smp_mb_master(void)
200 if (caa_likely(urcu_bp_has_sys_membarrier
))
201 (void) membarrier(MEMBARRIER_CMD_SHARED
, 0);
207 * Always called with rcu_registry lock held. Releases this lock between
208 * iterations and grabs it again. Holds the lock when it returns.
210 static void wait_for_readers(struct cds_list_head
*input_readers
,
211 struct cds_list_head
*cur_snap_readers
,
212 struct cds_list_head
*qsreaders
)
214 unsigned int wait_loops
= 0;
215 struct rcu_reader
*index
, *tmp
;
218 * Wait for each thread URCU_TLS(rcu_reader).ctr to either
219 * indicate quiescence (not nested), or observe the current
223 if (wait_loops
< RCU_QS_ACTIVE_ATTEMPTS
)
226 cds_list_for_each_entry_safe(index
, tmp
, input_readers
, node
) {
227 switch (rcu_reader_state(&index
->ctr
)) {
228 case RCU_READER_ACTIVE_CURRENT
:
229 if (cur_snap_readers
) {
230 cds_list_move(&index
->node
,
235 case RCU_READER_INACTIVE
:
236 cds_list_move(&index
->node
, qsreaders
);
238 case RCU_READER_ACTIVE_OLD
:
240 * Old snapshot. Leaving node in
241 * input_readers will make us busy-loop
242 * until the snapshot becomes current or
243 * the reader becomes inactive.
249 if (cds_list_empty(input_readers
)) {
252 /* Temporarily unlock the registry lock. */
253 mutex_unlock(&rcu_registry_lock
);
254 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
)
255 (void) poll(NULL
, 0, RCU_SLEEP_DELAY_MS
);
258 /* Re-lock the registry lock before the next loop. */
259 mutex_lock(&rcu_registry_lock
);
264 void synchronize_rcu(void)
266 CDS_LIST_HEAD(cur_snap_readers
);
267 CDS_LIST_HEAD(qsreaders
);
268 sigset_t newmask
, oldmask
;
271 ret
= sigfillset(&newmask
);
273 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
276 mutex_lock(&rcu_gp_lock
);
278 mutex_lock(&rcu_registry_lock
);
280 if (cds_list_empty(®istry
))
283 /* All threads should read qparity before accessing data structure
284 * where new ptr points to. */
285 /* Write new ptr before changing the qparity */
289 * Wait for readers to observe original parity or be quiescent.
290 * wait_for_readers() can release and grab again rcu_registry_lock
293 wait_for_readers(®istry
, &cur_snap_readers
, &qsreaders
);
296 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
297 * model easier to understand. It does not have a big performance impact
298 * anyway, given this is the write-side.
302 /* Switch parity: 0 -> 1, 1 -> 0 */
303 CMM_STORE_SHARED(rcu_gp
.ctr
, rcu_gp
.ctr
^ RCU_GP_CTR_PHASE
);
306 * Must commit qparity update to memory before waiting for other parity
307 * quiescent state. Failure to do so could result in the writer waiting
308 * forever while new readers are always accessing data (no progress).
309 * Ensured by CMM_STORE_SHARED and CMM_LOAD_SHARED.
313 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
314 * model easier to understand. It does not have a big performance impact
315 * anyway, given this is the write-side.
320 * Wait for readers to observe new parity or be quiescent.
321 * wait_for_readers() can release and grab again rcu_registry_lock
324 wait_for_readers(&cur_snap_readers
, NULL
, &qsreaders
);
327 * Put quiescent reader list back into registry.
329 cds_list_splice(&qsreaders
, ®istry
);
332 * Finish waiting for reader threads before letting the old ptr being
337 mutex_unlock(&rcu_registry_lock
);
338 mutex_unlock(&rcu_gp_lock
);
339 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
344 * library wrappers to be used by non-LGPL compatible source code.
347 void rcu_read_lock(void)
352 void rcu_read_unlock(void)
357 int rcu_read_ongoing(void)
359 return _rcu_read_ongoing();
363 * Only grow for now. If empty, allocate a ARENA_INIT_ALLOC sized chunk.
364 * Else, try expanding the last chunk. If this fails, allocate a new
365 * chunk twice as big as the last chunk.
366 * Memory used by chunks _never_ moves. A chunk could theoretically be
367 * freed when all "used" slots are released, but we don't do it at this
371 void expand_arena(struct registry_arena
*arena
)
373 struct registry_chunk
*new_chunk
, *last_chunk
;
374 size_t old_chunk_len
, new_chunk_len
;
377 if (cds_list_empty(&arena
->chunk_list
)) {
378 assert(ARENA_INIT_ALLOC
>=
379 sizeof(struct registry_chunk
)
380 + sizeof(struct rcu_reader
));
381 new_chunk_len
= ARENA_INIT_ALLOC
;
382 new_chunk
= mmap(NULL
, new_chunk_len
,
383 PROT_READ
| PROT_WRITE
,
384 MAP_ANONYMOUS
| MAP_PRIVATE
,
386 if (new_chunk
== MAP_FAILED
)
388 bzero(new_chunk
, new_chunk_len
);
389 new_chunk
->data_len
=
390 new_chunk_len
- sizeof(struct registry_chunk
);
391 cds_list_add_tail(&new_chunk
->node
, &arena
->chunk_list
);
392 return; /* We're done. */
395 /* Try expanding last chunk. */
396 last_chunk
= cds_list_entry(arena
->chunk_list
.prev
,
397 struct registry_chunk
, node
);
399 last_chunk
->data_len
+ sizeof(struct registry_chunk
);
400 new_chunk_len
= old_chunk_len
<< 1;
402 /* Don't allow memory mapping to move, just expand. */
403 new_chunk
= mremap_wrapper(last_chunk
, old_chunk_len
,
405 if (new_chunk
!= MAP_FAILED
) {
406 /* Should not have moved. */
407 assert(new_chunk
== last_chunk
);
408 bzero((char *) last_chunk
+ old_chunk_len
,
409 new_chunk_len
- old_chunk_len
);
410 last_chunk
->data_len
=
411 new_chunk_len
- sizeof(struct registry_chunk
);
412 return; /* We're done. */
415 /* Remap did not succeed, we need to add a new chunk. */
416 new_chunk
= mmap(NULL
, new_chunk_len
,
417 PROT_READ
| PROT_WRITE
,
418 MAP_ANONYMOUS
| MAP_PRIVATE
,
420 if (new_chunk
== MAP_FAILED
)
422 bzero(new_chunk
, new_chunk_len
);
423 new_chunk
->data_len
=
424 new_chunk_len
- sizeof(struct registry_chunk
);
425 cds_list_add_tail(&new_chunk
->node
, &arena
->chunk_list
);
429 struct rcu_reader
*arena_alloc(struct registry_arena
*arena
)
431 struct registry_chunk
*chunk
;
432 struct rcu_reader
*rcu_reader_reg
;
433 int expand_done
= 0; /* Only allow to expand once per alloc */
434 size_t len
= sizeof(struct rcu_reader
);
437 cds_list_for_each_entry(chunk
, &arena
->chunk_list
, node
) {
438 if (chunk
->data_len
- chunk
->used
< len
)
441 for (rcu_reader_reg
= (struct rcu_reader
*) &chunk
->data
[0];
442 rcu_reader_reg
< (struct rcu_reader
*) &chunk
->data
[chunk
->data_len
];
444 if (!rcu_reader_reg
->alloc
) {
445 rcu_reader_reg
->alloc
= 1;
447 return rcu_reader_reg
;
461 /* Called with signals off and mutex locked */
463 void add_thread(void)
465 struct rcu_reader
*rcu_reader_reg
;
468 rcu_reader_reg
= arena_alloc(®istry_arena
);
471 ret
= pthread_setspecific(urcu_bp_key
, rcu_reader_reg
);
475 /* Add to registry */
476 rcu_reader_reg
->tid
= pthread_self();
477 assert(rcu_reader_reg
->ctr
== 0);
478 cds_list_add(&rcu_reader_reg
->node
, ®istry
);
480 * Reader threads are pointing to the reader registry. This is
481 * why its memory should never be relocated.
483 URCU_TLS(rcu_reader
) = rcu_reader_reg
;
486 /* Called with mutex locked */
488 void cleanup_thread(struct registry_chunk
*chunk
,
489 struct rcu_reader
*rcu_reader_reg
)
491 rcu_reader_reg
->ctr
= 0;
492 cds_list_del(&rcu_reader_reg
->node
);
493 rcu_reader_reg
->tid
= 0;
494 rcu_reader_reg
->alloc
= 0;
495 chunk
->used
-= sizeof(struct rcu_reader
);
499 struct registry_chunk
*find_chunk(struct rcu_reader
*rcu_reader_reg
)
501 struct registry_chunk
*chunk
;
503 cds_list_for_each_entry(chunk
, ®istry_arena
.chunk_list
, node
) {
504 if (rcu_reader_reg
< (struct rcu_reader
*) &chunk
->data
[0])
506 if (rcu_reader_reg
>= (struct rcu_reader
*) &chunk
->data
[chunk
->data_len
])
513 /* Called with signals off and mutex locked */
515 void remove_thread(struct rcu_reader
*rcu_reader_reg
)
517 cleanup_thread(find_chunk(rcu_reader_reg
), rcu_reader_reg
);
518 URCU_TLS(rcu_reader
) = NULL
;
521 /* Disable signals, take mutex, add to registry */
522 void rcu_bp_register(void)
524 sigset_t newmask
, oldmask
;
527 ret
= sigfillset(&newmask
);
530 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
535 * Check if a signal concurrently registered our thread since
536 * the check in rcu_read_lock().
538 if (URCU_TLS(rcu_reader
))
542 * Take care of early registration before urcu_bp constructor.
546 mutex_lock(&rcu_registry_lock
);
548 mutex_unlock(&rcu_registry_lock
);
550 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
555 /* Disable signals, take mutex, remove from registry */
557 void rcu_bp_unregister(struct rcu_reader
*rcu_reader_reg
)
559 sigset_t newmask
, oldmask
;
562 ret
= sigfillset(&newmask
);
565 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
569 mutex_lock(&rcu_registry_lock
);
570 remove_thread(rcu_reader_reg
);
571 mutex_unlock(&rcu_registry_lock
);
572 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
579 * Remove thread from the registry when it exits, and flag it as
580 * destroyed so garbage collection can take care of it.
583 void urcu_bp_thread_exit_notifier(void *rcu_key
)
585 rcu_bp_unregister(rcu_key
);
589 void rcu_bp_init(void)
591 mutex_lock(&init_lock
);
592 if (!rcu_bp_refcount
++) {
595 ret
= pthread_key_create(&urcu_bp_key
,
596 urcu_bp_thread_exit_notifier
);
599 ret
= membarrier(MEMBARRIER_CMD_QUERY
, 0);
600 if (ret
>= 0 && (ret
& MEMBARRIER_CMD_SHARED
)) {
601 urcu_bp_has_sys_membarrier
= 1;
605 mutex_unlock(&init_lock
);
609 void rcu_bp_exit(void)
611 mutex_lock(&init_lock
);
612 if (!--rcu_bp_refcount
) {
613 struct registry_chunk
*chunk
, *tmp
;
616 cds_list_for_each_entry_safe(chunk
, tmp
,
617 ®istry_arena
.chunk_list
, node
) {
618 munmap(chunk
, chunk
->data_len
619 + sizeof(struct registry_chunk
));
621 ret
= pthread_key_delete(urcu_bp_key
);
625 mutex_unlock(&init_lock
);
629 * Holding the rcu_gp_lock and rcu_registry_lock across fork will make
630 * sure we fork() don't race with a concurrent thread executing with
631 * any of those locks held. This ensures that the registry and data
632 * protected by rcu_gp_lock are in a coherent state in the child.
634 void rcu_bp_before_fork(void)
636 sigset_t newmask
, oldmask
;
639 ret
= sigfillset(&newmask
);
641 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
643 mutex_lock(&rcu_gp_lock
);
644 mutex_lock(&rcu_registry_lock
);
645 saved_fork_signal_mask
= oldmask
;
648 void rcu_bp_after_fork_parent(void)
653 oldmask
= saved_fork_signal_mask
;
654 mutex_unlock(&rcu_registry_lock
);
655 mutex_unlock(&rcu_gp_lock
);
656 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
661 * Prune all entries from registry except our own thread. Fits the Linux
662 * fork behavior. Called with rcu_gp_lock and rcu_registry_lock held.
665 void urcu_bp_prune_registry(void)
667 struct registry_chunk
*chunk
;
668 struct rcu_reader
*rcu_reader_reg
;
670 cds_list_for_each_entry(chunk
, ®istry_arena
.chunk_list
, node
) {
671 for (rcu_reader_reg
= (struct rcu_reader
*) &chunk
->data
[0];
672 rcu_reader_reg
< (struct rcu_reader
*) &chunk
->data
[chunk
->data_len
];
674 if (!rcu_reader_reg
->alloc
)
676 if (rcu_reader_reg
->tid
== pthread_self())
678 cleanup_thread(chunk
, rcu_reader_reg
);
683 void rcu_bp_after_fork_child(void)
688 urcu_bp_prune_registry();
689 oldmask
= saved_fork_signal_mask
;
690 mutex_unlock(&rcu_registry_lock
);
691 mutex_unlock(&rcu_gp_lock
);
692 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
696 void *rcu_dereference_sym_bp(void *p
)
698 return _rcu_dereference(p
);
701 void *rcu_set_pointer_sym_bp(void **p
, void *v
)
708 void *rcu_xchg_pointer_sym_bp(void **p
, void *v
)
711 return uatomic_xchg(p
, v
);
714 void *rcu_cmpxchg_pointer_sym_bp(void **p
, void *old
, void *_new
)
717 return uatomic_cmpxchg(p
, old
, _new
);
720 DEFINE_RCU_FLAVOR(rcu_flavor
);
722 #include "urcu-call-rcu-impl.h"
723 #include "urcu-defer-impl.h"