fix: handle EINTR correctly in get_cpu_mask_from_sysfs
[urcu.git] / src / urcu.c
index 2ebe993e33b5fdd3a540f3b15d7b92b450106766..5f4a7d71c2fad83e414e5c32f09ff884aac13488 100644 (file)
@@ -1,25 +1,11 @@
+// SPDX-FileCopyrightText: 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+// 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 <mathieu.desnoyers@efficios.com>
- * 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.
  */
 
@@ -30,7 +16,6 @@
 #include <stdio.h>
 #include <pthread.h>
 #include <signal.h>
-#include <assert.h>
 #include <stdlib.h>
 #include <stdint.h>
 #include <string.h>
@@ -39,6 +24,8 @@
 #include <poll.h>
 
 #include <urcu/config.h>
+#include <urcu/annotate.h>
+#include <urcu/assert.h>
 #include <urcu/arch.h>
 #include <urcu/wfcqueue.h>
 #include <urcu/map/urcu.h>
@@ -94,27 +81,19 @@ static int urcu_memb_has_sys_membarrier_private_expedited;
  * uninitialized variable.
  */
 int urcu_memb_has_sys_membarrier = 0;
-URCU_ATTR_ALIAS("urcu_memb_has_sys_membarrier")
-extern int rcu_has_sys_membarrier_memb;
 #endif
 
 void __attribute__((constructor)) rcu_init(void);
 #endif
 
-#ifdef RCU_MB
+#if defined(RCU_MB)
 void rcu_init(void)
 {
 }
-URCU_ATTR_ALIAS(urcu_stringify(rcu_init))
-void alias_rcu_init(void);
 #endif
 
-#ifdef RCU_SIGNAL
-static int init_done;
-
-void __attribute__((constructor)) rcu_init(void);
 void __attribute__((destructor)) rcu_exit(void);
-#endif
+static void urcu_call_rcu_exit(void);
 
 /*
  * rcu_gp_lock ensures mutual exclusion between threads calling
@@ -132,15 +111,12 @@ static pthread_mutex_t rcu_gp_lock = PTHREAD_MUTEX_INITIALIZER;
  */
 static pthread_mutex_t rcu_registry_lock = PTHREAD_MUTEX_INITIALIZER;
 struct urcu_gp rcu_gp = { .ctr = URCU_GP_COUNT };
-URCU_ATTR_ALIAS(urcu_stringify(rcu_gp))
-extern struct urcu_gp alias_rcu_gp;
 
 /*
  * Written to only by each individual reader. Read by both the reader and the
  * writers.
  */
 DEFINE_URCU_TLS(struct urcu_reader, rcu_reader);
-DEFINE_URCU_TLS_ALIAS(struct urcu_reader, rcu_reader, alias_rcu_reader);
 
 static CDS_LIST_HEAD(registry);
 
@@ -195,62 +171,13 @@ static void smp_mb_master(void)
 }
 #endif
 
-#ifdef RCU_MB
+#if defined(RCU_MB)
 static void smp_mb_master(void)
 {
        cmm_smp_mb();
 }
 #endif
 
-#ifdef RCU_SIGNAL
-static void force_mb_all_readers(void)
-{
-       struct urcu_reader *index;
-
-       /*
-        * Ask for each threads to execute a cmm_smp_mb() so we can consider the
-        * compiler barriers around rcu read lock as real memory barriers.
-        */
-       if (cds_list_empty(&registry))
-               return;
-       /*
-        * pthread_kill has a cmm_smp_mb(). But beware, we assume it performs
-        * a cache flush on architectures with non-coherent cache. Let's play
-        * safe and don't assume anything : we use cmm_smp_mc() to make sure the
-        * cache flush is enforced.
-        */
-       cds_list_for_each_entry(index, &registry, node) {
-               CMM_STORE_SHARED(index->need_mb, 1);
-               pthread_kill(index->tid, SIGRCU);
-       }
-       /*
-        * Wait for sighandler (and thus mb()) to execute on every thread.
-        *
-        * Note that the pthread_kill() will never be executed on systems
-        * that correctly deliver signals in a timely manner.  However, it
-        * is not uncommon for kernels to have bugs that can result in
-        * lost or unduly delayed signals.
-        *
-        * If you are seeing the below pthread_kill() executing much at
-        * all, we suggest testing the underlying kernel and filing the
-        * relevant bug report.  For Linux kernels, we recommend getting
-        * the Linux Test Project (LTP).
-        */
-       cds_list_for_each_entry(index, &registry, node) {
-               while (CMM_LOAD_SHARED(index->need_mb)) {
-                       pthread_kill(index->tid, SIGRCU);
-                       (void) poll(NULL, 0, 1);
-               }
-       }
-       cmm_smp_mb();   /* read ->need_mb before ending the barrier */
-}
-
-static void smp_mb_master(void)
-{
-       force_mb_all_readers();
-}
-#endif /* #ifdef RCU_SIGNAL */
-
 /*
  * synchronize_rcu() waiting. Single thread.
  * Always called with rcu_registry lock held. Releases this lock and
@@ -259,24 +186,30 @@ static void smp_mb_master(void)
 static void wait_gp(void)
 {
        /*
-        * Read reader_gp before read futex. smp_mb_master() needs to
-        * be called with the rcu registry lock held in RCU_SIGNAL
-        * flavor.
+        * Read reader_gp before read futex.
         */
        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);
@@ -295,7 +228,8 @@ 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 urcu_reader *index, *tmp;
@@ -318,7 +252,7 @@ static void wait_for_readers(struct cds_list_head *input_readers,
                }
 
                cds_list_for_each_entry_safe(index, tmp, input_readers, node) {
-                       switch (urcu_common_reader_state(&rcu_gp, &index->ctr)) {
+                       switch (urcu_common_reader_state(&rcu_gp, &index->ctr, group)) {
                        case URCU_READER_ACTIVE_CURRENT:
                                if (cur_snap_readers) {
                                        cds_list_move(&index->node,
@@ -402,6 +336,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);
@@ -416,10 +352,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 */
@@ -444,13 +381,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(&registry, &cur_snap_readers, &qsreaders);
+       wait_for_readers(&registry, &cur_snap_readers, &qsreaders, &acquire_group);
 
        /*
         * Must finish waiting for quiescent state for original parity before
@@ -469,7 +407,8 @@ void synchronize_rcu(void)
        cmm_smp_mb();
 
        /* Switch parity: 0 -> 1, 1 -> 0 */
-       CMM_STORE_SHARED(rcu_gp.ctr, rcu_gp.ctr ^ URCU_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
@@ -490,9 +429,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.
@@ -505,6 +444,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);
@@ -516,8 +456,6 @@ out:
         */
        urcu_wake_all_waiters(&waiters);
 }
-URCU_ATTR_ALIAS(urcu_stringify(synchronize_rcu))
-void alias_synchronize_rcu();
 
 /*
  * library wrappers to be used by non-LGPL compatible source code.
@@ -527,49 +465,39 @@ void rcu_read_lock(void)
 {
        _rcu_read_lock();
 }
-URCU_ATTR_ALIAS(urcu_stringify(rcu_read_lock))
-void alias_rcu_read_lock();
 
 void rcu_read_unlock(void)
 {
        _rcu_read_unlock();
 }
-URCU_ATTR_ALIAS(urcu_stringify(rcu_read_unlock))
-void alias_rcu_read_unlock();
 
 int rcu_read_ongoing(void)
 {
        return _rcu_read_ongoing();
 }
-URCU_ATTR_ALIAS(urcu_stringify(rcu_read_ongoing))
-void alias_rcu_read_ongoing();
 
 void rcu_register_thread(void)
 {
        URCU_TLS(rcu_reader).tid = pthread_self();
-       assert(URCU_TLS(rcu_reader).need_mb == 0);
-       assert(!(URCU_TLS(rcu_reader).ctr & URCU_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, &registry);
        mutex_unlock(&rcu_registry_lock);
 }
-URCU_ATTR_ALIAS(urcu_stringify(rcu_register_thread))
-void alias_rcu_register_thread();
 
 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_ATTR_ALIAS(urcu_stringify(rcu_unregister_thread))
-void alias_rcu_unregister_thread();
 
 #ifdef RCU_MEMBARRIER
 
@@ -617,68 +545,15 @@ void rcu_init(void)
        init_done = 1;
        rcu_sys_membarrier_init();
 }
-URCU_ATTR_ALIAS(urcu_stringify(rcu_init))
-void alias_rcu_init(void);
 #endif
 
-#ifdef RCU_SIGNAL
-static void sigrcu_handler(int signo, siginfo_t *siginfo, void *context)
-{
-       /*
-        * Executing this cmm_smp_mb() is the only purpose of this signal handler.
-        * It punctually promotes cmm_barrier() into cmm_smp_mb() on every thread it is
-        * executed on.
-        */
-       cmm_smp_mb();
-       _CMM_STORE_SHARED(URCU_TLS(rcu_reader).need_mb, 0);
-       cmm_smp_mb();
-}
-
-/*
- * rcu_init constructor. Called when the library is linked, but also when
- * reader threads are calling rcu_register_thread().
- * Should only be called by a single thread at a given time. This is ensured by
- * holing the rcu_registry_lock from rcu_register_thread() or by running
- * at library load time, which should not be executed by multiple
- * threads nor concurrently with rcu_register_thread() anyway.
- */
-void rcu_init(void)
-{
-       struct sigaction act;
-       int ret;
-
-       if (init_done)
-               return;
-       init_done = 1;
-
-       act.sa_sigaction = sigrcu_handler;
-       act.sa_flags = SA_SIGINFO | SA_RESTART;
-       sigemptyset(&act.sa_mask);
-       ret = sigaction(SIGRCU, &act, NULL);
-       if (ret)
-               urcu_die(errno);
-}
-URCU_ATTR_ALIAS(urcu_stringify(rcu_init))
-void alias_rcu_init(void);
-
 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(&registry));
-        */
+       urcu_call_rcu_exit();
 }
-URCU_ATTR_ALIAS(urcu_stringify(rcu_exit))
-void alias_rcu_exit(void);
-
-#endif /* #ifdef RCU_SIGNAL */
 
 DEFINE_RCU_FLAVOR(rcu_flavor);
-DEFINE_RCU_FLAVOR_ALIAS(rcu_flavor, alias_rcu_flavor);
 
 #include "urcu-call-rcu-impl.h"
 #include "urcu-defer-impl.h"
+#include "urcu-poll-impl.h"
This page took 0.0271 seconds and 4 git commands to generate.