Re: [PATCH 3/4] KVM: x86: introduce get_kvmclock_ns

From: Roman Kagan
Date: Sat Sep 03 2016 - 04:27:58 EST


On Fri, Sep 02, 2016 at 06:37:59PM +0200, Paolo Bonzini wrote:
>
>
> On 02/09/2016 16:51, Roman Kagan wrote:
> > On Fri, Sep 02, 2016 at 04:09:42PM +0200, Paolo Bonzini wrote:
> >> On 02/09/2016 15:52, Roman Kagan wrote:
> >> vs. using a single offset as in the TSC ref page is one nanosecond---and
> >> the ref page only has a resolution of 100 ns.
> >
> > AFAICS it's not a matter of resolution. If one calculation flips from
> > value T to T+1 at tsc1, while the other at tsc2, during the window
> > between tsc1 and tsc2 we can have monotonicity violation.
>
> Ok, tried "empirically" (throw numbers in a spreadsheet :)) and indeed
> the maximum error is not 1 ns but 100 ns (1 unit in the time reference
> count MSR).
>
> You can get a flip between T/T+1 because the time reference counter may
> be more precise with its rounding due to the separation between
> tsc_timestamp and system_time. This separation provides some extra
> decimal digits to the offset, which the TSC page lacks. For example:
>
> 51256391 tsc_timestamp
> 3311474323 tsc_to_system_mul
> 254246 system_time
> -1 shift
> -195054.1816 offset (computed exactly)
>
> So the flip happens when the nanoseconds are around 81/82:
>
> tsc kvmclock refcount TSC page
> 51256391 254246 2542 2542
> 51256483 254281 2542 2542
> 51256484 254281 2542 2543
> 51256486 254282 2542 2543
> 51256746 254382 2543 2544
>
> I'll change patch 4 to store the parameters and use them when accessing
> the time reference counter MSR. I'll still keep the procedure that goes
> through kvmclock. It's a bit more involved for the scale, but
> vcpu->last_guest_tsc only provides a part of the offset computation; the
> other half is vcpu->hv_clock.system_time and it's not stored anywhere.

Erm... It is stored right there, in vcpu->hv_clock.system_time, you can
access it just fine when you populate tsc_ref_page values. Am I missing
anything?

Roman.