Skip to contentSkip to conversation

cpus: fix various issues with vCPU unplug

PATCH In review Paolo Bonzini 2 participants 8 messages Feb 1, 2018

lore
Share & tools
lore
#0 lore raw

cover cpus: fix various issues with vCPU unplug

Fix the thread exit conditions and clean up the threads correctly.

Thanks,

Paolo

Paolo Bonzini (6):
cpus: hax: register/unregister thread with RCU, exit loop on unplug
cpus: kvm: unregister thread with RCU
cpus: dummy: unregister thread with RCU, exit loop on unplug
cpus: tcg: unregister thread with RCU, fix exiting of loop on unplug
cpus: hvf: unregister thread with RCU
cpus: join thread when removing a vCPU

 cpus.c | 40 ++++++++++++++++++++--------------------
 1 file changed, 20 insertions(+), 20 deletions(-)
Signature · 1 line
2.14.3
#1 lore raw

PATCH 1/6 cpus: hax: register/unregister thread with RCU, exit loop on unplug

---
 cpus.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
cpus.c +4 -2
index 1495ed5c36..231550a0d8 100644
@@ -1474,6 +1474,7 @@ static void *qemu_hax_cpu_thread_fn(void *arg)
     CPUState *cpu = arg;
     int r;
 
+    rcu_register_thread();
     qemu_mutex_lock_iothread();
     qemu_thread_get_self(cpu->thread);
 
@@ -1485,7 +1486,7 @@ static void *qemu_hax_cpu_thread_fn(void *arg)
     hax_init_vcpu(cpu);
     qemu_cond_signal(&qemu_cpu_cond);
 
-    while (1) {
+    do {
         if (cpu_can_run(cpu)) {
             r = hax_smp_cpu_exec(cpu);
             if (r == EXCP_DEBUG) {
@@ -1494,7 +1495,8 @@ static void *qemu_hax_cpu_thread_fn(void *arg)
         }
 
         qemu_wait_io_event(cpu);
-    }
+    } while (!cpu->unplug || cpu_can_run(cpu));
+    rcu_unregister_thread();
     return NULL;
 }
 
Signature · 1 line
2.14.3
#2 lore raw

PATCH 2/6 cpus: kvm: unregister thread with RCU

In reply to Paolo Bonzini
---
 cpus.c | 1 +
 1 file changed, 1 insertion(+)
cpus.c +1 -0
index 231550a0d8..789ff7897b 100644
@@ -1206,6 +1206,7 @@ static void *qemu_kvm_cpu_thread_fn(void *arg)
     cpu->created = false;
     qemu_cond_signal(&qemu_cpu_cond);
     qemu_mutex_unlock_iothread();
+    rcu_unregister_thread();
     return NULL;
 }
 
Signature · 1 line
2.14.3
#3 lore raw

PATCH 3/6 cpus: dummy: unregister thread with RCU, exit loop on unplug

In reply to Paolo Bonzini
---
 cpus.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
cpus.c +3 -2
index 789ff7897b..34c86192d1 100644
@@ -1235,7 +1235,7 @@ static void *qemu_dummy_cpu_thread_fn(void *arg)
     cpu->created = true;
     qemu_cond_signal(&qemu_cpu_cond);
 
-    while (1) {
+    do {
         qemu_mutex_unlock_iothread();
         do {
             int sig;
@@ -1247,8 +1247,9 @@ static void *qemu_dummy_cpu_thread_fn(void *arg)
         }
         qemu_mutex_lock_iothread();
         qemu_wait_io_event(cpu);
-    }
+    } while (!cpu->unplug);
 
+    rcu_unregister_thread();
     return NULL;
 #endif
 }
Signature · 1 line
2.14.3
#4 lore raw

PATCH 4/6 cpus: tcg: unregister thread with RCU, fix exiting of loop on unplug

In reply to Paolo Bonzini

Keep running until cpu_can_run(cpu) becomes false, for consistency with other acceslerators.

---
 cpus.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
cpus.c +7 -7
index 34c86192d1..9f48ccad9e 100644
@@ -1468,6 +1468,7 @@ static void *qemu_tcg_rr_cpu_thread_fn(void *arg)
         deal_with_unplugged_cpus();
     }
 
+    rcu_unregister_thread();
     return NULL;
 }
 
@@ -1644,18 +1645,17 @@ static void *qemu_tcg_cpu_thread_fn(void *arg)
                 /* Ignore everything else? */
                 break;
             }
-        } else if (cpu->unplug) {
-            qemu_tcg_destroy_vcpu(cpu);
-            cpu->created = false;
-            qemu_cond_signal(&qemu_cpu_cond);
-            qemu_mutex_unlock_iothread();
-            return NULL;
         }
 
         atomic_mb_set(&cpu->exit_request, 0);
         qemu_wait_io_event(cpu);
-    }
+    } while (!cpu->unplug || cpu_can_run(cpu));
 
+    qemu_tcg_destroy_vcpu(cpu);
+    cpu->created = false;
+    qemu_cond_signal(&qemu_cpu_cond);
+    qemu_mutex_unlock_iothread();
+    rcu_unregister_thread();
     return NULL;
 }
 
Signature · 1 line
2.14.3
#5 lore raw

Re: [Qemu-devel] [PATCH 4/6] cpus: tcg: unregister thread with RCU, fix exiting of loop on unplug

Quoted 2 lines · Paolo Bonzini

Keep running until cpu_can_run(cpu) becomes false, for consistency with other acceslerators.

s/acceslerators/accelerators/

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#6 lore raw

PATCH 5/6 cpus: hvf: unregister thread with RCU

In reply to Paolo Bonzini
---
 cpus.c | 1 +
 1 file changed, 1 insertion(+)
cpus.c +1 -0
index 9f48ccad9e..03b4533b7d 100644
@@ -1542,6 +1542,7 @@ static void *qemu_hvf_cpu_thread_fn(void *arg)
     cpu->created = false;
     qemu_cond_signal(&qemu_cpu_cond);
     qemu_mutex_unlock_iothread();
+    rcu_unregister_thread();
     return NULL;
 }
 
Signature · 1 line
2.14.3
#7 lore raw

PATCH 6/6 cpus: join thread when removing a vCPU

In reply to Paolo Bonzini

If no one joins the thread, its associated memory is leaked.

---
 cpus.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
cpus.c +4 -9
index 03b4533b7d..e8c3ba064c 100644
@@ -1795,19 +1795,14 @@ void resume_all_vcpus(void)
     }
 }
 
-void cpu_remove(CPUState *cpu)
+void cpu_remove_sync(CPUState *cpu)
 {
     cpu->stop = true;
     cpu->unplug = true;
     qemu_cpu_kick(cpu);
-}
-
-void cpu_remove_sync(CPUState *cpu)
-{
-    cpu_remove(cpu);
-    while (cpu->created) {
-        qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
-    }
+    qemu_mutex_unlock_iothread();
+    qemu_thread_join(cpu->thread);
+    qemu_mutex_lock_iothread();
 }
 
 /* For temporary buffers for forming a name */
Signature · 1 line
2.14.3