From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] [PATCH 1/2] rcu: completely disable pthread_atfork callbacks as soon as possible
Date: Fri, 4 Aug 2017 18:01:10 +0100 [thread overview]
Message-ID: <20170804170110.GD2087@work-vm> (raw)
In-Reply-To: <20170804161452.17413-2-pbonzini@redhat.com>
* Paolo Bonzini (pbonzini@redhat.com) wrote:
> 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: David Gilbert <dgilbert@redhat.com>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
That seems to fix that issue (not tested it any more than that though),
so for fixing that issue:
Tested-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
> ---
> include/qemu/rcu.h | 6 ++++++
> util/rcu.c | 20 ++++++++++++++++++++
> vl.c | 1 +
> 3 files changed, 27 insertions(+)
>
> diff --git a/include/qemu/rcu.h b/include/qemu/rcu.h
> index 83ae2808be..c0da9907e8 100644
> --- a/include/qemu/rcu.h
> +++ b/include/qemu/rcu.h
> @@ -105,6 +105,12 @@ extern void synchronize_rcu(void);
> */
> extern void rcu_register_thread(void);
> extern void rcu_unregister_thread(void);
> +
> +/*
> + * Support for fork(). fork() support is enabled at startup.
> + */
> +extern void rcu_enable_atfork(void);
> +extern void rcu_disable_atfork(void);
> extern void rcu_after_fork(void);
>
> struct rcu_head;
> diff --git a/util/rcu.c b/util/rcu.c
> index 9adc5e4a36..2142ddd93b 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);
> }
> diff --git a/vl.c b/vl.c
> index 99fcfa0442..8967115514 100644
> --- a/vl.c
> +++ b/vl.c
> @@ -4121,6 +4121,7 @@ int main(int argc, char **argv, char **envp)
> set_memory_options(&ram_slots, &maxram_size, machine_class);
>
> os_daemonize();
> + rcu_disable_atfork();
>
> if (pid_file && qemu_create_pidfile(pid_file) != 0) {
> error_report("could not acquire pid file: %s", strerror(errno));
> --
> 2.13.3
>
>
--
Dr. David Alan Gilbert / dgilbert@redhat.com / Manchester, UK
next prev parent reply other threads:[~2017-08-04 17:01 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-08-04 16:14 [Qemu-devel] [PATCH for-2.10 0/2] RCU: forking fix and cleanups Paolo Bonzini
2017-08-04 16:14 ` [Qemu-devel] [PATCH 1/2] rcu: completely disable pthread_atfork callbacks as soon as possible Paolo Bonzini
2017-08-04 17:01 ` Dr. David Alan Gilbert [this message]
2017-08-04 16:14 ` [Qemu-devel] [PATCH 2/2] Revert "rcu: do not create thread in pthread_atfork callback" Paolo Bonzini
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170804170110.GD2087@work-vm \
--to=dgilbert@redhat.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).