1 // SPDX-FileCopyrightText: 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
2 // SPDX-FileCopyrightText: 2009 Paul E. McKenney, IBM Corporation.
4 // SPDX-License-Identifier: LGPL-2.1-or-later
7 * Userspace RCU library
9 * IBM's contributions to this file may be relicensed under LGPLv2 or later.
12 #define URCU_NO_COMPAT_IDENTIFIERS
15 #define _DEFAULT_SOURCE
26 #include <urcu/config.h>
27 #include <urcu/assert.h>
28 #include <urcu/arch.h>
29 #include <urcu/wfcqueue.h>
30 #include <urcu/map/urcu.h>
31 #include <urcu/static/urcu.h>
32 #include <urcu/pointer.h>
33 #include <urcu/tls-compat.h>
36 #include "urcu-wait.h"
37 #include "urcu-utils.h"
40 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
42 #include <urcu/urcu.h>
46 * If a reader is really non-cooperative and refuses to commit its
47 * rcu_active_readers count to memory (there is no barrier in the reader
48 * per-se), kick it after 10 loops waiting for it.
50 #define KICK_READER_LOOPS 10
53 * Active attempts to check for reader Q.S. before calling futex().
55 #define RCU_QS_ACTIVE_ATTEMPTS 100
57 /* If the headers do not support membarrier system call, fall back on RCU_MB */
58 #ifdef __NR_membarrier
59 # define membarrier(...) syscall(__NR_membarrier, __VA_ARGS__)
61 # define membarrier(...) -ENOSYS
65 MEMBARRIER_CMD_QUERY
= 0,
66 MEMBARRIER_CMD_SHARED
= (1 << 0),
67 /* reserved for MEMBARRIER_CMD_SHARED_EXPEDITED (1 << 1) */
68 /* reserved for MEMBARRIER_CMD_PRIVATE (1 << 2) */
69 MEMBARRIER_CMD_PRIVATE_EXPEDITED
= (1 << 3),
70 MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED
= (1 << 4),
75 static int urcu_memb_has_sys_membarrier_private_expedited
;
77 #ifndef CONFIG_RCU_FORCE_SYS_MEMBARRIER
79 * Explicitly initialize to zero because we can't alias a non-static
80 * uninitialized variable.
82 int urcu_memb_has_sys_membarrier
= 0;
85 void __attribute__((constructor
)) rcu_init(void);
97 void __attribute__((constructor
)) rcu_init(void);
99 static DEFINE_URCU_TLS(int, rcu_signal_was_blocked
);
102 void __attribute__((destructor
)) rcu_exit(void);
103 static void urcu_call_rcu_exit(void);
106 * rcu_gp_lock ensures mutual exclusion between threads calling
109 static pthread_mutex_t rcu_gp_lock
= PTHREAD_MUTEX_INITIALIZER
;
111 * rcu_registry_lock ensures mutual exclusion between threads
112 * registering and unregistering themselves to/from the registry, and
113 * with threads reading that registry from synchronize_rcu(). However,
114 * this lock is not held all the way through the completion of awaiting
115 * for the grace period. It is sporadically released between iterations
117 * rcu_registry_lock may nest inside rcu_gp_lock.
119 static pthread_mutex_t rcu_registry_lock
= PTHREAD_MUTEX_INITIALIZER
;
120 struct urcu_gp rcu_gp
= { .ctr
= URCU_GP_COUNT
};
123 * Written to only by each individual reader. Read by both the reader and the
126 DEFINE_URCU_TLS(struct urcu_reader
, rcu_reader
);
128 static CDS_LIST_HEAD(registry
);
131 * Queue keeping threads awaiting to wait for a grace period. Contains
132 * struct gp_waiters_thread objects.
134 static DEFINE_URCU_WAIT_QUEUE(gp_waiters
);
136 static void mutex_lock(pthread_mutex_t
*mutex
)
140 #ifndef DISTRUST_SIGNALS_EXTREME
141 ret
= pthread_mutex_lock(mutex
);
144 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
145 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
146 if (ret
!= EBUSY
&& ret
!= EINTR
)
148 if (CMM_LOAD_SHARED(URCU_TLS(rcu_reader
).need_mb
)) {
150 _CMM_STORE_SHARED(URCU_TLS(rcu_reader
).need_mb
, 0);
153 (void) poll(NULL
, 0, 10);
155 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
158 static void mutex_unlock(pthread_mutex_t
*mutex
)
162 ret
= pthread_mutex_unlock(mutex
);
167 #ifdef RCU_MEMBARRIER
168 static void smp_mb_master(void)
170 if (caa_likely(urcu_memb_has_sys_membarrier
)) {
171 if (membarrier(urcu_memb_has_sys_membarrier_private_expedited
?
172 MEMBARRIER_CMD_PRIVATE_EXPEDITED
:
173 MEMBARRIER_CMD_SHARED
, 0))
182 static void smp_mb_master(void)
189 static void force_mb_all_readers(void)
191 struct urcu_reader
*index
;
194 * Ask for each threads to execute a cmm_smp_mb() so we can consider the
195 * compiler barriers around rcu read lock as real memory barriers.
197 if (cds_list_empty(®istry
))
200 * pthread_kill has a cmm_smp_mb(). But beware, we assume it performs
201 * a cache flush on architectures with non-coherent cache. Let's play
202 * safe and don't assume anything : we use cmm_smp_mc() to make sure the
203 * cache flush is enforced.
205 cds_list_for_each_entry(index
, ®istry
, node
) {
206 CMM_STORE_SHARED(index
->need_mb
, 1);
207 pthread_kill(index
->tid
, SIGRCU
);
210 * Wait for sighandler (and thus mb()) to execute on every thread.
212 * Note that the pthread_kill() will never be executed on systems
213 * that correctly deliver signals in a timely manner. However, it
214 * is not uncommon for kernels to have bugs that can result in
215 * lost or unduly delayed signals.
217 * If you are seeing the below pthread_kill() executing much at
218 * all, we suggest testing the underlying kernel and filing the
219 * relevant bug report. For Linux kernels, we recommend getting
220 * the Linux Test Project (LTP).
222 cds_list_for_each_entry(index
, ®istry
, node
) {
223 while (CMM_LOAD_SHARED(index
->need_mb
)) {
224 pthread_kill(index
->tid
, SIGRCU
);
225 (void) poll(NULL
, 0, 1);
228 cmm_smp_mb(); /* read ->need_mb before ending the barrier */
231 static void smp_mb_master(void)
233 force_mb_all_readers();
235 #endif /* #ifdef RCU_SIGNAL */
238 * synchronize_rcu() waiting. Single thread.
239 * Always called with rcu_registry lock held. Releases this lock and
240 * grabs it again. Holds the lock when it returns.
242 static void wait_gp(void)
245 * Read reader_gp before read futex. smp_mb_master() needs to
246 * be called with the rcu registry lock held in RCU_SIGNAL
250 /* Temporarily unlock the registry lock. */
251 mutex_unlock(&rcu_registry_lock
);
252 while (uatomic_read(&rcu_gp
.futex
) == -1) {
253 if (!futex_async(&rcu_gp
.futex
, FUTEX_WAIT
, -1, NULL
, NULL
, 0)) {
255 * Prior queued wakeups queued by unrelated code
256 * using the same address can cause futex wait to
257 * return 0 even through the futex value is still
258 * -1 (spurious wakeups). Check the value again
259 * in user-space to validate whether it really
266 /* Value already changed. */
269 /* Retry if interrupted by signal. */
270 break; /* Get out of switch. Check again. */
272 /* Unexpected error. */
278 * Re-lock the registry lock before the next loop.
280 mutex_lock(&rcu_registry_lock
);
284 * Always called with rcu_registry lock held. Releases this lock between
285 * iterations and grabs it again. Holds the lock when it returns.
287 static void wait_for_readers(struct cds_list_head
*input_readers
,
288 struct cds_list_head
*cur_snap_readers
,
289 struct cds_list_head
*qsreaders
)
291 unsigned int wait_loops
= 0;
292 struct urcu_reader
*index
, *tmp
;
293 #ifdef HAS_INCOHERENT_CACHES
294 unsigned int wait_gp_loops
= 0;
295 #endif /* HAS_INCOHERENT_CACHES */
298 * Wait for each thread URCU_TLS(rcu_reader).ctr to either
299 * indicate quiescence (not nested), or observe the current
303 if (wait_loops
< RCU_QS_ACTIVE_ATTEMPTS
)
305 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
306 uatomic_dec(&rcu_gp
.futex
);
307 /* Write futex before read reader_gp */
311 cds_list_for_each_entry_safe(index
, tmp
, input_readers
, node
) {
312 switch (urcu_common_reader_state(&rcu_gp
, &index
->ctr
)) {
313 case URCU_READER_ACTIVE_CURRENT
:
314 if (cur_snap_readers
) {
315 cds_list_move(&index
->node
,
320 case URCU_READER_INACTIVE
:
321 cds_list_move(&index
->node
, qsreaders
);
323 case URCU_READER_ACTIVE_OLD
:
325 * Old snapshot. Leaving node in
326 * input_readers will make us busy-loop
327 * until the snapshot becomes current or
328 * the reader becomes inactive.
334 #ifndef HAS_INCOHERENT_CACHES
335 if (cds_list_empty(input_readers
)) {
336 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
337 /* Read reader_gp before write futex */
339 uatomic_set(&rcu_gp
.futex
, 0);
343 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
344 /* wait_gp unlocks/locks registry lock. */
347 /* Temporarily unlock the registry lock. */
348 mutex_unlock(&rcu_registry_lock
);
351 * Re-lock the registry lock before the
354 mutex_lock(&rcu_registry_lock
);
357 #else /* #ifndef HAS_INCOHERENT_CACHES */
359 * BUSY-LOOP. Force the reader thread to commit its
360 * URCU_TLS(rcu_reader).ctr update to memory if we wait
363 if (cds_list_empty(input_readers
)) {
364 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
365 /* Read reader_gp before write futex */
367 uatomic_set(&rcu_gp
.futex
, 0);
371 if (wait_gp_loops
== KICK_READER_LOOPS
) {
375 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
376 /* wait_gp unlocks/locks registry lock. */
380 /* Temporarily unlock the registry lock. */
381 mutex_unlock(&rcu_registry_lock
);
384 * Re-lock the registry lock before the
387 mutex_lock(&rcu_registry_lock
);
390 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
394 void synchronize_rcu(void)
396 CDS_LIST_HEAD(cur_snap_readers
);
397 CDS_LIST_HEAD(qsreaders
);
398 DEFINE_URCU_WAIT_NODE(wait
, URCU_WAIT_WAITING
);
399 struct urcu_waiters waiters
;
402 * Add ourself to gp_waiters queue of threads awaiting to wait
403 * for a grace period. Proceed to perform the grace period only
404 * if we are the first thread added into the queue.
405 * The implicit memory barrier before urcu_wait_add()
406 * orders prior memory accesses of threads put into the wait
407 * queue before their insertion into the wait queue.
409 if (urcu_wait_add(&gp_waiters
, &wait
) != 0) {
410 /* Not first in queue: will be awakened by another thread. */
411 urcu_adaptative_busy_wait(&wait
);
412 /* Order following memory accesses after grace period. */
416 /* We won't need to wake ourself up */
417 urcu_wait_set_state(&wait
, URCU_WAIT_RUNNING
);
419 mutex_lock(&rcu_gp_lock
);
422 * Move all waiters into our local queue.
424 urcu_move_waiters(&waiters
, &gp_waiters
);
426 mutex_lock(&rcu_registry_lock
);
428 if (cds_list_empty(®istry
))
432 * All threads should read qparity before accessing data structure
433 * where new ptr points to. Must be done within rcu_registry_lock
434 * because it iterates on reader threads.
436 /* Write new ptr before changing the qparity */
440 * Wait for readers to observe original parity or be quiescent.
441 * wait_for_readers() can release and grab again rcu_registry_lock
444 wait_for_readers(®istry
, &cur_snap_readers
, &qsreaders
);
447 * Must finish waiting for quiescent state for original parity before
448 * committing next rcu_gp.ctr update to memory. Failure to do so could
449 * result in the writer waiting forever while new readers are always
450 * accessing data (no progress). Enforce compiler-order of load
451 * URCU_TLS(rcu_reader).ctr before store to rcu_gp.ctr.
456 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
457 * model easier to understand. It does not have a big performance impact
458 * anyway, given this is the write-side.
462 /* Switch parity: 0 -> 1, 1 -> 0 */
463 CMM_STORE_SHARED(rcu_gp
.ctr
, rcu_gp
.ctr
^ URCU_GP_CTR_PHASE
);
466 * Must commit rcu_gp.ctr update to memory before waiting for quiescent
467 * state. Failure to do so could result in the writer waiting forever
468 * while new readers are always accessing data (no progress). Enforce
469 * compiler-order of store to rcu_gp.ctr before load rcu_reader ctr.
475 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
476 * model easier to understand. It does not have a big performance impact
477 * anyway, given this is the write-side.
482 * Wait for readers to observe new parity or be quiescent.
483 * wait_for_readers() can release and grab again rcu_registry_lock
486 wait_for_readers(&cur_snap_readers
, NULL
, &qsreaders
);
489 * Put quiescent reader list back into registry.
491 cds_list_splice(&qsreaders
, ®istry
);
494 * Finish waiting for reader threads before letting the old ptr
495 * being freed. Must be done within rcu_registry_lock because it
496 * iterates on reader threads.
500 mutex_unlock(&rcu_registry_lock
);
501 mutex_unlock(&rcu_gp_lock
);
504 * Wakeup waiters only after we have completed the grace period
505 * and have ensured the memory barriers at the end of the grace
506 * period have been issued.
508 urcu_wake_all_waiters(&waiters
);
512 * library wrappers to be used by non-LGPL compatible source code.
515 void rcu_read_lock(void)
520 void rcu_read_unlock(void)
525 int rcu_read_ongoing(void)
527 return _rcu_read_ongoing();
532 * Make sure the signal used by the urcu-signal flavor is unblocked
533 * while the thread is registered.
536 void urcu_signal_unblock(void)
538 sigset_t mask
, oldmask
;
541 ret
= sigemptyset(&mask
);
542 urcu_posix_assert(!ret
);
543 ret
= sigaddset(&mask
, SIGRCU
);
544 urcu_posix_assert(!ret
);
545 ret
= pthread_sigmask(SIG_UNBLOCK
, &mask
, &oldmask
);
546 urcu_posix_assert(!ret
);
547 URCU_TLS(rcu_signal_was_blocked
) = sigismember(&oldmask
, SIGRCU
);
551 void urcu_signal_restore(void)
556 if (!URCU_TLS(rcu_signal_was_blocked
))
558 ret
= sigemptyset(&mask
);
559 urcu_posix_assert(!ret
);
560 ret
= sigaddset(&mask
, SIGRCU
);
561 urcu_posix_assert(!ret
);
562 ret
= pthread_sigmask(SIG_BLOCK
, &mask
, NULL
);
563 urcu_posix_assert(!ret
);
567 void urcu_signal_unblock(void) { }
569 void urcu_signal_restore(void) { }
572 void rcu_register_thread(void)
574 urcu_signal_unblock();
576 URCU_TLS(rcu_reader
).tid
= pthread_self();
577 urcu_posix_assert(URCU_TLS(rcu_reader
).need_mb
== 0);
578 urcu_posix_assert(!(URCU_TLS(rcu_reader
).ctr
& URCU_GP_CTR_NEST_MASK
));
580 mutex_lock(&rcu_registry_lock
);
581 urcu_posix_assert(!URCU_TLS(rcu_reader
).registered
);
582 URCU_TLS(rcu_reader
).registered
= 1;
583 rcu_init(); /* In case gcc does not support constructor attribute */
584 cds_list_add(&URCU_TLS(rcu_reader
).node
, ®istry
);
585 mutex_unlock(&rcu_registry_lock
);
588 void rcu_unregister_thread(void)
590 mutex_lock(&rcu_registry_lock
);
591 urcu_posix_assert(URCU_TLS(rcu_reader
).registered
);
592 URCU_TLS(rcu_reader
).registered
= 0;
593 cds_list_del(&URCU_TLS(rcu_reader
).node
);
594 mutex_unlock(&rcu_registry_lock
);
596 urcu_signal_restore();
599 #ifdef RCU_MEMBARRIER
601 #ifdef CONFIG_RCU_FORCE_SYS_MEMBARRIER
603 void rcu_sys_membarrier_status(bool available
)
610 void rcu_sys_membarrier_status(bool available
)
614 urcu_memb_has_sys_membarrier
= 1;
619 void rcu_sys_membarrier_init(void)
621 bool available
= false;
624 mask
= membarrier(MEMBARRIER_CMD_QUERY
, 0);
626 if (mask
& MEMBARRIER_CMD_PRIVATE_EXPEDITED
) {
627 if (membarrier(MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED
, 0))
629 urcu_memb_has_sys_membarrier_private_expedited
= 1;
631 } else if (mask
& MEMBARRIER_CMD_SHARED
) {
635 rcu_sys_membarrier_status(available
);
643 rcu_sys_membarrier_init();
648 static void sigrcu_handler(int signo
__attribute__((unused
)),
649 siginfo_t
*siginfo
__attribute__((unused
)),
650 void *context
__attribute__((unused
)))
653 * Executing this cmm_smp_mb() is the only purpose of this signal handler.
654 * It punctually promotes cmm_barrier() into cmm_smp_mb() on every thread it is
658 _CMM_STORE_SHARED(URCU_TLS(rcu_reader
).need_mb
, 0);
663 * rcu_init constructor. Called when the library is linked, but also when
664 * reader threads are calling rcu_register_thread().
665 * Should only be called by a single thread at a given time. This is ensured by
666 * holing the rcu_registry_lock from rcu_register_thread() or by running
667 * at library load time, which should not be executed by multiple
668 * threads nor concurrently with rcu_register_thread() anyway.
672 struct sigaction act
;
679 act
.sa_sigaction
= sigrcu_handler
;
680 act
.sa_flags
= SA_SIGINFO
| SA_RESTART
;
681 sigemptyset(&act
.sa_mask
);
682 ret
= sigaction(SIGRCU
, &act
, NULL
);
688 * Don't unregister the SIGRCU signal handler anymore, because
689 * call_rcu threads could still be using it shortly before the
691 * Assertion disabled because call_rcu threads are now rcu
692 * readers, and left running at exit.
693 * urcu_posix_assert(cds_list_empty(®istry));
696 #endif /* #ifdef RCU_SIGNAL */
700 urcu_call_rcu_exit();
703 DEFINE_RCU_FLAVOR(rcu_flavor
);
705 #include "urcu-call-rcu-impl.h"
706 #include "urcu-defer-impl.h"
707 #include "urcu-poll-impl.h"