X-Git-Url: https://git.lttng.org/?a=blobdiff_plain;f=src%2Furcu.c;h=76c8720c288b5d5ce52eacdd58714126513645c4;hb=601922a81d884e16ff404cee7534ede56fb87d0a;hp=c47f51b53330322468cd5a6b870eab612907fb68;hpb=c0bb9f693f926595a7cb8b4ce712cef08d9f5d49;p=userspace-rcu.git diff --git a/src/urcu.c b/src/urcu.c index c47f51b..76c8720 100644 --- a/src/urcu.c +++ b/src/urcu.c @@ -1,35 +1,21 @@ +// SPDX-FileCopyrightText: 2009 Mathieu Desnoyers +// SPDX-FileCopyrightText: 2009 Paul E. McKenney, IBM Corporation. +// +// SPDX-License-Identifier: LGPL-2.1-or-later + /* - * urcu.c - * * Userspace RCU library * - * Copyright (c) 2009 Mathieu Desnoyers - * Copyright (c) 2009 Paul E. McKenney, IBM Corporation. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - * * IBM's contributions to this file may be relicensed under LGPLv2 or later. */ +#define URCU_NO_COMPAT_IDENTIFIERS #define _BSD_SOURCE #define _LGPL_SOURCE #define _DEFAULT_SOURCE #include #include #include -#include #include #include #include @@ -37,19 +23,24 @@ #include #include -#include "urcu/arch.h" -#include "urcu/wfcqueue.h" -#include "urcu/map/urcu.h" -#include "urcu/static/urcu.h" -#include "urcu-pointer.h" -#include "urcu/tls-compat.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include #include "urcu-die.h" #include "urcu-wait.h" +#include "urcu-utils.h" +#define URCU_API_MAP /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */ #undef _LGPL_SOURCE -#include "urcu.h" +#include #define _LGPL_SOURCE /* @@ -82,10 +73,14 @@ enum membarrier_cmd { #ifdef RCU_MEMBARRIER static int init_done; -static int has_sys_membarrier_private_expedited; +static int urcu_memb_has_sys_membarrier_private_expedited; #ifndef CONFIG_RCU_FORCE_SYS_MEMBARRIER -int rcu_has_sys_membarrier_memb; +/* + * Explicitly initialize to zero because we can't alias a non-static + * uninitialized variable. + */ +int urcu_memb_has_sys_membarrier = 0; #endif void __attribute__((constructor)) rcu_init(void); @@ -101,9 +96,13 @@ void rcu_init(void) static int init_done; void __attribute__((constructor)) rcu_init(void); -void __attribute__((destructor)) rcu_exit(void); + +static DEFINE_URCU_TLS(int, rcu_signal_was_blocked); #endif +void __attribute__((destructor)) rcu_exit(void); +static void urcu_call_rcu_exit(void); + /* * rcu_gp_lock ensures mutual exclusion between threads calling * synchronize_rcu(). @@ -119,13 +118,13 @@ static pthread_mutex_t rcu_gp_lock = PTHREAD_MUTEX_INITIALIZER; * rcu_registry_lock may nest inside rcu_gp_lock. */ static pthread_mutex_t rcu_registry_lock = PTHREAD_MUTEX_INITIALIZER; -struct rcu_gp rcu_gp = { .ctr = RCU_GP_COUNT }; +struct urcu_gp rcu_gp = { .ctr = URCU_GP_COUNT }; /* * Written to only by each individual reader. Read by both the reader and the * writers. */ -DEFINE_URCU_TLS(struct rcu_reader, rcu_reader); +DEFINE_URCU_TLS(struct urcu_reader, rcu_reader); static CDS_LIST_HEAD(registry); @@ -169,8 +168,8 @@ static void mutex_unlock(pthread_mutex_t *mutex) #ifdef RCU_MEMBARRIER static void smp_mb_master(void) { - if (caa_likely(rcu_has_sys_membarrier_memb)) { - if (membarrier(has_sys_membarrier_private_expedited ? + if (caa_likely(urcu_memb_has_sys_membarrier)) { + if (membarrier(urcu_memb_has_sys_membarrier_private_expedited ? MEMBARRIER_CMD_PRIVATE_EXPEDITED : MEMBARRIER_CMD_SHARED, 0)) urcu_die(errno); @@ -190,7 +189,7 @@ static void smp_mb_master(void) #ifdef RCU_SIGNAL static void force_mb_all_readers(void) { - struct rcu_reader *index; + struct urcu_reader *index; /* * Ask for each threads to execute a cmm_smp_mb() so we can consider the @@ -251,17 +250,25 @@ static void wait_gp(void) smp_mb_master(); /* Temporarily unlock the registry lock. */ mutex_unlock(&rcu_registry_lock); - if (uatomic_read(&rcu_gp.futex) != -1) - goto end; - while (futex_async(&rcu_gp.futex, FUTEX_WAIT, -1, - NULL, NULL, 0)) { + while (uatomic_read(&rcu_gp.futex) == -1) { + if (!futex_async(&rcu_gp.futex, FUTEX_WAIT, -1, NULL, NULL, 0)) { + /* + * Prior queued wakeups queued by unrelated code + * using the same address can cause futex wait to + * return 0 even through the futex value is still + * -1 (spurious wakeups). Check the value again + * in user-space to validate whether it really + * differs from -1. + */ + continue; + } switch (errno) { - case EWOULDBLOCK: + case EAGAIN: /* Value already changed. */ goto end; case EINTR: /* Retry if interrupted by signal. */ - break; /* Get out of switch. */ + break; /* Get out of switch. Check again. */ default: /* Unexpected error. */ urcu_die(errno); @@ -280,10 +287,11 @@ end: */ static void wait_for_readers(struct cds_list_head *input_readers, struct cds_list_head *cur_snap_readers, - struct cds_list_head *qsreaders) + struct cds_list_head *qsreaders, + cmm_annotate_t *group) { unsigned int wait_loops = 0; - struct rcu_reader *index, *tmp; + struct urcu_reader *index, *tmp; #ifdef HAS_INCOHERENT_CACHES unsigned int wait_gp_loops = 0; #endif /* HAS_INCOHERENT_CACHES */ @@ -303,18 +311,18 @@ static void wait_for_readers(struct cds_list_head *input_readers, } cds_list_for_each_entry_safe(index, tmp, input_readers, node) { - switch (rcu_reader_state(&index->ctr)) { - case RCU_READER_ACTIVE_CURRENT: + switch (urcu_common_reader_state(&rcu_gp, &index->ctr, group)) { + case URCU_READER_ACTIVE_CURRENT: if (cur_snap_readers) { cds_list_move(&index->node, cur_snap_readers); break; } /* Fall-through */ - case RCU_READER_INACTIVE: + case URCU_READER_INACTIVE: cds_list_move(&index->node, qsreaders); break; - case RCU_READER_ACTIVE_OLD: + case URCU_READER_ACTIVE_OLD: /* * Old snapshot. Leaving node in * input_readers will make us busy-loop @@ -387,6 +395,8 @@ static void wait_for_readers(struct cds_list_head *input_readers, void synchronize_rcu(void) { + cmm_annotate_define(acquire_group); + cmm_annotate_define(release_group); CDS_LIST_HEAD(cur_snap_readers); CDS_LIST_HEAD(qsreaders); DEFINE_URCU_WAIT_NODE(wait, URCU_WAIT_WAITING); @@ -401,10 +411,11 @@ void synchronize_rcu(void) * queue before their insertion into the wait queue. */ if (urcu_wait_add(&gp_waiters, &wait) != 0) { - /* Not first in queue: will be awakened by another thread. */ + /* + * Not first in queue: will be awakened by another thread. + * Implies a memory barrier after grace period. + */ urcu_adaptative_busy_wait(&wait); - /* Order following memory accesses after grace period. */ - cmm_smp_mb(); return; } /* We won't need to wake ourself up */ @@ -429,13 +440,14 @@ void synchronize_rcu(void) */ /* Write new ptr before changing the qparity */ smp_mb_master(); + cmm_annotate_group_mb_release(&release_group); /* * Wait for readers to observe original parity or be quiescent. * wait_for_readers() can release and grab again rcu_registry_lock - * interally. + * internally. */ - wait_for_readers(®istry, &cur_snap_readers, &qsreaders); + wait_for_readers(®istry, &cur_snap_readers, &qsreaders, &acquire_group); /* * Must finish waiting for quiescent state for original parity before @@ -454,7 +466,8 @@ void synchronize_rcu(void) cmm_smp_mb(); /* Switch parity: 0 -> 1, 1 -> 0 */ - CMM_STORE_SHARED(rcu_gp.ctr, rcu_gp.ctr ^ RCU_GP_CTR_PHASE); + cmm_annotate_group_mem_release(&release_group, &rcu_gp.ctr); + uatomic_store(&rcu_gp.ctr, rcu_gp.ctr ^ URCU_GP_CTR_PHASE, CMM_RELAXED); /* * Must commit rcu_gp.ctr update to memory before waiting for quiescent @@ -475,9 +488,9 @@ void synchronize_rcu(void) /* * Wait for readers to observe new parity or be quiescent. * wait_for_readers() can release and grab again rcu_registry_lock - * interally. + * internally. */ - wait_for_readers(&cur_snap_readers, NULL, &qsreaders); + wait_for_readers(&cur_snap_readers, NULL, &qsreaders, &acquire_group); /* * Put quiescent reader list back into registry. @@ -490,6 +503,7 @@ void synchronize_rcu(void) * iterates on reader threads. */ smp_mb_master(); + cmm_annotate_group_mb_acquire(&acquire_group); out: mutex_unlock(&rcu_registry_lock); mutex_unlock(&rcu_gp_lock); @@ -521,14 +535,58 @@ int rcu_read_ongoing(void) return _rcu_read_ongoing(); } +#ifdef RCU_SIGNAL +/* + * Make sure the signal used by the urcu-signal flavor is unblocked + * while the thread is registered. + */ +static +void urcu_signal_unblock(void) +{ + sigset_t mask, oldmask; + int ret; + + ret = sigemptyset(&mask); + urcu_posix_assert(!ret); + ret = sigaddset(&mask, SIGRCU); + urcu_posix_assert(!ret); + ret = pthread_sigmask(SIG_UNBLOCK, &mask, &oldmask); + urcu_posix_assert(!ret); + URCU_TLS(rcu_signal_was_blocked) = sigismember(&oldmask, SIGRCU); +} + +static +void urcu_signal_restore(void) +{ + sigset_t mask; + int ret; + + if (!URCU_TLS(rcu_signal_was_blocked)) + return; + ret = sigemptyset(&mask); + urcu_posix_assert(!ret); + ret = sigaddset(&mask, SIGRCU); + urcu_posix_assert(!ret); + ret = pthread_sigmask(SIG_BLOCK, &mask, NULL); + urcu_posix_assert(!ret); +} +#else +static +void urcu_signal_unblock(void) { } +static +void urcu_signal_restore(void) { } +#endif + void rcu_register_thread(void) { + urcu_signal_unblock(); + URCU_TLS(rcu_reader).tid = pthread_self(); - assert(URCU_TLS(rcu_reader).need_mb == 0); - assert(!(URCU_TLS(rcu_reader).ctr & RCU_GP_CTR_NEST_MASK)); + urcu_posix_assert(URCU_TLS(rcu_reader).need_mb == 0); + urcu_posix_assert(!(URCU_TLS(rcu_reader).ctr & URCU_GP_CTR_NEST_MASK)); mutex_lock(&rcu_registry_lock); - assert(!URCU_TLS(rcu_reader).registered); + urcu_posix_assert(!URCU_TLS(rcu_reader).registered); URCU_TLS(rcu_reader).registered = 1; rcu_init(); /* In case gcc does not support constructor attribute */ cds_list_add(&URCU_TLS(rcu_reader).node, ®istry); @@ -538,10 +596,12 @@ void rcu_register_thread(void) void rcu_unregister_thread(void) { mutex_lock(&rcu_registry_lock); - assert(URCU_TLS(rcu_reader).registered); + urcu_posix_assert(URCU_TLS(rcu_reader).registered); URCU_TLS(rcu_reader).registered = 0; cds_list_del(&URCU_TLS(rcu_reader).node); mutex_unlock(&rcu_registry_lock); + + urcu_signal_restore(); } #ifdef RCU_MEMBARRIER @@ -559,7 +619,7 @@ void rcu_sys_membarrier_status(bool available) { if (!available) return; - rcu_has_sys_membarrier_memb = 1; + urcu_memb_has_sys_membarrier = 1; } #endif @@ -574,7 +634,7 @@ void rcu_sys_membarrier_init(void) if (mask & MEMBARRIER_CMD_PRIVATE_EXPEDITED) { if (membarrier(MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED, 0)) urcu_die(errno); - has_sys_membarrier_private_expedited = 1; + urcu_memb_has_sys_membarrier_private_expedited = 1; available = true; } else if (mask & MEMBARRIER_CMD_SHARED) { available = true; @@ -593,7 +653,9 @@ void rcu_init(void) #endif #ifdef RCU_SIGNAL -static void sigrcu_handler(int signo, siginfo_t *siginfo, void *context) +static void sigrcu_handler(int signo __attribute__((unused)), + siginfo_t *siginfo __attribute__((unused)), + void *context __attribute__((unused))) { /* * Executing this cmm_smp_mb() is the only purpose of this signal handler. @@ -630,21 +692,24 @@ void rcu_init(void) urcu_die(errno); } +/* + * Don't unregister the SIGRCU signal handler anymore, because + * call_rcu threads could still be using it shortly before the + * application exits. + * Assertion disabled because call_rcu threads are now rcu + * readers, and left running at exit. + * urcu_posix_assert(cds_list_empty(®istry)); + */ + +#endif /* #ifdef RCU_SIGNAL */ + void rcu_exit(void) { - /* - * Don't unregister the SIGRCU signal handler anymore, because - * call_rcu threads could still be using it shortly before the - * application exits. - * Assertion disabled because call_rcu threads are now rcu - * readers, and left running at exit. - * assert(cds_list_empty(®istry)); - */ + urcu_call_rcu_exit(); } -#endif /* #ifdef RCU_SIGNAL */ - DEFINE_RCU_FLAVOR(rcu_flavor); #include "urcu-call-rcu-impl.h" #include "urcu-defer-impl.h" +#include "urcu-poll-impl.h"