From nobody Fri Oct 24 20:18:49 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 1518776323977301.5757963392149; Fri, 16 Feb 2018 02:18:43 -0800 (PST) Received: from localhost ([::1]:59153 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1emd6I-0003FR-S3 for importer@patchew.org; Fri, 16 Feb 2018 05:18:42 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:35520) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1emcuY-0001g6-HX for qemu-devel@nongnu.org; Fri, 16 Feb 2018 05:06:36 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1emcuT-0005DT-4T for qemu-devel@nongnu.org; Fri, 16 Feb 2018 05:06:34 -0500 Received: from ozlabs.org ([2401:3900:2:1::2]:53047) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1emcuS-00057v-OB; Fri, 16 Feb 2018 05:06:29 -0500 Received: by ozlabs.org (Postfix, from userid 1007) id 3zjTLl0khgz9t6k; Fri, 16 Feb 2018 21:06:23 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gibson.dropbear.id.au; s=201602; t=1518775583; bh=1+SZT/Mabl9a5O7IWh35YpOorEpUK77a+lFLlGc1Zyg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PohOakDtkLqKYhOe/cFxbQDD3NlDJhz0DPCr2BGDn3qDhJJwqPKvNR2xLtIRna/kJ e14spgfxrIj9HBdyHLpZZonc7zFIGSjhzh03M0C4ZylRlzmDPlSTuTj8uXotuzfGv+ d6bICPyKJIxw3kgw6PGLupMFUurGVH4Ph80nGsQ8= From: David Gibson To: peter.maydell@linaro.org Date: Fri, 16 Feb 2018 21:06:13 +1100 Message-Id: <20180216100617.25265-11-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180216100617.25265-1-david@gibson.dropbear.id.au> References: <20180216100617.25265-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 10/14] 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: lvivier@redhat.com, surajjs@au1.ibm.com, qemu-devel@nongnu.org, groug@kaod.org, qemu-ppc@nongnu.org, David Gibson 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 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 [dwg: Rename spapr_is_vcore() to spapr_is_thread0_in_vcore() for clarity] Signed-off-by: David Gibson --- hw/ppc/spapr.c | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/hw/ppc/spapr.c b/hw/ppc/spapr.c index 18ebc058ac..83c9d66dd5 100644 --- a/hw/ppc/spapr.c +++ b/hw/ppc/spapr.c @@ -99,6 +99,21 @@ =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_thread0_in_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 +360,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_thread0_in_vcore(spapr, cpu)) { continue; } =20 @@ -630,7 +645,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_thread0_in_vcore(spapr, cpu)) { continue; } =20 @@ -2251,7 +2266,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 +3308,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 +3338,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 +3824,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); --=20 2.14.3