Skip to content

Commit 1b3c380

Browse files
committed
KVM: x86: Override TSC_STABLE flag for Xen PV clocks in kvm_guest_time_update()
When updating PV clocks, handle the Xen-specific UNSTABLE_TSC override in the main kvm_guest_time_update() by simply clearing PVCLOCK_TSC_STABLE_BIT in the flags of the reference pvclock structure. Expand the comment to (hopefully) make it obvious that Xen clocks need to be processed after all clocks that care about the TSC_STABLE flag. No functional change intended. Cc: Paul Durrant <pdurrant@amazon.com> Cc: David Woodhouse <dwmw@amazon.co.uk> Reviewed-by: Paul Durrant <paul@xen.org> Link: https://lore.kernel.org/r/20250201013827.680235-12-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 847d68a commit 1b3c380

File tree

1 file changed

+15
-20
lines changed

1 file changed

+15
-20
lines changed

arch/x86/kvm/x86.c

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3119,8 +3119,7 @@ u64 get_kvmclock_ns(struct kvm *kvm)
31193119
static void kvm_setup_guest_pvclock(struct pvclock_vcpu_time_info *ref_hv_clock,
31203120
struct kvm_vcpu *vcpu,
31213121
struct gfn_to_pfn_cache *gpc,
3122-
unsigned int offset,
3123-
bool force_tsc_unstable)
3122+
unsigned int offset)
31243123
{
31253124
struct pvclock_vcpu_time_info *guest_hv_clock;
31263125
struct pvclock_vcpu_time_info hv_clock;
@@ -3155,9 +3154,6 @@ static void kvm_setup_guest_pvclock(struct pvclock_vcpu_time_info *ref_hv_clock,
31553154

31563155
memcpy(guest_hv_clock, &hv_clock, sizeof(*guest_hv_clock));
31573156

3158-
if (force_tsc_unstable)
3159-
guest_hv_clock->flags &= ~PVCLOCK_TSC_STABLE_BIT;
3160-
31613157
smp_wmb();
31623158

31633159
guest_hv_clock->version = ++hv_clock.version;
@@ -3178,16 +3174,6 @@ static int kvm_guest_time_update(struct kvm_vcpu *v)
31783174
s64 kernel_ns;
31793175
u64 tsc_timestamp, host_tsc;
31803176
bool use_master_clock;
3181-
#ifdef CONFIG_KVM_XEN
3182-
/*
3183-
* For Xen guests we may need to override PVCLOCK_TSC_STABLE_BIT as unless
3184-
* explicitly told to use TSC as its clocksource Xen will not set this bit.
3185-
* This default behaviour led to bugs in some guest kernels which cause
3186-
* problems if they observe PVCLOCK_TSC_STABLE_BIT in the pvclock flags.
3187-
*/
3188-
bool xen_pvclock_tsc_unstable =
3189-
ka->xen_hvm_config.flags & KVM_XEN_HVM_CONFIG_PVCLOCK_TSC_UNSTABLE;
3190-
#endif
31913177

31923178
kernel_ns = 0;
31933179
host_tsc = 0;
@@ -3275,21 +3261,30 @@ static int kvm_guest_time_update(struct kvm_vcpu *v)
32753261
hv_clock.flags |= PVCLOCK_GUEST_STOPPED;
32763262
vcpu->pvclock_set_guest_stopped_request = false;
32773263
}
3278-
kvm_setup_guest_pvclock(&hv_clock, v, &vcpu->pv_time, 0, false);
3264+
kvm_setup_guest_pvclock(&hv_clock, v, &vcpu->pv_time, 0);
32793265

32803266
hv_clock.flags &= ~PVCLOCK_GUEST_STOPPED;
32813267
}
32823268

32833269
kvm_hv_setup_tsc_page(v->kvm, &hv_clock);
32843270

32853271
#ifdef CONFIG_KVM_XEN
3272+
/*
3273+
* For Xen guests we may need to override PVCLOCK_TSC_STABLE_BIT as unless
3274+
* explicitly told to use TSC as its clocksource Xen will not set this bit.
3275+
* This default behaviour led to bugs in some guest kernels which cause
3276+
* problems if they observe PVCLOCK_TSC_STABLE_BIT in the pvclock flags.
3277+
*
3278+
* Note! Clear TSC_STABLE only for Xen clocks, i.e. the order matters!
3279+
*/
3280+
if (ka->xen_hvm_config.flags & KVM_XEN_HVM_CONFIG_PVCLOCK_TSC_UNSTABLE)
3281+
hv_clock.flags &= ~PVCLOCK_TSC_STABLE_BIT;
3282+
32863283
if (vcpu->xen.vcpu_info_cache.active)
32873284
kvm_setup_guest_pvclock(&hv_clock, v, &vcpu->xen.vcpu_info_cache,
3288-
offsetof(struct compat_vcpu_info, time),
3289-
xen_pvclock_tsc_unstable);
3285+
offsetof(struct compat_vcpu_info, time));
32903286
if (vcpu->xen.vcpu_time_info_cache.active)
3291-
kvm_setup_guest_pvclock(&hv_clock, v, &vcpu->xen.vcpu_time_info_cache, 0,
3292-
xen_pvclock_tsc_unstable);
3287+
kvm_setup_guest_pvclock(&hv_clock, v, &vcpu->xen.vcpu_time_info_cache, 0);
32933288
#endif
32943289
return 0;
32953290
}

0 commit comments

Comments
 (0)