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_NR_THREADS 8
92 #define ARENA_INIT_ALLOC \
93 sizeof(struct registry_chunk) \
94 + INIT_NR_THREADS * sizeof(struct urcu_bp_reader)
97 * Active attempts to check for reader Q.S. before calling sleep().
99 #define RCU_QS_ACTIVE_ATTEMPTS 100
102 int urcu_bp_refcount
;
104 /* If the headers do not support membarrier system call, fall back smp_mb. */
105 #ifdef __NR_membarrier
106 # define membarrier(...) syscall(__NR_membarrier, __VA_ARGS__)
108 # define membarrier(...) -ENOSYS
111 enum membarrier_cmd
{
112 MEMBARRIER_CMD_QUERY
= 0,
113 MEMBARRIER_CMD_SHARED
= (1 << 0),
114 /* reserved for MEMBARRIER_CMD_SHARED_EXPEDITED (1 << 1) */
115 /* reserved for MEMBARRIER_CMD_PRIVATE (1 << 2) */
116 MEMBARRIER_CMD_PRIVATE_EXPEDITED
= (1 << 3),
117 MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED
= (1 << 4),
121 void __attribute__((constructor
)) _urcu_bp_init(void);
123 void __attribute__((destructor
)) urcu_bp_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 data_len
; /* data length */
162 size_t used
; /* amount of data used */
163 struct cds_list_head node
; /* chunk_list node */
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))
215 * Always called with rcu_registry lock held. Releases this lock between
216 * iterations and grabs it again. Holds the lock when it returns.
218 static void wait_for_readers(struct cds_list_head
*input_readers
,
219 struct cds_list_head
*cur_snap_readers
,
220 struct cds_list_head
*qsreaders
)
222 unsigned int wait_loops
= 0;
223 struct urcu_bp_reader
*index
, *tmp
;
226 * Wait for each thread URCU_TLS(urcu_bp_reader).ctr to either
227 * indicate quiescence (not nested), or observe the current
231 if (wait_loops
< RCU_QS_ACTIVE_ATTEMPTS
)
234 cds_list_for_each_entry_safe(index
, tmp
, input_readers
, node
) {
235 switch (urcu_bp_reader_state(&index
->ctr
)) {
236 case URCU_BP_READER_ACTIVE_CURRENT
:
237 if (cur_snap_readers
) {
238 cds_list_move(&index
->node
,
243 case URCU_BP_READER_INACTIVE
:
244 cds_list_move(&index
->node
, qsreaders
);
246 case URCU_BP_READER_ACTIVE_OLD
:
248 * Old snapshot. Leaving node in
249 * input_readers will make us busy-loop
250 * until the snapshot becomes current or
251 * the reader becomes inactive.
257 if (cds_list_empty(input_readers
)) {
260 /* Temporarily unlock the registry lock. */
261 mutex_unlock(&rcu_registry_lock
);
262 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
)
263 (void) poll(NULL
, 0, RCU_SLEEP_DELAY_MS
);
266 /* Re-lock the registry lock before the next loop. */
267 mutex_lock(&rcu_registry_lock
);
272 void urcu_bp_synchronize_rcu(void)
274 CDS_LIST_HEAD(cur_snap_readers
);
275 CDS_LIST_HEAD(qsreaders
);
276 sigset_t newmask
, oldmask
;
279 ret
= sigfillset(&newmask
);
280 urcu_posix_assert(!ret
);
281 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
282 urcu_posix_assert(!ret
);
284 mutex_lock(&rcu_gp_lock
);
286 mutex_lock(&rcu_registry_lock
);
288 if (cds_list_empty(®istry
))
291 /* All threads should read qparity before accessing data structure
292 * where new ptr points to. */
293 /* Write new ptr before changing the qparity */
297 * Wait for readers to observe original parity or be quiescent.
298 * wait_for_readers() can release and grab again rcu_registry_lock
301 wait_for_readers(®istry
, &cur_snap_readers
, &qsreaders
);
304 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
305 * model easier to understand. It does not have a big performance impact
306 * anyway, given this is the write-side.
310 /* Switch parity: 0 -> 1, 1 -> 0 */
311 CMM_STORE_SHARED(rcu_gp
.ctr
, rcu_gp
.ctr
^ URCU_BP_GP_CTR_PHASE
);
314 * Must commit qparity update to memory before waiting for other parity
315 * quiescent state. Failure to do so could result in the writer waiting
316 * forever while new readers are always accessing data (no progress).
317 * Ensured by CMM_STORE_SHARED and CMM_LOAD_SHARED.
321 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
322 * model easier to understand. It does not have a big performance impact
323 * anyway, given this is the write-side.
328 * Wait for readers to observe new parity or be quiescent.
329 * wait_for_readers() can release and grab again rcu_registry_lock
332 wait_for_readers(&cur_snap_readers
, NULL
, &qsreaders
);
335 * Put quiescent reader list back into registry.
337 cds_list_splice(&qsreaders
, ®istry
);
340 * Finish waiting for reader threads before letting the old ptr being
345 mutex_unlock(&rcu_registry_lock
);
346 mutex_unlock(&rcu_gp_lock
);
347 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
348 urcu_posix_assert(!ret
);
352 * library wrappers to be used by non-LGPL compatible source code.
355 void urcu_bp_read_lock(void)
357 _urcu_bp_read_lock();
360 void urcu_bp_read_unlock(void)
362 _urcu_bp_read_unlock();
365 int urcu_bp_read_ongoing(void)
367 return _urcu_bp_read_ongoing();
371 * Only grow for now. If empty, allocate a ARENA_INIT_ALLOC sized chunk.
372 * Else, try expanding the last chunk. If this fails, allocate a new
373 * chunk twice as big as the last chunk.
374 * Memory used by chunks _never_ moves. A chunk could theoretically be
375 * freed when all "used" slots are released, but we don't do it at this
379 void expand_arena(struct registry_arena
*arena
)
381 struct registry_chunk
*new_chunk
, *last_chunk
;
382 size_t old_chunk_len
, new_chunk_len
;
385 if (cds_list_empty(&arena
->chunk_list
)) {
386 urcu_posix_assert(ARENA_INIT_ALLOC
>=
387 sizeof(struct registry_chunk
)
388 + sizeof(struct rcu_reader
));
389 new_chunk_len
= ARENA_INIT_ALLOC
;
390 new_chunk
= (struct registry_chunk
*) mmap(NULL
,
392 PROT_READ
| PROT_WRITE
,
393 MAP_ANONYMOUS
| MAP_PRIVATE
,
395 if (new_chunk
== MAP_FAILED
)
397 memset(new_chunk
, 0, new_chunk_len
);
398 new_chunk
->data_len
=
399 new_chunk_len
- sizeof(struct registry_chunk
);
400 cds_list_add_tail(&new_chunk
->node
, &arena
->chunk_list
);
401 return; /* We're done. */
404 /* Try expanding last chunk. */
405 last_chunk
= cds_list_entry(arena
->chunk_list
.prev
,
406 struct registry_chunk
, node
);
408 last_chunk
->data_len
+ sizeof(struct registry_chunk
);
409 new_chunk_len
= old_chunk_len
<< 1;
411 /* Don't allow memory mapping to move, just expand. */
412 new_chunk
= mremap_wrapper(last_chunk
, old_chunk_len
,
414 if (new_chunk
!= MAP_FAILED
) {
415 /* Should not have moved. */
416 urcu_posix_assert(new_chunk
== last_chunk
);
417 memset((char *) last_chunk
+ old_chunk_len
, 0,
418 new_chunk_len
- old_chunk_len
);
419 last_chunk
->data_len
=
420 new_chunk_len
- sizeof(struct registry_chunk
);
421 return; /* We're done. */
424 /* Remap did not succeed, we need to add a new chunk. */
425 new_chunk
= (struct registry_chunk
*) mmap(NULL
,
427 PROT_READ
| PROT_WRITE
,
428 MAP_ANONYMOUS
| MAP_PRIVATE
,
430 if (new_chunk
== MAP_FAILED
)
432 memset(new_chunk
, 0, new_chunk_len
);
433 new_chunk
->data_len
=
434 new_chunk_len
- sizeof(struct registry_chunk
);
435 cds_list_add_tail(&new_chunk
->node
, &arena
->chunk_list
);
439 struct rcu_reader
*arena_alloc(struct registry_arena
*arena
)
441 struct registry_chunk
*chunk
;
442 struct rcu_reader
*rcu_reader_reg
;
443 int expand_done
= 0; /* Only allow to expand once per alloc */
444 size_t len
= sizeof(struct rcu_reader
);
447 cds_list_for_each_entry(chunk
, &arena
->chunk_list
, node
) {
448 if (chunk
->data_len
- chunk
->used
< len
)
451 for (rcu_reader_reg
= (struct rcu_reader
*) &chunk
->data
[0];
452 rcu_reader_reg
< (struct rcu_reader
*) &chunk
->data
[chunk
->data_len
];
454 if (!rcu_reader_reg
->alloc
) {
455 rcu_reader_reg
->alloc
= 1;
457 return rcu_reader_reg
;
471 /* Called with signals off and mutex locked */
473 void add_thread(void)
475 struct rcu_reader
*rcu_reader_reg
;
478 rcu_reader_reg
= arena_alloc(®istry_arena
);
481 ret
= pthread_setspecific(urcu_bp_key
, rcu_reader_reg
);
485 /* Add to registry */
486 rcu_reader_reg
->tid
= pthread_self();
487 urcu_posix_assert(rcu_reader_reg
->ctr
== 0);
488 cds_list_add(&rcu_reader_reg
->node
, ®istry
);
490 * Reader threads are pointing to the reader registry. This is
491 * why its memory should never be relocated.
493 URCU_TLS(urcu_bp_reader
) = rcu_reader_reg
;
496 /* Called with mutex locked */
498 void cleanup_thread(struct registry_chunk
*chunk
,
499 struct rcu_reader
*rcu_reader_reg
)
501 rcu_reader_reg
->ctr
= 0;
502 cds_list_del(&rcu_reader_reg
->node
);
503 rcu_reader_reg
->tid
= 0;
504 rcu_reader_reg
->alloc
= 0;
505 chunk
->used
-= sizeof(struct rcu_reader
);
509 struct registry_chunk
*find_chunk(struct rcu_reader
*rcu_reader_reg
)
511 struct registry_chunk
*chunk
;
513 cds_list_for_each_entry(chunk
, ®istry_arena
.chunk_list
, node
) {
514 if (rcu_reader_reg
< (struct rcu_reader
*) &chunk
->data
[0])
516 if (rcu_reader_reg
>= (struct rcu_reader
*) &chunk
->data
[chunk
->data_len
])
523 /* Called with signals off and mutex locked */
525 void remove_thread(struct rcu_reader
*rcu_reader_reg
)
527 cleanup_thread(find_chunk(rcu_reader_reg
), rcu_reader_reg
);
528 URCU_TLS(urcu_bp_reader
) = NULL
;
531 /* Disable signals, take mutex, add to registry */
532 void urcu_bp_register(void)
534 sigset_t newmask
, oldmask
;
537 ret
= sigfillset(&newmask
);
540 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
545 * Check if a signal concurrently registered our thread since
546 * the check in rcu_read_lock().
548 if (URCU_TLS(urcu_bp_reader
))
552 * Take care of early registration before urcu_bp constructor.
556 mutex_lock(&rcu_registry_lock
);
558 mutex_unlock(&rcu_registry_lock
);
560 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
565 void urcu_bp_register_thread(void)
567 if (caa_unlikely(!URCU_TLS(urcu_bp_reader
)))
568 urcu_bp_register(); /* If not yet registered. */
571 /* Disable signals, take mutex, remove from registry */
573 void urcu_bp_unregister(struct rcu_reader
*rcu_reader_reg
)
575 sigset_t newmask
, oldmask
;
578 ret
= sigfillset(&newmask
);
581 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
585 mutex_lock(&rcu_registry_lock
);
586 remove_thread(rcu_reader_reg
);
587 mutex_unlock(&rcu_registry_lock
);
588 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
595 * Remove thread from the registry when it exits, and flag it as
596 * destroyed so garbage collection can take care of it.
599 void urcu_bp_thread_exit_notifier(void *rcu_key
)
601 urcu_bp_unregister(rcu_key
);
604 #ifdef CONFIG_RCU_FORCE_SYS_MEMBARRIER
606 void urcu_bp_sys_membarrier_status(bool available
)
613 void urcu_bp_sys_membarrier_status(bool available
)
617 urcu_bp_has_sys_membarrier
= 1;
622 void urcu_bp_sys_membarrier_init(void)
624 bool available
= false;
627 mask
= membarrier(MEMBARRIER_CMD_QUERY
, 0);
629 if (mask
& MEMBARRIER_CMD_PRIVATE_EXPEDITED
) {
630 if (membarrier(MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED
, 0))
635 urcu_bp_sys_membarrier_status(available
);
639 void _urcu_bp_init(void)
641 mutex_lock(&init_lock
);
642 if (!urcu_bp_refcount
++) {
645 ret
= pthread_key_create(&urcu_bp_key
,
646 urcu_bp_thread_exit_notifier
);
649 urcu_bp_sys_membarrier_init();
652 mutex_unlock(&init_lock
);
656 void urcu_bp_exit(void)
658 mutex_lock(&init_lock
);
659 if (!--urcu_bp_refcount
) {
660 struct registry_chunk
*chunk
, *tmp
;
663 cds_list_for_each_entry_safe(chunk
, tmp
,
664 ®istry_arena
.chunk_list
, node
) {
665 munmap((void *) chunk
, chunk
->data_len
666 + sizeof(struct registry_chunk
));
668 CDS_INIT_LIST_HEAD(®istry_arena
.chunk_list
);
669 ret
= pthread_key_delete(urcu_bp_key
);
673 mutex_unlock(&init_lock
);
677 * Holding the rcu_gp_lock and rcu_registry_lock across fork will make
678 * sure we fork() don't race with a concurrent thread executing with
679 * any of those locks held. This ensures that the registry and data
680 * protected by rcu_gp_lock are in a coherent state in the child.
682 void urcu_bp_before_fork(void)
684 sigset_t newmask
, oldmask
;
687 ret
= sigfillset(&newmask
);
688 urcu_posix_assert(!ret
);
689 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
690 urcu_posix_assert(!ret
);
691 mutex_lock(&rcu_gp_lock
);
692 mutex_lock(&rcu_registry_lock
);
693 saved_fork_signal_mask
= oldmask
;
696 void urcu_bp_after_fork_parent(void)
701 oldmask
= saved_fork_signal_mask
;
702 mutex_unlock(&rcu_registry_lock
);
703 mutex_unlock(&rcu_gp_lock
);
704 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
705 urcu_posix_assert(!ret
);
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
);
741 urcu_posix_assert(!ret
);
744 void *urcu_bp_dereference_sym(void *p
)
746 return _rcu_dereference(p
);
749 void *urcu_bp_set_pointer_sym(void **p
, void *v
)
756 void *urcu_bp_xchg_pointer_sym(void **p
, void *v
)
759 return uatomic_xchg(p
, v
);
762 void *urcu_bp_cmpxchg_pointer_sym(void **p
, void *old
, void *_new
)
765 return uatomic_cmpxchg(p
, old
, _new
);
768 DEFINE_RCU_FLAVOR(rcu_flavor
);
770 #include "urcu-call-rcu-impl.h"
771 #include "urcu-defer-impl.h"