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
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 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);
124 static void urcu_call_rcu_exit(void);
126 #ifndef CONFIG_RCU_FORCE_SYS_MEMBARRIER
127 int urcu_bp_has_sys_membarrier
;
131 * rcu_gp_lock ensures mutual exclusion between threads calling
134 static pthread_mutex_t rcu_gp_lock
= PTHREAD_MUTEX_INITIALIZER
;
136 * rcu_registry_lock ensures mutual exclusion between threads
137 * registering and unregistering themselves to/from the registry, and
138 * with threads reading that registry from synchronize_rcu(). However,
139 * this lock is not held all the way through the completion of awaiting
140 * for the grace period. It is sporadically released between iterations
142 * rcu_registry_lock may nest inside rcu_gp_lock.
144 static pthread_mutex_t rcu_registry_lock
= PTHREAD_MUTEX_INITIALIZER
;
146 static pthread_mutex_t init_lock
= PTHREAD_MUTEX_INITIALIZER
;
147 static int initialized
;
149 static pthread_key_t urcu_bp_key
;
151 struct urcu_bp_gp urcu_bp_gp
= { .ctr
= URCU_BP_GP_COUNT
};
152 URCU_ATTR_ALIAS("urcu_bp_gp") extern struct urcu_bp_gp rcu_gp_bp
;
155 * Pointer to registry elements. Written to only by each individual reader. Read
156 * by both the reader and the writers.
158 DEFINE_URCU_TLS(struct urcu_bp_reader
*, urcu_bp_reader
);
159 DEFINE_URCU_TLS_ALIAS(struct urcu_bp_reader
*, urcu_bp_reader
, rcu_reader_bp
);
161 static CDS_LIST_HEAD(registry
);
163 struct registry_chunk
{
164 size_t data_len
; /* data length */
165 size_t used
; /* amount of data used */
166 struct cds_list_head node
; /* chunk_list node */
170 struct registry_arena
{
171 struct cds_list_head chunk_list
;
174 static struct registry_arena registry_arena
= {
175 .chunk_list
= CDS_LIST_HEAD_INIT(registry_arena
.chunk_list
),
178 /* Saved fork signal mask, protected by rcu_gp_lock */
179 static sigset_t saved_fork_signal_mask
;
181 static void mutex_lock(pthread_mutex_t
*mutex
)
185 #ifndef DISTRUST_SIGNALS_EXTREME
186 ret
= pthread_mutex_lock(mutex
);
189 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
190 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
191 if (ret
!= EBUSY
&& ret
!= EINTR
)
195 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
198 static void mutex_unlock(pthread_mutex_t
*mutex
)
202 ret
= pthread_mutex_unlock(mutex
);
207 static void smp_mb_master(void)
209 if (caa_likely(urcu_bp_has_sys_membarrier
)) {
210 if (membarrier(MEMBARRIER_CMD_PRIVATE_EXPEDITED
, 0))
218 * Always called with rcu_registry lock held. Releases this lock between
219 * iterations and grabs it again. Holds the lock when it returns.
221 static void wait_for_readers(struct cds_list_head
*input_readers
,
222 struct cds_list_head
*cur_snap_readers
,
223 struct cds_list_head
*qsreaders
)
225 unsigned int wait_loops
= 0;
226 struct urcu_bp_reader
*index
, *tmp
;
229 * Wait for each thread URCU_TLS(urcu_bp_reader).ctr to either
230 * indicate quiescence (not nested), or observe the current
234 if (wait_loops
< RCU_QS_ACTIVE_ATTEMPTS
)
237 cds_list_for_each_entry_safe(index
, tmp
, input_readers
, node
) {
238 switch (urcu_bp_reader_state(&index
->ctr
)) {
239 case URCU_BP_READER_ACTIVE_CURRENT
:
240 if (cur_snap_readers
) {
241 cds_list_move(&index
->node
,
246 case URCU_BP_READER_INACTIVE
:
247 cds_list_move(&index
->node
, qsreaders
);
249 case URCU_BP_READER_ACTIVE_OLD
:
251 * Old snapshot. Leaving node in
252 * input_readers will make us busy-loop
253 * until the snapshot becomes current or
254 * the reader becomes inactive.
260 if (cds_list_empty(input_readers
)) {
263 /* Temporarily unlock the registry lock. */
264 mutex_unlock(&rcu_registry_lock
);
265 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
)
266 (void) poll(NULL
, 0, RCU_SLEEP_DELAY_MS
);
269 /* Re-lock the registry lock before the next loop. */
270 mutex_lock(&rcu_registry_lock
);
275 void urcu_bp_synchronize_rcu(void)
277 CDS_LIST_HEAD(cur_snap_readers
);
278 CDS_LIST_HEAD(qsreaders
);
279 sigset_t newmask
, oldmask
;
282 ret
= sigfillset(&newmask
);
284 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
287 mutex_lock(&rcu_gp_lock
);
289 mutex_lock(&rcu_registry_lock
);
291 if (cds_list_empty(®istry
))
294 /* All threads should read qparity before accessing data structure
295 * where new ptr points to. */
296 /* Write new ptr before changing the qparity */
300 * Wait for readers to observe original parity or be quiescent.
301 * wait_for_readers() can release and grab again rcu_registry_lock
304 wait_for_readers(®istry
, &cur_snap_readers
, &qsreaders
);
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.
313 /* Switch parity: 0 -> 1, 1 -> 0 */
314 CMM_STORE_SHARED(rcu_gp
.ctr
, rcu_gp
.ctr
^ URCU_BP_GP_CTR_PHASE
);
317 * Must commit qparity update to memory before waiting for other parity
318 * quiescent state. Failure to do so could result in the writer waiting
319 * forever while new readers are always accessing data (no progress).
320 * Ensured by CMM_STORE_SHARED and CMM_LOAD_SHARED.
324 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
325 * model easier to understand. It does not have a big performance impact
326 * anyway, given this is the write-side.
331 * Wait for readers to observe new parity or be quiescent.
332 * wait_for_readers() can release and grab again rcu_registry_lock
335 wait_for_readers(&cur_snap_readers
, NULL
, &qsreaders
);
338 * Put quiescent reader list back into registry.
340 cds_list_splice(&qsreaders
, ®istry
);
343 * Finish waiting for reader threads before letting the old ptr being
348 mutex_unlock(&rcu_registry_lock
);
349 mutex_unlock(&rcu_gp_lock
);
350 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
353 URCU_ATTR_ALIAS("urcu_bp_synchronize_rcu") void synchronize_rcu_bp();
356 * library wrappers to be used by non-LGPL compatible source code.
359 void urcu_bp_read_lock(void)
361 _urcu_bp_read_lock();
363 URCU_ATTR_ALIAS("urcu_bp_read_lock") void rcu_read_lock_bp();
365 void urcu_bp_read_unlock(void)
367 _urcu_bp_read_unlock();
369 URCU_ATTR_ALIAS("urcu_bp_read_unlock") void rcu_read_unlock_bp();
371 int urcu_bp_read_ongoing(void)
373 return _urcu_bp_read_ongoing();
375 URCU_ATTR_ALIAS("urcu_bp_read_ongoing") int rcu_read_ongoing_bp();
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_len
, new_chunk_len
;
392 if (cds_list_empty(&arena
->chunk_list
)) {
393 assert(ARENA_INIT_ALLOC
>=
394 sizeof(struct registry_chunk
)
395 + sizeof(struct rcu_reader
));
396 new_chunk_len
= ARENA_INIT_ALLOC
;
397 new_chunk
= (struct registry_chunk
*) mmap(NULL
,
399 PROT_READ
| PROT_WRITE
,
400 MAP_ANONYMOUS
| MAP_PRIVATE
,
402 if (new_chunk
== MAP_FAILED
)
404 memset(new_chunk
, 0, new_chunk_len
);
405 new_chunk
->data_len
=
406 new_chunk_len
- sizeof(struct registry_chunk
);
407 cds_list_add_tail(&new_chunk
->node
, &arena
->chunk_list
);
408 return; /* We're done. */
411 /* Try expanding last chunk. */
412 last_chunk
= cds_list_entry(arena
->chunk_list
.prev
,
413 struct registry_chunk
, node
);
415 last_chunk
->data_len
+ sizeof(struct registry_chunk
);
416 new_chunk_len
= old_chunk_len
<< 1;
418 /* Don't allow memory mapping to move, just expand. */
419 new_chunk
= mremap_wrapper(last_chunk
, old_chunk_len
,
421 if (new_chunk
!= MAP_FAILED
) {
422 /* Should not have moved. */
423 assert(new_chunk
== last_chunk
);
424 memset((char *) last_chunk
+ old_chunk_len
, 0,
425 new_chunk_len
- old_chunk_len
);
426 last_chunk
->data_len
=
427 new_chunk_len
- sizeof(struct registry_chunk
);
428 return; /* We're done. */
431 /* Remap did not succeed, we need to add a new chunk. */
432 new_chunk
= (struct registry_chunk
*) mmap(NULL
,
434 PROT_READ
| PROT_WRITE
,
435 MAP_ANONYMOUS
| MAP_PRIVATE
,
437 if (new_chunk
== MAP_FAILED
)
439 memset(new_chunk
, 0, new_chunk_len
);
440 new_chunk
->data_len
=
441 new_chunk_len
- sizeof(struct registry_chunk
);
442 cds_list_add_tail(&new_chunk
->node
, &arena
->chunk_list
);
446 struct rcu_reader
*arena_alloc(struct registry_arena
*arena
)
448 struct registry_chunk
*chunk
;
449 struct rcu_reader
*rcu_reader_reg
;
450 int expand_done
= 0; /* Only allow to expand once per alloc */
451 size_t len
= sizeof(struct rcu_reader
);
454 cds_list_for_each_entry(chunk
, &arena
->chunk_list
, node
) {
455 if (chunk
->data_len
- chunk
->used
< len
)
458 for (rcu_reader_reg
= (struct rcu_reader
*) &chunk
->data
[0];
459 rcu_reader_reg
< (struct rcu_reader
*) &chunk
->data
[chunk
->data_len
];
461 if (!rcu_reader_reg
->alloc
) {
462 rcu_reader_reg
->alloc
= 1;
464 return rcu_reader_reg
;
478 /* Called with signals off and mutex locked */
480 void add_thread(void)
482 struct rcu_reader
*rcu_reader_reg
;
485 rcu_reader_reg
= arena_alloc(®istry_arena
);
488 ret
= pthread_setspecific(urcu_bp_key
, rcu_reader_reg
);
492 /* Add to registry */
493 rcu_reader_reg
->tid
= pthread_self();
494 assert(rcu_reader_reg
->ctr
== 0);
495 cds_list_add(&rcu_reader_reg
->node
, ®istry
);
497 * Reader threads are pointing to the reader registry. This is
498 * why its memory should never be relocated.
500 URCU_TLS(urcu_bp_reader
) = rcu_reader_reg
;
503 /* Called with mutex locked */
505 void cleanup_thread(struct registry_chunk
*chunk
,
506 struct rcu_reader
*rcu_reader_reg
)
508 rcu_reader_reg
->ctr
= 0;
509 cds_list_del(&rcu_reader_reg
->node
);
510 rcu_reader_reg
->tid
= 0;
511 rcu_reader_reg
->alloc
= 0;
512 chunk
->used
-= sizeof(struct rcu_reader
);
516 struct registry_chunk
*find_chunk(struct rcu_reader
*rcu_reader_reg
)
518 struct registry_chunk
*chunk
;
520 cds_list_for_each_entry(chunk
, ®istry_arena
.chunk_list
, node
) {
521 if (rcu_reader_reg
< (struct rcu_reader
*) &chunk
->data
[0])
523 if (rcu_reader_reg
>= (struct rcu_reader
*) &chunk
->data
[chunk
->data_len
])
530 /* Called with signals off and mutex locked */
532 void remove_thread(struct rcu_reader
*rcu_reader_reg
)
534 cleanup_thread(find_chunk(rcu_reader_reg
), rcu_reader_reg
);
535 URCU_TLS(urcu_bp_reader
) = NULL
;
538 /* Disable signals, take mutex, add to registry */
539 void urcu_bp_register(void)
541 sigset_t newmask
, oldmask
;
544 ret
= sigfillset(&newmask
);
547 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
552 * Check if a signal concurrently registered our thread since
553 * the check in rcu_read_lock().
555 if (URCU_TLS(urcu_bp_reader
))
559 * Take care of early registration before urcu_bp constructor.
563 mutex_lock(&rcu_registry_lock
);
565 mutex_unlock(&rcu_registry_lock
);
567 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
571 URCU_ATTR_ALIAS("urcu_bp_register") void rcu_bp_register();
573 void urcu_bp_register_thread(void)
575 if (caa_unlikely(!URCU_TLS(urcu_bp_reader
)))
576 urcu_bp_register(); /* If not yet registered. */
579 /* Disable signals, take mutex, remove from registry */
581 void urcu_bp_unregister(struct rcu_reader
*rcu_reader_reg
)
583 sigset_t newmask
, oldmask
;
586 ret
= sigfillset(&newmask
);
589 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
593 mutex_lock(&rcu_registry_lock
);
594 remove_thread(rcu_reader_reg
);
595 mutex_unlock(&rcu_registry_lock
);
596 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
603 * Remove thread from the registry when it exits, and flag it as
604 * destroyed so garbage collection can take care of it.
607 void urcu_bp_thread_exit_notifier(void *rcu_key
)
609 urcu_bp_unregister(rcu_key
);
612 #ifdef CONFIG_RCU_FORCE_SYS_MEMBARRIER
614 void urcu_bp_sys_membarrier_status(bool available
)
621 void urcu_bp_sys_membarrier_status(bool available
)
625 urcu_bp_has_sys_membarrier
= 1;
630 void urcu_bp_sys_membarrier_init(void)
632 bool available
= false;
635 mask
= membarrier(MEMBARRIER_CMD_QUERY
, 0);
637 if (mask
& MEMBARRIER_CMD_PRIVATE_EXPEDITED
) {
638 if (membarrier(MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED
, 0))
643 urcu_bp_sys_membarrier_status(available
);
647 void _urcu_bp_init(void)
649 mutex_lock(&init_lock
);
650 if (!urcu_bp_refcount
++) {
653 ret
= pthread_key_create(&urcu_bp_key
,
654 urcu_bp_thread_exit_notifier
);
657 urcu_bp_sys_membarrier_init();
660 mutex_unlock(&init_lock
);
664 void urcu_bp_exit(void)
666 urcu_call_rcu_exit();
668 mutex_lock(&init_lock
);
669 if (!--urcu_bp_refcount
) {
670 struct registry_chunk
*chunk
, *tmp
;
673 cds_list_for_each_entry_safe(chunk
, tmp
,
674 ®istry_arena
.chunk_list
, node
) {
675 munmap((void *) chunk
, chunk
->data_len
676 + sizeof(struct registry_chunk
));
678 CDS_INIT_LIST_HEAD(®istry_arena
.chunk_list
);
679 ret
= pthread_key_delete(urcu_bp_key
);
683 mutex_unlock(&init_lock
);
687 * Holding the rcu_gp_lock and rcu_registry_lock across fork will make
688 * sure we fork() don't race with a concurrent thread executing with
689 * any of those locks held. This ensures that the registry and data
690 * protected by rcu_gp_lock are in a coherent state in the child.
692 void urcu_bp_before_fork(void)
694 sigset_t newmask
, oldmask
;
697 ret
= sigfillset(&newmask
);
699 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
701 mutex_lock(&rcu_gp_lock
);
702 mutex_lock(&rcu_registry_lock
);
703 saved_fork_signal_mask
= oldmask
;
705 URCU_ATTR_ALIAS("urcu_bp_before_fork") void rcu_bp_before_fork();
707 void urcu_bp_after_fork_parent(void)
712 oldmask
= saved_fork_signal_mask
;
713 mutex_unlock(&rcu_registry_lock
);
714 mutex_unlock(&rcu_gp_lock
);
715 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
718 URCU_ATTR_ALIAS("urcu_bp_after_fork_parent")
719 void rcu_bp_after_fork_parent(void);
722 * Prune all entries from registry except our own thread. Fits the Linux
723 * fork behavior. Called with rcu_gp_lock and rcu_registry_lock held.
726 void urcu_bp_prune_registry(void)
728 struct registry_chunk
*chunk
;
729 struct urcu_bp_reader
*rcu_reader_reg
;
731 cds_list_for_each_entry(chunk
, ®istry_arena
.chunk_list
, node
) {
732 for (rcu_reader_reg
= (struct urcu_bp_reader
*) &chunk
->data
[0];
733 rcu_reader_reg
< (struct urcu_bp_reader
*) &chunk
->data
[chunk
->data_len
];
735 if (!rcu_reader_reg
->alloc
)
737 if (rcu_reader_reg
->tid
== pthread_self())
739 cleanup_thread(chunk
, rcu_reader_reg
);
744 void urcu_bp_after_fork_child(void)
749 urcu_bp_prune_registry();
750 oldmask
= saved_fork_signal_mask
;
751 mutex_unlock(&rcu_registry_lock
);
752 mutex_unlock(&rcu_gp_lock
);
753 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
756 URCU_ATTR_ALIAS("urcu_bp_after_fork_child")
757 void rcu_bp_after_fork_child(void);
759 void *urcu_bp_dereference_sym(void *p
)
761 return _rcu_dereference(p
);
763 URCU_ATTR_ALIAS("urcu_bp_dereference_sym")
764 void *rcu_dereference_sym_bp();
766 void *urcu_bp_set_pointer_sym(void **p
, void *v
)
772 URCU_ATTR_ALIAS("urcu_bp_set_pointer_sym")
773 void *rcu_set_pointer_sym_bp();
775 void *urcu_bp_xchg_pointer_sym(void **p
, void *v
)
778 return uatomic_xchg(p
, v
);
780 URCU_ATTR_ALIAS("urcu_bp_xchg_pointer_sym")
781 void *rcu_xchg_pointer_sym_bp();
783 void *urcu_bp_cmpxchg_pointer_sym(void **p
, void *old
, void *_new
)
786 return uatomic_cmpxchg(p
, old
, _new
);
788 URCU_ATTR_ALIAS("urcu_bp_cmpxchg_pointer_sym")
789 void *rcu_cmpxchg_pointer_sym_bp();
791 DEFINE_RCU_FLAVOR(rcu_flavor
);
792 DEFINE_RCU_FLAVOR_ALIAS(rcu_flavor
, alias_rcu_flavor
);
794 #include "urcu-call-rcu-impl.h"
795 #include "urcu-defer-impl.h"