Skip to content

Commit e3fd9a9

Browse files
bonzinirkrcmar
authored andcommitted
kvm: kvmclock: let KVM_GET_CLOCK return whether the master clock is in use
Userspace can read the exact value of kvmclock by reading the TSC and fetching the timekeeping parameters out of guest memory. This however is brittle and not necessary anymore with KVM 4.11. Provide a mechanism that lets userspace know if the new KVM_GET_CLOCK semantics are in effect, and---since we are at it---if the clock is stable across all VCPUs. Cc: Radim Krčmář <rkrcmar@redhat.com> Cc: Marcelo Tosatti <mtosatti@redhat.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> Signed-off-by: Radim Krčmář <rkrcmar@redhat.com>
1 parent 1650b4e commit e3fd9a9

3 files changed

Lines changed: 25 additions & 3 deletions

File tree

Documentation/virtual/kvm/api.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,17 @@ Gets the current timestamp of kvmclock as seen by the current guest. In
777777
conjunction with KVM_SET_CLOCK, it is used to ensure monotonicity on scenarios
778778
such as migration.
779779

780+
When KVM_CAP_ADJUST_CLOCK is passed to KVM_CHECK_EXTENSION, it returns the
781+
set of bits that KVM can return in struct kvm_clock_data's flag member.
782+
783+
The only flag defined now is KVM_CLOCK_TSC_STABLE. If set, the returned
784+
value is the exact kvmclock value seen by all VCPUs at the instant
785+
when KVM_GET_CLOCK was called. If clear, the returned value is simply
786+
CLOCK_MONOTONIC plus a constant offset; the offset can be modified
787+
with KVM_SET_CLOCK. KVM will try to make all VCPUs follow this clock,
788+
but the exact value read by each VCPU could differ, because the host
789+
TSC is not stable.
790+
780791
struct kvm_clock_data {
781792
__u64 clock; /* kvmclock current value */
782793
__u32 flags;

arch/x86/kvm/x86.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2610,7 +2610,6 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
26102610
case KVM_CAP_PIT_STATE2:
26112611
case KVM_CAP_SET_IDENTITY_MAP_ADDR:
26122612
case KVM_CAP_XEN_HVM:
2613-
case KVM_CAP_ADJUST_CLOCK:
26142613
case KVM_CAP_VCPU_EVENTS:
26152614
case KVM_CAP_HYPERV:
26162615
case KVM_CAP_HYPERV_VAPIC:
@@ -2637,6 +2636,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
26372636
#endif
26382637
r = 1;
26392638
break;
2639+
case KVM_CAP_ADJUST_CLOCK:
2640+
r = KVM_CLOCK_TSC_STABLE;
2641+
break;
26402642
case KVM_CAP_X86_SMM:
26412643
/* SMBASE is usually relocated above 1M on modern chipsets,
26422644
* and SMM handlers might indeed rely on 4G segment limits,
@@ -4117,9 +4119,11 @@ long kvm_arch_vm_ioctl(struct file *filp,
41174119
struct kvm_clock_data user_ns;
41184120
u64 now_ns;
41194121

4120-
now_ns = get_kvmclock_ns(kvm);
4122+
local_irq_disable();
4123+
now_ns = __get_kvmclock_ns(kvm);
41214124
user_ns.clock = now_ns;
4122-
user_ns.flags = 0;
4125+
user_ns.flags = kvm->arch.use_master_clock ? KVM_CLOCK_TSC_STABLE : 0;
4126+
local_irq_enable();
41234127
memset(&user_ns.pad, 0, sizeof(user_ns.pad));
41244128

41254129
r = -EFAULT;

include/uapi/linux/kvm.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -972,12 +972,19 @@ struct kvm_irqfd {
972972
__u8 pad[16];
973973
};
974974

975+
/* For KVM_CAP_ADJUST_CLOCK */
976+
977+
/* Do not use 1, KVM_CHECK_EXTENSION returned it before we had flags. */
978+
#define KVM_CLOCK_TSC_STABLE 2
979+
975980
struct kvm_clock_data {
976981
__u64 clock;
977982
__u32 flags;
978983
__u32 pad[9];
979984
};
980985

986+
/* For KVM_CAP_SW_TLB */
987+
981988
#define KVM_MMU_FSL_BOOKE_NOHV 0
982989
#define KVM_MMU_FSL_BOOKE_HV 1
983990

0 commit comments

Comments
 (0)