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>
47 #include "urcu-utils.h"
50 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
52 #include <urcu/urcu-bp.h>
56 #define MAP_ANONYMOUS MAP_ANON
61 void *mremap_wrapper(void *old_address
, size_t old_size
,
62 size_t new_size
, int flags
)
64 return mremap(old_address
, old_size
, new_size
, flags
);
68 #define MREMAP_MAYMOVE 1
69 #define MREMAP_FIXED 2
72 * mremap wrapper for non-Linux systems not allowing MAYMOVE.
73 * This is not generic.
76 void *mremap_wrapper(void *old_address
, size_t old_size
,
77 size_t new_size
, int flags
)
79 assert(!(flags
& MREMAP_MAYMOVE
));
85 /* Sleep delay in ms */
86 #define RCU_SLEEP_DELAY_MS 10
87 #define INIT_NR_THREADS 8
88 #define ARENA_INIT_ALLOC \
89 sizeof(struct registry_chunk) \
90 + INIT_NR_THREADS * sizeof(struct urcu_bp_reader)
93 * Active attempts to check for reader Q.S. before calling sleep().
95 #define RCU_QS_ACTIVE_ATTEMPTS 100
100 /* If the headers do not support membarrier system call, fall back smp_mb. */
101 #ifdef __NR_membarrier
102 # define membarrier(...) syscall(__NR_membarrier, __VA_ARGS__)
104 # define membarrier(...) -ENOSYS
107 enum membarrier_cmd
{
108 MEMBARRIER_CMD_QUERY
= 0,
109 MEMBARRIER_CMD_SHARED
= (1 << 0),
110 /* reserved for MEMBARRIER_CMD_SHARED_EXPEDITED (1 << 1) */
111 /* reserved for MEMBARRIER_CMD_PRIVATE (1 << 2) */
112 MEMBARRIER_CMD_PRIVATE_EXPEDITED
= (1 << 3),
113 MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED
= (1 << 4),
117 void __attribute__((constructor
)) _urcu_bp_init(void);
119 void __attribute__((destructor
)) urcu_bp_exit(void);
121 #ifndef CONFIG_RCU_FORCE_SYS_MEMBARRIER
122 int urcu_bp_has_sys_membarrier
;
126 * rcu_gp_lock ensures mutual exclusion between threads calling
129 static pthread_mutex_t rcu_gp_lock
= PTHREAD_MUTEX_INITIALIZER
;
131 * rcu_registry_lock ensures mutual exclusion between threads
132 * registering and unregistering themselves to/from the registry, and
133 * with threads reading that registry from synchronize_rcu(). However,
134 * this lock is not held all the way through the completion of awaiting
135 * for the grace period. It is sporadically released between iterations
137 * rcu_registry_lock may nest inside rcu_gp_lock.
139 static pthread_mutex_t rcu_registry_lock
= PTHREAD_MUTEX_INITIALIZER
;
141 static pthread_mutex_t init_lock
= PTHREAD_MUTEX_INITIALIZER
;
142 static int initialized
;
144 static pthread_key_t urcu_bp_key
;
146 struct urcu_bp_gp urcu_bp_gp
= { .ctr
= URCU_BP_GP_COUNT
};
147 URCU_ATTR_ALIAS("urcu_bp_gp") extern struct urcu_bp_gp rcu_gp_bp
;
150 * Pointer to registry elements. Written to only by each individual reader. Read
151 * by both the reader and the writers.
153 DEFINE_URCU_TLS(struct urcu_bp_reader
*, urcu_bp_reader
);
154 DEFINE_URCU_TLS_ALIAS(struct urcu_bp_reader
*, urcu_bp_reader
, rcu_reader_bp
);
156 static CDS_LIST_HEAD(registry
);
158 struct registry_chunk
{
159 size_t data_len
; /* data length */
160 size_t used
; /* amount of data used */
161 struct cds_list_head node
; /* chunk_list node */
165 struct registry_arena
{
166 struct cds_list_head chunk_list
;
169 static struct registry_arena registry_arena
= {
170 .chunk_list
= CDS_LIST_HEAD_INIT(registry_arena
.chunk_list
),
173 /* Saved fork signal mask, protected by rcu_gp_lock */
174 static sigset_t saved_fork_signal_mask
;
176 static void mutex_lock(pthread_mutex_t
*mutex
)
180 #ifndef DISTRUST_SIGNALS_EXTREME
181 ret
= pthread_mutex_lock(mutex
);
184 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
185 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
186 if (ret
!= EBUSY
&& ret
!= EINTR
)
190 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
193 static void mutex_unlock(pthread_mutex_t
*mutex
)
197 ret
= pthread_mutex_unlock(mutex
);
202 static void smp_mb_master(void)
204 if (caa_likely(urcu_bp_has_sys_membarrier
)) {
205 if (membarrier(MEMBARRIER_CMD_PRIVATE_EXPEDITED
, 0))
213 * Always called with rcu_registry lock held. Releases this lock between
214 * iterations and grabs it again. Holds the lock when it returns.
216 static void wait_for_readers(struct cds_list_head
*input_readers
,
217 struct cds_list_head
*cur_snap_readers
,
218 struct cds_list_head
*qsreaders
)
220 unsigned int wait_loops
= 0;
221 struct urcu_bp_reader
*index
, *tmp
;
224 * Wait for each thread URCU_TLS(urcu_bp_reader).ctr to either
225 * indicate quiescence (not nested), or observe the current
229 if (wait_loops
< RCU_QS_ACTIVE_ATTEMPTS
)
232 cds_list_for_each_entry_safe(index
, tmp
, input_readers
, node
) {
233 switch (urcu_bp_reader_state(&index
->ctr
)) {
234 case URCU_BP_READER_ACTIVE_CURRENT
:
235 if (cur_snap_readers
) {
236 cds_list_move(&index
->node
,
241 case URCU_BP_READER_INACTIVE
:
242 cds_list_move(&index
->node
, qsreaders
);
244 case URCU_BP_READER_ACTIVE_OLD
:
246 * Old snapshot. Leaving node in
247 * input_readers will make us busy-loop
248 * until the snapshot becomes current or
249 * the reader becomes inactive.
255 if (cds_list_empty(input_readers
)) {
258 /* Temporarily unlock the registry lock. */
259 mutex_unlock(&rcu_registry_lock
);
260 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
)
261 (void) poll(NULL
, 0, RCU_SLEEP_DELAY_MS
);
264 /* Re-lock the registry lock before the next loop. */
265 mutex_lock(&rcu_registry_lock
);
270 void urcu_bp_synchronize_rcu(void)
272 CDS_LIST_HEAD(cur_snap_readers
);
273 CDS_LIST_HEAD(qsreaders
);
274 sigset_t newmask
, oldmask
;
277 ret
= sigfillset(&newmask
);
279 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
282 mutex_lock(&rcu_gp_lock
);
284 mutex_lock(&rcu_registry_lock
);
286 if (cds_list_empty(®istry
))
289 /* All threads should read qparity before accessing data structure
290 * where new ptr points to. */
291 /* Write new ptr before changing the qparity */
295 * Wait for readers to observe original parity or be quiescent.
296 * wait_for_readers() can release and grab again rcu_registry_lock
299 wait_for_readers(®istry
, &cur_snap_readers
, &qsreaders
);
302 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
303 * model easier to understand. It does not have a big performance impact
304 * anyway, given this is the write-side.
308 /* Switch parity: 0 -> 1, 1 -> 0 */
309 CMM_STORE_SHARED(rcu_gp
.ctr
, rcu_gp
.ctr
^ URCU_BP_GP_CTR_PHASE
);
312 * Must commit qparity update to memory before waiting for other parity
313 * quiescent state. Failure to do so could result in the writer waiting
314 * forever while new readers are always accessing data (no progress).
315 * Ensured by CMM_STORE_SHARED and CMM_LOAD_SHARED.
319 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
320 * model easier to understand. It does not have a big performance impact
321 * anyway, given this is the write-side.
326 * Wait for readers to observe new parity or be quiescent.
327 * wait_for_readers() can release and grab again rcu_registry_lock
330 wait_for_readers(&cur_snap_readers
, NULL
, &qsreaders
);
333 * Put quiescent reader list back into registry.
335 cds_list_splice(&qsreaders
, ®istry
);
338 * Finish waiting for reader threads before letting the old ptr being
343 mutex_unlock(&rcu_registry_lock
);
344 mutex_unlock(&rcu_gp_lock
);
345 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
348 URCU_ATTR_ALIAS("urcu_bp_synchronize_rcu") void synchronize_rcu_bp();
351 * library wrappers to be used by non-LGPL compatible source code.
354 void urcu_bp_read_lock(void)
356 _urcu_bp_read_lock();
358 URCU_ATTR_ALIAS("urcu_bp_read_lock") void rcu_read_lock_bp();
360 void urcu_bp_read_unlock(void)
362 _urcu_bp_read_unlock();
364 URCU_ATTR_ALIAS("urcu_bp_read_unlock") void rcu_read_unlock_bp();
366 int urcu_bp_read_ongoing(void)
368 return _urcu_bp_read_ongoing();
370 URCU_ATTR_ALIAS("urcu_bp_read_ongoing") int rcu_read_ongoing_bp();
373 * Only grow for now. If empty, allocate a ARENA_INIT_ALLOC sized chunk.
374 * Else, try expanding the last chunk. If this fails, allocate a new
375 * chunk twice as big as the last chunk.
376 * Memory used by chunks _never_ moves. A chunk could theoretically be
377 * freed when all "used" slots are released, but we don't do it at this
381 void expand_arena(struct registry_arena
*arena
)
383 struct registry_chunk
*new_chunk
, *last_chunk
;
384 size_t old_chunk_len
, new_chunk_len
;
387 if (cds_list_empty(&arena
->chunk_list
)) {
388 assert(ARENA_INIT_ALLOC
>=
389 sizeof(struct registry_chunk
)
390 + sizeof(struct rcu_reader
));
391 new_chunk_len
= ARENA_INIT_ALLOC
;
392 new_chunk
= (struct registry_chunk
*) mmap(NULL
,
394 PROT_READ
| PROT_WRITE
,
395 MAP_ANONYMOUS
| MAP_PRIVATE
,
397 if (new_chunk
== MAP_FAILED
)
399 memset(new_chunk
, 0, new_chunk_len
);
400 new_chunk
->data_len
=
401 new_chunk_len
- sizeof(struct registry_chunk
);
402 cds_list_add_tail(&new_chunk
->node
, &arena
->chunk_list
);
403 return; /* We're done. */
406 /* Try expanding last chunk. */
407 last_chunk
= cds_list_entry(arena
->chunk_list
.prev
,
408 struct registry_chunk
, node
);
410 last_chunk
->data_len
+ sizeof(struct registry_chunk
);
411 new_chunk_len
= old_chunk_len
<< 1;
413 /* Don't allow memory mapping to move, just expand. */
414 new_chunk
= mremap_wrapper(last_chunk
, old_chunk_len
,
416 if (new_chunk
!= MAP_FAILED
) {
417 /* Should not have moved. */
418 assert(new_chunk
== last_chunk
);
419 memset((char *) last_chunk
+ old_chunk_len
, 0,
420 new_chunk_len
- old_chunk_len
);
421 last_chunk
->data_len
=
422 new_chunk_len
- sizeof(struct registry_chunk
);
423 return; /* We're done. */
426 /* Remap did not succeed, we need to add a new chunk. */
427 new_chunk
= (struct registry_chunk
*) mmap(NULL
,
429 PROT_READ
| PROT_WRITE
,
430 MAP_ANONYMOUS
| MAP_PRIVATE
,
432 if (new_chunk
== MAP_FAILED
)
434 memset(new_chunk
, 0, new_chunk_len
);
435 new_chunk
->data_len
=
436 new_chunk_len
- sizeof(struct registry_chunk
);
437 cds_list_add_tail(&new_chunk
->node
, &arena
->chunk_list
);
441 struct rcu_reader
*arena_alloc(struct registry_arena
*arena
)
443 struct registry_chunk
*chunk
;
444 struct rcu_reader
*rcu_reader_reg
;
445 int expand_done
= 0; /* Only allow to expand once per alloc */
446 size_t len
= sizeof(struct rcu_reader
);
449 cds_list_for_each_entry(chunk
, &arena
->chunk_list
, node
) {
450 if (chunk
->data_len
- chunk
->used
< len
)
453 for (rcu_reader_reg
= (struct rcu_reader
*) &chunk
->data
[0];
454 rcu_reader_reg
< (struct rcu_reader
*) &chunk
->data
[chunk
->data_len
];
456 if (!rcu_reader_reg
->alloc
) {
457 rcu_reader_reg
->alloc
= 1;
459 return rcu_reader_reg
;
473 /* Called with signals off and mutex locked */
475 void add_thread(void)
477 struct rcu_reader
*rcu_reader_reg
;
480 rcu_reader_reg
= arena_alloc(®istry_arena
);
483 ret
= pthread_setspecific(urcu_bp_key
, rcu_reader_reg
);
487 /* Add to registry */
488 rcu_reader_reg
->tid
= pthread_self();
489 assert(rcu_reader_reg
->ctr
== 0);
490 cds_list_add(&rcu_reader_reg
->node
, ®istry
);
492 * Reader threads are pointing to the reader registry. This is
493 * why its memory should never be relocated.
495 URCU_TLS(urcu_bp_reader
) = rcu_reader_reg
;
498 /* Called with mutex locked */
500 void cleanup_thread(struct registry_chunk
*chunk
,
501 struct rcu_reader
*rcu_reader_reg
)
503 rcu_reader_reg
->ctr
= 0;
504 cds_list_del(&rcu_reader_reg
->node
);
505 rcu_reader_reg
->tid
= 0;
506 rcu_reader_reg
->alloc
= 0;
507 chunk
->used
-= sizeof(struct rcu_reader
);
511 struct registry_chunk
*find_chunk(struct rcu_reader
*rcu_reader_reg
)
513 struct registry_chunk
*chunk
;
515 cds_list_for_each_entry(chunk
, ®istry_arena
.chunk_list
, node
) {
516 if (rcu_reader_reg
< (struct rcu_reader
*) &chunk
->data
[0])
518 if (rcu_reader_reg
>= (struct rcu_reader
*) &chunk
->data
[chunk
->data_len
])
525 /* Called with signals off and mutex locked */
527 void remove_thread(struct rcu_reader
*rcu_reader_reg
)
529 cleanup_thread(find_chunk(rcu_reader_reg
), rcu_reader_reg
);
530 URCU_TLS(urcu_bp_reader
) = NULL
;
533 /* Disable signals, take mutex, add to registry */
534 void urcu_bp_register(void)
536 sigset_t newmask
, oldmask
;
539 ret
= sigfillset(&newmask
);
542 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
547 * Check if a signal concurrently registered our thread since
548 * the check in rcu_read_lock().
550 if (URCU_TLS(urcu_bp_reader
))
554 * Take care of early registration before urcu_bp constructor.
558 mutex_lock(&rcu_registry_lock
);
560 mutex_unlock(&rcu_registry_lock
);
562 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
566 URCU_ATTR_ALIAS("urcu_bp_register") void rcu_bp_register();
568 /* Disable signals, take mutex, remove from registry */
570 void urcu_bp_unregister(struct rcu_reader
*rcu_reader_reg
)
572 sigset_t newmask
, oldmask
;
575 ret
= sigfillset(&newmask
);
578 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
582 mutex_lock(&rcu_registry_lock
);
583 remove_thread(rcu_reader_reg
);
584 mutex_unlock(&rcu_registry_lock
);
585 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
592 * Remove thread from the registry when it exits, and flag it as
593 * destroyed so garbage collection can take care of it.
596 void urcu_bp_thread_exit_notifier(void *rcu_key
)
598 urcu_bp_unregister(rcu_key
);
601 #ifdef CONFIG_RCU_FORCE_SYS_MEMBARRIER
603 void urcu_bp_sys_membarrier_status(bool available
)
610 void urcu_bp_sys_membarrier_status(bool available
)
614 urcu_bp_has_sys_membarrier
= 1;
619 void urcu_bp_sys_membarrier_init(void)
621 bool available
= false;
624 mask
= membarrier(MEMBARRIER_CMD_QUERY
, 0);
626 if (mask
& MEMBARRIER_CMD_PRIVATE_EXPEDITED
) {
627 if (membarrier(MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED
, 0))
632 urcu_bp_sys_membarrier_status(available
);
636 void _urcu_bp_init(void)
638 mutex_lock(&init_lock
);
639 if (!urcu_bp_refcount
++) {
642 ret
= pthread_key_create(&urcu_bp_key
,
643 urcu_bp_thread_exit_notifier
);
646 urcu_bp_sys_membarrier_init();
649 mutex_unlock(&init_lock
);
653 void urcu_bp_exit(void)
655 mutex_lock(&init_lock
);
656 if (!--urcu_bp_refcount
) {
657 struct registry_chunk
*chunk
, *tmp
;
660 cds_list_for_each_entry_safe(chunk
, tmp
,
661 ®istry_arena
.chunk_list
, node
) {
662 munmap((void *) chunk
, chunk
->data_len
663 + sizeof(struct registry_chunk
));
665 CDS_INIT_LIST_HEAD(®istry_arena
.chunk_list
);
666 ret
= pthread_key_delete(urcu_bp_key
);
670 mutex_unlock(&init_lock
);
674 * Holding the rcu_gp_lock and rcu_registry_lock across fork will make
675 * sure we fork() don't race with a concurrent thread executing with
676 * any of those locks held. This ensures that the registry and data
677 * protected by rcu_gp_lock are in a coherent state in the child.
679 void urcu_bp_before_fork(void)
681 sigset_t newmask
, oldmask
;
684 ret
= sigfillset(&newmask
);
686 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
688 mutex_lock(&rcu_gp_lock
);
689 mutex_lock(&rcu_registry_lock
);
690 saved_fork_signal_mask
= oldmask
;
692 URCU_ATTR_ALIAS("urcu_bp_before_fork") void rcu_bp_before_fork();
694 void urcu_bp_after_fork_parent(void)
699 oldmask
= saved_fork_signal_mask
;
700 mutex_unlock(&rcu_registry_lock
);
701 mutex_unlock(&rcu_gp_lock
);
702 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
705 URCU_ATTR_ALIAS("urcu_bp_after_fork_parent")
706 void rcu_bp_after_fork_parent(void);
709 * Prune all entries from registry except our own thread. Fits the Linux
710 * fork behavior. Called with rcu_gp_lock and rcu_registry_lock held.
713 void urcu_bp_prune_registry(void)
715 struct registry_chunk
*chunk
;
716 struct urcu_bp_reader
*rcu_reader_reg
;
718 cds_list_for_each_entry(chunk
, ®istry_arena
.chunk_list
, node
) {
719 for (rcu_reader_reg
= (struct urcu_bp_reader
*) &chunk
->data
[0];
720 rcu_reader_reg
< (struct urcu_bp_reader
*) &chunk
->data
[chunk
->data_len
];
722 if (!rcu_reader_reg
->alloc
)
724 if (rcu_reader_reg
->tid
== pthread_self())
726 cleanup_thread(chunk
, rcu_reader_reg
);
731 void urcu_bp_after_fork_child(void)
736 urcu_bp_prune_registry();
737 oldmask
= saved_fork_signal_mask
;
738 mutex_unlock(&rcu_registry_lock
);
739 mutex_unlock(&rcu_gp_lock
);
740 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
743 URCU_ATTR_ALIAS("urcu_bp_after_fork_child")
744 void rcu_bp_after_fork_child(void);
746 void *urcu_bp_dereference_sym(void *p
)
748 return _rcu_dereference(p
);
750 URCU_ATTR_ALIAS("urcu_bp_dereference_sym")
751 void *rcu_dereference_sym_bp();
753 void *urcu_bp_set_pointer_sym(void **p
, void *v
)
759 URCU_ATTR_ALIAS("urcu_bp_set_pointer_sym")
760 void *rcu_set_pointer_sym_bp();
762 void *urcu_bp_xchg_pointer_sym(void **p
, void *v
)
765 return uatomic_xchg(p
, v
);
767 URCU_ATTR_ALIAS("urcu_bp_xchg_pointer_sym")
768 void *rcu_xchg_pointer_sym_bp();
770 void *urcu_bp_cmpxchg_pointer_sym(void **p
, void *old
, void *_new
)
773 return uatomic_cmpxchg(p
, old
, _new
);
775 URCU_ATTR_ALIAS("urcu_bp_cmpxchg_pointer_sym")
776 void *rcu_cmpxchg_pointer_sym_bp();
778 DEFINE_RCU_FLAVOR(rcu_flavor
);
779 DEFINE_RCU_FLAVOR_ALIAS(rcu_flavor
, alias_rcu_flavor
);
781 #include "urcu-call-rcu-impl.h"
782 #include "urcu-defer-impl.h"