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.
38 #include "urcu/arch.h"
39 #include "urcu/wfcqueue.h"
40 #include "urcu/map/urcu-bp.h"
41 #include "urcu/static/urcu-bp.h"
42 #include "urcu-pointer.h"
43 #include "urcu/tls-compat.h"
47 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
53 #define MAP_ANONYMOUS MAP_ANON
58 void *mremap_wrapper(void *old_address
, size_t old_size
,
59 size_t new_size
, int flags
)
61 return mremap(old_address
, old_size
, new_size
, flags
);
65 #define MREMAP_MAYMOVE 1
66 #define MREMAP_FIXED 2
69 * mremap wrapper for non-Linux systems not allowing MAYMOVE.
70 * This is not generic.
73 void *mremap_wrapper(void *old_address
, size_t old_size
,
74 size_t new_size
, int flags
)
76 assert(!(flags
& MREMAP_MAYMOVE
));
82 /* Sleep delay in ms */
83 #define RCU_SLEEP_DELAY_MS 10
84 #define INIT_NR_THREADS 8
85 #define ARENA_INIT_ALLOC \
86 sizeof(struct registry_chunk) \
87 + INIT_NR_THREADS * sizeof(struct rcu_reader)
90 * Active attempts to check for reader Q.S. before calling sleep().
92 #define RCU_QS_ACTIVE_ATTEMPTS 100
97 /* If the headers do not support membarrier system call, fall back smp_mb. */
98 #ifdef __NR_membarrier
99 # define membarrier(...) syscall(__NR_membarrier, __VA_ARGS__)
101 # define membarrier(...) -ENOSYS
104 enum membarrier_cmd
{
105 MEMBARRIER_CMD_QUERY
= 0,
106 MEMBARRIER_CMD_SHARED
= (1 << 0),
110 void __attribute__((constructor
)) rcu_bp_init(void);
112 void __attribute__((destructor
)) rcu_bp_exit(void);
114 #ifndef CONFIG_RCU_FORCE_SYS_MEMBARRIER
115 int urcu_bp_has_sys_membarrier
;
119 * rcu_gp_lock ensures mutual exclusion between threads calling
122 static pthread_mutex_t rcu_gp_lock
= PTHREAD_MUTEX_INITIALIZER
;
124 * rcu_registry_lock ensures mutual exclusion between threads
125 * registering and unregistering themselves to/from the registry, and
126 * with threads reading that registry from synchronize_rcu(). However,
127 * this lock is not held all the way through the completion of awaiting
128 * for the grace period. It is sporadically released between iterations
130 * rcu_registry_lock may nest inside rcu_gp_lock.
132 static pthread_mutex_t rcu_registry_lock
= PTHREAD_MUTEX_INITIALIZER
;
134 static pthread_mutex_t init_lock
= PTHREAD_MUTEX_INITIALIZER
;
135 static int initialized
;
137 static pthread_key_t urcu_bp_key
;
139 struct rcu_gp rcu_gp
= { .ctr
= RCU_GP_COUNT
};
142 * Pointer to registry elements. Written to only by each individual reader. Read
143 * by both the reader and the writers.
145 DEFINE_URCU_TLS(struct rcu_reader
*, rcu_reader
);
147 static CDS_LIST_HEAD(registry
);
149 struct registry_chunk
{
150 size_t data_len
; /* data length */
151 size_t used
; /* amount of data used */
152 struct cds_list_head node
; /* chunk_list node */
156 struct registry_arena
{
157 struct cds_list_head chunk_list
;
160 static struct registry_arena registry_arena
= {
161 .chunk_list
= CDS_LIST_HEAD_INIT(registry_arena
.chunk_list
),
164 /* Saved fork signal mask, protected by rcu_gp_lock */
165 static sigset_t saved_fork_signal_mask
;
167 static void mutex_lock(pthread_mutex_t
*mutex
)
171 #ifndef DISTRUST_SIGNALS_EXTREME
172 ret
= pthread_mutex_lock(mutex
);
175 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
176 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
177 if (ret
!= EBUSY
&& ret
!= EINTR
)
181 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
184 static void mutex_unlock(pthread_mutex_t
*mutex
)
188 ret
= pthread_mutex_unlock(mutex
);
193 static void smp_mb_master(void)
195 if (caa_likely(urcu_bp_has_sys_membarrier
))
196 (void) membarrier(MEMBARRIER_CMD_SHARED
, 0);
202 * Always called with rcu_registry lock held. Releases this lock between
203 * iterations and grabs it again. Holds the lock when it returns.
205 static void wait_for_readers(struct cds_list_head
*input_readers
,
206 struct cds_list_head
*cur_snap_readers
,
207 struct cds_list_head
*qsreaders
)
209 unsigned int wait_loops
= 0;
210 struct rcu_reader
*index
, *tmp
;
213 * Wait for each thread URCU_TLS(rcu_reader).ctr to either
214 * indicate quiescence (not nested), or observe the current
218 if (wait_loops
< RCU_QS_ACTIVE_ATTEMPTS
)
221 cds_list_for_each_entry_safe(index
, tmp
, input_readers
, node
) {
222 switch (rcu_reader_state(&index
->ctr
)) {
223 case RCU_READER_ACTIVE_CURRENT
:
224 if (cur_snap_readers
) {
225 cds_list_move(&index
->node
,
230 case RCU_READER_INACTIVE
:
231 cds_list_move(&index
->node
, qsreaders
);
233 case RCU_READER_ACTIVE_OLD
:
235 * Old snapshot. Leaving node in
236 * input_readers will make us busy-loop
237 * until the snapshot becomes current or
238 * the reader becomes inactive.
244 if (cds_list_empty(input_readers
)) {
247 /* Temporarily unlock the registry lock. */
248 mutex_unlock(&rcu_registry_lock
);
249 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
)
250 (void) poll(NULL
, 0, RCU_SLEEP_DELAY_MS
);
253 /* Re-lock the registry lock before the next loop. */
254 mutex_lock(&rcu_registry_lock
);
259 void synchronize_rcu(void)
261 CDS_LIST_HEAD(cur_snap_readers
);
262 CDS_LIST_HEAD(qsreaders
);
263 sigset_t newmask
, oldmask
;
266 ret
= sigfillset(&newmask
);
268 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
271 mutex_lock(&rcu_gp_lock
);
273 mutex_lock(&rcu_registry_lock
);
275 if (cds_list_empty(®istry
))
278 /* All threads should read qparity before accessing data structure
279 * where new ptr points to. */
280 /* Write new ptr before changing the qparity */
284 * Wait for readers to observe original parity or be quiescent.
285 * wait_for_readers() can release and grab again rcu_registry_lock
288 wait_for_readers(®istry
, &cur_snap_readers
, &qsreaders
);
291 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
292 * model easier to understand. It does not have a big performance impact
293 * anyway, given this is the write-side.
297 /* Switch parity: 0 -> 1, 1 -> 0 */
298 CMM_STORE_SHARED(rcu_gp
.ctr
, rcu_gp
.ctr
^ RCU_GP_CTR_PHASE
);
301 * Must commit qparity update to memory before waiting for other parity
302 * quiescent state. Failure to do so could result in the writer waiting
303 * forever while new readers are always accessing data (no progress).
304 * Ensured by CMM_STORE_SHARED and CMM_LOAD_SHARED.
308 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
309 * model easier to understand. It does not have a big performance impact
310 * anyway, given this is the write-side.
315 * Wait for readers to observe new parity or be quiescent.
316 * wait_for_readers() can release and grab again rcu_registry_lock
319 wait_for_readers(&cur_snap_readers
, NULL
, &qsreaders
);
322 * Put quiescent reader list back into registry.
324 cds_list_splice(&qsreaders
, ®istry
);
327 * Finish waiting for reader threads before letting the old ptr being
332 mutex_unlock(&rcu_registry_lock
);
333 mutex_unlock(&rcu_gp_lock
);
334 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
339 * library wrappers to be used by non-LGPL compatible source code.
342 void rcu_read_lock(void)
347 void rcu_read_unlock(void)
352 int rcu_read_ongoing(void)
354 return _rcu_read_ongoing();
358 * Only grow for now. If empty, allocate a ARENA_INIT_ALLOC sized chunk.
359 * Else, try expanding the last chunk. If this fails, allocate a new
360 * chunk twice as big as the last chunk.
361 * Memory used by chunks _never_ moves. A chunk could theoretically be
362 * freed when all "used" slots are released, but we don't do it at this
366 void expand_arena(struct registry_arena
*arena
)
368 struct registry_chunk
*new_chunk
, *last_chunk
;
369 size_t old_chunk_len
, new_chunk_len
;
372 if (cds_list_empty(&arena
->chunk_list
)) {
373 assert(ARENA_INIT_ALLOC
>=
374 sizeof(struct registry_chunk
)
375 + sizeof(struct rcu_reader
));
376 new_chunk_len
= ARENA_INIT_ALLOC
;
377 new_chunk
= (struct registry_chunk
*) mmap(NULL
,
379 PROT_READ
| PROT_WRITE
,
380 MAP_ANONYMOUS
| MAP_PRIVATE
,
382 if (new_chunk
== MAP_FAILED
)
384 memset(new_chunk
, 0, new_chunk_len
);
385 new_chunk
->data_len
=
386 new_chunk_len
- sizeof(struct registry_chunk
);
387 cds_list_add_tail(&new_chunk
->node
, &arena
->chunk_list
);
388 return; /* We're done. */
391 /* Try expanding last chunk. */
392 last_chunk
= cds_list_entry(arena
->chunk_list
.prev
,
393 struct registry_chunk
, node
);
395 last_chunk
->data_len
+ sizeof(struct registry_chunk
);
396 new_chunk_len
= old_chunk_len
<< 1;
398 /* Don't allow memory mapping to move, just expand. */
399 new_chunk
= mremap_wrapper(last_chunk
, old_chunk_len
,
401 if (new_chunk
!= MAP_FAILED
) {
402 /* Should not have moved. */
403 assert(new_chunk
== last_chunk
);
404 memset((char *) last_chunk
+ old_chunk_len
, 0,
405 new_chunk_len
- old_chunk_len
);
406 last_chunk
->data_len
=
407 new_chunk_len
- sizeof(struct registry_chunk
);
408 return; /* We're done. */
411 /* Remap did not succeed, we need to add a new chunk. */
412 new_chunk
= (struct registry_chunk
*) mmap(NULL
,
414 PROT_READ
| PROT_WRITE
,
415 MAP_ANONYMOUS
| MAP_PRIVATE
,
417 if (new_chunk
== MAP_FAILED
)
419 memset(new_chunk
, 0, new_chunk_len
);
420 new_chunk
->data_len
=
421 new_chunk_len
- sizeof(struct registry_chunk
);
422 cds_list_add_tail(&new_chunk
->node
, &arena
->chunk_list
);
426 struct rcu_reader
*arena_alloc(struct registry_arena
*arena
)
428 struct registry_chunk
*chunk
;
429 struct rcu_reader
*rcu_reader_reg
;
430 int expand_done
= 0; /* Only allow to expand once per alloc */
431 size_t len
= sizeof(struct rcu_reader
);
434 cds_list_for_each_entry(chunk
, &arena
->chunk_list
, node
) {
435 if (chunk
->data_len
- chunk
->used
< len
)
438 for (rcu_reader_reg
= (struct rcu_reader
*) &chunk
->data
[0];
439 rcu_reader_reg
< (struct rcu_reader
*) &chunk
->data
[chunk
->data_len
];
441 if (!rcu_reader_reg
->alloc
) {
442 rcu_reader_reg
->alloc
= 1;
444 return rcu_reader_reg
;
458 /* Called with signals off and mutex locked */
460 void add_thread(void)
462 struct rcu_reader
*rcu_reader_reg
;
465 rcu_reader_reg
= arena_alloc(®istry_arena
);
468 ret
= pthread_setspecific(urcu_bp_key
, rcu_reader_reg
);
472 /* Add to registry */
473 rcu_reader_reg
->tid
= pthread_self();
474 assert(rcu_reader_reg
->ctr
== 0);
475 cds_list_add(&rcu_reader_reg
->node
, ®istry
);
477 * Reader threads are pointing to the reader registry. This is
478 * why its memory should never be relocated.
480 URCU_TLS(rcu_reader
) = rcu_reader_reg
;
483 /* Called with mutex locked */
485 void cleanup_thread(struct registry_chunk
*chunk
,
486 struct rcu_reader
*rcu_reader_reg
)
488 rcu_reader_reg
->ctr
= 0;
489 cds_list_del(&rcu_reader_reg
->node
);
490 rcu_reader_reg
->tid
= 0;
491 rcu_reader_reg
->alloc
= 0;
492 chunk
->used
-= sizeof(struct rcu_reader
);
496 struct registry_chunk
*find_chunk(struct rcu_reader
*rcu_reader_reg
)
498 struct registry_chunk
*chunk
;
500 cds_list_for_each_entry(chunk
, ®istry_arena
.chunk_list
, node
) {
501 if (rcu_reader_reg
< (struct rcu_reader
*) &chunk
->data
[0])
503 if (rcu_reader_reg
>= (struct rcu_reader
*) &chunk
->data
[chunk
->data_len
])
510 /* Called with signals off and mutex locked */
512 void remove_thread(struct rcu_reader
*rcu_reader_reg
)
514 cleanup_thread(find_chunk(rcu_reader_reg
), rcu_reader_reg
);
515 URCU_TLS(rcu_reader
) = NULL
;
518 /* Disable signals, take mutex, add to registry */
519 void rcu_bp_register(void)
521 sigset_t newmask
, oldmask
;
524 ret
= sigfillset(&newmask
);
527 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
532 * Check if a signal concurrently registered our thread since
533 * the check in rcu_read_lock().
535 if (URCU_TLS(rcu_reader
))
539 * Take care of early registration before urcu_bp constructor.
543 mutex_lock(&rcu_registry_lock
);
545 mutex_unlock(&rcu_registry_lock
);
547 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
552 /* Disable signals, take mutex, remove from registry */
554 void rcu_bp_unregister(struct rcu_reader
*rcu_reader_reg
)
556 sigset_t newmask
, oldmask
;
559 ret
= sigfillset(&newmask
);
562 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
566 mutex_lock(&rcu_registry_lock
);
567 remove_thread(rcu_reader_reg
);
568 mutex_unlock(&rcu_registry_lock
);
569 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
576 * Remove thread from the registry when it exits, and flag it as
577 * destroyed so garbage collection can take care of it.
580 void urcu_bp_thread_exit_notifier(void *rcu_key
)
582 rcu_bp_unregister(rcu_key
);
585 #ifdef CONFIG_RCU_FORCE_SYS_MEMBARRIER
587 void rcu_sys_membarrier_status(int available
)
594 void rcu_sys_membarrier_status(int available
)
597 urcu_bp_has_sys_membarrier
= 1;
602 void rcu_bp_init(void)
604 mutex_lock(&init_lock
);
605 if (!rcu_bp_refcount
++) {
608 ret
= pthread_key_create(&urcu_bp_key
,
609 urcu_bp_thread_exit_notifier
);
612 ret
= membarrier(MEMBARRIER_CMD_QUERY
, 0);
613 rcu_sys_membarrier_status(ret
>= 0
614 && (ret
& MEMBARRIER_CMD_SHARED
));
617 mutex_unlock(&init_lock
);
621 void rcu_bp_exit(void)
623 mutex_lock(&init_lock
);
624 if (!--rcu_bp_refcount
) {
625 struct registry_chunk
*chunk
, *tmp
;
628 cds_list_for_each_entry_safe(chunk
, tmp
,
629 ®istry_arena
.chunk_list
, node
) {
630 munmap((void *) chunk
, chunk
->data_len
631 + sizeof(struct registry_chunk
));
633 CDS_INIT_LIST_HEAD(®istry_arena
.chunk_list
);
634 ret
= pthread_key_delete(urcu_bp_key
);
638 mutex_unlock(&init_lock
);
642 * Holding the rcu_gp_lock and rcu_registry_lock across fork will make
643 * sure we fork() don't race with a concurrent thread executing with
644 * any of those locks held. This ensures that the registry and data
645 * protected by rcu_gp_lock are in a coherent state in the child.
647 void rcu_bp_before_fork(void)
649 sigset_t newmask
, oldmask
;
652 ret
= sigfillset(&newmask
);
654 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
656 mutex_lock(&rcu_gp_lock
);
657 mutex_lock(&rcu_registry_lock
);
658 saved_fork_signal_mask
= oldmask
;
661 void rcu_bp_after_fork_parent(void)
666 oldmask
= saved_fork_signal_mask
;
667 mutex_unlock(&rcu_registry_lock
);
668 mutex_unlock(&rcu_gp_lock
);
669 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
674 * Prune all entries from registry except our own thread. Fits the Linux
675 * fork behavior. Called with rcu_gp_lock and rcu_registry_lock held.
678 void urcu_bp_prune_registry(void)
680 struct registry_chunk
*chunk
;
681 struct rcu_reader
*rcu_reader_reg
;
683 cds_list_for_each_entry(chunk
, ®istry_arena
.chunk_list
, node
) {
684 for (rcu_reader_reg
= (struct rcu_reader
*) &chunk
->data
[0];
685 rcu_reader_reg
< (struct rcu_reader
*) &chunk
->data
[chunk
->data_len
];
687 if (!rcu_reader_reg
->alloc
)
689 if (rcu_reader_reg
->tid
== pthread_self())
691 cleanup_thread(chunk
, rcu_reader_reg
);
696 void rcu_bp_after_fork_child(void)
701 urcu_bp_prune_registry();
702 oldmask
= saved_fork_signal_mask
;
703 mutex_unlock(&rcu_registry_lock
);
704 mutex_unlock(&rcu_gp_lock
);
705 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
709 void *rcu_dereference_sym_bp(void *p
)
711 return _rcu_dereference(p
);
714 void *rcu_set_pointer_sym_bp(void **p
, void *v
)
721 void *rcu_xchg_pointer_sym_bp(void **p
, void *v
)
724 return uatomic_xchg(p
, v
);
727 void *rcu_cmpxchg_pointer_sym_bp(void **p
, void *old
, void *_new
)
730 return uatomic_cmpxchg(p
, old
, _new
);
733 DEFINE_RCU_FLAVOR(rcu_flavor
);
735 #include "urcu-call-rcu-impl.h"
736 #include "urcu-defer-impl.h"