diff mbox series

[kvmtool,1/2] kvm: Do not pause already paused vcpus

Message ID 1539941791-40118-2-git-send-email-julien.thierry@arm.com (mailing list archive)
State New, archived
Headers show
Series Fix kvm__pause races | expand

Commit Message

Julien Thierry Oct. 19, 2018, 9:36 a.m. UTC
With the following sequence:
	kvm__pause();
	kvm__continue();
	kvm__pause();

There is a chance that not all paused threads have been resumed, and the
second kvm__pause will attempt to pause them again. Since the paused thread
is waiting to own the pause_lock, it won't write its second pause
notification. kvm__pause will be waiting for that notification while owning
pause_lock, so... deadlock.

Simple solution is not to try to pause thread that had not the chance to
resume.

Signed-off-by: Julien Thierry <julien.thierry@arm.com>
---
 kvm-cpu.c | 4 +---
 kvm.c     | 5 ++++-
 2 files changed, 5 insertions(+), 4 deletions(-)
diff mbox series

Patch

diff --git a/kvm-cpu.c b/kvm-cpu.c
index cc8385f..4107841 100644
--- a/kvm-cpu.c
+++ b/kvm-cpu.c
@@ -148,10 +148,8 @@  int kvm_cpu__start(struct kvm_cpu *cpu)
 		kvm_cpu__enable_singlestep(cpu);
 
 	while (cpu->is_running) {
-		if (cpu->paused) {
+		if (cpu->paused)
 			kvm__notify_paused();
-			cpu->paused = 0;
-		}
 
 		if (cpu->needs_nmi) {
 			kvm_cpu__arch_nmi(cpu);
diff --git a/kvm.c b/kvm.c
index 05ad0b6..052c6e8 100644
--- a/kvm.c
+++ b/kvm.c
@@ -63,6 +63,8 @@  extern struct kvm_ext kvm_req_ext[];
 
 static char kvm_dir[PATH_MAX];
 
+extern __thread struct kvm_cpu *current_kvm_cpu;
+
 static int set_dir(const char *fmt, va_list args)
 {
 	char tmp[PATH_MAX];
@@ -521,7 +523,7 @@  void kvm__pause(struct kvm *kvm)
 	if (pause_event < 0)
 		die("Failed creating pause notification event");
 	for (i = 0; i < kvm->nrcpus; i++) {
-		if (kvm->cpus[i]->is_running)
+		if (kvm->cpus[i]->is_running && kvm->cpus[i]->paused == 0)
 			pthread_kill(kvm->cpus[i]->thread, SIGKVMPAUSE);
 		else
 			paused_vcpus++;
@@ -545,5 +547,6 @@  void kvm__notify_paused(void)
 		die("Failed notifying of paused VCPU.");
 
 	mutex_lock(&pause_lock);
+	current_kvm_cpu->paused = 0;
 	mutex_unlock(&pause_lock);
 }