Skip to contentSkip to conversation

KVM: X86: Use the common definition - 'GPA_INVALID' for X86

PATCH In review Yu Zhang 2 participants 3 messages Dec 9, 2022 – Dec 13, 2022

Subsystems (4)
lore
Share & tools
lore

Yu Zhang

author
#0 lore raw

PATCH 1/1 KVM: X86: Use the common definition - 'GPA_INVALID' for X86

KVM already has a 'GPA_INVALID' defined as (~(gpa_t)0) in kvm_types.h, and it is used by ARM and X86 xen code. We do not need a replicated 'INVALID_GPA' for X86 specifically. Just replace it with the common one.

Tested by rebuilding KVM.

No functional change intended.

---
 arch/x86/include/asm/kvm_host.h |  2 -
 arch/x86/kvm/hyperv.c           |  2 +-
 arch/x86/kvm/mmu/paging_tmpl.h  |  6 +--
 arch/x86/kvm/svm/nested.c       |  4 +-
 arch/x86/kvm/svm/svm.c          |  4 +-
 arch/x86/kvm/vmx/nested.c       | 66 ++++++++++++++++-----------------
 arch/x86/kvm/vmx/sgx.c          |  2 +-
 arch/x86/kvm/vmx/vmx.c          |  6 +--
 arch/x86/kvm/x86.c              | 18 ++++-----
 9 files changed, 54 insertions(+), 56 deletions(-)
arch/x86/include/asm/kvm_host.h +0 -2
index d1013c4f673c..461c0b699a38 100644
@@ -134,8 +134,6 @@
 #define INVALID_PAGE (~(hpa_t)0)
 #define VALID_PAGE(x) ((x) != INVALID_PAGE)
 
-#define INVALID_GPA (~(gpa_t)0)
-
 /* KVM Hugepage definitions for x86 */
 #define KVM_MAX_HUGEPAGE_LEVEL	PG_LEVEL_1G
 #define KVM_NR_PAGE_SIZES	(KVM_MAX_HUGEPAGE_LEVEL - PG_LEVEL_4K + 1)
arch/x86/kvm/hyperv.c +1 -1
index 2c7f2a26421e..09282f0cf0cd 100644
@@ -1945,7 +1945,7 @@ static u64 kvm_hv_flush_tlb(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc)
 	 */
 	if (!hc->fast && is_guest_mode(vcpu)) {
 		hc->ingpa = translate_nested_gpa(vcpu, hc->ingpa, 0, NULL);
-		if (unlikely(hc->ingpa == INVALID_GPA))
+		if (unlikely(hc->ingpa == GPA_INVALID))
 			return HV_STATUS_INVALID_HYPERCALL_INPUT;
 	}
 
arch/x86/kvm/mmu/paging_tmpl.h +3 -3
index 0f6455072055..2709c135a798 100644
@@ -378,7 +378,7 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker,
 		 * information to fix the exit_qualification or exit_info_1
 		 * fields.
 		 */
-		if (unlikely(real_gpa == INVALID_GPA))
+		if (unlikely(real_gpa == GPA_INVALID))
 			return 0;
 
 		host_addr = kvm_vcpu_gfn_to_hva_prot(vcpu, gpa_to_gfn(real_gpa),
@@ -431,7 +431,7 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker,
 #endif
 
 	real_gpa = kvm_translate_gpa(vcpu, mmu, gfn_to_gpa(gfn), access, &walker->fault);
-	if (real_gpa == INVALID_GPA)
+	if (real_gpa == GPA_INVALID)
 		return 0;
 
 	walker->gfn = real_gpa >> PAGE_SHIFT;
@@ -962,7 +962,7 @@ static gpa_t FNAME(gva_to_gpa)(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
 			       struct x86_exception *exception)
 {
 	struct guest_walker walker;
-	gpa_t gpa = INVALID_GPA;
+	gpa_t gpa = GPA_INVALID;
 	int r;
 
 #ifndef CONFIG_X86_64
arch/x86/kvm/svm/nested.c +2 -2
index bc9cd7086fa9..5531263f6045 100644
@@ -1169,7 +1169,7 @@ void svm_free_nested(struct vcpu_svm *svm)
 	 *
 	 * When the vmcb02 is freed, this optimization becomes invalid.
 	 */
-	svm->nested.last_vmcb12_gpa = INVALID_GPA;
+	svm->nested.last_vmcb12_gpa = GPA_INVALID;
 
 	svm->nested.initialized = false;
 }
@@ -1180,7 +1180,7 @@ void svm_leave_nested(struct kvm_vcpu *vcpu)
 
 	if (is_guest_mode(vcpu)) {
 		svm->nested.nested_run_pending = 0;
-		svm->nested.vmcb12_gpa = INVALID_GPA;
+		svm->nested.vmcb12_gpa = GPA_INVALID;
 
 		leave_guest_mode(vcpu);
 
arch/x86/kvm/svm/svm.c +2 -2
index 91352d692845..4d88f5293968 100644
@@ -1275,8 +1275,8 @@ static void init_vmcb(struct kvm_vcpu *vcpu)
 	svm->current_vmcb->asid_generation = 0;
 	svm->asid = 0;
 
-	svm->nested.vmcb12_gpa = INVALID_GPA;
-	svm->nested.last_vmcb12_gpa = INVALID_GPA;
+	svm->nested.vmcb12_gpa = GPA_INVALID;
+	svm->nested.last_vmcb12_gpa = GPA_INVALID;
 
 	if (!kvm_pause_in_guest(vcpu->kvm)) {
 		control->pause_filter_count = pause_filter_count;
arch/x86/kvm/vmx/nested.c +33 -33

232 lines not on this page · raw message

arch/x86/kvm/vmx/sgx.c +1 -1
index 8f95c7c01433..7c5a9bbe2908 100644
@@ -79,7 +79,7 @@ static int sgx_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t gva, bool write,
 	else
 		*gpa = kvm_mmu_gva_to_gpa_read(vcpu, gva, &ex);
 
-	if (*gpa == INVALID_GPA) {
+	if (*gpa == GPA_INVALID) {
 		kvm_inject_emulated_page_fault(vcpu, &ex);
 		return -EFAULT;
 	}
arch/x86/kvm/vmx/vmx.c +3 -3
index cea8c07f5229..21109269b88a 100644
@@ -4593,7 +4593,7 @@ static void init_vmcs(struct vcpu_vmx *vmx)
 	if (cpu_has_vmx_msr_bitmap())
 		vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
 
-	vmcs_write64(VMCS_LINK_POINTER, INVALID_GPA); /* 22.3.1.5 */
+	vmcs_write64(VMCS_LINK_POINTER, GPA_INVALID); /* 22.3.1.5 */
 
 	/* Control */
 	pin_controls_set(vmx, vmx_pin_based_exec_ctrl(vmx));
@@ -4712,8 +4712,8 @@ static void __vmx_vcpu_reset(struct kvm_vcpu *vcpu)
 	vcpu_setup_sgx_lepubkeyhash(vcpu);
 
 	vmx->nested.posted_intr_nv = -1;
-	vmx->nested.vmxon_ptr = INVALID_GPA;
-	vmx->nested.current_vmptr = INVALID_GPA;
+	vmx->nested.vmxon_ptr = GPA_INVALID;
+	vmx->nested.current_vmptr = GPA_INVALID;
 	vmx->nested.hv_evmcs_vmptr = EVMPTR_INVALID;
 
 	vcpu->arch.microcode_version = 0x100000000ULL;
arch/x86/kvm/x86.c +9 -9
index 72ac6bf05c8b..fe984df5c602 100644
@@ -871,7 +871,7 @@ int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
 	 */
 	real_gpa = kvm_translate_gpa(vcpu, mmu, gfn_to_gpa(pdpt_gfn),
 				     PFERR_USER_MASK | PFERR_WRITE_MASK, NULL);
-	if (real_gpa == INVALID_GPA)
+	if (real_gpa == GPA_INVALID)
 		return 0;
 
 	/* Note the offset, PDPTRs are 32 byte aligned when using PAE paging. */
@@ -7200,7 +7200,7 @@ static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
 		unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
 		int ret;
 
-		if (gpa == INVALID_GPA)
+		if (gpa == GPA_INVALID)
 			return X86EMUL_PROPAGATE_FAULT;
 		ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, data,
 					       offset, toread);
@@ -7231,7 +7231,7 @@ static int kvm_fetch_guest_virt(struct x86_emulate_ctxt *ctxt,
 	/* Inline kvm_read_guest_virt_helper for speed.  */
 	gpa_t gpa = mmu->gva_to_gpa(vcpu, mmu, addr, access|PFERR_FETCH_MASK,
 				    exception);
-	if (unlikely(gpa == INVALID_GPA))
+	if (unlikely(gpa == GPA_INVALID))
 		return X86EMUL_PROPAGATE_FAULT;
 
 	offset = addr & (PAGE_SIZE-1);
@@ -7292,7 +7292,7 @@ static int kvm_write_guest_virt_helper(gva_t addr, void *val, unsigned int bytes
 		unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset);
 		int ret;
 
-		if (gpa == INVALID_GPA)
+		if (gpa == GPA_INVALID)
 			return X86EMUL_PROPAGATE_FAULT;
 		ret = kvm_vcpu_write_guest(vcpu, gpa, data, towrite);
 		if (ret < 0) {
@@ -7406,7 +7406,7 @@ static int vcpu_mmio_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
 
 	*gpa = mmu->gva_to_gpa(vcpu, mmu, gva, access, exception);
 
-	if (*gpa == INVALID_GPA)
+	if (*gpa == GPA_INVALID)
 		return -1;
 
 	return vcpu_is_mmio_gpa(vcpu, gva, *gpa, write);
@@ -7643,7 +7643,7 @@ static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt,
 
 	gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, NULL);
 
-	if (gpa == INVALID_GPA ||
+	if (gpa == GPA_INVALID ||
 	    (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
 		goto emul_write;
 
@@ -8439,7 +8439,7 @@ static bool reexecute_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
 		 * If the mapping is invalid in guest, let cpu retry
 		 * it to generate fault.
 		 */
-		if (gpa == INVALID_GPA)
+		if (gpa == GPA_INVALID)
 			return true;
 	}
 
@@ -11426,7 +11426,7 @@ int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
 	gpa = kvm_mmu_gva_to_gpa_system(vcpu, vaddr, NULL);
 	srcu_read_unlock(&vcpu->kvm->srcu, idx);
 	tr->physical_address = gpa;
-	tr->valid = gpa != INVALID_GPA;
+	tr->valid = gpa != GPA_INVALID;
 	tr->writeable = 1;
 	tr->usermode = 0;
 
@@ -13096,7 +13096,7 @@ void kvm_fixup_and_inject_pf_error(struct kvm_vcpu *vcpu, gva_t gva, u16 error_c
 		(PFERR_WRITE_MASK | PFERR_FETCH_MASK | PFERR_USER_MASK);
 
 	if (!(error_code & PFERR_PRESENT_MASK) ||
-	    mmu->gva_to_gpa(vcpu, mmu, gva, access, &fault) != INVALID_GPA) {
+	    mmu->gva_to_gpa(vcpu, mmu, gva, access, &fault) != GPA_INVALID) {
 		/*
 		 * If vcpu->arch.walk_mmu->gva_to_gpa succeeded, the page
 		 * tables probably do not match the TLB.  Just proceed
Signature · 1 line
2.25.1
#1 lore raw
Quoted 10 lines · Yu Zhang

KVM already has a 'GPA_INVALID' defined as (~(gpa_t)0) in kvm_types.h, and it is used by ARM and X86 xen code. We do not need a replicated 'INVALID_GPA' for X86 specifically. Just replace it with the common one.

Tested by rebuilding KVM.

No functional change intended.

Quoted 11 lines
---
 arch/x86/include/asm/kvm_host.h |  2 -
 arch/x86/kvm/hyperv.c           |  2 +-
 arch/x86/kvm/mmu/paging_tmpl.h  |  6 +--
 arch/x86/kvm/svm/nested.c       |  4 +-
 arch/x86/kvm/svm/svm.c          |  4 +-
 arch/x86/kvm/vmx/nested.c       | 66 ++++++++++++++++-----------------
 arch/x86/kvm/vmx/sgx.c          |  2 +-
 arch/x86/kvm/vmx/vmx.c          |  6 +--
 arch/x86/kvm/x86.c              | 18 ++++-----
 9 files changed, 54 insertions(+), 56 deletions(-)

What if we rename GPA_INVALID to INVALID_GPA and modify _those_ users? I have a slight preference for INVALID_GPA, and we also have INVALID_PAGE. It'll also yield a smaller diff.

EVMPTR_INVALID is the counter argument, but that's more of an error code than a semi-arbitrary value, e.g. there's also EVMPTR_MAP_PENDING.

$ git grep GPA_INVALID | wc -l
17
$ git grep INVALID_GPA | wc -l
55
author
#2 lore raw
Quoted 4 lines · Sean Christopherson

What if we rename GPA_INVALID to INVALID_GPA and modify _those_ users? I have a slight preference for INVALID_GPA, and we also have INVALID_PAGE. It'll also yield a smaller diff.

Quoted 8 lines

EVMPTR_INVALID is the counter argument, but that's more of an error code than a semi-arbitrary value, e.g. there's also EVMPTR_MAP_PENDING.

$ git grep GPA_INVALID | wc -l
17
$ git grep INVALID_GPA | wc -l
55

Good point. Thanks!

B.R.
Yu