remove static declaration from wall clock version
PATCH 1/1 remove static declaration from wall clock version
Matt T. Yourst noted that we're currently having a dumb race for no reason in paravirtual wall clock. This is due to the use of a static variable to hold the counting.
This can race with multiple guests reading wallclock at the same time, since the static variable value would then be accessible to all callers. This wasn't noted before because it is a rather rare scenario.
Instead, just use a normal stack variable. This will mean that each caller will have it's version written separatedly. No need for a global counter.
--- arch/x86/kvm/x86.c | 4 +--- 1 files changed, 1 insertions(+), 3 deletions(-)
arch/x86/kvm/x86.c +1 -3
@@ -548,15 +548,13 @@ static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)static void kvm_write_wall_clock (struct kvm *kvm,gpa_t wall_clock) { -static int version; +int version =1 ;struct pvclock_wall_clock wc;struct timespec now, sys, boot;if (!wall_clock)return ; - version++; -kvm_write_guest (kvm, wall_clock, &version,sizeof (version));/*
Signature · 1 line
1.5.6.5
Quoted 9 lines · Glauber Costa
(quoted diff) +0
-0@@ -548,15 +548,13 @@ static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
static voidkvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock){
- static int version;
+ int version = 1;
structpvclock_wall_clockwc;
struct timespec now, sys, boot;
Quoted 8 lines
if (!wall_clock)
return;
- version++;
-
kvm_write_guest(kvm,wall_clock, &version, sizeof(version));
/*
Doesn't this mean that kvm_write_guest now writes an uninitialized value to the guest?
I think what you need here is a 'static atomic_t version;' so you can do an atomic_inc instead of the ++.
Arnd <><
Quoted 13 lines · Arnd Bergmann
Quoted 12 lines · Glauber Costa
(quoted diff) +0
-0@@ -548,15 +548,13 @@ static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
static voidkvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock){
- static int version;
+ int version = 1;
structpvclock_wall_clockwc;
struct timespec now, sys, boot;
if (!wall_clock)
return;
Quoted 8 lines
Quoted 5 lines
- version++;
-
kvm_write_guest(kvm,wall_clock, &version, sizeof(version));
/*Doesn't this mean that
kvm_write_guestnow writes an uninitialized value to the guest?
No. If you look closely, it's now initialized to 1.
Quoted 3 lines
I think what you need here is a 'static
atomic_tversion;' so you can do anatomic_incinstead of the ++.
I don't see the need for atomicity. This is just called once, at boot time. The only thing we're protecting here is one guest from another. The stack will do fine for this.
Quoted 4 lines · Glauber Costa
Quoted 3 lines
Doesn't this mean that
kvm_write_guestnow writes an uninitialized value to the guest?No. If you look closely, it's now initialized to 1.
Right, I didn't see that change at first.
Arnd <><
Matt T. Yourst noted that we're currently having a dumb race for no reason in paravirtual wall clock. This is due to the use of a static variable to hold the counting.
This can race with multiple guests reading wallclock at the same time, since the static variable value would then be accessible to all callers. This wasn't noted before because it is a rather rare scenario.
Instead, just use a normal stack variable. This will mean that each caller will have it's version written separatedly. No need for a global counter.
--- arch/x86/kvm/x86.c | 4 +--- 1 files changed, 1 insertions(+), 3 deletions(-)
arch/x86/kvm/x86.c +1 -3
@@ -548,15 +548,13 @@ static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)static void kvm_write_wall_clock (struct kvm *kvm,gpa_t wall_clock) { -static int version; +int version =1 ;struct pvclock_wall_clock wc;struct timespec now, sys, boot;if (!wall_clock)return ; - version++; -kvm_write_guest (kvm, wall_clock, &version,sizeof (version));/*
Signature · 1 line
1.5.6.5
Marcelo Tosatti
AppliedQuoted 4 lines · Glauber Costa
Matt T. Yourst noted that we're currently having a dumb race for no reason in paravirtual wall clock. This is due to the use of a static variable to hold the counting.
Quoted 8 lines
This can race with multiple guests reading wallclock at the same time, since the static variable value would then be accessible to all callers. This wasn't noted before because it is a rather rare scenario.
Instead, just use a normal stack variable. This will mean that each caller will have it's version written separatedly. No need for a global counter.
Applied, thanks.
Quoted 28 lines · Glauber Costa
Matt T. Yourst noted that we're currently having a dumb race for no reason in paravirtual wall clock. This is due to the use of a static variable to hold the counting.
This can race with multiple guests reading wallclock at the same time, since the static variable value would then be accessible to all callers. This wasn't noted before because it is a rather rare scenario.
Instead, just use a normal stack variable. This will mean that each caller will have it's version written separatedly. No need for a global counter.
Signed-off-by Glauber Costa <[email protected]>--- arch/x86/kvm/x86.c | 4 +--- 1 files changed, 1 insertions(+), 3 deletions(-)arch/x86/kvm/x86.c +1
-1@@ -548,15 +548,13 @@ static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)static void kvm_write_wall_clock (struct kvm *kvm,gpa_t wall_clock) { -static int version; +int version =1 ;
arch/x86/kvm/x86.c static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data) · line 559
Quoted 10 lines
arch/x86/kvm/x86.c +0
-2struct pvclock_wall_clock wc;struct timespec now, sys, boot;if (!wall_clock)return ; - version++; -kvm_write_guest (kvm, wall_clock, &version,sizeof (version));
Suppose currently version == 2.
guest: read version (2)
guest: read sec
host: write version (1)
host: write sec
host: write nsec
host: write version (2)
guest: read nsec
guest: read version (2)
So now we have inconsistent time (sec from old data, nsec from new data).
We need to make version a per-vm value. Best to read it from guest memory, so nothing special needs to be done for live migration. Also use mutual exclusion in kvm_write_wall_clock() - sequence locks don't support multiple writers.
Signature · 1 line
error compiling committee.c: too many arguments to function