Re: [PATCH 1/1] selftests: KVM: add test to print boottime wallclock

2023-10-20 Thread David Woodhouse
On Fri, 2023-10-20 at 07:39 -0700, Dongli Zhang wrote: > > 1. Capture boottime_wallclock_01. > 2. Wait for 10-second by default (configurable, e.g., max 60-second) > 3. Capture boottime_wallclock_02. > 4. Report error if drift. > > > I have another pvclock vCPU hotplug test with the same flow.

[PATCH] KVM: selftests: add -MP to CFLAGS

2023-10-28 Thread David Woodhouse
From: David Woodhouse Using -MD without -MP causes build failures when a header file is deleted or moved. With -MP, the compiler will emit phony targets for the header files it lists as dependencies, and the Makefiles won't refuse to attempt to rebuild a C unit which no longer include

Re: [PATCH] KVM: selftests: add -MP to CFLAGS

2023-10-30 Thread David Woodhouse
On Sun, 2023-10-29 at 21:13 +, David Laight wrote: > From: David Woodhouse > > Sent: 28 October 2023 20:35 > > > > Using -MD without -MP causes build failures when a header file is deleted > > or moved. With -MP, the compiler will emit phony targets for the h

Re: [PATCH] KVM: selftests: add -MP to CFLAGS

2023-10-30 Thread David Woodhouse
On Mon, 2023-10-30 at 10:08 +, David Laight wrote: > From: David Woodhouse > > Sent: 30 October 2023 09:46 > > > > On Sun, 2023-10-29 at 21:13 +, David Laight wrote: > > > From: David Woodhouse > > > > Sent: 28 October 2023 20:35 > >

Re: [PATCH v10 09/19] KVM: xen: separate initialization of shared_info cache and content

2023-12-14 Thread David Woodhouse
age dirty. > +    */ > +   wall_nsec = ktime_get_real_ns() - get_kvmclock_ns(kvm); (Hint: it's commit 5d6d6a7d7e66a) With that fixed, Reviewed-by: David Woodhouse smime.p7s Description: S/MIME cryptographic signature

Re: [PATCH v10 10/19] KVM: xen: (re-)initialize shared_info if guest (32/64-bit) mode is set

2023-12-14 Thread David Woodhouse
ode is either done explicitly by the VMM via the > KVM_XEN_ATTR_TYPE_LONG_MODE attribute, or implicitly when the guest writes > the MSR to set up the hypercall page. > > Signed-off-by: Paul Durrant Reviewed-by: David Woodhouse smime.p7s Description: S/MIME cryptographic signature

Re: [PATCH v10 16/19] KVM: xen: split up kvm_xen_set_evtchn_fast()

2023-12-14 Thread David Woodhouse
On Mon, 2023-12-04 at 14:43 +, Paul Durrant wrote: > From: Paul Durrant > > The implementation of kvm_xen_set_evtchn_fast() is a rather lengthy piece > of code that performs two operations: updating of the shared_info > evtchn_pending mask, and updating of the vcpu_info evtchn_pending_sel > m

Re: [PATCH v10 17/19] KVM: xen: don't block on pfncache locks in kvm_xen_set_evtchn_fast()

2023-12-14 Thread David Woodhouse
o lock the same as an > invalid cache. > > [1] > https://lore.kernel.org/lkml/99771ef3a4966a01fefd3adbb2ba9c3a75f97cf2.ca...@infradead.org/T/#mbd06e5a04534ce9c0ee94bd8f1e8d942b2d45bd6 > > Signed-off-by: Paul Durrant Reviewed-by: David Woodhouse Although I would prefer the co

Re: [PATCH v10 18/19] KVM: pfncache: check the need for invalidation under read lock first

2023-12-14 Thread David Woodhouse
r a match; only taking a > write lock to actually perform an invalidation (after a another check). > > Signed-off-by: Paul Durrant Reviewed-by: David Woodhouse In particular, the previous 'don't block on pfncache locks in kvm_xen_set_evtchn_fast()' patch in this series is ea

Re: [PATCH v10 16/19] KVM: xen: split up kvm_xen_set_evtchn_fast()

2023-12-14 Thread David Woodhouse
On Thu, 2023-12-14 at 14:00 +, David Woodhouse wrote: > > > +   if (IS_ENABLED(CONFIG_64BIT) && kvm->arch.xen.long_mode) { > > +   struct vcpu_info *vcpu_info = gpc->khva; > > +   u32 port_word_bit = port / 32; > > Shouldn

Re: [PATCH v11 16/19] KVM: xen: split up kvm_xen_set_evtchn_fast()

2023-12-19 Thread David Woodhouse
On 19 December 2023 16:11:06 GMT, Paul Durrant wrote: >From: Paul Durrant > >The implementation of kvm_xen_set_evtchn_fast() is a rather lengthy piece >of code that performs two operations: updating of the shared_info >evtchn_pending mask, and updating of the vcpu_info evtchn_pending_sel >mask. I

Re: [PATCH v12 00/20] KVM: xen: update shared_info and vcpu_info handling

2024-01-25 Thread David Woodhouse
> > > > The v11 patch failed to set the return code of the ioctl if the mode > > was not actually changed, leading to a spurious failure. > > > > This version of the series also contains a new bug-fix to the pfncache > > code from David Woodhouse. > &

Re: [PATCH v12 03/20] KVM: xen: mark guest pages dirty with the pfncache lock held

2024-02-06 Thread David Woodhouse
On Tue, 2024-02-06 at 19:17 -0800, Sean Christopherson wrote: > KVM: x86/xen: for the scope please.  A few commits have "KVM: xen:", but > "x86/xen" > is the overwhelming favorite. Paul's been using "KVM: xen:" in this patch series since first posting it in September of last year. If there aren't

Re: [PATCH v12 08/20] KVM: pfncache: allow a cache to be activated with a fixed (userspace) HVA

2024-02-06 Thread David Woodhouse
On Tue, 2024-02-06 at 20:03 -0800, Sean Christopherson wrote: > +s390 folks (question on kvm_is_error_gpa() for ya) > > On Mon, Jan 15, 2024, Paul Durrant wrote: > > @@ -1398,7 +1414,9 @@ void kvm_gpc_deactivate(struct gfn_to_pfn_cache *gpc); > >   static inline void kvm_gpc_mark_dirty(struct gfn_

Re: [PATCH v12 17/20] KVM: xen: don't block on pfncache locks in kvm_xen_set_evtchn_fast()

2024-02-06 Thread David Woodhouse
On Tue, 2024-02-06 at 20:17 -0800, Sean Christopherson wrote: > On Mon, Jan 15, 2024, Paul Durrant wrote: > > From: Paul Durrant > > > > As described in [1] compiling with CONFIG_PROVE_RAW_LOCK_NESTING shows that > > kvm_xen_set_evtchn_fast() is blocking on pfncache locks in IRQ context. > > Ther

Re: [PATCH v12 18/20] KVM: pfncache: check the need for invalidation under read lock first

2024-02-06 Thread David Woodhouse
On Tue, 2024-02-06 at 20:22 -0800, Sean Christopherson wrote: > On Mon, Jan 15, 2024, Paul Durrant wrote: > > From: Paul Durrant > > > > Taking a write lock on a pfncache will be disruptive if the cache is > > *Unnecessarily* taking a write lock. No. Taking a write lock will be disrupting. Unn

Re: [PATCH v12 18/20] KVM: pfncache: check the need for invalidation under read lock first

2024-02-06 Thread David Woodhouse
On Tue, 2024-02-06 at 20:47 -0800, Sean Christopherson wrote: > > I'm saying this: > >   When processing mmu_notifier invalidations for gpc caches, pre-check for >   overlap with the invalidation event while holding gpc->lock for read, and >   only take gpc->lock for write if the cache needs to b

Re: [PATCH v12 11/20] KVM: xen: allow shared_info to be mapped by fixed HVA

2024-02-08 Thread David Woodhouse
On Thu, 2024-02-08 at 16:51 +, Paul Durrant wrote: > On 08/02/2024 16:48, Sean Christopherson wrote: > > On Thu, Feb 08, 2024, Paul Durrant wrote: > > > With the benefit of some sleep, I'm wondering why 0 is a 'ridiculous' > > > invalid value for a *virtual* address? Surely it's essentially a n

Re: [PATCH v13 21/21] KVM: pfncache: rework __kvm_gpc_refresh() to fix locking issues

2024-02-16 Thread David Woodhouse
On Thu, 2024-02-15 at 15:29 +, Paul Durrant wrote: > From: David Woodhouse > > This function can race with kvm_gpc_deactivate(), which does not take > the ->refresh_lock. This means kvm_gpc_deactivate() can wipe the ->pfn > and ->khva fields, and unmap the latter

Re: [PATCH v13 21/21] KVM: pfncache: rework __kvm_gpc_refresh() to fix locking issues

2024-02-17 Thread David Woodhouse
On Fri, 2024-02-16 at 07:52 -0800, Sean Christopherson wrote: > > FWIW, I'm not opposed to overhauling the gpc locking, I agree it's a mess.  I > just > want to proceed slower than I would for a fix, it's a lot to digest. Agreed. I'll do a *really* simple expansion of ->refresh_lock for the fix,

Re: [PATCH v13 00/21] KVM: xen: update shared_info and vcpu_info handling

2024-02-20 Thread David Woodhouse
On 20 February 2024 17:15:06 CET, Sean Christopherson wrote: >On Tue, Feb 20, 2024, Paul Durrant wrote: >> On 20/02/2024 15:55, Sean Christopherson wrote: >> > On Thu, 15 Feb 2024 15:28:55 +, Paul Durrant wrote: >> > > From: Paul Durrant >> > > >> > > This series contains a new patch from Se

Re: [PATCH 1/1] KVM: selftests: add kvmclock drift test

2024-04-04 Thread David Woodhouse
On Sat, 2024-01-06 at 00:33 -0800, Dongli Zhang wrote: > There is kvmclock drift issue during the vCPU hotplug. It has been fixed by > the commit c52ffadc65e2 ("KVM: x86: Don't unnecessarily force masterclock > update on vCPU hotplug"). > > This is to add the test to verify if the master clock is

Re: [PATCH 2/2] KVM: selftests: Add KVM/PV clock selftest to prove timer drift correction

2024-04-08 Thread David Woodhouse
On Mon, 2024-04-08 at 17:43 -0700, Dongli Zhang wrote: > Hi Jack, > > On 4/8/24 15:07, Jack Allister wrote: > > This test proves that there is an inherent KVM/PV clock drift away from the > > guest TSC when KVM decides to update the PV time information structure due > > to a KVM_REQ_MASTERCLOCK_UP

Re: [PATCH RFC 0/3] add support for mm-local memory allocations

2024-07-04 Thread David Woodhouse
On Fri, 2024-06-21 at 22:14 +0200, Roman Kagan wrote: > > Compared to the approach used in the orignal series, where a dedicated kernel > address range and thus a dedicated PGD was used for mm-local allocations, the > one proposed here may have certain drawbacks, in particular > > - using user ad

Re: [PATCH 2/2] KVM: selftests: Add KVM/PV clock selftest to prove timer drift correction

2024-04-11 Thread David Woodhouse
On Wed, 2024-04-10 at 10:15 +, Allister, Jack wrote: > > AFAIR, I copied check_clocksource() from existing code during that > > time. > > > The commit e440c5f2e ("KVM: selftests: Generalize check_clocksource() > > from kvm_clock_test") has introduced sys_clocksource_is_tsc(). Later > > it is r

[PATCH 07/10] KVM: x86: Avoid NTP frequency skew for KVM clock on 32-bit host

2024-04-18 Thread David Woodhouse
From: David Woodhouse Commit 53fafdbb8b21 ("KVM: x86: switch KVMCLOCK base to monotonic raw clock") did so only for 64-bit hosts, by capturing the boot offset from within the existing clocksource notifier update_pvclock_gtod(). That notifier was added in commit 16e8d74d2da9 ("KVM

[PATCH 04/10] KVM: selftests: Add KVM/PV clock selftest to prove timer correction

2024-04-18 Thread David Woodhouse
37 corrected=5038374945 * delta_uncorrected=3509 delta_corrected=1 Signed-off-by: Jack Allister Signed-off-by: David Woodhouse CC: Paul Durrant CC: Dongli Zhang --- tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/x86_64/pvclock_test.c | 192 ++ 2 files changed, 19

[PATCH 02/10] KVM: x86: Improve accuracy of KVM clock when TSC scaling is in force

2024-04-18 Thread David Woodhouse
From: David Woodhouse The kvm_guest_time_update() function scales the host TSC frequency to the guest's using kvm_scale_tsc() and the v->arch.l1_tsc_scaling_ratio scaling ratio previously calculated for that vCPU. Then calcuates the scaling factors for the KVM clock itself based on th

[PATCH 06/10] KVM: x86: Add KVM_VCPU_TSC_SCALE and fix the documentation on TSC migration

2024-04-18 Thread David Woodhouse
From: David Woodhouse The documentation on TSC migration using KVM_VCPU_TSC_OFFSET is woefully inadequate. It ignores TSC scaling, and ignores the fact that the host TSC may differ from one host to the next (and in fact because of the way the kernel calibrates it, it generally differs from one

[RFC PATCH 0/10] Cleaning up the KVM clock mess

2024-04-18 Thread David Woodhouse
from VMMs, but I think it's ready for some heckling at least. https://git.infradead.org/users/dwmw2/linux.git/shortlog/refs/heads/clocks David Woodhouse (8): KVM: x86/xen: Do not corrupt KVM clock in kvm_xen_shared_info_init() KVM: x86: Improve accuracy of KVM clock when TSC s

[PATCH 01/10] KVM: x86/xen: Do not corrupt KVM clock in kvm_xen_shared_info_init()

2024-04-18 Thread David Woodhouse
From: David Woodhouse The KVM clock is an interesting thing. It is defined as "nanoseconds since the guest was created", but in practice it runs at two *different* rates — or three different rates, if you count implementation bugs. Definition A is that it runs synchronousl

[PATCH 05/10] KVM: x86: Explicitly disable TSC scaling without CONSTANT_TSC

2024-04-18 Thread David Woodhouse
From: David Woodhouse KVM does make an attempt to cope with non-constant TSC, and has notifiers to handle host TSC frequency changes. However, it *only* adjusts the KVM clock, and doesn't adjust TSC frequency scaling when the host changes. This is presumably because non-constant TSCs were

[PATCH 08/10] KVM: x86: Remove periodic global clock updates

2024-04-18 Thread David Woodhouse
From: David Woodhouse This effectively reverts commit 332967a3eac0 ("x86: kvm: introduce periodic global clock updates"). The periodic update was introduced to propagate NTP corrections to the guest KVM clock, when the KVM clock was based on CLOCK_MONOTONIC. However, commit 53fafdbb

[PATCH 09/10] KVM: x86: Kill KVM_REQ_GLOBAL_CLOCK_UPDATE

2024-04-18 Thread David Woodhouse
From: David Woodhouse This was introduced in commit 0061d53daf26 ("KVM: x86: limit difference between kvmclock updates") to reduce cross-vCPU differences which arose because the KVM clock was based on CLOCK_MONOTONIC and thus subject to NTP frequency corrections. However, commit 53

[PATCH 10/10] KVM: x86: Fix KVM clock precision in __get_kvmclock()

2024-04-18 Thread David Woodhouse
From: David Woodhouse When in 'master clock mode' (i.e. when host and guest TSCs are behaving sanely and in sync), the KVM clock is defined in terms of the guest TSC. When TSC scaling is used, calculating the KVM clock directly from *host* TSC cycles leads to a systemic drift from

[PATCH 03/10] KVM: x86: Add KVM_[GS]ET_CLOCK_GUEST for accurate KVM clock migration

2024-04-18 Thread David Woodhouse
way. The explicit fallback allows userspace to deliberately fail migration to a host with misbehaving TSC where master clock mode wouldn't be active.) Co-developed-by: David Woodhouse Signed-off-by: Jack Allister Signed-off-by: David Woodhouse CC: Paul Durrant CC: Dongli Zhang ---

[PATCH 11/10] KVM: x86: Fix software TSC upscaling in kvm_update_guest_time()

2024-04-19 Thread David Woodhouse
From: David Woodhouse There was some confusion in kvm_update_guest_time() when software needs to advance the guest TSC. In master clock mode, there are two points of time which need to be taken into account. First there is the master clock reference point, stored in kvm->arch.master_kernel

Re: [RFC PATCH 0/10] Cleaning up the KVM clock mess

2024-04-19 Thread David Woodhouse
On Thu, 2024-04-18 at 20:34 +0100, David Woodhouse wrote: > >   KVM: x86: Remove periodic global clock updates >   KVM: x86: Kill KVM_REQ_GLOBAL_CLOCK_UPDATE Meh, I might have to put those back. They were originally introduced to cope with NTP frequency skew which is no longer

Re: [PATCH 03/10] KVM: x86: Add KVM_[GS]ET_CLOCK_GUEST for accurate KVM clock migration

2024-04-19 Thread David Woodhouse
On Fri, 2024-04-19 at 16:40 +0100, Paul Durrant wrote: > > > +    * If KVM_REQ_CLOCK_UPDATE is already pending, or if the > > hv_clock has > > +    * never been generated at all, call > > kvm_guest_time_update() to do so. > > +    * Might as well use the PVCLOCK_TSC_STABLE_BIT as the >

Re: [PATCH 06/10] KVM: x86: Add KVM_VCPU_TSC_SCALE and fix the documentation on TSC migration

2024-04-19 Thread David Woodhouse
On Fri, 2024-04-19 at 16:49 +0100, Paul Durrant wrote: > On 18/04/2024 20:34, David Woodhouse wrote: > > From: David Woodhouse > > > > The documentation on TSC migration using KVM_VCPU_TSC_OFFSET is woefully > > inadequate. It ignores TSC scaling, and ignores the fac

Re: [PATCH 2/2] KVM: selftests: Add KVM/PV clock selftest to prove timer drift correction

2024-04-19 Thread David Woodhouse
On 19 April 2024 19:40:06 BST, David Woodhouse wrote: >On 19 April 2024 18:13:16 BST, "Chen, Zide" wrote: >>I'm wondering what's the underling theory that we definitely can achieve >>±1ns accuracy? I tested it on a Sapphire Rapids @2100MHz TSC frequency, >

Re: [PATCH 2/2] KVM: selftests: Add KVM/PV clock selftest to prove timer drift correction

2024-04-19 Thread David Woodhouse
On 19 April 2024 18:13:16 BST, "Chen, Zide" wrote: >I'm wondering what's the underling theory that we definitely can achieve >±1ns accuracy? I tested it on a Sapphire Rapids @2100MHz TSC frequency, >and I can see delta_corrected=2 in ~2% cases. Hm. Thanks for testing! So the KVM clock is based o

Re: [PATCH 2/2] KVM: selftests: Add KVM/PV clock selftest to prove timer drift correction

2024-04-20 Thread David Woodhouse
On 20 April 2024 00:54:05 BST, "Chen, Zide" wrote: > > >On 4/19/2024 11:43 AM, David Woodhouse wrote: >> On 19 April 2024 19:40:06 BST, David Woodhouse wrote: >>> On 19 April 2024 18:13:16 BST, "Chen, Zide" wrote: >>>> I'm wonderi

Re: [PATCH 2/2] KVM: selftests: Add KVM/PV clock selftest to prove timer drift correction

2024-04-20 Thread David Woodhouse
On Fri, 2024-04-19 at 16:54 -0700, Chen, Zide wrote: > > However, the selftest hangs: > > [Apr19 16:15] kselftest: Running tests in kvm > [Apr19 16:16] rcu: INFO: rcu_preempt detected stalls on CPUs/tasks: > [  +0.000628] rcu:  78-...0: (1 GPs behind) > idle=3c8c/1/0x4000 softirq

Re: [PATCH 03/10] KVM: x86: Add KVM_[GS]ET_CLOCK_GUEST for accurate KVM clock migration

2024-04-22 Thread David Woodhouse
On Mon, 2024-04-22 at 16:11 +0200, Paolo Bonzini wrote: > On Thu, Apr 18, 2024 at 9:46 PM David Woodhouse wrote: > > +   curr_tsc_hz = get_cpu_tsc_khz() * 1000LL; > > +   if (unlikely(curr_tsc_hz == 0)) { > > +   rc = -EINVAL; > &g

Re: [PATCH 02/10] KVM: x86: Improve accuracy of KVM clock when TSC scaling is in force

2024-04-22 Thread David Woodhouse
On Mon, 2024-04-22 at 14:22 +0200, Paolo Bonzini wrote: > On Thu, Apr 18, 2024 at 9:51 PM David Woodhouse wrote: > > gpa_t time; > > struct pvclock_vcpu_time_info hv_clock; > > -   unsigned int hw_tsc_khz; > > +   unsigned int hw_tsc_hz; >

Re: [PATCH 02/10] KVM: x86: Improve accuracy of KVM clock when TSC scaling is in force

2024-04-22 Thread David Woodhouse
On Mon, 2024-04-22 at 17:54 +0200, Paolo Bonzini wrote: > On Mon, Apr 22, 2024 at 5:39 PM David Woodhouse wrote: > > > > ... especially considering that you did use a 64-bit integer here > > > (though---please use u64 not uint64_t; and BTW if you want to add

Re: [PATCH 2/2] KVM: selftests: Add KVM/PV clock selftest to prove timer drift correction

2024-04-23 Thread David Woodhouse
On Mon, 2024-04-22 at 15:02 -0700, Chen, Zide wrote: > > > On 4/20/2024 9:03 AM, David Woodhouse wrote: > > On Fri, 2024-04-19 at 16:54 -0700, Chen, Zide wrote: > > > > > > However, the selftest hangs: > > > > > > [Apr19 16:15] kselftest:

Re: [PATCH 2/2] KVM: selftests: Add KVM/PV clock selftest to prove timer drift correction

2024-04-23 Thread David Woodhouse
On 23 April 2024 18:59:21 BST, "Chen, Zide" wrote: > > >On 4/23/2024 12:49 AM, David Woodhouse wrote: >>> If I restored the KVM_REQ_GLOBAL_CLOCK_UPDATE request from >>> kvm_arch_vcpu_load(), the selftest works for me, and I ran the test for >>>

Re: [PATCH 2/2] KVM: selftests: Add KVM/PV clock selftest to prove timer drift correction

2024-04-24 Thread David Woodhouse
On Mon, 2024-04-22 at 15:02 -0700, Chen, Zide wrote: > the selftest works for me, and I ran the test for 1000+ iterations, > w/ or w/o TSC scaling, the TEST_ASSERT(delta_corrected <= ±1) never > got hit. This is awesome! I think that with further care we can get even better than that. Let's look

[PATCH v2 05/15] KVM: x86: Explicitly disable TSC scaling without CONSTANT_TSC

2024-04-27 Thread David Woodhouse
From: David Woodhouse KVM does make an attempt to cope with non-constant TSC, and has notifiers to handle host TSC frequency changes. However, it *only* adjusts the KVM clock, and doesn't adjust TSC frequency scaling when the host changes. This is presumably because non-constant TSCs were

[PATCH v2 04/15] KVM: selftests: Add KVM/PV clock selftest to prove timer correction

2024-04-27 Thread David Woodhouse
37 corrected=5038374945 * delta_uncorrected=3509 delta_corrected=1 Signed-off-by: Jack Allister Signed-off-by: David Woodhouse Reviewed-by: Paul Durrant CC: Dongli Zhang --- tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/x86_64/pvclock_test.c | 192 ++ 2 files

[PATCH v2 11/15] KVM: x86: Remove implicit rdtsc() from kvm_compute_l1_tsc_offset()

2024-04-27 Thread David Woodhouse
From: David Woodhouse Let the callers pass the host TSC value in as an explicit parameter. This leaves some fairly obviously stupid code, which using this function to compare the guest TSC at some *other* time, with the newly-minted TSC value from rdtsc(). Unless it's being used to me

[PATCH v2 12/15] KVM: x86: Improve synchronization in kvm_synchronize_tsc()

2024-04-27 Thread David Woodhouse
From: David Woodhouse When synchronizing to an existing TSC (either by explicitly writing zero, or the legacy hack where the TSC is written within one second's worth of the previously written TSC), the last_tsc_write and last_tsc_nsec values were being misrecorded by __kvm_synchronize_tsc()

[PATCH v2 09/15] KVM: x86: Fix software TSC upscaling in kvm_update_guest_time()

2024-04-27 Thread David Woodhouse
From: David Woodhouse There was some confusion in kvm_update_guest_time() when software needs to advance the guest TSC. In master clock mode, there are two points of time which need to be taken into account. First there is the master clock reference point, stored in kvm->arch.master_kernel

[RFC PATCH v2] Cleaning up the KVM clock mess

2024-04-27 Thread David Woodhouse
IME} without even *caring* about the eponymous KVM clock. Especially as this information is (a) fundamentally what the vDSO gettimeofday() exposes to us anyway, (b) using CLOCK_REALTIME not TAI, (c) not available on other platforms, for example for migrating the Arm arch counter.) David Woodhouse (13

[PATCH v2 07/15] KVM: x86: Avoid NTP frequency skew for KVM clock on 32-bit host

2024-04-27 Thread David Woodhouse
From: David Woodhouse Commit 53fafdbb8b21 ("KVM: x86: switch KVMCLOCK base to monotonic raw clock") did so only for 64-bit hosts, by capturing the boot offset from within the existing clocksource notifier update_pvclock_gtod(). That notifier was added in commit 16e8d74d2da9 ("KVM

[PATCH v2 14/15] KVM: x86: Allow KVM master clock mode when TSCs are offset from each other

2024-04-27 Thread David Woodhouse
From: David Woodhouse There is no reason why the KVM clock cannot be in masterclock mode when the TSCs are not in sync, as long as they are at the same *frequency*. Running at a different frequency would lead to a systemic skew between the clock(s) as observed by different vCPUs due to

[PATCH v2 13/15] KVM: x86: Kill cur_tsc_{nsec,offset,write} fields

2024-04-27 Thread David Woodhouse
From: David Woodhouse These pointlessly duplicate of the last_tsc_{nsec,offset,write} values. The only place they were used was where the TSC is stable and a new vCPU is being synchronized to the previous setting, in which case the 'last_' value is definitely identical. Signed-off

[PATCH v2 02/15] KVM: x86: Improve accuracy of KVM clock when TSC scaling is in force

2024-04-27 Thread David Woodhouse
From: David Woodhouse The kvm_guest_time_update() function scales the host TSC frequency to the guest's using kvm_scale_tsc() and the v->arch.l1_tsc_scaling_ratio scaling ratio previously calculated for that vCPU. Then calcuates the scaling factors for the KVM clock itself based on th

[PATCH v2 08/15] KVM: x86: Fix KVM clock precision in __get_kvmclock()

2024-04-27 Thread David Woodhouse
From: David Woodhouse When in 'master clock mode' (i.e. when host and guest TSCs are behaving sanely and in sync), the KVM clock is defined in terms of the guest TSC. When TSC scaling is used, calculating the KVM clock directly from *host* TSC cycles leads to a systemic drift from

[PATCH v2 01/15] KVM: x86/xen: Do not corrupt KVM clock in kvm_xen_shared_info_init()

2024-04-27 Thread David Woodhouse
From: David Woodhouse The KVM clock is an interesting thing. It is defined as "nanoseconds since the guest was created", but in practice it runs at two *different* rates — or three different rates, if you count implementation bugs. Definition A is that it runs synchronousl

[PATCH v2 15/15] KVM: x86: Factor out kvm_use_master_clock()

2024-04-27 Thread David Woodhouse
From: David Woodhouse Both kvm_track_tsc_matching() and pvclock_update_vm_gtod_copy() make a decision about whether the KVM clock should be in master clock mode. They use *different* criteria for the decision though. This isn't really a problem; it only has the potential to cause unnece

[PATCH v2 10/15] KVM: x86: Simplify and comment kvm_get_time_scale()

2024-04-27 Thread David Woodhouse
From: David Woodhouse Commit 3ae13faac400 ("KVM: x86: pass kvm_get_time_scale arguments in hertz") made this function take 64-bit values in Hz rather than 32-bit kHz. Thus making it entrely pointless to shadow its arguments into local 64-bit variables. Just use scaled_hz and base_h

[PATCH v2 06/15] KVM: x86: Add KVM_VCPU_TSC_SCALE and fix the documentation on TSC migration

2024-04-27 Thread David Woodhouse
From: David Woodhouse The documentation on TSC migration using KVM_VCPU_TSC_OFFSET is woefully inadequate. It ignores TSC scaling, and ignores the fact that the host TSC may differ from one host to the next (and in fact because of the way the kernel calibrates it, it generally differs from one

[PATCH v2 03/15] KVM: x86: Add KVM_[GS]ET_CLOCK_GUEST for accurate KVM clock migration

2024-04-27 Thread David Woodhouse
way. The explicit fallback allows userspace to deliberately fail migration to a host with misbehaving TSC where master clock mode wouldn't be active.) Co-developed-by: David Woodhouse Signed-off-by: Jack Allister Signed-off-by: David Woodhouse Reviewed-by: Paul Durrant CC: Dongli Zhang ---

Re: [PATCH v2 15/15] KVM: x86: Factor out kvm_use_master_clock()

2024-05-01 Thread David Woodhouse
On Wed, 2024-05-01 at 10:55 -0700, Chen, Zide wrote: > > > +static bool kvm_use_master_clock(strut kvm *kvm) > > typo: 'strut' -> 'struct' Yeah, sorry about that. Apparently I forgot to *commit* the fixups for that last-minute cleanup, before mailing the series out. Fixed in https://git.infrade

Re: [PATCH v2 01/15] KVM: x86/xen: Do not corrupt KVM clock in kvm_xen_shared_info_init()

2024-05-07 Thread David Woodhouse
On Sat, 2024-05-04 at 04:42 -0300, Marcelo Tosatti wrote: > On Sat, Apr 27, 2024 at 12:04:58PM +0100, David Woodhouse wrote: > > > > In particular, KVM_REQ_MASTERCLOCK_UPDATE will take a new snapshot of > > time as the reference in master_kernel_ns and master_cycle_now, ya

Re: [PATCH v2 13/15] KVM: x86: Kill cur_tsc_{nsec,offset,write} fields

2024-05-14 Thread David Woodhouse
On Fri, 2024-05-10 at 17:03 +0800, Chenyi Qiang wrote: > > Do we need to track vcpu->arch.this_tsc_nsec/this_tsc_write? At least > they are still used in compute_guest_tsc() to calculate the guest > tsc. Ah yes, that's true. Good catch; thanks. That should be caught by a test case which runs the