]> www.infradead.org Git - users/dwmw2/linux.git/commitdiff
KVM: x86: Avoid NTP frequency skew for KVM clock on 32-bit host
authorDavid Woodhouse <dwmw@amazon.co.uk>
Thu, 18 Apr 2024 12:13:04 +0000 (13:13 +0100)
committerDavid Woodhouse <dwmw@amazon.co.uk>
Fri, 2 Aug 2024 14:51:45 +0000 (15:51 +0100)
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: x86: notifier for
clocksource changes") but only on x86_64, because its original purpose
was just to disable the "master clock" mode which is only supported on
x86_64.

Now that the notifier is used for more than disabling master clock mode,
(well, OK, more than a decade later but clocks are hard), enable it for
the 32-bit build too so that get_kvmclock_base_ns() can be unaffected by
NTP sync on 32-bit too.

Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
Reviewed-by: Paul Durrant <paul@xen.org>
arch/x86/kvm/x86.c

index abc2918ad4035a1058099f07c2412da93686c3b1..2304763fa4482305c470a48539199b96f540423f 100644 (file)
@@ -2222,7 +2222,6 @@ static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
        return kvm_set_msr_ignored_check(vcpu, index, *data, true);
 }
 
-#ifdef CONFIG_X86_64
 struct pvclock_clock {
        int vclock_mode;
        u64 cycle_last;
@@ -2280,13 +2279,6 @@ static s64 get_kvmclock_base_ns(void)
        /* Count up from boot time, but with the frequency of the raw clock.  */
        return ktime_to_ns(ktime_add(ktime_get_raw(), pvclock_gtod_data.offs_boot));
 }
-#else
-static s64 get_kvmclock_base_ns(void)
-{
-       /* Master clock not used, so we can just use CLOCK_BOOTTIME.  */
-       return ktime_get_boottime_ns();
-}
-#endif
 
 static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock, int sec_hi_ofs)
 {
@@ -9845,6 +9837,7 @@ static void pvclock_irq_work_fn(struct irq_work *w)
 }
 
 static DEFINE_IRQ_WORK(pvclock_irq_work, pvclock_irq_work_fn);
+#endif
 
 /*
  * Notification about pvclock gtod data update.
@@ -9852,26 +9845,26 @@ 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);
 
+#ifdef CONFIG_X86_64
        /*
         * 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(pvclock_gtod_data.clock.vclock_mode) &&
            atomic_read(&kvm_guest_has_master_clock) != 0)
                irq_work_queue(&pvclock_irq_work);
+#endif
        return 0;
 }
 
 static struct notifier_block pvclock_gtod_notifier = {
        .notifier_call = pvclock_gtod_notify,
 };
-#endif
 
 static inline void kvm_ops_update(struct kvm_x86_init_ops *ops)
 {
@@ -10015,9 +10008,10 @@ int kvm_x86_vendor_init(struct kvm_x86_init_ops *ops)
 
        if (pi_inject_timer == -1)
                pi_inject_timer = housekeeping_enabled(HK_TYPE_TIMER);
-#ifdef CONFIG_X86_64
+
        pvclock_gtod_register_notifier(&pvclock_gtod_notifier);
 
+#ifdef CONFIG_X86_64
        if (hypervisor_is_type(X86_HYPER_MS_HYPERV))
                set_hv_tscchange_cb(kvm_hyperv_tsc_notifier);
 #endif