From: David Woodhouse Eliminate two sources of unnecessary KVM_REQ_GLOBAL_CLOCK_UPDATE: 1. kvm_write_system_time(): The global clock update was a workaround for ever-drifting clocks based on the host's CLOCK_MONOTONIC subject to NTP skew. Now that the KVM clock uses CLOCK_MONOTONIC_RAW, the clock does not drift with NTP corrections and there is no need to synchronize all vCPUs on boot or resume. Use KVM_REQ_CLOCK_UPDATE on the vCPU itself, and only when the clock is being enabled, not disabled. 2. kvm_arch_vcpu_load(): In master clock mode, migration between pCPUs does not require any clock update since the master clock reference is shared. Only request a local KVM_REQ_CLOCK_UPDATE for a load with no prior physical CPU (vcpu->cpu == -1: its first ever load, or a TDX re-association) to generate the pvclock parameters. In non-master-clock mode, keep the global update to synchronize all vCPUs. A host in non-master-clock mode because its TSC is unstable still re-anchors all vCPUs through the global update in kvm_arch_vcpu_load(), which fires on every load there. What this deliberately no longer covers is a stable-TSC host manually switched to a non-TSC clocksource with pinned vCPUs, where vCPUs anchored far apart can skew by the TSC calibration error over the elapsed time; the removed broadcast only masked that when a guest happened to rewrite MSR_KVM_SYSTEM_TIME, which a running guest does not do. Signed-off-by: David Woodhouse --- arch/x86/kvm/msrs.c | 7 ++++--- arch/x86/kvm/x86.c | 4 +++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/msrs.c b/arch/x86/kvm/msrs.c index e91650af44c2..a2dffa1c706f 100644 --- a/arch/x86/kvm/msrs.c +++ b/arch/x86/kvm/msrs.c @@ -1285,14 +1285,15 @@ static void kvm_write_system_time(struct kvm_vcpu *vcpu, gpa_t system_time, } vcpu->arch.time = system_time; - kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu); /* we verify if the enable bit is set... */ - if (system_time & 1) + if (system_time & 1) { kvm_gpc_activate(&vcpu->arch.pv_time, system_time & ~1ULL, sizeof(struct pvclock_vcpu_time_info)); - else + kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); + } else { kvm_gpc_deactivate(&vcpu->arch.pv_time); + } return; } diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 9ad217e8640a..05c801973a7f 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -2765,11 +2765,13 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) * On a host with synchronized TSC, there is no need to update * kvmclock on vcpu->cpu migration */ - if (!vcpu->kvm->arch.use_master_clock || vcpu->cpu == -1) { + if (!vcpu->kvm->arch.use_master_clock) { if (__ratelimit(&vcpu->kvm->arch.kvmclock_update_rs)) kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu); else kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); + } else if (vcpu->cpu == -1) { + kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); } if (vcpu->cpu != cpu) -- 2.43.0