From: Glauber Costa <glommer@redhat.com>
To: kvm@vger.kernel.org
Cc: avi@redhat.com, ehabkost@redhat.com
Subject: [PATCH 1/2] don't start cpu main loop while there is still init work to do.
Date: Wed, 29 Apr 2009 16:31:40 -0400 [thread overview]
Message-ID: <1241037101-24842-2-git-send-email-glommer@redhat.com> (raw)
In-Reply-To: <1241037101-24842-1-git-send-email-glommer@redhat.com>
As soon as we call kvm_init_vcpu(), we start the vcpu thread.
However, there is still things that has to be done, as soon
as the new CPUState is created. Examples include initializing the
apic, halting the cpu, etc.
Without this patch, it is possible that the cpu may want to start
using those things, before initializing them, leading to segfaults.
We introduce another state variable, "initialized", meaning that
the cpu is already created, but not totally initialized,
to serialize it.
Before this patch:
(qemu) cpu_set X online => segfaults ~ 80 % of the time
After this patch:
(qemu) cpu_set X online => works.
Signed-off-by: Glauber Costa <glommer@redhat.com>
---
qemu/cpu-defs.h | 1 +
qemu/hw/pc.c | 1 +
qemu/qemu-kvm.c | 11 +++++++++++
3 files changed, 13 insertions(+), 0 deletions(-)
diff --git a/qemu/cpu-defs.h b/qemu/cpu-defs.h
index f439ac0..83bddad 100644
--- a/qemu/cpu-defs.h
+++ b/qemu/cpu-defs.h
@@ -170,6 +170,7 @@ struct KVMCPUState {
int stop;
int stopped;
int created;
+ int initialized;
struct qemu_work_item *queued_work_first, *queued_work_last;
};
diff --git a/qemu/hw/pc.c b/qemu/hw/pc.c
index 19d75b9..64e6ca5 100644
--- a/qemu/hw/pc.c
+++ b/qemu/hw/pc.c
@@ -800,6 +800,7 @@ CPUState *pc_new_cpu(int cpu, const char *cpu_model, int pci_enabled)
if (pci_enabled) {
apic_init(env);
}
+ kvm_signal_vcpu_creation(env);
return env;
}
diff --git a/qemu/qemu-kvm.c b/qemu/qemu-kvm.c
index ed76367..c032618 100644
--- a/qemu/qemu-kvm.c
+++ b/qemu/qemu-kvm.c
@@ -37,6 +37,7 @@ kvm_context_t kvm_context;
pthread_mutex_t qemu_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t qemu_vcpu_cond = PTHREAD_COND_INITIALIZER;
+pthread_cond_t qemu_vcpu_init_cond = PTHREAD_COND_INITIALIZER;
pthread_cond_t qemu_system_cond = PTHREAD_COND_INITIALIZER;
pthread_cond_t qemu_pause_cond = PTHREAD_COND_INITIALIZER;
pthread_cond_t qemu_work_cond = PTHREAD_COND_INITIALIZER;
@@ -439,12 +440,22 @@ static void *ap_main_loop(void *_env)
/* and wait for machine initialization */
while (!qemu_system_ready)
qemu_cond_wait(&qemu_system_cond);
+
+ while (!env->kvm_cpu_state.initialized)
+ qemu_cond_wait(&qemu_vcpu_init_cond);
+
pthread_mutex_unlock(&qemu_mutex);
kvm_main_loop_cpu(env);
return NULL;
}
+void kvm_signal_vcpu_creation(CPUState *env)
+{
+ env->kvm_cpu_state.initialized = 1;
+ pthread_cond_signal(&qemu_vcpu_init_cond);
+}
+
void kvm_init_vcpu(CPUState *env)
{
pthread_create(&env->kvm_cpu_state.thread, NULL, ap_main_loop, env);
--
1.5.6.6
next prev parent reply other threads:[~2009-04-29 20:31 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-04-29 20:31 [PATCH 0/2] Fix cpu hotplug in upstream kvm Glauber Costa
2009-04-29 20:31 ` Glauber Costa [this message]
2009-04-29 20:31 ` [PATCH 2/2] Present kvm with corret apic phys id Glauber Costa
2009-05-04 8:32 ` Avi Kivity
2009-05-04 8:30 ` [PATCH 1/2] don't start cpu main loop while there is still init work to do Avi Kivity
2009-05-04 14:26 ` Glauber Costa
2009-05-04 14:33 ` Avi Kivity
2009-05-04 14:44 ` Glauber Costa
2009-05-04 14:48 ` Avi Kivity
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=1241037101-24842-2-git-send-email-glommer@redhat.com \
--to=glommer@redhat.com \
--cc=avi@redhat.com \
--cc=ehabkost@redhat.com \
--cc=kvm@vger.kernel.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