Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1233849 > unrolled thread

[PATCH 00/12] KVM: x86: add support for VMX TSC scaling

Started byHaozhong Zhang <haozhong.zhang@intel.com>
First post2015-09-28 07:40 +0200
Last post2015-09-29 06:10 +0200
Articles 8 on this page of 28 — 5 participants

Back to article view | Back to linux.kernel


Contents

  [PATCH 00/12] KVM: x86: add support for VMX TSC scaling Haozhong Zhang <haozhong.zhang@intel.com> - 2015-09-28 07:40 +0200
    [PATCH 07/12] KVM: x86: Move TSC scaling logic out of call-back read_l1_tsc() Haozhong Zhang <haozhong.zhang@intel.com> - 2015-09-28 07:40 +0200
    [PATCH 11/12] KVM: VMX: Use a scaled host TSC for guest readings of MSR_IA32_TSC Haozhong Zhang <haozhong.zhang@intel.com> - 2015-09-28 07:40 +0200
    [PATCH 06/12] KVM: x86: Move TSC scaling logic out of call-back adjust_tsc_offset() Haozhong Zhang <haozhong.zhang@intel.com> - 2015-09-28 07:40 +0200
      Re: [PATCH 06/12] KVM: x86: Move TSC scaling logic out of call-back  adjust_tsc_offset() Paolo Bonzini <pbonzini@redhat.com> - 2015-09-28 22:20 +0200
        Re: [PATCH 06/12] KVM: x86: Move TSC scaling logic out of call-back  adjust_tsc_offset() Haozhong Zhang <haozhong.zhang@intel.com> - 2015-09-29 03:50 +0200
    [PATCH 01/12] KVM: x86: Collect information for setting TSC scaling ratio Haozhong Zhang <haozhong.zhang@intel.com> - 2015-09-28 07:40 +0200
      Re: [PATCH 01/12] KVM: x86: Collect information for setting TSC  scaling ratio Eric Northup <digitaleric@google.com> - 2015-09-29 05:30 +0200
        Re: [PATCH 01/12] KVM: x86: Collect information for setting TSC  scaling ratio Haozhong Zhang <haozhong.zhang@intel.com> - 2015-09-29 06:10 +0200
    [PATCH 02/12] KVM: x86: Add a common TSC scaling ratio field in kvm_vcpu_arch Haozhong Zhang <haozhong.zhang@intel.com> - 2015-09-28 07:50 +0200
      Re: [PATCH 02/12] KVM: x86: Add a common TSC scaling ratio field in  kvm_vcpu_arch Radim Krčmář <rkrcmar@redhat.com> - 2015-10-05 21:30 +0200
        Re: [PATCH 02/12] KVM: x86: Add a common TSC scaling ratio field in  kvm_vcpu_arch Haozhong Zhang <haozhong.zhang@intel.com> - 2015-10-06 04:00 +0200
    [PATCH 08/12] KVM: x86: Use the correct vcpu's TSC rate to compute time scale Haozhong Zhang <haozhong.zhang@intel.com> - 2015-09-28 07:50 +0200
      Re: [PATCH 08/12] KVM: x86: Use the correct vcpu's TSC rate to  compute time scale Radim Krčmář <rkrcmar@redhat.com> - 2015-10-05 22:20 +0200
    [PATCH 05/12] KVM: x86: Replace call-back compute_tsc_offset() with a common function Haozhong Zhang <haozhong.zhang@intel.com> - 2015-09-28 07:50 +0200
    [PATCH 03/12] KVM: x86: Add a common TSC scaling function Haozhong Zhang <haozhong.zhang@intel.com> - 2015-09-28 07:50 +0200
      Re: [PATCH 03/12] KVM: x86: Add a common TSC scaling function Paolo Bonzini <pbonzini@redhat.com> - 2015-09-28 22:20 +0200
        Re: [PATCH 03/12] KVM: x86: Add a common TSC scaling function Haozhong Zhang <haozhong.zhang@intel.com> - 2015-09-29 04:00 +0200
    [PATCH 10/12] KVM: VMX: Setup TSC scaling ratio when a vcpu is loaded Haozhong Zhang <haozhong.zhang@intel.com> - 2015-09-28 07:50 +0200
    [PATCH 04/12] KVM: x86: Replace call-back set_tsc_khz() with a common function Haozhong Zhang <haozhong.zhang@intel.com> - 2015-09-28 07:50 +0200
      Re: [PATCH 04/12] KVM: x86: Replace call-back set_tsc_khz() with a  common function Haozhong Zhang <haozhong.zhang@intel.com> - 2015-09-29 05:50 +0200
      Re: [PATCH 04/12] KVM: x86: Replace call-back set_tsc_khz() with a  common function Radim Krčmář <rkrcmar@redhat.com> - 2015-10-05 22:00 +0200
        Re: [PATCH 04/12] KVM: x86: Replace call-back set_tsc_khz() with a  common function David Matlack <dmatlack@google.com> - 2015-10-05 22:50 +0200
        Re: [PATCH 04/12] KVM: x86: Replace call-back set_tsc_khz() with a  common function Haozhong Zhang <haozhong.zhang@intel.com> - 2015-10-06 06:10 +0200
          Re: [PATCH 04/12] KVM: x86: Replace call-back set_tsc_khz() with a  common function Paolo Bonzini <pbonzini@redhat.com> - 2015-10-06 12:50 +0200
            Re: [PATCH 04/12] KVM: x86: Replace call-back set_tsc_khz() with a  common function Haozhong Zhang <haozhong.zhang@intel.com> - 2015-10-06 13:40 +0200
    Re: [PATCH 00/12] KVM: x86: add support for VMX TSC scaling Haozhong Zhang <haozhong.zhang@intel.com> - 2015-09-29 06:10 +0200
    Re: [PATCH 00/12] KVM: x86: add support for VMX TSC scaling Eric Northup <digitaleric@google.com> - 2015-09-29 06:10 +0200

Page 2 of 2 — ← Prev page 1 [2]


#1234684 — Re: [PATCH 04/12] KVM: x86: Replace call-back set_tsc_khz() with a common function

FromHaozhong Zhang <haozhong.zhang@intel.com>
Date2015-09-29 05:50 +0200
SubjectRe: [PATCH 04/12] KVM: x86: Replace call-back set_tsc_khz() with a common function
Message-ID<qdU5s-373-17@gated-at.bofh.it>
In reply to#1233863
On Mon, Sep 28, 2015 at 08:27:02PM -0700, Eric Northup wrote:
> On Sun, Sep 27, 2015 at 10:38 PM, Haozhong Zhang <haozhong.zhang@intel.com>
> wrote:
> 
> > Both VMX and SVM propagate virtual_tsc_khz in the same way, so this
> > patch removes the call-back set_tsc_khz() and replaces it with a common
> > function.
> >
> > Signed-off-by: Haozhong Zhang <haozhong.zhang@intel.com>
> > ---
> >  arch/x86/include/asm/kvm_host.h |  1 -
> >  arch/x86/kvm/svm.c              | 36 ------------------------------------
> >  arch/x86/kvm/vmx.c              | 17 -----------------
> >  arch/x86/kvm/x86.c              | 41
> > ++++++++++++++++++++++++++++++++++++++++-
> >  4 files changed, 40 insertions(+), 55 deletions(-)
> >
> > diff --git a/arch/x86/include/asm/kvm_host.h
> > b/arch/x86/include/asm/kvm_host.h
> > index 4f32c68..5a0c435 100644
> > --- a/arch/x86/include/asm/kvm_host.h
> > +++ b/arch/x86/include/asm/kvm_host.h
> > @@ -842,7 +842,6 @@ struct kvm_x86_ops {
> >
> >         bool (*has_wbinvd_exit)(void);
> >
> > -       void (*set_tsc_khz)(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool
> > scale);
> >         u64 (*read_tsc_offset)(struct kvm_vcpu *vcpu);
> >         void (*write_tsc_offset)(struct kvm_vcpu *vcpu, u64 offset);
> >
> > diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c
> > index 1a333bd..d46dcf3 100644
> > --- a/arch/x86/kvm/svm.c
> > +++ b/arch/x86/kvm/svm.c
> > @@ -1015,41 +1015,6 @@ static void init_sys_seg(struct vmcb_seg *seg,
> > uint32_t type)
> >         seg->base = 0;
> >  }
> >
> > -static void svm_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool
> > scale)
> > -{
> > -       u64 ratio;
> > -       u64 khz;
> > -
> > -       /* Guest TSC same frequency as host TSC? */
> > -       if (!scale) {
> > -               vcpu->arch.tsc_scaling_ratio = TSC_RATIO_DEFAULT;
> > -               return;
> > -       }
> > -
> > -       /* TSC scaling supported? */
> > -       if (!boot_cpu_has(X86_FEATURE_TSCRATEMSR)) {
> > -               if (user_tsc_khz > tsc_khz) {
> > -                       vcpu->arch.tsc_catchup = 1;
> > -                       vcpu->arch.tsc_always_catchup = 1;
> > -               } else
> > -                       WARN(1, "user requested TSC rate below hardware
> > speed\n");
> > -               return;
> > -       }
> > -
> > -       khz = user_tsc_khz;
> > -
> > -       /* TSC scaling required  - calculate ratio */
> > -       ratio = khz << 32;
> > -       do_div(ratio, tsc_khz);
> > -
> > -       if (ratio == 0 || ratio & TSC_RATIO_RSVD) {
> > -               WARN_ONCE(1, "Invalid TSC ratio - virtual-tsc-khz=%u\n",
> > -                               user_tsc_khz);
> > -               return;
> > -       }
> > -       vcpu->arch.tsc_scaling_ratio = ratio;
> > -}
> > -
> >  static u64 svm_read_tsc_offset(struct kvm_vcpu *vcpu)
> >  {
> >         struct vcpu_svm *svm = to_svm(vcpu);
> > @@ -4507,7 +4472,6 @@ static struct kvm_x86_ops svm_x86_ops = {
> >
> >         .has_wbinvd_exit = svm_has_wbinvd_exit,
> >
> > -       .set_tsc_khz = svm_set_tsc_khz,
> >         .read_tsc_offset = svm_read_tsc_offset,
> >         .write_tsc_offset = svm_write_tsc_offset,
> >         .adjust_tsc_offset = svm_adjust_tsc_offset,
> > diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
> > index 6407674..1751537 100644
> > --- a/arch/x86/kvm/vmx.c
> > +++ b/arch/x86/kvm/vmx.c
> > @@ -2255,22 +2255,6 @@ static u64 vmx_read_l1_tsc(struct kvm_vcpu *vcpu,
> > u64 host_tsc)
> >         return host_tsc + tsc_offset;
> >  }
> >
> > -/*
> > - * Engage any workarounds for mis-matched TSC rates.  Currently limited to
> > - * software catchup for faster rates on slower CPUs.
> > - */
> > -static void vmx_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool
> > scale)
> > -{
> > -       if (!scale)
> > -               return;
> > -
> > -       if (user_tsc_khz > tsc_khz) {
> > -               vcpu->arch.tsc_catchup = 1;
> > -               vcpu->arch.tsc_always_catchup = 1;
> > -       } else
> > -               WARN(1, "user requested TSC rate below hardware speed\n");
> > -}
> > -
> >  static u64 vmx_read_tsc_offset(struct kvm_vcpu *vcpu)
> >  {
> >         return vmcs_read64(TSC_OFFSET);
> > @@ -10380,7 +10364,6 @@ static struct kvm_x86_ops vmx_x86_ops = {
> >
> >         .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
> >
> > -       .set_tsc_khz = vmx_set_tsc_khz,
> >         .read_tsc_offset = vmx_read_tsc_offset,
> >         .write_tsc_offset = vmx_write_tsc_offset,
> >         .adjust_tsc_offset = vmx_adjust_tsc_offset,
> > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> > index 920c302..e2e1fdb 100644
> > --- a/arch/x86/kvm/x86.c
> > +++ b/arch/x86/kvm/x86.c
> > @@ -1248,6 +1248,45 @@ static u32 adjust_tsc_khz(u32 khz, s32 ppm)
> >         return v;
> >  }
> >
> > +static void set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool
> > scale)
> > +{
> > +       u64 ratio, khz;
> > +       s8 shift;
> > +
> > +       /* Guest TSC same frequency as host TSC? */
> > +       if (!scale) {
> > +               vcpu->arch.tsc_scaling_ratio =
> > kvm_default_tsc_scaling_ratio;
> > +               return;
> > +       }
> > +
> > +       /* TSC scaling supported? */
> > +       if (!kvm_has_tsc_control) {
> > +               if (user_tsc_khz > tsc_khz) {
> > +                       vcpu->arch.tsc_catchup = 1;
> > +                       vcpu->arch.tsc_always_catchup = 1;
> > +               } else
> > +                       WARN(1, "user requested TSC rate below hardware
> > speed\n");
> >
> 
> It was like this before, but why should KVM_SET_TSC_KHZ ioctl have return
> value of 0 in this case?  Failing the request would be better than kernel
> log spew (and below).
> 
>

Yes, failing the request is a better way. I'll change this and below.

> > +               return;
> > +       }
> > +
> > +       khz = user_tsc_khz;
> > +
> > +       /* TSC scaling required  - calculate ratio */
> > +       shift = (kvm_tsc_scaling_ratio_frac_bits <= 32) ?
> > +               kvm_tsc_scaling_ratio_frac_bits : 32;
> > +       ratio = khz << shift;
> > +       do_div(ratio, tsc_khz);
> > +       ratio <<= (kvm_tsc_scaling_ratio_frac_bits - shift);
> > +
> > +       if (ratio == 0 || ratio & kvm_tsc_scaling_ratio_rsvd) {
> > +               WARN_ONCE(1, "Invalid TSC scaling ratio -
> > virtual-tsc-khz=%u\n",
> > +                         user_tsc_khz);
> > +               return;
> > +       }
> > +
> > +       vcpu->arch.tsc_scaling_ratio = ratio;
> > +}
> > +
> >  static void kvm_set_tsc_khz(struct kvm_vcpu *vcpu, u32 this_tsc_khz)
> >  {
> >         u32 thresh_lo, thresh_hi;
> > @@ -1275,7 +1314,7 @@ static void kvm_set_tsc_khz(struct kvm_vcpu *vcpu,
> > u32 this_tsc_khz)
> >                 pr_debug("kvm: requested TSC rate %u falls outside
> > tolerance [%u,%u]\n", this_tsc_khz, thresh_lo, thresh_hi);
> >                 use_scaling = 1;
> >         }
> > -       kvm_x86_ops->set_tsc_khz(vcpu, this_tsc_khz, use_scaling);
> > +       set_tsc_khz(vcpu, this_tsc_khz, use_scaling);
> >  }
> >
> >  static u64 compute_guest_tsc(struct kvm_vcpu *vcpu, s64 kernel_ns)
> > --
> > 2.4.8
> >
> > --
> > 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
> >
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [prev] | [next] | [standalone]


#1239909 — Re: [PATCH 04/12] KVM: x86: Replace call-back set_tsc_khz() with a common function

FromRadim Krčmář <rkrcmar@redhat.com>
Date2015-10-05 22:00 +0200
SubjectRe: [PATCH 04/12] KVM: x86: Replace call-back set_tsc_khz() with a common function
Message-ID<qgk5r-kd-1@gated-at.bofh.it>
In reply to#1233863
2015-09-28 13:38+0800, Haozhong Zhang:
> Both VMX and SVM propagate virtual_tsc_khz in the same way, so this
> patch removes the call-back set_tsc_khz() and replaces it with a common
> function.
> 
> Signed-off-by: Haozhong Zhang <haozhong.zhang@intel.com>
> ---
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> +static void set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)
> +{
> +	u64 ratio, khz;
| [...]
> +	khz = user_tsc_khz;

I'd use "user_tsc_khz" directly.

> +	/* TSC scaling required  - calculate ratio */
> +	shift = (kvm_tsc_scaling_ratio_frac_bits <= 32) ?
> +		kvm_tsc_scaling_ratio_frac_bits : 32;
> +	ratio = khz << shift;
> +	do_div(ratio, tsc_khz);
> +	ratio <<= (kvm_tsc_scaling_ratio_frac_bits - shift);

VMX is losing 16 bits by this operation;  normal fixed point division
could get us a smaller drift (and an one-liner here) ...
at 4.3 GHz, 32 instead of 48 bits after decimal point translate to one
"lost" TSC tick per second, in the worst case.

Please mention that we are truncating on purpose :)
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [prev] | [next] | [standalone]


#1239949 — Re: [PATCH 04/12] KVM: x86: Replace call-back set_tsc_khz() with a common function

FromDavid Matlack <dmatlack@google.com>
Date2015-10-05 22:50 +0200
SubjectRe: [PATCH 04/12] KVM: x86: Replace call-back set_tsc_khz() with a common function
Message-ID<qgkRQ-1x8-13@gated-at.bofh.it>
In reply to#1239909
On Mon, Oct 5, 2015 at 12:53 PM, Radim Krčmář <rkrcmar@redhat.com> wrote:
> 2015-09-28 13:38+0800, Haozhong Zhang:
>> Both VMX and SVM propagate virtual_tsc_khz in the same way, so this
>> patch removes the call-back set_tsc_khz() and replaces it with a common
>> function.
>>
>> Signed-off-by: Haozhong Zhang <haozhong.zhang@intel.com>
>> ---
>> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
>> +static void set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)
>> +{
>> +     u64 ratio, khz;
> | [...]
>> +     khz = user_tsc_khz;
>
> I'd use "user_tsc_khz" directly.
>
>> +     /* TSC scaling required  - calculate ratio */
>> +     shift = (kvm_tsc_scaling_ratio_frac_bits <= 32) ?
>> +             kvm_tsc_scaling_ratio_frac_bits : 32;
>> +     ratio = khz << shift;
>> +     do_div(ratio, tsc_khz);
>> +     ratio <<= (kvm_tsc_scaling_ratio_frac_bits - shift);
>
> VMX is losing 16 bits by this operation;  normal fixed point division
> could get us a smaller drift (and an one-liner here) ...
> at 4.3 GHz, 32 instead of 48 bits after decimal point translate to one
> "lost" TSC tick per second, in the worst case.

We can easily avoid losing precision on x86_64 (divq allows a 128-bit
dividend). 32-bit can just lose the 16 bits of precision (TSC scaling
is only available on SkyLake, and I'd be surprised if there were
many hosts running KVM in protected mode on SkyLake :)).

>
> Please mention that we are truncating on purpose :)
> --
> 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
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [prev] | [next] | [standalone]


#1240144 — Re: [PATCH 04/12] KVM: x86: Replace call-back set_tsc_khz() with a common function

FromHaozhong Zhang <haozhong.zhang@intel.com>
Date2015-10-06 06:10 +0200
SubjectRe: [PATCH 04/12] KVM: x86: Replace call-back set_tsc_khz() with a common function
Message-ID<qgrJD-3gz-1@gated-at.bofh.it>
In reply to#1239909
On Mon, Oct 05, 2015 at 09:53:26PM +0200, Radim Krčmář wrote:
> 2015-09-28 13:38+0800, Haozhong Zhang:
> > Both VMX and SVM propagate virtual_tsc_khz in the same way, so this
> > patch removes the call-back set_tsc_khz() and replaces it with a common
> > function.
> > 
> > Signed-off-by: Haozhong Zhang <haozhong.zhang@intel.com>
> > ---
> > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> > +static void set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)
> > +{
> > +	u64 ratio, khz;
> | [...]
> > +	khz = user_tsc_khz;
> 
> I'd use "user_tsc_khz" directly.
>

I'll do so.

> > +	/* TSC scaling required  - calculate ratio */
> > +	shift = (kvm_tsc_scaling_ratio_frac_bits <= 32) ?
> > +		kvm_tsc_scaling_ratio_frac_bits : 32;
> > +	ratio = khz << shift;
> > +	do_div(ratio, tsc_khz);
> > +	ratio <<= (kvm_tsc_scaling_ratio_frac_bits - shift);
> 
> VMX is losing 16 bits by this operation;  normal fixed point division
> could get us a smaller drift (and an one-liner here) ...
> at 4.3 GHz, 32 instead of 48 bits after decimal point translate to one
> "lost" TSC tick per second, in the worst case.
>
> Please mention that we are truncating on purpose :)

It's intentional to avoid the potential overflow in
  khz << kvm_tsc_scaling_ratio_frac_bits.

For VMX where kvm_tsc_scaling_ratio_frac_bits == 48, the above
expression is only safe to left shift a pretty small khz (< 2^16 KHz
or 65.5 MHz). Thus, I decided to sacrifice the precision for safety.
I chose to truncate at the boundary of 32 bits which can handle
khz as large as 4294 GHz.

Though this truncation results in losing TSC ticks when khz is larger
than 4.3 GHz, the lost is however pretty small compared with the large
khz.

Alternatively, it's also possible to follow David's comment to use
divq on x86_64 to keep both precision and safety. On i386, it just
falls back to above truncating approach.

- Haozhong
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [prev] | [next] | [standalone]


#1240359 — Re: [PATCH 04/12] KVM: x86: Replace call-back set_tsc_khz() with a common function

FromPaolo Bonzini <pbonzini@redhat.com>
Date2015-10-06 12:50 +0200
SubjectRe: [PATCH 04/12] KVM: x86: Replace call-back set_tsc_khz() with a common function
Message-ID<qgxYK-3JK-13@gated-at.bofh.it>
In reply to#1240144

On 06/10/2015 06:06, Haozhong Zhang wrote:
> Alternatively, it's also possible to follow David's comment to use
> divq on x86_64 to keep both precision and safety. On i386, it just
> falls back to above truncating approach.

khz is just 32 bits, so we can do a 96/32 division.  And because this is
a slow path, we can code a generic u64*u32/u32 function and use it to do
(1 << kvm_tsc_scaling_ratio_frac_bits) * khz / tsc_khz:

diff --git a/include/linux/math64.h b/include/linux/math64.h
index c45c089bfdac..5b70af4fa386 100644
--- a/include/linux/math64.h
+++ b/include/linux/math64.h
@@ -142,6 +142,13 @@ static inline u64 mul_u64_u32_shr(u64 a, u32 mul,
unsigned int shift)
 }
 #endif /* mul_u64_u32_shr */

+#ifndef mul_u64_u32_div
+static inline u64 mul_u64_u32_div(u64 x, u32 num, u32 den)
+{
+	return (u64)(((unsigned __int128)a * mul) / den);
+}
+#endif
+
 #else

 #ifndef mul_u64_u32_shr
@@ -161,6 +168,35 @@ static inline u64 mul_u64_u32_shr(u64 a, u32 mul,
unsigned int shift)
 }
 #endif /* mul_u64_u32_shr */

+#ifndef mul_u64_u32_div
+static inline u64 mul_u64_u32_div(u64 a, u32 num, u32 den)
+{
+	union {
+		u64 ll;
+		struct {
+#ifdef __BIG_ENDIAN
+			u32 high, low;
+#else
+			u32 low, high;
+#endif
+		} l;
+	} u, rl, rh;
+
+	u.ll = a;
+	rl.ll = (u64)u.l.low * num;
+	rh.ll = (u64)u.l.high * num + rl.l.high;
+
+	/* Bits 32-63 of the result will be in rh.l.low.  */
+	rl.l.high = do_div(rh.ll, den);
+
+	/* Bits 0-31 of the result will be in rl.l.low.  */
+	do_div(rl.ll, den);
+
+	rl.l.high = rh.l.low;
+	return rl.ll;
+}
+#endif
+
 #endif

 #endif /* _LINUX_MATH64_H */
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [prev] | [next] | [standalone]


#1240381 — Re: [PATCH 04/12] KVM: x86: Replace call-back set_tsc_khz() with a common function

FromHaozhong Zhang <haozhong.zhang@intel.com>
Date2015-10-06 13:40 +0200
SubjectRe: [PATCH 04/12] KVM: x86: Replace call-back set_tsc_khz() with a common function
Message-ID<qgyL7-4TN-1@gated-at.bofh.it>
In reply to#1240359
On Tue, Oct 06, 2015 at 12:40:49PM +0200, Paolo Bonzini wrote:
> 
> 
> On 06/10/2015 06:06, Haozhong Zhang wrote:
> > Alternatively, it's also possible to follow David's comment to use
> > divq on x86_64 to keep both precision and safety. On i386, it just
> > falls back to above truncating approach.
> 
> khz is just 32 bits, so we can do a 96/32 division.  And because this is
> a slow path, we can code a generic u64*u32/u32 function and use it to do
> (1 << kvm_tsc_scaling_ratio_frac_bits) * khz / tsc_khz:
>

This is much better! Thanks Paolo! I'll use this mul_u64_u32_shr() in
the next version.

> diff --git a/include/linux/math64.h b/include/linux/math64.h
> index c45c089bfdac..5b70af4fa386 100644
> --- a/include/linux/math64.h
> +++ b/include/linux/math64.h
> @@ -142,6 +142,13 @@ static inline u64 mul_u64_u32_shr(u64 a, u32 mul,
> unsigned int shift)
>  }
>  #endif /* mul_u64_u32_shr */
> 
> +#ifndef mul_u64_u32_div
> +static inline u64 mul_u64_u32_div(u64 x, u32 num, u32 den)
> +{
> +	return (u64)(((unsigned __int128)a * mul) / den);
> +}
> +#endif
> +
>  #else
> 
>  #ifndef mul_u64_u32_shr
> @@ -161,6 +168,35 @@ static inline u64 mul_u64_u32_shr(u64 a, u32 mul,
> unsigned int shift)
>  }
>  #endif /* mul_u64_u32_shr */
> 
> +#ifndef mul_u64_u32_div
> +static inline u64 mul_u64_u32_div(u64 a, u32 num, u32 den)
> +{
> +	union {
> +		u64 ll;
> +		struct {
> +#ifdef __BIG_ENDIAN
> +			u32 high, low;
> +#else
> +			u32 low, high;
> +#endif
> +		} l;
> +	} u, rl, rh;
> +
> +	u.ll = a;
> +	rl.ll = (u64)u.l.low * num;
> +	rh.ll = (u64)u.l.high * num + rl.l.high;
> +
> +	/* Bits 32-63 of the result will be in rh.l.low.  */
> +	rl.l.high = do_div(rh.ll, den);
> +
> +	/* Bits 0-31 of the result will be in rl.l.low.  */
> +	do_div(rl.ll, den);
> +
> +	rl.l.high = rh.l.low;
> +	return rl.ll;
> +}
> +#endif
> +
>  #endif
> 
>  #endif /* _LINUX_MATH64_H */
> --
> 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
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [prev] | [next] | [standalone]


#1234708

FromHaozhong Zhang <haozhong.zhang@intel.com>
Date2015-09-29 06:10 +0200
Message-ID<qdUoO-3IA-3@gated-at.bofh.it>
In reply to#1233849
On Mon, Sep 28, 2015 at 09:00:39PM -0700, Eric Northup wrote:
> On Sun, Sep 27, 2015 at 10:37 PM, Haozhong Zhang
> <haozhong.zhang@intel.com> wrote:
> > This patchset adds support for VMX TSC scaling feature which is
> > available on Intel Skylake CPU. The specification of VMX TSC scaling
> > can be found at
> > http://www.intel.com/content/www/us/en/processors/timestamp-counter-scaling-virtualization-white-paper.html
> >
> > VMX TSC scaling allows guest TSC which is read by guest rdtsc(p)
> > instructions increases in a rate that is customized by the hypervisor
> > and can be different than the host TSC rate. Basically, VMX TSC
> > scaling adds a 64-bit field called TSC multiplier in VMCS so that, if
> > VMX TSC scaling is enabled, TSC read by guest rdtsc(p) instructions
> > will be calculated by the following formula:
> >
> >   guest EDX:EAX = (Host TSC * TSC multiplier) >> 48 + VMX TSC Offset
> >
> > where, Host TSC = Host MSR_IA32_TSC + Host MSR_IA32_TSC_ADJUST.
> >
> > This patchset, when cooperating with another QEMU patchset (sent in
> > another email "target-i386: save/restore vcpu's TSC rate during
> > migration"), allows guest programs observe a consistent TSC rate even
> > though they are migrated among machines with different host TSC rates.
> >
> > VMX TSC scaling shares some common logics with SVM TSC scaling which
> > is already supported by KVM. Patch 1 ~ 8 move those common logics from
> > SVM code to the common code. Upon them, patch 9 ~ 12 add VMX-specific
> > support for VMX TSC scaling.
> 
> reviewed-by: Eric Northup <digitaleric@google.com>
>

Thank you for the review!

> >
> > Haozhong Zhang (12):
> >   KVM: x86: Collect information for setting TSC scaling ratio
> >   KVM: x86: Add a common TSC scaling ratio field in kvm_vcpu_arch
> >   KVM: x86: Add a common TSC scaling function
> >   KVM: x86: Replace call-back set_tsc_khz() with a common function
> >   KVM: x86: Replace call-back compute_tsc_offset() with a common function
> >   KVM: x86: Move TSC scaling logic out of call-back adjust_tsc_offset()
> >   KVM: x86: Move TSC scaling logic out of call-back read_l1_tsc()
> >   KVM: x86: Use the correct vcpu's TSC rate to compute time scale
> >   KVM: VMX: Enable and initialize VMX TSC scaling
> >   KVM: VMX: Setup TSC scaling ratio when a vcpu is loaded
> >   KVM: VMX: Use a scaled host TSC for guest readings of MSR_IA32_TSC
> >   KVM: VMX: Dump TSC multiplier in dump_vmcs()
> >
> >  arch/x86/include/asm/kvm_host.h |  24 +++----
> >  arch/x86/include/asm/vmx.h      |   4 +-
> >  arch/x86/kvm/lapic.c            |   5 +-
> >  arch/x86/kvm/svm.c              | 113 +++--------------------------
> >  arch/x86/kvm/vmx.c              |  60 ++++++++--------
> >  arch/x86/kvm/x86.c              | 154 +++++++++++++++++++++++++++++++++++++---
> >  include/linux/kvm_host.h        |  21 +++++-
> >  7 files changed, 221 insertions(+), 160 deletions(-)
> >
> > --
> > 2.4.8
> >
> > --
> > 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
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [prev] | [next] | [standalone]


#1234709

FromEric Northup <digitaleric@google.com>
Date2015-09-29 06:10 +0200
Message-ID<qdUoO-3IA-5@gated-at.bofh.it>
In reply to#1233849
On Sun, Sep 27, 2015 at 10:37 PM, Haozhong Zhang
<haozhong.zhang@intel.com> wrote:
> This patchset adds support for VMX TSC scaling feature which is
> available on Intel Skylake CPU. The specification of VMX TSC scaling
> can be found at
> http://www.intel.com/content/www/us/en/processors/timestamp-counter-scaling-virtualization-white-paper.html
>
> VMX TSC scaling allows guest TSC which is read by guest rdtsc(p)
> instructions increases in a rate that is customized by the hypervisor
> and can be different than the host TSC rate. Basically, VMX TSC
> scaling adds a 64-bit field called TSC multiplier in VMCS so that, if
> VMX TSC scaling is enabled, TSC read by guest rdtsc(p) instructions
> will be calculated by the following formula:
>
>   guest EDX:EAX = (Host TSC * TSC multiplier) >> 48 + VMX TSC Offset
>
> where, Host TSC = Host MSR_IA32_TSC + Host MSR_IA32_TSC_ADJUST.
>
> This patchset, when cooperating with another QEMU patchset (sent in
> another email "target-i386: save/restore vcpu's TSC rate during
> migration"), allows guest programs observe a consistent TSC rate even
> though they are migrated among machines with different host TSC rates.
>
> VMX TSC scaling shares some common logics with SVM TSC scaling which
> is already supported by KVM. Patch 1 ~ 8 move those common logics from
> SVM code to the common code. Upon them, patch 9 ~ 12 add VMX-specific
> support for VMX TSC scaling.

reviewed-by: Eric Northup <digitaleric@google.com>

>
> Haozhong Zhang (12):
>   KVM: x86: Collect information for setting TSC scaling ratio
>   KVM: x86: Add a common TSC scaling ratio field in kvm_vcpu_arch
>   KVM: x86: Add a common TSC scaling function
>   KVM: x86: Replace call-back set_tsc_khz() with a common function
>   KVM: x86: Replace call-back compute_tsc_offset() with a common function
>   KVM: x86: Move TSC scaling logic out of call-back adjust_tsc_offset()
>   KVM: x86: Move TSC scaling logic out of call-back read_l1_tsc()
>   KVM: x86: Use the correct vcpu's TSC rate to compute time scale
>   KVM: VMX: Enable and initialize VMX TSC scaling
>   KVM: VMX: Setup TSC scaling ratio when a vcpu is loaded
>   KVM: VMX: Use a scaled host TSC for guest readings of MSR_IA32_TSC
>   KVM: VMX: Dump TSC multiplier in dump_vmcs()
>
>  arch/x86/include/asm/kvm_host.h |  24 +++----
>  arch/x86/include/asm/vmx.h      |   4 +-
>  arch/x86/kvm/lapic.c            |   5 +-
>  arch/x86/kvm/svm.c              | 113 +++--------------------------
>  arch/x86/kvm/vmx.c              |  60 ++++++++--------
>  arch/x86/kvm/x86.c              | 154 +++++++++++++++++++++++++++++++++++++---
>  include/linux/kvm_host.h        |  21 +++++-
>  7 files changed, 221 insertions(+), 160 deletions(-)
>
> --
> 2.4.8
>
> --
> 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
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

Back to top | Article view | linux.kernel


csiph-web