From: Avi Kivity <a...@redhat.com>

KVM_REQ_KICK poisons vcpu->requests by having a bit set during normal
operation.  This causes the fast path check for a clear vcpu->requests
to fail all the time, triggering tons of atomic operations.

Fix by replacing KVM_REQ_KICK with a vcpu->guest_mode atomic.

Signed-off-by: Avi Kivity <a...@redhat.com>

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index cb301a3..11adf97 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -4559,13 +4559,15 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
        if (vcpu->fpu_active)
                kvm_load_guest_fpu(vcpu);
 
-       local_irq_disable();
+       atomic_set(&vcpu->guest_mode, 1);
+       smp_wmb();
 
-       clear_bit(KVM_REQ_KICK, &vcpu->requests);
-       smp_mb__after_clear_bit();
+       local_irq_disable();
 
-       if (vcpu->requests || need_resched() || signal_pending(current)) {
-               set_bit(KVM_REQ_KICK, &vcpu->requests);
+       if (!atomic_read(&vcpu->guest_mode) || vcpu->requests
+           || need_resched() || signal_pending(current)) {
+               atomic_set(&vcpu->guest_mode, 0);
+               smp_wmb();
                local_irq_enable();
                preempt_enable();
                r = 1;
@@ -4610,7 +4612,8 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
        if (hw_breakpoint_active())
                hw_breakpoint_restore();
 
-       set_bit(KVM_REQ_KICK, &vcpu->requests);
+       atomic_set(&vcpu->guest_mode, 0);
+       smp_wmb();
        local_irq_enable();
 
        ++vcpu->stat.exits;
@@ -5535,7 +5538,7 @@ void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
 
        me = get_cpu();
        if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu))
-               if (!test_and_set_bit(KVM_REQ_KICK, &vcpu->requests))
+               if (atomic_xchg(&vcpu->guest_mode, 0))
                        smp_send_reschedule(cpu);
        put_cpu();
 }
diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index 7cb116a..4e8fdbf 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -81,6 +81,7 @@ struct kvm_vcpu {
        int vcpu_id;
        struct mutex mutex;
        int   cpu;
+       atomic_t guest_mode;
        struct kvm_run *run;
        unsigned long requests;
        unsigned long guest_debug;
--
To unsubscribe from this list: send the line "unsubscribe kvm-commits" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to