[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH v11 03/23] KVM: x86: Replace nr_vcpus_matched_tsc count with all_vcpus_matched_tsc bool



From: David Woodhouse <dwmw@xxxxxxxxxxxx>

Using a count and comparing with kvm->online_vcpus was always racy
because a new vCPU could be created while kvm_track_tsc_matching() was
running and comparing with kvm->online_vcpus. That variable is only
atomic with respect to itself; kvm_arch_vcpu_create() runs before
kvm->online_vcpus is incremented for the new vCPU.

Replace the count with a boolean that is set in kvm_track_tsc_matching()
after comparing the count, and cleared when a new TSC generation starts.
The boolean is consumed by pvclock_update_vm_gtod_copy() under the
tsc_write_lock, which serializes against __kvm_synchronize_tsc().

Keep the count for now as it's still used in the trace event.

Note that the explicit clear of the flag when a new generation starts
is currently overwritten by the unconditional recompute in
kvm_track_tsc_matching() under the same lock; it is preparation for
subsequent patches which consume the previous value of the flag.

Initialize the flag to true at VM creation: a VM whose vCPUs never
reach kvm_track_tsc_matching() — notably with a protected guest TSC
(TDX, SNP Secure TSC), where __kvm_synchronize_tsc() returns early —
would otherwise never be eligible for PVCLOCK_TSC_STABLE_BIT, where
the old read-time computation gave 0 + 1 == 1 for a single-vCPU VM.

Signed-off-by: David Woodhouse <dwmw@xxxxxxxxxxxx>
---
 arch/x86/include/asm/kvm_host.h |  1 +
 arch/x86/kvm/x86.c              | 18 ++++++++++++++----
 2 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index ea48b1949316..1efca01647d2 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -1242,6 +1242,7 @@ struct kvm_arch {
        u64 cur_tsc_write;
        u64 cur_tsc_offset;
        u64 cur_tsc_generation;
+       bool all_vcpus_matched_tsc;
        int nr_vcpus_matched_tsc;
 
        u32 default_tsc_khz;
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 90f9b07ef749..ec8693f4827f 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -1125,8 +1125,10 @@ static void kvm_track_tsc_matching(struct kvm_vcpu 
*vcpu, bool new_generation)
         * and all vCPUs must have matching TSCs.  Note, the count for matching
         * vCPUs doesn't include the reference vCPU, hence "+1".
         */
-       bool use_master_clock = (ka->nr_vcpus_matched_tsc + 1 ==
-                                atomic_read(&vcpu->kvm->online_vcpus)) &&
+       ka->all_vcpus_matched_tsc = (ka->nr_vcpus_matched_tsc + 1 ==
+                                    atomic_read(&vcpu->kvm->online_vcpus));
+
+       bool use_master_clock = ka->all_vcpus_matched_tsc &&
                                gtod_is_based_on_tsc(gtod->clock.vclock_mode);
 
        /*
@@ -1367,6 +1369,7 @@ static void __kvm_synchronize_tsc(struct kvm_vcpu *vcpu, 
u64 offset, u64 tsc,
                kvm->arch.cur_tsc_write = tsc;
                kvm->arch.cur_tsc_offset = offset;
                kvm->arch.nr_vcpus_matched_tsc = 0;
+               kvm->arch.all_vcpus_matched_tsc = false;
        } else if (vcpu->arch.this_tsc_generation != 
kvm->arch.cur_tsc_generation) {
                kvm->arch.nr_vcpus_matched_tsc++;
        }
@@ -1591,8 +1594,7 @@ static void pvclock_update_vm_gtod_copy(struct kvm *kvm)
        bool host_tsc_clocksource, vcpus_matched;
 
        lockdep_assert_held(&kvm->arch.tsc_write_lock);
-       vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
-                       atomic_read(&kvm->online_vcpus));
+       vcpus_matched = ka->all_vcpus_matched_tsc;
 
        /*
         * If the host uses TSC clock, then passthrough TSC as stable
@@ -10007,6 +10009,14 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long 
type)
        ratelimit_set_flags(&kvm->arch.kvmclock_update_rs, 
RATELIMIT_MSG_ON_RELEASE);
        kvm->arch.kvmclock_offset = -get_kvmclock_base_ns();
 
+       /*
+        * Start with all (zero) vCPUs considered matched, so that a VM whose
+        * vCPUs never reach kvm_track_tsc_matching() — notably when the guest
+        * TSC is protected (TDX, SNP Secure TSC) and __kvm_synchronize_tsc()
+        * returns early — is still eligible for PVCLOCK_TSC_STABLE_BIT.
+        */
+       kvm->arch.all_vcpus_matched_tsc = true;
+
        raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags);
        pvclock_update_vm_gtod_copy(kvm);
        raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags);
-- 
2.43.0




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.