linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] KVM: VMX: Cache IA32_DEBUGCTL in memory
@ 2017-11-29  9:31 Wanpeng Li
  2017-11-29 16:55 ` Jim Mattson
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Wanpeng Li @ 2017-11-29  9:31 UTC (permalink / raw)
  To: linux-kernel, kvm
  Cc: Paolo Bonzini, Radim Krčmář, Wanpeng Li, Jim Mattson

From: Wanpeng Li <wanpeng.li@hotmail.com>

MSR_IA32_DEBUGCTLMSR is zeroed on VMEXIT, so it is saved/restored 
each time during world switch. Jim from Google pointed out that 
when running schbench in L2, vmx_vcpu_run will occupy 4% cpu time, 
and the 25% of vmx_vcpu_run cpu time is occupied by get_debugctlmsr(). 
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.

Suggested-by: Jim Mattson <jmattson@google.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Radim Krčmář <rkrcmar@redhat.com>
Cc: Jim Mattson <jmattson@google.com>
Signed-off-by: Wanpeng Li <wanpeng.li@hotmail.com>
---
v1 ->  v2:
 * rename to host_debugctlmsr and place it in struct vcpu_struct
 * update_debugctlmsr stay in vmx_vcpu_run

 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 8c7e816..d293c29 100644
--- a/arch/x86/kvm/vmx.c
+++ b/arch/x86/kvm/vmx.c
@@ -658,6 +658,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
@@ -2326,6 +2328,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)
@@ -9346,7 +9349,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 &&
@@ -9399,7 +9402,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);
 
@@ -9510,8 +9512,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
 	/*
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] KVM: VMX: Cache IA32_DEBUGCTL in memory
  2017-11-29  9:31 [PATCH v2] KVM: VMX: Cache IA32_DEBUGCTL in memory Wanpeng Li
@ 2017-11-29 16:55 ` Jim Mattson
  2017-11-29 17:32 ` David Hildenbrand
  2017-12-05 21:54 ` Radim Krčmář
  2 siblings, 0 replies; 5+ messages in thread
From: Jim Mattson @ 2017-11-29 16:55 UTC (permalink / raw)
  To: Wanpeng Li
  Cc: LKML, kvm list, Paolo Bonzini, Radim Krčmář, Wanpeng Li

Thanks for doing this!

Reviewed-by: Jim Mattson <jmattson@google.com>

On Wed, Nov 29, 2017 at 1:31 AM, Wanpeng Li <kernellwp@gmail.com> wrote:
> From: Wanpeng Li <wanpeng.li@hotmail.com>
>
> MSR_IA32_DEBUGCTLMSR is zeroed on VMEXIT, so it is saved/restored
> each time during world switch. Jim from Google pointed out that
> when running schbench in L2, vmx_vcpu_run will occupy 4% cpu time,
> and the 25% of vmx_vcpu_run cpu time is occupied by get_debugctlmsr().
> 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.
>
> Suggested-by: Jim Mattson <jmattson@google.com>
> Cc: Paolo Bonzini <pbonzini@redhat.com>
> Cc: Radim Krčmář <rkrcmar@redhat.com>
> Cc: Jim Mattson <jmattson@google.com>
> Signed-off-by: Wanpeng Li <wanpeng.li@hotmail.com>
> ---
> v1 ->  v2:
>  * rename to host_debugctlmsr and place it in struct vcpu_struct
>  * update_debugctlmsr stay in vmx_vcpu_run
>
>  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 8c7e816..d293c29 100644
> --- a/arch/x86/kvm/vmx.c
> +++ b/arch/x86/kvm/vmx.c
> @@ -658,6 +658,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
> @@ -2326,6 +2328,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)
> @@ -9346,7 +9349,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 &&
> @@ -9399,7 +9402,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);
>
> @@ -9510,8 +9512,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
>         /*
> --
> 2.7.4
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] KVM: VMX: Cache IA32_DEBUGCTL in memory
  2017-11-29  9:31 [PATCH v2] KVM: VMX: Cache IA32_DEBUGCTL in memory Wanpeng Li
  2017-11-29 16:55 ` Jim Mattson
@ 2017-11-29 17:32 ` David Hildenbrand
  2017-12-05 21:54 ` Radim Krčmář
  2 siblings, 0 replies; 5+ messages in thread
From: David Hildenbrand @ 2017-11-29 17:32 UTC (permalink / raw)
  To: Wanpeng Li, linux-kernel, kvm
  Cc: Paolo Bonzini, Radim Krčmář, Wanpeng Li, Jim Mattson

On 29.11.2017 10:31, Wanpeng Li wrote:
> From: Wanpeng Li <wanpeng.li@hotmail.com>
> 
> MSR_IA32_DEBUGCTLMSR is zeroed on VMEXIT, so it is saved/restored 
> each time during world switch. Jim from Google pointed out that 
> when running schbench in L2, vmx_vcpu_run will occupy 4% cpu time, 
> and the 25% of vmx_vcpu_run cpu time is occupied by get_debugctlmsr(). 
> 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.
> 
> Suggested-by: Jim Mattson <jmattson@google.com>
> Cc: Paolo Bonzini <pbonzini@redhat.com>
> Cc: Radim Krčmář <rkrcmar@redhat.com>
> Cc: Jim Mattson <jmattson@google.com>
> Signed-off-by: Wanpeng Li <wanpeng.li@hotmail.com>
> ---
> v1 ->  v2:
>  * rename to host_debugctlmsr and place it in struct vcpu_struct
>  * update_debugctlmsr stay in vmx_vcpu_run
> 
>  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 8c7e816..d293c29 100644
> --- a/arch/x86/kvm/vmx.c
> +++ b/arch/x86/kvm/vmx.c
> @@ -658,6 +658,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
> @@ -2326,6 +2328,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)
> @@ -9346,7 +9349,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 &&
> @@ -9399,7 +9402,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);
>  
> @@ -9510,8 +9512,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
>  	/*
> 

Reviewed-by: David Hildenbrand <david@redhat.com>

-- 

Thanks,

David / dhildenb

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] KVM: VMX: Cache IA32_DEBUGCTL in memory
  2017-11-29  9:31 [PATCH v2] KVM: VMX: Cache IA32_DEBUGCTL in memory Wanpeng Li
  2017-11-29 16:55 ` Jim Mattson
  2017-11-29 17:32 ` David Hildenbrand
@ 2017-12-05 21:54 ` Radim Krčmář
  2017-12-06  0:15   ` Nadav Amit
  2 siblings, 1 reply; 5+ messages in thread
From: Radim Krčmář @ 2017-12-05 21:54 UTC (permalink / raw)
  To: Wanpeng Li; +Cc: linux-kernel, kvm, Paolo Bonzini, Wanpeng Li, Jim Mattson

2017-11-29 01:31-0800, Wanpeng Li:
> From: Wanpeng Li <wanpeng.li@hotmail.com>
> 
> MSR_IA32_DEBUGCTLMSR is zeroed on VMEXIT, so it is saved/restored 
> each time during world switch. Jim from Google pointed out that 
> when running schbench in L2, vmx_vcpu_run will occupy 4% cpu time, 
> and the 25% of vmx_vcpu_run cpu time is occupied by get_debugctlmsr(). 
> 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.
> 
> Suggested-by: Jim Mattson <jmattson@google.com>
> Cc: Paolo Bonzini <pbonzini@redhat.com>
> Cc: Radim Krčmář <rkrcmar@redhat.com>
> Cc: Jim Mattson <jmattson@google.com>
> Signed-off-by: Wanpeng Li <wanpeng.li@hotmail.com>
> ---

Queued, thanks.

And there is another optimization loosely connected to the "[PATCH v3
00/16] Move vcpu_load and vcpu_put calls to arch code" series:
We only need to read the value for the KVM_RUN ioctl.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] KVM: VMX: Cache IA32_DEBUGCTL in memory
  2017-12-05 21:54 ` Radim Krčmář
@ 2017-12-06  0:15   ` Nadav Amit
  0 siblings, 0 replies; 5+ messages in thread
From: Nadav Amit @ 2017-12-06  0:15 UTC (permalink / raw)
  To: Radim Krčmář
  Cc: Wanpeng Li, LKML, kvm list, Paolo Bonzini, Wanpeng Li, Jim Mattson

Radim Krčmář <rkrcmar@redhat.com> wrote:

> 2017-11-29 01:31-0800, Wanpeng Li:
>> From: Wanpeng Li <wanpeng.li@hotmail.com>
>> 
>> MSR_IA32_DEBUGCTLMSR is zeroed on VMEXIT, so it is saved/restored 
>> each time during world switch. Jim from Google pointed out that 
>> when running schbench in L2, vmx_vcpu_run will occupy 4% cpu time, 
>> and the 25% of vmx_vcpu_run cpu time is occupied by get_debugctlmsr(). 
>> 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.
>> 
>> Suggested-by: Jim Mattson <jmattson@google.com>
>> Cc: Paolo Bonzini <pbonzini@redhat.com>
>> Cc: Radim Krčmář <rkrcmar@redhat.com>
>> Cc: Jim Mattson <jmattson@google.com>
>> Signed-off-by: Wanpeng Li <wanpeng.li@hotmail.com>
>> ---
> 
> Queued, thanks.
> 
> And there is another optimization loosely connected to the "[PATCH v3
> 00/16] Move vcpu_load and vcpu_put calls to arch code" series:
> We only need to read the value for the KVM_RUN ioctl.

Can you add some warning that would fire in some “debug” mode if the cached
value is not identical to the real one?

Thanks,
Nadav

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2017-12-06  0:15 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-11-29  9:31 [PATCH v2] KVM: VMX: Cache IA32_DEBUGCTL in memory Wanpeng Li
2017-11-29 16:55 ` Jim Mattson
2017-11-29 17:32 ` David Hildenbrand
2017-12-05 21:54 ` Radim Krčmář
2017-12-06  0:15   ` Nadav Amit

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).