qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH 0/6] cpus: fix various issues with vCPU unplug
@ 2018-02-01 14:44 Paolo Bonzini
  2018-02-01 14:44 ` [Qemu-devel] [PATCH 1/6] cpus: hax: register/unregister thread with RCU, exit loop on unplug Paolo Bonzini
                   ` (5 more replies)
  0 siblings, 6 replies; 8+ messages in thread
From: Paolo Bonzini @ 2018-02-01 14:44 UTC (permalink / raw)
  To: qemu-devel; +Cc: linzc

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(-)

-- 
2.14.3

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Qemu-devel] [PATCH 1/6] cpus: hax: register/unregister thread with RCU, exit loop on unplug
  2018-02-01 14:44 [Qemu-devel] [PATCH 0/6] cpus: fix various issues with vCPU unplug Paolo Bonzini
@ 2018-02-01 14:44 ` Paolo Bonzini
  2018-02-01 14:44 ` [Qemu-devel] [PATCH 2/6] cpus: kvm: unregister thread with RCU Paolo Bonzini
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: Paolo Bonzini @ 2018-02-01 14:44 UTC (permalink / raw)
  To: qemu-devel; +Cc: linzc

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 cpus.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/cpus.c b/cpus.c
index 1495ed5c36..231550a0d8 100644
--- a/cpus.c
+++ b/cpus.c
@@ -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;
 }
 
-- 
2.14.3

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [Qemu-devel] [PATCH 2/6] cpus: kvm: unregister thread with RCU
  2018-02-01 14:44 [Qemu-devel] [PATCH 0/6] cpus: fix various issues with vCPU unplug Paolo Bonzini
  2018-02-01 14:44 ` [Qemu-devel] [PATCH 1/6] cpus: hax: register/unregister thread with RCU, exit loop on unplug Paolo Bonzini
@ 2018-02-01 14:44 ` Paolo Bonzini
  2018-02-01 14:44 ` [Qemu-devel] [PATCH 3/6] cpus: dummy: unregister thread with RCU, exit loop on unplug Paolo Bonzini
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: Paolo Bonzini @ 2018-02-01 14:44 UTC (permalink / raw)
  To: qemu-devel; +Cc: linzc

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 cpus.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/cpus.c b/cpus.c
index 231550a0d8..789ff7897b 100644
--- a/cpus.c
+++ b/cpus.c
@@ -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;
 }
 
-- 
2.14.3

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [Qemu-devel] [PATCH 3/6] cpus: dummy: unregister thread with RCU, exit loop on unplug
  2018-02-01 14:44 [Qemu-devel] [PATCH 0/6] cpus: fix various issues with vCPU unplug Paolo Bonzini
  2018-02-01 14:44 ` [Qemu-devel] [PATCH 1/6] cpus: hax: register/unregister thread with RCU, exit loop on unplug Paolo Bonzini
  2018-02-01 14:44 ` [Qemu-devel] [PATCH 2/6] cpus: kvm: unregister thread with RCU Paolo Bonzini
@ 2018-02-01 14:44 ` Paolo Bonzini
  2018-02-01 14:44 ` [Qemu-devel] [PATCH 4/6] cpus: tcg: unregister thread with RCU, fix exiting of " Paolo Bonzini
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: Paolo Bonzini @ 2018-02-01 14:44 UTC (permalink / raw)
  To: qemu-devel; +Cc: linzc

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 cpus.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/cpus.c b/cpus.c
index 789ff7897b..34c86192d1 100644
--- a/cpus.c
+++ b/cpus.c
@@ -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
 }
-- 
2.14.3

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [Qemu-devel] [PATCH 4/6] cpus: tcg: unregister thread with RCU, fix exiting of loop on unplug
  2018-02-01 14:44 [Qemu-devel] [PATCH 0/6] cpus: fix various issues with vCPU unplug Paolo Bonzini
                   ` (2 preceding siblings ...)
  2018-02-01 14:44 ` [Qemu-devel] [PATCH 3/6] cpus: dummy: unregister thread with RCU, exit loop on unplug Paolo Bonzini
@ 2018-02-01 14:44 ` Paolo Bonzini
  2018-02-01 15:05   ` Eric Blake
  2018-02-01 14:44 ` [Qemu-devel] [PATCH 5/6] cpus: hvf: unregister thread with RCU Paolo Bonzini
  2018-02-01 14:44 ` [Qemu-devel] [PATCH 6/6] cpus: join thread when removing a vCPU Paolo Bonzini
  5 siblings, 1 reply; 8+ messages in thread
From: Paolo Bonzini @ 2018-02-01 14:44 UTC (permalink / raw)
  To: qemu-devel; +Cc: linzc

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

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 cpus.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/cpus.c b/cpus.c
index 34c86192d1..9f48ccad9e 100644
--- a/cpus.c
+++ b/cpus.c
@@ -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;
 }
 
-- 
2.14.3

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [Qemu-devel] [PATCH 5/6] cpus: hvf: unregister thread with RCU
  2018-02-01 14:44 [Qemu-devel] [PATCH 0/6] cpus: fix various issues with vCPU unplug Paolo Bonzini
                   ` (3 preceding siblings ...)
  2018-02-01 14:44 ` [Qemu-devel] [PATCH 4/6] cpus: tcg: unregister thread with RCU, fix exiting of " Paolo Bonzini
@ 2018-02-01 14:44 ` Paolo Bonzini
  2018-02-01 14:44 ` [Qemu-devel] [PATCH 6/6] cpus: join thread when removing a vCPU Paolo Bonzini
  5 siblings, 0 replies; 8+ messages in thread
From: Paolo Bonzini @ 2018-02-01 14:44 UTC (permalink / raw)
  To: qemu-devel; +Cc: linzc

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 cpus.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/cpus.c b/cpus.c
index 9f48ccad9e..03b4533b7d 100644
--- a/cpus.c
+++ b/cpus.c
@@ -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;
 }
 
-- 
2.14.3

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [Qemu-devel] [PATCH 6/6] cpus: join thread when removing a vCPU
  2018-02-01 14:44 [Qemu-devel] [PATCH 0/6] cpus: fix various issues with vCPU unplug Paolo Bonzini
                   ` (4 preceding siblings ...)
  2018-02-01 14:44 ` [Qemu-devel] [PATCH 5/6] cpus: hvf: unregister thread with RCU Paolo Bonzini
@ 2018-02-01 14:44 ` Paolo Bonzini
  5 siblings, 0 replies; 8+ messages in thread
From: Paolo Bonzini @ 2018-02-01 14:44 UTC (permalink / raw)
  To: qemu-devel; +Cc: linzc

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

Reported-by: CheneyLin <linzc@zju.edu.cn>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 cpus.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)

diff --git a/cpus.c b/cpus.c
index 03b4533b7d..e8c3ba064c 100644
--- a/cpus.c
+++ b/cpus.c
@@ -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 */
-- 
2.14.3

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [Qemu-devel] [PATCH 4/6] cpus: tcg: unregister thread with RCU, fix exiting of loop on unplug
  2018-02-01 14:44 ` [Qemu-devel] [PATCH 4/6] cpus: tcg: unregister thread with RCU, fix exiting of " Paolo Bonzini
@ 2018-02-01 15:05   ` Eric Blake
  0 siblings, 0 replies; 8+ messages in thread
From: Eric Blake @ 2018-02-01 15:05 UTC (permalink / raw)
  To: Paolo Bonzini, qemu-devel; +Cc: linzc

[-- Attachment #1: Type: text/plain, Size: 310 bytes --]

On 02/01/2018 08:44 AM, Paolo Bonzini wrote:
> Keep running until cpu_can_run(cpu) becomes false, for consistency
> with other acceslerators.

s/acceslerators/accelerators/

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 619 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2018-02-01 15:05 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-02-01 14:44 [Qemu-devel] [PATCH 0/6] cpus: fix various issues with vCPU unplug Paolo Bonzini
2018-02-01 14:44 ` [Qemu-devel] [PATCH 1/6] cpus: hax: register/unregister thread with RCU, exit loop on unplug Paolo Bonzini
2018-02-01 14:44 ` [Qemu-devel] [PATCH 2/6] cpus: kvm: unregister thread with RCU Paolo Bonzini
2018-02-01 14:44 ` [Qemu-devel] [PATCH 3/6] cpus: dummy: unregister thread with RCU, exit loop on unplug Paolo Bonzini
2018-02-01 14:44 ` [Qemu-devel] [PATCH 4/6] cpus: tcg: unregister thread with RCU, fix exiting of " Paolo Bonzini
2018-02-01 15:05   ` Eric Blake
2018-02-01 14:44 ` [Qemu-devel] [PATCH 5/6] cpus: hvf: unregister thread with RCU Paolo Bonzini
2018-02-01 14:44 ` [Qemu-devel] [PATCH 6/6] cpus: join thread when removing a vCPU Paolo Bonzini

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).