Skip to content

Commit 6d72283

Browse files
Paul Durrantsean-jc
authored andcommitted
KVM x86/xen: add an override for PVCLOCK_TSC_STABLE_BIT
Unless explicitly told to do so (by passing 'clocksource=tsc' and 'tsc=stable:socket', and then jumping through some hoops concerning potential CPU hotplug) Xen will never use TSC as its clocksource. Hence, by default, a Xen guest will not see PVCLOCK_TSC_STABLE_BIT set in either the primary or secondary pvclock memory areas. This has led to bugs in some guest kernels which only become evident if PVCLOCK_TSC_STABLE_BIT *is* set in the pvclocks. Hence, to support such guests, give the VMM a new Xen HVM config flag to tell KVM to forcibly clear the bit in the Xen pvclocks. Signed-off-by: Paul Durrant <pdurrant@amazon.com> Reviewed-by: David Woodhouse <dwmw@amazon.co.uk> Link: https://lore.kernel.org/r/20231102162128.2353459-1-paul@xen.org Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent e9e60c8 commit 6d72283

File tree

4 files changed

+38
-6
lines changed

4 files changed

+38
-6
lines changed

Documentation/virt/kvm/api.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8562,6 +8562,7 @@ PVHVM guests. Valid flags are::
85628562
#define KVM_XEN_HVM_CONFIG_EVTCHN_2LEVEL (1 << 4)
85638563
#define KVM_XEN_HVM_CONFIG_EVTCHN_SEND (1 << 5)
85648564
#define KVM_XEN_HVM_CONFIG_RUNSTATE_UPDATE_FLAG (1 << 6)
8565+
#define KVM_XEN_HVM_CONFIG_PVCLOCK_TSC_UNSTABLE (1 << 7)
85658566

85668567
The KVM_XEN_HVM_CONFIG_HYPERCALL_MSR flag indicates that the KVM_XEN_HVM_CONFIG
85678568
ioctl is available, for the guest to set its hypercall page.
@@ -8605,6 +8606,11 @@ behave more correctly, not using the XEN_RUNSTATE_UPDATE flag until/unless
86058606
specifically enabled (by the guest making the hypercall, causing the VMM
86068607
to enable the KVM_XEN_ATTR_TYPE_RUNSTATE_UPDATE_FLAG attribute).
86078608

8609+
The KVM_XEN_HVM_CONFIG_PVCLOCK_TSC_UNSTABLE flag indicates that KVM supports
8610+
clearing the PVCLOCK_TSC_STABLE_BIT flag in Xen pvclock sources. This will be
8611+
done when the KVM_CAP_XEN_HVM ioctl sets the
8612+
KVM_XEN_HVM_CONFIG_PVCLOCK_TSC_UNSTABLE flag.
8613+
86088614
8.31 KVM_CAP_PPC_MULTITCE
86098615
-------------------------
86108616

arch/x86/kvm/x86.c

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3104,7 +3104,8 @@ u64 get_kvmclock_ns(struct kvm *kvm)
31043104

31053105
static void kvm_setup_guest_pvclock(struct kvm_vcpu *v,
31063106
struct gfn_to_pfn_cache *gpc,
3107-
unsigned int offset)
3107+
unsigned int offset,
3108+
bool force_tsc_unstable)
31083109
{
31093110
struct kvm_vcpu_arch *vcpu = &v->arch;
31103111
struct pvclock_vcpu_time_info *guest_hv_clock;
@@ -3141,6 +3142,10 @@ static void kvm_setup_guest_pvclock(struct kvm_vcpu *v,
31413142
}
31423143

31433144
memcpy(guest_hv_clock, &vcpu->hv_clock, sizeof(*guest_hv_clock));
3145+
3146+
if (force_tsc_unstable)
3147+
guest_hv_clock->flags &= ~PVCLOCK_TSC_STABLE_BIT;
3148+
31443149
smp_wmb();
31453150

31463151
guest_hv_clock->version = ++vcpu->hv_clock.version;
@@ -3161,6 +3166,16 @@ static int kvm_guest_time_update(struct kvm_vcpu *v)
31613166
u64 tsc_timestamp, host_tsc;
31623167
u8 pvclock_flags;
31633168
bool use_master_clock;
3169+
#ifdef CONFIG_KVM_XEN
3170+
/*
3171+
* For Xen guests we may need to override PVCLOCK_TSC_STABLE_BIT as unless
3172+
* explicitly told to use TSC as its clocksource Xen will not set this bit.
3173+
* This default behaviour led to bugs in some guest kernels which cause
3174+
* problems if they observe PVCLOCK_TSC_STABLE_BIT in the pvclock flags.
3175+
*/
3176+
bool xen_pvclock_tsc_unstable =
3177+
ka->xen_hvm_config.flags & KVM_XEN_HVM_CONFIG_PVCLOCK_TSC_UNSTABLE;
3178+
#endif
31643179

31653180
kernel_ns = 0;
31663181
host_tsc = 0;
@@ -3239,13 +3254,15 @@ static int kvm_guest_time_update(struct kvm_vcpu *v)
32393254
vcpu->hv_clock.flags = pvclock_flags;
32403255

32413256
if (vcpu->pv_time.active)
3242-
kvm_setup_guest_pvclock(v, &vcpu->pv_time, 0);
3257+
kvm_setup_guest_pvclock(v, &vcpu->pv_time, 0, false);
32433258
#ifdef CONFIG_KVM_XEN
32443259
if (vcpu->xen.vcpu_info_cache.active)
32453260
kvm_setup_guest_pvclock(v, &vcpu->xen.vcpu_info_cache,
3246-
offsetof(struct compat_vcpu_info, time));
3261+
offsetof(struct compat_vcpu_info, time),
3262+
xen_pvclock_tsc_unstable);
32473263
if (vcpu->xen.vcpu_time_info_cache.active)
3248-
kvm_setup_guest_pvclock(v, &vcpu->xen.vcpu_time_info_cache, 0);
3264+
kvm_setup_guest_pvclock(v, &vcpu->xen.vcpu_time_info_cache, 0,
3265+
xen_pvclock_tsc_unstable);
32493266
#endif
32503267
kvm_hv_setup_tsc_page(v->kvm, &vcpu->hv_clock);
32513268
return 0;
@@ -4646,7 +4663,8 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
46464663
KVM_XEN_HVM_CONFIG_INTERCEPT_HCALL |
46474664
KVM_XEN_HVM_CONFIG_SHARED_INFO |
46484665
KVM_XEN_HVM_CONFIG_EVTCHN_2LEVEL |
4649-
KVM_XEN_HVM_CONFIG_EVTCHN_SEND;
4666+
KVM_XEN_HVM_CONFIG_EVTCHN_SEND |
4667+
KVM_XEN_HVM_CONFIG_PVCLOCK_TSC_UNSTABLE;
46504668
if (sched_info_on())
46514669
r |= KVM_XEN_HVM_CONFIG_RUNSTATE |
46524670
KVM_XEN_HVM_CONFIG_RUNSTATE_UPDATE_FLAG;

arch/x86/kvm/xen.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1162,7 +1162,9 @@ int kvm_xen_hvm_config(struct kvm *kvm, struct kvm_xen_hvm_config *xhc)
11621162
{
11631163
/* Only some feature flags need to be *enabled* by userspace */
11641164
u32 permitted_flags = KVM_XEN_HVM_CONFIG_INTERCEPT_HCALL |
1165-
KVM_XEN_HVM_CONFIG_EVTCHN_SEND;
1165+
KVM_XEN_HVM_CONFIG_EVTCHN_SEND |
1166+
KVM_XEN_HVM_CONFIG_PVCLOCK_TSC_UNSTABLE;
1167+
u32 old_flags;
11661168

11671169
if (xhc->flags & ~permitted_flags)
11681170
return -EINVAL;
@@ -1183,9 +1185,14 @@ int kvm_xen_hvm_config(struct kvm *kvm, struct kvm_xen_hvm_config *xhc)
11831185
else if (!xhc->msr && kvm->arch.xen_hvm_config.msr)
11841186
static_branch_slow_dec_deferred(&kvm_xen_enabled);
11851187

1188+
old_flags = kvm->arch.xen_hvm_config.flags;
11861189
memcpy(&kvm->arch.xen_hvm_config, xhc, sizeof(*xhc));
11871190

11881191
mutex_unlock(&kvm->arch.xen.xen_lock);
1192+
1193+
if ((old_flags ^ xhc->flags) & KVM_XEN_HVM_CONFIG_PVCLOCK_TSC_UNSTABLE)
1194+
kvm_make_all_cpus_request(kvm, KVM_REQ_CLOCK_UPDATE);
1195+
11891196
return 0;
11901197
}
11911198

include/uapi/linux/kvm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,6 +1318,7 @@ struct kvm_x86_mce {
13181318
#define KVM_XEN_HVM_CONFIG_EVTCHN_2LEVEL (1 << 4)
13191319
#define KVM_XEN_HVM_CONFIG_EVTCHN_SEND (1 << 5)
13201320
#define KVM_XEN_HVM_CONFIG_RUNSTATE_UPDATE_FLAG (1 << 6)
1321+
#define KVM_XEN_HVM_CONFIG_PVCLOCK_TSC_UNSTABLE (1 << 7)
13211322

13221323
struct kvm_xen_hvm_config {
13231324
__u32 flags;

0 commit comments

Comments
 (0)