2 * SPDX-License-Identifier: LGPL-2.1-or-later
4 * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * Copyright (c) 2009 Paul E. McKenney, IBM Corporation.
7 * Userspace RCU library for LTTng-UST, derived from liburcu "bulletproof" version.
23 #include <urcu/arch.h>
24 #include <urcu/wfcqueue.h>
25 #include <lttng/urcu/static/urcu-ust.h>
26 #include <lttng/urcu/pointer.h>
27 #include <urcu/tls-compat.h>
29 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
31 #include <lttng/urcu/urcu-ust.h>
35 #define MAP_ANONYMOUS MAP_ANON
40 void *mremap_wrapper(void *old_address
, size_t old_size
,
41 size_t new_size
, int flags
)
43 return mremap(old_address
, old_size
, new_size
, flags
);
47 #define MREMAP_MAYMOVE 1
48 #define MREMAP_FIXED 2
51 * mremap wrapper for non-Linux systems not allowing MAYMOVE.
52 * This is not generic.
55 void *mremap_wrapper(void *old_address
, size_t old_size
,
56 size_t new_size
, int flags
)
58 assert(!(flags
& MREMAP_MAYMOVE
));
64 /* Sleep delay in ms */
65 #define RCU_SLEEP_DELAY_MS 10
66 #define INIT_NR_THREADS 8
67 #define ARENA_INIT_ALLOC \
68 sizeof(struct registry_chunk) \
69 + INIT_NR_THREADS * sizeof(struct lttng_ust_urcu_reader)
72 * Active attempts to check for reader Q.S. before calling sleep().
74 #define RCU_QS_ACTIVE_ATTEMPTS 100
77 int lttng_ust_urcu_refcount
;
79 /* If the headers do not support membarrier system call, fall back smp_mb. */
80 #ifdef __NR_membarrier
81 # define membarrier(...) syscall(__NR_membarrier, __VA_ARGS__)
83 # define membarrier(...) -ENOSYS
87 MEMBARRIER_CMD_QUERY
= 0,
88 MEMBARRIER_CMD_SHARED
= (1 << 0),
89 /* reserved for MEMBARRIER_CMD_SHARED_EXPEDITED (1 << 1) */
90 /* reserved for MEMBARRIER_CMD_PRIVATE (1 << 2) */
91 MEMBARRIER_CMD_PRIVATE_EXPEDITED
= (1 << 3),
92 MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED
= (1 << 4),
96 void __attribute__((constructor
)) _lttng_ust_urcu_init(void);
98 void __attribute__((destructor
)) lttng_ust_urcu_exit(void);
100 #ifndef CONFIG_RCU_FORCE_SYS_MEMBARRIER
101 int lttng_ust_urcu_has_sys_membarrier
;
105 * rcu_gp_lock ensures mutual exclusion between threads calling
108 static pthread_mutex_t rcu_gp_lock
= PTHREAD_MUTEX_INITIALIZER
;
110 * rcu_registry_lock ensures mutual exclusion between threads
111 * registering and unregistering themselves to/from the registry, and
112 * with threads reading that registry from synchronize_rcu(). However,
113 * this lock is not held all the way through the completion of awaiting
114 * for the grace period. It is sporadically released between iterations
116 * rcu_registry_lock may nest inside rcu_gp_lock.
118 static pthread_mutex_t rcu_registry_lock
= PTHREAD_MUTEX_INITIALIZER
;
120 static pthread_mutex_t init_lock
= PTHREAD_MUTEX_INITIALIZER
;
121 static int initialized
;
123 static pthread_key_t lttng_ust_urcu_key
;
125 struct lttng_ust_urcu_gp lttng_ust_urcu_gp
= { .ctr
= LTTNG_UST_URCU_GP_COUNT
};
128 * Pointer to registry elements. Written to only by each individual reader. Read
129 * by both the reader and the writers.
131 DEFINE_URCU_TLS(struct lttng_ust_urcu_reader
*, lttng_ust_urcu_reader
);
133 static CDS_LIST_HEAD(registry
);
135 struct registry_chunk
{
136 size_t data_len
; /* data length */
137 size_t used
; /* amount of data used */
138 struct cds_list_head node
; /* chunk_list node */
142 struct registry_arena
{
143 struct cds_list_head chunk_list
;
146 static struct registry_arena registry_arena
= {
147 .chunk_list
= CDS_LIST_HEAD_INIT(registry_arena
.chunk_list
),
150 /* Saved fork signal mask, protected by rcu_gp_lock */
151 static sigset_t saved_fork_signal_mask
;
153 static void mutex_lock(pthread_mutex_t
*mutex
)
157 #ifndef DISTRUST_SIGNALS_EXTREME
158 ret
= pthread_mutex_lock(mutex
);
161 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
162 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
163 if (ret
!= EBUSY
&& ret
!= EINTR
)
167 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
170 static void mutex_unlock(pthread_mutex_t
*mutex
)
174 ret
= pthread_mutex_unlock(mutex
);
179 static void smp_mb_master(void)
181 if (caa_likely(lttng_ust_urcu_has_sys_membarrier
)) {
182 if (membarrier(MEMBARRIER_CMD_PRIVATE_EXPEDITED
, 0))
190 * Always called with rcu_registry lock held. Releases this lock between
191 * iterations and grabs it again. Holds the lock when it returns.
193 static void wait_for_readers(struct cds_list_head
*input_readers
,
194 struct cds_list_head
*cur_snap_readers
,
195 struct cds_list_head
*qsreaders
)
197 unsigned int wait_loops
= 0;
198 struct lttng_ust_urcu_reader
*index
, *tmp
;
201 * Wait for each thread URCU_TLS(lttng_ust_urcu_reader).ctr to either
202 * indicate quiescence (not nested), or observe the current
206 if (wait_loops
< RCU_QS_ACTIVE_ATTEMPTS
)
209 cds_list_for_each_entry_safe(index
, tmp
, input_readers
, node
) {
210 switch (lttng_ust_urcu_reader_state(&index
->ctr
)) {
211 case LTTNG_UST_URCU_READER_ACTIVE_CURRENT
:
212 if (cur_snap_readers
) {
213 cds_list_move(&index
->node
,
218 case LTTNG_UST_URCU_READER_INACTIVE
:
219 cds_list_move(&index
->node
, qsreaders
);
221 case LTTNG_UST_URCU_READER_ACTIVE_OLD
:
223 * Old snapshot. Leaving node in
224 * input_readers will make us busy-loop
225 * until the snapshot becomes current or
226 * the reader becomes inactive.
232 if (cds_list_empty(input_readers
)) {
235 /* Temporarily unlock the registry lock. */
236 mutex_unlock(&rcu_registry_lock
);
237 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
)
238 (void) poll(NULL
, 0, RCU_SLEEP_DELAY_MS
);
241 /* Re-lock the registry lock before the next loop. */
242 mutex_lock(&rcu_registry_lock
);
247 void lttng_ust_urcu_synchronize_rcu(void)
249 CDS_LIST_HEAD(cur_snap_readers
);
250 CDS_LIST_HEAD(qsreaders
);
251 sigset_t newmask
, oldmask
;
254 ret
= sigfillset(&newmask
);
256 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
259 mutex_lock(&rcu_gp_lock
);
261 mutex_lock(&rcu_registry_lock
);
263 if (cds_list_empty(®istry
))
266 /* All threads should read qparity before accessing data structure
267 * where new ptr points to. */
268 /* Write new ptr before changing the qparity */
272 * Wait for readers to observe original parity or be quiescent.
273 * wait_for_readers() can release and grab again rcu_registry_lock
276 wait_for_readers(®istry
, &cur_snap_readers
, &qsreaders
);
279 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
280 * model easier to understand. It does not have a big performance impact
281 * anyway, given this is the write-side.
285 /* Switch parity: 0 -> 1, 1 -> 0 */
286 CMM_STORE_SHARED(lttng_ust_urcu_gp
.ctr
, lttng_ust_urcu_gp
.ctr
^ LTTNG_UST_URCU_GP_CTR_PHASE
);
289 * Must commit qparity update to memory before waiting for other parity
290 * quiescent state. Failure to do so could result in the writer waiting
291 * forever while new readers are always accessing data (no progress).
292 * Ensured by CMM_STORE_SHARED and CMM_LOAD_SHARED.
296 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
297 * model easier to understand. It does not have a big performance impact
298 * anyway, given this is the write-side.
303 * Wait for readers to observe new parity or be quiescent.
304 * wait_for_readers() can release and grab again rcu_registry_lock
307 wait_for_readers(&cur_snap_readers
, NULL
, &qsreaders
);
310 * Put quiescent reader list back into registry.
312 cds_list_splice(&qsreaders
, ®istry
);
315 * Finish waiting for reader threads before letting the old ptr being
320 mutex_unlock(&rcu_registry_lock
);
321 mutex_unlock(&rcu_gp_lock
);
322 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
327 * library wrappers to be used by non-LGPL compatible source code.
330 void lttng_ust_urcu_read_lock(void)
332 _lttng_ust_urcu_read_lock();
335 void lttng_ust_urcu_read_unlock(void)
337 _lttng_ust_urcu_read_unlock();
340 int lttng_ust_urcu_read_ongoing(void)
342 return _lttng_ust_urcu_read_ongoing();
346 * Only grow for now. If empty, allocate a ARENA_INIT_ALLOC sized chunk.
347 * Else, try expanding the last chunk. If this fails, allocate a new
348 * chunk twice as big as the last chunk.
349 * Memory used by chunks _never_ moves. A chunk could theoretically be
350 * freed when all "used" slots are released, but we don't do it at this
354 void expand_arena(struct registry_arena
*arena
)
356 struct registry_chunk
*new_chunk
, *last_chunk
;
357 size_t old_chunk_len
, new_chunk_len
;
360 if (cds_list_empty(&arena
->chunk_list
)) {
361 assert(ARENA_INIT_ALLOC
>=
362 sizeof(struct registry_chunk
)
363 + sizeof(struct lttng_ust_urcu_reader
));
364 new_chunk_len
= ARENA_INIT_ALLOC
;
365 new_chunk
= (struct registry_chunk
*) mmap(NULL
,
367 PROT_READ
| PROT_WRITE
,
368 MAP_ANONYMOUS
| MAP_PRIVATE
,
370 if (new_chunk
== MAP_FAILED
)
372 memset(new_chunk
, 0, 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
);
376 return; /* We're done. */
379 /* Try expanding last chunk. */
380 last_chunk
= cds_list_entry(arena
->chunk_list
.prev
,
381 struct registry_chunk
, node
);
383 last_chunk
->data_len
+ sizeof(struct registry_chunk
);
384 new_chunk_len
= old_chunk_len
<< 1;
386 /* Don't allow memory mapping to move, just expand. */
387 new_chunk
= mremap_wrapper(last_chunk
, old_chunk_len
,
389 if (new_chunk
!= MAP_FAILED
) {
390 /* Should not have moved. */
391 assert(new_chunk
== last_chunk
);
392 memset((char *) last_chunk
+ old_chunk_len
, 0,
393 new_chunk_len
- old_chunk_len
);
394 last_chunk
->data_len
=
395 new_chunk_len
- sizeof(struct registry_chunk
);
396 return; /* We're done. */
399 /* Remap did not succeed, we need to add a new chunk. */
400 new_chunk
= (struct registry_chunk
*) mmap(NULL
,
402 PROT_READ
| PROT_WRITE
,
403 MAP_ANONYMOUS
| MAP_PRIVATE
,
405 if (new_chunk
== MAP_FAILED
)
407 memset(new_chunk
, 0, new_chunk_len
);
408 new_chunk
->data_len
=
409 new_chunk_len
- sizeof(struct registry_chunk
);
410 cds_list_add_tail(&new_chunk
->node
, &arena
->chunk_list
);
414 struct lttng_ust_urcu_reader
*arena_alloc(struct registry_arena
*arena
)
416 struct registry_chunk
*chunk
;
417 struct lttng_ust_urcu_reader
*rcu_reader_reg
;
418 int expand_done
= 0; /* Only allow to expand once per alloc */
419 size_t len
= sizeof(struct lttng_ust_urcu_reader
);
422 cds_list_for_each_entry(chunk
, &arena
->chunk_list
, node
) {
423 if (chunk
->data_len
- chunk
->used
< len
)
426 for (rcu_reader_reg
= (struct lttng_ust_urcu_reader
*) &chunk
->data
[0];
427 rcu_reader_reg
< (struct lttng_ust_urcu_reader
*) &chunk
->data
[chunk
->data_len
];
429 if (!rcu_reader_reg
->alloc
) {
430 rcu_reader_reg
->alloc
= 1;
432 return rcu_reader_reg
;
446 /* Called with signals off and mutex locked */
448 void add_thread(void)
450 struct lttng_ust_urcu_reader
*rcu_reader_reg
;
453 rcu_reader_reg
= arena_alloc(®istry_arena
);
456 ret
= pthread_setspecific(lttng_ust_urcu_key
, rcu_reader_reg
);
460 /* Add to registry */
461 rcu_reader_reg
->tid
= pthread_self();
462 assert(rcu_reader_reg
->ctr
== 0);
463 cds_list_add(&rcu_reader_reg
->node
, ®istry
);
465 * Reader threads are pointing to the reader registry. This is
466 * why its memory should never be relocated.
468 URCU_TLS(lttng_ust_urcu_reader
) = rcu_reader_reg
;
471 /* Called with mutex locked */
473 void cleanup_thread(struct registry_chunk
*chunk
,
474 struct lttng_ust_urcu_reader
*rcu_reader_reg
)
476 rcu_reader_reg
->ctr
= 0;
477 cds_list_del(&rcu_reader_reg
->node
);
478 rcu_reader_reg
->tid
= 0;
479 rcu_reader_reg
->alloc
= 0;
480 chunk
->used
-= sizeof(struct lttng_ust_urcu_reader
);
484 struct registry_chunk
*find_chunk(struct lttng_ust_urcu_reader
*rcu_reader_reg
)
486 struct registry_chunk
*chunk
;
488 cds_list_for_each_entry(chunk
, ®istry_arena
.chunk_list
, node
) {
489 if (rcu_reader_reg
< (struct lttng_ust_urcu_reader
*) &chunk
->data
[0])
491 if (rcu_reader_reg
>= (struct lttng_ust_urcu_reader
*) &chunk
->data
[chunk
->data_len
])
498 /* Called with signals off and mutex locked */
500 void remove_thread(struct lttng_ust_urcu_reader
*rcu_reader_reg
)
502 cleanup_thread(find_chunk(rcu_reader_reg
), rcu_reader_reg
);
503 URCU_TLS(lttng_ust_urcu_reader
) = NULL
;
506 /* Disable signals, take mutex, add to registry */
507 void lttng_ust_urcu_register(void)
509 sigset_t newmask
, oldmask
;
512 ret
= sigfillset(&newmask
);
515 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
520 * Check if a signal concurrently registered our thread since
521 * the check in rcu_read_lock().
523 if (URCU_TLS(lttng_ust_urcu_reader
))
527 * Take care of early registration before lttng_ust_urcu constructor.
529 _lttng_ust_urcu_init();
531 mutex_lock(&rcu_registry_lock
);
533 mutex_unlock(&rcu_registry_lock
);
535 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
540 void lttng_ust_urcu_register_thread(void)
542 if (caa_unlikely(!URCU_TLS(lttng_ust_urcu_reader
)))
543 lttng_ust_urcu_register(); /* If not yet registered. */
546 /* Disable signals, take mutex, remove from registry */
548 void lttng_ust_urcu_unregister(struct lttng_ust_urcu_reader
*rcu_reader_reg
)
550 sigset_t newmask
, oldmask
;
553 ret
= sigfillset(&newmask
);
556 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
560 mutex_lock(&rcu_registry_lock
);
561 remove_thread(rcu_reader_reg
);
562 mutex_unlock(&rcu_registry_lock
);
563 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
566 lttng_ust_urcu_exit();
570 * Remove thread from the registry when it exits, and flag it as
571 * destroyed so garbage collection can take care of it.
574 void lttng_ust_urcu_thread_exit_notifier(void *rcu_key
)
576 lttng_ust_urcu_unregister(rcu_key
);
579 #ifdef CONFIG_RCU_FORCE_SYS_MEMBARRIER
581 void lttng_ust_urcu_sys_membarrier_status(bool available
)
588 void lttng_ust_urcu_sys_membarrier_status(bool available
)
592 lttng_ust_urcu_has_sys_membarrier
= 1;
597 void lttng_ust_urcu_sys_membarrier_init(void)
599 bool available
= false;
602 mask
= membarrier(MEMBARRIER_CMD_QUERY
, 0);
604 if (mask
& MEMBARRIER_CMD_PRIVATE_EXPEDITED
) {
605 if (membarrier(MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED
, 0))
610 lttng_ust_urcu_sys_membarrier_status(available
);
614 void _lttng_ust_urcu_init(void)
616 mutex_lock(&init_lock
);
617 if (!lttng_ust_urcu_refcount
++) {
620 ret
= pthread_key_create(<tng_ust_urcu_key
,
621 lttng_ust_urcu_thread_exit_notifier
);
624 lttng_ust_urcu_sys_membarrier_init();
627 mutex_unlock(&init_lock
);
631 void lttng_ust_urcu_exit(void)
633 mutex_lock(&init_lock
);
634 if (!--lttng_ust_urcu_refcount
) {
635 struct registry_chunk
*chunk
, *tmp
;
638 cds_list_for_each_entry_safe(chunk
, tmp
,
639 ®istry_arena
.chunk_list
, node
) {
640 munmap((void *) chunk
, chunk
->data_len
641 + sizeof(struct registry_chunk
));
643 CDS_INIT_LIST_HEAD(®istry_arena
.chunk_list
);
644 ret
= pthread_key_delete(lttng_ust_urcu_key
);
648 mutex_unlock(&init_lock
);
652 * Holding the rcu_gp_lock and rcu_registry_lock across fork will make
653 * sure we fork() don't race with a concurrent thread executing with
654 * any of those locks held. This ensures that the registry and data
655 * protected by rcu_gp_lock are in a coherent state in the child.
657 void lttng_ust_urcu_before_fork(void)
659 sigset_t newmask
, oldmask
;
662 ret
= sigfillset(&newmask
);
664 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
666 mutex_lock(&rcu_gp_lock
);
667 mutex_lock(&rcu_registry_lock
);
668 saved_fork_signal_mask
= oldmask
;
671 void lttng_ust_urcu_after_fork_parent(void)
676 oldmask
= saved_fork_signal_mask
;
677 mutex_unlock(&rcu_registry_lock
);
678 mutex_unlock(&rcu_gp_lock
);
679 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
684 * Prune all entries from registry except our own thread. Fits the Linux
685 * fork behavior. Called with rcu_gp_lock and rcu_registry_lock held.
688 void lttng_ust_urcu_prune_registry(void)
690 struct registry_chunk
*chunk
;
691 struct lttng_ust_urcu_reader
*rcu_reader_reg
;
693 cds_list_for_each_entry(chunk
, ®istry_arena
.chunk_list
, node
) {
694 for (rcu_reader_reg
= (struct lttng_ust_urcu_reader
*) &chunk
->data
[0];
695 rcu_reader_reg
< (struct lttng_ust_urcu_reader
*) &chunk
->data
[chunk
->data_len
];
697 if (!rcu_reader_reg
->alloc
)
699 if (rcu_reader_reg
->tid
== pthread_self())
701 cleanup_thread(chunk
, rcu_reader_reg
);
706 void lttng_ust_urcu_after_fork_child(void)
711 lttng_ust_urcu_prune_registry();
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
);