Skip to contentSkip to conversation

kvm tools: Use correct value for user signal base

PATCH New Sasha Levin 1 participant 9 messages May 30, 2011 KVM

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/8 kvm tools: Use correct value for user signal base

The bug has caused us to exit gracefully when receiving SIGUSR2.

---
 tools/kvm/include/kvm/kvm.h |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
tools/kvm/include/kvm/kvm.h +1 -1
index f951f2d..6a17362 100644
@@ -11,7 +11,7 @@
 #define KVM_32BIT_GAP_SIZE	(512 << 20)
 #define KVM_32BIT_GAP_START	((1ULL << 32) - KVM_32BIT_GAP_SIZE)
 
-#define SIGKVMEXIT		(SIGUSR1 + 2)
+#define SIGKVMEXIT		(SIGRTMIN + 0)
 
 struct kvm {
 	int			sys_fd;		/* For system ioctls(), i.e. /dev/kvm */
Signature · 1 line
1.7.5.3
#1 lore raw

PATCH v3 2/8 kvm tools: Remove wrong global definition of kvm

---
 tools/kvm/include/kvm/mptable.h |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
tools/kvm/include/kvm/mptable.h +1 -1
index 3c8362d..8557ae8 100644
@@ -1,7 +1,7 @@
 #ifndef KVM_MPTABLE_H_
 #define KVM_MPTABLE_H_
 
-struct kvm kvm;
+struct kvm;
 
 void mptable_setup(struct kvm *kvm, unsigned int ncpus);
 
Signature · 1 line
1.7.5.3
#2 lore raw

PATCH v3 3/8 kvm tools: Add APIs to allow pausing guests

In reply to Sasha Levin

Allow pausing and unpausing guests running on the host. Pausing a guest means that none of the VCPU threads are running KVM_RUN until they are unpaused.

The following API functions are added:
void kvm__pause(void);
void kvm__continue(void);
void kvm__notify_paused(void);

---
 tools/kvm/include/kvm/kvm-cpu.h |    1 +
 tools/kvm/include/kvm/kvm.h     |    4 +++
 tools/kvm/kvm-cpu.c             |   20 +++++++++++---
 tools/kvm/kvm-run.c             |    4 +-
 tools/kvm/kvm.c                 |   54 +++++++++++++++++++++++++++++++++++++++
 5 files changed, 76 insertions(+), 7 deletions(-)
tools/kvm/include/kvm/kvm-cpu.h +1 -0
index b2b6fce..4d99246 100644
@@ -23,6 +23,7 @@ struct kvm_cpu {
 	struct kvm_msrs		*msrs;		/* dynamically allocated */
 
 	u8			is_running;
+	u8			paused;
 };
 
 struct kvm_cpu *kvm_cpu__init(struct kvm *kvm, unsigned long cpu_id);
tools/kvm/include/kvm/kvm.h +4 -0
index 6a17362..d22a849 100644
@@ -12,6 +12,7 @@
 #define KVM_32BIT_GAP_START	((1ULL << 32) - KVM_32BIT_GAP_SIZE)
 
 #define SIGKVMEXIT		(SIGRTMIN + 0)
+#define SIGKVMPAUSE		(SIGRTMIN + 1)
 
 struct kvm {
 	int			sys_fd;		/* For system ioctls(), i.e. /dev/kvm */
@@ -50,6 +51,9 @@ bool kvm__emulate_io(struct kvm *kvm, u16 port, void *data, int direction, int s
 bool kvm__emulate_mmio(struct kvm *kvm, u64 phys_addr, u8 *data, u32 len, u8 is_write);
 bool kvm__register_mmio(u64 phys_addr, u64 phys_addr_len, void (*kvm_mmio_callback_fn)(u64 addr, u8 *data, u32 len, u8 is_write));
 bool kvm__deregister_mmio(u64 phys_addr);
+void kvm__pause(void);
+void kvm__continue(void);
+void kvm__notify_paused(void);
 
 /*
  * Debugging
tools/kvm/kvm-cpu.c +15 -5
index de0591f..be0528b 100644
@@ -383,11 +383,15 @@ void kvm_cpu__run(struct kvm_cpu *vcpu)
 		die_perror("KVM_RUN failed");
 }
 
-static void kvm_cpu_exit_handler(int signum)
+static void kvm_cpu_signal_handler(int signum)
 {
-	if (current_kvm_cpu->is_running) {
-		current_kvm_cpu->is_running = false;
-		pthread_kill(pthread_self(), SIGKVMEXIT);
+	if (signum == SIGKVMEXIT) {
+		if (current_kvm_cpu->is_running) {
+			current_kvm_cpu->is_running = false;
+			pthread_kill(pthread_self(), SIGKVMEXIT);
+		}
+	} else if (signum == SIGKVMPAUSE) {
+		current_kvm_cpu->paused = 1;
 	}
 }
 
@@ -400,12 +404,18 @@ int kvm_cpu__start(struct kvm_cpu *cpu)
 
 	pthread_sigmask(SIG_BLOCK, &sigset, NULL);
 
-	signal(SIGKVMEXIT, kvm_cpu_exit_handler);
+	signal(SIGKVMEXIT, kvm_cpu_signal_handler);
+	signal(SIGKVMPAUSE, kvm_cpu_signal_handler);
 
 	kvm_cpu__setup_cpuid(cpu);
 	kvm_cpu__reset_vcpu(cpu);
 
 	for (;;) {
+		if (cpu->paused) {
+			kvm__notify_paused();
+			cpu->paused = 0;
+		}
+
 		kvm_cpu__run(cpu);
 
 		switch (cpu->kvm_run->exit_reason) {
tools/kvm/kvm-run.c +2 -2
index 48b8e70..761ac0d 100644
@@ -47,8 +47,8 @@
 #define MIN_RAM_SIZE_MB		(64ULL)
 #define MIN_RAM_SIZE_BYTE	(MIN_RAM_SIZE_MB << MB_SHIFT)
 
-static struct kvm *kvm;
-static struct kvm_cpu *kvm_cpus[KVM_NR_CPUS];
+struct kvm *kvm;
+struct kvm_cpu *kvm_cpus[KVM_NR_CPUS];
 __thread struct kvm_cpu *current_kvm_cpu;
 
 static u64 ram_size;
tools/kvm/kvm.c +54 -0

75 lines not on this page · raw message

Signature · 1 line
1.7.5.3
#3 lore raw

PATCH v2 3/8 kvm tools: Add APIs to allow pausing guests

In reply to Sasha Levin

Allow pausing and unpausing guests running on the host. Pausing a guest means that none of the VCPU threads are running KVM_RUN until they are unpaused.

The following API functions are added:
void kvm__pause(void);
void kvm__continue(void);
void kvm__notify_paused(void);

---
 tools/kvm/include/kvm/kvm-cpu.h |    1 +
 tools/kvm/include/kvm/kvm.h     |    4 +++
 tools/kvm/kvm-cpu.c             |   20 +++++++++++---
 tools/kvm/kvm-run.c             |    4 +-
 tools/kvm/kvm.c                 |   54 +++++++++++++++++++++++++++++++++++++++
 5 files changed, 76 insertions(+), 7 deletions(-)
tools/kvm/include/kvm/kvm-cpu.h +1 -0
index b2b6fce..4d99246 100644
@@ -23,6 +23,7 @@ struct kvm_cpu {
 	struct kvm_msrs		*msrs;		/* dynamically allocated */
 
 	u8			is_running;
+	u8			paused;
 };
 
 struct kvm_cpu *kvm_cpu__init(struct kvm *kvm, unsigned long cpu_id);
tools/kvm/include/kvm/kvm.h +4 -0
index 6a17362..d22a849 100644
@@ -12,6 +12,7 @@
 #define KVM_32BIT_GAP_START	((1ULL << 32) - KVM_32BIT_GAP_SIZE)
 
 #define SIGKVMEXIT		(SIGRTMIN + 0)
+#define SIGKVMPAUSE		(SIGRTMIN + 1)
 
 struct kvm {
 	int			sys_fd;		/* For system ioctls(), i.e. /dev/kvm */
@@ -50,6 +51,9 @@ bool kvm__emulate_io(struct kvm *kvm, u16 port, void *data, int direction, int s
 bool kvm__emulate_mmio(struct kvm *kvm, u64 phys_addr, u8 *data, u32 len, u8 is_write);
 bool kvm__register_mmio(u64 phys_addr, u64 phys_addr_len, void (*kvm_mmio_callback_fn)(u64 addr, u8 *data, u32 len, u8 is_write));
 bool kvm__deregister_mmio(u64 phys_addr);
+void kvm__pause(void);
+void kvm__continue(void);
+void kvm__notify_paused(void);
 
 /*
  * Debugging
tools/kvm/kvm-cpu.c +15 -5
index de0591f..be0528b 100644
@@ -383,11 +383,15 @@ void kvm_cpu__run(struct kvm_cpu *vcpu)
 		die_perror("KVM_RUN failed");
 }
 
-static void kvm_cpu_exit_handler(int signum)
+static void kvm_cpu_signal_handler(int signum)
 {
-	if (current_kvm_cpu->is_running) {
-		current_kvm_cpu->is_running = false;
-		pthread_kill(pthread_self(), SIGKVMEXIT);
+	if (signum == SIGKVMEXIT) {
+		if (current_kvm_cpu->is_running) {
+			current_kvm_cpu->is_running = false;
+			pthread_kill(pthread_self(), SIGKVMEXIT);
+		}
+	} else if (signum == SIGKVMPAUSE) {
+		current_kvm_cpu->paused = 1;
 	}
 }
 
@@ -400,12 +404,18 @@ int kvm_cpu__start(struct kvm_cpu *cpu)
 
 	pthread_sigmask(SIG_BLOCK, &sigset, NULL);
 
-	signal(SIGKVMEXIT, kvm_cpu_exit_handler);
+	signal(SIGKVMEXIT, kvm_cpu_signal_handler);
+	signal(SIGKVMPAUSE, kvm_cpu_signal_handler);
 
 	kvm_cpu__setup_cpuid(cpu);
 	kvm_cpu__reset_vcpu(cpu);
 
 	for (;;) {
+		if (cpu->paused) {
+			kvm__notify_paused();
+			cpu->paused = 0;
+		}
+
 		kvm_cpu__run(cpu);
 
 		switch (cpu->kvm_run->exit_reason) {
tools/kvm/kvm-run.c +2 -2
index 48b8e70..761ac0d 100644
@@ -47,8 +47,8 @@
 #define MIN_RAM_SIZE_MB		(64ULL)
 #define MIN_RAM_SIZE_BYTE	(MIN_RAM_SIZE_MB << MB_SHIFT)
 
-static struct kvm *kvm;
-static struct kvm_cpu *kvm_cpus[KVM_NR_CPUS];
+struct kvm *kvm;
+struct kvm_cpu *kvm_cpus[KVM_NR_CPUS];
 __thread struct kvm_cpu *current_kvm_cpu;
 
 static u64 ram_size;
tools/kvm/kvm.c +54 -0

75 lines not on this page · raw message

Signature · 1 line
1.7.5.rc3
#4 lore raw

PATCH v3 4/8 kvm tools: Pause/resume guest using SIGUSR2

In reply to Sasha Levin

Make SIGUSR2 pause/resume a guest, this allows to easily test pausing a guest.
Can be tested using cmdline 'kill -USR2 $(pidof kvm)'.

---
 tools/kvm/kvm-run.c |   15 +++++++++++++++
 1 files changed, 15 insertions(+), 0 deletions(-)
tools/kvm/kvm-run.c +15 -0
index 761ac0d..b0ef333 100644
@@ -165,6 +165,20 @@ static void handle_sigusr1(int sig)
 	mb();
 }
 
+/* Pause/resume the guest using SIGUSR2 */
+static int is_paused;
+
+static void handle_sigusr2(int sig)
+{
+	if (is_paused)
+		kvm__continue();
+	else
+		kvm__pause();
+
+	is_paused = !is_paused;
+	pr_info("Guest %s\n", is_paused ? "paused" : "resumed");
+}
+
 static void handle_sigquit(int sig)
 {
 	int i;
@@ -422,6 +436,7 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix)
 	signal(SIGALRM, handle_sigalrm);
 	signal(SIGQUIT, handle_sigquit);
 	signal(SIGUSR1, handle_sigusr1);
+	signal(SIGUSR2, handle_sigusr2);
 
 	nr_online_cpus = sysconf(_SC_NPROCESSORS_ONLN);
 
Signature · 1 line
1.7.5.3
#5 lore raw

PATCH v3 5/8 kvm tools: Add a brlock

In reply to Sasha Levin

brlock is a lock which is very cheap for reads, but very expensive for writes.
This lock will be used when updates are very rare and reads are common.
This lock is currently implemented by stopping the guest while performing the updates. We assume that the only threads which read from the locked data are VCPU threads, and the only writer isn't a VCPU thread.

---
 tools/kvm/include/kvm/brlock.h |   25 +++++++++++++++++++++++++
 1 files changed, 25 insertions(+), 0 deletions(-)
 create mode 100644 tools/kvm/include/kvm/brlock.h
tools/kvm/include/kvm/brlock.h new file +25 -0
new file mode 100644
index 0000000..2e2e0f8
@@ -0,0 +1,25 @@
+#ifndef KVM__BRLOCK_H
+#define KVM__BRLOCK_H
+
+#include "kvm/kvm.h"
+#include "kvm/barrier.h"
+
+/*
+ * brlock is a lock which is very cheap for reads, but very expensive
+ * for writes.
+ * This lock will be used when updates are very rare and reads are common.
+ * This lock is currently implemented by stopping the guest while
+ * performing the updates. We assume that the only threads whichread from
+ * the locked data are VCPU threads, and the only writer isn't a VCPU thread.
+ */
+
+#ifndef barrier
+#define barrier()		__asm__ __volatile__("": : :"memory")
+#endif
+
+#define br_read_lock()		barrier()
+#define br_read_unlock()	barrier()
+
+#define br_write_lock()		kvm__pause()
+#define br_write_unlock()	kvm__continue()
+#endif
Signature · 1 line
1.7.5.3
#6 lore raw

PATCH v3 6/8 kvm tools: Add rwlock wrapper

In reply to Sasha Levin

Adds a rwlock wrapper which like the mutex wrapper makes rwlock calls similar to their kernel counterparts.

---
 tools/kvm/include/kvm/rwsem.h |   39 +++++++++++++++++++++++++++++++++++++++
 1 files changed, 39 insertions(+), 0 deletions(-)
 create mode 100644 tools/kvm/include/kvm/rwsem.h
tools/kvm/include/kvm/rwsem.h new file +39 -0
new file mode 100644
index 0000000..75a22f8
@@ -0,0 +1,39 @@
+#ifndef KVM__RWSEM_H
+#define KVM__RWSEM_H
+
+#include <pthread.h>
+
+#include "kvm/util.h"
+
+/*
+ * Kernel-alike rwsem API - to make it easier for kernel developers
+ * to write user-space code! :-)
+ */
+
+#define DECLARE_RWSEM(sem) pthread_rwlock_t sem = PTHREAD_RWLOCK_INITIALIZER
+
+static inline void down_read(pthread_rwlock_t *rwsem)
+{
+	if (pthread_rwlock_rdlock(rwsem) != 0)
+		die("unexpected pthread_rwlock_rdlock() failure!");
+}
+
+static inline void down_write(pthread_rwlock_t *rwsem)
+{
+	if (pthread_rwlock_wrlock(rwsem) != 0)
+		die("unexpected pthread_rwlock_wrlock() failure!");
+}
+
+static inline void up_read(pthread_rwlock_t *rwsem)
+{
+	if (pthread_rwlock_unlock(rwsem) != 0)
+		die("unexpected pthread_rwlock_unlock() failure!");
+}
+
+static inline void up_write(pthread_rwlock_t *rwsem)
+{
+	if (pthread_rwlock_unlock(rwsem) != 0)
+		die("unexpected pthread_rwlock_unlock() failure!");
+}
+
+#endif /* KVM__RWSEM_H */
Signature · 1 line
1.7.5.3
#7 lore raw

PATCH v3 7/8 kvm tools: Add debug mode to brlock

In reply to Sasha Levin

Adds a debug mode which allows to switch the brlock into a big rwlock.
This can be used to verify we don't end up with a BKL kind of lock with the current brlock implementation.

---
 tools/kvm/include/kvm/brlock.h |   16 ++++++++++++++++
 1 files changed, 16 insertions(+), 0 deletions(-)
tools/kvm/include/kvm/brlock.h +16 -0
index 2e2e0f8..bd1d882 100644
@@ -17,9 +17,25 @@
 #define barrier()		__asm__ __volatile__("": : :"memory")
 #endif
 
+#ifdef KVM_BRLOCK_DEBUG
+
+#include "kvm/rwsem.h"
+
+DECLARE_RWSEM(brlock_sem);
+
+#define br_read_lock()		down_read(&brlock_sem);
+#define br_read_unlock()	up_read(&brlock_sem);
+
+#define br_write_lock()		down_write(&brlock_sem);
+#define br_write_unlock()	up_write(&brlock_sem);
+
+#else
+
 #define br_read_lock()		barrier()
 #define br_read_unlock()	barrier()
 
 #define br_write_lock()		kvm__pause()
 #define br_write_unlock()	kvm__continue()
 #endif
+
+#endif
Signature · 1 line
1.7.5.3
#8 lore raw

PATCH v3 8/8 kvm tools: Use brlock in MMIO and IOPORT

In reply to Sasha Levin

Use brlock to protect mmio and ioport modules and make them update-safe.

---
 tools/kvm/ioport.c |   10 +++++++++-
 tools/kvm/mmio.c   |   21 ++++++++++++++++++---
 2 files changed, 27 insertions(+), 4 deletions(-)
tools/kvm/ioport.c +9 -1
index d0a1aa8..e00fb59 100644
@@ -2,7 +2,7 @@
 
 #include "kvm/kvm.h"
 #include "kvm/util.h"
-
+#include "kvm/brlock.h"
 #include "kvm/rbtree-interval.h"
 #include "kvm/mutex.h"
 
@@ -84,6 +84,7 @@ u16 ioport__register(u16 port, struct ioport_operations *ops, int count, void *p
 {
 	struct ioport *entry;
 
+	br_write_lock();
 	if (port == IOPORT_EMPTY)
 		port = ioport__find_free_port();
 
@@ -105,6 +106,8 @@ u16 ioport__register(u16 port, struct ioport_operations *ops, int count, void *p
 
 	ioport_insert(&ioport_tree, entry);
 
+	br_write_unlock();
+
 	return port;
 }
 
@@ -127,6 +130,7 @@ bool kvm__emulate_io(struct kvm *kvm, u16 port, void *data, int direction, int s
 	bool ret = false;
 	struct ioport *entry;
 
+	br_read_lock();
 	entry = ioport_search(&ioport_tree, port);
 	if (!entry)
 		goto error;
@@ -141,11 +145,15 @@ bool kvm__emulate_io(struct kvm *kvm, u16 port, void *data, int direction, int s
 			ret = ops->io_out(entry, kvm, port, data, size, count);
 	}
 
+	br_read_unlock();
+
 	if (!ret)
 		goto error;
 
 	return true;
 error:
+	br_read_unlock();
+
 	if (ioport_debug)
 		ioport_error(port, data, direction, size, count);
 
tools/kvm/mmio.c +18 -3
index ef986bf..acd091e 100644
@@ -1,5 +1,6 @@
 #include "kvm/kvm.h"
 #include "kvm/rbtree-interval.h"
+#include "kvm/brlock.h"
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -55,6 +56,7 @@ static const char *to_direction(u8 is_write)
 bool kvm__register_mmio(u64 phys_addr, u64 phys_addr_len, void (*kvm_mmio_callback_fn)(u64 addr, u8 *data, u32 len, u8 is_write))
 {
 	struct mmio_mapping *mmio;
+	int ret;
 
 	mmio = malloc(sizeof(*mmio));
 	if (mmio == NULL)
@@ -65,31 +67,44 @@ bool kvm__register_mmio(u64 phys_addr, u64 phys_addr_len, void (*kvm_mmio_callba
 		.kvm_mmio_callback_fn = kvm_mmio_callback_fn,
 	};
 
-	return mmio_insert(&mmio_tree, mmio);
+	br_write_lock();
+	ret = mmio_insert(&mmio_tree, mmio);
+	br_write_unlock();
+
+	return ret;
 }
 
 bool kvm__deregister_mmio(u64 phys_addr)
 {
 	struct mmio_mapping *mmio;
 
+	br_write_lock();
 	mmio = mmio_search_single(&mmio_tree, phys_addr);
-	if (mmio == NULL)
+	if (mmio == NULL) {
+		br_write_unlock();
 		return false;
+	}
 
 	rb_int_erase(&mmio_tree, &mmio->node);
+	br_write_unlock();
+
 	free(mmio);
 	return true;
 }
 
 bool kvm__emulate_mmio(struct kvm *kvm, u64 phys_addr, u8 *data, u32 len, u8 is_write)
 {
-	struct mmio_mapping *mmio = mmio_search(&mmio_tree, phys_addr, len);
+	struct mmio_mapping *mmio;
+
+	br_read_lock();
+	mmio = mmio_search(&mmio_tree, phys_addr, len);
 
 	if (mmio)
 		mmio->kvm_mmio_callback_fn(phys_addr, data, len, is_write);
 	else
 		fprintf(stderr, "Warning: Ignoring MMIO %s at %016llx (length %u)\n",
 			to_direction(is_write), phys_addr, len);
+	br_read_unlock();
 
 	return true;
 }
Signature · 1 line
1.7.5.3