Skip to contentSkip to conversation

Fix a couple of races in vmexit.flat

PATCH New Avi Kivity 1 participant 4 messages Jan 6, 2011 KVM

lore
Share & tools
lore

Avi Kivity

author
#0 lore raw

cover Fix a couple of races in vmexit.flat

This patchset fixes a couple of races and speeds up an important function.

Avi Kivity (3):
smp: fix race in async on_cpu()
smp: speed up cpu_count()
vmexit: fix race in joining smp tests

 lib/x86/smp.c |   33 +++++++++++++++++++++++----------
 x86/vmexit.c  |    9 +++++----
 2 files changed, 28 insertions(+), 14 deletions(-)
author
#1 lore raw

PATCH 1/3 smp: fix race in async on_cpu()

We fire off the IPI, but don't wait for the other cpu to pickk up the function and data before returning.

Fix by making the other cpu ACK the receipt of the IPI (but still execute the result asynchrously).

---
 lib/x86/smp.c |   29 ++++++++++++++++++++---------
 1 files changed, 20 insertions(+), 9 deletions(-)
lib/x86/smp.c +20 -9
index 241f755..8da614a 100644
@@ -7,15 +7,27 @@
 #define IPI_VECTOR 0x20
 
 static struct spinlock ipi_lock;
-static void (*ipi_function)(void *data);
-static void *ipi_data;
+static volatile void (*ipi_function)(void *data);
+static volatile void *ipi_data;
 static volatile int ipi_done;
+static volatile bool ipi_wait;
+static int _cpu_count;
 
 static __attribute__((used)) void ipi()
 {
-    ipi_function(ipi_data);
-    apic_write(APIC_EOI, 0);
-    ipi_done = 1;
+    void (*function)(void *data) = ipi_function;
+    void *data = ipi_data;
+    bool wait = ipi_wait;
+
+    if (!wait) {
+	ipi_done = 1;
+	apic_write(APIC_EOI, 0);
+    }
+    function(data);
+    if (wait) {
+	ipi_done = 1;
+	apic_write(APIC_EOI, 0);
+    }
 }
 
 asm (
@@ -92,13 +104,12 @@ static void __on_cpu(int cpu, void (*function)(void *data), void *data,
 	ipi_done = 0;
 	ipi_function = function;
 	ipi_data = data;
+	ipi_wait = wait;
 	apic_icr_write(APIC_INT_ASSERT | APIC_DEST_PHYSICAL | APIC_DM_FIXED
                        | IPI_VECTOR,
                        cpu);
-	if (wait) {
-		while (!ipi_done)
-		    ;
-	}
+	while (!ipi_done)
+	    ;
     }
     spin_unlock(&ipi_lock);
 }
Signature · 1 line
1.7.1
author
#2 lore raw

PATCH 2/3 smp: speed up cpu_count()

In reply to Avi Kivity

cpu_count() is used in important places, like vmexit.flat's measuring loop, yet it is ridiculously slow as it talks to the firmware config interface.

Speed it up by reading the value from memory.

---
 lib/x86/smp.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
lib/x86/smp.c +3 -1
index 8da614a..d41c332 100644
@@ -78,7 +78,7 @@ void spin_unlock(struct spinlock *lock)
 
 int cpu_count(void)
 {
-    return fwcfg_get_nb_cpus();
+    return _cpu_count;
 }
 
 int smp_id(void)
@@ -130,6 +130,8 @@ void smp_init(void)
     int i;
     void ipi_entry(void);
 
+    _cpu_count = fwcfg_get_nb_cpus();
+
     set_ipi_descriptor(ipi_entry);
 
     setup_smp_id(0);
Signature · 1 line
1.7.1
author
#3 lore raw

PATCH 3/3 vmexit: fix race in joining smp tests

In reply to Avi Kivity

'nr_cpus_done' is not incremented atomically; this has been observed to cause tests to stall. Fix by using a proper atomic increment.

---
 x86/vmexit.c |    9 +++++----
 1 files changed, 5 insertions(+), 4 deletions(-)
x86/vmexit.c +5 -4
index 875caa3..ad8ab55 100644
@@ -2,6 +2,7 @@
 #include "libcflat.h"
 #include "smp.h"
 #include "processor.h"
+#include "atomic.h"
 
 static unsigned int inl(unsigned short port)
 {
@@ -121,7 +122,7 @@ static struct test {
 };
 
 unsigned iterations;
-volatile int nr_cpus_done;
+static atomic_t nr_cpus_done;
 
 static void run_test(void *_func)
 {
@@ -131,7 +132,7 @@ static void run_test(void *_func)
     for (i = 0; i < iterations; ++i)
         func();
 
-    nr_cpus_done++;
+    atomic_inc(&nr_cpus_done);
 }
 
 static void do_test(struct test *test)
@@ -155,10 +156,10 @@ static void do_test(struct test *test)
 			for (i = 0; i < iterations; ++i)
 				func();
 		} else {
-			nr_cpus_done = 0;
+			atomic_set(&nr_cpus_done, 0);
 			for (i = cpu_count(); i > 0; i--)
 				on_cpu_async(i-1, run_test, func);
-			while (nr_cpus_done < cpu_count())
+			while (atomic_read(&nr_cpus_done) < cpu_count())
 				;
 		}
 		t2 = rdtsc();
Signature · 1 line
1.7.1