pv clock misc fixes
Subsystems (5)
cover pv clock misc fixes
Hello folks,
In this series, I present a couple of fixes for kvmclock.
In patch 1, a guest-side fix is proposed for a problem that is biting us for quite a while now. the tsc inside VMs does not seem to be that good, (up to now, only single-socket nehalems were stable enough), and we're seeing small (but nevertheless wrong) time warps inside SMP guests. I am proposing the fix to reside on common code in pvclock.c, but would be good to hear Jeremy on this.
On the other 3 patches, I change kvmclock MSR numbers in a compatible fashion. Both MSR sets will be supported for a while.
Patch 5 adds documentation about kvmclock, which to date, we lacked.
Glauber Costa (5):
Add a global synchronization point for pvclock
change msr numbers for kvmclock
Try using new kvm clock msrs
export new cpuid KVM_CAP
add documentation about kvmclock
Documentation/kvm/kvmclock.txt | 138 +++++++++++++++++++++++++++++++++++++++ arch/x86/include/asm/kvm_para.h | 12 +++- arch/x86/kernel/kvmclock.c | 68 +++++++++++++------ arch/x86/kernel/pvclock.c | 23 +++++++ arch/x86/kvm/x86.c | 13 ++++- include/linux/kvm.h | 1 + 6 files changed, 231 insertions(+), 24 deletions(-) create mode 100644 Documentation/kvm/kvmclock.txt
PATCH 1/5 Add a global synchronization point for pvclock
In recent stress tests, it was found that pvclock-based systems could seriously warp in smp systems. Using ingo's time-warp-test.c, I could trigger a scenario as bad as 1.5mi warps a minute in some systems. (to be fair, it wasn't that bad in most of them). Investigating further, I found out that such warps were caused by the very offset-based calculation pvclock is based on.
This happens even on some machines that report constant_tsc in its tsc flags, specially on multi-socket ones.
Two reads of the same kernel timestamp at approx the same time, will likely have tsc timestamped in different occasions too. This means the delta we calculate is unpredictable at best, and can probably be smaller in a cpu that is legitimately reading clock in a forward ocasion.
Some adjustments on the host could make this window less likely to happen, but still, it pretty much poses as an intrinsic problem of the mechanism.
A while ago, I though about using a shared variable anyway, to hold clock last state, but gave up due to the high contention locking was likely to introduce, possibly rendering the thing useless on big machines. I argue, however, that locking is not necessary.
We do a read-and-return sequence in pvclock, and between read and return, the global value can have changed. However, it can only have changed by means of an addition of a positive value. So if we detected that our clock timestamp is less than the current global, we know that we need to return a higher one, even though it is not exactly the one we compared to.
OTOH, if we detect we're greater than the current time source, we atomically replace the value with our new readings. This do causes contention on big boxes (but big here means *BIG*), but it seems like a good trade off, since it provide us with a time source guaranteed to be stable wrt time warps.
After this patch is applied, I don't see a single warp in time during 5 days of execution, in any of the machines I saw them before.
--- arch/x86/kernel/pvclock.c | 23 +++++++++++++++++++++++ 1 files changed, 23 insertions(+), 0 deletions(-)
arch/x86/kernel/pvclock.c +23 -0
@@ -109,11 +109,14 @@ unsigned long pvclock_tsc_khz(struct pvclock_vcpu_time_info *src)return pv_tsc_khz; } +static u64 last_value =0 ; +cycle_t pvclock_clocksource_read (struct pvclock_vcpu_time_info *src) {struct pvclock_shadow_time shadow;unsigned version;cycle_t ret, offset; +u64 last; do { version = pvclock_get_time_values(&shadow, src);
@@ -123,6 +126,26 @@ cycle_t pvclock_clocksource_read(struct pvclock_vcpu_time_info *src)barrier (); }while (version != src->version); +/* +* Assumption here is that last_value, a global accumulator, always goes +* forward. If we are less than that, we should not be much smaller. +* We assume there is an error marging we're inside, and then the correction +* does not sacrifice accuracy. +* +* For reads: global may have changed between test and return, +* but this means someone else updated poked the clock at a later time. +* We just need to make sure we are not seeing a backwards event. +* +* For updates: last_value = ret is not enough, since two vcpus could be +* updating at the same time, and one of them could be slightly behind, +* making the assumption that last_value always go forward fail to hold. +*/ +do { + last = last_value; +if (ret < last) +return last; + }while (unlikely (cmpxchg64 (&last_value, last, ret) != ret)); +return ret; }
Signature · 1 line
1.6.2.2
PATCH 2/5 change msr numbers for kvmclock
Avi pointed out a while ago that those MSRs falls into the pentium PMU range. So the idea here is to add new ones, and after a while, deprecate the old ones.
--- arch/x86/include/asm/kvm_para.h | 8 ++++++-- arch/x86/kvm/x86.c | 7 ++++++- 2 files changed, 12 insertions(+), 3 deletions(-)
arch/x86/include/asm/kvm_para.h +6 -2
@@ -17,8 +17,12 @@#define KVM_FEATURE_NOP_IO_DELAY 1#define KVM_FEATURE_MMU_OP 2 -#define MSR_KVM_WALL_CLOCK 0x11 -#define MSR_KVM_SYSTEM_TIME 0x12 +#define MSR_KVM_WALL_CLOCK_OLD 0x11 +#define MSR_KVM_SYSTEM_TIME_OLD 0x12 + +/* Custom MSRs falls in the range 0x4b564d00-0x4b564dff */ +#define MSR_KVM_WALL_CLOCK 0x4b564d00 +#define MSR_KVM_SYSTEM_TIME 0x4b564d01#define KVM_MAX_MMU_OP_BATCH 32
arch/x86/kvm/x86.c +6 -1
@@ -575,9 +575,10 @@ static inline u32 bit(int bitno)* kvm-specific. Those are put in the beginning of the list. */ -#define KVM_SAVE_MSRS_BEGIN 5 +#define KVM_SAVE_MSRS_BEGIN 7static u32 msrs_to_save[] = {MSR_KVM_SYSTEM_TIME ,MSR_KVM_WALL_CLOCK , +MSR_KVM_SYSTEM_TIME_OLD ,MSR_KVM_WALL_CLOCK_OLD ,HV_X64_MSR_GUEST_OS_ID ,HV_X64_MSR_HYPERCALL ,HV_X64_MSR_APIC_ASSIST_PAGE ,MSR_IA32_SYSENTER_CS ,MSR_IA32_SYSENTER_ESP ,MSR_IA32_SYSENTER_EIP ,
@@ -1099,10 +1100,12 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)case MSR_IA32_MISC_ENABLE : vcpu->arch.ia32_misc_enable_msr = data;break ; +case MSR_KVM_WALL_CLOCK_OLD :case MSR_KVM_WALL_CLOCK : vcpu->kvm->arch.wall_clock = data;kvm_write_wall_clock (vcpu->kvm, data);break ; +case MSR_KVM_SYSTEM_TIME_OLD :case MSR_KVM_SYSTEM_TIME : {if (vcpu->arch.time_page) {kvm_release_page_dirty (vcpu->arch.time_page);
@@ -1374,9 +1377,11 @@ int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) data = vcpu->arch.efer;break ;case MSR_KVM_WALL_CLOCK : +case MSR_KVM_WALL_CLOCK_OLD : data = vcpu->kvm->arch.wall_clock;break ;case MSR_KVM_SYSTEM_TIME : +case MSR_KVM_SYSTEM_TIME_OLD : data = vcpu->arch.time;break ;case MSR_IA32_P5_MC_ADDR :
Signature · 1 line
1.6.2.2
PATCH 3/5 Try using new kvm clock msrs
We now added a new set of clock-related msrs in replacement of the old ones. In theory, we could just try to use them and get a return value indicating they do not exist, due to our use of kvm_write_msr_save.
However, kvm clock registration happens very early, and if we ever try to write to a non-existant MSR, we raise a lethal #GP, since our idt handlers are not in place yet.
So this patch tests for a cpuid feature exported by the host to decide which set of msrs are supported.
--- arch/x86/include/asm/kvm_para.h | 4 ++ arch/x86/kernel/kvmclock.c | 68 +++++++++++++++++++++++++++------------ 2 files changed, 51 insertions(+), 21 deletions(-)
arch/x86/include/asm/kvm_para.h +4 -0
@@ -16,6 +16,10 @@#define KVM_FEATURE_CLOCKSOURCE 0#define KVM_FEATURE_NOP_IO_DELAY 1#define KVM_FEATURE_MMU_OP 2 +/* We could just try to use new msr values, but they are queried very early, +* kernel does not have idt handlers yet, and failures are fatal */ +#define KVM_FEATURE_CLOCKSOURCE2 3 +#define MSR_KVM_WALL_CLOCK_OLD 0x11#define MSR_KVM_SYSTEM_TIME_OLD 0x12
arch/x86/kernel/kvmclock.c +47 -21
114 lines not on this page · raw message
Signature · 1 line
1.6.2.2
PATCH 4/5 export new cpuid KVM_CAP
Since we're changing the msrs kvmclock uses, we have to communicate that to the guest, through cpuid. We can add a new KVM_CAP to the hypervisor, and then patch userspace to recognize it.
And if we ever add a new cpuid bit in the future, we have to do that again, which create some complexity and delay in feature adoption.
Instead, what I'm proposing in this patch is a new capability, called KVM_CAP_X86_CPUID_FEATURE_LIST, that returns the current feature list currently supported by the hypervisor. If we ever want to add or remove some feature, we only need to tweak into the HV, leaving userspace untouched.
--- arch/x86/kvm/x86.c | 6 ++++++ include/linux/kvm.h | 1 + 2 files changed, 7 insertions(+), 0 deletions(-)
arch/x86/kvm/x86.c +6 -0
@@ -1545,6 +1545,12 @@ int kvm_dev_ioctl_check_extension(long ext)case KVM_CAP_MCE : r =KVM_MAX_MCE_BANKS ;break ; +case KVM_CAP_X86_CPUID_FEATURE_LIST : + r = (1 <<KVM_FEATURE_CLOCKSOURCE ) | + (1 <<KVM_FEATURE_NOP_IO_DELAY ) | + (1 <<KVM_FEATURE_MMU_OP ) | + (1 <<KVM_FEATURE_CLOCKSOURCE2 ); +break ;default : r =0 ;break ;
include/linux/kvm.h +1 -0
@@ -507,6 +507,7 @@ struct kvm_ioeventfd {#define KVM_CAP_DEBUGREGS 50#endif #define KVM_CAP_X86_ROBUST_SINGLESTEP 51 +#define KVM_CAP_X86_CPUID_FEATURE_LIST 52#ifdef KVM_CAP_IRQ_ROUTING
Signature · 1 line
1.6.2.2
PATCH 5/5 add documentation about kvmclock
This patch adds a new file, kvm/kvmclock.txt, describing the mechanism we use in kvmclock.
--- Documentation/kvm/kvmclock.txt | 138 ++++++++++++++++++++++++++++++++++++++++ 1 files changed, 138 insertions(+), 0 deletions(-) create mode 100644 Documentation/kvm/kvmclock.txt
Documentation/kvm/kvmclock.txt new file +138 -0
@@ -0,0 +1,138 @@ +KVM Paravirtual Clocksource driver +Glauber Costa, Red Hat Inc. +================================== + +1. General Description +======================= + +Keeping time in virtual machine is acknowledged as a hard problem. The most +basic mode of operation, usually done by older guests, assumes a fixed length +between timer interrupts. It then counts the number of interrupts and +calculates elapsed time. This method fails easily in virtual machines, since +we can't guarantee that the virtual interrupt will be delivered in time. + +Another possibility is to emulate modern devices like HPET, or any other we +see fit. A modern guest which implements something like the clocksource +infrastructure, can then ask this virtual device about current time when it +needs to. The problem with this approach, is that it bumps the guest out +of guest mode operation, and in some cases, even to userspace very frequently. + +In this context, the best approach is to provide the guest with a +virtualization-aware (paravirtual) clock device. It the asks the hypervisor +about current time, guaranteeing both stable and accurate timekeeping. + +2. kvmclock basics +=========================== + +When supported by the hypervisor, guests can register a memory page +to contain kvmclock data. This page has to be present in guest's address space +throughout its whole life. The hypervisor continues to write to it until it is +explicitly disabled or the guest is turned off. + +2.1 kvmclock availability +------------------------- + +Guests that want to take advantage of kvmclock should first check its +availability through cpuid. + +kvm features are presented to the guest in leaf 0x40000001. Bit 3 indicates +the present of kvmclock. Bit 0 indicates that kvmclock is present, but the +old MSR set must be used. See section 2.3 for details. + +2.2 kvmclock functionality +-------------------------- + +Two MSRs are provided by the hypervisor, controlling kvmclock operation: + + * MSR_KVM_WALL_CLOCK, value 0x4b564d00 and + * MSR_KVM_SYSTEM_TIME, value 0x4b564d01. + +The first one is only used in rare situations, like boot-time and a +suspend-resume cycle. Data is disposable, and after used, the guest +may use it for something else. This is hardly a hot path for anything. +The Hypervisor fills in the address provided through this MSR with the +following structure: + +struct pvclock_wall_clock { + u32 version; + u32 sec; + u32 nsec; +} __attribute__((__packed__)); + +Guest should only trust data to be valid when version haven't changed before +and after reads of sec and nsec. Besides not changing, it has to be an even +number. Hypervisor may write an odd number to version field to indicate that +an update is in progress. + +MSR_KVM_SYSTEM_TIME, on the other hand, has persistent data, and is +constantly updated by the hypervisor with time information. The data +written in this MSR contains two pieces of information: the address in which +the guests expects time data to be present 4-byte aligned or'ed with an +enabled bit. If one wants to shutdown kvmclock, it just needs to write +anything that has 0 as its last bit. + +Time information presented by the hypervisor follows the structure: + +struct pvclock_vcpu_time_info { + u32 version; + u32 pad0; + u64 tsc_timestamp; + u64 system_time; + u32 tsc_to_system_mul; + s8 tsc_shift; + u8 pad[3]; +} __attribute__((__packed__)); + +The version field plays the same role as with the one in struct +pvclock_wall_clock. The other fields, are: + + a. tsc_timestamp: the guest-visible tsc (result of rdtsc + tsc_offset) of + this cpu at the moment we recorded system_time. Note that some time is + inevitably spent between system_time and tsc_timestamp measurements. + Guests can subtract this quantity from the current value of tsc to obtain + a delta to be added to system_time + + b. system_time: this is the most recent host-time we could be provided with. + host gets it through ktime_get_ts, using whichever clocksource is + registered at the moment + + c. tsc_to_system_mul: this is the number that tsc delta has to be multiplied + by in order to obtain time in nanoseconds. Hypervisor is free to change + this value in face of events like cpu frequency change, pcpu migration, + etc. + + d. tsc_shift: guests must shift + +With this information available, guest calculates current time as: + + T = kt + to_nsec(tsc - tsc_0) + +2.3 Compatibility MSRs +---------------------- + +Guests running on top of older hypervisors may have to use a different set of +MSRs. This is because originally, kvmclock MSRs were exported within a +reserved range by accident. Guests should check cpuid leaf 0x40000001 for the +presence of kvmclock. If bit 3 is disabled, but bit 0 is enabled, guests can +have access to kvmclock functionality through + + * MSR_KVM_WALL_CLOCK_OLD, value 0x11 and + * MSR_KVM_SYSTEM_TIME_OLD, value 0x12. + +Note, however, that this is deprecated. + +3. Migration +============ + +Two ioctls are provided to aid the task of migration: + + * KVM_GET_CLOCK and + * KVM_SET_CLOCK + +Their aim is to control an offset that can be summed to system_time, in order +to guarantee monotonicity on the time over guest migration. Source host +executes KVM_GET_CLOCK, obtaining the last valid timestamp in this host, while +destination sets it with KVM_SET_CLOCK. It's the destination responsibility to +never return time that is less than that. + +
Signature · 1 line
1.6.2.2
Re: [PATCH 5/5] add documentation about kvmclock
Quoted 22 lines · On Thu, 15 Apr 2010 14:37:28 -0400 Glauber Costa wrote:
This patch adds a new file,
kvm/kvmclock.txt, describing the mechanism we use in kvmclock.Signed-off-by Glauber Costa <[email protected]>--- Documentation/kvm/kvmclock.txt | 138 ++++++++++++++++++++++++++++++++++++++++ 1 files changed, 138 insertions(+), 0 deletions(-) create mode 100644 Documentation/kvm/kvmclock.txtDocumentation/kvm/kvmclock.txt new file +7
-0@@ -0,0 +1,138 @@ +KVM Paravirtual Clocksource driver +Glauber Costa, Red Hat Inc. +================================== + +1. General Description +======================= +
...
Quoted 7 lines
Documentation/kvm/kvmclock.txt new file +7
-0+ +2. kvmclock basics +=========================== + +When supported by the hypervisor, guests can register a memory page +to contain kvmclock data. This page has to be present in guest's address space +throughout its whole life. The hypervisor continues to write to it until it is
Documentation/kvm/kvmclock.txt line 39
Quoted 10 lines
Documentation/kvm/kvmclock.txt new file +10
-0+explicitly disabled or the guest is turned off. + +2.1 kvmclock availability +------------------------- + +Guests that want to take advantage of kvmclock should first check its +availability through cpuid. + +kvm features are presented to the guest in leaf 0x40000001. Bit 3 indicates +the present of kvmclock. Bit 0 indicates that kvmclock is present, but the
presence
but it's confusing. Is it bit 3 or bit 0? They seem to indicate the same thing.
Quoted 1 line
+old MSR set must be used. See section 2.3 for details.
"old MSR set": what does this mean?
Quoted 12 lines
Documentation/kvm/kvmclock.txt new file +12
-0+ +2.2 kvmclock functionality +-------------------------- + +Two MSRs are provided by the hypervisor, controlling kvmclock operation: + + * MSR_KVM_WALL_CLOCK, value 0x4b564d00 and + * MSR_KVM_SYSTEM_TIME, value 0x4b564d01. + +The first one is only used in rare situations, like boot-time and a +suspend-resume cycle. Data is disposable, and after used, the guest +may use it for something else. This is hardly a hot path for anything.
Documentation/kvm/kvmclock.txt line 62
Quoted 10 lines
Documentation/kvm/kvmclock.txt new file +10
-0+The Hypervisor fills in the address provided through this MSR with the +following structure: + +struct pvclock_wall_clock { + u32 version; + u32 sec; + u32 nsec; +} __attribute__((__packed__)); + +Guest should only trust data to be valid when version haven't changed before
has not
Quoted 18 lines
Documentation/kvm/kvmclock.txt new file +18
-0+and after reads of sec and nsec. Besides not changing, it has to be an even +number. Hypervisor may write an odd number to version field to indicate that +an update is in progress. + +MSR_KVM_SYSTEM_TIME, on the other hand, has persistent data, and is +constantly updated by the hypervisor with time information. The data +written in this MSR contains two pieces of information: the address in which +the guests expects time data to be present 4-byte aligned or'ed with an +enabled bit. If one wants to shutdown kvmclock, it just needs to write +anything that has 0 as its last bit. + +Time information presented by the hypervisor follows the structure: + +struct pvclock_vcpu_time_info { + u32 version; + u32 pad0; + u64 tsc_timestamp; + u64 system_time;
Documentation/kvm/kvmclock.txt line 90
Quoted 10 lines
Documentation/kvm/kvmclock.txt new file +10
-0+ u32 tsc_to_system_mul; + s8 tsc_shift; + u8 pad[3]; +} __attribute__((__packed__)); + +The version field plays the same role as with the one in struct +pvclock_wall_clock. The other fields, are: + + a. tsc_timestamp: the guest-visible tsc (result of rdtsc + tsc_offset) of + this cpu at the moment we recorded system_time. Note that some time is
CPU (please)
Documentation/kvm/kvmclock.txt line 93
Quoted 3 lines
Documentation/kvm/kvmclock.txt new file +3
-0+ inevitably spent between system_time and tsc_timestamp measurements. + Guests can subtract this quantity from the current value of tsc to obtain + a delta to be added to system_time
to system_time.
Documentation/kvm/kvmclock.txt line 97
Quoted 4 lines
Documentation/kvm/kvmclock.txt new file +4
-0+ + b. system_time: this is the most recent host-time we could be provided with. + host gets it through ktime_get_ts, using whichever clocksource is + registered at the moment
moment.
Documentation/kvm/kvmclock.txt line 101
Quoted 4 lines
Documentation/kvm/kvmclock.txt new file +4
-0+ + c. tsc_to_system_mul: this is the number that tsc delta has to be multiplied + by in order to obtain time in nanoseconds. Hypervisor is free to change + this value in face of events like cpu frequency change, pcpu migration,
CPU
Quoted 3 lines
+ etc.
+
+ d.tsc_shift: guests must shift
missing text??
Quoted 22 lines
Documentation/kvm/kvmclock.txt new file +22
-0+ +With this information available, guest calculates current time as: + + T = kt + to_nsec(tsc - tsc_0) + +2.3 Compatibility MSRs +---------------------- + +Guests running on top of older hypervisors may have to use a different set of +MSRs. This is because originally, kvmclock MSRs were exported within a +reserved range by accident. Guests should check cpuid leaf 0x40000001 for the +presence of kvmclock. If bit 3 is disabled, but bit 0 is enabled, guests can +have access to kvmclock functionality through + + * MSR_KVM_WALL_CLOCK_OLD, value 0x11 and + * MSR_KVM_SYSTEM_TIME_OLD, value 0x12. + +Note, however, that this is deprecated. + +3. Migration +============ +
Documentation/kvm/kvmclock.txt line 136
Quoted 10 lines
Documentation/kvm/kvmclock.txt new file +10
-0+Two ioctls are provided to aid the task of migration: + + * KVM_GET_CLOCK and + * KVM_SET_CLOCK + +Their aim is to control an offset that can be summed to system_time, in order +to guarantee monotonicity on the time over guest migration. Source host +executes KVM_GET_CLOCK, obtaining the last valid timestamp in this host, while +destination sets it with KVM_SET_CLOCK. It's the destination responsibility to +never return time that is less than that.
---
~Randy
Re: [PATCH 5/5] add documentation about kvmclock
Quoted 2 lines · Randy Dunlap
On Thu, 15 Apr 2010 14:37:28 -0400 Glauber Costa wrote:
Thanks Randy,
All coments relevant. I'll update the document to cover them.
As for your question: Both bit 3 and 0 are used, yes. But they tell the guest to use a different MSR pair. However, this document is intented for people not familiar with kvmclock. If you read it, and could not extract that from the text, it definitely needs to be augmented.
Thanks!
Re: [PATCH 4/5] export new cpuid KVM_CAP
In reply to Glauber CostaQuoted 4 lines · Glauber Costa
Since we're changing the msrs kvmclock uses, we have to communicate that to the guest, through cpuid. We can add a new
KVM_CAPto the hypervisor, and then patch userspace to recognize it.
Quoted 9 lines
And if we ever add a new cpuid bit in the future, we have to do that again, which create some complexity and delay in feature adoption.
Instead, what I'm proposing in this patch is a new capability, called
KVM_CAP_X86_CPUID_FEATURE_LIST, that returns the current feature list currently supported by the hypervisor. If we ever want to add or remove some feature, we only need to tweak into the HV, leaving userspace untouched.
Hm. We need to update userspace anyway, since we don't like turning features on unconditionally (it breaks live migration into an older kernel).
Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
Re: [PATCH 4/5] export new cpuid KVM_CAP
Quoted 14 lines · Avi Kivity
Quoted 4 lines · Glauber Costa
Since we're changing the msrs kvmclock uses, we have to communicate that to the guest, through cpuid. We can add a new
KVM_CAPto the hypervisor, and then patch userspace to recognize it.Quoted 8 lines
And if we ever add a new cpuid bit in the future, we have to do that again, which create some complexity and delay in feature adoption.
Instead, what I'm proposing in this patch is a new capability, called
KVM_CAP_X86_CPUID_FEATURE_LIST, that returns the current feature list currently supported by the hypervisor. If we ever want to add or remove some feature, we only need to tweak into the HV, leaving userspace untouched.
Quoted 3 lines
Hm. We need to update userspace anyway, since we don't like turning features on unconditionally (it breaks live migration into an older kernel).
Right now, we don't have any mechanism to disable, say, kvmclock cpuid bit at userspace. But let's suppose we have: What's the difference between disabling it in the way it is now, and disabling it with the method I am proposing?
All this ioctl say is: "Those are the current supported stuff in this HV". It does not mandate userspace to expose all of this to the guest. It just saves us from the job of creating yet another CAP for every bit we plan on including.
If we want to be conservative, we can keep everything but the things we know already disabled, in userspace.
Re: [PATCH 4/5] export new cpuid KVM_CAP
Quoted 17 lines · Glauber Costa
Quoted 15 lines · Avi Kivity
Quoted 13 lines · Glauber Costa
Since we're changing the msrs kvmclock uses, we have to communicate that to the guest, through cpuid. We can add a new
KVM_CAPto the hypervisor, and then patch userspace to recognize it.And if we ever add a new cpuid bit in the future, we have to do that again, which create some complexity and delay in feature adoption.
Instead, what I'm proposing in this patch is a new capability, called
KVM_CAP_X86_CPUID_FEATURE_LIST, that returns the current feature list currently supported by the hypervisor. If we ever want to add or remove some feature, we only need to tweak into the HV, leaving userspace untouched.
Quoted 6 lines
Quoted 4 lines
Hm. We need to update userspace anyway, since we don't like turning features on unconditionally (it breaks live migration into an older kernel).
Right now, we don't have any mechanism to disable, say, kvmclock cpuid bit at userspace.
(that's a serious bug wrt migration, btw)
Quoted 3 lines
But let's suppose we have: What's the difference between disabling it in the way it is now, and disabling it with the method I am proposing?
No difference.
Quoted 4 lines
All this ioctl say is: "Those are the current supported stuff in this HV". It does not mandate userspace to expose all of this to the guest. It just saves us from the job of creating yet another CAP for every bit we plan on including.
Right. Well, creating a new CAP and creating a new FEATURE flag aren't very different, and I'd like to avoid API churn. We have enough new APIs due to missing or badly implemented features; I'd like to avoid new ones whenever possible.
It's not like it saves userspace anything, it has to accommodate older kernels anyhow. We are able to ignore pre 2.6.27 kernels, but now with kvm shipped in long term support distributions, deprecating APIs will be much harder.
Quoted 3 lines
If we want to be conservative, we can keep everything but the things we know already disabled, in userspace.
We definitely need to do that.
Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
Re: [PATCH 3/5] Try using new kvm clock msrs
In reply to Glauber CostaQuoted 18 lines · Glauber Costa
We now added a new set of clock-related msrs in replacement of the old ones. In theory, we could just try to use them and get a return value indicating they do not exist, due to our use of
kvm_write_msr_save.However, kvm clock registration happens very early, and if we ever try to write to a non-existant MSR, we raise a lethal #GP, since our idt handlers are not in place yet.
So this patch tests for a cpuid feature exported by the host to decide which set of msrs are supported.
arch/x86/include/asm/kvm_para.h +0
-0
arch/x86/include/asm/kvm_para.h line 22
Quoted 7 lines
arch/x86/include/asm/kvm_para.h +3
-0@@ -16,6 +16,10 @@#define KVM_FEATURE_CLOCKSOURCE 0#define KVM_FEATURE_NOP_IO_DELAY 1#define KVM_FEATURE_MMU_OP 2 +/* We could just try to use new msr values, but they are queried very early, +* kernel does not have idt handlers yet, and failures are fatal */ +#define KVM_FEATURE_CLOCKSOURCE2 3
This comment has no place in a header that's oriented to userspace. Instead, it's better to document what this bit exposes.
arch/x86/include/asm/kvm_para.h line 25
Quoted 4 lines
arch/x86/include/asm/kvm_para.h +1
-0static int kvmclock =1 ; +static int kvm_use_new_msrs =0 ;
Instead of this and all those if ()s all over the place, you can define variables for the MSR indices. Set them up on initialization, the rest of the code just looks them up.
Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
Re: [PATCH 2/5] change msr numbers for kvmclock
In reply to Glauber CostaQuoted 17 lines · Glauber Costa
Avi pointed out a while ago that those MSRs falls into the pentium PMU range. So the idea here is to add new ones, and after a while, deprecate the old ones.
Signed-off-by Glauber Costa<[email protected]>--- arch/x86/include/asm/kvm_para.h | 8 ++++++-- arch/x86/kvm/x86.c | 7 ++++++- 2 files changed, 12 insertions(+), 3 deletions(-)arch/x86/include/asm/kvm_para.h +0
-0@@ -17,8 +17,12 @@#define KVM_FEATURE_NOP_IO_DELAY 1#define KVM_FEATURE_MMU_OP 2
arch/x86/include/asm/kvm_para.h line 26
Quoted 10 lines
arch/x86/include/asm/kvm_para.h +6
-2-#define MSR_KVM_WALL_CLOCK 0x11 -#define MSR_KVM_SYSTEM_TIME 0x12 +#define MSR_KVM_WALL_CLOCK_OLD 0x11 +#define MSR_KVM_SYSTEM_TIME_OLD 0x12 + +/* Custom MSRs falls in the range 0x4b564d00-0x4b564dff */ +#define MSR_KVM_WALL_CLOCK 0x4b564d00 +#define MSR_KVM_SYSTEM_TIME 0x4b564d01
This is exposed to userspace. Userspace that is compiled with the new headers, but runs on an old kernel, will break.
So you need to keep the old names, and define a new KVM_FEATURE for the new names.
Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
Re: [PATCH 1/5] Add a global synchronization point for pvclock
In reply to Glauber CostaQuoted 51 lines · Glauber Costa
In recent stress tests, it was found that pvclock-based systems could seriously warp in smp systems. Using ingo's
time-warp-test.c, I could trigger a scenario as bad as 1.5mi warps a minute in some systems. (to be fair, it wasn't that bad in most of them). Investigating further, I found out that such warps were caused by the very offset-based calculation pvclock is based on.This happens even on some machines that report
constant_tscin its tsc flags, specially on multi-socket ones.Two reads of the same kernel timestamp at approx the same time, will likely have tsc timestamped in different occasions too. This means the delta we calculate is unpredictable at best, and can probably be smaller in a cpu that is legitimately reading clock in a forward ocasion.
Some adjustments on the host could make this window less likely to happen, but still, it pretty much poses as an intrinsic problem of the mechanism.
A while ago, I though about using a shared variable anyway, to hold clock last state, but gave up due to the high contention locking was likely to introduce, possibly rendering the thing useless on big machines. I argue, however, that locking is not necessary.
We do a read-and-return sequence in pvclock, and between read and return, the global value can have changed. However, it can only have changed by means of an addition of a positive value. So if we detected that our clock timestamp is less than the current global, we know that we need to return a higher one, even though it is not exactly the one we compared to.
OTOH, if we detect we're greater than the current time source, we atomically replace the value with our new readings. This do causes contention on big boxes (but big here means *BIG*), but it seems like a good trade off, since it provide us with a time source guaranteed to be stable wrt time warps.
After this patch is applied, I don't see a single warp in time during 5 days of execution, in any of the machines I saw them before.
Signed-off-by Glauber Costa <[email protected]>CC Jeremy Fitzhardinge <[email protected]>CC Avi Kivity <[email protected]>CC Marcelo Tosatti <[email protected]>CC Zachary Amsden <[email protected]>--- arch/x86/kernel/pvclock.c | 23 +++++++++++++++++++++++ 1 files changed, 23 insertions(+), 0 deletions(-)arch/x86/kernel/pvclock.c +0
-0
arch/x86/kernel/pvclock.c unsigned long pvclock_tsc_khz(struct pvclock_vcpu_time_info *src) · line 113
Quoted 5 lines
arch/x86/kernel/pvclock.c +2
-0@@ -109,11 +109,14 @@ unsigned long pvclock_tsc_khz(struct pvclock_vcpu_time_info *src)return pv_tsc_khz; } +static u64 last_value =0 ; +
__cacheline_aligned_in_smp to avoid other data from sharing the cacheline.
Quoted 23 lines
arch/x86/kernel/pvclock.c +11
-0cycle_t pvclock_clocksource_read (struct pvclock_vcpu_time_info *src) {struct pvclock_shadow_time shadow;unsigned version;cycle_t ret, offset; +u64 last; do { version = pvclock_get_time_values(&shadow, src);@@ -123,6 +126,26 @@ cycle_t pvclock_clocksource_read(struct pvclock_vcpu_time_info *src)barrier (); }while (version != src->version); +/* +* Assumption here is that last_value, a global accumulator, always goes +* forward. If we are less than that, we should not be much smaller. +* We assume there is an error marging we're inside, and then the correction +* does not sacrifice accuracy. +* +* For reads: global may have changed between test and return, +* but this means someone else updated poked the clock at a later time. +* We just need to make sure we are not seeing a backwards event. +*
arch/x86/kernel/pvclock.c cycle_t pvclock_clocksource_read(struct pvclock_vcpu_time_info *src) · line 148
Quoted 10 lines
arch/x86/kernel/pvclock.c +10
-0+* For updates: last_value = ret is not enough, since two vcpus could be +* updating at the same time, and one of them could be slightly behind, +* making the assumption that last_value always go forward fail to hold. +*/ +do { + last = last_value; +if (ret < last) +return last; + }while (unlikely (cmpxchg64 (&last_value, last, ret) != ret)); +
Don't you need to handle wrap-around?
Re: [PATCH 1/5] Add a global synchronization point for pvclock
In reply to Glauber CostaQuoted 4 lines · Glauber Costa
In recent stress tests, it was found that pvclock-based systems could seriously warp in smp systems. Using ingo's
time-warp-test.c, I could trigger a scenario as bad as 1.5mi warps a minute in some systems.
Is that "1.5 million"?
Quoted 4 lines
(to be fair, it wasn't that bad in most of them). Investigating further, I found out that such warps were caused by the very offset-based calculation pvclock is based on.
Is the problem that the tscs are starting out of sync, or that they're drifting relative to each other over time? Do the problems become worse the longer the uptime? How large are the offsets we're talking about here?
Quoted 69 lines
This happens even on some machines that report
constant_tscin its tsc flags, specially on multi-socket ones.Two reads of the same kernel timestamp at approx the same time, will likely have tsc timestamped in different occasions too. This means the delta we calculate is unpredictable at best, and can probably be smaller in a cpu that is legitimately reading clock in a forward ocasion.
Some adjustments on the host could make this window less likely to happen, but still, it pretty much poses as an intrinsic problem of the mechanism.
A while ago, I though about using a shared variable anyway, to hold clock last state, but gave up due to the high contention locking was likely to introduce, possibly rendering the thing useless on big machines. I argue, however, that locking is not necessary.
We do a read-and-return sequence in pvclock, and between read and return, the global value can have changed. However, it can only have changed by means of an addition of a positive value. So if we detected that our clock timestamp is less than the current global, we know that we need to return a higher one, even though it is not exactly the one we compared to.
OTOH, if we detect we're greater than the current time source, we atomically replace the value with our new readings. This do causes contention on big boxes (but big here means *BIG*), but it seems like a good trade off, since it provide us with a time source guaranteed to be stable wrt time warps.
After this patch is applied, I don't see a single warp in time during 5 days of execution, in any of the machines I saw them before.
Signed-off-by Glauber Costa <[email protected]>CC Jeremy Fitzhardinge <[email protected]>CC Avi Kivity <[email protected]>CC Marcelo Tosatti <[email protected]>CC Zachary Amsden <[email protected]>--- arch/x86/kernel/pvclock.c | 23 +++++++++++++++++++++++ 1 files changed, 23 insertions(+), 0 deletions(-)arch/x86/kernel/pvclock.c +10
-0@@ -109,11 +109,14 @@ unsigned long pvclock_tsc_khz(struct pvclock_vcpu_time_info *src)return pv_tsc_khz; } +static u64 last_value =0 ; +cycle_t pvclock_clocksource_read (struct pvclock_vcpu_time_info *src) {struct pvclock_shadow_time shadow;unsigned version;cycle_t ret, offset; +u64 last; do { version = pvclock_get_time_values(&shadow, src);@@ -123,6 +126,26 @@ cycle_t pvclock_clocksource_read(struct pvclock_vcpu_time_info *src)barrier (); }while (version != src->version); +/* +* Assumption here is that last_value, a global accumulator, always goes +* forward. If we are less than that, we should not be much smaller. +* We assume there is an error marging we're inside, and then the correction +* does not sacrifice accuracy. +* +* For reads: global may have changed between test and return,
arch/x86/kernel/pvclock.c cycle_t pvclock_clocksource_read(struct pvclock_vcpu_time_info *src) · line 145
Quoted 10 lines
arch/x86/kernel/pvclock.c +9
-0+* but this means someone else updated poked the clock at a later time. +* We just need to make sure we are not seeing a backwards event. +* +* For updates: last_value = ret is not enough, since two vcpus could be +* updating at the same time, and one of them could be slightly behind, +* making the assumption that last_value always go forward fail to hold. +*/ + do { + last = last_value;
Does this need a barrier() to prevent the compiler from re-reading last_value for the subsequent lines? Otherwise "(ret < last)" and "return last" could execute with different values for "last".
arch/x86/kernel/pvclock.c cycle_t pvclock_clocksource_read(struct pvclock_vcpu_time_info *src) · line 151
Quoted 4 lines
arch/x86/kernel/pvclock.c +3
-0+if (ret < last) +return last; + }while (unlikely (cmpxchg64 (&last_value, last, ret) != ret));
So if CPU A's tsc is, say, 50us behind CPU B's, then it will return a static time (from last_time) for 50us until its tsc catched up - or if CPU A happens to update last_time to give it a kick?
Is it worth trying to update CPU A's tsc offset at the same time?
J
Re: [PATCH 1/5] Add a global synchronization point for pvclock
Quoted 10 lines · Jeremy Fitzhardinge
Quoted 5 lines · Glauber Costa
In recent stress tests, it was found that pvclock-based systems could seriously warp in smp systems. Using ingo's
time-warp-test.c, I could trigger a scenario as bad as 1.5mi warps a minute in some systems.Is that "1.5 million"?
Quoted 9 lines
Quoted 5 lines
(to be fair, it wasn't that bad in most of them). Investigating further, I found out that such warps were caused by the very offset-based calculation pvclock is based on.
Is the problem that the tscs are starting out of sync, or that they're drifting relative to each other over time? Do the problems become worse the longer the uptime? How large are the offsets we're talking about here?
This is one source of the problem, but the same thing happens at many levels... tsc may start out of sync, drift between sockets, be badly re-calibrated by the BIOS, etc... the issue persists even if the TSCs are perfectly in sync - the measurement of them is not.
So reading TSC == 100,000 units at time A and then waiting 10 units, one may read TSC == 100,010 +/- 5 units because the code stream is not perfectly serialized - nor can it be. There will always be some amount of error unless running in perfect lock-step, which only happens in a simulator.
This inherent measurement error can cause apparent time to go backwards when measured simultaneously across multiple CPUs, or when re-calibrating against an external clocksource. Combined with other factors as above, it can be of sufficient magnitude to be noticed. KVM clock is particularly exposed to the problem because the TSC is measured and recalibrated for each virtual CPU whenever there is a physical CPU switch, so micro-adjustments forwards and backwards may occur during the recalibration - and appear as a real backwards time warp to the guest. I have some patches to fix that issue, but the SMP problem remains to be fixed - and is addressed quite thoroughly by this patch.
Zach
Re: [PATCH 1/5] Add a global synchronization point for pvclock
In reply to Jeremy FitzhardingeQuoted 7 lines · Jeremy Fitzhardinge
arch/x86/kernel/pvclock.c cycle_t pvclock_clocksource_read(struct pvclock_vcpu_time_info *src) · line 144
Quoted 3 lines
arch/x86/kernel/pvclock.c +2
-0+ do { + last = last_value;Does this need a
barrier()to prevent the compiler from re-readinglast_valuefor the subsequent lines? Otherwise "(ret < last)" and "return last" could execute with different values for "last".
ACCESS_ONCE() is your friend.
Re: [PATCH 1/5] Add a global synchronization point for pvclock
Quoted 6 lines · Peter Zijlstra
Quoted 4 lines · Jeremy Fitzhardinge
Quoted 4 lines
arch/x86/kernel/pvclock.c +2
-0+ do { + last = last_value;
Quoted 7 lines
Quoted 5 lines
Does this need a
barrier()to prevent the compiler from re-readinglast_valuefor the subsequent lines? Otherwise "(ret< last)" and "return last" could execute with different values for "last".
ACCESS_ONCE()is your friend.
I think it's implied with atomic64_read().
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH 1/5] Add a global synchronization point for pvclock
Quoted 7 lines · Avi Kivity
Quoted 6 lines · Peter Zijlstra
Quoted 4 lines · Jeremy Fitzhardinge
Quoted 4 lines
arch/x86/kernel/pvclock.c +2
-0+ do { + last = last_value;
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Does this need a
barrier()to prevent the compiler from re-readinglast_valuefor the subsequent lines? Otherwise "(ret< last)" and "return last" could execute with different values for "last".
ACCESS_ONCE()is your friend.I think it's implied with
atomic64_read().
Yes it would be. I was merely trying to point out that
last = ACCESS_ONCE(last_value);
Is a narrower way of writing:
last = last_value; barrier();
In that it need not clobber all memory locations and makes it instantly clear what we want the barrier for.
Re: [PATCH 1/5] Add a global synchronization point for pvclock
Quoted 10 lines · Peter Zijlstra
Quoted 6 lines
Quoted 4 lines
ACCESS_ONCE()is your friend.I think it's implied with
atomic64_read().Yes it would be. I was merely trying to point out that
last = ACCESS_ONCE(last_value);
Quoted 8 lines
Is a narrower way of writing:
last = last_value; barrier();In that it need not clobber all memory locations and makes it instantly clear what we want the barrier for.
Oh yes, just trying to avoid a patch with both atomic64_read() and ACCESS_ONCE().
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH 1/5] Add a global synchronization point for pvclock
Quoted 11 lines · Avi Kivity
Quoted 10 lines · Peter Zijlstra
Quoted 4 lines
Quoted 3 lines
ACCESS_ONCE()is your friend.I think it's implied with
atomic64_read().Yes it would be. I was merely trying to point out that
last = ACCESS_ONCE(last_value);Is a narrower way of writing:
Quoted 8 lines
Quoted 5 lines
last = last_value; barrier();In that it need not clobber all memory locations and makes it instantly clear what we want the barrier for.
Oh yes, just trying to avoid a patch with both
atomic64_read()andACCESS_ONCE().
you're mixing the private version of the patch you saw with this one. there isn't any atomic reads in here. I'll use a barrier then
Re: [PATCH 1/5] Add a global synchronization point for pvclock
Quoted 7 lines · Glauber Costa
Quoted 4 lines
Oh yes, just trying to avoid a patch with both
atomic64_read()andACCESS_ONCE().you're mixing the private version of the patch you saw with this one. there isn't any atomic reads in here. I'll use a barrier then
This patch writes last_value atomically, but reads it non-atomically. A barrier is insufficient.
Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
Re: [PATCH 1/5] Add a global synchronization point for pvclock
Quoted 11 lines · Avi Kivity
Quoted 7 lines · Glauber Costa
Quoted 4 lines
Oh yes, just trying to avoid a patch with both
atomic64_read()andACCESS_ONCE().you're mixing the private version of the patch you saw with this one. there isn't any atomic reads in here. I'll use a barrier then
This patch writes
last_valueatomically, but reads it non-atomically. A barrier is insufficient.
What avi says! :-)
On a 32bit machine a 64bit read are two 32bit reads, so
last = last_value;
becomes:
last.high = last_value.high; last.low = last_vlue.low;
(or the reverse of course)
Now imagine a write getting interleaved with that ;-)
Re: [PATCH 1/5] Add a global synchronization point for pvclock
Quoted 6 lines · Peter Zijlstra
What avi says! :-)
On a 32bit machine a 64bit read are two 32bit reads, so
last = last_value;
Quoted 9 lines
becomes:
last.high = last_value.high; last.low = last_vlue.low;(or the reverse of course)
Now imagine a write getting interleaved with that ;-)
You could explicitly do:
do {
h = last.high;
barrier();
l = last.low;
barrier();
} while (last.high != h);This works because we expect last to be always increasing, so the only worry is low wrapping and incrementing high, and is more efficient than making the read fully atomic (the write is still cmpxchg64). But it's pretty ugly to open code just for 32b architectures; its something that might be useful to turn into a general abstraction (monotonic_read_64 FTW!). I already have code like this in the Xen time code, so I could make immediate use of it.
J
Re: [PATCH 1/5] Add a global synchronization point for pvclock
Quoted 28 lines · Jeremy Fitzhardinge
Quoted 6 lines · Peter Zijlstra
What avi says! :-)
On a 32bit machine a 64bit read are two 32bit reads, so
last = last_value;Quoted 10 lines
becomes:
last.high = last_value.high; last.low = last_vlue.low;(or the reverse of course)
Now imagine a write getting interleaved with that ;-)
You could explicitly do:
do { h = last.high; barrier(); l = last.low; barrier(); } while (last.high != h);
Quoted 8 lines
This works because we expect last to be always increasing, so the only worry is low wrapping and incrementing high, and is more efficient than making the read fully atomic (the write is still cmpxchg64). But it's pretty ugly to open code just for 32b architectures; its something that might be useful to turn into a general abstraction (
monotonic_read_64FTW!). I already have code like this in the Xen time code, so I could make immediate use of it.
I don't think this is worthwhile - the cmpxchg is not that expensive on most kvm capable hosts (the exception is the Pentium D).
btw, do you want this code in pvclock.c, or shall we keep it kvmclock specific?
Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
Re: [PATCH 1/5] Add a global synchronization point for pvclock
Quoted 2 lines · Avi Kivity
btw, do you want this code in
pvclock.c, or shall we keep it kvmclock specific?
I think its a pvclock-level fix. I'd been hoping to avoid having something like this, but I think its ultimately necessary.
J
Re: [PATCH 1/5] Add a global synchronization point for pvclock
Quoted 8 lines · Jeremy Fitzhardinge
Quoted 3 lines · Avi Kivity
btw, do you want this code in
pvclock.c, or shall we keep it kvmclock specific?I think its a pvclock-level fix. I'd been hoping to avoid having something like this, but I think its ultimately necessary.
Did you observe drift on Xen, or is this "ultimately" pointing at the future?
Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
Re: [PATCH 1/5] Add a global synchronization point for pvclock
Quoted 6 lines · Avi Kivity
Quoted 5 lines · Jeremy Fitzhardinge
Quoted 3 lines · Avi Kivity
btw, do you want this code in
pvclock.c, or shall we keep it kvmclock specific?
Quoted 6 lines
Quoted 3 lines
I think its a pvclock-level fix. I'd been hoping to avoid having something like this, but I think its ultimately necessary.
Did you observe drift on Xen, or is this "ultimately" pointing at the future?
People are reporting weirdnesses that "clocksource=jiffies" apparently resolves. Xen and KVM are faced with the same hardware constraints, and it wouldn't surprise me if there were small measurable non-monotonicities in the PV clock under Xen. May as well be safe.
Of course, it kills any possibility of being able to usefully export this interface down to usermode.
My main concern about this kind of simple fix is that if there's a long term systematic drift between different CPU's tscs, then this will somewhat mask the problem while giving really awful time measurement on the "slow" CPU(s). In that case it really needs to adjust the scaling factor to correct for the drift (*not* update the offset). But if we're definitely only talking about fixed, relatively small time offsets then it is fine.
J
Re: [PATCH 1/5] Add a global synchronization point for pvclock
Quoted 15 lines · Jeremy Fitzhardinge
Quoted 13 lines · Avi Kivity
Quoted 11 lines · Jeremy Fitzhardinge
Quoted 4 lines · Avi Kivity
btw, do you want this code in
pvclock.c, or shall we keep it kvmclock specific?I think its a pvclock-level fix. I'd been hoping to avoid having something like this, but I think its ultimately necessary.
Quoted 8 lines
Quoted 3 lines
Did you observe drift on Xen, or is this "ultimately" pointing at the future?
People are reporting weirdnesses that "clocksource=jiffies" apparently resolves. Xen and KVM are faced with the same hardware constraints, and it wouldn't surprise me if there were small measurable non-monotonicities in the PV clock under Xen. May as well be safe.
Does the drift only occur on SMP VMs?
Re: [PATCH 1/5] Add a global synchronization point for pvclock
In reply to Jeremy FitzhardingeQuoted 22 lines · Jeremy Fitzhardinge
Quoted 6 lines · Avi Kivity
Quoted 5 lines · Jeremy Fitzhardinge
Quoted 3 lines · Avi Kivity
btw, do you want this code in
pvclock.c, or shall we keep it kvmclock specific?Quoted 6 lines
Quoted 3 lines
I think its a pvclock-level fix. I'd been hoping to avoid having something like this, but I think its ultimately necessary.
Did you observe drift on Xen, or is this "ultimately" pointing at the future?
People are reporting weirdnesses that "clocksource=jiffies" apparently resolves. Xen and KVM are faced with the same hardware constraints, and it wouldn't surprise me if there were small measurable non-monotonicities in the PV clock under Xen. May as well be safe.
Of course, it kills any possibility of being able to usefully export this interface down to usermode.
Quoted 7 lines
My main concern about this kind of simple fix is that if there's a long term systematic drift between different CPU's tscs, then this will somewhat mask the problem while giving really awful time measurement on the "slow" CPU(s). In that case it really needs to adjust the scaling factor to correct for the drift (*not* update the offset). But if we're definitely only talking about fixed, relatively small time offsets then it is fine.
Can you by any chance run ingo's time warp test on those machines?
You need to define TOD to 1, and leave out the TSC test.
For me, warps exists on every machine out there, but the nehalems, so far
Re: [PATCH 1/5] Add a global synchronization point for pvclock
Quoted 36 lines · Glauber Costa
Quoted 26 lines · Jeremy Fitzhardinge
Quoted 9 lines · Avi Kivity
Quoted 7 lines · Jeremy Fitzhardinge
Quoted 4 lines · Avi Kivity
btw, do you want this code in
pvclock.c, or shall we keep it kvmclock specific?Quoted 7 lines
Quoted 4 lines
I think its a pvclock-level fix. I'd been hoping to avoid having something like this, but I think its ultimately necessary.
Did you observe drift on Xen, or is this "ultimately" pointing at the future?
People are reporting weirdnesses that "clocksource=jiffies" apparently resolves. Xen and KVM are faced with the same hardware constraints, and it wouldn't surprise me if there were small measurable non-monotonicities in the PV clock under Xen. May as well be safe.
Of course, it kills any possibility of being able to usefully export this interface down to usermode.
Quoted 8 lines
My main concern about this kind of simple fix is that if there's a long term systematic drift between different CPU's tscs, then this will somewhat mask the problem while giving really awful time measurement on the "slow" CPU(s). In that case it really needs to adjust the scaling factor to correct for the drift (*not* update the offset). But if we're definitely only talking about fixed, relatively small time offsets then it is fine.
Quoted 6 lines
Can you by any chance run ingo's time warp test on those machines?
You need to define TOD to 1, and leave out the TSC test.
For me, warps exists on every machine out there, but the nehalems, so far
Or apply this patch.
time-warp-test.c +1 -1
@@ -91,7 +91,7 @@ static inline unsigned long long __rdtsc {DECLARE_ARGS (val, low, high); - asm volatile("cpuid; rdtsc" : EAX_EDX_RET(val, low, high)); + asm volatile("cpuid; rdtsc" : EAX_EDX_RET(val, low, high) :: "ebx", "ecx"); returnEAX_EDX_VAL (val, low, high); }
Re: [PATCH 1/5] Add a global synchronization point for pvclock
Quoted 8 lines · Zachary Amsden
Or apply this patch.
time-warp.patchdiff -rup
a/time-warp-test.cb/time-warp-test.c
time-warp-test.c static inline unsigned long long __rdtsc · line 96
Quoted 7 lines
time-warp-test.c +1
-1@@ -91,7 +91,7 @@ static inline unsigned long long __rdtsc {DECLARE_ARGS (val, low, high); - asm volatile("cpuid; rdtsc" : EAX_EDX_RET(val, low, high)); + asm volatile("cpuid; rdtsc" : EAX_EDX_RET(val, low, high) :: "ebx", "ecx");
Plus, replace cpuid by lfence/mfence. cpuid will trap.
Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
Re: [PATCH 1/5] Add a global synchronization point for pvclock
Quoted 8 lines · Avi Kivity
Quoted 8 lines
diff -rup
a/time-warp-test.cb/time-warp-test.ctime-warp-test.c 2010-04-15 16:35:37.777982377 -1000 renamed from time-warp-test.c 2010-04-15 16:30:13.955981607 -1000 +0
-0@@ -91,7 +91,7 @@ static inline unsigned long long __rdtsc { DECLARE_ARGS(val, low, high);
Quoted 8 lines
time-warp-test.c 2010-04-15 16:35:37.777982377 -1000 static inline unsigned long long __rdtsc · line 97
Quoted 5 lines
time-warp-test.c 2010-04-15 16:35:37.777982377 -1000 renamed from time-warp-test.c 2010-04-15 16:30:13.955981607 -1000 +1
-1- asm volatile("cpuid; rdtsc" : EAX_EDX_RET(val, low, high)); + asm volatile("cpuid; rdtsc" : EAX_EDX_RET(val, low, high) :: "ebx", "ecx");Plus, replace cpuid by lfence/mfence. cpuid will trap.
I presume the clobbers are needed for cpuid; if you use [lm]fence then they shouldn't be needed, right?
J
Re: [PATCH 1/5] Add a global synchronization point for pvclock
Quoted 18 lines · Jeremy Fitzhardinge
Quoted 7 lines · Avi Kivity
Quoted 7 lines
diff -rup
a/time-warp-test.cb/time-warp-test.ctime-warp-test.c 2010-04-15 16:35:37.777982377 -1000 renamed from time-warp-test.c 2010-04-15 16:30:13.955981607 -1000 +0
-0@@ -91,7 +91,7 @@ static inline unsigned long long __rdtsc { DECLARE_ARGS(val, low, high);Quoted 9 lines
time-warp-test.c 2010-04-15 16:35:37.777982377 -1000 static inline unsigned long long __rdtsc · line 97
Quoted 6 lines
time-warp-test.c 2010-04-15 16:35:37.777982377 -1000 renamed from time-warp-test.c 2010-04-15 16:30:13.955981607 -1000 +1
-1- asm volatile("cpuid; rdtsc" : EAX_EDX_RET(val, low, high)); + asm volatile("cpuid; rdtsc" : EAX_EDX_RET(val, low, high) :: "ebx", "ecx");Plus, replace cpuid by lfence/mfence. cpuid will trap.
Quoted 4 lines
I presume the clobbers are needed for cpuid; if you use [lm]fence then they shouldn't be needed, right?
Right. But I'm not sure lfence/mfence are sufficient - from what I understand rdtsc can pass right through them.
Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
Re: [PATCH 1/5] Add a global synchronization point for pvclock
In reply to Avi KivityQuoted 12 lines · Avi Kivity
Quoted 11 lines · Zachary Amsden
Or apply this patch.
time-warp.patchdiff -rup
a/time-warp-test.cb/time-warp-test.ctime-warp-test.c 2010-04-15 16:35:37.777982377 -1000 renamed from time-warp-test.c 2010-04-15 16:30:13.955981607 -1000 +0
-0@@ -91,7 +91,7 @@ static inline unsigned long long __rdtsc { DECLARE_ARGS(val, low, high);
Quoted 7 lines
time-warp-test.c 2010-04-15 16:35:37.777982377 -1000 static inline unsigned long long __rdtsc · line 96
Quoted 4 lines
time-warp-test.c 2010-04-15 16:35:37.777982377 -1000 renamed from time-warp-test.c 2010-04-15 16:30:13.955981607 -1000 +1
-1- asm volatile("cpuid; rdtsc" : EAX_EDX_RET(val, low, high)); + asm volatile("cpuid; rdtsc" : EAX_EDX_RET(val, low, high) :: "ebx", "ecx");Plus, replace cpuid by lfence/mfence. cpuid will trap.
Does lfence / mfence actually serialize? I thought there was some great confusion about that not being the case on all AMD processors, and possibly not at all on Intel.
A trap, however is a great way to serialize.
I think, there is no serializing instruction which can be used from userspace which does not trap, at least, I don't know one off the top of my head.
Zach
Re: [PATCH 1/5] Add a global synchronization point for pvclock
Quoted 9 lines · Zachary Amsden
Does lfence / mfence actually serialize? I thought there was some great confusion about that not being the case on all AMD processors, and possibly not at all on Intel.
A trap, however is a great way to serialize.
I think, there is no serializing instruction which can be used from userspace which does not trap, at least, I don't know one off the top of my head.
rsm is not technically privileged... but not quite usable from usermode ;)
J
Re: [PATCH 1/5] Add a global synchronization point for pvclock
Quoted 6 lines · Jeremy Fitzhardinge
Quoted 4 lines · Zachary Amsden
Does lfence / mfence actually serialize? I thought there was some great confusion about that not being the case on all AMD processors, and possibly not at all on Intel.
Quoted 8 lines
Quoted 6 lines
A trap, however is a great way to serialize.
I think, there is no serializing instruction which can be used from userspace which does not trap, at least, I don't know one off the top of my head.
rsm is not technically privileged... but not quite usable from usermode ;)
rsm under hardware virtualization makes my head hurt
Re: [PATCH 1/5] Add a global synchronization point for pvclock
Quoted 4 lines · Zachary Amsden
Quoted 1 line
rsm is not technically privileged... but not quite usable from usermode ;)
rsm under hardware virtualization makes my head hurt
Either one independently is sufficient for me.
Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
Re: [PATCH 1/5] Add a global synchronization point for pvclock
In reply to Zachary AmsdenQuoted 21 lines · Zachary Amsden
Quoted 12 lines · Avi Kivity
Quoted 11 lines · Zachary Amsden
Or apply this patch.
time-warp.patchdiff -rup
a/time-warp-test.cb/time-warp-test.ctime-warp-test.c 2010-04-15 16:35:37.777982377 -1000 renamed from time-warp-test.c 2010-04-15 16:30:13.955981607 -1000 +0
-0@@ -91,7 +91,7 @@ static inline unsigned long long __rdtsc { DECLARE_ARGS(val, low, high);Quoted 7 lines
time-warp-test.c 2010-04-15 16:35:37.777982377 -1000 static inline unsigned long long __rdtsc · line 96
Quoted 4 lines
time-warp-test.c 2010-04-15 16:35:37.777982377 -1000 renamed from time-warp-test.c 2010-04-15 16:30:13.955981607 -1000 +1
-1- asm volatile("cpuid; rdtsc" : EAX_EDX_RET(val, low, high)); + asm volatile("cpuid; rdtsc" : EAX_EDX_RET(val, low, high) :: "ebx", "ecx");Plus, replace cpuid by lfence/mfence. cpuid will trap.
Quoted 1 line
Does lfence / mfence actually serialize? I thought there was some great confusion about that not being the case on all AMD processors, and possibly not at all on Intel.
They don't.
Quoted 4 lines
A trap, however is a great way to serialize.
I think, there is no serializing instruction which can be used from userspace which does not trap, at least, I don't know one off the top of my head.
iret.
Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
Re: [PATCH 1/5] Add a global synchronization point for pvclock
In reply to Avi KivityQuoted 11 lines · Avi Kivity
Quoted 7 lines · Glauber Costa
Quoted 4 lines
Oh yes, just trying to avoid a patch with both
atomic64_read()andACCESS_ONCE().you're mixing the private version of the patch you saw with this one. there isn't any atomic reads in here. I'll use a barrier then
This patch writes
last_valueatomically, but reads it non-atomically. A barrier is insufficient.
Well, on a 32b system, you can explicitly order the updates of low and high, then do a high-low-checkhigh read. That would be much more efficient than atomic64. If we really care about 32b.
J
Re: [PATCH 1/5] Add a global synchronization point for pvclock
In reply to Jeremy FitzhardingeQuoted 7 lines · Jeremy Fitzhardinge
Quoted 4 lines · Glauber Costa
In recent stress tests, it was found that pvclock-based systems could seriously warp in smp systems. Using ingo's
time-warp-test.c, I could trigger a scenario as bad as 1.5mi warps a minute in some systems.Is that "1.5 million"?
Yes it is.
But as I said, this seem to be a very deep worst case scenario. Most of boxes are not even close to being that bad.
Quoted 9 lines
Quoted 5 lines
(to be fair, it wasn't that bad in most of them). Investigating further, I found out that such warps were caused by the very offset-based calculation pvclock is based on.
Is the problem that the tscs are starting out of sync, or that they're drifting relative to each other over time? Do the problems become worse the longer the uptime? How large are the offsets we're talking about here?
The offsets usually seem pretty small, under a microsecond. So I don't think it has anything to do with tscs starting out of sync. Specially because the delta-based calculation has the exact purpose of handling that case.
Re: [PATCH 1/5] Add a global synchronization point for pvclock
Quoted 8 lines · Glauber Costa
Quoted 4 lines
Is the problem that the tscs are starting out of sync, or that they're drifting relative to each other over time? Do the problems become worse the longer the uptime? How large are the offsets we're talking about here?
The offsets usually seem pretty small, under a microsecond. So I don't think it has anything to do with tscs starting out of sync. Specially because the delta-based calculation has the exact purpose of handling that case.
So you think they're drifting out of sync from an initially synced state? If so, what would bound the drift?
J
Re: [PATCH 1/5] Add a global synchronization point for pvclock
Quoted 5 lines · Jeremy Fitzhardinge
Quoted 4 lines · Glauber Costa
Quoted 4 lines
Is the problem that the tscs are starting out of sync, or that they're drifting relative to each other over time? Do the problems become worse the longer the uptime? How large are the offsets we're talking about here?
Quoted 7 lines
Quoted 4 lines
The offsets usually seem pretty small, under a microsecond. So I don't think it has anything to do with tscs starting out of sync. Specially because the delta-based calculation has the exact purpose of handling that case.
So you think they're drifting out of sync from an initially synced state? If so, what would bound the drift?
I think delta calculation introduces errors.
Marcelo can probably confirm it, but he has a nehalem with an appearently very good tsc source. Even this machine warps.
It stops warping if we only write pvclock data structure once and forget it, (which only updated tsc_timestamp once), according to him.
Obviously, we can't do that everywhere.
Re: [PATCH 1/5] Add a global synchronization point for pvclock
Quoted 6 lines · Glauber Costa
Quoted 5 lines · Jeremy Fitzhardinge
Quoted 4 lines · Glauber Costa
Quoted 4 lines
Is the problem that the tscs are starting out of sync, or that they're drifting relative to each other over time? Do the problems become worse the longer the uptime? How large are the offsets we're talking about here?
Quoted 8 lines
Quoted 7 lines
Quoted 4 lines
The offsets usually seem pretty small, under a microsecond. So I don't think it has anything to do with tscs starting out of sync. Specially because the delta-based calculation has the exact purpose of handling that case.
So you think they're drifting out of sync from an initially synced state? If so, what would bound the drift?
I think delta calculation introduces errors.
Yes.
Quoted 5 lines
Marcelo can probably confirm it, but he has a nehalem with an appearently very good tsc source. Even this machine warps.
It stops warping if we only write pvclock data structure once and forget it, (which only updated
tsc_timestamponce), according to him.
Yes. So its not as if the guest visible TSCs go out of sync (they don't on this machine Glauber mentioned, or even on a multi-core Core 2 Duo), but the delta calculation is very hard (if not impossible) to get right.
The timewarps i've seen were in the 0-200ns range, and very rare (once every 10 minutes or so).
Quoted 1 line
Obviously, we can't do that everywhere.
Re: [PATCH 1/5] Add a global synchronization point for pvclock
Quoted 7 lines · Marcelo Tosatti
Quoted 7 lines
Marcelo can probably confirm it, but he has a nehalem with an appearently very good tsc source. Even this machine warps.
It stops warping if we only write pvclock data structure once and forget it, (which only updated
tsc_timestamponce), according to him.
Quoted 7 lines
Yes. So its not as if the guest visible TSCs go out of sync (they don't on this machine Glauber mentioned, or even on a multi-core Core 2 Duo), but the delta calculation is very hard (if not impossible) to get right.
The timewarps i've seen were in the 0-200ns range, and very rare (once every 10 minutes or so).
Might be due to NMIs or SMIs interrupting the rdtsc(); ktime_get() operation which establishes the timeline. We could limit it by having a loop doing rdtsc(); ktime_get(); rdtsc(); and checking for some bound, but it isn't worthwhile (and will break nested virtualization for sure). Better to have the option to calibrate kvmclock just once on machines with X86_FEATURE_NONSTOP_TRULY_RELIABLE _TSC_HONESTLY.
Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
Re: [PATCH 1/5] Add a global synchronization point for pvclock
Quoted 14 lines · Avi Kivity
Quoted 4 lines · Marcelo Tosatti
Quoted 4 lines
Marcelo can probably confirm it, but he has a nehalem with an appearently very good tsc source. Even this machine warps.
Quoted 8 lines
Quoted 2 lines
It stops warping if we only write pvclock data structure once and forget it, (which only updated
tsc_timestamponce), according to him.Yes. So its not as if the guest visible TSCs go out of sync (they don't on this machine Glauber mentioned, or even on a multi-core Core 2 Duo), but the delta calculation is very hard (if not impossible) to get right.
The timewarps i've seen were in the 0-200ns range, and very rare (once every 10 minutes or so).
Quoted 7 lines
Might be due to NMIs or SMIs interrupting the
rdtsc();ktime_get()operation which establishes the timeline. We could limit it by having a loop doingrdtsc();ktime_get();rdtsc(); and checking for some bound, but it isn't worthwhile (and will break nested virtualization for sure). Better to have the option to calibrate kvmclock just once on machines withX86_FEATURE_NONSTOP_TRULY_RELIABLE_TSC_HONESTLY.
For the record, we can only even do that in those machines. If we try to update time structures only once in machines with the X86_FEATURE_TSC_SAYS_IT_IS_OKAY_BUT_IN_REALITY_IS_NOT_OKAY feature flag, guests won't even boot.
We can detect that, and besides doing calculation only once, also export some bit indicating that to the guest. Humm... I'm thinking now, that because of migration, we should check this bit every time, because we might have changed host. So instead of using an expensive cpuid check, we should probably use some bit in the vcpu_time_info structure, and use a cpuid bit just to say it is enabled.
Jeremy,
are you okay in turning one of the pad fields in the structure into a flags field?
Re: [PATCH 1/5] Add a global synchronization point for pvclock
Quoted 14 lines · Glauber Costa
Quoted 9 lines
Might be due to NMIs or SMIs interrupting the
rdtsc();ktime_get()operation which establishes the timeline. We could limit it by having a loop doingrdtsc();ktime_get();rdtsc(); and checking for some bound, but it isn't worthwhile (and will break nested virtualization for sure). Better to have the option to calibrate kvmclock just once on machines withX86_FEATURE_NONSTOP_TRULY_RELIABLE_TSC_HONESTLY.For the record, we can only even do that in those machines. If we try to update time structures only once in machines with the
X86_FEATURE_TSC_SAYS_IT_IS_OKAY_BUT_IN_REALITY_IS_NOT_OKAYfeature flag, guests won't even boot.
Quoted 6 lines
We can detect that, and besides doing calculation only once, also export some bit indicating that to the guest. Humm... I'm thinking now, that because of migration, we should check this bit every time, because we might have changed host. So instead of using an expensive cpuid check, we should probably use some bit in the
vcpu_time_infostructure, and use a cpuid bit just to say it is enabled.
Right, we need a bit in the structure itself that says you can trust the time not to go backwards, and a bit in cpuid that says you can trust the bit in the structure (unless we can be sure no implementations leak garbage into the padding field).
Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
Re: [PATCH 1/5] Add a global synchronization point for pvclock
In reply to Avi KivityQuoted 7 lines · Avi Kivity
Quoted 6 lines · Marcelo Tosatti
Quoted 6 lines
Marcelo can probably confirm it, but he has a nehalem with an appearently very good tsc source. Even this machine warps.
It stops warping if we only write pvclock data structure once and forget it, (which only updated
tsc_timestamponce), according to him.
Quoted 8 lines
Quoted 6 lines
Yes. So its not as if the guest visible TSCs go out of sync (they don't on this machine Glauber mentioned, or even on a multi-core Core 2 Duo), but the delta calculation is very hard (if not impossible) to get right.
The timewarps i've seen were in the 0-200ns range, and very rare (once every 10 minutes or so).
Might be due to NMIs or SMIs interrupting the
rdtsc();ktime_get()operation which establishes the timeline. We could limit it by having a loop doingrdtsc();ktime_get();rdtsc(); and checking for some bound, but it isn't worthwhile (and will break nested virtualization for sure). Better to have the option to calibrate kvmclock just once on machines withX86_FEATURE_NONSTOP_TRULY_RELIABLE_TSC_HONESTLY.
There's a perfect way to do this and it still fails to stop timewarps. You can set the performance counters to overflow if more instructions are issued than your code path, run an assembly instruction stream and if the performance interrupt hits, restart the calibration.
The calibration happens not just once, but on every migration, and currently, I believe, on every VCPU switch. Even if we reduce the number of calibrations to the bare minimum and rule out SMIs and NMIs, there will still be variation due to factors beyond our control because of the unpredictable nature of cache and instruction issue.
However, X86_FEATURE_NONSTOP_TRULY_RELIABLE_TSC_HONESTLY does imply one key feature which the code is missing today: on SMP VMs, the calibration of kvmclock needs to be done only once, and the clock can then be used for all VCPUs. That, I think, stops Glauber's bug from appearing on the server side.
I will spin that into my web of patches and send the cocoon out sometime this evening.
Zach
Re: [PATCH 1/5] Add a global synchronization point for pvclock
Quoted 10 lines · Zachary Amsden
Quoted 7 lines
Quoted 5 lines
on this machine Glauber mentioned, or even on a multi-core Core 2 Duo), but the delta calculation is very hard (if not impossible) to get right.
The timewarps i've seen were in the 0-200ns range, and very rare (once every 10 minutes or so).
Might be due to NMIs or SMIs interrupting the
rdtsc();ktime_get()operation which establishes the timeline. We could limit it by having a loop doingrdtsc();ktime_get();rdtsc(); and checking for some bound, but it isn't worthwhile (and will break nested virtualization for sure). Better to have the option to calibrate kvmclock just once on machines withX86_FEATURE_NONSTOP_TRULY_RELIABLE_TSC_HONESTLY.Yes. So its not as if the guest visible TSCs go out of sync (they don't
There's a perfect way to do this and it still fails to stop timewarps. You can set the performance counters to overflow if more instructions are issued than your code path, run an assembly instruction stream and if the performance interrupt hits, restart the calibration.
It's completely impractical. The PMU is a global resource that is already shared among users and the host; programming and restoring it is expensive; and in a virtualized environment it the whole scheme may fail.
Quoted 2 lines
The calibration happens not just once, but on every migration, and currently, I believe, on every VCPU switch. Even if we reduce the number of calibrations to the bare minimum and rule out SMIs and NMIs, there will still be variation due to factors beyond our control because of the unpredictable nature of cache and instruction issue.
Right.
Quoted 2 lines
However,
X86_FEATURE_NONSTOP_TRULY_RELIABLE_TSC_HONESTLYdoes imply one key feature which the code is missing today: on SMP VMs, the calibration of kvmclock needs to be done only once, and the clock can then be used for all VCPUs. That, I think, stops Glauber's bug from appearing on the server side.
That's the plan.
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH 1/5] Add a global synchronization point for pvclock
In reply to Glauber CostaQuoted 29 lines · Glauber Costa
In recent stress tests, it was found that pvclock-based systems could seriously warp in smp systems. Using ingo's
time-warp-test.c, I could trigger a scenario as bad as 1.5mi warps a minute in some systems. (to be fair, it wasn't that bad in most of them). Investigating further, I found out that such warps were caused by the very offset-based calculation pvclock is based on.This happens even on some machines that report
constant_tscin its tsc flags, specially on multi-socket ones.Two reads of the same kernel timestamp at approx the same time, will likely have tsc timestamped in different occasions too. This means the delta we calculate is unpredictable at best, and can probably be smaller in a cpu that is legitimately reading clock in a forward ocasion.
Some adjustments on the host could make this window less likely to happen, but still, it pretty much poses as an intrinsic problem of the mechanism.
A while ago, I though about using a shared variable anyway, to hold clock last state, but gave up due to the high contention locking was likely to introduce, possibly rendering the thing useless on big machines. I argue, however, that locking is not necessary.
We do a read-and-return sequence in pvclock, and between read and return, the global value can have changed. However, it can only have changed by means of an addition of a positive value. So if we detected that our clock timestamp is less than the current global, we know that we need to return a higher one, even though it is not exactly the one we compared to.
Quoted 9 lines
OTOH, if we detect we're greater than the current time source, we atomically replace the value with our new readings. This do causes contention on big boxes (but big here means *BIG*), but it seems like a good trade off, since it provide us with a time source guaranteed to be stable wrt time warps.
After this patch is applied, I don't see a single warp in time during 5 days of execution, in any of the machines I saw them before.
Please define a cpuid bit that makes this optional. When we eventually enable it in the future, it will allow a wider range of guests to enjoy it.
Quoted 3 lines
+static u64
last_value= 0;
Needs to be atomic64_t.
arch/x86/kernel/pvclock.c unsigned long pvclock_tsc_khz(struct pvclock_vcpu_time_info *src)
Quoted 12 lines
arch/x86/kernel/pvclock.c +4
-0+cycle_t pvclock_clocksource_read (struct pvclock_vcpu_time_info *src) {struct pvclock_shadow_time shadow;unsigned version;cycle_t ret, offset; +u64 last; + do { + last = last_value;
Otherwise, this assignment can see a partial update.
Quoted 8 lines
arch/x86/kernel/pvclock.c +4
-0+if (ret< last) +return last; + }while (unlikely (cmpxchg64 (&last_value, last, ret) != ret)); +return ret; }
Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
Re: [PATCH 1/5] Add a global synchronization point for pvclock
Quoted 15 lines · Avi Kivity
Quoted 3 lines
+static u64
last_value= 0;Needs to be
atomic64_t.Quoted 9 lines
arch/x86/kernel/pvclock.c +2
-0+cycle_t pvclock_clocksource_read (struct pvclock_vcpu_time_info *src) {struct pvclock_shadow_time shadow;unsigned version;cycle_t ret, offset; +u64 last;
Quoted 4 lines
arch/x86/kernel/pvclock.c cycle_t pvclock_clocksource_read(struct pvclock_vcpu_time_info *src) · line 144
Quoted 2 lines
arch/x86/kernel/pvclock.c +2
-0+ do { + last = last_value;Otherwise, this assignment can see a partial update.
On a 32-bit guest, that is.
Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
Re: [PATCH 1/5] Add a global synchronization point for pvclock
Quoted 16 lines · Avi Kivity
Quoted 15 lines · Avi Kivity
Quoted 3 lines
+static u64
last_value= 0;Needs to be
atomic64_t.Quoted 9 lines
arch/x86/kernel/pvclock.c +2
-0+cycle_t pvclock_clocksource_read (struct pvclock_vcpu_time_info *src) {struct pvclock_shadow_time shadow;unsigned version;cycle_t ret, offset; +u64 last;
Quoted 6 lines
Quoted 4 lines
arch/x86/kernel/pvclock.c cycle_t pvclock_clocksource_read(struct pvclock_vcpu_time_info *src) · line 144
Quoted 2 lines
arch/x86/kernel/pvclock.c +2
-0+ do { + last = last_value;Otherwise, this assignment can see a partial update.
On a 32-bit guest, that is.
Right, do bear in mind that the x86 implementation of atomic64_read() is terrifyingly expensive, it is better to not do that read and simply use the result of the cmpxchg.
Re: [PATCH 1/5] Add a global synchronization point for pvclock
Quoted 14 lines · Peter Zijlstra
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
arch/x86/kernel/pvclock.c +4
-0+cycle_t pvclock_clocksource_read (struct pvclock_vcpu_time_info *src) {struct pvclock_shadow_time shadow;unsigned version;cycle_t ret, offset; +u64 last; + do { + last = last_value;
Quoted 9 lines
Quoted 4 lines
Quoted 2 lines
Otherwise, this assignment can see a partial update.
On a 32-bit guest, that is.
Right, do bear in mind that the x86 implementation of
atomic64_read()is terrifyingly expensive, it is better to not do that read and simply use the result of the cmpxchg.
atomic64_read() _is_ cmpxchg64b. Are you thinking of some clever implementation for smp i386?
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH 1/5] Add a global synchronization point for pvclock
Quoted 19 lines · Avi Kivity
Quoted 18 lines · Peter Zijlstra
Quoted 18 lines
Quoted 11 lines
Quoted 11 lines
arch/x86/kernel/pvclock.c +2
-0+cycle_t pvclock_clocksource_read (struct pvclock_vcpu_time_info *src) {struct pvclock_shadow_time shadow;unsigned version;cycle_t ret, offset; +u64 last;Quoted 5 lines
arch/x86/kernel/pvclock.c cycle_t pvclock_clocksource_read(struct pvclock_vcpu_time_info *src) · line 144
Quoted 3 lines
arch/x86/kernel/pvclock.c +2
-0+ do { + last = last_value;Otherwise, this assignment can see a partial update.
On a 32-bit guest, that is.
Quoted 8 lines
Quoted 5 lines
Right, do bear in mind that the x86 implementation of
atomic64_read()is terrifyingly expensive, it is better to not do that read and simply use the result of the cmpxchg.
atomic64_read()_is_ cmpxchg64b. Are you thinking of some clever implementation for smp i386?
No, what I was suggesting was to rewrite that loop no to need the initial read but use the cmpxchg result of the previous iteration.
Something like:
u64 last = 0;
/* more stuff */
do {
if (ret < last)
return last;
last = cmpxchg64(&last_value, last, ret);
} while (last != ret);That only has a single cmpxchg8 in there per loop instead of two (avoiding the atomic64_read() one).
Re: [PATCH 1/5] Add a global synchronization point for pvclock
Quoted 27 lines · Peter Zijlstra
Quoted 11 lines
Quoted 8 lines
Right, do bear in mind that the x86 implementation of
atomic64_read()is terrifyingly expensive, it is better to not do that read and simply use the result of the cmpxchg.
atomic64_read()_is_ cmpxchg64b. Are you thinking of some clever implementation for smp i386?No, what I was suggesting was to rewrite that loop no to need the initial read but use the cmpxchg result of the previous iteration.
Something like:
u64 last = 0;/* more stuff */ do { if (ret< last) return last; last = cmpxchg64(&last_value, last, ret); } while (last != ret);
Quoted 3 lines
That only has a single cmpxchg8 in there per loop instead of two (avoiding the
atomic64_read()one).
Still have two cmpxchgs in the common case. The first iteration will fail, fetching last_value, the second will work.
It will be better when we have contention, though, so it's worthwhile.
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH 1/5] Add a global synchronization point for pvclock
Quoted 28 lines · Avi Kivity
Quoted 27 lines · Peter Zijlstra
Quoted 11 lines
Quoted 8 lines
Right, do bear in mind that the x86 implementation of
atomic64_read()is terrifyingly expensive, it is better to not do that read and simply use the result of the cmpxchg.
atomic64_read()_is_ cmpxchg64b. Are you thinking of some clever implementation for smp i386?No, what I was suggesting was to rewrite that loop no to need the initial read but use the cmpxchg result of the previous iteration.
Something like:
u64 last = 0;/* more stuff */ do { if (ret< last) return last; last = cmpxchg64(&last_value, last, ret); } while (last != ret);
Quoted 8 lines
Quoted 3 lines
That only has a single cmpxchg8 in there per loop instead of two (avoiding the
atomic64_read()one).Still have two cmpxchgs in the common case. The first iteration will fail, fetching
last_value, the second will work.It will be better when we have contention, though, so it's worthwhile.
Right, another option is to put the initial read outside of the loop, that way you'll have the best of all cases, a single LOCK'ed op in the loop, and only a single LOCK'ed op for the fast path on sensible architectures ;-)
last = atomic64_read(&last_value);
do {
if (ret < last)
return last;
last = atomic64_cmpxchg(&last_value, last, ret);
} while (unlikely(last != ret));Or so.
Re: [PATCH 1/5] Add a global synchronization point for pvclock
Quoted 11 lines · Peter Zijlstra
Quoted 6 lines
Still have two cmpxchgs in the common case. The first iteration will fail, fetching
last_value, the second will work.It will be better when we have contention, though, so it's worthwhile.
Right, another option is to put the initial read outside of the loop, that way you'll have the best of all cases, a single LOCK'ed op in the loop, and only a single LOCK'ed op for the fast path on sensible architectures ;-)
Quoted 10 lines
last = atomic64_read(&last_value); do { if (ret< last) return last; last = atomic64_cmpxchg(&last_value, last, ret); } while (unlikely(last != ret));Or so.
Yes, looks optimal when !NONSTOP_TSC.
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH 1/5] Add a global synchronization point for pvclock
In reply to Peter ZijlstraQuoted 36 lines · Peter Zijlstra
Quoted 35 lines · Avi Kivity
Quoted 27 lines · Peter Zijlstra
Quoted 11 lines
Quoted 8 lines
Right, do bear in mind that the x86 implementation of
atomic64_read()is terrifyingly expensive, it is better to not do that read and simply use the result of the cmpxchg.
atomic64_read()_is_ cmpxchg64b. Are you thinking of some clever implementation for smp i386?No, what I was suggesting was to rewrite that loop no to need the initial read but use the cmpxchg result of the previous iteration.
Something like:
u64 last = 0;/* more stuff */ do { if (ret< last) return last; last = cmpxchg64(&last_value, last, ret); } while (last != ret);Quoted 3 lines
That only has a single cmpxchg8 in there per loop instead of two (avoiding the
atomic64_read()one).Still have two cmpxchgs in the common case. The first iteration will fail, fetching
last_value, the second will work.
Quoted 8 lines
Quoted 1 line
It will be better when we have contention, though, so it's worthwhile.
Right, another option is to put the initial read outside of the loop, that way you'll have the best of all cases, a single LOCK'ed op in the loop, and only a single LOCK'ed op for the fast path on sensible architectures ;-)
last = atomic64_read(&last_value);
isn't a barrier enough here?
Re: [PATCH 1/5] Add a global synchronization point for pvclock
Quoted 11 lines · Glauber Costa
Quoted 8 lines
Right, another option is to put the initial read outside of the loop, that way you'll have the best of all cases, a single LOCK'ed op in the loop, and only a single LOCK'ed op for the fast path on sensible architectures ;-)
last = atomic64_read(&last_value);isn't a barrier enough here?
No. On i386, the statement
last = last_value;
will be split by the compiler into two 32-bit loads. If a write (atomic, using cmpxchg) on another cpu happens between those two loads, then the variable last will have a corrupted value.
Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
Re: [PATCH 1/5] Add a global synchronization point for pvclock
In reply to Avi KivityQuoted 7 lines · Avi Kivity
Quoted 4 lines
After this patch is applied, I don't see a single warp in time during 5 days of execution, in any of the machines I saw them before.
Please define a cpuid bit that makes this optional. When we eventually enable it in the future, it will allow a wider range of guests to enjoy it.
Right, so on x86 we have:
X86_FEATURE_CONSTANT_TSC, which only states that TSC is frequency independent, not that it doesn't stop in C states and similar fun stuff.
X86_FEATURE_TSC_RELIABLE, which IIRC should indicate the TSC is constant and synced between cores.
Re: [PATCH 1/5] Add a global synchronization point for pvclock
Quoted 10 lines · Peter Zijlstra
Quoted 8 lines · Avi Kivity
Quoted 5 lines
After this patch is applied, I don't see a single warp in time during 5 days of execution, in any of the machines I saw them before.
Please define a cpuid bit that makes this optional. When we eventually enable it in the future, it will allow a wider range of guests to enjoy it.
Quoted 9 lines
Right, so on x86 we have:
X86_FEATURE_CONSTANT_TSC, which only states that TSC is frequency independent, not that it doesn't stop in C states and similar fun stuff.
X86_FEATURE_TSC_RELIABLE, which IIRC should indicate the TSC is constant and synced between cores.
Sockets and boards too? (IOW, how reliable is TSC_RELIABLE)?
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH 1/5] Add a global synchronization point for pvclock
Quoted 16 lines · Avi Kivity
Quoted 15 lines · Peter Zijlstra
Quoted 8 lines · Avi Kivity
Quoted 5 lines
After this patch is applied, I don't see a single warp in time during 5 days of execution, in any of the machines I saw them before.
Please define a cpuid bit that makes this optional. When we eventually enable it in the future, it will allow a wider range of guests to enjoy it.
Right, so on x86 we have:
X86_FEATURE_CONSTANT_TSC, which only states that TSC is frequency independent, not that it doesn't stop in C states and similar fun stuff.
Quoted 6 lines
Quoted 4 lines
X86_FEATURE_TSC_RELIABLE, which IIRC should indicate the TSC is constant and synced between cores.Sockets and boards too? (IOW, how reliable is
TSC_RELIABLE)?
Not sure, IIRC we clear that when the TSC sync test fails, eg when we mark the tsc clocksource unusable.
Re: [PATCH 1/5] Add a global synchronization point for pvclock
Quoted 12 lines · Peter Zijlstra
Quoted 12 lines
Quoted 12 lines
Right, so on x86 we have:
X86_FEATURE_CONSTANT_TSC, which only states that TSC is frequency independent, not that it doesn't stop in C states and similar fun stuff.
X86_FEATURE_TSC_RELIABLE, which IIRC should indicate the TSC is constant and synced between cores.
Quoted 5 lines
Quoted 2 lines
Sockets and boards too? (IOW, how reliable is
TSC_RELIABLE)?Not sure, IIRC we clear that when the TSC sync test fails, eg when we mark the tsc clocksource unusable.
Worrying. By the time we detect this the guest may already have gotten confused by clocks going backwards.
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH 1/5] Add a global synchronization point for pvclock
Quoted 11 lines · Avi Kivity
Quoted 10 lines · Peter Zijlstra
Quoted 10 lines
Quoted 10 lines
Right, so on x86 we have:
X86_FEATURE_CONSTANT_TSC, which only states that TSC is frequency independent, not that it doesn't stop in C states and similar fun stuff.
X86_FEATURE_TSC_RELIABLE, which IIRC should indicate the TSC is constant and synced between cores.
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
Sockets and boards too? (IOW, how reliable is
TSC_RELIABLE)?Not sure, IIRC we clear that when the TSC sync test fails, eg when we mark the tsc clocksource unusable.
Worrying. By the time we detect this the guest may already have gotten confused by clocks going backwards.
Upstream, we are marking the TSC unstable preemptively when hardware which will eventually sync test is detected, so this should be fine.
Zach
Re: [PATCH 1/5] Add a global synchronization point for pvclock
Quoted 8 lines · Zachary Amsden
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
Sockets and boards too? (IOW, how reliable is
TSC_RELIABLE)?Not sure, IIRC we clear that when the TSC sync test fails, eg when we mark the tsc clocksource unusable.
Worrying. By the time we detect this the guest may already have gotten confused by clocks going backwards.
Upstream, we are marking the TSC unstable preemptively when hardware which will eventually sync test is detected, so this should be fine.
ENOPARSE?
Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
Re: [PATCH 1/5] Add a global synchronization point for pvclock
Quoted 5 lines · Avi Kivity
Quoted 4 lines · Zachary Amsden
Quoted 4 lines
Quoted 3 lines
Quoted 1 line
Sockets and boards too? (IOW, how reliable is
TSC_RELIABLE)?Not sure, IIRC we clear that when the TSC sync test fails, eg when we mark the tsc clocksource unusable.
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
Worrying. By the time we detect this the guest may already have gotten confused by clocks going backwards.
Upstream, we are marking the TSC unstable preemptively when hardware which will eventually sync test is detected, so this should be fine.
ENOPARSE?
Instead of detecting TSC warp, c1e_idle, power_saving_mwait_init, tsc_check_state, dmi_mark_tsc_unstable all do something similar to this to disable TSC before warp even occurs:
static void c1e_idle(void)
{
if (need_resched())
return;
if (!c1e_detected) {
u32 lo, hi;
rdmsr(MSR_K8_INT_PENDING_MSG, lo, hi);
if (lo & K8_INTP_C1E_ACTIVE_MASK) {
c1e_detected = 1;
if (!boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
mark_tsc_unstable("TSC halt in AMD C1E");
printk(KERN_INFO "System has AMD C1E enabled\n");
set_cpu_cap(&boot_cpu_data, X86_FEATURE_AMDC1E);
}
}Re: [PATCH 1/5] Add a global synchronization point for pvclock
Quoted 17 lines · Zachary Amsden
Quoted 5 lines · Avi Kivity
Quoted 4 lines · Zachary Amsden
Quoted 4 lines
Quoted 3 lines
Quoted 1 line
Sockets and boards too? (IOW, how reliable is
TSC_RELIABLE)?Not sure, IIRC we clear that when the TSC sync test fails, eg when we mark the tsc clocksource unusable.
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
Worrying. By the time we detect this the guest may already have gotten confused by clocks going backwards.
Upstream, we are marking the TSC unstable preemptively when hardware which will eventually sync test is detected, so this should be fine.
ENOPARSE?
Instead of detecting TSC warp,
c1e_idle,power_saving_mwait_init,tsc_check_state,dmi_mark_tsc_unstableall do something similar to this to disable TSC before warp even occurs:static void
c1e_idle(void)
Quoted 18 lines
{
if (need_resched())
return;
if (!c1e_detected) {
u32 lo, hi;
rdmsr(MSR_K8_INT_PENDING_MSG, lo, hi);
if (lo & K8_INTP_C1E_ACTIVE_MASK) {
c1e_detected = 1;
if (!boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
mark_tsc_unstable("TSC halt in AMD C1E");
printk(KERN_INFO "System has AMD C1E enabled\n");
set_cpu_cap(&boot_cpu_data, X86_FEATURE_AMDC1E);
}
}That works within a socket; but multiple socket machines need not feed all sockets from the same crystal and reset line (esp. likely for multiple board machines, but might even happen with single board FSB-less processors).
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH 1/5] Add a global synchronization point for pvclock
In reply to Peter ZijlstraQuoted 9 lines · Peter Zijlstra
Quoted 7 lines · Avi Kivity
Quoted 4 lines
After this patch is applied, I don't see a single warp in time during 5 days of execution, in any of the machines I saw them before.
Please define a cpuid bit that makes this optional. When we eventually enable it in the future, it will allow a wider range of guests to enjoy it.
Quoted 7 lines
Right, so on x86 we have:
X86_FEATURE_CONSTANT_TSC, which only states that TSC is frequency independent, not that it doesn't stop in C states and similar fun stuff.
X86_FEATURE_TSC_RELIABLE, which IIRC should indicate the TSC is constant and synced between cores.
Fun, we also have:
X86_FEATURE_NONSTOP_TSC, which states the thing doesn't stop in C states.
Re: [PATCH 1/5] Add a global synchronization point for pvclock
Quoted 6 lines · Peter Zijlstra
Quoted 6 lines
Right, so on x86 we have:
X86_FEATURE_CONSTANT_TSC, which only states that TSC is frequency independent, not that it doesn't stop in C states and similar fun stuff.
Quoted 8 lines
Quoted 3 lines
X86_FEATURE_TSC_RELIABLE, which IIRC should indicate the TSC is constant and synced between cores.Fun, we also have:
X86_FEATURE_NONSTOP_TSC, which states the thing doesn't stop in C states.
All of them? I though tsc stops in some mwait deep REM sleep thing.
So what do we need? test for both TSC_RELIABLE and NONSTOP_TSC? IMO TSC_RELIABLE should imply NONSTOP_TSC.
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH 1/5] Add a global synchronization point for pvclock
Quoted 10 lines · Avi Kivity
Quoted 9 lines · Peter Zijlstra
Quoted 9 lines
Right, so on x86 we have:
X86_FEATURE_CONSTANT_TSC, which only states that TSC is frequency independent, not that it doesn't stop in C states and similar fun stuff.
X86_FEATURE_TSC_RELIABLE, which IIRC should indicate the TSC is constant and synced between cores.
Quoted 7 lines
Quoted 5 lines
Fun, we also have:
X86_FEATURE_NONSTOP_TSC, which states the thing doesn't stop in C states.All of them? I though tsc stops in some mwait deep REM sleep thing.
The idea is that TSC will not stop ever (except s2r like stuff), not sure about the actual implementation of the NONSTOP_TSC bit though.
Quoted 2 lines
So what do we need? test for both
TSC_RELIABLEandNONSTOP_TSC? IMOTSC_RELIABLEshould implyNONSTOP_TSC.
Yeah, I think RELIABLE does imply NONSTOP and CONSTANT, but NONSTOP && CONSTANT does not make RELIABLE.
Re: [PATCH 1/5] Add a global synchronization point for pvclock
Quoted 7 lines · Peter Zijlstra
Quoted 4 lines
So what do we need? test for both
TSC_RELIABLEandNONSTOP_TSC? IMOTSC_RELIABLEshould implyNONSTOP_TSC.Yeah, I think RELIABLE does imply NONSTOP and CONSTANT, but NONSTOP&& CONSTANT does not make RELIABLE.
The manual says:
Quoted 10 lines
16.11.1 Invariant TSC
The time stamp counter in newer processors may support an enhancement, referred to as invariant TSC. Processor’s support for invariant TSC is indicated by CPUID.80000007H:EDX[8].
The invariant TSC will run at a constant rate in all ACPI P-, C-. and T-states. This is the architectural behavior moving forward. On processors with invariant TSC support, the OS may use the TSC for wall clock timer services (instead of ACPI or HPET timers). TSC reads are much more efficient and do not incur the overhead associated with a ring transition or access to a platform resource.
and this maps to NONSTOP, so I think we're fine.
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH 1/5] Add a global synchronization point for pvclock
In reply to Glauber CostaQuoted 29 lines · Glauber Costa
In recent stress tests, it was found that pvclock-based systems could seriously warp in smp systems. Using ingo's
time-warp-test.c, I could trigger a scenario as bad as 1.5mi warps a minute in some systems. (to be fair, it wasn't that bad in most of them). Investigating further, I found out that such warps were caused by the very offset-based calculation pvclock is based on.This happens even on some machines that report
constant_tscin its tsc flags, specially on multi-socket ones.Two reads of the same kernel timestamp at approx the same time, will likely have tsc timestamped in different occasions too. This means the delta we calculate is unpredictable at best, and can probably be smaller in a cpu that is legitimately reading clock in a forward ocasion.
Some adjustments on the host could make this window less likely to happen, but still, it pretty much poses as an intrinsic problem of the mechanism.
A while ago, I though about using a shared variable anyway, to hold clock last state, but gave up due to the high contention locking was likely to introduce, possibly rendering the thing useless on big machines. I argue, however, that locking is not necessary.
We do a read-and-return sequence in pvclock, and between read and return, the global value can have changed. However, it can only have changed by means of an addition of a positive value. So if we detected that our clock timestamp is less than the current global, we know that we need to return a higher one, even though it is not exactly the one we compared to.
Quoted 7 lines
OTOH, if we detect we're greater than the current time source, we atomically replace the value with our new readings. This do causes contention on big boxes (but big here means *BIG*), but it seems like a good trade off, since it provide us with a time source guaranteed to be stable wrt time warps.
After this patch is applied, I don't see a single warp in time during 5 days of execution, in any of the machines I saw them before.
Unfortunately this is breaking Xen save/restore: if you restore on a host which was booted more recently than the save host, causing the system time to be smaller. The effect is that the domain's time leaps forward to a fixed point, and stays there until the host catches up to the source host...
I guess last_time needs to be reset on this type of event. I guess the cleanest way would be for pvclock.c to register a sysdev suspend/resume handler.
J
Quoted 55 lines
Signed-off-by Glauber Costa <[email protected]>CC Jeremy Fitzhardinge <[email protected]>CC Avi Kivity <[email protected]>CC Marcelo Tosatti <[email protected]>CC Zachary Amsden <[email protected]>--- arch/x86/kernel/pvclock.c | 23 +++++++++++++++++++++++ 1 files changed, 23 insertions(+), 0 deletions(-)arch/x86/kernel/pvclock.c +23
-0@@ -109,11 +109,14 @@ unsigned long pvclock_tsc_khz(struct pvclock_vcpu_time_info *src)return pv_tsc_khz; } +static u64 last_value =0 ; +cycle_t pvclock_clocksource_read (struct pvclock_vcpu_time_info *src) {struct pvclock_shadow_time shadow;unsigned version;cycle_t ret, offset; +u64 last; do { version = pvclock_get_time_values(&shadow, src);@@ -123,6 +126,26 @@ cycle_t pvclock_clocksource_read(struct pvclock_vcpu_time_info *src)barrier (); }while (version != src->version); +/* +* Assumption here is that last_value, a global accumulator, always goes +* forward. If we are less than that, we should not be much smaller. +* We assume there is an error marging we're inside, and then the correction +* does not sacrifice accuracy. +* +* For reads: global may have changed between test and return, +* but this means someone else updated poked the clock at a later time. +* We just need to make sure we are not seeing a backwards event. +* +* For updates: last_value = ret is not enough, since two vcpus could be +* updating at the same time, and one of them could be slightly behind, +* making the assumption that last_value always go forward fail to hold. +*/ +do { + last = last_value; +if (ret < last) +return last; + }while (unlikely (cmpxchg64 (&last_value, last, ret) != ret)); +return ret; }
Re: [PATCH 1/5] Add a global synchronization point for pvclock
Quoted 5 lines · Jeremy Fitzhardinge
Unfortunately this is breaking Xen save/restore: if you restore on a host which was booted more recently than the save host, causing the system time to be smaller. The effect is that the domain's time leaps forward to a fixed point, and stays there until the host catches up to the source host...
Shouldn't save/restore also save the timebase?
Quoted 3 lines
I guess
last_timeneeds to be reset on this type of event. I guess the cleanest way would be forpvclock.cto register a sysdev suspend/resume handler.
Should be for Xen only; kvm save/restore doesn't involve the guest.
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH 1/5] Add a global synchronization point for pvclock
Quoted 7 lines · Avi Kivity
Quoted 5 lines · Jeremy Fitzhardinge
Unfortunately this is breaking Xen save/restore: if you restore on a host which was booted more recently than the save host, causing the system time to be smaller. The effect is that the domain's time leaps forward to a fixed point, and stays there until the host catches up to the source host...
Quoted 7 lines
Shouldn't save/restore also save the timebase?
Quoted 3 lines
I guess
last_timeneeds to be reset on this type of event. I guess the cleanest way would be forpvclock.cto register a sysdev suspend/resume handler.Should be for Xen only; kvm save/restore doesn't involve the guest.
the suspend/resume path do adjust the time base. Maybe something similar should be done ?
Re: [PATCH 1/5] Add a global synchronization point for pvclock
In reply to Avi KivityQuoted 8 lines · Avi Kivity
Quoted 5 lines · Jeremy Fitzhardinge
Unfortunately this is breaking Xen save/restore: if you restore on a host which was booted more recently than the save host, causing the system time to be smaller. The effect is that the domain's time leaps forward to a fixed point, and stays there until the host catches up to the source host...
Shouldn't save/restore also save the timebase?
Xen doesn't guarantee the system time is monotonic across those kinds of events. The domain could maintain its own offset to maintain an illusion of monotonicity, but I think its simpler to just zero last_value on resume.
J