[Qemu-devel] [PATCH v2] vcpu: join vcpu thread after it exiting

linzhecheng posted 1 patch 6 years, 2 months ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/1517483068-18735-1-git-send-email-linzc@zju.edu.cn
Test checkpatch passed
Test docker-build@min-glib passed
Test docker-mingw@fedora passed
Test docker-quick@centos6 passed
Test ppc passed
Test s390x passed
[Qemu-devel] [PATCH v2] vcpu: join vcpu thread after it exiting
Posted by linzhecheng 6 years, 2 months ago
As we create vcpu thread with QEMU_THREAD_JOINABLE mode,
we should join it after it exiting to cleanup resources.

Signed-off-by: linzhecheng <linzhecheng@huawei.com>

diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c
index f290f48..5cc1ba2 100644
--- a/accel/kvm/kvm-all.c
+++ b/accel/kvm/kvm-all.c
@@ -282,9 +282,9 @@ err:
 
 static int kvm_get_vcpu(KVMState *s, unsigned long vcpu_id)
 {
-    struct KVMParkedVcpu *cpu;
+    struct KVMParkedVcpu *cpu, *next_cpu;
 
-    QLIST_FOREACH(cpu, &s->kvm_parked_vcpus, node) {
+    QLIST_FOREACH_SAFE(cpu, &s->kvm_parked_vcpus, node, next_cpu) {
         if (cpu->vcpu_id == vcpu_id) {
             int kvm_fd;
 
diff --git a/cpus.c b/cpus.c
index 2cb0af9..1890bfe 100644
--- a/cpus.c
+++ b/cpus.c
@@ -1205,6 +1205,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;
 }
 
@@ -1759,6 +1760,7 @@ void cpu_remove_sync(CPUState *cpu)
     cpu_remove(cpu);
     while (cpu->created) {
         qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
+        qemu_thread_join(cpu->thread);
     }
 }
 
-- 
1.8.3.1


Re: [Qemu-devel] [PATCH v2] vcpu: join vcpu thread after it exiting
Posted by Paolo Bonzini 6 years, 2 months ago
On 01/02/2018 06:04, linzhecheng wrote:
> As we create vcpu thread with QEMU_THREAD_JOINABLE mode,
> we should join it after it exiting to cleanup resources.
> 
> Signed-off-by: linzhecheng <linzhecheng@huawei.com>
> 
> diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c
> index f290f48..5cc1ba2 100644
> --- a/accel/kvm/kvm-all.c
> +++ b/accel/kvm/kvm-all.c
> @@ -282,9 +282,9 @@ err:
>  
>  static int kvm_get_vcpu(KVMState *s, unsigned long vcpu_id)
>  {
> -    struct KVMParkedVcpu *cpu;
> +    struct KVMParkedVcpu *cpu, *next_cpu;
>  
> -    QLIST_FOREACH(cpu, &s->kvm_parked_vcpus, node) {
> +    QLIST_FOREACH_SAFE(cpu, &s->kvm_parked_vcpus, node, next_cpu) {
>          if (cpu->vcpu_id == vcpu_id) {
>              int kvm_fd;

This is not needed because removing the node results in an immediate
return..

> diff --git a/cpus.c b/cpus.c
> index 2cb0af9..1890bfe 100644
> --- a/cpus.c
> +++ b/cpus.c
> @@ -1205,6 +1205,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;
>  }
>  
> @@ -1759,6 +1760,7 @@ void cpu_remove_sync(CPUState *cpu)
>      cpu_remove(cpu);
>      while (cpu->created) {
>          qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
> +        qemu_thread_join(cpu->thread);
>      }
>  }
>  
> 

Thanks.  There are other issues in qemu_*_cpu_thread_fn, I'll send a
more complete series shortly.

Paolo