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
__attribute__((unused
)),
56 size_t old_size
__attribute__((unused
)),
57 size_t new_size
__attribute__((unused
)),
60 assert(!(flags
& MREMAP_MAYMOVE
));
66 /* Sleep delay in ms */
67 #define RCU_SLEEP_DELAY_MS 10
68 #define INIT_NR_THREADS 8
69 #define ARENA_INIT_ALLOC \
70 sizeof(struct registry_chunk) \
71 + INIT_NR_THREADS * sizeof(struct lttng_ust_urcu_reader)
74 * Active attempts to check for reader Q.S. before calling sleep().
76 #define RCU_QS_ACTIVE_ATTEMPTS 100
79 int lttng_ust_urcu_refcount
;
81 /* If the headers do not support membarrier system call, fall back smp_mb. */
82 #ifdef __NR_membarrier
83 # define membarrier(...) syscall(__NR_membarrier, __VA_ARGS__)
85 # define membarrier(...) -ENOSYS
89 MEMBARRIER_CMD_QUERY
= 0,
90 MEMBARRIER_CMD_SHARED
= (1 << 0),
91 /* reserved for MEMBARRIER_CMD_SHARED_EXPEDITED (1 << 1) */
92 /* reserved for MEMBARRIER_CMD_PRIVATE (1 << 2) */
93 MEMBARRIER_CMD_PRIVATE_EXPEDITED
= (1 << 3),
94 MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED
= (1 << 4),
98 void _lttng_ust_urcu_init(void)
99 __attribute__((constructor
));
101 void lttng_ust_urcu_exit(void)
102 __attribute__((destructor
));
104 #ifndef CONFIG_RCU_FORCE_SYS_MEMBARRIER
105 int lttng_ust_urcu_has_sys_membarrier
;
109 * rcu_gp_lock ensures mutual exclusion between threads calling
112 static pthread_mutex_t rcu_gp_lock
= PTHREAD_MUTEX_INITIALIZER
;
114 * rcu_registry_lock ensures mutual exclusion between threads
115 * registering and unregistering themselves to/from the registry, and
116 * with threads reading that registry from synchronize_rcu(). However,
117 * this lock is not held all the way through the completion of awaiting
118 * for the grace period. It is sporadically released between iterations
120 * rcu_registry_lock may nest inside rcu_gp_lock.
122 static pthread_mutex_t rcu_registry_lock
= PTHREAD_MUTEX_INITIALIZER
;
124 static pthread_mutex_t init_lock
= PTHREAD_MUTEX_INITIALIZER
;
125 static int initialized
;
127 static pthread_key_t lttng_ust_urcu_key
;
129 struct lttng_ust_urcu_gp lttng_ust_urcu_gp
= { .ctr
= LTTNG_UST_URCU_GP_COUNT
};
132 * Pointer to registry elements. Written to only by each individual reader. Read
133 * by both the reader and the writers.
135 DEFINE_URCU_TLS(struct lttng_ust_urcu_reader
*, lttng_ust_urcu_reader
);
137 static CDS_LIST_HEAD(registry
);
139 struct registry_chunk
{
140 size_t data_len
; /* data length */
141 size_t used
; /* amount of data used */
142 struct cds_list_head node
; /* chunk_list node */
146 struct registry_arena
{
147 struct cds_list_head chunk_list
;
150 static struct registry_arena registry_arena
= {
151 .chunk_list
= CDS_LIST_HEAD_INIT(registry_arena
.chunk_list
),
154 /* Saved fork signal mask, protected by rcu_gp_lock */
155 static sigset_t saved_fork_signal_mask
;
157 static void mutex_lock(pthread_mutex_t
*mutex
)
161 #ifndef DISTRUST_SIGNALS_EXTREME
162 ret
= pthread_mutex_lock(mutex
);
165 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
166 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
167 if (ret
!= EBUSY
&& ret
!= EINTR
)
171 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
174 static void mutex_unlock(pthread_mutex_t
*mutex
)
178 ret
= pthread_mutex_unlock(mutex
);
183 static void smp_mb_master(void)
185 if (caa_likely(lttng_ust_urcu_has_sys_membarrier
)) {
186 if (membarrier(MEMBARRIER_CMD_PRIVATE_EXPEDITED
, 0))
194 * Always called with rcu_registry lock held. Releases this lock between
195 * iterations and grabs it again. Holds the lock when it returns.
197 static void wait_for_readers(struct cds_list_head
*input_readers
,
198 struct cds_list_head
*cur_snap_readers
,
199 struct cds_list_head
*qsreaders
)
201 unsigned int wait_loops
= 0;
202 struct lttng_ust_urcu_reader
*index
, *tmp
;
205 * Wait for each thread URCU_TLS(lttng_ust_urcu_reader).ctr to either
206 * indicate quiescence (not nested), or observe the current
210 if (wait_loops
< RCU_QS_ACTIVE_ATTEMPTS
)
213 cds_list_for_each_entry_safe(index
, tmp
, input_readers
, node
) {
214 switch (lttng_ust_urcu_reader_state(&index
->ctr
)) {
215 case LTTNG_UST_URCU_READER_ACTIVE_CURRENT
:
216 if (cur_snap_readers
) {
217 cds_list_move(&index
->node
,
222 case LTTNG_UST_URCU_READER_INACTIVE
:
223 cds_list_move(&index
->node
, qsreaders
);
225 case LTTNG_UST_URCU_READER_ACTIVE_OLD
:
227 * Old snapshot. Leaving node in
228 * input_readers will make us busy-loop
229 * until the snapshot becomes current or
230 * the reader becomes inactive.
236 if (cds_list_empty(input_readers
)) {
239 /* Temporarily unlock the registry lock. */
240 mutex_unlock(&rcu_registry_lock
);
241 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
)
242 (void) poll(NULL
, 0, RCU_SLEEP_DELAY_MS
);
245 /* Re-lock the registry lock before the next loop. */
246 mutex_lock(&rcu_registry_lock
);
251 void lttng_ust_urcu_synchronize_rcu(void)
253 CDS_LIST_HEAD(cur_snap_readers
);
254 CDS_LIST_HEAD(qsreaders
);
255 sigset_t newmask
, oldmask
;
258 ret
= sigfillset(&newmask
);
260 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
263 mutex_lock(&rcu_gp_lock
);
265 mutex_lock(&rcu_registry_lock
);
267 if (cds_list_empty(®istry
))
270 /* All threads should read qparity before accessing data structure
271 * where new ptr points to. */
272 /* Write new ptr before changing the qparity */
276 * Wait for readers to observe original parity or be quiescent.
277 * wait_for_readers() can release and grab again rcu_registry_lock
280 wait_for_readers(®istry
, &cur_snap_readers
, &qsreaders
);
283 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
284 * model easier to understand. It does not have a big performance impact
285 * anyway, given this is the write-side.
289 /* Switch parity: 0 -> 1, 1 -> 0 */
290 CMM_STORE_SHARED(lttng_ust_urcu_gp
.ctr
, lttng_ust_urcu_gp
.ctr
^ LTTNG_UST_URCU_GP_CTR_PHASE
);
293 * Must commit qparity update to memory before waiting for other parity
294 * quiescent state. Failure to do so could result in the writer waiting
295 * forever while new readers are always accessing data (no progress).
296 * Ensured by CMM_STORE_SHARED and CMM_LOAD_SHARED.
300 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
301 * model easier to understand. It does not have a big performance impact
302 * anyway, given this is the write-side.
307 * Wait for readers to observe new parity or be quiescent.
308 * wait_for_readers() can release and grab again rcu_registry_lock
311 wait_for_readers(&cur_snap_readers
, NULL
, &qsreaders
);
314 * Put quiescent reader list back into registry.
316 cds_list_splice(&qsreaders
, ®istry
);
319 * Finish waiting for reader threads before letting the old ptr being
324 mutex_unlock(&rcu_registry_lock
);
325 mutex_unlock(&rcu_gp_lock
);
326 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
331 * library wrappers to be used by non-LGPL compatible source code.
334 void lttng_ust_urcu_read_lock(void)
336 _lttng_ust_urcu_read_lock();
339 void lttng_ust_urcu_read_unlock(void)
341 _lttng_ust_urcu_read_unlock();
344 int lttng_ust_urcu_read_ongoing(void)
346 return _lttng_ust_urcu_read_ongoing();
350 * Only grow for now. If empty, allocate a ARENA_INIT_ALLOC sized chunk.
351 * Else, try expanding the last chunk. If this fails, allocate a new
352 * chunk twice as big as the last chunk.
353 * Memory used by chunks _never_ moves. A chunk could theoretically be
354 * freed when all "used" slots are released, but we don't do it at this
358 void expand_arena(struct registry_arena
*arena
)
360 struct registry_chunk
*new_chunk
, *last_chunk
;
361 size_t old_chunk_len
, new_chunk_len
;
364 if (cds_list_empty(&arena
->chunk_list
)) {
365 assert(ARENA_INIT_ALLOC
>=
366 sizeof(struct registry_chunk
)
367 + sizeof(struct lttng_ust_urcu_reader
));
368 new_chunk_len
= ARENA_INIT_ALLOC
;
369 new_chunk
= (struct registry_chunk
*) mmap(NULL
,
371 PROT_READ
| PROT_WRITE
,
372 MAP_ANONYMOUS
| MAP_PRIVATE
,
374 if (new_chunk
== MAP_FAILED
)
376 memset(new_chunk
, 0, new_chunk_len
);
377 new_chunk
->data_len
=
378 new_chunk_len
- sizeof(struct registry_chunk
);
379 cds_list_add_tail(&new_chunk
->node
, &arena
->chunk_list
);
380 return; /* We're done. */
383 /* Try expanding last chunk. */
384 last_chunk
= cds_list_entry(arena
->chunk_list
.prev
,
385 struct registry_chunk
, node
);
387 last_chunk
->data_len
+ sizeof(struct registry_chunk
);
388 new_chunk_len
= old_chunk_len
<< 1;
390 /* Don't allow memory mapping to move, just expand. */
391 new_chunk
= mremap_wrapper(last_chunk
, old_chunk_len
,
393 if (new_chunk
!= MAP_FAILED
) {
394 /* Should not have moved. */
395 assert(new_chunk
== last_chunk
);
396 memset((char *) last_chunk
+ old_chunk_len
, 0,
397 new_chunk_len
- old_chunk_len
);
398 last_chunk
->data_len
=
399 new_chunk_len
- sizeof(struct registry_chunk
);
400 return; /* We're done. */
403 /* Remap did not succeed, we need to add a new chunk. */
404 new_chunk
= (struct registry_chunk
*) mmap(NULL
,
406 PROT_READ
| PROT_WRITE
,
407 MAP_ANONYMOUS
| MAP_PRIVATE
,
409 if (new_chunk
== MAP_FAILED
)
411 memset(new_chunk
, 0, new_chunk_len
);
412 new_chunk
->data_len
=
413 new_chunk_len
- sizeof(struct registry_chunk
);
414 cds_list_add_tail(&new_chunk
->node
, &arena
->chunk_list
);
418 struct lttng_ust_urcu_reader
*arena_alloc(struct registry_arena
*arena
)
420 struct registry_chunk
*chunk
;
421 struct lttng_ust_urcu_reader
*rcu_reader_reg
;
422 int expand_done
= 0; /* Only allow to expand once per alloc */
423 size_t len
= sizeof(struct lttng_ust_urcu_reader
);
426 cds_list_for_each_entry(chunk
, &arena
->chunk_list
, node
) {
427 if (chunk
->data_len
- chunk
->used
< len
)
430 for (rcu_reader_reg
= (struct lttng_ust_urcu_reader
*) &chunk
->data
[0];
431 rcu_reader_reg
< (struct lttng_ust_urcu_reader
*) &chunk
->data
[chunk
->data_len
];
433 if (!rcu_reader_reg
->alloc
) {
434 rcu_reader_reg
->alloc
= 1;
436 return rcu_reader_reg
;
450 /* Called with signals off and mutex locked */
452 void add_thread(void)
454 struct lttng_ust_urcu_reader
*rcu_reader_reg
;
457 rcu_reader_reg
= arena_alloc(®istry_arena
);
460 ret
= pthread_setspecific(lttng_ust_urcu_key
, rcu_reader_reg
);
464 /* Add to registry */
465 rcu_reader_reg
->tid
= pthread_self();
466 assert(rcu_reader_reg
->ctr
== 0);
467 cds_list_add(&rcu_reader_reg
->node
, ®istry
);
469 * Reader threads are pointing to the reader registry. This is
470 * why its memory should never be relocated.
472 URCU_TLS(lttng_ust_urcu_reader
) = rcu_reader_reg
;
475 /* Called with mutex locked */
477 void cleanup_thread(struct registry_chunk
*chunk
,
478 struct lttng_ust_urcu_reader
*rcu_reader_reg
)
480 rcu_reader_reg
->ctr
= 0;
481 cds_list_del(&rcu_reader_reg
->node
);
482 rcu_reader_reg
->tid
= 0;
483 rcu_reader_reg
->alloc
= 0;
484 chunk
->used
-= sizeof(struct lttng_ust_urcu_reader
);
488 struct registry_chunk
*find_chunk(struct lttng_ust_urcu_reader
*rcu_reader_reg
)
490 struct registry_chunk
*chunk
;
492 cds_list_for_each_entry(chunk
, ®istry_arena
.chunk_list
, node
) {
493 if (rcu_reader_reg
< (struct lttng_ust_urcu_reader
*) &chunk
->data
[0])
495 if (rcu_reader_reg
>= (struct lttng_ust_urcu_reader
*) &chunk
->data
[chunk
->data_len
])
502 /* Called with signals off and mutex locked */
504 void remove_thread(struct lttng_ust_urcu_reader
*rcu_reader_reg
)
506 cleanup_thread(find_chunk(rcu_reader_reg
), rcu_reader_reg
);
507 URCU_TLS(lttng_ust_urcu_reader
) = NULL
;
510 /* Disable signals, take mutex, add to registry */
511 void lttng_ust_urcu_register(void)
513 sigset_t newmask
, oldmask
;
516 ret
= sigfillset(&newmask
);
519 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
524 * Check if a signal concurrently registered our thread since
525 * the check in rcu_read_lock().
527 if (URCU_TLS(lttng_ust_urcu_reader
))
531 * Take care of early registration before lttng_ust_urcu constructor.
533 _lttng_ust_urcu_init();
535 mutex_lock(&rcu_registry_lock
);
537 mutex_unlock(&rcu_registry_lock
);
539 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
544 void lttng_ust_urcu_register_thread(void)
546 if (caa_unlikely(!URCU_TLS(lttng_ust_urcu_reader
)))
547 lttng_ust_urcu_register(); /* If not yet registered. */
550 /* Disable signals, take mutex, remove from registry */
552 void lttng_ust_urcu_unregister(struct lttng_ust_urcu_reader
*rcu_reader_reg
)
554 sigset_t newmask
, oldmask
;
557 ret
= sigfillset(&newmask
);
560 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
564 mutex_lock(&rcu_registry_lock
);
565 remove_thread(rcu_reader_reg
);
566 mutex_unlock(&rcu_registry_lock
);
567 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
570 lttng_ust_urcu_exit();
574 * Remove thread from the registry when it exits, and flag it as
575 * destroyed so garbage collection can take care of it.
578 void lttng_ust_urcu_thread_exit_notifier(void *rcu_key
)
580 lttng_ust_urcu_unregister(rcu_key
);
583 #ifdef CONFIG_RCU_FORCE_SYS_MEMBARRIER
585 void lttng_ust_urcu_sys_membarrier_status(bool available
)
592 void lttng_ust_urcu_sys_membarrier_status(bool available
)
596 lttng_ust_urcu_has_sys_membarrier
= 1;
601 void lttng_ust_urcu_sys_membarrier_init(void)
603 bool available
= false;
606 mask
= membarrier(MEMBARRIER_CMD_QUERY
, 0);
608 if (mask
& MEMBARRIER_CMD_PRIVATE_EXPEDITED
) {
609 if (membarrier(MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED
, 0))
614 lttng_ust_urcu_sys_membarrier_status(available
);
618 void _lttng_ust_urcu_init(void)
620 mutex_lock(&init_lock
);
621 if (!lttng_ust_urcu_refcount
++) {
624 ret
= pthread_key_create(<tng_ust_urcu_key
,
625 lttng_ust_urcu_thread_exit_notifier
);
628 lttng_ust_urcu_sys_membarrier_init();
631 mutex_unlock(&init_lock
);
635 void lttng_ust_urcu_exit(void)
637 mutex_lock(&init_lock
);
638 if (!--lttng_ust_urcu_refcount
) {
639 struct registry_chunk
*chunk
, *tmp
;
642 cds_list_for_each_entry_safe(chunk
, tmp
,
643 ®istry_arena
.chunk_list
, node
) {
644 munmap((void *) chunk
, chunk
->data_len
645 + sizeof(struct registry_chunk
));
647 CDS_INIT_LIST_HEAD(®istry_arena
.chunk_list
);
648 ret
= pthread_key_delete(lttng_ust_urcu_key
);
652 mutex_unlock(&init_lock
);
656 * Holding the rcu_gp_lock and rcu_registry_lock across fork will make
657 * sure we fork() don't race with a concurrent thread executing with
658 * any of those locks held. This ensures that the registry and data
659 * protected by rcu_gp_lock are in a coherent state in the child.
661 void lttng_ust_urcu_before_fork(void)
663 sigset_t newmask
, oldmask
;
666 ret
= sigfillset(&newmask
);
668 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
670 mutex_lock(&rcu_gp_lock
);
671 mutex_lock(&rcu_registry_lock
);
672 saved_fork_signal_mask
= oldmask
;
675 void lttng_ust_urcu_after_fork_parent(void)
680 oldmask
= saved_fork_signal_mask
;
681 mutex_unlock(&rcu_registry_lock
);
682 mutex_unlock(&rcu_gp_lock
);
683 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
688 * Prune all entries from registry except our own thread. Fits the Linux
689 * fork behavior. Called with rcu_gp_lock and rcu_registry_lock held.
692 void lttng_ust_urcu_prune_registry(void)
694 struct registry_chunk
*chunk
;
695 struct lttng_ust_urcu_reader
*rcu_reader_reg
;
697 cds_list_for_each_entry(chunk
, ®istry_arena
.chunk_list
, node
) {
698 for (rcu_reader_reg
= (struct lttng_ust_urcu_reader
*) &chunk
->data
[0];
699 rcu_reader_reg
< (struct lttng_ust_urcu_reader
*) &chunk
->data
[chunk
->data_len
];
701 if (!rcu_reader_reg
->alloc
)
703 if (rcu_reader_reg
->tid
== pthread_self())
705 cleanup_thread(chunk
, rcu_reader_reg
);
710 void lttng_ust_urcu_after_fork_child(void)
715 lttng_ust_urcu_prune_registry();
716 oldmask
= saved_fork_signal_mask
;
717 mutex_unlock(&rcu_registry_lock
);
718 mutex_unlock(&rcu_gp_lock
);
719 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);