KVM x86 PV clocks and timekeeping related changes for 7.3

- Remove a defunct masterclock update from kvm_xen_shared_info_init() that
    could result in corrupting kvmclock, for a lose definition or "corrupting",
    due to triggering an unnecessary switch to/from masterclock mode.
 
  - Skip Xen runstate time updates if time has effectively gone backwards, so
    that the guest doesn't report 100% steal time for a very, very long time.
 
  - Drop KVM's runtime updates of the Xen PV timing CPUID leaf, as KVM was
    updating the wrong sub-leaf, and upstream KVM will soon provide all the
    information needed by userspace to populate the CPUID field itself.
 -----BEGIN PGP SIGNATURE-----
 
 iQIzBAABCgAdFiEEKTobbabEP7vbhhN9OlYIJqCjN/0FAmp7vSIACgkQOlYIJqCj
 N/1FzRAAhtEbAmo1wQAfwCfC1zCGxxfNS45+KOUoa9qbBkmhqou0Vn648O4BxamK
 7MJ2J9OZ25X5O+kkx0YTTqiRhPZ+z8NfAQeCAd2XhwqmLtKaY8PLea+Ei132ztLV
 1UDHjxTjgrDrwZvV5V2q7IzHHWFxWm4HVX1BrArcsB2hndiq7Re+eYGF12IL2DEJ
 nmL1Atc4xAVQYpeLqt9hT3mmbv09mifjEHGrzyP0xmCs6V8HKtpdM0tv9mdSvuRn
 gTpnAmD9akfk4iT8xLW/1Di86a7KkWPqMU3te9GP5XBXeRld+iLZCPG1f5zq9wmT
 I7wdScR8TPeEsawDIfwgW+SqbBE/452fbLk6spoIAJYysR8k5sXY5QMbmADznpBE
 6MtDQgQ8IbevMZsb6NnfmYz2AFdBQcJN50/Zdk2SEXwXZJD6gZdNIVovlZURl0I5
 9ApfHl7tSxW+Wz5aRL/iDCxQeoh+0cRXNbi4WRb6am3wgn8LVBvOgkZZNoTI1qE2
 MN/Oc6WVavbMlOBC1mz9LuuiFugJnprWkxBjzvpzrlUBjt5m6lIni+9SctEArMuD
 7tZbr1w8XGBwJU4xrMn16KC53+HAltuBgN0BK1umKtIGasn5K6cmGADpONclJ6RN
 IKhcqIGwF+eqtYfthYIrGGsvMjrw9h1MGMMnLS3mt/J1GjX1VOo=
 =oKIz
 -----END PGP SIGNATURE-----

Merge tag 'kvm-x86-clocks-7.3' of https://github.com/kvm-x86/linux into HEAD

KVM x86 PV clocks and timekeeping related changes for 7.3

 - Remove a defunct masterclock update from kvm_xen_shared_info_init() that
   could result in corrupting kvmclock, for a lose definition or "corrupting",
   due to triggering an unnecessary switch to/from masterclock mode.

 - Skip Xen runstate time updates if time has effectively gone backwards, so
   that the guest doesn't report 100% steal time for a very, very long time.

 - Drop KVM's runtime updates of the Xen PV timing CPUID leaf, as KVM was
   updating the wrong sub-leaf, and upstream KVM will soon provide all the
   information needed by userspace to populate the CPUID field itself.
This commit is contained in:
Paolo Bonzini 2026-08-18 13:26:05 +02:00
commit c4a6ae0ea9
3 changed files with 22 additions and 37 deletions

View File

@ -2118,22 +2118,6 @@ bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx,
} else if (function == 0x80000007) {
if (kvm_hv_invtsc_suppressed(vcpu))
*edx &= ~feature_bit(CONSTANT_TSC);
} else if (IS_ENABLED(CONFIG_KVM_XEN) &&
kvm_xen_is_tsc_leaf(vcpu, function)) {
/*
* Update guest TSC frequency information if necessary.
* Ignore failures, there is no sane value that can be
* provided if KVM can't get the TSC frequency.
*/
if (kvm_check_request(KVM_REQ_CLOCK_UPDATE, vcpu))
kvm_guest_time_update(vcpu);
if (index == 1) {
*ecx = vcpu->arch.pvclock_tsc_mul;
*edx = vcpu->arch.pvclock_tsc_shift;
} else if (index == 2) {
*eax = vcpu->arch.hw_tsc_khz;
}
}
} else {
*eax = *ebx = *ecx = *edx = 0;

View File

@ -98,8 +98,6 @@ static int kvm_xen_shared_info_init(struct kvm *kvm)
wc->version = wc_version + 1;
read_unlock_irq(&gpc->lock);
kvm_make_all_cpus_request(kvm, KVM_REQ_MASTERCLOCK_UPDATE);
out:
srcu_read_unlock(&kvm->srcu, idx);
return ret;
@ -588,29 +586,45 @@ void kvm_xen_update_runstate(struct kvm_vcpu *v, int state)
{
struct kvm_vcpu_xen *vx = &v->arch.xen;
u64 now = get_kvmclock_ns(v->kvm);
u64 delta_ns = now - vx->runstate_entry_time;
u64 run_delay = current->sched_info.run_delay;
s64 delta_ns = now - vx->runstate_entry_time;
s64 steal_ns = run_delay - vx->last_steal;
/*
* If the vCPU was never run before, its prior state should
* be considered RUNSTATE_offline.
*/
if (unlikely(!vx->runstate_entry_time))
vx->current_runstate = RUNSTATE_offline;
/*
* If KVM clock went backwards, just update the current runstate
* but don't account any time. Leave entry_time unchanged so the
* next positive delta covers the full period once the clock
* catches up. Update last_steal every time so stolen time only
* reflects the interval since the most recent call.
*/
if (delta_ns < 0)
goto update_guest;
/*
* Time waiting for the scheduler isn't "stolen" if the
* vCPU wasn't running anyway.
*/
if (vx->current_runstate == RUNSTATE_running) {
u64 steal_ns = run_delay - vx->last_steal;
if (vx->current_runstate == RUNSTATE_running && steal_ns > 0) {
if (steal_ns > delta_ns)
steal_ns = delta_ns;
delta_ns -= steal_ns;
vx->runstate_times[RUNSTATE_runnable] += steal_ns;
}
vx->last_steal = run_delay;
vx->runstate_times[vx->current_runstate] += delta_ns;
vx->current_runstate = state;
vx->runstate_entry_time = now;
update_guest:
vx->current_runstate = state;
vx->last_steal = run_delay;
if (vx->runstate_cache.active)
kvm_xen_update_runstate_guest(v, state == RUNSTATE_runnable);
}

View File

@ -50,14 +50,6 @@ static inline void kvm_xen_sw_enable_lapic(struct kvm_vcpu *vcpu)
kvm_xen_inject_vcpu_vector(vcpu);
}
static inline bool kvm_xen_is_tsc_leaf(struct kvm_vcpu *vcpu, u32 function)
{
return static_branch_unlikely(&kvm_xen_enabled.key) &&
vcpu->arch.xen.cpuid.base &&
function <= vcpu->arch.xen.cpuid.limit &&
function == (vcpu->arch.xen.cpuid.base | XEN_CPUID_LEAF(3));
}
static inline bool kvm_xen_msr_enabled(struct kvm *kvm)
{
return static_branch_unlikely(&kvm_xen_enabled.key) &&
@ -177,11 +169,6 @@ static inline bool kvm_xen_timer_enabled(struct kvm_vcpu *vcpu)
{
return false;
}
static inline bool kvm_xen_is_tsc_leaf(struct kvm_vcpu *vcpu, u32 function)
{
return false;
}
#endif
int kvm_xen_hypercall(struct kvm_vcpu *vcpu);