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 lttng_ust_urcu_exit(void)
100 __attribute__((destructor
));
102 #ifndef CONFIG_RCU_FORCE_SYS_MEMBARRIER
103 int lttng_ust_urcu_has_sys_membarrier
;
107 * rcu_gp_lock ensures mutual exclusion between threads calling
110 static pthread_mutex_t rcu_gp_lock
= PTHREAD_MUTEX_INITIALIZER
;
112 * rcu_registry_lock ensures mutual exclusion between threads
113 * registering and unregistering themselves to/from the registry, and
114 * with threads reading that registry from synchronize_rcu(). However,
115 * this lock is not held all the way through the completion of awaiting
116 * for the grace period. It is sporadically released between iterations
118 * rcu_registry_lock may nest inside rcu_gp_lock.
120 static pthread_mutex_t rcu_registry_lock
= PTHREAD_MUTEX_INITIALIZER
;
122 static pthread_mutex_t init_lock
= PTHREAD_MUTEX_INITIALIZER
;
123 static int initialized
;
125 static pthread_key_t lttng_ust_urcu_key
;
127 struct lttng_ust_urcu_gp lttng_ust_urcu_gp
= { .ctr
= LTTNG_UST_URCU_GP_COUNT
};
130 * Pointer to registry elements. Written to only by each individual reader. Read
131 * by both the reader and the writers.
133 DEFINE_URCU_TLS(struct lttng_ust_urcu_reader
*, lttng_ust_urcu_reader
);
135 static CDS_LIST_HEAD(registry
);
137 struct registry_chunk
{
138 size_t data_len
; /* data length */
139 size_t used
; /* amount of data used */
140 struct cds_list_head node
; /* chunk_list node */
144 struct registry_arena
{
145 struct cds_list_head chunk_list
;
148 static struct registry_arena registry_arena
= {
149 .chunk_list
= CDS_LIST_HEAD_INIT(registry_arena
.chunk_list
),
152 /* Saved fork signal mask, protected by rcu_gp_lock */
153 static sigset_t saved_fork_signal_mask
;
155 static void mutex_lock(pthread_mutex_t
*mutex
)
159 #ifndef DISTRUST_SIGNALS_EXTREME
160 ret
= pthread_mutex_lock(mutex
);
163 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
164 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
165 if (ret
!= EBUSY
&& ret
!= EINTR
)
169 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
172 static void mutex_unlock(pthread_mutex_t
*mutex
)
176 ret
= pthread_mutex_unlock(mutex
);
181 static void smp_mb_master(void)
183 if (caa_likely(lttng_ust_urcu_has_sys_membarrier
)) {
184 if (membarrier(MEMBARRIER_CMD_PRIVATE_EXPEDITED
, 0))
192 * Always called with rcu_registry lock held. Releases this lock between
193 * iterations and grabs it again. Holds the lock when it returns.
195 static void wait_for_readers(struct cds_list_head
*input_readers
,
196 struct cds_list_head
*cur_snap_readers
,
197 struct cds_list_head
*qsreaders
)
199 unsigned int wait_loops
= 0;
200 struct lttng_ust_urcu_reader
*index
, *tmp
;
203 * Wait for each thread URCU_TLS(lttng_ust_urcu_reader).ctr to either
204 * indicate quiescence (not nested), or observe the current
208 if (wait_loops
< RCU_QS_ACTIVE_ATTEMPTS
)
211 cds_list_for_each_entry_safe(index
, tmp
, input_readers
, node
) {
212 switch (lttng_ust_urcu_reader_state(&index
->ctr
)) {
213 case LTTNG_UST_URCU_READER_ACTIVE_CURRENT
:
214 if (cur_snap_readers
) {
215 cds_list_move(&index
->node
,
220 case LTTNG_UST_URCU_READER_INACTIVE
:
221 cds_list_move(&index
->node
, qsreaders
);
223 case LTTNG_UST_URCU_READER_ACTIVE_OLD
:
225 * Old snapshot. Leaving node in
226 * input_readers will make us busy-loop
227 * until the snapshot becomes current or
228 * the reader becomes inactive.
234 if (cds_list_empty(input_readers
)) {
237 /* Temporarily unlock the registry lock. */
238 mutex_unlock(&rcu_registry_lock
);
239 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
)
240 (void) poll(NULL
, 0, RCU_SLEEP_DELAY_MS
);
243 /* Re-lock the registry lock before the next loop. */
244 mutex_lock(&rcu_registry_lock
);
249 void lttng_ust_urcu_synchronize_rcu(void)
251 CDS_LIST_HEAD(cur_snap_readers
);
252 CDS_LIST_HEAD(qsreaders
);
253 sigset_t newmask
, oldmask
;
256 ret
= sigfillset(&newmask
);
258 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
261 mutex_lock(&rcu_gp_lock
);
263 mutex_lock(&rcu_registry_lock
);
265 if (cds_list_empty(®istry
))
268 /* All threads should read qparity before accessing data structure
269 * where new ptr points to. */
270 /* Write new ptr before changing the qparity */
274 * Wait for readers to observe original parity or be quiescent.
275 * wait_for_readers() can release and grab again rcu_registry_lock
278 wait_for_readers(®istry
, &cur_snap_readers
, &qsreaders
);
281 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
282 * model easier to understand. It does not have a big performance impact
283 * anyway, given this is the write-side.
287 /* Switch parity: 0 -> 1, 1 -> 0 */
288 CMM_STORE_SHARED(lttng_ust_urcu_gp
.ctr
, lttng_ust_urcu_gp
.ctr
^ LTTNG_UST_URCU_GP_CTR_PHASE
);
291 * Must commit qparity update to memory before waiting for other parity
292 * quiescent state. Failure to do so could result in the writer waiting
293 * forever while new readers are always accessing data (no progress).
294 * Ensured by CMM_STORE_SHARED and CMM_LOAD_SHARED.
298 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
299 * model easier to understand. It does not have a big performance impact
300 * anyway, given this is the write-side.
305 * Wait for readers to observe new parity or be quiescent.
306 * wait_for_readers() can release and grab again rcu_registry_lock
309 wait_for_readers(&cur_snap_readers
, NULL
, &qsreaders
);
312 * Put quiescent reader list back into registry.
314 cds_list_splice(&qsreaders
, ®istry
);
317 * Finish waiting for reader threads before letting the old ptr being
322 mutex_unlock(&rcu_registry_lock
);
323 mutex_unlock(&rcu_gp_lock
);
324 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
329 * library wrappers to be used by non-LGPL compatible source code.
332 void lttng_ust_urcu_read_lock(void)
334 _lttng_ust_urcu_read_lock();
337 void lttng_ust_urcu_read_unlock(void)
339 _lttng_ust_urcu_read_unlock();
342 int lttng_ust_urcu_read_ongoing(void)
344 return _lttng_ust_urcu_read_ongoing();
348 * Only grow for now. If empty, allocate a ARENA_INIT_ALLOC sized chunk.
349 * Else, try expanding the last chunk. If this fails, allocate a new
350 * chunk twice as big as the last chunk.
351 * Memory used by chunks _never_ moves. A chunk could theoretically be
352 * freed when all "used" slots are released, but we don't do it at this
356 void expand_arena(struct registry_arena
*arena
)
358 struct registry_chunk
*new_chunk
, *last_chunk
;
359 size_t old_chunk_len
, new_chunk_len
;
362 if (cds_list_empty(&arena
->chunk_list
)) {
363 assert(ARENA_INIT_ALLOC
>=
364 sizeof(struct registry_chunk
)
365 + sizeof(struct lttng_ust_urcu_reader
));
366 new_chunk_len
= ARENA_INIT_ALLOC
;
367 new_chunk
= (struct registry_chunk
*) mmap(NULL
,
369 PROT_READ
| PROT_WRITE
,
370 MAP_ANONYMOUS
| MAP_PRIVATE
,
372 if (new_chunk
== MAP_FAILED
)
374 memset(new_chunk
, 0, new_chunk_len
);
375 new_chunk
->data_len
=
376 new_chunk_len
- sizeof(struct registry_chunk
);
377 cds_list_add_tail(&new_chunk
->node
, &arena
->chunk_list
);
378 return; /* We're done. */
381 /* Try expanding last chunk. */
382 last_chunk
= cds_list_entry(arena
->chunk_list
.prev
,
383 struct registry_chunk
, node
);
385 last_chunk
->data_len
+ sizeof(struct registry_chunk
);
386 new_chunk_len
= old_chunk_len
<< 1;
388 /* Don't allow memory mapping to move, just expand. */
389 new_chunk
= mremap_wrapper(last_chunk
, old_chunk_len
,
391 if (new_chunk
!= MAP_FAILED
) {
392 /* Should not have moved. */
393 assert(new_chunk
== last_chunk
);
394 memset((char *) last_chunk
+ old_chunk_len
, 0,
395 new_chunk_len
- old_chunk_len
);
396 last_chunk
->data_len
=
397 new_chunk_len
- sizeof(struct registry_chunk
);
398 return; /* We're done. */
401 /* Remap did not succeed, we need to add a new chunk. */
402 new_chunk
= (struct registry_chunk
*) mmap(NULL
,
404 PROT_READ
| PROT_WRITE
,
405 MAP_ANONYMOUS
| MAP_PRIVATE
,
407 if (new_chunk
== MAP_FAILED
)
409 memset(new_chunk
, 0, new_chunk_len
);
410 new_chunk
->data_len
=
411 new_chunk_len
- sizeof(struct registry_chunk
);
412 cds_list_add_tail(&new_chunk
->node
, &arena
->chunk_list
);
416 struct lttng_ust_urcu_reader
*arena_alloc(struct registry_arena
*arena
)
418 struct registry_chunk
*chunk
;
419 struct lttng_ust_urcu_reader
*rcu_reader_reg
;
420 int expand_done
= 0; /* Only allow to expand once per alloc */
421 size_t len
= sizeof(struct lttng_ust_urcu_reader
);
424 cds_list_for_each_entry(chunk
, &arena
->chunk_list
, node
) {
425 if (chunk
->data_len
- chunk
->used
< len
)
428 for (rcu_reader_reg
= (struct lttng_ust_urcu_reader
*) &chunk
->data
[0];
429 rcu_reader_reg
< (struct lttng_ust_urcu_reader
*) &chunk
->data
[chunk
->data_len
];
431 if (!rcu_reader_reg
->alloc
) {
432 rcu_reader_reg
->alloc
= 1;
434 return rcu_reader_reg
;
448 /* Called with signals off and mutex locked */
450 void add_thread(void)
452 struct lttng_ust_urcu_reader
*rcu_reader_reg
;
455 rcu_reader_reg
= arena_alloc(®istry_arena
);
458 ret
= pthread_setspecific(lttng_ust_urcu_key
, rcu_reader_reg
);
462 /* Add to registry */
463 rcu_reader_reg
->tid
= pthread_self();
464 assert(rcu_reader_reg
->ctr
== 0);
465 cds_list_add(&rcu_reader_reg
->node
, ®istry
);
467 * Reader threads are pointing to the reader registry. This is
468 * why its memory should never be relocated.
470 URCU_TLS(lttng_ust_urcu_reader
) = rcu_reader_reg
;
473 /* Called with mutex locked */
475 void cleanup_thread(struct registry_chunk
*chunk
,
476 struct lttng_ust_urcu_reader
*rcu_reader_reg
)
478 rcu_reader_reg
->ctr
= 0;
479 cds_list_del(&rcu_reader_reg
->node
);
480 rcu_reader_reg
->tid
= 0;
481 rcu_reader_reg
->alloc
= 0;
482 chunk
->used
-= sizeof(struct lttng_ust_urcu_reader
);
486 struct registry_chunk
*find_chunk(struct lttng_ust_urcu_reader
*rcu_reader_reg
)
488 struct registry_chunk
*chunk
;
490 cds_list_for_each_entry(chunk
, ®istry_arena
.chunk_list
, node
) {
491 if (rcu_reader_reg
< (struct lttng_ust_urcu_reader
*) &chunk
->data
[0])
493 if (rcu_reader_reg
>= (struct lttng_ust_urcu_reader
*) &chunk
->data
[chunk
->data_len
])
500 /* Called with signals off and mutex locked */
502 void remove_thread(struct lttng_ust_urcu_reader
*rcu_reader_reg
)
504 cleanup_thread(find_chunk(rcu_reader_reg
), rcu_reader_reg
);
505 URCU_TLS(lttng_ust_urcu_reader
) = NULL
;
508 /* Disable signals, take mutex, add to registry */
509 void lttng_ust_urcu_register(void)
511 sigset_t newmask
, oldmask
;
514 ret
= sigfillset(&newmask
);
517 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
522 * Check if a signal concurrently registered our thread since
523 * the check in rcu_read_lock().
525 if (URCU_TLS(lttng_ust_urcu_reader
))
529 * Take care of early registration before lttng_ust_urcu constructor.
531 _lttng_ust_urcu_init();
533 mutex_lock(&rcu_registry_lock
);
535 mutex_unlock(&rcu_registry_lock
);
537 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
542 void lttng_ust_urcu_register_thread(void)
544 if (caa_unlikely(!URCU_TLS(lttng_ust_urcu_reader
)))
545 lttng_ust_urcu_register(); /* If not yet registered. */
548 /* Disable signals, take mutex, remove from registry */
550 void lttng_ust_urcu_unregister(struct lttng_ust_urcu_reader
*rcu_reader_reg
)
552 sigset_t newmask
, oldmask
;
555 ret
= sigfillset(&newmask
);
558 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
562 mutex_lock(&rcu_registry_lock
);
563 remove_thread(rcu_reader_reg
);
564 mutex_unlock(&rcu_registry_lock
);
565 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
568 lttng_ust_urcu_exit();
572 * Remove thread from the registry when it exits, and flag it as
573 * destroyed so garbage collection can take care of it.
576 void lttng_ust_urcu_thread_exit_notifier(void *rcu_key
)
578 lttng_ust_urcu_unregister(rcu_key
);
581 #ifdef CONFIG_RCU_FORCE_SYS_MEMBARRIER
583 void lttng_ust_urcu_sys_membarrier_status(bool available
)
590 void lttng_ust_urcu_sys_membarrier_status(bool available
)
594 lttng_ust_urcu_has_sys_membarrier
= 1;
599 void lttng_ust_urcu_sys_membarrier_init(void)
601 bool available
= false;
604 mask
= membarrier(MEMBARRIER_CMD_QUERY
, 0);
606 if (mask
& MEMBARRIER_CMD_PRIVATE_EXPEDITED
) {
607 if (membarrier(MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED
, 0))
612 lttng_ust_urcu_sys_membarrier_status(available
);
616 void _lttng_ust_urcu_init(void)
618 mutex_lock(&init_lock
);
619 if (!lttng_ust_urcu_refcount
++) {
622 ret
= pthread_key_create(<tng_ust_urcu_key
,
623 lttng_ust_urcu_thread_exit_notifier
);
626 lttng_ust_urcu_sys_membarrier_init();
629 mutex_unlock(&init_lock
);
633 void lttng_ust_urcu_exit(void)
635 mutex_lock(&init_lock
);
636 if (!--lttng_ust_urcu_refcount
) {
637 struct registry_chunk
*chunk
, *tmp
;
640 cds_list_for_each_entry_safe(chunk
, tmp
,
641 ®istry_arena
.chunk_list
, node
) {
642 munmap((void *) chunk
, chunk
->data_len
643 + sizeof(struct registry_chunk
));
645 CDS_INIT_LIST_HEAD(®istry_arena
.chunk_list
);
646 ret
= pthread_key_delete(lttng_ust_urcu_key
);
650 mutex_unlock(&init_lock
);
654 * Holding the rcu_gp_lock and rcu_registry_lock across fork will make
655 * sure we fork() don't race with a concurrent thread executing with
656 * any of those locks held. This ensures that the registry and data
657 * protected by rcu_gp_lock are in a coherent state in the child.
659 void lttng_ust_urcu_before_fork(void)
661 sigset_t newmask
, oldmask
;
664 ret
= sigfillset(&newmask
);
666 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
668 mutex_lock(&rcu_gp_lock
);
669 mutex_lock(&rcu_registry_lock
);
670 saved_fork_signal_mask
= oldmask
;
673 void lttng_ust_urcu_after_fork_parent(void)
678 oldmask
= saved_fork_signal_mask
;
679 mutex_unlock(&rcu_registry_lock
);
680 mutex_unlock(&rcu_gp_lock
);
681 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
686 * Prune all entries from registry except our own thread. Fits the Linux
687 * fork behavior. Called with rcu_gp_lock and rcu_registry_lock held.
690 void lttng_ust_urcu_prune_registry(void)
692 struct registry_chunk
*chunk
;
693 struct lttng_ust_urcu_reader
*rcu_reader_reg
;
695 cds_list_for_each_entry(chunk
, ®istry_arena
.chunk_list
, node
) {
696 for (rcu_reader_reg
= (struct lttng_ust_urcu_reader
*) &chunk
->data
[0];
697 rcu_reader_reg
< (struct lttng_ust_urcu_reader
*) &chunk
->data
[chunk
->data_len
];
699 if (!rcu_reader_reg
->alloc
)
701 if (rcu_reader_reg
->tid
== pthread_self())
703 cleanup_thread(chunk
, rcu_reader_reg
);
708 void lttng_ust_urcu_after_fork_child(void)
713 lttng_ust_urcu_prune_registry();
714 oldmask
= saved_fork_signal_mask
;
715 mutex_unlock(&rcu_registry_lock
);
716 mutex_unlock(&rcu_gp_lock
);
717 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);