use little granularity lock to substitue qemu_mutex_lock_iothread
Nowadays, we use qemu_mutex_lock_iothread()/qemu_mutex_unlock_iothread() to protect the race to access the emulated dev launched by vcpu threads & iothread.
But this lock is too big. We can break it down.
These patches separate the CPUArchState's protection from the other devices, so we can have a per-cpu lock for each CPUArchState, not the big lock any longer.
PATCH 1/2 CPUArchState: introduce per-cpu lock
introduce a lock for per-cpu to protect agaist accesing from other vcpu thread.
--- cpu-defs.h | 2 ++ cpus.c | 17 +++++++++++++++++ main-loop.h | 3 +++ 3 files changed, 22 insertions(+), 0 deletions(-)
cpu-defs.h +2 -0
@@ -30,6 +30,7 @@#include "osdep.h" #include "qemu-queue.h" #include "targphys.h" +#include "qemu-thread-posix.h" #ifndef TARGET_LONG_BITS #error TARGET_LONG_BITS must be defined before including this header
@@ -220,6 +221,7 @@ typedef struct CPUWatchpoint {CPU_COMMON_THREAD \ struct QemuCond *halt_cond; \int thread_kicked; \ +struct QemuMutex *cpu_lock; \struct qemu_work_item *queued_work_first, *queued_work_last; \const char *cpu_model_str; \struct KVMState *kvm_state; \
cpus.c +17 -0
@@ -735,6 +735,7 @@ static void *qemu_kvm_cpu_thread_fn(void *arg) env->thread_id =qemu_get_thread_id (); cpu_single_env = env; + r =kvm_init_vcpu (env);if (r <0 ) {fprintf (stderr,"kvm_init_vcpu failed: %s\n" ,strerror (-r));
@@ -891,6 +892,20 @@ int qemu_cpu_is_self(void *_env)return qemu_thread_is_self (env->thread); } +void qemu_mutex_lock_cpu (void *_env) +{ +CPUArchState *env = _env; + +qemu_mutex_lock (env->cpu_lock); +} + +void qemu_mutex_unlock_cpu (void *_env) +{ +CPUArchState *env = _env; + +qemu_mutex_unlock (env->cpu_lock); +} +void qemu_mutex_lock_iothread (void ) {if (!tcg_enabled ()) {
@@ -1027,6 +1042,8 @@ void qemu_init_vcpu(void *_env) env->nr_cores = smp_cores; env->nr_threads = smp_threads; env->stopped =1 ; + env->cpu_lock =g_malloc0 (sizeof (QemuMutex)); +qemu_mutex_init (env->cpu_lock);if (kvm_enabled ()) {qemu_kvm_start_vcpu (env); }else if (tcg_enabled ()) {
main-loop.h +3 -0
@@ -323,6 +323,9 @@ void qemu_bh_delete(QEMUBH *bh);int qemu_add_child_watch (pid_t pid);#endif +void qemu_mutex_lock_cpu (void *_env); +void qemu_mutex_unlock_cpu (void *_env); +/** * qemu_mutex_lock_iothread: Lock the main loop mutex. *
Signature · 1 line
1.7.4.4
PATCH 2/2 kvm: use per-cpu lock to free vcpu thread out of the big lock
In reply to Liu Ping FanIn order to break the big lock, using per-cpu_lock in kvm_cpu_exec() to protect the race from other cpu's access to env->apic_state & related field in env.
Also, we need to protect agaist run_on_cpu().
Race condition can be like this:
1. vcpu-1 IPI vcpu-2
vcpu-3 IPI vcpu-2
Open window exists for accessing to vcpu-2's apic_state & env
2. run_on_cpu() write env->queued_work_last, while flush_queued_work() read
--- cpus.c | 6 ++++-- hw/apic.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---- hw/pc.c | 8 +++++++- kvm-all.c | 13 +++++++++++-- 4 files changed, 76 insertions(+), 9 deletions(-)
cpus.c +4 -2
@@ -649,6 +649,7 @@ void run_on_cpu(CPUArchState *env, void (*func)(void *data), void *data) wi.func = func; wi.data = data; +qemu_mutex_lock (env->cpu_lock);if (!env->queued_work_first) { env->queued_work_first = &wi; }else {
@@ -657,6 +658,7 @@ void run_on_cpu(CPUArchState *env, void (*func)(void *data), void *data) env->queued_work_last = &wi; wi.next =NULL ; wi.done = false; +qemu_mutex_unlock (env->cpu_lock);qemu_cpu_kick (env);while (!wi.done) {
@@ -718,7 +720,7 @@ static void qemu_tcg_wait_io_event(void)static void qemu_kvm_wait_io_event (CPUArchState *env) {while (cpu_thread_is_idle (env)) { -qemu_cond_wait (env->halt_cond, &qemu_global_mutex); +qemu_cond_wait (env->halt_cond, env->cpu_lock); }qemu_kvm_eat_signals (env);
@@ -729,8 +731,8 @@ static void *qemu_kvm_cpu_thread_fn(void *arg) {CPUArchState *env = arg;int r; +qemu_mutex_lock_cpu (env); -qemu_mutex_lock (&qemu_global_mutex);qemu_thread_get_self (env->thread); env->thread_id =qemu_get_thread_id (); cpu_single_env = env;
hw/apic.c +54 -4
288 lines not on this page · raw message
hw/pc.c +7 -1
@@ -147,7 +147,7 @@ void cpu_smm_update(CPUX86State *env)smm_set (!!(env->hflags &HF_SMM_MASK ), smm_arg); } - +/* Called by kvm_cpu_exec(), already with lock protection */ /* IRQ handling */ int cpu_get_pic_interrupt (CPUX86State *env) {
@@ -173,9 +173,15 @@ static void pic_irq_request(void *opaque, int irq, int level)DPRINTF ("pic_irqs: %s irq %d\n" , level?"raise" :"lower" , irq);if (env->apic_state) {while (env) { +if (!qemu_cpu_is_self (env)) { +qemu_mutex_lock_cpu (env); + }if (apic_accept_pic_intr (env->apic_state)) {apic_deliver_pic_intr (env->apic_state, level); } +if (!qemu_cpu_is_self (env)) { +qemu_mutex_unlock_cpu (env); + } env = env->next_cpu; } }else {
kvm-all.c +11 -2
@@ -29,6 +29,7 @@#include "bswap.h" #include "memory.h" #include "exec-memory.h" +#include "qemu-thread.h" /* This check must be after config-host.h is included */ #ifdef CONFIG_EVENTFD
@@ -1252,12 +1253,15 @@ int kvm_cpu_exec(CPUArchState *env)*/ qemu_cpu_kick_self (); } -qemu_mutex_unlock_iothread (); +qemu_mutex_unlock (env->cpu_lock); run_ret =kvm_vcpu_ioctl (env,KVM_RUN ,0 ); -qemu_mutex_lock_iothread (); +qemu_mutex_lock (env->cpu_lock);kvm_arch_post_run (env, run); +qemu_mutex_unlock_cpu (env); + +qemu_mutex_lock_iothread ();kvm_flush_coalesced_mmio_buffer ();
@@ -1265,6 +1269,8 @@ int kvm_cpu_exec(CPUArchState *env)if (run_ret == -EINTR || run_ret == -EAGAIN ) {DPRINTF ("io window exit\n" ); ret =EXCP_INTERRUPT ; +qemu_mutex_unlock_iothread (); +qemu_mutex_lock_cpu (env);break ; } fprintf(stderr, "error: kvm run failed %s\n",
@@ -1312,6 +1318,9 @@ int kvm_cpu_exec(CPUArchState *env) ret =kvm_arch_handle_exit (env, run);break ; } + +qemu_mutex_unlock_iothread (); +qemu_mutex_lock_cpu (env); }while (ret ==0 );if (ret <0 ) {
Signature · 1 line
1.7.4.4
Re: [Qemu-devel] [PATCH 2/2] kvm: use per-cpu lock to free vcpu thread out of the big lock
Hi Liu,
Quoted 3 lines · Liu Ping Fan
In order to break the big lock, using per-cpu_lock in
kvm_cpu_exec()to protect the race from other cpu's access toenv->apic_state& related field in env.
Can this also be applied on tcg_cpu_exec(), too?
Regards,
chenwj
Signature · 5 lines
Wei-Ren Chen (陳韋任) Computer Systems Lab, Institute of Information Science, Academia Sinica, Taiwan (R.O.C.) Tel:886-2-2788-3799 #1667 Homepage: http://people.cs.nctu.edu.tw/~chenwj
Re: [Qemu-devel] [PATCH 2/2] kvm: use per-cpu lock to free vcpu thread out of the big lock
Quoted 9 lines · 陳韋任 (Wei-Ren Chen)
Hi Liu,
Quoted 3 lines · Liu Ping Fan
In order to break the big lock, using per-cpu_lock in
kvm_cpu_exec()to protect the race from other cpu's access toenv->apic_state& related field in env.Can this also be applied on
tcg_cpu_exec(), too?
No, currently, I am focusing on kvm branch
Regards,
pingfan
Quoted 9 lines
Regards,
chenwj--
Wei-Ren Chen (陳韋任)
Computer Systems Lab, Institute of Information Science, Academia Sinica, Taiwan (R.O.C.) Tel:886-2-2788-3799 #1667
Homepage: http://people.cs.nctu.edu.tw/~chenwj
Re: [Qemu-devel] [PATCH 1/2] CPUArchState: introduce per-cpu lock
In reply to Liu Ping Fancpu-defs.h line 33
Quoted 9 lines · Liu Ping Fan
cpu-defs.h +1
-0@@ -30,6 +30,7 @@ #include "osdep.h" #include "qemu-queue.h" #include "targphys.h" +#include "qemu-thread-posix.h"
This breaks Windows, you need qemu-thread.h.
cpu-defs.h typedef struct CPUWatchpoint { · line 224
Quoted 8 lines
cpu-defs.h +1
-0#ifndef TARGET_LONG_BITS #error TARGET_LONG_BITS must be defined before including this header@@ -220,6 +221,7 @@ typedef struct CPUWatchpoint { CPU_COMMON_THREAD \ struct QemuCond *halt_cond; \ int thread_kicked; \ + struct QemuMutex *cpu_lock; \
It would be nicer to declare it QemuMutex cpu_lock (no pointer) so that you don't need to worry about malloc/free.
Quoted 8 lines
cpu-defs.h +0
-0struct qemu_work_item *queued_work_first, *queued_work_last; \ const char *cpu_model_str; \ struct KVMState *kvm_state; \cpus.c +0
-0
cpus.c static void *qemu_kvm_cpu_thread_fn(void *arg) · line 741
Quoted 7 lines
cpus.c +1
-0@@ -735,6 +735,7 @@ static void *qemu_kvm_cpu_thread_fn(void *arg) env->thread_id = qemu_get_thread_id(); cpu_single_env = env; + r = kvm_init_vcpu(env); if (r < 0) { fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
Spurious whitespace change, this should be dropped from the patch.
main-loop.h void qemu_bh_delete(QEMUBH *bh); · line 327
Quoted 10 lines
main-loop.h +2
-0@@ -323,6 +323,9 @@ void qemu_bh_delete(QEMUBH *bh); int qemu_add_child_watch(pid_t pid); #endif +void qemu_mutex_lock_cpu (void *_env); +void qemu_mutex_unlock_cpu (void *_env);
Why void* instead of CPUArchState*?
Stefan
Re: [Qemu-devel] [PATCH 1/2] CPUArchState: introduce per-cpu lock
In reply to Liu Ping FanAm 21.06.2012 17:06, schrieb Liu Ping Fan:
Quoted 23 lines
introduce a lock for per-cpu to protect agaist accesing from other vcpu thread.
Signed-off-by Liu Ping Fan <[email protected]>--- cpu-defs.h | 2 ++ cpus.c | 17 +++++++++++++++++ main-loop.h | 3 +++ 3 files changed, 22 insertions(+), 0 deletions(-)cpu-defs.h +1
-0@@ -30,6 +30,7 @@#include "osdep.h" #include "qemu-queue.h" #include "targphys.h" +#include "qemu-thread-posix.h" #ifndef TARGET_LONG_BITS #error TARGET_LONG_BITS must be defined before including this header
cpu-defs.h typedef struct CPUWatchpoint { · line 227
Quoted 7 lines
cpu-defs.h +1
-0@@ -220,6 +221,7 @@ typedef struct CPUWatchpoint {CPU_COMMON_THREAD \ struct QemuCond *halt_cond; \int thread_kicked; \ +struct QemuMutex *cpu_lock; \struct qemu_work_item *queued_work_first, *queued_work_last; \const char *cpu_model_str; \struct KVMState *kvm_state; \
Please don't add stuff to CPU_COMMON. Instead add to CPUState in qom/cpu.c. The QOM CPUState part 4 series moves many of those fields there.
cpus.c static void *qemu_kvm_cpu_thread_fn(void *arg) · line 738
Quoted 9 lines
cpus.c +1
-0@@ -735,6 +735,7 @@ static void *qemu_kvm_cpu_thread_fn(void *arg) env->thread_id =qemu_get_thread_id (); cpu_single_env = env; +
Stray whitespace addition.
Quoted 11 lines
cpus.c +4
-0r =kvm_init_vcpu (env);if (r <0 ) {fprintf (stderr,"kvm_init_vcpu failed: %s\n" ,strerror (-r));@@ -891,6 +892,20 @@ int qemu_cpu_is_self(void *_env)return qemu_thread_is_self (env->thread); } +void qemu_mutex_lock_cpu (void *_env) +{ +CPUArchState *env = _env; +
cpus.c int qemu_cpu_is_self(void *_env) · line 908
Quoted 10 lines
cpus.c +10
-0+qemu_mutex_lock (env->cpu_lock); +} + +void qemu_mutex_unlock_cpu (void *_env) +{ +CPUArchState *env = _env; + +qemu_mutex_unlock (env->cpu_lock); +} +
I don't like these helpers. For one, you are using void * arguments and casting them, for another you are using CPUArchState at all. With my suggestion above these can be CPUState *cpu.
cpus.c void qemu_init_vcpu(void *_env) · line 1046
Quoted 9 lines
cpus.c +2
-0void qemu_mutex_lock_iothread (void ) {if (!tcg_enabled ()) {@@ -1027,6 +1042,8 @@ void qemu_init_vcpu(void *_env) env->nr_cores = smp_cores; env->nr_threads = smp_threads; env->stopped =1 ; + env->cpu_lock =g_malloc0 (sizeof (QemuMutex)); +qemu_mutex_init (env->cpu_lock);
Are you sure this is not needed for linux-user/bsd-user? If not needed, then the field should be #ifdef'ed in the struct to assure that. Otherwise this function is never called and you need to move the initialization to the initfn in qom/cpu.c and then should also clean it up in a finalizer.
Andreas
Quoted 17 lines
cpus.c +0
-0if (kvm_enabled ()) {qemu_kvm_start_vcpu (env); }else if (tcg_enabled ()) {main-loop.h +3
-0@@ -323,6 +323,9 @@ void qemu_bh_delete(QEMUBH *bh);int qemu_add_child_watch (pid_t pid);#endif +void qemu_mutex_lock_cpu (void *_env); +void qemu_mutex_unlock_cpu (void *_env); +/** * qemu_mutex_lock_iothread: Lock the main loop mutex. *
Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
Re: [Qemu-devel] [PATCH 1/2] CPUArchState: introduce per-cpu lock
Quoted 21 lines · Andreas Färber
Am 21.06.2012 17:06, schrieb Liu Ping Fan:
Quoted 20 lines
introduce a lock for per-cpu to protect agaist accesing from other vcpu thread.
Signed-off-by Liu Ping Fan <[email protected]>---
cpu-defs.h | 2 ++
cpus.c | 17 +++++++++++++++++
main-loop.h | 3 +++3 files changed, 22 insertions(+), 0 deletions(-)cpu-defs.h +1
-0@@ -30,6 +30,7 @@ #include "osdep.h" #include "qemu-queue.h" #include "targphys.h" +#include "qemu-thread-posix.h"
Quoted 14 lines
cpu-defs.h typedef struct CPUWatchpoint { · line 227
Quoted 10 lines
cpu-defs.h +1
-0#ifndef TARGET_LONG_BITS #error TARGET_LONG_BITS must be defined before including this header@@ -220,6 +221,7 @@ typedef struct CPUWatchpoint { CPU_COMMON_THREAD \ struct QemuCond *halt_cond; \ int thread_kicked; \ + struct QemuMutex *cpu_lock; \ struct qemu_work_item *queued_work_first, *queued_work_last; \ const char *cpu_model_str; \ struct KVMState *kvm_state; \Please don't add stuff to
CPU_COMMON. Instead add to CPUState inqom/cpu.c. The QOM CPUState part 4 series moves many of those fields there.
Ok, thanks.
Quoted 29 lines
cpus.c static void *qemu_kvm_cpu_thread_fn(void *arg) · line 738
Quoted 9 lines
cpus.c +1
-0@@ -735,6 +735,7 @@ static void *qemu_kvm_cpu_thread_fn(void *arg) env->thread_id = qemu_get_thread_id(); cpu_single_env = env; +Stray whitespace addition.
Quoted 17 lines
cpus.c +10
-0r = kvm_init_vcpu(env); if (r < 0) { fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));@@ -891,6 +892,20 @@ int qemu_cpu_is_self(void *_env) return qemu_thread_is_self(env->thread); } +void qemu_mutex_lock_cpu (void *_env) +{ + CPUArchState *env = _env; + + qemu_mutex_lock(env->cpu_lock); +} + +void qemu_mutex_unlock_cpu (void *_env) +{ + CPUArchState *env = _env;
Quoted 9 lines
cpus.c int qemu_cpu_is_self(void *_env) · line 908
Quoted 4 lines
cpus.c +4
-0+ + qemu_mutex_unlock(env->cpu_lock); +} +I don't like these helpers. For one, you are using void * arguments and casting them, for another you are using CPUArchState at all. With my suggestion above these can be CPUState *cpu.
For using it in apic.c, we need to hide the CPUArchState structure
Quoted 7 lines
Quoted 7 lines
cpus.c +0
-0void qemu_mutex_lock_iothread(void) { if (!tcg_enabled()) {@@ -1027,6 +1042,8 @@ void qemu_init_vcpu(void *_env) env->nr_cores = smp_cores; env->nr_threads = smp_threads; env->stopped = 1;
Quoted 9 lines
cpus.c void qemu_init_vcpu(void *_env) · line 1046
Quoted 2 lines
cpus.c +2
-0+ env->cpu_lock = g_malloc0(sizeof(QemuMutex)); + qemu_mutex_init(env->cpu_lock);Are you sure this is not needed for linux-user/bsd-user? If not needed, then the field should be #ifdef'ed in the struct to assure that. Otherwise this function is never called and you need to move the initialization to the initfn in
qom/cpu.cand then should also clean it up in a finalizer.
It is not needed for linux-user/bsd-user. I will use the macro,
Thanks and regards,
pingfan
Quoted 23 lines
Andreas
Quoted 8 lines
cpus.c +0
-0if (kvm_enabled()) { qemu_kvm_start_vcpu(env); } else if (tcg_enabled()) {main-loop.h +0
-0main-loop.h void qemu_bh_delete(QEMUBH *bh); · line 331
Quoted 9 lines
main-loop.h +3
-0@@ -323,6 +323,9 @@ void qemu_bh_delete(QEMUBH *bh); int qemu_add_child_watch(pid_t pid); #endif +void qemu_mutex_lock_cpu (void *_env); +void qemu_mutex_unlock_cpu (void *_env); + /*** qemu_mutex_lock_iothread: Lock the main loop mutex. * --
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
Re: [Qemu-devel] [PATCH 1/2] CPUArchState: introduce per-cpu lock
In reply to Stefan HajnocziQuoted 14 lines · Stefan Hajnoczi
cpu-defs.h line 33
Quoted 9 lines · Liu Ping Fan
cpu-defs.h +1
-0@@ -30,6 +30,7 @@ #include "osdep.h" #include "qemu-queue.h" #include "targphys.h" +#include "qemu-thread-posix.h" This breaks Windows, you need
qemu-thread.h.Quoted 1 line
Quoted 11 lines
cpu-defs.h typedef struct CPUWatchpoint { · line 224
Quoted 7 lines
#ifndef
TARGET_LONG_BITS
#errorTARGET_LONG_BITSmust be defined before including this headercpu-defs.h +1
-0@@ -220,6 +221,7 @@ typedef struct CPUWatchpoint { CPU_COMMON_THREAD \ struct QemuCond *halt_cond; \ int thread_kicked; \ + struct QemuMutex *cpu_lock; \It would be nicer to declare it QemuMutex
cpu_lock(no pointer) so that you don't need to worry about malloc/free.
Yes :), I have considered about it, and not quite sure. But I figure out the lock for per-device to break BQL will be like this for some reason.
After all, have not decided yet.
Quoted 26 lines
Quoted 8 lines
cpu-defs.h +0
-0struct qemu_work_item *queued_work_first, *queued_work_last; \ const char *cpu_model_str; \ struct KVMState *kvm_state; \cpus.c +0
-0cpus.c static void *qemu_kvm_cpu_thread_fn(void *arg) · line 741
Quoted 7 lines
cpus.c +1
-0@@ -735,6 +735,7 @@ static void *qemu_kvm_cpu_thread_fn(void *arg) env->thread_id = qemu_get_thread_id(); cpu_single_env = env; + r = kvm_init_vcpu(env); if (r < 0) { fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));Spurious whitespace change, this should be dropped from the patch.
Quoted 8 lines
main-loop.h +0
-0@@ -323,6 +323,9 @@ void qemu_bh_delete(QEMUBH *bh); int qemu_add_child_watch(pid_t pid); #endif
Quoted 5 lines
main-loop.h void qemu_bh_delete(QEMUBH *bh); · line 327
Quoted 2 lines
main-loop.h +2
-0+void qemu_mutex_lock_cpu (void *_env); +void qemu_mutex_unlock_cpu (void *_env);Why void* instead of CPUArchState*?
Because we can hide the CPUArchState from apic.c
Thanks and regards,
pingfan
Quoted 1 line
Stefan