xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: Haozhong Zhang <haozhong.zhang@intel.com>
To: Boris Ostrovsky <boris.ostrovsky@oracle.com>
Cc: Kevin Tian <kevin.tian@intel.com>, Wei Liu <wei.liu2@citrix.com>,
	Ian Campbell <ian.campbell@citrix.com>,
	Stefano Stabellini <stefano.stabellini@eu.citrix.com>,
	Jun Nakajima <jun.nakajima@intel.com>,
	Andrew Cooper <andrew.cooper3@citrix.com>,
	Ian Jackson <ian.jackson@eu.citrix.com>,
	xen-devel@lists.xen.org,
	George Dunlap <George.Dunlap@eu.citrix.com>,
	Aravind Gopalakrishnan <Aravind.Gopalakrishnan@amd.com>,
	Jan Beulich <jbeulich@suse.com>, Keir Fraser <keir@xen.org>,
	Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>
Subject: Re: [PATCH 00/13] Add VMX TSC scaling support
Date: Tue, 24 Nov 2015 21:05:32 +0800	[thread overview]
Message-ID: <20151124130532.GA8995@hzzhang-desktop.sh.intel.com> (raw)
In-Reply-To: <565332B9.9040200@oracle.com>

On 11/23/15 10:37, Boris Ostrovsky wrote:
> On 11/22/2015 12:54 PM, Haozhong Zhang wrote:
> >Hi Jan, Boris and Aravind,
> >
> >(Sorry for sending such a long email and thanks for your patience)
> 
> First, thank you very much for doing this.
> 
> >
> >Because this patchset also touches the existing SVM TSC ratio code, I
> >tested it on an AMD machine with an AMD A10-7700K CPU (3.4 GHz) that
> >supports SVM TSC ratio. There are two goals of the test:
> >  (1) Check whether this patchset works well for SVM TSC ratio.
> >  (2) Check whether the existing SVM TSC ratio code works correctly.
> >
> >* TL;DR
> >   The detailed testing process is boring and long, so I put the
> >   conclusions first.
> >
> >   According to the following test,
> >   (1) this patchset works well for SVM TSC ratio, and
> >   (2) the existing SVM TSC ratio code does not work correctly.
> >
> >
> >* Preliminary bug fix
> >
> >   Before testing (specially for goal (2)), I have to fix another bug
> >   found in the current svm_get_tsc_offset() (commit e08f383):
> >
> >   static uint64_t svm_get_tsc_offset(uint64_t host_tsc, uint64_t guest_tsc,
> >     uint64_t ratio)
> >   {
> >       uint64_t offset;
> >
> >       if (ratio == DEFAULT_TSC_RATIO)
> >           return guest_tsc - host_tsc;
> >
> >       /* calculate hi,lo parts in 64bits to prevent overflow */
> >       offset = (((host_tsc >> 32U) * (ratio >> 32U)) << 32U) +
> >       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> >             (host_tsc & 0xffffffffULL) * (ratio & 0xffffffffULL);
> >             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> >             ^^ wrong
> >
> >       return guest_tsc - offset;
> >   }
> >
> >   Looking at the AMD's spec about TSC ratio MSR and where this function is
> >   called, it's expected to calculate
> >       guest_tsc - (host_tsc * ratio) >> 32
> >   but above underlined code is definitely not "(host_tsc * ratio) >> 32",
> >   and above function will return a much larger result than
> >   expected if (guest TSC rate / host TSC rate) > 1. In practice, it
> >   could result the guest TSC jumping to several years later after
> >   migration (which I came across and was confuse by in this test).
> 
> Yes, this is obviously wrong.
> 
> >
> >   This bug can be fixed either later by patch 5 which introduces a
> >   common function hvm_scale_tsc() to scale TSC, or by replacing above
> >   underlined code with a simplified and inlined version of
> >   hvm_scale_tsc() as below:
> >       uint64_t mult, frac;
> >       mult    = ratio >> 32;
> >       frac    = ratio & ((1ULL << 32) - 1);
> >       offset  = host_tsc * mult;                               
> >       offset += (host_tsc >> 32) * frac;                       
> >       offset += ((host_tsc & ((1ULL << 32) - 1)) * frac) >> 32; 
> 
> I am not sure I understand the last line (or maybe 2 lines)
>

Just simple math with carefulness to avoid 64-bit integer overflow:

suppose the most significant 32 bits of host_tsc and ratio are tsc_h
and mult, and the least significant 32 bits of them are tsc_l and
frac, then
    host_tsc * ratio * 2^-32
    = host_tsc * (mult * 2^32 + frac) * 2^-32
    = host_tsc * mult + (tsc_h * 2^32 + tsc_l) * frac * 2^-32
    = host_tsc * mult + tsc_h * frac + ((tsc_l * frac) >> 32)
      
All multiplications in the last line are between 32-bit integers, so none
of them could overflow 64-bit integers.

Consider a simple example that host_tsc = 1ULL << 33 and ratio = 0xffffffff.
Overflow happens in the multiplication of the second term of your formula below,
and all overflowed bits are lost in the next right shift.

Haozhong

> If by 'offset' here you are trying to calculate the scaled version of host
> TSC then I think it would be
> 
> (host_tsc * (ratio >> 32)) + ( (host_tsc * (ratio & 0xffffffff)) >> 32 )
> 
> (sanity check: assuming host_tsc is 8 and the ratio is 1.5 (i.e.
> 0x180000000) we get 12)
> 
> 
> -boris
> 
> 
> >   For testing goal (2), I apply the latter fix.
> >
> >
> >* Test for goal (1)
> >
> >   * Environment
> >     (1) Xen (commit e08f383)
> >     (2) Host Linux kernel 3.19.0
> >     (3) Guest Linux kernel 3.19.0 & 4.2.0
> >
> >   * Process
> >     (1) Apply the whole patchset on commit e08f383.
> >
> >     (2) Launch a HVM domain from the configuration xl-high.cfg (in
> >         attachment).
> >
> >         Expected: The guest Linux should boot normally in the domain.
> >
> >     (3) Execute the command "dmesg | grep -i tsc" in the guest Linux
> >         to check the TSC rate detected by the guest Linux.
> >
> >         Expected: Suppose the detected TSC rate is 'gtsc_khz' in KHz,
> >	          then it should be as close to the value of 'vtsc_khz'
> >		  option in xl-high.cfg as possible.
> >
> >     (4) Execute the program "./test_tsc <nr_secs> gtsc_khz" to check
> >         whether the guest TSC rate is synchronized with the wall clock.
> >         The code of test_tsc is also in the attachment. It records the
> >         beginning and ending TSC values (tsc0 and tsc1) for a period
> >         of nr_secs and outputs the result of
> >	(tsc1 - tsc0) / (gtsc_khz * 1000).
> >
> >         Expected: The output should be as close to nr_secs as possible.
> >
> >      Follows test the migration.
> >
> >      (5) Save the current domain by "xl save hvm-test saved_domain".
> >
> >      (6) Restore the domain.
> >
> >      (7) Take above step (4) again to check whether the guest TSC rate
> >          is still synchronized with the wall clock.
> >
> >          Expected: the same as step (5)
> >
> >      (8) Switch to the configuration xl-low.cfg and take above
> >          steps (2) ~ (6) again.
> >
> >   * Results (OK: All as expected)
> >     First round w/ xl-high.cfg (vtsc_khz = 4000000):
> >     (3) gtsc_khz = 4000000 KHz
> >     (4) ./test_tsc 10 4000000   outputs: Passed 9.99895 s
> >         ./test_tsc 3600 4000000 outputs: Passed 3599.99754 s
> >     (7) ./test_tsc 10 4000000   outputs: Passed 9.99885 s
> >         ./test_tsc 3600 4000000 outputs: Passed 3599.98987 s
> >
> >     Second round w/ xl-low.cfg (vtsc_khz = 2000000):
> >     (3) gtsc_khz = 2000000 KHz
> >     (4) ./test_tsc 10 4000000   outputs: Passed 9.99886 s
> >         ./test_tsc 3600 4000000 outputs: Passed 3599.99810 s
> >     (7) ./test_tsc 10 4000000   outputs: Passed 9.99885 s
> >         ./test_tsc 3600 4000000 outputs: Passed 3599.99853 s
> >
> >    I also switched the clocksource of guest Linux to 'hpet' and got
> >    very similar results to above.
> >
> >
> >* Test for goal (2)
> >
> >   * Environment
> >     The same as above
> >
> >   * Process
> >     (1) ~ (5): the same as above.
> >     (6) Reboot to Xen hypervisor and toolstack w/o this patchset but
> >         w/ the bug fix at the beginning and restore the domain.
> >     (7) the same as above.
> >
> >   * Results (Failed)
> >     (7) ./test_tsc 10 4000000 outputs: Passed 63.319284 s
> >
> >
> >* Conclusion
> >
> >   This patchset works well for SVM TSC ratio and fixes existing bugs
> >   in SVM TSC ratio code.
> >
> >
> >Thanks for your patience to read such a long email,
> >Haozhong
> >
> 
> 
> _______________________________________________
> Xen-devel mailing list
> Xen-devel@lists.xen.org
> http://lists.xen.org/xen-devel

  reply	other threads:[~2015-11-24 13:05 UTC|newest]

Thread overview: 117+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-09-28  7:13 [PATCH 00/13] Add VMX TSC scaling support Haozhong Zhang
2015-09-28  7:13 ` [PATCH 01/13] x86/time.c: Use system time to calculate elapsed_nsec in tsc_get_info() Haozhong Zhang
2015-10-09  6:51   ` Jan Beulich
2015-10-09 13:41     ` Boris Ostrovsky
2015-10-09 14:00       ` Haozhong Zhang
2015-10-09 15:11         ` Jan Beulich
2015-10-09 16:09           ` Boris Ostrovsky
2015-10-09 16:19             ` Jan Beulich
2015-10-09 16:31               ` Boris Ostrovsky
2015-10-09 16:51                 ` Haozhong Zhang
2015-10-09 18:59                   ` Boris Ostrovsky
2015-10-09 14:39       ` Jan Beulich
2015-10-09 15:37         ` Boris Ostrovsky
2015-10-09 16:39           ` Haozhong Zhang
2015-10-09 16:44             ` Boris Ostrovsky
2015-10-09 14:35     ` Haozhong Zhang
2015-10-09 14:43       ` Jan Beulich
2015-10-09 15:56         ` Boris Ostrovsky
2015-10-14  2:45     ` Haozhong Zhang
2015-10-14  9:40       ` Jan Beulich
2015-10-14 10:00         ` Haozhong Zhang
2015-10-14 10:20           ` Jan Beulich
2015-10-14 10:24             ` Haozhong Zhang
2015-09-28  7:13 ` [PATCH 02/13] x86/time.c: Get the correct guest TSC rate " Haozhong Zhang
2015-09-28  7:13 ` [PATCH 03/13] x86/hvm: Collect information of TSC scaling ratio Haozhong Zhang
2015-10-22 12:53   ` Jan Beulich
2015-10-22 14:40     ` Haozhong Zhang
2015-10-22 14:51       ` Jan Beulich
2015-10-22 15:57         ` Haozhong Zhang
2015-09-28  7:13 ` [PATCH 04/13] x86/hvm: Setup " Haozhong Zhang
2015-10-22 13:13   ` Jan Beulich
2015-10-22 15:55     ` Haozhong Zhang
2015-10-22 16:05       ` Jan Beulich
2015-10-22 16:39         ` Haozhong Zhang
2015-10-23  7:44     ` Haozhong Zhang
2015-10-23  7:59       ` Jan Beulich
2015-10-23  8:18         ` Haozhong Zhang
2015-10-23  8:31           ` Jan Beulich
2015-10-23  8:40             ` Haozhong Zhang
2015-10-23  9:18               ` Jan Beulich
2015-09-28  7:13 ` [PATCH 05/13] x86/hvm: Replace architecture TSC scaling by a common function Haozhong Zhang
2015-10-22 13:52   ` Jan Beulich
2015-10-23  0:49     ` Haozhong Zhang
2015-09-28  7:13 ` [PATCH 06/13] x86/hvm: Scale host TSC when setting/getting guest TSC Haozhong Zhang
2015-10-22 14:17   ` Jan Beulich
2015-10-22 15:44     ` Boris Ostrovsky
2015-10-22 16:23       ` Haozhong Zhang
2015-10-27 20:16       ` Aravind Gopalakrishnan
2015-10-28  1:51         ` Haozhong Zhang
2015-11-09  7:43         ` Haozhong Zhang
2015-11-12 13:50           ` George Dunlap
2015-10-22 16:03     ` Haozhong Zhang
2015-10-27  1:54     ` Haozhong Zhang
2015-10-27  8:15       ` Jan Beulich
2015-10-27  8:25         ` Haozhong Zhang
2015-10-27  8:44     ` Haozhong Zhang
2015-10-27 13:10       ` Boris Ostrovsky
2015-10-27 13:55         ` Boris Ostrovsky
2015-10-27 16:13           ` haozhong.zhang
2015-10-27 16:13         ` haozhong.zhang
2015-09-28  7:13 ` [PATCH 07/13] x86/hvm: Move saving/loading vcpu's TSC to common code Haozhong Zhang
2015-10-22 14:54   ` Jan Beulich
2015-09-28  7:13 ` [PATCH 08/13] x86/hvm: Detect TSC scaling through hvm_funcs in tsc_set_info() Haozhong Zhang
2015-10-22 15:01   ` Jan Beulich
2015-09-28  7:13 ` [PATCH 09/13] x86/time.c: Scale host TSC in pvclock properly Haozhong Zhang
2015-09-28 16:36   ` Boris Ostrovsky
2015-09-29  0:19     ` Haozhong Zhang
2015-10-22 15:50   ` Boris Ostrovsky
2015-10-22 16:44     ` Haozhong Zhang
2015-10-22 19:15       ` Boris Ostrovsky
2015-09-28  7:13 ` [PATCH 10/13] vmx: Detect and initialize VMX RDTSC(P) scaling Haozhong Zhang
2015-10-27 13:19   ` Jan Beulich
2015-10-27 16:17     ` Haozhong Zhang
2015-09-28  7:13 ` [PATCH 11/13] vmx: Use scaled host TSC to calculate TSC offset Haozhong Zhang
2015-10-22 15:55   ` Boris Ostrovsky
2015-10-22 17:12     ` Haozhong Zhang
2015-10-22 19:19       ` Boris Ostrovsky
2015-10-23  0:52         ` Haozhong Zhang
2015-10-27 13:29   ` Jan Beulich
2015-10-27 16:21     ` Haozhong Zhang
2015-09-28  7:13 ` [PATCH 12/13] vmx: Add a call-back to apply TSC scaling ratio to hardware Haozhong Zhang
2015-09-28 16:02   ` Boris Ostrovsky
2015-09-29  1:07     ` Haozhong Zhang
2015-09-29  9:33       ` Andrew Cooper
2015-09-29 10:02         ` Haozhong Zhang
2015-09-29 10:25           ` Andrew Cooper
2015-09-29 13:59             ` Haozhong Zhang
2015-10-27 13:33   ` Jan Beulich
2015-10-28  2:41     ` Haozhong Zhang
2015-09-28  7:13 ` [PATCH 13/13] tools/libxl: Add 'vtsc_khz' option to set guest TSC rate Haozhong Zhang
2015-09-28 11:47   ` Julien Grall
2015-09-28 12:11     ` Haozhong Zhang
2015-09-28 14:19   ` Wei Liu
2015-09-29  0:40     ` Haozhong Zhang
2015-09-29  9:20       ` Wei Liu
2015-09-29  9:50         ` Haozhong Zhang
2015-09-29 10:24           ` Julien Grall
2015-09-29 10:07       ` Ian Campbell
2015-09-29 10:33         ` Wei Liu
2015-09-29 12:57         ` Haozhong Zhang
2015-09-29 10:04   ` Ian Campbell
2015-09-29 10:13     ` Haozhong Zhang
2015-09-29 10:24       ` Andrew Cooper
2015-09-29 10:28         ` Ian Campbell
2015-09-29 10:31           ` Andrew Cooper
2015-09-29 13:53           ` Haozhong Zhang
2015-09-29 13:56             ` Andrew Cooper
2015-09-29 14:01               ` Haozhong Zhang
2015-09-29 14:37                 ` Ian Campbell
2015-09-29 15:16                   ` Haozhong Zhang
2015-09-28 10:51 ` [PATCH 00/13] Add VMX TSC scaling support Andrew Cooper
2015-09-28 13:48   ` Boris Ostrovsky
2015-11-22 17:54 ` Haozhong Zhang
2015-11-23 15:37   ` Boris Ostrovsky
2015-11-24 13:05     ` Haozhong Zhang [this message]
2015-11-24 14:19       ` Boris Ostrovsky
2015-11-24 14:25       ` Haozhong Zhang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20151124130532.GA8995@hzzhang-desktop.sh.intel.com \
    --to=haozhong.zhang@intel.com \
    --cc=Aravind.Gopalakrishnan@amd.com \
    --cc=George.Dunlap@eu.citrix.com \
    --cc=andrew.cooper3@citrix.com \
    --cc=boris.ostrovsky@oracle.com \
    --cc=ian.campbell@citrix.com \
    --cc=ian.jackson@eu.citrix.com \
    --cc=jbeulich@suse.com \
    --cc=jun.nakajima@intel.com \
    --cc=keir@xen.org \
    --cc=kevin.tian@intel.com \
    --cc=stefano.stabellini@eu.citrix.com \
    --cc=suravee.suthikulpanit@amd.com \
    --cc=wei.liu2@citrix.com \
    --cc=xen-devel@lists.xen.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).