From nobody Sat Feb 7 06:12:46 2026 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 (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 151863759929643.39395280037593; Wed, 14 Feb 2018 11:46:39 -0800 (PST) Received: from localhost ([::1]:53699 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1em30j-00084V-9F for importer@patchew.org; Wed, 14 Feb 2018 14:46:33 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:52089) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1em2uz-00036Q-LJ for qemu-devel@nongnu.org; Wed, 14 Feb 2018 14:40:39 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1em2uu-0002sx-Re for qemu-devel@nongnu.org; Wed, 14 Feb 2018 14:40:37 -0500 Received: from 7.mo179.mail-out.ovh.net ([46.105.61.94]:44031) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1em2uu-0002s6-4X for qemu-devel@nongnu.org; Wed, 14 Feb 2018 14:40:32 -0500 Received: from player168.ha.ovh.net (b7.ovh.net [213.186.33.57]) by mo179.mail-out.ovh.net (Postfix) with ESMTP id 8DAAAA345E for ; Wed, 14 Feb 2018 20:40:30 +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 player168.ha.ovh.net (Postfix) with ESMTPA id 4634A420084; Wed, 14 Feb 2018 20:40:26 +0100 (CET) From: Greg Kurz To: qemu-devel@nongnu.org Date: Wed, 14 Feb 2018 20:40:26 +0100 Message-ID: <151863722617.3003.10199760495589842414.stgit@bahia.lan> In-Reply-To: <151863720814.3003.4939908778788942974.stgit@bahia.lan> References: <151863720814.3003.4939908778788942974.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: 10425270188665116966 X-VR-SPAMSTATE: OK X-VR-SPAMSCORE: -100 X-VR-SPAMCAUSE: gggruggvucftvghtrhhoucdtuddrgedtfedrfedtgdduvdekucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuqfggjfdpvefjgfevmfevgfenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddm X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 46.105.61.94 Subject: [Qemu-devel] [PATCH 1/5] spapr: use spapr->vsmt to compute VCPU ids 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 , qemu-ppc@nongnu.org, Sam Bobroff , David Gibson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Since the introduction of VSMT in 2.11, the spacing of VCPU ids between cores is controllable through a machine property instead of being only dictated by the SMT mode of the host: cpu->vcpu_id =3D (cc->core_id * spapr->vsmt / smp_threads) + i Until recently, the machine code would try to change the SMT mode of the host to be equal to VSMT or exit. This allowed the rest of the code to assume that kvmppc_smt_threads() =3D=3D spapr->vsmt is always true. Recent commit "8904e5a75005 spapr: Adjust default VSMT value for better migration compatibility" relaxed the rule. If the VSMT mode cannot be set in KVM for some reasons, but the requested CPU topology is compatible with the current SMT mode, then we let the guest run with kvmppc_smt_threads() !=3D spapr->vsmt. This breaks quite a few places in the code, in particular when calculating DRC indexes. This is what happens on a POWER host with subcores-per-core=3D2 (ie, supports up to SMT4) when passing the following topology: -smp threads=3D4,maxcpus=3D16 \ -device host-spapr-cpu-core,core-id=3D4,id=3Dcore1 \ -device host-spapr-cpu-core,core-id=3D8,id=3Dcore2 qemu-system-ppc64: warning: Failed to set KVM's VSMT mode to 8 (errno -22) This is expected since KVM is limited to SMT4, but the guest is started anyway because this topology can run on SMT4 even with a VSMT8 spacing. But when we look at the DT, things get nastier: cpus { ... ibm,drc-indexes =3D <0x4 0x10000000 0x10000004 0x10000008 0x1000000= c>; This means that we have the following association: CPU core device | DRC | VCPU id -----------------+------------+--------- boot core | 0x10000000 | 0 core1 | 0x10000004 | 4 core2 | 0x10000008 | 8 core3 | 0x1000000c | 12 But since the spacing of VCPU ids is 8, the DRC for core1 points to a VCPU that doesn't exist, the DRC for core2 points to the first VCPU of core1 and and so on... ... PowerPC,POWER8@0 { ... ibm,my-drc-index =3D <0x10000000>; ... }; PowerPC,POWER8@8 { ... ibm,my-drc-index =3D <0x10000008>; ... }; PowerPC,POWER8@10 { ... No ibm,my-drc-index property for this core since 0x10000010 doesn't exist in ibm,drc-indexes above. ... }; }; ... interrupt-controller { ... ibm,interrupt-server-ranges =3D <0x0 0x10>; With a spacing of 8, the highest VCPU id for the given topology should be: 16 * 8 / 4 =3D 32 and not 16 ... linux,phandle =3D <0x7e7323b8>; interrupt-controller; }; And CPU hot-plug/unplug is broken: (qemu) device_del core1 pseries-hotplug-cpu: Cannot find CPU (drc index 10000004) to remove (qemu) device_del core2 cpu 4 (hwid 8) Ready to die... cpu 5 (hwid 9) Ready to die... cpu 6 (hwid 10) Ready to die... cpu 7 (hwid 11) Ready to die... These are the VCPU ids of core1 actually (qemu) device_add host-spapr-cpu-core,core-id=3D12,id=3Dcore3 (qemu) device_del core3 pseries-hotplug-cpu: Cannot find CPU (drc index 1000000c) to remove This patches all the code in hw/ppc/spapr.c to assume the VSMT spacing when manipulating VCPU ids. Fixes: 8904e5a75005 Signed-off-by: Greg Kurz --- hw/ppc/spapr.c | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/hw/ppc/spapr.c b/hw/ppc/spapr.c index 9f29434819bd..ea7429c92a97 100644 --- a/hw/ppc/spapr.c +++ b/hw/ppc/spapr.c @@ -160,9 +160,9 @@ static void pre_2_10_vmstate_unregister_dummy_icp(int i) (void *)(uintptr_t) i); } =20 -static inline int xics_max_server_number(void) +static int xics_max_server_number(sPAPRMachineState *spapr) { - return DIV_ROUND_UP(max_cpus * kvmppc_smt_threads(), smp_threads); + return DIV_ROUND_UP(max_cpus * spapr->vsmt, smp_threads); } =20 static void xics_system_init(MachineState *machine, int nr_irqs, Error **e= rrp) @@ -194,7 +194,7 @@ static void xics_system_init(MachineState *machine, int= nr_irqs, Error **errp) if (smc->pre_2_10_has_unused_icps) { int i; =20 - for (i =3D 0; i < xics_max_server_number(); 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. */ @@ -337,7 +337,6 @@ static int spapr_fixup_cpu_dt(void *fdt, sPAPRMachineSt= ate *spapr) int ret =3D 0, offset, cpus_offset; CPUState *cs; char cpu_model[32]; - int smt =3D kvmppc_smt_threads(); uint32_t pft_size_prop[] =3D {0, cpu_to_be32(spapr->htab_shift)}; =20 CPU_FOREACH(cs) { @@ -346,7 +345,7 @@ static int spapr_fixup_cpu_dt(void *fdt, sPAPRMachineSt= ate *spapr) int index =3D spapr_vcpu_id(cpu); int compat_smt =3D MIN(smp_threads, ppc_compat_max_vthreads(cpu)); =20 - if ((index % smt) !=3D 0) { + if (index % spapr->vsmt !=3D 0) { continue; } =20 @@ -614,7 +613,6 @@ static void spapr_populate_cpus_dt_node(void *fdt, sPAP= RMachineState *spapr) CPUState *cs; int cpus_offset; char *nodename; - int smt =3D kvmppc_smt_threads(); =20 cpus_offset =3D fdt_add_subnode(fdt, 0, "cpus"); _FDT(cpus_offset); @@ -632,7 +630,7 @@ static void spapr_populate_cpus_dt_node(void *fdt, sPAP= RMachineState *spapr) DeviceClass *dc =3D DEVICE_GET_CLASS(cs); int offset; =20 - if ((index % smt) !=3D 0) { + if (index % spapr->vsmt !=3D 0) { continue; } =20 @@ -1131,7 +1129,7 @@ static void *spapr_build_fdt(sPAPRMachineState *spapr, _FDT(fdt_setprop_cell(fdt, 0, "#size-cells", 2)); =20 /* /interrupt controller */ - spapr_dt_xics(xics_max_server_number(), fdt, PHANDLE_XICP); + spapr_dt_xics(xics_max_server_number(spapr), fdt, PHANDLE_XICP); =20 ret =3D spapr_populate_memory(spapr, fdt); if (ret < 0) { @@ -2224,7 +2222,6 @@ static void spapr_init_cpus(sPAPRMachineState *spapr) MachineState *machine =3D MACHINE(spapr); MachineClass *mc =3D MACHINE_GET_CLASS(machine); const char *type =3D spapr_get_cpu_core_type(machine->cpu_type); - int smt =3D kvmppc_smt_threads(); const CPUArchIdList *possible_cpus; int boot_cores_nr =3D smp_cpus / smp_threads; int i; @@ -2254,7 +2251,7 @@ static void spapr_init_cpus(sPAPRMachineState *spapr) =20 if (mc->has_hotpluggable_cpus) { spapr_dr_connector_new(OBJECT(spapr), TYPE_SPAPR_DRC_CPU, - (core_id / smp_threads) * smt); + (core_id / smp_threads) * spapr->vsmt); } =20 if (i < boot_cores_nr) { @@ -3281,10 +3278,10 @@ static void spapr_core_unplug_request(HotplugHandler *hotplug_dev, DeviceState *d= ev, Error **errp) { + sPAPRMachineState *spapr =3D SPAPR_MACHINE(OBJECT(hotplug_dev)); int index; sPAPRDRConnector *drc; CPUCore *cc =3D CPU_CORE(dev); - int smt =3D kvmppc_smt_threads(); =20 if (!spapr_find_cpu_slot(MACHINE(hotplug_dev), cc->core_id, &index)) { error_setg(errp, "Unable to find CPU core with core-id: %d", @@ -3296,7 +3293,7 @@ void spapr_core_unplug_request(HotplugHandler *hotplu= g_dev, DeviceState *dev, return; } =20 - drc =3D spapr_drc_by_id(TYPE_SPAPR_DRC_CPU, index * smt); + drc =3D spapr_drc_by_id(TYPE_SPAPR_DRC_CPU, index * spapr->vsmt); g_assert(drc); =20 spapr_drc_detach(drc); @@ -3315,7 +3312,6 @@ static void spapr_core_plug(HotplugHandler *hotplug_d= ev, DeviceState *dev, CPUState *cs =3D CPU(core->threads[0]); sPAPRDRConnector *drc; Error *local_err =3D NULL; - int smt =3D kvmppc_smt_threads(); CPUArchId *core_slot; int index; bool hotplugged =3D spapr_drc_hotplugged(dev); @@ -3326,7 +3322,7 @@ static void spapr_core_plug(HotplugHandler *hotplug_d= ev, DeviceState *dev, cc->core_id); return; } - drc =3D spapr_drc_by_id(TYPE_SPAPR_DRC_CPU, index * smt); + drc =3D spapr_drc_by_id(TYPE_SPAPR_DRC_CPU, index * spapr->vsmt); =20 g_assert(drc || !mc->has_hotpluggable_cpus); =20 From nobody Sat Feb 7 06:12:46 2026 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 1518637763493163.18007030499416; Wed, 14 Feb 2018 11:49:23 -0800 (PST) Received: from localhost ([::1]:53856 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1em33J-0001WN-PH for importer@patchew.org; Wed, 14 Feb 2018 14:49:13 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:52156) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1em2v8-0003Fy-9G for qemu-devel@nongnu.org; Wed, 14 Feb 2018 14:40:51 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1em2v3-0002wu-QX for qemu-devel@nongnu.org; Wed, 14 Feb 2018 14:40:46 -0500 Received: from 9.mo179.mail-out.ovh.net ([46.105.76.148]:59775) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1em2v3-0002wO-Gi for qemu-devel@nongnu.org; Wed, 14 Feb 2018 14:40:41 -0500 Received: from player168.ha.ovh.net (b7.ovh.net [213.186.33.57]) by mo179.mail-out.ovh.net (Postfix) with ESMTP id C7FDBA3544 for ; Wed, 14 Feb 2018 20:40:39 +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 player168.ha.ovh.net (Postfix) with ESMTPA id 8138A42008C; Wed, 14 Feb 2018 20:40:35 +0100 (CET) From: Greg Kurz To: qemu-devel@nongnu.org Date: Wed, 14 Feb 2018 20:40:35 +0100 Message-ID: <151863723541.3003.11101410914900480056.stgit@bahia.lan> In-Reply-To: <151863720814.3003.4939908778788942974.stgit@bahia.lan> References: <151863720814.3003.4939908778788942974.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: 10427803466469710118 X-VR-SPAMSTATE: OK X-VR-SPAMSCORE: -100 X-VR-SPAMCAUSE: gggruggvucftvghtrhhoucdtuddrgedtfedrfedtgdduvdekucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuqfggjfdpvefjgfevmfevgfenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddm X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 46.105.76.148 Subject: [Qemu-devel] [PATCH 2/5] spapr: move VCPU calculation to core machine code 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 , qemu-ppc@nongnu.org, Sam Bobroff , David Gibson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 The VCPU ids are currently computed and assigned to each individual CPU threads in spapr_cpu_core_realize(). But the numbering logic of VCPU ids is actually a machine-level concept, and many places in hw/ppc/spapr.c also have to compute VCPU ids out of CPU indexes. The current formula used in spapr_cpu_core_realize() is: vcpu_id =3D (cc->core_id * spapr->vsmt / smp_threads) + i where: cc->core_id is a multiple of smp_threads cpu_index =3D cc->core_id + i 0 <=3D i < smp_threads So we have: cpu_index % smp_threads =3D=3D i cc->core_id / smp_threads =3D=3D cpu_index / smp_threads hence: vcpu_id =3D (cpu_index / smp_threads) * spapr->vsmt + cpu_index % smp_threads; This formula was used before VSMT at the time VCPU ids where computed at the target emulation level. It has the advantage of being useable to derive a VPCU id out of a CPU index only. It is fitted for all the places where the machine code has to compute a VCPU id. This patch introduces an accessor to set the VCPU id in a PowerPCCPU object using the above formula. It is a first step to consolidate all the VCPU id logic in a single place. Signed-off-by: Greg Kurz --- hw/ppc/spapr.c | 19 +++++++++++++++++++ hw/ppc/spapr_cpu_core.c | 9 ++------- include/hw/ppc/spapr.h | 1 + 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/hw/ppc/spapr.c b/hw/ppc/spapr.c index ea7429c92a97..30cc48fd5264 100644 --- a/hw/ppc/spapr.c +++ b/hw/ppc/spapr.c @@ -3802,6 +3802,25 @@ int spapr_vcpu_id(PowerPCCPU *cpu) } } =20 +void spapr_set_vcpu_id(PowerPCCPU *cpu, int cpu_index, Error **errp) +{ + sPAPRMachineState *spapr =3D SPAPR_MACHINE(qdev_get_machine()); + int vcpu_id; + + vcpu_id =3D + (cpu_index / smp_threads) * spapr->vsmt + cpu_index % smp_threads; + + if (kvm_enabled() && !kvm_vcpu_id_is_valid(vcpu_id)) { + error_setg(errp, "Can't create CPU with id %d in KVM", vcpu_id); + error_append_hint(errp, "Adjust the number of cpus to %d " + "or try to raise the number of threads per core\= n", + vcpu_id * smp_threads / spapr->vsmt); + return; + } + + cpu->vcpu_id =3D vcpu_id; +} + PowerPCCPU *spapr_find_cpu(int vcpu_id) { CPUState *cs; diff --git a/hw/ppc/spapr_cpu_core.c b/hw/ppc/spapr_cpu_core.c index 590d167b04c8..94afeb399e99 100644 --- a/hw/ppc/spapr_cpu_core.c +++ b/hw/ppc/spapr_cpu_core.c @@ -172,13 +172,8 @@ static void spapr_cpu_core_realize(DeviceState *dev, E= rror **errp) cs =3D CPU(obj); cpu =3D sc->threads[i] =3D POWERPC_CPU(obj); cs->cpu_index =3D cc->core_id + i; - cpu->vcpu_id =3D (cc->core_id * spapr->vsmt / smp_threads) + i; - if (kvm_enabled() && !kvm_vcpu_id_is_valid(cpu->vcpu_id)) { - error_setg(&local_err, "Can't create CPU with id %d in KVM", - cpu->vcpu_id); - error_append_hint(&local_err, "Adjust the number of cpus to %d= " - "or try to raise the number of threads per c= ore\n", - cpu->vcpu_id * smp_threads / spapr->vsmt); + spapr_set_vcpu_id(cpu, cs->cpu_index, &local_err); + if (local_err) { goto err; } =20 diff --git a/include/hw/ppc/spapr.h b/include/hw/ppc/spapr.h index 62c077ac2037..af19320d2f8a 100644 --- a/include/hw/ppc/spapr.h +++ b/include/hw/ppc/spapr.h @@ -767,6 +767,7 @@ void spapr_do_system_reset_on_cpu(CPUState *cs, run_on_= cpu_data arg); #define HTAB_SIZE(spapr) (1ULL << ((spapr)->htab_shift)) =20 int spapr_vcpu_id(PowerPCCPU *cpu); +void spapr_set_vcpu_id(PowerPCCPU *cpu, int cpu_index, Error **errp); PowerPCCPU *spapr_find_cpu(int vcpu_id); =20 int spapr_irq_alloc(sPAPRMachineState *spapr, int irq_hint, bool lsi, From nobody Sat Feb 7 06:12:46 2026 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 1518637878377774.6954791478083; Wed, 14 Feb 2018 11:51:18 -0800 (PST) Received: from localhost ([::1]:53981 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1em35J-0003Lz-JM for importer@patchew.org; Wed, 14 Feb 2018 14:51:17 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:52193) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1em2vD-0003Kk-Eq for qemu-devel@nongnu.org; Wed, 14 Feb 2018 14:40:52 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1em2vC-0002zl-J3 for qemu-devel@nongnu.org; Wed, 14 Feb 2018 14:40:51 -0500 Received: from 4.mo179.mail-out.ovh.net ([46.105.36.149]:55681) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1em2vC-0002zO-BC for qemu-devel@nongnu.org; Wed, 14 Feb 2018 14:40:50 -0500 Received: from player168.ha.ovh.net (b7.ovh.net [213.186.33.57]) by mo179.mail-out.ovh.net (Postfix) with ESMTP id 20F2AA38BC for ; Wed, 14 Feb 2018 20:40:49 +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 player168.ha.ovh.net (Postfix) with ESMTPA id BA01842007E; Wed, 14 Feb 2018 20:40:44 +0100 (CET) From: Greg Kurz To: qemu-devel@nongnu.org Date: Wed, 14 Feb 2018 20:40:44 +0100 Message-ID: <151863724465.3003.14485203815672867421.stgit@bahia.lan> In-Reply-To: <151863720814.3003.4939908778788942974.stgit@bahia.lan> References: <151863720814.3003.4939908778788942974.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: 10430336738540165414 X-VR-SPAMSTATE: OK X-VR-SPAMSCORE: -100 X-VR-SPAMCAUSE: gggruggvucftvghtrhhoucdtuddrgedtfedrfedtgdduvdekucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuqfggjfdpvefjgfevmfevgfenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddm X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 46.105.36.149 Subject: [Qemu-devel] [PATCH 3/5] spapr: rename spapr_vcpu_id() to spapr_get_vcpu_id() 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 , qemu-ppc@nongnu.org, Sam Bobroff , David Gibson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 The spapr_vcpu_id() function is an accessor actually. Let's rename it for symmetry with the recently added spapr_set_vcpu_id() helper. The motivation behind this is that a later patch will consolidate the VCPU id formula in a function and spapr_vcpu_id looks like an appropriate name. Signed-off-by: Greg Kurz --- hw/ppc/spapr.c | 16 ++++++++-------- include/hw/ppc/spapr.h | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/hw/ppc/spapr.c b/hw/ppc/spapr.c index 30cc48fd5264..18ebc058acdd 100644 --- a/hw/ppc/spapr.c +++ b/hw/ppc/spapr.c @@ -209,7 +209,7 @@ static int spapr_fixup_cpu_smt_dt(void *fdt, int offset= , PowerPCCPU *cpu, int i, ret =3D 0; uint32_t servers_prop[smt_threads]; uint32_t gservers_prop[smt_threads * 2]; - int index =3D spapr_vcpu_id(cpu); + int index =3D spapr_get_vcpu_id(cpu); =20 if (cpu->compat_pvr) { ret =3D fdt_setprop_cell(fdt, offset, "cpu-version", cpu->compat_p= vr); @@ -238,7 +238,7 @@ static int spapr_fixup_cpu_smt_dt(void *fdt, int offset= , PowerPCCPU *cpu, =20 static int spapr_fixup_cpu_numa_dt(void *fdt, int offset, PowerPCCPU *cpu) { - int index =3D spapr_vcpu_id(cpu); + int index =3D spapr_get_vcpu_id(cpu); uint32_t associativity[] =3D {cpu_to_be32(0x5), cpu_to_be32(0x0), cpu_to_be32(0x0), @@ -342,7 +342,7 @@ static int spapr_fixup_cpu_dt(void *fdt, sPAPRMachineSt= ate *spapr) CPU_FOREACH(cs) { PowerPCCPU *cpu =3D POWERPC_CPU(cs); DeviceClass *dc =3D DEVICE_GET_CLASS(cs); - int index =3D spapr_vcpu_id(cpu); + int index =3D spapr_get_vcpu_id(cpu); int compat_smt =3D MIN(smp_threads, ppc_compat_max_vthreads(cpu)); =20 if (index % spapr->vsmt !=3D 0) { @@ -492,7 +492,7 @@ static void spapr_populate_cpu_dt(CPUState *cs, void *f= dt, int offset, PowerPCCPU *cpu =3D POWERPC_CPU(cs); CPUPPCState *env =3D &cpu->env; PowerPCCPUClass *pcc =3D POWERPC_CPU_GET_CLASS(cs); - int index =3D spapr_vcpu_id(cpu); + int index =3D spapr_get_vcpu_id(cpu); uint32_t segs[] =3D {cpu_to_be32(28), cpu_to_be32(40), 0xffffffff, 0xffffffff}; uint32_t tbfreq =3D kvm_enabled() ? kvmppc_get_tbfreq() @@ -626,7 +626,7 @@ static void spapr_populate_cpus_dt_node(void *fdt, sPAP= RMachineState *spapr) */ CPU_FOREACH_REVERSE(cs) { PowerPCCPU *cpu =3D POWERPC_CPU(cs); - int index =3D spapr_vcpu_id(cpu); + int index =3D spapr_get_vcpu_id(cpu); DeviceClass *dc =3D DEVICE_GET_CLASS(cs); int offset; =20 @@ -3234,7 +3234,7 @@ static void *spapr_populate_hotplug_cpu_dt(CPUState *= cs, int *fdt_offset, { PowerPCCPU *cpu =3D POWERPC_CPU(cs); DeviceClass *dc =3D DEVICE_GET_CLASS(cs); - int id =3D spapr_vcpu_id(cpu); + int id =3D spapr_get_vcpu_id(cpu); void *fdt; int offset, fdt_size; char *nodename; @@ -3791,7 +3791,7 @@ static void spapr_pic_print_info(InterruptStatsProvid= er *obj, ics_pic_print_info(spapr->ics, mon); } =20 -int spapr_vcpu_id(PowerPCCPU *cpu) +int spapr_get_vcpu_id(PowerPCCPU *cpu) { CPUState *cs =3D CPU(cpu); =20 @@ -3828,7 +3828,7 @@ PowerPCCPU *spapr_find_cpu(int vcpu_id) CPU_FOREACH(cs) { PowerPCCPU *cpu =3D POWERPC_CPU(cs); =20 - if (spapr_vcpu_id(cpu) =3D=3D vcpu_id) { + if (spapr_get_vcpu_id(cpu) =3D=3D vcpu_id) { return cpu; } } diff --git a/include/hw/ppc/spapr.h b/include/hw/ppc/spapr.h index af19320d2f8a..36942b378daa 100644 --- a/include/hw/ppc/spapr.h +++ b/include/hw/ppc/spapr.h @@ -766,7 +766,7 @@ void spapr_do_system_reset_on_cpu(CPUState *cs, run_on_= cpu_data arg); =20 #define HTAB_SIZE(spapr) (1ULL << ((spapr)->htab_shift)) =20 -int spapr_vcpu_id(PowerPCCPU *cpu); +int spapr_get_vcpu_id(PowerPCCPU *cpu); void spapr_set_vcpu_id(PowerPCCPU *cpu, int cpu_index, Error **errp); PowerPCCPU *spapr_find_cpu(int vcpu_id); =20 From nobody Sat Feb 7 06:12:46 2026 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 1518637409693843.4910523528947; Wed, 14 Feb 2018 11:43:29 -0800 (PST) Received: from localhost ([::1]:53319 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1em2xk-0004wp-MC for importer@patchew.org; Wed, 14 Feb 2018 14:43:28 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:52285) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1em2vQ-0003Wt-DV for qemu-devel@nongnu.org; Wed, 14 Feb 2018 14:41:05 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1em2vL-00033Z-Us for qemu-devel@nongnu.org; Wed, 14 Feb 2018 14:41:04 -0500 Received: from 6.mo179.mail-out.ovh.net ([46.105.56.76]:37798) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1em2vL-00032g-Ne for qemu-devel@nongnu.org; Wed, 14 Feb 2018 14:40:59 -0500 Received: from player168.ha.ovh.net (b7.ovh.net [213.186.33.57]) by mo179.mail-out.ovh.net (Postfix) with ESMTP id 33249A3A06 for ; Wed, 14 Feb 2018 20:40:58 +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 player168.ha.ovh.net (Postfix) with ESMTPA id F2BCE42007E; Wed, 14 Feb 2018 20:40:53 +0100 (CET) From: Greg Kurz To: qemu-devel@nongnu.org Date: Wed, 14 Feb 2018 20:40:53 +0100 Message-ID: <151863725388.3003.620735626834741475.stgit@bahia.lan> In-Reply-To: <151863720814.3003.4939908778788942974.stgit@bahia.lan> References: <151863720814.3003.4939908778788942974.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: 10433151490729548070 X-VR-SPAMSTATE: OK X-VR-SPAMSCORE: -100 X-VR-SPAMCAUSE: gggruggvucftvghtrhhoucdtuddrgedtfedrfedtgdduvdekucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuqfggjfdpvefjgfevmfevgfenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddm X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 46.105.56.76 Subject: [Qemu-devel] [PATCH 4/5] spapr: consolidate the VCPU id numbering logic in a single place 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 , qemu-ppc@nongnu.org, Sam Bobroff , David Gibson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Several places in the code need to calculate a VCPU id: (cpu_index / smp_threads) * spapr->vsmt + cpu_index % smp_threads (core_id / smp_threads) * spapr->vsmt (1 user) index * spapr->vsmt (2 users) or guess that the VCPU id of a given VCPU is the first thread of a virtual core: index % spapr->vsmt !=3D 0 Even if the numbering logic isn't that complex, it is rather fragile to have these assumptions open-coded in several places. FWIW this was proved with recent issues related to VSMT. This patch moves the VCPU id formula to a single function to be called everywhere the code needs to compute one. It also adds an helper to guess if a VCPU is the first thread of a VCORE. Signed-off-by: Greg Kurz --- hw/ppc/spapr.c | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/hw/ppc/spapr.c b/hw/ppc/spapr.c index 18ebc058acdd..800d3f001253 100644 --- a/hw/ppc/spapr.c +++ b/hw/ppc/spapr.c @@ -99,6 +99,20 @@ =20 #define PHANDLE_XICP 0x00001111 =20 +/* These two functions implement the VCPU id numbering: one to compute them + * all and one to identify thread 0 of a VCORE. Any change to the first one + * is likely to have an impact on the second one, so let's keep them close. + */ +static int spapr_vcpu_id(sPAPRMachineState *spapr, int cpu_index) +{ + return + (cpu_index / smp_threads) * spapr->vsmt + cpu_index % smp_threads; +} +static bool spapr_is_vcore(sPAPRMachineState *spapr, PowerPCCPU *cpu) +{ + return spapr_get_vcpu_id(cpu) % spapr->vsmt =3D=3D 0; +} + static ICSState *spapr_ics_create(sPAPRMachineState *spapr, const char *type_ics, int nr_irqs, Error **errp) @@ -345,7 +359,7 @@ static int spapr_fixup_cpu_dt(void *fdt, sPAPRMachineSt= ate *spapr) int index =3D spapr_get_vcpu_id(cpu); int compat_smt =3D MIN(smp_threads, ppc_compat_max_vthreads(cpu)); =20 - if (index % spapr->vsmt !=3D 0) { + if (!spapr_is_vcore(spapr, cpu)) { continue; } =20 @@ -630,7 +644,7 @@ static void spapr_populate_cpus_dt_node(void *fdt, sPAP= RMachineState *spapr) DeviceClass *dc =3D DEVICE_GET_CLASS(cs); int offset; =20 - if (index % spapr->vsmt !=3D 0) { + if (!spapr_is_vcore(spapr, cpu)) { continue; } =20 @@ -2251,7 +2265,7 @@ static void spapr_init_cpus(sPAPRMachineState *spapr) =20 if (mc->has_hotpluggable_cpus) { spapr_dr_connector_new(OBJECT(spapr), TYPE_SPAPR_DRC_CPU, - (core_id / smp_threads) * spapr->vsmt); + spapr_vcpu_id(spapr, core_id)); } =20 if (i < boot_cores_nr) { @@ -3293,7 +3307,8 @@ void spapr_core_unplug_request(HotplugHandler *hotplu= g_dev, DeviceState *dev, return; } =20 - drc =3D spapr_drc_by_id(TYPE_SPAPR_DRC_CPU, index * spapr->vsmt); + drc =3D spapr_drc_by_id(TYPE_SPAPR_DRC_CPU, + spapr_vcpu_id(spapr, cc->core_id)); g_assert(drc); =20 spapr_drc_detach(drc); @@ -3322,7 +3337,8 @@ static void spapr_core_plug(HotplugHandler *hotplug_d= ev, DeviceState *dev, cc->core_id); return; } - drc =3D spapr_drc_by_id(TYPE_SPAPR_DRC_CPU, index * spapr->vsmt); + drc =3D spapr_drc_by_id(TYPE_SPAPR_DRC_CPU, + spapr_vcpu_id(spapr, cc->core_id)); =20 g_assert(drc || !mc->has_hotpluggable_cpus); =20 @@ -3807,8 +3823,7 @@ void spapr_set_vcpu_id(PowerPCCPU *cpu, int cpu_index= , Error **errp) sPAPRMachineState *spapr =3D SPAPR_MACHINE(qdev_get_machine()); int vcpu_id; =20 - vcpu_id =3D - (cpu_index / smp_threads) * spapr->vsmt + cpu_index % smp_threads; + vcpu_id =3D spapr_vcpu_id(spapr, cpu_index); =20 if (kvm_enabled() && !kvm_vcpu_id_is_valid(vcpu_id)) { error_setg(errp, "Can't create CPU with id %d in KVM", vcpu_id); From nobody Sat Feb 7 06:12:46 2026 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 1518637452613765.5313960415696; Wed, 14 Feb 2018 11:44:12 -0800 (PST) Received: from localhost ([::1]:53320 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1em2yN-0005Y2-Db for importer@patchew.org; Wed, 14 Feb 2018 14:44:07 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:52346) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1em2vb-0003gr-7y for qemu-devel@nongnu.org; Wed, 14 Feb 2018 14:41:18 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1em2vV-00036g-Cb for qemu-devel@nongnu.org; Wed, 14 Feb 2018 14:41:15 -0500 Received: from 2.mo179.mail-out.ovh.net ([178.33.250.45]:44638) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1em2vV-00035q-5w for qemu-devel@nongnu.org; Wed, 14 Feb 2018 14:41:09 -0500 Received: from player168.ha.ovh.net (b7.ovh.net [213.186.33.57]) by mo179.mail-out.ovh.net (Postfix) with ESMTP id 80B8FA3FFA for ; Wed, 14 Feb 2018 20:41:07 +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 player168.ha.ovh.net (Postfix) with ESMTPA id 38EFF420084; Wed, 14 Feb 2018 20:41:03 +0100 (CET) From: Greg Kurz To: qemu-devel@nongnu.org Date: Wed, 14 Feb 2018 20:41:03 +0100 Message-ID: <151863726311.3003.8227524786940828598.stgit@bahia.lan> In-Reply-To: <151863720814.3003.4939908778788942974.stgit@bahia.lan> References: <151863720814.3003.4939908778788942974.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: 10435684762792597798 X-VR-SPAMSTATE: OK X-VR-SPAMSCORE: -100 X-VR-SPAMCAUSE: gggruggvucftvghtrhhoucdtuddrgedtfedrfedtgdduvdekucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuqfggjfdpvefjgfevmfevgfenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddm X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 178.33.250.45 Subject: [Qemu-devel] [PATCH 5/5] spapr: drop DIV_ROUND_UP() from xics_max_server_number() 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 , qemu-ppc@nongnu.org, Sam Bobroff , David Gibson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 XICS needs to know the highest VCPU id that may be presented to the guest plus 1. Commit f303f117fec3 "spapr: ensure we have at least one XICS server" changed how the maximum is computed from: smp_cpus * kvmppc_smt_threads() / smp_threads to: DIV_ROUND_UP(smp_cpus * kvmppc_smt_threads(), smp_threads) This was done because at the time we could pass broken CPU topologies to the -smp command line options, such as threads=3D9,cpus=3D1. On a POWER8 host this would give: 1 * 8 / 9 =3D=3D 0 servers and cause QEMU to crash later during XICS setup. The formulat evolved a bit to accomodate CPU hot-plug and VSMT, but most important, stricter checks are performed on the CPU topology. With -smp threads=3D9,cpus=3D1: qemu-system-ppc64: cpu topology: sockets (1) * cores (1) * threads (9) > maxcpus (1) With -smp threads=3D9,maxcpus=3D1: qemu-system-ppc64: maxcpus must be equal to or greater than smp More generally, machine types with hotplug support (2.7 and up), no longer allow to set maxcpus or smp_cpus to a value that isnt't a multiple of smp_threads. With -smp threads=3D4,cpus=3D6: qemu-system-ppc64: smp_cpus (6) must be multiple of threads (4) With -smp threads=3D4,maxcpus=3D6: qemu-system-ppc64: max_cpus (6) must be multiple of threads (4) This means that the division is perfect and we don't need DIV_ROUND_UP(), and we could do a regular division: max_cpus * spapr->vsmt / smp_threads So this patch changes xics_max_server_number() to use the spapr_vcpu_id(), which works too since max_cpus is a multiple of smp_threads: (max_cpus / smp_threads ) * spapr->vsmt + max_cpus % smp_threads It breaks migration of pre-2.7 machine types with unusual CPU topologies, but I guess this is an acceptable trade-off. Signed-off-by: Greg Kurz --- hw/ppc/spapr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hw/ppc/spapr.c b/hw/ppc/spapr.c index 800d3f001253..f1722214cc74 100644 --- a/hw/ppc/spapr.c +++ b/hw/ppc/spapr.c @@ -176,7 +176,7 @@ static void pre_2_10_vmstate_unregister_dummy_icp(int i) =20 static int xics_max_server_number(sPAPRMachineState *spapr) { - return DIV_ROUND_UP(max_cpus * spapr->vsmt, smp_threads); + return spapr_vcpu_id(spapr, max_cpus) } =20 static void xics_system_init(MachineState *machine, int nr_irqs, Error **e= rrp)