[PATCH v4 6/8] KVM-HV: Add flush_on_enter before guest enter

Nikunj A. Dadhania
2012-08-21T11:28:14Z

PV-Flush guest would indicate to flush on enter, flush the TLB before
entering and exiting the guest.

Signed-off-by: Nikunj A. Dadhania <nikunj@linux.vnet.ibm.com>
---
arch/x86/kvm/x86.c | 28 ++++++++++++----------------
1 files changed, 12 insertions(+), 16 deletions(-)

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 43f2c19..07fdb0f 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -1557,20 +1557,9 @@ static void record_steal_time(struct kvm_vcpu *vcpu)
&vcpu->arch.st.steal, sizeof(struct kvm_steal_time));
}

-static void kvm_set_atomic(u64 *addr, u64 old, u64 new)
-{
- int loop = 1000000;
- while (1) {
- if (cmpxchg(addr, old, new) == old)
- break;
- loop--;
- if (!loop) {
- pr_info("atomic cur: %lx old: %lx new: %lx\n",
- *addr, old, new);
- break;
- }
- }
-}
+#define VS_NOT_IN_GUEST (0)
+#define VS_IN_GUEST (1 << KVM_VCPU_STATE_IN_GUEST_MODE)
+#define VS_SHOULD_FLUSH (1 << KVM_VCPU_STATE_SHOULD_FLUSH)

static void kvm_set_vcpu_state(struct kvm_vcpu *vcpu)
{
@@ -1584,7 +1573,13 @@ static void kvm_set_vcpu_state(struct kvm_vcpu *vcpu)
kaddr = kmap_atomic(vcpu->arch.v_state.vs_page);
kaddr += vcpu->arch.v_state.vs_offset;
vs = kaddr;
- kvm_set_atomic(&vs->state, 0, 1 << KVM_VCPU_STATE_IN_GUEST_MODE);
+ if (xchg(&vs->state, VS_IN_GUEST) == VS_SHOULD_FLUSH) {
+ /*
+ * Do TLB_FLUSH before entering the guest, its passed
+ * the stage of request checking
+ */
+ kvm_x86_ops->tlb_flush(vcpu);
+ }
kunmap_atomic(kaddr);
}

@@ -1600,7 +1595,8 @@ static void kvm_clear_vcpu_state(struct kvm_vcpu *vcpu)
kaddr = kmap_atomic(vcpu->arch.v_state.vs_page);
kaddr += vcpu->arch.v_state.vs_offset;
vs = kaddr;
- kvm_set_atomic(&vs->state, 1 << KVM_VCPU_STATE_IN_GUEST_MODE, 0);
+ if (xchg(&vs->state, VS_NOT_IN_GUEST) == VS_SHOULD_FLUSH)
+ kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
kunmap_atomic(kaddr);
}