Received: from eggs.gnu.org ([2001:4830:134:3::10]:58959) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Vo2EG-0003Qq-RD for qemu-devel@nongnu.org; Tue, 03 Dec 2013 21:30:25 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Vo2EC-0006vz-74 for qemu-devel@nongnu.org; Tue, 03 Dec 2013 21:30:20 -0500 Received: from [222.73.24.84] (port=2431 helo=song.cn.fujitsu.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Vo2EB-0006ul-53 for qemu-devel@nongnu.org; Tue, 03 Dec 2013 21:30:16 -0500 Message-ID: <1386124097.21156.23.camel@G08FNSTD131468> From: Chen Fan Date: Wed, 04 Dec 2013 10:28:17 +0800 In-Reply-To: <20131128154849.45a36a87@nial.usersys.redhat.com> References: <6a9db256b60b3e06eeadac6e5c12bf5875d5a144.1381304905.git.chen.fan.fnst@cn.fujitsu.com> <20131128154849.45a36a87@nial.usersys.redhat.com> Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="UTF-8" Subject: Re: [Qemu-devel] [RFC qom-cpu v4 10/10] cpus: reclaim allocated vCPU objects List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Igor Mammedov Cc: qemu-devel@nongnu.org, Andreas =?ISO-8859-1?Q?F=E4rber?= On Thu, 2013-11-28 at 15:48 +0100, Igor Mammedov wrote: > On Wed, 9 Oct 2013 17:43:18 +0800 > Chen Fan wrote: >=20 > > After ACPI get a signal to eject a vCPU, then it will notify > > the vCPU thread to exit in KVM, and the vCPU must be removed from CPU l= ist=EF=BC=8C > > before the vCPU really removed, there will release the all related vCPU= objects. > >=20 > > Signed-off-by: Chen Fan > > --- > > cpus.c | 46 ++++++++++++++++++++++++++++++++++++++++++++= ++ > > hw/acpi/piix4.c | 23 +++++++++++++++++------ > > include/qom/cpu.h | 10 ++++++++++ > > include/sysemu/kvm.h | 1 + > > kvm-all.c | 25 +++++++++++++++++++++++++ > > 5 files changed, 99 insertions(+), 6 deletions(-) > >=20 > > diff --git a/cpus.c b/cpus.c > > index 4ace860..942af0a 100644 > > --- a/cpus.c > > +++ b/cpus.c > > @@ -714,6 +714,26 @@ void async_run_on_cpu(CPUState *cpu, void (*func)(= void *data), void *data) > > qemu_cpu_kick(cpu); > > } > > =20 > > +static void qemu_kvm_destroy_vcpu(CPUState *cpu) > > +{ > > + CPU_REMOVE(cpu); > > + > > + if (kvm_destroy_vcpu(cpu) < 0) { > Does it actually do what it's supposed to do? > If I recall correctly KVM patches were dimmed as not correct approach. >=20 > perhaps in case of KVM for now, it should return failure (i.e. no unplug > for KVM). yes, for now there must be to make some patches to release cpu for KVM. hoping KVM developers could give some advices. Thanks, Chen >=20 > > + fprintf(stderr, "kvm_destroy_vcpu failed.\n"); > > + exit(1); > > + } > > + > > + object_property_set_bool(OBJECT(cpu), false, "realized", NULL); > > + qdev_free(DEVICE(cpu)); > > +} > > + > > +static void qemu_tcg_destroy_vcpu(CPUState *cpu) > > +{ > > + CPU_REMOVE(cpu); > > + object_property_set_bool(OBJECT(cpu), false, "realized", NULL); > > + qdev_free(DEVICE(cpu)); > > +} > > + > > static void flush_queued_work(CPUState *cpu) > > { > > struct qemu_work_item *wi; > > @@ -805,6 +825,11 @@ static void *qemu_kvm_cpu_thread_fn(void *arg) > > } > > } > > qemu_kvm_wait_io_event(cpu); > > + if (cpu->exit && !cpu_can_run(cpu)) { > > + qemu_kvm_destroy_vcpu(cpu); > > + qemu_mutex_unlock(&qemu_global_mutex); > > + return NULL; > > + } > > } > > =20 > > return NULL; > > @@ -857,6 +882,7 @@ static void tcg_exec_all(void); > > static void *qemu_tcg_cpu_thread_fn(void *arg) > > { > > CPUState *cpu =3D arg; > > + CPUState *remove_cpu =3D NULL; > > =20 > > qemu_tcg_init_cpu_signals(); > > qemu_thread_get_self(cpu->thread); > > @@ -889,6 +915,16 @@ static void *qemu_tcg_cpu_thread_fn(void *arg) > > } > > } > > qemu_tcg_wait_io_event(); > > + CPU_FOREACH(cpu) { > > + if (cpu->exit && !cpu_can_run(cpu)) { > > + remove_cpu =3D cpu; > > + break; > > + } > > + } > > + if (remove_cpu) { > > + qemu_tcg_destroy_vcpu(remove_cpu); > > + remove_cpu =3D NULL; > > + } > > } > > =20 > > return NULL; > > @@ -1045,6 +1081,13 @@ void resume_all_vcpus(void) > > } > > } > > =20 > > +void cpu_remove(CPUState *cpu) > > +{ > > + cpu->stop =3D true; > > + cpu->exit =3D true; > > + qemu_cpu_kick(cpu); > > +} > > + > > static void qemu_tcg_init_vcpu(CPUState *cpu) > > { > > /* share a single thread for all cpus with TCG */ > > @@ -1219,6 +1262,9 @@ static void tcg_exec_all(void) > > break; > > } > > } else if (cpu->stop || cpu->stopped) { > > + if (cpu->exit) { > > + next_cpu =3D CPU_NEXT(cpu); > > + } > > break; > > } > > } > > diff --git a/hw/acpi/piix4.c b/hw/acpi/piix4.c > > index fd27001..bde8123 100644 > > --- a/hw/acpi/piix4.c > > +++ b/hw/acpi/piix4.c > > @@ -612,10 +612,21 @@ static const MemoryRegionOps piix4_pci_ops =3D { > > }, > > }; > > =20 > > -static void acpi_piix_eject_vcpu(int64_t cpuid) > > +static void acpi_piix_eject_vcpu(PIIX4PMState *s, int64_t cpuid) > > { > > - /* TODO: eject a vcpu, release allocated vcpu and exit the vcpu pt= hread. */ > > - PIIX4_DPRINTF("vcpu: %" PRIu64 " need to be ejected.\n", cpuid); > > + CPUStatus *g =3D &s->gpe_cpu; > > + CPUState *cpu; > > + > > + CPU_FOREACH(cpu) { > > + CPUClass *cc =3D CPU_GET_CLASS(cpu); > > + int64_t id =3D cc->get_arch_id(cpu); > > + > > + if (cpuid =3D=3D id) { > > + g->old_sts[cpuid / 8] &=3D ~(1 << (cpuid % 8)); > > + cpu_remove(cpu); > > + break; > > + } > > + } > > } > > =20 > > static uint64_t cpu_status_read(void *opaque, hwaddr addr, unsigned in= t size) > > @@ -634,7 +645,7 @@ static void cpu_status_write(void *opaque, hwaddr a= ddr, uint64_t data, > > CPUStatus *cpus =3D &s->gpe_cpu; > > uint8_t val; > > int i; > > - int64_t cpuid =3D 0; > > + int64_t cpuid =3D -1; > > =20 > > val =3D cpus->old_sts[addr] ^ data; > > =20 > > @@ -648,8 +659,8 @@ static void cpu_status_write(void *opaque, hwaddr a= ddr, uint64_t data, > > } > > } > > =20 > > - if (cpuid !=3D 0) { > > - acpi_piix_eject_vcpu(cpuid); > > + if (cpuid !=3D -1) { > > + acpi_piix_eject_vcpu(s, cpuid); > > } > > } > > =20 > > diff --git a/include/qom/cpu.h b/include/qom/cpu.h > > index 0238532..eb8d32b 100644 > > --- a/include/qom/cpu.h > > +++ b/include/qom/cpu.h > > @@ -181,6 +181,7 @@ struct CPUState { > > bool created; > > bool stop; > > bool stopped; > > + bool exit; > > volatile sig_atomic_t exit_request; > > volatile sig_atomic_t tcg_exit_req; > > uint32_t interrupt_request; > > @@ -206,6 +207,7 @@ struct CPUState { > > QTAILQ_HEAD(CPUTailQ, CPUState); > > extern struct CPUTailQ cpus; > > #define CPU_NEXT(cpu) QTAILQ_NEXT(cpu, node) > > +#define CPU_REMOVE(cpu) QTAILQ_REMOVE(&cpus, cpu, node) > > #define CPU_FOREACH(cpu) QTAILQ_FOREACH(cpu, &cpus, node) > > #define CPU_FOREACH_SAFE(cpu, next_cpu) \ > > QTAILQ_FOREACH_SAFE(cpu, &cpus, node, next_cpu) > > @@ -487,6 +489,14 @@ void cpu_exit(CPUState *cpu); > > void cpu_resume(CPUState *cpu); > > =20 > > /** > > + * qemu_remove_vcpu: > > + * @cpu: The vCPU to remove. > > + * > > + * Requests the CPU @cpu to be removed. > > + */ > > +void cpu_remove(CPUState *cpu); > > + > > +/** > > * qemu_init_vcpu: > > * @cpu: The vCPU to initialize. > > * > > diff --git a/include/sysemu/kvm.h b/include/sysemu/kvm.h > > index 3b25f27..f3f1279 100644 > > --- a/include/sysemu/kvm.h > > +++ b/include/sysemu/kvm.h > > @@ -167,6 +167,7 @@ int kvm_has_intx_set_mask(void); > > =20 > > int kvm_init_vcpu(CPUState *cpu); > > int kvm_cpu_exec(CPUState *cpu); > > +int kvm_destroy_vcpu(CPUState *cpu); > > =20 > > #ifdef NEED_CPU_H > > =20 > > diff --git a/kvm-all.c b/kvm-all.c > > index 4478969..605e00e 100644 > > --- a/kvm-all.c > > +++ b/kvm-all.c > > @@ -226,6 +226,31 @@ static void kvm_reset_vcpu(void *opaque) > > kvm_arch_reset_vcpu(cpu); > > } > > =20 > > +int kvm_destroy_vcpu(CPUState *cpu) > > +{ > > + KVMState *s =3D kvm_state; > > + long mmap_size; > > + int ret =3D 0; > > + > > + DPRINTF("kvm_destroy_vcpu\n"); > > + > > + mmap_size =3D kvm_ioctl(s, KVM_GET_VCPU_MMAP_SIZE, 0); > > + if (mmap_size < 0) { > > + ret =3D mmap_size; > > + DPRINTF("KVM_GET_VCPU_MMAP_SIZE failed\n"); > > + goto err; > > + } > > + > > + ret =3D munmap(cpu->kvm_run, mmap_size); > > + if (ret < 0) { > > + goto err; > > + } > > + > > + close(cpu->kvm_fd); > > +err: > > + return ret; > > +} > > + > > int kvm_init_vcpu(CPUState *cpu) > > { > > KVMState *s =3D kvm_state; >=20 >=20