diff mbox

[2/3] target-i386: initialize vcpu's TSC rate to the value from KVM

Message ID 1443418711-24106-3-git-send-email-haozhong.zhang@intel.com
State New
Headers show

Commit Message

Haozhong Zhang Sept. 28, 2015, 5:38 a.m. UTC
When creating a vcpu, we initialize its TSC rate to the value from
KVM (through ioctl KVM_GET_TSC_KHZ).

Signed-off-by: Haozhong Zhang <haozhong.zhang@intel.com>
---
 target-i386/kvm.c | 7 +++++++
 1 file changed, 7 insertions(+)

Comments

Eduardo Habkost Sept. 28, 2015, 4:17 p.m. UTC | #1
On Mon, Sep 28, 2015 at 01:38:30PM +0800, Haozhong Zhang wrote:
> When creating a vcpu, we initialize its TSC rate to the value from
> KVM (through ioctl KVM_GET_TSC_KHZ).
> 
> Signed-off-by: Haozhong Zhang <haozhong.zhang@intel.com>
> ---
>  target-i386/kvm.c | 7 +++++++
>  1 file changed, 7 insertions(+)
> 
> diff --git a/target-i386/kvm.c b/target-i386/kvm.c
> index 7b0ba17..c2b161a 100644
> --- a/target-i386/kvm.c
> +++ b/target-i386/kvm.c
> @@ -751,6 +751,13 @@ int kvm_arch_init_vcpu(CPUState *cs)
>          }
>      }
>  
> +    r = kvm_vcpu_ioctl(cs, KVM_GET_TSC_KHZ);
> +    if (r < 0) {
> +        fprintf(stderr, "KVM_GET_TSC_KHZ failed\n");
> +        return r;
> +    }
> +    env->tsc_khz = r;

You are silently overwriting the tsc_khz value set by the user, why?
Haozhong Zhang Sept. 29, 2015, 1:23 a.m. UTC | #2
On Mon, Sep 28, 2015 at 01:17:44PM -0300, Eduardo Habkost wrote:
> On Mon, Sep 28, 2015 at 01:38:30PM +0800, Haozhong Zhang wrote:
> > When creating a vcpu, we initialize its TSC rate to the value from
> > KVM (through ioctl KVM_GET_TSC_KHZ).
> > 
> > Signed-off-by: Haozhong Zhang <haozhong.zhang@intel.com>
> > ---
> >  target-i386/kvm.c | 7 +++++++
> >  1 file changed, 7 insertions(+)
> > 
> > diff --git a/target-i386/kvm.c b/target-i386/kvm.c
> > index 7b0ba17..c2b161a 100644
> > --- a/target-i386/kvm.c
> > +++ b/target-i386/kvm.c
> > @@ -751,6 +751,13 @@ int kvm_arch_init_vcpu(CPUState *cs)
> >          }
> >      }
> >  
> > +    r = kvm_vcpu_ioctl(cs, KVM_GET_TSC_KHZ);
> > +    if (r < 0) {
> > +        fprintf(stderr, "KVM_GET_TSC_KHZ failed\n");
> > +        return r;
> > +    }
> > +    env->tsc_khz = r;
> 
> You are silently overwriting the tsc_khz value set by the user, why?
>

Oh, I need to check if user has provided tsc_khz, and if so then just
use the user-provided value. So I'll replace it with code like

if (env->tsc_khz) {
    kvm_vcpu_ioctl(cs, KVM_SET_TSC_KHZ, env->tsc_khz);
} else {
    r = kvm_vcpu_ioctl(cs, KVM_GET_TSC_KHZ);
    if (r < 0) {
        fprintf(stderr, "KVM_GET_TSC_KHZ failed\n");
        return r;
    }
    env->tsc_khz = r;
}

- Haozhong

> -- 
> Eduardo
Haozhong Zhang Sept. 29, 2015, 1:46 a.m. UTC | #3
On Tue, Sep 29, 2015 at 09:23:39AM +0800, Haozhong Zhang wrote:
> On Mon, Sep 28, 2015 at 01:17:44PM -0300, Eduardo Habkost wrote:
> > On Mon, Sep 28, 2015 at 01:38:30PM +0800, Haozhong Zhang wrote:
> > > When creating a vcpu, we initialize its TSC rate to the value from
> > > KVM (through ioctl KVM_GET_TSC_KHZ).
> > > 
> > > Signed-off-by: Haozhong Zhang <haozhong.zhang@intel.com>
> > > ---
> > >  target-i386/kvm.c | 7 +++++++
> > >  1 file changed, 7 insertions(+)
> > > 
> > > diff --git a/target-i386/kvm.c b/target-i386/kvm.c
> > > index 7b0ba17..c2b161a 100644
> > > --- a/target-i386/kvm.c
> > > +++ b/target-i386/kvm.c
> > > @@ -751,6 +751,13 @@ int kvm_arch_init_vcpu(CPUState *cs)
> > >          }
> > >      }
> > >  
> > > +    r = kvm_vcpu_ioctl(cs, KVM_GET_TSC_KHZ);
> > > +    if (r < 0) {
> > > +        fprintf(stderr, "KVM_GET_TSC_KHZ failed\n");
> > > +        return r;
> > > +    }
> > > +    env->tsc_khz = r;
> > 
> > You are silently overwriting the tsc_khz value set by the user, why?
> >
> 
> Oh, I need to check if user has provided tsc_khz, and if so then just
> use the user-provided value. So I'll replace it with code like
> 
> if (env->tsc_khz) {
>     kvm_vcpu_ioctl(cs, KVM_SET_TSC_KHZ, env->tsc_khz);

Just notice this line duplicates code several lines above. Only the
else branch is needed.

> } else {
>     r = kvm_vcpu_ioctl(cs, KVM_GET_TSC_KHZ);
>     if (r < 0) {
>         fprintf(stderr, "KVM_GET_TSC_KHZ failed\n");
>         return r;
>     }
>     env->tsc_khz = r;
> }
> 
> - Haozhong
> 
> > -- 
> > Eduardo
> 
> --
> To unsubscribe from this list: send the line "unsubscribe kvm" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
- Haozhong Zhang
diff mbox

Patch

diff --git a/target-i386/kvm.c b/target-i386/kvm.c
index 7b0ba17..c2b161a 100644
--- a/target-i386/kvm.c
+++ b/target-i386/kvm.c
@@ -751,6 +751,13 @@  int kvm_arch_init_vcpu(CPUState *cs)
         }
     }
 
+    r = kvm_vcpu_ioctl(cs, KVM_GET_TSC_KHZ);
+    if (r < 0) {
+        fprintf(stderr, "KVM_GET_TSC_KHZ failed\n");
+        return r;
+    }
+    env->tsc_khz = r;
+
     if (kvm_has_xsave()) {
         env->kvm_xsave_buf = qemu_memalign(4096, sizeof(struct kvm_xsave));
     }