src: use SPDX identifiers
[urcu.git] / src / urcu.c
1 // SPDX-FileCopyrightText: 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
2 // SPDX-FileCopyrightText: 2009 Paul E. McKenney, IBM Corporation.
3 //
4 // SPDX-License-Identifier: LGPL-2.1-or-later
5
6 /*
7 * Userspace RCU library
8 *
9 * IBM's contributions to this file may be relicensed under LGPLv2 or later.
10 */
11
12 #define URCU_NO_COMPAT_IDENTIFIERS
13 #define _BSD_SOURCE
14 #define _LGPL_SOURCE
15 #define _DEFAULT_SOURCE
16 #include <stdio.h>
17 #include <pthread.h>
18 #include <signal.h>
19 #include <stdlib.h>
20 #include <stdint.h>
21 #include <string.h>
22 #include <errno.h>
23 #include <stdbool.h>
24 #include <poll.h>
25
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>
34
35 #include "urcu-die.h"
36 #include "urcu-wait.h"
37 #include "urcu-utils.h"
38
39 #define URCU_API_MAP
40 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
41 #undef _LGPL_SOURCE
42 #include <urcu/urcu.h>
43 #define _LGPL_SOURCE
44
45 /*
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.
49 */
50 #define KICK_READER_LOOPS 10
51
52 /*
53 * Active attempts to check for reader Q.S. before calling futex().
54 */
55 #define RCU_QS_ACTIVE_ATTEMPTS 100
56
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__)
60 #else
61 # define membarrier(...) -ENOSYS
62 #endif
63
64 enum membarrier_cmd {
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),
71 };
72
73 #ifdef RCU_MEMBARRIER
74 static int init_done;
75 static int urcu_memb_has_sys_membarrier_private_expedited;
76
77 #ifndef CONFIG_RCU_FORCE_SYS_MEMBARRIER
78 /*
79 * Explicitly initialize to zero because we can't alias a non-static
80 * uninitialized variable.
81 */
82 int urcu_memb_has_sys_membarrier = 0;
83 #endif
84
85 void __attribute__((constructor)) rcu_init(void);
86 #endif
87
88 #ifdef RCU_MB
89 void rcu_init(void)
90 {
91 }
92 #endif
93
94 #ifdef RCU_SIGNAL
95 static int init_done;
96
97 void __attribute__((constructor)) rcu_init(void);
98
99 static DEFINE_URCU_TLS(int, rcu_signal_was_blocked);
100 #endif
101
102 void __attribute__((destructor)) rcu_exit(void);
103 static void urcu_call_rcu_exit(void);
104
105 /*
106 * rcu_gp_lock ensures mutual exclusion between threads calling
107 * synchronize_rcu().
108 */
109 static pthread_mutex_t rcu_gp_lock = PTHREAD_MUTEX_INITIALIZER;
110 /*
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
116 * on the registry.
117 * rcu_registry_lock may nest inside rcu_gp_lock.
118 */
119 static pthread_mutex_t rcu_registry_lock = PTHREAD_MUTEX_INITIALIZER;
120 struct urcu_gp rcu_gp = { .ctr = URCU_GP_COUNT };
121
122 /*
123 * Written to only by each individual reader. Read by both the reader and the
124 * writers.
125 */
126 DEFINE_URCU_TLS(struct urcu_reader, rcu_reader);
127
128 static CDS_LIST_HEAD(registry);
129
130 /*
131 * Queue keeping threads awaiting to wait for a grace period. Contains
132 * struct gp_waiters_thread objects.
133 */
134 static DEFINE_URCU_WAIT_QUEUE(gp_waiters);
135
136 static void mutex_lock(pthread_mutex_t *mutex)
137 {
138 int ret;
139
140 #ifndef DISTRUST_SIGNALS_EXTREME
141 ret = pthread_mutex_lock(mutex);
142 if (ret)
143 urcu_die(ret);
144 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
145 while ((ret = pthread_mutex_trylock(mutex)) != 0) {
146 if (ret != EBUSY && ret != EINTR)
147 urcu_die(ret);
148 if (CMM_LOAD_SHARED(URCU_TLS(rcu_reader).need_mb)) {
149 cmm_smp_mb();
150 _CMM_STORE_SHARED(URCU_TLS(rcu_reader).need_mb, 0);
151 cmm_smp_mb();
152 }
153 (void) poll(NULL, 0, 10);
154 }
155 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
156 }
157
158 static void mutex_unlock(pthread_mutex_t *mutex)
159 {
160 int ret;
161
162 ret = pthread_mutex_unlock(mutex);
163 if (ret)
164 urcu_die(ret);
165 }
166
167 #ifdef RCU_MEMBARRIER
168 static void smp_mb_master(void)
169 {
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))
174 urcu_die(errno);
175 } else {
176 cmm_smp_mb();
177 }
178 }
179 #endif
180
181 #ifdef RCU_MB
182 static void smp_mb_master(void)
183 {
184 cmm_smp_mb();
185 }
186 #endif
187
188 #ifdef RCU_SIGNAL
189 static void force_mb_all_readers(void)
190 {
191 struct urcu_reader *index;
192
193 /*
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.
196 */
197 if (cds_list_empty(&registry))
198 return;
199 /*
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.
204 */
205 cds_list_for_each_entry(index, &registry, node) {
206 CMM_STORE_SHARED(index->need_mb, 1);
207 pthread_kill(index->tid, SIGRCU);
208 }
209 /*
210 * Wait for sighandler (and thus mb()) to execute on every thread.
211 *
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.
216 *
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).
221 */
222 cds_list_for_each_entry(index, &registry, node) {
223 while (CMM_LOAD_SHARED(index->need_mb)) {
224 pthread_kill(index->tid, SIGRCU);
225 (void) poll(NULL, 0, 1);
226 }
227 }
228 cmm_smp_mb(); /* read ->need_mb before ending the barrier */
229 }
230
231 static void smp_mb_master(void)
232 {
233 force_mb_all_readers();
234 }
235 #endif /* #ifdef RCU_SIGNAL */
236
237 /*
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.
241 */
242 static void wait_gp(void)
243 {
244 /*
245 * Read reader_gp before read futex. smp_mb_master() needs to
246 * be called with the rcu registry lock held in RCU_SIGNAL
247 * flavor.
248 */
249 smp_mb_master();
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)) {
254 /*
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
260 * differs from -1.
261 */
262 continue;
263 }
264 switch (errno) {
265 case EAGAIN:
266 /* Value already changed. */
267 goto end;
268 case EINTR:
269 /* Retry if interrupted by signal. */
270 break; /* Get out of switch. Check again. */
271 default:
272 /* Unexpected error. */
273 urcu_die(errno);
274 }
275 }
276 end:
277 /*
278 * Re-lock the registry lock before the next loop.
279 */
280 mutex_lock(&rcu_registry_lock);
281 }
282
283 /*
284 * Always called with rcu_registry lock held. Releases this lock between
285 * iterations and grabs it again. Holds the lock when it returns.
286 */
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)
290 {
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 */
296
297 /*
298 * Wait for each thread URCU_TLS(rcu_reader).ctr to either
299 * indicate quiescence (not nested), or observe the current
300 * rcu_gp.ctr value.
301 */
302 for (;;) {
303 if (wait_loops < RCU_QS_ACTIVE_ATTEMPTS)
304 wait_loops++;
305 if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) {
306 uatomic_dec(&rcu_gp.futex);
307 /* Write futex before read reader_gp */
308 smp_mb_master();
309 }
310
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,
316 cur_snap_readers);
317 break;
318 }
319 /* Fall-through */
320 case URCU_READER_INACTIVE:
321 cds_list_move(&index->node, qsreaders);
322 break;
323 case URCU_READER_ACTIVE_OLD:
324 /*
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.
329 */
330 break;
331 }
332 }
333
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 */
338 smp_mb_master();
339 uatomic_set(&rcu_gp.futex, 0);
340 }
341 break;
342 } else {
343 if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) {
344 /* wait_gp unlocks/locks registry lock. */
345 wait_gp();
346 } else {
347 /* Temporarily unlock the registry lock. */
348 mutex_unlock(&rcu_registry_lock);
349 caa_cpu_relax();
350 /*
351 * Re-lock the registry lock before the
352 * next loop.
353 */
354 mutex_lock(&rcu_registry_lock);
355 }
356 }
357 #else /* #ifndef HAS_INCOHERENT_CACHES */
358 /*
359 * BUSY-LOOP. Force the reader thread to commit its
360 * URCU_TLS(rcu_reader).ctr update to memory if we wait
361 * for too long.
362 */
363 if (cds_list_empty(input_readers)) {
364 if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) {
365 /* Read reader_gp before write futex */
366 smp_mb_master();
367 uatomic_set(&rcu_gp.futex, 0);
368 }
369 break;
370 } else {
371 if (wait_gp_loops == KICK_READER_LOOPS) {
372 smp_mb_master();
373 wait_gp_loops = 0;
374 }
375 if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) {
376 /* wait_gp unlocks/locks registry lock. */
377 wait_gp();
378 wait_gp_loops++;
379 } else {
380 /* Temporarily unlock the registry lock. */
381 mutex_unlock(&rcu_registry_lock);
382 caa_cpu_relax();
383 /*
384 * Re-lock the registry lock before the
385 * next loop.
386 */
387 mutex_lock(&rcu_registry_lock);
388 }
389 }
390 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
391 }
392 }
393
394 void synchronize_rcu(void)
395 {
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;
400
401 /*
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.
408 */
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. */
413 cmm_smp_mb();
414 return;
415 }
416 /* We won't need to wake ourself up */
417 urcu_wait_set_state(&wait, URCU_WAIT_RUNNING);
418
419 mutex_lock(&rcu_gp_lock);
420
421 /*
422 * Move all waiters into our local queue.
423 */
424 urcu_move_waiters(&waiters, &gp_waiters);
425
426 mutex_lock(&rcu_registry_lock);
427
428 if (cds_list_empty(&registry))
429 goto out;
430
431 /*
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.
435 */
436 /* Write new ptr before changing the qparity */
437 smp_mb_master();
438
439 /*
440 * Wait for readers to observe original parity or be quiescent.
441 * wait_for_readers() can release and grab again rcu_registry_lock
442 * internally.
443 */
444 wait_for_readers(&registry, &cur_snap_readers, &qsreaders);
445
446 /*
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.
452 */
453 cmm_barrier();
454
455 /*
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.
459 */
460 cmm_smp_mb();
461
462 /* Switch parity: 0 -> 1, 1 -> 0 */
463 CMM_STORE_SHARED(rcu_gp.ctr, rcu_gp.ctr ^ URCU_GP_CTR_PHASE);
464
465 /*
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.
470 */
471 cmm_barrier();
472
473 /*
474 *
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.
478 */
479 cmm_smp_mb();
480
481 /*
482 * Wait for readers to observe new parity or be quiescent.
483 * wait_for_readers() can release and grab again rcu_registry_lock
484 * internally.
485 */
486 wait_for_readers(&cur_snap_readers, NULL, &qsreaders);
487
488 /*
489 * Put quiescent reader list back into registry.
490 */
491 cds_list_splice(&qsreaders, &registry);
492
493 /*
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.
497 */
498 smp_mb_master();
499 out:
500 mutex_unlock(&rcu_registry_lock);
501 mutex_unlock(&rcu_gp_lock);
502
503 /*
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.
507 */
508 urcu_wake_all_waiters(&waiters);
509 }
510
511 /*
512 * library wrappers to be used by non-LGPL compatible source code.
513 */
514
515 void rcu_read_lock(void)
516 {
517 _rcu_read_lock();
518 }
519
520 void rcu_read_unlock(void)
521 {
522 _rcu_read_unlock();
523 }
524
525 int rcu_read_ongoing(void)
526 {
527 return _rcu_read_ongoing();
528 }
529
530 #ifdef RCU_SIGNAL
531 /*
532 * Make sure the signal used by the urcu-signal flavor is unblocked
533 * while the thread is registered.
534 */
535 static
536 void urcu_signal_unblock(void)
537 {
538 sigset_t mask, oldmask;
539 int ret;
540
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);
548 }
549
550 static
551 void urcu_signal_restore(void)
552 {
553 sigset_t mask;
554 int ret;
555
556 if (!URCU_TLS(rcu_signal_was_blocked))
557 return;
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);
564 }
565 #else
566 static
567 void urcu_signal_unblock(void) { }
568 static
569 void urcu_signal_restore(void) { }
570 #endif
571
572 void rcu_register_thread(void)
573 {
574 urcu_signal_unblock();
575
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));
579
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, &registry);
585 mutex_unlock(&rcu_registry_lock);
586 }
587
588 void rcu_unregister_thread(void)
589 {
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);
595
596 urcu_signal_restore();
597 }
598
599 #ifdef RCU_MEMBARRIER
600
601 #ifdef CONFIG_RCU_FORCE_SYS_MEMBARRIER
602 static
603 void rcu_sys_membarrier_status(bool available)
604 {
605 if (!available)
606 abort();
607 }
608 #else
609 static
610 void rcu_sys_membarrier_status(bool available)
611 {
612 if (!available)
613 return;
614 urcu_memb_has_sys_membarrier = 1;
615 }
616 #endif
617
618 static
619 void rcu_sys_membarrier_init(void)
620 {
621 bool available = false;
622 int mask;
623
624 mask = membarrier(MEMBARRIER_CMD_QUERY, 0);
625 if (mask >= 0) {
626 if (mask & MEMBARRIER_CMD_PRIVATE_EXPEDITED) {
627 if (membarrier(MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED, 0))
628 urcu_die(errno);
629 urcu_memb_has_sys_membarrier_private_expedited = 1;
630 available = true;
631 } else if (mask & MEMBARRIER_CMD_SHARED) {
632 available = true;
633 }
634 }
635 rcu_sys_membarrier_status(available);
636 }
637
638 void rcu_init(void)
639 {
640 if (init_done)
641 return;
642 init_done = 1;
643 rcu_sys_membarrier_init();
644 }
645 #endif
646
647 #ifdef RCU_SIGNAL
648 static void sigrcu_handler(int signo __attribute__((unused)),
649 siginfo_t *siginfo __attribute__((unused)),
650 void *context __attribute__((unused)))
651 {
652 /*
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
655 * executed on.
656 */
657 cmm_smp_mb();
658 _CMM_STORE_SHARED(URCU_TLS(rcu_reader).need_mb, 0);
659 cmm_smp_mb();
660 }
661
662 /*
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.
669 */
670 void rcu_init(void)
671 {
672 struct sigaction act;
673 int ret;
674
675 if (init_done)
676 return;
677 init_done = 1;
678
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);
683 if (ret)
684 urcu_die(errno);
685 }
686
687 /*
688 * Don't unregister the SIGRCU signal handler anymore, because
689 * call_rcu threads could still be using it shortly before the
690 * application exits.
691 * Assertion disabled because call_rcu threads are now rcu
692 * readers, and left running at exit.
693 * urcu_posix_assert(cds_list_empty(&registry));
694 */
695
696 #endif /* #ifdef RCU_SIGNAL */
697
698 void rcu_exit(void)
699 {
700 urcu_call_rcu_exit();
701 }
702
703 DEFINE_RCU_FLAVOR(rcu_flavor);
704
705 #include "urcu-call-rcu-impl.h"
706 #include "urcu-defer-impl.h"
707 #include "urcu-poll-impl.h"
This page took 0.042343 seconds and 5 git commands to generate.