linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] KVM: Reduce the execution of one instruction
@ 2022-09-06 15:33 Liam Ni
  2022-09-06 16:14 ` Sean Christopherson
  0 siblings, 1 reply; 3+ messages in thread
From: Liam Ni @ 2022-09-06 15:33 UTC (permalink / raw)
  To: linux-kernel, kvm, x86; +Cc: seanjc, pbonzini, tglx, dave.hansen, zhiguangni01

From: Liam Ni <zhiguangni01@gmail.com>

If the condition is met, reduce the execution of one instruction.

Signed-off-by: Liam Ni <zhiguangni01@gmail.com>
---
 arch/x86/kvm/emulate.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c
index f8382abe22ff..ebb95f3f9862 100644
--- a/arch/x86/kvm/emulate.c
+++ b/arch/x86/kvm/emulate.c
@@ -1139,10 +1139,12 @@ static int em_fnstsw(struct x86_emulate_ctxt *ctxt)
 static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
 				    struct operand *op)
 {
-	unsigned reg = ctxt->modrm_reg;
+	unsigned int reg;
 
 	if (!(ctxt->d & ModRM))
 		reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
+	else
+		reg = ctxt->modrm_reg;
 
 	if (ctxt->d & Sse) {
 		op->type = OP_XMM;
-- 
2.25.1


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

* Re: [PATCH] KVM: Reduce the execution of one instruction
  2022-09-06 15:33 [PATCH] KVM: Reduce the execution of one instruction Liam Ni
@ 2022-09-06 16:14 ` Sean Christopherson
  2022-09-07 16:03   ` Liam Ni
  0 siblings, 1 reply; 3+ messages in thread
From: Sean Christopherson @ 2022-09-06 16:14 UTC (permalink / raw)
  To: Liam Ni; +Cc: linux-kernel, kvm, x86, pbonzini, tglx, dave.hansen

"KVM: x86:" for the shortlog.

On Tue, Sep 06, 2022, Liam Ni wrote:
> From: Liam Ni <zhiguangni01@gmail.com>
> 
> If the condition is met,

Please describe this specific code change, "If the condition is met" is extremely
generic and doesn't help the reader understand what change is being made.

> reduce the execution of one instruction.

This is highly speculative, e.g. clang will generate identical output since it's
trivial for the compiler to observe that ctxt->modrm_reg doesn't need to be read.

And similar to the above "If the condition is met", the shortlog is too generic
even if it were 100% accurate.

I do think this change is a net positive, but it's beneficial only in making the
code easier to read.  Shaving a single cheap instruction in a relatively slow path
isn't sufficient justification even if the compiler isn't clever enough to optimize
away the load in the first place.

E.g. something like:

  KVM: x86: Clean up ModR/M "reg" initialization in reg op decoding

  Refactor decode_register_operand() to get the ModR/M register if and
  only if the instruction uses a ModR/M encoding to make it more obvious
  how the register operand is retrieved.

> Signed-off-by: Liam Ni <zhiguangni01@gmail.com>
> ---
>  arch/x86/kvm/emulate.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c
> index f8382abe22ff..ebb95f3f9862 100644
> --- a/arch/x86/kvm/emulate.c
> +++ b/arch/x86/kvm/emulate.c
> @@ -1139,10 +1139,12 @@ static int em_fnstsw(struct x86_emulate_ctxt *ctxt)
>  static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
>  				    struct operand *op)
>  {
> -	unsigned reg = ctxt->modrm_reg;
> +	unsigned int reg;
>  
>  	if (!(ctxt->d & ModRM))
>  		reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
> +	else
> +		reg = ctxt->modrm_reg;

I'd prefer to write this as:

	unsigned int reg;

	if (ctxt->d & ModRM)
		reg = ctxt->modrm_reg;
	else
		reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);

so that "is ModRM" check is immediately followed by "get ModRM".

>  
>  	if (ctxt->d & Sse) {
>  		op->type = OP_XMM;
> -- 
> 2.25.1

> 

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

* Re: [PATCH] KVM: Reduce the execution of one instruction
  2022-09-06 16:14 ` Sean Christopherson
@ 2022-09-07 16:03   ` Liam Ni
  0 siblings, 0 replies; 3+ messages in thread
From: Liam Ni @ 2022-09-07 16:03 UTC (permalink / raw)
  To: Sean Christopherson; +Cc: linux-kernel, kvm, x86, pbonzini, tglx, dave.hansen

On Wed, 7 Sept 2022 at 00:14, Sean Christopherson <seanjc@google.com> wrote:
>
> "KVM: x86:" for the shortlog.
>
> On Tue, Sep 06, 2022, Liam Ni wrote:
> > From: Liam Ni <zhiguangni01@gmail.com>
> >
> > If the condition is met,
>
> Please describe this specific code change, "If the condition is met" is extremely
> generic and doesn't help the reader understand what change is being made.
>
> > reduce the execution of one instruction.
>
> This is highly speculative, e.g. clang will generate identical output since it's
> trivial for the compiler to observe that ctxt->modrm_reg doesn't need to be read.
>
> And similar to the above "If the condition is met", the shortlog is too generic
> even if it were 100% accurate.
>
> I do think this change is a net positive, but it's beneficial only in making the
> code easier to read.  Shaving a single cheap instruction in a relatively slow path
> isn't sufficient justification even if the compiler isn't clever enough to optimize
> away the load in the first place.
>
> E.g. something like:
>
>   KVM: x86: Clean up ModR/M "reg" initialization in reg op decoding
>
>   Refactor decode_register_operand() to get the ModR/M register if and
>   only if the instruction uses a ModR/M encoding to make it more obvious
>   how the register operand is retrieved.
>
> > Signed-off-by: Liam Ni <zhiguangni01@gmail.com>
> > ---
> >  arch/x86/kvm/emulate.c | 4 +++-
> >  1 file changed, 3 insertions(+), 1 deletion(-)
> >
> > diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c
> > index f8382abe22ff..ebb95f3f9862 100644
> > --- a/arch/x86/kvm/emulate.c
> > +++ b/arch/x86/kvm/emulate.c
> > @@ -1139,10 +1139,12 @@ static int em_fnstsw(struct x86_emulate_ctxt *ctxt)
> >  static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
> >                                   struct operand *op)
> >  {
> > -     unsigned reg = ctxt->modrm_reg;
> > +     unsigned int reg;
> >
> >       if (!(ctxt->d & ModRM))
> >               reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
> > +     else
> > +             reg = ctxt->modrm_reg;
>
> I'd prefer to write this as:
>
>         unsigned int reg;
>
>         if (ctxt->d & ModRM)
>                 reg = ctxt->modrm_reg;
>         else
>                 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
>
> so that "is ModRM" check is immediately followed by "get ModRM".
>
> >
> >       if (ctxt->d & Sse) {
> >               op->type = OP_XMM;
> > --
> > 2.25.1
>
> >

Thanks for the suggestion, I will submit a new patch V2.

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

end of thread, other threads:[~2022-09-07 16:05 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-06 15:33 [PATCH] KVM: Reduce the execution of one instruction Liam Ni
2022-09-06 16:14 ` Sean Christopherson
2022-09-07 16:03   ` Liam Ni

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