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.
39 #include "urcu/wfqueue.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
98 void __attribute__((constructor
)) rcu_bp_init(void);
100 void __attribute__((destructor
)) _rcu_bp_exit(void);
103 * rcu_gp_lock ensures mutual exclusion between threads calling
106 static pthread_mutex_t rcu_gp_lock
= PTHREAD_MUTEX_INITIALIZER
;
108 * rcu_registry_lock ensures mutual exclusion between threads
109 * registering and unregistering themselves to/from the registry, and
110 * with threads reading that registry from synchronize_rcu(). However,
111 * this lock is not held all the way through the completion of awaiting
112 * for the grace period. It is sporadically released between iterations
114 * rcu_registry_lock may nest inside rcu_gp_lock.
116 static pthread_mutex_t rcu_registry_lock
= PTHREAD_MUTEX_INITIALIZER
;
118 static pthread_mutex_t init_lock
= PTHREAD_MUTEX_INITIALIZER
;
119 static int initialized
;
121 static pthread_key_t urcu_bp_key
;
124 unsigned int yield_active
;
125 __DEFINE_URCU_TLS_GLOBAL(unsigned int, rand_yield
);
129 * Global grace period counter.
130 * Contains the current RCU_GP_CTR_PHASE.
131 * Also has a RCU_GP_COUNT of 1, to accelerate the reader fast path.
132 * Written to only by writer with mutex taken. Read by both writer and readers.
134 long rcu_gp_ctr
= RCU_GP_COUNT
;
137 * Pointer to registry elements. Written to only by each individual reader. Read
138 * by both the reader and the writers.
140 __DEFINE_URCU_TLS_GLOBAL(struct rcu_reader
*, rcu_reader
);
142 static CDS_LIST_HEAD(registry
);
144 struct registry_chunk
{
145 size_t data_len
; /* data length */
146 size_t used
; /* amount of data used */
147 struct cds_list_head node
; /* chunk_list node */
151 struct registry_arena
{
152 struct cds_list_head chunk_list
;
155 static struct registry_arena registry_arena
= {
156 .chunk_list
= CDS_LIST_HEAD_INIT(registry_arena
.chunk_list
),
159 /* Saved fork signal mask, protected by rcu_gp_lock */
160 static sigset_t saved_fork_signal_mask
;
162 static void mutex_lock(pthread_mutex_t
*mutex
)
166 #ifndef DISTRUST_SIGNALS_EXTREME
167 ret
= pthread_mutex_lock(mutex
);
170 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
171 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
172 if (ret
!= EBUSY
&& ret
!= EINTR
)
176 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
179 static void mutex_unlock(pthread_mutex_t
*mutex
)
183 ret
= pthread_mutex_unlock(mutex
);
189 * Always called with rcu_registry lock held. Releases this lock between
190 * iterations and grabs it again. Holds the lock when it returns.
192 void update_counter_and_wait(void)
194 CDS_LIST_HEAD(qsreaders
);
195 unsigned int wait_loops
= 0;
196 struct rcu_reader
*index
, *tmp
;
198 /* Switch parity: 0 -> 1, 1 -> 0 */
199 CMM_STORE_SHARED(rcu_gp_ctr
, rcu_gp_ctr
^ RCU_GP_CTR_PHASE
);
202 * Must commit qparity update to memory before waiting for other parity
203 * quiescent state. Failure to do so could result in the writer waiting
204 * forever while new readers are always accessing data (no progress).
205 * Ensured by CMM_STORE_SHARED and CMM_LOAD_SHARED.
209 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
210 * model easier to understand. It does not have a big performance impact
211 * anyway, given this is the write-side.
216 * Wait for each thread rcu_reader.ctr count to become 0.
219 if (wait_loops
< RCU_QS_ACTIVE_ATTEMPTS
)
222 cds_list_for_each_entry_safe(index
, tmp
, ®istry
, node
) {
223 if (!rcu_old_gp_ongoing(&index
->ctr
))
224 cds_list_move(&index
->node
, &qsreaders
);
227 if (cds_list_empty(®istry
)) {
230 /* Temporarily unlock the registry lock. */
231 mutex_unlock(&rcu_registry_lock
);
232 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
)
233 (void) poll(NULL
, 0, RCU_SLEEP_DELAY_MS
);
236 /* Re-lock the registry lock before the next loop. */
237 mutex_lock(&rcu_registry_lock
);
240 /* put back the reader list in the registry */
241 cds_list_splice(&qsreaders
, ®istry
);
244 void synchronize_rcu(void)
246 sigset_t newmask
, oldmask
;
249 ret
= sigfillset(&newmask
);
251 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
254 mutex_lock(&rcu_gp_lock
);
255 mutex_lock(&rcu_registry_lock
);
257 if (cds_list_empty(®istry
))
260 /* All threads should read qparity before accessing data structure
261 * where new ptr points to. */
262 /* Write new ptr before changing the qparity */
266 * Wait for previous parity to be empty of readers.
267 * update_counter_and_wait() can release and grab again
268 * rcu_registry_lock interally.
270 update_counter_and_wait(); /* 0 -> 1, wait readers in parity 0 */
273 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
274 * model easier to understand. It does not have a big performance impact
275 * anyway, given this is the write-side.
280 * Wait for previous parity to be empty of readers.
281 * update_counter_and_wait() can release and grab again
282 * rcu_registry_lock interally.
284 update_counter_and_wait(); /* 1 -> 0, wait readers in parity 1 */
287 * Finish waiting for reader threads before letting the old ptr being
292 mutex_unlock(&rcu_registry_lock
);
293 mutex_unlock(&rcu_gp_lock
);
294 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
299 * library wrappers to be used by non-LGPL compatible source code.
302 void rcu_read_lock(void)
307 void rcu_read_unlock(void)
313 * Only grow for now. If empty, allocate a ARENA_INIT_ALLOC sized chunk.
314 * Else, try expanding the last chunk. If this fails, allocate a new
315 * chunk twice as big as the last chunk.
316 * Memory used by chunks _never_ moves. A chunk could theoretically be
317 * freed when all "used" slots are released, but we don't do it at this
321 void expand_arena(struct registry_arena
*arena
)
323 struct registry_chunk
*new_chunk
, *last_chunk
;
324 size_t old_chunk_len
, new_chunk_len
;
327 if (cds_list_empty(&arena
->chunk_list
)) {
328 assert(ARENA_INIT_ALLOC
>=
329 sizeof(struct registry_chunk
)
330 + sizeof(struct rcu_reader
));
331 new_chunk_len
= ARENA_INIT_ALLOC
;
332 new_chunk
= mmap(NULL
, new_chunk_len
,
333 PROT_READ
| PROT_WRITE
,
334 MAP_ANONYMOUS
| MAP_PRIVATE
,
336 if (new_chunk
== MAP_FAILED
)
338 bzero(new_chunk
, new_chunk_len
);
339 new_chunk
->data_len
=
340 new_chunk_len
- sizeof(struct registry_chunk
);
341 cds_list_add_tail(&new_chunk
->node
, &arena
->chunk_list
);
342 return; /* We're done. */
345 /* Try expanding last chunk. */
346 last_chunk
= cds_list_entry(arena
->chunk_list
.prev
,
347 struct registry_chunk
, node
);
349 last_chunk
->data_len
+ sizeof(struct registry_chunk
);
350 new_chunk_len
= old_chunk_len
<< 1;
352 /* Don't allow memory mapping to move, just expand. */
353 new_chunk
= mremap_wrapper(last_chunk
, old_chunk_len
,
355 if (new_chunk
!= MAP_FAILED
) {
356 /* Should not have moved. */
357 assert(new_chunk
== last_chunk
);
358 bzero((char *) last_chunk
+ old_chunk_len
,
359 new_chunk_len
- old_chunk_len
);
360 last_chunk
->data_len
=
361 new_chunk_len
- sizeof(struct registry_chunk
);
362 return; /* We're done. */
365 /* Remap did not succeed, we need to add a new chunk. */
366 new_chunk
= mmap(NULL
, new_chunk_len
,
367 PROT_READ
| PROT_WRITE
,
368 MAP_ANONYMOUS
| MAP_PRIVATE
,
370 if (new_chunk
== MAP_FAILED
)
372 bzero(new_chunk
, new_chunk_len
);
373 new_chunk
->data_len
=
374 new_chunk_len
- sizeof(struct registry_chunk
);
375 cds_list_add_tail(&new_chunk
->node
, &arena
->chunk_list
);
379 struct rcu_reader
*arena_alloc(struct registry_arena
*arena
)
381 struct registry_chunk
*chunk
;
382 struct rcu_reader
*rcu_reader_reg
;
383 int expand_done
= 0; /* Only allow to expand once per alloc */
384 size_t len
= sizeof(struct rcu_reader
);
387 cds_list_for_each_entry(chunk
, &arena
->chunk_list
, node
) {
388 if (chunk
->data_len
- chunk
->used
< len
)
391 for (rcu_reader_reg
= (struct rcu_reader
*) &chunk
->data
[0];
392 rcu_reader_reg
< (struct rcu_reader
*) &chunk
->data
[chunk
->data_len
];
394 if (!rcu_reader_reg
->alloc
) {
395 rcu_reader_reg
->alloc
= 1;
397 return rcu_reader_reg
;
411 /* Called with signals off and mutex locked */
413 void add_thread(void)
415 struct rcu_reader
*rcu_reader_reg
;
418 rcu_reader_reg
= arena_alloc(®istry_arena
);
421 ret
= pthread_setspecific(urcu_bp_key
, rcu_reader_reg
);
425 /* Add to registry */
426 rcu_reader_reg
->tid
= pthread_self();
427 assert(rcu_reader_reg
->ctr
== 0);
428 cds_list_add(&rcu_reader_reg
->node
, ®istry
);
430 * Reader threads are pointing to the reader registry. This is
431 * why its memory should never be relocated.
433 URCU_TLS(rcu_reader
) = rcu_reader_reg
;
436 /* Called with mutex locked */
438 void cleanup_thread(struct registry_chunk
*chunk
,
439 struct rcu_reader
*rcu_reader_reg
)
441 rcu_reader_reg
->ctr
= 0;
442 cds_list_del(&rcu_reader_reg
->node
);
443 rcu_reader_reg
->tid
= 0;
444 rcu_reader_reg
->alloc
= 0;
445 chunk
->used
-= sizeof(struct rcu_reader
);
449 struct registry_chunk
*find_chunk(struct rcu_reader
*rcu_reader_reg
)
451 struct registry_chunk
*chunk
;
453 cds_list_for_each_entry(chunk
, ®istry_arena
.chunk_list
, node
) {
454 if (rcu_reader_reg
< (struct rcu_reader
*) &chunk
->data
[0])
456 if (rcu_reader_reg
>= (struct rcu_reader
*) &chunk
->data
[chunk
->data_len
])
463 /* Called with signals off and mutex locked */
465 void remove_thread(struct rcu_reader
*rcu_reader_reg
)
467 cleanup_thread(find_chunk(rcu_reader_reg
), rcu_reader_reg
);
468 URCU_TLS(rcu_reader
) = NULL
;
471 /* Disable signals, take mutex, add to registry */
472 void rcu_bp_register(void)
474 sigset_t newmask
, oldmask
;
477 ret
= sigfillset(&newmask
);
480 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
485 * Check if a signal concurrently registered our thread since
486 * the check in rcu_read_lock().
488 if (URCU_TLS(rcu_reader
))
492 * Take care of early registration before urcu_bp constructor.
496 mutex_lock(&rcu_registry_lock
);
498 mutex_unlock(&rcu_registry_lock
);
500 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
505 /* Disable signals, take mutex, remove from registry */
507 void rcu_bp_unregister(struct rcu_reader
*rcu_reader_reg
)
509 sigset_t newmask
, oldmask
;
512 ret
= sigfillset(&newmask
);
515 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
519 mutex_lock(&rcu_registry_lock
);
520 remove_thread(rcu_reader_reg
);
521 mutex_unlock(&rcu_registry_lock
);
522 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
529 * Remove thread from the registry when it exits, and flag it as
530 * destroyed so garbage collection can take care of it.
533 void urcu_bp_thread_exit_notifier(void *rcu_key
)
535 rcu_bp_unregister(rcu_key
);
539 void rcu_bp_init(void)
541 mutex_lock(&init_lock
);
542 if (!rcu_bp_refcount
++) {
545 ret
= pthread_key_create(&urcu_bp_key
,
546 urcu_bp_thread_exit_notifier
);
551 mutex_unlock(&init_lock
);
555 void _rcu_bp_exit(void)
557 mutex_lock(&init_lock
);
558 if (!--rcu_bp_refcount
) {
559 struct registry_chunk
*chunk
, *tmp
;
562 cds_list_for_each_entry_safe(chunk
, tmp
,
563 ®istry_arena
.chunk_list
, node
) {
564 munmap(chunk
, chunk
->data_len
565 + sizeof(struct registry_chunk
));
567 ret
= pthread_key_delete(urcu_bp_key
);
571 mutex_unlock(&init_lock
);
575 * Keep ABI compability within stable versions. This has never been
576 * exposed through a header, but needs to stay in the .so until the
579 void rcu_bp_exit(void)
584 * Holding the rcu_gp_lock and rcu_registry_lock across fork will make
585 * sure we fork() don't race with a concurrent thread executing with
586 * any of those locks held. This ensures that the registry and data
587 * protected by rcu_gp_lock are in a coherent state in the child.
589 void rcu_bp_before_fork(void)
591 sigset_t newmask
, oldmask
;
594 ret
= sigfillset(&newmask
);
596 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
598 mutex_lock(&rcu_gp_lock
);
599 mutex_lock(&rcu_registry_lock
);
600 saved_fork_signal_mask
= oldmask
;
603 void rcu_bp_after_fork_parent(void)
608 oldmask
= saved_fork_signal_mask
;
609 mutex_unlock(&rcu_registry_lock
);
610 mutex_unlock(&rcu_gp_lock
);
611 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
616 * Prune all entries from registry except our own thread. Fits the Linux
617 * fork behavior. Called with rcu_gp_lock and rcu_registry_lock held.
620 void urcu_bp_prune_registry(void)
622 struct registry_chunk
*chunk
;
623 struct rcu_reader
*rcu_reader_reg
;
625 cds_list_for_each_entry(chunk
, ®istry_arena
.chunk_list
, node
) {
626 for (rcu_reader_reg
= (struct rcu_reader
*) &chunk
->data
[0];
627 rcu_reader_reg
< (struct rcu_reader
*) &chunk
->data
[chunk
->data_len
];
629 if (!rcu_reader_reg
->alloc
)
631 if (rcu_reader_reg
->tid
== pthread_self())
633 cleanup_thread(chunk
, rcu_reader_reg
);
638 void rcu_bp_after_fork_child(void)
643 urcu_bp_prune_registry();
644 oldmask
= saved_fork_signal_mask
;
645 mutex_unlock(&rcu_registry_lock
);
646 mutex_unlock(&rcu_gp_lock
);
647 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
651 void *rcu_dereference_sym_bp(void *p
)
653 return _rcu_dereference(p
);
656 void *rcu_set_pointer_sym_bp(void **p
, void *v
)
663 void *rcu_xchg_pointer_sym_bp(void **p
, void *v
)
666 return uatomic_xchg(p
, v
);
669 void *rcu_cmpxchg_pointer_sym_bp(void **p
, void *old
, void *_new
)
672 return uatomic_cmpxchg(p
, old
, _new
);
675 DEFINE_RCU_FLAVOR(rcu_flavor
);
677 #include "urcu-call-rcu-impl.h"
678 #include "urcu-defer-impl.h"