All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Roger Pau Monné" <roger.pau@citrix.com>
To: Jan Beulich <jbeulich@suse.com>
Cc: Andrew Cooper <andrew.cooper3@citrix.com>,
	Teddy Astie <teddy.astie@vates.tech>,
	xen-devel@lists.xenproject.org
Subject: Re: [PATCH 1/2] x86/time: use native TSC scaling factors when TSC is not scaled
Date: Thu, 16 Apr 2026 15:13:39 +0200	[thread overview]
Message-ID: <aeDgg2gB09-G2FQD@macbook.local> (raw)
In-Reply-To: <f424bf3a-0113-4c66-b165-8dae82817f24@suse.com>

On Thu, Apr 16, 2026 at 01:28:11PM +0200, Jan Beulich wrote:
> On 14.04.2026 12:33, Roger Pau Monne wrote:
> > When running HVM guest in native TSC mode avoid using the recalculated vTSC
> > scaling factors based on the cpu_khz value.  Using the kHz based frequency
> > leads to the TSC scaling values possibly not being the same as the ones
> > used by the per CPU cpu_time->tsc_scale field, which introduces skew
> > between the guest and Xen's calculations of the system time.
> > 
> > On a 2gHz system, where the frequency is possibly detected as 1999999999Hz
> > (note this is a worse-case scenario), the cpu_khz variable will be set to
> > 1999999kHz, and hence 999Hz cycles will be not accounted for per second.
> > Over a second (the time synchronization period), this leads to a skew of:
> > 
> > cycles * 1 / (Hz freq) = 999 / 1999999999 = 499,5ns
> > 
> > So far this has gone unnoticed because the time synchronization rendezvous
> > forces the update of the tsc_timestamp and system_time fields in the vCPU
> > time info area, and hence the skew only accumulates up to the rendezvous
> > period.  Attempting to remove the rendezvous causes the skew to grow
> > unbounded.
> > 
> > Fix by using the native TSC scaling values (as used by Xen) when the guest
> > TSC is not scaled.
> > 
> > Fixes: eab8a90be723 ("x86/time: scale host TSC in pvclock properly")
> > Signed-off-by: Roger Pau Monné <roger.pau@citrix.com>
> > ---
> > I'm worried about the usage of cpu_khz beyond simple printing it for
> > informational purposes.  Overall I think it would be safer to store the
> > frequency in Hz, as to avoid losing the least significant digits.
> > 
> > In any case, that's a different change.
> 
> I'm not quite sure - improving accuracy is of course a good thing, but will
> we ever be able to do any such calculations error free, when already the
> detected frequency isn't exactly precise?

I think getting them fully accurate is not strictly required.  The
specific issue here was that the guest was supposedly running with the
native TSC frequency, but the vCPU time info scaling factors where
(slightly) different from the ones using natively by Xen, hence resulting in a
time skew.

When the guest runs with a different TSC frequency Xen already
accounts for it properly, and hence there's no skew.

However, as noted in the next patch, I don't really see the benefit of
storing the frequency in kHz instead of using plain Hz.

Thanks, Roger.


  parent reply	other threads:[~2026-04-16 13:14 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-14 10:33 [PATCH 0/2] time: fix time accounting for x86 HVM guests Roger Pau Monne
2026-04-14 10:33 ` [PATCH 1/2] x86/time: use native TSC scaling factors when TSC is not scaled Roger Pau Monne
2026-04-16 11:28   ` Jan Beulich
2026-04-16 12:51     ` Roger Pau Monné
2026-04-16 12:57       ` Jan Beulich
2026-04-16 13:13     ` Roger Pau Monné [this message]
2026-04-16 13:21       ` Jan Beulich
2026-04-14 10:33 ` [PATCH 2/2] xen/cpu: round up cpu_khz calculations Roger Pau Monne
2026-04-14 11:36   ` Tu Dinh
2026-04-14 11:56     ` Roger Pau Monné
2026-04-16  7:55   ` Jan Beulich
2026-04-14 20:08 ` [PATCH 0/2] time: fix time accounting for x86 HVM guests Stefano Stabellini
2026-04-15  8:44 ` Marek Marczykowski-Górecki

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=aeDgg2gB09-G2FQD@macbook.local \
    --to=roger.pau@citrix.com \
    --cc=andrew.cooper3@citrix.com \
    --cc=jbeulich@suse.com \
    --cc=teddy.astie@vates.tech \
    --cc=xen-devel@lists.xenproject.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.