We were calling rcu_init_complete() twice in the child processes when
fork happened. However the pthread library does not really suggest to do
it that way:


"Attempting to initialise an already initialised mutex results in
 undefined behaviour."

Actually, IMHO we can do it in a more natural way: Firstly, we only init
the RCU globals once in rcu_init(). Then, in rcu_init_child(), we unlock
all the locks held in rcu_init_lock() just like what we do in the parent
process, then do the rest of RCU re-init (e.g., create the RCU thread).

CC: Paolo Bonzini <pbonz...@redhat.com>
Signed-off-by: Peter Xu <pet...@redhat.com>
this is based on Paolo's series:
"[PATCH for-2.10 0/2] RCU: forking fix and cleanups"
 util/rcu.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/util/rcu.c b/util/rcu.c
index ca5a63e..6fbbe4c 100644
--- a/util/rcu.c
+++ b/util/rcu.c
@@ -299,15 +299,17 @@ void rcu_unregister_thread(void)
-static void rcu_init_complete(void)
+static void rcu_init_globals(void)
-    QemuThread thread;
     qemu_event_init(&rcu_gp_event, true);
     qemu_event_init(&rcu_call_ready_event, false);
+static void rcu_init_complete(void)
+    QemuThread thread;
     /* The caller is assumed to have iothread lock, so the call_rcu thread
      * must have been quiescent even after forking, just recreate it.
@@ -357,6 +359,13 @@ static void rcu_init_child(void)
+    rcu_init_unlock();
+    /*
+     * For the newly forked child, we need something extra: since
+     * after fork the threads are all gone, we need to re-init the RCU
+     * thread, along with the globals.
+     */
     memset(&registry, 0, sizeof(registry));
@@ -367,5 +376,6 @@ static void __attribute__((__constructor__)) rcu_init(void)
     pthread_atfork(rcu_init_lock, rcu_init_unlock, rcu_init_child);
+    rcu_init_globals();

Reply via email to