From nobody Fri Oct 24 20:19:13 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; 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 1519745377367683.7053812782433; Tue, 27 Feb 2018 07:29:37 -0800 (PST) Received: from localhost ([::1]:38054 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eqhC7-0006pI-5t for importer@patchew.org; Tue, 27 Feb 2018 10:29:31 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40467) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eqh64-0008A0-Ta for qemu-devel@nongnu.org; Tue, 27 Feb 2018 10:23:23 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eqh62-0000YP-8i for qemu-devel@nongnu.org; Tue, 27 Feb 2018 10:23:16 -0500 Received: from 8.mo179.mail-out.ovh.net ([46.105.75.26]:57736) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eqh61-0000Xn-UP for qemu-devel@nongnu.org; Tue, 27 Feb 2018 10:23:14 -0500 Received: from player729.ha.ovh.net (b9.ovh.net [213.186.33.59]) by mo179.mail-out.ovh.net (Postfix) with ESMTP id 151A3A57E5 for ; Tue, 27 Feb 2018 16:23:11 +0100 (CET) Received: from bahia.lan (lns-bzn-46-82-253-208-248.adsl.proxad.net [82.253.208.248]) (Authenticated sender: groug@kaod.org) by player729.ha.ovh.net (Postfix) with ESMTPA id 98A545E00A7; Tue, 27 Feb 2018 16:23:07 +0100 (CET) From: Greg Kurz To: qemu-devel@nongnu.org Date: Tue, 27 Feb 2018 16:23:07 +0100 Message-ID: <151974498751.17040.3762718971248982609.stgit@bahia.lan> In-Reply-To: <151974496089.17040.8929640429507724311.stgit@bahia.lan> References: <151974496089.17040.8929640429507724311.stgit@bahia.lan> User-Agent: StGit/0.17.1-46-g6855-dirty MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Ovh-Tracer-Id: 8637622610592897318 X-VR-SPAMSTATE: OK X-VR-SPAMSCORE: -100 X-VR-SPAMCAUSE: gggruggvucftvghtrhhoucdtuddrgedtfedrieefgdeijecutefuodetggdotefrodftvfcurfhrohhfihhlvgemucfqggfjpdevjffgvefmvefgnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 46.105.75.26 Subject: [Qemu-devel] [PATCH 2/2] 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: Laurent Vivier , Lukas Doktor , qemu-ppc@nongnu.org, David Gibson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 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 --- 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 ae411057272f..62081f68740e 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()) {