[v2,2/2] KVM: CPUID: Put maxphyaddr updating together with virtual address width checking
diff mbox series

Message ID 20190910102742.47729-3-xiaoyao.li@intel.com
State New
Headers show
Series
  • KVM: CPUID: emulation flow adjustment and one minor refinement when updating maxphyaddr
Related show

Commit Message

Xiaoyao Li Sept. 10, 2019, 10:27 a.m. UTC
Since both of maxphyaddr updating and virtual address width checking
need to query the cpuid leaf 0x80000008. We can put them together.

Signed-off-by: Xiaoyao Li <xiaoyao.li@intel.com>
---
 arch/x86/kvm/cpuid.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

Comments

Jim Mattson Sept. 10, 2019, 5:13 p.m. UTC | #1
On Tue, Sep 10, 2019 at 3:42 AM Xiaoyao Li <xiaoyao.li@intel.com> wrote:
>
> Since both of maxphyaddr updating and virtual address width checking
> need to query the cpuid leaf 0x80000008. We can put them together.
>
> Signed-off-by: Xiaoyao Li <xiaoyao.li@intel.com>
> ---
>  arch/x86/kvm/cpuid.c | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
>
> diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c
> index 67fa44ab87af..fd0a66079001 100644
> --- a/arch/x86/kvm/cpuid.c
> +++ b/arch/x86/kvm/cpuid.c
> @@ -118,6 +118,7 @@ int kvm_update_cpuid(struct kvm_vcpu *vcpu)
>                 best->ebx = xstate_required_size(vcpu->arch.xcr0, true);
>
>         /*
> +        * Update physical address width and check virtual address width.
>          * The existing code assumes virtual address is 48-bit or 57-bit in the
>          * canonical address checks; exit if it is ever changed.
>          */
> @@ -127,7 +128,10 @@ int kvm_update_cpuid(struct kvm_vcpu *vcpu)
>
>                 if (vaddr_bits != 48 && vaddr_bits != 57 && vaddr_bits != 0)
>                         return -EINVAL;
> +
> +               vcpu->arch.maxphyaddr = best->eax & 0xff;
>         }
> +       vcpu->arch.maxphyaddr = 36;

Perhaps I'm missing something, but it looks to me like you always set
vcpu->arch.maxphyaddr to 36, regardless of what may be enumerated by
leaf 0x80000008.

Is there really much of an advantage to open-coding
cpuid_query_maxphyaddr() here?

>         best = kvm_find_cpuid_entry(vcpu, KVM_CPUID_FEATURES, 0);
>         if (kvm_hlt_in_guest(vcpu->kvm) && best &&
> @@ -144,8 +148,6 @@ int kvm_update_cpuid(struct kvm_vcpu *vcpu)
>                 }
>         }
>
> -       /* Update physical-address width */
> -       vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu);
>         kvm_mmu_reset_context(vcpu);
>
>         kvm_pmu_refresh(vcpu);
> --
> 2.19.1
>
Xiaoyao Li Sept. 10, 2019, 10:45 p.m. UTC | #2
On Tue, 2019-09-10 at 10:13 -0700, Jim Mattson wrote:
> On Tue, Sep 10, 2019 at 3:42 AM Xiaoyao Li <xiaoyao.li@intel.com> wrote:
> > 
> > Since both of maxphyaddr updating and virtual address width checking
> > need to query the cpuid leaf 0x80000008. We can put them together.
> > 
> > Signed-off-by: Xiaoyao Li <xiaoyao.li@intel.com>
> > ---
> >  arch/x86/kvm/cpuid.c | 6 ++++--
> >  1 file changed, 4 insertions(+), 2 deletions(-)
> > 
> > diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c
> > index 67fa44ab87af..fd0a66079001 100644
> > --- a/arch/x86/kvm/cpuid.c
> > +++ b/arch/x86/kvm/cpuid.c
> > @@ -118,6 +118,7 @@ int kvm_update_cpuid(struct kvm_vcpu *vcpu)
> >                 best->ebx = xstate_required_size(vcpu->arch.xcr0, true);
> > 
> >         /*
> > +        * Update physical address width and check virtual address width.
> >          * The existing code assumes virtual address is 48-bit or 57-bit in
> > the
> >          * canonical address checks; exit if it is ever changed.
> >          */
> > @@ -127,7 +128,10 @@ int kvm_update_cpuid(struct kvm_vcpu *vcpu)
> > 
> >                 if (vaddr_bits != 48 && vaddr_bits != 57 && vaddr_bits != 0)
> >                         return -EINVAL;
> > +
> > +               vcpu->arch.maxphyaddr = best->eax & 0xff;
> >         }
> > +       vcpu->arch.maxphyaddr = 36;
> 
> Perhaps I'm missing something, but it looks to me like you always set
> vcpu->arch.maxphyaddr to 36, regardless of what may be enumerated by
> leaf 0x80000008.

Oh, I made a stupid mistake. It should be included in the else case.
 
> 
> Is there really much of an advantage to open-coding
> cpuid_query_maxphyaddr() here?

Indeed not so much.
It can avoid two more kvm_find_cpuid_entry() calling that we don't handle leaf
0x80000008 twice in two place. 

> >         best = kvm_find_cpuid_entry(vcpu, KVM_CPUID_FEATURES, 0);
> >         if (kvm_hlt_in_guest(vcpu->kvm) && best &&
> > @@ -144,8 +148,6 @@ int kvm_update_cpuid(struct kvm_vcpu *vcpu)
> >                 }
> >         }
> > 
> > -       /* Update physical-address width */
> > -       vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu);
> >         kvm_mmu_reset_context(vcpu);
> > 
> >         kvm_pmu_refresh(vcpu);
> > --
> > 2.19.1
> >
Jim Mattson Sept. 10, 2019, 11:26 p.m. UTC | #3
On Tue, Sep 10, 2019 at 3:52 PM Xiaoyao Li <xiaoyao.li@intel.com> wrote:
>
> On Tue, 2019-09-10 at 10:13 -0700, Jim Mattson wrote:
> > On Tue, Sep 10, 2019 at 3:42 AM Xiaoyao Li <xiaoyao.li@intel.com> wrote:
> > >
> > > Since both of maxphyaddr updating and virtual address width checking
> > > need to query the cpuid leaf 0x80000008. We can put them together.
> > >
> > > Signed-off-by: Xiaoyao Li <xiaoyao.li@intel.com>
> > > ---
> > >  arch/x86/kvm/cpuid.c | 6 ++++--
> > >  1 file changed, 4 insertions(+), 2 deletions(-)
> > >
> > > diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c
> > > index 67fa44ab87af..fd0a66079001 100644
> > > --- a/arch/x86/kvm/cpuid.c
> > > +++ b/arch/x86/kvm/cpuid.c
> > > @@ -118,6 +118,7 @@ int kvm_update_cpuid(struct kvm_vcpu *vcpu)
> > >                 best->ebx = xstate_required_size(vcpu->arch.xcr0, true);
> > >
> > >         /*
> > > +        * Update physical address width and check virtual address width.
> > >          * The existing code assumes virtual address is 48-bit or 57-bit in
> > > the
> > >          * canonical address checks; exit if it is ever changed.
> > >          */
> > > @@ -127,7 +128,10 @@ int kvm_update_cpuid(struct kvm_vcpu *vcpu)
> > >
> > >                 if (vaddr_bits != 48 && vaddr_bits != 57 && vaddr_bits != 0)
> > >                         return -EINVAL;
> > > +
> > > +               vcpu->arch.maxphyaddr = best->eax & 0xff;
> > >         }
> > > +       vcpu->arch.maxphyaddr = 36;
> >
> > Perhaps I'm missing something, but it looks to me like you always set
> > vcpu->arch.maxphyaddr to 36, regardless of what may be enumerated by
> > leaf 0x80000008.
>
> Oh, I made a stupid mistake. It should be included in the else case.
>
> >
> > Is there really much of an advantage to open-coding
> > cpuid_query_maxphyaddr() here?
>
> Indeed not so much.
> It can avoid two more kvm_find_cpuid_entry() calling that we don't handle leaf
> 0x80000008 twice in two place.

I'm inclined to leave things as they are. As your previous attempt
demonstrates, having the same logic replicated in multiple places is
prone to error. I can't imagine that this would be a
performance-sensitive path.

> > >         best = kvm_find_cpuid_entry(vcpu, KVM_CPUID_FEATURES, 0);
> > >         if (kvm_hlt_in_guest(vcpu->kvm) && best &&
> > > @@ -144,8 +148,6 @@ int kvm_update_cpuid(struct kvm_vcpu *vcpu)
> > >                 }
> > >         }
> > >
> > > -       /* Update physical-address width */
> > > -       vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu);
> > >         kvm_mmu_reset_context(vcpu);
> > >
> > >         kvm_pmu_refresh(vcpu);
> > > --
> > > 2.19.1
> > >
>

Patch
diff mbox series

diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c
index 67fa44ab87af..fd0a66079001 100644
--- a/arch/x86/kvm/cpuid.c
+++ b/arch/x86/kvm/cpuid.c
@@ -118,6 +118,7 @@  int kvm_update_cpuid(struct kvm_vcpu *vcpu)
 		best->ebx = xstate_required_size(vcpu->arch.xcr0, true);
 
 	/*
+	 * Update physical address width and check virtual address width.
 	 * The existing code assumes virtual address is 48-bit or 57-bit in the
 	 * canonical address checks; exit if it is ever changed.
 	 */
@@ -127,7 +128,10 @@  int kvm_update_cpuid(struct kvm_vcpu *vcpu)
 
 		if (vaddr_bits != 48 && vaddr_bits != 57 && vaddr_bits != 0)
 			return -EINVAL;
+
+		vcpu->arch.maxphyaddr = best->eax & 0xff;
 	}
+	vcpu->arch.maxphyaddr = 36;
 
 	best = kvm_find_cpuid_entry(vcpu, KVM_CPUID_FEATURES, 0);
 	if (kvm_hlt_in_guest(vcpu->kvm) && best &&
@@ -144,8 +148,6 @@  int kvm_update_cpuid(struct kvm_vcpu *vcpu)
 		}
 	}
 
-	/* Update physical-address width */
-	vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu);
 	kvm_mmu_reset_context(vcpu);
 
 	kvm_pmu_refresh(vcpu);