KVM: add spinlock optimization framework
authorLongpeng(Mike) <longpeng2@huawei.com>
Tue, 8 Aug 2017 04:05:32 +0000 (12:05 +0800)
committerPaolo Bonzini <pbonzini@redhat.com>
Tue, 8 Aug 2017 08:57:43 +0000 (10:57 +0200)
If a vcpu exits due to request a user mode spinlock, then
the spinlock-holder may be preempted in user mode or kernel mode.
(Note that not all architectures trap spin loops in user mode,
only AMD x86 and ARM/ARM64 currently do).

But if a vcpu exits in kernel mode, then the holder must be
preempted in kernel mode, so we should choose a vcpu in kernel mode
as a more likely candidate for the lock holder.

This introduces kvm_arch_vcpu_in_kernel() to decide whether the
vcpu is in kernel-mode when it's preempted.  kvm_vcpu_on_spin's
new argument says the same of the spinning VCPU.

Signed-off-by: Longpeng(Mike) <longpeng2@huawei.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
13 files changed:
arch/arm/kvm/handle_exit.c
arch/arm64/kvm/handle_exit.c
arch/mips/kvm/mips.c
arch/powerpc/kvm/powerpc.c
arch/s390/kvm/diag.c
arch/s390/kvm/kvm-s390.c
arch/x86/kvm/hyperv.c
arch/x86/kvm/svm.c
arch/x86/kvm/vmx.c
arch/x86/kvm/x86.c
include/linux/kvm_host.h
virt/kvm/arm/arm.c
virt/kvm/kvm_main.c

index 54442e3753542e342d8ba8b2c227ec4f31910175..196122bb696827ffc55c7be116f2183b24b63529 100644 (file)
@@ -67,7 +67,7 @@ static int kvm_handle_wfx(struct kvm_vcpu *vcpu, struct kvm_run *run)
        if (kvm_vcpu_get_hsr(vcpu) & HSR_WFI_IS_WFE) {
                trace_kvm_wfx(*vcpu_pc(vcpu), true);
                vcpu->stat.wfe_exit_stat++;
-               kvm_vcpu_on_spin(vcpu);
+               kvm_vcpu_on_spin(vcpu, false);
        } else {
                trace_kvm_wfx(*vcpu_pc(vcpu), false);
                vcpu->stat.wfi_exit_stat++;
index 17d8a1677a0b34baf4356bf09b0a809cf0a8e58d..da57622cacca882103640cbcb6f1472ce0f6761c 100644 (file)
@@ -84,7 +84,7 @@ static int kvm_handle_wfx(struct kvm_vcpu *vcpu, struct kvm_run *run)
        if (kvm_vcpu_get_hsr(vcpu) & ESR_ELx_WFx_ISS_WFE) {
                trace_kvm_wfx_arm64(*vcpu_pc(vcpu), true);
                vcpu->stat.wfe_exit_stat++;
-               kvm_vcpu_on_spin(vcpu);
+               kvm_vcpu_on_spin(vcpu, false);
        } else {
                trace_kvm_wfx_arm64(*vcpu_pc(vcpu), false);
                vcpu->stat.wfi_exit_stat++;
index d4b2ad18eef2023d23701b0dd5a320d2370fee5d..bce2a6431430366ee626ddc4f6282665caeb7f06 100644 (file)
@@ -98,6 +98,11 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
        return !!(vcpu->arch.pending_exceptions);
 }
 
+bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
+{
+       return false;
+}
+
 int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
 {
        return 1;
index 1a75c0b5f4ca8fba049f572e829d46c0cd40329d..3480faaf1ef886118ba1ee26389ea25c4c3d8a69 100644 (file)
@@ -58,6 +58,11 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
        return !!(v->arch.pending_exceptions) || kvm_request_pending(v);
 }
 
+bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
+{
+       return false;
+}
+
 int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
 {
        return 1;
index ce865bd4f81d9696e7c9907d7bcaedede8f7cf43..6182edebea3db9477bb67219738e576f3e46688a 100644 (file)
@@ -150,7 +150,7 @@ static int __diag_time_slice_end(struct kvm_vcpu *vcpu)
 {
        VCPU_EVENT(vcpu, 5, "%s", "diag time slice end");
        vcpu->stat.diagnose_44++;
-       kvm_vcpu_on_spin(vcpu);
+       kvm_vcpu_on_spin(vcpu, false);
        return 0;
 }
 
index af09d3437631d348dca2f1a6699c34ed49c624ed..5e07322034b9f7a016bc56842ece38f30d9d5863 100644 (file)
@@ -2447,6 +2447,11 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
        return kvm_s390_vcpu_has_irq(vcpu, 0);
 }
 
+bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
+{
+       return false;
+}
+
 void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu)
 {
        atomic_or(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
index bf9992300efaa550bb70aa2eef8253d39c0610ca..5243d54f73abaeeda43db011a1e5dbb498431809 100644 (file)
@@ -1274,7 +1274,7 @@ int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
 
        switch (code) {
        case HVCALL_NOTIFY_LONG_SPIN_WAIT:
-               kvm_vcpu_on_spin(vcpu);
+               kvm_vcpu_on_spin(vcpu, false);
                break;
        case HVCALL_POST_MESSAGE:
        case HVCALL_SIGNAL_EVENT:
index 2432bb952a3073bcebe8c9b06c15dc16626102d2..0cc486fd98713626dea2e993832b41b1bcc34663 100644 (file)
@@ -3749,7 +3749,7 @@ static int interrupt_window_interception(struct vcpu_svm *svm)
 
 static int pause_interception(struct vcpu_svm *svm)
 {
-       kvm_vcpu_on_spin(&(svm->vcpu));
+       kvm_vcpu_on_spin(&svm->vcpu, false);
        return 1;
 }
 
index 2c0f5287fb782d4f842911189c6778b819754371..fef784c22190bd61ef422db58f77a0b627dde85d 100644 (file)
@@ -6781,7 +6781,7 @@ static int handle_pause(struct kvm_vcpu *vcpu)
        if (ple_gap)
                grow_ple_window(vcpu);
 
-       kvm_vcpu_on_spin(vcpu);
+       kvm_vcpu_on_spin(vcpu, false);
        return kvm_skip_emulated_instruction(vcpu);
 }
 
index 33fd6b6419ef54af4d1ea70a03789f6c552a9613..6125e1743b69580c1e244653fcf805379905d6b0 100644 (file)
@@ -8432,6 +8432,11 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
        return kvm_vcpu_running(vcpu) || kvm_vcpu_has_events(vcpu);
 }
 
+bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
+{
+       return false;
+}
+
 int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
 {
        return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
index 28112d7917c15f46ce48a33a11fb688939795159..6882538eda329fb4a59c4bb1b2957003f6fdb9e9 100644 (file)
@@ -720,7 +720,7 @@ void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu);
 bool kvm_vcpu_wake_up(struct kvm_vcpu *vcpu);
 void kvm_vcpu_kick(struct kvm_vcpu *vcpu);
 int kvm_vcpu_yield_to(struct kvm_vcpu *target);
-void kvm_vcpu_on_spin(struct kvm_vcpu *vcpu);
+void kvm_vcpu_on_spin(struct kvm_vcpu *vcpu, bool usermode_vcpu_not_eligible);
 void kvm_load_guest_fpu(struct kvm_vcpu *vcpu);
 void kvm_put_guest_fpu(struct kvm_vcpu *vcpu);
 
@@ -800,6 +800,7 @@ int kvm_arch_hardware_setup(void);
 void kvm_arch_hardware_unsetup(void);
 void kvm_arch_check_processor_compat(void *rtn);
 int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu);
+bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu);
 int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu);
 
 #ifndef __KVM_HAVE_ARCH_VM_ALLOC
index a39a1e161e63d53f5e199189c0427a733b2e9544..862f820d06d4bfd3cd2b1ea5b176f65ca960079a 100644 (file)
@@ -416,6 +416,11 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
                && !v->arch.power_off && !v->arch.pause);
 }
 
+bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
+{
+       return false;
+}
+
 /* Just ensure a guest exit from a particular CPU */
 static void exit_vm_noop(void *info)
 {
index 15252d723b54e196d864d29a7aac2040686dc59a..e17c40d986f3c274aec2dfabd0ceb9ac87109f21 100644 (file)
@@ -2317,7 +2317,7 @@ static bool kvm_vcpu_eligible_for_directed_yield(struct kvm_vcpu *vcpu)
 #endif
 }
 
-void kvm_vcpu_on_spin(struct kvm_vcpu *me)
+void kvm_vcpu_on_spin(struct kvm_vcpu *me, bool yield_to_kernel_mode)
 {
        struct kvm *kvm = me->kvm;
        struct kvm_vcpu *vcpu;
@@ -2348,6 +2348,8 @@ void kvm_vcpu_on_spin(struct kvm_vcpu *me)
                                continue;
                        if (swait_active(&vcpu->wq) && !kvm_arch_vcpu_runnable(vcpu))
                                continue;
+                       if (yield_to_kernel_mode && !kvm_arch_vcpu_in_kernel(vcpu))
+                               continue;
                        if (!kvm_vcpu_eligible_for_directed_yield(vcpu))
                                continue;