From 73c6e4013b4cd92d3d531bc22cc29e6036ef42e0 Mon Sep 17 00:00:00 2001 From: Paolo Bonzini Date: Wed, 27 Jan 2016 08:49:21 +0100 Subject: rcu: completely disable pthread_atfork callbacks as soon as possible Because of -daemonize, system mode QEMU sometimes needs to fork() and keep RCU enabled in the child. However, there is a possible deadlock with synchronize_rcu: - the CPU thread is inside a RCU critical section and wants to take the BQL in order to do MMIO - the monitor thread, which is owning the BQL, calls rcu_init_lock which tries to take the rcu_sync_lock - the call_rcu thread has taken rcu_sync_lock in synchronize_rcu, but synchronize_rcu needs the CPU thread to end the critical section before returning. This cannot happen for user-mode emulation, because it does not have a BQL. To fix it, assume that system mode QEMU only forks in preparation for exec (except when daemonizing) and disable pthread_atfork as soon as the double fork has happened. Reported-by: Dr. David Alan Gilbert Tested-by: Dr. David Alan Gilbert Signed-off-by: Paolo Bonzini --- util/rcu.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'util') diff --git a/util/rcu.c b/util/rcu.c index 9adc5e4..2142ddd 100644 --- a/util/rcu.c +++ b/util/rcu.c @@ -318,15 +318,35 @@ static void rcu_init_complete(void) rcu_register_thread(); } +static int atfork_depth = 1; + +void rcu_enable_atfork(void) +{ + atfork_depth++; +} + +void rcu_disable_atfork(void) +{ + atfork_depth--; +} + #ifdef CONFIG_POSIX static void rcu_init_lock(void) { + if (atfork_depth < 1) { + return; + } + qemu_mutex_lock(&rcu_sync_lock); qemu_mutex_lock(&rcu_registry_lock); } static void rcu_init_unlock(void) { + if (atfork_depth < 1) { + return; + } + qemu_mutex_unlock(&rcu_registry_lock); qemu_mutex_unlock(&rcu_sync_lock); } -- cgit v1.1