From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:39893) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YGeDy-0000dE-Hp for qemu-devel@nongnu.org; Wed, 28 Jan 2015 20:48:52 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YGeDu-0000ZI-Fe for qemu-devel@nongnu.org; Wed, 28 Jan 2015 20:48:50 -0500 Received: from ozlabs.org ([103.22.144.67]:59151) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YGeDt-0000YM-TY for qemu-devel@nongnu.org; Wed, 28 Jan 2015 20:48:46 -0500 Date: Thu, 29 Jan 2015 12:48:39 +1100 From: David Gibson Message-ID: <20150129014839.GT14681@voom> References: <1420697420-16053-1-git-send-email-bharata@linux.vnet.ibm.com> <1420697420-16053-11-git-send-email-bharata@linux.vnet.ibm.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="TVVcQco/7vcH19KK" Content-Disposition: inline In-Reply-To: <1420697420-16053-11-git-send-email-bharata@linux.vnet.ibm.com> Subject: Re: [Qemu-devel] [RFC PATCH v1 10/13] cpus, spapr: reclaim allocated vCPU objects List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Bharata B Rao Cc: Gu Zheng , imammedo@redhat.com, agraf@suse.de, qemu-devel@nongnu.org, mdroth@linux.vnet.ibm.com --TVVcQco/7vcH19KK Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Thu, Jan 08, 2015 at 11:40:17AM +0530, Bharata B Rao wrote: > From: Gu Zheng This needs a commit message, it's not at all clear from the 1-line descript= ion. >=20 > Signed-off-by: Gu Zheng > Signed-off-by: Bharata B Rao > (added spapr bits) > --- > cpus.c | 44 ++++++++++++++++++++++++++++++++++++++++ > hw/ppc/spapr.c | 14 ++++++++++++- > include/qom/cpu.h | 11 ++++++++++ > include/sysemu/kvm.h | 1 + > kvm-all.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++= +++++- > 5 files changed, 125 insertions(+), 2 deletions(-) The generic and PAPR specific parts should probably be divided into different patches, since they'll want to go via different trees. > diff --git a/cpus.c b/cpus.c > index 1b5168a..98b7199 100644 > --- a/cpus.c > +++ b/cpus.c > @@ -871,6 +871,24 @@ void async_run_on_cpu(CPUState *cpu, void (*func)(vo= id *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) { > + fprintf(stderr, "kvm_destroy_vcpu failed.\n"); > + exit(1); > + } > + > + object_unparent(OBJECT(cpu)); > +} > + > +static void qemu_tcg_destroy_vcpu(CPUState *cpu) > +{ > + CPU_REMOVE(cpu); > + object_unparent(OBJECT(cpu)); > +} > + > static void flush_queued_work(CPUState *cpu) > { > struct qemu_work_item *wi; > @@ -964,6 +982,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; > @@ -1018,6 +1041,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); > @@ -1052,6 +1076,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; > @@ -1208,6 +1242,13 @@ void resume_all_vcpus(void) > } > } > =20 > +void cpu_remove(CPUState *cpu) > +{ > + cpu->stop =3D true; > + cpu->exit =3D true; > + qemu_cpu_kick(cpu); > +} > + > /* For temporary buffers for forming a name */ > #define VCPU_THREAD_NAME_SIZE 16 > =20 > @@ -1402,6 +1443,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/ppc/spapr.c b/hw/ppc/spapr.c > index ec793b1..44405b2 100644 > --- a/hw/ppc/spapr.c > +++ b/hw/ppc/spapr.c > @@ -1910,7 +1910,19 @@ static void spapr_cpu_hotplug_add(DeviceState *dev= , CPUState *cs) > =20 > static void spapr_cpu_release(DeviceState *dev, void *opaque) > { > - /* Release vCPU */ > + CPUState *cs; > + int i; > + int id =3D ppc_get_vcpu_dt_id(POWERPC_CPU(CPU(dev))); > + > + for (i =3D id; i < id + smp_threads; i++) { > + CPU_FOREACH(cs) { > + PowerPCCPU *cpu =3D POWERPC_CPU(cs); > + > + if (i =3D=3D ppc_get_vcpu_dt_id(cpu)) { > + cpu_remove(cs); > + } > + } > + } > } > =20 > static void spapr_cpu_hotplug_remove(DeviceState *dev, CPUState *cs) > diff --git a/include/qom/cpu.h b/include/qom/cpu.h > index 2098f1c..30fd0cd 100644 > --- a/include/qom/cpu.h > +++ b/include/qom/cpu.h > @@ -206,6 +206,7 @@ struct kvm_run; > * @halted: Nonzero if the CPU is in suspended state. > * @stop: Indicates a pending stop request. > * @stopped: Indicates the CPU has been artificially stopped. > + * @exit: Indicates the CPU has exited due to an unplug operation. > * @tcg_exit_req: Set to force TCG to stop executing linked TBs for this > * CPU and return to its top level loop. > * @singlestep_enabled: Flags for single-stepping. > @@ -249,6 +250,7 @@ struct CPUState { > bool created; > bool stop; > bool stopped; > + bool exit; > volatile sig_atomic_t exit_request; > uint32_t interrupt_request; > int singlestep_enabled; > @@ -305,6 +307,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) > @@ -610,6 +613,14 @@ void cpu_exit(CPUState *cpu); > void cpu_resume(CPUState *cpu); > =20 > /** > + * cpu_remove: > + * @cpu: The CPU to remove. > + * > + * Requests the 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 104cf35..da064c1 100644 > --- a/include/sysemu/kvm.h > +++ b/include/sysemu/kvm.h > @@ -186,6 +186,7 @@ int kvm_has_gsi_routing(void); > int kvm_has_intx_set_mask(void); > =20 > int kvm_init_vcpu(CPUState *cpu); > +int kvm_destroy_vcpu(CPUState *cpu); > int kvm_cpu_exec(CPUState *cpu); > =20 > #ifdef NEED_CPU_H > diff --git a/kvm-all.c b/kvm-all.c > index 18cc6b4..6f543ce 100644 > --- a/kvm-all.c > +++ b/kvm-all.c > @@ -71,6 +71,12 @@ typedef struct KVMSlot > =20 > typedef struct kvm_dirty_log KVMDirtyLog; > =20 > +struct KVMParkedVcpu { > + unsigned long vcpu_id; > + int kvm_fd; > + QLIST_ENTRY(KVMParkedVcpu) node; > +}; > + > struct KVMState > { > AccelState parent_obj; > @@ -107,6 +113,7 @@ struct KVMState > QTAILQ_HEAD(msi_hashtab, KVMMSIRoute) msi_hashtab[KVM_MSI_HASHTAB_SI= ZE]; > bool direct_msi; > #endif > + QLIST_HEAD(, KVMParkedVcpu) kvm_parked_vcpus; > }; > =20 > #define TYPE_KVM_ACCEL ACCEL_CLASS_NAME("kvm") > @@ -247,6 +254,53 @@ static int kvm_set_user_memory_region(KVMState *s, K= VMSlot *slot) > return kvm_vm_ioctl(s, KVM_SET_USER_MEMORY_REGION, &mem); > } > =20 > +int kvm_destroy_vcpu(CPUState *cpu) > +{ > + KVMState *s =3D kvm_state; > + long mmap_size; > + struct KVMParkedVcpu *vcpu =3D NULL; > + 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_destroy_vcpu failed\n"); > + goto err; > + } > + > + ret =3D munmap(cpu->kvm_run, mmap_size); > + if (ret < 0) { > + goto err; > + } > + > + vcpu =3D g_malloc0(sizeof(*vcpu)); > + vcpu->vcpu_id =3D kvm_arch_vcpu_id(cpu); > + vcpu->kvm_fd =3D cpu->kvm_fd; > + QLIST_INSERT_HEAD(&kvm_state->kvm_parked_vcpus, vcpu, node); What's the reason for parking vcpus rather than removing / recreating them at the kvm level? > + > +err: > + return ret; > +} > + > +static int kvm_get_vcpu(KVMState *s, unsigned long vcpu_id) > +{ > + struct KVMParkedVcpu *cpu; > + > + QLIST_FOREACH(cpu, &s->kvm_parked_vcpus, node) { > + if (cpu->vcpu_id =3D=3D vcpu_id) { > + int kvm_fd; > + > + QLIST_REMOVE(cpu, node); > + kvm_fd =3D cpu->kvm_fd; > + g_free(cpu); > + return kvm_fd; > + } > + } > + > + return kvm_vm_ioctl(s, KVM_CREATE_VCPU, (void *)vcpu_id); > +} > int kvm_init_vcpu(CPUState *cpu) > { > KVMState *s =3D kvm_state; > @@ -255,7 +309,7 @@ int kvm_init_vcpu(CPUState *cpu) > =20 > DPRINTF("kvm_init_vcpu\n"); > =20 > - ret =3D kvm_vm_ioctl(s, KVM_CREATE_VCPU, (void *)kvm_arch_vcpu_id(cp= u)); > + ret =3D kvm_get_vcpu(s, kvm_arch_vcpu_id(cpu)); > if (ret < 0) { > DPRINTF("kvm_create_vcpu failed\n"); > goto err; > @@ -1441,6 +1495,7 @@ static int kvm_init(MachineState *ms) > #ifdef KVM_CAP_SET_GUEST_DEBUG > QTAILQ_INIT(&s->kvm_sw_breakpoints); > #endif > + QLIST_INIT(&s->kvm_parked_vcpus); > s->vmfd =3D -1; > s->fd =3D qemu_open("/dev/kvm", O_RDWR); > if (s->fd =3D=3D -1) { --=20 David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson --TVVcQco/7vcH19KK Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1 iQIcBAEBAgAGBQJUyZF3AAoJEGw4ysog2bOSt4QP/3T51ROPK7ddxPD2OHSCUkKn wgTbGV0Zw4I0JIa4OZMvHctMMxNmoKyFX/vZycx1MiSr6Nrix0X7NtdbFk+R5u5v HdyYcKWeCZfAWLUFCHAc72IQ+s+abgztjLDQaJ8Rti2rC7VQQaW7OzGw3hY/++kS aLUPSsMJnBJeqKU/HxWUrpwVKkgx+K1MhuIjJs9p1jKp7k5lq/VVBmA5QIp3wpgb zImv3BozoFdHCkdxvcxgb2Cu/yZweTsi2FjyzupsGltYoTsmLzGov7Xa8wAqihkc PVt4pMeTgyCzISDZjzpFhokgK6rA5CqHPkICvDnX5StbobJ8B8ZJK/lzlbjLV8x2 h6l9RhvNa0w4tfxeMZxUaDMFyE0ACbnyXfBIWX2TCSYAiRKCCcW+QsSP3ttJXDyY F+zZJ98SLyyUrGs3rnf3ETQWKyaQ5vyXmzXU+7470iLREstVvLDPjdfJVVSkwrxF NZ7tuKPu1CL49ZZ5FOa6HRld1wZBZj0Gv4tT6CC7FIldQ/G84fWrkxDObzJIMtT0 hwB7bqdQO6qBZvVr7ObpV5fG3tD0jvxgVdBtSbQvNVwKSHQ9lv8u13s5mVihb74C Lxdrt8VNxY7D/LBLyImODJH1B3ZK2a9vWAMewNH+4ChgBfyucne04Wl2ERFvyYrI bcb7r4Awksmn7sf36OQf =MCL0 -----END PGP SIGNATURE----- --TVVcQco/7vcH19KK--