From: David Woodhouse <[email protected]> The remaining users of pvclock_gtod_data only need to know whether the host clocksource is TSC-based. Replace all vclock_mode checks with a simple kvm_host_has_tsc_clocksource boolean, updated by the pvclock_gtod_notify callback.
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 <[email protected]> Assisted-by: Kiro:claude-opus-4.6-1m --- arch/x86/kvm/x86.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index c18947c5b63f..93a428c37847 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -2649,6 +2649,8 @@ static u64 compute_guest_tsc(struct kvm_vcpu *vcpu, s64 kernel_ns) } #ifdef CONFIG_X86_64 +static bool kvm_host_has_tsc_clocksource; + static inline bool gtod_is_based_on_tsc(int mode) { return mode == VDSO_CLOCKMODE_TSC || mode == VDSO_CLOCKMODE_HVCLOCK; @@ -2678,7 +2680,6 @@ static bool kvm_use_master_clock(struct kvm *kvm) static void kvm_track_tsc_matching(struct kvm_vcpu *vcpu, bool new_generation) { struct kvm_arch *ka = &vcpu->kvm->arch; - struct pvclock_gtod_data *gtod = &pvclock_gtod_data; /* * Track whether all vCPUs have matching TSC offsets (for @@ -2712,7 +2713,7 @@ static void kvm_track_tsc_matching(struct kvm_vcpu *vcpu, bool new_generation) * accounts for its offset. */ bool use_master_clock = kvm_use_master_clock(vcpu->kvm) && - gtod_is_based_on_tsc(gtod->clock.vclock_mode); + kvm_host_has_tsc_clocksource; /* * Request a masterclock update if the masterclock needs to be toggled @@ -2726,7 +2727,7 @@ static void kvm_track_tsc_matching(struct kvm_vcpu *vcpu, bool new_generation) 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, kvm_host_has_tsc_clocksource); } #else static inline void kvm_track_tsc_matching(struct kvm_vcpu *vcpu, @@ -2850,7 +2851,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 (kvm_host_has_tsc_clocksource) return false; #endif return check_tsc_unstable(); @@ -3315,7 +3316,7 @@ static void pvclock_update_vm_gtod_copy(struct kvm *kvm) ka->use_master_clock = false; } - vclock_mode = pvclock_gtod_data.clock.vclock_mode; + vclock_mode = kvm_host_has_tsc_clocksource; trace_kvm_update_master_clock(ka->use_master_clock, vclock_mode, ka->all_vcpus_matched_freq); #endif @@ -10407,12 +10408,15 @@ static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused, update_pvclock_gtod(tk); #ifdef CONFIG_X86_64 + kvm_host_has_tsc_clocksource = + gtod_is_based_on_tsc(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(pvclock_gtod_data.clock.vclock_mode) && + if (!kvm_host_has_tsc_clocksource && atomic_read(&kvm_guest_has_master_clock) != 0) irq_work_queue(&pvclock_irq_work); #endif -- 2.54.0

