Skip to contentSkip to conversation

remove static declaration from wall clock version

PATCH Applied Glauber Costa 4 participants 7 messages Feb 26, 2009 – Mar 8, 2009

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

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
index 2511708..d7236f6 100644
@@ -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
#1 lore raw
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 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;

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 <><
#2 lore raw
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 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;

Quoted 8 lines
Quoted 5 lines

-       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?

No. If you look closely, it's now initialized to 1.

Quoted 3 lines

I think what you need here is a 'static atomic_t version;' so you can do an atomic_inc instead 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.

#3 lore raw
Quoted 4 lines · Glauber Costa
Quoted 3 lines

Doesn't this mean that kvm_write_guest now 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 <><
Acked-by on v1 1/1
#4 lore raw
In reply to 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.

---
 arch/x86/kvm/x86.c |    4 +---
 1 files changed, 1 insertions(+), 3 deletions(-)
arch/x86/kvm/x86.c +1 -3
index 2511708..d7236f6 100644
@@ -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
#5 lore raw
In reply to Glauber Costa
Quoted 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.

#6 lore raw
In reply to Glauber Costa
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.

---
 arch/x86/kvm/x86.c |    4 +---
 1 files changed, 1 insertions(+), 3 deletions(-)
arch/x86/kvm/x86.c +1 -1
index 2511708..d7236f6 100644
@@ -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 -2
 	struct 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