KVM: VMX: Cache IA32_DEBUGCTL in memory

"Linux Kernel Mailing List" <[email protected]> Sat, 10 Feb 2018 22:00:18 +0000 (UTC)
Newsgroups gmane.linux.kernel.commits.head
Message-ID <[email protected]>
Web:        https://git.kernel.org/torvalds/c/74c55931c71352317ae0f5736ee9e4ca07ba4238
Commit:     74c55931c71352317ae0f5736ee9e4ca07ba4238
Parent:     276c796cfef5bdaf9aae055f520b8857eaa3fa19
Refname:    refs/heads/master
Author:     Wanpeng Li <[email protected]>
AuthorDate: Wed Nov 29 01:31:20 2017 -0800
Committer:  Paolo Bonzini <[email protected]>
CommitDate: Thu Dec 14 09:26:47 2017 +0100

    KVM: VMX: Cache IA32_DEBUGCTL in memory
    
    MSR_IA32_DEBUGCTLMSR is zeroed on VMEXIT, so it is saved/restored each
    time during world switch.  This patch caches the host IA32_DEBUGCTL MSR
    and saves/restores the host IA32_DEBUGCTL msr when guest/host switches
    to avoid to save/restore each time during world switch.  This saves
    about 100 clock cycles per vmexit.
    
    Suggested-by: Jim Mattson <[email protected]>
    Cc: Paolo Bonzini <[email protected]>
    Cc: Radim Krčmář <[email protected]>
    Cc: Jim Mattson <[email protected]>
    Signed-off-by: Wanpeng Li <[email protected]>
    Reviewed-by: Jim Mattson <[email protected]>
    Reviewed-by: David Hildenbrand <[email protected]>
    Signed-off-by: Radim Krčmář <[email protected]>
---
 arch/x86/kvm/vmx.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
index 3b5f70285414..4e7da90b4426 100644
--- a/arch/x86/kvm/vmx.c
+++ b/arch/x86/kvm/vmx.c
@@ -650,6 +650,8 @@ struct vcpu_vmx {
 
 	u32 host_pkru;
 
+	unsigned long host_debugctlmsr;
+
 	/*
 	 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
 	 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
@@ -2318,6 +2320,7 @@ static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
 
 	vmx_vcpu_pi_load(vcpu, cpu);
 	vmx->host_pkru = read_pkru();
+	vmx->host_debugctlmsr = get_debugctlmsr();
 }
 
 static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
@@ -9261,7 +9264,7 @@ static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
 static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
 {
 	struct vcpu_vmx *vmx = to_vmx(vcpu);
-	unsigned long debugctlmsr, cr3, cr4;
+	unsigned long cr3, cr4;
 
 	/* Record the guest's net vcpu time for enforced NMI injections. */
 	if (unlikely(!enable_vnmi &&
@@ -9314,7 +9317,6 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
 		__write_pkru(vcpu->arch.pkru);
 
 	atomic_switch_perf_msrs(vmx);
-	debugctlmsr = get_debugctlmsr();
 
 	vmx_arm_hv_timer(vcpu);
 
@@ -9425,8 +9427,8 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
 	      );
 
 	/* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
-	if (debugctlmsr)
-		update_debugctlmsr(debugctlmsr);
+	if (vmx->host_debugctlmsr)
+		update_debugctlmsr(vmx->host_debugctlmsr);
 
 #ifndef CONFIG_X86_64
 	/*
--
To unsubscribe from this list: send the line "unsubscribe git-commits-head" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html