kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] kvm:vmx:code changes in handle_io() to save some CPU cycles.
@ 2020-12-10  8:15 Stephen Zhang
  2020-12-10  9:56 ` Vitaly Kuznetsov
  2020-12-12  0:34 ` Paolo Bonzini
  0 siblings, 2 replies; 3+ messages in thread
From: Stephen Zhang @ 2020-12-10  8:15 UTC (permalink / raw)
  To: pbonzini, seanjc, vkuznets, wanpengli, jmattson, joro, tglx,
	mingo, bp, x86, hpa
  Cc: kvm, linux-kernel, Stephen Zhang

code changes in handle_io() to save some CPU cycles.

Signed-off-by: Stephen Zhang <starzhangzsd@gmail.com>
---
 arch/x86/kvm/vmx/vmx.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index 47b8357..109bcf64 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -4899,15 +4899,14 @@ static int handle_triple_fault(struct kvm_vcpu *vcpu)
 static int handle_io(struct kvm_vcpu *vcpu)
 {
 	unsigned long exit_qualification;
-	int size, in, string;
+	int size, in;
 	unsigned port;
 
 	exit_qualification = vmx_get_exit_qual(vcpu);
-	string = (exit_qualification & 16) != 0;
 
 	++vcpu->stat.io_exits;
 
-	if (string)
+	if (exit_qualification & 16)
 		return kvm_emulate_instruction(vcpu, 0);
 
 	port = exit_qualification >> 16;
-- 
1.8.3.1


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

* Re: [PATCH] kvm:vmx:code changes in handle_io() to save some CPU cycles.
  2020-12-10  8:15 [PATCH] kvm:vmx:code changes in handle_io() to save some CPU cycles Stephen Zhang
@ 2020-12-10  9:56 ` Vitaly Kuznetsov
  2020-12-12  0:34 ` Paolo Bonzini
  1 sibling, 0 replies; 3+ messages in thread
From: Vitaly Kuznetsov @ 2020-12-10  9:56 UTC (permalink / raw)
  To: Stephen Zhang
  Cc: kvm, linux-kernel, Stephen Zhang, pbonzini, seanjc, wanpengli,
	jmattson, joro, tglx, mingo, bp, x86, hpa

Stephen Zhang <starzhangzsd@gmail.com> writes:

> code changes in handle_io() to save some CPU cycles.
>
> Signed-off-by: Stephen Zhang <starzhangzsd@gmail.com>
> ---
>  arch/x86/kvm/vmx/vmx.c | 5 ++---
>  1 file changed, 2 insertions(+), 3 deletions(-)
>
> diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
> index 47b8357..109bcf64 100644
> --- a/arch/x86/kvm/vmx/vmx.c
> +++ b/arch/x86/kvm/vmx/vmx.c
> @@ -4899,15 +4899,14 @@ static int handle_triple_fault(struct kvm_vcpu *vcpu)
>  static int handle_io(struct kvm_vcpu *vcpu)
>  {
>  	unsigned long exit_qualification;
> -	int size, in, string;
> +	int size, in;
>  	unsigned port;
>  
>  	exit_qualification = vmx_get_exit_qual(vcpu);
> -	string = (exit_qualification & 16) != 0;
>  
>  	++vcpu->stat.io_exits;
>  
> -	if (string)
> +	if (exit_qualification & 16)
>  		return kvm_emulate_instruction(vcpu, 0);
>  
>  	port = exit_qualification >> 16;

I seriously doubt this can save any CPU cycles as compiler will likely
re-order and optimize this check anyway, I don't expect to see any
push/pop operations here. I agree that having local 'string' variable is
likely an overkill, however, the 'exit_qualification & 16' check we have
is hard to read so 'string' here works as a comment :-)

I would've liked the patch in the following shape more:

diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index e35552055c07..65b6062ad7bc 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -4898,15 +4898,15 @@ static int handle_triple_fault(struct kvm_vcpu *vcpu)
 static int handle_io(struct kvm_vcpu *vcpu)
 {
        unsigned long exit_qualification;
-       int size, in, string;
+       int size, in;
        unsigned port;
 
        exit_qualification = vmx_get_exit_qual(vcpu);
-       string = (exit_qualification & 16) != 0;
 
        ++vcpu->stat.io_exits;
 
-       if (string)
+       /* String instruction */
+       if (exit_qualification & BIT(4))
                return kvm_emulate_instruction(vcpu, 0);
 
        port = exit_qualification >> 16;

Also, the changelog needs to be re-phrased to state code cleanup and not
CPU cycles optimization.

-- 
Vitaly


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

* Re: [PATCH] kvm:vmx:code changes in handle_io() to save some CPU cycles.
  2020-12-10  8:15 [PATCH] kvm:vmx:code changes in handle_io() to save some CPU cycles Stephen Zhang
  2020-12-10  9:56 ` Vitaly Kuznetsov
@ 2020-12-12  0:34 ` Paolo Bonzini
  1 sibling, 0 replies; 3+ messages in thread
From: Paolo Bonzini @ 2020-12-12  0:34 UTC (permalink / raw)
  To: Stephen Zhang, seanjc, vkuznets, wanpengli, jmattson, joro, tglx,
	mingo, bp, x86, hpa
  Cc: kvm, linux-kernel

On 10/12/20 09:15, Stephen Zhang wrote:
> code changes in handle_io() to save some CPU cycles.
> 
> Signed-off-by: Stephen Zhang <starzhangzsd@gmail.com>
> ---
>   arch/x86/kvm/vmx/vmx.c | 5 ++---
>   1 file changed, 2 insertions(+), 3 deletions(-)
> 
> diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
> index 47b8357..109bcf64 100644
> --- a/arch/x86/kvm/vmx/vmx.c
> +++ b/arch/x86/kvm/vmx/vmx.c
> @@ -4899,15 +4899,14 @@ static int handle_triple_fault(struct kvm_vcpu *vcpu)
>   static int handle_io(struct kvm_vcpu *vcpu)
>   {
>   	unsigned long exit_qualification;
> -	int size, in, string;
> +	int size, in;
>   	unsigned port;
>   
>   	exit_qualification = vmx_get_exit_qual(vcpu);
> -	string = (exit_qualification & 16) != 0;
>   
>   	++vcpu->stat.io_exits;
>   
> -	if (string)
> +	if (exit_qualification & 16)
>   		return kvm_emulate_instruction(vcpu, 0);
>   
>   	port = exit_qualification >> 16;
> 

I would be very surprised if there's any change in the assembly code 
after this patch.

Paolo


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

end of thread, other threads:[~2020-12-12  0:50 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-10  8:15 [PATCH] kvm:vmx:code changes in handle_io() to save some CPU cycles Stephen Zhang
2020-12-10  9:56 ` Vitaly Kuznetsov
2020-12-12  0:34 ` Paolo Bonzini

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).