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/arch.h"
40 #include "urcu/wfcqueue.h"
41 #include "urcu/map/urcu-bp.h"
42 #include "urcu/static/urcu-bp.h"
43 #include "urcu-pointer.h"
44 #include "urcu/tls-compat.h"
48 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
54 #define MAP_ANONYMOUS MAP_ANON
59 void *mremap_wrapper(void *old_address
, size_t old_size
,
60 size_t new_size
, int flags
)
62 return mremap(old_address
, old_size
, new_size
, flags
);
66 #define MREMAP_MAYMOVE 1
67 #define MREMAP_FIXED 2
70 * mremap wrapper for non-Linux systems not allowing MAYMOVE.
71 * This is not generic.
74 void *mremap_wrapper(void *old_address
, size_t old_size
,
75 size_t new_size
, int flags
)
77 assert(!(flags
& MREMAP_MAYMOVE
));
83 /* Sleep delay in ms */
84 #define RCU_SLEEP_DELAY_MS 10
85 #define INIT_NR_THREADS 8
86 #define ARENA_INIT_ALLOC \
87 sizeof(struct registry_chunk) \
88 + INIT_NR_THREADS * sizeof(struct rcu_reader)
91 * Active attempts to check for reader Q.S. before calling sleep().
93 #define RCU_QS_ACTIVE_ATTEMPTS 100
98 /* If the headers do not support membarrier system call, fall back smp_mb. */
99 #ifdef __NR_membarrier
100 # define membarrier(...) syscall(__NR_membarrier, __VA_ARGS__)
102 # define membarrier(...) -ENOSYS
105 enum membarrier_cmd
{
106 MEMBARRIER_CMD_QUERY
= 0,
107 MEMBARRIER_CMD_SHARED
= (1 << 0),
111 void __attribute__((constructor
)) rcu_bp_init(void);
113 void __attribute__((destructor
)) rcu_bp_exit(void);
115 int urcu_bp_has_sys_membarrier
;
118 * rcu_gp_lock ensures mutual exclusion between threads calling
121 static pthread_mutex_t rcu_gp_lock
= PTHREAD_MUTEX_INITIALIZER
;
123 * rcu_registry_lock ensures mutual exclusion between threads
124 * registering and unregistering themselves to/from the registry, and
125 * with threads reading that registry from synchronize_rcu(). However,
126 * this lock is not held all the way through the completion of awaiting
127 * for the grace period. It is sporadically released between iterations
129 * rcu_registry_lock may nest inside rcu_gp_lock.
131 static pthread_mutex_t rcu_registry_lock
= PTHREAD_MUTEX_INITIALIZER
;
133 static pthread_mutex_t init_lock
= PTHREAD_MUTEX_INITIALIZER
;
134 static int initialized
;
136 static pthread_key_t urcu_bp_key
;
138 struct rcu_gp rcu_gp
= { .ctr
= RCU_GP_COUNT
};
141 * Pointer to registry elements. Written to only by each individual reader. Read
142 * by both the reader and the writers.
144 DEFINE_URCU_TLS(struct rcu_reader
*, rcu_reader
);
146 static CDS_LIST_HEAD(registry
);
148 struct registry_chunk
{
149 size_t data_len
; /* data length */
150 size_t used
; /* amount of data used */
151 struct cds_list_head node
; /* chunk_list node */
155 struct registry_arena
{
156 struct cds_list_head chunk_list
;
159 static struct registry_arena registry_arena
= {
160 .chunk_list
= CDS_LIST_HEAD_INIT(registry_arena
.chunk_list
),
163 /* Saved fork signal mask, protected by rcu_gp_lock */
164 static sigset_t saved_fork_signal_mask
;
166 static void mutex_lock(pthread_mutex_t
*mutex
)
170 #ifndef DISTRUST_SIGNALS_EXTREME
171 ret
= pthread_mutex_lock(mutex
);
174 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
175 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
176 if (ret
!= EBUSY
&& ret
!= EINTR
)
180 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
183 static void mutex_unlock(pthread_mutex_t
*mutex
)
187 ret
= pthread_mutex_unlock(mutex
);
192 static void smp_mb_master(void)
194 if (caa_likely(urcu_bp_has_sys_membarrier
))
195 (void) membarrier(MEMBARRIER_CMD_SHARED
, 0);
201 * Always called with rcu_registry lock held. Releases this lock between
202 * iterations and grabs it again. Holds the lock when it returns.
204 static void wait_for_readers(struct cds_list_head
*input_readers
,
205 struct cds_list_head
*cur_snap_readers
,
206 struct cds_list_head
*qsreaders
)
208 unsigned int wait_loops
= 0;
209 struct rcu_reader
*index
, *tmp
;
212 * Wait for each thread URCU_TLS(rcu_reader).ctr to either
213 * indicate quiescence (not nested), or observe the current
217 if (wait_loops
< RCU_QS_ACTIVE_ATTEMPTS
)
220 cds_list_for_each_entry_safe(index
, tmp
, input_readers
, node
) {
221 switch (rcu_reader_state(&index
->ctr
)) {
222 case RCU_READER_ACTIVE_CURRENT
:
223 if (cur_snap_readers
) {
224 cds_list_move(&index
->node
,
229 case RCU_READER_INACTIVE
:
230 cds_list_move(&index
->node
, qsreaders
);
232 case RCU_READER_ACTIVE_OLD
:
234 * Old snapshot. Leaving node in
235 * input_readers will make us busy-loop
236 * until the snapshot becomes current or
237 * the reader becomes inactive.
243 if (cds_list_empty(input_readers
)) {
246 /* Temporarily unlock the registry lock. */
247 mutex_unlock(&rcu_registry_lock
);
248 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
)
249 (void) poll(NULL
, 0, RCU_SLEEP_DELAY_MS
);
252 /* Re-lock the registry lock before the next loop. */
253 mutex_lock(&rcu_registry_lock
);
258 void synchronize_rcu(void)
260 CDS_LIST_HEAD(cur_snap_readers
);
261 CDS_LIST_HEAD(qsreaders
);
262 sigset_t newmask
, oldmask
;
265 ret
= sigfillset(&newmask
);
267 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
270 mutex_lock(&rcu_gp_lock
);
272 mutex_lock(&rcu_registry_lock
);
274 if (cds_list_empty(®istry
))
277 /* All threads should read qparity before accessing data structure
278 * where new ptr points to. */
279 /* Write new ptr before changing the qparity */
283 * Wait for readers to observe original parity or be quiescent.
284 * wait_for_readers() can release and grab again rcu_registry_lock
287 wait_for_readers(®istry
, &cur_snap_readers
, &qsreaders
);
290 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
291 * model easier to understand. It does not have a big performance impact
292 * anyway, given this is the write-side.
296 /* Switch parity: 0 -> 1, 1 -> 0 */
297 CMM_STORE_SHARED(rcu_gp
.ctr
, rcu_gp
.ctr
^ RCU_GP_CTR_PHASE
);
300 * Must commit qparity update to memory before waiting for other parity
301 * quiescent state. Failure to do so could result in the writer waiting
302 * forever while new readers are always accessing data (no progress).
303 * Ensured by CMM_STORE_SHARED and CMM_LOAD_SHARED.
307 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
308 * model easier to understand. It does not have a big performance impact
309 * anyway, given this is the write-side.
314 * Wait for readers to observe new parity or be quiescent.
315 * wait_for_readers() can release and grab again rcu_registry_lock
318 wait_for_readers(&cur_snap_readers
, NULL
, &qsreaders
);
321 * Put quiescent reader list back into registry.
323 cds_list_splice(&qsreaders
, ®istry
);
326 * Finish waiting for reader threads before letting the old ptr being
331 mutex_unlock(&rcu_registry_lock
);
332 mutex_unlock(&rcu_gp_lock
);
333 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
338 * library wrappers to be used by non-LGPL compatible source code.
341 void rcu_read_lock(void)
346 void rcu_read_unlock(void)
351 int rcu_read_ongoing(void)
353 return _rcu_read_ongoing();
357 * Only grow for now. If empty, allocate a ARENA_INIT_ALLOC sized chunk.
358 * Else, try expanding the last chunk. If this fails, allocate a new
359 * chunk twice as big as the last chunk.
360 * Memory used by chunks _never_ moves. A chunk could theoretically be
361 * freed when all "used" slots are released, but we don't do it at this
365 void expand_arena(struct registry_arena
*arena
)
367 struct registry_chunk
*new_chunk
, *last_chunk
;
368 size_t old_chunk_len
, new_chunk_len
;
371 if (cds_list_empty(&arena
->chunk_list
)) {
372 assert(ARENA_INIT_ALLOC
>=
373 sizeof(struct registry_chunk
)
374 + sizeof(struct rcu_reader
));
375 new_chunk_len
= ARENA_INIT_ALLOC
;
376 new_chunk
= mmap(NULL
, new_chunk_len
,
377 PROT_READ
| PROT_WRITE
,
378 MAP_ANONYMOUS
| MAP_PRIVATE
,
380 if (new_chunk
== MAP_FAILED
)
382 memset(new_chunk
, 0, new_chunk_len
);
383 new_chunk
->data_len
=
384 new_chunk_len
- sizeof(struct registry_chunk
);
385 cds_list_add_tail(&new_chunk
->node
, &arena
->chunk_list
);
386 return; /* We're done. */
389 /* Try expanding last chunk. */
390 last_chunk
= cds_list_entry(arena
->chunk_list
.prev
,
391 struct registry_chunk
, node
);
393 last_chunk
->data_len
+ sizeof(struct registry_chunk
);
394 new_chunk_len
= old_chunk_len
<< 1;
396 /* Don't allow memory mapping to move, just expand. */
397 new_chunk
= mremap_wrapper(last_chunk
, old_chunk_len
,
399 if (new_chunk
!= MAP_FAILED
) {
400 /* Should not have moved. */
401 assert(new_chunk
== last_chunk
);
402 memset((char *) last_chunk
+ old_chunk_len
, 0,
403 new_chunk_len
- old_chunk_len
);
404 last_chunk
->data_len
=
405 new_chunk_len
- sizeof(struct registry_chunk
);
406 return; /* We're done. */
409 /* Remap did not succeed, we need to add a new chunk. */
410 new_chunk
= mmap(NULL
, new_chunk_len
,
411 PROT_READ
| PROT_WRITE
,
412 MAP_ANONYMOUS
| MAP_PRIVATE
,
414 if (new_chunk
== MAP_FAILED
)
416 memset(new_chunk
, 0, new_chunk_len
);
417 new_chunk
->data_len
=
418 new_chunk_len
- sizeof(struct registry_chunk
);
419 cds_list_add_tail(&new_chunk
->node
, &arena
->chunk_list
);
423 struct rcu_reader
*arena_alloc(struct registry_arena
*arena
)
425 struct registry_chunk
*chunk
;
426 struct rcu_reader
*rcu_reader_reg
;
427 int expand_done
= 0; /* Only allow to expand once per alloc */
428 size_t len
= sizeof(struct rcu_reader
);
431 cds_list_for_each_entry(chunk
, &arena
->chunk_list
, node
) {
432 if (chunk
->data_len
- chunk
->used
< len
)
435 for (rcu_reader_reg
= (struct rcu_reader
*) &chunk
->data
[0];
436 rcu_reader_reg
< (struct rcu_reader
*) &chunk
->data
[chunk
->data_len
];
438 if (!rcu_reader_reg
->alloc
) {
439 rcu_reader_reg
->alloc
= 1;
441 return rcu_reader_reg
;
455 /* Called with signals off and mutex locked */
457 void add_thread(void)
459 struct rcu_reader
*rcu_reader_reg
;
462 rcu_reader_reg
= arena_alloc(®istry_arena
);
465 ret
= pthread_setspecific(urcu_bp_key
, rcu_reader_reg
);
469 /* Add to registry */
470 rcu_reader_reg
->tid
= pthread_self();
471 assert(rcu_reader_reg
->ctr
== 0);
472 cds_list_add(&rcu_reader_reg
->node
, ®istry
);
474 * Reader threads are pointing to the reader registry. This is
475 * why its memory should never be relocated.
477 URCU_TLS(rcu_reader
) = rcu_reader_reg
;
480 /* Called with mutex locked */
482 void cleanup_thread(struct registry_chunk
*chunk
,
483 struct rcu_reader
*rcu_reader_reg
)
485 rcu_reader_reg
->ctr
= 0;
486 cds_list_del(&rcu_reader_reg
->node
);
487 rcu_reader_reg
->tid
= 0;
488 rcu_reader_reg
->alloc
= 0;
489 chunk
->used
-= sizeof(struct rcu_reader
);
493 struct registry_chunk
*find_chunk(struct rcu_reader
*rcu_reader_reg
)
495 struct registry_chunk
*chunk
;
497 cds_list_for_each_entry(chunk
, ®istry_arena
.chunk_list
, node
) {
498 if (rcu_reader_reg
< (struct rcu_reader
*) &chunk
->data
[0])
500 if (rcu_reader_reg
>= (struct rcu_reader
*) &chunk
->data
[chunk
->data_len
])
507 /* Called with signals off and mutex locked */
509 void remove_thread(struct rcu_reader
*rcu_reader_reg
)
511 cleanup_thread(find_chunk(rcu_reader_reg
), rcu_reader_reg
);
512 URCU_TLS(rcu_reader
) = NULL
;
515 /* Disable signals, take mutex, add to registry */
516 void rcu_bp_register(void)
518 sigset_t newmask
, oldmask
;
521 ret
= sigfillset(&newmask
);
524 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
529 * Check if a signal concurrently registered our thread since
530 * the check in rcu_read_lock().
532 if (URCU_TLS(rcu_reader
))
536 * Take care of early registration before urcu_bp constructor.
540 mutex_lock(&rcu_registry_lock
);
542 mutex_unlock(&rcu_registry_lock
);
544 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
549 /* Disable signals, take mutex, remove from registry */
551 void rcu_bp_unregister(struct rcu_reader
*rcu_reader_reg
)
553 sigset_t newmask
, oldmask
;
556 ret
= sigfillset(&newmask
);
559 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
563 mutex_lock(&rcu_registry_lock
);
564 remove_thread(rcu_reader_reg
);
565 mutex_unlock(&rcu_registry_lock
);
566 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
573 * Remove thread from the registry when it exits, and flag it as
574 * destroyed so garbage collection can take care of it.
577 void urcu_bp_thread_exit_notifier(void *rcu_key
)
579 rcu_bp_unregister(rcu_key
);
583 void rcu_bp_init(void)
585 mutex_lock(&init_lock
);
586 if (!rcu_bp_refcount
++) {
589 ret
= pthread_key_create(&urcu_bp_key
,
590 urcu_bp_thread_exit_notifier
);
593 ret
= membarrier(MEMBARRIER_CMD_QUERY
, 0);
594 if (ret
>= 0 && (ret
& MEMBARRIER_CMD_SHARED
)) {
595 urcu_bp_has_sys_membarrier
= 1;
599 mutex_unlock(&init_lock
);
603 void rcu_bp_exit(void)
605 mutex_lock(&init_lock
);
606 if (!--rcu_bp_refcount
) {
607 struct registry_chunk
*chunk
, *tmp
;
610 cds_list_for_each_entry_safe(chunk
, tmp
,
611 ®istry_arena
.chunk_list
, node
) {
612 munmap(chunk
, chunk
->data_len
613 + sizeof(struct registry_chunk
));
615 ret
= pthread_key_delete(urcu_bp_key
);
619 mutex_unlock(&init_lock
);
623 * Holding the rcu_gp_lock and rcu_registry_lock across fork will make
624 * sure we fork() don't race with a concurrent thread executing with
625 * any of those locks held. This ensures that the registry and data
626 * protected by rcu_gp_lock are in a coherent state in the child.
628 void rcu_bp_before_fork(void)
630 sigset_t newmask
, oldmask
;
633 ret
= sigfillset(&newmask
);
635 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
637 mutex_lock(&rcu_gp_lock
);
638 mutex_lock(&rcu_registry_lock
);
639 saved_fork_signal_mask
= oldmask
;
642 void rcu_bp_after_fork_parent(void)
647 oldmask
= saved_fork_signal_mask
;
648 mutex_unlock(&rcu_registry_lock
);
649 mutex_unlock(&rcu_gp_lock
);
650 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
655 * Prune all entries from registry except our own thread. Fits the Linux
656 * fork behavior. Called with rcu_gp_lock and rcu_registry_lock held.
659 void urcu_bp_prune_registry(void)
661 struct registry_chunk
*chunk
;
662 struct rcu_reader
*rcu_reader_reg
;
664 cds_list_for_each_entry(chunk
, ®istry_arena
.chunk_list
, node
) {
665 for (rcu_reader_reg
= (struct rcu_reader
*) &chunk
->data
[0];
666 rcu_reader_reg
< (struct rcu_reader
*) &chunk
->data
[chunk
->data_len
];
668 if (!rcu_reader_reg
->alloc
)
670 if (rcu_reader_reg
->tid
== pthread_self())
672 cleanup_thread(chunk
, rcu_reader_reg
);
677 void rcu_bp_after_fork_child(void)
682 urcu_bp_prune_registry();
683 oldmask
= saved_fork_signal_mask
;
684 mutex_unlock(&rcu_registry_lock
);
685 mutex_unlock(&rcu_gp_lock
);
686 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
690 void *rcu_dereference_sym_bp(void *p
)
692 return _rcu_dereference(p
);
695 void *rcu_set_pointer_sym_bp(void **p
, void *v
)
702 void *rcu_xchg_pointer_sym_bp(void **p
, void *v
)
705 return uatomic_xchg(p
, v
);
708 void *rcu_cmpxchg_pointer_sym_bp(void **p
, void *old
, void *_new
)
711 return uatomic_cmpxchg(p
, old
, _new
);
714 DEFINE_RCU_FLAVOR(rcu_flavor
);
716 #include "urcu-call-rcu-impl.h"
717 #include "urcu-defer-impl.h"