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.
26 #define URCU_NO_COMPAT_IDENTIFIERS
39 #include <urcu/assert.h>
40 #include <urcu/config.h>
41 #include <urcu/arch.h>
42 #include <urcu/wfcqueue.h>
43 #include <urcu/map/urcu-bp.h>
44 #include <urcu/static/urcu-bp.h>
45 #include <urcu/pointer.h>
46 #include <urcu/tls-compat.h>
49 #include "urcu-utils.h"
52 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
54 #include <urcu/urcu-bp.h>
58 #define MAP_ANONYMOUS MAP_ANON
63 void *mremap_wrapper(void *old_address
, size_t old_size
,
64 size_t new_size
, int flags
)
66 return mremap(old_address
, old_size
, new_size
, flags
);
70 #define MREMAP_MAYMOVE 1
71 #define MREMAP_FIXED 2
74 * mremap wrapper for non-Linux systems not allowing MAYMOVE.
75 * This is not generic.
78 void *mremap_wrapper(void *old_address
__attribute__((unused
)),
79 size_t old_size
__attribute__((unused
)),
80 size_t new_size
__attribute__((unused
)),
83 urcu_posix_assert(!(flags
& MREMAP_MAYMOVE
));
89 /* Sleep delay in ms */
90 #define RCU_SLEEP_DELAY_MS 10
91 #define INIT_READER_COUNT 8
94 * Active attempts to check for reader Q.S. before calling sleep().
96 #define RCU_QS_ACTIVE_ATTEMPTS 100
101 /* If the headers do not support membarrier system call, fall back smp_mb. */
102 #ifdef __NR_membarrier
103 # define membarrier(...) syscall(__NR_membarrier, __VA_ARGS__)
105 # define membarrier(...) -ENOSYS
108 enum membarrier_cmd
{
109 MEMBARRIER_CMD_QUERY
= 0,
110 MEMBARRIER_CMD_SHARED
= (1 << 0),
111 /* reserved for MEMBARRIER_CMD_SHARED_EXPEDITED (1 << 1) */
112 /* reserved for MEMBARRIER_CMD_PRIVATE (1 << 2) */
113 MEMBARRIER_CMD_PRIVATE_EXPEDITED
= (1 << 3),
114 MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED
= (1 << 4),
118 void __attribute__((constructor
)) _urcu_bp_init(void);
120 void urcu_bp_exit(void);
122 void __attribute__((destructor
)) urcu_bp_exit_destructor(void);
123 static void urcu_call_rcu_exit(void);
125 #ifndef CONFIG_RCU_FORCE_SYS_MEMBARRIER
126 int urcu_bp_has_sys_membarrier
;
130 * rcu_gp_lock ensures mutual exclusion between threads calling
133 static pthread_mutex_t rcu_gp_lock
= PTHREAD_MUTEX_INITIALIZER
;
135 * rcu_registry_lock ensures mutual exclusion between threads
136 * registering and unregistering themselves to/from the registry, and
137 * with threads reading that registry from synchronize_rcu(). However,
138 * this lock is not held all the way through the completion of awaiting
139 * for the grace period. It is sporadically released between iterations
141 * rcu_registry_lock may nest inside rcu_gp_lock.
143 static pthread_mutex_t rcu_registry_lock
= PTHREAD_MUTEX_INITIALIZER
;
145 static pthread_mutex_t init_lock
= PTHREAD_MUTEX_INITIALIZER
;
146 static int initialized
;
148 static pthread_key_t urcu_bp_key
;
150 struct urcu_bp_gp urcu_bp_gp
= { .ctr
= URCU_BP_GP_COUNT
};
153 * Pointer to registry elements. Written to only by each individual reader. Read
154 * by both the reader and the writers.
156 DEFINE_URCU_TLS(struct urcu_bp_reader
*, urcu_bp_reader
);
158 static CDS_LIST_HEAD(registry
);
160 struct registry_chunk
{
161 size_t capacity
; /* capacity of this chunk (in elements) */
162 size_t used
; /* count of elements used */
163 struct cds_list_head node
; /* chunk_list node */
164 struct urcu_bp_reader readers
[];
167 struct registry_arena
{
168 struct cds_list_head chunk_list
;
171 static struct registry_arena registry_arena
= {
172 .chunk_list
= CDS_LIST_HEAD_INIT(registry_arena
.chunk_list
),
175 /* Saved fork signal mask, protected by rcu_gp_lock */
176 static sigset_t saved_fork_signal_mask
;
178 static void mutex_lock(pthread_mutex_t
*mutex
)
182 #ifndef DISTRUST_SIGNALS_EXTREME
183 ret
= pthread_mutex_lock(mutex
);
186 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
187 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
188 if (ret
!= EBUSY
&& ret
!= EINTR
)
192 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
195 static void mutex_unlock(pthread_mutex_t
*mutex
)
199 ret
= pthread_mutex_unlock(mutex
);
204 static void smp_mb_master(void)
206 if (caa_likely(urcu_bp_has_sys_membarrier
)) {
207 if (membarrier(MEMBARRIER_CMD_PRIVATE_EXPEDITED
, 0))
214 /* Get the size of a chunk's allocation from its capacity (an element count). */
215 static size_t chunk_allocation_size(size_t capacity
)
217 return (capacity
* sizeof(struct urcu_bp_reader
)) +
218 sizeof(struct registry_chunk
);
222 * Always called with rcu_registry lock held. Releases this lock between
223 * iterations and grabs it again. Holds the lock when it returns.
225 static void wait_for_readers(struct cds_list_head
*input_readers
,
226 struct cds_list_head
*cur_snap_readers
,
227 struct cds_list_head
*qsreaders
)
229 unsigned int wait_loops
= 0;
230 struct urcu_bp_reader
*index
, *tmp
;
233 * Wait for each thread URCU_TLS(urcu_bp_reader).ctr to either
234 * indicate quiescence (not nested), or observe the current
238 if (wait_loops
< RCU_QS_ACTIVE_ATTEMPTS
)
241 cds_list_for_each_entry_safe(index
, tmp
, input_readers
, node
) {
242 switch (urcu_bp_reader_state(&index
->ctr
)) {
243 case URCU_BP_READER_ACTIVE_CURRENT
:
244 if (cur_snap_readers
) {
245 cds_list_move(&index
->node
,
250 case URCU_BP_READER_INACTIVE
:
251 cds_list_move(&index
->node
, qsreaders
);
253 case URCU_BP_READER_ACTIVE_OLD
:
255 * Old snapshot. Leaving node in
256 * input_readers will make us busy-loop
257 * until the snapshot becomes current or
258 * the reader becomes inactive.
264 if (cds_list_empty(input_readers
)) {
267 /* Temporarily unlock the registry lock. */
268 mutex_unlock(&rcu_registry_lock
);
269 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
)
270 (void) poll(NULL
, 0, RCU_SLEEP_DELAY_MS
);
273 /* Re-lock the registry lock before the next loop. */
274 mutex_lock(&rcu_registry_lock
);
279 void urcu_bp_synchronize_rcu(void)
281 CDS_LIST_HEAD(cur_snap_readers
);
282 CDS_LIST_HEAD(qsreaders
);
283 sigset_t newmask
, oldmask
;
286 ret
= sigfillset(&newmask
);
287 urcu_posix_assert(!ret
);
288 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
289 urcu_posix_assert(!ret
);
291 mutex_lock(&rcu_gp_lock
);
293 mutex_lock(&rcu_registry_lock
);
295 if (cds_list_empty(®istry
))
298 /* All threads should read qparity before accessing data structure
299 * where new ptr points to. */
300 /* Write new ptr before changing the qparity */
304 * Wait for readers to observe original parity or be quiescent.
305 * wait_for_readers() can release and grab again rcu_registry_lock
308 wait_for_readers(®istry
, &cur_snap_readers
, &qsreaders
);
311 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
312 * model easier to understand. It does not have a big performance impact
313 * anyway, given this is the write-side.
317 /* Switch parity: 0 -> 1, 1 -> 0 */
318 CMM_STORE_SHARED(rcu_gp
.ctr
, rcu_gp
.ctr
^ URCU_BP_GP_CTR_PHASE
);
321 * Must commit qparity update to memory before waiting for other parity
322 * quiescent state. Failure to do so could result in the writer waiting
323 * forever while new readers are always accessing data (no progress).
324 * Ensured by CMM_STORE_SHARED and CMM_LOAD_SHARED.
328 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
329 * model easier to understand. It does not have a big performance impact
330 * anyway, given this is the write-side.
335 * Wait for readers to observe new parity or be quiescent.
336 * wait_for_readers() can release and grab again rcu_registry_lock
339 wait_for_readers(&cur_snap_readers
, NULL
, &qsreaders
);
342 * Put quiescent reader list back into registry.
344 cds_list_splice(&qsreaders
, ®istry
);
347 * Finish waiting for reader threads before letting the old ptr being
352 mutex_unlock(&rcu_registry_lock
);
353 mutex_unlock(&rcu_gp_lock
);
354 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
355 urcu_posix_assert(!ret
);
359 * library wrappers to be used by non-LGPL compatible source code.
362 void urcu_bp_read_lock(void)
364 _urcu_bp_read_lock();
367 void urcu_bp_read_unlock(void)
369 _urcu_bp_read_unlock();
372 int urcu_bp_read_ongoing(void)
374 return _urcu_bp_read_ongoing();
378 * Only grow for now. If empty, allocate a ARENA_INIT_ALLOC sized chunk.
379 * Else, try expanding the last chunk. If this fails, allocate a new
380 * chunk twice as big as the last chunk.
381 * Memory used by chunks _never_ moves. A chunk could theoretically be
382 * freed when all "used" slots are released, but we don't do it at this
386 void expand_arena(struct registry_arena
*arena
)
388 struct registry_chunk
*new_chunk
, *last_chunk
;
389 size_t old_chunk_size_bytes
, new_chunk_size_bytes
, new_capacity
;
392 if (cds_list_empty(&arena
->chunk_list
)) {
393 new_chunk_size_bytes
= chunk_allocation_size(INIT_READER_COUNT
);
394 new_chunk
= (struct registry_chunk
*) mmap(NULL
,
395 new_chunk_size_bytes
,
396 PROT_READ
| PROT_WRITE
,
397 MAP_ANONYMOUS
| MAP_PRIVATE
,
399 if (new_chunk
== MAP_FAILED
)
401 memset(new_chunk
, 0, new_chunk_size_bytes
);
402 new_chunk
->capacity
= INIT_READER_COUNT
;
403 cds_list_add_tail(&new_chunk
->node
, &arena
->chunk_list
);
404 return; /* We're done. */
407 /* Try expanding last chunk. */
408 last_chunk
= cds_list_entry(arena
->chunk_list
.prev
,
409 struct registry_chunk
, node
);
410 old_chunk_size_bytes
= chunk_allocation_size(last_chunk
->capacity
);
411 new_capacity
= last_chunk
->capacity
<< 1;
412 new_chunk_size_bytes
= chunk_allocation_size(new_capacity
);
414 /* Don't allow memory mapping to move, just expand. */
415 new_chunk
= mremap_wrapper(last_chunk
, old_chunk_size_bytes
,
416 new_chunk_size_bytes
, 0);
417 if (new_chunk
!= MAP_FAILED
) {
418 /* Should not have moved. */
419 assert(new_chunk
== last_chunk
);
420 memset((char *) last_chunk
+ old_chunk_size_bytes
, 0,
421 new_chunk_size_bytes
- old_chunk_size_bytes
);
422 last_chunk
->capacity
= new_capacity
;
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
,
428 new_chunk_size_bytes
,
429 PROT_READ
| PROT_WRITE
,
430 MAP_ANONYMOUS
| MAP_PRIVATE
,
432 if (new_chunk
== MAP_FAILED
)
434 memset(new_chunk
, 0, new_chunk_size_bytes
);
435 new_chunk
->capacity
= new_capacity
;
436 cds_list_add_tail(&new_chunk
->node
, &arena
->chunk_list
);
440 struct rcu_reader
*arena_alloc(struct registry_arena
*arena
)
442 struct registry_chunk
*chunk
;
443 int expand_done
= 0; /* Only allow to expand once per alloc */
446 cds_list_for_each_entry(chunk
, &arena
->chunk_list
, node
) {
449 /* Skip fully used chunks. */
450 if (chunk
->used
== chunk
->capacity
) {
455 for (spot_idx
= 0; spot_idx
< chunk
->capacity
; spot_idx
++) {
456 if (!chunk
->readers
[spot_idx
].alloc
) {
457 chunk
->readers
[spot_idx
].alloc
= 1;
459 return &chunk
->readers
[spot_idx
];
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 urcu_posix_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;
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 urcu_bp_reader
*) &chunk
->readers
[0])
518 if (rcu_reader_reg
>= (struct urcu_bp_reader
*) &chunk
->readers
[chunk
->capacity
])
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
);
567 void urcu_bp_register_thread(void)
569 if (caa_unlikely(!URCU_TLS(urcu_bp_reader
)))
570 urcu_bp_register(); /* If not yet registered. */
573 /* Disable signals, take mutex, remove from registry */
575 void urcu_bp_unregister(struct rcu_reader
*rcu_reader_reg
)
577 sigset_t newmask
, oldmask
;
580 ret
= sigfillset(&newmask
);
583 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
587 mutex_lock(&rcu_registry_lock
);
588 remove_thread(rcu_reader_reg
);
589 mutex_unlock(&rcu_registry_lock
);
590 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
597 * Remove thread from the registry when it exits, and flag it as
598 * destroyed so garbage collection can take care of it.
601 void urcu_bp_thread_exit_notifier(void *rcu_key
)
603 urcu_bp_unregister(rcu_key
);
606 #ifdef CONFIG_RCU_FORCE_SYS_MEMBARRIER
608 void urcu_bp_sys_membarrier_status(bool available
)
615 void urcu_bp_sys_membarrier_status(bool available
)
619 urcu_bp_has_sys_membarrier
= 1;
624 void urcu_bp_sys_membarrier_init(void)
626 bool available
= false;
629 mask
= membarrier(MEMBARRIER_CMD_QUERY
, 0);
631 if (mask
& MEMBARRIER_CMD_PRIVATE_EXPEDITED
) {
632 if (membarrier(MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED
, 0))
637 urcu_bp_sys_membarrier_status(available
);
641 void _urcu_bp_init(void)
643 mutex_lock(&init_lock
);
644 if (!urcu_bp_refcount
++) {
647 ret
= pthread_key_create(&urcu_bp_key
,
648 urcu_bp_thread_exit_notifier
);
651 urcu_bp_sys_membarrier_init();
654 mutex_unlock(&init_lock
);
658 void urcu_bp_exit(void)
660 mutex_lock(&init_lock
);
661 if (!--urcu_bp_refcount
) {
662 struct registry_chunk
*chunk
, *tmp
;
665 cds_list_for_each_entry_safe(chunk
, tmp
,
666 ®istry_arena
.chunk_list
, node
) {
667 munmap((void *) chunk
, chunk_allocation_size(chunk
->capacity
));
669 CDS_INIT_LIST_HEAD(®istry_arena
.chunk_list
);
670 ret
= pthread_key_delete(urcu_bp_key
);
674 mutex_unlock(&init_lock
);
678 void urcu_bp_exit_destructor(void)
680 urcu_call_rcu_exit();
685 * Holding the rcu_gp_lock and rcu_registry_lock across fork will make
686 * sure we fork() don't race with a concurrent thread executing with
687 * any of those locks held. This ensures that the registry and data
688 * protected by rcu_gp_lock are in a coherent state in the child.
690 void urcu_bp_before_fork(void)
692 sigset_t newmask
, oldmask
;
695 ret
= sigfillset(&newmask
);
696 urcu_posix_assert(!ret
);
697 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
698 urcu_posix_assert(!ret
);
699 mutex_lock(&rcu_gp_lock
);
700 mutex_lock(&rcu_registry_lock
);
701 saved_fork_signal_mask
= oldmask
;
704 void urcu_bp_after_fork_parent(void)
709 oldmask
= saved_fork_signal_mask
;
710 mutex_unlock(&rcu_registry_lock
);
711 mutex_unlock(&rcu_gp_lock
);
712 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
713 urcu_posix_assert(!ret
);
717 * Prune all entries from registry except our own thread. Fits the Linux
718 * fork behavior. Called with rcu_gp_lock and rcu_registry_lock held.
721 void urcu_bp_prune_registry(void)
723 struct registry_chunk
*chunk
;
725 cds_list_for_each_entry(chunk
, ®istry_arena
.chunk_list
, node
) {
728 for (spot_idx
= 0; spot_idx
< chunk
->capacity
; spot_idx
++) {
729 struct urcu_bp_reader
*reader
= &chunk
->readers
[spot_idx
];
733 if (reader
->tid
== pthread_self())
735 cleanup_thread(chunk
, reader
);
740 void urcu_bp_after_fork_child(void)
745 urcu_bp_prune_registry();
746 oldmask
= saved_fork_signal_mask
;
747 mutex_unlock(&rcu_registry_lock
);
748 mutex_unlock(&rcu_gp_lock
);
749 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
750 urcu_posix_assert(!ret
);
753 void *urcu_bp_dereference_sym(void *p
)
755 return _rcu_dereference(p
);
758 void *urcu_bp_set_pointer_sym(void **p
, void *v
)
765 void *urcu_bp_xchg_pointer_sym(void **p
, void *v
)
768 return uatomic_xchg(p
, v
);
771 void *urcu_bp_cmpxchg_pointer_sym(void **p
, void *old
, void *_new
)
774 return uatomic_cmpxchg(p
, old
, _new
);
777 DEFINE_RCU_FLAVOR(rcu_flavor
);
779 #include "urcu-call-rcu-impl.h"
780 #include "urcu-defer-impl.h"
781 #include "urcu-poll-impl.h"