maintaining of kvm_clock stability on guest vCPU hotplug
the series sending repeated
Quoted 25 lines · Radim Krčmář
2017-04-05 11:56+0300, Denis Plotnikov:
Quoted 1 line
ping
Please re-send the series, I don't have it in mailbox and web-archives don't show it either,
thanks.
Quoted 6 lines · Denis Plotnikov
Quoted 5 lines
It was found, that after adding a new vCPU to a running VM (CPU hotplug)
kvm_clocklost the stability property in case of using it. This happened because KVM didn't treated cpu hotplug as a special case of TSC synchronization.
This patch series adds cpu hotplug to the TSC synchronization cases.Right, it looks like an incarnation of the deep problem where the kvm clock with masterclock diverges from host (different frequency), so any case where the masterclock is synchronized results in time shifts in the guest.
Quoted 7 lines
Quoted 7 lines
Denis Plotnikov (2):
KVM: x86: remasterkvm_write_tsccode
KVM: x86: fix maintaining ofkvm_clockstability on guest CPU hotplugarch/x86/kvm/x86.c | 59 ++++++++++++++++++------------------------------------ 1 file changed, 20 insertions(+), 39 deletions(-)
PATCH 1/2 KVM: x86: remaster kvm_write_tsc code
Reuse existing code instead of using inline asm.
Make the code more concise and clear in the TSC
synchronization part.
--- arch/x86/kvm/x86.c | 51 ++++++++++++--------------------------------------- 1 file changed, 12 insertions(+), 39 deletions(-)
arch/x86/kvm/x86.c +12 -39
@@ -1444,10 +1444,10 @@ void kvm_write_tsc(struct kvm_vcpu *vcpu, struct msr_data *msr)struct kvm *kvm = vcpu->kvm;u64 offset, ns, elapsed;unsigned long flags; -s64 usdiff;bool matched;bool already_matched;u64 data = msr->data; +bool synchronizing = false;raw_spin_lock_irqsave (&kvm->arch.tsc_write_lock, flags); offset =kvm_compute_tsc_offset (vcpu, data);
@@ -1455,51 +1455,24 @@ void kvm_write_tsc(struct kvm_vcpu *vcpu, struct msr_data *msr) elapsed = ns - kvm->arch.last_tsc_nsec;if (vcpu->arch.virtual_tsc_khz) { -int faulted =0 ; - -/* n.b - signed multiplication and division required */ - usdiff = data - kvm->arch.last_tsc_write; -#ifdef CONFIG_X86_64 - usdiff = (usdiff *1000 ) / vcpu->arch.virtual_tsc_khz; -#else -/* do_div() only does unsigned */ - asm("1: idivl %[divisor]\n" - "2: xor %%edx, %%edx\n" - " movl $0, %[faulted]\n" - "3:\n" - ".section .fixup,\"ax\"\n" - "4: movl $1, %[faulted]\n" - " jmp 3b\n" - ".previous\n" - - _ASM_EXTABLE(1b, 4b) - - : "=A"(usdiff), [faulted] "=r" (faulted) - : "A"(usdiff * 1000), [divisor] "rm"(vcpu->arch.virtual_tsc_khz)); - -#endif - do_div(elapsed,1000 ); - usdiff -= elapsed; -if (usdiff <0 ) - usdiff = -usdiff; - -/* idivl overflow => difference is larger than USEC_PER_SEC */ -if (faulted) - usdiff = USEC_PER_SEC; - }else - usdiff =USEC_PER_SEC ;/* disable TSC match window below */ +u64 tsc_exp = kvm->arch.last_tsc_write + +nsec_to_cycles (vcpu, elapsed); +u64 tsc_hz = vcpu->arch.virtual_tsc_khz *1000LL ; +/* +* Special case: TSC write with a small delta (1 second) of virtual +* cycle time against real time is interpreted as an attempt to +* synchronize the CPU. +*/ + synchronizing = data < tsc_exp + tsc_hz && data > tsc_exp - tsc_hz; + }/* -* Special case: TSC write with a small delta (1 second) of virtual -* cycle time against real time is interpreted as an attempt to -* synchronize the CPU. -* * For a reliable TSC, we can match TSC offsets, and for an unstable * TSC, we add elapsed time in this computation. We could let the * compensation code attempt to catch up if we fall behind, but * it's better to try to match offsets from the beginning. */ -if (usdiff <USEC_PER_SEC && +if (synchronizing && vcpu->arch.virtual_tsc_khz == kvm->arch.last_tsc_khz) {if (!check_tsc_unstable ()) { offset = kvm->arch.cur_tsc_offset;
Signature · 1 line
1.8.3.1
Re: [PATCH 1/2] KVM: x86: remaster kvm_write_tsc code
2017-04-06 11:08+0300, Denis Plotnikov:
Quoted 9 lines
Reuse existing code instead of using inline asm.
Make the code more concise and clear in the TSC
synchronization part.Signed-off-by Denis Plotnikov <[email protected]>Reviewed-by Roman Kagan <[email protected]>--- arch/x86/kvm/x86.c | 51 ++++++++++++--------------------------------------- 1 file changed, 12 insertions(+), 39 deletions(-)
I like this patch a lot.
Quoted 19 lines
arch/x86/kvm/x86.c +0
-14@@ -1455,51 +1455,24 @@ void kvm_write_tsc(struct kvm_vcpu *vcpu, struct msr_data *msr) elapsed = ns - kvm->arch.last_tsc_nsec;if (vcpu->arch.virtual_tsc_khz) { -int faulted =0 ; - -/* n.b - signed multiplication and division required */ - usdiff = data - kvm->arch.last_tsc_write; -#ifdef CONFIG_X86_64 - usdiff = (usdiff *1000 ) / vcpu->arch.virtual_tsc_khz; -#else -/* do_div() only does unsigned */ - asm("1: idivl %[divisor]\n" -"2: xor %%edx, %%edx\n" -" movl $0, %[faulted]\n" -"3:\n" -".section .fixup,\"ax\"\n" - "4: movl $1, %[faulted]\n"
arch/x86/kvm/x86.c void kvm_write_tsc(struct kvm_vcpu *vcpu, struct msr_data *msr) · line 1481
Quoted 10 lines
arch/x86/kvm/x86.c +0
-10- " jmp 3b\n" - ".previous\n" - - _ASM_EXTABLE(1b, 4b) - - : "=A"(usdiff), [faulted] "=r" (faulted) - : "A"(usdiff * 1000), [divisor] "rm"(vcpu->arch.virtual_tsc_khz)); - -#endif - do_div(elapsed,1000 );
Oh, this is actually fixing a bug, because we later consider elapsed in nanoseconds, but this one converts it to microsends ...
Quoted 8 lines
arch/x86/kvm/x86.c +0
-8- usdiff -= elapsed; -if (usdiff <0 ) - usdiff = -usdiff; - -/* idivl overflow => difference is larger than USEC_PER_SEC */ -if (faulted) - usdiff =USEC_PER_SEC ; - }else
arch/x86/kvm/x86.c void kvm_write_tsc(struct kvm_vcpu *vcpu, struct msr_data *msr) · line 1466
Quoted 10 lines
arch/x86/kvm/x86.c +9
-1- usdiff =USEC_PER_SEC ;/* disable TSC match window below */ +u64 tsc_exp = kvm->arch.last_tsc_write + +nsec_to_cycles (vcpu, elapsed); +u64 tsc_hz = vcpu->arch.virtual_tsc_khz *1000LL ; +/* +* Special case: TSC write with a small delta (1 second) of virtual +* cycle time against real time is interpreted as an attempt to +* synchronize the CPU. +*/ + synchronizing = data < tsc_exp + tsc_hz && data > tsc_exp - tsc_hz;
This condition is wrong -- if tsc_exp < tsc_hz, then it will wraparound and resolve as false. It should read:
synchronizing = data < tsc_exp + tsc_hz && data + tsc_hz > tsc_exp;
Quoted 20 lines
arch/x86/kvm/x86.c +2
-5+ }/* -* Special case: TSC write with a small delta (1 second) of virtual -* cycle time against real time is interpreted as an attempt to -* synchronize the CPU. -* * For a reliable TSC, we can match TSC offsets, and for an unstable * TSC, we add elapsed time in this computation. We could let the * compensation code attempt to catch up if we fall behind, but * it's better to try to match offsets from the beginning. */ -if (usdiff <USEC_PER_SEC && +if (synchronizing && vcpu->arch.virtual_tsc_khz == kvm->arch.last_tsc_khz) {if (!check_tsc_unstable ()) { offset = kvm->arch.cur_tsc_offset;Signature · 1 line
1.8.3.1
PATCH 2/2 KVM: x86: fix maintaining of kvm_clock stability on guest CPU hotplug
In reply to Denis PlotnikovVCPU TSC synchronization is perfromed in kvm_write_tsc() when the TSC value being set is within 1 second from the expected, as obtained by extrapolating of the TSC in already synchronized VCPUs.
This is naturally achieved on all VCPUs at VM start and resume; however on VCPU hotplug it is not: the newly added VCPU is created with TSC == 0 while others are well ahead.
To compensate for that, consider host-initiated kvm_write_tsc() with TSC == 0 a special case requiring synchronization regardless of the current TSC on other VCPUs.
--- arch/x86/kvm/x86.c | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-)
arch/x86/kvm/x86.c +17 -9
@@ -1455,15 +1455,23 @@ void kvm_write_tsc(struct kvm_vcpu *vcpu, struct msr_data *msr) elapsed = ns - kvm->arch.last_tsc_nsec;if (vcpu->arch.virtual_tsc_khz) { -u64 tsc_exp = kvm->arch.last_tsc_write + -nsec_to_cycles (vcpu, elapsed); -u64 tsc_hz = vcpu->arch.virtual_tsc_khz *1000LL ; -/* -* Special case: TSC write with a small delta (1 second) of virtual -* cycle time against real time is interpreted as an attempt to -* synchronize the CPU. -*/ - synchronizing = data < tsc_exp + tsc_hz && data > tsc_exp - tsc_hz; +if ((data ==0 ) && msr->host_initiated) { +/* +* detection of vcpu initialization -- need to sync with other vCPUs +* particularly helps to keep kvm_clock stable after CPU hotplug +*/ + synchronizing = true; + }else { +u64 tsc_exp = kvm->arch.last_tsc_write + +nsec_to_cycles (vcpu, elapsed); +u64 tsc_hz = vcpu->arch.virtual_tsc_khz *1000LL ; +/* +* Special case: TSC write with a small delta (1 second) of virtual +* cycle time against real time is interpreted as an attempt to +* synchronize the CPU. +*/ + synchronizing = data < tsc_exp + tsc_hz && data > tsc_exp - tsc_hz; + } }/*
Signature · 1 line
1.8.3.1
Re: [PATCH 2/2] KVM: x86: fix maintaining of kvm_clock stability on guest CPU hotplug
2017-04-06 11:08+0300, Denis Plotnikov:
Quoted 8 lines
VCPU TSC synchronization is perfromed in
kvm_write_tsc()when the TSC value being set is within 1 second from the expected, as obtained by extrapolating of the TSC in already synchronized VCPUs.This is naturally achieved on all VCPUs at VM start and resume; however on VCPU hotplug it is not: the newly added VCPU is created with TSC == 0 while others are well ahead.
Quoted 7 lines
To compensate for that, consider host-initiated
kvm_write_tsc()with TSC == 0 a special case requiring synchronization regardless of the current TSC on other VCPUs.Signed-off-by Denis Plotnikov <[email protected]>Reviewed-by Roman Kagan <[email protected]>---
The idea goes well with current kvm clock.
Quoted 20 lines
arch/x86/kvm/x86.c +6
-9@@ -1455,15 +1455,23 @@ void kvm_write_tsc(struct kvm_vcpu *vcpu, struct msr_data *msr) elapsed = ns - kvm->arch.last_tsc_nsec;if (vcpu->arch.virtual_tsc_khz) { -u64 tsc_exp = kvm->arch.last_tsc_write + -nsec_to_cycles (vcpu, elapsed); -u64 tsc_hz = vcpu->arch.virtual_tsc_khz *1000LL ; -/* -* Special case: TSC write with a small delta (1 second) of virtual -* cycle time against real time is interpreted as an attempt to -* synchronize the CPU. -*/ - synchronizing = data < tsc_exp + tsc_hz && data > tsc_exp - tsc_hz; +if ((data ==0 ) && msr->host_initiated) { +/* +* detection of vcpu initialization -- need to sync with other vCPUs +* particularly helps to keep kvm_clock stable after CPU hotplug +*/ + synchronizing = true;
arch/x86/kvm/x86.c void kvm_write_tsc(struct kvm_vcpu *vcpu, struct msr_data *msr) · line 1473
Quoted 10 lines
arch/x86/kvm/x86.c +10
-0+ }else { +u64 tsc_exp = kvm->arch.last_tsc_write + +nsec_to_cycles (vcpu, elapsed); +u64 tsc_hz = vcpu->arch.virtual_tsc_khz *1000LL ; +/* +* Special case: TSC write with a small delta (1 second) of virtual +* cycle time against real time is interpreted as an attempt to +* synchronize the CPU. +*/ + synchronizing = data < tsc_exp + tsc_hz && data > tsc_exp - tsc_hz;
I would have proposed to fix the bug in [1/2] myself if there weren't too many overlong lines here. Please wrap it, so the last element doesn't start after 80 characters. (And the first comment can also use spaces before stars.)
Thanks.
Re: [PATCH 2/2] KVM: x86: fix maintaining of kvm_clock stability on guest CPU hotplug
2017-04-06 19:11+0200, Radim Krčmář:
Quoted 5 lines
2017-04-06 11:08+0300, Denis Plotnikov:
Quoted 4 lines
arch/x86/kvm/x86.c +0
-0@@ -1455,15 +1455,23 @@ void kvm_write_tsc(struct kvm_vcpu *vcpu, struct msr_data *msr) elapsed = ns - kvm->arch.last_tsc_nsec;
Quoted 11 lines
arch/x86/kvm/x86.c void kvm_write_tsc(struct kvm_vcpu *vcpu, struct msr_data *msr) · line 1458
Quoted 11 lines
arch/x86/kvm/x86.c +1
-9if (vcpu->arch.virtual_tsc_khz) { -u64 tsc_exp = kvm->arch.last_tsc_write + -nsec_to_cycles (vcpu, elapsed); -u64 tsc_hz = vcpu->arch.virtual_tsc_khz *1000LL ; -/* -* Special case: TSC write with a small delta (1 second) of virtual -* cycle time against real time is interpreted as an attempt to -* synchronize the CPU. -*/ - synchronizing = data < tsc_exp + tsc_hz && data > tsc_exp - tsc_hz; +if ((data ==0 ) && msr->host_initiated) {
And while I'm nitpicking ... the code would also be nicer without the parentheses around data.