Skip to contentSkip to conversation

KVM: VMX: Judge MMIO based on PFN rather than HVA in EPT violation

PATCH In review Sheng Yang 2 participants 4 messages Feb 10, 2009 – Feb 11, 2009

Subsystems (3)
lore
Share & tools
lore

Sheng Yang

author
#0 lore raw

PATCH 1/1 KVM: VMX: Judge MMIO based on PFN rather than HVA in EPT violation

One page can be unmapped from userspace, then HVA seems legal, but in fact, PFN is illegal.

---
 arch/x86/kvm/vmx.c |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
arch/x86/kvm/vmx.c +3 -3
index 9913a1d..a4fa1b5 100644
@@ -3061,7 +3061,7 @@ static int handle_ept_violation(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
 	u64 exit_qualification;
 	enum emulation_result er;
 	gpa_t gpa;
-	unsigned long hva;
+	pfn_t pfn;
 	int gla_validity;
 	int r;
 
@@ -3086,8 +3086,8 @@ static int handle_ept_violation(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
 	}
 
 	gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
-	hva = gfn_to_hva(vcpu->kvm, gpa >> PAGE_SHIFT);
-	if (!kvm_is_error_hva(hva)) {
+	pfn = gfn_to_pfn(vcpu->kvm, gpa >> PAGE_SHIFT);
+	if (!is_error_pfn(pfn)) {
 		r = kvm_mmu_page_fault(vcpu, gpa & PAGE_MASK, 0);
 		if (r < 0) {
 			printk(KERN_ERR "EPT: Not enough memory!\n");
Signature · 1 line
1.5.4.5
#1 lore raw

Hi Sheng,

Quoted 22 lines · Sheng Yang

One page can be unmapped from userspace, then HVA seems legal, but in fact, PFN is illegal.

---
 arch/x86/kvm/vmx.c |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
arch/x86/kvm/vmx.c +1 -1
index 9913a1d..a4fa1b5 100644
@@ -3061,7 +3061,7 @@ static int handle_ept_violation(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
 	u64 exit_qualification;
 	enum emulation_result er;
 	gpa_t gpa;
-	unsigned long hva;
+	pfn_t pfn;
 	int gla_validity;
 	int r;
 

arch/x86/kvm/vmx.c static int handle_ept_violation(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) · line 3093

Quoted 10 lines
arch/x86/kvm/vmx.c +2 -2
@@ -3086,8 +3086,8 @@ static int handle_ept_violation(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
 	}
 
 	gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
-	hva = gfn_to_hva(vcpu->kvm, gpa >> PAGE_SHIFT);
-	if (!kvm_is_error_hva(hva)) {
+	pfn = gfn_to_pfn(vcpu->kvm, gpa >> PAGE_SHIFT);
+	if (!is_error_pfn(pfn)) {
 		r = kvm_mmu_page_fault(vcpu, gpa & PAGE_MASK, 0);
 		if (r < 0) {
 			printk(KERN_ERR "EPT: Not enough memory!\n");

Forgot to drop the additional reference acquired by gfn_to_pfn ?

Hum, gfn_to_hva assumes the slot information is accurate, which is not the case if you simply munmap a page in the middle of a slot... we should provide a helper in userspace, and make sure nothing bad can happen otherwise.

Anyway, why don't you just let kvm_mmu_page_fault handle instruction emulation instead of opencoding it in handle_ept_violation ?

author
#2 lore raw
Quoted 27 lines · On Tuesday 10 February 2009 20:14:32 Marcelo Tosatti wrote:

Hi Sheng,

Quoted 24 lines · Sheng Yang

One page can be unmapped from userspace, then HVA seems legal, but in fact, PFN is illegal.

---
 arch/x86/kvm/vmx.c |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
arch/x86/kvm/vmx.c +1 -1
index 9913a1d..a4fa1b5 100644
@@ -3061,7 +3061,7 @@ static int handle_ept_violation(struct kvm_vcpu
 *vcpu, struct kvm_run *kvm_run) u64 exit_qualification;
 	enum emulation_result er;
 	gpa_t gpa;
-	unsigned long hva;
+	pfn_t pfn;
 	int gla_validity;
 	int r;
 
@@ -3086,8 +3086,8 @@ static int handle_ept_violation(struct kvm_vcpu
 *vcpu, struct kvm_run *kvm_run) }
 
Quoted 10 lines

arch/x86/kvm/vmx.c static int handle_ept_violation(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) · line 3093

Quoted 8 lines
arch/x86/kvm/vmx.c +2 -2
 	gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
-	hva = gfn_to_hva(vcpu->kvm, gpa >> PAGE_SHIFT);
-	if (!kvm_is_error_hva(hva)) {
+	pfn = gfn_to_pfn(vcpu->kvm, gpa >> PAGE_SHIFT);
+	if (!is_error_pfn(pfn)) {
 		r = kvm_mmu_page_fault(vcpu, gpa & PAGE_MASK, 0);
 		if (r < 0) {
 			printk(KERN_ERR "EPT: Not enough memory!\n");

Forgot to drop the additional reference acquired by gfn_to_pfn ?

Sadly, yeah... :(

Quoted 5 lines

Hum, gfn_to_hva assumes the slot information is accurate, which is not the case if you simply munmap a page in the middle of a slot... we should provide a helper in userspace, and make sure nothing bad can happen otherwise.

Yes. But maintain a list in kernel seems a little complex for it. I checked the callings of gfn_to_hva, seems most of them are safe with a inaccurate result(not sure about host_largepage_backed, seems this would be affected if one 4k page was unmapped inside a large page).

Quoted 3 lines

Anyway, why don't you just let kvm_mmu_page_fault handle instruction emulation instead of opencoding it in handle_ept_violation ?

It didn't work as expect. Checking it.

Signature · 2 lines
regards
Yang, Sheng
author
#3 lore raw
Quoted 51 lines · On Wednesday 11 February 2009 10:43:43 Sheng Yang wrote:
Quoted 27 lines · On Tuesday 10 February 2009 20:14:32 Marcelo Tosatti wrote:

Hi Sheng,

Quoted 24 lines · Sheng Yang

One page can be unmapped from userspace, then HVA seems legal, but in fact, PFN is illegal.

---
 arch/x86/kvm/vmx.c |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
arch/x86/kvm/vmx.c +1 -1
index 9913a1d..a4fa1b5 100644
@@ -3061,7 +3061,7 @@ static int handle_ept_violation(struct kvm_vcpu
 *vcpu, struct kvm_run *kvm_run) u64 exit_qualification;
 	enum emulation_result er;
 	gpa_t gpa;
-	unsigned long hva;
+	pfn_t pfn;
 	int gla_validity;
 	int r;
 
@@ -3086,8 +3086,8 @@ static int handle_ept_violation(struct kvm_vcpu
 *vcpu, struct kvm_run *kvm_run) }
 
Quoted 10 lines

arch/x86/kvm/vmx.c static int handle_ept_violation(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) · line 3093

Quoted 8 lines
arch/x86/kvm/vmx.c +2 -2
 	gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
-	hva = gfn_to_hva(vcpu->kvm, gpa >> PAGE_SHIFT);
-	if (!kvm_is_error_hva(hva)) {
+	pfn = gfn_to_pfn(vcpu->kvm, gpa >> PAGE_SHIFT);
+	if (!is_error_pfn(pfn)) {
 		r = kvm_mmu_page_fault(vcpu, gpa & PAGE_MASK, 0);
 		if (r < 0) {
 			printk(KERN_ERR "EPT: Not enough memory!\n");

Forgot to drop the additional reference acquired by gfn_to_pfn ?

Sadly, yeah... :(

Quoted 4 lines

Hum, gfn_to_hva assumes the slot information is accurate, which is not the case if you simply munmap a page in the middle of a slot... we should provide a helper in userspace, and make sure nothing bad can happen otherwise.

Yes. But maintain a list in kernel seems a little complex for it. I checked the callings of gfn_to_hva, seems most of them are safe with a inaccurate result(not sure about host_largepage_backed, seems this would be affected if one 4k page was unmapped inside a large page).

Quoted 4 lines
Quoted 2 lines

Anyway, why don't you just let kvm_mmu_page_fault handle instruction emulation instead of opencoding it in handle_ept_violation ?

It didn't work as expect. Checking it.

Oh, seems it didn't work due to my fault... Update the patch, thanks for reminder. :)

Signature · 2 lines
regards
Yang, Sheng