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

[PATCH v11 09/23] KVM: x86: Cache host vclock_mode for masterclock eligibility checks



From: David Woodhouse <dwmw@xxxxxxxxxxxx>

The remaining users of pvclock_gtod_data only need to know whether
the host clocksource is TSC-based. Cache the vclock_mode value in a
simple kvm_host_vclock_mode variable, updated by the pvclock_gtod_notify
callback, and use gtod_is_based_on_tsc() for eligibility checks.

This is inherently racy (as it always was — kvm_track_tsc_matching
never held the gtod seqcount), relying on eventual consistency: the
notifier fires on every timekeeping update and will correct any
transient inconsistency within one tick.

Signed-off-by: David Woodhouse <dwmw@xxxxxxxxxxxx>
---
 arch/x86/kvm/x86.c | 27 +++++++++++++++++----------
 1 file changed, 17 insertions(+), 10 deletions(-)

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index f551056d7274..e81e527e7b99 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -1136,6 +1136,8 @@ static u64 compute_guest_tsc(struct kvm_vcpu *vcpu, s64 
kernel_ns)
 }
 
 #ifdef CONFIG_X86_64
+static int kvm_host_vclock_mode;
+
 static inline bool gtod_is_based_on_tsc(int mode)
 {
        return mode == VDSO_CLOCKMODE_TSC || mode == VDSO_CLOCKMODE_HVCLOCK;
@@ -1165,7 +1167,6 @@ static bool kvm_use_master_clock(struct kvm *kvm)
 static void kvm_track_tsc_matching(struct kvm_vcpu *vcpu, bool update_mclock)
 {
        struct kvm_arch *ka = &vcpu->kvm->arch;
-       struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
        bool prev_matched_tsc = ka->all_vcpus_matched_tsc;
 
        /*
@@ -1209,8 +1210,9 @@ static void kvm_track_tsc_matching(struct kvm_vcpu *vcpu, 
bool update_mclock)
         * are fine — each vCPU's pvclock has its own tsc_timestamp that
         * accounts for its offset.
         */
+       int host_vclock_mode = READ_ONCE(kvm_host_vclock_mode);
        bool use_master_clock = kvm_use_master_clock(vcpu->kvm) &&
-                               gtod_is_based_on_tsc(gtod->clock.vclock_mode);
+                               gtod_is_based_on_tsc(host_vclock_mode);
 
        /*
         * Request a masterclock update if needed: toggling master clock,
@@ -1223,7 +1225,7 @@ static void kvm_track_tsc_matching(struct kvm_vcpu *vcpu, 
bool update_mclock)
 
        trace_kvm_track_tsc(vcpu->vcpu_id, ka->nr_vcpus_matched_tsc,
                            atomic_read(&vcpu->kvm->online_vcpus),
-                           ka->use_master_clock, gtod->clock.vclock_mode);
+                           ka->use_master_clock, host_vclock_mode);
 }
 #else
 static inline void kvm_track_tsc_matching(struct kvm_vcpu *vcpu,
@@ -1394,7 +1396,7 @@ static inline bool kvm_check_tsc_unstable(void)
         * TSC is marked unstable when we're running on Hyper-V,
         * 'TSC page' clocksource is good.
         */
-       if (pvclock_gtod_data.clock.vclock_mode == VDSO_CLOCKMODE_HVCLOCK)
+       if (READ_ONCE(kvm_host_vclock_mode) == VDSO_CLOCKMODE_HVCLOCK)
                return false;
 #endif
        return check_tsc_unstable();
@@ -1689,7 +1691,6 @@ static void pvclock_update_vm_gtod_copy(struct kvm *kvm)
 {
 #ifdef CONFIG_X86_64
        struct kvm_arch *ka = &kvm->arch;
-       int vclock_mode;
        bool host_tsc_clocksource;
 
        lockdep_assert_held(&kvm->arch.tsc_write_lock);
@@ -1729,9 +1730,9 @@ static void pvclock_update_vm_gtod_copy(struct kvm *kvm)
                        ka->use_master_clock = false;
        }
 
-       vclock_mode = pvclock_gtod_data.clock.vclock_mode;
-       trace_kvm_update_master_clock(ka->use_master_clock, vclock_mode,
-                                       ka->all_vcpus_matched_freq);
+       trace_kvm_update_master_clock(ka->use_master_clock,
+                                     READ_ONCE(kvm_host_vclock_mode),
+                                     ka->all_vcpus_matched_freq);
 #endif
 }
 
@@ -7089,17 +7090,23 @@ static DEFINE_IRQ_WORK(pvclock_irq_work, 
pvclock_irq_work_fn);
 static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused,
                               void *priv)
 {
-       struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
        struct timekeeper *tk = priv;
 
        update_pvclock_gtod(tk);
 
+       /*
+        * Pairs with the READ_ONCE() in the lockless readers; the race is
+        * benign by design, relying on eventual consistency (the notifier
+        * fires on every timekeeping update).
+        */
+       WRITE_ONCE(kvm_host_vclock_mode, tk->tkr_mono.clock->vdso_clock_mode);
+
        /*
         * Disable master clock if host does not trust, or does not use,
         * TSC based clocksource. Delegate queue_work() to irq_work as
         * this is invoked with tk_core.seq write held.
         */
-       if (!gtod_is_based_on_tsc(gtod->clock.vclock_mode) &&
+       if (!gtod_is_based_on_tsc(kvm_host_vclock_mode) &&
            atomic_read(&kvm_guest_has_master_clock) != 0)
                irq_work_queue(&pvclock_irq_work);
        return 0;
-- 
2.43.0




 


Rackspace

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