[4/5] KVM: nVMX: Skip Host State Area vmentry checks that are necessary only if VMCS12 is dirty
diff mbox series

Message ID 20190707071147.11651-5-krish.sadhukhan@oracle.com
State New
Headers show
Series
  • KVM: nVMX: Skip vmentry checks that are necessary only if VMCS12 is dirty
Related show

Commit Message

Krish Sadhukhan July 7, 2019, 7:11 a.m. UTC
..so that every nested vmentry is not slowed down by those checks.

Signed-off-by: Krish Sadhukhan <krish.sadhukhan@oracle.com>
---
 arch/x86/kvm/vmx/nested.c | 48 ++++++++++++++++++++++++++++-----------
 1 file changed, 35 insertions(+), 13 deletions(-)

Comments

Paolo Bonzini July 10, 2019, 4:26 p.m. UTC | #1
On 07/07/19 09:11, Krish Sadhukhan wrote:
> +				       struct vmcs12 *vmcs12)
> +{
> +	struct vcpu_vmx *vmx = to_vmx(vcpu);
> +
> +	if ((vmx->nested.dirty_vmcs12) &&
> +	    nested_vmx_check_host_state_full(vcpu, vmcs12))
> +		return -EINVAL;
> +
> +	if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0))
> +		return -EINVAL;
> +
> +	if (is_noncanonical_address(vmcs12->host_ia32_sysenter_esp, vcpu))
> +		return -EINVAL;

These two are not part of the shadowed state, so they can be done only
in the "rare" case.

> +	if (vmcs12->host_fs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK) ||
> +	    vmcs12->host_gs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK))
> +		return -EINVAL;
> +
> +#ifdef CONFIG_X86_64
> +	if (is_noncanonical_address(vmcs12->host_fs_base, vcpu) ||
> +	    is_noncanonical_address(vmcs12->host_gs_base, vcpu) ||
> +	    is_noncanonical_address(vmcs12->host_gdtr_base, vcpu) ||
> +	    is_noncanonical_address(vmcs12->host_tr_base, vcpu))
> +		return -EINVAL;
> +#endif

Same for host GDTR and TR base.

Paolo

Patch
diff mbox series

diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c
index ffeeeb5ff520..b610f389a01b 100644
--- a/arch/x86/kvm/vmx/nested.c
+++ b/arch/x86/kvm/vmx/nested.c
@@ -2630,18 +2630,16 @@  static int nested_vmx_check_controls(struct kvm_vcpu *vcpu,
 	return 0;
 }
 
-static int nested_vmx_check_host_state(struct kvm_vcpu *vcpu,
-				       struct vmcs12 *vmcs12)
+static int nested_vmx_check_host_state_full(struct kvm_vcpu *vcpu,
+					    struct vmcs12 *vmcs12)
 {
 	bool ia32e;
 
-	if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
-	    !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
+	if (!nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
 	    !nested_cr3_valid(vcpu, vmcs12->host_cr3))
 		return -EINVAL;
 
-	if (is_noncanonical_address(vmcs12->host_ia32_sysenter_esp, vcpu) ||
-	    is_noncanonical_address(vmcs12->host_ia32_sysenter_eip, vcpu))
+	if (is_noncanonical_address(vmcs12->host_ia32_sysenter_eip, vcpu))
 		return -EINVAL;
 
 	if ((vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) &&
@@ -2655,8 +2653,6 @@  static int nested_vmx_check_host_state(struct kvm_vcpu *vcpu,
 	    vmcs12->host_ss_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK) ||
 	    vmcs12->host_ds_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK) ||
 	    vmcs12->host_es_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK) ||
-	    vmcs12->host_fs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK) ||
-	    vmcs12->host_gs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK) ||
 	    vmcs12->host_tr_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK) ||
 	    vmcs12->host_cs_selector == 0 ||
 	    vmcs12->host_tr_selector == 0 ||
@@ -2664,11 +2660,7 @@  static int nested_vmx_check_host_state(struct kvm_vcpu *vcpu,
 		return -EINVAL;
 
 #ifdef CONFIG_X86_64
-	if (is_noncanonical_address(vmcs12->host_fs_base, vcpu) ||
-	    is_noncanonical_address(vmcs12->host_gs_base, vcpu) ||
-	    is_noncanonical_address(vmcs12->host_gdtr_base, vcpu) ||
-	    is_noncanonical_address(vmcs12->host_idtr_base, vcpu) ||
-	    is_noncanonical_address(vmcs12->host_tr_base, vcpu))
+	if (is_noncanonical_address(vmcs12->host_idtr_base, vcpu))
 		return -EINVAL;
 #endif
 
@@ -2688,6 +2680,36 @@  static int nested_vmx_check_host_state(struct kvm_vcpu *vcpu,
 	return 0;
 }
 
+static int nested_vmx_check_host_state(struct kvm_vcpu *vcpu,
+				       struct vmcs12 *vmcs12)
+{
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+
+	if ((vmx->nested.dirty_vmcs12) &&
+	    nested_vmx_check_host_state_full(vcpu, vmcs12))
+		return -EINVAL;
+
+	if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0))
+		return -EINVAL;
+
+	if (is_noncanonical_address(vmcs12->host_ia32_sysenter_esp, vcpu))
+		return -EINVAL;
+
+	if (vmcs12->host_fs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK) ||
+	    vmcs12->host_gs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK))
+		return -EINVAL;
+
+#ifdef CONFIG_X86_64
+	if (is_noncanonical_address(vmcs12->host_fs_base, vcpu) ||
+	    is_noncanonical_address(vmcs12->host_gs_base, vcpu) ||
+	    is_noncanonical_address(vmcs12->host_gdtr_base, vcpu) ||
+	    is_noncanonical_address(vmcs12->host_tr_base, vcpu))
+		return -EINVAL;
+#endif
+
+	return 0;
+}
+
 static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
 					  struct vmcs12 *vmcs12)
 {