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);
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
};
154 * Pointer to registry elements. Written to only by each individual reader. Read
155 * by both the reader and the writers.
157 DEFINE_URCU_TLS(struct urcu_bp_reader
*, urcu_bp_reader
);
159 static CDS_LIST_HEAD(registry
);
161 struct registry_chunk
{
162 size_t data_len
; /* data length */
163 size_t used
; /* amount of data used */
164 struct cds_list_head node
; /* chunk_list node */
168 struct registry_arena
{
169 struct cds_list_head chunk_list
;
172 static struct registry_arena registry_arena
= {
173 .chunk_list
= CDS_LIST_HEAD_INIT(registry_arena
.chunk_list
),
176 /* Saved fork signal mask, protected by rcu_gp_lock */
177 static sigset_t saved_fork_signal_mask
;
179 static void mutex_lock(pthread_mutex_t
*mutex
)
183 #ifndef DISTRUST_SIGNALS_EXTREME
184 ret
= pthread_mutex_lock(mutex
);
187 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
188 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
189 if (ret
!= EBUSY
&& ret
!= EINTR
)
193 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
196 static void mutex_unlock(pthread_mutex_t
*mutex
)
200 ret
= pthread_mutex_unlock(mutex
);
205 static void smp_mb_master(void)
207 if (caa_likely(urcu_bp_has_sys_membarrier
)) {
208 if (membarrier(MEMBARRIER_CMD_PRIVATE_EXPEDITED
, 0))
216 * Always called with rcu_registry lock held. Releases this lock between
217 * iterations and grabs it again. Holds the lock when it returns.
219 static void wait_for_readers(struct cds_list_head
*input_readers
,
220 struct cds_list_head
*cur_snap_readers
,
221 struct cds_list_head
*qsreaders
)
223 unsigned int wait_loops
= 0;
224 struct urcu_bp_reader
*index
, *tmp
;
227 * Wait for each thread URCU_TLS(urcu_bp_reader).ctr to either
228 * indicate quiescence (not nested), or observe the current
232 if (wait_loops
< RCU_QS_ACTIVE_ATTEMPTS
)
235 cds_list_for_each_entry_safe(index
, tmp
, input_readers
, node
) {
236 switch (urcu_bp_reader_state(&index
->ctr
)) {
237 case URCU_BP_READER_ACTIVE_CURRENT
:
238 if (cur_snap_readers
) {
239 cds_list_move(&index
->node
,
244 case URCU_BP_READER_INACTIVE
:
245 cds_list_move(&index
->node
, qsreaders
);
247 case URCU_BP_READER_ACTIVE_OLD
:
249 * Old snapshot. Leaving node in
250 * input_readers will make us busy-loop
251 * until the snapshot becomes current or
252 * the reader becomes inactive.
258 if (cds_list_empty(input_readers
)) {
261 /* Temporarily unlock the registry lock. */
262 mutex_unlock(&rcu_registry_lock
);
263 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
)
264 (void) poll(NULL
, 0, RCU_SLEEP_DELAY_MS
);
267 /* Re-lock the registry lock before the next loop. */
268 mutex_lock(&rcu_registry_lock
);
273 void urcu_bp_synchronize_rcu(void)
275 CDS_LIST_HEAD(cur_snap_readers
);
276 CDS_LIST_HEAD(qsreaders
);
277 sigset_t newmask
, oldmask
;
280 ret
= sigfillset(&newmask
);
281 urcu_posix_assert(!ret
);
282 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
283 urcu_posix_assert(!ret
);
285 mutex_lock(&rcu_gp_lock
);
287 mutex_lock(&rcu_registry_lock
);
289 if (cds_list_empty(®istry
))
292 /* All threads should read qparity before accessing data structure
293 * where new ptr points to. */
294 /* Write new ptr before changing the qparity */
298 * Wait for readers to observe original parity or be quiescent.
299 * wait_for_readers() can release and grab again rcu_registry_lock
302 wait_for_readers(®istry
, &cur_snap_readers
, &qsreaders
);
305 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
306 * model easier to understand. It does not have a big performance impact
307 * anyway, given this is the write-side.
311 /* Switch parity: 0 -> 1, 1 -> 0 */
312 CMM_STORE_SHARED(rcu_gp
.ctr
, rcu_gp
.ctr
^ URCU_BP_GP_CTR_PHASE
);
315 * Must commit qparity update to memory before waiting for other parity
316 * quiescent state. Failure to do so could result in the writer waiting
317 * forever while new readers are always accessing data (no progress).
318 * Ensured by CMM_STORE_SHARED and CMM_LOAD_SHARED.
322 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
323 * model easier to understand. It does not have a big performance impact
324 * anyway, given this is the write-side.
329 * Wait for readers to observe new parity or be quiescent.
330 * wait_for_readers() can release and grab again rcu_registry_lock
333 wait_for_readers(&cur_snap_readers
, NULL
, &qsreaders
);
336 * Put quiescent reader list back into registry.
338 cds_list_splice(&qsreaders
, ®istry
);
341 * Finish waiting for reader threads before letting the old ptr being
346 mutex_unlock(&rcu_registry_lock
);
347 mutex_unlock(&rcu_gp_lock
);
348 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
349 urcu_posix_assert(!ret
);
353 * library wrappers to be used by non-LGPL compatible source code.
356 void urcu_bp_read_lock(void)
358 _urcu_bp_read_lock();
361 void urcu_bp_read_unlock(void)
363 _urcu_bp_read_unlock();
366 int urcu_bp_read_ongoing(void)
368 return _urcu_bp_read_ongoing();
372 * Only grow for now. If empty, allocate a ARENA_INIT_ALLOC sized chunk.
373 * Else, try expanding the last chunk. If this fails, allocate a new
374 * chunk twice as big as the last chunk.
375 * Memory used by chunks _never_ moves. A chunk could theoretically be
376 * freed when all "used" slots are released, but we don't do it at this
380 void expand_arena(struct registry_arena
*arena
)
382 struct registry_chunk
*new_chunk
, *last_chunk
;
383 size_t old_chunk_len
, new_chunk_len
;
386 if (cds_list_empty(&arena
->chunk_list
)) {
387 urcu_posix_assert(ARENA_INIT_ALLOC
>=
388 sizeof(struct registry_chunk
)
389 + sizeof(struct rcu_reader
));
390 new_chunk_len
= ARENA_INIT_ALLOC
;
391 new_chunk
= (struct registry_chunk
*) mmap(NULL
,
393 PROT_READ
| PROT_WRITE
,
394 MAP_ANONYMOUS
| MAP_PRIVATE
,
396 if (new_chunk
== MAP_FAILED
)
398 memset(new_chunk
, 0, new_chunk_len
);
399 new_chunk
->data_len
=
400 new_chunk_len
- sizeof(struct registry_chunk
);
401 cds_list_add_tail(&new_chunk
->node
, &arena
->chunk_list
);
402 return; /* We're done. */
405 /* Try expanding last chunk. */
406 last_chunk
= cds_list_entry(arena
->chunk_list
.prev
,
407 struct registry_chunk
, node
);
409 last_chunk
->data_len
+ sizeof(struct registry_chunk
);
410 new_chunk_len
= old_chunk_len
<< 1;
412 /* Don't allow memory mapping to move, just expand. */
413 new_chunk
= mremap_wrapper(last_chunk
, old_chunk_len
,
415 if (new_chunk
!= MAP_FAILED
) {
416 /* Should not have moved. */
417 urcu_posix_assert(new_chunk
== last_chunk
);
418 memset((char *) last_chunk
+ old_chunk_len
, 0,
419 new_chunk_len
- old_chunk_len
);
420 last_chunk
->data_len
=
421 new_chunk_len
- sizeof(struct registry_chunk
);
422 return; /* We're done. */
425 /* Remap did not succeed, we need to add a new chunk. */
426 new_chunk
= (struct registry_chunk
*) mmap(NULL
,
428 PROT_READ
| PROT_WRITE
,
429 MAP_ANONYMOUS
| MAP_PRIVATE
,
431 if (new_chunk
== MAP_FAILED
)
433 memset(new_chunk
, 0, new_chunk_len
);
434 new_chunk
->data_len
=
435 new_chunk_len
- sizeof(struct registry_chunk
);
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 struct rcu_reader
*rcu_reader_reg
;
444 int expand_done
= 0; /* Only allow to expand once per alloc */
445 size_t len
= sizeof(struct rcu_reader
);
448 cds_list_for_each_entry(chunk
, &arena
->chunk_list
, node
) {
449 if (chunk
->data_len
- chunk
->used
< len
)
452 for (rcu_reader_reg
= (struct rcu_reader
*) &chunk
->data
[0];
453 rcu_reader_reg
< (struct rcu_reader
*) &chunk
->data
[chunk
->data_len
];
455 if (!rcu_reader_reg
->alloc
) {
456 rcu_reader_reg
->alloc
= 1;
458 return rcu_reader_reg
;
472 /* Called with signals off and mutex locked */
474 void add_thread(void)
476 struct rcu_reader
*rcu_reader_reg
;
479 rcu_reader_reg
= arena_alloc(®istry_arena
);
482 ret
= pthread_setspecific(urcu_bp_key
, rcu_reader_reg
);
486 /* Add to registry */
487 rcu_reader_reg
->tid
= pthread_self();
488 urcu_posix_assert(rcu_reader_reg
->ctr
== 0);
489 cds_list_add(&rcu_reader_reg
->node
, ®istry
);
491 * Reader threads are pointing to the reader registry. This is
492 * why its memory should never be relocated.
494 URCU_TLS(urcu_bp_reader
) = rcu_reader_reg
;
497 /* Called with mutex locked */
499 void cleanup_thread(struct registry_chunk
*chunk
,
500 struct rcu_reader
*rcu_reader_reg
)
502 rcu_reader_reg
->ctr
= 0;
503 cds_list_del(&rcu_reader_reg
->node
);
504 rcu_reader_reg
->tid
= 0;
505 rcu_reader_reg
->alloc
= 0;
506 chunk
->used
-= sizeof(struct rcu_reader
);
510 struct registry_chunk
*find_chunk(struct rcu_reader
*rcu_reader_reg
)
512 struct registry_chunk
*chunk
;
514 cds_list_for_each_entry(chunk
, ®istry_arena
.chunk_list
, node
) {
515 if (rcu_reader_reg
< (struct rcu_reader
*) &chunk
->data
[0])
517 if (rcu_reader_reg
>= (struct rcu_reader
*) &chunk
->data
[chunk
->data_len
])
524 /* Called with signals off and mutex locked */
526 void remove_thread(struct rcu_reader
*rcu_reader_reg
)
528 cleanup_thread(find_chunk(rcu_reader_reg
), rcu_reader_reg
);
529 URCU_TLS(urcu_bp_reader
) = NULL
;
532 /* Disable signals, take mutex, add to registry */
533 void urcu_bp_register(void)
535 sigset_t newmask
, oldmask
;
538 ret
= sigfillset(&newmask
);
541 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
546 * Check if a signal concurrently registered our thread since
547 * the check in rcu_read_lock().
549 if (URCU_TLS(urcu_bp_reader
))
553 * Take care of early registration before urcu_bp constructor.
557 mutex_lock(&rcu_registry_lock
);
559 mutex_unlock(&rcu_registry_lock
);
561 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
566 void urcu_bp_register_thread(void)
568 if (caa_unlikely(!URCU_TLS(urcu_bp_reader
)))
569 urcu_bp_register(); /* If not yet registered. */
572 /* Disable signals, take mutex, remove from registry */
574 void urcu_bp_unregister(struct rcu_reader
*rcu_reader_reg
)
576 sigset_t newmask
, oldmask
;
579 ret
= sigfillset(&newmask
);
582 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
586 mutex_lock(&rcu_registry_lock
);
587 remove_thread(rcu_reader_reg
);
588 mutex_unlock(&rcu_registry_lock
);
589 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
596 * Remove thread from the registry when it exits, and flag it as
597 * destroyed so garbage collection can take care of it.
600 void urcu_bp_thread_exit_notifier(void *rcu_key
)
602 urcu_bp_unregister(rcu_key
);
605 #ifdef CONFIG_RCU_FORCE_SYS_MEMBARRIER
607 void urcu_bp_sys_membarrier_status(bool available
)
614 void urcu_bp_sys_membarrier_status(bool available
)
618 urcu_bp_has_sys_membarrier
= 1;
623 void urcu_bp_sys_membarrier_init(void)
625 bool available
= false;
628 mask
= membarrier(MEMBARRIER_CMD_QUERY
, 0);
630 if (mask
& MEMBARRIER_CMD_PRIVATE_EXPEDITED
) {
631 if (membarrier(MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED
, 0))
636 urcu_bp_sys_membarrier_status(available
);
640 void _urcu_bp_init(void)
642 mutex_lock(&init_lock
);
643 if (!urcu_bp_refcount
++) {
646 ret
= pthread_key_create(&urcu_bp_key
,
647 urcu_bp_thread_exit_notifier
);
650 urcu_bp_sys_membarrier_init();
653 mutex_unlock(&init_lock
);
657 void urcu_bp_exit(void)
659 urcu_call_rcu_exit();
661 mutex_lock(&init_lock
);
662 if (!--urcu_bp_refcount
) {
663 struct registry_chunk
*chunk
, *tmp
;
666 cds_list_for_each_entry_safe(chunk
, tmp
,
667 ®istry_arena
.chunk_list
, node
) {
668 munmap((void *) chunk
, chunk
->data_len
669 + sizeof(struct registry_chunk
));
671 CDS_INIT_LIST_HEAD(®istry_arena
.chunk_list
);
672 ret
= pthread_key_delete(urcu_bp_key
);
676 mutex_unlock(&init_lock
);
680 * Holding the rcu_gp_lock and rcu_registry_lock across fork will make
681 * sure we fork() don't race with a concurrent thread executing with
682 * any of those locks held. This ensures that the registry and data
683 * protected by rcu_gp_lock are in a coherent state in the child.
685 void urcu_bp_before_fork(void)
687 sigset_t newmask
, oldmask
;
690 ret
= sigfillset(&newmask
);
691 urcu_posix_assert(!ret
);
692 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
693 urcu_posix_assert(!ret
);
694 mutex_lock(&rcu_gp_lock
);
695 mutex_lock(&rcu_registry_lock
);
696 saved_fork_signal_mask
= oldmask
;
699 void urcu_bp_after_fork_parent(void)
704 oldmask
= saved_fork_signal_mask
;
705 mutex_unlock(&rcu_registry_lock
);
706 mutex_unlock(&rcu_gp_lock
);
707 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
708 urcu_posix_assert(!ret
);
712 * Prune all entries from registry except our own thread. Fits the Linux
713 * fork behavior. Called with rcu_gp_lock and rcu_registry_lock held.
716 void urcu_bp_prune_registry(void)
718 struct registry_chunk
*chunk
;
719 struct urcu_bp_reader
*rcu_reader_reg
;
721 cds_list_for_each_entry(chunk
, ®istry_arena
.chunk_list
, node
) {
722 for (rcu_reader_reg
= (struct urcu_bp_reader
*) &chunk
->data
[0];
723 rcu_reader_reg
< (struct urcu_bp_reader
*) &chunk
->data
[chunk
->data_len
];
725 if (!rcu_reader_reg
->alloc
)
727 if (rcu_reader_reg
->tid
== pthread_self())
729 cleanup_thread(chunk
, rcu_reader_reg
);
734 void urcu_bp_after_fork_child(void)
739 urcu_bp_prune_registry();
740 oldmask
= saved_fork_signal_mask
;
741 mutex_unlock(&rcu_registry_lock
);
742 mutex_unlock(&rcu_gp_lock
);
743 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
744 urcu_posix_assert(!ret
);
747 void *urcu_bp_dereference_sym(void *p
)
749 return _rcu_dereference(p
);
752 void *urcu_bp_set_pointer_sym(void **p
, void *v
)
759 void *urcu_bp_xchg_pointer_sym(void **p
, void *v
)
762 return uatomic_xchg(p
, v
);
765 void *urcu_bp_cmpxchg_pointer_sym(void **p
, void *old
, void *_new
)
768 return uatomic_cmpxchg(p
, old
, _new
);
771 DEFINE_RCU_FLAVOR(rcu_flavor
);
773 #include "urcu-call-rcu-impl.h"
774 #include "urcu-defer-impl.h"
775 #include "urcu-poll-impl.h"