Skip to contentSkip to conversation

s390x/kvm: avoid synchronize_rcu's in kernel

PATCH In review Jens Freimann 2 participants 7 messages Aug 28, 2014

lore
Share & tools
lore
#0 lore raw

cover s390x/kvm: avoid synchronize_rcu's in kernel

Whenever we call an ioctl from a wrong vcpu thread, the next ioctl will be painfully slow because a synchronize_rcu thread has to be performed, therefore involving all vcpu.

This patch series forces most ioctls to run on the associated vcpu. It speeds up all start/restart/reset operations involving cpus drastically.

David Hildenbrand (5):
kvm: run cpu state synchronization on target vcpu thread s390x/kvm: run guest triggered resets on the target vcpu thread s390x/kvm: execute sigp orders on the target vcpu thread s390x/kvm: execute "system reset" cpu resets on the vcpu thread s390x/kvm: execute the first cpu reset on the vcpu thread

 kvm-all.c                  | 18 ++++++++++++++++--
 target-s390x/cpu.c         |  6 +++++-
 target-s390x/cpu.h         | 15 +++++++++++++++
 target-s390x/kvm.c         | 23 ++++++++++++++++-------
 target-s390x/misc_helper.c | 30 ++++++++----------------------
 5 files changed, 60 insertions(+), 32 deletions(-)
Signature · 1 line
1.9.3
#2 lore raw

PATCH 2/5 s390x/kvm: run guest triggered resets on the target vcpu thread

From: David Hildenbrand <[email protected]>

Currently, load_normal_reset() and modified_clear_reset() as triggered by a guest vcpu will initiate cpu resets on the current vcpu thread for all cpus. The reset should happen on the individual vcpu thread instead, so let's use run_on_cpu() for this.

This avoids calls to synchronize_rcu() in the kernel.

---
 target-s390x/cpu.h         | 15 +++++++++++++++
 target-s390x/misc_helper.c | 30 ++++++++----------------------
 2 files changed, 23 insertions(+), 22 deletions(-)
target-s390x/cpu.h +15 -0
index b13761d..17a3df4 100644
@@ -353,6 +353,21 @@ static inline hwaddr decode_basedisp_s(CPUS390XState *env, uint32_t ipb)
 /* Base/displacement are at the same locations. */
 #define decode_basedisp_rs decode_basedisp_s
 
+/* helper functions for run_on_cpu() */
+static inline void s390_do_cpu_reset(void *arg)
+{
+    CPUState *cs = arg;
+    S390CPUClass *scc = S390_CPU_GET_CLASS(cs);
+
+    scc->cpu_reset(cs);
+}
+static inline void s390_do_cpu_full_reset(void *arg)
+{
+    CPUState *cs = arg;
+
+    cpu_reset(cs);
+}
+
 void s390x_tod_timer(void *opaque);
 void s390x_cpu_timer(void *opaque);
 
target-s390x/misc_helper.c +8 -22
index 0b62582..ef9758a 100644
@@ -114,33 +114,16 @@ uint32_t HELPER(servc)(CPUS390XState *env, uint64_t r1, uint64_t r2)
 }
 
 #ifndef CONFIG_USER_ONLY
-static void cpu_reset_all(void)
-{
-    CPUState *cs;
-    S390CPUClass *scc;
-
-    CPU_FOREACH(cs) {
-        scc = S390_CPU_GET_CLASS(cs);
-        scc->cpu_reset(cs);
-    }
-}
-
-static void cpu_full_reset_all(void)
-{
-    CPUState *cpu;
-
-    CPU_FOREACH(cpu) {
-        cpu_reset(cpu);
-    }
-}
-
 static int modified_clear_reset(S390CPU *cpu)
 {
     S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
+    CPUState *t;
 
     pause_all_vcpus();
     cpu_synchronize_all_states();
-    cpu_full_reset_all();
+    CPU_FOREACH(t) {
+        run_on_cpu(t, s390_do_cpu_full_reset, t);
+    }
     cmma_reset(cpu);
     io_subsystem_reset();
     scc->load_normal(CPU(cpu));
@@ -152,10 +135,13 @@ static int modified_clear_reset(S390CPU *cpu)
 static int load_normal_reset(S390CPU *cpu)
 {
     S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
+    CPUState *t;
 
     pause_all_vcpus();
     cpu_synchronize_all_states();
-    cpu_reset_all();
+    CPU_FOREACH(t) {
+        run_on_cpu(t, s390_do_cpu_reset, t);
+    }
     cmma_reset(cpu);
     io_subsystem_reset();
     scc->initial_cpu_reset(CPU(cpu));
Signature · 1 line
1.9.3
#3 lore raw

PATCH 3/5 s390x/kvm: execute sigp orders on the target vcpu thread

In reply to Jens Freimann

From: David Hildenbrand <[email protected]>

All sigp orders that can result in ioctls on the target vcpu should be executed on the associated vcpu thread.

---
 target-s390x/kvm.c | 23 ++++++++++++++++-------
 1 file changed, 16 insertions(+), 7 deletions(-)
target-s390x/kvm.c +16 -7
index a32d91a..421ff43 100644
@@ -916,23 +916,30 @@ static int handle_diag(S390CPU *cpu, struct kvm_run *run, uint32_t ipb)
     return r;
 }
 
-static int kvm_s390_cpu_start(S390CPU *cpu)
+static void sigp_cpu_start(void *arg)
 {
+    CPUState *cs = arg;
+    S390CPU *cpu = S390_CPU(cs);
+
     s390_add_running_cpu(cpu);
-    qemu_cpu_kick(CPU(cpu));
     DPRINTF("DONE: KVM cpu start: %p\n", &cpu->env);
-    return 0;
 }
 
-int kvm_s390_cpu_restart(S390CPU *cpu)
+static void sigp_cpu_restart(void *arg)
 {
+    CPUState *cs = arg;
+    S390CPU *cpu = S390_CPU(cs);
     struct kvm_s390_irq irq = {
         .type = KVM_S390_RESTART,
     };
 
     kvm_s390_vcpu_interrupt(cpu, &irq);
     s390_add_running_cpu(cpu);
-    qemu_cpu_kick(CPU(cpu));
+}
+
+int kvm_s390_cpu_restart(S390CPU *cpu)
+{
+    run_on_cpu(CPU(cpu), sigp_cpu_restart, CPU(cpu));
     DPRINTF("DONE: KVM cpu restart: %p\n", &cpu->env);
     return 0;
 }
@@ -980,10 +987,12 @@ static int handle_sigp(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
 
     switch (order_code) {
     case SIGP_START:
-        cc = kvm_s390_cpu_start(target_cpu);
+        run_on_cpu(CPU(target_cpu), sigp_cpu_start, CPU(target_cpu));
+        cc = 0;
         break;
     case SIGP_RESTART:
-        cc = kvm_s390_cpu_restart(target_cpu);
+        run_on_cpu(CPU(target_cpu), sigp_cpu_restart, CPU(target_cpu));
+        cc = 0;
         break;
     case SIGP_SET_ARCH:
         *statusreg &= 0xffffffff00000000UL;
Signature · 1 line
1.9.3
#4 lore raw

PATCH 4/5 s390x/kvm: execute "system reset" cpu resets on the vcpu thread

In reply to Jens Freimann

From: David Hildenbrand <[email protected]>

Let's execute resets triggered by qemu system resets on the target vcpu thread. This will avoid synchronize_rcu's in the kernel.

---
 target-s390x/cpu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target-s390x/cpu.c +1 -1
index c3082b7..4633282 100644
@@ -165,7 +165,7 @@ static void s390_cpu_machine_reset_cb(void *opaque)
 {
     S390CPU *cpu = opaque;
 
-    cpu_reset(CPU(cpu));
+    run_on_cpu(CPU(cpu), s390_do_cpu_full_reset, CPU(cpu));
 }
 #endif
 
Signature · 1 line
1.9.3
#5 lore raw

PATCH 5/5 s390x/kvm: execute the first cpu reset on the vcpu thread

In reply to Jens Freimann

From: David Hildenbrand <[email protected]>

As all full cpu resets currently call into the kernel to do initial cpu reset, let's run this reset (triggered by cpu_s390x_init()) on the proper vcpu thread.

---
 target-s390x/cpu.c | 4 ++++
 1 file changed, 4 insertions(+)
target-s390x/cpu.c +4 -0
index 4633282..505a2fa 100644
@@ -175,7 +175,11 @@ static void s390_cpu_realizefn(DeviceState *dev, Error **errp)
     S390CPUClass *scc = S390_CPU_GET_CLASS(dev);
 
     qemu_init_vcpu(cs);
+#if !defined(CONFIG_USER_ONLY)
+    run_on_cpu(cs, s390_do_cpu_full_reset, cs);
+#else
     cpu_reset(cs);
+#endif
 
     scc->parent_realize(dev, errp);
 }
Signature · 1 line
1.9.3
#1 lore raw

PATCH 1/5 kvm: run cpu state synchronization on target vcpu thread

In reply to Jens Freimann

From: David Hildenbrand <[email protected]>

As already done for kvm_cpu_synchronize_state(), let's trigger kvm_arch_put_registers() via run_on_cpu() for kvm_cpu_synchronize_post_reset() and kvm_cpu_synchronize_post_init().

This way, we make sure that the register synchronizing ioctls are called from the proper vcpu thread; this avoids calls to synchronize_rcu() in the kernel.

---
 kvm-all.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
kvm-all.c +16 -2
index 1402f4f..b240bf8 100644
@@ -1669,18 +1669,32 @@ void kvm_cpu_synchronize_state(CPUState *cpu)
     }
 }
 
-void kvm_cpu_synchronize_post_reset(CPUState *cpu)
+static void do_kvm_cpu_synchronize_post_reset(void *arg)
 {
+    CPUState *cpu = arg;
+
     kvm_arch_put_registers(cpu, KVM_PUT_RESET_STATE);
     cpu->kvm_vcpu_dirty = false;
 }
 
-void kvm_cpu_synchronize_post_init(CPUState *cpu)
+void kvm_cpu_synchronize_post_reset(CPUState *cpu)
+{
+    run_on_cpu(cpu, do_kvm_cpu_synchronize_post_reset, cpu);
+}
+
+static void do_kvm_cpu_synchronize_post_init(void *arg)
 {
+    CPUState *cpu = arg;
+
     kvm_arch_put_registers(cpu, KVM_PUT_FULL_STATE);
     cpu->kvm_vcpu_dirty = false;
 }
 
+void kvm_cpu_synchronize_post_init(CPUState *cpu)
+{
+    run_on_cpu(cpu, do_kvm_cpu_synchronize_post_init, cpu);
+}
+
 int kvm_cpu_exec(CPUState *cpu)
 {
     struct kvm_run *run = cpu->kvm_run;
Signature · 1 line
1.9.3
#6 lore raw
In reply to Jens Freimann
Quoted 15 lines · Jens Freimann

Whenever we call an ioctl from a wrong vcpu thread, the next ioctl will be painfully slow because a synchronize_rcu thread has to be performed, therefore involving all vcpu.

This patch series forces most ioctls to run on the associated vcpu. It speeds up all start/restart/reset operations involving cpus drastically.

David Hildenbrand (5):
kvm: run cpu state synchronization on target vcpu thread s390x/kvm: run guest triggered resets on the target vcpu thread s390x/kvm: execute sigp orders on the target vcpu thread s390x/kvm: execute "system reset" cpu resets on the vcpu thread s390x/kvm: execute the first cpu reset on the vcpu thread

Quoted 7 lines
 kvm-all.c                  | 18 ++++++++++++++++--
 target-s390x/cpu.c         |  6 +++++-
 target-s390x/cpu.h         | 15 +++++++++++++++
 target-s390x/kvm.c         | 23 ++++++++++++++++-------
 target-s390x/misc_helper.c | 30 ++++++++----------------------
 5 files changed, 60 insertions(+), 32 deletions(-)

Thanks.

Applied 2-5 to https://github.com/borntraeger/qemu.git s390-next. Patch 1 will be taken care of in Paolos kvm tree.

I will wait some more days before sending the pull request to give others a chance for feedback.

Christian