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 _lttng_ust_urcu_init(void)
97 __attribute__((constructor
));
99 void __attribute__((destructor
)) lttng_ust_urcu_exit(void);
101 #ifndef CONFIG_RCU_FORCE_SYS_MEMBARRIER
102 int lttng_ust_urcu_has_sys_membarrier
;
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
;
121 static pthread_mutex_t init_lock
= PTHREAD_MUTEX_INITIALIZER
;
122 static int initialized
;
124 static pthread_key_t lttng_ust_urcu_key
;
126 struct lttng_ust_urcu_gp lttng_ust_urcu_gp
= { .ctr
= LTTNG_UST_URCU_GP_COUNT
};
129 * Pointer to registry elements. Written to only by each individual reader. Read
130 * by both the reader and the writers.
132 DEFINE_URCU_TLS(struct lttng_ust_urcu_reader
*, lttng_ust_urcu_reader
);
134 static CDS_LIST_HEAD(registry
);
136 struct registry_chunk
{
137 size_t data_len
; /* data length */
138 size_t used
; /* amount of data used */
139 struct cds_list_head node
; /* chunk_list node */
143 struct registry_arena
{
144 struct cds_list_head chunk_list
;
147 static struct registry_arena registry_arena
= {
148 .chunk_list
= CDS_LIST_HEAD_INIT(registry_arena
.chunk_list
),
151 /* Saved fork signal mask, protected by rcu_gp_lock */
152 static sigset_t saved_fork_signal_mask
;
154 static void mutex_lock(pthread_mutex_t
*mutex
)
158 #ifndef DISTRUST_SIGNALS_EXTREME
159 ret
= pthread_mutex_lock(mutex
);
162 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
163 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
164 if (ret
!= EBUSY
&& ret
!= EINTR
)
168 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
171 static void mutex_unlock(pthread_mutex_t
*mutex
)
175 ret
= pthread_mutex_unlock(mutex
);
180 static void smp_mb_master(void)
182 if (caa_likely(lttng_ust_urcu_has_sys_membarrier
)) {
183 if (membarrier(MEMBARRIER_CMD_PRIVATE_EXPEDITED
, 0))
191 * Always called with rcu_registry lock held. Releases this lock between
192 * iterations and grabs it again. Holds the lock when it returns.
194 static void wait_for_readers(struct cds_list_head
*input_readers
,
195 struct cds_list_head
*cur_snap_readers
,
196 struct cds_list_head
*qsreaders
)
198 unsigned int wait_loops
= 0;
199 struct lttng_ust_urcu_reader
*index
, *tmp
;
202 * Wait for each thread URCU_TLS(lttng_ust_urcu_reader).ctr to either
203 * indicate quiescence (not nested), or observe the current
207 if (wait_loops
< RCU_QS_ACTIVE_ATTEMPTS
)
210 cds_list_for_each_entry_safe(index
, tmp
, input_readers
, node
) {
211 switch (lttng_ust_urcu_reader_state(&index
->ctr
)) {
212 case LTTNG_UST_URCU_READER_ACTIVE_CURRENT
:
213 if (cur_snap_readers
) {
214 cds_list_move(&index
->node
,
219 case LTTNG_UST_URCU_READER_INACTIVE
:
220 cds_list_move(&index
->node
, qsreaders
);
222 case LTTNG_UST_URCU_READER_ACTIVE_OLD
:
224 * Old snapshot. Leaving node in
225 * input_readers will make us busy-loop
226 * until the snapshot becomes current or
227 * the reader becomes inactive.
233 if (cds_list_empty(input_readers
)) {
236 /* Temporarily unlock the registry lock. */
237 mutex_unlock(&rcu_registry_lock
);
238 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
)
239 (void) poll(NULL
, 0, RCU_SLEEP_DELAY_MS
);
242 /* Re-lock the registry lock before the next loop. */
243 mutex_lock(&rcu_registry_lock
);
248 void lttng_ust_urcu_synchronize_rcu(void)
250 CDS_LIST_HEAD(cur_snap_readers
);
251 CDS_LIST_HEAD(qsreaders
);
252 sigset_t newmask
, oldmask
;
255 ret
= sigfillset(&newmask
);
257 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
260 mutex_lock(&rcu_gp_lock
);
262 mutex_lock(&rcu_registry_lock
);
264 if (cds_list_empty(®istry
))
267 /* All threads should read qparity before accessing data structure
268 * where new ptr points to. */
269 /* Write new ptr before changing the qparity */
273 * Wait for readers to observe original parity or be quiescent.
274 * wait_for_readers() can release and grab again rcu_registry_lock
277 wait_for_readers(®istry
, &cur_snap_readers
, &qsreaders
);
280 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
281 * model easier to understand. It does not have a big performance impact
282 * anyway, given this is the write-side.
286 /* Switch parity: 0 -> 1, 1 -> 0 */
287 CMM_STORE_SHARED(lttng_ust_urcu_gp
.ctr
, lttng_ust_urcu_gp
.ctr
^ LTTNG_UST_URCU_GP_CTR_PHASE
);
290 * Must commit qparity update to memory before waiting for other parity
291 * quiescent state. Failure to do so could result in the writer waiting
292 * forever while new readers are always accessing data (no progress).
293 * Ensured by CMM_STORE_SHARED and CMM_LOAD_SHARED.
297 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
298 * model easier to understand. It does not have a big performance impact
299 * anyway, given this is the write-side.
304 * Wait for readers to observe new parity or be quiescent.
305 * wait_for_readers() can release and grab again rcu_registry_lock
308 wait_for_readers(&cur_snap_readers
, NULL
, &qsreaders
);
311 * Put quiescent reader list back into registry.
313 cds_list_splice(&qsreaders
, ®istry
);
316 * Finish waiting for reader threads before letting the old ptr being
321 mutex_unlock(&rcu_registry_lock
);
322 mutex_unlock(&rcu_gp_lock
);
323 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
328 * library wrappers to be used by non-LGPL compatible source code.
331 void lttng_ust_urcu_read_lock(void)
333 _lttng_ust_urcu_read_lock();
336 void lttng_ust_urcu_read_unlock(void)
338 _lttng_ust_urcu_read_unlock();
341 int lttng_ust_urcu_read_ongoing(void)
343 return _lttng_ust_urcu_read_ongoing();
347 * Only grow for now. If empty, allocate a ARENA_INIT_ALLOC sized chunk.
348 * Else, try expanding the last chunk. If this fails, allocate a new
349 * chunk twice as big as the last chunk.
350 * Memory used by chunks _never_ moves. A chunk could theoretically be
351 * freed when all "used" slots are released, but we don't do it at this
355 void expand_arena(struct registry_arena
*arena
)
357 struct registry_chunk
*new_chunk
, *last_chunk
;
358 size_t old_chunk_len
, new_chunk_len
;
361 if (cds_list_empty(&arena
->chunk_list
)) {
362 assert(ARENA_INIT_ALLOC
>=
363 sizeof(struct registry_chunk
)
364 + sizeof(struct lttng_ust_urcu_reader
));
365 new_chunk_len
= ARENA_INIT_ALLOC
;
366 new_chunk
= (struct registry_chunk
*) mmap(NULL
,
368 PROT_READ
| PROT_WRITE
,
369 MAP_ANONYMOUS
| MAP_PRIVATE
,
371 if (new_chunk
== MAP_FAILED
)
373 memset(new_chunk
, 0, new_chunk_len
);
374 new_chunk
->data_len
=
375 new_chunk_len
- sizeof(struct registry_chunk
);
376 cds_list_add_tail(&new_chunk
->node
, &arena
->chunk_list
);
377 return; /* We're done. */
380 /* Try expanding last chunk. */
381 last_chunk
= cds_list_entry(arena
->chunk_list
.prev
,
382 struct registry_chunk
, node
);
384 last_chunk
->data_len
+ sizeof(struct registry_chunk
);
385 new_chunk_len
= old_chunk_len
<< 1;
387 /* Don't allow memory mapping to move, just expand. */
388 new_chunk
= mremap_wrapper(last_chunk
, old_chunk_len
,
390 if (new_chunk
!= MAP_FAILED
) {
391 /* Should not have moved. */
392 assert(new_chunk
== last_chunk
);
393 memset((char *) last_chunk
+ old_chunk_len
, 0,
394 new_chunk_len
- old_chunk_len
);
395 last_chunk
->data_len
=
396 new_chunk_len
- sizeof(struct registry_chunk
);
397 return; /* We're done. */
400 /* Remap did not succeed, we need to add a new chunk. */
401 new_chunk
= (struct registry_chunk
*) mmap(NULL
,
403 PROT_READ
| PROT_WRITE
,
404 MAP_ANONYMOUS
| MAP_PRIVATE
,
406 if (new_chunk
== MAP_FAILED
)
408 memset(new_chunk
, 0, new_chunk_len
);
409 new_chunk
->data_len
=
410 new_chunk_len
- sizeof(struct registry_chunk
);
411 cds_list_add_tail(&new_chunk
->node
, &arena
->chunk_list
);
415 struct lttng_ust_urcu_reader
*arena_alloc(struct registry_arena
*arena
)
417 struct registry_chunk
*chunk
;
418 struct lttng_ust_urcu_reader
*rcu_reader_reg
;
419 int expand_done
= 0; /* Only allow to expand once per alloc */
420 size_t len
= sizeof(struct lttng_ust_urcu_reader
);
423 cds_list_for_each_entry(chunk
, &arena
->chunk_list
, node
) {
424 if (chunk
->data_len
- chunk
->used
< len
)
427 for (rcu_reader_reg
= (struct lttng_ust_urcu_reader
*) &chunk
->data
[0];
428 rcu_reader_reg
< (struct lttng_ust_urcu_reader
*) &chunk
->data
[chunk
->data_len
];
430 if (!rcu_reader_reg
->alloc
) {
431 rcu_reader_reg
->alloc
= 1;
433 return rcu_reader_reg
;
447 /* Called with signals off and mutex locked */
449 void add_thread(void)
451 struct lttng_ust_urcu_reader
*rcu_reader_reg
;
454 rcu_reader_reg
= arena_alloc(®istry_arena
);
457 ret
= pthread_setspecific(lttng_ust_urcu_key
, rcu_reader_reg
);
461 /* Add to registry */
462 rcu_reader_reg
->tid
= pthread_self();
463 assert(rcu_reader_reg
->ctr
== 0);
464 cds_list_add(&rcu_reader_reg
->node
, ®istry
);
466 * Reader threads are pointing to the reader registry. This is
467 * why its memory should never be relocated.
469 URCU_TLS(lttng_ust_urcu_reader
) = rcu_reader_reg
;
472 /* Called with mutex locked */
474 void cleanup_thread(struct registry_chunk
*chunk
,
475 struct lttng_ust_urcu_reader
*rcu_reader_reg
)
477 rcu_reader_reg
->ctr
= 0;
478 cds_list_del(&rcu_reader_reg
->node
);
479 rcu_reader_reg
->tid
= 0;
480 rcu_reader_reg
->alloc
= 0;
481 chunk
->used
-= sizeof(struct lttng_ust_urcu_reader
);
485 struct registry_chunk
*find_chunk(struct lttng_ust_urcu_reader
*rcu_reader_reg
)
487 struct registry_chunk
*chunk
;
489 cds_list_for_each_entry(chunk
, ®istry_arena
.chunk_list
, node
) {
490 if (rcu_reader_reg
< (struct lttng_ust_urcu_reader
*) &chunk
->data
[0])
492 if (rcu_reader_reg
>= (struct lttng_ust_urcu_reader
*) &chunk
->data
[chunk
->data_len
])
499 /* Called with signals off and mutex locked */
501 void remove_thread(struct lttng_ust_urcu_reader
*rcu_reader_reg
)
503 cleanup_thread(find_chunk(rcu_reader_reg
), rcu_reader_reg
);
504 URCU_TLS(lttng_ust_urcu_reader
) = NULL
;
507 /* Disable signals, take mutex, add to registry */
508 void lttng_ust_urcu_register(void)
510 sigset_t newmask
, oldmask
;
513 ret
= sigfillset(&newmask
);
516 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
521 * Check if a signal concurrently registered our thread since
522 * the check in rcu_read_lock().
524 if (URCU_TLS(lttng_ust_urcu_reader
))
528 * Take care of early registration before lttng_ust_urcu constructor.
530 _lttng_ust_urcu_init();
532 mutex_lock(&rcu_registry_lock
);
534 mutex_unlock(&rcu_registry_lock
);
536 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
541 void lttng_ust_urcu_register_thread(void)
543 if (caa_unlikely(!URCU_TLS(lttng_ust_urcu_reader
)))
544 lttng_ust_urcu_register(); /* If not yet registered. */
547 /* Disable signals, take mutex, remove from registry */
549 void lttng_ust_urcu_unregister(struct lttng_ust_urcu_reader
*rcu_reader_reg
)
551 sigset_t newmask
, oldmask
;
554 ret
= sigfillset(&newmask
);
557 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
561 mutex_lock(&rcu_registry_lock
);
562 remove_thread(rcu_reader_reg
);
563 mutex_unlock(&rcu_registry_lock
);
564 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
567 lttng_ust_urcu_exit();
571 * Remove thread from the registry when it exits, and flag it as
572 * destroyed so garbage collection can take care of it.
575 void lttng_ust_urcu_thread_exit_notifier(void *rcu_key
)
577 lttng_ust_urcu_unregister(rcu_key
);
580 #ifdef CONFIG_RCU_FORCE_SYS_MEMBARRIER
582 void lttng_ust_urcu_sys_membarrier_status(bool available
)
589 void lttng_ust_urcu_sys_membarrier_status(bool available
)
593 lttng_ust_urcu_has_sys_membarrier
= 1;
598 void lttng_ust_urcu_sys_membarrier_init(void)
600 bool available
= false;
603 mask
= membarrier(MEMBARRIER_CMD_QUERY
, 0);
605 if (mask
& MEMBARRIER_CMD_PRIVATE_EXPEDITED
) {
606 if (membarrier(MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED
, 0))
611 lttng_ust_urcu_sys_membarrier_status(available
);
615 void _lttng_ust_urcu_init(void)
617 mutex_lock(&init_lock
);
618 if (!lttng_ust_urcu_refcount
++) {
621 ret
= pthread_key_create(<tng_ust_urcu_key
,
622 lttng_ust_urcu_thread_exit_notifier
);
625 lttng_ust_urcu_sys_membarrier_init();
628 mutex_unlock(&init_lock
);
632 void lttng_ust_urcu_exit(void)
634 mutex_lock(&init_lock
);
635 if (!--lttng_ust_urcu_refcount
) {
636 struct registry_chunk
*chunk
, *tmp
;
639 cds_list_for_each_entry_safe(chunk
, tmp
,
640 ®istry_arena
.chunk_list
, node
) {
641 munmap((void *) chunk
, chunk
->data_len
642 + sizeof(struct registry_chunk
));
644 CDS_INIT_LIST_HEAD(®istry_arena
.chunk_list
);
645 ret
= pthread_key_delete(lttng_ust_urcu_key
);
649 mutex_unlock(&init_lock
);
653 * Holding the rcu_gp_lock and rcu_registry_lock across fork will make
654 * sure we fork() don't race with a concurrent thread executing with
655 * any of those locks held. This ensures that the registry and data
656 * protected by rcu_gp_lock are in a coherent state in the child.
658 void lttng_ust_urcu_before_fork(void)
660 sigset_t newmask
, oldmask
;
663 ret
= sigfillset(&newmask
);
665 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
667 mutex_lock(&rcu_gp_lock
);
668 mutex_lock(&rcu_registry_lock
);
669 saved_fork_signal_mask
= oldmask
;
672 void lttng_ust_urcu_after_fork_parent(void)
677 oldmask
= saved_fork_signal_mask
;
678 mutex_unlock(&rcu_registry_lock
);
679 mutex_unlock(&rcu_gp_lock
);
680 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
685 * Prune all entries from registry except our own thread. Fits the Linux
686 * fork behavior. Called with rcu_gp_lock and rcu_registry_lock held.
689 void lttng_ust_urcu_prune_registry(void)
691 struct registry_chunk
*chunk
;
692 struct lttng_ust_urcu_reader
*rcu_reader_reg
;
694 cds_list_for_each_entry(chunk
, ®istry_arena
.chunk_list
, node
) {
695 for (rcu_reader_reg
= (struct lttng_ust_urcu_reader
*) &chunk
->data
[0];
696 rcu_reader_reg
< (struct lttng_ust_urcu_reader
*) &chunk
->data
[chunk
->data_len
];
698 if (!rcu_reader_reg
->alloc
)
700 if (rcu_reader_reg
->tid
== pthread_self())
702 cleanup_thread(chunk
, rcu_reader_reg
);
707 void lttng_ust_urcu_after_fork_child(void)
712 lttng_ust_urcu_prune_registry();
713 oldmask
= saved_fork_signal_mask
;
714 mutex_unlock(&rcu_registry_lock
);
715 mutex_unlock(&rcu_gp_lock
);
716 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);