From: Xiao Guangrong <xiaoguangrong.eric@gmail.com>
To: Gleb Natapov <gleb@redhat.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>,
Marcelo Tosatti <mtosatti@redhat.com>,
LKML <linux-kernel@vger.kernel.org>, KVM <kvm@vger.kernel.org>
Subject: [PATCH] KVM: x86: fix missed memory synchronization when patch hypercall
Date: Sat, 08 Jun 2013 11:15:37 +0800 [thread overview]
Message-ID: <51B2A1D9.6060306@gmail.com> (raw)
From: Xiao Guangrong <xiaoguangrong@linux.vnet.ibm.com>
Currently, memory synchronization is missed in emulator_fix_hypercall,
please see the commit 758ccc89b83
(KVM: x86: drop calling kvm_mmu_zap_all in emulator_fix_hypercall)
This patch fixes it by introducing kvm_vcpus_hang_on_page_start() and
kvm_vcpus_hang_on_page_end which unmap the patched page from guest
and use kvm_flush_remote_tlbs() as the serializing instruction to
ensure the memory coherence
[ The SDM said that INVEPT, INVVPID and MOV (to control register, with
the exception of MOV CR8) are the serializing instructions. ]
The mmu-lock is held during host patches the page so that it stops vcpus
to fix its further page fault
Signed-off-by: Xiao Guangrong <xiaoguangrong@linux.vnet.ibm.com>
---
arch/x86/kvm/mmu.c | 25 +++++++++++++++++++++++++
arch/x86/kvm/mmu.h | 3 +++
arch/x86/kvm/x86.c | 7 +++++++
3 files changed, 35 insertions(+)
diff --git a/arch/x86/kvm/mmu.c b/arch/x86/kvm/mmu.c
index 7d50a2d..35cd0b6 100644
--- a/arch/x86/kvm/mmu.c
+++ b/arch/x86/kvm/mmu.c
@@ -4536,6 +4536,31 @@ int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4])
}
EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy);
+/*
+ * Force vcpu to hang when it is trying to access the specified page.
+ *
+ * kvm_vcpus_hang_on_page_start and kvm_vcpus_hang_on_page_end should
+ * be used in pairs and they are currently used to sync memory access
+ * between vcpus when host cross-modifies the code segment of guest.
+ *
+ * We unmap the page from the guest and do memory synchronization by
+ * kvm_flush_remote_tlbs() under the protection of mmu-lock. If vcpu
+ * accesses the page, it will trigger #PF and be blocked on mmu-lock.
+ */
+void kvm_vcpus_hang_on_page_start(struct kvm *kvm, gfn_t gfn)
+{
+ spin_lock(&kvm->mmu_lock);
+
+ /* kvm_flush_remote_tlbs() can act as serializing instruction. */
+ if (kvm_unmap_hva(kvm, gfn_to_hva(kvm, gfn)))
+ kvm_flush_remote_tlbs(kvm);
+}
+
+void kvm_vcpus_hang_on_page_end(struct kvm *kvm)
+{
+ spin_unlock(&kvm->mmu_lock);
+}
+
void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
{
ASSERT(vcpu);
diff --git a/arch/x86/kvm/mmu.h b/arch/x86/kvm/mmu.h
index 5b59c57..35910be 100644
--- a/arch/x86/kvm/mmu.h
+++ b/arch/x86/kvm/mmu.h
@@ -115,4 +115,7 @@ static inline bool permission_fault(struct kvm_mmu *mmu, unsigned pte_access,
}
void kvm_mmu_invalidate_zap_all_pages(struct kvm *kvm);
+
+void kvm_vcpus_hang_on_page_start(struct kvm *kvm, gfn_t gfn);
+void kvm_vcpus_hang_on_page_end(struct kvm *kvm);
#endif
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 9e4afa7..776bf1a 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -5528,8 +5528,15 @@ static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt)
struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
char instruction[3];
unsigned long rip = kvm_rip_read(vcpu);
+ gpa_t gpa;
+
+ gpa = kvm_mmu_gva_to_gpa_fetch(vcpu, rip, NULL);
+ if (gpa == UNMAPPED_GVA)
+ return X86EMUL_PROPAGATE_FAULT;
+ kvm_vcpus_hang_on_page_start(vcpu->kvm, gpa_to_gfn(gpa));
kvm_x86_ops->patch_hypercall(vcpu, instruction);
+ kvm_vcpus_hang_on_page_end(vcpu->kvm);
return emulator_write_emulated(ctxt, rip, instruction, 3, NULL);
}
--
1.8.1.4
next reply other threads:[~2013-06-08 3:15 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-06-08 3:15 Xiao Guangrong [this message]
2013-06-09 8:45 ` [PATCH] KVM: x86: fix missed memory synchronization when patch hypercall Gleb Natapov
2013-06-09 8:56 ` Xiao Guangrong
2013-06-09 8:59 ` Gleb Natapov
2013-06-09 9:08 ` Xiao Guangrong
2013-06-09 9:29 ` Xiao Guangrong
2013-06-09 9:39 ` Gleb Natapov
2013-06-09 10:01 ` Xiao Guangrong
2013-06-09 10:19 ` Gleb Natapov
2013-06-09 11:25 ` Xiao Guangrong
2013-06-09 11:36 ` Gleb Natapov
2013-06-09 11:44 ` Xiao Guangrong
2013-06-09 11:56 ` Gleb Natapov
2013-06-09 12:17 ` Xiao Guangrong
2013-06-09 12:27 ` Gleb Natapov
2013-06-09 12:52 ` Xiao Guangrong
2013-06-18 14:13 ` Paolo Bonzini
2013-06-18 15:22 ` Gleb Natapov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=51B2A1D9.6060306@gmail.com \
--to=xiaoguangrong.eric@gmail.com \
--cc=gleb@redhat.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mtosatti@redhat.com \
--cc=pbonzini@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.