From nobody Sat Oct 25 09:03:07 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1520309073055888.1858005931609; Mon, 5 Mar 2018 20:04:33 -0800 (PST) Received: from localhost ([::1]:52764 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1et3pz-000331-9Y for importer@patchew.org; Mon, 05 Mar 2018 23:04:27 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49242) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1et3nq-0001QJ-DK for qemu-devel@nongnu.org; Mon, 05 Mar 2018 23:02:16 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1et3no-0004Pc-Vg for qemu-devel@nongnu.org; Mon, 05 Mar 2018 23:02:14 -0500 Received: from ozlabs.org ([2401:3900:2:1::2]:32863) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1et3no-0004NS-Dr; Mon, 05 Mar 2018 23:02:12 -0500 Received: by ozlabs.org (Postfix, from userid 1007) id 3zwNQ242j6z9shB; Tue, 6 Mar 2018 15:02:01 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gibson.dropbear.id.au; s=201602; t=1520308922; bh=D4vz6xQXF7Qb4SSN2EPd+sEVNPtWxnByiqb86HmsZ5o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=K1nyfVJ8SvruRdHQCaDhmlBsV/Ks+Vmeq2BTlskcPJtFcOvUT5sJQyYmtnPbL45iM zokgbWvjGR8T6vM2fAT1HONkCZ+Nfiq4kVjhA7j4A8yPCr501FsnIQcRE9I6smgz2R okv1qa14Huf404wqtbBz7+Sm4t9Poy2hdNjuTMUY= From: David Gibson To: peter.maydell@linaro.org, groug@kaod.org Date: Tue, 6 Mar 2018 15:01:32 +1100 Message-Id: <20180306040154.3669-9-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180306040154.3669-1-david@gibson.dropbear.id.au> References: <20180306040154.3669-1-david@gibson.dropbear.id.au> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2401:3900:2:1::2 Subject: [Qemu-devel] [PULL 08/30] spapr: harden code that depends on VSMT X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: qemu-devel@nongnu.org, David Gibson , qemu-ppc@nongnu.org, agraf@suse.de, surajjs@au1.ibm.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Greg Kurz VSMT must be set in order to compute VCPU ids. This means that the following functions must not be called before spapr_set_vsmt_mode() was called: - spapr_vcpu_id() - spapr_is_thread0_in_vcore() - xics_max_server_number() We had a recent regression where the latter would be called before VSMT was set, and broke migration of some old machine types. This patch adds assert() in the above functions to avoid problems in the future. Also, since VSMT is really a CPU related thing, spapr_set_vsmt_mode() is now called from spapr_init_cpus(), just before the first VSMT user. Signed-off-by: Greg Kurz Signed-off-by: David Gibson --- hw/ppc/spapr.c | 144 ++++++++++++++++++++++++++++++-----------------------= ---- 1 file changed, 75 insertions(+), 69 deletions(-) diff --git a/hw/ppc/spapr.c b/hw/ppc/spapr.c index 3f1c5c5133..1c2703cb6b 100644 --- a/hw/ppc/spapr.c +++ b/hw/ppc/spapr.c @@ -105,12 +105,14 @@ */ static int spapr_vcpu_id(sPAPRMachineState *spapr, int cpu_index) { + assert(spapr->vsmt); return (cpu_index / smp_threads) * spapr->vsmt + cpu_index % smp_threads; } static bool spapr_is_thread0_in_vcore(sPAPRMachineState *spapr, PowerPCCPU *cpu) { + assert(spapr->vsmt); return spapr_get_vcpu_id(cpu) % spapr->vsmt =3D=3D 0; } =20 @@ -177,6 +179,7 @@ static void pre_2_10_vmstate_unregister_dummy_icp(int i) =20 static int xics_max_server_number(sPAPRMachineState *spapr) { + assert(spapr->vsmt); return DIV_ROUND_UP(max_cpus * spapr->vsmt, smp_threads); } =20 @@ -2220,73 +2223,6 @@ static CPUArchId *spapr_find_cpu_slot(MachineState *= ms, uint32_t id, int *idx) return &ms->possible_cpus->cpus[index]; } =20 -static void spapr_init_cpus(sPAPRMachineState *spapr) -{ - MachineState *machine =3D MACHINE(spapr); - MachineClass *mc =3D MACHINE_GET_CLASS(machine); - sPAPRMachineClass *smc =3D SPAPR_MACHINE_GET_CLASS(machine); - const char *type =3D spapr_get_cpu_core_type(machine->cpu_type); - const CPUArchIdList *possible_cpus; - int boot_cores_nr =3D smp_cpus / smp_threads; - int i; - - possible_cpus =3D mc->possible_cpu_arch_ids(machine); - if (mc->has_hotpluggable_cpus) { - if (smp_cpus % smp_threads) { - error_report("smp_cpus (%u) must be multiple of threads (%u)", - smp_cpus, smp_threads); - exit(1); - } - if (max_cpus % smp_threads) { - error_report("max_cpus (%u) must be multiple of threads (%u)", - max_cpus, smp_threads); - exit(1); - } - } else { - if (max_cpus !=3D smp_cpus) { - error_report("This machine version does not support CPU hotplu= g"); - exit(1); - } - boot_cores_nr =3D possible_cpus->len; - } - - if (smc->pre_2_10_has_unused_icps) { - int i; - - for (i =3D 0; i < xics_max_server_number(spapr); i++) { - /* Dummy entries get deregistered when real ICPState objects - * are registered during CPU core hotplug. - */ - pre_2_10_vmstate_register_dummy_icp(i); - } - } - - for (i =3D 0; i < possible_cpus->len; i++) { - int core_id =3D i * smp_threads; - - if (mc->has_hotpluggable_cpus) { - spapr_dr_connector_new(OBJECT(spapr), TYPE_SPAPR_DRC_CPU, - spapr_vcpu_id(spapr, core_id)); - } - - if (i < boot_cores_nr) { - Object *core =3D object_new(type); - int nr_threads =3D smp_threads; - - /* Handle the partially filled core for older machine types */ - if ((i + 1) * smp_threads >=3D smp_cpus) { - nr_threads =3D smp_cpus - i * smp_threads; - } - - object_property_set_int(core, nr_threads, "nr-threads", - &error_fatal); - object_property_set_int(core, core_id, CPU_CORE_PROP_CORE_ID, - &error_fatal); - object_property_set_bool(core, true, "realized", &error_fatal); - } - } -} - static void spapr_set_vsmt_mode(sPAPRMachineState *spapr, Error **errp) { Error *local_err =3D NULL; @@ -2359,6 +2295,78 @@ out: error_propagate(errp, local_err); } =20 +static void spapr_init_cpus(sPAPRMachineState *spapr) +{ + MachineState *machine =3D MACHINE(spapr); + MachineClass *mc =3D MACHINE_GET_CLASS(machine); + sPAPRMachineClass *smc =3D SPAPR_MACHINE_GET_CLASS(machine); + const char *type =3D spapr_get_cpu_core_type(machine->cpu_type); + const CPUArchIdList *possible_cpus; + int boot_cores_nr =3D smp_cpus / smp_threads; + int i; + + possible_cpus =3D mc->possible_cpu_arch_ids(machine); + if (mc->has_hotpluggable_cpus) { + if (smp_cpus % smp_threads) { + error_report("smp_cpus (%u) must be multiple of threads (%u)", + smp_cpus, smp_threads); + exit(1); + } + if (max_cpus % smp_threads) { + error_report("max_cpus (%u) must be multiple of threads (%u)", + max_cpus, smp_threads); + exit(1); + } + } else { + if (max_cpus !=3D smp_cpus) { + error_report("This machine version does not support CPU hotplu= g"); + exit(1); + } + boot_cores_nr =3D possible_cpus->len; + } + + /* VSMT must be set in order to be able to compute VCPU ids, ie to + * call xics_max_server_number() or spapr_vcpu_id(). + */ + spapr_set_vsmt_mode(spapr, &error_fatal); + + if (smc->pre_2_10_has_unused_icps) { + int i; + + for (i =3D 0; i < xics_max_server_number(spapr); i++) { + /* Dummy entries get deregistered when real ICPState objects + * are registered during CPU core hotplug. + */ + pre_2_10_vmstate_register_dummy_icp(i); + } + } + + for (i =3D 0; i < possible_cpus->len; i++) { + int core_id =3D i * smp_threads; + + if (mc->has_hotpluggable_cpus) { + spapr_dr_connector_new(OBJECT(spapr), TYPE_SPAPR_DRC_CPU, + spapr_vcpu_id(spapr, core_id)); + } + + if (i < boot_cores_nr) { + Object *core =3D object_new(type); + int nr_threads =3D smp_threads; + + /* Handle the partially filled core for older machine types */ + if ((i + 1) * smp_threads >=3D smp_cpus) { + nr_threads =3D smp_cpus - i * smp_threads; + } + + object_property_set_int(core, nr_threads, "nr-threads", + &error_fatal); + object_property_set_int(core, core_id, CPU_CORE_PROP_CORE_ID, + &error_fatal); + object_property_set_bool(core, true, "realized", &error_fatal); + } + } +} + /* pSeries LPAR / sPAPR hardware init */ static void spapr_machine_init(MachineState *machine) { @@ -2486,8 +2494,6 @@ static void spapr_machine_init(MachineState *machine) } =20 /* init CPUs */ - spapr_set_vsmt_mode(spapr, &error_fatal); - spapr_init_cpus(spapr); =20 if (kvm_enabled()) { --=20 2.14.3